In the sample font, AV is narrower than A and V measured separately: shaping applies the font’s kerning, which brings the letters closer together.
go get recursiveai.org/go/onefontRequires Go 1.24+.
package main import ( "fmt" "os" "recursiveai.org/go/onefont" ) func main() { face := loadFont() run, err := onefont.Shape("AV", face) if err != nil { panic(err) } px := 16.0 * float64(run.Width()) / float64(face.UnitsPerEm()) fmt.Printf("AV: %.2f px\n", px) } func loadFont() *onefont.Face { data, err := os.ReadFile( "NotoSans-Subset-Regular.ttf", ) if err != nil { panic(err) } face, err := onefont.Parse(data) if err != nil { panic(err) } return face }
AV: 19.18 pxThat’s the distance the pen advances when it draws AV. If the font file is missing or can’t be parsed, the example stops at the error instead of reporting a width.
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 NotoSans-Subset-Regular.ttf from the zip next to main.go, or use a different .ttf that contains A and V and change the filename in loadFont.
Variations
Measure at 32 px. Replace the px calculation. With the same font and text, this prints AV: 38.37 px.
px := 32.0 * float64(run.Width()) / float64(face.UnitsPerEm())
Related recipes
The short answer
Parse the exact font you plan to draw with. Call onefont.Shape and multiply run.Width() by the font size in pixels divided by face.UnitsPerEm(). Width() already sums the horizontal advances from shaping, in font units. It does not return pixels or the width of a box around the visible outline.
How do you calculate text width in pixels from a font size?
A font defines its shapes and spacing on its own grid. UnitsPerEm() tells you how many units on that grid correspond to one em. At a font size of 16 px, one em spans 16 px; the conversion is 16 × run width ÷ units per em. Keep that calculation in floating point and round only if a later step needs whole pixels. The example’s two decimal places are for display, not how the width is stored.
What is kerning, and why does it affect text width?
Kerning changes the spacing between a pair of glyphs. Shaping can also replace multiple input characters with one glyph, such as a ligature, or position marks relative to a base letter. The result depends on the text and on what the font supports. It’s not enough to multiply a character count by an average width or to add the advances of individually mapped letters. Shape the same text, font, and features you’ll use to draw it. For a line that mixes directions or needs several fonts, use ShapeText with a font fallback chain; measure each returned run with its own font’s units per em before adding the pixel widths.
Is advance width the same as the visible width of the text?
No. Advance answers “where should the next run start?” A space can advance the pen without painting anything, and an outline can stick out beyond its advance. For a tight crop around the ink, use glyph bounds instead: position each glyph’s bounds at its pen position plus the offsets from shaping, then take their union. The OpenType horizontal metrics specification distinguishes advance width from side bearings.