Actually Laurent, NeomerArcana has a point. Depending on how the font was designed there can be a bit too much extra space at the "top" of a glyph. SFML requests from freetype that it set its pixel size using FT_Set_Pixel_Sizes. In sf::Text, it is assumed that the character size of the font is exactly the same as its ascent. In cases where this is not the case, such as this font, it will leave some room between the ascent (nominally the height of the highest glyph) and the "true" top of the virtual space that the font uses in its definition.
I don't know how strict these font designing guidelines are, but I can imagine that many such fonts exist that don't truly fill up all the vertical room given to them, instead relying on the software to position based on ascent, descent and line gap which is what SFML already does for line spacing by using freetype's height (ascent - descent + line gap) field.
This picture shows the uncorrected and corrected positions:
The patch is simply to subtract the missing space that SFML currently assumes to be 0 from the glyph's own vertical offset:
src/SFML/Graphics/Font.cpp | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/src/SFML/Graphics/Font.cpp b/src/SFML/Graphics/Font.cpp
index 954c8b5..0cf7b3d 100644
--- a/src/SFML/Graphics/Font.cpp
+++ b/src/SFML/Graphics/Font.cpp
@@ -445,6 +445,11 @@ Glyph Font::loadGlyph(Uint32 codePoint, unsigned int characterSize, bool bold) c
int width = bitmap.width;
int height = bitmap.rows;
+ int ascender = face->size->metrics.ascender >> 6;
+
+ // Offset to make up for empty space between ascender and virtual top of the typeface
+ int ascenderOffset = characterSize - ascender;
+
if ((width > 0) && (height > 0))
{
// Leave a small padding around characters, so that filtering doesn't
@@ -459,7 +464,7 @@ Glyph Font::loadGlyph(Uint32 codePoint, unsigned int characterSize, bool bold) c
// Compute the glyph's bounding box
glyph.bounds.left = bitmapGlyph->left - padding;
- glyph.bounds.top = -bitmapGlyph->top - padding;
+ glyph.bounds.top = -bitmapGlyph->top - padding - ascenderOffset;
glyph.bounds.width = width + 2 * padding;
glyph.bounds.height = height + 2 * padding;
Of course, with this patch, fonts that have non-standard ascender definitions that don't cover the whole height of the glyphs will be incorrectly displayed, but I can imagine that that will be the case with any other software that uses the font as well.