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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
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) {
951 const base::string16& text = GetLayoutText();
952 // TODO(ckocagil|yukishiino): Implement font fallback.
953 const Font& primary_font = font_list().GetPrimaryFont(); 954 const Font& primary_font = font_list().GetPrimaryFont();
954 run->skia_face = internal::CreateSkiaTypeface(primary_font.GetFontName(), 955 const std::string primary_font_name = primary_font.GetFontName();
955 run->font_style);
956 run->font_size = primary_font.GetFontSize(); 956 run->font_size = primary_font.GetFontSize();
957 957
958 // Try shaping with |primary_font|.
959 ShapeRunWithFont(run, primary_font_name);
960 size_t best_font_missing = run->CountMissingGlyphs();
961 if (best_font_missing == 0)
962 return;
963 std::string best_font = primary_font_name;
964
965 #if defined(OS_WIN)
966 Font uniscribe_font;
967 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.
968 &(GetLayoutText()[run->range.start()]), run->range.length(),
969 &uniscribe_font)) {
970 ShapeRunWithFont(run, uniscribe_font.GetFontName());
971 size_t current_missing = run->CountMissingGlyphs();
972 if (current_missing == 0)
973 return;
974 if (current_missing < best_font_missing) {
975 best_font_missing = current_missing;
976 best_font = uniscribe_font.GetFontName();
977 }
978 }
979 #endif
980
981 // Try shaping with the fonts in the fallback list except the first, which is
982 // |primary_font|.
983 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
984 for (size_t i = 1; i < fonts.size(); ++i) {
985 ShapeRunWithFont(run, fonts[i]);
986 size_t current_missing = run->CountMissingGlyphs();
987 if (current_missing == 0)
988 return;
989 if (current_missing < best_font_missing) {
990 best_font_missing = current_missing;
991 best_font = fonts[i];
992 }
993 }
994
995 ShapeRunWithFont(run, best_font);
996 }
997
998 void RenderTextHarfBuzz::ShapeRunWithFont(internal::TextRunHarfBuzz* run,
999 std::string font) {
1000 const base::string16& text = GetLayoutText();
1001 run->skia_face = internal::CreateSkiaTypeface(font, run->font_style);
958 hb_font_t* harfbuzz_font = CreateHarfBuzzFont(run->skia_face.get(), 1002 hb_font_t* harfbuzz_font = CreateHarfBuzzFont(run->skia_face.get(),
959 run->font_size); 1003 run->font_size);
960 1004
961 // Create a HarfBuzz buffer and add the string to be shaped. The HarfBuzz 1005 // 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, 1006 // buffer holds our text, run information to be used by the shaping engine,
963 // and the resulting glyph data. 1007 // and the resulting glyph data.
964 hb_buffer_t* buffer = hb_buffer_create(); 1008 hb_buffer_t* buffer = hb_buffer_create();
965 hb_buffer_add_utf16(buffer, reinterpret_cast<const uint16*>(text.c_str()), 1009 hb_buffer_add_utf16(buffer, reinterpret_cast<const uint16*>(text.c_str()),
966 text.length(), run->range.start(), run->range.length()); 1010 text.length(), run->range.start(), run->range.length());
967 hb_buffer_set_script(buffer, ICUScriptToHBScript(run->script)); 1011 hb_buffer_set_script(buffer, ICUScriptToHBScript(run->script));
968 hb_buffer_set_direction(buffer, 1012 hb_buffer_set_direction(buffer,
969 run->is_rtl ? HB_DIRECTION_RTL : HB_DIRECTION_LTR); 1013 run->is_rtl ? HB_DIRECTION_RTL : HB_DIRECTION_LTR);
970 // TODO(ckocagil): Should we determine the actual language? 1014 // TODO(ckocagil): Should we call |hb_buffer_set_language()| here?
971 hb_buffer_set_language(buffer, hb_language_get_default());
972 1015
973 // Shape the text. 1016 // Shape the text.
974 hb_shape(harfbuzz_font, buffer, NULL, 0); 1017 hb_shape(harfbuzz_font, buffer, NULL, 0);
975 1018
976 // Populate the run fields with the resulting glyph data in the buffer. 1019 // Populate the run fields with the resulting glyph data in the buffer.
977 unsigned int glyph_count = 0; 1020 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); 1021 hb_glyph_position_t* hb_positions =
979 hb_glyph_position_t* hb_positions = hb_buffer_get_glyph_positions(buffer, 1022 hb_buffer_get_glyph_positions(buffer, NULL);
980 NULL);
981 run->glyph_count = glyph_count;
982 run->glyphs.reset(new uint16[run->glyph_count]); 1023 run->glyphs.reset(new uint16[run->glyph_count]);
983 run->glyph_to_char.reset(new uint32[run->glyph_count]); 1024 run->glyph_to_char.reset(new uint32[run->glyph_count]);
984 run->positions.reset(new SkPoint[run->glyph_count]); 1025 run->positions.reset(new SkPoint[run->glyph_count]);
1026 run->width = 0;
985 for (size_t i = 0; i < run->glyph_count; ++i) { 1027 for (size_t i = 0; i < run->glyph_count; ++i) {
986 run->glyphs[i] = infos[i].codepoint; 1028 run->glyphs[i] = infos[i].codepoint;
987 run->glyph_to_char[i] = infos[i].cluster; 1029 run->glyph_to_char[i] = infos[i].cluster;
988 const int x_offset = 1030 const int x_offset =
989 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].x_offset)); 1031 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].x_offset));
990 const int y_offset = 1032 const int y_offset =
991 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].y_offset)); 1033 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].y_offset));
992 run->positions[i].set(run->width + x_offset, -y_offset); 1034 run->positions[i].set(run->width + x_offset, y_offset);
993 run->width += 1035 run->width +=
994 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].x_advance)); 1036 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].x_advance));
995 } 1037 }
996 1038
997 hb_buffer_destroy(buffer); 1039 hb_buffer_destroy(buffer);
998 hb_font_destroy(harfbuzz_font); 1040 hb_font_destroy(harfbuzz_font);
999 } 1041 }
1000 1042
1001 } // namespace gfx 1043 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698