---
title: "Read font underline metrics in C#"
url: https://recursiveai.org/onefont/cookbook/read-font-underline-metrics/
description: "Read a font’s underline position and thickness with OneFont in C#. Convert design units to pixels from a known baseline without drawing text."
version: "1.4.0"
---

# Read font underline metrics 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-underline-metrics/read-font-underline-metrics.zip)

Read the underline position and thickness from `NotoSans-Regular.ttf`, included in the example 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 bytes = File.ReadAllBytes(
    "NotoSans-Regular.ttf");
var m = FontFace.Load(bytes).Metrics;
float scale = 20f / m.UnitsPerEm;
float top = 30f - m.UnderlinePosition * scale;
float height = m.UnderlineThickness * scale;
Console.WriteLine((top, height));
```

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

Prints `(top, thickness)` in pixels at a 20 px font size and a baseline at y = 30 px on a y-down screen.

```text
(32, 1)
```

## The full program

The download also prints the position and thickness in the font’s own design units.

```csharp
using System;
using System.IO;
using System.Globalization;
using OneFont;

CultureInfo.CurrentCulture =
    CultureInfo.InvariantCulture;
var bytes = File.ReadAllBytes(
    "NotoSans-Regular.ttf");
var metrics = FontFace.Load(bytes).Metrics;
int position = metrics.UnderlinePosition;
int thickness = metrics.UnderlineThickness;
float scale = 20f / metrics.UnitsPerEm;
float top = 30f - position * scale;
Console.WriteLine(
    $"Units: {position}, {thickness}");
Console.WriteLine($"Top: {top:0.##} px");
float pixels = thickness * scale;
Console.WriteLine($"Thickness: {pixels:0.##} px");
```

```text
Units: -100, 50
Top: 32 px
Thickness: 1 px
```

If the font has no readable `post` underline fields, both values are zero; choose a fallback if you need a visible line. The bundled Noto Sans is distributed under the SIL Open Font License included in the zip.

## Variations

### Compare underline offsets at two sizes

Append this to print the distance from the baseline to the top of the underline and the thickness, both in pixels. There’s no absolute y coordinate in this output.

```csharp
foreach (float size in new[] { 12f, 24f })
{
    float factor = size / metrics.UnitsPerEm;
    float depth = -position * factor;
    float height = thickness * factor;
    Console.WriteLine($"{size}px: {depth:0.##}, {height:0.##}");
}
```

```text
12px: 1.2, 0.6
24px: 2.4, 1.2
```

## Related recipes

[Read a font file](https://recursiveai.org/onefont/cookbook/read-font-file/)

[Read raw OpenType table bytes](https://recursiveai.org/onefont/cookbook/read-raw-opentype-tables/)

---

## The short answer

Read `m.UnderlinePosition` and `m.UnderlineThickness` from the loaded face’s metrics. Both are in font design units, so multiply them by the font size in pixels divided by `m.UnitsPerEm`. On a y-down screen, subtract the scaled position from the baseline’s y coordinate. Scale the thickness without subtracting it from the baseline; it’s a distance, not a coordinate.

## Where does the font store underline position and thickness?

OneFont reads both values from the OpenType [`post` table](https://learn.microsoft.com/en-us/typography/opentype/spec/post#header). The specification describes `underlinePosition` as the suggested y-coordinate of the **top** of the underline. That’s not the same as the center of a stroke. If another API takes a stroke center, it’s half the scaled thickness farther down than the top edge on a y-down screen.

[OneFont’s `FontMetrics` API](https://github.com/recursive-ai-oss/onefont-csharp/blob/7db48bc21a7eec46272aa73f4e76acf07645db03/src/OneFont/FontMetrics.cs#L52-L56) describes the position as “relative to the baseline (negative is below), in design units.” The baseline comes from your layout, not from the underline field. Measuring an underscore glyph instead would give its outline, not the font’s suggested decoration metrics.

## How do underline spacing and thickness change with font size?

The example font has 1,000 design units per em, so at 20 px each design unit is 0.02 px. Its −100-unit position is 2 px below the baseline, which puts the top at y = 32 px when the baseline is y = 30 px. The 50-unit thickness becomes 1 px. At 12 px the same thickness is 0.6 px; these are geometric values, not a decision about how to round or snap a line to pixels.

Use the size of the face you’re measuring, not the height of its line box. Line height can include extra spacing, while the scale is based on the em size. This example reads metrics and computes coordinates only; it doesn’t decide the length of the underline or draw it.
