Recursive AI

Use OpenType localized forms 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.

Some languages draw the same letter differently, and the font uses the language tag to pick the form. In the zip’s sans.ttf, Russian б looks like a 6, while Serbian б has an S-shaped stroke above its loop.

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("sans.ttf"));
Console.WriteLine($"ru: {Glyph("ru")}");
Console.WriteLine($"sr: {Glyph("sr")}");
 
int Glyph(string lang)
{
    var options = new ShapingOptions
        { Language = lang };
    var run = TextShaping.ShapeRun(
        "б", face, options: options);
    return run[0].Glyphs[0].GlyphId;
}

Unzip the example, then dotnet run.

Prints the glyph ID for Cyrillic б in each language:

Text
ru: 290
sr: 419

If your font has no matching language system, OneFont uses the script’s default. If the language exists but has no applicable substitution, that character can still get the same glyph.

Variations

Keep Serbian but turn localized forms off

To turn off localized forms (locl), replace the options declaration in Glyph:

C#
var options = new ShapingOptions
    { Language = lang };
options.Features.Add(FontFeature.Off("locl"));
Text
ru: 290
sr: 290

The short answer

Pass a language tag in ShapingOptions.Language when you call TextShaping.ShapeRun. For horizontal text locl is on by default. You need a font with a localized substitution for that script, language, and character. In the bundled face, setting the language to sr changes the glyph for б from 290 to 419; the string is still б.

What are OpenType localized forms?

locl, or localized forms, is an OpenType substitution feature. It lets a font substitute a glyph preferred by a language that shares its writing script with other languages. It doesn’t translate text or replace Unicode characters. Use the original string for storage and search, and the shaped glyph IDs with the same face for drawing. Glyph IDs belong to the font, so don’t expect another face to use 419 for the same form.

OneFont applies ccmp and locl before the later shaping features, as described in the shaping guide. Looking up the character with GetGlyphIndex alone reads its default mapping, not the localized substitution. Shaping the same string under two languages isolates the difference in this example.

What language tag enables an OpenType localized form?

Use the language of the text, not the language of the application interface. OneFont accepts a BCP 47 language tag such as sr, or an uppercase OpenType language tag such as SRB. The library selects the font’s language system for the script in the run; a tag alone can’t supply a form the font doesn’t have. When a document mixes languages within the same script, shape each language’s span with its own options.

To diagnose an unchanged result, start with a character known to have an alternate in the font, check its language tag and compare the glyph IDs under the two settings. Also check that your feature list hasn’t turned locl off.

All onefont recipes