'How to get height of font in Freetype2

In Freetype2, We are using the below formula to calculate the font height for the given size. height = (int)((point_size / face->units_per_EM) * (face->ascender - face->descender)); where as point_size is user supplied data.

But for some fonts it gives wrong data. Could anyone help to find the correct formula to find the correct height of font face for given point? (Not for individual glyph).



Solution 1:[1]

Below is the method used in cocos2d-x:

_lineHeight = static_cast((face->size->metrics.ascender - face->size->metrics.descender) >> 6);

Most fonts are ok. For the not-ok case, I calibrate the value after get GlyphBitmap's height, and then use the larger one as the max line height.

Solution 2:[2]

using the larger one as the max line height is a the best way to solve this problem. because if you write different string separately you may get a bad alignement of the line.

so the best solution that i found is the following:

int bbox_ymax = FT_MulFix(face->bbox.yMax, face->size->metrics.y_scale) >> 6;
int bbox_ymin = FT_MulFix(face->bbox.yMin, face->size->metrics.y_scale) >> 6;
int height = bbox_ymax - bbox_ymin;

and for some font you will need to use face->glyph->metrics.vertAdvance instead of bbox_ymax if vertAdvance < bbox_ymax

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 wan
Solution 2 zanninso