-
Notifications
You must be signed in to change notification settings - Fork 3
/
sfnt_cff.go
2298 lines (2139 loc) · 58.8 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 (
"bytes"
"fmt"
"math"
"sort"
"strconv"
"github.com/tdewolff/parse/v2"
)
// TODO: add method to regenerate subrs optimally (useful after subset)
// TODO: use FDSelect for Font DICTs
// TODO: CFF has winding rule even-odd? CFF2 has winding rule nonzero
var ErrBadNumOperands = fmt.Errorf("bad number of operands for operator")
type cffTable struct {
version int
name string
top *cffTopDICT
globalSubrs *cffINDEX
charset []string
charStrings *cffINDEX
fonts *cffFontINDEX
}
func (sfnt *SFNT) parseCFF() error {
b, ok := sfnt.Tables["CFF "]
if !ok {
return fmt.Errorf("CFF: missing table")
}
r := parse.NewBinaryReader(b)
major := r.ReadUint8()
minor := r.ReadUint8()
if major != 1 || minor != 0 {
return fmt.Errorf("CFF: bad version")
}
hdrSize := r.ReadUint8()
if hdrSize != 4 {
return fmt.Errorf("CFF: bad hdrSize")
}
offSize := r.ReadUint8() // offSize, not actually used
if offSize == 0 || 4 < offSize {
return fmt.Errorf("CFF: bad offSize")
}
nameINDEX, err := parseINDEX(r, false)
if err != nil {
return fmt.Errorf("CFF: Name INDEX: %w", err)
} else 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)
} else 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)
}
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)
}
globalSubrsINDEX, err := parseINDEX(r, false)
if err != nil {
return fmt.Errorf("CFF: Global Subrs INDEX: %w", err)
}
r.Seek(uint32(topDICT.CharStrings))
charStringsINDEX, err := parseINDEX(r, false)
if err != nil {
return fmt.Errorf("CFF: CharStrings INDEX: %w", err)
}
var charset []string
if topDICT.Charset < 3 {
if topDICT.Charset != 0 {
// 1 is Expert and 2 is ExpertSubset predefined glyph names
return fmt.Errorf("CFF: Charset offset of %d not currently supported", topDICT.Charset)
}
charset = make([]string, charStringsINDEX.Len())
copy(charset, cffStandardStrings)
} else if 1 < charStringsINDEX.Len() {
r.Seek(uint32(topDICT.Charset))
if r.Len() == 0 {
return fmt.Errorf("CFF: bad Charset")
}
numGlyphs := charStringsINDEX.Len()
charset = make([]string, numGlyphs)
charset[0] = ".notdef"
format := r.ReadUint8()
switch format {
case 0:
if r.Len() < uint32(2*(numGlyphs-1)) {
return fmt.Errorf("CFF: bad Charset format 0")
}
for i := 1; i < numGlyphs; i++ {
sid := r.ReadUint16()
charset[i] = stringINDEX.GetSID(int(sid))
}
case 1, 2:
for i := 1; i < numGlyphs; i++ {
if r.Len() < 3 {
return fmt.Errorf("CFF: bad Charset format %d", format)
}
first := int(r.ReadUint16())
nLeft := 0
if format == 1 {
nLeft = int(r.ReadUint8())
} else {
nLeft = int(r.ReadUint16())
}
if numGlyphs < i+nLeft+1 {
return fmt.Errorf("CFF: bad Charset format %d", format)
}
for j := 0; j < nLeft+1; j++ {
charset[i+j] = stringINDEX.GetSID(first + j)
}
i += nLeft
}
default:
return fmt.Errorf("CFF: unknown Charset format %d", format)
}
}
sfnt.CFF = &cffTable{
version: 1,
name: string(nameINDEX.Get(0)),
top: topDICT,
globalSubrs: globalSubrsINDEX,
charset: charset,
charStrings: charStringsINDEX,
}
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.fonts = &cffFontINDEX{
private: []*cffPrivateDICT{privateDICT},
localSubrs: []*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.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 := parse.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) {
privateDICT := cff.fonts.GetPrivate(glyphID)
if privateDICT == nil {
return nil, fmt.Errorf("bad glyph ID %v for Private DICT", glyphID)
}
return privateDICT, nil
}
func (cff *cffTable) GlyphName(glyphID uint16) (string, bool) {
if int(glyphID) < len(cff.charset) {
return cff.charset[glyphID], true
}
return "", false
}
func (cff *cffTable) SetGlyphName(glyphID uint16, name string) {
if int(glyphID) < len(cff.charset) {
cff.charset[glyphID] = name
}
}
func (cff *cffTable) SetGlyphNames(names []string) {
cff.charset = names
}
const (
cffHstem int32 = 1
cffVstem int32 = 3
cffVmoveto int32 = 4
cffRlineto int32 = 5
cffHlineto int32 = 6
cffVlineto int32 = 7
cffRrcurveto int32 = 8
cffCallsubr int32 = 10
cffReturn int32 = 11
cffEscape int32 = 12
cffEndchar int32 = 14
cffHstemhm int32 = 18
cffHintmask int32 = 19
cffCntrmask int32 = 20
cffRmoveto int32 = 21
cffHmoveto int32 = 22
cffVstemhm int32 = 23
cffRcurveline int32 = 24
cffRlinecurve int32 = 25
cffVvcurveto int32 = 26
cffHhcurveto int32 = 27
cffShortint int32 = 28
cffCallgsubr int32 = 29
cffVhcurveto int32 = 30
cffHvcurveto int32 = 31
cffAnd int32 = 256 + 3
cffOr int32 = 256 + 4
cffNot int32 = 256 + 5
cffAbs int32 = 256 + 9
cffAdd int32 = 256 + 10
cffSub int32 = 256 + 11
cffDiv int32 = 256 + 12
cffNeg int32 = 256 + 14
cffEq int32 = 256 + 15
cffDrop int32 = 256 + 18
cffPut int32 = 256 + 20
cffGet int32 = 256 + 21
cffIfelse int32 = 256 + 22
cffRandom int32 = 256 + 23
cffMul int32 = 256 + 24
cffSqrt int32 = 256 + 26
cffDup int32 = 256 + 27
cffExch int32 = 256 + 28
cffIndex int32 = 256 + 29
cffRoll int32 = 256 + 30
cffHflex int32 = 256 + 34
cffFlex int32 = 256 + 35
cffHflex1 int32 = 256 + 36
cffFlex1 int32 = 256 + 37
cff2Vsindex int32 = 15
cff2Blend int32 = 16
)
func cffReadCharStringNumber(r *parse.BinaryReader, b0 int32) int32 {
var v int32
if b0 == 28 {
v = int32(r.ReadInt16()) << 16
} 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 16 bits are fraction
}
return v
}
func (cff *cffTable) getSubroutine(glyphID uint16, b0 int32, stack int32) (int32, []byte, error) {
typ := "local"
var subrs *cffINDEX
if b0 == cffCallsubr {
subrs = cff.fonts.GetLocalSubrs(glyphID)
if subrs == nil {
return 0, nil, fmt.Errorf("%v subroutine: glyph's font doesn't have local subroutines", typ)
}
} else {
typ = "global"
subrs = cff.globalSubrs
}
// add bias
n := subrs.Len()
index := stack >> 16
if n < 1240 {
index += 107
} else if n < 33900 {
index += 1131
} else {
index += 32768
}
if index < 0 || math.MaxUint16 < index {
return 0, nil, fmt.Errorf("%v subroutine: bad index %v", typ, index)
}
// get subroutine charString
subr := subrs.Get(uint16(index))
if subr == nil {
return 0, nil, fmt.Errorf("%v subroutine: %v doesn't exist", typ, index)
} else if 65535 < len(subr) {
return 0, nil, fmt.Errorf("%v subroutine: %v too long", typ, index)
}
return index, subr, nil
}
func (cff *cffTable) parseCharString(glyphID uint16, cb func(*parse.BinaryReader, int32, []int32) error) error {
table := "CFF"
if cff.version == 2 {
table = "CFF2"
}
charString := cff.charStrings.Get(glyphID)
if charString == nil {
return fmt.Errorf("%v: bad glyphID %v", table, glyphID)
} else if 65535 < len(charString) {
return fmt.Errorf("%v: charstring too long", table)
}
callStack := []*parse.BinaryReader{}
r := parse.NewBinaryReader(charString)
hints := 0
stack := []int32{}
beforeMoveto := true
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 == cffShortint {
v := cffReadCharStringNumber(r, b0)
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 b0 == cffEscape {
b0 = 256 + int32(r.ReadUint8())
}
if beforeMoveto && cff.version == 1 {
if b0 == cffHstem || b0 == cffVstem || b0 == cffVmoveto || b0 == cffEndchar || cffHstemhm <= b0 && b0 <= cffVstemhm {
// stack clearing operators, parse optional width
hasWidth := len(stack)%2 == 1
if b0 == cffHmoveto || b0 == cffVmoveto {
hasWidth = !hasWidth
}
if hasWidth {
stack = stack[1:]
}
if b0 == cffRmoveto || b0 == cffHmoveto || b0 == cffVmoveto {
beforeMoveto = false
}
} else if b0 != cffCallsubr && b0 != cffCallgsubr && b0 != cffReturn {
// return could be of main charstring or of subroutine
return fmt.Errorf("%v: unexpected operator %d before moveto", table, b0)
}
} else if !beforeMoveto && cff.version == 1 && (b0 == cffHstem || b0 == cffVstem || b0 == cffHstemhm || b0 == cffVstemhm) {
return fmt.Errorf("%v: unexpected operator %d after moveto", table, b0)
}
if err := cb(r, b0, stack); err != nil {
return fmt.Errorf("%v: %v", table, err)
}
// handle hint and subroutine operators, clear stack for most operators
switch b0 {
case cffEndchar:
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 fmt.Errorf("%v: %v", table, ErrBadNumOperands)
}
return nil
case cffHstem, cffVstem, cffHstemhm, cffVstemhm:
if len(stack) < 2 || len(stack)%2 != 0 {
return fmt.Errorf("%v: %v", table, ErrBadNumOperands)
}
hints += len(stack) / 2
if 96 < hints {
return fmt.Errorf("%v: too many stem hints", table)
}
stack = stack[:0]
case cffHintmask, cffCntrmask:
if b0 == cffHintmask && 0 < len(stack) {
// vstem or vstemhm
if len(stack)%2 != 0 {
return fmt.Errorf("%v: %v", table, ErrBadNumOperands)
}
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))
stack = stack[:0]
case cffCallsubr, cffCallgsubr:
// callsubr and callgsubr
if 10 < len(callStack) {
return fmt.Errorf("%v: too many nested subroutines", table)
} else if len(stack) == 0 {
return fmt.Errorf("%v: %v", table, ErrBadNumOperands)
}
_, subr, err := cff.getSubroutine(glyphID, b0, stack[len(stack)-1])
if err != nil {
return fmt.Errorf("%v: %v", table, err)
}
stack = stack[:len(stack)-1]
callStack = append(callStack, r)
r = parse.NewBinaryReader(subr)
case cffReturn:
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 cffRmoveto, cffHmoveto, cffVmoveto, cffRlineto, cffHlineto, cffVlineto, cffRrcurveto, cffHhcurveto, cffHvcurveto, cffRcurveline, cffRlinecurve, cffVhcurveto, cffVvcurveto, cffFlex, cffHflex, cffHflex1, cffFlex1:
// path contruction operators
stack = stack[:0]
case cff2Blend:
// blend
if cff.version == 1 {
return fmt.Errorf("CFF: unsupported operator %d", b0)
}
// TODO: blend
case cff2Vsindex:
// vsindex
if cff.version == 1 {
return fmt.Errorf("CFF: unsupported operator %d", b0)
}
// TODO: vsindex
default:
// TODO: arithmetic, storage, and conditional operators for CFF version 1?
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
}
func (cff *cffTable) ToPath(p Pather, glyphID, ppem uint16, x0, y0, f float64, hinting Hinting) error {
// x,y are raised to most-significant 16 bits and treat less-significant bits as fraction
var x, y int32
f /= float64(1 << 16) // correct back
err := cff.parseCharString(glyphID, func(_ *parse.BinaryReader, b0 int32, stack []int32) error {
switch b0 {
case cffRmoveto:
if len(stack) != 2 {
return ErrBadNumOperands
}
x += stack[0]
y += stack[1]
p.Close()
p.MoveTo(x0+f*float64(x), y0+f*float64(y))
case cffHmoveto:
if len(stack) != 1 {
return ErrBadNumOperands
}
x += stack[0]
p.Close()
p.MoveTo(x0+f*float64(x), y0+f*float64(y))
case cffVmoveto:
if len(stack) != 1 {
return ErrBadNumOperands
}
y += stack[0]
p.Close()
p.MoveTo(x0+f*float64(x), y0+f*float64(y))
case cffRlineto:
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))
}
case cffHlineto, cffVlineto:
if len(stack) == 0 {
return ErrBadNumOperands
}
vertical := b0 == cffVlineto
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
}
case cffRrcurveto:
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))
}
case cffHhcurveto, cffVvcurveto:
if len(stack) < 4 || len(stack)%4 != 0 && (len(stack)-1)%4 != 0 {
return ErrBadNumOperands
}
vertical := b0 == cffVvcurveto
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))
}
case cffHvcurveto, cffVhcurveto:
if len(stack) < 4 || len(stack)%4 != 0 && (len(stack)-1)%4 != 0 {
return ErrBadNumOperands
}
vertical := b0 == cffVhcurveto
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
}
case cffRcurveline:
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))
case cffRlinecurve:
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))
case cffFlex:
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))
}
case cffHflex:
// 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))
case cffHflex1:
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))
case cffFlex1:
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))
}
return nil
})
if err != nil {
return err
}
p.Close()
return nil
}
func cffCharStringSubrsBias(n int) int {
bias := 32768
if n < 1240 {
bias = 107
} else if n < 33900 {
bias = 1131
}
return bias
}
func cffNumberSize(i int) int {
if -107 <= i && i <= 107 {
return 1
} else if -1131 <= i && i <= -108 || 108 <= i && i <= 1131 {
return 2
} else if -32767 <= i && i <= 32767 {
return 3
}
return 5
}
type cffSubrIndexChange struct {
start, end uint32
index int32
}
// updateSubrs changes all indices to local and global subroutines given the mappings for both
func (cff *cffTable) updateSubrs(localSubrsMap, globalSubrsMap map[int32]int32, localSubrs, globalSubrs *cffINDEX) error {
if 1 < len(cff.fonts.localSubrs) {
return fmt.Errorf("only single-font CFFs are supported")
} else if len(localSubrsMap) == 0 && len(globalSubrsMap) == 0 {
return nil
}
oldLocalSubrsLen := 0
if 0 < len(cff.fonts.localSubrs) {
oldLocalSubrsLen = cff.fonts.localSubrs[0].Len()
}
oldGlobalSubrsLen := cff.globalSubrs.Len()
oldLocalSubrsBias := int32(cffCharStringSubrsBias(oldLocalSubrsLen))
oldGlobalSubrsBias := int32(cffCharStringSubrsBias(oldGlobalSubrsLen))
localSubrsHandled := map[int32]bool{} // old indices
globalSubrsHandled := map[int32]bool{} // old indices
localSubrsBias := int32(cffCharStringSubrsBias(localSubrs.Len()))
globalSubrsBias := int32(cffCharStringSubrsBias(globalSubrs.Len()))
indexChanges := map[*cffINDEX][]cffSubrIndexChange{}
var indexStack []*cffINDEX
var offsetStack []uint32
numGlyphs := uint16(cff.charStrings.Len())
for glyphID := uint16(0); glyphID < numGlyphs; glyphID++ {
// Change subroutine indices in the BinaryReader, this will make parseCharString use the
// new index to pick the right subroutine. localSubrs and globalSubrs must thus already
// have the new order/content.
skipDepth := 0
indexStack = append(indexStack[:0], cff.charStrings)
offsetStack = append(offsetStack[:0], cff.charStrings.offset[glyphID])
err := cff.parseCharString(glyphID, func(r *parse.BinaryReader, b0 int32, stack []int32) error {
if b0 == cffCallsubr || b0 == cffCallgsubr {
if len(stack) == 0 {
return ErrBadNumOperands
}
num := stack[len(stack)-1]
oldIndex, _, err := cff.getSubroutine(glyphID, b0, num)
if err != nil {
return err
}
mapped := false
var oldBias int32
var newIndex, newBias int32
if b0 == cffCallsubr {
if v, ok := localSubrsMap[oldIndex]; ok {
oldBias = oldLocalSubrsBias
newIndex = v
newBias = localSubrsBias
mapped = true
}
} else {
if v, ok := globalSubrsMap[oldIndex]; ok {
oldBias = oldGlobalSubrsBias
newIndex = v
newBias = globalSubrsBias
mapped = true
}
}
if skipDepth == 0 && mapped && oldIndex != newIndex {
lenNumber := uint32(cffNumberSize(int(oldIndex - oldBias)))
posNumber := r.Pos() - 1 - lenNumber // -1 as we're past the operator
index := indexStack[len(indexStack)-1]
offset := offsetStack[len(offsetStack)-1]
indexChanges[index] = append(indexChanges[index], cffSubrIndexChange{
start: offset + posNumber,
end: offset + posNumber + lenNumber,
index: newIndex - newBias,
})
}
// only update subroutines once
if 0 < skipDepth {
skipDepth++
return nil
} else if b0 == cffCallsubr {
if localSubrsHandled[oldIndex] {
skipDepth++
return nil
} else {
localSubrsHandled[oldIndex] = true
}
} else {
if globalSubrsHandled[oldIndex] {
skipDepth++
return nil
} else {
globalSubrsHandled[oldIndex] = true
}
}
var index *cffINDEX
if b0 == cffCallsubr {
index = localSubrs
} else {
index = globalSubrs
}
indexStack = append(indexStack, index)
offsetStack = append(offsetStack, index.offset[newIndex])
} else if b0 == cffReturn {
if 0 < skipDepth {
skipDepth--
return nil
}
indexStack = indexStack[:len(indexStack)-1]
offsetStack = offsetStack[:len(offsetStack)-1]
}
return nil
})
if err != nil {
return err
}
}
for index, changes := range indexChanges {
sort.Slice(changes, func(i, j int) bool {
return changes[i].start < changes[j].start
})
k := 1 // index into index.offset
offset := uint32(0) // index into index.data
diff := int32(0) // total number of bytes grown
data := make([]byte, 0, len(index.data)) // destination, usually same size or shrinks
for _, ch := range changes {
// update index.offset upto the current change
for k < len(index.offset) && index.offset[k] <= ch.start {
index.offset[k] = uint32(int32(index.offset[k]) + diff)
k++
}
// move bytes before current change
data = append(data, index.data[offset:ch.start]...)
// write new number
n := len(data)
if -107 <= ch.index && ch.index <= 107 {
data = append(data, uint8(ch.index+139))
} else if 108 <= ch.index && ch.index <= 1131 {
ch.index -= 108
data = append(data, uint8(ch.index/256+247))
data = append(data, uint8(ch.index%256))
} else if -1131 <= ch.index && ch.index <= -108 {
ch.index = -ch.index - 108
data = append(data, uint8(ch.index/256+251))
data = append(data, uint8(ch.index%256))
} else if -32768 <= ch.index && ch.index <= 32767 {
data = append(data, 28, uint8(ch.index>>8), uint8(ch.index))
} else {
return fmt.Errorf("subroutine index outside valid range")
}
diff += int32(len(data)-n) - int32(ch.end-ch.start)
offset = ch.end
}
// update index.offset upto the current change
if diff != 0 {
for k < len(index.offset) {
index.offset[k] = uint32(int32(index.offset[k]) + diff)