osb/source/rendering/StarFontTextureGroup.cpp

146 lines
5.2 KiB
C++
Raw Normal View History

2023-06-20 04:33:09 +00:00
#include "StarFontTextureGroup.hpp"
#include "StarTime.hpp"
#include "StarImageProcessing.hpp"
#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)
: 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_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);
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;
}
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();
m_activeFont = m_defaultFont;
2023-06-21 13:13:37 +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;
}
2023-07-03 20:01:29 +00:00
const FontTextureGroup::GlyphTexture& FontTextureGroup::glyphTexture(String::Char c, unsigned size, Directives const* processingDirectives)
{
Font* font = getFontForCharacter(c);
if (font == m_emojiFont.get())
processingDirectives = nullptr;
auto res = m_glyphs.insert(GlyphDescriptor{c, size, processingDirectives ? processingDirectives->hash() : 0, font}, GlyphTexture());
auto& glyphTexture = res.first->second;
2023-06-20 04:33:09 +00:00
if (res.second) {
font->setPixelSize(size);
auto renderResult = font->render(c);
Image& image = get<0>(renderResult);
if (processingDirectives) {
Directives const& directives = *processingDirectives;
Vec2F preSize = Vec2F(image.size());
bool broken = false;
for (auto& entry : directives->entries) {
if (auto error = entry.operation.ptr<ErrorImageOperation>()) {
if (auto string = error->cause.ptr<std::string>()) {
if (!string->empty()) {
Logger::error("Error in parsed font directives: {}", *string);
string->clear();
}
} else if (auto& exception = error->cause.get<std::exception_ptr>()) {
try
{ std::rethrow_exception(error->cause.get<std::exception_ptr>()); }
catch (std::exception const& e)
{ Logger::error("Exception in parsed font directives: {}", e.what()); };
exception = {};
}
broken |= true;
} else {
try { processImageOperation(entry.operation, image); }
catch (StarException const& e) {
broken |= true;
Logger::error("Exception processing font directives for {}px '{}': {}", size, String(c), e.what());
}
}
}
if (broken) {
glyphTexture.colored = true;
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.offset = (preSize - Vec2F(image.size())) / 2;
}
2023-06-20 04:33:09 +00:00
glyphTexture.colored |= get<2>(renderResult);
glyphTexture.offset += Vec2F(get<1>(renderResult));
glyphTexture.texture = m_textureGroup->create(image);
2023-06-20 04:33:09 +00:00
}
glyphTexture.time = Time::monotonicMilliseconds();
return glyphTexture;
}
TexturePtr FontTextureGroup::glyphTexturePtr(String::Char c, unsigned size) {
2023-07-03 20:01:29 +00:00
return glyphTexture(c, size, nullptr).texture;
}
2023-07-03 20:01:29 +00:00
TexturePtr FontTextureGroup::glyphTexturePtr(String::Char c, unsigned size, Directives const* processingDirectives) {
return glyphTexture(c, size, processingDirectives).texture;
2023-06-20 04:33:09 +00:00
}
unsigned FontTextureGroup::glyphWidth(String::Char c, unsigned fontSize) {
Font* font = getFontForCharacter(c);
font->setPixelSize(fontSize);
return font->width(c);
2023-06-20 04:33:09 +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
}