---
title: "Measure text width in Go with a font"
url: https://recursiveai.org/onefont/go/cookbook/measure-shaped-text-width/
description: "Shape text with a TTF or OTF font in Go, then convert the run’s horizontal advance from font units to pixels at your chosen size."
version: "1.1.1"
---

# Measure text width in Go with a font

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

[pkg.go.dev](https://pkg.go.dev/recursiveai.org/go/onefont) · [GitHub](https://github.com/recursive-ai-oss/onefont-go) · [Docs](https://recursiveai.org/onefont/go/docs/) · [Download example (.zip)](https://recursiveai.org/onefont/go/cookbook/measure-shaped-text-width/measure-shaped-text-width.zip)

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.

```shell
go get recursiveai.org/go/onefont
```

Requires Go 1.24+.

```go
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
}
```

```text
AV: 19.18 px
```

That’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](https://recursiveai.org/onefont/go/cookbook/measure-shaped-text-width/measure-shaped-text-width.zip) 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`.

```go
px := 32.0 * float64(run.Width()) /
    float64(face.UnitsPerEm())
```

## Related recipes

- [Set a variable font axis in Go](https://recursiveai.org/onefont/go/cookbook/instantiate-variable-font-axis/)
- [Split mixed RTL and LTR text into bidi runs in Go](https://recursiveai.org/onefont/go/cookbook/resolve-bidirectional-text-runs/)

## 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](https://learn.microsoft.com/en-us/typography/opentype/spec/hmtx) distinguishes advance width from side bearings.
