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

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: gyp -> gn 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
« no previous file with comments | « ui/gfx/render_text_harfbuzz.h ('k') | ui/gfx/render_text_win.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..a89272f59fc240e977845b87898d6320681a8e4d 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;
+ const base::char16* run_text = &(GetLayoutText()[run->range.start()]);
+ if (GetUniscribeFallbackFont(primary_font, run_text, 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);
+ 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,
+ const std::string& font_family) {
+ const base::string16& text = GetLayoutText();
+ run->skia_face = internal::CreateSkiaTypeface(font_family, run->font_style);
hb_font_t* harfbuzz_font = CreateHarfBuzzFont(run->skia_face.get(),
run->font_size);
@@ -967,8 +1011,7 @@ 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);
@@ -976,12 +1019,13 @@ void RenderTextHarfBuzz::ShapeRun(internal::TextRunHarfBuzz* run) {
// 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_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 +1033,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);
ebrahimgnu 2014/07/19 21:02:43 Seems this caused regression of https://code.googl
msw 2014/07/19 23:31:41 I'll fix this in https://codereview.chromium.org/4
run->width +=
SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].x_advance));
}
« no previous file with comments | « ui/gfx/render_text_harfbuzz.h ('k') | ui/gfx/render_text_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698