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 <limits> | 7 #include <limits> |
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 455 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
466 } | 466 } |
467 | 467 |
468 TextRunHarfBuzz::TextRunHarfBuzz() | 468 TextRunHarfBuzz::TextRunHarfBuzz() |
469 : width(0.0f), | 469 : width(0.0f), |
470 preceding_run_widths(0.0f), | 470 preceding_run_widths(0.0f), |
471 is_rtl(false), | 471 is_rtl(false), |
472 level(0), | 472 level(0), |
473 script(USCRIPT_INVALID_CODE), | 473 script(USCRIPT_INVALID_CODE), |
474 glyph_count(static_cast<size_t>(-1)), | 474 glyph_count(static_cast<size_t>(-1)), |
475 font_size(0), | 475 font_size(0), |
476 baseline_offset(0), | |
477 baseline_type(0), | |
476 font_style(0), | 478 font_style(0), |
477 strike(false), | 479 strike(false), |
478 diagonal_strike(false), | 480 diagonal_strike(false), |
479 underline(false) {} | 481 underline(false) {} |
480 | 482 |
481 TextRunHarfBuzz::~TextRunHarfBuzz() {} | 483 TextRunHarfBuzz::~TextRunHarfBuzz() {} |
482 | 484 |
483 void TextRunHarfBuzz::GetClusterAt(size_t pos, | 485 void TextRunHarfBuzz::GetClusterAt(size_t pos, |
484 Range* chars, | 486 Range* chars, |
485 Range* glyphs) const { | 487 Range* glyphs) const { |
(...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
960 background_is_transparent()); | 962 background_is_transparent()); |
961 Range glyphs_range = run.CharRangeToGlyphRange(segment.char_range); | 963 Range glyphs_range = run.CharRangeToGlyphRange(segment.char_range); |
962 scoped_ptr<SkPoint[]> positions(new SkPoint[glyphs_range.length()]); | 964 scoped_ptr<SkPoint[]> positions(new SkPoint[glyphs_range.length()]); |
963 SkScalar offset_x = | 965 SkScalar offset_x = |
964 preceding_segment_widths - run.positions[glyphs_range.start()].x(); | 966 preceding_segment_widths - run.positions[glyphs_range.start()].x(); |
965 for (size_t j = 0; j < glyphs_range.length(); ++j) { | 967 for (size_t j = 0; j < glyphs_range.length(); ++j) { |
966 positions[j] = run.positions[(glyphs_range.is_reversed()) ? | 968 positions[j] = run.positions[(glyphs_range.is_reversed()) ? |
967 (glyphs_range.start() - j) : | 969 (glyphs_range.start() - j) : |
968 (glyphs_range.start() + j)]; | 970 (glyphs_range.start() + j)]; |
969 positions[j].offset(SkIntToScalar(origin.x()) + offset_x, | 971 positions[j].offset(SkIntToScalar(origin.x()) + offset_x, |
970 SkIntToScalar(origin.y())); | 972 SkIntToScalar(origin.y() + run.baseline_offset)); |
971 } | 973 } |
972 for (BreakList<SkColor>::const_iterator it = | 974 for (BreakList<SkColor>::const_iterator it = |
973 colors().GetBreak(segment.char_range.start()); | 975 colors().GetBreak(segment.char_range.start()); |
974 it != colors().breaks().end() && | 976 it != colors().breaks().end() && |
975 it->first < segment.char_range.end(); | 977 it->first < segment.char_range.end(); |
976 ++it) { | 978 ++it) { |
977 const Range intersection = | 979 const Range intersection = |
978 colors().GetRange(it).Intersect(segment.char_range); | 980 colors().GetRange(it).Intersect(segment.char_range); |
979 const Range colored_glyphs = run.CharRangeToGlyphRange(intersection); | 981 const Range colored_glyphs = run.CharRangeToGlyphRange(intersection); |
980 // The range may be empty if a portion of a multi-character grapheme is | 982 // The range may be empty if a portion of a multi-character grapheme is |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1069 return; | 1071 return; |
1070 } | 1072 } |
1071 | 1073 |
1072 // Temporarily apply composition underlines and selection colors. | 1074 // Temporarily apply composition underlines and selection colors. |
1073 ApplyCompositionAndSelectionStyles(); | 1075 ApplyCompositionAndSelectionStyles(); |
1074 | 1076 |
1075 // Build the list of runs from the script items and ranged styles. Use an | 1077 // Build the list of runs from the script items and ranged styles. Use an |
1076 // empty color BreakList to avoid breaking runs at color boundaries. | 1078 // empty color BreakList to avoid breaking runs at color boundaries. |
1077 BreakList<SkColor> empty_colors; | 1079 BreakList<SkColor> empty_colors; |
1078 empty_colors.SetMax(text.length()); | 1080 empty_colors.SetMax(text.length()); |
1079 internal::StyleIterator style(empty_colors, styles()); | 1081 internal::StyleIterator style(empty_colors, baselines(), styles()); |
1080 | 1082 |
1081 for (size_t run_break = 0; run_break < text.length();) { | 1083 for (size_t run_break = 0; run_break < text.length();) { |
1082 internal::TextRunHarfBuzz* run = new internal::TextRunHarfBuzz; | 1084 internal::TextRunHarfBuzz* run = new internal::TextRunHarfBuzz; |
1083 run->range.set_start(run_break); | 1085 run->range.set_start(run_break); |
1084 run->font_style = (style.style(BOLD) ? Font::BOLD : 0) | | 1086 run->font_style = (style.style(BOLD) ? Font::BOLD : 0) | |
1085 (style.style(ITALIC) ? Font::ITALIC : 0); | 1087 (style.style(ITALIC) ? Font::ITALIC : 0); |
1088 run->baseline_type = style.baseline(); | |
1086 run->strike = style.style(STRIKE); | 1089 run->strike = style.style(STRIKE); |
1087 run->diagonal_strike = style.style(DIAGONAL_STRIKE); | 1090 run->diagonal_strike = style.style(DIAGONAL_STRIKE); |
1088 run->underline = style.style(UNDERLINE); | 1091 run->underline = style.style(UNDERLINE); |
1089 | 1092 |
1090 int32 script_item_break = 0; | 1093 int32 script_item_break = 0; |
1091 bidi_iterator.GetLogicalRun(run_break, &script_item_break, &run->level); | 1094 bidi_iterator.GetLogicalRun(run_break, &script_item_break, &run->level); |
1092 // Odd BiDi embedding levels correspond to RTL runs. | 1095 // Odd BiDi embedding levels correspond to RTL runs. |
1093 run->is_rtl = (run->level % 2) == 1; | 1096 run->is_rtl = (run->level % 2) == 1; |
1094 // Find the length and script of this script run. | 1097 // Find the length and script of this script run. |
1095 script_item_break = ScriptInterval(text, run_break, | 1098 script_item_break = ScriptInterval(text, run_break, |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1142 *best_render_params = render_params; | 1145 *best_render_params = render_params; |
1143 *best_missing_glyphs = missing_glyphs; | 1146 *best_missing_glyphs = missing_glyphs; |
1144 } | 1147 } |
1145 return missing_glyphs == 0; | 1148 return missing_glyphs == 0; |
1146 } | 1149 } |
1147 | 1150 |
1148 void RenderTextHarfBuzz::ShapeRun(internal::TextRunHarfBuzz* run) { | 1151 void RenderTextHarfBuzz::ShapeRun(internal::TextRunHarfBuzz* run) { |
1149 const Font& primary_font = font_list().GetPrimaryFont(); | 1152 const Font& primary_font = font_list().GetPrimaryFont(); |
1150 const std::string primary_family = primary_font.GetFontName(); | 1153 const std::string primary_family = primary_font.GetFontName(); |
1151 run->font_size = primary_font.GetFontSize(); | 1154 run->font_size = primary_font.GetFontSize(); |
1155 if (run->baseline_type) { | |
msw
2015/02/18 17:07:23
nit: explicitly compare if (run->baseline_type !=
dschuyler
2015/02/18 22:36:03
Done.
| |
1156 // Calculate a slightly smaller font. The ratio here is somewhat arbitrary. | |
1157 // Proportions from 5/9 to 5/7 all look pretty good. | |
1158 const float ratio = 5.0f / 9.0f; | |
1159 run->font_size = round(primary_font.GetFontSize() * ratio); | |
1160 int cap_height = round(primary_font.GetCapHeight() * ratio); | |
1161 switch (run->baseline_type) { | |
msw
2015/02/18 17:07:23
nit: I think you'll need a default case or explici
dschuyler
2015/02/18 22:36:03
I moved the INFERIOR case to fall through to the n
| |
1162 case SUPERSCRIPT: | |
1163 run->baseline_offset = | |
1164 primary_font.GetCapHeight() - primary_font.GetHeight(); | |
1165 break; | |
1166 case SUPERIOR: | |
1167 run->baseline_offset = cap_height - primary_font.GetCapHeight(); | |
1168 break; | |
1169 case INFERIOR: | |
1170 run->baseline_offset = 0; | |
1171 break; | |
1172 case SUBSCRIPT: | |
1173 run->baseline_offset = | |
1174 primary_font.GetHeight() - primary_font.GetBaseline(); | |
1175 break; | |
1176 } | |
1177 } | |
1152 | 1178 |
1153 std::string best_family; | 1179 std::string best_family; |
1154 FontRenderParams best_render_params; | 1180 FontRenderParams best_render_params; |
1155 size_t best_missing_glyphs = std::numeric_limits<size_t>::max(); | 1181 size_t best_missing_glyphs = std::numeric_limits<size_t>::max(); |
1156 | 1182 |
1157 for (const Font& font : font_list().GetFonts()) { | 1183 for (const Font& font : font_list().GetFonts()) { |
1158 if (CompareFamily(run, font.GetFontName(), font.GetFontRenderParams(), | 1184 if (CompareFamily(run, font.GetFontName(), font.GetFontRenderParams(), |
1159 &best_family, &best_render_params, &best_missing_glyphs)) | 1185 &best_family, &best_render_params, &best_missing_glyphs)) |
1160 return; | 1186 return; |
1161 } | 1187 } |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1324 if (!run->render_params.subpixel_positioning) | 1350 if (!run->render_params.subpixel_positioning) |
1325 run->width = std::floor(run->width + 0.5f); | 1351 run->width = std::floor(run->width + 0.5f); |
1326 } | 1352 } |
1327 | 1353 |
1328 hb_buffer_destroy(buffer); | 1354 hb_buffer_destroy(buffer); |
1329 hb_font_destroy(harfbuzz_font); | 1355 hb_font_destroy(harfbuzz_font); |
1330 return true; | 1356 return true; |
1331 } | 1357 } |
1332 | 1358 |
1333 } // namespace gfx | 1359 } // namespace gfx |
OLD | NEW |