Chromium Code Reviews| 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/debug/leak_annotations.h" | 9 #include "base/debug/leak_annotations.h" |
| 10 #include "base/i18n/bidi_line_iterator.h" | 10 #include "base/i18n/bidi_line_iterator.h" |
| 11 #include "base/i18n/break_iterator.h" | 11 #include "base/i18n/break_iterator.h" |
| 12 #include "base/i18n/char_iterator.h" | 12 #include "base/i18n/char_iterator.h" |
| 13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 14 #include "third_party/harfbuzz-ng/src/hb.h" | 14 #include "third_party/harfbuzz-ng/src/hb.h" |
| 15 #include "third_party/icu/source/common/unicode/ubidi.h" | 15 #include "third_party/icu/source/common/unicode/ubidi.h" |
| 16 #include "third_party/skia/include/core/SkColor.h" | 16 #include "third_party/skia/include/core/SkColor.h" |
| 17 #include "third_party/skia/include/core/SkTypeface.h" | 17 #include "third_party/skia/include/core/SkTypeface.h" |
| 18 #include "ui/gfx/canvas.h" | 18 #include "ui/gfx/canvas.h" |
| 19 #include "ui/gfx/font_fallback.h" | |
| 19 #include "ui/gfx/font_render_params.h" | 20 #include "ui/gfx/font_render_params.h" |
| 20 #include "ui/gfx/utf16_indexing.h" | 21 #include "ui/gfx/utf16_indexing.h" |
| 21 | 22 |
| 23 #if defined(OS_WIN) | |
| 24 #include "ui/gfx/font_fallback_win.h" | |
| 25 #endif | |
| 26 | |
| 22 namespace gfx { | 27 namespace gfx { |
| 23 | 28 |
| 24 namespace { | 29 namespace { |
| 25 | 30 |
| 26 // The maximum number of scripts a Unicode character can belong to. This value | 31 // The maximum number of scripts a Unicode character can belong to. This value |
| 27 // is arbitrarily chosen to be a good limit because it is unlikely for a single | 32 // is arbitrarily chosen to be a good limit because it is unlikely for a single |
| 28 // character to belong to more scripts. | 33 // character to belong to more scripts. |
| 29 const size_t kMaxScripts = 5; | 34 const size_t kMaxScripts = 5; |
| 30 | 35 |
| 31 // Maps from code points to glyph indices in a font. | 36 // Maps from code points to glyph indices in a font. |
| (...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 456 | 461 |
| 457 first = CharToGlyph(char_range.start()); | 462 first = CharToGlyph(char_range.start()); |
| 458 for (size_t i = char_range.end(); i < range.end(); ++i) { | 463 for (size_t i = char_range.end(); i < range.end(); ++i) { |
| 459 last = CharToGlyph(i); | 464 last = CharToGlyph(i); |
| 460 if (first != last) | 465 if (first != last) |
| 461 return Range(first, last); | 466 return Range(first, last); |
| 462 } | 467 } |
| 463 return Range(first, glyph_count); | 468 return Range(first, glyph_count); |
| 464 } | 469 } |
| 465 | 470 |
| 466 // Returns whether the given shaped run contains any missing glyphs. | 471 size_t TextRunHarfBuzz::CountMissingGlyphs() const { |
| 467 bool TextRunHarfBuzz::HasMissingGlyphs() const { | |
| 468 static const int kMissingGlyphId = 0; | 472 static const int kMissingGlyphId = 0; |
| 469 for (size_t i = 0; i < glyph_count; ++i) { | 473 size_t missing = 0; |
| 470 if (glyphs[i] == kMissingGlyphId) | 474 for (size_t i = 0; i < glyph_count; ++i) |
| 471 return true; | 475 missing += (glyphs[i] == kMissingGlyphId) ? 1 : 0; |
| 472 } | 476 return missing; |
| 473 return false; | |
| 474 } | 477 } |
| 475 | 478 |
| 476 int TextRunHarfBuzz::GetGlyphXBoundary(size_t text_index, bool trailing) const { | 479 int TextRunHarfBuzz::GetGlyphXBoundary(size_t text_index, bool trailing) const { |
| 477 if (text_index == range.end()) { | 480 if (text_index == range.end()) { |
| 478 trailing = true; | 481 trailing = true; |
| 479 --text_index; | 482 --text_index; |
| 480 } | 483 } |
| 481 Range glyph_range = CharRangeToGlyphRange(Range(text_index, text_index + 1)); | 484 Range glyph_range = CharRangeToGlyphRange(Range(text_index, text_index + 1)); |
| 482 const size_t glyph_pos = (is_rtl == trailing) ? | 485 const size_t glyph_pos = (is_rtl == trailing) ? |
| 483 glyph_range.start() : glyph_range.end(); | 486 glyph_range.start() : glyph_range.end(); |
| (...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 941 std::vector<UBiDiLevel> levels(num_runs); | 944 std::vector<UBiDiLevel> levels(num_runs); |
| 942 for (size_t i = 0; i < num_runs; ++i) | 945 for (size_t i = 0; i < num_runs; ++i) |
| 943 levels[i] = runs_[i]->level; | 946 levels[i] = runs_[i]->level; |
| 944 visual_to_logical_.resize(num_runs); | 947 visual_to_logical_.resize(num_runs); |
| 945 ubidi_reorderVisual(&levels[0], num_runs, &visual_to_logical_[0]); | 948 ubidi_reorderVisual(&levels[0], num_runs, &visual_to_logical_[0]); |
| 946 logical_to_visual_.resize(num_runs); | 949 logical_to_visual_.resize(num_runs); |
| 947 ubidi_reorderLogical(&levels[0], num_runs, &logical_to_visual_[0]); | 950 ubidi_reorderLogical(&levels[0], num_runs, &logical_to_visual_[0]); |
| 948 } | 951 } |
| 949 | 952 |
| 950 void RenderTextHarfBuzz::ShapeRun(internal::TextRunHarfBuzz* run) { | 953 void RenderTextHarfBuzz::ShapeRun(internal::TextRunHarfBuzz* run) { |
| 954 const Font& primary_font = font_list().GetPrimaryFont(); | |
| 955 run->font_size = primary_font.GetFontSize(); | |
| 956 std::vector<std::string> fonts = | |
| 957 GetFallbackFontFamilies(primary_font.GetFontName()); | |
|
msw
2014/07/12 17:41:11
Let's only call this if |primary_font| doesn't sha
ckocagil
2014/07/12 19:53:26
Done.
| |
| 958 std::string best_font = primary_font.GetFontName(); | |
| 959 size_t best_font_missing = -1; | |
| 960 | |
| 961 // Try shaping with the first font in the list, which is |primary_font|. | |
| 962 ShapeRunWithFont(run, fonts[0]); | |
| 963 size_t current_missing = run->CountMissingGlyphs(); | |
| 964 if (current_missing == 0) | |
| 965 return; | |
| 966 if (current_missing < best_font_missing) { | |
| 967 best_font_missing = current_missing; | |
| 968 best_font = fonts[0]; | |
| 969 } | |
| 970 | |
| 971 #if defined(OS_WIN) | |
| 972 Font uniscribe_font; | |
| 973 if (GetUniscribeFallbackFont(NULL, primary_font, | |
|
msw
2014/07/12 17:41:11
Could font_fallback_win insert any Uniscribe fallb
ckocagil
2014/07/12 19:53:26
This would issue a call to GetUniscribeFallbackFon
msw
2014/07/12 22:03:38
You're right, forget this for now, we can consider
| |
| 974 &(GetLayoutText()[run->range.start()]), run->range.length(), | |
| 975 &uniscribe_font)) { | |
| 976 ShapeRunWithFont(run, uniscribe_font.GetFontName()); | |
| 977 size_t current_missing = run->CountMissingGlyphs(); | |
| 978 if (current_missing == 0) | |
| 979 return; | |
| 980 if (current_missing < best_font_missing) { | |
| 981 best_font_missing = current_missing; | |
| 982 best_font = uniscribe_font.GetFontName(); | |
| 983 } | |
| 984 } | |
| 985 #endif | |
| 986 | |
| 987 // Try shaping with the rest of the fonts in the list. | |
| 988 for (size_t i = 1; i < fonts.size(); ++i) { | |
| 989 ShapeRunWithFont(run, fonts[i]); | |
| 990 size_t current_missing = run->CountMissingGlyphs(); | |
| 991 if (current_missing == 0) | |
| 992 return; | |
| 993 if (current_missing < best_font_missing) { | |
| 994 best_font_missing = current_missing; | |
| 995 best_font = fonts[i]; | |
| 996 } | |
| 997 } | |
| 998 | |
| 999 ShapeRunWithFont(run, best_font); | |
| 1000 } | |
| 1001 | |
| 1002 void RenderTextHarfBuzz::ShapeRunWithFont(internal::TextRunHarfBuzz* run, | |
| 1003 std::string font) { | |
| 951 const base::string16& text = GetLayoutText(); | 1004 const base::string16& text = GetLayoutText(); |
| 952 // TODO(ckocagil|yukishiino): Implement font fallback. | 1005 run->skia_face = internal::CreateSkiaTypeface(font, run->font_style); |
| 953 const Font& primary_font = font_list().GetPrimaryFont(); | |
| 954 run->skia_face = internal::CreateSkiaTypeface(primary_font.GetFontName(), | |
| 955 run->font_style); | |
| 956 run->font_size = primary_font.GetFontSize(); | |
| 957 | |
| 958 hb_font_t* harfbuzz_font = CreateHarfBuzzFont(run->skia_face.get(), | 1006 hb_font_t* harfbuzz_font = CreateHarfBuzzFont(run->skia_face.get(), |
| 959 run->font_size); | 1007 run->font_size); |
| 960 | 1008 |
| 961 // Create a HarfBuzz buffer and add the string to be shaped. The HarfBuzz | 1009 // Create a HarfBuzz buffer and add the string to be shaped. The HarfBuzz |
| 962 // buffer holds our text, run information to be used by the shaping engine, | 1010 // buffer holds our text, run information to be used by the shaping engine, |
| 963 // and the resulting glyph data. | 1011 // and the resulting glyph data. |
| 964 hb_buffer_t* buffer = hb_buffer_create(); | 1012 hb_buffer_t* buffer = hb_buffer_create(); |
| 965 hb_buffer_add_utf16(buffer, reinterpret_cast<const uint16*>(text.c_str()), | 1013 hb_buffer_add_utf16(buffer, reinterpret_cast<const uint16*>(text.c_str()), |
| 966 text.length(), run->range.start(), run->range.length()); | 1014 text.length(), run->range.start(), run->range.length()); |
| 967 hb_buffer_set_script(buffer, ICUScriptToHBScript(run->script)); | 1015 hb_buffer_set_script(buffer, ICUScriptToHBScript(run->script)); |
| 968 hb_buffer_set_direction(buffer, | 1016 hb_buffer_set_direction(buffer, |
| 969 run->is_rtl ? HB_DIRECTION_RTL : HB_DIRECTION_LTR); | 1017 run->is_rtl ? HB_DIRECTION_RTL : HB_DIRECTION_LTR); |
| 970 // TODO(ckocagil): Should we determine the actual language? | 1018 // TODO(ckocagil): Should we call |hb_buffer_set_language()| here? |
| 971 hb_buffer_set_language(buffer, hb_language_get_default()); | |
| 972 | 1019 |
| 973 // Shape the text. | 1020 // Shape the text. |
| 974 hb_shape(harfbuzz_font, buffer, NULL, 0); | 1021 hb_shape(harfbuzz_font, buffer, NULL, 0); |
| 975 | 1022 |
| 976 // Populate the run fields with the resulting glyph data in the buffer. | 1023 // Populate the run fields with the resulting glyph data in the buffer. |
| 977 unsigned int glyph_count = 0; | 1024 hb_glyph_info_t* infos = hb_buffer_get_glyph_infos(buffer, &run->glyph_count); |
| 978 hb_glyph_info_t* infos = hb_buffer_get_glyph_infos(buffer, &glyph_count); | 1025 hb_glyph_position_t* hb_positions = |
| 979 hb_glyph_position_t* hb_positions = hb_buffer_get_glyph_positions(buffer, | 1026 hb_buffer_get_glyph_positions(buffer, NULL); |
| 980 NULL); | |
| 981 run->glyph_count = glyph_count; | |
| 982 run->glyphs.reset(new uint16[run->glyph_count]); | 1027 run->glyphs.reset(new uint16[run->glyph_count]); |
| 983 run->glyph_to_char.reset(new uint32[run->glyph_count]); | 1028 run->glyph_to_char.reset(new uint32[run->glyph_count]); |
| 984 run->positions.reset(new SkPoint[run->glyph_count]); | 1029 run->positions.reset(new SkPoint[run->glyph_count]); |
| 1030 run->width = 0; | |
| 985 for (size_t i = 0; i < run->glyph_count; ++i) { | 1031 for (size_t i = 0; i < run->glyph_count; ++i) { |
| 986 run->glyphs[i] = infos[i].codepoint; | 1032 run->glyphs[i] = infos[i].codepoint; |
| 987 run->glyph_to_char[i] = infos[i].cluster; | 1033 run->glyph_to_char[i] = infos[i].cluster; |
| 988 const int x_offset = | 1034 const int x_offset = |
| 989 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].x_offset)); | 1035 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].x_offset)); |
| 990 const int y_offset = | 1036 const int y_offset = |
| 991 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].y_offset)); | 1037 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].y_offset)); |
| 992 run->positions[i].set(run->width + x_offset, -y_offset); | 1038 run->positions[i].set(run->width + x_offset, y_offset); |
| 993 run->width += | 1039 run->width += |
| 994 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].x_advance)); | 1040 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].x_advance)); |
| 995 } | 1041 } |
| 996 | 1042 |
| 997 hb_buffer_destroy(buffer); | 1043 hb_buffer_destroy(buffer); |
| 998 hb_font_destroy(harfbuzz_font); | 1044 hb_font_destroy(harfbuzz_font); |
| 999 } | 1045 } |
| 1000 | 1046 |
| 1001 } // namespace gfx | 1047 } // namespace gfx |
| OLD | NEW |