---
title: "Get a font’s x-height and cap height in C#"
url: https://recursiveai.org/onefont/cookbook/read-font-x-height-and-cap-height/
description: "Read a font’s declared x-height and cap height in C#, convert them to pixels with OneFont, and distinguish missing metadata from a measured outline."
version: "1.4.0"
---

# Get a font’s x-height and cap height 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-x-height-and-cap-height/read-font-x-height-and-cap-height.zip)

The zip includes `NotoSans-Regular.ttf`.

```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 data = File.ReadAllBytes(
    "NotoSans-Regular.ttf");
var metrics = FontFace.Load(data).Metrics;
double scale = 20.0 / metrics.UnitsPerEm;
Console.WriteLine(FormattableString.Invariant(
    $"{metrics.XHeight * scale:0.##}"));
Console.WriteLine(FormattableString.Invariant(
    $"{metrics.CapHeight * scale:0.##}"));
```

Unzip [the example](https://recursiveai.org/onefont/cookbook/read-font-x-height-and-cap-height/read-font-x-height-and-cap-height.zip), then `dotnet run`.

Prints x-height and cap height in pixels at a 20 px font size, in that order.

```text
10.72
14.28
```

`0` means the font doesn’t declare that height.

## The full program

```csharp
using OneFont;
var data = File.ReadAllBytes(
    "NotoSans-Regular.ttf");
var metrics = FontFace.Load(data).Metrics;

const double size = 20;
double scale = size / metrics.UnitsPerEm;
Report("x-height", metrics.XHeight, scale);
Report("Cap height", metrics.CapHeight, scale);

static void Report(string name, int units, double scale)
{
    if (units == 0)
    {
        Console.WriteLine($"{name}: not declared");
        return;
    }

    Console.WriteLine(FormattableString.Invariant(
        $"{name}: {units} units, {units * scale:0.##} px"));
}
```

```text
x-height: 536 units, 10.72 px
Cap height: 714 units, 14.28 px
```

A zero field means the font didn’t declare a height; the full program reports that separately for each metric. Noto Sans is included under the SIL Open Font License.

## Variations

Append this to the snippet at the top to find the font size that gives lowercase `x` a declared height of 12 px.

```csharp
// Size this font for a 12 px x-height.
if (metrics.XHeight > 0)
{
    double size = 12.0 * metrics.UnitsPerEm
        / metrics.XHeight;
    Console.WriteLine(FormattableString.Invariant(
        $"Font size: {size:0.##} px"));
}
```

```text
Font size: 22.39 px
```

## Related recipes

- [Read a font’s underline metrics](https://recursiveai.org/onefont/cookbook/read-font-underline-metrics/)
- [Render a glyph outline as an SVG path](https://recursiveai.org/onefont/cookbook/render-glyph-outline-as-svg-path/)

## The short answer

Read `XHeight` and `CapHeight` from `FontFace.Metrics` and multiply each by `fontSizeInPixels / UnitsPerEm`. These are the values declared by the font; OneFont does not measure glyph outlines to fill the fields. If either value is zero, treat that value as unavailable rather than as a height of zero pixels.

## What is the difference between x-height and cap height in fonts?

The [OpenType `OS/2` specification](https://learn.microsoft.com/en-us/typography/opentype/spec/os2#sxheight) defines x-height as the approximate height of non-ascending lowercase letters and cap height as the approximate height of uppercase letters, both above the baseline. The lowercase `x` and uppercase `H` are useful references. Neither field is a line height or the maximum extent of all the letters in the font. A lowercase `g`, for example, can go below the baseline without changing x-height.

At 20 px, the bundled face declares 10.72 px for x-height and 14.28 px for cap height. The 20 px applies to the em, not either letter height. When choosing a fallback font, matching the lowercase heights can make a mixed-case paragraph look more consistent. For an all-caps label, compare cap heights instead. The variation solves the same scaling equation backward to choose a font size for a target x-height.

## How do you find the x-height when a font does not declare it?

OneFont reads `sxHeight` and `sCapHeight` from [version 2 or later of the `OS/2` table when the fields are present](https://github.com/recursive-ai-oss/onefont-csharp/blob/7db48bc21a7eec46272aa73f4e76acf07645db03/src/OneFont/Fonts/FontFace.cs#L103-L107). A zero in one field tells you nothing about the other, so check them independently. Keep the original size if matching a height is optional; don’t divide by zero to calculate a new size.

If you need a measured fallback, use the outline of `x` for x-height or `H` for cap height. The specification recommends these reference glyphs when an optical value isn’t available. Measure from the baseline to the top of the outline, then apply the same em scale. That gives you the height of a specific glyph in that face, not recovered metadata. If the face doesn’t contain the reference glyph, leave the height unknown or choose a different face for the comparison.
