From de6c8fd70442d3a5ef05224ecbc07e1b06aede90 Mon Sep 17 00:00:00 2001 From: Bill Meltsner Date: Thu, 15 Apr 2010 14:04:16 -0400 Subject: [PATCH] fonts now work (seemingly) as well as they used to --- src/modules/graphics/opengl/Font.cpp | 35 ++++++++++++----------- src/modules/graphics/opengl/Font.h | 6 ++-- src/modules/graphics/opengl/wrap_Font.cpp | 2 +- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/src/modules/graphics/opengl/Font.cpp b/src/modules/graphics/opengl/Font.cpp index b86b16ebc..f0b67517e 100644 --- a/src/modules/graphics/opengl/Font.cpp +++ b/src/modules/graphics/opengl/Font.cpp @@ -31,7 +31,7 @@ namespace opengl { Font::Font(love::font::FontData * data) - : height(data->getHeight()), lineHeight(1.25) + : height(data->getHeight()), lineHeight(1.25), mSpacing(1) { glyphs = new Glyph*[MAX_CHARS]; for(unsigned int i = 0; i < MAX_CHARS; i++) @@ -52,7 +52,7 @@ namespace opengl float Font::getHeight() const { - return height; + return height / lineHeight; } void Font::print(std::string text, float x, float y) const @@ -64,45 +64,48 @@ namespace opengl { glPushMatrix(); - glTranslatef(ceil(x), ceil(y+getHeight()), 0.0f); + glTranslatef(ceil(x), ceil(y), 0.0f); glRotatef(LOVE_TODEG(angle), 0, 0, 1.0f); glScalef(sx, sy, 1.0f); - int s = 0; + int first = (int)text[0]; + int s = -bearingX[first]; + int by = bearingY[first]; for (unsigned int i = 0; i < text.size(); i++) { int g = (int)text[i]; - glyphs[g]->draw(bearingX[g] + s, -bearingY[g], 0, 1, 1, 0, 0); - s += spacing[g]; + if (!glyphs[g]) g = 32; // space + glyphs[g]->draw(bearingX[g] + s, -bearingY[g]+by, 0, 1, 1, 0, 0); + s += spacing[g] * mSpacing; } glPopMatrix(); } void Font::print(char character, float x, float y) const { - glyphs[character]->draw(x, y+getHeight(), 0, 1, 1, 0, 0); + if (!glyphs[character]) character = ' '; + glyphs[character]->draw(x, y+height, 0, 1, 1, 0, 0); } - float Font::getWidth(const std::string & line) const + int Font::getWidth(const std::string & line) const { if(line.size() == 0) return 0; - float temp = 0; + int temp = 0; - for(unsigned int i = 0; i < line.size() - 1; i++) + for(unsigned int i = 0; i < line.size(); i++) { - temp += widths[(int)line[i]] + (spacing[(int)line[i]] * mSpacing); + temp += (spacing[(int)line[i]] * mSpacing); } - temp += widths[(int)line[line.size() - 1]]; // the last character's spacing isn't counted - + return temp; } - float Font::getWidth(const char * line) const + int Font::getWidth(const char * line) const { return this->getWidth(std::string(line)); } - float Font::getWidth(const char character) const + int Font::getWidth(const char character) const { - return (float)widths[(int)character]; + return widths[(int)character]; } void Font::setLineHeight(float height) diff --git a/src/modules/graphics/opengl/Font.h b/src/modules/graphics/opengl/Font.h index 736e99740..eb8779d14 100644 --- a/src/modules/graphics/opengl/Font.h +++ b/src/modules/graphics/opengl/Font.h @@ -102,15 +102,15 @@ namespace opengl * * @param line A line of text. **/ - float getWidth(const std::string & line) const; - float getWidth(const char * line) const; + int getWidth(const std::string & line) const; + int getWidth(const char * line) const; /** * Returns the width of the passed character. * * @param character A character. **/ - float getWidth(const char character) const; + int getWidth(const char character) const; /** * Sets the line height (which should be a number to multiply the font size by, diff --git a/src/modules/graphics/opengl/wrap_Font.cpp b/src/modules/graphics/opengl/wrap_Font.cpp index 711ff8dfd..5191f0b18 100644 --- a/src/modules/graphics/opengl/wrap_Font.cpp +++ b/src/modules/graphics/opengl/wrap_Font.cpp @@ -43,7 +43,7 @@ namespace opengl { Font * t = luax_checkfont(L, 1); const char * str = luaL_checkstring(L, 2); - lua_pushnumber(L, t->getWidth(str)); + lua_pushinteger(L, t->getWidth(str)); return 1; }