OLD | NEW |
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 Loading... |
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 Loading... |
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 Loading... |
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 = |
| 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 Loading... |
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 // If subpixel positioning isn't enabled, round the glyph x coordinates. |
| 1161 if (!run->render_params.subpixel_positioning) |
| 1162 run->width = std::floor(run->width + 0.5f); |
1150 } | 1163 } |
1151 | 1164 |
1152 hb_buffer_destroy(buffer); | 1165 hb_buffer_destroy(buffer); |
1153 hb_font_destroy(harfbuzz_font); | 1166 hb_font_destroy(harfbuzz_font); |
1154 return true; | 1167 return true; |
1155 } | 1168 } |
1156 | 1169 |
1157 } // namespace gfx | 1170 } // namespace gfx |
OLD | NEW |