Added stub MediaSourcePlayer for developing behind the flag
[chromium-blink-merge.git] / chrome / common / badge_util.cc
blobbf295917b6100f80af619e264d6a2792a621ed60
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 <cmath>
9 #include "base/logging.h"
10 #include "base/strings/utf_string_conversions.h"
11 #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"
16 #include "ui/gfx/geometry/rect.h"
17 #include "ui/gfx/geometry/size.h"
18 #include "ui/resources/grit/ui_resources.h"
20 namespace {
22 // Different platforms need slightly different constants to look good.
23 #if defined(OS_WIN)
24 const float kTextSize = 10;
25 const int kBottomMarginBrowserAction = 0;
26 const int kBottomMarginPageAction = 2;
27 const int kPadding = 2;
28 // The padding between the top of the badge and the top of the text.
29 const int kTopTextPadding = -1;
30 #elif defined(OS_MACOSX)
31 const float kTextSize = 9.0;
32 const int kBottomMarginBrowserAction = 5;
33 const int kBottomMarginPageAction = 2;
34 const int kPadding = 2;
35 const int kTopTextPadding = 0;
36 #elif defined(OS_CHROMEOS)
37 const float kTextSize = 8.0;
38 const int kBottomMarginBrowserAction = 0;
39 const int kBottomMarginPageAction = 2;
40 const int kPadding = 2;
41 const int kTopTextPadding = 1;
42 #elif defined(OS_POSIX)
43 const float kTextSize = 9.0;
44 const int kBottomMarginBrowserAction = 0;
45 const int kBottomMarginPageAction = 2;
46 const int kPadding = 2;
47 const int kTopTextPadding = 0;
48 #endif
50 const int kBadgeHeight = 11;
51 const int kMaxTextWidth = 23;
53 // The minimum width for center-aligning the badge.
54 const int kCenterAlignThreshold = 20;
56 } // namespace
58 namespace badge_util {
60 SkPaint* GetBadgeTextPaintSingleton() {
61 #if defined(OS_MACOSX)
62 const char kPreferredTypeface[] = "Helvetica Bold";
63 #else
64 const char kPreferredTypeface[] = "Arial";
65 #endif
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);
73 skia::RefPtr<SkTypeface> typeface = skia::AdoptRef(
74 SkTypeface::CreateFromName(kPreferredTypeface, SkTypeface::kBold));
75 // 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);
86 typeface = skia::AdoptRef(SkTypeface::CreateFromName(
87 base_font.GetFontName().c_str(), SkTypeface::kNormal));
88 DCHECK(typeface);
91 text_paint->setTypeface(typeface.get());
92 // |text_paint| adds its own ref. Release the ref from CreateFontName.
94 return text_paint;
97 void 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;
107 SkColor text_color = text_color_in;
108 if (SkColorGetA(text_color_in) == 0x00)
109 text_color = SK_ColorWHITE;
111 SkColor background_color = background_color_in;
112 if (SkColorGetA(background_color_in) == 0x00)
113 background_color = SkColorSetARGB(255, 218, 0, 24);
115 canvas->Save();
117 SkPaint* text_paint = badge_util::GetBadgeTextPaintSingleton();
118 text_paint->setColor(text_color);
119 float scale = canvas->image_scale();
121 // 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));
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)));
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);
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);
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);
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);
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());
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();
196 } // namespace badge_util