Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(228)

Unified Diff: ui/gfx/render_text_harfbuzz.cc

Issue 331713003: RenderTextHarfBuzz: Implement font fallback for Win and Linux (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: comments addressed 3 Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/gfx/render_text_harfbuzz.cc
diff --git a/ui/gfx/render_text_harfbuzz.cc b/ui/gfx/render_text_harfbuzz.cc
index 4d902cd87adae62b5cf5c4481288dccb3ecc9823..30889b366daf7b18994d5669923a505c03b2e4d3 100644
--- a/ui/gfx/render_text_harfbuzz.cc
+++ b/ui/gfx/render_text_harfbuzz.cc
@@ -16,9 +16,14 @@
#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/font_render_params.h"
#include "ui/gfx/utf16_indexing.h"
+#if defined(OS_WIN)
+#include "ui/gfx/font_fallback_win.h"
+#endif
+
namespace gfx {
namespace {
@@ -463,14 +468,12 @@ 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;
- for (size_t i = 0; i < glyph_count; ++i) {
- if (glyphs[i] == kMissingGlyphId)
- return true;
- }
- return false;
+ size_t missing = 0;
+ for (size_t i = 0; i < glyph_count; ++i)
+ missing += (glyphs[i] == kMissingGlyphId) ? 1 : 0;
+ return missing;
}
int TextRunHarfBuzz::GetGlyphXBoundary(size_t text_index, bool trailing) const {
@@ -948,13 +951,54 @@ 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);
+ const std::string primary_font_name = primary_font.GetFontName();
run->font_size = primary_font.GetFontSize();
+ // Try shaping with |primary_font|.
+ ShapeRunWithFont(run, primary_font_name);
+ size_t best_font_missing = run->CountMissingGlyphs();
+ if (best_font_missing == 0)
+ return;
+ std::string best_font = primary_font_name;
+
+#if defined(OS_WIN)
+ Font uniscribe_font;
+ if (GetUniscribeFallbackFont(NULL, primary_font,
msw 2014/07/12 22:03:38 Is it okay that we're passing in NULL here?
ckocagil 2014/07/16 23:42:22 It should be okay in the sense that MSDN documents
msw 2014/07/17 00:08:03 Nice.
+ &(GetLayoutText()[run->range.start()]), run->range.length(),
+ &uniscribe_font)) {
+ ShapeRunWithFont(run, uniscribe_font.GetFontName());
+ size_t current_missing = run->CountMissingGlyphs();
+ if (current_missing == 0)
+ return;
+ if (current_missing < best_font_missing) {
+ best_font_missing = current_missing;
+ best_font = uniscribe_font.GetFontName();
+ }
+ }
+#endif
+
+ // Try shaping with the fonts in the fallback list except the first, which is
+ // |primary_font|.
+ std::vector<std::string> fonts = GetFallbackFontFamilies(primary_font_name);
msw 2014/07/12 22:03:38 I think in a followup, we'll want to use the other
+ for (size_t i = 1; 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);
@@ -967,21 +1011,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());
+ // 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;
@@ -989,7 +1031,7 @@ void RenderTextHarfBuzz::ShapeRun(internal::TextRunHarfBuzz* run) {
SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].x_offset));
const int y_offset =
SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].y_offset));
- run->positions[i].set(run->width + x_offset, -y_offset);
+ run->positions[i].set(run->width + x_offset, y_offset);
run->width +=
SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].x_advance));
}

Powered by Google App Engine
This is Rietveld 408576698