-
Notifications
You must be signed in to change notification settings - Fork 5
/
export.go
584 lines (507 loc) · 14.8 KB
/
export.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
/*
* This file is subject to the terms and conditions defined in
* file 'LICENSE.md', which is part of this source code package.
*/
package unitype
import (
"bytes"
"errors"
"io"
"math"
"os"
"sort"
"github.com/sirupsen/logrus"
)
// Font wraps font for outside access.
type Font struct {
br *byteReader
*font
}
// Parse parses the truetype font from `rs` and returns a new Font.
func Parse(rs io.ReadSeeker) (*Font, error) {
r := newByteReader(rs)
fnt, err := parseFont(r)
if err != nil {
return nil, err
}
return &Font{
br: r,
font: fnt,
}, nil
}
// ParseFile parses the truetype font from file given by path.
func ParseFile(filePath string) (*Font, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer f.Close()
return Parse(f)
}
// ValidateBytes validates the turetype font represented by the byte stream.
func ValidateBytes(b []byte) error {
r := bytes.NewReader(b)
br := newByteReader(r)
fnt, err := parseFont(br)
if err != nil {
return err
}
return fnt.validate(br)
}
// ValidateFile validates the truetype font given by `filePath`.
func ValidateFile(filePath string) error {
f, err := os.Open(filePath)
if err != nil {
return err
}
defer f.Close()
br := newByteReader(f)
fnt, err := parseFont(br)
if err != nil {
return err
}
return fnt.validate(br)
}
// GetCmap returns the specific cmap specified by `platformID` and platform-specific `encodingID`.
// If not available, nil is returned. Used in PDF for decoding.
func (f *Font) GetCmap(platformID, encodingID int) map[rune]GlyphIndex {
if f.cmap == nil {
return nil
}
for _, subt := range f.cmap.subtables {
if subt.platformID == platformID && subt.encodingID == encodingID {
return subt.cmap
}
}
return nil
}
// LookupRunes looks up each rune in `rune` and returns a matching slice of glyph indices.
// When a rune is not found, a GID of 0 is used (notdef).
func (f *Font) LookupRunes(runes []rune) []GlyphIndex {
var maps []map[rune]GlyphIndex
// Search order (3,1), (1,0), (0,3), (3,10).
maps = append(maps,
f.GetCmap(3, 1),
f.GetCmap(1, 0),
f.GetCmap(0, 3),
f.GetCmap(3, 10),
)
var indices []GlyphIndex
for _, r := range runes {
index := GlyphIndex(0)
for _, cmap := range maps {
ind, has := cmap[r]
if has {
index = ind
break
}
}
indices = append(indices, index)
}
logrus.Debugf("Runes: %+v %s", runes, string(runes))
logrus.Debugf("GIDs: %+v", indices)
return indices
}
// SubsetKeepRunes prunes data for all GIDs except the ones corresponding to `runes`. The GIDs are
// maintained. Typically reduces glyf table size significantly.
func (f *Font) SubsetKeepRunes(runes []rune) (*Font, error) {
indices := f.LookupRunes(runes)
return f.SubsetKeepIndices(indices)
}
// SubsetKeepIndices prunes data for all GIDs outside of `indices`. The GIDs are maintained.
// This typically works well and is a simple way to prune most of the unnecessary data as the
// glyf table is usually the biggest by far.
func (f *Font) SubsetKeepIndices(indices []GlyphIndex) (*Font, error) {
newfnt := font{}
// Expand the set of indices if any of the indices are composite
// glyphs depending on other glyphs.
gidIncludedMap := make(map[GlyphIndex]struct{}, len(indices))
for _, gid := range indices {
gidIncludedMap[gid] = struct{}{}
}
toscan := make([]GlyphIndex, 0, len(gidIncludedMap))
for gid := range gidIncludedMap {
toscan = append(toscan, gid)
}
// Find dependencies of core sets of glyph, and expand until have all relations.
for len(toscan) > 0 {
var newgids []GlyphIndex
for _, gid := range toscan {
components, err := f.glyf.GetComponents(gid)
if err != nil {
logrus.Debugf("Error getting components for %d", gid)
return nil, err
}
for _, gid := range components {
if _, has := gidIncludedMap[gid]; !has {
gidIncludedMap[gid] = struct{}{}
newgids = append(newgids, gid)
}
}
}
toscan = newgids
}
newfnt.ot = &offsetTable{}
*newfnt.ot = *f.font.ot
newfnt.trec = &tableRecords{}
*newfnt.trec = *f.font.trec
if f.font.head != nil {
newfnt.head = &headTable{}
*newfnt.head = *f.font.head
}
if f.font.maxp != nil {
newfnt.maxp = &maxpTable{}
*newfnt.maxp = *f.font.maxp
}
if f.font.hhea != nil {
newfnt.hhea = &hheaTable{}
*newfnt.hhea = *f.font.hhea
}
if f.font.hmtx != nil {
newfnt.hmtx = &hmtxTable{}
*newfnt.hmtx = *f.font.hmtx
newfnt.optimizeHmtx()
}
if f.font.glyf != nil && f.font.loca != nil {
newfnt.loca = &locaTable{}
newfnt.glyf = &glyfTable{}
*newfnt.glyf = *f.font.glyf
// Empty glyf contents for non-included glyphs.
for i := range newfnt.glyf.descs {
if _, has := gidIncludedMap[GlyphIndex(i)]; has {
continue
}
newfnt.glyf.descs[i].raw = nil
}
// Update loca offsets.
isShort := f.font.head.indexToLocFormat == 0
if isShort {
newfnt.loca.offsetsShort = make([]offset16, len(newfnt.glyf.descs)+1)
newfnt.loca.offsetsShort[0] = f.font.loca.offsetsShort[0]
} else {
newfnt.loca.offsetsLong = make([]offset32, len(newfnt.glyf.descs)+1)
newfnt.loca.offsetsLong[0] = f.font.loca.offsetsLong[0]
}
for i, desc := range newfnt.glyf.descs {
if isShort {
newfnt.loca.offsetsShort[i+1] = newfnt.loca.offsetsShort[i] + offset16(len(desc.raw))/2
} else {
newfnt.loca.offsetsLong[i+1] = newfnt.loca.offsetsLong[i] + offset32(len(desc.raw))
}
}
}
if f.font.prep != nil {
newfnt.prep = &prepTable{}
*newfnt.prep = *f.font.prep
}
if f.font.cvt != nil {
newfnt.cvt = &cvtTable{}
*newfnt.cvt = *f.font.cvt
}
if f.font.fpgm != nil {
newfnt.fpgm = &fpgmTable{}
*newfnt.fpgm = *f.font.fpgm
}
if f.font.name != nil {
newfnt.name = &nameTable{}
*newfnt.name = *f.font.name
}
if f.font.os2 != nil {
newfnt.os2 = &os2Table{}
*newfnt.os2 = *f.font.os2
}
if f.font.post != nil {
newfnt.post = &postTable{}
*newfnt.post = *f.font.post
}
if f.font.cmap != nil {
newfnt.cmap = &cmapTable{}
*newfnt.cmap = *f.font.cmap
}
subfnt := &Font{
br: nil,
font: &newfnt,
}
// Trim down to the first fonts.
var maxgid GlyphIndex
for gid := range gidIncludedMap {
if gid > maxgid {
maxgid = gid
}
}
// Trim font down to only maximum needed glyphs without changing order.
maxNeededNum := int(maxgid) + 1
return subfnt.SubsetFirst(maxNeededNum)
}
// SubsetFirst creates a subset of `f` limited to only the first `numGlyphs` glyphs.
// Prunes out the glyphs from the previous font beyond that number.
// NOTE: If any of the first numGlyphs depend on later glyphs, it can lead to incorrect rendering.
func (f *Font) SubsetFirst(numGlyphs int) (*Font, error) {
if int(f.maxp.numGlyphs) <= numGlyphs {
logrus.Debugf("Attempting to subset font with same number of glyphs - Ignoring, returning same back")
return f, nil
}
newfnt := font{}
newfnt.ot = &offsetTable{}
*newfnt.ot = *f.font.ot
newfnt.trec = &tableRecords{}
*newfnt.trec = *f.font.trec
if f.font.head != nil {
newfnt.head = &headTable{}
*newfnt.head = *f.font.head
}
if f.font.maxp != nil {
newfnt.maxp = &maxpTable{}
*newfnt.maxp = *f.font.maxp
newfnt.maxp.numGlyphs = uint16(numGlyphs)
}
if f.font.hhea != nil {
newfnt.hhea = &hheaTable{}
*newfnt.hhea = *f.font.hhea
if newfnt.hhea.numberOfHMetrics > uint16(numGlyphs) {
newfnt.hhea.numberOfHMetrics = uint16(numGlyphs)
}
}
if f.font.hmtx != nil {
newfnt.hmtx = &hmtxTable{}
*newfnt.hmtx = *f.font.hmtx
if len(newfnt.hmtx.hMetrics) > numGlyphs {
newfnt.hmtx.hMetrics = newfnt.hmtx.hMetrics[0:numGlyphs]
newfnt.hmtx.leftSideBearings = nil
} else {
numKeep := numGlyphs - len(newfnt.hmtx.hMetrics)
if numKeep > len(newfnt.hmtx.leftSideBearings) {
numKeep = len(newfnt.hmtx.leftSideBearings)
}
newfnt.hmtx.leftSideBearings = newfnt.hmtx.leftSideBearings[0:numKeep]
}
newfnt.optimizeHmtx()
}
if f.font.glyf != nil && f.font.loca != nil {
newfnt.loca = &locaTable{}
newfnt.glyf = &glyfTable{
descs: f.font.glyf.descs[0:numGlyphs],
}
// Update loca offsets.
isShort := f.font.head.indexToLocFormat == 0
if isShort {
newfnt.loca.offsetsShort = make([]offset16, numGlyphs+1)
newfnt.loca.offsetsShort[0] = f.font.loca.offsetsShort[0]
} else {
newfnt.loca.offsetsLong = make([]offset32, numGlyphs+1)
newfnt.loca.offsetsLong[0] = f.font.loca.offsetsLong[0]
}
for i, desc := range newfnt.glyf.descs {
if isShort {
newfnt.loca.offsetsShort[i+1] = newfnt.loca.offsetsShort[i] + offset16(len(desc.raw))/2
} else {
newfnt.loca.offsetsLong[i+1] = newfnt.loca.offsetsLong[i] + offset32(len(desc.raw))
}
}
}
if f.font.prep != nil {
newfnt.prep = &prepTable{}
*newfnt.prep = *f.font.prep
}
if f.font.cvt != nil {
newfnt.cvt = &cvtTable{}
*newfnt.cvt = *f.font.cvt
}
if f.font.fpgm != nil {
newfnt.fpgm = &fpgmTable{}
*newfnt.fpgm = *f.font.fpgm
}
if f.font.name != nil {
newfnt.name = &nameTable{}
*newfnt.name = *f.font.name
}
if f.font.os2 != nil {
newfnt.os2 = &os2Table{}
*newfnt.os2 = *f.font.os2
}
if f.font.post != nil {
newfnt.post = &postTable{}
*newfnt.post = *f.font.post
if newfnt.post.numGlyphs > 0 {
newfnt.post.numGlyphs = uint16(numGlyphs)
}
if len(newfnt.post.glyphNameIndex) > numGlyphs {
newfnt.post.glyphNameIndex = newfnt.post.glyphNameIndex[0:numGlyphs]
}
if len(newfnt.post.offsets) > numGlyphs {
newfnt.post.offsets = newfnt.post.offsets[0:numGlyphs]
}
if len(newfnt.post.glyphNames) > numGlyphs {
newfnt.post.glyphNames = newfnt.post.glyphNames[0:numGlyphs]
}
}
if f.font.cmap != nil {
newfnt.cmap = &cmapTable{
version: f.cmap.version,
subtables: map[string]*cmapSubtable{},
}
for _, name := range f.cmap.subtableKeys {
subt := f.cmap.subtables[name]
switch t := subt.ctx.(type) {
case cmapSubtableFormat0:
for i := range t.glyphIDArray {
if i > numGlyphs {
t.glyphIDArray[i] = 0
}
}
case cmapSubtableFormat4:
newt := cmapSubtableFormat4{}
// Generates a new table: going from glyph index 0 up to numGlyphs.
// Makes continous entries with deltas.
// Does not use glyphIDData, but only the deltas. Can lead to many segments, but should not
// be too bad (especially since subsetting).
charcodes := make([]CharCode, 0, len(subt.charcodeToGID))
for cc, gid := range subt.charcodeToGID {
if int(gid) >= numGlyphs {
continue
}
charcodes = append(charcodes, cc)
}
sort.Slice(charcodes, func(i, j int) bool {
return charcodes[i] < charcodes[j]
})
segments := 0
i := 0
for i < len(charcodes) {
j := i + 1
for ; j < len(charcodes); j++ {
if int(charcodes[j]-charcodes[i]) != j-i ||
int(subt.charcodeToGID[charcodes[j]]-subt.charcodeToGID[charcodes[i]]) != j-i {
break
}
}
// from i:j-1 maps to subt.charcodes[i]:subt.charcodes[i]+j-i-1
startCode := uint16(charcodes[i])
endCode := uint16(charcodes[i]) + uint16(j-i-1)
idDelta := uint16(subt.charcodeToGID[charcodes[i]]) - uint16(charcodes[i])
newt.startCode = append(newt.startCode, startCode)
newt.endCode = append(newt.endCode, endCode)
newt.idDelta = append(newt.idDelta, idDelta)
newt.idRangeOffset = append(newt.idRangeOffset, 0)
segments++
i = j
}
if segments > 0 && newt.endCode[segments-1] < 65535 {
newt.endCode = append(newt.endCode, 65535)
newt.startCode = append(newt.startCode, 65535)
newt.idDelta = append(newt.idDelta, 1)
newt.idRangeOffset = append(newt.idRangeOffset, 0)
segments++
}
newt.length = uint16(2*8 + 2*4*segments)
newt.language = t.language
newt.segCountX2 = uint16(segments * 2)
newt.searchRange = 2 * uint16(math.Pow(2, math.Floor(math.Log2(float64(segments)))))
newt.entrySelector = uint16(math.Log2(float64(newt.searchRange) / 2.0))
newt.rangeShift = uint16(segments*2) - newt.searchRange
subt.ctx = newt
case cmapSubtableFormat6:
for i := range t.glyphIDArray {
if int(t.glyphIDArray[i]) >= numGlyphs {
t.glyphIDArray[i] = 0
}
}
case cmapSubtableFormat12:
newt := cmapSubtableFormat12{}
groups := 0
charcodes := make([]CharCode, 0, len(subt.charcodeToGID))
for cc, gid := range subt.charcodeToGID {
if int(gid) >= numGlyphs {
continue
}
charcodes = append(charcodes, cc)
}
sort.Slice(charcodes, func(i, j int) bool {
return charcodes[i] < charcodes[j]
})
i := 0
for i < len(charcodes) {
j := i + 1
for ; j < len(charcodes); j++ {
if int(charcodes[j]-charcodes[i]) != j-i ||
int(subt.charcodeToGID[charcodes[j]]-subt.charcodeToGID[charcodes[i]]) != j-i {
break
}
}
// from i:j-1 maps to subt.charcodes[i]:subt.charcodes[i]+j-i-1
startCharCode := uint32(charcodes[i])
endCharCode := uint32(charcodes[i]) + uint32(j-i-1)
startGlyphID := uint32(subt.charcodeToGID[charcodes[i]])
group := sequentialMapGroup{
startCharCode: startCharCode,
endCharCode: endCharCode,
startGlyphID: startGlyphID,
}
newt.groups = append(newt.groups, group)
groups++
i = j
}
newt.length = uint32(2*2 + 3*4 + groups*3*4)
newt.language = t.language
newt.numGroups = uint32(groups)
subt.ctx = newt
}
newfnt.cmap.subtableKeys = append(newfnt.cmap.subtableKeys, name)
newfnt.cmap.subtables[name] = subt
}
newfnt.cmap.numTables = uint16(len(newfnt.cmap.subtables))
}
subfnt := &Font{
br: nil,
font: &newfnt,
}
return subfnt, nil
}
// Subset creates a subset of `f` including only glyph indices specified by `indices`.
// Returns the new subsetted font, a map of old to new GlyphIndex to GlyphIndex as the removal
// of glyphs requires reordering.
func (f *Font) Subset(indices []GlyphIndex) (newf *Font, oldnew map[GlyphIndex]GlyphIndex, err error) {
// TODO:
// 1. Make the new cmap for `runes` if `cmap` is nil, using the cmap table and make a []GlyphIndex
// with the glyph indices to keep (index prior to subsetting).
// 2. Go through each table and leave only data for the glyph indices to be kept.
return nil, nil, errors.New("not implemented yet")
}
// PruneTables prunes font tables `tables` by name from font.
// Currently supports: "cmap", "post", "name".
func (f *Font) PruneTables(tables ...string) error {
for _, table := range tables {
switch table {
case "cmap":
f.cmap = nil
case "post":
f.post = nil
case "name":
f.name = nil
}
}
return nil
}
// Optimize does some optimization such as reducing hmtx table.
func (f *Font) Optimize() error {
f.optimizeHmtx()
return nil
}
// Write writes the font to `w`.
func (f *Font) Write(w io.Writer) error {
bw := newByteWriter(w)
err := f.font.write(bw)
if err != nil {
return err
}
return bw.flush()
}
// WriteFile writes the font to `outPath`.
func (f *Font) WriteFile(outPath string) error {
of, err := os.Create(outPath)
if err != nil {
return err
}
defer of.Close()
return f.Write(of)
}