---
title: "Align mixed-font baselines in C#"
url: https://recursiveai.org/onefont/cookbook/align-mixed-font-baselines/
description: "Align two fonts and sizes on a shared baseline in C#. Scale ascent and descent per face, then check the shaped glyphs for vertical clipping."
version: "1.4.0"
---

# Align mixed-font baselines 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/align-mixed-font-baselines/align-mixed-font-baselines.zip)

Set the baseline, where letters sit, at the largest scaled ascent (the font’s height above the baseline). Use `serif.ttf` and `sans.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 large = LoadMetrics("serif.ttf", 28);
var small = LoadMetrics("sans.ttf", 16);
var baseline = Math.Max(large.Up, small.Up);
var below = Math.Max(large.Down, small.Down);
Console.WriteLine(FormattableString.Invariant(
    $"Baseline: {baseline:F2} px"));
Console.WriteLine(FormattableString.Invariant(
    $"Line height: {baseline + below:F2} px"));

static (double Up, double Down) LoadMetrics(
    string path, double size)
{
    var bytes = File.ReadAllBytes(path);
    var face = FontFace.Load(bytes);
    var scale = size / face.UnitsPerEm;
    return (face.Metrics.Ascender * scale,
        -face.Metrics.Descender * scale);
}
```

Unzip [the example](https://recursiveai.org/onefont/cookbook/align-mixed-font-baselines/align-mixed-font-baselines.zip), then `dotnet run`.

Prints the baseline measured down from the top of the line, and the line height.

```text
Baseline: 25.99 px
Line height: 32.59 px
```

Draw each run with its baseline at that y coordinate, or set its top to the baseline minus that run’s scaled ascent.

## The full program

The program shapes `Type` at 28 px in DejaVu Serif and `gyp` at 16 px in Noto Sans. It places both runs on the same baseline and checks the vertical bounds of the positioned outlines.

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

CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
var large = Measure("Type", "serif.ttf", 28);
var small = Measure("gyp", "sans.ttf", 16);
double baseline = Math.Max(large.Up, small.Up);
double height = baseline + Math.Max(large.Down, small.Down);
Console.WriteLine($"Baseline: {baseline:F2} px");
Console.WriteLine($"Line height: {height:F2} px");

foreach (var (label, run) in new[] { ("Type", large), ("gyp", small) })
{
    double top = baseline - run.InkTop;
    double bottom = baseline - run.InkBottom;
    Console.WriteLine($"{label}: ink y={top:F2}..{bottom:F2} px");
    if (top < 0 || bottom > height)
        throw new InvalidOperationException($"{label} exceeds the line box.");
}
Console.WriteLine("Both runs fit vertically.");

static (double Up, double Down, double InkTop, double InkBottom) Measure(
    string text, string path, double size)
{
    var face = FontFace.Load(File.ReadAllBytes(path));
    double scale = size / face.UnitsPerEm;
    double inkTop = 0, inkBottom = 0;
    foreach (var cluster in TextShaping.ShapeRun(text, face))
    foreach (var glyph in cluster.Glyphs)
    {
        var outline = face.GetOutline(glyph.GlyphId);
        if (outline.IsEmpty)
            continue;
        var bounds = outline.TightBox();
        inkTop = Math.Max(inkTop, (glyph.YOffset + bounds.MaxY) * scale);
        inkBottom = Math.Min(inkBottom, (glyph.YOffset + bounds.MinY) * scale);
    }
    return (face.Metrics.Ascender * scale, -face.Metrics.Descender * scale,
        inkTop, inkBottom);
}
```

```text
Baseline: 25.99 px
Line height: 32.59 px
Type: ink y=5.58..32.21 px
gyp: ink y=17.25..29.83 px
Both runs fit vertically.
```

The line runs from y = 0 to y = 32.59 px. Both sets of outlines stay inside it. If a run’s ink falls outside the line box, the program throws instead of reporting that it fits.

## Variations

**Show the top edge of each run.** Add these lines after the other prints in the snippet at the top. The runs have different top edges even though they share a baseline.

```csharp
Console.WriteLine(FormattableString.Invariant(
    $"Large top: {baseline - large.Up:F2} px"));
Console.WriteLine(FormattableString.Invariant(
    $"Small top: {baseline - small.Up:F2} px"));
```

```text
Large top: 0.00 px
Small top: 8.89 px
```

## Related recipes

- [Measure text width in C#](https://recursiveai.org/onefont/cookbook/measure-text-width/)
- [Fit text into a fixed-size box in C#](https://recursiveai.org/onefont/cookbook/fit-text-to-fixed-size-box/)

<!-- article -->

## The short answer

Give each run its own scale: its font size in pixels divided by its face’s units per em. Put the baseline at the largest scaled ascent from the line’s top. Leave room below it for the largest scaled descent. One face can set the top and another can set the bottom, so use the two maxima separately even if one run is visibly bigger.

## What are the baseline, ascent, and descent of a font?

The baseline is the common reference for placing glyphs. Ascent measures above it and descent measures below it. In [OneFont’s metrics](https://github.com/recursive-ai-oss/onefont-csharp/blob/7db48bc21a7eec46272aa73f4e76acf07645db03/src/OneFont/FontMetrics.cs#L40-L50), `Ascender` is positive and `Descender` is negative. Negate the descent to get a positive amount of room to reserve below the line. Both values come in design units, so raw metrics from two faces aren’t directly comparable.

`LineGap` is extra spacing between lines, not space that moves a run off the shared baseline. The example computes one line without this extra spacing. For a paragraph, choose a line-gap policy separately so you don’t accidentally add a different gap to each run.

## How do you align text with different font sizes?

Set both baseline origins to the same y coordinate. If a drawing API asks for the top of the run’s metric box rather than its baseline, the two origins are `baseline - scaledAscent` for each face. You can see the difference in the variation: the smaller run’s top begins lower down the line. Aligning the two tops instead would give the smaller run a higher baseline.

Line metrics reserve space; they don’t give the bounds of a specific word. For example, `gyp` has descenders, whereas all-cap text need not reach below the baseline at all. To avoid clipping ink, the full program gets the exact vertical curve bounds from `TightBox()` and adds each shaped glyph’s `YOffset` before scaling. Because outline y increases upward and screen y increases downward, it subtracts the scaled value from the baseline. If an accent extends above the line or a descender extends below it, either expand the line box using the measured bounds or leave that edge unclipped. Horizontal overhang is a separate check when the container also clips on the left or right.
