diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2025-02-04 10:49:26 +1100 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2025-02-04 10:49:26 +1100 |
commit | 6f3e477f78e8a0cedea25ee8c4355d7ef8b6bd87 (patch) | |
tree | 9537d6f0a6a50b2b935b5a4bcc54d308fd82eb13 /source/core | |
parent | 0f569ea0700d62d2bdd43cb80b400cf97eec7fa5 (diff) |
fix font size updates, add destructor
Diffstat (limited to 'source/core')
-rw-r--r-- | source/core/StarFont.cpp | 33 | ||||
-rw-r--r-- | source/core/StarFont.hpp | 1 |
2 files changed, 20 insertions, 14 deletions
diff --git a/source/core/StarFont.cpp b/source/core/StarFont.cpp index dc0a2a7..6452d54 100644 --- a/source/core/StarFont.cpp +++ b/source/core/StarFont.cpp @@ -15,7 +15,7 @@ struct FTContext { FTContext() { library = nullptr; if (FT_Init_FreeType(&library)) - throw FontException("Could not initialize freetype library."); + throw FontException("Could not initialize FreeType library."); } ~FTContext() { @@ -46,19 +46,24 @@ FontPtr Font::loadFont(ByteArrayConstPtr const& bytes, unsigned pixelSize) { Font::Font() : m_pixelSize(0), m_alphaThreshold(0) {} +Font::~Font() { + if (m_fontImpl) + FT_Done_Face(m_fontImpl->face); +} + FontPtr Font::clone() const { return Font::loadFont(m_fontBuffer, m_pixelSize); } void Font::setPixelSize(unsigned pixelSize) { - if (pixelSize == 0) { + if (pixelSize == 0) pixelSize = 1; - } if (m_pixelSize == pixelSize) return; - m_pixelSize = pixelSize; + if (m_fontImpl && FT_Set_Pixel_Sizes(m_fontImpl->face, m_pixelSize = pixelSize, 0)) + throw FontException(strf("Cannot set font pixel size to: {}", m_pixelSize)); } void Font::setAlphaThreshold(uint8_t alphaThreshold) { @@ -73,7 +78,7 @@ unsigned Font::width(String::Char c) { if (auto width = m_widthCache.maybe({c, m_pixelSize})) { return *width; } else { - loadFontImpl(); + 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); @@ -83,21 +88,21 @@ 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 (!m_fontBuffer || m_fontBuffer->empty()) + throw FontException("Font buffer is null or empty"); - if (FT_Set_Pixel_Sizes(fontImpl->face, m_pixelSize, 0)) - throw FontException(strf("Cannot set font pixel size to: {}", m_pixelSize)); + 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::format("Could not load font from buffer"); - m_fontImpl = fontImpl; + if (FT_Set_Pixel_Sizes(fontImpl->face, m_pixelSize, 0)) + throw FontException::format("Cannot set font pixel size to: {}", m_pixelSize); + + m_fontImpl = fontImpl; } } tuple<Image, Vec2I, bool> Font::render(String::Char c) { - if (!m_fontImpl && !m_fontBuffer) - throw FontException("Font::render called on uninitialized font."); - loadFontImpl(); FT_Face face = m_fontImpl->face; diff --git a/source/core/StarFont.hpp b/source/core/StarFont.hpp index 40a6513..09d22c0 100644 --- a/source/core/StarFont.hpp +++ b/source/core/StarFont.hpp @@ -18,6 +18,7 @@ public: static FontPtr loadFont(ByteArrayConstPtr const& bytes, unsigned pixelSize = 12); Font(); + ~Font(); Font(Font const&) = delete; Font const& operator=(Font const&) = delete; |