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

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 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
« no previous file with comments | « ui/gfx/render_text_harfbuzz.h ('k') | ui/gfx/render_text_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/utf16_indexing.h" 20 #include "ui/gfx/utf16_indexing.h"
20 21
21 #if defined(OS_WIN) 22 #if defined(OS_WIN)
22 #include "ui/gfx/font_smoothing_win.h" 23 #include "ui/gfx/font_smoothing_win.h"
23 #endif 24 #endif
24 25
25 namespace gfx { 26 namespace gfx {
26 27
27 namespace { 28 namespace {
28 29
(...skipping 431 matching lines...) Expand 10 before | Expand all | Expand 10 after
460 461
461 first = CharToGlyph(char_range.start()); 462 first = CharToGlyph(char_range.start());
462 for (size_t i = char_range.end(); i < range.end(); ++i) { 463 for (size_t i = char_range.end(); i < range.end(); ++i) {
463 last = CharToGlyph(i); 464 last = CharToGlyph(i);
464 if (first != last) 465 if (first != last)
465 return Range(first, last); 466 return Range(first, last);
466 } 467 }
467 return Range(first, glyph_count); 468 return Range(first, glyph_count);
468 } 469 }
469 470
470 // Returns whether the given shaped run contains any missing glyphs. 471 size_t TextRunHarfBuzz::CountMissingGlyphs() const {
471 bool TextRunHarfBuzz::HasMissingGlyphs() const {
472 static const int kMissingGlyphId = 0; 472 static const int kMissingGlyphId = 0;
473 size_t missing = 0;
473 for (size_t i = 0; i < glyph_count; ++i) { 474 for (size_t i = 0; i < glyph_count; ++i) {
474 if (glyphs[i] == kMissingGlyphId) 475 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.
475 return true; 476 ++missing;
476 } 477 }
477 return false; 478 return missing;
478 } 479 }
479 480
480 int TextRunHarfBuzz::GetGlyphXBoundary(size_t text_index, bool trailing) const { 481 int TextRunHarfBuzz::GetGlyphXBoundary(size_t text_index, bool trailing) const {
481 if (text_index == range.end()) { 482 if (text_index == range.end()) {
482 trailing = true; 483 trailing = true;
483 --text_index; 484 --text_index;
484 } 485 }
485 Range glyph_range = CharRangeToGlyphRange(Range(text_index, text_index + 1)); 486 Range glyph_range = CharRangeToGlyphRange(Range(text_index, text_index + 1));
486 const size_t glyph_pos = (is_rtl == trailing) ? 487 const size_t glyph_pos = (is_rtl == trailing) ?
487 glyph_range.start() : glyph_range.end(); 488 glyph_range.start() : glyph_range.end();
(...skipping 464 matching lines...) Expand 10 before | Expand all | Expand 10 after
952 std::vector<UBiDiLevel> levels(num_runs); 953 std::vector<UBiDiLevel> levels(num_runs);
953 for (size_t i = 0; i < num_runs; ++i) 954 for (size_t i = 0; i < num_runs; ++i)
954 levels[i] = runs_[i]->level; 955 levels[i] = runs_[i]->level;
955 visual_to_logical_.resize(num_runs); 956 visual_to_logical_.resize(num_runs);
956 ubidi_reorderVisual(&levels[0], num_runs, &visual_to_logical_[0]); 957 ubidi_reorderVisual(&levels[0], num_runs, &visual_to_logical_[0]);
957 logical_to_visual_.resize(num_runs); 958 logical_to_visual_.resize(num_runs);
958 ubidi_reorderLogical(&levels[0], num_runs, &logical_to_visual_[0]); 959 ubidi_reorderLogical(&levels[0], num_runs, &logical_to_visual_[0]);
959 } 960 }
960 961
961 void RenderTextHarfBuzz::ShapeRun(internal::TextRunHarfBuzz* run) { 962 void RenderTextHarfBuzz::ShapeRun(internal::TextRunHarfBuzz* run) {
963 const Font& primary_font = font_list().GetPrimaryFont();
964 run->font_size = primary_font.GetFontSize();
965 std::vector<std::string> fonts =
966 GetFallbackFontFamilies(primary_font.GetFontName());
967 std::string best_font = primary_font.GetFontName();
968 size_t best_font_missing = -1;
969
970 for (size_t i = 0; i < fonts.size(); ++i) {
971 ShapeRunWithFont(run, fonts[i]);
972 size_t current_missing = run->CountMissingGlyphs();
973 if (current_missing == 0)
974 return;
975 if (current_missing < best_font_missing) {
976 best_font_missing = current_missing;
977 best_font = fonts[i];
978 }
979 }
980
981 ShapeRunWithFont(run, best_font);
982 }
983
984 void RenderTextHarfBuzz::ShapeRunWithFont(internal::TextRunHarfBuzz* run,
985 std::string font) {
962 const base::string16& text = GetLayoutText(); 986 const base::string16& text = GetLayoutText();
963 // TODO(ckocagil|yukishiino): Implement font fallback. 987 run->skia_face = internal::CreateSkiaTypeface(font, run->font_style);
964 const Font& primary_font = font_list().GetPrimaryFont();
965 run->skia_face = internal::CreateSkiaTypeface(primary_font.GetFontName(),
966 run->font_style);
967 run->font_size = primary_font.GetFontSize();
968
969 hb_font_t* harfbuzz_font = CreateHarfBuzzFont(run->skia_face.get(), 988 hb_font_t* harfbuzz_font = CreateHarfBuzzFont(run->skia_face.get(),
970 run->font_size); 989 run->font_size);
971 990
972 // Create a HarfBuzz buffer and add the string to be shaped. The HarfBuzz 991 // Create a HarfBuzz buffer and add the string to be shaped. The HarfBuzz
973 // buffer holds our text, run information to be used by the shaping engine, 992 // buffer holds our text, run information to be used by the shaping engine,
974 // and the resulting glyph data. 993 // and the resulting glyph data.
975 hb_buffer_t* buffer = hb_buffer_create(); 994 hb_buffer_t* buffer = hb_buffer_create();
976 hb_buffer_add_utf16(buffer, reinterpret_cast<const uint16*>(text.c_str()), 995 hb_buffer_add_utf16(buffer, reinterpret_cast<const uint16*>(text.c_str()),
977 text.length(), run->range.start(), run->range.length()); 996 text.length(), run->range.start(), run->range.length());
978 hb_buffer_set_script(buffer, ICUScriptToHBScript(run->script)); 997 hb_buffer_set_script(buffer, ICUScriptToHBScript(run->script));
979 hb_buffer_set_direction(buffer, 998 hb_buffer_set_direction(buffer,
980 run->is_rtl ? HB_DIRECTION_RTL : HB_DIRECTION_LTR); 999 run->is_rtl ? HB_DIRECTION_RTL : HB_DIRECTION_LTR);
981 // TODO(ckocagil): Should we determine the actual language? 1000 // TODO(ckocagil): Should we call |hb_buffer_set_language()| here?
982 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
983 1001
984 // Shape the text. 1002 // Shape the text.
985 hb_shape(harfbuzz_font, buffer, NULL, 0); 1003 hb_shape(harfbuzz_font, buffer, NULL, 0);
986 1004
987 // Populate the run fields with the resulting glyph data in the buffer. 1005 // Populate the run fields with the resulting glyph data in the buffer.
988 unsigned int glyph_count = 0; 1006 hb_glyph_info_t* infos = hb_buffer_get_glyph_infos(buffer, &run->glyph_count);
989 hb_glyph_info_t* infos = hb_buffer_get_glyph_infos(buffer, &glyph_count); 1007 hb_glyph_position_t* hb_positions =
990 hb_glyph_position_t* hb_positions = hb_buffer_get_glyph_positions(buffer, 1008 hb_buffer_get_glyph_positions(buffer, NULL);
991 NULL);
992 run->glyph_count = glyph_count;
993 run->glyphs.reset(new uint16[run->glyph_count]); 1009 run->glyphs.reset(new uint16[run->glyph_count]);
994 run->glyph_to_char.reset(new uint32[run->glyph_count]); 1010 run->glyph_to_char.reset(new uint32[run->glyph_count]);
995 run->positions.reset(new SkPoint[run->glyph_count]); 1011 run->positions.reset(new SkPoint[run->glyph_count]);
1012 run->width = 0;
996 for (size_t i = 0; i < run->glyph_count; ++i) { 1013 for (size_t i = 0; i < run->glyph_count; ++i) {
997 run->glyphs[i] = infos[i].codepoint; 1014 run->glyphs[i] = infos[i].codepoint;
998 run->glyph_to_char[i] = infos[i].cluster; 1015 run->glyph_to_char[i] = infos[i].cluster;
999 const int x_offset = 1016 const int x_offset =
1000 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].x_offset)); 1017 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].x_offset));
1001 const int y_offset = 1018 const int y_offset =
1002 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].y_offset)); 1019 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].y_offset));
1003 run->positions[i].set(run->width + x_offset, y_offset); 1020 run->positions[i].set(run->width + x_offset, y_offset);
1004 run->width += 1021 run->width +=
1005 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].x_advance)); 1022 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].x_advance));
1006 } 1023 }
1007 1024
1008 hb_buffer_destroy(buffer); 1025 hb_buffer_destroy(buffer);
1009 hb_font_destroy(harfbuzz_font); 1026 hb_font_destroy(harfbuzz_font);
1010 } 1027 }
1011 1028
1012 } // namespace gfx 1029 } // namespace gfx
OLDNEW
« 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