-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtable_feat_test.go
319 lines (279 loc) · 8.27 KB
/
table_feat_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package graphite
import (
"bytes"
"encoding/binary"
"fmt"
"strings"
"testing"
testdata "github.com/benoitkugler/textlayout-testdata/graphite"
"github.com/benoitkugler/textlayout/fonts/truetype"
)
// adapted from graphite/tests/featuremap/featuremaptest.cpp
type featHeader struct {
major uint16
minor uint16
numFeat uint16
_ uint16
_ uint32
}
type featDefn struct {
featId Tag
numFeatSettings uint16
_ uint16
settingsOffset uint32
flags uint16
label truetype.NameID
}
type featSetting struct {
value int16
label truetype.NameID
}
const (
featHeaderSize = 12
featDefnSize = 16
featSettingSize = 4
)
type featTableTest struct {
defs []featDefn
setting []featSetting
header featHeader
}
type featTableTestA struct {
header featHeader
defs [1]featDefn
settings [2]featSetting
}
func (ct featTableTestA) asFeatTableTest() featTableTest {
return featTableTest{
header: ct.header,
defs: ct.defs[:],
setting: ct.settings[:],
}
}
var testDataA = featTableTestA{
featHeader{2, 0, 1, 0, 0},
[1]featDefn{{0x41424344, 2, 0, featHeaderSize + featDefnSize, 0, 1}},
[2]featSetting{{0, 10}, {1, 11}},
}
type featTableTestB struct {
header featHeader
defs [2]featDefn
settings [4]featSetting
}
func (ct featTableTestB) asFeatTableTest() featTableTest {
return featTableTest{
header: ct.header,
defs: ct.defs[:],
setting: ct.settings[:],
}
}
var testDataB = featTableTestB{
featHeader{2, 0, 2, 0, 0},
[2]featDefn{
{0x41424344, 2, 0, featHeaderSize + 2*featDefnSize, 0, 1},
{0x41424345, 2, 0, featHeaderSize + 2*featDefnSize + 2*featSettingSize, 0, 2},
},
[4]featSetting{{0, 10}, {1, 11}, {0, 12}, {1, 13}},
}
var testDataBunsorted = featTableTestB{
featHeader{2, 0, 2, 0, 0},
[2]featDefn{
{0x41424345, 2, 0, featHeaderSize + 2*featDefnSize + 2*featSettingSize, 0, 2},
{0x41424344, 2, 0, featHeaderSize + 2*featDefnSize, 0, 1},
},
[4]featSetting{{0, 10}, {1, 11}, {0, 12}, {1, 13}},
}
type featTableTestC struct {
header featHeader
defs [3]featDefn
settings [7]featSetting
}
func (ct featTableTestC) asFeatTableTest() featTableTest {
return featTableTest{
header: ct.header,
defs: ct.defs[:],
setting: ct.settings[:],
}
}
var testDataCunsorted = featTableTestC{
featHeader{2, 0, 3, 0, 0},
[3]featDefn{
{0x41424343, 3, 0, featHeaderSize + 3*featDefnSize + 4*featSettingSize, 0, 1},
{0x41424345, 2, 0, featHeaderSize + 3*featDefnSize + 2*featSettingSize, 0, 3},
{0x41424344, 2, 0, featHeaderSize + 3*featDefnSize, 0, 2},
},
[7]featSetting{{0, 10}, {1, 11}, {0, 12}, {1, 13}, {0, 14}, {1, 15}, {2, 16}},
}
type featTableTestD struct {
header featHeader
defs [4]featDefn
settings [9]featSetting
}
func (ct featTableTestD) asFeatTableTest() featTableTest {
return featTableTest{
header: ct.header,
defs: ct.defs[:],
setting: ct.settings[:],
}
}
var testDataDunsorted = featTableTestD{
featHeader{2, 0, 4, 0, 0},
[4]featDefn{
{400, 3, 0, featHeaderSize + 4*featDefnSize + 4*featSettingSize, 0, 1},
{100, 2, 0, featHeaderSize + 4*featDefnSize + 2*featSettingSize, 0, 3},
{300, 2, 0, featHeaderSize + 4*featDefnSize, 0, 2},
{200, 2, 0, featHeaderSize + 4*featDefnSize + 7*featSettingSize, 0, 2},
},
[9]featSetting{{0, 10}, {1, 11}, {0, 12}, {10, 13}, {0, 14}, {1, 15}, {2, 16}, {2, 17}, {4, 18}},
}
type featTableTestE struct {
header featHeader
defs [5]featDefn
settings [11]featSetting
}
func (ct featTableTestE) asFeatTableTest() featTableTest {
return featTableTest{
header: ct.header,
defs: ct.defs[:],
setting: ct.settings[:],
}
}
var testDataE = featTableTestE{
featHeader{2, 0, 5, 0, 0},
[5]featDefn{
{400, 3, 0, featHeaderSize + 5*featDefnSize + 4*featSettingSize, 0, 1},
{100, 2, 0, featHeaderSize + 5*featDefnSize + 2*featSettingSize, 0, 3},
{500, 2, 0, featHeaderSize + 5*featDefnSize + 9*featSettingSize, 0, 3},
{300, 2, 0, featHeaderSize + 5*featDefnSize, 0, 2},
{200, 2, 0, featHeaderSize + 5*featDefnSize + 7*featSettingSize, 0, 2},
},
[11]featSetting{{0, 10}, {1, 11}, {0, 12}, {10, 13}, {0, 14}, {1, 15}, {2, 16}, {2, 17}, {4, 18}, {1, 19}, {2, 20}},
}
var testBadOffset = featTableTestE{
featHeader{2, 0, 5, 0, 0},
[5]featDefn{
{400, 3, 0, featHeaderSize + 5*featDefnSize + 4*featSettingSize, 0, 1},
{100, 2, 0, featHeaderSize + 5*featDefnSize + 2*featSettingSize, 0, 3},
{500, 2, 0, featHeaderSize + 5*featDefnSize + 9*featSettingSize, 0, 3},
{300, 2, 0, featHeaderSize + 5*featDefnSize, 0, 2},
{200, 2, 0, featHeaderSize + 5*featDefnSize + 10*featSettingSize, 0, 2},
},
[11]featSetting{{0, 10}, {1, 11}, {0, 12}, {10, 13}, {0, 14}, {1, 15}, {2, 16}, {2, 17}, {4, 18}, {1, 19}, {2, 20}},
}
func asBinary(data interface{}) []byte {
var buf bytes.Buffer
_ = binary.Write(&buf, binary.BigEndian, data)
return buf.Bytes()
}
func testOneParseFeat(t *testing.T, data interface{ asFeatTableTest() featTableTest }) {
inputData := asBinary(data)
out, err := parseTableFeat(inputData)
if err != nil {
t.Fatalf("unexpected error on table feat: %s", err)
}
expected := data.asFeatTableTest()
if len(out) != int(expected.header.numFeat) {
t.Fatalf("expected %d features, got %d", expected.header.numFeat, len(out))
}
for _, def := range expected.defs {
feat, ok := out.findFeature(def.featId)
if !ok {
t.Fatalf("feature %d not found", def.featId)
}
if feat.label != def.label {
t.Fatalf("expected %d, got %d", def.label, feat.label)
}
if len(feat.settings) != int(def.numFeatSettings) {
t.Fatalf("expected %d feature settings, got %d", int(def.numFeatSettings), len(feat.settings))
}
settingIndex := int((def.settingsOffset - featHeaderSize - featDefnSize*uint32(expected.header.numFeat)) / featSettingSize)
for j, gotFeat := range feat.settings {
if exp := expected.setting[settingIndex+j].label; gotFeat.Label != exp {
t.Fatalf("expected %d, got %d", exp, gotFeat.Label)
}
}
}
}
func TestParseTableFeat(t *testing.T) {
testOneParseFeat(t, testDataA)
testOneParseFeat(t, testDataB)
testOneParseFeat(t, testDataBunsorted)
testOneParseFeat(t, testDataCunsorted)
testOneParseFeat(t, testDataDunsorted)
testOneParseFeat(t, testDataE)
badInput := asBinary(testBadOffset)
if _, err := parseTableFeat(badInput); err == nil {
t.Fatalf("expected error on bad input")
}
}
func dumpFeatures(ft *GraphiteFace) []byte {
var buf bytes.Buffer
fmt.Fprintf(&buf, "%d features\n", len(ft.feat))
for _, feat := range ft.feat {
label := ft.names.SelectEntry(feat.label)
if label != nil {
if (byte(feat.id>>24) >= 0x20 && byte(feat.id>>24) < 0x7F) &&
(byte(feat.id>>16) >= 0x20 && byte(feat.id>>16) < 0x7F) &&
(byte(feat.id>>8) >= 0x20 && byte(feat.id>>8) < 0x7F) &&
(byte(feat.id) >= 0x20 && byte(feat.id) < 0x7F) {
fmt.Fprintf(&buf, "%d %c%c%c%c %s\n", feat.id, byte(feat.id>>24), byte(feat.id>>16), byte(feat.id>>8), byte(feat.id), label)
} else {
fmt.Fprintf(&buf, "%d %s\n", feat.id, label)
}
} else {
fmt.Fprintf(&buf, "%d\n", feat.id)
}
for _, setting := range feat.settings {
labelName := ""
if label := ft.names.SelectEntry(setting.Label); label != nil {
labelName = label.String()
}
fmt.Fprintf(&buf, "\t%d\t%s\n", setting.Value, labelName)
}
}
fmt.Fprintf(&buf, "Feature Languages:")
for _, lang := range ft.sill {
fmt.Fprintf(&buf, "\t")
for j := 4; j != 0; j-- {
c := byte(lang.langcode >> (j*8 - 8))
if (c >= 0x20) && (c < 0x80) {
fmt.Fprintf(&buf, "%c", c)
}
}
}
buf.WriteString("\n")
return buf.Bytes()
}
func TestFindFeatures(t *testing.T) {
// compare with graphite log output
fonts := []string{
"charis.ttf",
"Padauk.ttf",
"Scheherazadegr.ttf",
}
for _, filename := range fonts {
expected, err := testdata.Files.ReadFile(strings.TrimSuffix(filename, ".ttf") + "_feat.log")
if err != nil {
t.Fatal(err)
}
ft := loadGraphite(t, filename)
got := dumpFeatures(ft)
if !bytes.Equal(expected, got) {
t.Fatalf("expected \n%s\n got \n%s", expected, got)
}
}
}
func TestGetFeature(t *testing.T) {
ft := loadGraphite(t, "Padauk.ttf")
feats := ft.FeaturesForLang(0)
if len(feats) != 11 {
t.Fatal("wrong number of default features")
}
dotc := truetype.MustNewTag("dotc")
if ft.feat[6].id != dotc {
t.Fatal("expected dotc")
}
if feats.FindFeature(dotc) == nil {
t.Fatal("feature not found")
}
}