OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
Devlin
2015/11/24 17:26:15
Can you reduce the similarity count so that Rietve
Evan Stade
2015/11/24 18:24:14
Done, but it decided it was a move of badge_util.c
| |
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/browser/ui/extensions/icon_with_badge_image_source.h" | |
6 | |
7 #include <algorithm> | |
8 #include <cmath> | |
9 | |
10 #include "base/logging.h" | |
11 #include "base/strings/utf_string_conversions.h" | |
12 #include "third_party/skia/include/core/SkPaint.h" | |
13 #include "third_party/skia/include/core/SkTypeface.h" | |
14 #include "ui/base/resource/material_design/material_design_controller.h" | |
15 #include "ui/base/resource/resource_bundle.h" | |
16 #include "ui/gfx/canvas.h" | |
17 #include "ui/gfx/color_palette.h" | |
18 #include "ui/gfx/font.h" | |
19 #include "ui/gfx/geometry/rect.h" | |
20 #include "ui/gfx/geometry/size.h" | |
21 #include "ui/gfx/image/image_skia_operations.h" | |
22 #include "ui/resources/grit/ui_resources.h" | |
23 | |
24 namespace { | |
25 | |
26 // Different platforms need slightly different constants to look good. | |
27 // TODO(devlin): Comb through these and see if they are all still needed/ | |
28 // appropriate. | |
29 #if defined(OS_WIN) | |
30 const float kTextSize = 10; | |
31 // The padding between the top of the badge and the top of the text. | |
32 const int kTopTextPadding = -1; | |
33 #elif defined(OS_MACOSX) | |
34 const float kTextSize = 9.0; | |
35 const int kTopTextPadding = 0; | |
36 #elif defined(OS_CHROMEOS) | |
37 const float kTextSize = 8.0; | |
38 const int kTopTextPadding = 1; | |
39 #elif defined(OS_POSIX) | |
40 const float kTextSize = 9.0; | |
41 const int kTopTextPadding = 0; | |
42 #endif | |
43 | |
44 const int kPadding = 2; | |
45 const int kBadgeHeight = 11; | |
46 const int kMaxTextWidth = 23; | |
47 | |
48 // The minimum width for center-aligning the badge. | |
49 const int kCenterAlignThreshold = 20; | |
50 | |
51 // Helper routine that returns a singleton SkPaint object configured for | |
52 // rendering badge overlay text (correct font, typeface, etc). | |
53 SkPaint* GetBadgeTextPaintSingleton() { | |
54 #if defined(OS_MACOSX) | |
55 const char kPreferredTypeface[] = "Helvetica Bold"; | |
56 #else | |
57 const char kPreferredTypeface[] = "Arial"; | |
58 #endif | |
59 | |
60 static SkPaint* text_paint = NULL; | |
61 if (!text_paint) { | |
62 text_paint = new SkPaint; | |
63 text_paint->setAntiAlias(true); | |
64 text_paint->setTextAlign(SkPaint::kLeft_Align); | |
65 | |
66 skia::RefPtr<SkTypeface> typeface = skia::AdoptRef( | |
67 SkTypeface::CreateFromName(kPreferredTypeface, SkTypeface::kBold)); | |
68 // Skia doesn't do any font fallback---if the user is missing the font then | |
69 // typeface will be NULL. If we don't do manual fallback then we'll crash. | |
70 if (typeface) { | |
71 text_paint->setFakeBoldText(true); | |
72 } else { | |
73 // Fall back to the system font. We don't bold it because we aren't sure | |
74 // how it will look. | |
75 // For the most part this code path will only be hit on Linux systems | |
76 // that don't have Arial. | |
77 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | |
78 const gfx::Font& base_font = rb.GetFont(ResourceBundle::BaseFont); | |
79 typeface = skia::AdoptRef(SkTypeface::CreateFromName( | |
80 base_font.GetFontName().c_str(), SkTypeface::kNormal)); | |
81 DCHECK(typeface); | |
82 } | |
83 | |
84 text_paint->setTypeface(typeface.get()); | |
85 // |text_paint| adds its own ref. Release the ref from CreateFontName. | |
86 } | |
87 return text_paint; | |
88 } | |
89 | |
90 } // namespace | |
91 | |
92 IconWithBadgeImageSource::Badge::Badge(const std::string& text, | |
93 SkColor text_color, | |
94 SkColor background_color) | |
95 : text(text), text_color(text_color), background_color(background_color) {} | |
96 | |
97 IconWithBadgeImageSource::Badge::~Badge() {} | |
98 | |
99 IconWithBadgeImageSource::IconWithBadgeImageSource(const gfx::Size& size) | |
100 : gfx::CanvasImageSource(size, false), | |
101 grayscale_(false), | |
102 paint_decoration_(false) {} | |
103 | |
104 IconWithBadgeImageSource::~IconWithBadgeImageSource() {} | |
105 | |
106 void IconWithBadgeImageSource::SetIcon(const gfx::Image& icon) { | |
107 icon_ = icon; | |
108 } | |
109 | |
110 void IconWithBadgeImageSource::SetBadge(scoped_ptr<Badge> badge) { | |
111 badge_ = badge.Pass(); | |
112 } | |
113 | |
114 void IconWithBadgeImageSource::Draw(gfx::Canvas* canvas) { | |
115 if (icon_.IsEmpty()) | |
116 return; | |
117 | |
118 int x_offset = std::floor((size().width() - icon_.Width()) / 2.0); | |
119 int y_offset = std::floor((size().height() - icon_.Height()) / 2.0); | |
120 gfx::ImageSkia skia = icon_.AsImageSkia(); | |
121 if (grayscale_) | |
122 skia = gfx::ImageSkiaOperations::CreateHSLShiftedImage(skia, {-1, 0, 0.6}); | |
123 canvas->DrawImageInt(skia, x_offset, y_offset); | |
124 | |
125 // Draw a badge on the provided browser action icon's canvas. | |
126 PaintBadge(canvas); | |
127 | |
128 if (paint_decoration_) | |
129 PaintDecoration(canvas); | |
130 } | |
131 | |
132 // Paints badge with specified parameters to |canvas|. | |
133 void IconWithBadgeImageSource::PaintBadge(gfx::Canvas* canvas) { | |
134 if (!badge_ || badge_->text.empty()) | |
135 return; | |
136 | |
137 SkColor text_color = SkColorGetA(badge_->text_color) == SK_AlphaTRANSPARENT | |
138 ? SK_ColorWHITE | |
139 : badge_->text_color; | |
140 | |
141 SkColor background_color = ui::MaterialDesignController::IsModeMaterial() | |
142 ? gfx::kGoogleBlue500 | |
143 : SkColorSetARGB(255, 218, 0, 24); | |
144 if (SkColorGetA(badge_->background_color) != SK_AlphaTRANSPARENT) | |
145 background_color = badge_->background_color; | |
146 | |
147 canvas->Save(); | |
148 | |
149 SkPaint* text_paint = nullptr; | |
150 int text_width = 0; | |
151 ResourceBundle* rb = &ResourceBundle::GetSharedInstance(); | |
152 gfx::FontList base_font = rb->GetFontList(ResourceBundle::BaseFont) | |
153 .DeriveWithHeightUpperBound(kBadgeHeight); | |
154 base::string16 utf16_text = base::UTF8ToUTF16(badge_->text); | |
155 if (ui::MaterialDesignController::IsModeMaterial()) { | |
156 text_width = | |
157 std::min(kMaxTextWidth, canvas->GetStringWidth(utf16_text, base_font)); | |
158 } else { | |
159 text_paint = GetBadgeTextPaintSingleton(); | |
160 text_paint->setColor(text_color); | |
161 float scale = canvas->image_scale(); | |
162 | |
163 // Calculate text width. Font width may not be linear with respect to the | |
164 // scale factor (e.g. when hinting is applied), so we need to use the font | |
165 // size that canvas actually uses when drawing a text. | |
166 text_paint->setTextSize(SkFloatToScalar(kTextSize) * scale); | |
167 SkScalar sk_text_width_in_pixel = | |
168 text_paint->measureText(badge_->text.c_str(), badge_->text.size()); | |
169 text_paint->setTextSize(SkFloatToScalar(kTextSize)); | |
170 | |
171 // We clamp the width to a max size. SkPaint::measureText returns the width | |
172 // in pixel (as a result of scale multiplier), so convert | |
173 // sk_text_width_in_pixel back to DIP (density independent pixel) first. | |
174 text_width = std::min( | |
175 kMaxTextWidth, static_cast<int>(std::ceil( | |
176 SkScalarToFloat(sk_text_width_in_pixel) / scale))); | |
177 } | |
178 | |
179 // Calculate badge size. It is clamped to a min width just because it looks | |
180 // silly if it is too skinny. | |
181 int badge_width = text_width + kPadding * 2; | |
182 // Force the pixel width of badge to be either odd (if the icon width is odd) | |
183 // or even otherwise. If there is a mismatch you get http://crbug.com/26400. | |
184 if (size().width() != 0 && (badge_width % 2 != size().width() % 2)) | |
185 badge_width += 1; | |
186 badge_width = std::max(kBadgeHeight, badge_width); | |
187 | |
188 // Paint the badge background color in the right location. It is usually | |
189 // right-aligned, but it can also be center-aligned if it is large. | |
190 gfx::Rect rect(badge_width >= kCenterAlignThreshold | |
191 ? (size().width() - badge_width) / 2 | |
192 : size().width() - badge_width, | |
193 size().height() - kBadgeHeight, badge_width, kBadgeHeight); | |
194 SkPaint rect_paint; | |
195 rect_paint.setStyle(SkPaint::kFill_Style); | |
196 rect_paint.setAntiAlias(true); | |
197 rect_paint.setColor(background_color); | |
198 | |
199 if (ui::MaterialDesignController::IsModeMaterial()) { | |
200 gfx::Rect cutout_rect(rect); | |
201 cutout_rect.Inset(-1, -1); | |
202 SkPaint cutout_paint = rect_paint; | |
203 cutout_paint.setXfermodeMode(SkXfermode::kClear_Mode); | |
204 canvas->DrawRoundRect(cutout_rect, 2, cutout_paint); | |
205 } | |
206 | |
207 canvas->DrawRoundRect( | |
208 rect, ui::MaterialDesignController::IsModeMaterial() ? 1 : 2, rect_paint); | |
209 | |
210 if (ui::MaterialDesignController::IsModeMaterial()) { | |
211 rect.Inset(std::max(kPadding, (rect.width() - text_width) / 2), | |
212 kBadgeHeight - base_font.GetHeight(), kPadding, 0); | |
213 canvas->DrawStringRect(utf16_text, base_font, text_color, rect); | |
214 } else { | |
215 // Overlay the gradient. It is stretchy, so we do this in three parts. | |
216 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | |
217 gfx::ImageSkia* gradient_left = | |
218 rb.GetImageSkiaNamed(IDR_BROWSER_ACTION_BADGE_LEFT); | |
219 gfx::ImageSkia* gradient_right = | |
220 rb.GetImageSkiaNamed(IDR_BROWSER_ACTION_BADGE_RIGHT); | |
221 gfx::ImageSkia* gradient_center = | |
222 rb.GetImageSkiaNamed(IDR_BROWSER_ACTION_BADGE_CENTER); | |
223 | |
224 canvas->DrawImageInt(*gradient_left, rect.x(), rect.y()); | |
225 canvas->TileImageInt( | |
226 *gradient_center, rect.x() + gradient_left->width(), rect.y(), | |
227 rect.width() - gradient_left->width() - gradient_right->width(), | |
228 rect.height()); | |
229 canvas->DrawImageInt(*gradient_right, | |
230 rect.right() - gradient_right->width(), rect.y()); | |
231 | |
232 // Finally, draw the text centered within the badge. We set a clip in case | |
233 // the text was too large. | |
234 rect.Inset(kPadding, 0); | |
235 canvas->ClipRect(rect); | |
236 canvas->sk_canvas()->drawText( | |
237 badge_->text.c_str(), badge_->text.size(), | |
238 SkFloatToScalar(rect.x() + | |
239 static_cast<float>(rect.width() - text_width) / 2), | |
240 SkFloatToScalar(rect.y() + kTextSize + kTopTextPadding), *text_paint); | |
241 } | |
242 canvas->Restore(); | |
243 } | |
244 | |
245 void IconWithBadgeImageSource::PaintDecoration(gfx::Canvas* canvas) { | |
246 static const SkColor decoration_color = SkColorSetARGB(255, 70, 142, 226); | |
247 | |
248 int major_radius = std::ceil(size().width() / 5.0); | |
249 int minor_radius = std::ceil(major_radius / 2.0); | |
250 gfx::Point center_point(major_radius + 1, size().height() - (major_radius)-1); | |
251 SkPaint paint; | |
252 paint.setAntiAlias(true); | |
253 paint.setStyle(SkPaint::kFill_Style); | |
254 paint.setColor(SK_ColorTRANSPARENT); | |
255 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | |
256 canvas->DrawCircle(center_point, major_radius, paint); | |
257 paint.setColor(decoration_color); | |
258 canvas->DrawCircle(center_point, minor_radius, paint); | |
259 } | |
OLD | NEW |