/
cache_test.go
79 lines (66 loc) · 1.58 KB
/
cache_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package fontcache_test
import (
"fmt"
"log"
"testing"
"github.com/golang/freetype/truetype"
"github.com/stephenwithav/fontcache"
)
var fc fontcache.Cache
func init() {
fc = fontcache.New()
fc.Init("./fonts/")
}
func TestGet(t *testing.T) {
testcases := []struct {
name, style string
}{
{"Luxi Sans", "Regular"},
{"Luxi Mono", "Regular"},
{"Luxi Serif", "Regular"},
}
for _, tt := range testcases {
_, err := fc.Get(tt.name, tt.style)
if err != nil {
t.Errorf("error: %s", err)
}
}
}
func TestFallback(t *testing.T) {
testcases := []struct {
name, style string
}{
{"Luxi Sans", "Luxi Sans"},
{"Luxi Monooooooo", "Regular"},
{"Luxi Seriqzxf", "Regular"},
}
expectedName, expectedStyle := "Luxi Sans", "Regular"
fc.SetFallbackFont(expectedName, expectedStyle)
for _, tt := range testcases {
font, err := fc.Get(tt.name, tt.style)
if err != nil {
t.Errorf("error: %s", err)
} else {
if got := font.Name(truetype.NameIDFontFamily); got != expectedName {
t.Errorf("wanted familyName of %q, got %q", expectedName, got)
}
if got := font.Name(truetype.NameIDFontSubfamily); got != expectedStyle {
t.Errorf("wanted subfamilyName of %q, got %q", expectedStyle, got)
}
}
}
}
func Example() {
fontPath := "./fonts"
fc := fontcache.New()
fc.Init(fontPath)
// Retrieve font by name for use in a program.
font, err := fc.Get("Luxi Sans", "Regular")
if err != nil {
log.Fatalf("%s", err)
}
func(f *truetype.Font) {
fmt.Printf("Do something with %s", f.Name(truetype.NameIDFontFamily))
}(font)
// Output: Do something with Luxi Sans
}