Ink bounds are the tight box around a glyph’s drawn outline, in the font’s design units rather than pixels. The example zip includes NotoSans-Regular.ttf.
dotnet add package RecursiveAI.OneFontRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneFont; var font = FontFace.Load(File.ReadAllBytes( "NotoSans-Regular.ttf")); int glyph = font.GetGlyphIndex('g'); if (glyph == 0) throw new Exception("No g."); var outline = font.GetOutline(glyph); Console.WriteLine(outline.TightBox());
Unzip the example, then dotnet run.
Prints (minX, minY, maxX, maxY) in font units, with y increasing upward.
(55, -240, 530, 546)The full program
using System; using System.IO; using System.Globalization; using OneFont; CultureInfo.CurrentCulture = CultureInfo.InvariantCulture; var bytes = File.ReadAllBytes( "NotoSans-Regular.ttf"); var font = FontFace.Load(bytes); int glyph = font.GetGlyphIndex('g'); if (glyph == 0) throw new Exception("Missing g glyph."); var outline = font.GetOutline(glyph); Console.WriteLine(outline.TightBox());
(55, -240, 530, 546)If the font doesn’t map g, GetGlyphIndex returns 0; the snippet stops rather than measuring the missing-glyph symbol. The bundled Noto Sans is distributed under the SIL Open Font License included in the zip.
Variations
Check whether a space has ink
Append this to check the space in the same face. It has no outline, but the pen still advances by 260 font units.
int space = font.GetGlyphIndex(' '); if (space == 0) throw new Exception("Missing space glyph."); Console.WriteLine(font.GetOutline(space).IsEmpty); Console.WriteLine(font.GetAdvance(space));
True 260
Related recipes
Check whether a font supports a character
The short answer
Map a character to a glyph ID with GetGlyphIndex, load that glyph’s outline with GetOutline, and call TightBox(). The result gives the extrema of the unhinted outline in font design units relative to the glyph origin, not the dimensions of a rendered bitmap. The bundled g extends from −240 to 546 vertically: it descends below the baseline at y = 0.
What’s the difference between a glyph bounding box and a control box?
A Bézier curve uses control points to set its direction and shape. The curve doesn’t necessarily pass through those points, so a box around all the points can extend past the outline. A tight box considers the points where the curve itself reaches its leftmost, rightmost, lowest, and highest positions. The OneFont API at v1.4.0 describes this as “The exact extents of the curves (not of their control points), in font units.” The distinction applies to both quadratic TrueType curves and cubic OpenType CFF curves.
Is a glyph’s bounding box the same as its advance width?
No. For this g, the ink width is 530 − 55 = 475 font units. The advance is how far the pen moves to position the next glyph, not the width of the outline. It can include blank space beside the ink, and ink can extend beyond the advance. The space example makes the distinction visible in numbers: there’s an advance even though no outline is present. Check IsEmpty before treating the result as a rectangle to place; TightBox() returns four zeros for an empty outline.
For a single character, the character map is sufficient to look up its default glyph. For text after shaping, use the glyph IDs and positions that shaping produced: the default glyph alone won’t describe a ligature or a mark positioned relative to another glyph.
How do you convert a glyph bounding box to pixels?
Divide the size in pixels by the face’s UnitsPerEm. Multiply the coordinates by that scale. For a screen where y increases downward, subtract the scaled y values from the baseline: the top comes from maxY and the bottom comes from minY. Add the scaled x values to the pen’s x coordinate. This gives the scaled geometric bounds, without hinting or pixel rounding. See the coordinate convention in OneFont’s outline API.