OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/favicon_base/fallback_icon_service.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "base/i18n/case_conversion.h" | |
10 #include "base/strings/utf_string_conversions.h" | |
11 #include "components/favicon_base/favicon_types.h" | |
12 #include "grit/platform_locale_settings.h" | |
13 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | |
14 #include "third_party/skia/include/core/SkPaint.h" | |
15 #include "ui/base/l10n/l10n_util.h" | |
16 #include "ui/gfx/canvas.h" | |
17 #include "ui/gfx/codec/png_codec.h" | |
18 #include "ui/gfx/font_list.h" | |
19 #include "ui/gfx/geometry/rect.h" | |
20 #include "ui/gfx/geometry/size.h" | |
21 #include "url/gurl.h" | |
22 | |
23 namespace favicon_base { | |
24 | |
25 namespace { | |
26 | |
27 const int kMaxFallbackFaviconSize = 288; | |
28 | |
29 // Returns a single character to represent a fallback URL. To do this we take | |
30 // the first letter in a domain's name and make it upper case. Currently we do | |
31 // not handle non-ASCII domain names. | |
pkotwicz
2015/01/23 16:00:48
Nit: fallback URL -> page URL
huangs
2015/01/23 19:47:52
Done, updated comment to have TODO(huangs) for non
| |
32 base::string16 GetFallbackIconText(const GURL& url) { | |
33 std::string domain = net::registry_controlled_domains::GetDomainAndRegistry( | |
34 url, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | |
35 return domain.empty() ? base::string16() : | |
36 base::i18n::ToUpper(base::ASCIIToUTF16(std::string(1, domain[0]))); | |
37 } | |
38 | |
39 } // namespace | |
40 | |
41 FallbackIconService::FallbackIconService() { | |
42 std::string font_name = | |
43 l10n_util::GetStringUTF8(IDS_SANS_SERIF_FONT_FAMILY); | |
44 #if defined(OS_CHROMEOS) | |
45 const std::string kChromeOSFontFamily = "Noto Sans"; | |
46 font_name = kChromeOSFontFamily; | |
47 #endif | |
48 fallback_icon_font_list_.push_back(font_name); | |
49 } | |
50 | |
51 FallbackIconService::~FallbackIconService() { | |
52 } | |
53 | |
54 std::vector<unsigned char> FallbackIconService::RenderFallbackIconBitmap( | |
55 const GURL& icon_url, | |
56 int size_in_pixels, | |
57 const FallbackIconStyle& style) { | |
58 size_in_pixels = std::min(kMaxFallbackFaviconSize, size_in_pixels); | |
59 gfx::Canvas canvas(gfx::Size(size_in_pixels, size_in_pixels), 1.0f, false); | |
60 DrawFallbackIcon(icon_url, 0, 0, size_in_pixels, style, &canvas); | |
61 | |
62 std::vector<unsigned char> bitmap_data; | |
63 if (!gfx::PNGCodec::EncodeBGRASkBitmap(canvas.ExtractImageRep().sk_bitmap(), | |
64 false, &bitmap_data)) { | |
65 bitmap_data.clear(); | |
66 } | |
67 return bitmap_data; | |
68 } | |
69 | |
pkotwicz
2015/01/23 16:00:48
Nit: Skip |x| and |y| for now.
Btw, I think the |
huangs
2015/01/23 19:47:52
Done. Will add them later if needed. Nice catch
| |
70 void FallbackIconService::DrawFallbackIcon(const GURL& icon_url, | |
71 int x, | |
72 int y, | |
73 int size_in_pixels, | |
pkotwicz
2015/01/23 16:00:48
Nit: |size_in_pixels| -> |size|
huangs
2015/01/23 19:47:52
Done. Note I updated RenderFallbackIconBitmap() a
| |
74 const FallbackIconStyle& style, | |
75 gfx::Canvas* canvas) { | |
76 SkPaint paint; | |
77 paint.setStyle(SkPaint::kFill_Style); | |
78 paint.setAntiAlias(true); | |
79 | |
80 // Draw a filled, colored rounded square. | |
81 paint.setColor(style.background_color); | |
82 int corner_radius = static_cast<int>( | |
83 size_in_pixels * style.roundness * 0.5 + 0.5); | |
84 canvas->DrawRoundRect(gfx::Rect(x, y, size_in_pixels, size_in_pixels), | |
85 corner_radius, paint); | |
86 | |
87 // Draw text. | |
88 base::string16 icon_text = GetFallbackIconText(icon_url); | |
89 if (!icon_text.empty()) { | |
90 int font_size = static_cast<int>(size_in_pixels * style.font_size_ratio); | |
91 if (font_size > 0) { | |
92 // TODO(huangs): See how expensive gfx::FontList() is, and possibly cache. | |
93 canvas->DrawStringRectWithFlags( | |
94 icon_text, | |
95 gfx::FontList(fallback_icon_font_list_, gfx::Font::NORMAL, font_size), | |
96 style.text_color, | |
97 gfx::Rect(0, 0, size_in_pixels, size_in_pixels), | |
98 gfx::Canvas::TEXT_ALIGN_CENTER); | |
99 } | |
100 } | |
101 } | |
102 | |
103 } // namespace favicon_base | |
OLD | NEW |