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); | |
78 | |
76 ~AvatarImageSource() override; | 79 ~AvatarImageSource() override; |
77 | 80 |
78 // CanvasImageSource override: | 81 // CanvasImageSource override: |
79 void Draw(gfx::Canvas* canvas) override; | 82 void Draw(gfx::Canvas* canvas) override; |
80 | 83 |
81 private: | 84 private: |
82 gfx::ImageSkia avatar_; | 85 gfx::ImageSkia avatar_; |
83 const gfx::Size canvas_size_; | 86 const gfx::Size canvas_size_; |
84 const int width_; | 87 const int width_; |
85 const int height_; | 88 const int height_; |
86 const AvatarPosition position_; | 89 const AvatarPosition position_; |
87 const AvatarBorder border_; | 90 const AvatarBorder border_; |
91 const profiles::AvatarShape shape_; | |
88 | 92 |
89 DISALLOW_COPY_AND_ASSIGN(AvatarImageSource); | 93 DISALLOW_COPY_AND_ASSIGN(AvatarImageSource); |
90 }; | 94 }; |
91 | 95 |
92 AvatarImageSource::AvatarImageSource(gfx::ImageSkia avatar, | 96 AvatarImageSource::AvatarImageSource(gfx::ImageSkia avatar, |
93 const gfx::Size& canvas_size, | 97 const gfx::Size& canvas_size, |
94 int width, | 98 int width, |
95 AvatarPosition position, | 99 AvatarPosition position, |
96 AvatarBorder border) | 100 AvatarBorder border, |
101 profiles::AvatarShape shape | |
102 ) | |
97 : gfx::CanvasImageSource(canvas_size, false), | 103 : gfx::CanvasImageSource(canvas_size, false), |
98 canvas_size_(canvas_size), | 104 canvas_size_(canvas_size), |
99 width_(width), | 105 width_(width), |
100 height_(GetScaledAvatarHeightForWidth(width, avatar)), | 106 height_(GetScaledAvatarHeightForWidth(width, avatar)), |
101 position_(position), | 107 position_(position), |
102 border_(border) { | 108 border_(border), |
109 shape_(shape) | |
110 { | |
103 avatar_ = gfx::ImageSkiaOperations::CreateResizedImage( | 111 avatar_ = gfx::ImageSkiaOperations::CreateResizedImage( |
104 avatar, skia::ImageOperations::RESIZE_BEST, | 112 avatar, skia::ImageOperations::RESIZE_BEST, |
105 gfx::Size(width_, height_)); | 113 gfx::Size(width_, height_)); |
106 } | 114 } |
107 | 115 |
108 AvatarImageSource::~AvatarImageSource() { | 116 AvatarImageSource::~AvatarImageSource() { |
109 } | 117 } |
110 | 118 |
111 void AvatarImageSource::Draw(gfx::Canvas* canvas) { | 119 void AvatarImageSource::Draw(gfx::Canvas* canvas) { |
112 // Center the avatar horizontally. | 120 // Center the avatar horizontally. |
113 int x = (canvas_size_.width() - width_) / 2; | 121 int x = (canvas_size_.width() - width_) / 2; |
114 int y; | 122 int y; |
115 | 123 |
116 if (position_ == POSITION_CENTER) { | 124 if (position_ == POSITION_CENTER) { |
117 // Draw the avatar centered on the canvas. | 125 // Draw the avatar centered on the canvas. |
118 y = (canvas_size_.height() - height_) / 2; | 126 y = (canvas_size_.height() - height_) / 2; |
119 } else { | 127 } else { |
120 // Draw the avatar on the bottom center of the canvas, leaving 1px below. | 128 // Draw the avatar on the bottom center of the canvas, leaving 1px below. |
121 y = canvas_size_.height() - height_ - 1; | 129 y = canvas_size_.height() - height_ - 1; |
122 } | 130 } |
123 | 131 |
132 #if defined(OS_ANDROID) | |
133 // Circular shape is only available on desktop platforms | |
Roger Tawa OOO till Jul 10th
2016/06/20 14:04:44
Nit: end comment with period.
Jane
2016/06/27 21:52:16
Done.
| |
134 DCHECK(shape_ != profiles::SHAPE_CIRCLE); | |
135 #else | |
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) | |
258 { | |
Roger Tawa OOO till Jul 10th
2016/06/20 14:04:44
{ should stay on previous line.
Jane
2016/06/27 21:52:16
Done.
| |
230 if (!is_rectangle && image.Height() <= height) | 259 if (!is_rectangle && image.Height() <= height) |
231 return image; | 260 return image; |
232 | 261 |
233 gfx::Size size(width, height); | 262 gfx::Size size(width, height); |
234 | 263 |
235 // Source for a centered, sized icon. GAIA images get a border. | 264 // Source for a centered, sized icon. GAIA images get a border. |
236 std::unique_ptr<gfx::ImageSkiaSource> source(new AvatarImageSource( | 265 std::unique_ptr<gfx::ImageSkiaSource> source(new AvatarImageSource( |
237 *image.ToImageSkia(), size, std::min(width, height), | 266 *image.ToImageSkia(), size, std::min(width, height), |
238 AvatarImageSource::POSITION_CENTER, AvatarImageSource::BORDER_NONE)); | 267 AvatarImageSource::POSITION_CENTER, AvatarImageSource::BORDER_NONE, |
268 shape)); | |
239 | 269 |
240 return gfx::Image(gfx::ImageSkia(source.release(), size)); | 270 return gfx::Image(gfx::ImageSkia(source.release(), size)); |
241 } | 271 } |
242 | 272 |
243 gfx::Image GetAvatarIconForMenu(const gfx::Image& image, | 273 gfx::Image GetAvatarIconForMenu(const gfx::Image& image, |
244 bool is_rectangle) { | 274 bool is_rectangle) { |
245 return GetSizedAvatarIcon( | 275 return GetSizedAvatarIcon( |
246 image, is_rectangle, kAvatarIconWidth, kAvatarIconHeight); | 276 image, is_rectangle, kAvatarIconWidth, kAvatarIconHeight); |
247 } | 277 } |
248 | 278 |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
487 *new_url = old_url.ReplaceComponents(replacement); | 517 *new_url = old_url.ReplaceComponents(replacement); |
488 return new_url->is_valid(); | 518 return new_url->is_valid(); |
489 } | 519 } |
490 | 520 |
491 // We can't set the image size, just use the default size. | 521 // We can't set the image size, just use the default size. |
492 *new_url = old_url; | 522 *new_url = old_url; |
493 return true; | 523 return true; |
494 } | 524 } |
495 | 525 |
496 } // namespace profiles | 526 } // namespace profiles |
OLD | NEW |