Commit 60033857 authored by Tibor Goldschwendt's avatar Tibor Goldschwendt Committed by Commit Bot

[vr] Move TextTexture into its own file

Change-Id: I0ac74f33ceb8f61593a16a957ee8b9c7c93f51fd
Reviewed-on: https://chromium-review.googlesource.com/702775Reviewed-by: default avatarIan Vollick <vollick@chromium.org>
Commit-Queue: Tibor Goldschwendt <tiborg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#506893}
parent 5aff5c5e
...@@ -63,6 +63,8 @@ source_set("vr_common_source") { ...@@ -63,6 +63,8 @@ source_set("vr_common_source") {
"elements/system_indicator_texture.h", "elements/system_indicator_texture.h",
"elements/text.cc", "elements/text.cc",
"elements/text.h", "elements/text.h",
"elements/text_texture.cc",
"elements/text_texture.h",
"elements/textured_element.cc", "elements/textured_element.cc",
"elements/textured_element.h", "elements/textured_element.h",
"elements/transient_element.cc", "elements/transient_element.cc",
......
...@@ -5,49 +5,10 @@ ...@@ -5,49 +5,10 @@
#include "chrome/browser/vr/elements/text.h" #include "chrome/browser/vr/elements/text.h"
#include "base/memory/ptr_util.h" #include "base/memory/ptr_util.h"
#include "cc/paint/skia_paint_canvas.h" #include "chrome/browser/vr/elements/text_texture.h"
#include "chrome/browser/vr/elements/ui_texture.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/gfx/render_text.h"
namespace vr { namespace vr {
class TextTexture : public UiTexture {
public:
TextTexture(const base::string16& text, float font_height, float text_width)
: text_(text), font_height_(font_height), text_width_(text_width) {}
~TextTexture() override {}
void SetColor(SkColor color) {
if (color_ == color)
return;
color_ = color;
set_dirty();
}
private:
gfx::Size GetPreferredTextureSize(int width) const override {
return gfx::Size(width, width);
}
gfx::SizeF GetDrawnSize() const override { return size_; }
void Draw(SkCanvas* sk_canvas, const gfx::Size& texture_size) override;
gfx::SizeF size_;
base::string16 text_;
// These widths are in meters.
float font_height_;
float text_width_;
SkColor color_ = SK_ColorBLACK;
DISALLOW_COPY_AND_ASSIGN(TextTexture);
};
Text::Text(int maximum_width_pixels, Text::Text(int maximum_width_pixels,
float font_height_meters, float font_height_meters,
float text_width_meters, float text_width_meters,
...@@ -58,36 +19,12 @@ Text::Text(int maximum_width_pixels, ...@@ -58,36 +19,12 @@ Text::Text(int maximum_width_pixels,
text_width_meters)) {} text_width_meters)) {}
Text::~Text() {} Text::~Text() {}
UiTexture* Text::GetTexture() const {
return texture_.get();
}
void Text::SetColor(SkColor color) { void Text::SetColor(SkColor color) {
texture_->SetColor(color); texture_->SetColor(color);
} }
void TextTexture::Draw(SkCanvas* sk_canvas, const gfx::Size& texture_size) { UiTexture* Text::GetTexture() const {
cc::SkiaPaintCanvas paint_canvas(sk_canvas); return texture_.get();
gfx::Canvas gfx_canvas(&paint_canvas, 1.0f);
gfx::Canvas* canvas = &gfx_canvas;
gfx::FontList fonts;
float pixels_per_meter = texture_size.width() / text_width_;
int pixel_font_height = static_cast<int>(font_height_ * pixels_per_meter);
GetFontList(pixel_font_height, text_, &fonts);
gfx::Rect text_bounds(texture_size.width(), 0);
std::vector<std::unique_ptr<gfx::RenderText>> lines =
// TODO(vollick): if this subsumes all text, then we should probably move
// this function into this class.
PrepareDrawStringRect(text_, fonts, color_, &text_bounds,
kTextAlignmentCenter, kWrappingBehaviorWrap);
// Draw the text.
for (auto& render_text : lines)
render_text->Draw(canvas);
// Note, there is no padding here whatsoever.
size_ = gfx::SizeF(text_bounds.size());
} }
} // namespace vr } // namespace vr
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/vr/elements/text_texture.h"
#include "base/memory/ptr_util.h"
#include "cc/paint/skia_paint_canvas.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/font_list.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/geometry/vector2d.h"
#include "ui/gfx/render_text.h"
namespace vr {
TextTexture::TextTexture(const base::string16& text,
float font_height,
float text_width)
: text_(text),
font_height_meters_(font_height),
text_width_meters_(text_width) {}
TextTexture::~TextTexture() {}
void TextTexture::SetColor(SkColor color) {
if (color_ == color)
return;
color_ = color;
set_dirty();
}
gfx::Size TextTexture::GetPreferredTextureSize(int width) const {
return gfx::Size(width, width);
}
gfx::SizeF TextTexture::GetDrawnSize() const {
return size_;
}
void TextTexture::Draw(SkCanvas* sk_canvas, const gfx::Size& texture_size) {
cc::SkiaPaintCanvas paint_canvas(sk_canvas);
gfx::Canvas gfx_canvas(&paint_canvas, 1.0f);
gfx::Canvas* canvas = &gfx_canvas;
gfx::FontList fonts;
float pixels_per_meter = texture_size.width() / text_width_meters_;
int pixel_font_height =
static_cast<int>(font_height_meters_ * pixels_per_meter);
GetFontList(pixel_font_height, text_, &fonts);
gfx::Rect text_bounds(texture_size.width(), 0);
std::vector<std::unique_ptr<gfx::RenderText>> lines =
// TODO(vollick): if this subsumes all text, then we should probably move
// this function into this class.
PrepareDrawStringRect(text_, fonts, color_, &text_bounds,
kTextAlignmentCenter, kWrappingBehaviorWrap);
// Draw the text.
for (auto& render_text : lines)
render_text->Draw(canvas);
// Note, there is no padding here whatsoever.
size_ = gfx::SizeF(text_bounds.size());
}
} // namespace vr
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_VR_ELEMENTS_TEXT_TEXTURE_H_
#define CHROME_BROWSER_VR_ELEMENTS_TEXT_TEXTURE_H_
#include "chrome/browser/vr/elements/ui_texture.h"
namespace vr {
class TextTexture : public UiTexture {
public:
TextTexture(const base::string16& text, float font_height, float text_width);
~TextTexture() override;
void SetColor(SkColor color);
private:
gfx::Size GetPreferredTextureSize(int width) const override;
gfx::SizeF GetDrawnSize() const override;
void Draw(SkCanvas* sk_canvas, const gfx::Size& texture_size) override;
gfx::SizeF size_;
base::string16 text_;
float font_height_meters_;
float text_width_meters_;
SkColor color_ = SK_ColorBLACK;
DISALLOW_COPY_AND_ASSIGN(TextTexture);
};
} // namespace vr
#endif // CHROME_BROWSER_VR_ELEMENTS_TEXT_TEXTURE_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment