diff --git a/modules/juce_graphics/fonts/juce_Typeface.cpp b/modules/juce_graphics/fonts/juce_Typeface.cpp index 8531d710b4..be4ab55c51 100644 --- a/modules/juce_graphics/fonts/juce_Typeface.cpp +++ b/modules/juce_graphics/fonts/juce_Typeface.cpp @@ -413,6 +413,23 @@ void Typeface::getOutlineForGlyph (TypefaceMetricsKind kind, int glyphNumber, Pa path.applyTransform (AffineTransform::scale (scale, -scale)); } +Rectangle Typeface::getGlyphBounds (TypefaceMetricsKind kind, int glyphNumber) const +{ + auto* font = getNativeDetails().getFont(); + + hb_glyph_extents_t extents{}; + if (! hb_font_get_glyph_extents (font, (hb_codepoint_t) glyphNumber, &extents)) + return {}; + + const auto native = getNativeDetails(); + const auto metrics = native.getMetrics (kind); + const auto scale = metrics.getHeightToPointsFactor() / (float) hb_face_get_upem (hb_font_get_face (font)); + + return Rectangle { (float) extents.width, (float) extents.height } + .withPosition ((float) extents.x_bearing, (float) extents.y_bearing) + .transformedBy (AffineTransform::scale (scale).scaled (1.0f, -1.0f)); +} + void Typeface::applyVerticalHintingTransform (float, Path&) { jassertfalse; diff --git a/modules/juce_graphics/fonts/juce_Typeface.h b/modules/juce_graphics/fonts/juce_Typeface.h index 709088b89c..f7fae451bc 100644 --- a/modules/juce_graphics/fonts/juce_Typeface.h +++ b/modules/juce_graphics/fonts/juce_Typeface.h @@ -214,6 +214,9 @@ public: */ void getOutlineForGlyph (TypefaceMetricsKind, int glyphNumber, Path& path) const; + /** Returns glyph bounds, normalised to a font height of 1.0. */ + Rectangle getGlyphBounds (TypefaceMetricsKind, int glyphNumber) const; + /** @deprecated Returns a new EdgeTable that contains the path for the given glyph, with the specified transform applied.