Recursive AI

Apply an OpenType feature to a range in Go

onefont-go is a free, open-source font and text-shaping library for Go from Recursive AI. MIT or Apache-2.0, no dependencies.

Pass -liga[3:5] to ParseFeatureRange to turn off the fi ligature over those bytes only. The zip includes the sample font.

Terminal
go get recursiveai.org/go/onefont
GO
package main
 
import (
	"fmt"
	"log"
	"os"
 
	"recursiveai.org/go/onefont"
)
 
func main() {
	text := "é fi fi"
	feature, err := onefont.ParseFeatureRange("-liga[3:5]")
	if err != nil {
		log.Fatal(err)
	}
 
	data, err := os.ReadFile("NotoSans-Subset-Regular.ttf")
	if err != nil {
		log.Fatal(err)
	}
	face, err := onefont.Parse(data)
	if err != nil {
		log.Fatal(err)
	}
 
	run, err := onefont.Shape(text, face,
		onefont.WithFeatureRanges(feature))
	if err != nil {
		log.Fatal(err)
	}
	for _, glyph := range run.All() {
		fmt.Printf("byte %d: glyph %d\n",
			glyph.Cluster, glyph.Glyph)
	}
}
Text
byte 0: glyph 169
byte 2: glyph 1
byte 3: glyph 71
byte 4: glyph 74
byte 5: glyph 1
byte 6: glyph 339

Offsets are UTF-8 bytes: é takes two bytes, so the first fi spans [3:5] (byte 5 is excluded).

The first fi yields separate glyphs at bytes 3 and 4. The second yields one glyph at byte 6. Glyph IDs are specific to this font.

Paste the code above into main.go, put NotoSans-Subset-Regular.ttf from the zip next to it, run go mod init example and go get recursiveai.org/go/onefont, then go run .; or unzip the example and go run ..

Requires Go 1.24 or later.

Variations

Disable the second ligature instead. Change the range in the sample:

GO
feature, err := onefont.ParseFeatureRange("-liga[6:8]")
Text
byte 0: glyph 169
byte 2: glyph 1
byte 3: glyph 339
byte 5: glyph 1
byte 6: glyph 71
byte 7: glyph 74

The short answer

Parse a setting like -liga[3:5] with onefont.ParseFeatureRange and pass it to onefont.Shape through onefont.WithFeatureRanges. The interval is start-inclusive and end-exclusive, in UTF-8 bytes. The feature is off inside that range; it keeps its normal value elsewhere.

What is an OpenType feature?

An OpenType feature is a font rule for substituting or positioning glyphs. The registered OpenType feature tags include liga for standard ligatures and kern for kerning, which adjusts spacing between glyphs. Turning off liga changes the glyphs the shaper returns. It doesn’t edit the Go string or replace its letters with the Unicode ligature character. If your font lacks the feature, the shaper ignores the setting; the same range applied to another font need not give the same glyph count.

How do I use OpenType features on just part of a string?

In é fi fi, é takes two UTF-8 bytes. The first space is at byte 2, so the first fi spans [3:5]. The second fi starts at byte 6. If your selection comes from a rune or character index, convert it to a byte offset before building the range. For text selected by a user, choose boundaries that don’t cut through a grapheme cluster. If you are composing the text from known pieces, the byte length of the preceding text is the start, and adding the byte length of the selected piece gives the end.

A range can override a whole-run WithFeatures setting as well as a script default. Outside the range, the whole-run or default setting still applies. If ranges for the same tag overlap, the last setting wins. An empty or reversed range matches no glyphs. When shaping with ShapeRuns or ShapeText, count these offsets from the whole string you passed in, even if the shaper splits it into several runs. Recalculate the range if the source string changes before the call.

All onefont Go recipes