Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(832)

Side by Side Diff: components/favicon_base/fallback_icon_service.cc

Issue 886163003: [Favicon] Add FallbackIconStyle and FallbackIconService. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Moving FallbackIconStyle to its own file to fix Skia dependency issues. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/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/base/l10n/l10n_util.h"
15 #include "ui/gfx/canvas.h"
16 #include "ui/gfx/codec/png_codec.h"
17 #include "ui/gfx/font_list.h"
18 #include "ui/gfx/geometry/rect.h"
19 #include "ui/gfx/geometry/size.h"
20 #include "url/gurl.h"
21
22 namespace favicon_base {
23
24 namespace {
25
26 const int kMaxFallbackFaviconSize = 288;
stevenjb 2015/02/11 18:04:37 nit: document reason for '288'. "Arbitrary maximum
huangs 2015/02/11 20:28:38 Done.
27
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)));
36 }
37
38 } // namespace
39
40 FallbackIconService::FallbackIconService(
41 const std::vector<std::string>& font_list)
42 : font_list_(font_list) {
43 }
44
45 FallbackIconService::~FallbackIconService() {
46 }
47
48 std::vector<unsigned char> FallbackIconService::RenderFallbackIconBitmap(
49 const GURL& icon_url,
50 int size,
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);
55
56 std::vector<unsigned char> bitmap_data;
57 if (!gfx::PNGCodec::EncodeBGRASkBitmap(canvas.ExtractImageRep().sk_bitmap(),
58 false, &bitmap_data)) {
59 bitmap_data.clear();
60 }
61 return bitmap_data;
62 }
63
64 void FallbackIconService::DrawFallbackIcon(const GURL& icon_url,
65 int size,
66 const FallbackIconStyle& style,
67 gfx::Canvas* canvas) {
68 const int kOffsetX = 0;
69 const int kOffsetY = 0;
70 SkPaint paint;
71 paint.setStyle(SkPaint::kFill_Style);
72 paint.setAntiAlias(true);
73
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);
79
80 // Draw text.
81 base::string16 icon_text = GetFallbackIconText(icon_url);
82 if (!icon_text.empty()) {
stevenjb 2015/02/11 18:04:37 nit: this could be an early exit
huangs 2015/02/11 20:28:38 Done.
83 int font_size = static_cast<int>(size * style.font_size_ratio);
84 if (font_size > 0) {
stevenjb 2015/02/11 18:04:37 nit: this too
huangs 2015/02/11 20:28:38 Done.
85 // TODO(huangs): See how expensive gfx::FontList() is, and possibly cache.
86 canvas->DrawStringRectWithFlags(
87 icon_text,
88 gfx::FontList(font_list_, gfx::Font::NORMAL, font_size),
89 style.text_color,
90 gfx::Rect(kOffsetX, kOffsetY, size, size),
91 gfx::Canvas::TEXT_ALIGN_CENTER);
92 }
93 }
94 }
95
96 } // namespace favicon_base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698