2023-06-20 04:33:09 +00:00
|
|
|
#include "StarFontTextureGroup.hpp"
|
|
|
|
#include "StarTime.hpp"
|
|
|
|
#include "StarImageProcessing.hpp"
|
|
|
|
|
|
|
|
namespace Star {
|
|
|
|
|
2023-06-21 09:46:23 +00:00
|
|
|
FontTextureGroup::FontTextureGroup(TextureGroupPtr textureGroup)
|
2024-02-19 15:55:19 +00:00
|
|
|
: m_textureGroup(std::move(textureGroup)) {}
|
2023-06-20 04:33:09 +00:00
|
|
|
|
|
|
|
void FontTextureGroup::cleanup(int64_t timeout) {
|
|
|
|
int64_t currentTime = Time::monotonicMilliseconds();
|
|
|
|
eraseWhere(m_glyphs, [&](auto const& p) { return currentTime - p.second.time > timeout; });
|
|
|
|
}
|
|
|
|
|
2023-06-21 09:46:23 +00:00
|
|
|
void FontTextureGroup::switchFont(String const& font) {
|
2023-06-21 12:29:40 +00:00
|
|
|
if (font.empty()) {
|
|
|
|
m_font = m_defaultFont;
|
|
|
|
m_fontName.clear();
|
|
|
|
}
|
|
|
|
else if (m_fontName != font) {
|
2023-06-21 09:46:23 +00:00
|
|
|
m_fontName = font;
|
|
|
|
auto find = m_fonts.find(font);
|
|
|
|
m_font = find != m_fonts.end() ? find->second : m_defaultFont;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-21 12:29:40 +00:00
|
|
|
String const& FontTextureGroup::activeFont() {
|
|
|
|
return m_fontName;
|
|
|
|
}
|
|
|
|
|
2023-06-23 08:54:39 +00:00
|
|
|
void FontTextureGroup::addFont(FontPtr const& font, String const& name, bool isDefault) {
|
2023-06-21 09:46:23 +00:00
|
|
|
m_fonts[name] = font;
|
2023-06-23 08:54:39 +00:00
|
|
|
if (isDefault)
|
2023-06-21 09:46:23 +00:00
|
|
|
m_defaultFont = m_font = font;
|
|
|
|
}
|
|
|
|
|
2023-06-21 13:13:37 +00:00
|
|
|
void FontTextureGroup::clearFonts() {
|
|
|
|
m_fonts.clear();
|
|
|
|
m_font = m_defaultFont;
|
|
|
|
}
|
|
|
|
|
2023-07-03 20:01:29 +00:00
|
|
|
const FontTextureGroup::GlyphTexture& FontTextureGroup::glyphTexture(String::Char c, unsigned size, Directives const* processingDirectives)
|
2023-06-20 14:59:41 +00:00
|
|
|
{
|
2023-07-03 20:01:29 +00:00
|
|
|
auto res = m_glyphs.insert(GlyphDescriptor{c, size, processingDirectives ? processingDirectives->hash() : 0, m_font.get()}, GlyphTexture());
|
2023-06-20 04:33:09 +00:00
|
|
|
|
|
|
|
if (res.second) {
|
|
|
|
m_font->setPixelSize(size);
|
2023-07-03 04:21:51 +00:00
|
|
|
auto pair = m_font->render(c);
|
|
|
|
Image& image = pair.first;
|
2023-07-03 20:01:29 +00:00
|
|
|
if (processingDirectives && *processingDirectives) {
|
2023-06-21 10:29:23 +00:00
|
|
|
try {
|
|
|
|
Vec2F preSize = Vec2F(image.size());
|
2023-07-03 20:01:29 +00:00
|
|
|
|
|
|
|
for (auto& entry : processingDirectives->shared->entries)
|
|
|
|
processImageOperation(entry.operation, image);
|
|
|
|
|
2023-07-03 04:21:51 +00:00
|
|
|
res.first->second.offset = (preSize - Vec2F(image.size())) / 2;
|
2023-06-21 10:29:23 +00:00
|
|
|
}
|
2024-04-14 22:03:26 +00:00
|
|
|
catch (StarException const&) {
|
2023-06-21 10:29:23 +00:00
|
|
|
image.forEachPixel([](unsigned x, unsigned y, Vec4B& pixel) {
|
|
|
|
pixel = ((x + y) % 2 == 0) ? Vec4B(255, 0, 255, pixel[3]) : Vec4B(0, 0, 0, pixel[3]);
|
|
|
|
});
|
2023-06-21 08:59:15 +00:00
|
|
|
}
|
2023-06-20 14:59:41 +00:00
|
|
|
}
|
|
|
|
else
|
2023-07-03 04:21:51 +00:00
|
|
|
res.first->second.offset = Vec2F();
|
2023-06-20 04:33:09 +00:00
|
|
|
|
2023-07-03 09:46:56 +00:00
|
|
|
res.first->second.offset += Vec2F(pair.second);
|
2023-06-20 04:33:09 +00:00
|
|
|
res.first->second.texture = m_textureGroup->create(image);
|
|
|
|
}
|
|
|
|
|
|
|
|
res.first->second.time = Time::monotonicMilliseconds();
|
2023-06-20 14:59:41 +00:00
|
|
|
return res.first->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
TexturePtr FontTextureGroup::glyphTexturePtr(String::Char c, unsigned size) {
|
2023-07-03 20:01:29 +00:00
|
|
|
return glyphTexture(c, size, nullptr).texture;
|
2023-06-20 14:59:41 +00:00
|
|
|
}
|
|
|
|
|
2023-07-03 20:01:29 +00:00
|
|
|
TexturePtr FontTextureGroup::glyphTexturePtr(String::Char c, unsigned size, Directives const* processingDirectives) {
|
2023-06-20 14:59:41 +00:00
|
|
|
return glyphTexture(c, size, processingDirectives).texture;
|
2023-06-20 04:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned FontTextureGroup::glyphWidth(String::Char c, unsigned fontSize) {
|
|
|
|
m_font->setPixelSize(fontSize);
|
|
|
|
return m_font->width(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|