This source file includes following definitions.
- GetBadgeTextPaintSingleton
- DrawBadgeIconOverlay
- PaintBadge
#include "chrome/common/badge_util.h"
#include "base/logging.h"
#include "base/strings/utf_string_conversions.h"
#include "grit/ui_resources.h"
#include "third_party/skia/include/core/SkPaint.h"
#include "third_party/skia/include/core/SkTypeface.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/canvas.h"
#include "ui/gfx/font.h"
#include "ui/gfx/rect.h"
#include "ui/gfx/size.h"
namespace {
#if defined(OS_LINUX) && !defined(TOOLKIT_VIEWS)
const float kTextSize = 9.0;
const int kBottomMarginBrowserAction = 0;
const int kBottomMarginPageAction = 2;
const int kPadding = 2;
const int kTopTextPadding = 0;
#elif defined(OS_LINUX) && defined(TOOLKIT_VIEWS)
const float kTextSize = 8.0;
const int kBottomMarginBrowserAction = 0;
const int kBottomMarginPageAction = 2;
const int kPadding = 2;
const int kTopTextPadding = 1;
#elif defined(OS_MACOSX)
const float kTextSize = 9.0;
const int kBottomMarginBrowserAction = 5;
const int kBottomMarginPageAction = 2;
const int kPadding = 2;
const int kTopTextPadding = 0;
#else
const float kTextSize = 10;
const int kBottomMarginBrowserAction = 0;
const int kBottomMarginPageAction = 2;
const int kPadding = 2;
const int kTopTextPadding = -1;
#endif
const int kBadgeHeight = 11;
const int kMaxTextWidth = 23;
const int kCenterAlignThreshold = 20;
}
namespace badge_util {
SkPaint* GetBadgeTextPaintSingleton() {
#if defined(OS_MACOSX)
const char kPreferredTypeface[] = "Helvetica Bold";
#else
const char kPreferredTypeface[] = "Arial";
#endif
static SkPaint* text_paint = NULL;
if (!text_paint) {
text_paint = new SkPaint;
text_paint->setAntiAlias(true);
text_paint->setTextAlign(SkPaint::kLeft_Align);
skia::RefPtr<SkTypeface> typeface = skia::AdoptRef(
SkTypeface::CreateFromName(kPreferredTypeface, SkTypeface::kBold));
if (typeface) {
text_paint->setFakeBoldText(true);
} else {
ResourceBundle& rb = ResourceBundle::GetSharedInstance();
const gfx::Font& base_font = rb.GetFont(ResourceBundle::BaseFont);
typeface = skia::AdoptRef(SkTypeface::CreateFromName(
base_font.GetFontName().c_str(), SkTypeface::kNormal));
DCHECK(typeface);
}
text_paint->setTypeface(typeface.get());
}
return text_paint;
}
SkBitmap DrawBadgeIconOverlay(const SkBitmap& icon,
float font_size,
const base::string16& text,
const base::string16& fallback) {
const int kMinPadding = 1;
SkPaint* paint = badge_util::GetBadgeTextPaintSingleton();
paint->setTextSize(SkFloatToScalar(font_size));
paint->setColor(SK_ColorWHITE);
std::string badge_text = base::UTF16ToUTF8(text);
SkScalar text_width = paint->measureText(badge_text.c_str(),
badge_text.size());
if (SkScalarRoundToInt(text_width) > (icon.width() - kMinPadding * 2)) {
badge_text = base::UTF16ToUTF8(fallback);
text_width = paint->measureText(badge_text.c_str(), badge_text.size());
}
int badge_width = icon.width();
if ((SkScalarRoundToInt(text_width) % 1) != (badge_width % 1))
badge_width--;
scoped_ptr<gfx::Canvas> canvas(new gfx::Canvas(
gfx::Size(badge_width, icon.height()), 1.0f, false));
canvas->DrawImageInt(gfx::ImageSkia::CreateFrom1xBitmap(icon), 0, 0);
SkScalar x = (badge_width - text_width)/2;
SkScalar y = (icon.height() - font_size)/2 + font_size - 1;
canvas->sk_canvas()->drawText(
badge_text.c_str(), badge_text.size(), x, y, *paint);
return canvas->ExtractImageRep().sk_bitmap();
}
void PaintBadge(gfx::Canvas* canvas,
const gfx::Rect& bounds,
const std::string& text,
const SkColor& text_color_in,
const SkColor& background_color_in,
int icon_width,
extensions::ActionInfo::Type action_type) {
if (text.empty())
return;
SkColor text_color = text_color_in;
if (SkColorGetA(text_color_in) == 0x00)
text_color = SK_ColorWHITE;
SkColor background_color = background_color_in;
if (SkColorGetA(background_color_in) == 0x00)
background_color = SkColorSetARGB(255, 218, 0, 24);
canvas->Save();
SkPaint* text_paint = badge_util::GetBadgeTextPaintSingleton();
text_paint->setTextSize(SkFloatToScalar(kTextSize));
text_paint->setColor(text_color);
SkScalar sk_text_width = text_paint->measureText(text.c_str(), text.size());
int text_width = std::min(kMaxTextWidth, SkScalarFloorToInt(sk_text_width));
int badge_width = text_width + kPadding * 2;
if (icon_width != 0 && (badge_width % 2 != icon_width % 2))
badge_width += 1;
badge_width = std::max(kBadgeHeight, badge_width);
int rect_height = kBadgeHeight;
int bottom_margin =
action_type == extensions::ActionInfo::TYPE_BROWSER ?
kBottomMarginBrowserAction : kBottomMarginPageAction;
int rect_y = bounds.bottom() - bottom_margin - kBadgeHeight;
int rect_width = badge_width;
int rect_x = (badge_width >= kCenterAlignThreshold) ?
bounds.x() + (bounds.width() - badge_width) / 2 :
bounds.right() - badge_width;
gfx::Rect rect(rect_x, rect_y, rect_width, rect_height);
SkPaint rect_paint;
rect_paint.setStyle(SkPaint::kFill_Style);
rect_paint.setAntiAlias(true);
rect_paint.setColor(background_color);
canvas->DrawRoundRect(rect, 2, rect_paint);
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
gfx::ImageSkia* gradient_left = rb.GetImageSkiaNamed(
IDR_BROWSER_ACTION_BADGE_LEFT);
gfx::ImageSkia* gradient_right = rb.GetImageSkiaNamed(
IDR_BROWSER_ACTION_BADGE_RIGHT);
gfx::ImageSkia* gradient_center = rb.GetImageSkiaNamed(
IDR_BROWSER_ACTION_BADGE_CENTER);
canvas->DrawImageInt(*gradient_left, rect.x(), rect.y());
canvas->TileImageInt(*gradient_center,
rect.x() + gradient_left->width(),
rect.y(),
rect.width() - gradient_left->width() - gradient_right->width(),
rect.height());
canvas->DrawImageInt(*gradient_right,
rect.right() - gradient_right->width(), rect.y());
rect.Inset(kPadding, 0);
canvas->ClipRect(rect);
canvas->sk_canvas()->drawText(
text.c_str(), text.size(),
SkFloatToScalar(rect.x() +
static_cast<float>(rect.width() - text_width) / 2),
SkFloatToScalar(rect.y() + kTextSize + kTopTextPadding),
*text_paint);
canvas->Restore();
}
}