| 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/core/fallback_icon_service.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 | |
| 9 #include <algorithm> | |
| 10 | |
| 11 #include "cc/paint/skia_paint_canvas.h" | |
| 12 #include "components/favicon/core/fallback_icon_client.h" | |
| 13 #include "components/favicon/core/fallback_url_util.h" | |
| 14 #include "components/favicon_base/fallback_icon_style.h" | |
| 15 #include "third_party/skia/include/core/SkPaint.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 { | |
| 24 namespace { | |
| 25 | |
| 26 // Arbitrary maximum icon size, can be reasonably increased if needed. | |
| 27 const int kMaxFallbackFaviconSize = 288; | |
| 28 | |
| 29 } // namespace | |
| 30 | |
| 31 FallbackIconService::FallbackIconService( | |
| 32 FallbackIconClient* fallback_icon_client) | |
| 33 : fallback_icon_client_(fallback_icon_client) { | |
| 34 } | |
| 35 | |
| 36 FallbackIconService::~FallbackIconService() { | |
| 37 } | |
| 38 | |
| 39 std::vector<unsigned char> FallbackIconService::RenderFallbackIconBitmap( | |
| 40 const GURL& icon_url, | |
| 41 int size, | |
| 42 const favicon_base::FallbackIconStyle& style) { | |
| 43 int size_to_use = std::min(kMaxFallbackFaviconSize, size); | |
| 44 SkBitmap bitmap; | |
| 45 bitmap.allocN32Pixels(size_to_use, size_to_use, false); | |
| 46 cc::SkiaPaintCanvas paint_canvas(bitmap); | |
| 47 gfx::Canvas canvas(&paint_canvas, 1.f); | |
| 48 | |
| 49 DrawFallbackIcon(icon_url, size_to_use, style, &canvas); | |
| 50 | |
| 51 std::vector<unsigned char> bitmap_data; | |
| 52 if (!gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &bitmap_data)) | |
| 53 bitmap_data.clear(); | |
| 54 return bitmap_data; | |
| 55 } | |
| 56 | |
| 57 void FallbackIconService::DrawFallbackIcon( | |
| 58 const GURL& icon_url, | |
| 59 int size, | |
| 60 const favicon_base::FallbackIconStyle& style, | |
| 61 gfx::Canvas* canvas) { | |
| 62 const int kOffsetX = 0; | |
| 63 const int kOffsetY = 0; | |
| 64 cc::PaintFlags flags; | |
| 65 flags.setStyle(cc::PaintFlags::kFill_Style); | |
| 66 flags.setAntiAlias(true); | |
| 67 | |
| 68 // Draw a filled, colored rounded square. | |
| 69 flags.setColor(style.background_color); | |
| 70 int corner_radius = static_cast<int>(size * style.roundness * 0.5 + 0.5); | |
| 71 canvas->DrawRoundRect(gfx::Rect(kOffsetX, kOffsetY, size, size), | |
| 72 corner_radius, flags); | |
| 73 | |
| 74 // Draw text. | |
| 75 base::string16 icon_text = GetFallbackIconText(icon_url); | |
| 76 if (icon_text.empty()) | |
| 77 return; | |
| 78 int font_size = static_cast<int>(size * style.font_size_ratio); | |
| 79 if (font_size <= 0) | |
| 80 return; | |
| 81 | |
| 82 canvas->DrawStringRectWithFlags( | |
| 83 icon_text, | |
| 84 gfx::FontList(fallback_icon_client_->GetFontNameList(), gfx::Font::NORMAL, | |
| 85 font_size, gfx::Font::Weight::NORMAL), | |
| 86 style.text_color, gfx::Rect(kOffsetX, kOffsetY, size, size), | |
| 87 gfx::Canvas::TEXT_ALIGN_CENTER); | |
| 88 } | |
| 89 | |
| 90 } // namespace favicon | |
| OLD | NEW |