Skip to content

Conversation

ivan-mogilko
Copy link
Contributor

@ivan-mogilko ivan-mogilko commented Aug 25, 2025

Resolves #536
(This is a remake of #538 ported to SDL3-backed branch)

This adds a new function called TTF_GetFontBBox:

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

This function returns font's bounding box, which defines the maximal bounds enough to contain any glyph from the font.
My understanding is this information is only available when reading the scalable font formats, so for non-scalable fonts this function always returns "false". (If that's wrong, then the function may be trivially updated to cover bitmap fonts as well.)

TTF_GetFontBBox complements TTF_GetGlyphMetrics, and is useful in a case when you need to know max possible bounds instead of particular glyph's bounds. The "bbox" information is available in the format header, thus it can be obtained without measuring every single glyph.

Since I was previously asked to explain use cases for this function, here are couple of real examples from my practice:

  1. You want to get a rectangle suitable for any glyph from this font, for example, when displaying a font's preview as a table (grid) with all glyphs arranged evenly.
  2. Some rare fonts have wrong info about their ascent and descent, which results in them reporting inaccurate or just nonsensical height value. Getting bbox may help in such case, if we compare their Y extent with height and choose whatever is bigger. This helps if we need to precreate a drawing surface where we print this text, or arrange objects on screen if their positions are based on the actual text size. I know that you can get text measurements when you have a text, but sometimes we want to do these calculations before any text is displayed (or when it is not known what text will be displayed).

@sezero sezero requested review from 1bsyl and slouken August 25, 2025 09:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature request]: query font's BBOX

1 participant