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

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: fix tests; add placeholder impl for Mac Created 6 years, 6 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/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 924 matching lines...) Expand 10 before | Expand all | Expand 10 after
953 visual_to_logical_.resize(num_runs); 954 visual_to_logical_.resize(num_runs);
954 ubidi_reorderVisual(&levels[0], num_runs, &visual_to_logical_[0]); 955 ubidi_reorderVisual(&levels[0], num_runs, &visual_to_logical_[0]);
955 logical_to_visual_.resize(num_runs); 956 logical_to_visual_.resize(num_runs);
956 ubidi_reorderLogical(&levels[0], num_runs, &logical_to_visual_[0]); 957 ubidi_reorderLogical(&levels[0], num_runs, &logical_to_visual_[0]);
957 } 958 }
958 959
959 void RenderTextHarfBuzz::ShapeRun(internal::TextRunHarfBuzz* run) { 960 void RenderTextHarfBuzz::ShapeRun(internal::TextRunHarfBuzz* run) {
960 const base::string16& text = GetLayoutText(); 961 const base::string16& text = GetLayoutText();
961 // TODO(ckocagil|yukishiino): Implement font fallback. 962 // TODO(ckocagil|yukishiino): Implement font fallback.
962 const Font& primary_font = font_list().GetPrimaryFont(); 963 const Font& primary_font = font_list().GetPrimaryFont();
963 run->skia_face = internal::CreateSkiaTypeface(primary_font.GetFontName(),
964 run->font_style);
965 run->font_size = primary_font.GetFontSize(); 964 run->font_size = primary_font.GetFontSize();
965 std::vector<std::string> fonts =
966 GetFallbackFontFamilies(primary_font.GetFontName());
966 967
967 hb_font_t* harfbuzz_font = CreateHarfBuzzFont(run->skia_face.get(), 968 for (size_t i = 0; !shaped && i < fonts.size(); ++i) {
968 run->font_size); 969 run->skia_face = internal::CreateSkiaTypeface(fonts[i], run->font_style);
969 970
970 // Create a HarfBuzz buffer and add the string to be shaped. The HarfBuzz 971 hb_font_t* harfbuzz_font = CreateHarfBuzzFont(run->skia_face.get(),
971 // buffer holds our text, run information to be used by the shaping engine, 972 run->font_size);
972 // and the resulting glyph data.
973 hb_buffer_t* buffer = hb_buffer_create();
974 hb_buffer_add_utf16(buffer, reinterpret_cast<const uint16*>(text.c_str()),
975 text.length(), run->range.start(), run->range.length());
976 hb_buffer_set_script(buffer, ICUScriptToHBScript(run->script));
977 hb_buffer_set_direction(buffer,
978 run->is_rtl ? HB_DIRECTION_RTL : HB_DIRECTION_LTR);
979 // TODO(ckocagil): Should we determine the actual language?
980 hb_buffer_set_language(buffer, hb_language_get_default());
981 973
982 // Shape the text. 974 // Create a HarfBuzz buffer and add the string to be shaped. The HarfBuzz
983 hb_shape(harfbuzz_font, buffer, NULL, 0); 975 // buffer holds our text, run information to be used by the shaping engine,
976 // and the resulting glyph data.
977 hb_buffer_t* buffer = hb_buffer_create();
978 hb_buffer_add_utf16(buffer, reinterpret_cast<const uint16*>(text.c_str()),
979 text.length(), run->range.start(), run->range.length());
980 hb_buffer_set_script(buffer, ICUScriptToHBScript(run->script));
981 hb_buffer_set_direction(buffer,
982 run->is_rtl ? HB_DIRECTION_RTL : HB_DIRECTION_LTR);
983 // TODO(ckocagil): Should we determine the actual language?
984 hb_buffer_set_language(buffer, hb_language_get_default());
984 985
985 // Populate the run fields with the resulting glyph data in the buffer. 986 // Shape the text.
986 unsigned int glyph_count = 0; 987 hb_shape(harfbuzz_font, buffer, NULL, 0);
987 hb_glyph_info_t* infos = hb_buffer_get_glyph_infos(buffer, &glyph_count); 988
988 hb_glyph_position_t* hb_positions = hb_buffer_get_glyph_positions(buffer, 989 // Populate the run fields with the resulting glyph data in the buffer.
989 NULL); 990 unsigned int glyph_count = 0;
990 run->glyph_count = glyph_count; 991 hb_glyph_info_t* infos = hb_buffer_get_glyph_infos(buffer, &glyph_count);
991 run->glyphs.reset(new uint16[run->glyph_count]); 992 hb_glyph_position_t* hb_positions = hb_buffer_get_glyph_positions(buffer,
992 run->glyph_to_char.reset(new uint32[run->glyph_count]); 993 NULL);
993 run->positions.reset(new SkPoint[run->glyph_count]); 994 run->glyph_count = glyph_count;
994 for (size_t i = 0; i < run->glyph_count; ++i) { 995 run->glyphs.reset(new uint16[run->glyph_count]);
995 run->glyphs[i] = infos[i].codepoint; 996 run->glyph_to_char.reset(new uint32[run->glyph_count]);
996 run->glyph_to_char[i] = infos[i].cluster; 997 run->positions.reset(new SkPoint[run->glyph_count]);
997 const int x_offset = 998 run->width = 0;
998 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].x_offset)); 999 for (size_t i = 0; i < run->glyph_count; ++i) {
999 const int y_offset = 1000 run->glyphs[i] = infos[i].codepoint;
1000 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].y_offset)); 1001 run->glyph_to_char[i] = infos[i].cluster;
1001 run->positions[i].set(run->width + x_offset, y_offset); 1002 const int x_offset =
1002 run->width += 1003 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].x_offset));
1003 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].x_advance)); 1004 const int y_offset =
1005 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].y_offset));
1006 run->positions[i].set(run->width + x_offset, y_offset);
1007 run->width +=
1008 SkScalarRoundToInt(SkFixedToScalar(hb_positions[i].x_advance));
1009 }
1010
1011 hb_buffer_destroy(buffer);
1012 hb_font_destroy(harfbuzz_font);
1013
1014 if (!run->HasMissingGlyphs())
msw 2014/06/30 17:35:54 Do we want something like RenderTextWin's best_par
ckocagil 2014/07/04 15:30:16 Done.
1015 return;
1004 } 1016 }
1005
1006 hb_buffer_destroy(buffer);
1007 hb_font_destroy(harfbuzz_font);
1008 } 1017 }
1009 1018
1010 } // namespace gfx 1019 } // namespace gfx
OLDNEW
« ui/gfx/font_fallback_mac.cc ('K') | « ui/gfx/gfx.gyp ('k') | ui/gfx/render_text_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698