Recursive AI

Read a glyph pair’s kerning adjustment in C#

onefont is a free, open-source font and text-shaping library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Use GetKerning to read the spacing change for AV in kern.ttf from the zip.

Terminal
dotnet add package RecursiveAI.OneFont

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneFont;
 
var face = FontFace.Load(File.ReadAllBytes(
    "kern.ttf"));
int left = face.GetGlyphIndex('A');
int right = face.GetGlyphIndex('V');
if (left == 0 || right == 0)
    throw new Exception("Missing glyph.");
Console.WriteLine(face.GetKerning(left, right));

Unzip the example, then dotnet run.

Prints the change to the first glyph’s advance in font units. A negative value brings the second glyph closer. To use your own font, replace kern.ttf from the zip with the path to your .ttf file; the adjustment may differ from -120.

Text
-120

The full program

C#
using OneFont;
 
var face = FontFace.Load(File.ReadAllBytes(
    "kern.ttf"));
int left = face.GetGlyphIndex('A');
int right = face.GetGlyphIndex('V');
if (left == 0 || right == 0)
    throw new Exception("Missing glyph.");
Console.WriteLine(face.GetKerning(left, right));
int advance = face.GetAdvance(left);
int adjusted = advance + face.GetKerning(left, right);
Console.WriteLine($"A advance: {advance}");
Console.WriteLine($"A advance before V: {adjusted}");
Text
-120
A advance: 690
A advance before V: 570

If either character has no glyph, GetGlyphIndex returns 0; the program stops rather than looking up a pair containing the missing glyph. The zip includes a Noto Sans subset with a synthetic kern table and its SIL Open Font License (OFL.txt).

Variations

Scale the adjustment to pixels

Append this for a font size of 20 pixels. The sign stays negative: this is a reduction in spacing, not the new spacing.

C#
double pixels = face.GetKerning(left, right)
    * (20.0 / face.UnitsPerEm);
Console.WriteLine(FormattableString.Invariant($"{pixels:G} px"));
Text
-2.4 px

The short answer

Get each glyph ID from the same FontFace, then call face.GetKerning(left, right). It returns the signed change to the first glyph’s horizontal advance in font units. The order matters: pass A then V for AV. This is a two-glyph metric, not the width of a shaped string.

What are kerning pairs in fonts?

A kerning pair is two adjacent glyphs whose spacing is adjusted together. With a pair such as AV, space left by the angled strokes lets the glyphs sit closer together without touching. The OpenType kern feature describes these font-supplied adjustments. Unlike tracking, which changes spacing across a range of text, a pair adjustment depends on the two glyphs and their order.

In kern.ttf, the sample A advances by 690 units on its own. Before V, the adjustment of −120 changes that advance to 570, which means the origin of V moves 120 units closer to the origin of A. It doesn’t make A narrower or change the outline of either glyph. The example’s −120 belongs to this particular font file, which carries a synthetic legacy kerning table; don’t hard-code that value for other faces.

How do you determine kerning for a glyph pair?

Use the face that will draw both glyphs and read the pair in the order you want to measure. OneFont’s GetKerning looks at OpenType glyph positioning (GPOS) for Latin when the plan has GPOS kerning, and otherwise looks for legacy kern data. It does not retry the legacy table just because the GPOS adjustment is zero. A zero result means the lookup did not change the first glyph’s advance; it does not say that the font lacks kerning altogether.

For an isolated spacing calculation, add the signed result to the first glyph’s advance, as the program does. If your layout is in pixels, use the same pixels-per-font-unit scale for the adjustment as for the advance. For actual text in a chosen language, use the advances from shaping instead. Substitutions can change which glyphs are adjacent, and run positioning can do more than adjust the first advance. If the run already includes kerning, adding this adjustment again counts it twice.

All onefont recipes