1package text
2
3import (
4 "bytes"
5 "image"
6 "testing"
7
8 "github.com/go-text/typesetting/font"
9)
10
11func TestMarkAllDirty(t *testing.T) {
12 a, err := NewAtlas(16, 2)
13 if err != nil {
14 t.Fatal(err)
15 }
16 a.MarkAllDirty()
17 if got := a.Uploads(); len(got) != 0 {
18 t.Fatalf("empty atlas: %v", got)
19 }
20 m := Mask{Alpha: image.NewAlpha(image.Rect(0, 0, 2, 2))}
21 m.Alpha.Pix[0] = 123
22 if _, err := a.Insert(Key{Glyph: 1}, m); err != nil {
23 t.Fatal(err)
24 }
25 a.Uploads()
26 a.MarkAllDirty()
27 a.MarkAllDirty()
28 got := a.Uploads()
29 if len(got) != 1 || got[0].Page != 0 || got[0].Rect != image.Rect(0, 0, 16, 16) || !bytes.Equal(got[0].Bytes, a.pages[0].pixels) {
30 t.Fatalf("dirty snapshot: %v", got)
31 }
32 if got := a.Uploads(); len(got) != 0 {
33 t.Fatalf("not drained: %v", got)
34 }
35}
36
37// TestAtlasCachesEmptyGlyphs keeps spaces out of the per-frame raster path and
38// bounds their entries, which no page eviction removes.
39func TestAtlasCachesEmptyGlyphs(t *testing.T) {
40 a, err := NewAtlas(16, 1)
41 if err != nil {
42 t.Fatal(err)
43 }
44 space := Mask{Alpha: image.NewAlpha(image.Rect(0, 0, 0, 0)), Origin: image.Pt(0, -3)}
45 if p, err := a.Insert(Key{Glyph: 3}, space); err != nil || p.Page != -1 {
46 t.Fatalf("insert: %+v %v", p, err)
47 }
48 a.BeginFrame()
49 if p, ok := a.Lookup(Key{Glyph: 3}); !ok || p.Page != -1 || p.Origin != space.Origin {
50 t.Fatalf("lookup: %+v %v", p, ok)
51 }
52 // Evicting the only page for a new glyph must keep the empty entry.
53 ink := Mask{Alpha: image.NewAlpha(image.Rect(0, 0, 10, 10))}
54 for g := range 3 {
55 a.BeginFrame()
56 if _, err := a.Insert(Key{Glyph: font.GID(10 + g)}, ink); err != nil {
57 t.Fatal(err)
58 }
59 }
60 if _, ok := a.Lookup(Key{Glyph: 3}); !ok {
61 t.Fatal("page eviction dropped an empty glyph")
62 }
63 for g := range maxEmptyGlyphs + 10 {
64 if _, err := a.Insert(Key{Glyph: font.GID(100 + g)}, space); err != nil {
65 t.Fatal(err)
66 }
67 }
68 if a.empty > maxEmptyGlyphs || len(a.entries) > maxEmptyGlyphs+1 {
69 t.Fatalf("empty glyphs unbounded: %d counted, %d entries", a.empty, len(a.entries))
70 }
71}