From c78386e648d7912e40c549e5806d5a496b095229 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Thu, 30 Sep 2021 19:49:18 +0200 Subject: [PATCH] Fixed an issue where defining a really small font size would crash the application. --- es-core/src/resources/Font.cpp | 15 ++++++++------- es-core/src/resources/Font.h | 5 +++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/es-core/src/resources/Font.cpp b/es-core/src/resources/Font.cpp index 5cdb52e53..aafd533db 100644 --- a/es-core/src/resources/Font.cpp +++ b/es-core/src/resources/Font.cpp @@ -79,7 +79,14 @@ Font::Font(int size, const std::string& path) : mSize(size) , mPath(path) { - assert(mSize > 0); + if (mSize < 9) { + mSize = 9; + LOG(LogWarning) << "Requested font size too small, changing to minimum supported size"; + } + else if (mSize > Renderer::getScreenHeight()) { + mSize = Renderer::getScreenHeight(); + LOG(LogWarning) << "Requested font size too large, changing to maximum supported size"; + } mMaxGlyphHeight = 0; @@ -274,8 +281,6 @@ FT_Face Font::getFaceForChar(unsigned int id) return mFaceCache.cbegin()->second->face; } -void Font::clearFaceCache() { mFaceCache.clear(); } - Font::Glyph* Font::getGlyph(unsigned int id) { // Is it already loaded? @@ -339,7 +344,6 @@ Font::Glyph* Font::getGlyph(unsigned int id) return &glyph; } -// Completely recreate the texture data for all textures based on mGlyphs information. void Font::rebuildTextures() { // Recreate OpenGL textures. @@ -441,7 +445,6 @@ float Font::getLetterHeight() return glyph->texSize.y * glyph->texture->textureSize.y; } -// Breaks up a normal string with newlines to make it fit xLen. std::string Font::wrapText(std::string text, float xLen) { std::string out; @@ -666,8 +669,6 @@ TextCache* Font::buildTextCache(const std::string& text, x += glyph->advance.x; } - // TextCache::CacheMetrics metrics = { sizeText(text, lineSpacing) }; - TextCache* cache = new TextCache(); cache->vertexLists.resize(vertMap.size()); cache->metrics = {sizeText(text, lineSpacing)}; diff --git a/es-core/src/resources/Font.h b/es-core/src/resources/Font.h index 30a24c872..50e830d22 100644 --- a/es-core/src/resources/Font.h +++ b/es-core/src/resources/Font.h @@ -141,6 +141,7 @@ private: virtual ~FontFace(); }; + // Completely recreate the texture data for all textures based on mGlyphs information. void rebuildTextures(); void unloadTextures(); @@ -152,7 +153,7 @@ private: std::map> mFaceCache; FT_Face getFaceForChar(unsigned int id); - void clearFaceCache(); + void clearFaceCache() { mFaceCache.clear(); } struct Glyph { FontTexture* texture; @@ -170,7 +171,7 @@ private: int mMaxGlyphHeight; - const int mSize; + int mSize; const std::string mPath; float getNewlineStartOffset(const std::string& text,