Recursive AI

Set a variable font axis 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.

Set wght to 900 with Face.Instance, then read A’s advance width: the horizontal space the glyph takes.

Terminal
go get recursiveai.org/go/onefont

Requires Go 1.24+.

GO
package main
 
import (
	"fmt"
	"os"
 
	"recursiveai.org/go/onefont"
)
 
func main() {
	face := loadFont()
	instance, err := face.Instance(
		onefont.Variation{
			Tag:   onefont.MakeTag("wght"),
			Value: 900,
		},
	)
	if err != nil {
		panic(err)
	}
	glyph := instance.GlyphIndex('A')
	fmt.Println("Default A:",
		face.Advance(glyph))
	fmt.Println("wght 900 A:",
		instance.Advance(glyph))
}
 
func loadFont() *onefont.Face {
	data, err := os.ReadFile("RobotoFlex.ttf")
	if err != nil {
		panic(err)
	}
	face, err := onefont.Parse(data)
	if err != nil {
		panic(err)
	}
	return face
}
Text
Default A: 1280
wght 900 A: 1452

If the font has no variation axes, Instance returns the default design rather than an error. Check face.Axes() if you need to require a specific axis.

Paste the code above into main.go, run go mod init example and go get recursiveai.org/go/onefont, then go run .; or unzip the example and go run ..

If you paste the code, put RobotoFlex.ttf from the zip next to main.go, or use a different variable .ttf with a wght axis and change the filename in loadFont.

Variations

Set the width axis instead. Replace the face.Instance call, keeping the error check. Change the second output label to wdth 75 A:. The advance at this width is 1216 font units.

GO
instance, err := face.Instance(
    onefont.Variation{
        Tag: onefont.MakeTag("wdth"),
        Value: 75,
    },
)

The short answer

Call face.Instance with a Variation containing the four-letter axis tag and a coordinate. For example, wght at 900 sets the weight axis of the returned instance; it does not modify the original face. Read the new design’s metric with instance.Advance(glyph). This returns an instance for measuring and shaping, not a new TTF file.

What is a variable weight font?

A variable weight font can provide intermediate designs between its light and heavy extremes. wght is the registered OpenType axis for weight. The value is a design coordinate, not a percentage of boldness. A heavier design can also change letter spacing: in the bundled Roboto Flex file, A advances 1280 font units at the default design and 1452 at wght 900. That difference is specific to this font and glyph. Don’t assume every heavier glyph will get wider.

How do you find a variable font’s axis ranges?

Read face.Axes() for the tag and the Min, Default, and Max values for each axis. Use them to decide which controls to offer and what ranges they accept. Instance clamps a supplied coordinate to the font’s range, and each axis you omit keeps its default. An unknown tag won’t change the font, so check that a tag is present before accepting it from a user. instance.Variations() reports the effective coordinates after clamping. Those are more useful to show in an interface than the raw numbers you requested.

Does setting font weight change how text is measured?

It can. The example reads the advance of one glyph, the distance from its origin to the next pen position. For a word, pass the instance instead of the face to onefont.Shape, use the shaped run’s width, and convert from font units at your chosen size. Use the same instance for drawing the glyph outlines. Otherwise you can measure a word at the new weight and accidentally draw it with the original advances or outlines.

All onefont Go recipes