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 <string> | 8 #include <string> |
8 #include <vector> | 9 #include <vector> |
9 | 10 |
| 11 #include "base/i18n/char_iterator.h" |
| 12 #include "base/i18n/rtl.h" |
10 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
11 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "chrome/browser/browser_process.h" |
12 #include "third_party/skia/include/core/SkCanvas.h" | 16 #include "third_party/skia/include/core/SkCanvas.h" |
| 17 #include "third_party/skia/include/ports/SkFontMgr.h" |
13 #include "ui/gfx/canvas.h" | 18 #include "ui/gfx/canvas.h" |
| 19 #include "ui/gfx/font_list.h" |
14 #include "ui/gl/gl_bindings.h" | 20 #include "ui/gl/gl_bindings.h" |
15 | 21 |
16 namespace vr_shell { | 22 namespace vr_shell { |
17 | 23 |
| 24 namespace { |
| 25 constexpr char kDefaultFontFamily[] = "sans-serif"; |
| 26 } // namespace |
| 27 |
18 // TODO(acondor): Create non-square textures to reduce memory usage. | 28 // TODO(acondor): Create non-square textures to reduce memory usage. |
19 UITexture::UITexture(int texture_handle, int texture_size) | 29 UITexture::UITexture(int texture_handle, int texture_size) |
20 : texture_handle_(texture_handle), | 30 : texture_handle_(texture_handle), |
21 texture_size_(texture_size), | 31 texture_size_(texture_size), |
22 surface_(SkSurface::MakeRasterN32Premul(texture_size_, texture_size_)) {} | 32 surface_(SkSurface::MakeRasterN32Premul(texture_size_, texture_size_)) {} |
23 | 33 |
24 UITexture::~UITexture() = default; | 34 UITexture::~UITexture() = default; |
25 | 35 |
26 void UITexture::DrawAndLayout() { | 36 void UITexture::DrawAndLayout() { |
27 cc::SkiaPaintCanvas paint_canvas(surface_->getCanvas()); | 37 cc::SkiaPaintCanvas paint_canvas(surface_->getCanvas()); |
(...skipping 11 matching lines...) Expand all Loading... |
39 | 49 |
40 glBindTexture(GL_TEXTURE_2D, texture_handle_); | 50 glBindTexture(GL_TEXTURE_2D, texture_handle_); |
41 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pixmap.width(), pixmap.height(), 0, | 51 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, pixmap.width(), pixmap.height(), 0, |
42 GL_RGBA, GL_UNSIGNED_BYTE, pixmap.addr()); | 52 GL_RGBA, GL_UNSIGNED_BYTE, pixmap.addr()); |
43 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 53 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
44 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 54 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
45 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | 55 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
46 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 56 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
47 } | 57 } |
48 | 58 |
| 59 bool UITexture::IsRTL() { |
| 60 return base::i18n::IsRTL(); |
| 61 } |
| 62 |
| 63 gfx::FontList UITexture::GetFontList(int size, base::string16 text) { |
| 64 gfx::Font default_font(kDefaultFontFamily, size); |
| 65 std::vector<gfx::Font> fonts{default_font}; |
| 66 |
| 67 // TODO(acondor): Obtain fallback fonts with gfx::GetFallbackFonts |
| 68 // (which is not implemented for android yet) in order to avoid |
| 69 // querying per character. |
| 70 |
| 71 sk_sp<SkFontMgr> font_mgr(SkFontMgr::RefDefault()); |
| 72 std::set<std::string> names; |
| 73 // TODO(acondor): Query BrowserProcess to obtain the application locale. |
| 74 for (base::i18n::UTF16CharIterator it(&text); !it.end(); it.Advance()) { |
| 75 sk_sp<SkTypeface> tf(font_mgr->matchFamilyStyleCharacter( |
| 76 default_font.GetFontName().c_str(), SkFontStyle(), nullptr, 0, |
| 77 it.get())); |
| 78 SkString sk_name; |
| 79 tf->getFamilyName(&sk_name); |
| 80 std::string name(sk_name.c_str()); |
| 81 if (name != kDefaultFontFamily) |
| 82 names.insert(name); |
| 83 } |
| 84 for (const auto& name : names) |
| 85 fonts.push_back(gfx::Font(name, size)); |
| 86 return gfx::FontList(fonts); |
| 87 } |
| 88 |
49 } // namespace vr_shell | 89 } // namespace vr_shell |
OLD | NEW |