diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2024-04-24 07:44:53 +1000 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2024-04-24 07:44:53 +1000 |
commit | d0f8aec244a0d71f67863f94cab4c5f84d93de22 (patch) | |
tree | a8e69fa28b7841d942e7e5f994518a69916c45c8 /source/rendering/StarFontTextureGroup.cpp | |
parent | 6ac139321b2a03d71192f852ff958cf6176e1c2d (diff) |
feat: unicode emoji support + other stuff
Diffstat (limited to 'source/rendering/StarFontTextureGroup.cpp')
-rw-r--r-- | source/rendering/StarFontTextureGroup.cpp | 67 |
1 files changed, 46 insertions, 21 deletions
diff --git a/source/rendering/StarFontTextureGroup.cpp b/source/rendering/StarFontTextureGroup.cpp index d5e6ef9..5f0cdbc 100644 --- a/source/rendering/StarFontTextureGroup.cpp +++ b/source/rendering/StarFontTextureGroup.cpp @@ -14,13 +14,13 @@ void FontTextureGroup::cleanup(int64_t timeout) { void FontTextureGroup::switchFont(String const& font) { if (font.empty()) { - m_font = m_defaultFont; + m_activeFont = m_defaultFont; m_fontName.clear(); } else if (m_fontName != font) { m_fontName = font; auto find = m_fonts.find(font); - m_font = find != m_fonts.end() ? find->second : m_defaultFont; + m_activeFont = find != m_fonts.end() ? find->second : m_defaultFont; } } @@ -28,31 +28,35 @@ String const& FontTextureGroup::activeFont() { return m_fontName; } -void FontTextureGroup::addFont(FontPtr const& font, String const& name, bool isDefault) { +void FontTextureGroup::addFont(FontPtr const& font, String const& name) { m_fonts[name] = font; - if (isDefault) - m_defaultFont = m_font = font; } void FontTextureGroup::clearFonts() { m_fonts.clear(); - m_font = m_defaultFont; + m_activeFont = m_defaultFont; } -void FontTextureGroup::setFallbackFont(String const& fontName) { - if (auto font = m_fonts.ptr(fontName)) - m_fallbackFont = *font; +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; } const FontTextureGroup::GlyphTexture& FontTextureGroup::glyphTexture(String::Char c, unsigned size, Directives const* processingDirectives) { - Font* font = (m_font->exists(c) || !m_fallbackFont) ? m_font.get() : m_fallbackFont.get(); + 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; if (res.second) { font->setPixelSize(size); - auto pair = font->render(c); - Image& image = pair.first; + auto renderResult = font->render(c); + Image& image = get<0>(renderResult); if (processingDirectives) { try { Directives const& directives = *processingDirectives; @@ -61,7 +65,7 @@ const FontTextureGroup::GlyphTexture& FontTextureGroup::glyphTexture(String::Cha for (auto& entry : directives->entries) processImageOperation(entry.operation, image); - res.first->second.offset = (preSize - Vec2F(image.size())) / 2; + glyphTexture.offset = (preSize - Vec2F(image.size())) / 2; } catch (StarException const&) { image.forEachPixel([](unsigned x, unsigned y, Vec4B& pixel) { @@ -69,15 +73,14 @@ const FontTextureGroup::GlyphTexture& FontTextureGroup::glyphTexture(String::Cha }); } } - else - res.first->second.offset = Vec2F(); - res.first->second.offset += Vec2F(pair.second); - res.first->second.texture = m_textureGroup->create(image); + glyphTexture.colored = get<2>(renderResult); + glyphTexture.offset += Vec2F(get<1>(renderResult)); + glyphTexture.texture = m_textureGroup->create(image); } - res.first->second.time = Time::monotonicMilliseconds(); - return res.first->second; + glyphTexture.time = Time::monotonicMilliseconds(); + return glyphTexture; } TexturePtr FontTextureGroup::glyphTexturePtr(String::Char c, unsigned size) { @@ -89,9 +92,31 @@ TexturePtr FontTextureGroup::glyphTexturePtr(String::Char c, unsigned size, Dire } unsigned FontTextureGroup::glyphWidth(String::Char c, unsigned fontSize) { - Font* font = (m_font->exists(c) || !m_fallbackFont) ? m_font.get() : m_fallbackFont.get(); + Font* font = getFontForCharacter(c); font->setPixelSize(fontSize); return font->width(c); } +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(); +} + } |