package fonts import ( "bytes" "fmt" "sync" "github.com/go-text/typesetting/di" gofont "github.com/go-text/typesetting/font" "github.com/go-text/typesetting/language" "github.com/go-text/typesetting/shaping" "golang.org/x/image/math/fixed" ) // Shaping, so the PDF kerns and ligates the way the browser does // (CANDIDATES.md ยง5). Without it the exporter summed advance widths and // broke lines where the screen did not; with Carlito, whose kerning // pairs are many, that was a different line in most paragraphs. // // go-text/typesetting's HarfBuzz port applies the same default features // a browser does (kern, liga, clig, calt and the script's own). The // screen shapes word by word, splitting at spaces, unless a font kerns // against the space; Shaper is called per word for the same reason. // Glyph is one shaped glyph, in em units. type Glyph struct { ID uint16 // Advance is how far the pen moves after this glyph; XOffset shifts // the glyph itself (a mark on its base). Both are in ems. Advance, XOffset float64 // Text is the text this glyph stands for: one rune, several for a // ligature, or empty for the second glyph of a cluster. Text string } // Run is a shaped string. type Run struct { Glyphs []Glyph // Width is the sum of the advances, in ems. Width float64 // Missing is every rune the face has no glyph for. Missing []rune } var ( goFontsMu sync.Mutex goFonts = map[*Face]*gofont.Font{} ) // goFont parses a face for the shaper once per process. A *font.Font is // safe to share; the *font.Face wrapped around it is not. func (f *Face) goFont() (*gofont.Font, error) { goFontsMu.Lock() defer goFontsMu.Unlock() if ft, ok := goFonts[f]; ok { return ft, nil } face, err := gofont.ParseTTF(bytes.NewReader(f.Data)) if err != nil { return nil, fmt.Errorf("fonts: shape %s: %w", f.File, err) } goFonts[f] = face.Font return face.Font, nil } // Shaper shapes text. One per export: it reuses buffers and is not safe // for concurrent use. type Shaper struct { hb shaping.HarfbuzzShaper faces map[*Face]*gofont.Face } // NewShaper is a Shaper. func NewShaper() *Shaper { return &Shaper{faces: map[*Face]*gofont.Face{}} } // Shape shapes s, left to right, in face. func (sh *Shaper) Shape(face *Face, s string) (Run, error) { if s == "" { return Run{}, nil } gf, ok := sh.faces[face] if !ok { ft, err := face.goFont() if err != nil { return Run{}, err } gf = gofont.NewFace(ft) sh.faces[face] = gf } parsed, err := face.Parsed() if err != nil { return Run{}, err } upm := float64(parsed.UnitsPerEm) runes := []rune(s) script := language.Latin for _, r := range runes { if sc := language.LookupScript(r); sc != language.Common && sc != language.Inherited && sc != language.Unknown { script = sc break } } out := sh.hb.Shape(shaping.Input{ Text: runes, RunStart: 0, RunEnd: len(runes), Direction: di.DirectionLTR, Face: gf, // Font units: the advances come back in units, exact. Size: fixed.I(int(parsed.UnitsPerEm)), Script: script, Language: language.NewLanguage("en"), }) run := Run{Glyphs: make([]Glyph, 0, len(out.Glyphs))} lastCluster := -1 for _, g := range out.Glyphs { text := "" if g.ClusterIndex != lastCluster { end := g.ClusterIndex + g.RuneCount if end > len(runes) { end = len(runes) } text = string(runes[g.ClusterIndex:end]) lastCluster = g.ClusterIndex } gl := Glyph{ ID: uint16(g.GlyphID), Advance: float64(g.Advance) / 64 / upm, XOffset: float64(g.XOffset) / 64 / upm, Text: text, } if gl.ID == 0 { run.Missing = append(run.Missing, []rune(text)...) } run.Width += gl.Advance run.Glyphs = append(run.Glyphs, gl) } return run, nil }