Skip to content
Closed
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
21 changes: 21 additions & 0 deletions SDL_ttf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should make sure the font is valid, see the other API functions for examples.

Copy link
Contributor Author

@ivan-mogilko ivan-mogilko Mar 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a font pointer check. By the way, I noticed that not all functions have this check, only some do.

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;
Expand Down
16 changes: 16 additions & 0 deletions SDL_ttf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the opposite of how coordinates are usually done in SDL. Can you double check the other SDL_ttf APIs and see if that's consistent here?

Copy link
Contributor Author

@ivan-mogilko ivan-mogilko Mar 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By "opposite", are you referring to Y axis? I know that SDL has it downwards in e.g. drawing, the upwards Y is FT specific, it has ascender as a positive offset and descender as a negative offset afaik.
But I will check what other functions are doing.

Copy link
Contributor Author

@ivan-mogilko ivan-mogilko Mar 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can confirm that TTF_FontAscent and TTF_FontDescent functions return these offsets in a similar style: ascent is normally a positive value and descent is a negative value, as if Y axis is pointing up. For properly done fonts their return values also match miny and maxy values returned from TTF_GetFontBBox. (Their values differ for the fonts which have incorrect metrics information in them.)

EDIT: Also checked TTF_GlyphMetrics, and its return values seem fully consistent as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, since there's a TTF_GlyphMetrics function, do you think that TTF_GetFontBBox is a okay name, or should I rename to something more consistent with "GlyphMetrics"? Because the meaning of BBox is essentially "max known glyph metrics".

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, thanks for checking, that seems fine to me. I'm not sure on the name, let me think about it a bit. What's the use case for this function?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the use case for this function?

In my case (mentioned in #536), there are fonts that have incorrect ascent/descent information for some reason (maybe because font's creator made a mistake), but there's still a way to tell how big their characters are using this bounding box info. This helps if we want to precalculate a necessary texture size before first render happened, for example.

* "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.
*
Expand Down
Loading