Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions include/SDL3_ttf/SDL_ttf.h
Original file line number Diff line number Diff line change
Expand Up @@ -844,6 +844,33 @@ extern SDL_DECLSPEC void SDLCALL TTF_SetFontKerning(TTF_Font *font, bool enabled
*/
extern SDL_DECLSPEC bool SDLCALL TTF_GetFontKerning(const TTF_Font *font);

/**
* Query the font's 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 maxy offset may be seen as the
* "maximum ascender" and miny offset - as the "minimum descender".
*
* \param font the font to query.
* \param minx a pointer filled in with the minimum x coordinate of *any* glyph
* in this font from it's origin. This value may be negative.
* \param maxx a pointer filled in with the maximum x coordinate of *any* glyph
* in this font from it's origin.
* \param miny a pointer filled in with the minimum y coordinate of *any* glyph
* in this font from it's origin. This value may be negative.
* \param maxy a pointer filled in with the maximum y coordinate of *any* glyph
* in this font from it's origin.
* \returns true on success or false on failure; call SDL_GetError() for more
* information.
*
* \threadsafety This function should be called on the thread that created the
* font.
*
* \since This function is available since SDL_ttf 3.4.0.
*/
extern SDL_DECLSPEC bool SDLCALL TTF_GetFontBBox(const TTF_Font *font, int *minx, int *maxx, int *miny, int *maxy);

/**
* Query whether a font is fixed-width.
*
Expand Down
28 changes: 28 additions & 0 deletions src/SDL_ttf.c
Original file line number Diff line number Diff line change
Expand Up @@ -5915,6 +5915,34 @@ bool TTF_GetFontKerning(const TTF_Font *font)
return font->enable_kerning;
}

bool TTF_GetFontBBox(const TTF_Font *font, int *minx, int *maxx, int *miny, int *maxy)
{
TTF_CHECK_FONT(font, false);

/* Bitmap fonts do not contain bbox information */
if (!FT_IS_SCALABLE(font->face)) {
return false;
}

const FT_Face face = font->face;
/* Recalculate FT_Face's bbox from font units to pixels */
FT_Fixed xscale = face->size->metrics.x_scale;
FT_Fixed yscale = face->size->metrics.y_scale;
if (minx) {
*minx = FT_CEIL(FT_MulFix(face->bbox.xMin, xscale));
}
if (maxx) {
*maxx = FT_CEIL(FT_MulFix(face->bbox.xMax, xscale));
}
if (miny) {
*miny = FT_CEIL(FT_MulFix(face->bbox.yMin, yscale));
}
if (maxy) {
*maxy = FT_CEIL(FT_MulFix(face->bbox.yMax, yscale));
}
return true;
}

int TTF_GetNumFontFaces(const TTF_Font *font)
{
TTF_CHECK_FONT(font, 0);
Expand Down
1 change: 1 addition & 0 deletions src/SDL_ttf.sym
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,6 @@ SDL3_ttf_0.0.0 {
TTF_WasInit;
TTF_SetFontCharSpacing;
TTF_GetFontCharSpacing;
TTF_GetFontBBox;
local: *;
};
Loading