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 "base/file_util.h" |
7 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
8 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/path_service.h" |
9 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
10 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "chrome/common/chrome_paths.h" |
11 #include "grit/generated_resources.h" | 14 #include "grit/generated_resources.h" |
12 #include "grit/theme_resources.h" | 15 #include "grit/theme_resources.h" |
13 #include "third_party/skia/include/core/SkPaint.h" | 16 #include "third_party/skia/include/core/SkPaint.h" |
14 #include "third_party/skia/include/core/SkPath.h" | 17 #include "third_party/skia/include/core/SkPath.h" |
15 #include "third_party/skia/include/core/SkScalar.h" | 18 #include "third_party/skia/include/core/SkScalar.h" |
16 #include "third_party/skia/include/core/SkXfermode.h" | 19 #include "third_party/skia/include/core/SkXfermode.h" |
17 #include "ui/base/resource/resource_bundle.h" | 20 #include "ui/base/resource/resource_bundle.h" |
18 #include "ui/gfx/canvas.h" | 21 #include "ui/gfx/canvas.h" |
19 #include "ui/gfx/image/canvas_image_source.h" | 22 #include "ui/gfx/image/canvas_image_source.h" |
| 23 #include "ui/gfx/image/image.h" |
20 #include "ui/gfx/image/image_skia_operations.h" | 24 #include "ui/gfx/image/image_skia_operations.h" |
21 | 25 |
22 // Helper methods for transforming and drawing avatar icons. | 26 // Helper methods for transforming and drawing avatar icons. |
23 namespace { | 27 namespace { |
24 | 28 |
25 // Determine what the scaled height of the avatar icon should be for a | 29 // Determine what the scaled height of the avatar icon should be for a |
26 // specified width, to preserve the aspect ratio. | 30 // specified width, to preserve the aspect ratio. |
27 int GetScaledAvatarHeightForWidth(int width, const gfx::ImageSkia& avatar) { | 31 int GetScaledAvatarHeightForWidth(int width, const gfx::ImageSkia& avatar) { |
28 | |
29 // Multiply the width by the inverted aspect ratio (height over | 32 // Multiply the width by the inverted aspect ratio (height over |
30 // width), and then add 0.5 to ensure the int truncation rounds nicely. | 33 // width), and then add 0.5 to ensure the int truncation rounds nicely. |
31 int scaled_height = width * | 34 int scaled_height = width * |
32 ((float) avatar.height() / (float) avatar.width()) + 0.5f; | 35 ((float) avatar.height() / (float) avatar.width()) + 0.5f; |
33 return scaled_height; | 36 return scaled_height; |
34 } | 37 } |
35 | 38 |
36 // A CanvasImageSource that draws a sized and positioned avatar with an | 39 // A CanvasImageSource that draws a sized and positioned avatar with an |
37 // optional border independently of the scale factor. | 40 // optional border independently of the scale factor. |
38 class AvatarImageSource : public gfx::CanvasImageSource { | 41 class AvatarImageSource : public gfx::CanvasImageSource { |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 | 216 |
214 gfx::Size size(width, height); | 217 gfx::Size size(width, height); |
215 | 218 |
216 // Source for a centered, sized icon. GAIA images get a border. | 219 // Source for a centered, sized icon. GAIA images get a border. |
217 scoped_ptr<gfx::ImageSkiaSource> source( | 220 scoped_ptr<gfx::ImageSkiaSource> source( |
218 new AvatarImageSource( | 221 new AvatarImageSource( |
219 *image.ToImageSkia(), | 222 *image.ToImageSkia(), |
220 size, | 223 size, |
221 std::min(width, height), | 224 std::min(width, height), |
222 AvatarImageSource::POSITION_CENTER, | 225 AvatarImageSource::POSITION_CENTER, |
223 AvatarImageSource::BORDER_NONE)); | 226 is_rectangle ? AvatarImageSource::BORDER_NORMAL : |
| 227 AvatarImageSource::BORDER_NONE)); |
224 | 228 |
225 return gfx::Image(gfx::ImageSkia(source.release(), size)); | 229 return gfx::Image(gfx::ImageSkia(source.release(), size)); |
226 } | 230 } |
227 | 231 |
228 gfx::Image GetAvatarIconForMenu(const gfx::Image& image, | 232 gfx::Image GetAvatarIconForMenu(const gfx::Image& image, |
229 bool is_rectangle) { | 233 bool is_rectangle) { |
230 return GetSizedAvatarIcon( | 234 return GetSizedAvatarIcon( |
231 image, is_rectangle, kAvatarIconWidth, kAvatarIconHeight); | 235 image, is_rectangle, kAvatarIconWidth, kAvatarIconHeight); |
232 } | 236 } |
233 | 237 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 | 324 |
321 const char* GetDefaultAvatarIconFileNameAtIndex(size_t index) { | 325 const char* GetDefaultAvatarIconFileNameAtIndex(size_t index) { |
322 DCHECK(index < kDefaultAvatarIconsCount); | 326 DCHECK(index < kDefaultAvatarIconsCount); |
323 return GetDefaultAvatarIconResourceInfo(index)->filename; | 327 return GetDefaultAvatarIconResourceInfo(index)->filename; |
324 } | 328 } |
325 | 329 |
326 const char* GetNoHighResAvatarFileName() { | 330 const char* GetNoHighResAvatarFileName() { |
327 return kNoHighResAvatar; | 331 return kNoHighResAvatar; |
328 } | 332 } |
329 | 333 |
| 334 base::FilePath GetPathOfHighResAvatarAtIndex(size_t index) { |
| 335 std::string file_name = GetDefaultAvatarIconResourceInfo(index)->filename; |
| 336 base::FilePath user_data_dir; |
| 337 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
| 338 return user_data_dir.AppendASCII( |
| 339 kHighResAvatarFolderName).AppendASCII(file_name); |
| 340 } |
| 341 |
330 std::string GetDefaultAvatarIconUrl(size_t index) { | 342 std::string GetDefaultAvatarIconUrl(size_t index) { |
331 DCHECK(IsDefaultAvatarIconIndex(index)); | 343 DCHECK(IsDefaultAvatarIconIndex(index)); |
332 return base::StringPrintf("%s%" PRIuS, kDefaultUrlPrefix, index); | 344 return base::StringPrintf("%s%" PRIuS, kDefaultUrlPrefix, index); |
333 } | 345 } |
334 | 346 |
335 bool IsDefaultAvatarIconIndex(size_t index) { | 347 bool IsDefaultAvatarIconIndex(size_t index) { |
336 return index < kDefaultAvatarIconsCount; | 348 return index < kDefaultAvatarIconsCount; |
337 } | 349 } |
338 | 350 |
339 bool IsDefaultAvatarIconUrl(const std::string& url, | 351 bool IsDefaultAvatarIconUrl(const std::string& url, size_t* icon_index) { |
340 size_t* icon_index) { | |
341 DCHECK(icon_index); | 352 DCHECK(icon_index); |
342 if (url.find(kDefaultUrlPrefix) != 0) | 353 if (url.find(kDefaultUrlPrefix) != 0) |
343 return false; | 354 return false; |
344 | 355 |
345 int int_value = -1; | 356 int int_value = -1; |
346 if (base::StringToInt(base::StringPiece(url.begin() + | 357 if (base::StringToInt(base::StringPiece(url.begin() + |
347 strlen(kDefaultUrlPrefix), | 358 strlen(kDefaultUrlPrefix), |
348 url.end()), | 359 url.end()), |
349 &int_value)) { | 360 &int_value)) { |
350 if (int_value < 0 || | 361 if (int_value < 0 || |
351 int_value >= static_cast<int>(kDefaultAvatarIconsCount)) | 362 int_value >= static_cast<int>(kDefaultAvatarIconsCount)) |
352 return false; | 363 return false; |
353 *icon_index = int_value; | 364 *icon_index = int_value; |
354 return true; | 365 return true; |
355 } | 366 } |
356 | 367 |
357 return false; | 368 return false; |
358 } | 369 } |
359 | 370 |
360 } // namespace profiles | 371 } // namespace profiles |
OLD | NEW |