diff options
author | xiyuan@chromium.org <xiyuan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-11 02:23:56 +0000 |
---|---|---|
committer | xiyuan@chromium.org <xiyuan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-04-11 02:23:56 +0000 |
commit | 0834e1b153c28dec0ae9e90080ad7a106f1c8c84 (patch) | |
tree | 3a939399a68c91722271573bf2589266d642d58f /ash | |
parent | c8678d8870826350577c3f00e922e442abb647c9 (diff) | |
download | chromium_src-0834e1b153c28dec0ae9e90080ad7a106f1c8c84.zip chromium_src-0834e1b153c28dec0ae9e90080ad7a106f1c8c84.tar.gz chromium_src-0834e1b153c28dec0ae9e90080ad7a106f1c8c84.tar.bz2 |
ash: Better and faster text shadows.
- Add a DrawStringWithShadows that draws text with shadows to canvas skia
and RenderText;
- Use DrawStringWithShadows in DropShadowLabel for better and faster text
shadows;
BUG=121694
TEST=Verify fix for issue 121694.
Review URL: http://codereview.chromium.org/10008027
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@131696 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ash')
-rw-r--r-- | ash/app_list/app_list_item_view.cc | 10 | ||||
-rw-r--r-- | ash/app_list/drop_shadow_label.cc | 107 | ||||
-rw-r--r-- | ash/app_list/drop_shadow_label.h | 28 |
3 files changed, 45 insertions, 100 deletions
diff --git a/ash/app_list/app_list_item_view.cc b/ash/app_list/app_list_item_view.cc index 5addda1..c9e7492 100644 --- a/ash/app_list/app_list_item_view.cc +++ b/ash/app_list/app_list_item_view.cc @@ -17,6 +17,7 @@ #include "ui/base/resource/resource_bundle.h" #include "ui/gfx/canvas.h" #include "ui/gfx/font.h" +#include "ui/gfx/shadow_value.h" #include "ui/gfx/skbitmap_operations.h" #include "ui/views/controls/image_view.h" #include "ui/views/controls/menu/menu_item_view.h" @@ -184,7 +185,14 @@ AppListItemView::AppListItemView(AppListModelView* list_model_view, ALLOW_THIS_IN_INITIALIZER_LIST(apply_shadow_factory_(this)) { title_->SetBackgroundColor(0); title_->SetEnabledColor(kTitleColor); - title_->SetDropShadowSize(3); + + const gfx::ShadowValue kTitleShadows[] = { + gfx::ShadowValue(gfx::Point(0, 0), 1, SkColorSetARGB(0x66, 0, 0, 0)), + gfx::ShadowValue(gfx::Point(0, 0), 10, SkColorSetARGB(0x66, 0, 0, 0)), + gfx::ShadowValue(gfx::Point(0, 2), 2, SkColorSetARGB(0x66, 0, 0, 0)), + gfx::ShadowValue(gfx::Point(0, 2), 4, SkColorSetARGB(0x66, 0, 0, 0)), + }; + title_->SetTextShadows(arraysize(kTitleShadows), kTitleShadows); AddChildView(icon_); AddChildView(title_); diff --git a/ash/app_list/drop_shadow_label.cc b/ash/app_list/drop_shadow_label.cc index de1c90f..c897e2a 100644 --- a/ash/app_list/drop_shadow_label.cc +++ b/ash/app_list/drop_shadow_label.cc @@ -8,97 +8,49 @@ #include "third_party/skia/include/effects/SkGradientShader.h" #include "ui/gfx/canvas.h" #include "ui/gfx/color_utils.h" +#include "ui/gfx/insets.h" #include "ui/gfx/skbitmap_operations.h" using views::Label; namespace ash { -static const int kDefaultDropShadowSize = 2; +DropShadowLabel::DropShadowLabel() { +} -DropShadowLabel::DropShadowLabel() : drop_shadow_size_(kDefaultDropShadowSize) { +DropShadowLabel::~DropShadowLabel() { } -void DropShadowLabel::SetDropShadowSize(int drop_shadow_size) { - if (drop_shadow_size != drop_shadow_size_) { - drop_shadow_size_ = drop_shadow_size; - invalidate_text_size(); - SchedulePaint(); +void DropShadowLabel::SetTextShadows(int shadow_count, + const gfx::ShadowValue* shadows) { + text_shadows_.clear(); + + if (shadow_count && shadows) { + for (int i = 0; i < shadow_count; ++i) + text_shadows_.push_back(shadows[i]); } } +gfx::Insets DropShadowLabel::GetInsets() const { + gfx::Insets insets = views::Label::GetInsets(); + gfx::Insets shadow_margin = gfx::ShadowValue::GetMargin(text_shadows_); + // Negate |shadow_margin| to convert it to a padding insets needed inside + // the bounds and combine with label's insets. + insets += -shadow_margin; + return insets; +} + void DropShadowLabel::PaintText(gfx::Canvas* canvas, const string16& text, const gfx::Rect& text_bounds, int flags) { SkColor text_color = enabled() ? enabled_color() : disabled_color(); - if (drop_shadow_size_ > 0) { - // To properly render shadow with elliding fade effect, text and shadow - // is rendered to this canvas first with elliding disable so that underlying - // code would not mix shadow color into text area because of elliding fade. - // When that is done and if we need elliding fade, an alpha mask is applied - // when transfering contents on this canvas to target canvas. - gfx::Size canvas_size(text_bounds.width() + drop_shadow_size_, - text_bounds.height() + drop_shadow_size_); - gfx::Canvas text_canvas(canvas_size, false); - - const double kShadowOpacity = 0.2; - const SkColor shadow_color = - SkColorSetA(SK_ColorBLACK, kShadowOpacity * SkColorGetA(text_color)); - gfx::Size text_size = GetTextSize(); - for (int i = 0; i < drop_shadow_size_; i++) { - text_canvas.DrawStringInt(text, font(), shadow_color, i, 0, - text_size.width(), text_size.height(), - flags | gfx::Canvas::NO_ELLIPSIS); - text_canvas.DrawStringInt(text, font(), shadow_color, i, i, - text_size.width(), text_size.height(), - flags | gfx::Canvas::NO_ELLIPSIS); - text_canvas.DrawStringInt(text, font(), shadow_color, 0, i, - text_size.width(), text_size.height(), - flags | gfx::Canvas::NO_ELLIPSIS); - } - text_canvas.DrawStringInt(text, font(), text_color, 0, 0, - text_size.width(), text_size.height(), - flags | gfx::Canvas::NO_ELLIPSIS); - - const SkBitmap& text_bitmap = const_cast<SkBitmap&>( - skia::GetTopDevice(*text_canvas.sk_canvas())->accessBitmap(false)); - - if (text_size.width() > text_bounds.width() && - !(flags & gfx::Canvas::NO_ELLIPSIS)) { - // Apply an gradient alpha mask for elliding fade effect. - const double kFadeWidthFactor = 1.5; - int fade_width = std::min(text_size.width() / 2, - static_cast<int>(text_size.height() * kFadeWidthFactor)); - - const SkColor kColors[] = { SK_ColorWHITE, 0 }; - const SkScalar kPoints[] = { SkIntToScalar(0), SkIntToScalar(1) }; - SkPoint p[2]; - p[0].set(SkIntToScalar(text_bounds.width() - fade_width), - SkIntToScalar(0)); - p[1].set(SkIntToScalar(text_bounds.width()), - SkIntToScalar(0)); - SkShader* s = SkGradientShader::CreateLinear( - p, kColors, kPoints, 2, SkShader::kClamp_TileMode, NULL); - - SkPaint paint; - paint.setShader(s)->unref(); - - gfx::Canvas alpha_canvas(canvas_size, false); - alpha_canvas.DrawRect(gfx::Rect(canvas_size), paint); - - const SkBitmap& alpha_bitmap = const_cast<SkBitmap&>( - skia::GetTopDevice(*alpha_canvas.sk_canvas())->accessBitmap(false)); - SkBitmap blended = SkBitmapOperations::CreateMaskedBitmap(text_bitmap, - alpha_bitmap); - canvas->DrawBitmapInt(blended, text_bounds.x(), text_bounds.y()); - } else { - canvas->DrawBitmapInt(text_bitmap, text_bounds.x(), text_bounds.y()); - } - } else { - canvas->DrawStringInt(text, font(), text_color, text_bounds.x(), - text_bounds.y(), text_bounds.width(), text_bounds.height(), flags); - } + canvas->DrawStringWithShadows(text, + font(), + text_color, + text_bounds, + flags, + text_shadows_); if (HasFocus() || paint_as_focused()) { gfx::Rect focus_bounds = text_bounds; @@ -108,11 +60,4 @@ void DropShadowLabel::PaintText(gfx::Canvas* canvas, } } -gfx::Size DropShadowLabel::GetTextSize() const { - gfx::Size text_size = Label::GetTextSize(); - text_size.SetSize(text_size.width() + drop_shadow_size_, - text_size.height() + drop_shadow_size_); - return text_size; -} - } // namespace ash diff --git a/ash/app_list/drop_shadow_label.h b/ash/app_list/drop_shadow_label.h index 2a7ce5d..25a24e1 100644 --- a/ash/app_list/drop_shadow_label.h +++ b/ash/app_list/drop_shadow_label.h @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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. @@ -6,7 +6,9 @@ #define ASH_APP_LIST_DROP_SHADOW_LABEL_H_ #pragma once -#include "ui/gfx/font.h" +#include <vector> + +#include "ui/gfx/shadow_value.h" #include "ui/views/controls/label.h" namespace ash { @@ -22,29 +24,19 @@ namespace ash { class DropShadowLabel : public views::Label { public: DropShadowLabel(); + virtual ~DropShadowLabel(); - // Sets the size of the drop shadow drawn under the text. - // Defaults to two. Note that this is a really simplistic drop - // shadow -- it gets more expensive to draw the larger it gets, - // since it simply draws more copies of the string. For instance, - // for a value of two, the string is draw seven times. In general, - // it is drawn three extra times for each increment of |size|. - void SetDropShadowSize(int size); - - // Return the size of the drop shadow in pixels. - int drop_shadow_size() const { return drop_shadow_size_; } + void SetTextShadows(int shadow_count, const gfx::ShadowValue* shadows); - // Overridden to paint the text differently from the base class. + private: + // Overridden from views::Label: + virtual gfx::Insets GetInsets() const OVERRIDE; virtual void PaintText(gfx::Canvas* canvas, const string16& text, const gfx::Rect& text_bounds, int flags) OVERRIDE; - protected: - virtual gfx::Size GetTextSize() const OVERRIDE; - - private: - int drop_shadow_size_; + std::vector<gfx::ShadowValue> text_shadows_; DISALLOW_COPY_AND_ASSIGN(DropShadowLabel); }; |