---
title: "Convert a glyph outline to an SVG path in Go"
url: https://recursiveai.org/onefont/go/cookbook/export-glyph-outline-as-svg-path/
description: "Read a TTF or OTF glyph outline in Go and turn its lines and curves into SVG path data, flipping the y coordinates so the glyph stays upright."
version: "1.1.1"
---

# Convert a glyph outline to an SVG path 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.

[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/export-glyph-outline-as-svg-path/export-glyph-outline-as-svg-path.zip)

Read the glyph with `GlyphPath` and negate y for SVG.

```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()
	glyph := face.GlyphIndex('L')
	if glyph == 0 {
		panic("glyph not in font")
	}
	path, err := face.GlyphPath(glyph)
	if err != nil {
		panic(err)
	}
	// M, L, Q, C, Z in PathSegmentKind order.
	points := [...]int{1, 1, 2, 3, 0}
	for seg := range path.All() {
		fmt.Printf("%c", "MLQCZ"[seg.Kind])
		n := points[seg.Kind]
		for _, p := range seg.Pts[:n] {
			fmt.Printf(" %g %g", p.X, 0-p.Y)
		}
		fmt.Println()
	}
}

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
M 97 0
L 97 -714
L 187 -714
L 187 -80
L 499 -80
L 499 0
Z
```

If the font doesn’t map the letter, the example stops instead of drawing the font’s missing glyph. If reading the outline fails, discard the path: it can be incomplete.

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/export-glyph-outline-as-svg-path/export-glyph-outline-as-svg-path.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 `L` and change the filename in `loadFont`.

## Variations

**Export a glyph with curves.** Swap `L` for `C` in the example. TrueType outlines give `Q` (quadratic) curves, as in this example, while OpenType fonts with CFF outlines give `C` (cubic) curves.

```go
glyph := face.GlyphIndex('C')
```

**Display the `L` as a standalone SVG.** Paste the output into `d` and give the SVG a `viewBox` that includes the negative y coordinates. Save this as `letter.svg` and open it in a browser.

```svg
<svg xmlns="http://www.w3.org/2000/svg"
     viewBox="0 -750 600 800">
  <path d="M 97 0 L 97 -714 L 187 -714
           L 187 -80 L 499 -80 L 499 0 Z"/>
</svg>
```

## Related recipes

- [Measure text width in Go with a font](https://recursiveai.org/onefont/go/cookbook/measure-shaped-text-width/)
- [Set a variable font axis in Go](https://recursiveai.org/onefont/go/cookbook/instantiate-variable-font-axis/)

## The short answer

Look up the character’s glyph ID with `GlyphIndex`, then call `GlyphPath`. Write an absolute SVG command for each segment: `M` for move, `L` for line, `Q` for a quadratic curve, `C` for a cubic curve, and `Z` to close. For every endpoint and control point, keep x and negate y. The output is geometry in font units, not an image or an embedded font.

## How do you generate SVG path data from a font glyph?

The segment kind tells you how many coordinate pairs to write. Moves and lines have one pair. A TrueType quadratic curve has a control point followed by the endpoint. A CFF cubic curve has two controls followed by the endpoint. The uppercase commands in the [SVG path specification](https://www.w3.org/TR/SVG2/paths.html#PathData) use absolute positions, so you can write the points directly after flipping y. Keep every new `M` and closing `Z`: a glyph can contain multiple contours, including contours that form holes. A space may return an empty path without returning an error.

## Why is my SVG glyph upside down or clipped?

Font outlines normally put the baseline at y = 0 and use positive y above it. In SVG, positive y points down in the [initial coordinate system](https://www.w3.org/TR/SVG2/coords.html#InitialCoordinateSystem). After the flip, the top of this `L` is at −714. A view box starting at y = 0 would miss the part above the baseline, so the standalone example starts at −750. Those bounds fit this sample; choose new bounds when you export a different glyph. To compute a box from `GlyphBounds`, flip the y range as well: the new minimum y is the negative of the font’s maximum y.

## Can you convert text to SVG paths the same way?

For a string, shape it first and export the glyph IDs the shaper returns. Mapping letters one by one can miss ligatures and contextual forms. Place each outline using the pen advance and offsets from the shaped run; then convert the positioned points to SVG coordinates. Paths no longer carry the original text for selection, search, or assistive technology, so keep the text separately where that matters.
