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/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
16 #include "third_party/icu/source/common/unicode/uscript.h" | |
16 #include "third_party/skia/include/core/SkCanvas.h" | 17 #include "third_party/skia/include/core/SkCanvas.h" |
17 #include "third_party/skia/include/ports/SkFontMgr.h" | |
18 #include "ui/gfx/canvas.h" | 18 #include "ui/gfx/canvas.h" |
19 #include "ui/gfx/font_fallback_android.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 { |
Alexei Svitkine (slow)
2017/05/09 14:52:16
Nit: Add an empty line after this now that the blo
acondor_
2017/05/09 16:21:51
Done.
| |
25 constexpr char kDefaultFontFamily[] = "sans-serif"; | 26 constexpr char kDefaultFontFamily[] = "sans-serif"; |
27 | |
28 std::set<UChar32> CollectDifferentChars(base::string16 text) { | |
29 std::set<UChar32> characters; | |
30 for (base::i18n::UTF16CharIterator it(&text); !it.end(); it.Advance()) { | |
31 characters.insert(it.get()); | |
32 } | |
33 return characters; | |
34 } | |
35 | |
26 } // namespace | 36 } // namespace |
27 | 37 |
28 UiTexture::UiTexture() = default; | 38 UiTexture::UiTexture() = default; |
29 | 39 |
30 UiTexture::~UiTexture() = default; | 40 UiTexture::~UiTexture() = default; |
31 | 41 |
32 void UiTexture::DrawAndLayout(SkCanvas* canvas, const gfx::Size& texture_size) { | 42 void UiTexture::DrawAndLayout(SkCanvas* canvas, const gfx::Size& texture_size) { |
33 cc::SkiaPaintCanvas paint_canvas(canvas); | 43 cc::SkiaPaintCanvas paint_canvas(canvas); |
34 gfx::Canvas gfx_canvas(&paint_canvas, 1.0f); | 44 gfx::Canvas gfx_canvas(&paint_canvas, 1.0f); |
45 | |
Alexei Svitkine (slow)
2017/05/09 14:52:16
Nit: Remove extra unnecessary change
acondor_
2017/05/09 16:21:51
Done.
| |
35 gfx_canvas.DrawColor(SK_ColorTRANSPARENT); | 46 gfx_canvas.DrawColor(SK_ColorTRANSPARENT); |
36 Draw(&gfx_canvas, texture_size); | 47 Draw(&gfx_canvas, texture_size); |
37 } | 48 } |
38 | 49 |
39 bool UiTexture::IsRTL() { | 50 bool UiTexture::IsRTL() { |
40 return base::i18n::IsRTL(); | 51 return base::i18n::IsRTL(); |
41 } | 52 } |
42 | 53 |
43 gfx::FontList UiTexture::GetFontList(int size, base::string16 text) { | 54 gfx::FontList UiTexture::GetFontList(int size, base::string16 text) { |
44 gfx::Font default_font(kDefaultFontFamily, size); | 55 gfx::Font default_font(kDefaultFontFamily, size); |
45 std::vector<gfx::Font> fonts{default_font}; | 56 std::vector<gfx::Font> fonts{default_font}; |
46 | 57 |
47 std::set<wchar_t> characters; | |
48 for (base::i18n::UTF16CharIterator it(&text); !it.end(); it.Advance()) { | |
49 characters.insert(it.get()); | |
50 } | |
51 // TODO(acondor): Obtain fallback fonts with gfx::GetFallbackFonts | |
52 // (which is not implemented for android yet) in order to avoid | |
53 // querying per character. | |
54 | |
55 sk_sp<SkFontMgr> font_mgr(SkFontMgr::RefDefault()); | |
56 std::set<std::string> names; | 58 std::set<std::string> names; |
57 // TODO(acondor): Query BrowserProcess to obtain the application locale. | 59 // TODO(acondor): Query BrowserProcess to obtain the application locale. |
58 for (wchar_t character : characters) { | 60 for (UChar32 c : CollectDifferentChars(text)) { |
59 sk_sp<SkTypeface> tf(font_mgr->matchFamilyStyleCharacter( | 61 std::string name = gfx::GetFallbackFontNameForChar(default_font, c, ""); |
60 kDefaultFontFamily, SkFontStyle(), nullptr, 0, character)); | 62 if (!name.empty()) |
61 | |
62 // TODO(acondor): How should we handle no matching font? | |
63 if (!tf) | |
64 continue; | |
65 SkString sk_name; | |
66 tf->getFamilyName(&sk_name); | |
67 std::string name(sk_name.c_str()); | |
68 if (name != kDefaultFontFamily) | |
69 names.insert(name); | 63 names.insert(name); |
70 } | 64 } |
71 for (const auto& name : names) | 65 for (const auto& name : names) |
72 fonts.push_back(gfx::Font(name, size)); | 66 fonts.push_back(gfx::Font(name, size)); |
73 return gfx::FontList(fonts); | 67 return gfx::FontList(fonts); |
74 } | 68 } |
75 | 69 |
76 } // namespace vr_shell | 70 } // namespace vr_shell |
OLD | NEW |