BLF: Public Function to Return Maximum Glyph Bounds

This BLF adds a public function that returns the maximum extext of a
font's glyphs. BLF_bounds_max returns a rctf of the minimum rect that
can contain any of the font's glyphs. Can be useful to know when sizing
a bitmap without knowing the final string contents.

Pull Request: https://projects.blender.org/blender/blender/pulls/145016
This commit is contained in:
Harley Acheson
2025-08-26 21:31:29 +02:00
committed by Harley Acheson
parent ebfad2c071
commit 2dd420d40d
4 changed files with 36 additions and 0 deletions

View File

@@ -30,6 +30,7 @@
struct ListBase;
struct ResultBLF;
struct rcti;
struct rctf;
namespace blender::ocio {
class ColorSpace;
@@ -293,6 +294,17 @@ int BLF_width_max(int fontid) ATTR_WARN_UNUSED_RESULT;
int BLF_descender(int fontid) ATTR_WARN_UNUSED_RESULT;
int BLF_ascender(int fontid) ATTR_WARN_UNUSED_RESULT;
/**
* Returns the minimum bounding box that can enclose all glyphs in the font at
* the current size. Expect negative values as Y=0 is the baseline, X=0 is normal
* advance position (glyphs can have negative bearing and positioning). There
* should be little use for this as it is best to measure the bounds of the actual
* text to be drawn. These values (unscaled) are set in the font file, not calculated
* from the actual glyphs at load time. This should be considered correct but it is
* possible, although very unlikely, for a defective font to contain incorrect values.
*/
bool BLF_bounds_max(int fontid, rctf *r_bounds) ATTR_NONNULL(2);
/**
* The following function return the width and height of the string, but
* just in one call, so avoid extra freetype2 stuff.

View File

@@ -891,6 +891,15 @@ int BLF_ascender(int fontid)
return 0.0f;
}
bool BLF_bounds_max(int fontid, rctf *r_bounds)
{
FontBLF *font = blf_get(fontid);
if (font) {
return blf_font_bounds_max(font, r_bounds);
}
return false;
}
void BLF_rotation(int fontid, float angle)
{
FontBLF *font = blf_get(fontid);

View File

@@ -1588,6 +1588,19 @@ int blf_font_ascender(FontBLF *font)
return ft_pix_to_int((ft_pix)font->ft_size->metrics.ascender);
}
bool blf_font_bounds_max(FontBLF *font, rctf *r_bounds)
{
if (!blf_ensure_face(font)) {
return false;
}
r_bounds->xmin = float(font->face->bbox.xMin) / float(font->face->units_per_EM) * font->size;
r_bounds->xmax = float(font->face->bbox.xMax) / float(font->face->units_per_EM) * font->size;
r_bounds->ymin = float(font->face->bbox.yMin) / float(font->face->units_per_EM) * font->size;
r_bounds->ymax = float(font->face->bbox.yMax) / float(font->face->units_per_EM) * font->size;
return true;
}
char *blf_display_name(FontBLF *font)
{
if (!blf_ensure_face(font) || !font->face->family_name) {

View File

@@ -20,6 +20,7 @@ struct GlyphCacheBLF;
struct ListBase;
struct ResultBLF;
struct rcti;
struct rctf;
enum class BLFWrapMode;
/**
@@ -153,6 +154,7 @@ int blf_font_height_max(FontBLF *font);
int blf_font_width_max(FontBLF *font);
int blf_font_descender(FontBLF *font);
int blf_font_ascender(FontBLF *font);
bool blf_font_bounds_max(FontBLF *font, rctf *r_bounds);
char *blf_display_name(FontBLF *font);