Chromium Code Reviews| 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" |
| 11 #include "third_party/skia/include/core/SkTypeface.h" | 11 #include "third_party/skia/include/core/SkTypeface.h" |
| 12 #include "ui/base/resource/resource_bundle.h" | 12 #include "ui/base/resource/resource_bundle.h" |
| 13 #include "ui/gfx/canvas.h" | 13 #include "ui/gfx/canvas.h" |
| 14 #include "ui/gfx/font.h" | 14 #include "ui/gfx/font.h" |
| 15 #include "ui/gfx/rect.h" | 15 #include "ui/gfx/rect.h" |
| 16 #include "ui/gfx/size.h" | 16 #include "ui/gfx/size.h" |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 // Different platforms need slightly different constants to look good. | 20 // Different platforms need slightly different constants to look good. |
| 21 #if defined(OS_LINUX) && !defined(TOOLKIT_VIEWS) | 21 #if defined(OS_CHROMEOS) |
|
Lei Zhang
2014/05/23 21:23:02
Can we restructure this?
#if defined(OS_WIN)
#eli
| |
| 22 const float kTextSize = 8.0; | |
| 23 const int kBottomMarginBrowserAction = 0; | |
| 24 const int kBottomMarginPageAction = 2; | |
| 25 const int kPadding = 2; | |
| 26 const int kTopTextPadding = 1; | |
| 27 #elif defined(OS_LINUX) | |
| 22 const float kTextSize = 9.0; | 28 const float kTextSize = 9.0; |
| 23 const int kBottomMarginBrowserAction = 0; | 29 const int kBottomMarginBrowserAction = 0; |
| 24 const int kBottomMarginPageAction = 2; | 30 const int kBottomMarginPageAction = 2; |
| 25 const int kPadding = 2; | 31 const int kPadding = 2; |
| 26 const int kTopTextPadding = 0; | 32 const int kTopTextPadding = 0; |
| 27 #elif defined(OS_LINUX) && defined(TOOLKIT_VIEWS) | |
| 28 const float kTextSize = 8.0; | |
| 29 const int kBottomMarginBrowserAction = 0; | |
| 30 const int kBottomMarginPageAction = 2; | |
| 31 const int kPadding = 2; | |
| 32 const int kTopTextPadding = 1; | |
| 33 #elif defined(OS_MACOSX) | 33 #elif defined(OS_MACOSX) |
| 34 const float kTextSize = 9.0; | 34 const float kTextSize = 9.0; |
| 35 const int kBottomMarginBrowserAction = 5; | 35 const int kBottomMarginBrowserAction = 5; |
| 36 const int kBottomMarginPageAction = 2; | 36 const int kBottomMarginPageAction = 2; |
| 37 const int kPadding = 2; | 37 const int kPadding = 2; |
| 38 const int kTopTextPadding = 0; | 38 const int kTopTextPadding = 0; |
| 39 #else | 39 #else |
| 40 const float kTextSize = 10; | 40 const float kTextSize = 10; |
| 41 const int kBottomMarginBrowserAction = 0; | 41 const int kBottomMarginBrowserAction = 0; |
| 42 const int kBottomMarginPageAction = 2; | 42 const int kBottomMarginPageAction = 2; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 85 base_font.GetFontName().c_str(), SkTypeface::kNormal)); | 85 base_font.GetFontName().c_str(), SkTypeface::kNormal)); |
| 86 DCHECK(typeface); | 86 DCHECK(typeface); |
| 87 } | 87 } |
| 88 | 88 |
| 89 text_paint->setTypeface(typeface.get()); | 89 text_paint->setTypeface(typeface.get()); |
| 90 // |text_paint| adds its own ref. Release the ref from CreateFontName. | 90 // |text_paint| adds its own ref. Release the ref from CreateFontName. |
| 91 } | 91 } |
| 92 return text_paint; | 92 return text_paint; |
| 93 } | 93 } |
| 94 | 94 |
| 95 SkBitmap DrawBadgeIconOverlay(const SkBitmap& icon, | |
| 96 float font_size, | |
| 97 const base::string16& text, | |
| 98 const base::string16& fallback) { | |
| 99 const int kMinPadding = 1; | |
| 100 | |
| 101 // Calculate the proper style/text overlay to render on the badge. | |
| 102 SkPaint* paint = badge_util::GetBadgeTextPaintSingleton(); | |
| 103 paint->setTextSize(SkFloatToScalar(font_size)); | |
| 104 paint->setColor(SK_ColorWHITE); | |
| 105 | |
| 106 std::string badge_text = base::UTF16ToUTF8(text); | |
| 107 | |
| 108 // See if the text will fit - otherwise use a default. | |
| 109 SkScalar text_width = paint->measureText(badge_text.c_str(), | |
| 110 badge_text.size()); | |
| 111 | |
| 112 if (SkScalarRoundToInt(text_width) > (icon.width() - kMinPadding * 2)) { | |
| 113 // String is too large - use the alternate text. | |
| 114 badge_text = base::UTF16ToUTF8(fallback); | |
| 115 text_width = paint->measureText(badge_text.c_str(), badge_text.size()); | |
| 116 } | |
| 117 | |
| 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 | |
| 120 // padding would be uneven, clip one pixel off the right side. | |
| 121 int badge_width = icon.width(); | |
| 122 if ((SkScalarRoundToInt(text_width) % 1) != (badge_width % 1)) | |
| 123 badge_width--; | |
| 124 | |
| 125 // Render the badge bitmap and overlay into a canvas. | |
| 126 scoped_ptr<gfx::Canvas> canvas(new gfx::Canvas( | |
| 127 gfx::Size(badge_width, icon.height()), 1.0f, false)); | |
| 128 canvas->DrawImageInt(gfx::ImageSkia::CreateFrom1xBitmap(icon), 0, 0); | |
| 129 | |
| 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 | |
| 132 // add 'font_size - 1' to the height. | |
| 133 SkScalar x = (badge_width - text_width)/2; | |
| 134 SkScalar y = (icon.height() - font_size)/2 + font_size - 1; | |
| 135 canvas->sk_canvas()->drawText( | |
| 136 badge_text.c_str(), badge_text.size(), x, y, *paint); | |
| 137 | |
| 138 // Return the generated image. | |
| 139 return canvas->ExtractImageRep().sk_bitmap(); | |
| 140 } | |
| 141 | |
| 142 void PaintBadge(gfx::Canvas* canvas, | 95 void PaintBadge(gfx::Canvas* canvas, |
| 143 const gfx::Rect& bounds, | 96 const gfx::Rect& bounds, |
| 144 const std::string& text, | 97 const std::string& text, |
| 145 const SkColor& text_color_in, | 98 const SkColor& text_color_in, |
| 146 const SkColor& background_color_in, | 99 const SkColor& background_color_in, |
| 147 int icon_width, | 100 int icon_width, |
| 148 extensions::ActionInfo::Type action_type) { | 101 extensions::ActionInfo::Type action_type) { |
| 149 if (text.empty()) | 102 if (text.empty()) |
| 150 return; | 103 return; |
| 151 | 104 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 220 canvas->sk_canvas()->drawText( | 173 canvas->sk_canvas()->drawText( |
| 221 text.c_str(), text.size(), | 174 text.c_str(), text.size(), |
| 222 SkFloatToScalar(rect.x() + | 175 SkFloatToScalar(rect.x() + |
| 223 static_cast<float>(rect.width() - text_width) / 2), | 176 static_cast<float>(rect.width() - text_width) / 2), |
| 224 SkFloatToScalar(rect.y() + kTextSize + kTopTextPadding), | 177 SkFloatToScalar(rect.y() + kTextSize + kTopTextPadding), |
| 225 *text_paint); | 178 *text_paint); |
| 226 canvas->Restore(); | 179 canvas->Restore(); |
| 227 } | 180 } |
| 228 | 181 |
| 229 } // namespace badge_util | 182 } // namespace badge_util |
| OLD | NEW |