#include "ash/style/text_image.h"
#include "ash/style/typography.h"
#include "base/strings/utf_string_conversion_utils.h"
#include "ui/base/models/image_model.h"
#include "ui/color/color_provider.h"
#include "ui/color/color_variant.h"
#include "ui/gfx/canvas.h"
namespace ash {
namespace {
gfx::ImageSkia Generator(const gfx::Size& size,
base_icu::UChar32 symbol,
ui::ColorVariant color,
const ui::ColorProvider* color_provider) {
auto text_image = std::make_unique<TextImage>(size, symbol);
text_image->set_color(color.ResolveToSkColor(color_provider));
return gfx::ImageSkia(std::move(text_image), size);
}
std::u16string ToString(base_icu::UChar32 symbol) {
std::u16string str;
base::WriteUnicodeCharacter(symbol, &str);
return str;
}
}
TextImage::TextImage(const gfx::Size& size, base_icu::UChar32 symbol)
: gfx::CanvasImageSource(size), symbol_(ToString(symbol)) {}
TextImage::~TextImage() = default;
ui::ImageModel TextImage::AsImageModel(const gfx::Size& size,
base_icu::UChar32 symbol,
ui::ColorVariant color) {
return ui::ImageModel::FromImageGenerator(
base::BindRepeating(&Generator, size, symbol, color), size);
}
void TextImage::Draw(gfx::Canvas* canvas) {
const int font_size = size().height();
const gfx::FontList font({"Google Sans", "Roboto"}, gfx::Font::NORMAL,
font_size, gfx::Font::Weight::MEDIUM);
canvas->DrawStringRectWithFlags(symbol_, font, color_, gfx::Rect(size()),
gfx::Canvas::TEXT_ALIGN_CENTER);
}
}