2023-06-20 04:33:09 +00:00
|
|
|
#include "StarFontTextureGroup.hpp"
|
|
|
|
#include "StarTime.hpp"
|
|
|
|
#include "StarImageProcessing.hpp"
|
2024-04-24 06:29:51 +00:00
|
|
|
#include "StarLogging.hpp"
|
2023-06-20 04:33:09 +00:00
|
|
|
|
|
|
|
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()) {
|
2024-04-23 21:44:53 +00:00
|
|
|
m_activeFont = m_defaultFont;
|
2023-06-21 12:29:40 +00:00
|
|
|
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);
|
2024-04-23 21:44:53 +00:00
|
|
|
m_activeFont = find != m_fonts.end() ? find->second : m_defaultFont;
|
2023-06-21 09:46:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-21 12:29:40 +00:00
|
|
|
String const& FontTextureGroup::activeFont() {
|
|
|
|
return m_fontName;
|
|
|
|
}
|
|
|
|
|
2024-04-23 21:44:53 +00:00
|
|
|
void FontTextureGroup::addFont(FontPtr const& font, String const& name) {
|
2023-06-21 09:46:23 +00:00
|
|
|
m_fonts[name] = font;
|
|
|
|
}
|
|
|
|
|
2023-06-21 13:13:37 +00:00
|
|
|
void FontTextureGroup::clearFonts() {
|
|
|
|
m_fonts.clear();
|
2024-04-23 21:44:53 +00:00
|
|
|
m_activeFont = m_defaultFont;
|
2023-06-21 13:13:37 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 21:44:53 +00:00
|
|
|
void FontTextureGroup::setFixedFonts(String const& defaultFontName, String const& fallbackFontName, String const& emojiFontName) {
|
|
|
|
if (auto defaultFont = m_fonts.ptr(defaultFontName))
|
|
|
|
m_defaultFont = m_activeFont = *defaultFont;
|
|
|
|
if (auto fallbackFont = m_fonts.ptr(fallbackFontName))
|
|
|
|
m_fallbackFont = *fallbackFont;
|
|
|
|
if (auto emojiFont = m_fonts.ptr(emojiFontName))
|
|
|
|
m_emojiFont = *emojiFont;
|
2024-04-21 22:17:10 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2024-04-23 21:44:53 +00:00
|
|
|
Font* font = getFontForCharacter(c);
|
|
|
|
if (font == m_emojiFont.get())
|
|
|
|
processingDirectives = nullptr;
|
2024-04-21 22:17:10 +00:00
|
|
|
auto res = m_glyphs.insert(GlyphDescriptor{c, size, processingDirectives ? processingDirectives->hash() : 0, font}, GlyphTexture());
|
2024-04-23 21:44:53 +00:00
|
|
|
auto& glyphTexture = res.first->second;
|
2023-06-20 04:33:09 +00:00
|
|
|
if (res.second) {
|
2024-04-21 22:17:10 +00:00
|
|
|
font->setPixelSize(size);
|
2024-04-23 21:44:53 +00:00
|
|
|
auto renderResult = font->render(c);
|
|
|
|
Image& image = get<0>(renderResult);
|
2024-04-21 20:07:59 +00:00
|
|
|
if (processingDirectives) {
|
2024-04-24 06:29:51 +00:00
|
|
|
Directives const& directives = *processingDirectives;
|
|
|
|
Vec2F preSize = Vec2F(image.size());
|
|
|
|
|
|
|
|
for (auto& entry : directives->entries) {
|
|
|
|
if (auto error = entry.operation.ptr<ErrorImageOperation>()) {
|
2024-04-24 23:39:23 +00:00
|
|
|
if (auto string = error->cause.ptr<std::string>()) {
|
|
|
|
if (!string->empty()) {
|
|
|
|
Logger::error("Error parsing font directives: {}", *string);
|
|
|
|
string->clear();
|
|
|
|
}
|
|
|
|
} else if (auto& exception = error->cause.get<std::exception_ptr>()) {
|
2024-04-24 06:29:51 +00:00
|
|
|
try
|
2024-04-24 23:39:23 +00:00
|
|
|
{ std::rethrow_exception(error->cause.get<std::exception_ptr>()); }
|
2024-04-24 06:29:51 +00:00
|
|
|
catch (std::exception const& e)
|
2024-04-24 23:39:23 +00:00
|
|
|
{ Logger::error("Exception parsing font directives: {}", e.what()); };
|
|
|
|
exception = {};
|
2024-04-24 06:29:51 +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]);
|
|
|
|
});
|
|
|
|
glyphTexture.colored = true;
|
|
|
|
} else
|
2023-07-03 20:01:29 +00:00
|
|
|
processImageOperation(entry.operation, image);
|
2023-06-21 08:59:15 +00:00
|
|
|
}
|
2024-04-24 06:29:51 +00:00
|
|
|
|
|
|
|
glyphTexture.offset = (preSize - Vec2F(image.size())) / 2;
|
2023-06-20 14:59:41 +00:00
|
|
|
}
|
2023-06-20 04:33:09 +00:00
|
|
|
|
2024-04-24 06:29:51 +00:00
|
|
|
glyphTexture.colored |= get<2>(renderResult);
|
2024-04-23 21:44:53 +00:00
|
|
|
glyphTexture.offset += Vec2F(get<1>(renderResult));
|
|
|
|
glyphTexture.texture = m_textureGroup->create(image);
|
2023-06-20 04:33:09 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 21:44:53 +00:00
|
|
|
glyphTexture.time = Time::monotonicMilliseconds();
|
|
|
|
return glyphTexture;
|
2023-06-20 14:59:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2024-04-23 21:44:53 +00:00
|
|
|
Font* font = getFontForCharacter(c);
|
2024-04-21 22:17:10 +00:00
|
|
|
font->setPixelSize(fontSize);
|
|
|
|
return font->width(c);
|
2023-06-20 04:33:09 +00:00
|
|
|
}
|
|
|
|
|
2024-04-23 21:44:53 +00:00
|
|
|
Font* FontTextureGroup::getFontForCharacter(String::Char c) {
|
|
|
|
if (((c >= 0x1F600 && c <= 0x1F64F) || // Emoticons
|
|
|
|
(c >= 0x1F300 && c <= 0x1F5FF) || // Misc Symbols and Pictographs
|
|
|
|
(c >= 0x1F680 && c <= 0x1F6FF) || // Transport and Map
|
|
|
|
(c >= 0x1F1E6 && c <= 0x1F1FF) || // Regional country flags
|
|
|
|
(c >= 0x2600 && c <= 0x26FF ) || // Misc symbols 9728 - 9983
|
|
|
|
(c >= 0x2700 && c <= 0x27BF ) || // Dingbats
|
|
|
|
(c >= 0xFE00 && c <= 0xFE0F ) || // Variation Selectors
|
|
|
|
(c >= 0x1F900 && c <= 0x1F9FF) || // Supplemental Symbols and Pictographs
|
|
|
|
(c >= 0x1F018 && c <= 0x1F270) || // Various asian characters
|
|
|
|
(c >= 65024 && c <= 65039 ) || // Variation selector
|
|
|
|
(c >= 9100 && c <= 9300 ) || // Misc items
|
|
|
|
(c >= 8400 && c <= 8447 ))&& // Combining Diacritical Marks for Symbols
|
|
|
|
m_emojiFont->exists(c)
|
|
|
|
)
|
|
|
|
return m_emojiFont.get();
|
|
|
|
else if (m_activeFont->exists(c) || !m_fallbackFont)
|
|
|
|
return m_activeFont.get();
|
|
|
|
else
|
|
|
|
return m_fallbackFont.get();
|
|
|
|
}
|
|
|
|
|
2023-06-20 04:33:09 +00:00
|
|
|
}
|