Recursive AI

Build an OpenType font from glyph outlines 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.

Draw a triangle for A, set its width, and save it as an .otf font.

Terminal
dotnet add package RecursiveAI.OneFont

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using OneFont;
 
var path = new GlyphOutlineBuilder();
path.MoveTo(50, 0);
path.LineTo(250, 700);
path.LineTo(450, 0);
path.Close();
 
var font = new FontBuilder(1000);
font.FamilyName = "Triangle Demo";
int glyph = font.AddGlyph(
    path.ToOutline(), advance: 500);
font.Map('A', glyph);
File.WriteAllBytes("Triangle.otf", font.Build());
Console.WriteLine("Wrote Triangle.otf");

Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.

Text
Wrote Triangle.otf

To check the result, install Triangle.otf, choose Triangle Demo in a font picker, and type A to see the triangle.

The triangle is the only mapped glyph. AddGlyph throws ArgumentException if an outline coordinate is outside ±16,383 font units.

Variations

Give a glyph a curved edge. Replace the three path commands before Close() with these. The same program writes Triangle.otf, but A now has a curved top.

C#
path.MoveTo(50, 0);
path.QuadTo(250, 1000, 450, 0);

The short answer

Use FontBuilder to combine three inputs: a path for the shape, an advance to place the next glyph, and a Unicode mapping so text can select the shape. Build() returns OpenType bytes with Compact Font Format (CFF) outlines. Write them to an .otf file. You don’t need an existing font: the triangle’s coordinates are all the input the example needs to draw A.

How do I create my own OTF font?

Construct each outline as one or more closed contours. MoveTo starts a contour, LineTo draws a straight edge, and Close joins the end back to the start. If a glyph needs curved edges, use QuadTo for a quadratic Bézier (one control point) or CubicTo for a cubic Bézier (two control points). The builder docs describe the coordinate system: font units with Y going up. If your input comes from a drawing surface with Y going down, convert the coordinates relative to your chosen baseline rather than feeding screen coordinates straight in.

Give the builder the number of units per em before adding any glyphs. That’s the design grid shared by the outlines and the widths. The example’s 500-unit advance at 1,000 units per em means half an em; it is not the 400-unit distance between the triangle’s left and right points.

Use the glyph ID returned by AddGlyph when calling Map. Map('A', id) associates the outline with U+0041; it doesn’t draw the outline or infer a character from its shape. Repeat the outline, advance, and mapping steps for the other characters your text needs. For example, a space can have an empty outline and a nonzero advance. FamilyName sets the name shown in font pickers.

What’s the difference between OTF and TTF when building a font?

The OpenType file specification defines both TrueType and CFF outline tables. FontBuilder always writes CFF outlines, regardless of the suffix you give the file. Choose .otf for its output; renaming the file to .ttf doesn’t convert the curves to TrueType outlines.

The builder docs say quadratic segments are raised to cubic segments for storage and no hinting is added. Hinting is information that can adjust strokes to the pixel grid at small sizes. The builder packages the geometry and spacing you give it; it isn’t a font editor that fills in missing letters, draws those shapes for you, or supplies hinting instructions.

All onefont recipes