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 11 matching lines...) Expand all Loading... |
22 #include "third_party/skia/include/core/SkPaint.h" | 22 #include "third_party/skia/include/core/SkPaint.h" |
23 #include "third_party/skia/include/core/SkPath.h" | 23 #include "third_party/skia/include/core/SkPath.h" |
24 #include "third_party/skia/include/core/SkScalar.h" | 24 #include "third_party/skia/include/core/SkScalar.h" |
25 #include "third_party/skia/include/core/SkXfermode.h" | 25 #include "third_party/skia/include/core/SkXfermode.h" |
26 #include "ui/base/resource/resource_bundle.h" | 26 #include "ui/base/resource/resource_bundle.h" |
27 #include "ui/gfx/canvas.h" | 27 #include "ui/gfx/canvas.h" |
28 #include "ui/gfx/geometry/rect.h" | 28 #include "ui/gfx/geometry/rect.h" |
29 #include "ui/gfx/image/canvas_image_source.h" | 29 #include "ui/gfx/image/canvas_image_source.h" |
30 #include "ui/gfx/image/image.h" | 30 #include "ui/gfx/image/image.h" |
31 #include "ui/gfx/image/image_skia_operations.h" | 31 #include "ui/gfx/image/image_skia_operations.h" |
| 32 #include "ui/gfx/path.h" |
32 #include "ui/gfx/skia_util.h" | 33 #include "ui/gfx/skia_util.h" |
33 #include "url/gurl.h" | 34 #include "url/gurl.h" |
34 #include "url/url_canon.h" | 35 #include "url/url_canon.h" |
35 | 36 |
36 // Helper methods for transforming and drawing avatar icons. | 37 // Helper methods for transforming and drawing avatar icons. |
37 namespace { | 38 namespace { |
38 | 39 |
39 // Path format for specifying thumbnail's size. | 40 // Path format for specifying thumbnail's size. |
40 const char kThumbnailSizeFormat[] = "s%d-c"; | 41 const char kThumbnailSizeFormat[] = "s%d-c"; |
41 // Default thumbnail size. | 42 // Default thumbnail size. |
(...skipping 23 matching lines...) Expand all Loading... |
65 enum AvatarBorder { | 66 enum AvatarBorder { |
66 BORDER_NONE, | 67 BORDER_NONE, |
67 BORDER_NORMAL, | 68 BORDER_NORMAL, |
68 BORDER_ETCHED, | 69 BORDER_ETCHED, |
69 }; | 70 }; |
70 | 71 |
71 AvatarImageSource(gfx::ImageSkia avatar, | 72 AvatarImageSource(gfx::ImageSkia avatar, |
72 const gfx::Size& canvas_size, | 73 const gfx::Size& canvas_size, |
73 int width, | 74 int width, |
74 AvatarPosition position, | 75 AvatarPosition position, |
75 AvatarBorder border); | 76 AvatarBorder border, |
| 77 profiles::AvatarShape shape=profiles::SHAPE_SQUARE); |
76 ~AvatarImageSource() override; | 78 ~AvatarImageSource() override; |
77 | 79 |
78 // CanvasImageSource override: | 80 // CanvasImageSource override: |
79 void Draw(gfx::Canvas* canvas) override; | 81 void Draw(gfx::Canvas* canvas) override; |
80 | 82 |
81 private: | 83 private: |
82 gfx::ImageSkia avatar_; | 84 gfx::ImageSkia avatar_; |
83 const gfx::Size canvas_size_; | 85 const gfx::Size canvas_size_; |
84 const int width_; | 86 const int width_; |
85 const int height_; | 87 const int height_; |
86 const AvatarPosition position_; | 88 const AvatarPosition position_; |
| 89 #if !defined(OS_ANDROID) |
| 90 const profiles::AvatarShape shape_; |
| 91 #endif |
87 const AvatarBorder border_; | 92 const AvatarBorder border_; |
88 | 93 |
| 94 |
89 DISALLOW_COPY_AND_ASSIGN(AvatarImageSource); | 95 DISALLOW_COPY_AND_ASSIGN(AvatarImageSource); |
90 }; | 96 }; |
91 | 97 |
92 AvatarImageSource::AvatarImageSource(gfx::ImageSkia avatar, | 98 AvatarImageSource::AvatarImageSource(gfx::ImageSkia avatar, |
93 const gfx::Size& canvas_size, | 99 const gfx::Size& canvas_size, |
94 int width, | 100 int width, |
95 AvatarPosition position, | 101 AvatarPosition position, |
96 AvatarBorder border) | 102 AvatarBorder border, |
| 103 profiles::AvatarShape shape) |
97 : gfx::CanvasImageSource(canvas_size, false), | 104 : gfx::CanvasImageSource(canvas_size, false), |
98 canvas_size_(canvas_size), | 105 canvas_size_(canvas_size), |
99 width_(width), | 106 width_(width), |
100 height_(GetScaledAvatarHeightForWidth(width, avatar)), | 107 height_(GetScaledAvatarHeightForWidth(width, avatar)), |
101 position_(position), | 108 position_(position), |
102 border_(border) { | 109 #if !defined(OS_ANDROID) |
| 110 shape_(shape), |
| 111 #endif |
| 112 border_(border) |
| 113 { |
103 avatar_ = gfx::ImageSkiaOperations::CreateResizedImage( | 114 avatar_ = gfx::ImageSkiaOperations::CreateResizedImage( |
104 avatar, skia::ImageOperations::RESIZE_BEST, | 115 avatar, skia::ImageOperations::RESIZE_BEST, |
105 gfx::Size(width_, height_)); | 116 gfx::Size(width_, height_)); |
106 } | 117 } |
107 | 118 |
108 AvatarImageSource::~AvatarImageSource() { | 119 AvatarImageSource::~AvatarImageSource() { |
109 } | 120 } |
110 | 121 |
111 void AvatarImageSource::Draw(gfx::Canvas* canvas) { | 122 void AvatarImageSource::Draw(gfx::Canvas* canvas) { |
112 // Center the avatar horizontally. | 123 // Center the avatar horizontally. |
113 int x = (canvas_size_.width() - width_) / 2; | 124 int x = (canvas_size_.width() - width_) / 2; |
114 int y; | 125 int y; |
115 | 126 |
116 if (position_ == POSITION_CENTER) { | 127 if (position_ == POSITION_CENTER) { |
117 // Draw the avatar centered on the canvas. | 128 // Draw the avatar centered on the canvas. |
118 y = (canvas_size_.height() - height_) / 2; | 129 y = (canvas_size_.height() - height_) / 2; |
119 } else { | 130 } else { |
120 // Draw the avatar on the bottom center of the canvas, leaving 1px below. | 131 // Draw the avatar on the bottom center of the canvas, leaving 1px below. |
121 y = canvas_size_.height() - height_ - 1; | 132 y = canvas_size_.height() - height_ - 1; |
122 } | 133 } |
123 | 134 |
| 135 #if !defined(OS_ANDROID) |
| 136 if (shape_ == profiles::SHAPE_CIRCLE) { |
| 137 // Draw the avatar on the bottom center of the canvas; overrides the |
| 138 // previous position specification. |
| 139 y = canvas_size_.height() - height_; |
| 140 |
| 141 // Calculate the circular mask that will be used to display the avatar |
| 142 // image. |
| 143 gfx::Path circular_mask; |
| 144 circular_mask.addCircle(SkIntToScalar(canvas_size_.width() / 2), |
| 145 SkIntToScalar(canvas_size_.height() / 2), |
| 146 SkIntToScalar(canvas_size_.width() / 2)); |
| 147 canvas->ClipPath(circular_mask, true); |
| 148 } |
| 149 #endif |
| 150 |
124 canvas->DrawImageInt(avatar_, x, y); | 151 canvas->DrawImageInt(avatar_, x, y); |
125 | 152 |
126 // The border should be square. | 153 // The border should be square. |
127 int border_size = std::max(width_, height_); | 154 int border_size = std::max(width_, height_); |
128 // Reset the x and y for the square border. | 155 // Reset the x and y for the square border. |
129 x = (canvas_size_.width() - border_size) / 2; | 156 x = (canvas_size_.width() - border_size) / 2; |
130 y = (canvas_size_.height() - border_size) / 2; | 157 y = (canvas_size_.height() - border_size) / 2; |
131 | 158 |
132 if (border_ == BORDER_NORMAL) { | 159 if (border_ == BORDER_NORMAL) { |
133 // Draw a gray border on the inside of the avatar. | 160 // Draw a gray border on the inside of the avatar. |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 const size_t kDefaultAvatarIconsCount = 27; | 246 const size_t kDefaultAvatarIconsCount = 27; |
220 | 247 |
221 // The first 8 icons are generic. | 248 // The first 8 icons are generic. |
222 const size_t kGenericAvatarIconsCount = 8; | 249 const size_t kGenericAvatarIconsCount = 8; |
223 | 250 |
224 // The avatar used as a placeholder (grey silhouette). | 251 // The avatar used as a placeholder (grey silhouette). |
225 const size_t kPlaceholderAvatarIndex = 26; | 252 const size_t kPlaceholderAvatarIndex = 26; |
226 | 253 |
227 gfx::Image GetSizedAvatarIcon(const gfx::Image& image, | 254 gfx::Image GetSizedAvatarIcon(const gfx::Image& image, |
228 bool is_rectangle, | 255 bool is_rectangle, |
229 int width, int height) { | 256 int width, int height, |
| 257 AvatarShape shape) { |
230 if (!is_rectangle && image.Height() <= height) | 258 if (!is_rectangle && image.Height() <= height) |
231 return image; | 259 return image; |
232 | 260 |
233 gfx::Size size(width, height); | 261 gfx::Size size(width, height); |
234 | 262 |
235 // Source for a centered, sized icon. GAIA images get a border. | 263 // Source for a centered, sized icon. GAIA images get a border. |
236 std::unique_ptr<gfx::ImageSkiaSource> source(new AvatarImageSource( | 264 std::unique_ptr<gfx::ImageSkiaSource> source(new AvatarImageSource( |
237 *image.ToImageSkia(), size, std::min(width, height), | 265 *image.ToImageSkia(), size, std::min(width, height), |
238 AvatarImageSource::POSITION_CENTER, AvatarImageSource::BORDER_NONE)); | 266 AvatarImageSource::POSITION_CENTER, AvatarImageSource::BORDER_NONE, |
| 267 shape)); |
239 | 268 |
240 return gfx::Image(gfx::ImageSkia(source.release(), size)); | 269 return gfx::Image(gfx::ImageSkia(source.release(), size)); |
241 } | 270 } |
242 | 271 |
243 gfx::Image GetAvatarIconForMenu(const gfx::Image& image, | 272 gfx::Image GetAvatarIconForMenu(const gfx::Image& image, |
244 bool is_rectangle) { | 273 bool is_rectangle) { |
245 return GetSizedAvatarIcon( | 274 return GetSizedAvatarIcon( |
246 image, is_rectangle, kAvatarIconWidth, kAvatarIconHeight); | 275 image, is_rectangle, kAvatarIconWidth, kAvatarIconHeight); |
247 } | 276 } |
248 | 277 |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
487 *new_url = old_url.ReplaceComponents(replacement); | 516 *new_url = old_url.ReplaceComponents(replacement); |
488 return new_url->is_valid(); | 517 return new_url->is_valid(); |
489 } | 518 } |
490 | 519 |
491 // We can't set the image size, just use the default size. | 520 // We can't set the image size, just use the default size. |
492 *new_url = old_url; | 521 *new_url = old_url; |
493 return true; | 522 return true; |
494 } | 523 } |
495 | 524 |
496 } // namespace profiles | 525 } // namespace profiles |
OLD | NEW |