OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/common/badge_util.h" | |
6 | |
7 #include <cmath> | |
8 | |
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" | |
19 | |
20 namespace { | |
21 | |
22 // Different platforms need slightly different constants to look good. | |
23 // TODO(devlin): Comb through these and see if they are all still needed/ | |
24 // appropriate. | |
25 #if defined(OS_WIN) | |
26 const float kTextSize = 10; | |
27 // The padding between the top of the badge and the top of the text. | |
28 const int kTopTextPadding = -1; | |
29 #elif defined(OS_MACOSX) | |
30 const float kTextSize = 9.0; | |
31 const int kTopTextPadding = 0; | |
32 #elif defined(OS_CHROMEOS) | |
33 const float kTextSize = 8.0; | |
34 const int kTopTextPadding = 1; | |
35 #elif defined(OS_POSIX) | |
36 const float kTextSize = 9.0; | |
37 const int kTopTextPadding = 0; | |
38 #endif | |
39 | |
40 const int kPadding = 2; | |
41 const int kBadgeHeight = 11; | |
42 const int kMaxTextWidth = 23; | |
43 | |
44 // The minimum width for center-aligning the badge. | |
45 const int kCenterAlignThreshold = 20; | |
46 | |
47 } // namespace | |
48 | |
49 namespace badge_util { | |
50 | |
51 SkPaint* GetBadgeTextPaintSingleton() { | |
52 #if defined(OS_MACOSX) | |
53 const char kPreferredTypeface[] = "Helvetica Bold"; | |
54 #else | |
55 const char kPreferredTypeface[] = "Arial"; | |
56 #endif | |
57 | |
58 static SkPaint* text_paint = NULL; | |
59 if (!text_paint) { | |
60 text_paint = new SkPaint; | |
61 text_paint->setAntiAlias(true); | |
62 text_paint->setTextAlign(SkPaint::kLeft_Align); | |
63 | |
64 skia::RefPtr<SkTypeface> typeface = skia::AdoptRef( | |
65 SkTypeface::CreateFromName(kPreferredTypeface, SkTypeface::kBold)); | |
66 // Skia doesn't do any font fallback---if the user is missing the font then | |
67 // typeface will be NULL. If we don't do manual fallback then we'll crash. | |
68 if (typeface) { | |
69 text_paint->setFakeBoldText(true); | |
70 } else { | |
71 // Fall back to the system font. We don't bold it because we aren't sure | |
72 // how it will look. | |
73 // For the most part this code path will only be hit on Linux systems | |
74 // that don't have Arial. | |
75 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
76 const gfx::Font& base_font = rb.GetFont(ResourceBundle::BaseFont); | |
77 typeface = skia::AdoptRef(SkTypeface::CreateFromName( | |
78 base_font.GetFontName().c_str(), SkTypeface::kNormal)); | |
79 DCHECK(typeface); | |
80 } | |
81 | |
82 text_paint->setTypeface(typeface.get()); | |
83 // |text_paint| adds its own ref. Release the ref from CreateFontName. | |
84 } | |
85 return text_paint; | |
86 } | |
87 | |
88 void PaintBadge(gfx::Canvas* canvas, | |
89 const gfx::Rect& bounds, | |
90 const std::string& text, | |
91 const SkColor& text_color_in, | |
92 const SkColor& background_color_in, | |
93 int icon_width) { | |
94 if (text.empty()) | |
95 return; | |
96 | |
97 SkColor text_color = text_color_in; | |
98 if (SkColorGetA(text_color_in) == 0x00) | |
99 text_color = SK_ColorWHITE; | |
100 | |
101 SkColor background_color = background_color_in; | |
102 if (SkColorGetA(background_color_in) == 0x00) | |
103 background_color = SkColorSetARGB(255, 218, 0, 24); | |
104 | |
105 canvas->Save(); | |
106 | |
107 SkPaint* text_paint = badge_util::GetBadgeTextPaintSingleton(); | |
108 text_paint->setColor(text_color); | |
109 float scale = canvas->image_scale(); | |
110 | |
111 // Calculate text width. Font width may not be linear with respect to the | |
112 // scale factor (e.g. when hinting is applied), so we need to use the font | |
113 // size that canvas actually uses when drawing a text. | |
114 text_paint->setTextSize(SkFloatToScalar(kTextSize) * scale); | |
115 SkScalar sk_text_width_in_pixel = | |
116 text_paint->measureText(text.c_str(), text.size()); | |
117 text_paint->setTextSize(SkFloatToScalar(kTextSize)); | |
118 | |
119 // We clamp the width to a max size. SkPaint::measureText returns the width in | |
120 // pixel (as a result of scale multiplier), so convert sk_text_width_in_pixel | |
121 // back to DIP (density independent pixel) first. | |
122 int text_width = | |
123 std::min(kMaxTextWidth, | |
124 static_cast<int>( | |
125 std::ceil(SkScalarToFloat(sk_text_width_in_pixel) / scale))); | |
126 | |
127 // Calculate badge size. It is clamped to a min width just because it looks | |
128 // silly if it is too skinny. | |
129 int badge_width = text_width + kPadding * 2; | |
130 // Force the pixel width of badge to be either odd (if the icon width is odd) | |
131 // or even otherwise. If there is a mismatch you get http://crbug.com/26400. | |
132 if (icon_width != 0 && (badge_width % 2 != icon_width % 2)) | |
133 badge_width += 1; | |
134 badge_width = std::max(kBadgeHeight, badge_width); | |
135 | |
136 // Paint the badge background color in the right location. It is usually | |
137 // right-aligned, but it can also be center-aligned if it is large. | |
138 int rect_height = kBadgeHeight; | |
139 int rect_y = bounds.bottom() - kBadgeHeight; | |
140 int rect_width = badge_width; | |
141 int rect_x = (badge_width >= kCenterAlignThreshold) ? | |
142 bounds.x() + (bounds.width() - badge_width) / 2 : | |
143 bounds.right() - badge_width; | |
144 gfx::Rect rect(rect_x, rect_y, rect_width, rect_height); | |
145 | |
146 SkPaint rect_paint; | |
147 rect_paint.setStyle(SkPaint::kFill_Style); | |
148 rect_paint.setAntiAlias(true); | |
149 rect_paint.setColor(background_color); | |
150 canvas->DrawRoundRect(rect, 2, rect_paint); | |
151 | |
152 // Overlay the gradient. It is stretchy, so we do this in three parts. | |
153 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
154 gfx::ImageSkia* gradient_left = rb.GetImageSkiaNamed( | |
155 IDR_BROWSER_ACTION_BADGE_LEFT); | |
156 gfx::ImageSkia* gradient_right = rb.GetImageSkiaNamed( | |
157 IDR_BROWSER_ACTION_BADGE_RIGHT); | |
158 gfx::ImageSkia* gradient_center = rb.GetImageSkiaNamed( | |
159 IDR_BROWSER_ACTION_BADGE_CENTER); | |
160 | |
161 canvas->DrawImageInt(*gradient_left, rect.x(), rect.y()); | |
162 canvas->TileImageInt(*gradient_center, | |
163 rect.x() + gradient_left->width(), | |
164 rect.y(), | |
165 rect.width() - gradient_left->width() - gradient_right->width(), | |
166 rect.height()); | |
167 canvas->DrawImageInt(*gradient_right, | |
168 rect.right() - gradient_right->width(), rect.y()); | |
169 | |
170 // Finally, draw the text centered within the badge. We set a clip in case the | |
171 // text was too large. | |
172 rect.Inset(kPadding, 0); | |
173 canvas->ClipRect(rect); | |
174 canvas->sk_canvas()->drawText( | |
175 text.c_str(), text.size(), | |
176 SkFloatToScalar(rect.x() + | |
177 static_cast<float>(rect.width() - text_width) / 2), | |
178 SkFloatToScalar(rect.y() + kTextSize + kTopTextPadding), | |
179 *text_paint); | |
180 canvas->Restore(); | |
181 } | |
182 | |
183 } // namespace badge_util | |
OLD | NEW |