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 bool is_circular = false); | |
Roger Tawa OOO till Jul 10th
2016/06/13 15:20:44
No spaces around = for default args.
Jane
2016/06/13 17:53:42
Done.
| |
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 bool is_circular_; | |
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 bool is_circular) | |
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 is_circular_(is_circular) { | |
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) { |
117 if (is_circular_) { | |
118 // Calculate the circular mask that will be used to display the avatar | |
119 // image. | |
120 gfx::Path circular_mask; | |
Jane
2016/06/10 17:28:55
Question: Why am I getting "error: undefined refer
Roger Tawa OOO till Jul 10th
2016/06/13 15:20:44
Sorry, not sure. Try asking Justin (junov@)
Jane
2016/06/14 03:02:48
Added #ifdef and the Android unit tests passed! PT
| |
121 circular_mask.addCircle(SkIntToScalar(canvas_size_.width() / 2), | |
122 SkIntToScalar(canvas_size_.height() / 2), | |
123 SkIntToScalar(canvas_size_.width() / 2)); | |
124 canvas->ClipPath(circular_mask, true); | |
125 } | |
126 | |
112 // Center the avatar horizontally. | 127 // Center the avatar horizontally. |
113 int x = (canvas_size_.width() - width_) / 2; | 128 int x = (canvas_size_.width() - width_) / 2; |
114 int y; | 129 int y; |
115 | 130 |
116 if (position_ == POSITION_CENTER) { | 131 if (is_circular_) { |
132 // Draw the avatar on the bottom center of the canvas; overrides the | |
133 // position specification. | |
134 y = canvas_size_.height() - height_; | |
135 } else if (position_ == POSITION_CENTER) { | |
117 // Draw the avatar centered on the canvas. | 136 // Draw the avatar centered on the canvas. |
118 y = (canvas_size_.height() - height_) / 2; | 137 y = (canvas_size_.height() - height_) / 2; |
119 } else { | 138 } else { |
120 // Draw the avatar on the bottom center of the canvas, leaving 1px below. | 139 // Draw the avatar on the bottom center of the canvas, leaving 1px below. |
121 y = canvas_size_.height() - height_ - 1; | 140 y = canvas_size_.height() - height_ - 1; |
122 } | 141 } |
123 | 142 |
124 canvas->DrawImageInt(avatar_, x, y); | 143 canvas->DrawImageInt(avatar_, x, y); |
125 | 144 |
126 // The border should be square. | 145 // The border should be square. |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
219 const size_t kDefaultAvatarIconsCount = 27; | 238 const size_t kDefaultAvatarIconsCount = 27; |
220 | 239 |
221 // The first 8 icons are generic. | 240 // The first 8 icons are generic. |
222 const size_t kGenericAvatarIconsCount = 8; | 241 const size_t kGenericAvatarIconsCount = 8; |
223 | 242 |
224 // The avatar used as a placeholder (grey silhouette). | 243 // The avatar used as a placeholder (grey silhouette). |
225 const size_t kPlaceholderAvatarIndex = 26; | 244 const size_t kPlaceholderAvatarIndex = 26; |
226 | 245 |
227 gfx::Image GetSizedAvatarIcon(const gfx::Image& image, | 246 gfx::Image GetSizedAvatarIcon(const gfx::Image& image, |
228 bool is_rectangle, | 247 bool is_rectangle, |
229 int width, int height) { | 248 int width, int height, |
249 bool is_circular) { | |
230 if (!is_rectangle && image.Height() <= height) | 250 if (!is_rectangle && image.Height() <= height) |
231 return image; | 251 return image; |
232 | 252 |
233 gfx::Size size(width, height); | 253 gfx::Size size(width, height); |
234 | 254 |
235 // Source for a centered, sized icon. GAIA images get a border. | 255 // Source for a centered, sized icon. GAIA images get a border. |
236 std::unique_ptr<gfx::ImageSkiaSource> source(new AvatarImageSource( | 256 std::unique_ptr<gfx::ImageSkiaSource> source(new AvatarImageSource( |
237 *image.ToImageSkia(), size, std::min(width, height), | 257 *image.ToImageSkia(), size, std::min(width, height), |
238 AvatarImageSource::POSITION_CENTER, AvatarImageSource::BORDER_NONE)); | 258 AvatarImageSource::POSITION_CENTER, AvatarImageSource::BORDER_NONE, |
259 is_circular)); | |
239 | 260 |
240 return gfx::Image(gfx::ImageSkia(source.release(), size)); | 261 return gfx::Image(gfx::ImageSkia(source.release(), size)); |
241 } | 262 } |
242 | 263 |
243 gfx::Image GetAvatarIconForMenu(const gfx::Image& image, | 264 gfx::Image GetAvatarIconForMenu(const gfx::Image& image, |
244 bool is_rectangle) { | 265 bool is_rectangle) { |
245 return GetSizedAvatarIcon( | 266 return GetSizedAvatarIcon( |
246 image, is_rectangle, kAvatarIconWidth, kAvatarIconHeight); | 267 image, is_rectangle, kAvatarIconWidth, kAvatarIconHeight); |
247 } | 268 } |
248 | 269 |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
487 *new_url = old_url.ReplaceComponents(replacement); | 508 *new_url = old_url.ReplaceComponents(replacement); |
488 return new_url->is_valid(); | 509 return new_url->is_valid(); |
489 } | 510 } |
490 | 511 |
491 // We can't set the image size, just use the default size. | 512 // We can't set the image size, just use the default size. |
492 *new_url = old_url; | 513 *new_url = old_url; |
493 return true; | 514 return true; |
494 } | 515 } |
495 | 516 |
496 } // namespace profiles | 517 } // namespace profiles |
OLD | NEW |