1 // Copyright 2015 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.
5 #include "extensions/browser/extension_icon_placeholder.h"
7 #include "base/strings/string_util.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "extensions/grit/extensions_browser_resources.h"
10 #include "ui/base/resource/resource_bundle.h"
11 #include "ui/gfx/canvas.h"
12 #include "ui/gfx/codec/png_codec.h"
13 #include "ui/gfx/geometry/rect.h"
14 #include "ui/gfx/geometry/size.h"
15 #include "ui/gfx/image/canvas_image_source.h"
16 #include "ui/gfx/image/image_skia.h"
18 namespace extensions
{
22 // Returns the FontStyle to use for the given icon |size|.
23 ui::ResourceBundle::FontStyle
GetFontStyleForIconSize(
24 extension_misc::ExtensionIcons size
) {
26 case extension_misc::EXTENSION_ICON_INVALID
:
27 case extension_misc::EXTENSION_ICON_BITTY
:
28 return ui::ResourceBundle::SmallFont
;
29 case extension_misc::EXTENSION_ICON_ACTION
:
30 case extension_misc::EXTENSION_ICON_SMALLISH
:
31 case extension_misc::EXTENSION_ICON_SMALL
:
32 return ui::ResourceBundle::MediumFont
;
33 case extension_misc::EXTENSION_ICON_MEDIUM
:
34 case extension_misc::EXTENSION_ICON_LARGE
:
35 case extension_misc::EXTENSION_ICON_EXTRA_LARGE
:
36 case extension_misc::EXTENSION_ICON_GIGANTOR
:
37 return ui::ResourceBundle::LargeFont
;
40 return ui::ResourceBundle::MediumFont
;
43 // Returns the background image to use for the given icon |size|.
44 gfx::Image
GetBackgroundImageForIconSize(extension_misc::ExtensionIcons size
) {
46 // Right now, we have resources for a 19x19 (action) and a 48x48 (extensions
47 // page icon). The implementation of the placeholder scales these correctly,
48 // so it's not a big deal to use these for other sizes, but if it's something
49 // that will be done frequently, we should probably make a devoted asset for
52 case extension_misc::EXTENSION_ICON_INVALID
:
53 case extension_misc::EXTENSION_ICON_BITTY
:
54 case extension_misc::EXTENSION_ICON_ACTION
:
55 case extension_misc::EXTENSION_ICON_SMALLISH
:
56 case extension_misc::EXTENSION_ICON_SMALL
:
57 resource_id
= IDR_EXTENSION_ACTION_PLAIN_BACKGROUND
;
59 case extension_misc::EXTENSION_ICON_MEDIUM
:
60 case extension_misc::EXTENSION_ICON_LARGE
:
61 case extension_misc::EXTENSION_ICON_EXTRA_LARGE
:
62 case extension_misc::EXTENSION_ICON_GIGANTOR
:
63 resource_id
= IDR_EXTENSION_ICON_PLAIN_BACKGROUND
;
66 return ui::ResourceBundle::GetSharedInstance().GetImageNamed(resource_id
);
71 ExtensionIconPlaceholder::ExtensionIconPlaceholder(
72 extension_misc::ExtensionIcons size
,
73 const std::string
& letter
)
74 : gfx::CanvasImageSource(gfx::Size(size
, size
), false),
76 letter_(base::UTF8ToUTF16(letter
.substr(0, 1))),
77 base_image_(GetBackgroundImageForIconSize(size
)) {
80 ExtensionIconPlaceholder::~ExtensionIconPlaceholder() {
83 gfx::Image
ExtensionIconPlaceholder::CreateImage(
84 extension_misc::ExtensionIcons size
,
85 const std::string
& name
) {
86 return gfx::Image(gfx::ImageSkia(new ExtensionIconPlaceholder(size
, name
),
87 gfx::Size(size
, size
)));
90 void ExtensionIconPlaceholder::Draw(gfx::Canvas
* canvas
) {
91 // Draw the background image, correctly scaled.
92 canvas
->DrawImageInt(*base_image_
.ToImageSkia(), 0, 0,
93 base_image_
.Size().width(), base_image_
.Size().height(),
94 0, 0, size().width(), size().height(), true);
95 gfx::Rect
bounds(size().width(), size().height());
96 // Draw the letter on top.
97 canvas
->DrawStringRectWithFlags(
98 letter_
, ui::ResourceBundle::GetSharedInstance().GetFontList(
99 GetFontStyleForIconSize(icon_size_
)),
100 SK_ColorWHITE
, bounds
, gfx::Canvas::TEXT_ALIGN_CENTER
);
103 } // namespace extensions