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.
5 #include "components/favicon/core/fallback_icon_service.h"
9 #include "components/favicon/core/fallback_icon_client.h"
10 #include "components/favicon_base/fallback_icon_style.h"
11 #include "third_party/skia/include/core/SkPaint.h"
12 #include "ui/gfx/canvas.h"
13 #include "ui/gfx/codec/png_codec.h"
14 #include "ui/gfx/font_list.h"
15 #include "ui/gfx/geometry/rect.h"
16 #include "ui/gfx/geometry/size.h"
22 // Arbitrary maximum icon size, can be reasonably increased if needed.
23 const int kMaxFallbackFaviconSize
= 288;
27 FallbackIconService::FallbackIconService(
28 FallbackIconClient
* fallback_icon_client
)
29 : fallback_icon_client_(fallback_icon_client
) {
32 FallbackIconService::~FallbackIconService() {
35 std::vector
<unsigned char> FallbackIconService::RenderFallbackIconBitmap(
38 const favicon_base::FallbackIconStyle
& style
) {
39 int size_to_use
= std::min(kMaxFallbackFaviconSize
, size
);
40 gfx::Canvas
canvas(gfx::Size(size_to_use
, size_to_use
), 1.0f
, false);
41 DrawFallbackIcon(icon_url
, size_to_use
, style
, &canvas
);
43 std::vector
<unsigned char> bitmap_data
;
44 if (!gfx::PNGCodec::EncodeBGRASkBitmap(canvas
.ExtractImageRep().sk_bitmap(),
45 false, &bitmap_data
)) {
51 void FallbackIconService::DrawFallbackIcon(
54 const favicon_base::FallbackIconStyle
& style
,
55 gfx::Canvas
* canvas
) {
56 const int kOffsetX
= 0;
57 const int kOffsetY
= 0;
59 paint
.setStyle(SkPaint::kFill_Style
);
60 paint
.setAntiAlias(true);
62 // Draw a filled, colored rounded square.
63 paint
.setColor(style
.background_color
);
64 int corner_radius
= static_cast<int>(size
* style
.roundness
* 0.5 + 0.5);
65 canvas
->DrawRoundRect(
66 gfx::Rect(kOffsetX
, kOffsetY
, size
, size
), corner_radius
, paint
);
69 base::string16 icon_text
=
70 fallback_icon_client_
->GetFallbackIconText(icon_url
);
71 if (icon_text
.empty())
73 int font_size
= static_cast<int>(size
* style
.font_size_ratio
);
77 canvas
->DrawStringRectWithFlags(
79 gfx::FontList(fallback_icon_client_
->GetFontNameList(), gfx::Font::NORMAL
,
82 gfx::Rect(kOffsetX
, kOffsetY
, size
, size
),
83 gfx::Canvas::TEXT_ALIGN_CENTER
);
86 } // namespace favicon