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.
5 #include "components/favicon_base/fallback_icon_service.h"
9 #include "base/i18n/case_conversion.h"
10 #include "base/strings/utf_string_conversions.h"
11 #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"
14 #include "ui/gfx/canvas.h"
15 #include "ui/gfx/codec/png_codec.h"
16 #include "ui/gfx/font_list.h"
17 #include "ui/gfx/geometry/rect.h"
18 #include "ui/gfx/geometry/size.h"
21 namespace favicon_base
{
25 // Arbitrary maximum icon size, can be reasonably increased if needed.
26 const int kMaxFallbackFaviconSize
= 288;
28 // 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.
30 // TODO(huangs): Handle non-ASCII ("xn--") domain names.
31 base::string16
GetFallbackIconText(const GURL
& url
) {
32 std::string domain
= net::registry_controlled_domains::GetDomainAndRegistry(
33 url
, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES
);
34 return domain
.empty() ? base::string16() :
35 base::i18n::ToUpper(base::ASCIIToUTF16(domain
.substr(0, 1)));
40 FallbackIconService::FallbackIconService(
41 const std::vector
<std::string
>& font_list
)
42 : font_list_(font_list
) {
45 FallbackIconService::~FallbackIconService() {
48 std::vector
<unsigned char> FallbackIconService::RenderFallbackIconBitmap(
51 const FallbackIconStyle
& style
) {
52 int size_to_use
= std::min(kMaxFallbackFaviconSize
, size
);
53 gfx::Canvas
canvas(gfx::Size(size_to_use
, size_to_use
), 1.0f
, false);
54 DrawFallbackIcon(icon_url
, size_to_use
, style
, &canvas
);
56 std::vector
<unsigned char> bitmap_data
;
57 if (!gfx::PNGCodec::EncodeBGRASkBitmap(canvas
.ExtractImageRep().sk_bitmap(),
58 false, &bitmap_data
)) {
64 void FallbackIconService::DrawFallbackIcon(const GURL
& icon_url
,
66 const FallbackIconStyle
& style
,
67 gfx::Canvas
* canvas
) {
68 const int kOffsetX
= 0;
69 const int kOffsetY
= 0;
71 paint
.setStyle(SkPaint::kFill_Style
);
72 paint
.setAntiAlias(true);
74 // Draw a filled, colored rounded square.
75 paint
.setColor(style
.background_color
);
76 int corner_radius
= static_cast<int>(size
* style
.roundness
* 0.5 + 0.5);
77 canvas
->DrawRoundRect(
78 gfx::Rect(kOffsetX
, kOffsetY
, size
, size
), corner_radius
, paint
);
81 base::string16 icon_text
= GetFallbackIconText(icon_url
);
82 if (icon_text
.empty())
84 int font_size
= static_cast<int>(size
* style
.font_size_ratio
);
88 // TODO(huangs): See how expensive gfx::FontList() is, and possibly cache.
89 canvas
->DrawStringRectWithFlags(
91 gfx::FontList(font_list_
, gfx::Font::NORMAL
, font_size
),
93 gfx::Rect(kOffsetX
, kOffsetY
, size
, size
),
94 gfx::Canvas::TEXT_ALIGN_CENTER
);
97 } // namespace favicon_base