Chromium Code Reviews| Index: ui/gfx/render_text_harfbuzz.cc |
| diff --git a/ui/gfx/render_text_harfbuzz.cc b/ui/gfx/render_text_harfbuzz.cc |
| index a174aeadbc0966d7af9ca91ef7515ee48f294023..6f28c3c053fd05a9470298fbf637296fe8e367a8 100644 |
| --- a/ui/gfx/render_text_harfbuzz.cc |
| +++ b/ui/gfx/render_text_harfbuzz.cc |
| @@ -16,6 +16,7 @@ |
| #include "third_party/skia/include/core/SkColor.h" |
| #include "third_party/skia/include/core/SkTypeface.h" |
| #include "ui/gfx/canvas.h" |
| +#include "ui/gfx/font_fallback.h" |
| #include "ui/gfx/utf16_indexing.h" |
| #if defined(OS_WIN) |
| @@ -467,14 +468,14 @@ Range TextRunHarfBuzz::CharRangeToGlyphRange(const Range& char_range) const { |
| return Range(first, glyph_count); |
| } |
| -// Returns whether the given shaped run contains any missing glyphs. |
| -bool TextRunHarfBuzz::HasMissingGlyphs() const { |
| +size_t TextRunHarfBuzz::CountMissingGlyphs() const { |
| static const int kMissingGlyphId = 0; |
| + size_t missing = 0; |
| for (size_t i = 0; i < glyph_count; ++i) { |
| if (glyphs[i] == kMissingGlyphId) |
|
msw
2014/07/07 22:52:46
optional nit: missing += glyphs[i] == kMissingGlyp
ckocagil
2014/07/12 11:47:54
Done.
|
| - return true; |
| + ++missing; |
| } |
| - return false; |
| + return missing; |
| } |
| int TextRunHarfBuzz::GetGlyphXBoundary(size_t text_index, bool trailing) const { |
| @@ -959,13 +960,31 @@ void RenderTextHarfBuzz::ItemizeText() { |
| } |
| void RenderTextHarfBuzz::ShapeRun(internal::TextRunHarfBuzz* run) { |
| - const base::string16& text = GetLayoutText(); |
| - // TODO(ckocagil|yukishiino): Implement font fallback. |
| const Font& primary_font = font_list().GetPrimaryFont(); |
| - run->skia_face = internal::CreateSkiaTypeface(primary_font.GetFontName(), |
| - run->font_style); |
| run->font_size = primary_font.GetFontSize(); |
| + std::vector<std::string> fonts = |
| + GetFallbackFontFamilies(primary_font.GetFontName()); |
| + std::string best_font = primary_font.GetFontName(); |
| + size_t best_font_missing = -1; |
| + |
| + for (size_t i = 0; i < fonts.size(); ++i) { |
| + ShapeRunWithFont(run, fonts[i]); |
| + size_t current_missing = run->CountMissingGlyphs(); |
| + if (current_missing == 0) |
| + return; |
| + if (current_missing < best_font_missing) { |
| + best_font_missing = current_missing; |
| + best_font = fonts[i]; |
| + } |
| + } |
| + ShapeRunWithFont(run, best_font); |
| +} |
| + |
| +void RenderTextHarfBuzz::ShapeRunWithFont(internal::TextRunHarfBuzz* run, |
| + std::string font) { |
| + const base::string16& text = GetLayoutText(); |
| + run->skia_face = internal::CreateSkiaTypeface(font, run->font_style); |
| hb_font_t* harfbuzz_font = CreateHarfBuzzFont(run->skia_face.get(), |
| run->font_size); |
| @@ -978,21 +997,19 @@ void RenderTextHarfBuzz::ShapeRun(internal::TextRunHarfBuzz* run) { |
| hb_buffer_set_script(buffer, ICUScriptToHBScript(run->script)); |
| hb_buffer_set_direction(buffer, |
| run->is_rtl ? HB_DIRECTION_RTL : HB_DIRECTION_LTR); |
| - // TODO(ckocagil): Should we determine the actual language? |
| - hb_buffer_set_language(buffer, hb_language_get_default()); |
|
msw
2014/07/07 22:52:46
q: why are you removing this? Is |buffer| already
ckocagil
2014/07/12 11:47:54
Added back.
I had no idea what this did, so I dec
|
| + // TODO(ckocagil): Should we call |hb_buffer_set_language()| here? |
| // Shape the text. |
| hb_shape(harfbuzz_font, buffer, NULL, 0); |
| // Populate the run fields with the resulting glyph data in the buffer. |
| - unsigned int glyph_count = 0; |
| - hb_glyph_info_t* infos = hb_buffer_get_glyph_infos(buffer, &glyph_count); |
| - hb_glyph_position_t* hb_positions = hb_buffer_get_glyph_positions(buffer, |
| - NULL); |
| - run->glyph_count = glyph_count; |
| + hb_glyph_info_t* infos = hb_buffer_get_glyph_infos(buffer, &run->glyph_count); |
| + hb_glyph_position_t* hb_positions = |
| + hb_buffer_get_glyph_positions(buffer, NULL); |
| run->glyphs.reset(new uint16[run->glyph_count]); |
| run->glyph_to_char.reset(new uint32[run->glyph_count]); |
| run->positions.reset(new SkPoint[run->glyph_count]); |
| + run->width = 0; |
| for (size_t i = 0; i < run->glyph_count; ++i) { |
| run->glyphs[i] = infos[i].codepoint; |
| run->glyph_to_char[i] = infos[i].cluster; |