OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/badge_util.h" | 5 #include "chrome/common/badge_util.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "grit/ui_resources.h" | 9 #include "grit/ui_resources.h" |
10 #include "third_party/skia/include/core/SkPaint.h" | 10 #include "third_party/skia/include/core/SkPaint.h" |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 SkPaint* paint = badge_util::GetBadgeTextPaintSingleton(); | 102 SkPaint* paint = badge_util::GetBadgeTextPaintSingleton(); |
103 paint->setTextSize(SkFloatToScalar(font_size)); | 103 paint->setTextSize(SkFloatToScalar(font_size)); |
104 paint->setColor(SK_ColorWHITE); | 104 paint->setColor(SK_ColorWHITE); |
105 | 105 |
106 std::string badge_text = base::UTF16ToUTF8(text); | 106 std::string badge_text = base::UTF16ToUTF8(text); |
107 | 107 |
108 // See if the text will fit - otherwise use a default. | 108 // See if the text will fit - otherwise use a default. |
109 SkScalar text_width = paint->measureText(badge_text.c_str(), | 109 SkScalar text_width = paint->measureText(badge_text.c_str(), |
110 badge_text.size()); | 110 badge_text.size()); |
111 | 111 |
112 if (SkScalarRound(text_width) > (icon.width() - kMinPadding * 2)) { | 112 if (SkScalarRoundToInt(text_width) > (icon.width() - kMinPadding * 2)) { |
113 // String is too large - use the alternate text. | 113 // String is too large - use the alternate text. |
114 badge_text = base::UTF16ToUTF8(fallback); | 114 badge_text = base::UTF16ToUTF8(fallback); |
115 text_width = paint->measureText(badge_text.c_str(), badge_text.size()); | 115 text_width = paint->measureText(badge_text.c_str(), badge_text.size()); |
116 } | 116 } |
117 | 117 |
118 // When centering the text, we need to make sure there are an equal number | 118 // When centering the text, we need to make sure there are an equal number |
119 // of pixels on each side as otherwise the text looks off-center. So if the | 119 // of pixels on each side as otherwise the text looks off-center. So if the |
120 // padding would be uneven, clip one pixel off the right side. | 120 // padding would be uneven, clip one pixel off the right side. |
121 int badge_width = icon.width(); | 121 int badge_width = icon.width(); |
122 if ((SkScalarRound(text_width) % 1) != (badge_width % 1)) | 122 if ((SkScalarRoundToInt(text_width) % 1) != (badge_width % 1)) |
123 badge_width--; | 123 badge_width--; |
124 | 124 |
125 // Render the badge bitmap and overlay into a canvas. | 125 // Render the badge bitmap and overlay into a canvas. |
126 scoped_ptr<gfx::Canvas> canvas(new gfx::Canvas( | 126 scoped_ptr<gfx::Canvas> canvas(new gfx::Canvas( |
127 gfx::Size(badge_width, icon.height()), 1.0f, false)); | 127 gfx::Size(badge_width, icon.height()), 1.0f, false)); |
128 canvas->DrawImageInt(gfx::ImageSkia::CreateFrom1xBitmap(icon), 0, 0); | 128 canvas->DrawImageInt(gfx::ImageSkia::CreateFrom1xBitmap(icon), 0, 0); |
129 | 129 |
130 // Draw the text overlay centered horizontally and vertically. Skia expects | 130 // Draw the text overlay centered horizontally and vertically. Skia expects |
131 // us to specify the lower left coordinate of the text box, which is why we | 131 // us to specify the lower left coordinate of the text box, which is why we |
132 // add 'font_size - 1' to the height. | 132 // add 'font_size - 1' to the height. |
(...skipping 25 matching lines...) Expand all Loading... |
158 background_color = SkColorSetARGB(255, 218, 0, 24); | 158 background_color = SkColorSetARGB(255, 218, 0, 24); |
159 | 159 |
160 canvas->Save(); | 160 canvas->Save(); |
161 | 161 |
162 SkPaint* text_paint = badge_util::GetBadgeTextPaintSingleton(); | 162 SkPaint* text_paint = badge_util::GetBadgeTextPaintSingleton(); |
163 text_paint->setTextSize(SkFloatToScalar(kTextSize)); | 163 text_paint->setTextSize(SkFloatToScalar(kTextSize)); |
164 text_paint->setColor(text_color); | 164 text_paint->setColor(text_color); |
165 | 165 |
166 // Calculate text width. We clamp it to a max size. | 166 // Calculate text width. We clamp it to a max size. |
167 SkScalar sk_text_width = text_paint->measureText(text.c_str(), text.size()); | 167 SkScalar sk_text_width = text_paint->measureText(text.c_str(), text.size()); |
168 int text_width = std::min(kMaxTextWidth, SkScalarFloor(sk_text_width)); | 168 int text_width = std::min(kMaxTextWidth, SkScalarFloorToInt(sk_text_width)); |
169 | 169 |
170 // Calculate badge size. It is clamped to a min width just because it looks | 170 // Calculate badge size. It is clamped to a min width just because it looks |
171 // silly if it is too skinny. | 171 // silly if it is too skinny. |
172 int badge_width = text_width + kPadding * 2; | 172 int badge_width = text_width + kPadding * 2; |
173 // Force the pixel width of badge to be either odd (if the icon width is odd) | 173 // Force the pixel width of badge to be either odd (if the icon width is odd) |
174 // or even otherwise. If there is a mismatch you get http://crbug.com/26400. | 174 // or even otherwise. If there is a mismatch you get http://crbug.com/26400. |
175 if (icon_width != 0 && (badge_width % 2 != icon_width % 2)) | 175 if (icon_width != 0 && (badge_width % 2 != icon_width % 2)) |
176 badge_width += 1; | 176 badge_width += 1; |
177 badge_width = std::max(kBadgeHeight, badge_width); | 177 badge_width = std::max(kBadgeHeight, badge_width); |
178 | 178 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 canvas->sk_canvas()->drawText( | 220 canvas->sk_canvas()->drawText( |
221 text.c_str(), text.size(), | 221 text.c_str(), text.size(), |
222 SkFloatToScalar(rect.x() + | 222 SkFloatToScalar(rect.x() + |
223 static_cast<float>(rect.width() - text_width) / 2), | 223 static_cast<float>(rect.width() - text_width) / 2), |
224 SkFloatToScalar(rect.y() + kTextSize + kTopTextPadding), | 224 SkFloatToScalar(rect.y() + kTextSize + kTopTextPadding), |
225 *text_paint); | 225 *text_paint); |
226 canvas->Restore(); | 226 canvas->Restore(); |
227 } | 227 } |
228 | 228 |
229 } // namespace badge_util | 229 } // namespace badge_util |
OLD | NEW |