OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "chrome/browser/android/vr_shell/textures/ui_texture.h" | 5 #include "chrome/browser/android/vr_shell/textures/ui_texture.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/i18n/char_iterator.h" | 11 #include "base/i18n/char_iterator.h" |
12 #include "base/i18n/rtl.h" | 12 #include "base/i18n/rtl.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "chrome/browser/android/vr_shell/font_fallback.h" |
15 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
| 17 #include "third_party/icu/source/common/unicode/uscript.h" |
16 #include "third_party/skia/include/core/SkCanvas.h" | 18 #include "third_party/skia/include/core/SkCanvas.h" |
17 #include "third_party/skia/include/ports/SkFontMgr.h" | |
18 #include "ui/gfx/canvas.h" | 19 #include "ui/gfx/canvas.h" |
19 #include "ui/gfx/font_list.h" | 20 #include "ui/gfx/font_list.h" |
20 #include "ui/gl/gl_bindings.h" | 21 #include "ui/gl/gl_bindings.h" |
21 | 22 |
22 namespace vr_shell { | 23 namespace vr_shell { |
23 | 24 |
24 namespace { | 25 namespace { |
25 | 26 |
26 static constexpr char kDefaultFontFamily[] = "sans-serif"; | 27 static constexpr char kDefaultFontFamily[] = "sans-serif"; |
27 | 28 |
| 29 std::set<UChar32> CollectDifferentChars(base::string16 text) { |
| 30 std::set<UChar32> characters; |
| 31 for (base::i18n::UTF16CharIterator it(&text); !it.end(); it.Advance()) { |
| 32 characters.insert(it.get()); |
| 33 } |
| 34 return characters; |
| 35 } |
| 36 |
28 } // namespace | 37 } // namespace |
29 | 38 |
30 UiTexture::UiTexture() = default; | 39 UiTexture::UiTexture() = default; |
31 | 40 |
32 UiTexture::~UiTexture() = default; | 41 UiTexture::~UiTexture() = default; |
33 | 42 |
34 void UiTexture::DrawAndLayout(SkCanvas* canvas, const gfx::Size& texture_size) { | 43 void UiTexture::DrawAndLayout(SkCanvas* canvas, const gfx::Size& texture_size) { |
35 canvas->drawColor(SK_ColorTRANSPARENT); | 44 canvas->drawColor(SK_ColorTRANSPARENT); |
36 Draw(canvas, texture_size); | 45 Draw(canvas, texture_size); |
37 } | 46 } |
38 | 47 |
39 bool UiTexture::IsRTL() { | 48 bool UiTexture::IsRTL() { |
40 return base::i18n::IsRTL(); | 49 return base::i18n::IsRTL(); |
41 } | 50 } |
42 | 51 |
43 gfx::FontList UiTexture::GetDefaultFontList(int size) { | 52 gfx::FontList UiTexture::GetDefaultFontList(int size) { |
44 return gfx::FontList(gfx::Font(kDefaultFontFamily, size)); | 53 return gfx::FontList(gfx::Font(kDefaultFontFamily, size)); |
45 } | 54 } |
46 | 55 |
47 gfx::FontList UiTexture::GetFontList(int size, base::string16 text) { | 56 gfx::FontList UiTexture::GetFontList(int size, base::string16 text) { |
48 gfx::Font default_font(kDefaultFontFamily, size); | 57 gfx::Font default_font(kDefaultFontFamily, size); |
49 std::vector<gfx::Font> fonts{default_font}; | 58 std::vector<gfx::Font> fonts{default_font}; |
50 | 59 |
51 std::set<wchar_t> characters; | |
52 for (base::i18n::UTF16CharIterator it(&text); !it.end(); it.Advance()) { | |
53 characters.insert(it.get()); | |
54 } | |
55 // TODO(acondor): Obtain fallback fonts with gfx::GetFallbackFonts | |
56 // (which is not implemented for android yet) in order to avoid | |
57 // querying per character. | |
58 | |
59 sk_sp<SkFontMgr> font_mgr(SkFontMgr::RefDefault()); | |
60 std::set<std::string> names; | 60 std::set<std::string> names; |
61 // TODO(acondor): Query BrowserProcess to obtain the application locale. | 61 // TODO(acondor): Query BrowserProcess to obtain the application locale. |
62 for (wchar_t character : characters) { | 62 for (UChar32 c : CollectDifferentChars(text)) { |
63 sk_sp<SkTypeface> tf(font_mgr->matchFamilyStyleCharacter( | 63 std::string name = GetFallbackFontNameForChar(default_font, c, ""); |
64 kDefaultFontFamily, SkFontStyle(), nullptr, 0, character)); | 64 if (!name.empty()) |
65 | |
66 // TODO(acondor): How should we handle no matching font? | |
67 if (!tf) | |
68 continue; | |
69 SkString sk_name; | |
70 tf->getFamilyName(&sk_name); | |
71 std::string name(sk_name.c_str()); | |
72 if (name != kDefaultFontFamily) | |
73 names.insert(name); | 65 names.insert(name); |
74 } | 66 } |
75 for (const auto& name : names) | 67 for (const auto& name : names) |
76 fonts.push_back(gfx::Font(name, size)); | 68 fonts.push_back(gfx::Font(name, size)); |
77 return gfx::FontList(fonts); | 69 return gfx::FontList(fonts); |
78 } | 70 } |
79 | 71 |
80 } // namespace vr_shell | 72 } // namespace vr_shell |
OLD | NEW |