-
Notifications
You must be signed in to change notification settings - Fork 7
/
table_gsub.go
459 lines (403 loc) · 14.2 KB
/
table_gsub.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package truetype
import (
"encoding/binary"
"errors"
"fmt"
)
// TableGSUB is the Glyph Substitution (GSUB) table.
// It provides data for substition of glyphs for appropriate rendering of scripts,
// such as cursively-connecting forms in Arabic script,
// or for advanced typographic effects, such as ligatures.
type TableGSUB struct {
Lookups []LookupGSUB
TableLayout
}
func parseTableGSUB(data []byte) (out TableGSUB, err error) {
tableLayout, lookups, err := parseTableLayout(data)
if err != nil {
return out, err
}
out = TableGSUB{
TableLayout: tableLayout,
Lookups: make([]LookupGSUB, len(lookups)),
}
for i, l := range lookups {
out.Lookups[i], err = l.parseGSUB(uint16(len(lookups)))
if err != nil {
return out, err
}
}
return out, nil
}
// GSUBType identifies the kind of lookup format, for GSUB tables.
type GSUBType uint16
const (
GSUBSingle GSUBType = 1 + iota // Single (format 1.1 1.2) Replace one glyph with one glyph
GSUBMultiple // Multiple (format 2.1) Replace one glyph with more than one glyph
GSUBAlternate // Alternate (format 3.1) Replace one glyph with one of many glyphs
GSUBLigature // Ligature (format 4.1) Replace multiple glyphs with one glyph
GSUBContext // Context (format 5.1 5.2 5.3) Replace one or more glyphs in context
GSUBChaining // Chaining Context (format 6.1 6.2 6.3) Replace one or more glyphs in chained context
// Extension Substitution (format 7.1) Extension mechanism for other substitutions
// The table pointed at by this lookup is returned instead.
gsubExtension
GSUBReverse // Reverse chaining context single (format 8.1)
)
// GSUBSubtable is one of the subtables of a
// GSUB lookup.
type GSUBSubtable struct {
// For SubChaining - Format 3, its the coverage of the first input.
Coverage Coverage
Data interface{ Type() GSUBType }
}
// LookupGSUB is a lookup for GSUB tables.
type LookupGSUB struct {
// After successful parsing, all subtables have the `GSUBType`.
Subtables []GSUBSubtable
LookupOptions
Type GSUBType
}
// interpret the lookup as a GSUB lookup
// lookupLength is used to sanitize nested lookups
func (header lookup) parseGSUB(lookupListLength uint16) (out LookupGSUB, err error) {
out.Type = GSUBType(header.kind)
out.LookupOptions = header.LookupOptions
out.Subtables = make([]GSUBSubtable, len(header.subtableOffsets))
for i, offset := range header.subtableOffsets {
out.Subtables[i], err = parseGSUBSubtable(header.data, int(offset), out.Type, lookupListLength)
if err != nil {
return out, err
}
}
return out, nil
}
func parseGSUBSubtable(data []byte, offset int, kind GSUBType, lookupListLength uint16) (out GSUBSubtable, err error) {
// read the format and coverage
if offset+4 >= len(data) {
return out, fmt.Errorf("invalid lookup subtable offset %d", offset)
}
format := binary.BigEndian.Uint16(data[offset:])
// almost all table have a coverage offset, right after the format; special case the others
// see below for the coverage
if kind == gsubExtension || (kind == GSUBChaining || kind == GSUBContext) && format == 3 {
out.Coverage = CoverageList{}
} else {
covOffset := uint32(binary.BigEndian.Uint16(data[offset+2:])) // relative to the subtable
out.Coverage, err = parseCoverage(data[offset:], covOffset)
if err != nil {
return out, fmt.Errorf("invalid GSUB table (format %d-%d): %s", kind, format, err)
}
}
// read the actual lookup
switch kind {
case GSUBSingle:
out.Data, err = parseSingleSub(format, data[offset:])
case GSUBMultiple:
out.Data, err = parseMultipleSub(data[offset:], out.Coverage)
case GSUBAlternate:
out.Data, err = parseAlternateSub(data[offset:], out.Coverage)
case GSUBLigature:
out.Data, err = parseLigatureSub(data[offset:], out.Coverage)
case GSUBContext:
out.Data, err = parseSequenceContextSub(format, data[offset:], lookupListLength, &out.Coverage)
case GSUBChaining:
out.Data, err = parseChainedSequenceContextSub(format, data[offset:], lookupListLength, &out.Coverage)
case gsubExtension:
out, err = parseExtensionSub(data[offset:], lookupListLength)
case GSUBReverse:
out.Data, err = parseReverseChainedSequenceContextSub(data[offset:], out.Coverage)
default:
return out, fmt.Errorf("unsupported gsub lookup type %d", kind)
}
return out, err
}
func (GSUBSingle1) Type() GSUBType { return GSUBSingle }
func (GSUBSingle2) Type() GSUBType { return GSUBSingle }
// data starts at the subtable (but format has already been read)
func parseSingleSub(format uint16, data []byte) (out interface{ Type() GSUBType }, err error) {
switch format {
case 1:
return parseSingleSub1(data)
case 2:
return parseSingleSub2(data)
default:
return nil, fmt.Errorf("unsupported single substitution format: %d", format)
}
}
// GSUBSingle1 is a Single Substitution Format 1, expressed as a delta
// from the coverage.
type GSUBSingle1 int16
// data is at the begining of the subtable
func parseSingleSub1(data []byte) (GSUBSingle1, error) {
if len(data) < 6 {
return 0, errors.New("invalid single subsitution table (format 1)")
}
// format and coverage already read
delta := GSUBSingle1(binary.BigEndian.Uint16(data[4:]))
return delta, nil
}
// GSUBSingle2 is a Single Substitution Format 2, expressed as substitutes
type GSUBSingle2 []GID
// data is at the begining of the subtable
func parseSingleSub2(data []byte) (GSUBSingle2, error) {
if len(data) < 6 {
return nil, errors.New("invalid single subsitution table (format 2)")
}
// format and coverage already read
glyphCount := binary.BigEndian.Uint16(data[4:])
if len(data) < 6+int(glyphCount)*2 {
return nil, errors.New("invalid single subsitution table (format 2)")
}
out := make(GSUBSingle2, glyphCount)
for i := range out {
out[i] = GID(binary.BigEndian.Uint16(data[6+2*i:]))
}
return out, nil
}
type GSUBMultiple1 [][]GID
func (GSUBMultiple1) Type() GSUBType { return GSUBMultiple }
// data starts at the subtable (but format has already been read)
func parseMultipleSub(data []byte, cov Coverage) (GSUBMultiple1, error) {
if len(data) < 6 {
return nil, errors.New("invalid multiple subsitution table")
}
// format and coverage already processed
count := binary.BigEndian.Uint16(data[4:])
// check length conformance
if cov.Size() != int(count) {
return nil, errors.New("invalid multiple subsitution table")
}
if 6+int(count)*2 > len(data) {
return nil, fmt.Errorf("invalid multiple subsitution table")
}
out := make(GSUBMultiple1, count)
var err error
for i := range out {
offset := binary.BigEndian.Uint16(data[6+2*i:])
if int(offset) > len(data) {
return out, errors.New("invalid multiple subsitution table")
}
out[i], err = parseMultipleSet(data[offset:])
if err != nil {
return out, err
}
}
return out, nil
}
func parseMultipleSet(data []byte) ([]GID, error) {
if len(data) < 2 {
return nil, errors.New("invalid multiple subsitution table")
}
count := binary.BigEndian.Uint16(data)
if 2+int(count)*2 > len(data) {
return nil, fmt.Errorf("invalid multiple subsitution table")
}
out := make([]GID, count)
for i := range out {
out[i] = GID(binary.BigEndian.Uint16(data[2+2*i:]))
}
return out, nil
}
type GSUBAlternate1 [][]GID
func (GSUBAlternate1) Type() GSUBType { return GSUBAlternate }
// data starts at the subtable (but format has already been read)
func parseAlternateSub(data []byte, cov Coverage) (GSUBAlternate1, error) {
out, err := parseMultipleSub(data, cov)
if err != nil {
return nil, errors.New("invalid alternate substitution table")
}
return GSUBAlternate1(out), nil
}
// GSUBLigature1 stores one ligature set per glyph in the coverage.
type GSUBLigature1 [][]LigatureGlyph
func (GSUBLigature1) Type() GSUBType { return GSUBLigature }
func parseLigatureSub(data []byte, cov Coverage) (GSUBLigature1, error) {
if len(data) < 6 {
return nil, errors.New("invalid ligature subsitution table")
}
// format and coverage already processed
count := binary.BigEndian.Uint16(data[4:])
// check length conformance
if cov.Size() != int(count) {
return nil, errors.New("invalid ligature subsitution table")
}
if 6+int(count)*2 > len(data) {
return nil, fmt.Errorf("invalid ligature subsitution table")
}
out := make([][]LigatureGlyph, count)
var err error
for i := range out {
ligSetOffset := binary.BigEndian.Uint16(data[6+2*i:])
if int(ligSetOffset) > len(data) {
return out, errors.New("invalid ligature subsitution table")
}
out[i], err = parseLigatureSet(data[ligSetOffset:])
if err != nil {
return out, err
}
}
return out, nil
}
type LigatureGlyph struct {
// Glyphs composing the ligature, starting after the
// implicit first glyph, given in the coverage of the
// SubstitutionLigature table
Components []uint16
// Output ligature glyph
Glyph GID
}
// Matches tests if the ligature should be applied on `glyphsFromSecond`,
// which starts from the second glyph.
func (l LigatureGlyph) Matches(glyphsFromSecond []GID) bool {
if len(glyphsFromSecond) != len(l.Components) {
return false
}
for i, g := range glyphsFromSecond {
if g != GID(l.Components[i]) {
return false
}
}
return true
}
// data is at the begining of the ligature set table
func parseLigatureSet(data []byte) ([]LigatureGlyph, error) {
if len(data) < 2 {
return nil, errors.New("invalid ligature set table")
}
count := binary.BigEndian.Uint16(data)
out := make([]LigatureGlyph, count)
var err error
for i := range out {
ligOffset := binary.BigEndian.Uint16(data[2+2*i:])
if int(ligOffset)+4 > len(data) {
return nil, errors.New("invalid ligature set table")
}
out[i].Glyph = GID(binary.BigEndian.Uint16(data[ligOffset:]))
ligCount := binary.BigEndian.Uint16(data[ligOffset+2:])
if ligCount == 0 {
return nil, errors.New("invalid ligature set table")
}
out[i].Components, err = parseUint16s(data[ligOffset+4:], int(ligCount)-1)
if err != nil {
return nil, fmt.Errorf("invalid ligature set table: %s", err)
}
}
return out, nil
}
type (
GSUBContext1 LookupContext1
GSUBContext2 LookupContext2
GSUBContext3 LookupContext3
)
func (GSUBContext1) Type() GSUBType { return GSUBContext }
func (GSUBContext2) Type() GSUBType { return GSUBContext }
func (GSUBContext3) Type() GSUBType { return GSUBContext }
// lookupLength is used to sanitize lookup indexes.
// cov is used for ContextFormat3
func parseSequenceContextSub(format uint16, data []byte, lookupLength uint16, cov *Coverage) (interface{ Type() GSUBType }, error) {
switch format {
case 1:
out, err := parseSequenceContext1(data, lookupLength)
return GSUBContext1(out), err
case 2:
out, err := parseSequenceContext2(data, lookupLength)
return GSUBContext2(out), err
case 3:
out, err := parseSequenceContext3(data, lookupLength)
if len(out.Coverages) != 0 {
*cov = out.Coverages[0]
}
return GSUBContext3(out), err
default:
return nil, fmt.Errorf("unsupported sequence context format %d", format)
}
}
type (
GSUBChainedContext1 LookupChainedContext1
GSUBChainedContext2 LookupChainedContext2
GSUBChainedContext3 LookupChainedContext3
)
func (GSUBChainedContext1) Type() GSUBType { return GSUBChaining }
func (GSUBChainedContext2) Type() GSUBType { return GSUBChaining }
func (GSUBChainedContext3) Type() GSUBType { return GSUBChaining }
// lookupLength is used to sanitize lookup indexes.
// cov is used for ContextFormat3
func parseChainedSequenceContextSub(format uint16, data []byte, lookupLength uint16, cov *Coverage) (interface{ Type() GSUBType }, error) {
switch format {
case 1:
out, err := parseChainedSequenceContext1(data, lookupLength)
return GSUBChainedContext1(out), err
case 2:
out, err := parseChainedSequenceContext2(data, lookupLength)
return GSUBChainedContext2(out), err
case 3:
out, err := parseChainedSequenceContext3(data, lookupLength)
if len(out.Input) != 0 {
*cov = out.Input[0]
}
return GSUBChainedContext3(out), err
default:
return nil, fmt.Errorf("unsupported sequence context format %d", format)
}
}
// returns the extension subtable instead
func parseExtensionSub(data []byte, lookupListLength uint16) (GSUBSubtable, error) {
if len(data) < 8 {
return GSUBSubtable{}, errors.New("invalid extension substitution table")
}
extensionType := GSUBType(binary.BigEndian.Uint16(data[2:]))
offset := binary.BigEndian.Uint32(data[4:])
if extensionType == gsubExtension {
return GSUBSubtable{}, errors.New("invalid extension substitution table")
}
return parseGSUBSubtable(data, int(offset), extensionType, lookupListLength)
}
type GSUBReverseChainedContext1 struct {
Backtrack []Coverage
Lookahead []Coverage
Substitutes []GID
}
func (GSUBReverseChainedContext1) Type() GSUBType { return GSUBReverse }
func parseReverseChainedSequenceContextSub(data []byte, cov Coverage) (out GSUBReverseChainedContext1, err error) {
if len(data) < 6 {
return out, errors.New("invalid reversed chained sequence context format 3 table")
}
covCount := binary.BigEndian.Uint16(data[4:])
out.Backtrack = make([]Coverage, covCount)
for i := range out.Backtrack {
covOffset := uint32(binary.BigEndian.Uint16(data[6+2*i:]))
out.Backtrack[i], err = parseCoverage(data, covOffset)
if err != nil {
return out, err
}
}
endBacktrack := 6 + 2*int(covCount)
if len(data) < endBacktrack+2 {
return out, errors.New("invalid reversed chained sequence context format 3 table")
}
covCount = binary.BigEndian.Uint16(data[endBacktrack:])
out.Lookahead = make([]Coverage, covCount)
for i := range out.Lookahead {
covOffset := uint32(binary.BigEndian.Uint16(data[endBacktrack+2+2*i:]))
out.Lookahead[i], err = parseCoverage(data, covOffset)
if err != nil {
return out, err
}
}
endLookahead := endBacktrack + 2 + 2*int(covCount)
if len(data) < endBacktrack+2 {
return out, errors.New("invalid reversed chained sequence context format 3 table")
}
glyphCount := binary.BigEndian.Uint16(data[endLookahead:])
if cov.Size() != int(glyphCount) {
return out, errors.New("invalid reversed chained sequence context format 3 table")
}
if len(data) < endLookahead+2+2*int(glyphCount) {
return out, errors.New("invalid reversed chained sequence context format 3 table")
}
out.Substitutes = make([]GID, glyphCount)
for i := range out.Substitutes {
out.Substitutes[i] = GID(binary.BigEndian.Uint16(data[endLookahead+2+2*i:]))
}
return out, err
}