Chromium Code Reviews| Index: chrome/browser/android/vr_shell/textures/ui_texture.cc |
| diff --git a/chrome/browser/android/vr_shell/textures/ui_texture.cc b/chrome/browser/android/vr_shell/textures/ui_texture.cc |
| index 6d14ae5cb676b73045bd062227a2fd91cf4c5bdd..ceac570aaddd37841fa00b7c8f7b0cca9aedc243 100644 |
| --- a/chrome/browser/android/vr_shell/textures/ui_texture.cc |
| +++ b/chrome/browser/android/vr_shell/textures/ui_texture.cc |
| @@ -12,12 +12,15 @@ |
| #include "base/i18n/rtl.h" |
| #include "base/memory/ptr_util.h" |
| #include "base/strings/string_util.h" |
| +#include "base/trace_event/trace_event.h" |
| #include "chrome/browser/android/vr_shell/font_fallback.h" |
| #include "chrome/browser/browser_process.h" |
| #include "third_party/icu/source/common/unicode/uscript.h" |
| #include "third_party/skia/include/core/SkCanvas.h" |
| #include "ui/gfx/canvas.h" |
| #include "ui/gfx/font_list.h" |
| +#include "ui/gfx/render_text.h" |
| +#include "ui/gfx/text_elider.h" |
| #include "ui/gl/gl_bindings.h" |
| namespace vr_shell { |
| @@ -26,6 +29,33 @@ namespace { |
| static constexpr char kDefaultFontFamily[] = "sans-serif"; |
| +std::unique_ptr<gfx::RenderText> CreateRenderText( |
| + const base::string16& text, |
| + const gfx::FontList& font_list, |
| + SkColor color, |
| + int flags) { |
| + std::unique_ptr<gfx::RenderText> render_text( |
| + gfx::RenderText::CreateInstance()); |
| + render_text->SetText(text); |
| + render_text->SetFontList(font_list); |
| + render_text->SetColor(color); |
| + |
| + if (flags & UiTexture::TEXT_ALIGN_LEFT) |
|
cjgrant
2017/05/11 21:15:40
It looks like RenderText defaults to either RIGHT
acondor_
2017/05/11 22:26:49
Good catch. I'm removing the flag setup in Prepare
|
| + render_text->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| + else if (flags & UiTexture::TEXT_ALIGN_RIGHT) |
| + render_text->SetHorizontalAlignment(gfx::ALIGN_RIGHT); |
| + else |
| + render_text->SetHorizontalAlignment(gfx::ALIGN_CENTER); |
| + |
| + const int font_style = font_list.GetFontStyle(); |
| + render_text->SetStyle(gfx::ITALIC, (font_style & gfx::Font::ITALIC) != 0); |
| + render_text->SetStyle(gfx::UNDERLINE, |
| + (font_style & gfx::Font::UNDERLINE) != 0); |
| + render_text->SetWeight(font_list.GetFontWeight()); |
| + |
| + return render_text; |
| +} |
| + |
| std::set<UChar32> CollectDifferentChars(base::string16 text) { |
| std::set<UChar32> characters; |
| for (base::i18n::UTF16CharIterator it(&text); !it.end(); it.Advance()) { |
| @@ -41,10 +71,78 @@ UiTexture::UiTexture() = default; |
| UiTexture::~UiTexture() = default; |
| void UiTexture::DrawAndLayout(SkCanvas* canvas, const gfx::Size& texture_size) { |
| + TRACE_EVENT0("gpu", "UiTexture::DrawAndLayout"); |
| canvas->drawColor(SK_ColorTRANSPARENT); |
| Draw(canvas, texture_size); |
| } |
| +std::vector<std::unique_ptr<gfx::RenderText>> UiTexture::PrepareDrawStringRect( |
| + const base::string16& text, |
| + const gfx::FontList& font_list, |
| + SkColor color, |
| + gfx::Rect* bounds, |
| + int flags) { |
| + DCHECK(bounds); |
| + |
| + std::vector<std::unique_ptr<gfx::RenderText>> lines; |
| + gfx::Rect rect(*bounds); |
| + |
| + if (!(flags & (TEXT_ALIGN_LEFT | TEXT_ALIGN_CENTER | TEXT_ALIGN_RIGHT))) { |
| + flags |= IsRTL() ? TEXT_ALIGN_RIGHT : TEXT_ALIGN_LEFT; |
| + } |
| + |
| + if (flags & MULTI_LINE) { |
| + std::vector<base::string16> strings; |
| + gfx::ElideRectangleText(text, font_list, bounds->width(), |
| + bounds->height() ? bounds->height() : INT_MAX, |
| + gfx::ELIDE_LONG_WORDS, &strings); |
|
mthiesse
2017/05/12 01:14:38
Probably want WRAP_LONG_WORDS here, to align with
acondor_
2017/05/12 14:54:23
Done.
|
| + |
| + int height = 0; |
| + int line_height = 0; |
| + for (size_t i = 0; i < strings.size(); i++) { |
| + std::unique_ptr<gfx::RenderText> render_text = |
| + CreateRenderText(strings[i], font_list, color, flags); |
| + |
| + if (i == 0) { |
| + // Measure line and center text vertically. |
| + line_height = render_text->GetStringSize().height(); |
| + rect.set_height(line_height); |
| + if (bounds->height()) { |
| + const int text_height = strings.size() * line_height; |
| + rect += gfx::Vector2d(0, (bounds->height() - text_height) / 2); |
| + } |
| + } |
| + |
| + render_text->SetDisplayRect(rect); |
| + height += line_height; |
| + rect += gfx::Vector2d(0, line_height); |
| + lines.push_back(std::move(render_text)); |
| + } |
| + |
| + // Set calculated height. |
| + if (bounds->height() == 0) |
| + bounds->set_height(height); |
| + |
| + } else { |
| + std::unique_ptr<gfx::RenderText> render_text = |
| + CreateRenderText(text, font_list, color, flags); |
| + if (bounds->width() != 0) |
| + render_text->SetElideBehavior(gfx::FADE_TAIL); |
|
mthiesse
2017/05/12 01:14:38
Pretty sure we want gfx::TRUNCATE per UX specs.
acondor_
2017/05/12 14:54:23
Done.
|
| + else |
| + rect.set_width(INT_MAX); |
| + |
| + render_text->SetDisplayRect(rect); |
| + |
| + if (bounds->width() == 0) { |
| + int text_width = render_text->GetStringSize().width(); |
| + bounds->set_width(text_width); |
| + } |
| + |
| + lines.push_back(std::move(render_text)); |
| + } |
| + return lines; |
| +} |
| + |
| bool UiTexture::IsRTL() { |
| return base::i18n::IsRTL(); |
| } |