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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
|
if not modules then modules = { } end modules ['font-cff'] = {
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
----- 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
directives.register("fonts.streamreader",function()
streamreader = utilities.streams
readstring = streamreader.readstring
readbyte = streamreader.readcardinal1
readushort = streamreader.readcardinal2
readuint = streamreader.readcardinal3
readulong = streamreader.readcardinal4
setposition = streamreader.setposition
getposition = streamreader.getposition
readbytetable = streamreader.readbytetable
end)
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,
}
directives.register("fonts.streamreader",function()
cffreaders = {
readbyte,
readushort,
readuint,
readulong,
}
end)
-- 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.stdhw = stack[top]
top = 0
end
+ P("\11") / function()
result.stdvw = 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.blueshift = 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_1 = {
["\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 remap_2 = {
["\x0F"] = "0", ["\x1F"] = "1", ["\x2F"] = "2", ["\x3F"] = "3", ["\x4F"] = "4",
["\x5F"] = "5", ["\x6F"] = "6", ["\x7F"] = "7", ["\x8F"] = "8", ["\x9F"] = "9",
}
local p_last_1 = S("\x0F\x1F\x2F\x3F\x4F\x5F\x6F\x7F\x8F\x9F\xAF\xBF")
local p_last_2 = R("\xF0\xFF")
-- tricky, we don't want to append last
-- local p_nibbles = P("\30") * Cs(((1-p_last)/remap)^0 * (P(1)/remap)) / function(n)
local p_nibbles = P("\30") * Cs(((1-(p_last_1+p_last_2))/remap_1)^0 * (p_last_1/remap_2 + p_last_2/"")) / 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()
if trace_charstrings then
showstate("rrcurveto")
end
if top == 6 then
local ax = x + stack[1] -- dxa
local ay = y + stack[2] -- dya
local bx = ax + stack[3] -- dxb
local by = ay + stack[4] -- dyb
x = bx + stack[5] -- dxc
y = by + stack[6] -- dyc
xycurveto(ax,ay,bx,by,x,y,1,6)
else
-- print("rr",top==6,top)
for i=1,top,6 do
local ax = x + stack[i] -- dxa
local ay = y + stack[i+1] -- dya
local bx = ax + stack[i+2] -- dxb
local by = ay + stack[i+3] -- dyb
x = bx + stack[i+4] -- dxc
y = by + stack[i+5] -- dyc
xycurveto(ax,ay,bx,by,x,y,i,6)
end
end
top = 0
end
local function hhcurveto()
if trace_charstrings then
showstate("hhcurveto")
end
local s = 1
if top % 2 ~= 0 then
y = y + stack[1] -- dy1
s = 2
end
if top == 4 then
local ax = x + stack[1] -- dxa
local ay = y
local bx = ax + stack[2] -- dxb
local by = ay + stack[3] -- dyb
x = bx + stack[4] -- dxc
y = by
xycurveto(ax,ay,bx,by,x,y,1,4)
else
for i=s,top,4 do
local ax = x + stack[i] -- dxa
local ay = y
local bx = ax + stack[i+1] -- dxb
local by = ay + stack[i+2] -- dyb
x = bx + stack[i+3] -- dxc
y = by
xycurveto(ax,ay,bx,by,x,y,i,4)
end
end
top = 0
end
local function vvcurveto()
if trace_charstrings then
showstate("vvcurveto")
end
local s = 1
local d = 0
if top % 2 ~= 0 then
d = stack[1] -- dx1
s = 2
end
if top == 4 then
local ax = x + d
local ay = y + stack[1] -- dya
local bx = ax + stack[2] -- dxb
local by = ay + stack[3] -- dyb
x = bx
y = by + stack[4] -- dyc
xycurveto(ax,ay,bx,by,x,y,1,4)
d = 0
else
for i=s,top,4 do
local ax = x + d
local ay = y + stack[i] -- dya
local bx = ax + stack[i+1] -- dxb
local by = ay + stack[i+2] -- dyb
x = bx
y = by + stack[i+3] -- dyc
xycurveto(ax,ay,bx,by,x,y,i,4)
d = 0
end
end
top = 0
end
local function xxcurveto(swap)
local last = top % 4 ~= 0 and stack[top]
if last then
top = top - 1
end
if top == 4 then
local ax, ay, bx, by
if swap then
ax = x + stack[1]
ay = y
bx = ax + stack[2]
by = ay + stack[3]
y = by + stack[4]
if last then
x = bx + last
else
x = bx
end
else
ax = x
ay = y + stack[1]
bx = ax + stack[2]
by = ay + stack[3]
x = bx + stack[4]
if last then
y = by + last
else
y = by
end
end
xycurveto(ax,ay,bx,by,x,y,1 ,4)
else
for i=1,top,4 do
local ax, ay, bx, by
if swap then
ax = x + stack[i]
ay = y
bx = ax + stack[i+1]
by = ay + stack[i+2]
y = by + stack[i+3]
if last and i+3 == top then
x = bx + last
else
x = bx
end
swap = false
else
ax = x
ay = y + stack[i]
bx = ax + stack[i+1]
by = ay + stack[i+2]
x = bx + stack[i+3]
if last and i+3 == top then
y = by + last
else
y = by
end
swap = true
end
xycurveto(ax,ay,bx,by,x,y,i,4)
end
end
top = 0
end
local function hvcurveto()
if trace_charstrings then
showstate("hvcurveto")
end
xxcurveto(true)
end
local function vhcurveto()
if trace_charstrings then
showstate("vhcurveto")
end
xxcurveto(false)
end
local function rcurveline()
if trace_charstrings then
showstate("rcurveline")
end
for i=1,top-2,6 do
local ax = x + stack[i] -- dxa
local ay = y + stack[i+1] -- dya
local bx = ax + stack[i+2] -- dxb
local by = ay + stack[i+3] -- dyb
x = bx + stack[i+4] -- dxc
y = by + stack[i+5] -- dyc
xycurveto(ax,ay,bx,by,x,y,i,6)
end
x = x + stack[top-1] -- dxc
y = y + stack[top] -- dyc
xylineto()
top = 0
end
local function rlinecurve()
if trace_charstrings then
showstate("rlinecurve")
end
if top > 6 then
for i=1,top-6,2 do
x = x + stack[i]
y = y + stack[i+1]
xylineto()
end
end
local ax = x + stack[top-5]
local ay = y + stack[top-4]
local bx = ax + stack[top-3]
local by = ay + stack[top-2]
x = bx + stack[top-1]
y = by + stack[top]
xycurveto(ax,ay,bx,by,x,y)
top = 0
end
-- flex is not yet tested! no loop
local function flex() -- fd not used
if trace_charstrings then
showstate("flex")
end
local ax = x + stack[1] -- dx1
local ay = y + stack[2] -- dy1
local bx = ax + stack[3] -- dx2
local by = ay + stack[4] -- dy2
local cx = bx + stack[5] -- dx3
local cy = by + stack[6] -- dy3
xycurveto(ax,ay,bx,by,cx,cy)
local dx = cx + stack[7] -- dx4
local dy = cy + stack[8] -- dy4
local ex = dx + stack[9] -- dx5
local ey = dy + stack[10] -- dy5
x = ex + stack[11] -- dx6
y = ey + stack[12] -- dy6
xycurveto(dx,dy,ex,ey,x,y)
top = 0
end
local function hflex()
if trace_charstrings then
showstate("hflex")
end
local ax = x + stack[1] -- dx1
local ay = y
local bx = ax + stack[2] -- dx2
local by = ay + stack[3] -- dy2
local cx = bx + stack[4] -- dx3
local cy = by
xycurveto(ax,ay,bx,by,cx,cy)
local dx = cx + stack[5] -- dx4
local dy = by
local ex = dx + stack[6] -- dx5
local ey = y
x = ex + stack[7] -- dx6
xycurveto(dx,dy,ex,ey,x,y)
top = 0
end
local function hflex1()
if trace_charstrings then
showstate("hflex1")
end
local ax = x + stack[1] -- dx1
local ay = y + stack[2] -- dy1
local bx = ax + stack[3] -- dx2
local by = ay + stack[4] -- dy2
local cx = bx + stack[5] -- dx3
local cy = by
xycurveto(ax,ay,bx,by,cx,cy)
local dx = cx + stack[6] -- dx4
local dy = by
local ex = dx + stack[7] -- dx5
local ey = dy + stack[8] -- dy5
x = ex + stack[9] -- dx6
xycurveto(dx,dy,ex,ey,x,y)
top = 0
end
local function flex1()
if trace_charstrings then
showstate("flex1")
end
local ax = x + stack[1] --dx1
local ay = y + stack[2] --dy1
local bx = ax + stack[3] --dx2
local by = ay + stack[4] --dy2
local cx = bx + stack[5] --dx3
local cy = by + stack[6] --dy3
xycurveto(ax,ay,bx,by,cx,cy)
local dx = cx + stack[7] --dx4
local dy = cy + stack[8] --dy4
local ex = dx + stack[9] --dx5
local ey = dy + stack[10] --dy5
if abs(ex - x) > abs(ey - y) then -- spec: abs(dx) > abs(dy)
x = ex + stack[11]
else
y = ey + stack[11]
end
xycurveto(dx,dy,ex,ey,x,y)
top = 0
end
local function getstem()
if top == 0 then
-- bad
elseif top % 2 ~= 0 then
if width then
remove(stack,1)
else
width = remove(stack,1)
if trace_charstrings then
showvalue("width",width)
end
end
top = top - 1
end
if trace_charstrings then
showstate("stem")
end
stems = stems + (top // 2)
top = 0
end
local function getmask()
if top == 0 then
-- bad
elseif top % 2 ~= 0 then
if width then
remove(stack,1)
else
width = remove(stack,1)
if trace_charstrings then
showvalue("width",width)
end
end
top = top - 1
end
if trace_charstrings then
showstate(operator == 19 and "hintmark" or "cntrmask")
end
stems = stems + (top // 2)
top = 0
if stems == 0 then
-- forget about it
elseif stems <= 8 then
return 1
else
return (stems + 7) // 8
end
end
local function unsupported(t)
if trace_charstrings then
showstate("unsupported " .. t)
end
top = 0
end
local function unsupportedsub(t)
if trace_charstrings then
showstate("unsupported sub " .. t)
end
top = 0
end
-- type 1 (not used in type 2)
local function getstem3()
if trace_charstrings then
showstate("stem3")
end
top = 0
end
local function divide()
if version == "cff" then
local d = stack[top]
top = top - 1
stack[top] = stack[top] / d
end
end
local function closepath()
if version == "cff" then
if trace_charstrings then
showstate("closepath")
end
end
top = 0
end
local function hsbw()
if version == "cff" then
if trace_charstrings then
showstate("hsbw")
end
lsb = stack[top-1] or 0
width = stack[top]
end
top = 0
end
local function sbw()
if version == "cff" then
if trace_charstrings then
showstate("sbw")
end
lsb = stack[top-3]
width = stack[top-1]
end
top = 0
end
-- asb adx ady bchar achar seac (accented characters)
local function seac()
if version == "cff" then
if trace_charstrings then
showstate("seac")
end
end
top = 0
end
-- These are probably used for special cases i.e. call out to the
-- postscript interpreter (p 61 of the spec as well as chapter 8).
--
-- This needs checking (I have to ask Taco next time we meet.)
local popped = 3
local hints = 3
-- arg1 ... argn n othersubr# <callothersubr> (on postscript stack)
local function callothersubr()
if version == "cff" then
if trace_charstrings then
showstate("callothersubr")
end
if stack[top] == hints then
popped = stack[top-2]
else
popped = 3
end
local t = stack[top-1]
if t then
top = top - (t + 2)
if top < 0 then
top = 0
end
else
top = 0
end
else
top = 0
end
end
-- <pop> number (from postscript stack)
local function pop()
if version == "cff" then
if trace_charstrings then
showstate("pop")
end
top = top + 1
stack[top] = popped
else
top = 0
end
end
local function setcurrentpoint()
if version == "cff" then
if trace_charstrings then
showstate("setcurrentpoint (unsupported)")
end
x = x + stack[top-1]
y = y + stack[top]
end
top = 0
end
-- So far for unsupported postscript. Now some cff2 magic. As I still need
-- to wrap my head around the rather complex variable font specification
-- with regions and axis, the following approach kind of works but is more
-- some trial and error trick. It's still not clear how much of the complex
-- truetype description applies to cff. Once there are fonts out there we'll
-- get there. (Marcel and friends did some tests with recent cff2 fonts so
-- the code has been adapted accordingly.)
local reginit = false
local function updateregions(n) -- n + 1
if regions then
local current = regions[n+1] or regions[1]
nofregions = #current
if axis and n ~= reginit then
factors = { }
for i=1,nofregions do
local region = current[i]
local s = 1
for j=1,#axis do
local f = axis[j]
local r = region[j]
local start = r.start
local peak = r.peak
local stop = r.stop
if start > peak or peak > stop then
-- * 1
elseif start < 0 and stop > 0 and peak ~= 0 then
-- * 1
elseif peak == 0 then
-- * 1
elseif f < start or f > stop then
-- * 0
s = 0
break
elseif f < peak then
s = s * (f - start) / (peak - start)
elseif f > peak then
s = s * (stop - f) / (stop - peak)
else
-- * 1
end
end
factors[i] = s
end
end
end
reginit = n
end
local function setvsindex()
local vsindex = stack[top]
if trace_charstrings then
showstate(formatters["vsindex %i"](vsindex))
end
updateregions(vsindex)
top = top - 1
end
local function blend()
local n = stack[top]
top = top - 1
if axis then
-- x (r1x,r2x,r3x)
-- (x,y) (r1x,r2x,r3x) (r1y,r2y,r3y)
if trace_charstrings then
local t = top - nofregions * n
local m = t - n
for i=1,n do
local k = m + i
local d = m + n + (i-1)*nofregions
local old = stack[k]
local new = old
for r=1,nofregions do
new = new + stack[d+r] * factors[r]
end
stack[k] = new
showstate(formatters["blend %i of %i: %s -> %s"](i,n,old,new))
end
top = t
elseif n == 1 then
top = top - nofregions
local v = stack[top]
for r=1,nofregions do
v = v + stack[top+r] * factors[r]
end
stack[top] = v
else
top = top - nofregions * n
local d = top
local k = top - n
for i=1,n do
k = k + 1
local v = stack[k]
for r=1,nofregions do
v = v + stack[d+r] * factors[r]
end
stack[k] = v
d = d + nofregions
end
end
else
top = top - nofregions * n
end
end
-- Bah, we cannot use a fast lpeg because a hint has an unknown size and a
-- runtime capture cannot handle that well.
local actions = { [0] =
unsupported, -- 0
getstem, -- 1 -- hstem
unsupported, -- 2
getstem, -- 3 -- vstem
vmoveto, -- 4
rlineto, -- 5
hlineto, -- 6
vlineto, -- 7
rrcurveto, -- 8
unsupported, -- 9 -- closepath
unsupported, -- 10 -- calllocal,
unsupported, -- 11 -- callreturn,
unsupported, -- 12 -- elsewhere
hsbw, -- 13 -- hsbw (type 1 cff)
unsupported, -- 14 -- endchar,
setvsindex, -- 15 -- cff2
blend, -- 16 -- cff2
unsupported, -- 17
getstem, -- 18 -- hstemhm
getmask, -- 19 -- hintmask
getmask, -- 20 -- cntrmask
rmoveto, -- 21
hmoveto, -- 22
getstem, -- 23 -- vstemhm
rcurveline, -- 24
rlinecurve, -- 25
vvcurveto, -- 26
hhcurveto, -- 27
unsupported, -- 28 -- elsewhere
unsupported, -- 29 -- elsewhere
vhcurveto, -- 30
hvcurveto, -- 31
}
local reverse = { [0] =
"unsupported",
"getstem",
"unsupported",
"getstem",
"vmoveto",
"rlineto",
"hlineto",
"vlineto",
"rrcurveto",
"unsupported",
"unsupported",
"unsupported",
"unsupported",
"hsbw",
"unsupported",
"setvsindex",
"blend",
"unsupported",
"getstem",
"getmask",
"getmask",
"rmoveto",
"hmoveto",
"getstem",
"rcurveline",
"rlinecurve",
"vvcurveto",
"hhcurveto",
"unsupported",
"unsupported",
"vhcurveto",
"hvcurveto",
}
local subactions = {
-- cff 1
[000] = dotsection,
[001] = getstem3,
[002] = getstem3,
[006] = seac,
[007] = sbw,
[012] = divide,
[016] = callothersubr,
[017] = pop,
[033] = setcurrentpoint,
-- cff 2
[034] = hflex,
[035] = flex,
[036] = hflex1,
[037] = flex1,
}
local chars = setmetatableindex(function (t,k)
local v = char(k)
t[k] = v
return v
end)
local c_endchar = chars[14]
-- todo: round in blend
local encode = { }
local typeone = false
-- this eventually can become a helper
setmetatableindex(encode,function(t,i)
for i=-2048,-1130 do
-- t[i] = char(28,band(rshift(i,8),0xFF),band(i,0xFF))
t[i] = char(28,(i >> 8) & 0xFF,i & 0xFF)
end
for i=-1131,-108 do
local v = 0xFB00 - i - 108
-- t[i] = char(band(rshift(v,8),0xFF),band(v,0xFF))
t[i] = char((v >> 8) & 0xFF,v & 0xFF)
end
for i=-107,107 do
t[i] = chars[i + 139]
end
for i=108,1131 do
local v = 0xF700 + i - 108
-- t[i] = char(extract(v,8,8),extract(v,0,8))
t[i] = char((v >> 8) & 0xFF,v & 0xFF)
end
for i=1132,2048 do
-- t[i] = char(28,band(rshift(i,8),0xFF),band(i,0xFF))
t[i] = char(28,(i >> 8) & 0xFF,i & 0xFF)
end
setmetatableindex(encode,function(t,k)
-- as we're cff2 we write 16.16-bit signed fixed value
local r = round(k)
local v = rawget(t,r)
if v then
return v
end
local v1 = floor(k)
local v2 = floor((k - v1) * 0x10000)
-- return char(255,extract(v1,8,8),extract(v1,0,8),extract(v2,8,8),extract(v2,0,8))
return char(255,(v1 >> 8) & 0xFF,v1 & 0xFF,(v2 >> 8) & 0xFF,v2 & 0xFF)
end)
return t[i]
end)
readers.cffencoder = encode
local function p_setvsindex()
local vsindex = stack[top]
updateregions(vsindex)
top = top - 1
end
local function p_blend()
-- leaves n values on stack
local n = stack[top]
top = top - 1
if not axis then
-- fatal error
elseif n == 1 then
top = top - nofregions
local v = stack[top]
for r=1,nofregions do
v = v + stack[top+r] * factors[r]
end
stack[top] = round(v)
else
top = top - nofregions * n
local d = top
local k = top - n
for i=1,n do
k = k + 1
local v = stack[k]
for r=1,nofregions do
v = v + stack[d+r] * factors[r]
end
stack[k] = round(v)
d = d + nofregions
end
end
end
local function p_getstem()
local n = 0
if top % 2 ~= 0 then
n = 1
end
if top > n then
stems = stems + ((top - n) // 2)
end
end
local function p_getmask()
local n = 0
if top % 2 ~= 0 then
n = 1
end
if top > n then
stems = stems + ((top - n) // 2)
end
if stems == 0 then
return 0
elseif stems <= 8 then
return 1
else
return (stems + 7) // 8
end
end
-- end of experiment
local process
local function call(scope,list,bias) -- ,process)
depth = depth + 1
if top == 0 then
showstate(formatters["unknown %s call %s, case %s"](scope,"?",1))
top = 0
else
local index = stack[top] + bias
top = top - 1
if trace_charstrings then
showvalue(scope,index,true)
end
local tab = list[index]
if tab then
process(tab)
else
showstate(formatters["unknown %s call %s, case %s"](scope,index,2))
top = 0
end
end
depth = depth - 1
end
-- precompiling and reuse is much slower than redoing the calls
process = function(tab)
local i = 1
local n = #tab
while i <= n do
local t = tab[i]
if t >= 32 then
top = top + 1
if t <= 246 then
-- -107 .. +107
stack[top] = t - 139
i = i + 1
elseif t <= 250 then
-- +108 .. +1131
-- stack[top] = (t-247)*256 + tab[i+1] + 108
-- stack[top] = t*256 - 247*256 + tab[i+1] + 108
stack[top] = t*256 - 63124 + tab[i+1]
i = i + 2
elseif t <= 254 then
-- -1131 .. -108
-- stack[top] = -(t-251)*256 - tab[i+1] - 108
-- stack[top] = -t*256 + 251*256 - tab[i+1] - 108
stack[top] = -t*256 + 64148 - tab[i+1]
i = i + 2
elseif typeone then
local n = 0x1000000 * tab[i+1] + 0x10000 * tab[i+2] + 0x100 * tab[i+3] + tab[i+4]
if n >= 0x8000000 then
n = n - 0xFFFFFFFF - 1
end
stack[top] = n
i = i + 5
else
local n1 = 0x100 * tab[i+1] + tab[i+2]
local n2 = 0x100 * tab[i+3] + tab[i+4]
if n1 >= 0x8000 then
n1 = n1 - 0x10000
end
stack[top] = n1 + n2/0xFFFF
i = i + 5
end
elseif t == 28 then
-- -32768 .. +32767 : b1<<8 | b2
top = top + 1
local n = 0x100 * tab[i+1] + tab[i+2]
if n >= 0x8000 then
-- stack[top] = n - 0xFFFF - 1
stack[top] = n - 0x10000
else
stack[top] = n
end
i = i + 3
elseif t == 11 then -- not in cff2
if trace_charstrings then
showstate("return")
end
return
elseif t == 10 then
call("local",locals,localbias) -- ,process)
i = i + 1
elseif t == 14 then -- not in cff2
if width then
-- okay
elseif top > 0 then
width = stack[1]
if trace_charstrings then
showvalue("width",width)
end
else
width = true
end
if trace_charstrings then
showstate("endchar")
end
return
elseif t == 29 then
call("global",globals,globalbias) -- ,process)
i = i + 1
elseif t == 12 then
i = i + 1
local t = tab[i]
if justpass then
if t >= 34 and t <= 37 then -- flexes
for i=1,top do
r = r + 1 ; result[r] = encode[stack[i]]
end
r = r + 1 ; result[r] = chars[12]
r = r + 1 ; result[r] = chars[t]
top = 0
elseif t == 6 then
seacs[procidx] = {
asb = stack[1],
adx = stack[2],
ady = stack[3],
base = stack[4],
accent = stack[5],
width = width,
lsb = lsb,
}
top = 0
else
local a = subactions[t]
if a then
a(t)
else
top = 0
end
end
else
local a = subactions[t]
if a then
a(t)
else
if trace_charstrings then
showvalue("<subaction>",t)
end
top = 0
end
end
i = i + 1
elseif justpass then
-- todo: local a = passactions
if t == 15 then
p_setvsindex()
i = i + 1
elseif t == 16 then
local s = p_blend() or 0
i = i + s + 1
-- cff 1: (when cff2 strip them)
elseif t == 1 or t == 3 or t == 18 or operation == 23 then
p_getstem() -- at the start
if version == "cff" then
-- if true then
if top > 0 then
for i=1,top do
r = r + 1 ; result[r] = encode[stack[i]]
end
top = 0
end
r = r + 1 ; result[r] = chars[t]
else
top = 0
end
i = i + 1
-- cff 1: (when cff2 strip them)
elseif t == 19 or t == 20 then
local s = p_getmask() or 0 -- after the stems
-- if version == "cff" then
if true then
if top > 0 then
for i=1,top do
r = r + 1 ; result[r] = encode[stack[i]]
end
top = 0
end
r = r + 1 ; result[r] = chars[t]
for j=1,s do
i = i + 1
r = r + 1 ; result[r] = chars[tab[i]]
end
else
i = i + s
top = 0
end
i = i + 1
-- cff 1: closepath
elseif t == 9 then
top = 0
i = i + 1
elseif t == 13 then
hsbw()
-- if version == "cff" then
if true then
-- we do a moveto over lsb
r = r + 1 ; result[r] = encode[lsb]
r = r + 1 ; result[r] = chars[22]
else
-- lsb is supposed to be zero
end
i = i + 1
else
if trace_charstrings then
showstate(reverse[t] or "<action>")
end
if top > 0 then
-- if t == 8 and top > 42 then
if t == 8 and top > 48 then
-- let's assume this only happens for rrcurveto .. the other ones would need some more
-- complex handling (cff2 stuff)
--
-- dx1 dy1 (dx1+dx2) (dy1+dy2) (dx1+dx2+dx3) (dy1+dy2+dy3) rcurveto.
local n = 0
for i=1,top do
-- if n == 42 then
if n == 48 then
-- local zero = encode[0]
-- local res3 = result[r-3]
-- local res2 = result[r-2]
-- local res1 = result[r-1]
-- local res0 = result[r]
-- result[r-3] = zero
-- result[r-2] = zero
r = r + 1 ; result[r] = chars[t]
-- r = r + 1 ; result[r] = zero
-- r = r + 1 ; result[r] = zero
-- r = r + 1 ; result[r] = res3
-- r = r + 1 ; result[r] = res2
-- r = r + 1 ; result[r] = res1
-- r = r + 1 ; result[r] = res0
n = 1
else
n = n + 1
end
r = r + 1 ; result[r] = encode[stack[i]]
end
else
for i=1,top do
r = r + 1 ; result[r] = encode[stack[i]]
end
end
top = 0
end
r = r + 1 ; result[r] = chars[t]
i = i + 1
end
else
local a = actions[t]
if a then
local s = a(t)
if s then
i = i + s + 1
else
i = i + 1
end
else
if trace_charstrings then
showstate(reverse[t] or "<action>")
end
top = 0
i = i + 1
end
end
end
end
-- local function calculatebounds(segments,x,y)
-- local nofsegments = #segments
-- if nofsegments == 0 then
-- return { x, y, x, y }
-- else
-- local xmin = 10000
-- local xmax = -10000
-- local ymin = 10000
-- local ymax = -10000
-- if x < xmin then xmin = x end
-- if x > xmax then xmax = x end
-- if y < ymin then ymin = y end
-- if y > ymax then ymax = y end
-- -- we now have a reasonable start so we could
-- -- simplify the next checks
-- for i=1,nofsegments do
-- local s = segments[i]
-- local x = s[1]
-- local y = s[2]
-- if x < xmin then xmin = x end
-- if x > xmax then xmax = x end
-- if y < ymin then ymin = y end
-- if y > ymax then ymax = y end
-- if s[#s] == "c" then -- "curveto"
-- local x = s[3]
-- local y = s[4]
-- if x < xmin then xmin = x elseif x > xmax then xmax = x end
-- if y < ymin then ymin = y elseif y > ymax then ymax = y end
-- local x = s[5]
-- local y = s[6]
-- if x < xmin then xmin = x elseif x > xmax then xmax = x end
-- if y < ymin then ymin = y elseif y > ymax then ymax = y end
-- end
-- end
-- return { round(xmin), round(ymin), round(xmax), round(ymax) } -- doesn't make ceil more sense
-- end
-- end
local function setbias(globals,locals,nobias)
if nobias then
return 0, 0
else
local g = #globals
local l = #locals
return
((g < 1240 and 107) or (g < 33900 and 1131) or 32768) + 1,
((l < 1240 and 107) or (l < 33900 and 1131) or 32768) + 1
end
end
local function processshape(glyphs,tab,index,hack)
if not tab then
glyphs[index] = {
boundingbox = { 0, 0, 0, 0 },
width = 0,
name = charset and charset[index] or nil,
}
return
end
tab = bytetable(tab)
x = 0
y = 0
width = false
lsb = 0
r = 0
top = 0
stems = 0
result = { } -- we could reuse it when only boundingbox calculations are needed
popped = 3
procidx = index
xmin = 0
xmax = 0
ymin = 0
ymax = 0
checked = false
if trace_charstrings then
report("glyph: %i",index)
report("data : % t",tab)
end
if regions then
updateregions(vsindex)
end
process(tab)
if hack then
return x, y
end
local boundingbox = {
round(xmin),
round(ymin),
round(xmax),
round(ymax),
}
if width == true or width == false then
width = defaultwidth
else
width = nominalwidth + width
end
local glyph = glyphs[index] -- can be autodefined in otr
if justpass then
r = r + 1
result[r] = c_endchar
local stream = concat(result)
result = nil
-- if trace_charstrings then
-- report("vdata: %s",stream)
-- end
if glyph then
glyph.stream = stream
glyph.width = width
else
glyphs[index] = { stream = stream, width = width }
end
elseif glyph then
glyph.segments = keepcurve ~= false and result or nil
glyph.boundingbox = boundingbox
if not glyph.width then
glyph.width = width
end
if charset and not glyph.name then
glyph.name = charset[index]
end
-- glyph.sidebearing = 0 -- todo
elseif keepcurve then
glyphs[index] = {
segments = result,
boundingbox = boundingbox,
width = width,
name = charset and charset[index] or nil,
-- sidebearing = 0,
}
result = nil
else
glyphs[index] = {
boundingbox = boundingbox,
width = width,
name = charset and charset[index] or nil,
}
end
if trace_charstrings then
report("width : %s",tostring(width))
report("boundingbox: % t",boundingbox)
end
end
startparsing = function(fontdata,data,streams)
reginit = false
axis = false
regions = data.regions
justpass = streams == true
popped = 3
seacs = { }
if regions then
-- this was:
-- regions = { regions } -- needs checking
-- and is now (MFC):
regions = { }
local deltas = data.deltas
for i = 1, #deltas do
regions[i] = deltas[i].regions
end
axis = data.factors or false
end
end
stopparsing = function(fontdata,data)
stack = { }
glyphs = false
result = { }
top = 0
locals = false
globals = false
strings = false
popped = 3
seacs = { }
end
local function setwidths(private)
if not private then
return 0, 0
end
local privatedata = private.data
if not privatedata then
return 0, 0
end
return privatedata.nominalwidthx or 0, privatedata.defaultwidthx or 0
end
parsecharstrings = function(fontdata,data,glphs,doshapes,tversion,streams,nobias,istypeone)
local dictionary = data.dictionaries[1]
local charstrings = dictionary.charstrings
keepcurve = doshapes
version = tversion
typeone = istypeone or false
strings = data.strings
globals = data.routines or { }
locals = dictionary.subroutines or { }
charset = dictionary.charset
vsindex = dictionary.vsindex or 0
local glyphs = glphs or { }
globalbias, localbias = setbias(globals,locals,nobias)
nominalwidth, defaultwidth = setwidths(dictionary.private)
if charstrings then
startparsing(fontdata,data,streams)
for index=1,#charstrings do
processshape(glyphs,charstrings[index],index-1)
end
if justpass and next(seacs) then
-- old type 1 stuff ... seacs
local charset = data.dictionaries[1].charset
if charset then
local lookup = table.swapped(charset)
for index, v in next, seacs do
local bindex = lookup[standardnames[v.base]]
local aindex = lookup[standardnames[v.accent]]
local bglyph = bindex and glyphs[bindex]
local aglyph = aindex and glyphs[aindex]
if bglyph and aglyph then
-- this is a real ugly hack but we seldom enter this branch (e.g. old lbr)
local jp = justpass
justpass = false
local x, y = processshape(glyphs,charstrings[bindex+1],bindex,true)
justpass = jp
--
local base = bglyph.stream
local accent = aglyph.stream
local moveto = encode[-x-v.asb+v.adx] .. chars[22]
.. encode[-y +v.ady] .. chars[ 4]
-- prune an endchar
base = sub(base,1,#base-1)
-- combine them
glyphs[index].stream = base .. moveto .. accent
end
end
end
end
stopparsing(fontdata,data)
else
report("no charstrings")
end
return glyphs
end
parsecharstring = function(fontdata,data,dictionary,tab,glphs,index,doshapes,tversion,streams)
keepcurve = doshapes
version = tversion
strings = data.strings
globals = data.routines or { }
locals = dictionary.subroutines or { }
charset = false
vsindex = dictionary.vsindex or 0
local glyphs = glphs or { }
justpass = streams == true
seacs = { }
globalbias, localbias = setbias(globals,locals,nobias)
nominalwidth, defaultwidth = setwidths(dictionary.private)
processshape(glyphs,tab,index-1)
return glyphs[index]
end
end
local function readglobals(f,data,version)
local routines = readlengths(f,version == "cff2")
for i=1,#routines do
routines[i] = readbytetable(f,routines[i])
end
data.routines = routines
end
local function readencodings(f,data)
data.encodings = { }
end
local function readcharsets(f,data,dictionary)
local header = data.header
local strings = data.strings
local nofglyphs = data.nofglyphs
local charsetoffset = dictionary.charset
if charsetoffset and charsetoffset ~= 0 then
setposition(f,header.offset+charsetoffset)
local format = readbyte(f)
local charset = { [0] = ".notdef" }
dictionary.charset = charset
if format == 0 then
for i=1,nofglyphs do
charset[i] = strings[readushort(f)]
end
elseif format == 1 or format == 2 then
local readcount = format == 1 and readbyte or readushort
local i = 1
while i <= nofglyphs do
local sid = readushort(f)
local n = readcount(f)
for s=sid,sid+n do
charset[i] = strings[s]
i = i + 1
if i > nofglyphs then
break
end
end
end
else
report("cff parser: unsupported charset format %a",format)
end
else
dictionary.nocharset = true
dictionary.charset = nil
end
end
local function readprivates(f,data)
local header = data.header
local dictionaries = data.dictionaries
local private = dictionaries[1].private
if private then
setposition(f,header.offset+private.offset)
private.data = readstring(f,private.size)
end
end
local function readlocals(f,data,dictionary,version)
local header = data.header
local private = dictionary.private
if private then
local subroutineoffset = private.data.subroutines
if subroutineoffset ~= 0 then
setposition(f,header.offset+private.offset+subroutineoffset)
local subroutines = readlengths(f,version == "cff2")
for i=1,#subroutines do
subroutines[i] = readbytetable(f,subroutines[i])
end
dictionary.subroutines = subroutines
private.data.subroutines = nil
else
dictionary.subroutines = { }
end
else
dictionary.subroutines = { }
end
end
-- These charstrings are little programs and described in: Technical Note #5177. A truetype
-- font has only one dictionary.
local function readcharstrings(f,data,version)
local header = data.header
local dictionaries = data.dictionaries
local dictionary = dictionaries[1]
local stringtype = dictionary.charstringtype
local offset = dictionary.charstrings
if type(offset) ~= "number" then
-- weird
elseif stringtype == 2 then
setposition(f,header.offset+offset)
-- could be a metatable .. delayed loading
local charstrings = readlengths(f,version=="cff2")
local nofglyphs = #charstrings
for i=1,nofglyphs do
charstrings[i] = readstring(f,charstrings[i])
end
data.nofglyphs = nofglyphs
dictionary.charstrings = charstrings
else
report("unsupported charstr type %i",stringtype)
data.nofglyphs = 0
dictionary.charstrings = { }
end
end
-- cid (maybe do this stepwise so less mem) -- share with above
local function readcidprivates(f,data)
local header = data.header
local dictionaries = data.dictionaries[1].cid.dictionaries
for i=1,#dictionaries do
local dictionary = dictionaries[i]
local private = dictionary.private
if private then
setposition(f,header.offset+private.offset)
private.data = readstring(f,private.size)
end
end
parseprivates(data,dictionaries)
end
readers.parsecharstrings = parsecharstrings -- used in font-onr.lua (type 1)
local function readnoselect(f,fontdata,data,glyphs,doshapes,version,streams)
local dictionaries = data.dictionaries
local dictionary = dictionaries[1]
local cid = not dictionary.private and dictionary.cid
readglobals(f,data,version)
readcharstrings(f,data,version)
if version == "cff2" then
dictionary.charset = nil
else
readencodings(f,data)
readcharsets(f,data,dictionary)
end
if cid then
local fdarray = cid.fdarray
if fdarray then
setposition(f,data.header.offset + fdarray)
local dictionaries = readlengths(f,version=="cff2")
local nofdictionaries = #dictionaries
if nofdictionaries > 0 then
for i=1,nofdictionaries do
dictionaries[i] = readstring(f,dictionaries[i])
end
parsedictionaries(data,dictionaries)
dictionary.private = dictionaries[1].private
if nofdictionaries > 1 then
report("ignoring dictionaries > 1 in cid font")
end
end
end
end
readprivates(f,data)
parseprivates(data,data.dictionaries)
readlocals(f,data,dictionary,version)
startparsing(fontdata,data,streams)
parsecharstrings(fontdata,data,glyphs,doshapes,version,streams,false)
stopparsing(fontdata,data)
end
local function readfdselect(f,fontdata,data,glyphs,doshapes,version,streams)
local header = data.header
local dictionaries = data.dictionaries
local dictionary = dictionaries[1]
local cid = dictionary.cid
local cidselect = cid and cid.fdselect
readglobals(f,data,version)
readcharstrings(f,data,version)
if version ~= "cff2" then
readencodings(f,data)
end
local charstrings = dictionary.charstrings
local fdindex = { }
local nofglyphs = data.nofglyphs
local maxindex = -1
setposition(f,header.offset+cidselect)
local format = readbyte(f)
if format == 1 then
for i=0,nofglyphs do -- notdef included (needs checking)
local index = readbyte(f)
fdindex[i] = index
if index > maxindex then
maxindex = index
end
end
elseif format == 3 then
local nofranges = readushort(f)
local first = readushort(f)
local index = readbyte(f)
while true do
local last = readushort(f)
if index > maxindex then
maxindex = index
end
for i=first,last do
fdindex[i] = index
end
if last >= nofglyphs then
break
else
first = last + 1
index = readbyte(f)
end
end
else
report("unsupported fd index format %i",format)
end
-- hm, always
if maxindex >= 0 then
local cidarray = cid.fdarray
if cidarray then
setposition(f,header.offset+cidarray)
local dictionaries = readlengths(f,version == "cff2")
if #dictionaries > 0 then
for i=1,#dictionaries do
dictionaries[i] = readstring(f,dictionaries[i])
end
parsedictionaries(data,dictionaries)
cid.dictionaries = dictionaries
readcidprivates(f,data)
for i=1,#dictionaries do
readlocals(f,data,dictionaries[i],version)
end
startparsing(fontdata,data,streams)
for i=1,#charstrings do
local dictionary = dictionaries[fdindex[i]+1]
if dictionary then
parsecharstring(fontdata,data,dictionary,charstrings[i],glyphs,i,doshapes,version,streams)
else
-- report("no dictionary for %a : %a => %a",version,i,fdindex[i]+1)
end
-- charstrings[i] = false
end
stopparsing(fontdata,data)
else
report("no cid dictionaries")
end
else
report("no cid array")
end
end
end
local gotodatatable = readers.helpers.gotodatatable
local function cleanup(data,dictionaries)
-- for i=1,#dictionaries do
-- local d = dictionaries[i]
-- d.subroutines = nil
-- end
-- data.strings = nil
-- if data then
-- data.charstrings = nil
-- data.routines = nil
-- end
end
function readers.cff(f,fontdata,specification)
local tableoffset = gotodatatable(f,fontdata,"cff",specification.details or specification.glyphs)
if tableoffset then
local header = readheader(f)
if header.major ~= 1 then
report("only version %s is supported for table %a",1,"cff")
return
end
local glyphs = fontdata.glyphs
local names = readfontnames(f)
local dictionaries = readtopdictionaries(f)
local strings = readstrings(f)
local data = {
header = header,
names = names,
dictionaries = dictionaries,
strings = strings,
nofglyphs = fontdata.nofglyphs,
}
--
parsedictionaries(data,dictionaries,"cff")
--
local dic = dictionaries[1]
local cid = dic.cid
--
local cffinfo = {
familyname = dic.familyname,
fullname = dic.fullname,
boundingbox = dic.boundingbox,
weight = dic.weight,
italicangle = dic.italicangle,
underlineposition = dic.underlineposition,
underlinethickness = dic.underlinethickness,
defaultwidth = dic.defaultwidthx,
nominalwidth = dic.nominalwidthx,
monospaced = dic.monospaced,
}
fontdata.cidinfo = cid and {
registry = cid.registry,
ordering = cid.ordering,
supplement = cid.supplement,
}
fontdata.cffinfo = cffinfo
--
local all = specification.shapes or specification.streams or false
if specification.glyphs or all then
if cid and cid.fdselect then
readfdselect(f,fontdata,data,glyphs,all,"cff",specification.streams)
else
readnoselect(f,fontdata,data,glyphs,all,"cff",specification.streams)
end
end
local private = dic.private
if private then
local data = private.data
if type(data) == "table" then
cffinfo.defaultwidth = data.defaultwidthx or cffinfo.defaultwidth
cffinfo.nominalwidth = data.nominalwidthx or cffinfo.nominalwidth
cffinfo.bluevalues = data.bluevalues
cffinfo.otherblues = data.otherblues
cffinfo.familyblues = data.familyblues
cffinfo.familyotherblues = data.familyotherblues
cffinfo.bluescale = data.bluescale
cffinfo.blueshift = data.blueshift
cffinfo.bluefuzz = data.bluefuzz
cffinfo.stdhw = data.stdhw
cffinfo.stdvw = data.stdvw
cffinfo.stemsnaph = data.stemsnaph
cffinfo.stemsnapv = data.stemsnapv
end
end
cleanup(data,dictionaries)
end
end
function readers.cff2(f,fontdata,specification)
local tableoffset = gotodatatable(f,fontdata,"cff2",specification.glyphs)
if tableoffset then
local header = readheader(f)
if header.major ~= 2 then
report("only version %s is supported for table %a",2,"cff2")
return
end
local glyphs = fontdata.glyphs
local dictionaries = { readstring(f,header.dsize) }
local data = {
header = header,
dictionaries = dictionaries,
nofglyphs = fontdata.nofglyphs,
}
--
parsedictionaries(data,dictionaries,"cff2")
--
local offset = dictionaries[1].vstore
if offset > 0 then
local storeoffset = dictionaries[1].vstore + data.header.offset + 2 -- cff has a preceding size field
local regions, deltas = readers.helpers.readvariationdata(f,storeoffset,factors)
--
data.regions = regions
data.deltas = deltas
else
data.regions = { }
data.deltas = { }
end
data.factors = specification.factors
--
local cid = data.dictionaries[1].cid
local all = specification.shapes or specification.streams or false
if cid and cid.fdselect then
readfdselect(f,fontdata,data,glyphs,all,"cff2",specification.streams)
else
readnoselect(f,fontdata,data,glyphs,all,"cff2",specification.streams)
end
cleanup(data,dictionaries)
end
end
-- temporary helper needed for checking backend patches
-- function readers.cffcheck(filename)
-- local f = io.open(filename,"rb")
-- if f then
-- local fontdata = {
-- glyphs = { },
-- }
-- local header = readheader(f)
-- if header.major ~= 1 then
-- report("only version %s is supported for table %a",1,"cff")
-- return
-- end
-- local names = readfontnames(f)
-- local dictionaries = readtopdictionaries(f)
-- local strings = readstrings(f)
-- local glyphs = { }
-- local data = {
-- header = header,
-- names = names,
-- dictionaries = dictionaries,
-- strings = strings,
-- glyphs = glyphs,
-- nofglyphs = 0,
-- }
-- --
-- parsedictionaries(data,dictionaries,"cff")
-- --
-- local cid = data.dictionaries[1].cid
-- if cid and cid.fdselect then
-- readfdselect(f,fontdata,data,glyphs,false)
-- else
-- readnoselect(f,fontdata,data,glyphs,false)
-- end
-- return data
-- end
-- end
|