Skip to content

Commit 17dfc0c

Browse files
committed
Add TTF_GetFontBBox() which returns font's extremum bounding box
1 parent 2eb8938 commit 17dfc0c

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

SDL_ttf.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3035,6 +3035,27 @@ void TTF_SetFontKerning(TTF_Font *font, int allowed)
30353035
#endif
30363036
}
30373037

3038+
void TTF_GetFontBBox(const TTF_Font *font, int *minx, int *maxx, int *miny, int *maxy)
3039+
{
3040+
if (!font || !font->face) {
3041+
return;
3042+
}
3043+
/* Recalculate FT_Face's bbox from font units to pixels */
3044+
const FT_Face face = font->face;
3045+
if (minx) {
3046+
*minx = FT_MulFix(FT_DivFix(face->bbox.xMin, face->units_per_EM), face->size->metrics.x_ppem);
3047+
}
3048+
if (maxx) {
3049+
*maxx = FT_MulFix(FT_DivFix(face->bbox.xMax, face->units_per_EM), face->size->metrics.x_ppem);
3050+
}
3051+
if (miny) {
3052+
*miny = FT_MulFix(FT_DivFix(face->bbox.yMin, face->units_per_EM), face->size->metrics.y_ppem);
3053+
}
3054+
if (maxy) {
3055+
*maxy = FT_MulFix(FT_DivFix(face->bbox.yMax, face->units_per_EM), face->size->metrics.y_ppem);
3056+
}
3057+
}
3058+
30383059
long TTF_FontFaces(const TTF_Font *font)
30393060
{
30403061
return font->face->num_faces;

SDL_ttf.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,22 @@ extern DECLSPEC int SDLCALL TTF_GetFontKerning(const TTF_Font *font);
678678
*/
679679
extern DECLSPEC void SDLCALL TTF_SetFontKerning(TTF_Font *font, int allowed);
680680

681+
/**
682+
* Query the font bounding box.
683+
*
684+
* The bounding box defines bounds large enough to contain any glyph from
685+
* the font. It is expressed in pixel offsets from glyph's origin (0,0),
686+
* with Y axis pointing upwards. Thus ymax offset may be seen as the
687+
* "maximum ascender" and ymin offset - as the "minimum descender".
688+
*
689+
* \param font the font to query.
690+
*
691+
* \since This function is available since SDL_ttf 2.26.0.
692+
*/
693+
extern DECLSPEC void SDLCALL TTF_GetFontBBox(const TTF_Font *font,
694+
int *minx, int *maxx,
695+
int *miny, int *maxy);
696+
681697
/**
682698
* Query the number of faces of a font.
683699
*

0 commit comments

Comments
 (0)