diff options
author | bmdhacks <bmd@bmdhacks.com> | 2025-02-03 09:47:46 -0800 |
---|---|---|
committer | bmdhacks <bmd@bmdhacks.com> | 2025-02-03 09:47:46 -0800 |
commit | ad588b2197d810b0dc1d9c803250f31bf23a51ec (patch) | |
tree | ba46b64297b103ad80671e2a89927e2f7706135f /source/core/StarFont.cpp | |
parent | 27ffdb020534e3d815038c86d9ac6d1dddaf6598 (diff) |
implement lazy font loading
Diffstat (limited to 'source/core/StarFont.cpp')
-rw-r--r-- | source/core/StarFont.cpp | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/source/core/StarFont.cpp b/source/core/StarFont.cpp index d854d52..dc0a2a7 100644 --- a/source/core/StarFont.cpp +++ b/source/core/StarFont.cpp @@ -39,13 +39,6 @@ FontPtr Font::loadFont(String const& fileName, unsigned pixelSize) { FontPtr Font::loadFont(ByteArrayConstPtr const& bytes, unsigned pixelSize) { FontPtr font = make_shared<Font>(); font->m_fontBuffer = bytes; - - shared_ptr<FontImpl> fontImpl = make_shared<FontImpl>(); - if (FT_New_Memory_Face( - ftContext.library, (FT_Byte const*)font->m_fontBuffer->ptr(), font->m_fontBuffer->size(), 0, &fontImpl->face)) - throw FontException("Could not load font from buffer"); - - font->m_fontImpl = fontImpl; font->setPixelSize(pixelSize); return font; @@ -65,8 +58,6 @@ void Font::setPixelSize(unsigned pixelSize) { if (m_pixelSize == pixelSize) return; - if (FT_Set_Pixel_Sizes(m_fontImpl->face, pixelSize, 0)) - throw FontException(strf("Cannot set font pixel size to: {}", pixelSize)); m_pixelSize = pixelSize; } @@ -82,6 +73,7 @@ unsigned Font::width(String::Char c) { if (auto width = m_widthCache.maybe({c, m_pixelSize})) { return *width; } else { + loadFontImpl(); FT_Load_Char(m_fontImpl->face, c, FontLoadFlags); unsigned newWidth = (m_fontImpl->face->glyph->linearHoriAdvance + 32768) / 65536; m_widthCache.insert({c, m_pixelSize}, newWidth); @@ -89,11 +81,25 @@ unsigned Font::width(String::Char c) { } } +void Font::loadFontImpl() { + if (!m_fontImpl) { + shared_ptr<FontImpl> fontImpl = make_shared<FontImpl>(); + if (FT_New_Memory_Face(ftContext.library, (FT_Byte const*)m_fontBuffer->ptr(), m_fontBuffer->size(), 0, &fontImpl->face)) + throw FontException("Could not load font from buffer"); + + if (FT_Set_Pixel_Sizes(fontImpl->face, m_pixelSize, 0)) + throw FontException(strf("Cannot set font pixel size to: {}", m_pixelSize)); + + m_fontImpl = fontImpl; + } +} tuple<Image, Vec2I, bool> Font::render(String::Char c) { - if (!m_fontImpl) + if (!m_fontImpl && !m_fontBuffer) throw FontException("Font::render called on uninitialized font."); + loadFontImpl(); + FT_Face face = m_fontImpl->face; if (FT_Load_Glyph(face, FT_Get_Char_Index(face, c), FontLoadFlags) != 0) return {}; @@ -152,6 +158,7 @@ tuple<Image, Vec2I, bool> Font::render(String::Char c) { } bool Font::exists(String::Char c) { + loadFontImpl(); return FT_Get_Char_Index(m_fontImpl->face, c); } |