blob: 5bae4d5bcd0aeafbf2e6d7dcf72242e5d14614fa [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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.
4
5#include "chrome/common/badge_util.h"
6
Ben Murdoch116680a2014-07-20 18:25:52 -07007#include <cmath>
8
Torne (Richard Coles)58218062012-11-14 11:43:16 +00009#include "base/logging.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010010#include "base/strings/utf_string_conversions.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000011#include "third_party/skia/include/core/SkPaint.h"
12#include "third_party/skia/include/core/SkTypeface.h"
13#include "ui/base/resource/resource_bundle.h"
14#include "ui/gfx/canvas.h"
15#include "ui/gfx/font.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000016#include "ui/gfx/rect.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000017#include "ui/gfx/size.h"
Torne (Richard Coles)03b57e02014-08-28 12:05:23 +010018#include "ui/resources/grit/ui_resources.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000019
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000020namespace {
21
22// Different platforms need slightly different constants to look good.
Torne (Richard Coles)cedac222014-06-03 10:58:34 +010023#if defined(OS_WIN)
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000024const float kTextSize = 10;
Ben Murdocheffb81e2014-03-31 11:51:25 +010025const int kBottomMarginBrowserAction = 0;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000026const int kBottomMarginPageAction = 2;
27const int kPadding = 2;
28// The padding between the top of the badge and the top of the text.
29const int kTopTextPadding = -1;
Torne (Richard Coles)cedac222014-06-03 10:58:34 +010030#elif defined(OS_MACOSX)
31const float kTextSize = 9.0;
32const int kBottomMarginBrowserAction = 5;
33const int kBottomMarginPageAction = 2;
34const int kPadding = 2;
35const int kTopTextPadding = 0;
36#elif defined(OS_CHROMEOS)
37const float kTextSize = 8.0;
38const int kBottomMarginBrowserAction = 0;
39const int kBottomMarginPageAction = 2;
40const int kPadding = 2;
41const int kTopTextPadding = 1;
42#elif defined(OS_POSIX)
43const float kTextSize = 9.0;
44const int kBottomMarginBrowserAction = 0;
45const int kBottomMarginPageAction = 2;
46const int kPadding = 2;
47const int kTopTextPadding = 0;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000048#endif
49
50const int kBadgeHeight = 11;
51const int kMaxTextWidth = 23;
52
53// The minimum width for center-aligning the badge.
54const int kCenterAlignThreshold = 20;
55
56} // namespace
57
Torne (Richard Coles)58218062012-11-14 11:43:16 +000058namespace badge_util {
59
60SkPaint* GetBadgeTextPaintSingleton() {
61#if defined(OS_MACOSX)
62 const char kPreferredTypeface[] = "Helvetica Bold";
63#else
64 const char kPreferredTypeface[] = "Arial";
65#endif
66
67 static SkPaint* text_paint = NULL;
68 if (!text_paint) {
69 text_paint = new SkPaint;
70 text_paint->setAntiAlias(true);
71 text_paint->setTextAlign(SkPaint::kLeft_Align);
72
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000073 skia::RefPtr<SkTypeface> typeface = skia::AdoptRef(
74 SkTypeface::CreateFromName(kPreferredTypeface, SkTypeface::kBold));
Torne (Richard Coles)58218062012-11-14 11:43:16 +000075 // Skia doesn't do any font fallback---if the user is missing the font then
76 // typeface will be NULL. If we don't do manual fallback then we'll crash.
77 if (typeface) {
78 text_paint->setFakeBoldText(true);
79 } else {
80 // Fall back to the system font. We don't bold it because we aren't sure
81 // how it will look.
82 // For the most part this code path will only be hit on Linux systems
83 // that don't have Arial.
84 ResourceBundle& rb = ResourceBundle::GetSharedInstance();
85 const gfx::Font& base_font = rb.GetFont(ResourceBundle::BaseFont);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000086 typeface = skia::AdoptRef(SkTypeface::CreateFromName(
87 base_font.GetFontName().c_str(), SkTypeface::kNormal));
Torne (Richard Coles)58218062012-11-14 11:43:16 +000088 DCHECK(typeface);
89 }
90
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000091 text_paint->setTypeface(typeface.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +000092 // |text_paint| adds its own ref. Release the ref from CreateFontName.
Torne (Richard Coles)58218062012-11-14 11:43:16 +000093 }
94 return text_paint;
95}
96
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000097void PaintBadge(gfx::Canvas* canvas,
98 const gfx::Rect& bounds,
99 const std::string& text,
100 const SkColor& text_color_in,
101 const SkColor& background_color_in,
102 int icon_width,
103 extensions::ActionInfo::Type action_type) {
104 if (text.empty())
105 return;
106
107 SkColor text_color = text_color_in;
108 if (SkColorGetA(text_color_in) == 0x00)
109 text_color = SK_ColorWHITE;
110
111 SkColor background_color = background_color_in;
112 if (SkColorGetA(background_color_in) == 0x00)
113 background_color = SkColorSetARGB(255, 218, 0, 24);
114
115 canvas->Save();
116
117 SkPaint* text_paint = badge_util::GetBadgeTextPaintSingleton();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000118 text_paint->setColor(text_color);
Ben Murdoch116680a2014-07-20 18:25:52 -0700119 float scale = canvas->image_scale();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000120
Ben Murdoch116680a2014-07-20 18:25:52 -0700121 // Calculate text width. Font width may not be linear with respect to the
122 // scale factor (e.g. when hinting is applied), so we need to use the font
123 // size that canvas actually uses when drawing a text.
124 text_paint->setTextSize(SkFloatToScalar(kTextSize) * scale);
125 SkScalar sk_text_width_in_pixel =
126 text_paint->measureText(text.c_str(), text.size());
127 text_paint->setTextSize(SkFloatToScalar(kTextSize));
128
129 // We clamp the width to a max size. SkPaint::measureText returns the width in
130 // pixel (as a result of scale multiplier), so convert sk_text_width_in_pixel
131 // back to DIP (density independent pixel) first.
132 int text_width =
133 std::min(kMaxTextWidth,
134 static_cast<int>(
135 std::ceil(SkScalarToFloat(sk_text_width_in_pixel) / scale)));
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000136
137 // Calculate badge size. It is clamped to a min width just because it looks
138 // silly if it is too skinny.
139 int badge_width = text_width + kPadding * 2;
140 // Force the pixel width of badge to be either odd (if the icon width is odd)
141 // or even otherwise. If there is a mismatch you get http://crbug.com/26400.
142 if (icon_width != 0 && (badge_width % 2 != icon_width % 2))
143 badge_width += 1;
144 badge_width = std::max(kBadgeHeight, badge_width);
145
146 // Paint the badge background color in the right location. It is usually
147 // right-aligned, but it can also be center-aligned if it is large.
148 int rect_height = kBadgeHeight;
149 int bottom_margin =
150 action_type == extensions::ActionInfo::TYPE_BROWSER ?
151 kBottomMarginBrowserAction : kBottomMarginPageAction;
152 int rect_y = bounds.bottom() - bottom_margin - kBadgeHeight;
153 int rect_width = badge_width;
154 int rect_x = (badge_width >= kCenterAlignThreshold) ?
155 bounds.x() + (bounds.width() - badge_width) / 2 :
156 bounds.right() - badge_width;
157 gfx::Rect rect(rect_x, rect_y, rect_width, rect_height);
158
159 SkPaint rect_paint;
160 rect_paint.setStyle(SkPaint::kFill_Style);
161 rect_paint.setAntiAlias(true);
162 rect_paint.setColor(background_color);
163 canvas->DrawRoundRect(rect, 2, rect_paint);
164
165 // Overlay the gradient. It is stretchy, so we do this in three parts.
166 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
167 gfx::ImageSkia* gradient_left = rb.GetImageSkiaNamed(
168 IDR_BROWSER_ACTION_BADGE_LEFT);
169 gfx::ImageSkia* gradient_right = rb.GetImageSkiaNamed(
170 IDR_BROWSER_ACTION_BADGE_RIGHT);
171 gfx::ImageSkia* gradient_center = rb.GetImageSkiaNamed(
172 IDR_BROWSER_ACTION_BADGE_CENTER);
173
174 canvas->DrawImageInt(*gradient_left, rect.x(), rect.y());
175 canvas->TileImageInt(*gradient_center,
176 rect.x() + gradient_left->width(),
177 rect.y(),
178 rect.width() - gradient_left->width() - gradient_right->width(),
179 rect.height());
180 canvas->DrawImageInt(*gradient_right,
181 rect.right() - gradient_right->width(), rect.y());
182
183 // Finally, draw the text centered within the badge. We set a clip in case the
184 // text was too large.
185 rect.Inset(kPadding, 0);
186 canvas->ClipRect(rect);
187 canvas->sk_canvas()->drawText(
188 text.c_str(), text.size(),
189 SkFloatToScalar(rect.x() +
190 static_cast<float>(rect.width() - text_width) / 2),
191 SkFloatToScalar(rect.y() + kTextSize + kTopTextPadding),
192 *text_paint);
193 canvas->Restore();
194}
195
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000196} // namespace badge_util