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