Chromium Code Reviews| 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 "base/trace_event/trace_event.h" | |
| 15 #include "chrome/browser/android/vr_shell/font_fallback.h" | 16 #include "chrome/browser/android/vr_shell/font_fallback.h" |
| 16 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
| 17 #include "third_party/icu/source/common/unicode/uscript.h" | 18 #include "third_party/icu/source/common/unicode/uscript.h" |
| 18 #include "third_party/skia/include/core/SkCanvas.h" | 19 #include "third_party/skia/include/core/SkCanvas.h" |
| 19 #include "ui/gfx/canvas.h" | 20 #include "ui/gfx/canvas.h" |
| 20 #include "ui/gfx/font_list.h" | 21 #include "ui/gfx/font_list.h" |
| 22 #include "ui/gfx/render_text.h" | |
| 23 #include "ui/gfx/text_elider.h" | |
| 21 #include "ui/gl/gl_bindings.h" | 24 #include "ui/gl/gl_bindings.h" |
| 22 | 25 |
| 23 namespace vr_shell { | 26 namespace vr_shell { |
| 24 | 27 |
| 25 namespace { | 28 namespace { |
| 26 | 29 |
| 27 static constexpr char kDefaultFontFamily[] = "sans-serif"; | 30 static constexpr char kDefaultFontFamily[] = "sans-serif"; |
| 28 | 31 |
| 32 std::unique_ptr<gfx::RenderText> CreateRenderText( | |
| 33 const base::string16& text, | |
| 34 const gfx::FontList& font_list, | |
| 35 SkColor color, | |
| 36 int flags) { | |
| 37 std::unique_ptr<gfx::RenderText> render_text( | |
| 38 gfx::RenderText::CreateInstance()); | |
| 39 render_text->SetText(text); | |
| 40 render_text->SetFontList(font_list); | |
| 41 render_text->SetColor(color); | |
| 42 | |
| 43 if (flags & UiTexture::TEXT_ALIGN_LEFT) | |
| 44 render_text->SetHorizontalAlignment(gfx::ALIGN_LEFT); | |
| 45 else if (flags & UiTexture::TEXT_ALIGN_RIGHT) | |
| 46 render_text->SetHorizontalAlignment(gfx::ALIGN_RIGHT); | |
| 47 else if (flags & UiTexture::TEXT_ALIGN_CENTER) | |
| 48 render_text->SetHorizontalAlignment(gfx::ALIGN_CENTER); | |
| 49 | |
| 50 const int font_style = font_list.GetFontStyle(); | |
| 51 render_text->SetStyle(gfx::ITALIC, (font_style & gfx::Font::ITALIC) != 0); | |
| 52 render_text->SetStyle(gfx::UNDERLINE, | |
| 53 (font_style & gfx::Font::UNDERLINE) != 0); | |
| 54 render_text->SetWeight(font_list.GetFontWeight()); | |
| 55 | |
| 56 return render_text; | |
| 57 } | |
| 58 | |
| 29 std::set<UChar32> CollectDifferentChars(base::string16 text) { | 59 std::set<UChar32> CollectDifferentChars(base::string16 text) { |
| 30 std::set<UChar32> characters; | 60 std::set<UChar32> characters; |
| 31 for (base::i18n::UTF16CharIterator it(&text); !it.end(); it.Advance()) { | 61 for (base::i18n::UTF16CharIterator it(&text); !it.end(); it.Advance()) { |
| 32 characters.insert(it.get()); | 62 characters.insert(it.get()); |
| 33 } | 63 } |
| 34 return characters; | 64 return characters; |
| 35 } | 65 } |
| 36 | 66 |
| 37 } // namespace | 67 } // namespace |
| 38 | 68 |
| 39 UiTexture::UiTexture() = default; | 69 UiTexture::UiTexture() = default; |
| 40 | 70 |
| 41 UiTexture::~UiTexture() = default; | 71 UiTexture::~UiTexture() = default; |
| 42 | 72 |
| 43 void UiTexture::DrawAndLayout(SkCanvas* canvas, const gfx::Size& texture_size) { | 73 void UiTexture::DrawAndLayout(SkCanvas* canvas, const gfx::Size& texture_size) { |
| 74 TRACE_EVENT0("gpu", "UiTexture::DrawAndLayout"); | |
| 44 canvas->drawColor(SK_ColorTRANSPARENT); | 75 canvas->drawColor(SK_ColorTRANSPARENT); |
| 45 Draw(canvas, texture_size); | 76 Draw(canvas, texture_size); |
| 46 } | 77 } |
| 47 | 78 |
| 79 std::vector<std::unique_ptr<gfx::RenderText>> UiTexture::PrepareDrawStringRect( | |
| 80 const base::string16& text, | |
| 81 const gfx::FontList& font_list, | |
| 82 SkColor color, | |
| 83 gfx::Rect* bounds, | |
| 84 int flags) { | |
| 85 DCHECK(bounds); | |
| 86 | |
| 87 std::vector<std::unique_ptr<gfx::RenderText>> lines; | |
| 88 gfx::Rect rect(*bounds); | |
| 89 | |
| 90 if (flags & MULTI_LINE) { | |
| 91 std::vector<base::string16> strings; | |
| 92 gfx::ElideRectangleText(text, font_list, bounds->width(), | |
| 93 bounds->height() ? bounds->height() : INT_MAX, | |
| 94 gfx::WRAP_LONG_WORDS, &strings); | |
| 95 | |
| 96 int height = 0; | |
| 97 int line_height = 0; | |
| 98 for (size_t i = 0; i < strings.size(); i++) { | |
| 99 std::unique_ptr<gfx::RenderText> render_text = | |
| 100 CreateRenderText(strings[i], font_list, color, flags); | |
| 101 | |
| 102 if (i == 0) { | |
| 103 // Measure line and center text vertically. | |
| 104 line_height = render_text->GetStringSize().height(); | |
| 105 rect.set_height(line_height); | |
| 106 if (bounds->height()) { | |
| 107 const int text_height = strings.size() * line_height; | |
| 108 rect += gfx::Vector2d(0, (bounds->height() - text_height) / 2); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 render_text->SetDisplayRect(rect); | |
| 113 height += line_height; | |
| 114 rect += gfx::Vector2d(0, line_height); | |
| 115 lines.push_back(std::move(render_text)); | |
| 116 } | |
| 117 | |
| 118 // Set calculated height. | |
| 119 if (bounds->height() == 0) | |
| 120 bounds->set_height(height); | |
| 121 | |
| 122 } else { | |
| 123 std::unique_ptr<gfx::RenderText> render_text = | |
| 124 CreateRenderText(text, font_list, color, flags); | |
| 125 if (bounds->width() != 0) | |
| 126 render_text->SetElideBehavior(gfx::TRUNCATE); | |
| 127 else | |
| 128 rect.set_width(INT_MAX); | |
| 129 | |
| 130 render_text->SetDisplayRect(rect); | |
| 131 | |
| 132 if (bounds->width() == 0) { | |
| 133 int text_width = render_text->GetStringSize().width(); | |
| 134 bounds->set_width(text_width); | |
| 135 | |
| 136 // bringing text back to the left. | |
|
mthiesse
2017/05/12 19:32:59
The comment doesn't really make sense. I'd just re
| |
| 137 rect.set_width(text_width); | |
| 138 render_text->SetDisplayRect(rect); | |
| 139 } | |
| 140 | |
| 141 lines.push_back(std::move(render_text)); | |
| 142 } | |
| 143 return lines; | |
| 144 } | |
| 145 | |
| 48 bool UiTexture::IsRTL() { | 146 bool UiTexture::IsRTL() { |
| 49 return base::i18n::IsRTL(); | 147 return base::i18n::IsRTL(); |
| 50 } | 148 } |
| 51 | 149 |
| 52 gfx::FontList UiTexture::GetDefaultFontList(int size) { | 150 gfx::FontList UiTexture::GetDefaultFontList(int size) { |
| 53 return gfx::FontList(gfx::Font(kDefaultFontFamily, size)); | 151 return gfx::FontList(gfx::Font(kDefaultFontFamily, size)); |
| 54 } | 152 } |
| 55 | 153 |
| 56 gfx::FontList UiTexture::GetFontList(int size, base::string16 text) { | 154 gfx::FontList UiTexture::GetFontList(int size, base::string16 text) { |
| 57 gfx::Font default_font(kDefaultFontFamily, size); | 155 gfx::Font default_font(kDefaultFontFamily, size); |
| 58 std::vector<gfx::Font> fonts{default_font}; | 156 std::vector<gfx::Font> fonts{default_font}; |
| 59 | 157 |
| 60 std::set<std::string> names; | 158 std::set<std::string> names; |
| 61 // TODO(acondor): Query BrowserProcess to obtain the application locale. | 159 // TODO(acondor): Query BrowserProcess to obtain the application locale. |
| 62 for (UChar32 c : CollectDifferentChars(text)) { | 160 for (UChar32 c : CollectDifferentChars(text)) { |
| 63 std::string name = GetFallbackFontNameForChar(default_font, c, ""); | 161 std::string name = GetFallbackFontNameForChar(default_font, c, ""); |
| 64 if (!name.empty()) | 162 if (!name.empty()) |
| 65 names.insert(name); | 163 names.insert(name); |
| 66 } | 164 } |
| 67 for (const auto& name : names) | 165 for (const auto& name : names) |
| 68 fonts.push_back(gfx::Font(name, size)); | 166 fonts.push_back(gfx::Font(name, size)); |
| 69 return gfx::FontList(fonts); | 167 return gfx::FontList(fonts); |
| 70 } | 168 } |
| 71 | 169 |
| 72 } // namespace vr_shell | 170 } // namespace vr_shell |
| OLD | NEW |