diff --git a/SDL_ttf.c b/SDL_ttf.c index d5ca963b..e29a528e 100644 --- a/SDL_ttf.c +++ b/SDL_ttf.c @@ -3040,6 +3040,27 @@ void TTF_SetFontKerning(TTF_Font *font, int allowed) #endif } +void TTF_GetFontBBox(const TTF_Font *font, int *minx, int *maxx, int *miny, int *maxy) +{ + if (!font || !font->face) { + return; + } + /* Recalculate FT_Face's bbox from font units to pixels */ + const FT_Face face = font->face; + if (minx) { + *minx = FT_MulFix(FT_DivFix(face->bbox.xMin, face->units_per_EM), face->size->metrics.x_ppem); + } + if (maxx) { + *maxx = FT_MulFix(FT_DivFix(face->bbox.xMax, face->units_per_EM), face->size->metrics.x_ppem); + } + if (miny) { + *miny = FT_MulFix(FT_DivFix(face->bbox.yMin, face->units_per_EM), face->size->metrics.y_ppem); + } + if (maxy) { + *maxy = FT_MulFix(FT_DivFix(face->bbox.yMax, face->units_per_EM), face->size->metrics.y_ppem); + } +} + long TTF_FontFaces(const TTF_Font *font) { return font->face->num_faces; diff --git a/SDL_ttf.h b/SDL_ttf.h index 03c3cf83..282716f0 100644 --- a/SDL_ttf.h +++ b/SDL_ttf.h @@ -678,6 +678,22 @@ extern DECLSPEC int SDLCALL TTF_GetFontKerning(const TTF_Font *font); */ extern DECLSPEC void SDLCALL TTF_SetFontKerning(TTF_Font *font, int allowed); +/** + * Query the font bounding box. + * + * The bounding box defines bounds large enough to contain any glyph from + * the font. It is expressed in pixel offsets from glyph's origin (0,0), + * with Y axis pointing upwards. Thus ymax offset may be seen as the + * "maximum ascender" and ymin offset - as the "minimum descender". + * + * \param font the font to query. + * + * \since This function is available since SDL_ttf 2.26.0. + */ +extern DECLSPEC void SDLCALL TTF_GetFontBBox(const TTF_Font *font, + int *minx, int *maxx, + int *miny, int *maxy); + /** * Query the number of faces of a font. *