diff --git a/source/blender/blenfont/BLF_api.hh b/source/blender/blenfont/BLF_api.hh index 9b03eb3a3e2..9aac88e9a49 100644 --- a/source/blender/blenfont/BLF_api.hh +++ b/source/blender/blenfont/BLF_api.hh @@ -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. diff --git a/source/blender/blenfont/intern/blf.cc b/source/blender/blenfont/intern/blf.cc index e93d1f0cbec..997bdab3504 100644 --- a/source/blender/blenfont/intern/blf.cc +++ b/source/blender/blenfont/intern/blf.cc @@ -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); diff --git a/source/blender/blenfont/intern/blf_font.cc b/source/blender/blenfont/intern/blf_font.cc index 4f580956dfd..195b61fb986 100644 --- a/source/blender/blenfont/intern/blf_font.cc +++ b/source/blender/blenfont/intern/blf_font.cc @@ -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) { diff --git a/source/blender/blenfont/intern/blf_internal.hh b/source/blender/blenfont/intern/blf_internal.hh index 6ec8213fee8..0717f421c8b 100644 --- a/source/blender/blenfont/intern/blf_internal.hh +++ b/source/blender/blenfont/intern/blf_internal.hh @@ -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);