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, | |
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 Loading... | |
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 #if defined(OS_WIN) || defined(OS_LINUX) | |
msw
2014/08/19 17:26:21
nit: Is this guard still needed now?
ckocagil
2014/08/19 17:34:25
Nope. Removed!
| |
888 renderer.SetFontRenderParams(run.render_params, | |
889 background_is_transparent()); | |
890 #endif | |
888 | 891 |
889 Vector2d origin = line_offset + Vector2d(current_x, lines()[0].baseline); | 892 Vector2d origin = line_offset + Vector2d(current_x, lines()[0].baseline); |
890 scoped_ptr<SkPoint[]> positions(new SkPoint[run.glyph_count]); | 893 scoped_ptr<SkPoint[]> positions(new SkPoint[run.glyph_count]); |
891 for (size_t j = 0; j < run.glyph_count; ++j) { | 894 for (size_t j = 0; j < run.glyph_count; ++j) { |
892 positions[j] = run.positions[j]; | 895 positions[j] = run.positions[j]; |
893 positions[j].offset(SkIntToScalar(origin.x()), SkIntToScalar(origin.y())); | 896 positions[j].offset(SkIntToScalar(origin.x()), SkIntToScalar(origin.y())); |
894 } | 897 } |
895 | 898 |
896 for (BreakList<SkColor>::const_iterator it = | 899 for (BreakList<SkColor>::const_iterator it = |
897 colors().GetBreak(run.range.start()); | 900 colors().GetBreak(run.range.start()); |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1105 } | 1108 } |
1106 | 1109 |
1107 bool RenderTextHarfBuzz::ShapeRunWithFont(internal::TextRunHarfBuzz* run, | 1110 bool RenderTextHarfBuzz::ShapeRunWithFont(internal::TextRunHarfBuzz* run, |
1108 const std::string& font_family) { | 1111 const std::string& font_family) { |
1109 const base::string16& text = GetLayoutText(); | 1112 const base::string16& text = GetLayoutText(); |
1110 skia::RefPtr<SkTypeface> skia_face = | 1113 skia::RefPtr<SkTypeface> skia_face = |
1111 internal::CreateSkiaTypeface(font_family, run->font_style); | 1114 internal::CreateSkiaTypeface(font_family, run->font_style); |
1112 if (skia_face == NULL) | 1115 if (skia_face == NULL) |
1113 return false; | 1116 return false; |
1114 run->skia_face = skia_face; | 1117 run->skia_face = skia_face; |
1118 FontRenderParamsQuery query(false); | |
1119 query.families.push_back(font_family); | |
1120 query.pixel_size = run->font_size; | |
1121 query.style = run->font_style; | |
1122 run->render_params = GetFontRenderParams(query, NULL); | |
1115 hb_font_t* harfbuzz_font = CreateHarfBuzzFont(run->skia_face.get(), | 1123 hb_font_t* harfbuzz_font = CreateHarfBuzzFont(run->skia_face.get(), |
1116 run->font_size); | 1124 run->font_size, run->render_params, background_is_transparent()); |
1117 | 1125 |
1118 // Create a HarfBuzz buffer and add the string to be shaped. The HarfBuzz | 1126 // 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, | 1127 // buffer holds our text, run information to be used by the shaping engine, |
1120 // and the resulting glyph data. | 1128 // and the resulting glyph data. |
1121 hb_buffer_t* buffer = hb_buffer_create(); | 1129 hb_buffer_t* buffer = hb_buffer_create(); |
1122 hb_buffer_add_utf16(buffer, reinterpret_cast<const uint16*>(text.c_str()), | 1130 hb_buffer_add_utf16(buffer, reinterpret_cast<const uint16*>(text.c_str()), |
1123 text.length(), run->range.start(), run->range.length()); | 1131 text.length(), run->range.start(), run->range.length()); |
1124 hb_buffer_set_script(buffer, ICUScriptToHBScript(run->script)); | 1132 hb_buffer_set_script(buffer, ICUScriptToHBScript(run->script)); |
1125 hb_buffer_set_direction(buffer, | 1133 hb_buffer_set_direction(buffer, |
1126 run->is_rtl ? HB_DIRECTION_RTL : HB_DIRECTION_LTR); | 1134 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); | 1148 run->glyph_to_char.resize(run->glyph_count); |
1141 run->positions.reset(new SkPoint[run->glyph_count]); | 1149 run->positions.reset(new SkPoint[run->glyph_count]); |
1142 run->width = 0.0f; | 1150 run->width = 0.0f; |
1143 for (size_t i = 0; i < run->glyph_count; ++i) { | 1151 for (size_t i = 0; i < run->glyph_count; ++i) { |
1144 run->glyphs[i] = infos[i].codepoint; | 1152 run->glyphs[i] = infos[i].codepoint; |
1145 run->glyph_to_char[i] = infos[i].cluster; | 1153 run->glyph_to_char[i] = infos[i].cluster; |
1146 const int x_offset = SkFixedToScalar(hb_positions[i].x_offset); | 1154 const int x_offset = SkFixedToScalar(hb_positions[i].x_offset); |
1147 const int y_offset = SkFixedToScalar(hb_positions[i].y_offset); | 1155 const int y_offset = SkFixedToScalar(hb_positions[i].y_offset); |
1148 run->positions[i].set(run->width + x_offset, -y_offset); | 1156 run->positions[i].set(run->width + x_offset, -y_offset); |
1149 run->width += SkFixedToScalar(hb_positions[i].x_advance); | 1157 run->width += SkFixedToScalar(hb_positions[i].x_advance); |
1158 // If subpixel positioning isn't enabled, round the glyph x coordinates. | |
1159 if (!run->render_params.subpixel_positioning) | |
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 |
OLD | NEW |