extension_icon_placeholder.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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.
  4. #include "extensions/browser/extension_icon_placeholder.h"
  5. #include "base/i18n/rtl.h"
  6. #include "base/strings/string_util.h"
  7. #include "base/strings/utf_string_conversions.h"
  8. #include "extensions/grit/extensions_browser_resources.h"
  9. #include "ui/base/resource/resource_bundle.h"
  10. #include "ui/gfx/canvas.h"
  11. #include "ui/gfx/codec/png_codec.h"
  12. #include "ui/gfx/geometry/rect.h"
  13. #include "ui/gfx/geometry/size.h"
  14. #include "ui/gfx/image/canvas_image_source.h"
  15. #include "ui/gfx/image/image_skia.h"
  16. namespace extensions {
  17. namespace {
  18. // Returns the FontStyle to use for the given icon |size|.
  19. ui::ResourceBundle::FontStyle GetFontStyleForIconSize(
  20. extension_misc::ExtensionIcons size) {
  21. switch (size) {
  22. case extension_misc::EXTENSION_ICON_INVALID:
  23. case extension_misc::EXTENSION_ICON_BITTY:
  24. return ui::ResourceBundle::SmallFont;
  25. case extension_misc::EXTENSION_ICON_SMALLISH:
  26. case extension_misc::EXTENSION_ICON_SMALL:
  27. return ui::ResourceBundle::MediumFont;
  28. case extension_misc::EXTENSION_ICON_MEDIUM:
  29. case extension_misc::EXTENSION_ICON_LARGE:
  30. case extension_misc::EXTENSION_ICON_EXTRA_LARGE:
  31. case extension_misc::EXTENSION_ICON_GIGANTOR:
  32. return ui::ResourceBundle::LargeFont;
  33. }
  34. NOTREACHED();
  35. return ui::ResourceBundle::MediumFont;
  36. }
  37. // Returns the background image to use for the given icon |size|.
  38. gfx::Image GetBackgroundImageForIconSize(extension_misc::ExtensionIcons size) {
  39. int resource_id = 0;
  40. // Right now, we have resources for a 19x19 (action) and a 48x48 (extensions
  41. // page icon). The implementation of the placeholder scales these correctly,
  42. // so it's not a big deal to use these for other sizes, but if it's something
  43. // that will be done frequently, we should probably make a devoted asset for
  44. // that size.
  45. switch (size) {
  46. case extension_misc::EXTENSION_ICON_INVALID:
  47. case extension_misc::EXTENSION_ICON_BITTY:
  48. case extension_misc::EXTENSION_ICON_SMALLISH:
  49. case extension_misc::EXTENSION_ICON_SMALL:
  50. resource_id = IDR_EXTENSION_ACTION_PLAIN_BACKGROUND;
  51. break;
  52. case extension_misc::EXTENSION_ICON_MEDIUM:
  53. case extension_misc::EXTENSION_ICON_LARGE:
  54. case extension_misc::EXTENSION_ICON_EXTRA_LARGE:
  55. case extension_misc::EXTENSION_ICON_GIGANTOR:
  56. resource_id = IDR_EXTENSION_ICON_PLAIN_BACKGROUND;
  57. break;
  58. }
  59. return ui::ResourceBundle::GetSharedInstance().GetImageNamed(resource_id);
  60. }
  61. } // namespace
  62. ExtensionIconPlaceholder::ExtensionIconPlaceholder(
  63. extension_misc::ExtensionIcons size,
  64. const std::string& name)
  65. : gfx::CanvasImageSource(gfx::Size(size, size)),
  66. icon_size_(size),
  67. base_image_(GetBackgroundImageForIconSize(size)) {
  68. // Remove RTL formatting characters, if any, that may pad the extension name.
  69. // See https://crbug.com/869358
  70. std::u16string sanitized_name = base::UTF8ToUTF16(std::string(name));
  71. base::i18n::UnadjustStringForLocaleDirection(&sanitized_name);
  72. letter_ = sanitized_name.substr(0, 1);
  73. }
  74. ExtensionIconPlaceholder::~ExtensionIconPlaceholder() {
  75. }
  76. gfx::Image ExtensionIconPlaceholder::CreateImage(
  77. extension_misc::ExtensionIcons size,
  78. const std::string& name) {
  79. return gfx::Image(
  80. gfx::ImageSkia(std::make_unique<ExtensionIconPlaceholder>(size, name),
  81. gfx::Size(size, size)));
  82. }
  83. void ExtensionIconPlaceholder::Draw(gfx::Canvas* canvas) {
  84. // Draw the background image, correctly scaled.
  85. canvas->DrawImageInt(*base_image_.ToImageSkia(), 0, 0,
  86. base_image_.Size().width(), base_image_.Size().height(),
  87. 0, 0, size().width(), size().height(), true);
  88. gfx::Rect bounds(size().width(), size().height());
  89. // Draw the letter on top.
  90. canvas->DrawStringRectWithFlags(
  91. letter_, ui::ResourceBundle::GetSharedInstance().GetFontList(
  92. GetFontStyleForIconSize(icon_size_)),
  93. SK_ColorWHITE, bounds, gfx::Canvas::TEXT_ALIGN_CENTER);
  94. }
  95. } // namespace extensions