Recursive AI
onefontGoAvailable

onefont for Go

Font parsing, OpenType text shaping, bidi, line breaking and font fallback in pure Go, with nothing outside the standard library.

go get recursiveai.org/go/onefont

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 started
main.goGo
face, 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-shape leaves 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-shape does.
  • 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: UnwrapWebFont unpacks WOFF 1.0 and WOFF 2.0 files (including WOFF2 collections and the glyf, loca and hmtx transforms) to a plain font for Parse; IsWebFont tells the two apart and WithMaxFontSize bounds the unpacked size (default 64 MiB).
  • Hinting data: Face.GlyphDescription and Instance.GlyphDescription give a glyph’s points, contour ends, instructions and composite components, and Face.Hinting the fpgm, prep, cvt , gasp and maxp data a TrueType interpreter needs.
  • Shaping: WithGraphemeClusters makes clusters whole UAX #29 grapheme clusters (conjuncts included, GB9c); WithFeatureRanges, FeatureRange and ParseFeatureRange limit a feature to part of the text (liga[3:5]).
  • Fallback: FallbackChain.WithEmojiPresentation sends emoji sequences (variation selector 16, keycaps, ZWJ sequences, modifiers, flags) to the first colour font that covers them.
  • Dictionary line breaking: Segmenter.AppendLineBreaks allows 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.NoDictionary restores the rules of UAX #14 alone.

The package