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 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 float font_size, | 96 float font_size, |
97 const base::string16& text, | 97 const base::string16& text, |
98 const base::string16& fallback) { | 98 const base::string16& fallback) { |
99 const int kMinPadding = 1; | 99 const int kMinPadding = 1; |
100 | 100 |
101 // Calculate the proper style/text overlay to render on the badge. | 101 // Calculate the proper style/text overlay to render on the badge. |
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 = 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 (SkScalarRound(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 = 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 ((SkScalarRound(text_width) % 1) != (badge_width % 1)) |
123 badge_width--; | 123 badge_width--; |
124 | 124 |
(...skipping 95 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 |