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_; |
87 const AvatarBorder border_; | 89 const AvatarBorder border_; |
90 const profiles::AvatarShape shape_; | |
88 | 91 |
89 DISALLOW_COPY_AND_ASSIGN(AvatarImageSource); | 92 DISALLOW_COPY_AND_ASSIGN(AvatarImageSource); |
90 }; | 93 }; |
91 | 94 |
92 AvatarImageSource::AvatarImageSource(gfx::ImageSkia avatar, | 95 AvatarImageSource::AvatarImageSource(gfx::ImageSkia avatar, |
93 const gfx::Size& canvas_size, | 96 const gfx::Size& canvas_size, |
94 int width, | 97 int width, |
95 AvatarPosition position, | 98 AvatarPosition position, |
96 AvatarBorder border) | 99 AvatarBorder border, |
100 profiles::AvatarShape shape) | |
97 : gfx::CanvasImageSource(canvas_size, false), | 101 : gfx::CanvasImageSource(canvas_size, false), |
98 canvas_size_(canvas_size), | 102 canvas_size_(canvas_size), |
99 width_(width), | 103 width_(width), |
100 height_(GetScaledAvatarHeightForWidth(width, avatar)), | 104 height_(GetScaledAvatarHeightForWidth(width, avatar)), |
101 position_(position), | 105 position_(position), |
102 border_(border) { | 106 border_(border), |
107 shape_(shape) { | |
103 avatar_ = gfx::ImageSkiaOperations::CreateResizedImage( | 108 avatar_ = gfx::ImageSkiaOperations::CreateResizedImage( |
104 avatar, skia::ImageOperations::RESIZE_BEST, | 109 avatar, skia::ImageOperations::RESIZE_BEST, |
105 gfx::Size(width_, height_)); | 110 gfx::Size(width_, height_)); |
106 } | 111 } |
107 | 112 |
108 AvatarImageSource::~AvatarImageSource() { | 113 AvatarImageSource::~AvatarImageSource() { |
109 } | 114 } |
110 | 115 |
111 void AvatarImageSource::Draw(gfx::Canvas* canvas) { | 116 void AvatarImageSource::Draw(gfx::Canvas* canvas) { |
112 // Center the avatar horizontally. | 117 // Center the avatar horizontally. |
113 int x = (canvas_size_.width() - width_) / 2; | 118 int x = (canvas_size_.width() - width_) / 2; |
114 int y; | 119 int y; |
115 | 120 |
116 if (position_ == POSITION_CENTER) { | 121 if (shape_ == profiles::SHAPE_CIRCLE) { |
122 // Draw the avatar on the bottom center of the canvas; overrides the | |
123 // position specification. | |
124 y = canvas_size_.height() - height_; | |
125 #if !defined(OS_ANDROID) | |
126 // Calculate the circular mask that will be used to display the avatar | |
127 // image. | |
128 gfx::Path circular_mask; | |
129 circular_mask.addCircle(SkIntToScalar(canvas_size_.width() / 2), | |
130 SkIntToScalar(canvas_size_.height() / 2), | |
131 SkIntToScalar(canvas_size_.width() / 2)); | |
132 canvas->ClipPath(circular_mask, true); | |
133 #endif | |
134 } else if (position_ == POSITION_CENTER) { | |
Roger Tawa OOO till Jul 10th
2016/06/14 15:25:13
If someone happens to use SHAPE_CIRCLE in clank, t
Jane
2016/06/15 14:21:13
Done. I assume you meant putting the if condition
Roger Tawa OOO till Jul 10th
2016/06/17 14:35:56
As is, the CIRCLE enum is still defined on android
| |
117 // Draw the avatar centered on the canvas. | 135 // Draw the avatar centered on the canvas. |
118 y = (canvas_size_.height() - height_) / 2; | 136 y = (canvas_size_.height() - height_) / 2; |
119 } else { | 137 } else { |
120 // Draw the avatar on the bottom center of the canvas, leaving 1px below. | 138 // Draw the avatar on the bottom center of the canvas, leaving 1px below. |
121 y = canvas_size_.height() - height_ - 1; | 139 y = canvas_size_.height() - height_ - 1; |
122 } | 140 } |
123 | 141 |
124 canvas->DrawImageInt(avatar_, x, y); | 142 canvas->DrawImageInt(avatar_, x, y); |
125 | 143 |
126 // The border should be square. | 144 // The border should be square. |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 const size_t kDefaultAvatarIconsCount = 27; | 237 const size_t kDefaultAvatarIconsCount = 27; |
220 | 238 |
221 // The first 8 icons are generic. | 239 // The first 8 icons are generic. |
222 const size_t kGenericAvatarIconsCount = 8; | 240 const size_t kGenericAvatarIconsCount = 8; |
223 | 241 |
224 // The avatar used as a placeholder (grey silhouette). | 242 // The avatar used as a placeholder (grey silhouette). |
225 const size_t kPlaceholderAvatarIndex = 26; | 243 const size_t kPlaceholderAvatarIndex = 26; |
226 | 244 |
227 gfx::Image GetSizedAvatarIcon(const gfx::Image& image, | 245 gfx::Image GetSizedAvatarIcon(const gfx::Image& image, |
228 bool is_rectangle, | 246 bool is_rectangle, |
229 int width, int height) { | 247 int width, int height, |
248 AvatarShape shape) { | |
230 if (!is_rectangle && image.Height() <= height) | 249 if (!is_rectangle && image.Height() <= height) |
231 return image; | 250 return image; |
232 | 251 |
233 gfx::Size size(width, height); | 252 gfx::Size size(width, height); |
234 | 253 |
235 // Source for a centered, sized icon. GAIA images get a border. | 254 // Source for a centered, sized icon. GAIA images get a border. |
236 std::unique_ptr<gfx::ImageSkiaSource> source(new AvatarImageSource( | 255 std::unique_ptr<gfx::ImageSkiaSource> source(new AvatarImageSource( |
237 *image.ToImageSkia(), size, std::min(width, height), | 256 *image.ToImageSkia(), size, std::min(width, height), |
238 AvatarImageSource::POSITION_CENTER, AvatarImageSource::BORDER_NONE)); | 257 AvatarImageSource::POSITION_CENTER, AvatarImageSource::BORDER_NONE, |
258 shape)); | |
239 | 259 |
240 return gfx::Image(gfx::ImageSkia(source.release(), size)); | 260 return gfx::Image(gfx::ImageSkia(source.release(), size)); |
241 } | 261 } |
242 | 262 |
243 gfx::Image GetAvatarIconForMenu(const gfx::Image& image, | 263 gfx::Image GetAvatarIconForMenu(const gfx::Image& image, |
244 bool is_rectangle) { | 264 bool is_rectangle) { |
245 return GetSizedAvatarIcon( | 265 return GetSizedAvatarIcon( |
246 image, is_rectangle, kAvatarIconWidth, kAvatarIconHeight); | 266 image, is_rectangle, kAvatarIconWidth, kAvatarIconHeight); |
247 } | 267 } |
248 | 268 |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
487 *new_url = old_url.ReplaceComponents(replacement); | 507 *new_url = old_url.ReplaceComponents(replacement); |
488 return new_url->is_valid(); | 508 return new_url->is_valid(); |
489 } | 509 } |
490 | 510 |
491 // We can't set the image size, just use the default size. | 511 // We can't set the image size, just use the default size. |
492 *new_url = old_url; | 512 *new_url = old_url; |
493 return true; | 513 return true; |
494 } | 514 } |
495 | 515 |
496 } // namespace profiles | 516 } // namespace profiles |
OLD | NEW |