diff options
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; |