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