---
title: "Map a character sequence to an OpenType ligature in C#"
url: https://recursiveai.org/onefont/cookbook/map-ligature-sequence-in-opentype-font/
description: "Use OneFont’s MapSequence to make the letters ffi display as one glyph in an OpenType font. Build the OTF and see which glyph the shaper returns."
version: "1.6.0"
---

# Map a character sequence to an OpenType ligature 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/map-ligature-sequence-in-opentype-font/map-ligature-sequence-in-opentype-font.zip)

`MapSequence` makes `ffi` display as one glyph. The example uses a triangle for its shape.

```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(950, 0);
path.LineTo(500, 700); path.Close();
int ffi = font.AddGlyph(
    path.ToOutline(), 1100);
font.Map('f', font.AddGlyph(null, 400));
font.Map('i', font.AddGlyph(null, 300));
font.MapSequence(['f', 'f', 'i'], ffi);
File.WriteAllBytes(
    "ligatures.otf", font.Build());
Console.WriteLine("Wrote ligatures.otf");
```

[The example](https://recursiveai.org/onefont/cookbook/map-ligature-sequence-in-opentype-font/map-ligature-sequence-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 ligatures.otf
```

The full program below loads the font back and checks that `ffi` becomes glyph 1, with an advance of 1,100 font units.

## The full program

```csharp
using OneFont;

var font = new FontBuilder(1000);
var path = new GlyphOutlineBuilder();
path.MoveTo(50, 0); path.LineTo(950, 0);
path.LineTo(500, 700); path.Close();
int ffi = font.AddGlyph(
    path.ToOutline(), 1100);
font.Map('f', font.AddGlyph(null, 400));
font.Map('i', font.AddGlyph(null, 300));
font.MapSequence(['f', 'f', 'i'], ffi);
File.WriteAllBytes(
    "ligatures.otf", font.Build());
Console.WriteLine("Wrote ligatures.otf");

var face = FontFace.Load(
    File.ReadAllBytes("ligatures.otf"));
var shaped = TextShaping.ShapeRun(
    "ffi", face);
foreach (var cluster in shaped)
{
    foreach (var glyph in cluster.Glyphs)
    {
        Console.WriteLine(
            $"ffi: glyph {glyph.GlyphId}, " +
            $"advance {glyph.XAdvance}");
    }
}
```

```text
Wrote ligatures.otf
ffi: glyph 1, advance 1100
```

`ShapeRun` returns one glyph for all three letters. Its advance comes from the ligature’s `AddGlyph` call: 1,100 font units. If you pass just one code point to `MapSequence`, it throws `ArgumentException`; use `Map` for a single character.

## Variations

### Map the single Unicode ligature character too

Add this line before `Build()` if the font should also display the same outline for U+FB03, the single character `ﬃ`:

```csharp
font.Map(0xFB03, ffi);
```

This adds a separate character map entry. You don’t need it for the three-letter input `ffi`.

## Related recipes

- [Shape text into glyphs](https://recursiveai.org/onefont/cookbook/shape-text/)
- [Use OpenType features](https://recursiveai.org/onefont/cookbook/opentype-features/)
- [Set vertical glyph metrics in an OpenType font](https://recursiveai.org/onefont/cookbook/set-vertical-metrics-in-opentype-font/)

## The short answer

Add the ligature outline with `FontBuilder.AddGlyph`, pass the returned glyph ID and the input code points to `MapSequence`, then write `Build()` to an `.otf` file. Use `MapSequence` when the input really is three characters (`f`, `f`, `i`); the text stays that way even though it displays as one glyph.

## How do you add a ligature to a custom OpenType font?

There are two parts: draw the shape and tell the shaper when to use it. The example draws one closed triangle with coordinates in font units, with Y increasing upward. Replace those coordinates with the outline you want, and choose an advance for the complete form. The shaper uses that one advance after substitution, not the sum of the three original letters’ advances.

A font’s [`cmap`](https://learn.microsoft.com/en-us/typography/opentype/spec/cmap) maps one character at a time. The ligature rule instead goes in [`GSUB`](https://learn.microsoft.com/en-us/typography/opentype/spec/gsub), the glyph substitution table. The shaper first looks up the individual characters in `cmap`, then the ligature rule replaces their glyph IDs with the one ligature glyph ID. If you register overlapping sequences with the same start, such as `ff` and `ffi`, the builder gives the longer one priority.

This example maps `f` and `i` to separate blank glyphs. The shaper matches glyph IDs, not the original characters: if you leave both letters unmapped, OneFont gives them the same blank glyph and the rule can no longer tell `ffi` from `fff`. The blank glyphs in the example have no ink on their own. For a font that also displays ordinary words, replace each `null` with the outline for that letter.

## Do you need to enable ligatures to see the result?

Not the optional ligatures setting. [`MapSequence`](https://recursiveai.org/onefont/docs/building/#build-a-font) writes the substitution for the default script under `ccmp` (glyph composition) and `rlig` (required ligatures), not `liga` (standard ligatures). The rule is meant for sequences whose outlines should stay together, not for decorative alternates that a reader can choose to turn off. In a browser, `font-variant-ligatures: none` doesn’t turn off `ccmp` or `rlig`.

For a font generated from text that already has a combined outline, this keeps the original character sequence available to select and copy. Typing or storing `ﬃ` (U+FB03) instead is a different choice: it changes the underlying text to one character. Only add the single-character mapping if that code point can occur in your input as well.
