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

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

Issue 480533002: RenderTextHarfBuzz: Set font render parameters in font data functions (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: comments 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) {
272 typedef std::pair<HarfBuzzFace, GlyphCache> FaceCache; 274 typedef std::pair<HarfBuzzFace, GlyphCache> FaceCache;
273 275
274 // TODO(ckocagil): This shouldn't grow indefinitely. Maybe use base::MRUCache? 276 // TODO(ckocagil): This shouldn't grow indefinitely. Maybe use base::MRUCache?
275 static std::map<SkFontID, FaceCache> face_caches; 277 static std::map<SkFontID, FaceCache> face_caches;
276 278
277 FaceCache* face_cache = &face_caches[skia_face->uniqueID()]; 279 FaceCache* face_cache = &face_caches[skia_face->uniqueID()];
278 if (face_cache->first.get() == NULL) 280 if (face_cache->first.get() == NULL)
279 face_cache->first.Init(skia_face); 281 face_cache->first.Init(skia_face);
280 282
281 hb_font_t* harfbuzz_font = hb_font_create(face_cache->first.get()); 283 hb_font_t* harfbuzz_font = hb_font_create(face_cache->first.get());
282 const int scale = SkScalarToFixed(text_size); 284 const int scale = SkScalarToFixed(text_size);
283 hb_font_set_scale(harfbuzz_font, scale, scale); 285 hb_font_set_scale(harfbuzz_font, scale, scale);
284 FontData* hb_font_data = new FontData(&face_cache->second); 286 FontData* hb_font_data = new FontData(&face_cache->second);
285 hb_font_data->paint_.setTypeface(skia_face); 287 hb_font_data->paint_.setTypeface(skia_face);
286 hb_font_data->paint_.setTextSize(text_size); 288 hb_font_data->paint_.setTextSize(text_size);
289 // TODO(ckocagil): Do we need to update these params later?
290 internal::ApplyRenderParams(params, &hb_font_data->paint_);
287 hb_font_set_funcs(harfbuzz_font, g_font_funcs.Get().get(), hb_font_data, 291 hb_font_set_funcs(harfbuzz_font, g_font_funcs.Get().get(), hb_font_data,
288 DeleteByType<FontData>); 292 DeleteByType<FontData>);
289 hb_font_make_immutable(harfbuzz_font); 293 hb_font_make_immutable(harfbuzz_font);
290 return harfbuzz_font; 294 return harfbuzz_font;
291 } 295 }
292 296
293 // Returns true if characters of |block_code| may trigger font fallback. 297 // Returns true if characters of |block_code| may trigger font fallback.
294 bool IsUnusualBlockCode(UBlockCode block_code) { 298 bool IsUnusualBlockCode(UBlockCode block_code) {
295 return block_code == UBLOCK_GEOMETRIC_SHAPES || 299 return block_code == UBLOCK_GEOMETRIC_SHAPES ||
296 block_code == UBLOCK_MISCELLANEOUS_SYMBOLS; 300 block_code == UBLOCK_MISCELLANEOUS_SYMBOLS;
(...skipping 566 matching lines...) Expand 10 before | Expand all | Expand 10 after
863 867
864 set_lines(&lines); 868 set_lines(&lines);
865 } 869 }
866 } 870 }
867 871
868 void RenderTextHarfBuzz::DrawVisualText(Canvas* canvas) { 872 void RenderTextHarfBuzz::DrawVisualText(Canvas* canvas) {
869 DCHECK(!needs_layout_); 873 DCHECK(!needs_layout_);
870 internal::SkiaTextRenderer renderer(canvas); 874 internal::SkiaTextRenderer renderer(canvas);
871 ApplyFadeEffects(&renderer); 875 ApplyFadeEffects(&renderer);
872 ApplyTextShadows(&renderer); 876 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(); 877 ApplyCompositionAndSelectionStyles();
881 878
882 int current_x = 0; 879 int current_x = 0;
883 const Vector2d line_offset = GetLineOffset(0); 880 const Vector2d line_offset = GetLineOffset(0);
884 for (size_t i = 0; i < runs_.size(); ++i) { 881 for (size_t i = 0; i < runs_.size(); ++i) {
885 const internal::TextRunHarfBuzz& run = *runs_[visual_to_logical_[i]]; 882 const internal::TextRunHarfBuzz& run = *runs_[visual_to_logical_[i]];
886 renderer.SetTypeface(run.skia_face.get()); 883 renderer.SetTypeface(run.skia_face.get());
887 renderer.SetTextSize(run.font_size); 884 renderer.SetTextSize(run.font_size);
885 #if defined(OS_WIN) || defined(OS_LINUX)
886 renderer.SetFontRenderParams(run.render_params,
887 background_is_transparent());
888 #endif
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;
1115 hb_font_t* harfbuzz_font = CreateHarfBuzzFont(run->skia_face.get(), 1116 FontRenderParamsQuery query(false);
1116 run->font_size); 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);
1121 if (background_is_transparent()) {
1122 run->render_params.subpixel_rendering =
msw 2014/08/16 00:30:11 Is this needed for the SkPaint's settings here, or
ckocagil 2014/08/16 14:01:14 I prefer using the exact same font render params f
1123 FontRenderParams::SUBPIXEL_RENDERING_NONE;
1124 }
1125 hb_font_t* harfbuzz_font = CreateHarfBuzzFont(
1126 run->skia_face.get(), run->font_size, run->render_params);
1117 1127
1118 // Create a HarfBuzz buffer and add the string to be shaped. The HarfBuzz 1128 // 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, 1129 // buffer holds our text, run information to be used by the shaping engine,
1120 // and the resulting glyph data. 1130 // and the resulting glyph data.
1121 hb_buffer_t* buffer = hb_buffer_create(); 1131 hb_buffer_t* buffer = hb_buffer_create();
1122 hb_buffer_add_utf16(buffer, reinterpret_cast<const uint16*>(text.c_str()), 1132 hb_buffer_add_utf16(buffer, reinterpret_cast<const uint16*>(text.c_str()),
1123 text.length(), run->range.start(), run->range.length()); 1133 text.length(), run->range.start(), run->range.length());
1124 hb_buffer_set_script(buffer, ICUScriptToHBScript(run->script)); 1134 hb_buffer_set_script(buffer, ICUScriptToHBScript(run->script));
1125 hb_buffer_set_direction(buffer, 1135 hb_buffer_set_direction(buffer,
1126 run->is_rtl ? HB_DIRECTION_RTL : HB_DIRECTION_LTR); 1136 run->is_rtl ? HB_DIRECTION_RTL : HB_DIRECTION_LTR);
(...skipping 13 matching lines...) Expand all
1140 run->glyph_to_char.resize(run->glyph_count); 1150 run->glyph_to_char.resize(run->glyph_count);
1141 run->positions.reset(new SkPoint[run->glyph_count]); 1151 run->positions.reset(new SkPoint[run->glyph_count]);
1142 run->width = 0.0f; 1152 run->width = 0.0f;
1143 for (size_t i = 0; i < run->glyph_count; ++i) { 1153 for (size_t i = 0; i < run->glyph_count; ++i) {
1144 run->glyphs[i] = infos[i].codepoint; 1154 run->glyphs[i] = infos[i].codepoint;
1145 run->glyph_to_char[i] = infos[i].cluster; 1155 run->glyph_to_char[i] = infos[i].cluster;
1146 const int x_offset = SkFixedToScalar(hb_positions[i].x_offset); 1156 const int x_offset = SkFixedToScalar(hb_positions[i].x_offset);
1147 const int y_offset = SkFixedToScalar(hb_positions[i].y_offset); 1157 const int y_offset = SkFixedToScalar(hb_positions[i].y_offset);
1148 run->positions[i].set(run->width + x_offset, -y_offset); 1158 run->positions[i].set(run->width + x_offset, -y_offset);
1149 run->width += SkFixedToScalar(hb_positions[i].x_advance); 1159 run->width += SkFixedToScalar(hb_positions[i].x_advance);
1160 run->width = std::floor(run->width + 0.5f);
1150 } 1161 }
1151 1162
1152 hb_buffer_destroy(buffer); 1163 hb_buffer_destroy(buffer);
1153 hb_font_destroy(harfbuzz_font); 1164 hb_font_destroy(harfbuzz_font);
1154 return true; 1165 return true;
1155 } 1166 }
1156 1167
1157 } // namespace gfx 1168 } // 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