-
Notifications
You must be signed in to change notification settings - Fork 185
Add TTF_GetFontBBox() which returns font's extremum bounding box #538
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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".
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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. | ||
| * | ||
|
|
||
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a
fontpointer check. By the way, I noticed that not all functions have this check, only some do.