1package text
2
3import (
4 "image"
5 "os"
6 "path/filepath"
7 "testing"
8
9 "github.com/go-text/typesetting/font"
10 ot "github.com/go-text/typesetting/font/opentype"
11)
12
13func TestVariableDefaultAndDistinctKeys(t *testing.T) {
14 c := fixture(t)
15 f := c.primary(Request{Families: []string{"Adwaita Sans"}})
16 if !f.Variable {
17 t.Fatal("fvar missing")
18 }
19 glyph, _ := f.Shape.NominalGlyph('H')
20 def := f.WithVariations(nil)
21 if len(def.Shape.Coords()) != 0 {
22 t.Fatal("expected implicit default coordinates")
23 }
24 explicit := f.WithVariations([]font.Variation{{Tag: ot.MustNewTag("wght"), Value: 400}})
25 heavy := f.WithVariations([]font.Variation{{Tag: ot.MustNewTag("wght"), Value: 700}})
26 keys := map[[32]byte]bool{}
27 for _, face := range []*Face{def, explicit, heavy} {
28 k, err := GlyphKey(face, glyph, 32, 0, face.Variations)
29 if err != nil {
30 t.Fatal(err)
31 }
32 m, err := Rasterize(face, k)
33 if err != nil || m.Alpha.Rect.Empty() {
34 t.Fatalf("coords %v raster: %v", face.Shape.Coords(), err)
35 }
36 keys[k.Variations] = true
37 if _, err := Rasterize(face, Key{FaceID: face.ID, Glyph: glyph, Size: k.Size, Variations: VariationHash([]font.Variation{{Tag: ot.MustNewTag("wght"), Value: 300}})}); err == nil {
38 t.Error("accepted wrong key")
39 }
40 }
41 if len(keys) != 3 {
42 t.Fatalf("variation keys collapsed: %d", len(keys))
43 }
44}
45
46func TestClusterFallback(t *testing.T) {
47 c := fixture(t)
48 choices := c.candidates(Request{Families: []string{"Noto Sans"}})
49 cases := []string{"a\u1ab0", "क\u093f", "س\u064e", "♥\ufe0f", "👩\u200d💻"}
50 for _, s := range cases {
51 t.Run(s, func(t *testing.T) {
52 r := []rune(s)
53 faces := clusterFaces(r, choices)
54 if len(faces) != len(r) {
55 t.Fatalf("%d faces", len(faces))
56 }
57 for i := 1; i < len(faces); i++ {
58 if faces[i] != faces[0] {
59 t.Fatalf("cluster split at %d", i)
60 }
61 }
62 l, err := NewEngine(c).Measure(s, Request{Families: []string{"Noto Sans"}, Size: 20}, 0)
63 if err != nil || len(l.Lines) == 0 {
64 t.Fatalf("measure: %v", err)
65 }
66 })
67 }
68}
69
70func TestSourceDiagnostics(t *testing.T) {
71 b, err := os.ReadFile("../../testdata/fonts/NotoSans-Regular.ttf")
72 if err != nil {
73 t.Fatal(err)
74 }
75 c, err := Load(sourceMap{"a.ttf": []byte("corrupt"), "b.ttf": b})
76 if err != nil || len(c.Faces) != 1 || len(c.Diagnostics) != 1 {
77 t.Fatalf("partial load: %v %+v", err, c)
78 }
79 if os.Geteuid() == 0 {
80 t.Skip("root bypasses file permissions")
81 }
82 root := t.TempDir()
83 good, bad := filepath.Join(root, "good"), filepath.Join(root, "bad")
84 if err := os.Mkdir(good, 0700); err != nil {
85 t.Fatal(err)
86 }
87 if err := os.Mkdir(bad, 0700); err != nil {
88 t.Fatal(err)
89 }
90 if err := os.WriteFile(filepath.Join(good, "NotoSans-Regular.ttf"), b, 0600); err != nil {
91 t.Fatal(err)
92 }
93 if err := os.Chmod(bad, 0000); err != nil {
94 t.Fatal(err)
95 }
96 t.Cleanup(func() { _ = os.Chmod(bad, 0700) })
97 // A failing directory still returns a diagnostic along with the readable files.
98 files, e := DirectorySource(bad).Fonts()
99 if e == nil || len(files) != 0 {
100 t.Fatalf("unreadable dir: %v", e)
101 }
102 all, e := DirectorySource(root).Fonts()
103 if e == nil {
104 t.Fatal("missing diagnostic")
105 }
106 loaded, err := Load(sourceMapWithError{all, e})
107 if err != nil || len(loaded.Faces) != 1 || len(loaded.Diagnostics) != 1 {
108 t.Fatalf("partial scan: %v %+v", err, loaded)
109 }
110}
111
112type sourceMapWithError struct {
113 files map[string][]byte
114 err error
115}
116
117func (s sourceMapWithError) Fonts() (map[string][]byte, error) { return s.files, s.err }
118
119func TestAtlasUndrainedBound(t *testing.T) {
120 a, _ := NewAtlas(32, 2)
121 m := Mask{Alpha: image.NewAlpha(image.Rect(0, 0, 11, 11))}
122 m.Alpha.Pix[0] = 255
123 for i := 0; i < 120; i++ {
124 a.BeginFrame()
125 for j := 0; j < 8; j++ {
126 if _, err := a.Insert(Key{Glyph: font.GID(8*i + j + 1)}, m); err != nil {
127 t.Fatal(err)
128 }
129 }
130 if len(a.dirty) > a.MaxPages {
131 t.Fatal("unbounded pending pages")
132 }
133 }
134 up := a.Uploads()
135 if len(up) > 2 {
136 t.Fatalf("pending records %d", len(up))
137 }
138 total := 0
139 for _, u := range up {
140 total += len(u.Bytes)
141 if len(u.Bytes) != u.Rect.Dx()*u.Rect.Dy() {
142 t.Fatal("invalid upload stride")
143 }
144 }
145 if total > 2*32*32 {
146 t.Fatalf("pending bytes %d", total)
147 }
148 if len(a.Uploads()) != 0 {
149 t.Fatal("not drained")
150 }
151}
152
153func TestArabicJoiningAndBidi(t *testing.T) {
154 c := fixture(t)
155 e := NewEngine(c)
156 r := Request{Families: []string{"Noto Sans Arabic"}, Size: 20}
157 joined, err := e.Measure("سلام", r, 0)
158 if err != nil {
159 t.Fatal(err)
160 }
161 alone := map[font.GID]bool{}
162 for _, letter := range "سلام" {
163 l, err := e.Measure(string(letter), r, 0)
164 if err != nil {
165 t.Fatal(err)
166 }
167 for _, run := range l.Lines[0].Runs {
168 for _, g := range run.Glyphs {
169 alone[g.ID] = true
170 }
171 }
172 }
173 changed := false
174 for _, run := range joined.Lines[0].Runs {
175 for _, g := range run.Glyphs {
176 if !alone[g.ID] {
177 changed = true
178 }
179 }
180 }
181 if !changed {
182 t.Fatal("no joined glyph differs from isolated letters")
183 }
184}
185
186func TestDevanagariReordering(t *testing.T) {
187 c := fixture(t)
188 f := c.primary(Request{Families: []string{"Noto Sans Devanagari"}})
189 ka, _ := f.Shape.NominalGlyph('क')
190 // OpenType Indic substitution creates a pre-base matra form not found in cmap.
191 matra, _ := f.Shape.NominalGlyph('ि')
192 l, err := NewEngine(c).Measure("कि", Request{Families: []string{"Noto Sans Devanagari"}, Size: 24}, 0)
193 if err != nil {
194 t.Fatal(err)
195 }
196 var ids []font.GID
197 for _, r := range l.Lines[0].Runs {
198 for _, g := range r.Glyphs {
199 ids = append(ids, g.ID)
200 }
201 }
202 if len(ids) != 2 || ids[0] == ka || ids[1] != ka || ids[0] == 0 || ids[0] == matra {
203 t.Fatalf("expected substituted pre-base i-matra before ka: got %v, nominal matra %d ka %d", ids, matra, ka)
204 }
205}
206func BenchmarkAtlasWarmLookup(b *testing.B) {
207 a, _ := NewAtlas(64, 1)
208 k := Key{Glyph: 23}
209 if _, err := a.Insert(k, Mask{Alpha: image.NewAlpha(image.Rect(0, 0, 8, 8))}); err != nil {
210 b.Fatal(err)
211 }
212 a.Uploads()
213 b.ReportAllocs()
214 b.ResetTimer()
215 for i := 0; i < b.N; i++ {
216 if _, ok := a.Lookup(k); !ok {
217 b.Fatal("cache miss")
218 }
219 }
220}
221func BenchmarkRasterDistinct1000(b *testing.B) {
222 c := fixture(b)
223 f := c.primary(Request{Families: []string{"Noto Sans"}})
224 var keys []Key
225 for r := rune(33); r < rune(5000) && len(keys) < 1000; r++ {
226 gid, ok := f.Shape.NominalGlyph(r)
227 if !ok || gid == 0 {
228 continue
229 }
230 k, _ := GlyphKey(f, gid, 16, 0, nil)
231 keys = append(keys, k)
232 }
233 if len(keys) < 1000 {
234 b.Fatalf("only %d distinct glyphs", len(keys))
235 }
236 b.ResetTimer()
237 for i := 0; i < b.N; i++ {
238 for _, k := range keys {
239 if _, err := Rasterize(f, k); err != nil {
240 b.Fatal(err)
241 }
242 }
243 }
244}