Browse Source

Add Font:getAscent/getDescent/getBaseline (issue #445)

Bart van Strien 12 years ago
parent
commit
32684b674e

+ 17 - 2
src/modules/graphics/opengl/Font.cpp

@@ -291,8 +291,7 @@ void Font::print(const std::string &text, float x, float y, float letter_spacing
 				// copy glyphquad (4 vertices) from original glyph to our current quad list
 				glyphquads.push_back(glyph->quad);
 
-				// 1.25 is magic line height for true type fonts
-				float lineheight = (type == FONT_TRUETYPE) ? floor(getHeight() / 1.25f + 0.5f) : 0.0f;
+				float lineheight = getBaseline();
 
 				// set proper relative position
 				for (int i = 0; i < 4; i++)
@@ -592,6 +591,22 @@ void Font::unloadVolatile()
 	textures.clear();
 }
 
+int Font::getAscent() const
+{
+	return rasterizer->getAscent();
+}
+
+int Font::getDescent() const
+{
+	return rasterizer->getDescent();
+}
+
+float Font::getBaseline() const
+{
+	// 1.25 is magic line height for true type fonts
+	return (type == FONT_TRUETYPE) ? floor(getHeight() / 1.25f + 0.5f) : 0.0f;
+}
+
 } // opengl
 } // graphics
 } // love

+ 5 - 0
src/modules/graphics/opengl/Font.h

@@ -136,6 +136,11 @@ public:
 	bool loadVolatile();
 	void unloadVolatile();
 
+	// Extra font metrics
+	int getAscent() const;
+	int getDescent() const;
+	float getBaseline() const;
+
 private:
 
 	enum FontType

+ 24 - 0
src/modules/graphics/opengl/wrap_Font.cpp

@@ -162,6 +162,27 @@ int w_Font_getMipmapSharpness(lua_State *L)
 	return 1;
 }
 
+int w_Font_getAscent(lua_State *L)
+{
+	Font *t = luax_checkfont(L, 1);
+	lua_pushnumber(L, t->getAscent());
+	return 1;
+}
+
+int w_Font_getDescent(lua_State *L)
+{
+	Font *t = luax_checkfont(L, 1);
+	lua_pushnumber(L, t->getDescent());
+	return 1;
+}
+
+int w_Font_getBaseline(lua_State *L)
+{
+	Font *t = luax_checkfont(L, 1);
+	lua_pushnumber(L, t->getBaseline());
+	return 1;
+}
+
 static const luaL_Reg functions[] =
 {
 	{ "getHeight", w_Font_getHeight },
@@ -173,6 +194,9 @@ static const luaL_Reg functions[] =
 	{ "getFilter", w_Font_getFilter },
 	{ "setMipmapSharpness", w_Font_setMipmapSharpness },
 	{ "getMipmapSharpness", w_Font_getMipmapSharpness },
+	{ "getAscent", w_Font_getAscent },
+	{ "getDescent", w_Font_getDescent },
+	{ "getBaseline", w_Font_getBaseline },
 	{ 0, 0 }
 };
 

+ 3 - 0
src/modules/graphics/opengl/wrap_Font.h

@@ -42,6 +42,9 @@ int w_Font_setFilter(lua_State *L);
 int w_Font_getFilter(lua_State *L);
 int w_Font_setMipmapSharpness(lua_State *L);
 int w_Font_getMipmapSharpness(lua_State *L);
+int w_Font_getAscent(lua_State *L);
+int w_Font_getDescent(lua_State *L);
+int w_Font_getBaseline(lua_State *L);
 extern "C" int luaopen_font(lua_State *L);
 
 } // opengl