Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / profiles / profile_avatar_icon_util.cc
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
6
7 #include "base/files/file_util.h"
8 #include "base/format_macros.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/path_service.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/stringprintf.h"
13 #include "chrome/common/chrome_paths.h"
14 #include "grit/theme_resources.h"
15 #include "skia/ext/image_operations.h"
16 #include "third_party/skia/include/core/SkPaint.h"
17 #include "third_party/skia/include/core/SkPath.h"
18 #include "third_party/skia/include/core/SkScalar.h"
19 #include "third_party/skia/include/core/SkXfermode.h"
20 #include "ui/gfx/canvas.h"
21 #include "ui/gfx/image/canvas_image_source.h"
22 #include "ui/gfx/image/image.h"
23 #include "ui/gfx/image/image_skia_operations.h"
24 #include "ui/gfx/rect.h"
25 #include "ui/gfx/skia_util.h"
26
27 // Helper methods for transforming and drawing avatar icons.
28 namespace {
29
30 // Determine what the scaled height of the avatar icon should be for a
31 // specified width, to preserve the aspect ratio.
32 int GetScaledAvatarHeightForWidth(int width, const gfx::ImageSkia& avatar) {
33   // Multiply the width by the inverted aspect ratio (height over
34   // width), and then add 0.5 to ensure the int truncation rounds nicely.
35   int scaled_height = width *
36       ((float) avatar.height() / (float) avatar.width()) + 0.5f;
37   return scaled_height;
38 }
39
40 // A CanvasImageSource that draws a sized and positioned avatar with an
41 // optional border independently of the scale factor.
42 class AvatarImageSource : public gfx::CanvasImageSource {
43  public:
44   enum AvatarPosition {
45     POSITION_CENTER,
46     POSITION_BOTTOM_CENTER,
47   };
48
49   enum AvatarBorder {
50     BORDER_NONE,
51     BORDER_NORMAL,
52     BORDER_ETCHED,
53   };
54
55   AvatarImageSource(gfx::ImageSkia avatar,
56                     const gfx::Size& canvas_size,
57                     int width,
58                     AvatarPosition position,
59                     AvatarBorder border);
60   ~AvatarImageSource() override;
61
62   // CanvasImageSource override:
63   void Draw(gfx::Canvas* canvas) override;
64
65  private:
66   gfx::ImageSkia avatar_;
67   const gfx::Size canvas_size_;
68   const int width_;
69   const int height_;
70   const AvatarPosition position_;
71   const AvatarBorder border_;
72
73   DISALLOW_COPY_AND_ASSIGN(AvatarImageSource);
74 };
75
76 AvatarImageSource::AvatarImageSource(gfx::ImageSkia avatar,
77                                      const gfx::Size& canvas_size,
78                                      int width,
79                                      AvatarPosition position,
80                                      AvatarBorder border)
81     : gfx::CanvasImageSource(canvas_size, false),
82       canvas_size_(canvas_size),
83       width_(width),
84       height_(GetScaledAvatarHeightForWidth(width, avatar)),
85       position_(position),
86       border_(border) {
87   avatar_ = gfx::ImageSkiaOperations::CreateResizedImage(
88       avatar, skia::ImageOperations::RESIZE_BEST,
89       gfx::Size(width_, height_));
90 }
91
92 AvatarImageSource::~AvatarImageSource() {
93 }
94
95 void AvatarImageSource::Draw(gfx::Canvas* canvas) {
96   // Center the avatar horizontally.
97   int x = (canvas_size_.width() - width_) / 2;
98   int y;
99
100   if (position_ == POSITION_CENTER) {
101     // Draw the avatar centered on the canvas.
102     y = (canvas_size_.height() - height_) / 2;
103   } else {
104     // Draw the avatar on the bottom center of the canvas, leaving 1px below.
105     y = canvas_size_.height() - height_ - 1;
106   }
107
108   canvas->DrawImageInt(avatar_, x, y);
109
110   // The border should be square.
111   int border_size = std::max(width_, height_);
112   // Reset the x and y for the square border.
113   x = (canvas_size_.width() - border_size) / 2;
114   y = (canvas_size_.height() - border_size) / 2;
115
116   if (border_ == BORDER_NORMAL) {
117     // Draw a gray border on the inside of the avatar.
118     SkColor border_color = SkColorSetARGB(83, 0, 0, 0);
119
120     // Offset the rectangle by a half pixel so the border is drawn within the
121     // appropriate pixels no matter the scale factor. Subtract 1 from the right
122     // and bottom sizes to specify the endpoints, yielding -0.5.
123     SkPath path;
124     path.addRect(SkFloatToScalar(x + 0.5f),  // left
125                  SkFloatToScalar(y + 0.5f),  // top
126                  SkFloatToScalar(x + border_size - 0.5f),   // right
127                  SkFloatToScalar(y + border_size - 0.5f));  // bottom
128
129     SkPaint paint;
130     paint.setColor(border_color);
131     paint.setStyle(SkPaint::kStroke_Style);
132     paint.setStrokeWidth(SkIntToScalar(1));
133
134     canvas->DrawPath(path, paint);
135   } else if (border_ == BORDER_ETCHED) {
136     // Give the avatar an etched look by drawing a highlight on the bottom and
137     // right edges.
138     SkColor shadow_color = SkColorSetARGB(83, 0, 0, 0);
139     SkColor highlight_color = SkColorSetARGB(96, 255, 255, 255);
140
141     SkPaint paint;
142     paint.setStyle(SkPaint::kStroke_Style);
143     paint.setStrokeWidth(SkIntToScalar(1));
144
145     SkPath path;
146
147     // Left and top shadows. To support higher scale factors than 1, position
148     // the orthogonal dimension of each line on the half-pixel to separate the
149     // pixel. For a vertical line, this means adding 0.5 to the x-value.
150     path.moveTo(SkFloatToScalar(x + 0.5f), SkIntToScalar(y + height_));
151
152     // Draw up to the top-left. Stop with the y-value at a half-pixel.
153     path.rLineTo(SkIntToScalar(0), SkFloatToScalar(-height_ + 0.5f));
154
155     // Draw right to the top-right, stopping within the last pixel.
156     path.rLineTo(SkFloatToScalar(width_ - 0.5f), SkIntToScalar(0));
157
158     paint.setColor(shadow_color);
159     canvas->DrawPath(path, paint);
160
161     path.reset();
162
163     // Bottom and right highlights. Note that the shadows own the shared corner
164     // pixels, so reduce the sizes accordingly.
165     path.moveTo(SkIntToScalar(x + 1), SkFloatToScalar(y + height_ - 0.5f));
166
167     // Draw right to the bottom-right.
168     path.rLineTo(SkFloatToScalar(width_ - 1.5f), SkIntToScalar(0));
169
170     // Draw up to the top-right.
171     path.rLineTo(SkIntToScalar(0), SkFloatToScalar(-height_ + 1.5f));
172
173     paint.setColor(highlight_color);
174     canvas->DrawPath(path, paint);
175   }
176 }
177
178 }  // namespace
179
180 namespace profiles {
181
182 struct IconResourceInfo {
183   int resource_id;
184   const char* filename;
185 };
186
187 const int kAvatarIconWidth = 38;
188 const int kAvatarIconHeight = 31;
189 const SkColor kAvatarTutorialBackgroundColor = SkColorSetRGB(0x42, 0x85, 0xf4);
190 const SkColor kAvatarTutorialContentTextColor = SkColorSetRGB(0xc6, 0xda, 0xfc);
191 const SkColor kAvatarBubbleAccountsBackgroundColor =
192     SkColorSetRGB(0xf3, 0xf3, 0xf3);
193 const SkColor kAvatarBubbleGaiaBackgroundColor =
194     SkColorSetRGB(0xf5, 0xf5, 0xf5);
195 const SkColor kUserManagerBackgroundColor = SkColorSetRGB(0xee, 0xee, 0xee);
196
197 const char kDefaultUrlPrefix[] = "chrome://theme/IDR_PROFILE_AVATAR_";
198 const char kGAIAPictureFileName[] = "Google Profile Picture.png";
199 const char kHighResAvatarFolderName[] = "Avatars";
200
201 // This avatar does not exist on the server, the high res copy is in the build.
202 const char kNoHighResAvatar[] = "NothingToDownload";
203
204 // The size of the function-static kDefaultAvatarIconResources array below.
205 const size_t kDefaultAvatarIconsCount = 27;
206
207 // The first 8 icons are generic.
208 const size_t kGenericAvatarIconsCount = 8;
209
210 // The avatar used as a placeholder (grey silhouette).
211 const int kPlaceholderAvatarIcon = 26;
212
213 gfx::Image GetSizedAvatarIcon(const gfx::Image& image,
214                               bool is_rectangle,
215                               int width, int height) {
216   if (!is_rectangle && image.Height() <= height)
217     return image;
218
219   gfx::Size size(width, height);
220
221   // Source for a centered, sized icon. GAIA images get a border.
222   scoped_ptr<gfx::ImageSkiaSource> source(
223       new AvatarImageSource(
224           *image.ToImageSkia(),
225           size,
226           std::min(width, height),
227           AvatarImageSource::POSITION_CENTER,
228           AvatarImageSource::BORDER_NONE));
229
230   return gfx::Image(gfx::ImageSkia(source.release(), size));
231 }
232
233 gfx::Image GetAvatarIconForMenu(const gfx::Image& image,
234                                 bool is_rectangle) {
235   return GetSizedAvatarIcon(
236       image, is_rectangle, kAvatarIconWidth, kAvatarIconHeight);
237 }
238
239 gfx::Image GetAvatarIconForWebUI(const gfx::Image& image,
240                                  bool is_rectangle) {
241   return GetSizedAvatarIcon(image, is_rectangle,
242                             kAvatarIconWidth, kAvatarIconHeight);
243 }
244
245 gfx::Image GetAvatarIconForTitleBar(const gfx::Image& image,
246                                     bool is_gaia_image,
247                                     int dst_width,
248                                     int dst_height) {
249   // The image requires no border or resizing.
250   if (!is_gaia_image && image.Height() <= kAvatarIconHeight)
251     return image;
252
253   int size = std::min(std::min(kAvatarIconWidth, kAvatarIconHeight),
254                       std::min(dst_width, dst_height));
255   gfx::Size dst_size(dst_width, dst_height);
256
257   // Source for a sized icon drawn at the bottom center of the canvas,
258   // with an etched border (for GAIA images).
259   scoped_ptr<gfx::ImageSkiaSource> source(
260       new AvatarImageSource(
261           *image.ToImageSkia(),
262           dst_size,
263           size,
264           AvatarImageSource::POSITION_BOTTOM_CENTER,
265           is_gaia_image ? AvatarImageSource::BORDER_ETCHED :
266               AvatarImageSource::BORDER_NONE));
267
268   return gfx::Image(gfx::ImageSkia(source.release(), dst_size));
269 }
270
271 SkBitmap GetAvatarIconAsSquare(const SkBitmap& source_bitmap,
272                                int scale_factor) {
273   SkBitmap square_bitmap;
274   if ((source_bitmap.width() == scale_factor * profiles::kAvatarIconWidth) &&
275       (source_bitmap.height() == scale_factor * profiles::kAvatarIconHeight)) {
276     // Shave a couple of columns so the |source_bitmap| is more square. So when
277     // resized to a square aspect ratio it looks pretty.
278     gfx::Rect frame(scale_factor * profiles::kAvatarIconWidth,
279                     scale_factor * profiles::kAvatarIconHeight);
280     frame.Inset(scale_factor * 2, 0, scale_factor * 2, 0);
281     source_bitmap.extractSubset(&square_bitmap, gfx::RectToSkIRect(frame));
282   } else {
283     // If not the avatar icon's aspect ratio, the image should be square.
284     DCHECK(source_bitmap.width() == source_bitmap.height());
285     square_bitmap = source_bitmap;
286   }
287   return square_bitmap;
288 }
289
290 // Helper methods for accessing, transforming and drawing avatar icons.
291 size_t GetDefaultAvatarIconCount() {
292   return kDefaultAvatarIconsCount;
293 }
294
295 size_t GetGenericAvatarIconCount() {
296   return kGenericAvatarIconsCount;
297 }
298
299 int GetPlaceholderAvatarIndex() {
300   return kPlaceholderAvatarIcon;
301 }
302
303 int GetPlaceholderAvatarIconResourceID() {
304   return IDR_PROFILE_AVATAR_26;
305 }
306
307 const IconResourceInfo* GetDefaultAvatarIconResourceInfo(size_t index) {
308   static const IconResourceInfo resource_info[kDefaultAvatarIconsCount] = {
309     { IDR_PROFILE_AVATAR_0, "avatar_generic.png"},
310     { IDR_PROFILE_AVATAR_1, "avatar_generic_aqua.png"},
311     { IDR_PROFILE_AVATAR_2, "avatar_generic_blue.png"},
312     { IDR_PROFILE_AVATAR_3, "avatar_generic_green.png"},
313     { IDR_PROFILE_AVATAR_4, "avatar_generic_orange.png"},
314     { IDR_PROFILE_AVATAR_5, "avatar_generic_purple.png"},
315     { IDR_PROFILE_AVATAR_6, "avatar_generic_red.png"},
316     { IDR_PROFILE_AVATAR_7, "avatar_generic_yellow.png"},
317     { IDR_PROFILE_AVATAR_8, "avatar_secret_agent.png"},
318     { IDR_PROFILE_AVATAR_9, "avatar_superhero.png"},
319     { IDR_PROFILE_AVATAR_10, "avatar_volley_ball.png"},
320     { IDR_PROFILE_AVATAR_11, "avatar_businessman.png"},
321     { IDR_PROFILE_AVATAR_12, "avatar_ninja.png"},
322     { IDR_PROFILE_AVATAR_13, "avatar_alien.png"},
323     { IDR_PROFILE_AVATAR_14, "avatar_smiley.png"},
324     { IDR_PROFILE_AVATAR_15, "avatar_flower.png"},
325     { IDR_PROFILE_AVATAR_16, "avatar_pizza.png"},
326     { IDR_PROFILE_AVATAR_17, "avatar_soccer.png"},
327     { IDR_PROFILE_AVATAR_18, "avatar_burger.png"},
328     { IDR_PROFILE_AVATAR_19, "avatar_cat.png"},
329     { IDR_PROFILE_AVATAR_20, "avatar_cupcake.png"},
330     { IDR_PROFILE_AVATAR_21, "avatar_dog.png"},
331     { IDR_PROFILE_AVATAR_22, "avatar_horse.png"},
332     { IDR_PROFILE_AVATAR_23, "avatar_margarita.png"},
333     { IDR_PROFILE_AVATAR_24, "avatar_note.png"},
334     { IDR_PROFILE_AVATAR_25, "avatar_sun_cloud.png"},
335     { IDR_PROFILE_AVATAR_26, kNoHighResAvatar},
336   };
337   return &resource_info[index];
338 }
339
340 int GetDefaultAvatarIconResourceIDAtIndex(size_t index) {
341   DCHECK(IsDefaultAvatarIconIndex(index));
342   return GetDefaultAvatarIconResourceInfo(index)->resource_id;
343 }
344
345 const char* GetDefaultAvatarIconFileNameAtIndex(size_t index) {
346   DCHECK(index < kDefaultAvatarIconsCount);
347   return GetDefaultAvatarIconResourceInfo(index)->filename;
348 }
349
350 const char* GetNoHighResAvatarFileName() {
351   return kNoHighResAvatar;
352 }
353
354 base::FilePath GetPathOfHighResAvatarAtIndex(size_t index) {
355   std::string file_name = GetDefaultAvatarIconResourceInfo(index)->filename;
356   base::FilePath user_data_dir;
357   PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
358   return user_data_dir.AppendASCII(
359       kHighResAvatarFolderName).AppendASCII(file_name);
360 }
361
362 std::string GetDefaultAvatarIconUrl(size_t index) {
363   DCHECK(IsDefaultAvatarIconIndex(index));
364   return base::StringPrintf("%s%" PRIuS, kDefaultUrlPrefix, index);
365 }
366
367 bool IsDefaultAvatarIconIndex(size_t index) {
368   return index < kDefaultAvatarIconsCount;
369 }
370
371 bool IsDefaultAvatarIconUrl(const std::string& url, size_t* icon_index) {
372   DCHECK(icon_index);
373   if (url.find(kDefaultUrlPrefix) != 0)
374     return false;
375
376   int int_value = -1;
377   if (base::StringToInt(base::StringPiece(url.begin() +
378                                           strlen(kDefaultUrlPrefix),
379                                           url.end()),
380                         &int_value)) {
381     if (int_value < 0 ||
382         int_value >= static_cast<int>(kDefaultAvatarIconsCount))
383       return false;
384     *icon_index = int_value;
385     return true;
386   }
387
388   return false;
389 }
390
391 }  // namespace profiles