Skip to content

Commit

Permalink
UI: fixed the issue with garbled fonts that is mentioned in #85
Browse files Browse the repository at this point in the history
  • Loading branch information
mgerhardy committed Mar 20, 2016
1 parent c64d6d2 commit bbf3545
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
23 changes: 12 additions & 11 deletions src/modules/ui/FontDefinition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -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);
}
}

Expand All @@ -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;
}
17 changes: 9 additions & 8 deletions src/modules/ui/FontDefinition.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -85,7 +80,7 @@ class FontChar {
}
};

typedef std::vector<FontChar> FontChars;
typedef std::vector<FontChar*> FontChars;

class FontDef {
public:
Expand All @@ -101,6 +96,12 @@ class FontDef {
{
}

~FontDef() {
for (auto i : _fontCharMap) {
delete i.second;
}
}

inline int getHeight () const
{
const float newHeight = _height * _heightFactor;
Expand All @@ -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<int, FontChar> _fontCharMap;
std::unordered_map<int, FontChar*> _fontCharMap;
int _height;
int _metricsHeight;
int _metricsAscender;
Expand Down

0 comments on commit bbf3545

Please sign in to comment.