oficina / docs Public

Clone
git clone https://amadan.net/oficina/docs

Plain git — no account needed to clone.

Download

Download this file

1package fonts_test
2
3import (
4 "encoding/json"
5 "math"
6 "os"
7 "testing"
8
9 "github.com/carlosframework/docs/internal/fonts"
10)
11
12// The PDF must kern the way the browser does, or its lines break in
13// different places (Paul's default 6). Carlito kerns "AV" and "To"; a
14// shaped run is narrower than the sum of its glyphs' own advances.
15func TestShapingKerns(t *testing.T) {
16 face := family(t, "carlito").Face(false, false)
17 font, err := face.Parsed()
18 if err != nil {
19 t.Fatal(err)
20 }
21 sh := fonts.NewShaper()
22 for _, s := range []string{"AV", "To", "Wa"} {
23 run, err := sh.Shape(face, s)
24 if err != nil {
25 t.Fatal(err)
26 }
27 nominal, _ := font.WidthEm(s)
28 if run.Width >= nominal-0.01 {
29 t.Errorf("%q shaped to %.4f em, unkerned %.4f em: no kerning", s, run.Width, nominal)
30 }
31 text := ""
32 for _, g := range run.Glyphs {
33 text += g.Text
34 }
35 if text != s {
36 t.Errorf("%q: the glyphs say %q, so the PDF's text would copy out wrong", s, text)
37 }
38 }
39}
40
41// A ligature is one glyph standing for several characters, and the
42// text that copies out of the PDF must still be every character.
43func TestShapingLigatesAndKeepsTheText(t *testing.T) {
44 for _, f := range fonts.Families {
45 face := f.Face(false, false)
46 run, err := fonts.NewShaper().Shape(face, "office")
47 if err != nil {
48 t.Fatal(err)
49 }
50 text := ""
51 for _, g := range run.Glyphs {
52 text += g.Text
53 }
54 if text != "office" {
55 t.Errorf("%s: %q copies out as %q", f.Name, "office", text)
56 }
57 if len(run.Missing) > 0 {
58 t.Errorf("%s is missing %q", f.Name, run.Missing)
59 }
60 }
61}
62
63// The PDF's shaper sets every word the way HarfBuzz, the browser's
64// shaper, does, in every face Docs ships: the advances are equal to the
65// font unit. testdata/harfbuzz.json is HarfBuzz's own answer
66// (scripts/harfbuzz-golden.py). go-text/typesetting v0.3.5 failed this
67// for Inter, applying none of its kerning, so the PDF broke Inter lines
68// a word early.
69func TestShapingMatchesHarfBuzz(t *testing.T) {
70 raw, err := os.ReadFile("testdata/harfbuzz.json")
71 if err != nil {
72 t.Fatal(err)
73 }
74 var golden struct {
75 Faces map[string]map[string]int `json:"faces"`
76 }
77 if err := json.Unmarshal(raw, &golden); err != nil {
78 t.Fatal(err)
79 }
80 if len(golden.Faces) != len(fonts.All()) {
81 t.Fatalf("the golden file covers %d faces, Docs ships %d: rerun scripts/harfbuzz-golden.py", len(golden.Faces), len(fonts.All()))
82 }
83 sh := fonts.NewShaper()
84 for _, face := range fonts.All() {
85 words, ok := golden.Faces[face.File]
86 if !ok {
87 t.Errorf("%s is not in the golden file", face.File)
88 continue
89 }
90 font, err := face.Parsed()
91 if err != nil {
92 t.Fatal(err)
93 }
94 for w, want := range words {
95 run, err := sh.Shape(face, w)
96 if err != nil {
97 t.Fatal(err)
98 }
99 if got := int(math.Round(run.Width * float64(font.UnitsPerEm))); got != want {
100 t.Errorf("%s %q: the PDF sets it %d units wide, HarfBuzz %d", face.File, w, got, want)
101 }
102 }
103 }
104}
105