Compare the spacing of AV using sans.ttf from the zip.
dotnet add package RecursiveAI.OneFontRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using OneFont; var noKern = new ShapingOptions { Kerning = KerningMode.None }; var face = FontFace.Load( File.ReadAllBytes("sans.ttf")); var normal = TextShaping.ShapeRun("AV", face); var plain = TextShaping.ShapeRun( "AV", face, options: noKern); Console.WriteLine("Normal: " + normal.Sum(c => c.Advance)); Console.WriteLine("None: " + plain.Sum(c => c.Advance));
Unzip the example, then dotnet run.
Prints the width of AV in font design units: Normal applies kerning and None disables it.
Normal: 1199 None: 1239
If sans.ttf isn’t in the working directory, File.ReadAllBytes throws FileNotFoundException. The zip includes a Noto Sans subset under the SIL Open Font License (OFL.txt).
Variations
Convert the unkerned width to pixels
Append this to measure at a 20 px font size. It reuses face and noKern from the snippet at the top.
int advance = TextShaping.ShapeRun( "AV", face, options: noKern) .Sum(c => c.Advance); float pixels = advance * (20f / face.UnitsPerEm); Console.WriteLine(FormattableString.Invariant( $"{pixels:0.00} px"));
24.78 pxTurn off the OpenType kern feature directly
Append this if you keep your switches together in a feature list.
var features = new ShapingOptions(); features.Features.Add(FontFeature.Off("kern")); var noPairAdjustment = TextShaping.ShapeRun( "AV", face, options: features); Console.WriteLine( noPairAdjustment.Sum(c => c.Advance));
1239Related recipes
The short answer
Set ShapingOptions.Kerning to KerningMode.None, pass those options to TextShaping.ShapeRun, and sum Advance across the returned clusters. To compare against normal kerning, measure the same text with the same face, direction, language, and feature settings. Only the kerning mode should change.
What is kerning in typography?
OpenType kerning is a font-supplied spacing adjustment. A pair such as AV can fit more closely than its standalone advances suggest: the upper strokes and lower strokes leave space for the neighboring letter. In the bundled font the pair takes 1199 design units with kerning and 1239 without it, a difference of 40 units. That is a measurement of this pair in this face, not a fixed amount to add to all text. A different face or pair may produce the same width with either setting.
Is kerning the same as letter spacing?
No. Kerning uses the font’s positioning for particular glyph combinations. Letter spacing adds or removes spacing between adjacent typographic character units; disabling kerning doesn’t add a uniform gap. Nor does it switch off ligatures or mark placement. Shape the text with the options you intend to use for drawing, rather than summing widths from isolated characters, which would lose context for other shaping features too.
How do you measure text width in pixels in C#?
The sum of cluster advances tells you how far the text position moves when laying out a run. OneFont returns that sum in the font’s design units, not pixels. Multiply by the requested pixel size divided by face.UnitsPerEm, as in the 20 px variation. Keep the same scale for both measurements and round only when needed. Advance width isn’t an ink bounding box: a glyph can overhang its advance or leave blank space inside it. Use outline bounds if you need the extent of the visible marks.
Why might setting KerningMode.None not change the width?
The chosen pair may not have a kerning adjustment. Also check the feature list. The shaping guide says “an explicit kern setting wins over it.” If the options still include FontFeature.On("kern"), setting KerningMode.None alone won’t take precedence. Remove the conflicting setting or use the explicit Off("kern") in the variation above.