I wonder what's the best way of getting a text bounding box with FreeType 2?
To get the linespace bounding box width, I iterate over all the characters of the text and get it's advance and kearning:
FT_Face face = ...;
text_bbox_width = 0;
while (*p_text)
{
...
FT_Get_Kerning(...);
text_bbox_width += (face->glyph->advance.x + kerning.x) >> 6;
}
How to get linespace bounding box height? Is it necessary to iterate or can it be obtained using font face data? I.e:
text_bbox_height = (face->ascender - face->descender) >> 6
Good news: you do not need to iterate over the characters in each of your strings. You can use face->size->metrics->height
, as described in 3. Global glyph metrics of http://www.freetype.org/freetype2/docs/tutorial/step2.html. Note the warnings on using ascender
and descender
.
Do not mistake this height for the actual pixel bounding box. Individual glyphs may stick out of this box. You can use this line height to get an even spacing over multiple lines in the same text block. To get 'larger' or 'smaller' spacing, you can multiply this value with a constant, such as 1.5 or 2.0 for "double line spacing".
I'm guessing that the value of height
that Freetype calculates is the "normal" or "optimal" line spacing for a certain font.