---
title: "Read a glyph pair’s kerning adjustment in C#"
url: https://recursiveai.org/onefont/cookbook/read-font-kerning-pair-adjustment/
description: "Use OneFont to read a signed kerning adjustment for two glyphs from a font file, without shaping a text run. The example measures the pair AV."
version: "1.4.0"
---

# 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.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneFont) · [GitHub](https://github.com/recursive-ai-oss/onefont-csharp) · [Docs](https://recursiveai.org/onefont/docs/) · [Download example (.zip)](https://recursiveai.org/onefont/cookbook/read-font-kerning-pair-adjustment/read-font-kerning-pair-adjustment.zip)

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

```shell
dotnet add package RecursiveAI.OneFont
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onefont/cookbook/read-font-kerning-pair-adjustment/read-font-kerning-pair-adjustment.zip), 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

```csharp
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.

```csharp
double pixels = face.GetKerning(left, right)
    * (20.0 / face.UnitsPerEm);
Console.WriteLine(FormattableString.Invariant($"{pixels:G} px"));
```

```text
-2.4 px
```

## Related recipes

- [Measure text without font kerning](https://recursiveai.org/onefont/cookbook/disable-font-kerning/)
- [Read a glyph’s left side bearing](https://recursiveai.org/onefont/cookbook/read-glyph-left-side-bearing/)
- [Shape text](https://recursiveai.org/onefont/cookbook/shape-text/)

## 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](https://learn.microsoft.com/en-us/typography/opentype/spec/features_ko#tag-kern) 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`](https://github.com/recursive-ai-oss/onefont-csharp/blob/7db48bc21a7eec46272aa73f4e76acf07645db03/src/OneFont/FontFace.cs#L107-L111) looks at OpenType glyph positioning (GPOS) for Latin when the plan has GPOS kerning, and [otherwise looks for legacy `kern` data](https://github.com/recursive-ai-oss/onefont-csharp/blob/7db48bc21a7eec46272aa73f4e76acf07645db03/src/OneFont/Shaping/TextShaper.cs#L305-L323). 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.
