1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55

ash / style / text_image.cc [blame]

// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#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/gfx/canvas.h"

namespace ash {

namespace {

gfx::ImageSkia Generator(const gfx::Size& size,
                         base_icu::UChar32 symbol,
                         ui::ColorId color_id,
                         const ui::ColorProvider* color_provider) {
  auto text_image = std::make_unique<TextImage>(size, symbol);
  text_image->set_color(color_provider->GetColor(color_id));
  return gfx::ImageSkia(std::move(text_image), size);
}

std::u16string ToString(base_icu::UChar32 symbol) {
  std::u16string str;
  base::WriteUnicodeCharacter(symbol, &str);
  return str;
}

}  // namespace

TextImage::TextImage(const gfx::Size& size, base_icu::UChar32 symbol)
    : gfx::CanvasImageSource(size), symbol_(ToString(symbol)) {}

TextImage::~TextImage() = default;

// static
ui::ImageModel TextImage::AsImageModel(const gfx::Size& size,
                                       base_icu::UChar32 symbol,
                                       ui::ColorId color_id) {
  return ui::ImageModel::FromImageGenerator(
      base::BindRepeating(&Generator, size, symbol, color_id), 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);
}

}  // namespace ash