/
scriptlist.go
342 lines (304 loc) · 8.65 KB
/
scriptlist.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// seehuhn.de/go/sfnt - a library for reading and writing font files
// Copyright (C) 2022 Jochen Voss <voss@seehuhn.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package gtab
import (
"sort"
"golang.org/x/text/language"
"seehuhn.de/go/sfnt/parser"
)
// ScriptListInfo contains the information of an OpenType "ScriptList table".
// It maps BCP 47 tags to OpenType font features.
//
// https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#scriptList
type ScriptListInfo map[language.Tag]*Features
// Features describes the mandatory and optional features for a script/language.
type Features struct {
Required FeatureIndex // 0xFFFF, if no required feature
Optional []FeatureIndex
}
type scriptTableEntry struct {
script otfScript
offset uint16
}
// https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#script-list-table-and-script-record
func readScriptList(p *parser.Parser, pos int64) (ScriptListInfo, error) {
err := p.SeekPos(pos)
if err != nil {
return nil, err
}
scriptCount, err := p.ReadUint16()
if err != nil {
return nil, err
}
if 6*int64(scriptCount) > p.Size() {
return nil, &parser.InvalidFontError{
SubSystem: "sfnt/gtab",
Reason: "invalid scriptCount",
}
}
var entries []scriptTableEntry
for i := 0; i < int(scriptCount); i++ {
buf, err := p.ReadBytes(6)
if err != nil {
return nil, err
}
entry := scriptTableEntry{
script: otfScript(buf[:4]),
offset: uint16(buf[4])<<8 | uint16(buf[5]),
}
entries = append(entries, entry)
}
sort.Slice(entries, func(i, j int) bool {
return entries[i].offset < entries[j].offset
})
for _, entry := range entries {
if int(entry.offset) < 2+6*len(entries) {
return nil, &parser.InvalidFontError{
SubSystem: "sfnt/gtab",
Reason: "invalid script table offset",
}
}
}
info := ScriptListInfo{}
for _, entry := range entries {
err = info.readScriptTable(entry.script, p, pos+int64(entry.offset))
if err != nil {
return nil, err
}
}
return info, nil
}
// https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#script-table-and-language-system-record
func (info ScriptListInfo) readScriptTable(script otfScript, p *parser.Parser, pos int64) error {
err := p.SeekPos(pos)
if err != nil {
return err
}
data, err := p.ReadBytes(4)
if err != nil {
return err
}
defaultLangSysOffset := uint16(data[0])<<8 | uint16(data[1])
langSysCount := uint16(data[2])<<8 | uint16(data[3])
if defaultLangSysOffset > 0 && defaultLangSysOffset < 4+6*langSysCount {
return &parser.InvalidFontError{
SubSystem: "sfnt/gtab",
Reason: "invalid defaultLangSysOffset",
}
}
if 8+int64(langSysCount)*12 > p.Size() {
return &parser.InvalidFontError{
SubSystem: "sfnt/gtab",
Reason: "invalid langSysCount",
}
}
type langSysRecord struct {
lang otfLang
offset uint16
}
var records []langSysRecord
if defaultLangSysOffset != 0 {
records = append(records, langSysRecord{
offset: defaultLangSysOffset,
})
}
for i := 0; i < int(langSysCount); i++ {
buf, err := p.ReadBytes(6)
if err != nil {
return err
}
lang := otfLang(buf[:4])
records = append(records, langSysRecord{
lang: lang,
offset: uint16(buf[4])<<8 | uint16(buf[5]),
})
}
sort.Slice(records, func(i, j int) bool {
return records[i].offset < records[j].offset
})
for _, record := range records {
ff, err := readLangSysTable(p, pos+int64(record.offset))
if err != nil {
return err
}
tag, err := otfToBCP47(script, record.lang)
if err != nil {
// TODO(voss): what to do here?
continue
}
info[tag] = ff
}
return nil
}
// https://docs.microsoft.com/en-us/typography/opentype/spec/chapter2#language-system-table
func readLangSysTable(p *parser.Parser, pos int64) (*Features, error) {
err := p.SeekPos(pos)
if err != nil {
return nil, err
}
data, err := p.ReadBytes(6)
if err != nil {
return nil, err
}
lookupOrderOffset := uint16(data[0])<<8 | uint16(data[1])
requiredFeatureIndex := FeatureIndex(data[2])<<8 | FeatureIndex(data[3])
featureIndexCount := uint16(data[4])<<8 | uint16(data[5])
if lookupOrderOffset != 0 {
return nil, &parser.NotSupportedError{
SubSystem: "sfnt/gtab",
Feature: "use of reordering tables",
}
}
featureIndices := make([]FeatureIndex, featureIndexCount)
for i := 0; i < int(featureIndexCount); i++ {
idx, err := p.ReadUint16()
if err != nil {
return nil, err
} else if idx == 0xFFFF {
continue
}
featureIndices[i] = FeatureIndex(idx)
}
return &Features{
Required: requiredFeatureIndex,
Optional: featureIndices,
}, nil
}
func (info ScriptListInfo) encode() []byte {
if info == nil {
return nil
}
scriptLangs := map[otfScript][]language.Tag{}
for tag := range info {
script, _, err := bcp47ToOtf(tag)
if err != nil {
// TODO(voss): what to do here?
continue
}
scriptLangs[script] = append(scriptLangs[script], tag)
}
var scriptList []*scriptTableEntry
for otfScript := range scriptLangs {
scriptList = append(scriptList, &scriptTableEntry{
script: otfScript,
})
}
sort.Slice(scriptList, func(i, j int) bool {
return scriptList[i].script < scriptList[j].script
})
totalSize := 2 + 6*len(scriptLangs) // scriptCount, scriptRecords
for _, sRec := range scriptList {
sRec.offset = uint16(totalSize)
langCount := 0
for _, tag := range scriptLangs[sRec.script] {
_, lang, _ := bcp47ToOtf(tag)
if lang != "" {
langCount++
}
langSys := info[tag]
// lookupOrderOffset, requiredFeatureIndex, featureIndexCount, featureIndices:
totalSize += 6 + len(langSys.Optional)*2
}
totalSize += 4 + 6*langCount // defaultLangSysOffset, langSysCount, langSysRecords
}
buf := make([]byte, totalSize)
// write the ScriptList table
buf[0] = byte(len(scriptList) >> 8)
buf[1] = byte(len(scriptList))
for i, rec := range scriptList {
p := 2 + i*6
copy(buf[p:p+4], []byte(rec.script))
buf[p+4] = byte(rec.offset >> 8)
buf[p+5] = byte(rec.offset)
}
for _, sRec := range scriptList {
scriptTablePos := int(sRec.offset)
type langSysRecord struct {
langSys *Features
tag otfLang
offs uint16
}
var defaultRecord *langSysRecord
var langSysRecords []*langSysRecord
for _, tag := range scriptLangs[sRec.script] {
langSys := info[tag]
_, lang, _ := bcp47ToOtf(tag)
if lang == "" {
defaultRecord = &langSysRecord{
langSys: langSys,
}
continue
}
if len(lang) != 4 {
panic("invalid language " + string(lang))
}
langSysRecords = append(langSysRecords, &langSysRecord{
langSys: langSys,
tag: lang,
})
}
sort.Slice(langSysRecords, func(i, j int) bool {
return langSysRecords[i].tag < langSysRecords[j].tag
})
pos := 4 + 6*len(langSysRecords)
if defaultRecord != nil {
defaultRecord.offs = uint16(pos)
pos += 6 + len(defaultRecord.langSys.Optional)*2
}
for _, lRec := range langSysRecords {
lRec.offs = uint16(pos)
pos += 6 + len(lRec.langSys.Optional)*2
}
// write the Script table for sRec.tag
if defaultRecord != nil {
buf[scriptTablePos] = byte(defaultRecord.offs >> 8)
buf[scriptTablePos+1] = byte(defaultRecord.offs)
}
buf[scriptTablePos+2] = byte(len(langSysRecords) >> 8)
buf[scriptTablePos+3] = byte(len(langSysRecords))
for i, lRec := range langSysRecords {
p := scriptTablePos + 4 + i*6
copy(buf[p:p+4], []byte(lRec.tag))
buf[p+4] = byte(lRec.offs >> 8)
buf[p+5] = byte(lRec.offs)
}
if defaultRecord != nil {
p := scriptTablePos + int(defaultRecord.offs)
ff := defaultRecord.langSys
buf[p+2] = byte(ff.Required >> 8)
buf[p+3] = byte(ff.Required)
buf[p+4] = byte(len(ff.Optional) >> 8)
buf[p+5] = byte(len(ff.Optional))
for i, idx := range ff.Optional {
buf[p+6+2*i] = byte(idx >> 8)
buf[p+6+2*i+1] = byte(idx)
}
}
for _, lRec := range langSysRecords {
p := scriptTablePos + int(lRec.offs)
ff := lRec.langSys
buf[p+2] = byte(ff.Required >> 8)
buf[p+3] = byte(ff.Required)
buf[p+4] = byte(len(ff.Optional) >> 8)
buf[p+5] = byte(len(ff.Optional))
for i, idx := range ff.Optional {
buf[p+6+2*i] = byte(idx >> 8)
buf[p+6+2*i+1] = byte(idx)
}
}
}
return buf
}