Two methods on FontFace answer questions about a glyph's ink without drawing it. Both work at the face's current variation instance and for TrueType (simple and composite glyphs), OpenType-CFF and CFF2 outlines. All values are in font units with y pointing up.
Tight bounds
GlyphBounds b = face.GetGlyphBounds(glyphId); // b.MinX, b.MinY, b.MaxX, b.MaxY, b.IsEmptyThe bounds are the exact extents of the curves, not of their control points: a quadratic or cubic segment contributes its end points and the points where its x or y derivative vanishes. IsEmpty is true (and the values are zero) for a blank glyph, a glyph with no outline (including bitmap-only glyphs), a glyph whose data is damaged, and a glyph id out of range. The result equals GetOutline(glyphId).TightBox() to within single-precision rounding, and fontTools' BoundsPen on the committed test fonts.
Band intercepts (text-decoration-skip-ink)
Span<float> ranges = stackalloc float[32]; int need = face.GetGlyphIntercepts(glyphId, bottom, top, ranges); // [x0, x1, x2, x3, ...] float[] all = face.GetGlyphIntercepts(glyphId, bottom, top); // allocating form
The result is the sorted, merged list of x-intervals [x0, x1, x2, x3, ...] where the glyph's filled ink lies inside the horizontal band bottom < y < top.
- Fill rule. Nonzero winding, as in every font format here. A counter (the hole of an "O") is a gap between two intervals; a contour wound the same way as the one around it fills its inside.
- Open band. Ink that only touches the band's edge (a flat bottom sitting exactly on
top) does not count. Intervals of no width are dropped. Touching or overlapping intervals are merged. - Return value and short spans. The span form returns the number of floats the whole answer needs (twice the interval count) even when the span is too short, and writes as many whole pairs as fit, so the caller can retry with a larger span. It returns 0 when
bottomis not belowtop, when either is NaN, when the band holds no ink, for a blank or damaged glyph and for an id out of range. Infinite edges are clamped to ±1e8. - Accuracy. Curves are evaluated exactly, not flattened: each is cut where y (and x) are extreme so the pieces are monotone, and the band edge is found on a monotone piece by a 60-step bisection. The intervals are within 0.01 font unit of the true ink for coordinates up to 10,000; the remainder is rounding of the result to
float. - Method. The projection onto x of the ink inside the band is reached, at every x, by the highest ink point on that vertical line. That point lies either on an outline edge inside the band or on the band's top line. So the answer is the union of the x-extents of the edges clipped to the band, the filled spans of the horizontal line just under
topand the filled spans just overbottom. Edges are the outline's segments plus the straight segment that closes each figure.
Cost and limits
- Both methods read the glyph through the same cache as
GetOutline(up to 8,192 glyphs per face), so the first call for a glyph loads its outline and later calls reuse it. Once the outline is cached,GetGlyphBoundsand the span form ofGetGlyphInterceptsallocate nothing (scratch space comes fromArrayPool<double>.Shared). - Work is O(n log n) in the number of outline segments (an outline has at most 262,144), with no recursion; sorting is a heap sort, so results are identical on every platform.
- A glyph whose data is damaged or over the outline limits gives an empty result from these two methods, where
GetOutlineraisesFontFormatException.