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

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

Issue 835903005: [Favicon] Add new fallback icon rendering flow. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync and merge. 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/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 page URL. To do this we take the
30 // first letter in a domain's name and make it upper case.
31 // TODO(huangs): Handle non-ASCII ("xn--") domain names.
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);
stevenjb 2015/01/29 21:41:08 Pt this in #else to avoid duplicate assignment on
huangs 2015/01/29 23:48:27 Done.
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,
57 const FallbackIconStyle& style) {
58 int size_to_use = std::min(kMaxFallbackFaviconSize, size);
59 gfx::Canvas canvas(gfx::Size(size_to_use, size_to_use), 1.0f, false);
60 DrawFallbackIcon(icon_url, size_to_use, 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
70 void FallbackIconService::DrawFallbackIcon(const GURL& icon_url,
71 int size,
72 const FallbackIconStyle& style,
73 gfx::Canvas* canvas) {
74 const int x = 0;
75 const int y = 0;
stevenjb 2015/01/29 21:41:08 Either name these meaningfully as constants, e.g.
huangs 2015/01/29 23:48:27 Taking kOffsetX/Y.
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>(size * style.roundness * 0.5 + 0.5);
83 canvas->DrawRoundRect(gfx::Rect(x, y, size, size),
84 corner_radius, paint);
stevenjb 2015/01/29 21:41:08 Align (better yet, use git cl format)
huangs 2015/01/29 23:48:27 Moved content to following line.
85
86 // Draw text.
87 base::string16 icon_text = GetFallbackIconText(icon_url);
88 if (!icon_text.empty()) {
89 int font_size = static_cast<int>(size * style.font_size_ratio);
90 if (font_size > 0) {
91 // TODO(huangs): See how expensive gfx::FontList() is, and possibly cache.
92 canvas->DrawStringRectWithFlags(
93 icon_text,
94 gfx::FontList(fallback_icon_font_list_, gfx::Font::NORMAL, font_size),
95 style.text_color,
96 gfx::Rect(x, y, size, size),
97 gfx::Canvas::TEXT_ALIGN_CENTER);
98 }
99 }
100 }
101
102 } // namespace favicon_base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698