-
Notifications
You must be signed in to change notification settings - Fork 7
/
parser.go
770 lines (634 loc) · 17.3 KB
/
parser.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
package truetype
import (
"bytes"
"compress/zlib"
"errors"
"fmt"
"io"
"github.com/benoitkugler/textlayout/fonts"
type1c "github.com/benoitkugler/textlayout/fonts/type1C"
)
// FontParser serves as an intermediate when reading font files.
// Most of the time, the Parse and Loader.Load functions are enough,
// but `FontParser` may be used on its own when more control over table loading is needed.
type FontParser struct {
file fonts.Resource // source, needed to parse each table
tables map[Tag]tableSection // header only, contents is processed on demand
Type Tag
// True for fonts which include a 'hbed' table instead
// of a 'head' table. Apple uses it as a flag that a font doesn't have
// any glyph outlines but only embedded bitmaps
isBinary bool
}
// NewFontParser reads the `file` header and returns
// a parser.
// `file` will be used to parse tables, and should not be close.
func NewFontParser(file fonts.Resource) (*FontParser, error) {
return parseOneFont(file, 0, false)
}
// NewFontParsers is the same as `NewFontParser`, but supports collections.
func NewFontParsers(file fonts.Resource) ([]*FontParser, error) {
_, err := file.Seek(0, io.SeekStart) // file might have been used before
if err != nil {
return nil, err
}
var bytes [4]byte
_, err = file.Read(bytes[:])
if err != nil {
return nil, err
}
magic := newTag(bytes[:])
file.Seek(0, io.SeekStart)
var (
pr *FontParser
offsets []uint32
relativeOffset bool
)
switch magic {
case SignatureWOFF, TypeTrueType, TypeOpenType, TypePostScript1, TypeAppleTrueType:
pr, err = parseOneFont(file, 0, false)
case ttcTag:
offsets, err = parseTTCHeader(file)
case dfontResourceDataOffset:
offsets, err = parseDfont(file)
relativeOffset = true
default:
return nil, fmt.Errorf("unsupported font format %v", bytes)
}
if err != nil {
return nil, err
}
// only one font
if pr != nil {
return []*FontParser{pr}, nil
}
// collection
out := make([]*FontParser, len(offsets))
for i, o := range offsets {
out[i], err = parseOneFont(file, o, relativeOffset)
if err != nil {
return nil, err
}
}
return out, nil
}
// tableSection represents a table within the font file.
type tableSection struct {
offset uint32 // Offset into the file this table starts.
length uint32 // Length of this table within the file.
zLength uint32 // Uncompressed length of this table.
}
func (pr *FontParser) findTableBuffer(s tableSection) ([]byte, error) {
var buf []byte
if s.length != 0 && s.length < s.zLength {
zbuf := io.NewSectionReader(pr.file, int64(s.offset), int64(s.length))
r, err := zlib.NewReader(zbuf)
if err != nil {
return nil, err
}
defer r.Close()
buf = make([]byte, s.zLength)
if _, err := io.ReadFull(r, buf); err != nil {
return nil, err
}
} else {
buf = make([]byte, s.length)
if _, err := pr.file.ReadAt(buf, int64(s.offset)); err != nil {
return nil, err
}
}
return buf, nil
}
// HasTable returns `true` is the font has the given table.
func (pr *FontParser) HasTable(tag Tag) bool {
_, has := pr.tables[tag]
return has
}
// GetRawTable returns the binary content of the given table,
// or an error if not found.
// Note that many tables are already interpreted by this package,
// see the various XXXTable().
func (pr *FontParser) GetRawTable(tag Tag) ([]byte, error) {
s, found := pr.tables[tag]
if !found {
return nil, errMissingTable
}
return pr.findTableBuffer(s)
}
// loads the table corresponding to the 'head' tag.
// if a 'bhed' Apple table is present, it replaces the 'head' one
func (pr *FontParser) loadHeadTable() (TableHead, error) {
s, hasbhed := pr.tables[tagBhed]
if !hasbhed {
var hasHead bool
s, hasHead = pr.tables[tagHead]
if !hasHead {
return TableHead{}, errors.New("missing required head (or bhed) table")
}
}
pr.isBinary = hasbhed
buf, err := pr.findTableBuffer(s)
if err != nil {
return TableHead{}, err
}
return parseTableHead(buf)
}
// loads the table corresponding to the 'name' tag.
// error only if the table is present and invalid
func (pr *FontParser) tryAndLoadNameTable() (TableName, error) {
s, found := pr.tables[tagName]
if !found {
return nil, nil
}
buf, err := pr.findTableBuffer(s)
if err != nil {
return nil, err
}
return parseTableName(buf)
}
// GlyfTable parse the 'glyf' table.
// Note that glyphs may be defined in various format (like CFF or bitmaps), and stored
// in other tables.
// `locationIndexFormat` is found in the 'head' table.
func (pr *FontParser) GlyfTable(numGlyphs int, locationIndexFormat int16) (TableGlyf, error) {
buf, err := pr.GetRawTable(tagLoca)
if err != nil {
return nil, err
}
loca, err := parseTableLoca(buf, numGlyphs, locationIndexFormat == 1)
if err != nil {
return nil, err
}
buf, err = pr.GetRawTable(tagGlyf)
if err != nil {
return nil, err
}
return parseTableGlyf(buf, loca)
}
func (pr *FontParser) cffTable(numGlyphs int) (*type1c.Font, error) {
buf, err := pr.GetRawTable(tagCFF)
if err != nil {
return nil, err
}
out, err := type1c.Parse(bytes.NewReader(buf))
if err != nil {
return nil, err
}
if N := out.NumGlyphs(); N != numGlyphs {
return nil, fmt.Errorf("invalid number of glyphs in CFF table (%d != %d)", N, numGlyphs)
}
return out, nil
}
func (pr *FontParser) sbixTable(numGlyphs int) (tableSbix, error) {
buf, err := pr.GetRawTable(tagSbix)
if err != nil {
return tableSbix{}, err
}
return parseTableSbix(buf, numGlyphs)
}
// parse cblc and cbdt tables
func (pr *FontParser) colorBitmapTable() (bitmapTable, error) {
buf, err := pr.GetRawTable(tagCBLC)
if err != nil {
return nil, err
}
rawImageData, err := pr.GetRawTable(tagCBDT)
if err != nil {
return nil, err
}
return parseTableBitmap(buf, rawImageData)
}
// parse eblc and ebdt tables
func (pr *FontParser) grayBitmapTable() (bitmapTable, error) {
buf, err := pr.GetRawTable(tagEBLC)
if err != nil {
return nil, err
}
rawImageData, err := pr.GetRawTable(tagEBDT)
if err != nil {
return nil, err
}
return parseTableBitmap(buf, rawImageData)
}
// parse bloc and bdat tables
func (pr *FontParser) appleBitmapTable() (bitmapTable, error) {
buf, err := pr.GetRawTable(tagBloc)
if err != nil {
return nil, err
}
rawImageData, err := pr.GetRawTable(tagBdat)
if err != nil {
return nil, err
}
return parseTableBitmap(buf, rawImageData)
}
func (pr *FontParser) HheaTable() (*TableHVhea, error) {
buf, err := pr.GetRawTable(tagHhea)
if err != nil {
return nil, err
}
return parseTableHVhea(buf)
}
func (pr *FontParser) VheaTable() (*TableHVhea, error) {
buf, err := pr.GetRawTable(tagVhea)
if err != nil {
return nil, err
}
return parseTableHVhea(buf)
}
func (pr *FontParser) OS2Table() (*TableOS2, error) {
buf, err := pr.GetRawTable(tagOS2)
if err != nil {
return nil, err
}
return parseTableOS2(buf)
}
// GPOSTable returns the Glyph Positioning table identified with the 'GPOS' tag.
func (pr *FontParser) GPOSTable() (TableGPOS, error) {
buf, err := pr.GetRawTable(TagGpos)
if err != nil {
return TableGPOS{}, err
}
return parseTableGPOS(buf)
}
// GSUBTable returns the Glyph Substitution table identified with the 'GSUB' tag.
func (pr *FontParser) GSUBTable() (TableGSUB, error) {
buf, err := pr.GetRawTable(TagGsub)
if err != nil {
return TableGSUB{}, err
}
return parseTableGSUB(buf)
}
// GDEFTable returns the Glyph Definition table identified with the 'GDEF' tag.
func (pr *FontParser) GDEFTable(nbAxis int) (TableGDEF, error) {
buf, err := pr.GetRawTable(TagGdef)
if err != nil {
return TableGDEF{}, err
}
return parseTableGdef(buf, nbAxis)
}
func (pr *FontParser) CmapTable() (TableCmap, error) {
s, found := pr.tables[tagCmap]
if !found {
return TableCmap{}, errors.New("missing required 'cmap' table")
}
buf, err := pr.findTableBuffer(s)
if err != nil {
return TableCmap{}, fmt.Errorf("invalid required cmap table: %s", err)
}
return parseTableCmap(buf)
}
// PostTable returns the Post table names
func (pr *FontParser) PostTable(numGlyphs int) (TablePost, error) {
buf, err := pr.GetRawTable(tagPost)
if err != nil {
return TablePost{}, err
}
return parseTablePost(buf, uint16(numGlyphs))
}
// svgTable returns the Post table names
func (pr *FontParser) svgTable() (tableSVG, error) {
buf, err := pr.GetRawTable(tagSVG)
if err != nil {
return nil, err
}
return parseTableSVG(buf)
}
// NumGlyphs parses the 'maxp' table to find the number of glyphs in the font.
func (pr *FontParser) NumGlyphs() (int, error) {
buf, err := pr.GetRawTable(tagMaxp)
if err != nil {
return -1, err
}
return parseTableMaxp(buf)
}
// HtmxTable returns the glyphs horizontal metrics (array of size numGlyphs),
// expressed in fonts units.
func (pr *FontParser) HtmxTable(numGlyphs int) (TableHVmtx, error) {
hhea, err := pr.HheaTable()
if err != nil {
return nil, err
}
buf, err := pr.GetRawTable(tagHmtx)
if err != nil {
return nil, err
}
return parseHVmtxTable(buf, hhea.numOfLongMetrics, uint16(numGlyphs))
}
// VtmxTable returns the glyphs vertical metrics (array of size numGlyphs),
// expressed in fonts units.
func (pr *FontParser) VtmxTable(numGlyphs int) (TableHVmtx, error) {
vhea, err := pr.VheaTable()
if err != nil {
return nil, err
}
buf, err := pr.GetRawTable(tagVmtx)
if err != nil {
return nil, err
}
return parseHVmtxTable(buf, vhea.numOfLongMetrics, uint16(numGlyphs))
}
// KernTable parses and returns the 'kern' table.
func (pr *FontParser) KernTable(numGlyphs int) (TableKernx, error) {
buf, err := pr.GetRawTable(tagKern)
if err != nil {
return nil, err
}
return parseKernTable(buf, numGlyphs)
}
// MorxTable parse the AAT 'morx' table.
func (pr *FontParser) MorxTable(numGlyphs int) (TableMorx, error) {
buf, err := pr.GetRawTable(tagMorx)
if err != nil {
return nil, err
}
return parseTableMorx(buf, numGlyphs)
}
// KerxTable parse the AAT 'kerx' table.
func (pr *FontParser) KerxTable(numGlyphs int) (TableKernx, error) {
buf, err := pr.GetRawTable(tagKerx)
if err != nil {
return nil, err
}
return parseTableKerx(buf, numGlyphs)
}
// AnkrTable parse the AAT 'ankr' table.
func (pr *FontParser) AnkrTable(numGlyphs int) (TableAnkr, error) {
buf, err := pr.GetRawTable(tagAnkr)
if err != nil {
return TableAnkr{}, err
}
return parseTableAnkr(buf, numGlyphs)
}
// TrakTable parse the AAT 'trak' table.
func (pr *FontParser) TrakTable() (TableTrak, error) {
buf, err := pr.GetRawTable(tagTrak)
if err != nil {
return TableTrak{}, err
}
return parseTrakTable(buf)
}
// FeatTable parse the AAT 'feat' table.
func (pr *FontParser) FeatTable() (TableFeat, error) {
buf, err := pr.GetRawTable(tagFeat)
if err != nil {
return nil, err
}
return parseTableFeat(buf)
}
// error only if the table is present and invalid
func (pr *FontParser) tryAndLoadFvarTable(names TableName) (TableFvar, error) {
s, found := pr.tables[tagFvar]
if !found {
return TableFvar{}, nil
}
buf, err := pr.findTableBuffer(s)
if err != nil {
return TableFvar{}, err
}
return parseTableFvar(buf, names)
}
// error only if the table is present and invalid
func (pr *FontParser) tryAndLoadAvarTable(fvar TableFvar) (tableAvar, error) {
s, found := pr.tables[tagAvar]
if !found {
return nil, nil
}
buf, err := pr.findTableBuffer(s)
if err != nil {
return nil, err
}
return parseTableAvar(buf, len(fvar.Axis))
}
func (pr *FontParser) gvarTable(glyphs TableGlyf, fvar TableFvar) (tableGvar, error) {
buf, err := pr.GetRawTable(tagGvar)
if err != nil {
return tableGvar{}, err
}
return parseTableGvar(buf, len(fvar.Axis), glyphs)
}
func (pr *FontParser) hvarTable(fvar TableFvar) (tableHVvar, error) {
buf, err := pr.GetRawTable(tagHvar)
if err != nil {
return tableHVvar{}, err
}
return parseTableHVvar(buf, len(fvar.Axis))
}
func (pr *FontParser) vvarTable(fvar TableFvar) (tableHVvar, error) {
buf, err := pr.GetRawTable(tagVvar)
if err != nil {
return tableHVvar{}, err
}
return parseTableHVvar(buf, len(fvar.Axis))
}
func (pr *FontParser) mvarTable(fvar TableFvar) (TableMvar, error) {
buf, err := pr.GetRawTable(tagMvar)
if err != nil {
return TableMvar{}, err
}
return parseTableMvar(buf, len(fvar.Axis))
}
func (pr *FontParser) vorgTable() (tableVorg, error) {
buf, err := pr.GetRawTable(tagVorg)
if err != nil {
return tableVorg{}, err
}
return parseTableVorg(buf)
}
// best effort to load all valid tables
func (pr *FontParser) loadLayoutTables(numGlyphs int, fvar TableFvar) (out LayoutTables) {
if tb, err := pr.GDEFTable(len(fvar.Axis)); err == nil {
out.GDEF = tb
}
if tb, err := pr.GSUBTable(); err == nil {
out.GSUB = tb
}
if tb, err := pr.GPOSTable(); err == nil {
out.GPOS = tb
}
if tb, err := pr.MorxTable(numGlyphs); err == nil {
out.Morx = tb
}
if tb, err := pr.KernTable(numGlyphs); err == nil {
out.Kern = tb
}
if tb, err := pr.KerxTable(numGlyphs); err == nil {
out.Kerx = tb
}
if tb, err := pr.AnkrTable(numGlyphs); err == nil {
out.Ankr = tb
}
if tb, err := pr.TrakTable(); err == nil {
out.Trak = tb
}
if tb, err := pr.FeatTable(); err == nil {
out.Feat = tb
}
return out
}
// graphite support
var (
tagSilf = MustNewTag("Silf")
tagSill = MustNewTag("Sill")
tagGraphiteFeat = MustNewTag("Feat")
tagGloc = MustNewTag("Gloc")
tagGlat = MustNewTag("Glat")
)
type GraphiteTables struct {
Sill, Feat, Gloc, Glat, Silf []byte
}
// LoadGraphiteTables returns the raw tables required for
// Graphite engine support.
// See the package graphite for how to interpret these tables.
func (pr *FontParser) LoadGraphiteTables() (gr GraphiteTables, err error) {
gr.Sill, err = pr.GetRawTable(tagSill)
if err != nil {
return gr, fmt.Errorf("loading table Sill: %s", err)
}
gr.Feat, err = pr.GetRawTable(tagGraphiteFeat)
if err != nil {
return gr, fmt.Errorf("loading table Feat: %s", err)
}
gr.Gloc, err = pr.GetRawTable(tagGloc)
if err != nil {
return gr, fmt.Errorf("loading table Gloc: %s", err)
}
gr.Glat, err = pr.GetRawTable(tagGlat)
if err != nil {
return gr, fmt.Errorf("loading table Glat: %s", err)
}
gr.Silf, err = pr.GetRawTable(tagSilf)
if err != nil {
return gr, fmt.Errorf("loading table Silf: %s", err)
}
return gr, nil
}
func parseOneFont(file fonts.Resource, offset uint32, relativeOffset bool) (parser *FontParser, err error) {
_, err = file.Seek(int64(offset), io.SeekStart)
if err != nil {
return nil, fmt.Errorf("invalid offset: %s", err)
}
var bytes [4]byte
_, err = file.Read(bytes[:])
if err != nil {
return nil, err
}
magic := newTag(bytes[:])
switch magic {
case SignatureWOFF:
parser, err = parseWOFF(file, offset, relativeOffset)
case TypeTrueType, TypeOpenType, TypePostScript1, TypeAppleTrueType:
parser, err = parseOTF(file, offset, relativeOffset)
default:
// no more collections allowed here
return nil, errUnsupportedFormat
}
if err != nil {
return nil, err
}
return parser, nil
}
// loadTables calls all the functions loading the
// various font tables,
// and return the loaded font
func (pr *FontParser) loadTables() (*Font, error) {
var (
out Font
err error
)
out.Type = pr.Type
out.NumGlyphs, err = pr.NumGlyphs()
if err != nil {
return nil, err
}
cmaps, err := pr.CmapTable()
if err != nil {
return nil, err
}
out.Head, err = pr.loadHeadTable()
if err != nil {
return nil, err
}
out.Names, err = pr.tryAndLoadNameTable()
if err != nil {
return nil, err
}
out.fvar, err = pr.tryAndLoadFvarTable(out.Names)
if err != nil {
return nil, err
}
out.avar, err = pr.tryAndLoadAvarTable(out.fvar)
if err != nil {
return nil, err
}
out.upem = out.Head.Upem()
out.OS2, _ = pr.OS2Table()
out.Glyf, _ = pr.GlyfTable(out.NumGlyphs, out.Head.indexToLocFormat)
out.bitmap = pr.selectBitmapTable()
out.sbix, _ = pr.sbixTable(out.NumGlyphs)
out.cff, _ = pr.cffTable(out.NumGlyphs)
out.post, _ = pr.PostTable(out.NumGlyphs)
out.svg, _ = pr.svgTable()
out.hhea, _ = pr.HheaTable()
out.vhea, _ = pr.VheaTable()
out.Hmtx, _ = pr.HtmxTable(out.NumGlyphs)
out.vmtx, _ = pr.VtmxTable(out.NumGlyphs)
if len(out.fvar.Axis) != 0 {
out.mvar, _ = pr.mvarTable(out.fvar)
out.gvar, _ = pr.gvarTable(out.Glyf, out.fvar)
if v, err := pr.hvarTable(out.fvar); err == nil {
out.hvar = &v
}
if v, err := pr.vvarTable(out.fvar); err == nil {
out.vvar = &v
}
}
out.cmap, out.cmapEncoding = cmaps.BestEncoding()
out.cmapVar = cmaps.unicodeVariation
if vorg, err := pr.vorgTable(); err == nil {
out.vorg = &vorg
}
out.layoutTables = pr.loadLayoutTables(out.NumGlyphs, out.fvar)
if pr.HasTable(TagSilf) {
var gr GraphiteTables
gr, err = pr.LoadGraphiteTables()
if err != nil {
return nil, err
}
out.Graphite = &gr
}
if pr.HasTable(TagPrep) {
out.HasHint = true
}
err = pr.loadSummary(&out)
if err != nil {
return nil, err
}
return &out, nil
}
// Parse parses an OpenType or TrueType file and returns a Font.
// See Loader for support for collections, and FontParser for
// more control over table loading.
func Parse(file fonts.Resource) (*Font, error) {
pr, err := NewFontParser(file)
if err != nil {
return nil, err
}
return pr.loadTables()
}
// Load implements fonts.FontLoader. For collection font files (.ttc, .otc),
// multiple fonts may be returned.
func Load(file fonts.Resource) (fonts.Faces, error) {
prs, err := NewFontParsers(file)
if err != nil {
return nil, err
}
out := make(fonts.Faces, len(prs))
for i, pr := range prs {
out[i], err = pr.loadTables()
if err != nil {
return nil, err
}
}
return out, nil
}