Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(773)

Side by Side Diff: ui/gfx/render_text_harfbuzz.cc

Issue 484883003: Re-land: RenderTextHarfBuzz: Set font render parameters in font data functions (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: disable subpixel positioning on win Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/gfx/render_text_harfbuzz.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/gfx/render_text_harfbuzz.h" 5 #include "ui/gfx/render_text_harfbuzz.h"
6 6
7 #include <map> 7 #include <map>
8 8
9 #include "base/i18n/bidi_line_iterator.h" 9 #include "base/i18n/bidi_line_iterator.h"
10 #include "base/i18n/break_iterator.h" 10 #include "base/i18n/break_iterator.h"
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 261
262 hb_face_t* get() { 262 hb_face_t* get() {
263 return face_; 263 return face_;
264 } 264 }
265 265
266 private: 266 private:
267 hb_face_t* face_; 267 hb_face_t* face_;
268 }; 268 };
269 269
270 // Creates a HarfBuzz font from the given Skia face and text size. 270 // Creates a HarfBuzz font from the given Skia face and text size.
271 hb_font_t* CreateHarfBuzzFont(SkTypeface* skia_face, int text_size) { 271 hb_font_t* CreateHarfBuzzFont(SkTypeface* skia_face,
272 int text_size,
273 const FontRenderParams& params,
274 bool background_is_transparent) {
272 typedef std::pair<HarfBuzzFace, GlyphCache> FaceCache; 275 typedef std::pair<HarfBuzzFace, GlyphCache> FaceCache;
273 276
274 // TODO(ckocagil): This shouldn't grow indefinitely. Maybe use base::MRUCache? 277 // TODO(ckocagil): This shouldn't grow indefinitely. Maybe use base::MRUCache?
275 static std::map<SkFontID, FaceCache> face_caches; 278 static std::map<SkFontID, FaceCache> face_caches;
276 279
277 FaceCache* face_cache = &face_caches[skia_face->uniqueID()]; 280 FaceCache* face_cache = &face_caches[skia_face->uniqueID()];
278 if (face_cache->first.get() == NULL) 281 if (face_cache->first.get() == NULL)
279 face_cache->first.Init(skia_face); 282 face_cache->first.Init(skia_face);
280 283
281 hb_font_t* harfbuzz_font = hb_font_create(face_cache->first.get()); 284 hb_font_t* harfbuzz_font = hb_font_create(face_cache->first.get());
282 const int scale = SkScalarToFixed(text_size); 285 const int scale = SkScalarToFixed(text_size);
283 hb_font_set_scale(harfbuzz_font, scale, scale); 286 hb_font_set_scale(harfbuzz_font, scale, scale);
284 FontData* hb_font_data = new FontData(&face_cache->second); 287 FontData* hb_font_data = new FontData(&face_cache->second);
285 hb_font_data->paint_.setTypeface(skia_face); 288 hb_font_data->paint_.setTypeface(skia_face);
286 hb_font_data->paint_.setTextSize(text_size); 289 hb_font_data->paint_.setTextSize(text_size);
290 // TODO(ckocagil): Do we need to update these params later?
291 internal::ApplyRenderParams(params, background_is_transparent,
292 &hb_font_data->paint_);
287 hb_font_set_funcs(harfbuzz_font, g_font_funcs.Get().get(), hb_font_data, 293 hb_font_set_funcs(harfbuzz_font, g_font_funcs.Get().get(), hb_font_data,
288 DeleteByType<FontData>); 294 DeleteByType<FontData>);
289 hb_font_make_immutable(harfbuzz_font); 295 hb_font_make_immutable(harfbuzz_font);
290 return harfbuzz_font; 296 return harfbuzz_font;
291 } 297 }
292 298
293 // Returns true if characters of |block_code| may trigger font fallback. 299 // Returns true if characters of |block_code| may trigger font fallback.
294 bool IsUnusualBlockCode(UBlockCode block_code) { 300 bool IsUnusualBlockCode(UBlockCode block_code) {
295 return block_code == UBLOCK_GEOMETRIC_SHAPES || 301 return block_code == UBLOCK_GEOMETRIC_SHAPES ||
296 block_code == UBLOCK_MISCELLANEOUS_SYMBOLS; 302 block_code == UBLOCK_MISCELLANEOUS_SYMBOLS;
(...skipping 566 matching lines...) Expand 10 before | Expand all | Expand 10 after
863 869
864 set_lines(&lines); 870 set_lines(&lines);
865 } 871 }
866 } 872 }
867 873
868 void RenderTextHarfBuzz::DrawVisualText(Canvas* canvas) { 874 void RenderTextHarfBuzz::DrawVisualText(Canvas* canvas) {
869 DCHECK(!needs_layout_); 875 DCHECK(!needs_layout_);
870 internal::SkiaTextRenderer renderer(canvas); 876 internal::SkiaTextRenderer renderer(canvas);
871 ApplyFadeEffects(&renderer); 877 ApplyFadeEffects(&renderer);
872 ApplyTextShadows(&renderer); 878 ApplyTextShadows(&renderer);
873
874 #if defined(OS_WIN) || defined(OS_LINUX)
875 renderer.SetFontRenderParams(
876 font_list().GetPrimaryFont().GetFontRenderParams(),
877 background_is_transparent());
878 #endif
879
880 ApplyCompositionAndSelectionStyles(); 879 ApplyCompositionAndSelectionStyles();
881 880
882 int current_x = 0; 881 int current_x = 0;
883 const Vector2d line_offset = GetLineOffset(0); 882 const Vector2d line_offset = GetLineOffset(0);
884 for (size_t i = 0; i < runs_.size(); ++i) { 883 for (size_t i = 0; i < runs_.size(); ++i) {
885 const internal::TextRunHarfBuzz& run = *runs_[visual_to_logical_[i]]; 884 const internal::TextRunHarfBuzz& run = *runs_[visual_to_logical_[i]];
886 renderer.SetTypeface(run.skia_face.get()); 885 renderer.SetTypeface(run.skia_face.get());
887 renderer.SetTextSize(run.font_size); 886 renderer.SetTextSize(run.font_size);
887 renderer.SetFontRenderParams(run.render_params,
888 background_is_transparent());
888 889
889 Vector2d origin = line_offset + Vector2d(current_x, lines()[0].baseline); 890 Vector2d origin = line_offset + Vector2d(current_x, lines()[0].baseline);
890 scoped_ptr<SkPoint[]> positions(new SkPoint[run.glyph_count]); 891 scoped_ptr<SkPoint[]> positions(new SkPoint[run.glyph_count]);
891 for (size_t j = 0; j < run.glyph_count; ++j) { 892 for (size_t j = 0; j < run.glyph_count; ++j) {
892 positions[j] = run.positions[j]; 893 positions[j] = run.positions[j];
893 positions[j].offset(SkIntToScalar(origin.x()), SkIntToScalar(origin.y())); 894 positions[j].offset(SkIntToScalar(origin.x()), SkIntToScalar(origin.y()));
894 } 895 }
895 896
896 for (BreakList<SkColor>::const_iterator it = 897 for (BreakList<SkColor>::const_iterator it =
897 colors().GetBreak(run.range.start()); 898 colors().GetBreak(run.range.start());
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
1105 } 1106 }
1106 1107
1107 bool RenderTextHarfBuzz::ShapeRunWithFont(internal::TextRunHarfBuzz* run, 1108 bool RenderTextHarfBuzz::ShapeRunWithFont(internal::TextRunHarfBuzz* run,
1108 const std::string& font_family) { 1109 const std::string& font_family) {
1109 const base::string16& text = GetLayoutText(); 1110 const base::string16& text = GetLayoutText();
1110 skia::RefPtr<SkTypeface> skia_face = 1111 skia::RefPtr<SkTypeface> skia_face =
1111 internal::CreateSkiaTypeface(font_family, run->font_style); 1112 internal::CreateSkiaTypeface(font_family, run->font_style);
1112 if (skia_face == NULL) 1113 if (skia_face == NULL)
1113 return false; 1114 return false;
1114 run->skia_face = skia_face; 1115 run->skia_face = skia_face;
1116 FontRenderParamsQuery query(false);
1117 query.families.push_back(font_family);
1118 query.pixel_size = run->font_size;
1119 query.style = run->font_style;
1120 run->render_params = GetFontRenderParams(query, NULL);
1115 hb_font_t* harfbuzz_font = CreateHarfBuzzFont(run->skia_face.get(), 1121 hb_font_t* harfbuzz_font = CreateHarfBuzzFont(run->skia_face.get(),
1116 run->font_size); 1122 run->font_size, run->render_params, background_is_transparent());
1117 1123
1118 // Create a HarfBuzz buffer and add the string to be shaped. The HarfBuzz 1124 // Create a HarfBuzz buffer and add the string to be shaped. The HarfBuzz
1119 // buffer holds our text, run information to be used by the shaping engine, 1125 // buffer holds our text, run information to be used by the shaping engine,
1120 // and the resulting glyph data. 1126 // and the resulting glyph data.
1121 hb_buffer_t* buffer = hb_buffer_create(); 1127 hb_buffer_t* buffer = hb_buffer_create();
1122 hb_buffer_add_utf16(buffer, reinterpret_cast<const uint16*>(text.c_str()), 1128 hb_buffer_add_utf16(buffer, reinterpret_cast<const uint16*>(text.c_str()),
1123 text.length(), run->range.start(), run->range.length()); 1129 text.length(), run->range.start(), run->range.length());
1124 hb_buffer_set_script(buffer, ICUScriptToHBScript(run->script)); 1130 hb_buffer_set_script(buffer, ICUScriptToHBScript(run->script));
1125 hb_buffer_set_direction(buffer, 1131 hb_buffer_set_direction(buffer,
1126 run->is_rtl ? HB_DIRECTION_RTL : HB_DIRECTION_LTR); 1132 run->is_rtl ? HB_DIRECTION_RTL : HB_DIRECTION_LTR);
(...skipping 13 matching lines...) Expand all
1140 run->glyph_to_char.resize(run->glyph_count); 1146 run->glyph_to_char.resize(run->glyph_count);
1141 run->positions.reset(new SkPoint[run->glyph_count]); 1147 run->positions.reset(new SkPoint[run->glyph_count]);
1142 run->width = 0.0f; 1148 run->width = 0.0f;
1143 for (size_t i = 0; i < run->glyph_count; ++i) { 1149 for (size_t i = 0; i < run->glyph_count; ++i) {
1144 run->glyphs[i] = infos[i].codepoint; 1150 run->glyphs[i] = infos[i].codepoint;
1145 run->glyph_to_char[i] = infos[i].cluster; 1151 run->glyph_to_char[i] = infos[i].cluster;
1146 const int x_offset = SkFixedToScalar(hb_positions[i].x_offset); 1152 const int x_offset = SkFixedToScalar(hb_positions[i].x_offset);
1147 const int y_offset = SkFixedToScalar(hb_positions[i].y_offset); 1153 const int y_offset = SkFixedToScalar(hb_positions[i].y_offset);
1148 run->positions[i].set(run->width + x_offset, -y_offset); 1154 run->positions[i].set(run->width + x_offset, -y_offset);
1149 run->width += SkFixedToScalar(hb_positions[i].x_advance); 1155 run->width += SkFixedToScalar(hb_positions[i].x_advance);
1156 #if defined(OS_LINUX)
1157 // Match Pango's glyph rounding logic on Linux.
1158 run->width = std::floor(run->width + 0.5f);
msw 2014/08/22 21:59:36 I wonder if this should still be contingent on if
ckocagil 2014/08/22 22:22:40 Done. Subpixel positioning on Linux seems to be wo
1159 #endif
1150 } 1160 }
1151 1161
1152 hb_buffer_destroy(buffer); 1162 hb_buffer_destroy(buffer);
1153 hb_font_destroy(harfbuzz_font); 1163 hb_font_destroy(harfbuzz_font);
1154 return true; 1164 return true;
1155 } 1165 }
1156 1166
1157 } // namespace gfx 1167 } // namespace gfx
OLDNEW
« no previous file with comments | « ui/gfx/render_text_harfbuzz.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698