| 1 | package fonts |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "sync" |
| 7 | |
| 8 | "github.com/go-text/typesetting/di" |
| 9 | gofont "github.com/go-text/typesetting/font" |
| 10 | "github.com/go-text/typesetting/language" |
| 11 | "github.com/go-text/typesetting/shaping" |
| 12 | "golang.org/x/image/math/fixed" |
| 13 | ) |
| 14 | |
| 15 | // Shaping, so the PDF kerns and ligates the way the browser does |
| 16 | // (CANDIDATES.md §5). Without it the exporter summed advance widths and |
| 17 | // broke lines where the screen did not; with Carlito, whose kerning |
| 18 | // pairs are many, that was a different line in most paragraphs. |
| 19 | // |
| 20 | // go-text/typesetting's HarfBuzz port applies the same default features |
| 21 | // a browser does (kern, liga, clig, calt and the script's own). The |
| 22 | // screen shapes word by word, splitting at spaces, unless a font kerns |
| 23 | // against the space; Shaper is called per word for the same reason. |
| 24 | |
| 25 | // Glyph is one shaped glyph, in em units. |
| 26 | type Glyph struct { |
| 27 | ID uint16 |
| 28 | // Advance is how far the pen moves after this glyph; XOffset shifts |
| 29 | // the glyph itself (a mark on its base). Both are in ems. |
| 30 | Advance, XOffset float64 |
| 31 | // Text is the text this glyph stands for: one rune, several for a |
| 32 | // ligature, or empty for the second glyph of a cluster. |
| 33 | Text string |
| 34 | } |
| 35 | |
| 36 | // Run is a shaped string. |
| 37 | type Run struct { |
| 38 | Glyphs []Glyph |
| 39 | // Width is the sum of the advances, in ems. |
| 40 | Width float64 |
| 41 | // Missing is every rune the face has no glyph for. |
| 42 | Missing []rune |
| 43 | } |
| 44 | |
| 45 | var ( |
| 46 | goFontsMu sync.Mutex |
| 47 | goFonts = map[*Face]*gofont.Font{} |
| 48 | ) |
| 49 | |
| 50 | // goFont parses a face for the shaper once per process. A *font.Font is |
| 51 | // safe to share; the *font.Face wrapped around it is not. |
| 52 | func (f *Face) goFont() (*gofont.Font, error) { |
| 53 | goFontsMu.Lock() |
| 54 | defer goFontsMu.Unlock() |
| 55 | if ft, ok := goFonts[f]; ok { |
| 56 | return ft, nil |
| 57 | } |
| 58 | face, err := gofont.ParseTTF(bytes.NewReader(f.Data)) |
| 59 | if err != nil { |
| 60 | return nil, fmt.Errorf("fonts: shape %s: %w", f.File, err) |
| 61 | } |
| 62 | goFonts[f] = face.Font |
| 63 | return face.Font, nil |
| 64 | } |
| 65 | |
| 66 | // Shaper shapes text. One per export: it reuses buffers and is not safe |
| 67 | // for concurrent use. |
| 68 | type Shaper struct { |
| 69 | hb shaping.HarfbuzzShaper |
| 70 | faces map[*Face]*gofont.Face |
| 71 | } |
| 72 | |
| 73 | // NewShaper is a Shaper. |
| 74 | func NewShaper() *Shaper { return &Shaper{faces: map[*Face]*gofont.Face{}} } |
| 75 | |
| 76 | // Shape shapes s, left to right, in face. |
| 77 | func (sh *Shaper) Shape(face *Face, s string) (Run, error) { |
| 78 | if s == "" { |
| 79 | return Run{}, nil |
| 80 | } |
| 81 | gf, ok := sh.faces[face] |
| 82 | if !ok { |
| 83 | ft, err := face.goFont() |
| 84 | if err != nil { |
| 85 | return Run{}, err |
| 86 | } |
| 87 | gf = gofont.NewFace(ft) |
| 88 | sh.faces[face] = gf |
| 89 | } |
| 90 | parsed, err := face.Parsed() |
| 91 | if err != nil { |
| 92 | return Run{}, err |
| 93 | } |
| 94 | upm := float64(parsed.UnitsPerEm) |
| 95 | runes := []rune(s) |
| 96 | script := language.Latin |
| 97 | for _, r := range runes { |
| 98 | if sc := language.LookupScript(r); sc != language.Common && sc != language.Inherited && sc != language.Unknown { |
| 99 | script = sc |
| 100 | break |
| 101 | } |
| 102 | } |
| 103 | out := sh.hb.Shape(shaping.Input{ |
| 104 | Text: runes, |
| 105 | RunStart: 0, |
| 106 | RunEnd: len(runes), |
| 107 | Direction: di.DirectionLTR, |
| 108 | Face: gf, |
| 109 | // Font units: the advances come back in units, exact. |
| 110 | Size: fixed.I(int(parsed.UnitsPerEm)), |
| 111 | Script: script, |
| 112 | Language: language.NewLanguage("en"), |
| 113 | }) |
| 114 | run := Run{Glyphs: make([]Glyph, 0, len(out.Glyphs))} |
| 115 | lastCluster := -1 |
| 116 | for _, g := range out.Glyphs { |
| 117 | text := "" |
| 118 | if g.ClusterIndex != lastCluster { |
| 119 | end := g.ClusterIndex + g.RuneCount |
| 120 | if end > len(runes) { |
| 121 | end = len(runes) |
| 122 | } |
| 123 | text = string(runes[g.ClusterIndex:end]) |
| 124 | lastCluster = g.ClusterIndex |
| 125 | } |
| 126 | gl := Glyph{ |
| 127 | ID: uint16(g.GlyphID), |
| 128 | Advance: float64(g.Advance) / 64 / upm, |
| 129 | XOffset: float64(g.XOffset) / 64 / upm, |
| 130 | Text: text, |
| 131 | } |
| 132 | if gl.ID == 0 { |
| 133 | run.Missing = append(run.Missing, []rune(text)...) |
| 134 | } |
| 135 | run.Width += gl.Advance |
| 136 | run.Glyphs = append(run.Glyphs, gl) |
| 137 | } |
| 138 | return run, nil |
| 139 | } |
| 140 | |