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/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 // Arbitrary maximum icon size, can be reasonably increased if needed. |
| 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(domain.substr(0, 1))); |
| 37 } |
| 38 |
| 39 } // namespace |
| 40 |
| 41 FallbackIconService::FallbackIconService( |
| 42 const std::vector<std::string>& font_list) |
| 43 : font_list_(font_list) { |
| 44 } |
| 45 |
| 46 FallbackIconService::~FallbackIconService() { |
| 47 } |
| 48 |
| 49 std::vector<unsigned char> FallbackIconService::RenderFallbackIconBitmap( |
| 50 const GURL& icon_url, |
| 51 int size, |
| 52 const FallbackIconStyle& style) { |
| 53 int size_to_use = std::min(kMaxFallbackFaviconSize, size); |
| 54 gfx::Canvas canvas(gfx::Size(size_to_use, size_to_use), 1.0f, false); |
| 55 DrawFallbackIcon(icon_url, size_to_use, style, &canvas); |
| 56 |
| 57 std::vector<unsigned char> bitmap_data; |
| 58 if (!gfx::PNGCodec::EncodeBGRASkBitmap(canvas.ExtractImageRep().sk_bitmap(), |
| 59 false, &bitmap_data)) { |
| 60 bitmap_data.clear(); |
| 61 } |
| 62 return bitmap_data; |
| 63 } |
| 64 |
| 65 void FallbackIconService::DrawFallbackIcon(const GURL& icon_url, |
| 66 int size, |
| 67 const FallbackIconStyle& style, |
| 68 gfx::Canvas* canvas) { |
| 69 const int kOffsetX = 0; |
| 70 const int kOffsetY = 0; |
| 71 SkPaint paint; |
| 72 paint.setStyle(SkPaint::kFill_Style); |
| 73 paint.setAntiAlias(true); |
| 74 |
| 75 // Draw a filled, colored rounded square. |
| 76 paint.setColor(style.background_color); |
| 77 int corner_radius = static_cast<int>(size * style.roundness * 0.5 + 0.5); |
| 78 canvas->DrawRoundRect( |
| 79 gfx::Rect(kOffsetX, kOffsetY, size, size), corner_radius, paint); |
| 80 |
| 81 // Draw text. |
| 82 base::string16 icon_text = GetFallbackIconText(icon_url); |
| 83 if (icon_text.empty()) |
| 84 return; |
| 85 int font_size = static_cast<int>(size * style.font_size_ratio); |
| 86 if (font_size <= 0) |
| 87 return; |
| 88 |
| 89 // TODO(huangs): See how expensive gfx::FontList() is, and possibly cache. |
| 90 canvas->DrawStringRectWithFlags( |
| 91 icon_text, |
| 92 gfx::FontList(font_list_, gfx::Font::NORMAL, font_size), |
| 93 style.text_color, |
| 94 gfx::Rect(kOffsetX, kOffsetY, size, size), |
| 95 gfx::Canvas::TEXT_ALIGN_CENTER); |
| 96 } |
| 97 |
| 98 } // namespace favicon_base |
OLD | NEW |