onefont for Go
Font parsing, OpenType text shaping, bidi, line breaking and font fallback in pure Go, with nothing outside the standard library.
A quick look
Parse a font from its bytes and shape a string: glyph IDs, clusters and advances come back in font units, in visual order.
Getting startedface, err := onefont.Parse(data) run, err := onefont.Shape("office", face) for _, g := range run.All() { fmt.Println(g.Glyph, g.Cluster, g.AdvanceX) } // Mixed directions, a font per cluster chain := onefont.NewFallbackChain(latin, hebrew) for sr, err := range onefont.ShapeText(text, chain) { fmt.Println(sr.Font, sr.Level, sr.Run.Len()) }
New in 1.1.1
Shaping fixes: tatweel runs take Arabic forms, faces without a GPOS table place combining marks, and spacing marks keep their advance. From the changelog:
Changelog
- Tatweel: a run made only of Common characters that Arabic text uses (the tatweel U+0640, the Arabic comma) is shaped as Arabic when no script is given and none is carried, so a span of tatweels gets its initial, medial and final forms.
hb-shapeleaves such a run without a script; browsers give it the script of the text around it.- Faces without a GPOS table place combining marks. Each mark is placed from the glyph boxes by its combining class (above, below, attached, left, centre and right, double marks, Hebrew points, Arabic and Syriac vowel signs), and marks stacked on one base rise over each other, matching
hb-shape. A mark with no class keeps its place but gives up its advance. Faces with a GPOS table, and the Indic, Universal Shaping Engine, Khmer, Myanmar, Thai, Lao, Tibetan, Hangul, N’Ko and Mongolian shapers, are unchanged.- In a face without GDEF glyph classes only nonspacing marks lose their advance; spacing and enclosing marks (Mc, Me) keep it, as
hb-shapedoes.- The deprecated combining marks U+0340, U+0341, U+0343 and U+0344 are shaped as the plain marks they are equivalent to, even when the face maps them, and a base with marks after it is decomposed and recomposed even when the face maps the base itself (a Greek question mark before a mark, say).
New in 1.1
Web fonts, hinting data, grapheme clusters and feature ranges in shaping, emoji fallback, and dictionary line breaking. From the changelog:
Changelog
- Web fonts:
UnwrapWebFontunpacks WOFF 1.0 and WOFF 2.0 files (including WOFF2 collections and theglyf,locaandhmtxtransforms) to a plain font forParse;IsWebFonttells the two apart andWithMaxFontSizebounds the unpacked size (default 64 MiB).- Hinting data:
Face.GlyphDescriptionandInstance.GlyphDescriptiongive a glyph’s points, contour ends, instructions and composite components, andFace.Hintingthefpgm,prep,cvt,gaspandmaxpdata a TrueType interpreter needs.- Shaping:
WithGraphemeClustersmakes clusters whole UAX #29 grapheme clusters (conjuncts included, GB9c);WithFeatureRanges,FeatureRangeandParseFeatureRangelimit a feature to part of the text (liga[3:5]).- Fallback:
FallbackChain.WithEmojiPresentationsends emoji sequences (variation selector 16, keycaps, ZWJ sequences, modifiers, flags) to the first colour font that covers them.- Dictionary line breaking:
Segmenter.AppendLineBreaksallows a break between the words of a run of Thai, Lao, Khmer or Burmese letters, which UAX #14 leaves to a dictionary, as browsers do.
Upgrading from 1.0
Line breaks in Thai, Lao, Khmer and Burmese change by default: LineBreaks, Lines and Segmenter.AppendLineBreaks now allow breaks between words inside a run of those letters. To keep the 1.0 breaks, set NoDictionary on a Segmenter.
Segmenter.NoDictionaryrestores the rules of UAX #14 alone.
The package
- Version
v1.1.1. Go 1.24 or newer.
- Fonts
TrueType, OpenType CFF and CFF2, TrueType Collections and variable fonts; WOFF and WOFF2 through
UnwrapWebFont. - Shaping
GSUB and GPOS with every lookup type and flag, and shapers for Arabic, the Indic scripts, Khmer, Myanmar, Tibetan, the Universal Shaping Engine scripts, Thai, Lao and Hangul.
- Unicode
Bidi (UAX #9), line breaks (UAX #14) and grapheme, word and sentence boundaries (UAX #29), at Unicode 17.0.0.
- Safety
No cgo, no
unsafe, no dependencies outside the standard library. Every offset is checked and shaping keeps work budgets, so a hostile font can’t panic or run without bound. - License
MIT or Apache-2.0.