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