forked from tdewolff/font
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sfnt_cff.go
1230 lines (1159 loc) · 29.3 KB
/
sfnt_cff.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
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package font
import (
"fmt"
"math"
"strconv"
)
// TODO: use FDSelect for Font DICTs
// TODO: CFF has winding rule even-odd? CFF2 has winding rule nonzero
type cffTable struct {
version int
top *cffTopDICT
charStrings *cffINDEX
globalSubrs *cffINDEX
fonts *cffFontINDEX
}
func (sfnt *SFNT) parseCFF() error {
b, ok := sfnt.Tables["CFF "]
if !ok {
return fmt.Errorf("CFF: missing table")
}
r := NewBinaryReader(b)
major := r.ReadUint8()
minor := r.ReadUint8()
if major != 1 || minor != 0 {
return fmt.Errorf("CFF: bad version")
}
headerSize := r.ReadUint8()
if headerSize != 4 {
return fmt.Errorf("CFF: bad headerSize")
}
_ = r.ReadUint8() // offSize
nameINDEX, err := parseINDEX(r, false)
if err != nil {
return fmt.Errorf("CFF: Name INDEX: %w", err)
}
if len(nameINDEX.offset) != 2 {
return fmt.Errorf("CFF: Name INDEX: bad count")
}
topINDEX, err := parseINDEX(r, false)
if err != nil {
return fmt.Errorf("CFF: Top INDEX: %w", err)
}
if len(topINDEX.offset) != len(nameINDEX.offset) {
return fmt.Errorf("CFF: Top INDEX: bad count")
}
stringINDEX, err := parseINDEX(r, false)
if err != nil {
return fmt.Errorf("CFF: String INDEX: %w", err)
}
globalSubrsINDEX, err := parseINDEX(r, false)
if err != nil {
return fmt.Errorf("CFF: Global Subrs INDEX: %w", err)
}
topDICT, err := parseTopDICT(topINDEX.Get(0), stringINDEX)
if err != nil {
return fmt.Errorf("CFF: Top DICT: %w", err)
} else if topDICT.CharstringType != 2 {
return fmt.Errorf("CFF: Type %d Charstring format not supported", topDICT.CharstringType)
}
r.Seek(uint32(topDICT.CharStrings))
charStringsINDEX, err := parseINDEX(r, false)
if err != nil {
return fmt.Errorf("CFF: CharStrings INDEX: %w", err)
}
if !topDICT.IsCID {
if len(b) < topDICT.PrivateOffset || len(b)-topDICT.PrivateOffset < topDICT.PrivateLength {
return fmt.Errorf("CFF: bad Private DICT offset")
}
privateDICT, err := parsePrivateDICT(b[topDICT.PrivateOffset:topDICT.PrivateOffset+topDICT.PrivateLength], false)
if err != nil {
return fmt.Errorf("CFF: Private DICT: %w", err)
}
localSubrsINDEX := &cffINDEX{}
if privateDICT.Subrs != 0 {
if len(b)-topDICT.PrivateOffset < privateDICT.Subrs {
return fmt.Errorf("CFF: bad Local Subrs INDEX offset")
}
r.Seek(uint32(topDICT.PrivateOffset + privateDICT.Subrs))
localSubrsINDEX, err = parseINDEX(r, false)
if err != nil {
return fmt.Errorf("CFF: Local Subrs INDEX: %w", err)
}
}
sfnt.CFF = &cffTable{
version: 1,
charStrings: charStringsINDEX,
globalSubrs: globalSubrsINDEX,
fonts: &cffFontINDEX{
privateDICT: []*cffPrivateDICT{privateDICT},
localSubrsINDEX: []*cffINDEX{localSubrsINDEX},
first: []uint32{0, uint32(charStringsINDEX.Len())},
fd: []uint16{0},
},
}
} else {
// CID font
fonts, err := parseFontINDEX(b, topDICT.FDArray, topDICT.FDSelect, charStringsINDEX.Len(), false)
if err != nil {
return fmt.Errorf("CFF: %w", err)
}
sfnt.CFF = &cffTable{
version: 1,
charStrings: charStringsINDEX,
globalSubrs: globalSubrsINDEX,
fonts: fonts,
}
}
return nil
}
func (sfnt *SFNT) parseCFF2() error {
return fmt.Errorf("CFF2: not supported")
b, ok := sfnt.Tables["CFF2"]
if !ok {
return fmt.Errorf("CFF2: missing table")
}
r := NewBinaryReader(b)
major := r.ReadUint8()
minor := r.ReadUint8()
if major != 2 || minor != 0 {
return fmt.Errorf("CFF2: bad version")
}
headerSize := r.ReadUint8()
if headerSize != 5 {
return fmt.Errorf("CFF2: bad headerSize")
}
topDictLength := r.ReadUint16()
topDICT, err := parseTopDICT2(r.ReadBytes(uint32(topDictLength)))
if err != nil {
return fmt.Errorf("CFF2: Top DICT: %w", err)
}
globalSubrsINDEX, err := parseINDEX(r, true)
if err != nil {
return fmt.Errorf("CFF2: Global Subrs INDEX: %w", err)
}
r.Seek(uint32(topDICT.CharStrings))
charStringsINDEX, err := parseINDEX(r, true)
if err != nil {
return fmt.Errorf("CFF2: CharStrings INDEX: %w", err)
}
fonts, err := parseFontINDEX(b, topDICT.FDArray, topDICT.FDSelect, charStringsINDEX.Len(), true)
if err != nil {
return fmt.Errorf("CFF2: %w", err)
}
sfnt.CFF = &cffTable{
version: 2,
charStrings: charStringsINDEX,
globalSubrs: globalSubrsINDEX,
fonts: fonts,
}
return nil
}
func (cff *cffTable) Version() int {
return cff.version
}
func (cff *cffTable) TopDICT() *cffTopDICT {
return cff.top
}
func (cff *cffTable) PrivateDICT(glyphID uint16) (*cffPrivateDICT, error) {
return cff.fonts.GetPrivate(uint32(glyphID))
}
func (cff *cffTable) ToPath(p Pather, glyphID, ppem uint16, x0, y0, f float64, hinting Hinting) error {
table := "CFF"
if cff.version == 2 {
table = "CFF2"
}
errBadNumOperands := fmt.Errorf("%v: bad number of operands for operator", table)
charString := cff.charStrings.Get(glyphID)
if charString == nil {
return fmt.Errorf("%v: bad glyphID %v", table, glyphID)
} else if 65525 < len(charString) {
return fmt.Errorf("%v: charstring too long", table)
}
localSubrs, err := cff.fonts.GetLocalSubrs(uint32(glyphID))
if err != nil {
return fmt.Errorf("%v: %w", table, err)
}
// x,y are raise to most-significant 16 bits and treat less-significant bits as fraction
var x, y int32
f /= float64(1 << 16) // correct back
hints := 0
stack := []int32{} // TODO: may overflow?
firstOperator := true
callStack := []*BinaryReader{}
r := NewBinaryReader(charString)
for {
if cff.version == 2 && r.Len() == 0 && 0 < len(callStack) {
// end of subroutine
r = callStack[len(callStack)-1]
callStack = callStack[:len(callStack)-1]
continue
} else if r.Len() == 0 {
break
}
b0 := int32(r.ReadUint8())
if 32 <= b0 || b0 == 28 {
var v int32
if b0 == 28 {
v = int32(r.ReadInt16()) << 16
} else if b0 < 32 {
} else if b0 < 247 {
v = (b0 - 139) << 16
} else if b0 < 251 {
b1 := int32(r.ReadUint8())
v = ((b0-247)*256 + b1 + 108) << 16
} else if b0 < 255 {
b1 := int32(r.ReadUint8())
v = (-(b0-251)*256 - b1 - 108) << 16
} else {
v = r.ReadInt32() // least-significant bits are fraction
}
if cff.version == 1 && 48 <= len(stack) || cff.version == 2 && 513 <= len(stack) {
return fmt.Errorf("%v: too many operands for operator", table)
}
stack = append(stack, v)
} else {
if firstOperator && cff.version == 1 && (b0 == 1 || b0 == 3 || b0 == 4 || b0 == 14 || 18 <= b0 && b0 <= 23) {
// optionally parse width
hasWidth := len(stack)%2 == 1
if b0 == 22 || b0 == 4 {
hasWidth = !hasWidth
}
if hasWidth {
stack = stack[1:]
}
}
if b0 != 29 && b0 != 10 && b0 != 11 {
// callgsubr, callsubr, and return don't influece the width operator
firstOperator = false
}
if b0 == 12 {
b0 = 256 + int32(r.ReadUint8())
}
switch b0 {
case 21:
// rmoveto
if len(stack) != 2 {
return errBadNumOperands
}
x += stack[0]
y += stack[1]
p.Close()
p.MoveTo(x0+f*float64(x), y0+f*float64(y))
stack = stack[:0]
case 22:
// hmoveto
if len(stack) != 1 {
return errBadNumOperands
}
x += stack[0]
p.Close()
p.MoveTo(x0+f*float64(x), y0+f*float64(y))
stack = stack[:0]
case 4:
// vmoveto
if len(stack) != 1 {
return errBadNumOperands
}
y += stack[0]
p.Close()
p.MoveTo(x0+f*float64(x), y0+f*float64(y))
stack = stack[:0]
case 5:
// rlineto
if len(stack) == 0 || len(stack)%2 != 0 {
return errBadNumOperands
}
for i := 0; i < len(stack); i += 2 {
x += stack[i+0]
y += stack[i+1]
p.LineTo(x0+f*float64(x), y0+f*float64(y))
}
stack = stack[:0]
case 6, 7:
// hlineto and vlineto
if len(stack) == 0 {
return errBadNumOperands
}
vertical := b0 == 7
for i := 0; i < len(stack); i++ {
if !vertical {
x += stack[i]
} else {
y += stack[i]
}
p.LineTo(x0+f*float64(x), y0+f*float64(y))
vertical = !vertical
}
stack = stack[:0]
case 8:
// rrcurveto
if len(stack) == 0 || len(stack)%6 != 0 {
return errBadNumOperands
}
for i := 0; i < len(stack); i += 6 {
x += stack[i+0]
y += stack[i+1]
cpx1, cpy1 := x, y
x += stack[i+2]
y += stack[i+3]
cpx2, cpy2 := x, y
x += stack[i+4]
y += stack[i+5]
p.CubeTo(x0+f*float64(cpx1), y0+f*float64(cpy1), x0+f*float64(cpx2), y0+f*float64(cpy2), x0+f*float64(x), y0+f*float64(y))
}
stack = stack[:0]
case 27, 26:
// hhcurvetp and vvcurveto
if len(stack) < 4 || len(stack)%4 != 0 && (len(stack)-1)%4 != 0 {
return errBadNumOperands
}
vertical := b0 == 26
i := 0
if len(stack)%4 == 1 {
if !vertical {
y += stack[0]
} else {
x += stack[0]
}
i++
}
for ; i < len(stack); i += 4 {
if !vertical {
x += stack[i+0]
} else {
y += stack[i+0]
}
cpx1, cpy1 := x, y
x += stack[i+1]
y += stack[i+2]
cpx2, cpy2 := x, y
if !vertical {
x += stack[i+3]
} else {
y += stack[i+3]
}
p.CubeTo(x0+f*float64(cpx1), y0+f*float64(cpy1), x0+f*float64(cpx2), y0+f*float64(cpy2), x0+f*float64(x), y0+f*float64(y))
}
stack = stack[:0]
case 31, 30:
// hvcurvetp and vhcurveto
if len(stack) < 4 || len(stack)%4 != 0 && (len(stack)-1)%4 != 0 {
return errBadNumOperands
}
vertical := b0 == 30
for i := 0; i < len(stack); i += 4 {
if !vertical {
x += stack[i+0]
} else {
y += stack[i+0]
}
cpx1, cpy1 := x, y
x += stack[i+1]
y += stack[i+2]
cpx2, cpy2 := x, y
if !vertical {
y += stack[i+3]
} else {
x += stack[i+3]
}
if i+5 == len(stack) {
if !vertical {
x += stack[i+4]
} else {
y += stack[i+4]
}
i++
}
p.CubeTo(x0+f*float64(cpx1), y0+f*float64(cpy1), x0+f*float64(cpx2), y0+f*float64(cpy2), x0+f*float64(x), y0+f*float64(y))
vertical = !vertical
}
stack = stack[:0]
case 24:
// rcurveline
if len(stack) < 2 || (len(stack)-2)%6 != 0 {
return errBadNumOperands
}
i := 0
for ; i < len(stack)-2; i += 6 {
x += stack[i+0]
y += stack[i+1]
cpx1, cpy1 := x, y
x += stack[i+2]
y += stack[i+3]
cpx2, cpy2 := x, y
x += stack[i+4]
y += stack[i+5]
p.CubeTo(x0+f*float64(cpx1), y0+f*float64(cpy1), x0+f*float64(cpx2), y0+f*float64(cpy2), x0+f*float64(x), y0+f*float64(y))
}
x += stack[i+0]
y += stack[i+1]
p.LineTo(x0+f*float64(x), y0+f*float64(y))
stack = stack[:0]
case 25:
// rlinecurve
if len(stack) < 6 || (len(stack)-6)%2 != 0 {
return errBadNumOperands
}
i := 0
for ; i < len(stack)-6; i += 2 {
x += stack[i+0]
y += stack[i+1]
p.LineTo(x0+f*float64(x), y0+f*float64(y))
}
x += stack[i+0]
y += stack[i+1]
cpx1, cpy1 := x, y
x += stack[i+2]
y += stack[i+3]
cpx2, cpy2 := x, y
x += stack[i+4]
y += stack[i+5]
p.CubeTo(x0+f*float64(cpx1), y0+f*float64(cpy1), x0+f*float64(cpx2), y0+f*float64(cpy2), x0+f*float64(x), y0+f*float64(y))
stack = stack[:0]
case 256 + 35:
// flex
if len(stack) != 13 {
return errBadNumOperands
}
// always use cubic Béziers
for i := 0; i < 12; i += 6 {
x += stack[i+0]
y += stack[i+1]
cpx1, cpy1 := x, y
x += stack[i+2]
y += stack[i+3]
cpx2, cpy2 := x, y
x += stack[i+4]
y += stack[i+5]
p.CubeTo(x0+f*float64(cpx1), y0+f*float64(cpy1), x0+f*float64(cpx2), y0+f*float64(cpy2), x0+f*float64(x), y0+f*float64(y))
}
stack = stack[:0]
case 256 + 34:
// hflex
if len(stack) != 7 {
return errBadNumOperands
}
// always use cubic Béziers
y1 := y
x += stack[0]
cpx1, cpy1 := x, y
x += stack[1]
y += stack[2]
cpx2, cpy2 := x, y
x += stack[3]
p.CubeTo(x0+f*float64(cpx1), y0+f*float64(cpy1), x0+f*float64(cpx2), y0+f*float64(cpy2), x0+f*float64(x), y0+f*float64(y))
x += stack[4]
cpx1, cpy1 = x, y
x += stack[5]
y = y1
cpx2, cpy2 = x, y
x += stack[6]
p.CubeTo(x0+f*float64(cpx1), y0+f*float64(cpy1), x0+f*float64(cpx2), y0+f*float64(cpy2), x0+f*float64(x), y0+f*float64(y))
stack = stack[:0]
case 256 + 36:
// hflex1
if len(stack) != 9 {
return errBadNumOperands
}
// always use cubic Béziers
y1 := y
x += stack[0]
y += stack[1]
cpx1, cpy1 := x, y
x += stack[2]
y += stack[3]
cpx2, cpy2 := x, y
x += stack[4]
p.CubeTo(x0+f*float64(cpx1), y0+f*float64(cpy1), x0+f*float64(cpx2), y0+f*float64(cpy2), x0+f*float64(x), y0+f*float64(y))
x += stack[5]
cpx1, cpy1 = x, y
x += stack[6]
y += stack[7]
cpx2, cpy2 = x, y
x += stack[8]
y = y1
p.CubeTo(x0+f*float64(cpx1), y0+f*float64(cpy1), x0+f*float64(cpx2), y0+f*float64(cpy2), x0+f*float64(x), y0+f*float64(y))
stack = stack[:0]
case 256 + 37:
// flex1
if len(stack) != 11 {
return errBadNumOperands
}
// always use cubic Béziers
x1, y1 := x, y
x += stack[0]
y += stack[1]
cpx1, cpy1 := x, y
x += stack[2]
y += stack[3]
cpx2, cpy2 := x, y
x += stack[4]
y += stack[5]
p.CubeTo(x0+f*float64(cpx1), y0+f*float64(cpy1), x0+f*float64(cpx2), y0+f*float64(cpy2), x0+f*float64(x), y0+f*float64(y))
x += stack[6]
y += stack[7]
cpx1, cpy1 = x, y
x += stack[8]
y += stack[9]
cpx2, cpy2 = x, y
dx, dy := x-x1, y-y1
if dx < 0 {
dx = -dx
}
if dy < 0 {
dy = -dy
}
if dy < dx {
x += stack[10]
y = y1
} else {
x = x1
y += stack[10]
}
p.CubeTo(x0+f*float64(cpx1), y0+f*float64(cpy1), x0+f*float64(cpx2), y0+f*float64(cpy2), x0+f*float64(x), y0+f*float64(y))
stack = stack[:0]
case 14:
// endchar
if cff.version == 2 {
return fmt.Errorf("CFF2: unsupported operator %d", b0)
} else if len(stack) == 4 {
return fmt.Errorf("CFF: unsupported endchar operands")
} else if len(stack) != 0 {
return errBadNumOperands
}
p.Close()
return nil
case 1, 3, 18, 23:
// hstem, vstem, hstemhm, vstemhm
if len(stack) < 2 || len(stack)%2 != 0 {
return errBadNumOperands
}
// hints are not used
hints += len(stack) / 2
if 96 < hints {
return fmt.Errorf("%v: too many stem hints", table)
}
stack = stack[:0]
case 19, 20:
// hintmask, cntrmask
if len(stack)%2 != 0 {
return errBadNumOperands
}
if 0 < len(stack) {
// vstem
hints += len(stack) / 2
if 96 < hints {
return fmt.Errorf("%v: too many stem hints", table)
}
stack = stack[:0]
}
r.ReadBytes(uint32((hints + 7) / 8))
// TODO: arithmetic, storage, and conditional operators for CFF version 1?
case 10, 29:
// callsubr and callgsubr
if 10 < len(callStack) {
return fmt.Errorf("%v: too many nested subroutines", table)
} else if len(stack) == 0 {
return errBadNumOperands
}
n := 0
if b0 == 10 {
n = len(localSubrs.offset) - 1
} else {
n = len(cff.globalSubrs.offset) - 1
}
i := stack[len(stack)-1] >> 16
if n < 1240 {
i += 107
} else if n < 33900 {
i += 1131
} else {
i += 32768
}
stack = stack[:len(stack)-1]
if i < 0 || math.MaxUint16 < i {
return fmt.Errorf("%v: bad subroutine", table)
}
var subr []byte
if b0 == 10 {
subr = localSubrs.Get(uint16(i))
} else {
subr = cff.globalSubrs.Get(uint16(i))
}
if subr == nil {
return fmt.Errorf("%v: bad subroutine", table)
} else if 65525 < len(charString) {
return fmt.Errorf("%v: subroutine too long", table)
}
callStack = append(callStack, r)
r = NewBinaryReader(subr)
firstOperator = true
case 11:
// return
if cff.version == 2 {
return fmt.Errorf("%v: unsupported operator %d", table, b0)
} else if len(callStack) == 0 {
return fmt.Errorf("%v: bad return", table)
}
r = callStack[len(callStack)-1]
callStack = callStack[:len(callStack)-1]
case 16:
// blend
if cff.version == 1 {
return fmt.Errorf("CFF: unsupported operator %d", b0)
}
// TODO: blend
case 15:
// vsindex
if cff.version == 1 {
return fmt.Errorf("CFF: unsupported operator %d", b0)
}
// TODO: vsindex
default:
if 256 <= b0 {
return fmt.Errorf("%v: unsupported operator 12 %d", table, b0-256)
}
return fmt.Errorf("%v: unsupported operator %d", table, b0)
}
}
}
if cff.version == 1 {
return fmt.Errorf("CFF: charstring must end with endchar operator")
}
return nil
}
type cffINDEX struct {
offset []uint32
data []byte
}
func (t *cffINDEX) Len() int {
return len(t.offset) - 1
}
func (t *cffINDEX) Get(i uint16) []byte {
if int(i) < t.Len() {
return t.data[t.offset[i]:t.offset[i+1]]
}
return nil
}
func (t *cffINDEX) GetSID(sid int) string {
// only for String INDEX
if sid < len(cffStandardStrings) {
return cffStandardStrings[sid]
}
sid -= len(cffStandardStrings)
if math.MaxUint16 < sid {
return ""
}
if b := t.Get(uint16(sid)); b != nil {
return string(b)
}
return ""
}
func parseINDEX(r *BinaryReader, isCFF2 bool) (*cffINDEX, error) {
t := &cffINDEX{}
var count uint32
if !isCFF2 {
count = uint32(r.ReadUint16())
} else {
count = r.ReadUint32()
}
if count == 0 {
// empty
return t, nil
} else if 1e6 < count {
return nil, fmt.Errorf("too big")
}
offSize := r.ReadUint8()
if offSize == 0 || 4 < offSize {
return nil, fmt.Errorf("bad offSize")
}
if r.Len() < uint32(offSize)*(uint32(count)+1) {
return nil, fmt.Errorf("bad data")
}
t.offset = make([]uint32, count+1)
if offSize == 1 {
for i := uint32(0); i < count+1; i++ {
t.offset[i] = uint32(r.ReadUint8()) - 1
}
} else if offSize == 2 {
for i := uint32(0); i < count+1; i++ {
t.offset[i] = uint32(r.ReadUint16()) - 1
}
} else if offSize == 3 {
for i := uint32(0); i < count+1; i++ {
t.offset[i] = uint32(r.ReadUint16())<<8 + uint32(r.ReadUint8()) - 1
}
} else {
for i := uint32(0); i < count+1; i++ {
t.offset[i] = r.ReadUint32() - 1
}
}
if r.Len() < t.offset[count] {
return nil, fmt.Errorf("bad data")
}
t.data = r.ReadBytes(t.offset[count])
return t, nil
}
type cffTopDICT struct {
IsSynthetic bool
IsCID bool
Version string
Notice string
Copyright string
FullName string
FamilyName string
Weight string
IsFixedPitch bool
ItalicAngle float64
UnderlinePosition float64
UnderlineThickness float64
PaintType int
CharstringType int
FontMatrix [6]float64
UniqueID int
FontBBox [4]float64
StrokeWidth float64
XUID []int
Charset int
Encoding int
CharStrings int
PrivateOffset int
PrivateLength int
SyntheticBase int
PostScript string
BaseFontName string
BaseFontBlend []int
ROS1 string
ROS2 string
ROS3 int
CIDFontVersion int
CIDFontRevision int
CIDFontType int
CIDCount int
UIDBase int
FDArray int
FDSelect int
FontName string
Vstore int // CFF2
}
type cffFontDICT struct {
PrivateOffset int
PrivateLength int
}
type cffPrivateDICT struct {
BlueValues []float64
OtherBlues []float64
FamilyBlues []float64
FamilyOtherBlues []float64
BlueScale float64
BlueShift float64
BlueFuzz float64
StdHW float64
StdVW float64
StemSnapH []float64
StemSnapV []float64
ForceBold bool
LanguageGroup int
ExpansionFactor float64
InitialRandomSeed int
Subrs int
DefaultWidthX float64
NominalWidthX float64
// CFF2
Vsindex int
Blend []float64
}
func parseTopDICT(b []byte, stringINDEX *cffINDEX) (*cffTopDICT, error) {
dict := &cffTopDICT{
UnderlinePosition: -100,
UnderlineThickness: 50,
CharstringType: 2,
FontMatrix: [6]float64{0.001, 0.0, 0.0, 0.001, 0.0, 0.0},
CIDCount: 8720,
}
return dict, parseDICT(b, false, func(b0 int, is []int, fs []float64) bool {
switch b0 {
case 0:
dict.Version = stringINDEX.GetSID(is[0])
case 1:
dict.Notice = stringINDEX.GetSID(is[0])
case 256 + 0:
dict.Copyright = stringINDEX.GetSID(is[0])
case 2:
dict.FullName = stringINDEX.GetSID(is[0])
case 3:
dict.FamilyName = stringINDEX.GetSID(is[0])
case 4:
dict.Weight = stringINDEX.GetSID(is[0])
case 256 + 1:
dict.IsFixedPitch = is[0] != 0
case 256 + 2:
dict.ItalicAngle = fs[0]
case 256 + 3:
dict.UnderlinePosition = fs[0]
case 256 + 4:
dict.UnderlineThickness = fs[0]
case 256 + 5:
dict.PaintType = is[0]
case 256 + 6:
dict.CharstringType = is[0]
case 256 + 7:
copy(dict.FontMatrix[:], fs)
case 13:
dict.UniqueID = is[0]
case 5:
copy(dict.FontBBox[:], fs)
case 256 + 8:
dict.StrokeWidth = fs[0]
case 14:
dict.XUID = is
case 15:
dict.Charset = is[0]
case 16:
dict.Encoding = is[0]
case 17:
dict.CharStrings = is[0]
case 18:
dict.PrivateOffset = is[1]
dict.PrivateLength = is[0]
case 256 + 20:
dict.IsSynthetic = true
dict.SyntheticBase = is[0]
case 256 + 21:
dict.PostScript = stringINDEX.GetSID(is[0])
case 256 + 22:
dict.BaseFontName = stringINDEX.GetSID(is[0])
case 256 + 23:
dict.BaseFontBlend = is
case 256 + 30:
// TODO: it is unclear how the ROS operator influences the GIDs/CIDs
dict.IsCID = true
dict.ROS1 = stringINDEX.GetSID(is[0])
dict.ROS2 = stringINDEX.GetSID(is[1])
dict.ROS3 = is[2]
case 256 + 31:
dict.CIDFontVersion = is[0]
case 256 + 32:
dict.CIDFontRevision = is[0]
case 256 + 33:
dict.CIDFontType = is[0]
case 256 + 34:
dict.CIDCount = is[0]
case 256 + 35:
dict.UIDBase = is[0]
case 256 + 36:
dict.FDArray = is[0]
case 256 + 37:
dict.FDSelect = is[0]
case 256 + 38:
dict.FontName = stringINDEX.GetSID(is[0])
default:
return false
}
return true
})
}
func parseFontDICT(b []byte, isCFF2 bool) (*cffFontDICT, error) {
dict := &cffFontDICT{}
return dict, parseDICT(b, isCFF2, func(b0 int, is []int, fs []float64) bool {
switch b0 {
case 18:
dict.PrivateOffset = is[1]
dict.PrivateLength = is[0]
case 256 + 7:
// FontMatrix
case 256 + 38:
// FontName
default:
return false
}
return true
})
}
func parsePrivateDICT(b []byte, isCFF2 bool) (*cffPrivateDICT, error) {
dict := &cffPrivateDICT{
BlueScale: 0.039625,
BlueShift: 7.0,
BlueFuzz: 1.0,
ExpansionFactor: 0.06,
}
return dict, parseDICT(b, isCFF2, func(b0 int, is []int, fs []float64) bool {
switch b0 {
case 6:
dict.BlueValues = fs
case 7:
dict.OtherBlues = fs
case 8:
dict.FamilyBlues = fs
case 9:
dict.FamilyOtherBlues = fs
case 256 + 9:
dict.BlueScale = fs[0]
case 256 + 10:
dict.BlueShift = fs[0]
case 256 + 11:
dict.BlueFuzz = fs[0]
case 10:
dict.StdHW = fs[0]
case 11:
dict.StdVW = fs[0]
case 256 + 12:
dict.StemSnapH = fs
case 256 + 13:
dict.StemSnapV = fs
case 256 + 14:
dict.ForceBold = is[0] != 0
case 256 + 17:
dict.LanguageGroup = is[0]
case 256 + 18:
dict.ExpansionFactor = fs[0]
case 256 + 19:
dict.InitialRandomSeed = is[0]
case 19:
dict.Subrs = is[0]
case 20:
dict.DefaultWidthX = fs[0]
case 21:
dict.NominalWidthX = fs[0]
case 22:
dict.Vsindex = is[0]
case 23:
dict.Blend = fs
default:
return false
}
return true
})
}
func parseTopDICT2(b []byte) (*cffTopDICT, error) {
dict := &cffTopDICT{
FontMatrix: [6]float64{0.001, 0.0, 0.0, 0.001, 0.0, 0.0},
}
return dict, parseDICT(b, true, func(b0 int, is []int, fs []float64) bool {
switch b0 {
case 256 + 7:
copy(dict.FontMatrix[:], fs)
case 17:
dict.CharStrings = is[0]
case 256 + 36:
dict.FDArray = is[0]
case 256 + 37:
dict.FDSelect = is[0]
case 24:
dict.Vstore = is[0]
default:
return false