Read CFF2 vector shapes with GetOutline on a font instance. The zip’s cjk.otf is a Noto Sans CJK JP subset.
dotnet add package RecursiveAI.OneFontRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneFont; var face = FontFace.Load( File.ReadAllBytes("cjk.otf")); var bold = face.WithVariations( FontVariation.Weight(700)); int glyph = bold.GetGlyphIndex('永'); if (glyph == 0) throw new Exception("Glyph not found."); var outline = bold.GetOutline(glyph); var bounds = outline.TightBox(); Console.WriteLine(FormattableString.Invariant( $"Bounds at 700: {bounds}"));
Glyph ID 0 means the character is missing; damaged glyph data can throw FontFormatException when you call GetOutline.
Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.
If you paste the code, put cjk.otf from the zip next to Program.cs, or put your own CFF2 font there and change the filename in the code.
Prints the outline’s ink bounds as (minX, minY, maxX, maxY) in font units:
Bounds at 700: (25.839355, -88.28076, 977.74097, 846.3806)Variations
Read the same glyph at weight 100
Change FontVariation.Weight(700) to the following and change the printed label to Bounds at 100:
var bold = face.WithVariations( FontVariation.Weight(100));
Bounds at 100: (43, -68, 952, 830)Weight 100 is this font’s default and gives whole-number bounds here, while interpolation at 700 gives fractional bounds.
Related recipes
The short answer
Load the font with FontFace.Load, select an instance with WithVariations, and call GetOutline on that instance. Look up the glyph ID with GetGlyphIndex for a single character such as 永. If you’re drawing a shaped run, use the glyph IDs returned by shaping instead: those can include substitutions that the character map alone won’t find.
What is the CFF2 table in an OpenType font?
The OpenType CFF2 specification describes glyph programs that encode contours. Their curved segments are cubic Béziers, with two control points per segment, rather than the quadratic curves found in TrueType glyf outlines. Both are vector data, not pixels. An .otf extension doesn’t tell you whether the file has CFF2; check its table directory if you need to distinguish CFF2 from CFF or glyf.
OneFont uses the same GetOutline API for all three. TightBox() measures the drawn shape, not the horizontal advance or the line height. To consume the actual path, replay the outline with GlyphOutline.Write into an IOutlineSink. Handle CubicTo if your sink consumes CFF2, even if it previously only needed QuadTo for TrueType. The coordinates are in design units with y pointing up; choose a transform for your destination rather than interpreting those coordinates as pixels.
How does a variable font change a CFF2 outline?
The weight chooses a location on a font’s wght axis; it doesn’t stroke an existing path or pick another file. As documented in the OneFont shaping guide, CFF2’s blend and vsindex operate against the font’s variation store. The same character at weights 100 and 700 can have different contour coordinates, as the bounds above show.
Read face.Axes to see the actual axes and their minimum, default and maximum. Values beyond that range are clamped, and axes the font doesn’t have are ignored. Each call to WithVariations starts from the default coordinate on every axis, so pass all the settings together if you need both weight and optical size. Keep the selected face for both shaping and outline reads when you render text; choosing a different instance for the path can leave its geometry out of step with the advances you used for layout.