---
title: "Set vertical glyph metrics in an OpenType font in C#"
url: https://recursiveai.org/onefont/cookbook/set-vertical-metrics-in-opentype-font/
description: "Set a glyph’s advance height and vertical origin with OneFont’s SetVerticalMetrics. Write an OpenType font with vhea, vmtx, and VORG tables."
version: "1.6.0"
---

# Set vertical glyph metrics in an OpenType font 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/set-vertical-metrics-in-opentype-font/set-vertical-metrics-in-opentype-font.zip)

`SetVerticalMetrics` sets the glyph’s origin and advance down the column. OneFont writes the font but doesn’t lay out vertical text.

```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 font = new FontBuilder(1000);
var path = new GlyphOutlineBuilder();
path.MoveTo(50, 0); path.LineTo(650, 0);
path.LineTo(350, 700); path.Close();
int glyph = font.AddGlyph(
    path.ToOutline(), 700);
font.Map('A', glyph);
font.SetVerticalMetrics(glyph, 1200, 900);
File.WriteAllBytes(
    "vertical.otf", font.Build());
Console.WriteLine("Wrote vertical.otf");
```

[The example](https://recursiveai.org/onefont/cookbook/set-vertical-metrics-in-opentype-font/set-vertical-metrics-in-opentype-font.zip) is the full program below: unzip it and `dotnet run`. To run only the code above, paste it into Program.cs of a new `dotnet new console` project and `dotnet run`.

```text
Wrote vertical.otf
```

Both values are in font units: `1200` is the advance height (how far the pen moves down the column), and `900` is the vertical origin Y.

## The full program

```csharp
using System.Buffers.Binary;
using OneFont;

var font = new FontBuilder(1000);
var path = new GlyphOutlineBuilder();
path.MoveTo(50, 0); path.LineTo(650, 0);
path.LineTo(350, 700); path.Close();
int glyph = font.AddGlyph(
    path.ToOutline(), 700);
font.Map('A', glyph);
font.SetVerticalMetrics(glyph, 1200, 900);
File.WriteAllBytes(
    "vertical.otf", font.Build());
Console.WriteLine("Wrote vertical.otf");

var face = FontFace.Load(
    File.ReadAllBytes("vertical.otf"));
int horizontal = face.GetAdvance(glyph);
Console.WriteLine(
    $"Horizontal advance: {horizontal}");

// Each vmtx record here is four bytes.
var metrics = face.GetTable("vmtx").Span;
int offset = glyph * 4;
int advance = BinaryPrimitives
    .ReadUInt16BigEndian(metrics[offset..]);
Console.WriteLine(
    $"Vertical advance: {advance}");

var origins = face.GetTable("VORG").Span;
int origin = BinaryPrimitives
    .ReadInt16BigEndian(origins[4..]);
int count = BinaryPrimitives
    .ReadUInt16BigEndian(origins[6..]);
for (int i = 0; i < count; i++)
{
    var record = origins[(8 + 4 * i)..];
    int id = BinaryPrimitives
        .ReadUInt16BigEndian(record);
    if (id == glyph)
        origin = BinaryPrimitives
            .ReadInt16BigEndian(record[2..]);
}
Console.WriteLine(
    $"Vertical origin Y: {origin}");
```

```text
Wrote vertical.otf
Horizontal advance: 700
Vertical advance: 1200
Vertical origin Y: 900
```

The font maps `A` to the triangle drawn in the code. The program reads its metrics back from the font bytes. If the advance height is negative or the glyph ID is out of range, `SetVerticalMetrics` throws `ArgumentOutOfRangeException`.

## Variations

### Use the font’s ascent as the origin

Replace the `SetVerticalMetrics` call with this one to change only the advance height, leaving the origin at the same Y coordinate as glyphs without an override:

```csharp
font.SetVerticalMetrics(glyph, 1200, font.Ascent);
```

On this font, the full program then prints `Vertical origin Y: 800`.

## Related recipes

- [Read raw OpenType tables](https://recursiveai.org/onefont/cookbook/read-raw-opentype-tables/)
- [Read a glyph’s left side bearing](https://recursiveai.org/onefont/cookbook/read-glyph-left-side-bearing/)
- [Map a character sequence to an OpenType ligature](https://recursiveai.org/onefont/cookbook/map-ligature-sequence-in-opentype-font/)

## The short answer

Call `FontBuilder.SetVerticalMetrics(glyph, advanceHeight, originY)` with the ID returned by `AddGlyph`, then call `Build()`. The builder writes an OpenType font with CFF outlines and three vertical tables: `vhea`, `vmtx`, and `VORG`. Keep the horizontal advance in `AddGlyph`; the vertical call doesn’t replace it.

## What are advance height and vertical origin in a font?

The [advance height](https://learn.microsoft.com/en-us/typography/opentype/spec/vmtx) is the distance between consecutive glyph positions in vertical writing, not the height of the ink. The [vertical origin Y](https://learn.microsoft.com/en-us/typography/opentype/spec/vorg) is where the vertical pen position falls relative to the glyph’s baseline. The example uses coordinates that increase upward, so an origin of 900 puts the pen 900 units above the baseline. The triangle ends at Y = 700, putting the top of its outline 200 units below that origin. The pen advances 1,200 units down for the next glyph.

Use the same font units for the outline and both metrics. In a 1,000-unit font at a size of 20 pixels, that advance is 24 pixels before any extra spacing the layout engine adds. Moving the origin shifts the glyph relative to the pen without changing how far the pen advances. Changing the advance affects spacing without moving the outline relative to its own origin.

## Which OpenType tables store vertical glyph metrics?

[`vhea`](https://learn.microsoft.com/en-us/typography/opentype/spec/vhea) carries font-wide vertical information and the count of full metric records. `vmtx` stores advance heights and top side bearings. `VORG` records the default origin Y and per-glyph exceptions for CFF fonts. The program’s `vmtx` read uses the layout OneFont writes, one four-byte record per glyph. Don’t assume that layout for arbitrary fonts: `vhea` can specify fewer full advance records than there are glyphs.

OneFont writes these tables only once you set vertical metrics for at least one glyph. For a glyph without an override, it writes an advance of `Ascent - Descent` and an origin at `Ascent`. That’s 1,000 and 800, respectively, with the example’s defaults. If you want uniform spacing down a column, pick one advance height and override only the glyphs that need a different origin or advance from the font-wide defaults.

## How do you use a font for upright vertical text?

Use a layout engine that supports vertical writing. In a browser, load the font using `@font-face` and set [`writing-mode`](https://developer.mozilla.org/en-US/docs/Web/CSS/writing-mode) to `vertical-rl` or `vertical-lr`. Set [`text-orientation`](https://developer.mozilla.org/en-US/docs/Web/CSS/text-orientation) to `upright` when you want even Latin letters to stay upright rather than turn sideways. The font provides the metrics; the browser chooses the writing direction, orientation, and placement. OneFont does not provide that layout step, even for fonts it wrote itself.

Save this as `vertical.html` beside the generated `vertical.otf` and serve both from the same directory. The two `A` characters use the triangle outline from the recipe. At a 20 px font size, their vertical advances add up to 48 px.

```html
<!doctype html>
<meta charset="utf-8">
<title>Vertical font example</title>
<style>
@font-face {
  font-family: Demo;
  src: url("vertical.otf");
}
.column {
  font: 20px Demo;
  writing-mode: vertical-rl;
  text-orientation: upright;
}
</style>
<div class="column">AA</div>
```
