1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/common/badge_util.h"
7 #include "base/logging.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "grit/ui_resources.h"
10 #include "third_party/skia/include/core/SkPaint.h"
11 #include "third_party/skia/include/core/SkTypeface.h"
12 #include "ui/base/resource/resource_bundle.h"
13 #include "ui/gfx/canvas.h"
14 #include "ui/gfx/font.h"
15 #include "ui/gfx/rect.h"
16 #include "ui/gfx/size.h"
20 // Different platforms need slightly different constants to look good.
22 const float kTextSize
= 10;
23 const int kBottomMarginBrowserAction
= 0;
24 const int kBottomMarginPageAction
= 2;
25 const int kPadding
= 2;
26 // The padding between the top of the badge and the top of the text.
27 const int kTopTextPadding
= -1;
28 #elif defined(OS_MACOSX)
29 const float kTextSize
= 9.0;
30 const int kBottomMarginBrowserAction
= 5;
31 const int kBottomMarginPageAction
= 2;
32 const int kPadding
= 2;
33 const int kTopTextPadding
= 0;
34 #elif defined(OS_CHROMEOS)
35 const float kTextSize
= 8.0;
36 const int kBottomMarginBrowserAction
= 0;
37 const int kBottomMarginPageAction
= 2;
38 const int kPadding
= 2;
39 const int kTopTextPadding
= 1;
40 #elif defined(OS_POSIX)
41 const float kTextSize
= 9.0;
42 const int kBottomMarginBrowserAction
= 0;
43 const int kBottomMarginPageAction
= 2;
44 const int kPadding
= 2;
45 const int kTopTextPadding
= 0;
48 const int kBadgeHeight
= 11;
49 const int kMaxTextWidth
= 23;
51 // The minimum width for center-aligning the badge.
52 const int kCenterAlignThreshold
= 20;
56 namespace badge_util
{
58 SkPaint
* GetBadgeTextPaintSingleton() {
59 #if defined(OS_MACOSX)
60 const char kPreferredTypeface
[] = "Helvetica Bold";
62 const char kPreferredTypeface
[] = "Arial";
65 static SkPaint
* text_paint
= NULL
;
67 text_paint
= new SkPaint
;
68 text_paint
->setAntiAlias(true);
69 text_paint
->setTextAlign(SkPaint::kLeft_Align
);
71 skia::RefPtr
<SkTypeface
> typeface
= skia::AdoptRef(
72 SkTypeface::CreateFromName(kPreferredTypeface
, SkTypeface::kBold
));
73 // Skia doesn't do any font fallback---if the user is missing the font then
74 // typeface will be NULL. If we don't do manual fallback then we'll crash.
76 text_paint
->setFakeBoldText(true);
78 // Fall back to the system font. We don't bold it because we aren't sure
80 // For the most part this code path will only be hit on Linux systems
81 // that don't have Arial.
82 ResourceBundle
& rb
= ResourceBundle::GetSharedInstance();
83 const gfx::Font
& base_font
= rb
.GetFont(ResourceBundle::BaseFont
);
84 typeface
= skia::AdoptRef(SkTypeface::CreateFromName(
85 base_font
.GetFontName().c_str(), SkTypeface::kNormal
));
89 text_paint
->setTypeface(typeface
.get());
90 // |text_paint| adds its own ref. Release the ref from CreateFontName.
95 void PaintBadge(gfx::Canvas
* canvas
,
96 const gfx::Rect
& bounds
,
97 const std::string
& text
,
98 const SkColor
& text_color_in
,
99 const SkColor
& background_color_in
,
101 extensions::ActionInfo::Type action_type
) {
105 SkColor text_color
= text_color_in
;
106 if (SkColorGetA(text_color_in
) == 0x00)
107 text_color
= SK_ColorWHITE
;
109 SkColor background_color
= background_color_in
;
110 if (SkColorGetA(background_color_in
) == 0x00)
111 background_color
= SkColorSetARGB(255, 218, 0, 24);
115 SkPaint
* text_paint
= badge_util::GetBadgeTextPaintSingleton();
116 text_paint
->setTextSize(SkFloatToScalar(kTextSize
));
117 text_paint
->setColor(text_color
);
119 // Calculate text width. We clamp it to a max size.
120 SkScalar sk_text_width
= text_paint
->measureText(text
.c_str(), text
.size());
121 int text_width
= std::min(kMaxTextWidth
, SkScalarFloorToInt(sk_text_width
));
123 // Calculate badge size. It is clamped to a min width just because it looks
124 // silly if it is too skinny.
125 int badge_width
= text_width
+ kPadding
* 2;
126 // Force the pixel width of badge to be either odd (if the icon width is odd)
127 // or even otherwise. If there is a mismatch you get http://crbug.com/26400.
128 if (icon_width
!= 0 && (badge_width
% 2 != icon_width
% 2))
130 badge_width
= std::max(kBadgeHeight
, badge_width
);
132 // Paint the badge background color in the right location. It is usually
133 // right-aligned, but it can also be center-aligned if it is large.
134 int rect_height
= kBadgeHeight
;
136 action_type
== extensions::ActionInfo::TYPE_BROWSER
?
137 kBottomMarginBrowserAction
: kBottomMarginPageAction
;
138 int rect_y
= bounds
.bottom() - bottom_margin
- kBadgeHeight
;
139 int rect_width
= badge_width
;
140 int rect_x
= (badge_width
>= kCenterAlignThreshold
) ?
141 bounds
.x() + (bounds
.width() - badge_width
) / 2 :
142 bounds
.right() - badge_width
;
143 gfx::Rect
rect(rect_x
, rect_y
, rect_width
, rect_height
);
146 rect_paint
.setStyle(SkPaint::kFill_Style
);
147 rect_paint
.setAntiAlias(true);
148 rect_paint
.setColor(background_color
);
149 canvas
->DrawRoundRect(rect
, 2, rect_paint
);
151 // Overlay the gradient. It is stretchy, so we do this in three parts.
152 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
153 gfx::ImageSkia
* gradient_left
= rb
.GetImageSkiaNamed(
154 IDR_BROWSER_ACTION_BADGE_LEFT
);
155 gfx::ImageSkia
* gradient_right
= rb
.GetImageSkiaNamed(
156 IDR_BROWSER_ACTION_BADGE_RIGHT
);
157 gfx::ImageSkia
* gradient_center
= rb
.GetImageSkiaNamed(
158 IDR_BROWSER_ACTION_BADGE_CENTER
);
160 canvas
->DrawImageInt(*gradient_left
, rect
.x(), rect
.y());
161 canvas
->TileImageInt(*gradient_center
,
162 rect
.x() + gradient_left
->width(),
164 rect
.width() - gradient_left
->width() - gradient_right
->width(),
166 canvas
->DrawImageInt(*gradient_right
,
167 rect
.right() - gradient_right
->width(), rect
.y());
169 // Finally, draw the text centered within the badge. We set a clip in case the
170 // text was too large.
171 rect
.Inset(kPadding
, 0);
172 canvas
->ClipRect(rect
);
173 canvas
->sk_canvas()->drawText(
174 text
.c_str(), text
.size(),
175 SkFloatToScalar(rect
.x() +
176 static_cast<float>(rect
.width() - text_width
) / 2),
177 SkFloatToScalar(rect
.y() + kTextSize
+ kTopTextPadding
),
182 } // namespace badge_util