Skip to content

Commit dcefe5e

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

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

SDL_ttf.c

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

3038+
void TTF_GetFontBBox(const TTF_Font *font, TTF_BBox *bbox)
3039+
{
3040+
/* Recalculate FT_Face's bbox from font units to pixels */
3041+
const FT_Face face = font->face;
3042+
bbox->xmax = FT_MulFix(FT_DivFix(face->bbox.xMax, face->units_per_EM), face->size->metrics.x_ppem);
3043+
bbox->xmin = FT_MulFix(FT_DivFix(face->bbox.xMin, face->units_per_EM), face->size->metrics.x_ppem);
3044+
bbox->ymax = FT_MulFix(FT_DivFix(face->bbox.yMax, face->units_per_EM), face->size->metrics.y_ppem);
3045+
bbox->ymin = FT_MulFix(FT_DivFix(face->bbox.yMin, face->units_per_EM), face->size->metrics.y_ppem);
3046+
}
3047+
30383048
long TTF_FontFaces(const TTF_Font *font)
30393049
{
30403050
return font->face->num_faces;

SDL_ttf.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,15 @@ extern DECLSPEC void SDLCALL TTF_ByteSwappedUNICODE(SDL_bool swapped);
164164
*/
165165
typedef struct TTF_Font TTF_Font;
166166

167+
/**
168+
* A bounding box, represented in pixel offsets.
169+
*/
170+
typedef struct TTF_BBox
171+
{
172+
int xmin, xmax;
173+
int ymin, ymax;
174+
} TTF_BBox;
175+
167176
/**
168177
* Initialize SDL_ttf.
169178
*
@@ -678,6 +687,21 @@ extern DECLSPEC int SDLCALL TTF_GetFontKerning(const TTF_Font *font);
678687
*/
679688
extern DECLSPEC void SDLCALL TTF_SetFontKerning(TTF_Font *font, int allowed);
680689

690+
/**
691+
* Query the font bounding box.
692+
*
693+
* The bounding box defines bounds large enough to contain any glyph from
694+
* the font. It is expressed in pixel offsets from glyph's origin (0,0),
695+
* with Y axis pointing upwards. Thus ymax offset may be seen as the
696+
* "maximal ascender" and ymin offset - as the "maximal descender".
697+
*
698+
* \param font the font to query.
699+
* \param bbox the TTF_BBox struct to fill.
700+
*
701+
* \since This function is available since SDL_ttf 2.26.0.
702+
*/
703+
extern DECLSPEC void SDLCALL TTF_GetFontBBox(const TTF_Font *font, TTF_BBox *bbox);
704+
681705
/**
682706
* Query the number of faces of a font.
683707
*

0 commit comments

Comments
 (0)