Chromium Code Reviews| 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 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]))); | |
|
Ryan Sleevi
2015/02/03 00:23:17
nit: base::ASCIIToUTF16(domain.substr(0, 1));
huangs
2015/02/04 03:29:36
Done.
| |
| 37 } | |
| 38 | |
| 39 } // namespace | |
| 40 | |
| 41 FallbackIconService::FallbackIconService() { | |
| 42 #if defined(OS_CHROMEOS) | |
| 43 const std::string kChromeOSFontFamily = "Noto Sans"; | |
| 44 std::string font_name = kChromeOSFontFamily; | |
| 45 #else | |
| 46 std::string font_name = | |
| 47 l10n_util::GetStringUTF8(IDS_SANS_SERIF_FONT_FAMILY); | |
| 48 #endif | |
| 49 fallback_icon_font_list_.push_back(font_name); | |
| 50 } | |
| 51 | |
| 52 FallbackIconService::~FallbackIconService() { | |
| 53 } | |
| 54 | |
| 55 std::vector<unsigned char> FallbackIconService::RenderFallbackIconBitmap( | |
| 56 const GURL& icon_url, | |
| 57 int size, | |
| 58 const FallbackIconStyle& style) { | |
| 59 int size_to_use = std::min(kMaxFallbackFaviconSize, size); | |
| 60 gfx::Canvas canvas(gfx::Size(size_to_use, size_to_use), 1.0f, false); | |
| 61 DrawFallbackIcon(icon_url, size_to_use, style, &canvas); | |
| 62 | |
| 63 std::vector<unsigned char> bitmap_data; | |
| 64 if (!gfx::PNGCodec::EncodeBGRASkBitmap(canvas.ExtractImageRep().sk_bitmap(), | |
| 65 false, &bitmap_data)) { | |
| 66 bitmap_data.clear(); | |
| 67 } | |
| 68 return bitmap_data; | |
| 69 } | |
| 70 | |
| 71 void FallbackIconService::DrawFallbackIcon(const GURL& icon_url, | |
| 72 int size, | |
| 73 const FallbackIconStyle& style, | |
| 74 gfx::Canvas* canvas) { | |
| 75 const int kOffsetX = 0; | |
| 76 const int kOffsetY = 0; | |
| 77 SkPaint paint; | |
| 78 paint.setStyle(SkPaint::kFill_Style); | |
| 79 paint.setAntiAlias(true); | |
| 80 | |
| 81 // Draw a filled, colored rounded square. | |
| 82 paint.setColor(style.background_color); | |
| 83 int corner_radius = static_cast<int>(size * style.roundness * 0.5 + 0.5); | |
| 84 canvas->DrawRoundRect( | |
| 85 gfx::Rect(kOffsetX, kOffsetY, size, size), 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 * 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(kOffsetX, kOffsetY, size, size), | |
| 98 gfx::Canvas::TEXT_ALIGN_CENTER); | |
| 99 } | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 } // namespace favicon_base | |
| OLD | NEW |