onefont Go cookbook
Font outlines and metrics
Measure text width in Go with a fontShape 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.Convert a glyph outline to an SVG path in GoRead 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.Set a variable font axis in GoCreate a Roboto Flex instance at a chosen weight in Go, then read the glyph advance at that coordinate and compare it with the default.
Shape and segment text
Break text
Find Thai line break opportunities in GoFind Thai line break opportunities without splitting on spaces. Get UTF-8 byte offsets with onefont-go’s built-in dictionary before measuring lines.Iterate Unicode grapheme clusters in GoIterate user-perceived characters in a Go string without splitting accents or emoji sequences. Get whole clusters or UTF-8 byte boundaries.
Shape text
Choose an emoji fallback font in GoKeep text in your primary font and prefer a color emoji font for emoji sequences. Shape both presentations in Go and see which font each run uses.Apply an OpenType feature to a range in GoApply a ranged OpenType feature in Go using UTF-8 byte offsets. Disable the fi ligature in one part of a string without changing the rest.