-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfontloader-digitalkhatt-cff2.lua
2678 lines (2511 loc) · 87.8 KB
/
fontloader-digitalkhatt-cff2.lua
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
if not modules then modules = { } end modules ['font-cff2-digitalkhatt'] = {
version = 1.001,
optimize = true,
comment = "companion to font-ini.mkiv",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
-- todo: option.outlines
-- todo: option.boundingbox
-- per charstring (less memory)
-- This is a heavy one as it is a rather packed format. We don't need al the information
-- now but we might need it later (who know what magic we can do with metapost). So at
-- some point this might become a module. We just follow Adobe Technical Notes #5176 and
-- #5177. In case of doubt I looked in the fontforge code that comes with LuaTeX but
-- it's not the easiest source to read (and doesn't cover cff2).
-- For now we save the segments in a list of segments with the operator last in an entry
-- because that reflects the original. But it might make more sense to use a single array
-- per segment. For pdf a simple concat works ok, but for other purposes a operator first
-- flush is nicer.
--
-- In retrospect I could have looked into the backend code of LuaTeX but it never
-- occurred to me that parsing charstrings was needed there (which has to to
-- with merging subroutines and flattening, not so much with calculations.) On
-- the other hand, we can now feed back cff2 stuff.
local next, type, tonumber, rawget = next, type, tonumber, rawget
local byte, char, gmatch, sub = string.byte, string.char, string.gmatch, string.sub
local concat, insert, remove, unpack = table.concat, table.insert, table.remove, table.unpack
local floor, abs, round, ceil, min, max = math.floor, math.abs, math.round, math.ceil, math.min, math.max
local P, C, R, S, C, Cs, Ct = lpeg.P, lpeg.C, lpeg.R, lpeg.S, lpeg.C, lpeg.Cs, lpeg.Ct
local lpegmatch = lpeg.match
local formatters = string.formatters
local bytetable = string.bytetable
local idiv = number.idiv
local rshift, band, extract = bit32.rshift, bit32.band, bit32.extract
local readers = fonts.handlers.otf.readers
local streamreader = readers.streamreader
local readstring = streamreader.readstring
local readbyte = streamreader.readcardinal1 -- 8-bit unsigned integer
local readushort = streamreader.readcardinal2 -- 16-bit unsigned integer
local readuint = streamreader.readcardinal3 -- 24-bit unsigned integer
local readulong = streamreader.readcardinal4 -- 32-bit unsigned integer
local setposition = streamreader.setposition
local getposition = streamreader.getposition
local readbytetable = streamreader.readbytetable
local setmetatableindex = table.setmetatableindex
local trace_charstrings = false trackers.register("fonts.cff.charstrings",function(v) trace_charstrings = v end)
local report = logs.reporter("otf reader","cff")
local parsedictionaries
local parsecharstring
local parsecharstrings
local resetcharstrings
local parseprivates
local startparsing
local stopparsing
local defaultstrings = { [0] = -- taken from ff
".notdef", "space", "exclam", "quotedbl", "numbersign", "dollar", "percent",
"ampersand", "quoteright", "parenleft", "parenright", "asterisk", "plus",
"comma", "hyphen", "period", "slash", "zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine", "colon", "semicolon", "less",
"equal", "greater", "question", "at", "A", "B", "C", "D", "E", "F", "G", "H",
"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
"X", "Y", "Z", "bracketleft", "backslash", "bracketright", "asciicircum",
"underscore", "quoteleft", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y",
"z", "braceleft", "bar", "braceright", "asciitilde", "exclamdown", "cent",
"sterling", "fraction", "yen", "florin", "section", "currency",
"quotesingle", "quotedblleft", "guillemotleft", "guilsinglleft",
"guilsinglright", "fi", "fl", "endash", "dagger", "daggerdbl",
"periodcentered", "paragraph", "bullet", "quotesinglbase", "quotedblbase",
"quotedblright", "guillemotright", "ellipsis", "perthousand", "questiondown",
"grave", "acute", "circumflex", "tilde", "macron", "breve", "dotaccent",
"dieresis", "ring", "cedilla", "hungarumlaut", "ogonek", "caron", "emdash",
"AE", "ordfeminine", "Lslash", "Oslash", "OE", "ordmasculine", "ae",
"dotlessi", "lslash", "oslash", "oe", "germandbls", "onesuperior",
"logicalnot", "mu", "trademark", "Eth", "onehalf", "plusminus", "Thorn",
"onequarter", "divide", "brokenbar", "degree", "thorn", "threequarters",
"twosuperior", "registered", "minus", "eth", "multiply", "threesuperior",
"copyright", "Aacute", "Acircumflex", "Adieresis", "Agrave", "Aring",
"Atilde", "Ccedilla", "Eacute", "Ecircumflex", "Edieresis", "Egrave",
"Iacute", "Icircumflex", "Idieresis", "Igrave", "Ntilde", "Oacute",
"Ocircumflex", "Odieresis", "Ograve", "Otilde", "Scaron", "Uacute",
"Ucircumflex", "Udieresis", "Ugrave", "Yacute", "Ydieresis", "Zcaron",
"aacute", "acircumflex", "adieresis", "agrave", "aring", "atilde",
"ccedilla", "eacute", "ecircumflex", "edieresis", "egrave", "iacute",
"icircumflex", "idieresis", "igrave", "ntilde", "oacute", "ocircumflex",
"odieresis", "ograve", "otilde", "scaron", "uacute", "ucircumflex",
"udieresis", "ugrave", "yacute", "ydieresis", "zcaron", "exclamsmall",
"Hungarumlautsmall", "dollaroldstyle", "dollarsuperior", "ampersandsmall",
"Acutesmall", "parenleftsuperior", "parenrightsuperior", "twodotenleader",
"onedotenleader", "zerooldstyle", "oneoldstyle", "twooldstyle",
"threeoldstyle", "fouroldstyle", "fiveoldstyle", "sixoldstyle",
"sevenoldstyle", "eightoldstyle", "nineoldstyle", "commasuperior",
"threequartersemdash", "periodsuperior", "questionsmall", "asuperior",
"bsuperior", "centsuperior", "dsuperior", "esuperior", "isuperior",
"lsuperior", "msuperior", "nsuperior", "osuperior", "rsuperior", "ssuperior",
"tsuperior", "ff", "ffi", "ffl", "parenleftinferior", "parenrightinferior",
"Circumflexsmall", "hyphensuperior", "Gravesmall", "Asmall", "Bsmall",
"Csmall", "Dsmall", "Esmall", "Fsmall", "Gsmall", "Hsmall", "Ismall",
"Jsmall", "Ksmall", "Lsmall", "Msmall", "Nsmall", "Osmall", "Psmall",
"Qsmall", "Rsmall", "Ssmall", "Tsmall", "Usmall", "Vsmall", "Wsmall",
"Xsmall", "Ysmall", "Zsmall", "colonmonetary", "onefitted", "rupiah",
"Tildesmall", "exclamdownsmall", "centoldstyle", "Lslashsmall",
"Scaronsmall", "Zcaronsmall", "Dieresissmall", "Brevesmall", "Caronsmall",
"Dotaccentsmall", "Macronsmall", "figuredash", "hypheninferior",
"Ogoneksmall", "Ringsmall", "Cedillasmall", "questiondownsmall", "oneeighth",
"threeeighths", "fiveeighths", "seveneighths", "onethird", "twothirds",
"zerosuperior", "foursuperior", "fivesuperior", "sixsuperior",
"sevensuperior", "eightsuperior", "ninesuperior", "zeroinferior",
"oneinferior", "twoinferior", "threeinferior", "fourinferior",
"fiveinferior", "sixinferior", "seveninferior", "eightinferior",
"nineinferior", "centinferior", "dollarinferior", "periodinferior",
"commainferior", "Agravesmall", "Aacutesmall", "Acircumflexsmall",
"Atildesmall", "Adieresissmall", "Aringsmall", "AEsmall", "Ccedillasmall",
"Egravesmall", "Eacutesmall", "Ecircumflexsmall", "Edieresissmall",
"Igravesmall", "Iacutesmall", "Icircumflexsmall", "Idieresissmall",
"Ethsmall", "Ntildesmall", "Ogravesmall", "Oacutesmall", "Ocircumflexsmall",
"Otildesmall", "Odieresissmall", "OEsmall", "Oslashsmall", "Ugravesmall",
"Uacutesmall", "Ucircumflexsmall", "Udieresissmall", "Yacutesmall",
"Thornsmall", "Ydieresissmall", "001.000", "001.001", "001.002", "001.003",
"Black", "Bold", "Book", "Light", "Medium", "Regular", "Roman", "Semibold",
}
local standardnames = { [0] = -- needed for seac
false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false,
"space", "exclam", "quotedbl", "numbersign", "dollar", "percent",
"ampersand", "quoteright", "parenleft", "parenright", "asterisk", "plus",
"comma", "hyphen", "period", "slash", "zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine", "colon", "semicolon", "less",
"equal", "greater", "question", "at", "A", "B", "C", "D", "E", "F", "G", "H",
"I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
"X", "Y", "Z", "bracketleft", "backslash", "bracketright", "asciicircum",
"underscore", "quoteleft", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j",
"k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y",
"z", "braceleft", "bar", "braceright", "asciitilde", false, false, false,
false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false, "exclamdown",
"cent", "sterling", "fraction", "yen", "florin", "section", "currency",
"quotesingle", "quotedblleft", "guillemotleft", "guilsinglleft",
"guilsinglright", "fi", "fl", false, "endash", "dagger", "daggerdbl",
"periodcentered", false, "paragraph", "bullet", "quotesinglbase",
"quotedblbase", "quotedblright", "guillemotright", "ellipsis", "perthousand",
false, "questiondown", false, "grave", "acute", "circumflex", "tilde",
"macron", "breve", "dotaccent", "dieresis", false, "ring", "cedilla", false,
"hungarumlaut", "ogonek", "caron", "emdash", false, false, false, false,
false, false, false, false, false, false, false, false, false, false, false,
false, "AE", false, "ordfeminine", false, false, false, false, "Lslash",
"Oslash", "OE", "ordmasculine", false, false, false, false, false, "ae",
false, false, false, "dotlessi", false, false, "lslash", "oslash", "oe",
"germandbls", false, false, false, false
}
local cffreaders = {
readbyte,
readushort,
readuint,
readulong,
}
-- The header contains information about its own size.
local function readheader(f)
local offset = getposition(f)
local major = readbyte(f)
local header = {
offset = offset,
major = major,
minor = readbyte(f),
size = readbyte(f), -- headersize
}
if major == 1 then
header.dsize = readbyte(f) -- list of dict offsets
elseif major == 2 then
header.dsize = readushort(f) -- topdict size
else
-- I'm probably no longer around by then and we use AI's to
-- handle this kind of stuff, if we typeset documents at all.
end
setposition(f,offset+header.size)
return header
end
-- The indexes all look the same, so we share a loader. We could pass a handler
-- and run over the array but why bother, we only have a few uses.
local function readlengths(f,longcount)
local count = longcount and readulong(f) or readushort(f)
if count == 0 then
return { }
end
local osize = readbyte(f)
local read = cffreaders[osize]
if not read then
report("bad offset size: %i",osize)
return { }
end
local lengths = { }
local previous = read(f)
for i=1,count do
local offset = read(f)
local length = offset - previous
if length < 0 then
report("bad offset: %i",length)
length = 0
end
lengths[i] = length
previous = offset
end
return lengths
end
-- There can be subfonts so names is an array. However, in our case it's always
-- one font. The same is true for the top dictionaries. Watch how we only load
-- the dictionary string as for interpretation we need to have the strings loaded
-- as well.
local function readfontnames(f)
local names = readlengths(f)
for i=1,#names do
names[i] = readstring(f,names[i])
end
return names
end
local function readtopdictionaries(f)
local dictionaries = readlengths(f)
for i=1,#dictionaries do
dictionaries[i] = readstring(f,dictionaries[i])
end
return dictionaries
end
-- Strings are added to a list of standard strings so we start the font specific
-- one with an offset. Strings are shared so we have one table.
local function readstrings(f)
local lengths = readlengths(f)
local strings = setmetatableindex({ }, defaultstrings)
local index = #defaultstrings
for i=1,#lengths do
index = index + 1
strings[index] = readstring(f,lengths[i])
end
return strings
end
-- Parsing the dictionaries is delayed till we have the strings loaded. The parser
-- is stack based so the operands come before the operator (like in postscript).
-- local function delta(t)
-- local n = #t
-- if n > 1 then
-- local p = t[1]
-- for i=2,n do
-- local c = t[i]
-- t[i] = c + p
-- p = c
-- end
-- end
-- end
do
-- We use a closure so that we don't need to pass too much around. For cff2 we can
-- at some point use a simple version as there is less.
local stack = { }
local top = 0
local result = { }
local strings = { }
local p_single =
P("\00") / function()
result.version = strings[stack[top]] or "unset"
top = 0
end
+ P("\01") / function()
result.notice = strings[stack[top]] or "unset"
top = 0
end
+ P("\02") / function()
result.fullname = strings[stack[top]] or "unset"
top = 0
end
+ P("\03") / function()
result.familyname = strings[stack[top]] or "unset"
top = 0
end
+ P("\04") / function()
result.weight = strings[stack[top]] or "unset"
top = 0
end
+ P("\05") / function()
result.fontbbox = { unpack(stack,1,4) }
top = 0
end
+ P("\06") / function()
result.bluevalues = { unpack(stack,1,top) }
top = 0
end
+ P("\07") / function()
result.otherblues = { unpack(stack,1,top) }
top = 0
end
+ P("\08") / function()
result.familyblues = { unpack(stack,1,top) }
top = 0
end
+ P("\09") / function()
result.familyotherblues = { unpack(stack,1,top) }
top = 0
end
+ P("\10") / function()
result.strhw = stack[top]
top = 0
end
+ P("\11") / function()
result.strvw = stack[top]
top = 0
end
+ P("\13") / function()
result.uniqueid = stack[top]
top = 0
end
+ P("\14") / function()
result.xuid = concat(stack,"",1,top)
top = 0
end
+ P("\15") / function()
result.charset = stack[top]
top = 0
end
+ P("\16") / function()
result.encoding = stack[top]
top = 0
end
+ P("\17") / function() -- valid cff2
result.charstrings = stack[top]
top = 0
end
+ P("\18") / function()
result.private = {
size = stack[top-1],
offset = stack[top],
}
top = 0
end
+ P("\19") / function()
result.subroutines = stack[top]
top = 0 -- new, forgotten ?
end
+ P("\20") / function()
result.defaultwidthx = stack[top]
top = 0 -- new, forgotten ?
end
+ P("\21") / function()
result.nominalwidthx = stack[top]
top = 0 -- new, forgotten ?
end
-- + P("\22") / function() -- reserved
-- end
-- + P("\23") / function() -- reserved
-- end
+ P("\24") / function() -- new in cff2
result.vstore = stack[top]
top = 0
end
+ P("\25") / function() -- new in cff2
result.maxstack = stack[top]
top = 0
end
-- + P("\26") / function() -- reserved
-- end
-- + P("\27") / function() -- reserved
-- end
local p_double = P("\12") * (
P("\00") / function()
result.copyright = stack[top]
top = 0
end
+ P("\01") / function()
result.monospaced = stack[top] == 1 and true or false -- isfixedpitch
top = 0
end
+ P("\02") / function()
result.italicangle = stack[top]
top = 0
end
+ P("\03") / function()
result.underlineposition = stack[top]
top = 0
end
+ P("\04") / function()
result.underlinethickness = stack[top]
top = 0
end
+ P("\05") / function()
result.painttype = stack[top]
top = 0
end
+ P("\06") / function()
result.charstringtype = stack[top]
top = 0
end
+ P("\07") / function() -- valid cff2
result.fontmatrix = { unpack(stack,1,6) }
top = 0
end
+ P("\08") / function()
result.strokewidth = stack[top]
top = 0
end
+ P("\09") / function()
result.bluescale = stack[top]
top = 0
end
+ P("\10") / function()
result.bluesnap = stack[top]
top = 0
end
+ P("\11") / function()
result.bluefuzz = stack[top]
top = 0
end
+ P("\12") / function()
result.stemsnaph = { unpack(stack,1,top) }
top = 0
end
+ P("\13") / function()
result.stemsnapv = { unpack(stack,1,top) }
top = 0
end
+ P("\20") / function()
result.syntheticbase = stack[top]
top = 0
end
+ P("\21") / function()
result.postscript = strings[stack[top]] or "unset"
top = 0
end
+ P("\22") / function()
result.basefontname = strings[stack[top]] or "unset"
top = 0
end
+ P("\21") / function()
result.basefontblend = stack[top]
top = 0
end
+ P("\30") / function()
result.cid.registry = strings[stack[top-2]] or "unset"
result.cid.ordering = strings[stack[top-1]] or "unset"
result.cid.supplement = stack[top]
top = 0
end
+ P("\31") / function()
result.cid.fontversion = stack[top]
top = 0
end
+ P("\32") / function()
result.cid.fontrevision= stack[top]
top = 0
end
+ P("\33") / function()
result.cid.fonttype = stack[top]
top = 0
end
+ P("\34") / function()
result.cid.count = stack[top]
top = 0
end
+ P("\35") / function()
result.cid.uidbase = stack[top]
top = 0
end
+ P("\36") / function() -- valid cff2
result.cid.fdarray = stack[top]
top = 0
end
+ P("\37") / function() -- valid cff2
result.cid.fdselect = stack[top]
top = 0
end
+ P("\38") / function()
result.cid.fontname = strings[stack[top]] or "unset"
top = 0
end
)
-- Some lpeg fun ... a first variant split the byte and made a new string but
-- the second variant is much faster. Not that it matters much as we don't see
-- such numbers often.
local remap = {
["\x00"] = "00", ["\x01"] = "01", ["\x02"] = "02", ["\x03"] = "03", ["\x04"] = "04", ["\x05"] = "05", ["\x06"] = "06", ["\x07"] = "07", ["\x08"] = "08", ["\x09"] = "09", ["\x0A"] = "0.", ["\x0B"] = "0E", ["\x0C"] = "0E-", ["\x0D"] = "0", ["\x0E"] = "0-", ["\x0F"] = "0",
["\x10"] = "10", ["\x11"] = "11", ["\x12"] = "12", ["\x13"] = "13", ["\x14"] = "14", ["\x15"] = "15", ["\x16"] = "16", ["\x17"] = "17", ["\x18"] = "18", ["\x19"] = "19", ["\x1A"] = "1.", ["\x1B"] = "1E", ["\x1C"] = "1E-", ["\x1D"] = "1", ["\x1E"] = "1-", ["\x1F"] = "1",
["\x20"] = "20", ["\x21"] = "21", ["\x22"] = "22", ["\x23"] = "23", ["\x24"] = "24", ["\x25"] = "25", ["\x26"] = "26", ["\x27"] = "27", ["\x28"] = "28", ["\x29"] = "29", ["\x2A"] = "2.", ["\x2B"] = "2E", ["\x2C"] = "2E-", ["\x2D"] = "2", ["\x2E"] = "2-", ["\x2F"] = "2",
["\x30"] = "30", ["\x31"] = "31", ["\x32"] = "32", ["\x33"] = "33", ["\x34"] = "34", ["\x35"] = "35", ["\x36"] = "36", ["\x37"] = "37", ["\x38"] = "38", ["\x39"] = "39", ["\x3A"] = "3.", ["\x3B"] = "3E", ["\x3C"] = "3E-", ["\x3D"] = "3", ["\x3E"] = "3-", ["\x3F"] = "3",
["\x40"] = "40", ["\x41"] = "41", ["\x42"] = "42", ["\x43"] = "43", ["\x44"] = "44", ["\x45"] = "45", ["\x46"] = "46", ["\x47"] = "47", ["\x48"] = "48", ["\x49"] = "49", ["\x4A"] = "4.", ["\x4B"] = "4E", ["\x4C"] = "4E-", ["\x4D"] = "4", ["\x4E"] = "4-", ["\x4F"] = "4",
["\x50"] = "50", ["\x51"] = "51", ["\x52"] = "52", ["\x53"] = "53", ["\x54"] = "54", ["\x55"] = "55", ["\x56"] = "56", ["\x57"] = "57", ["\x58"] = "58", ["\x59"] = "59", ["\x5A"] = "5.", ["\x5B"] = "5E", ["\x5C"] = "5E-", ["\x5D"] = "5", ["\x5E"] = "5-", ["\x5F"] = "5",
["\x60"] = "60", ["\x61"] = "61", ["\x62"] = "62", ["\x63"] = "63", ["\x64"] = "64", ["\x65"] = "65", ["\x66"] = "66", ["\x67"] = "67", ["\x68"] = "68", ["\x69"] = "69", ["\x6A"] = "6.", ["\x6B"] = "6E", ["\x6C"] = "6E-", ["\x6D"] = "6", ["\x6E"] = "6-", ["\x6F"] = "6",
["\x70"] = "70", ["\x71"] = "71", ["\x72"] = "72", ["\x73"] = "73", ["\x74"] = "74", ["\x75"] = "75", ["\x76"] = "76", ["\x77"] = "77", ["\x78"] = "78", ["\x79"] = "79", ["\x7A"] = "7.", ["\x7B"] = "7E", ["\x7C"] = "7E-", ["\x7D"] = "7", ["\x7E"] = "7-", ["\x7F"] = "7",
["\x80"] = "80", ["\x81"] = "81", ["\x82"] = "82", ["\x83"] = "83", ["\x84"] = "84", ["\x85"] = "85", ["\x86"] = "86", ["\x87"] = "87", ["\x88"] = "88", ["\x89"] = "89", ["\x8A"] = "8.", ["\x8B"] = "8E", ["\x8C"] = "8E-", ["\x8D"] = "8", ["\x8E"] = "8-", ["\x8F"] = "8",
["\x90"] = "90", ["\x91"] = "91", ["\x92"] = "92", ["\x93"] = "93", ["\x94"] = "94", ["\x95"] = "95", ["\x96"] = "96", ["\x97"] = "97", ["\x98"] = "98", ["\x99"] = "99", ["\x9A"] = "9.", ["\x9B"] = "9E", ["\x9C"] = "9E-", ["\x9D"] = "9", ["\x9E"] = "9-", ["\x9F"] = "9",
["\xA0"] = ".0", ["\xA1"] = ".1", ["\xA2"] = ".2", ["\xA3"] = ".3", ["\xA4"] = ".4", ["\xA5"] = ".5", ["\xA6"] = ".6", ["\xA7"] = ".7", ["\xA8"] = ".8", ["\xA9"] = ".9", ["\xAA"] = "..", ["\xAB"] = ".E", ["\xAC"] = ".E-", ["\xAD"] = ".", ["\xAE"] = ".-", ["\xAF"] = ".",
["\xB0"] = "E0", ["\xB1"] = "E1", ["\xB2"] = "E2", ["\xB3"] = "E3", ["\xB4"] = "E4", ["\xB5"] = "E5", ["\xB6"] = "E6", ["\xB7"] = "E7", ["\xB8"] = "E8", ["\xB9"] = "E9", ["\xBA"] = "E.", ["\xBB"] = "EE", ["\xBC"] = "EE-", ["\xBD"] = "E", ["\xBE"] = "E-", ["\xBF"] = "E",
["\xC0"] = "E-0", ["\xC1"] = "E-1", ["\xC2"] = "E-2", ["\xC3"] = "E-3", ["\xC4"] = "E-4", ["\xC5"] = "E-5", ["\xC6"] = "E-6", ["\xC7"] = "E-7", ["\xC8"] = "E-8", ["\xC9"] = "E-9", ["\xCA"] = "E-.", ["\xCB"] = "E-E", ["\xCC"] = "E-E-", ["\xCD"] = "E-", ["\xCE"] = "E--", ["\xCF"] = "E-",
["\xD0"] = "-0", ["\xD1"] = "-1", ["\xD2"] = "-2", ["\xD3"] = "-3", ["\xD4"] = "-4", ["\xD5"] = "-5", ["\xD6"] = "-6", ["\xD7"] = "-7", ["\xD8"] = "-8", ["\xD9"] = "-9", ["\xDA"] = "-.", ["\xDB"] = "-E", ["\xDC"] = "-E-", ["\xDD"] = "-", ["\xDE"] = "--", ["\xDF"] = "-",
}
local p_last = S("\x0F\x1F\x2F\x3F\x4F\x5F\x6F\x7F\x8F\x9F\xAF\xBF")
+ R("\xF0\xFF")
local p_nibbles = P("\30") * Cs(((1-p_last)/remap)^0 * (P(1)/remap)) / function(n)
-- 0-9=digit a=. b=E c=E- d=reserved e=- f=finish
top = top + 1
stack[top] = tonumber(n) or 0
end
local p_byte = C(R("\32\246")) / function(b0)
-- -107 .. +107
top = top + 1
stack[top] = byte(b0) - 139
end
local p_positive = C(R("\247\250")) * C(1) / function(b0,b1)
-- +108 .. +1131
top = top + 1
stack[top] = (byte(b0)-247)*256 + byte(b1) + 108
end
local p_negative = C(R("\251\254")) * C(1) / function(b0,b1)
-- -1131 .. -108
top = top + 1
stack[top] = -(byte(b0)-251)*256 - byte(b1) - 108
end
-- local p_float = P("\255") * C(1) * C(1) * C(1) * C(1) / function(b0,b1,b2,b3)
-- top = top + 1
-- stack[top] = 0
-- end
local p_short = P("\28") * C(1) * C(1) / function(b1,b2)
-- -32768 .. +32767 : b1<<8 | b2
top = top + 1
local n = 0x100 * byte(b1) + byte(b2)
if n >= 0x8000 then
stack[top] = n - 0xFFFF - 1
else
stack[top] = n
end
end
local p_long = P("\29") * C(1) * C(1) * C(1) * C(1) / function(b1,b2,b3,b4)
-- -2^31 .. +2^31-1 : b1<<24 | b2<<16 | b3<<8 | b4
top = top + 1
local n = 0x1000000 * byte(b1) + 0x10000 * byte(b2) + 0x100 * byte(b3) + byte(b4)
if n >= 0x8000000 then
stack[top] = n - 0xFFFFFFFF - 1
else
stack[top] = n
end
end
local p_unsupported = P(1) / function(detail)
top = 0
end
local p_dictionary = (
p_byte
+ p_positive
+ p_negative
+ p_short
+ p_long
+ p_nibbles
+ p_single
+ p_double
-- + p_float
+ p_unsupported
)^1
parsedictionaries = function(data,dictionaries,version)
stack = { }
strings = data.strings
if trace_charstrings then
report("charstring format %a",version)
end
for i=1,#dictionaries do
top = 0
result = version == "cff" and {
monospaced = false,
italicangle = 0,
underlineposition = -100,
underlinethickness = 50,
painttype = 0,
charstringtype = 2,
fontmatrix = { 0.001, 0, 0, 0.001, 0, 0 },
fontbbox = { 0, 0, 0, 0 },
strokewidth = 0,
charset = 0,
encoding = 0,
cid = {
fontversion = 0,
fontrevision = 0,
fonttype = 0,
count = 8720,
}
} or {
charstringtype = 2,
charset = 0,
vstore = 0,
cid = {
-- nothing yet
},
}
lpegmatch(p_dictionary,dictionaries[i])
dictionaries[i] = result
end
--
result = { }
top = 0
stack = { }
end
parseprivates = function(data,dictionaries)
stack = { }
strings = data.strings
for i=1,#dictionaries do
local private = dictionaries[i].private
if private and private.data then
top = 0
result = {
forcebold = false,
languagegroup = 0,
expansionfactor = 0.06,
initialrandomseed = 0,
subroutines = 0,
defaultwidthx = 0,
nominalwidthx = 0,
cid = {
-- actually an error
},
}
lpegmatch(p_dictionary,private.data)
private.data = result
end
end
result = { }
top = 0
stack = { }
end
-- All bezier curves have 6 points with successive pairs relative to
-- the previous pair. Some can be left out and are then copied or zero
-- (optimization).
--
-- We are not really interested in all the details of a glyph because we
-- only need to calculate the boundingbox. So, todo: a quick no result but
-- calculate only variant.
--
-- The conversion is straightforward and the specification os clear once
-- you understand that the x and y needs to be updates each step. It's also
-- quite easy to test because in mp a shape will look bad when a few variables
-- are swapped. But still there might be bugs down here because not all
-- variants are seen in a font so far. We are less compact that the ff code
-- because there quite some variants are done in one helper with a lot of
-- testing for states.
local x = 0
local y = 0
local width = false
local lsb = 0
local result = { }
local r = 0
local stems = 0
local globalbias = 0
local localbias = 0
local nominalwidth = 0
local defaultwidth = 0
local charset = false
local globals = false
local locals = false
local depth = 1
local xmin = 0
local xmax = 0
local ymin = 0
local ymax = 0
local checked = false
local keepcurve = false
local version = 2
local regions = false
local nofregions = 0
local region = false
local factors = false
local axis = false
local vsindex = 0
local justpass = false
local seacs = { }
local procidx = nil
local function showstate(where,i,n)
if i then
local j = i + n - 1
report("%w%-10s : [%s] step",depth*2+2,where,concat(stack," ",i,j <= top and j or top))
else
report("%w%-10s : [%s] n=%i",depth*2,where,concat(stack," ",1,top),top)
end
end
local function showvalue(where,value,showstack)
if showstack then
report("%w%-10s : %s : [%s] n=%i",depth*2,where,tostring(value),concat(stack," ",1,top),top)
else
report("%w%-10s : %s",depth*2,where,tostring(value))
end
end
-- All these indirect calls make this run slower but it's cleaner this way
-- and we cache the result. As we moved the boundingbox code inline we gain
-- some back. I inlined some of then and a bit speed can be gained by more
-- inlining but not that much.
-- Maybe have several action tables:
--
-- keep curve / checked
-- keep curve / not checked
-- checked
-- not checked
local function xymoveto()
if keepcurve then
r = r + 1
result[r] = { x, y, "m" }
end
if checked then
if x > xmax then xmax = x elseif x < xmin then xmin = x end
if y > ymax then ymax = y elseif y < ymin then ymin = y end
else
xmin = x
ymin = y
xmax = x
ymax = y
checked = true
end
end
local function xmoveto() -- slight speedup
if keepcurve then
r = r + 1
result[r] = { x, y, "m" }
end
if not checked then
xmin = x
ymin = y
xmax = x
ymax = y
checked = true
elseif x > xmax then
xmax = x
elseif x < xmin then
xmin = x
end
end
local function ymoveto() -- slight speedup
if keepcurve then
r = r + 1
result[r] = { x, y, "m" }
end
if not checked then
xmin = x
ymin = y
xmax = x
ymax = y
checked = true
elseif y > ymax then
ymax = y
elseif y < ymin then
ymin = y
end
end
local function moveto()
if trace_charstrings then
showstate("moveto")
end
top = 0 -- forgotten
xymoveto()
end
local function xylineto() -- we could inline, no blend
if keepcurve then
r = r + 1
result[r] = { x, y, "l" }
end
if checked then
if x > xmax then xmax = x elseif x < xmin then xmin = x end
if y > ymax then ymax = y elseif y < ymin then ymin = y end
else
xmin = x
ymin = y
xmax = x
ymax = y
checked = true
end
end
local function xlineto() -- slight speedup
if keepcurve then
r = r + 1
result[r] = { x, y, "l" }
end
if not checked then
xmin = x
ymin = y
xmax = x
ymax = y
checked = true
elseif x > xmax then
xmax = x
elseif x < xmin then
xmin = x
end
end
local function ylineto() -- slight speedup
if keepcurve then
r = r + 1
result[r] = { x, y, "l" }
end
if not checked then
xmin = x
ymin = y
xmax = x
ymax = y
checked = true
elseif y > ymax then
ymax = y
elseif y < ymin then
ymin = y
end
end
local function xycurveto(x1,y1,x2,y2,x3,y3,i,n) -- called local so no blend here
if trace_charstrings then
showstate("curveto",i,n)
end
if keepcurve then
r = r + 1
result[r] = { x1, y1, x2, y2, x3, y3, "c" }
end
if checked then
if x1 > xmax then xmax = x1 elseif x1 < xmin then xmin = x1 end
if y1 > ymax then ymax = y1 elseif y1 < ymin then ymin = y1 end
else
xmin = x1
ymin = y1
xmax = x1
ymax = y1
checked = true
end
if x2 > xmax then xmax = x2 elseif x2 < xmin then xmin = x2 end
if y2 > ymax then ymax = y2 elseif y2 < ymin then ymin = y2 end
if x3 > xmax then xmax = x3 elseif x3 < xmin then xmin = x3 end
if y3 > ymax then ymax = y3 elseif y3 < ymin then ymin = y3 end
end
local function rmoveto()
if not width then
if top > 2 then
width = stack[1]
if trace_charstrings then
showvalue("backtrack width",width)
end
else
width = true
end
end
if trace_charstrings then
showstate("rmoveto")
end
x = x + stack[top-1] -- dx1
y = y + stack[top] -- dy1
top = 0
xymoveto()
end
local function hmoveto()
if not width then
if top > 1 then
width = stack[1]
if trace_charstrings then
showvalue("backtrack width",width)
end
else
width = true
end
end
if trace_charstrings then
showstate("hmoveto")
end
x = x + stack[top] -- dx1
top = 0
xmoveto()
end
local function vmoveto()
if not width then
if top > 1 then
width = stack[1]
if trace_charstrings then
showvalue("backtrack width",width)
end
else
width = true
end
end
if trace_charstrings then
showstate("vmoveto")
end
y = y + stack[top] -- dy1
top = 0
ymoveto()
end
local function rlineto()
if trace_charstrings then
showstate("rlineto")
end
for i=1,top,2 do
x = x + stack[i] -- dxa
y = y + stack[i+1] -- dya
xylineto()
end
top = 0
end
local function hlineto() -- x (y,x)+ | (x,y)+
if trace_charstrings then
showstate("hlineto")
end
if top == 1 then
x = x + stack[1]
xlineto()
else
local swap = true
for i=1,top do
if swap then
x = x + stack[i]
xlineto()
swap = false
else
y = y + stack[i]
ylineto()
swap = true
end
end
end
top = 0
end
local function vlineto() -- y (x,y)+ | (y,x)+
if trace_charstrings then
showstate("vlineto")
end
if top == 1 then
y = y + stack[1]
ylineto()
else
local swap = false
for i=1,top do
if swap then
x = x + stack[i]
xlineto()
swap = false
else
y = y + stack[i]
ylineto()
swap = true
end
end
end
top = 0
end
local function rrcurveto()