MapSequence makes ffi display as one glyph. The example uses a triangle for its shape.
dotnet add package RecursiveAI.OneFontRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
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 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.
Wrote ligatures.otfThe 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
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}"); } }
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 ffi:
font.Map(0xFB03, ffi);This adds a separate character map entry. You don’t need it for the three-letter input ffi.
Related recipes
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 maps one character at a time. The ligature rule instead goes in 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 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 ffi (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.