| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/browser/profiles/profile_avatar_icon_util.h" | 5 #include "chrome/browser/profiles/profile_avatar_icon_util.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "third_party/skia/include/core/SkPath.h" | 24 #include "third_party/skia/include/core/SkPath.h" |
| 25 #include "third_party/skia/include/core/SkScalar.h" | 25 #include "third_party/skia/include/core/SkScalar.h" |
| 26 #include "third_party/skia/include/core/SkXfermode.h" | 26 #include "third_party/skia/include/core/SkXfermode.h" |
| 27 #include "ui/base/l10n/l10n_util.h" | 27 #include "ui/base/l10n/l10n_util.h" |
| 28 #include "ui/base/resource/resource_bundle.h" | 28 #include "ui/base/resource/resource_bundle.h" |
| 29 #include "ui/gfx/canvas.h" | 29 #include "ui/gfx/canvas.h" |
| 30 #include "ui/gfx/geometry/rect.h" | 30 #include "ui/gfx/geometry/rect.h" |
| 31 #include "ui/gfx/image/canvas_image_source.h" | 31 #include "ui/gfx/image/canvas_image_source.h" |
| 32 #include "ui/gfx/image/image.h" | 32 #include "ui/gfx/image/image.h" |
| 33 #include "ui/gfx/image/image_skia_operations.h" | 33 #include "ui/gfx/image/image_skia_operations.h" |
| 34 #include "ui/gfx/path.h" |
| 34 #include "ui/gfx/skia_util.h" | 35 #include "ui/gfx/skia_util.h" |
| 35 #include "url/gurl.h" | 36 #include "url/gurl.h" |
| 36 #include "url/url_canon.h" | 37 #include "url/url_canon.h" |
| 37 | 38 |
| 38 // Helper methods for transforming and drawing avatar icons. | 39 // Helper methods for transforming and drawing avatar icons. |
| 39 namespace { | 40 namespace { |
| 40 | 41 |
| 41 // Path format for specifying thumbnail's size. | 42 // Path format for specifying thumbnail's size. |
| 42 const char kThumbnailSizeFormat[] = "s%d-c"; | 43 const char kThumbnailSizeFormat[] = "s%d-c"; |
| 43 // Default thumbnail size. | 44 // Default thumbnail size. |
| (...skipping 23 matching lines...) Expand all Loading... |
| 67 enum AvatarBorder { | 68 enum AvatarBorder { |
| 68 BORDER_NONE, | 69 BORDER_NONE, |
| 69 BORDER_NORMAL, | 70 BORDER_NORMAL, |
| 70 BORDER_ETCHED, | 71 BORDER_ETCHED, |
| 71 }; | 72 }; |
| 72 | 73 |
| 73 AvatarImageSource(gfx::ImageSkia avatar, | 74 AvatarImageSource(gfx::ImageSkia avatar, |
| 74 const gfx::Size& canvas_size, | 75 const gfx::Size& canvas_size, |
| 75 int width, | 76 int width, |
| 76 AvatarPosition position, | 77 AvatarPosition position, |
| 78 AvatarBorder border, |
| 79 profiles::AvatarShape shape); |
| 80 |
| 81 AvatarImageSource(gfx::ImageSkia avatar, |
| 82 const gfx::Size& canvas_size, |
| 83 int width, |
| 84 AvatarPosition position, |
| 77 AvatarBorder border); | 85 AvatarBorder border); |
| 86 |
| 78 ~AvatarImageSource() override; | 87 ~AvatarImageSource() override; |
| 79 | 88 |
| 80 // CanvasImageSource override: | 89 // CanvasImageSource override: |
| 81 void Draw(gfx::Canvas* canvas) override; | 90 void Draw(gfx::Canvas* canvas) override; |
| 82 | 91 |
| 83 private: | 92 private: |
| 84 gfx::ImageSkia avatar_; | 93 gfx::ImageSkia avatar_; |
| 85 const gfx::Size canvas_size_; | 94 const gfx::Size canvas_size_; |
| 86 const int width_; | 95 const int width_; |
| 87 const int height_; | 96 const int height_; |
| 88 const AvatarPosition position_; | 97 const AvatarPosition position_; |
| 89 const AvatarBorder border_; | 98 const AvatarBorder border_; |
| 99 const profiles::AvatarShape shape_; |
| 90 | 100 |
| 91 DISALLOW_COPY_AND_ASSIGN(AvatarImageSource); | 101 DISALLOW_COPY_AND_ASSIGN(AvatarImageSource); |
| 92 }; | 102 }; |
| 93 | 103 |
| 94 AvatarImageSource::AvatarImageSource(gfx::ImageSkia avatar, | 104 AvatarImageSource::AvatarImageSource(gfx::ImageSkia avatar, |
| 95 const gfx::Size& canvas_size, | 105 const gfx::Size& canvas_size, |
| 96 int width, | 106 int width, |
| 97 AvatarPosition position, | 107 AvatarPosition position, |
| 98 AvatarBorder border) | 108 AvatarBorder border, |
| 109 profiles::AvatarShape shape) |
| 99 : gfx::CanvasImageSource(canvas_size, false), | 110 : gfx::CanvasImageSource(canvas_size, false), |
| 100 canvas_size_(canvas_size), | 111 canvas_size_(canvas_size), |
| 101 width_(width), | 112 width_(width), |
| 102 height_(GetScaledAvatarHeightForWidth(width, avatar)), | 113 height_(GetScaledAvatarHeightForWidth(width, avatar)), |
| 103 position_(position), | 114 position_(position), |
| 104 border_(border) { | 115 border_(border), |
| 116 shape_(shape) { |
| 105 avatar_ = gfx::ImageSkiaOperations::CreateResizedImage( | 117 avatar_ = gfx::ImageSkiaOperations::CreateResizedImage( |
| 106 avatar, skia::ImageOperations::RESIZE_BEST, | 118 avatar, skia::ImageOperations::RESIZE_BEST, |
| 107 gfx::Size(width_, height_)); | 119 gfx::Size(width_, height_)); |
| 108 } | 120 } |
| 109 | 121 |
| 122 AvatarImageSource::AvatarImageSource(gfx::ImageSkia avatar, |
| 123 const gfx::Size& canvas_size, |
| 124 int width, |
| 125 AvatarPosition position, |
| 126 AvatarBorder border) |
| 127 : AvatarImageSource(avatar, |
| 128 canvas_size, |
| 129 width, |
| 130 position, |
| 131 border, |
| 132 profiles::SHAPE_SQUARE) {} |
| 133 |
| 110 AvatarImageSource::~AvatarImageSource() { | 134 AvatarImageSource::~AvatarImageSource() { |
| 111 } | 135 } |
| 112 | 136 |
| 113 void AvatarImageSource::Draw(gfx::Canvas* canvas) { | 137 void AvatarImageSource::Draw(gfx::Canvas* canvas) { |
| 114 // Center the avatar horizontally. | 138 // Center the avatar horizontally. |
| 115 int x = (canvas_size_.width() - width_) / 2; | 139 int x = (canvas_size_.width() - width_) / 2; |
| 116 int y; | 140 int y; |
| 117 | 141 |
| 118 if (position_ == POSITION_CENTER) { | 142 if (position_ == POSITION_CENTER) { |
| 119 // Draw the avatar centered on the canvas. | 143 // Draw the avatar centered on the canvas. |
| 120 y = (canvas_size_.height() - height_) / 2; | 144 y = (canvas_size_.height() - height_) / 2; |
| 121 } else { | 145 } else { |
| 122 // Draw the avatar on the bottom center of the canvas, leaving 1px below. | 146 // Draw the avatar on the bottom center of the canvas, leaving 1px below. |
| 123 y = canvas_size_.height() - height_ - 1; | 147 y = canvas_size_.height() - height_ - 1; |
| 124 } | 148 } |
| 125 | 149 |
| 150 #if defined(OS_ANDROID) |
| 151 // Circular shape is only available on desktop platforms. |
| 152 DCHECK(shape_ != profiles::SHAPE_CIRCLE); |
| 153 #else |
| 154 if (shape_ == profiles::SHAPE_CIRCLE) { |
| 155 // Draw the avatar on the bottom center of the canvas; overrides the |
| 156 // previous position specification to avoid leaving visible gap below the |
| 157 // avatar. |
| 158 y = canvas_size_.height() - height_; |
| 159 |
| 160 // Calculate the circular mask that will be used to display the avatar |
| 161 // image. |
| 162 gfx::Path circular_mask; |
| 163 circular_mask.addCircle(SkIntToScalar(canvas_size_.width() / 2), |
| 164 SkIntToScalar(canvas_size_.height() / 2), |
| 165 SkIntToScalar(canvas_size_.width() / 2)); |
| 166 canvas->ClipPath(circular_mask, true); |
| 167 } |
| 168 #endif |
| 169 |
| 126 canvas->DrawImageInt(avatar_, x, y); | 170 canvas->DrawImageInt(avatar_, x, y); |
| 127 | 171 |
| 128 // The border should be square. | 172 // The border should be square. |
| 129 int border_size = std::max(width_, height_); | 173 int border_size = std::max(width_, height_); |
| 130 // Reset the x and y for the square border. | 174 // Reset the x and y for the square border. |
| 131 x = (canvas_size_.width() - border_size) / 2; | 175 x = (canvas_size_.width() - border_size) / 2; |
| 132 y = (canvas_size_.height() - border_size) / 2; | 176 y = (canvas_size_.height() - border_size) / 2; |
| 133 | 177 |
| 134 if (border_ == BORDER_NORMAL) { | 178 if (border_ == BORDER_NORMAL) { |
| 135 // Draw a gray border on the inside of the avatar. | 179 // Draw a gray border on the inside of the avatar. |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 221 const size_t kDefaultAvatarIconsCount = 27; | 265 const size_t kDefaultAvatarIconsCount = 27; |
| 222 | 266 |
| 223 // The first 8 icons are generic. | 267 // The first 8 icons are generic. |
| 224 const size_t kGenericAvatarIconsCount = 8; | 268 const size_t kGenericAvatarIconsCount = 8; |
| 225 | 269 |
| 226 // The avatar used as a placeholder (grey silhouette). | 270 // The avatar used as a placeholder (grey silhouette). |
| 227 const size_t kPlaceholderAvatarIndex = 26; | 271 const size_t kPlaceholderAvatarIndex = 26; |
| 228 | 272 |
| 229 gfx::Image GetSizedAvatarIcon(const gfx::Image& image, | 273 gfx::Image GetSizedAvatarIcon(const gfx::Image& image, |
| 230 bool is_rectangle, | 274 bool is_rectangle, |
| 231 int width, int height) { | 275 int width, |
| 276 int height, |
| 277 AvatarShape shape) { |
| 232 if (!is_rectangle && image.Height() <= height) | 278 if (!is_rectangle && image.Height() <= height) |
| 233 return image; | 279 return image; |
| 234 | 280 |
| 235 gfx::Size size(width, height); | 281 gfx::Size size(width, height); |
| 236 | 282 |
| 237 // Source for a centered, sized icon. GAIA images get a border. | 283 // Source for a centered, sized icon. GAIA images get a border. |
| 238 std::unique_ptr<gfx::ImageSkiaSource> source(new AvatarImageSource( | 284 std::unique_ptr<gfx::ImageSkiaSource> source( |
| 239 *image.ToImageSkia(), size, std::min(width, height), | 285 new AvatarImageSource(*image.ToImageSkia(), size, std::min(width, height), |
| 240 AvatarImageSource::POSITION_CENTER, AvatarImageSource::BORDER_NONE)); | 286 AvatarImageSource::POSITION_CENTER, |
| 287 AvatarImageSource::BORDER_NONE, shape)); |
| 241 | 288 |
| 242 return gfx::Image(gfx::ImageSkia(source.release(), size)); | 289 return gfx::Image(gfx::ImageSkia(source.release(), size)); |
| 243 } | 290 } |
| 244 | 291 |
| 292 gfx::Image GetSizedAvatarIcon(const gfx::Image& image, |
| 293 bool is_rectangle, |
| 294 int width, |
| 295 int height) { |
| 296 return GetSizedAvatarIcon(image, is_rectangle, width, height, |
| 297 profiles::SHAPE_SQUARE); |
| 298 } |
| 299 |
| 245 gfx::Image GetAvatarIconForMenu(const gfx::Image& image, | 300 gfx::Image GetAvatarIconForMenu(const gfx::Image& image, |
| 246 bool is_rectangle) { | 301 bool is_rectangle) { |
| 247 return GetSizedAvatarIcon( | 302 return GetSizedAvatarIcon( |
| 248 image, is_rectangle, kAvatarIconWidth, kAvatarIconHeight); | 303 image, is_rectangle, kAvatarIconWidth, kAvatarIconHeight); |
| 249 } | 304 } |
| 250 | 305 |
| 251 gfx::Image GetAvatarIconForWebUI(const gfx::Image& image, | 306 gfx::Image GetAvatarIconForWebUI(const gfx::Image& image, |
| 252 bool is_rectangle) { | 307 bool is_rectangle) { |
| 253 return GetSizedAvatarIcon(image, is_rectangle, | 308 return GetSizedAvatarIcon(image, is_rectangle, |
| 254 kAvatarIconWidth, kAvatarIconHeight); | 309 kAvatarIconWidth, kAvatarIconHeight); |
| (...skipping 253 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 508 avatar_info->SetString( | 563 avatar_info->SetString( |
| 509 "label", l10n_util::GetStringUTF16( | 564 "label", l10n_util::GetStringUTF16( |
| 510 profiles::GetDefaultAvatarLabelResourceIDAtIndex(i))); | 565 profiles::GetDefaultAvatarLabelResourceIDAtIndex(i))); |
| 511 | 566 |
| 512 avatars->Append(std::move(avatar_info)); | 567 avatars->Append(std::move(avatar_info)); |
| 513 } | 568 } |
| 514 return avatars; | 569 return avatars; |
| 515 } | 570 } |
| 516 | 571 |
| 517 } // namespace profiles | 572 } // namespace profiles |
| OLD | NEW |