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 export
import (
"strings"
"testing"
)
func TestPrimaryFamily(t *testing.T) {
cases := map[string]string{
"Awke": "Awke",
"Untitled Sans": "Untitled Sans",
"Inter, system-ui, sans-serif": "Inter",
`"Untitled Sans", sans-serif`: "Untitled Sans",
"'Maple Mono', ui-monospace": "Maple Mono",
" Spaced , x": "Spaced",
"": "",
"system-ui": "", // generic-only โ nothing to embed
"Georgia, serif": "Georgia",
}
for in, want := range cases {
if got := primaryFamily(in); got != want {
t.Errorf("primaryFamily(%q) = %q, want %q", in, got, want)
}
}
}
func TestWeightAndItalicOf(t *testing.T) {
cases := []struct {
sub string
weight int
italic bool
}{
{"Regular", 400, false},
{"Bold", 700, false},
{"Italic", 400, true},
{"Bold Italic", 700, true},
{"Thin", 100, false},
{"Light", 300, false},
{"Medium", 500, false},
{"SemiBold", 600, false},
{"Black", 900, false},
{"Bold Oblique", 700, true},
}
for _, c := range cases {
if w := weightOf(c.sub); w != c.weight {
t.Errorf("weightOf(%q) = %d, want %d", c.sub, w, c.weight)
}
if it := italicOf(c.sub); it != c.italic {
t.Errorf("italicOf(%q) = %v, want %v", c.sub, it, c.italic)
}
}
}
func TestFontFaceCSS(t *testing.T) {
f := fontFace{family: "Untitled Sans", weight: 700, italic: true, ext: ".otf", data: []byte("FAKE")}
css := f.faceCSS()
for _, want := range []string{
`font-family:"Untitled Sans"`,
"font-weight:700",
"font-style:italic",
`format("opentype")`,
"data:font/otf;base64,RkFLRQ==", // base64("FAKE")
} {
if !strings.Contains(css, want) {
t.Errorf("@font-face missing %q:\n%s", want, css)
}
}
}
func TestFontFaceCSSTruetype(t *testing.T) {
f := fontFace{family: "Maple Mono", weight: 400, italic: false, ext: ".ttf", data: []byte("x")}
css := f.faceCSS()
if !strings.Contains(css, `format("truetype")`) {
t.Errorf("ttf should be truetype format:\n%s", css)
}
if !strings.Contains(css, "data:font/ttf;base64,") {
t.Errorf("ttf mime wrong:\n%s", css)
}
}
|