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/insecure_content_transient_te xture.h" | 5 #include "chrome/browser/android/vr_shell/textures/insecure_content_transient_te xture.h" |
| 6 | 6 |
| 7 #include "cc/paint/skia_paint_canvas.h" | 7 #include "cc/paint/skia_paint_canvas.h" |
| 8 #include "components/strings/grit/components_strings.h" | 8 #include "components/strings/grit/components_strings.h" |
| 9 #include "ui/base/l10n/l10n_util.h" | 9 #include "ui/base/l10n/l10n_util.h" |
| 10 #include "ui/gfx/canvas.h" | 10 #include "ui/gfx/canvas.h" |
| 11 #include "ui/gfx/font_list.h" | 11 #include "ui/gfx/font_list.h" |
| 12 #include "ui/gfx/geometry/rect.h" | 12 #include "ui/gfx/geometry/rect.h" |
| 13 #include "ui/gfx/geometry/vector2d.h" | 13 #include "ui/gfx/geometry/vector2d.h" |
| 14 #include "ui/gfx/paint_vector_icon.h" | 14 #include "ui/gfx/paint_vector_icon.h" |
| 15 #include "ui/gfx/render_text.h" | |
| 15 #include "ui/gfx/vector_icon_types.h" | 16 #include "ui/gfx/vector_icon_types.h" |
| 16 #include "ui/vector_icons/vector_icons.h" | 17 #include "ui/vector_icons/vector_icons.h" |
| 17 | 18 |
| 18 namespace vr_shell { | 19 namespace vr_shell { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 const SkColor kBackgroundColor = 0xCC1A1A1A; | 23 const SkColor kBackgroundColor = 0xCC1A1A1A; |
| 23 const SkColor kForegroundColor = SK_ColorWHITE; | 24 const SkColor kForegroundColor = SK_ColorWHITE; |
| 24 constexpr float kBorderFactor = 0.045; | 25 constexpr float kBorderFactor = 0.045; |
| 25 constexpr float kFontSizeFactor = 0.048; | 26 constexpr float kFontSizeFactor = 0.048; |
| 26 constexpr float kTextWidthFactor = 1.0 - 3 * kBorderFactor; | 27 constexpr float kTextWidthFactor = 1.0 - 3 * kBorderFactor; |
| 27 | 28 |
| 28 } // namespace | 29 } // namespace |
| 29 | 30 |
| 30 InsecureContentTransientTexture::InsecureContentTransientTexture() = default; | 31 InsecureContentTransientTexture::InsecureContentTransientTexture() = default; |
| 31 | 32 |
| 32 InsecureContentTransientTexture::~InsecureContentTransientTexture() = default; | 33 InsecureContentTransientTexture::~InsecureContentTransientTexture() = default; |
| 33 | 34 |
| 34 void InsecureContentTransientTexture::Draw(SkCanvas* sk_canvas, | 35 void InsecureContentTransientTexture::Draw(SkCanvas* sk_canvas, |
| 35 const gfx::Size& texture_size) { | 36 const gfx::Size& texture_size) { |
| 36 cc::SkiaPaintCanvas paint_canvas(sk_canvas); | 37 cc::SkiaPaintCanvas paint_canvas(sk_canvas); |
| 37 gfx::Canvas gfx_canvas(&paint_canvas, 1.0f); | 38 gfx::Canvas gfx_canvas(&paint_canvas, 1.0f); |
| 38 gfx::Canvas* canvas = &gfx_canvas; | 39 gfx::Canvas* canvas = &gfx_canvas; |
| 39 | 40 |
| 40 size_.set_width(texture_size.width()); | 41 size_.set_width(texture_size.width()); |
| 41 int max_height = texture_size.height(); | 42 SkPaint paint; |
| 42 cc::PaintFlags flags; | 43 paint.setColor(kBackgroundColor); |
| 43 flags.setColor(kBackgroundColor); | |
| 44 | |
| 45 int text_flags = gfx::Canvas::TEXT_ALIGN_CENTER | gfx::Canvas::MULTI_LINE; | |
| 46 auto text = | 44 auto text = |
| 47 l10n_util::GetStringUTF16(IDS_PAGE_INFO_INSECURE_WEBVR_CONTENT_TRANSIENT); | 45 l10n_util::GetStringUTF16(IDS_PAGE_INFO_INSECURE_WEBVR_CONTENT_TRANSIENT); |
| 48 auto fonts = GetFontList(size_.width() * kFontSizeFactor, text); | 46 auto fonts = GetFontList(size_.width() * kFontSizeFactor, text); |
| 49 int text_width = size_.width() * kTextWidthFactor; | 47 gfx::Rect text_size(size_.width() * kTextWidthFactor, 0); |
| 50 int text_height = 0; // Will be increased during text measurement. | |
| 51 gfx::Canvas::SizeStringInt(text, fonts, &text_width, &text_height, 0, | |
| 52 text_flags); | |
| 53 | 48 |
| 54 // Making sure the drawing fits within the texture. | 49 std::vector<std::unique_ptr<gfx::RenderText>> lines = |
| 55 text_height = std::min( | 50 PrepareDrawStringRect(text, fonts, kForegroundColor, &text_size, |
| 56 text_height, static_cast<int>((1.0 - 2 * kBorderFactor) * size_.width())); | 51 TEXT_ALIGN_CENTER | MULTI_LINE); |
| 57 size_.set_height(size_.width() * 2 * kBorderFactor + text_height); | 52 |
| 58 DCHECK_LE(size_.height(), max_height); | 53 DCHECK_LE(text_size.height(), |
|
mthiesse
2017/05/12 01:14:38
why remove this DCHECK?
acondor_
2017/05/12 14:54:23
Because it's kind of innecessary given that we are
| |
| 59 canvas->DrawRoundRect(gfx::Rect(size_.width(), size_.height()), | 54 static_cast<int>((1.0 - 2 * kBorderFactor) * size_.width())); |
| 60 size_.height() * kBorderFactor, flags); | 55 size_.set_height(size_.width() * 2 * kBorderFactor + text_size.height()); |
| 56 float radius = size_.height() * kBorderFactor; | |
| 57 sk_canvas->drawRoundRect(SkRect::MakeWH(size_.width(), size_.height()), | |
| 58 radius, radius, paint); | |
| 61 | 59 |
| 62 canvas->Save(); | 60 canvas->Save(); |
| 63 canvas->Translate(gfx::Vector2d(size_.width() * kBorderFactor, | 61 canvas->Translate(gfx::Vector2d(size_.width() * kBorderFactor, |
| 64 size_.width() * kBorderFactor)); | 62 size_.width() * kBorderFactor)); |
| 65 canvas->DrawStringRectWithFlags( | 63 for (auto& render_text : lines) |
| 66 text, fonts, kForegroundColor, | 64 render_text->Draw(canvas); |
| 67 gfx::Rect(kTextWidthFactor * size_.width(), text_height), text_flags); | |
| 68 canvas->Restore(); | 65 canvas->Restore(); |
| 69 } | 66 } |
| 70 | 67 |
| 71 gfx::Size InsecureContentTransientTexture::GetPreferredTextureSize( | 68 gfx::Size InsecureContentTransientTexture::GetPreferredTextureSize( |
| 72 int maximum_width) const { | 69 int maximum_width) const { |
| 73 return gfx::Size(maximum_width, maximum_width); | 70 return gfx::Size(maximum_width, maximum_width); |
| 74 } | 71 } |
| 75 | 72 |
| 76 gfx::SizeF InsecureContentTransientTexture::GetDrawnSize() const { | 73 gfx::SizeF InsecureContentTransientTexture::GetDrawnSize() const { |
| 77 return size_; | 74 return size_; |
| 78 } | 75 } |
| 79 | 76 |
| 80 } // namespace vr_shell | 77 } // namespace vr_shell |
| OLD | NEW |