OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/favicon_base/fallback_icon_service.h" | 5 #include "components/favicon/core/fallback_icon_service.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/i18n/case_conversion.h" | 9 #include "base/i18n/case_conversion.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "components/favicon_base/fallback_icon_style.h" | 11 #include "components/favicon_base/fallback_icon_style.h" |
12 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 12 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
13 #include "third_party/skia/include/core/SkPaint.h" | 13 #include "third_party/skia/include/core/SkPaint.h" |
14 #include "ui/gfx/canvas.h" | 14 #include "ui/gfx/canvas.h" |
15 #include "ui/gfx/codec/png_codec.h" | 15 #include "ui/gfx/codec/png_codec.h" |
16 #include "ui/gfx/font_list.h" | 16 #include "ui/gfx/font_list.h" |
17 #include "ui/gfx/geometry/rect.h" | 17 #include "ui/gfx/geometry/rect.h" |
18 #include "ui/gfx/geometry/size.h" | 18 #include "ui/gfx/geometry/size.h" |
19 #include "url/gurl.h" | 19 #include "url/gurl.h" |
20 | 20 |
21 namespace favicon_base { | |
22 | |
23 namespace { | 21 namespace { |
24 | 22 |
25 // Arbitrary maximum icon size, can be reasonably increased if needed. | 23 // Arbitrary maximum icon size, can be reasonably increased if needed. |
26 const int kMaxFallbackFaviconSize = 288; | 24 const int kMaxFallbackFaviconSize = 288; |
27 | 25 |
28 // Returns a single character to represent a page URL. To do this we take the | 26 // Returns a single character to represent a page URL. To do this we take the |
29 // first letter in a domain's name and make it upper case. | 27 // first letter in a domain's name and make it upper case. |
30 // TODO(huangs): Handle non-ASCII ("xn--") domain names. | 28 // TODO(huangs): Handle non-ASCII ("xn--") domain names. |
31 base::string16 GetFallbackIconText(const GURL& url) { | 29 base::string16 GetFallbackIconText(const GURL& url) { |
32 std::string domain = net::registry_controlled_domains::GetDomainAndRegistry( | 30 std::string domain = net::registry_controlled_domains::GetDomainAndRegistry( |
33 url, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | 31 url, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
34 return domain.empty() ? base::string16() : | 32 return domain.empty() ? base::string16() : |
35 base::i18n::ToUpper(base::ASCIIToUTF16(domain.substr(0, 1))); | 33 base::i18n::ToUpper(base::ASCIIToUTF16(domain.substr(0, 1))); |
36 } | 34 } |
37 | 35 |
38 } // namespace | 36 } // namespace |
39 | 37 |
40 FallbackIconService::FallbackIconService( | 38 FallbackIconService::FallbackIconService( |
41 const std::vector<std::string>& font_list) | 39 const std::vector<std::string>& font_list) |
42 : font_list_(font_list) { | 40 : font_list_(font_list) { |
43 } | 41 } |
44 | 42 |
45 FallbackIconService::~FallbackIconService() { | 43 FallbackIconService::~FallbackIconService() { |
46 } | 44 } |
47 | 45 |
48 std::vector<unsigned char> FallbackIconService::RenderFallbackIconBitmap( | 46 std::vector<unsigned char> FallbackIconService::RenderFallbackIconBitmap( |
49 const GURL& icon_url, | 47 const GURL& icon_url, |
50 int size, | 48 int size, |
51 const FallbackIconStyle& style) { | 49 const favicon_base::FallbackIconStyle& style) { |
52 int size_to_use = std::min(kMaxFallbackFaviconSize, size); | 50 int size_to_use = std::min(kMaxFallbackFaviconSize, size); |
53 gfx::Canvas canvas(gfx::Size(size_to_use, size_to_use), 1.0f, false); | 51 gfx::Canvas canvas(gfx::Size(size_to_use, size_to_use), 1.0f, false); |
54 DrawFallbackIcon(icon_url, size_to_use, style, &canvas); | 52 DrawFallbackIcon(icon_url, size_to_use, style, &canvas); |
55 | 53 |
56 std::vector<unsigned char> bitmap_data; | 54 std::vector<unsigned char> bitmap_data; |
57 if (!gfx::PNGCodec::EncodeBGRASkBitmap(canvas.ExtractImageRep().sk_bitmap(), | 55 if (!gfx::PNGCodec::EncodeBGRASkBitmap(canvas.ExtractImageRep().sk_bitmap(), |
58 false, &bitmap_data)) { | 56 false, &bitmap_data)) { |
59 bitmap_data.clear(); | 57 bitmap_data.clear(); |
60 } | 58 } |
61 return bitmap_data; | 59 return bitmap_data; |
62 } | 60 } |
63 | 61 |
64 void FallbackIconService::DrawFallbackIcon(const GURL& icon_url, | 62 void FallbackIconService::DrawFallbackIcon( |
65 int size, | 63 const GURL& icon_url, |
66 const FallbackIconStyle& style, | 64 int size, |
67 gfx::Canvas* canvas) { | 65 const favicon_base::FallbackIconStyle& style, |
| 66 gfx::Canvas* canvas) { |
68 const int kOffsetX = 0; | 67 const int kOffsetX = 0; |
69 const int kOffsetY = 0; | 68 const int kOffsetY = 0; |
70 SkPaint paint; | 69 SkPaint paint; |
71 paint.setStyle(SkPaint::kFill_Style); | 70 paint.setStyle(SkPaint::kFill_Style); |
72 paint.setAntiAlias(true); | 71 paint.setAntiAlias(true); |
73 | 72 |
74 // Draw a filled, colored rounded square. | 73 // Draw a filled, colored rounded square. |
75 paint.setColor(style.background_color); | 74 paint.setColor(style.background_color); |
76 int corner_radius = static_cast<int>(size * style.roundness * 0.5 + 0.5); | 75 int corner_radius = static_cast<int>(size * style.roundness * 0.5 + 0.5); |
77 canvas->DrawRoundRect( | 76 canvas->DrawRoundRect( |
78 gfx::Rect(kOffsetX, kOffsetY, size, size), corner_radius, paint); | 77 gfx::Rect(kOffsetX, kOffsetY, size, size), corner_radius, paint); |
79 | 78 |
80 // Draw text. | 79 // Draw text. |
81 base::string16 icon_text = GetFallbackIconText(icon_url); | 80 base::string16 icon_text = GetFallbackIconText(icon_url); |
82 if (icon_text.empty()) | 81 if (icon_text.empty()) |
83 return; | 82 return; |
84 int font_size = static_cast<int>(size * style.font_size_ratio); | 83 int font_size = static_cast<int>(size * style.font_size_ratio); |
85 if (font_size <= 0) | 84 if (font_size <= 0) |
86 return; | 85 return; |
87 | 86 |
88 // TODO(huangs): See how expensive gfx::FontList() is, and possibly cache. | 87 // TODO(huangs): See how expensive gfx::FontList() is, and possibly cache. |
89 canvas->DrawStringRectWithFlags( | 88 canvas->DrawStringRectWithFlags( |
90 icon_text, | 89 icon_text, |
91 gfx::FontList(font_list_, gfx::Font::NORMAL, font_size), | 90 gfx::FontList(font_list_, gfx::Font::NORMAL, font_size), |
92 style.text_color, | 91 style.text_color, |
93 gfx::Rect(kOffsetX, kOffsetY, size, size), | 92 gfx::Rect(kOffsetX, kOffsetY, size, size), |
94 gfx::Canvas::TEXT_ALIGN_CENTER); | 93 gfx::Canvas::TEXT_ALIGN_CENTER); |
95 } | 94 } |
96 | |
97 } // namespace favicon_base | |
OLD | NEW |