From bbf354581d6ad35b12e86ba2248e27039284cd62 Mon Sep 17 00:00:00 2001 From: Martin Gerhardy Date: Sun, 20 Mar 2016 15:49:12 +0100 Subject: [PATCH] UI: fixed the issue with garbled fonts that is mentioned in #85 --- src/modules/ui/FontDefinition.cpp | 23 ++++++++++++----------- src/modules/ui/FontDefinition.h | 17 +++++++++-------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/modules/ui/FontDefinition.cpp b/src/modules/ui/FontDefinition.cpp index daba7050a..74317edc0 100644 --- a/src/modules/ui/FontDefinition.cpp +++ b/src/modules/ui/FontDefinition.cpp @@ -51,7 +51,7 @@ FontDefinition::FontDefinition() { lua.pop(); FontDef *def = new FontDef(id, height, metricsHeight, metricsAscender, metricsDescender); - FontChars fontChars(128); + FontChars fontChars; // push the chars table const int chars = lua.getTable("chars"); @@ -76,7 +76,7 @@ FontDefinition::FontDefinition() { const int h = lua.getValueIntegerFromTable("h"); const int ox = lua.getValueIntegerFromTable("ox"); const int oy = lua.getValueIntegerFromTable("oy"); - const FontChar c(character, width, x, y, w, h, ox, oy); + FontChar* c = new FontChar(character, width, x, y, w, h, ox, oy); fontChars.push_back(c); // pop the char entry lua.pop(); @@ -104,11 +104,11 @@ FontDefinition::FontDefinition() { Log::debug(LOG_UI, "Loaded %i font definitions", (int)_fontDefs.size()); } -void FontDef::init (const FontChars& fontChars) +void FontDef::init (FontChars& fontChars) { SDL_assert(!fontChars.empty()); - for (const FontChar& c : fontChars) { - _fontCharMap[c.getCharacter()] = c; + for (FontChar* c : fontChars) { + _fontCharMap[c->getCharacter()] = c; } } @@ -118,9 +118,9 @@ void FontDef::updateChars (int tWidth, int tHeight) _heightFactor = tHeight / (float)textureHeight; for (auto entry : _fontCharMap) { - FontChar& c = entry.second; - c.setWidthFactor(_widthFactor); - c.setHeightFactor(_heightFactor); + FontChar* c = entry.second; + c->setWidthFactor(_widthFactor); + c->setHeightFactor(_heightFactor); } } @@ -129,9 +129,10 @@ const FontChar* FontDef::getFontChar (int character) auto iter = _fontCharMap.find(character); if (iter == _fontCharMap.end()) return nullptr; - const FontChar& fc = iter->second; - if (fc.getCharacter() != '\0') - return &fc; + const FontChar* fc = iter->second; + SDL_assert_always(fc != nullptr); + if (fc->getCharacter() != '\0') + return fc; return nullptr; } diff --git a/src/modules/ui/FontDefinition.h b/src/modules/ui/FontDefinition.h index 555e934b4..71fbf5c7c 100644 --- a/src/modules/ui/FontDefinition.h +++ b/src/modules/ui/FontDefinition.h @@ -27,11 +27,6 @@ class FontChar { { } - FontChar () : - character('\0'), width(-1), x(-1), y(-1), w(-1), h(-1), ox(-1), oy(-1), widthFactor(1.0f), heightFactor(1.0f) - { - } - inline void setWidthFactor (float _widthFactor) { widthFactor = _widthFactor; @@ -85,7 +80,7 @@ class FontChar { } }; -typedef std::vector FontChars; +typedef std::vector FontChars; class FontDef { public: @@ -101,6 +96,12 @@ class FontDef { { } + ~FontDef() { + for (auto i : _fontCharMap) { + delete i.second; + } + } + inline int getHeight () const { const float newHeight = _height * _heightFactor; @@ -125,10 +126,10 @@ class FontDef { const FontChar* getFontChar (int character); void updateChars (int textureWidth, int textureHeight); - void init (const FontChars& fontChars); + void init (FontChars& fontChars); private: - std::unordered_map _fontCharMap; + std::unordered_map _fontCharMap; int _height; int _metricsHeight; int _metricsAscender;