Recursive AI

Load a variable font named instance 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.

A named instance is a preset, like Bold, stored in the variable font. The program reads Newsreader.ttf from the working directory (the file comes in the zip); you can replace it with the path to any variable .ttf that declares the preset.

Terminal
dotnet add package RecursiveAI.OneFont

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

C#
using OneFont;
var face = FontFace.Load(
    File.ReadAllBytes("Newsreader.ttf"));
var bold = face.WithNamedInstance("Bold");
foreach (var axis in bold.Variations)
    Console.WriteLine(
        $"{axis.Tag}: {axis.Value}");

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

Selects the font’s Bold preset and prints its active weight and optical size.

Text
wght: 700
opsz: 18

WithNamedInstance throws ArgumentException if that name isn’t declared in the file; check NamedInstances for the available names. The zip includes the sample’s SIL Open Font License.

Variations

List the presets before choosing one

Append this to print the names a style picker can offer:

C#
foreach (var preset in face.NamedInstances)
    Console.WriteLine(preset.Name);
Text
ExtraLight
Light
Regular
Medium
SemiBold
Bold
ExtraBold

Read variable font axes and ranges

Set variable font weight and optical size


The short answer

Load the variable font once and call face.WithNamedInstance("Bold") to get a face at the coordinates of its Bold preset. Use the returned face for shaping or reading outlines and metrics; the original stays at its existing settings. To see what the preset selected, read bold.Variations. The sample’s Bold preset sets wght to 700 and leaves opsz at 18. Those values come from this font, not from a rule in the API about what “Bold” means.

What is a named instance of a variable font?

An instance is a point in the range the variable file describes. A named instance gives one such point a label: in a font with several axes, it can set weight, width, and optical size together. The OpenType fvar table holds these presets as records with a name reference and one user-space coordinate per axis. A preset is not a separate font file. For example, the Newsreader subset declares Regular and Bold inside the same .ttf, but an unlisted combination such as a weight of 450 and an optical size of 16 can still be selected by passing axis values directly.

How do you find the named instances in a font?

Read face.NamedInstances rather than guessing names from the filename or offering a fixed list like Regular, Italic, and Bold. Each entry exposes its Name, its Coordinates, and its PostScriptName when the font supplies one. The Name is the subfamily label used for selection, not the PostScript name. The OneFont API compares names “ignoring case; the first match wins”. If you show the entries in a picker, you can use the selected entry’s zero-based index with WithNamedInstance(index) to identify the exact record even if labels repeat. An index belongs to that font file’s list; don’t assume the same index means the same style in another file.

An empty list means there are no declared presets to offer, not necessarily that there are no axes to adjust. If the task is to use a font’s intended presets, list its names. If it’s to choose a combination in between those presets, use its axis ranges instead. OneFont selects a face over the existing bytes; it doesn’t export the preset as a static font.

All onefont recipes