summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/luatexdir/font/writecff.w
blob: 3c9174d1aae4719f8931f096e553b45bd0aa5f74 (plain)
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
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
% writecff.w
%
% Copyright 2006-2010 Taco Hoekwater <taco@@luatex.org>
%
% This file is part of LuaTeX.
%
% LuaTeX is free software; you can redistribute it and/or modify it under
% the terms of the GNU General Public License as published by the Free
% Software Foundation; either version 2 of the License, or (at your
% option) any later version.
%
% LuaTeX is distributed in the hope that it will be useful, but WITHOUT
% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
% FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
% License for more details.
%
% You should have received a copy of the GNU General Public License along
% with LuaTeX; if not, see <http://www.gnu.org/licenses/>.

@ @c
static const char _svn_version[] =
    "$Id: writecff.w 4956 2014-03-28 12:12:17Z luigi $"
    "$URL: https://foundry.supelec.fr/svn/luatex/trunk/source/texk/web2c/luatexdir/font/writecff.w $";

#include "ptexlib.h"
#include "lua/luatex-api.h"
#include "font/writecff.h"

extern int cidset;

@ @c
#define get_offset(s,n) get_unsigned(s, (n))
#define get_card8(a)  (card8)(a->stream[a->offset++])
#define get_card16(a) (card16)(get_unsigned(a,2))

#undef b0
#undef b1
#undef b2
#undef b3

#define WORK_BUFFER_SIZE 1024

static char work_buffer[WORK_BUFFER_SIZE];

static unsigned long get_unsigned(cff_font * cff, int n)
{
    unsigned long v = 0;
    while (n-- > 0)
        v = v * 256 + get_card8(cff);
    return v;
}

@ @c
#define CFF_ERROR luatex_fail
#undef WARN
#define WARN luatex_warn

const char *const cff_stdstr[CFF_STDSTR_MAX] = {
    ".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"
};


@ Only read header part but not body
@c
cff_index *cff_get_index_header(cff_font * cff)
{
    cff_index *idx;
    card16 i, count;

    idx = xcalloc(1, sizeof(cff_index));

    idx->count = count = get_card16(cff);
    if (count > 0) {
        idx->offsize = get_card8(cff);
        if (idx->offsize < 1 || idx->offsize > 4)
            CFF_ERROR("invalid offsize data");

        idx->offset =
            xmalloc((unsigned) (((unsigned) count + 1) * sizeof(l_offset)));
        for (i = 0; i < count + 1; i++) {
            (idx->offset)[i] = get_offset(cff, idx->offsize);
        }

        if (idx->offset[0] != 1)
            CFF_ERROR("cff_get_index(): invalid index data");

        idx->data = NULL;
    } else {
        idx->offsize = 0;
        idx->offset = NULL;
        idx->data = NULL;
    }

    return idx;
}



@ @c
cff_index *cff_get_index(cff_font * cff)
{
    cff_index *idx;
    card16 i, count;
    size_t length;

    idx = xcalloc(1, sizeof(cff_index));

    idx->count = count = get_card16(cff);
    if (count > 0) {
        idx->offsize = get_card8(cff);
        if (idx->offsize < 1 || idx->offsize > 4)
            CFF_ERROR("invalid offsize data");

        idx->offset =
            xmalloc((unsigned) (((unsigned) count + 1) * sizeof(l_offset)));
        for (i = 0; i < count + 1; i++) {
            idx->offset[i] = get_offset(cff, idx->offsize);
        }

        if (idx->offset[0] != 1)
            CFF_ERROR("Invalid CFF Index offset data");

        length = (size_t) (idx->offset[count] - idx->offset[0]);

        idx->data = xmalloc((unsigned) length * sizeof(card8));
        memcpy(idx->data, &cff->stream[cff->offset], length);
        cff->offset += length;

    } else {
        idx->offsize = 0;
        idx->offset = NULL;
        idx->data = NULL;
    }
    return idx;
}


@ @c
long cff_pack_index(cff_index * idx, card8 * dest, long destlen)
{
    long len = 0;
    unsigned long datalen;
    card16 i;

    if (idx->count < 1) {
        if (destlen < 2)
            CFF_ERROR("Not enough space available...");
        memset(dest, 0, 2);
        return 2;
    }

    len = cff_index_size(idx);
    datalen = idx->offset[idx->count] - 1;

    if (destlen < len)
        CFF_ERROR("Not enough space available...");

    *(dest++) = (card8) ((idx->count >> 8) & 0xff);
    *(dest++) = (card8) (idx->count & 0xff);

    if (datalen < 0xffUL) {
        idx->offsize = 1;
        *(dest++) = 1;
        for (i = 0; i <= idx->count; i++) {
            *(dest++) = (card8) (idx->offset[i] & 0xff);
        }
    } else if (datalen < 0xffffUL) {
        idx->offsize = 2;
        *(dest++) = 2;
        for (i = 0; i <= idx->count; i++) {
            *(dest++) = (card8) ((idx->offset[i] >> 8) & 0xff);
            *(dest++) = (card8) (idx->offset[i] & 0xff);
        }
    } else if (datalen < 0xffffffUL) {
        idx->offsize = 3;
        *(dest++) = 3;
        for (i = 0; i <= idx->count; i++) {
            *(dest++) = (card8) ((idx->offset[i] >> 16) & 0xff);
            *(dest++) = (card8) ((idx->offset[i] >> 8) & 0xff);
            *(dest++) = (card8) (idx->offset[i] & 0xff);
        }
    } else {
        idx->offsize = 4;
        *(dest++) = 4;
        for (i = 0; i <= idx->count; i++) {
            *(dest++) = (card8) ((idx->offset[i] >> 24) & 0xff);
            *(dest++) = (card8) ((idx->offset[i] >> 16) & 0xff);
            *(dest++) = (card8) ((idx->offset[i] >> 8) & 0xff);
            *(dest++) = (card8) (idx->offset[i] & 0xff);
        }
    }

    memmove(dest, idx->data, idx->offset[idx->count] - 1);

    return len;
}

@ @c
long cff_index_size(cff_index * idx)
{
    if (idx->count > 0) {
        l_offset datalen;

        datalen = idx->offset[idx->count] - 1;
        if (datalen < 0xffUL) {
            idx->offsize = 1;
        } else if (datalen < 0xffffUL) {
            idx->offsize = 2;
        } else if (datalen < 0xffffffUL) {
            idx->offsize = 3;
        } else {
            idx->offsize = 4;
        }
        return (3 + (idx->offsize) * (idx->count + 1) + (long) datalen);
    } else {
        return 2;
    }
}

@ @c
cff_index *cff_new_index(card16 count)
{
    cff_index *idx;

    idx = xcalloc(1, sizeof(cff_index));
    idx->count = count;
    idx->offsize = 0;

    if (count > 0) {
        idx->offset = xcalloc((unsigned) (count + 1), sizeof(l_offset));
        (idx->offset)[0] = 1;
    } else {
        idx->offset = NULL;
    }
    idx->data = NULL;

    return idx;
}


@ @c
void cff_release_index(cff_index * idx)
{
    if (idx) {
        xfree(idx->data);
        xfree(idx->offset);
        xfree(idx);
    }
}

@ @c
void cff_release_dict(cff_dict * dict)
{
    if (dict) {
        if (dict->entries) {
            int i;
            for (i = 0; i < dict->count; i++) {
                xfree((dict->entries)[i].values);
            }
            xfree(dict->entries);
        }
        xfree(dict);
    }
}


@ @c
void cff_release_encoding(cff_encoding * encoding)
{
    if (encoding) {
        switch (encoding->format & (~0x80)) {
        case 0:
            xfree(encoding->data.codes);
            break;
        case 1:
            xfree(encoding->data.range1);
            break;
        default:
            CFF_ERROR("Unknown Encoding format.");
        }
        if (encoding->format & 0x80)
            xfree(encoding->supp);
        xfree(encoding);
    }
}

@ @c
void cff_release_charsets(cff_charsets * charset)
{
    if (charset) {
        switch (charset->format) {
        case 0:
            xfree(charset->data.glyphs);
            break;
        case 1:
            xfree(charset->data.range1);
            break;
        case 2:
            xfree(charset->data.range2);
            break;
        default:
            break;
        }
        xfree(charset);
    }
}

@ @c
void cff_release_fdselect(cff_fdselect * fdselect)
{
    if (fdselect) {
        if (fdselect->format == 0) {
            xfree(fdselect->data.fds);
        } else if (fdselect->format == 3) {
            xfree(fdselect->data.ranges);
        }
        xfree(fdselect);
    }
}


@ @c
void cff_close(cff_font * cff)
{
    card16 i;

    if (cff) {
        xfree(cff->fontname);
        if (cff->name)
            cff_release_index(cff->name);
        if (cff->topdict)
            cff_release_dict(cff->topdict);
        if (cff->string)
            cff_release_index(cff->string);
        if (cff->gsubr)
            cff_release_index(cff->gsubr);
        if (cff->encoding)
            cff_release_encoding(cff->encoding);
        if (cff->charsets)
            cff_release_charsets(cff->charsets);
        if (cff->fdselect)
            cff_release_fdselect(cff->fdselect);
        if (cff->cstrings)
            cff_release_index(cff->cstrings);
        if (cff->fdarray) {
            for (i = 0; i < cff->num_fds; i++) {
                if (cff->fdarray[i])
                    cff_release_dict(cff->fdarray[i]);
            }
            xfree(cff->fdarray);
        }
        if (cff->private) {
            for (i = 0; i < cff->num_fds; i++) {
                if (cff->private[i])
                    cff_release_dict(cff->private[i]);
            }
            xfree(cff->private);
        }
        if (cff->subrs) {
            for (i = 0; i < cff->num_fds; i++) {
                if (cff->subrs[i])
                    cff_release_index(cff->subrs[i]);
            }
            xfree(cff->subrs);
        }
        if (cff->_string)
            cff_release_index(cff->_string);
        xfree(cff);
    }

    return;
}

@ @c
char *cff_get_name(cff_font * cff)
{
    char *fontname;
    l_offset len;
    cff_index *idx;

    idx = cff->name;
    len = idx->offset[cff->index + 1] - idx->offset[cff->index];
    fontname = xmalloc((unsigned) (len + 1) * sizeof(char));
    memcpy(fontname, idx->data + idx->offset[cff->index] - 1, len);
    fontname[len] = '\0';

    return fontname;
}


@ @c
long cff_set_name(cff_font * cff, char *name)
{
    cff_index *idx;

    if (strlen(name) > 127)
        CFF_ERROR("FontName string length too large...");

    if (cff->name)
        cff_release_index(cff->name);

    cff->name = idx = xcalloc(1, sizeof(cff_index));
    idx->count = 1;
    idx->offsize = 1;
    idx->offset = xmalloc(2 * sizeof(l_offset));
    (idx->offset)[0] = 1;
    (idx->offset)[1] = strlen(name) + 1;
    idx->data = xmalloc((unsigned) strlen(name) * sizeof(card8));
    memmove(idx->data, name, strlen(name));     /* no trailing |'\0'| */

    return (long) (5 + strlen(name));
}

long cff_put_header(cff_font * cff, card8 * dest, long destlen)
{
    if (destlen < 4)
        CFF_ERROR("Not enough space available...");

    *(dest++) = cff->header_major;
    *(dest++) = cff->header_minor;
    *(dest++) = 4;              /* Additional data in between header and
                                 * Name INDEX ignored.
                                 */
    /* We will set all offset (0) to four-byte integer. */
    *(dest++) = 4;
    cff->header_offsize = 4;

    return 4;
}

@ @c
#define CFF_PARSE_OK                0
#define CFF_CFF_ERROR_PARSE_CFF_ERROR      -1
#define CFF_CFF_ERROR_STACK_OVERFLOW   -2
#define CFF_CFF_ERROR_STACK_UNDERFLOW  -3
#define CFF_CFF_ERROR_STACK_RANGECHECK -4

#define DICT_ENTRY_MAX 16

cff_dict *cff_new_dict(void)
{
    cff_dict *dict;

    dict = xcalloc(1, sizeof(cff_dict));
    dict->max = DICT_ENTRY_MAX;
    dict->count = 0;
    dict->entries = xcalloc((unsigned) dict->max, sizeof(cff_dict_entry));
    return dict;
}

@

Operand stack:
 only numbers are stored (as double)

Operand types:

number : double (integer or real)
boolean: stored as a number
SID    : stored as a number
array  : array of numbers
delta  : array of numbers

@c
#define CFF_DICT_STACK_LIMIT 64
static int stack_top = 0;
static double arg_stack[CFF_DICT_STACK_LIMIT];

@
CFF DICT encoding:
TODO: default values

@c
#define CFF_LAST_DICT_OP1 22
#define CFF_LAST_DICT_OP2 39
#define CFF_LAST_DICT_OP (CFF_LAST_DICT_OP1 + CFF_LAST_DICT_OP2)

static struct {
    const char *opname;
    int argtype;
} dict_operator[CFF_LAST_DICT_OP] = {
    {
    "version", CFF_TYPE_SID}, {
    "Notice", CFF_TYPE_SID}, {
    "FullName", CFF_TYPE_SID}, {
    "FamilyName", CFF_TYPE_SID}, {
    "Weight", CFF_TYPE_SID}, {
    "FontBBox", CFF_TYPE_ARRAY}, {
    "BlueValues", CFF_TYPE_DELTA}, {
    "OtherBlues", CFF_TYPE_DELTA}, {
    "FamilyBlues", CFF_TYPE_DELTA}, {
    "FamilyOtherBlues", CFF_TYPE_DELTA}, {
    "StdHW", CFF_TYPE_NUMBER}, {
    "StdVW", CFF_TYPE_NUMBER}, {
    NULL, -1},                  /* first byte of two-byte operator */
        /* Top */
    {
    "UniqueID", CFF_TYPE_NUMBER}, {
    "XUID", CFF_TYPE_ARRAY}, {
    "charset", CFF_TYPE_OFFSET}, {
    "Encoding", CFF_TYPE_OFFSET}, {
    "CharStrings", CFF_TYPE_OFFSET}, {
    "Private", CFF_TYPE_SZOFF}, /* two numbers (size and offset) */
        /* Private */
    {
    "Subrs", CFF_TYPE_OFFSET}, {
    "defaultWidthX", CFF_TYPE_NUMBER}, {
    "nominalWidthX", CFF_TYPE_NUMBER},
        /* Operator 2 */
    {
    "Copyright", CFF_TYPE_SID}, {
    "IsFixedPitch", CFF_TYPE_BOOLEAN}, {
    "ItalicAngle", CFF_TYPE_NUMBER}, {
    "UnderlinePosition", CFF_TYPE_NUMBER}, {
    "UnderlineThickness", CFF_TYPE_NUMBER}, {
    "PaintType", CFF_TYPE_NUMBER}, {
    "CharstringType", CFF_TYPE_NUMBER}, {
    "FontMatrix", CFF_TYPE_ARRAY}, {
    "StrokeWidth", CFF_TYPE_NUMBER}, {
    "BlueScale", CFF_TYPE_NUMBER}, {
    "BlueShift", CFF_TYPE_NUMBER}, {
    "BlueFuzz", CFF_TYPE_NUMBER}, {
    "StemSnapH", CFF_TYPE_DELTA}, {
    "StemSnapV", CFF_TYPE_DELTA}, {
    "ForceBold", CFF_TYPE_BOOLEAN}, {
    NULL, -1}, {
    NULL, -1}, {
    "LanguageGroup", CFF_TYPE_NUMBER}, {
    "ExpansionFactor", CFF_TYPE_NUMBER}, {
    "InitialRandomSeed", CFF_TYPE_NUMBER}, {
    "SyntheticBase", CFF_TYPE_NUMBER}, {
    "PostScript", CFF_TYPE_SID}, {
    "BaseFontName", CFF_TYPE_SID}, {
    "BaseFontBlend", CFF_TYPE_DELTA},   /* MMaster ? */
    {
    NULL, -1}, {
    NULL, -1}, {
    NULL, -1}, {
    NULL, -1}, {
    NULL, -1}, {
    NULL, -1},
        /* CID-Keyed font */
    {
    "ROS", CFF_TYPE_ROS},       /* SID SID number */
    {
    "CIDFontVersion", CFF_TYPE_NUMBER}, {
    "CIDFontRevision", CFF_TYPE_NUMBER}, {
    "CIDFontType", CFF_TYPE_NUMBER}, {
    "CIDCount", CFF_TYPE_NUMBER}, {
    "UIDBase", CFF_TYPE_NUMBER}, {
    "FDArray", CFF_TYPE_OFFSET}, {
    "FDSelect", CFF_TYPE_OFFSET}, {
"FontName", CFF_TYPE_SID},};

@ Parse DICT data
@c
static double get_integer(card8 ** data, card8 * endptr, int *status)
{
    long result = 0;
    card8 b0, b1, b2;

    b0 = *(*data)++;
    if (b0 == 28 && *data < endptr - 2) {       /* shortint */
        b1 = *(*data)++;
        b2 = *(*data)++;
        result = b1 * 256 + b2;
        if (result > 0x7fffL)
            result -= 0x10000L;
    } else if (b0 == 29 && *data < endptr - 4) {        /* longint */
        int i;
        result = *(*data)++;
        if (result > 0x7f)
            result -= 0x100;
        for (i = 0; i < 3; i++) {
            result = result * 256 + (**data);
            *data += 1;
        }
    } else if (b0 >= 32 && b0 <= 246) { /* int (1) */
        result = b0 - 139;
    } else if (b0 >= 247 && b0 <= 250) {        /* int (2) */
        b1 = *(*data)++;
        result = (b0 - 247) * 256 + b1 + 108;
    } else if (b0 >= 251 && b0 <= 254) {
        b1 = *(*data)++;
        result = -(b0 - 251) * 256 - b1 - 108;
    } else {
        *status = CFF_CFF_ERROR_PARSE_CFF_ERROR;
    }

    return (double) result;
}

@ Simply uses strtod
@c
static double get_real(card8 ** data, card8 * endptr, int *status)
{
    double result = 0.0;
    int nibble = 0, pos = 0;
    int len = 0, fail = 0;

    if (**data != 30 || *data >= endptr - 1) {
        *status = CFF_CFF_ERROR_PARSE_CFF_ERROR;
        return 0.0;
    }

    *data += 1;                 /* skip first byte (30) */

    pos = 0;
    while ((!fail) && len < WORK_BUFFER_SIZE - 2 && *data < endptr) {
        /* get nibble */
        if (pos % 2) {
            nibble = **data & 0x0f;
            *data += 1;
        } else {
            nibble = (**data >> 4) & 0x0f;
        }
        if (nibble >= 0x00 && nibble <= 0x09) {
            work_buffer[len++] = (char) (nibble + '0');
        } else if (nibble == 0x0a) {    /* . */
            work_buffer[len++] = '.';
        } else if (nibble == 0x0b || nibble == 0x0c) {  /* E, E- */
            work_buffer[len++] = 'e';
            if (nibble == 0x0c)
                work_buffer[len++] = '-';
        } else if (nibble == 0x0e) {    /* `-' */
            work_buffer[len++] = '-';
        } else if (nibble == 0x0d) {    /* skip */
            /* do nothing */
        } else if (nibble == 0x0f) {    /* end */
            work_buffer[len++] = '\0';
            if (((pos % 2) == 0) && (**data != 0xff)) {
                fail = 1;
            }
            break;
        } else {                /* invalid */
            fail = 1;
        }
        pos++;
    }

    /* returned values */
    if (fail || nibble != 0x0f) {
        *status = CFF_CFF_ERROR_PARSE_CFF_ERROR;
    } else {
        char *s;
	/* strtod sets errno for  OVERFLOW and _maybe_ UNDERFLOW */
        /* but not for an invalid  conversion (as for example  if we try to convert "foo" in a double )*/
        /* At least in glib sets errno also for UNDERFLOW */
        /* We don't save/restore the prev. errno */
        errno=0;
        result = strtod(work_buffer, &s);
        if ( (result==0.0 && work_buffer==s) || errno ) {
              /* conversion is not possible */
             *status = CFF_CFF_ERROR_PARSE_CFF_ERROR;
         }
    }
    return result;
}

@ operators
@c
static void add_dict(cff_dict * dict,
                     card8 ** data, card8 * endptr, int *status)
{
    int id, argtype, t;

    id = **data;
    if (id == 0x0c) {
        *data += 1;
        if (*data >= endptr ||
            (id = **data + CFF_LAST_DICT_OP1) >= CFF_LAST_DICT_OP) {
            *status = CFF_CFF_ERROR_PARSE_CFF_ERROR;
            return;
        }
    } else if (id >= CFF_LAST_DICT_OP1) {
        *status = CFF_CFF_ERROR_PARSE_CFF_ERROR;
        return;
    }

    argtype = dict_operator[id].argtype;
    if (dict_operator[id].opname == NULL || argtype < 0) {
        *status = CFF_CFF_ERROR_PARSE_CFF_ERROR;
        return;
    }

    if (dict->count >= dict->max) {
        dict->max += DICT_ENTRY_MAX;
        /* not zeroed! */
        dict->entries =
            xrealloc(dict->entries,
                     (unsigned) ((unsigned) dict->max *
                                 sizeof(cff_dict_entry)));
    }

    (dict->entries)[dict->count].id = id;
    (dict->entries)[dict->count].key = dict_operator[id].opname;
    if (argtype == CFF_TYPE_NUMBER ||
        argtype == CFF_TYPE_BOOLEAN ||
        argtype == CFF_TYPE_SID || argtype == CFF_TYPE_OFFSET) {
        /* check for underflow here, as exactly one operand is expected */
        if (stack_top < 1) {
            *status = CFF_CFF_ERROR_STACK_UNDERFLOW;
            return;
        }
        stack_top--;
        (dict->entries)[dict->count].count = 1;
        (dict->entries)[dict->count].values = xcalloc(1, sizeof(double));
        (dict->entries)[dict->count].values[0] = arg_stack[stack_top];
        dict->count += 1;
    } else {
        /* just ignore operator if there were no operands provided;
           don't treat this as underflow (e.g. StemSnapV in TemporaLGCUni-Italic.otf) */
        if ((t = stack_top) > 0) {
            (dict->entries)[dict->count].count = stack_top;
            (dict->entries)[dict->count].values =
                xmalloc((unsigned) ((unsigned) stack_top * sizeof(double)));
            while (stack_top > 0) {
                stack_top--;
                (dict->entries)[dict->count].values[stack_top] =
                    arg_stack[stack_top];
            }
            if (t > 3 && strcmp(dict_operator[id].opname, "FontMatrix") == 0) {
                /* reset FontMatrix to [0.001 * * 0.001 * *],
                   fix mantis bug \# 0000200 (acroread "feature") */
                (dict->entries)[dict->count].values[0] = 0.001;
                (dict->entries)[dict->count].values[3] = 0.001;
            }
            dict->count += 1;
        }
    }

    *data += 1;

    return;
}


@ All operands are treated as number or array of numbers.
  Private: two numbers, size and offset
  ROS    : three numbers, SID, SID, and a number

@c
cff_dict *cff_dict_unpack(card8 * data, card8 * endptr)
{
    cff_dict *dict;
    int status = CFF_PARSE_OK;

    stack_top = 0;

    dict = cff_new_dict();
    while (data < endptr && status == CFF_PARSE_OK) {
        if (*data < 22) {       /* operator */
            add_dict(dict, &data, endptr, &status);
        } else if (*data == 30) {       /* real - First byte of a sequence (variable) */
            if (stack_top < CFF_DICT_STACK_LIMIT) {
                arg_stack[stack_top] = get_real(&data, endptr, &status);
                stack_top++;
            } else {
                status = CFF_CFF_ERROR_STACK_OVERFLOW;
            }
        } else if (*data == 255 || (*data >= 22 && *data <= 27)) {      /* reserved */
            data++;
        } else {                /* everything else are integer */
            if (stack_top < CFF_DICT_STACK_LIMIT) {
                arg_stack[stack_top] = get_integer(&data, endptr, &status);
                stack_top++;
            } else {
                status = CFF_CFF_ERROR_STACK_OVERFLOW;
            }
        }
    }

    if (status != CFF_PARSE_OK) {
        luatex_fail("Parsing CFF DICT failed. (error=%d)", status);
    } else if (stack_top != 0) {
        WARN("Garbage in CFF DICT data.");
        stack_top = 0;
    }

    return dict;
}

@ @c
int cff_dict_known(cff_dict * dict, const char *key)
{
    int i;

    for (i = 0; i < dict->count; i++) {
        if (key && strcmp(key, (dict->entries)[i].key) == 0
            && (dict->entries)[i].count > 0)
            return 1;
    }

    return 0;
}

@ @c
double cff_dict_get(cff_dict * dict, const char *key, int idx)
{
    double value = 0.0;
    int i;

    assert(key && dict);

    for (i = 0; i < dict->count; i++) {
        if (strcmp(key, (dict->entries)[i].key) == 0) {
           if ((dict->entries)[i].count > idx)
                value = (dict->entries)[i].values[idx];
            else
                luatex_fail("Invalid index number.");
            break;
        }
    }

    if (i == dict->count)
        luatex_fail("DICT entry \"%s\" not found.", key);

    return value;
}

@ @c
card8 cff_fdselect_lookup(cff_font * cff, card16 gid)
{
    card8 fd = 0xff;
    cff_fdselect *fdsel;

    if (cff->fdselect == NULL)
        CFF_ERROR("in cff_fdselect_lookup(): FDSelect not available");

    fdsel = cff->fdselect;

    if (gid >= cff->num_glyphs)
        CFF_ERROR("in cff_fdselect_lookup(): Invalid glyph index");

    switch (fdsel->format) {
    case 0:
        fd = fdsel->data.fds[gid];
        break;
    case 3:
        {
            if (gid == 0) {
                fd = (fdsel->data).ranges[0].fd;
            } else {
                card16 i;
                for (i = 1; i < (fdsel->num_entries); i++) {
                    if (gid < (fdsel->data).ranges[i].first)
                        break;
                }
                fd = (fdsel->data).ranges[i - 1].fd;
            }
        }
        break;
    default:
        CFF_ERROR("in cff_fdselect_lookup(): Invalid FDSelect format");
        break;
    }

    if (fd >= cff->num_fds)
        CFF_ERROR("in cff_fdselect_lookup(): Invalid Font DICT index");

    return fd;
}

@ @c
long cff_read_subrs(cff_font * cff)
{
    long len = 0;
    long offset;
    int i;


    if ((cff->flag & FONTTYPE_CIDFONT) && cff->fdselect == NULL) {
        cff_read_fdselect(cff);
    }

    if ((cff->flag & FONTTYPE_CIDFONT) && cff->fdarray == NULL) {
        cff_read_fdarray(cff);
    }

    if (cff->private == NULL)
        cff_read_private(cff);

    if (cff->gsubr == NULL) {
        cff->offset = cff->gsubr_offset;
        cff->gsubr = cff_get_index(cff);
    }

    cff->subrs = xcalloc(cff->num_fds, sizeof(cff_index *));
    if (cff->flag & FONTTYPE_CIDFONT) {
        for (i = 0; i < cff->num_fds; i++) {
            if (cff->private[i] == NULL ||
                !cff_dict_known(cff->private[i], "Subrs")) {
                (cff->subrs)[i] = NULL;
            } else {
                offset = (long) cff_dict_get(cff->fdarray[i], "Private", 1);
                offset += (long) cff_dict_get(cff->private[i], "Subrs", 0);
                cff->offset = (l_offset) offset;
                (cff->subrs)[i] = cff_get_index(cff);
                len += cff_index_size((cff->subrs)[i]);
            }
        }
    } else {
        if (cff->private[0] == NULL ||
            !cff_dict_known(cff->private[0], "Subrs")) {
            (cff->subrs)[0] = NULL;
        } else {
            offset = (long) cff_dict_get(cff->topdict, "Private", 1);
            offset += (long) cff_dict_get(cff->private[0], "Subrs", 0);
            cff->offset = (l_offset) offset;
            (cff->subrs)[0] = cff_get_index(cff);
            len += cff_index_size((cff->subrs)[0]);
        }
    }

    return len;
}


@ @c
long cff_read_fdarray(cff_font * cff)
{
    long len = 0;
    cff_index *idx;
    long offset, size;
    card16 i;

    if (cff->topdict == NULL)
        CFF_ERROR("in cff_read_fdarray(): Top DICT not found");

    if (!(cff->flag & FONTTYPE_CIDFONT))
        return 0;

    /* must exist */
    offset = (long) cff_dict_get(cff->topdict, "FDArray", 0);
    cff->offset = (l_offset) offset;
    idx = cff_get_index(cff);
    cff->num_fds = (card8) idx->count;
    cff->fdarray = xmalloc((unsigned) (idx->count * sizeof(cff_dict *)));
    for (i = 0; i < idx->count; i++) {
        card8 *data = idx->data + (idx->offset)[i] - 1;
        size = (long) ((idx->offset)[i + 1] - (idx->offset)[i]);
        if (size > 0) {
            (cff->fdarray)[i] = cff_dict_unpack(data, data + size);
        } else {
            (cff->fdarray)[i] = NULL;
        }
    }
    len = cff_index_size(idx);
    cff_release_index(idx);

    return len;
}


@ @c
long cff_read_private(cff_font * cff)
{
    long len = 0;
    card8 *data;
    long offset, size;

    if (cff->flag & FONTTYPE_CIDFONT) {
        int i;

        if (cff->fdarray == NULL)
            cff_read_fdarray(cff);

        cff->private = xmalloc((unsigned) (cff->num_fds * sizeof(cff_dict *)));
        for (i = 0; i < cff->num_fds; i++) {
            if (cff->fdarray[i] != NULL &&
                cff_dict_known(cff->fdarray[i], "Private") &&
                (size = (long) cff_dict_get(cff->fdarray[i], "Private", 0))
                > 0) {
                offset = (long) cff_dict_get(cff->fdarray[i], "Private", 1);
                cff->offset = (l_offset) offset;
                data = xmalloc((unsigned) size * sizeof(card8));
                memcpy(data, &cff->stream[cff->offset], (size_t) size);
                cff->offset = (l_offset) size;
                (cff->private)[i] = cff_dict_unpack(data, data + size);
                xfree(data);
                len += size;
            } else {
                (cff->private)[i] = NULL;
            }
        }
    } else {
        cff->num_fds = 1;
        cff->private = xmalloc(sizeof(cff_dict *));
        if (cff_dict_known(cff->topdict, "Private") &&
            (size = (long) cff_dict_get(cff->topdict, "Private", 0)) > 0) {
            offset = (long) cff_dict_get(cff->topdict, "Private", 1);
            cff->offset = (l_offset) offset;
            data = xmalloc((unsigned) size * sizeof(card8));
            memcpy(data, &cff->stream[cff->offset], (size_t) size);
            cff->offset = (l_offset) size;
            cff->private[0] = cff_dict_unpack(data, data + size);
            xfree(data);
            len += size;
        } else {
            (cff->private)[0] = NULL;
            len = 0;
        }
    }

    return len;
}


@ @c
cff_font *read_cff(unsigned char *buf, long buflength, int n)
{
    cff_font *cff;
    cff_index *idx;
    long offset;


    cff = xcalloc(1, sizeof(cff_font));

    cff->stream = buf;
    cff->stream_size = (l_offset) buflength;
    cff->index = n;

    cff->header_major = get_card8(cff);
    cff->header_minor = get_card8(cff);
    cff->header_hdr_size = get_card8(cff);
    cff->header_offsize = get_card8(cff);
    if (cff->header_offsize < 1 || cff->header_offsize > 4) {
        WARN("invalid offsize data");
        cff_close(cff);
        return NULL;
    }
    if (cff->header_major > 1) {
        luatex_warn("CFF major version %u not supported.", cff->header_major);
        cff_close(cff);
        return NULL;
    }
    cff->offset = cff->header_hdr_size;

    /* Name INDEX */
    idx = cff_get_index(cff);
    if (n > idx->count - 1) {
        luatex_warn("Invalid CFF fontset index number.");
        cff_close(cff);
        return NULL;
    }

    cff->name = idx;

    cff->fontname = cff_get_name(cff);

    /* Top DICT INDEX */
    idx = cff_get_index(cff);
    if (n > idx->count - 1) {
        WARN("CFF Top DICT not exist...");
        cff_close(cff);
        return NULL;
    }
    cff->topdict = cff_dict_unpack(idx->data + idx->offset[n] - 1,
                                   idx->data + idx->offset[n + 1] - 1);
    if (!cff->topdict) {
        WARN("Parsing CFF Top DICT data failed...");
        cff_close(cff);
        return NULL;
    }
    cff_release_index(idx);

    if (cff_dict_known(cff->topdict, "CharstringType") &&
        cff_dict_get(cff->topdict, "CharstringType", 0) != 2) {
        WARN("Only Type 2 Charstrings supported...");
        cff_close(cff);
        return NULL;
    }

    if (cff_dict_known(cff->topdict, "SyntheticBase")) {
        WARN("CFF Synthetic font not supported.");
        cff_close(cff);
        return NULL;
    }

    /* String INDEX */
    cff->string = cff_get_index(cff);

    /* offset to GSubr */
    cff->gsubr_offset = cff->offset;

    /* Number of glyphs */
    offset = (long) cff_dict_get(cff->topdict, "CharStrings", 0);
    cff->offset = (l_offset) offset;
    cff->num_glyphs = get_card16(cff);

    /* Check for font type */
    if (cff_dict_known(cff->topdict, "ROS")) {
        cff->flag |= FONTTYPE_CIDFONT;
    } else {
        cff->flag |= FONTTYPE_FONT;
    }

    /* Check for encoding */
    if (cff_dict_known(cff->topdict, "Encoding")) {
        offset = (long) cff_dict_get(cff->topdict, "Encoding", 0);
        if (offset == 0) {      /* predefined */
            cff->flag |= ENCODING_STANDARD;
        } else if (offset == 1) {
            cff->flag |= ENCODING_EXPERT;
        }
    } else {
        cff->flag |= ENCODING_STANDARD;
    }

    cff->offset = cff->gsubr_offset;    /* seek back to GSubr */

    return cff;
}

@* write a cff for opentype.

@ Pack DICT data
@c
static long pack_integer(card8 * dest, long destlen, long value)
{
    long len = 0;

    if (value >= -107 && value <= 107) {
        if (destlen < 1)
            CFF_ERROR("Buffer overflow.");
        dest[0] = (card8) ((value + 139) & 0xff);
        len = 1;
    } else if (value >= 108 && value <= 1131) {
        if (destlen < 2)
            CFF_ERROR("Buffer overflow.");
        value = (long) 0xf700u + value - 108;
        dest[0] = (card8) ((value >> 8) & 0xff);
        dest[1] = (card8) (value & 0xff);
        len = 2;
    } else if (value >= -1131 && value <= -108) {
        if (destlen < 2)
            CFF_ERROR("Buffer overflow.");
        value = (long) 0xfb00u - value - 108;
        dest[0] = (card8) ((value >> 8) & 0xff);
        dest[1] = (card8) (value & 0xff);
        len = 2;
    } else if (value >= -32768 && value <= 32767) {     /* shortint */
        if (destlen < 3)
            CFF_ERROR("Buffer overflow.");
        dest[0] = 28;
        dest[1] = (card8) ((value >> 8) & 0xff);
        dest[2] = (card8) (value & 0xff);
        len = 3;
    } else {                    /* longint */
        if (destlen < 5)
            CFF_ERROR("Buffer overflow.");
        dest[0] = 29;
        dest[1] = (card8) ((value >> 24) & 0xff);
        dest[2] = (card8) ((value >> 16) & 0xff);
        dest[3] = (card8) ((value >> 8) & 0xff);
        dest[4] = (card8) (value & 0xff);
        len = 5;
    }
    return len;
}

@ @c
static long pack_real(card8 * dest, long destlen, double value)
{
    long e;
    int i = 0, pos = 2;
    int res;
#define CFF_REAL_MAX_LEN 17

    if (destlen < 2)
        CFF_ERROR("Buffer overflow.");

    dest[0] = 30;

    if (value == 0.0) {
        dest[1] = 0x0f;
        return 2;
    }

    if (value < 0.0) {
        dest[1] = 0xe0;
        value *= -1.0;
        pos++;
    }

    e = 0;
    if (value >= 10.0) {
        while (value >= 10.0) {
            value /= 10.0;
            e++;
        }
    } else if (value < 1.0) {
        while (value < 1.0) {
            value *= 10.0;
            e--;
        }
    }

    res=sprintf(work_buffer, "%1.14g", value);
    if (res<0) CFF_ERROR("Invalid conversion.");
    if (res>CFF_REAL_MAX_LEN) res=CFF_REAL_MAX_LEN;

    for (i = 0; i < res; i++) {
        unsigned char ch = 0;

        if (work_buffer[i] == '\0') {
	  /* res should prevent this.  */
	  /* CFF_ERROR("Cannot happen"); */
            break;
        } else if (work_buffer[i] == '.') {
            ch = 0x0a;
        } else if (work_buffer[i] >= '0' && work_buffer[i] <= '9') {
            ch = (unsigned char) (work_buffer[i] - '0');
        } else {
            CFF_ERROR("Invalid character.");
        }

        if (destlen < pos / 2 + 1)
            CFF_ERROR("Buffer overflow.");

        if (pos % 2) {
            dest[pos / 2] = (card8) (dest[pos / 2] + ch);
        } else {
            dest[pos / 2] = (card8) (ch << 4);
        }
        pos++;
    }

    if (e > 0) {
        if (pos % 2) {
            dest[pos / 2] = (card8) (dest[pos / 2] + 0x0b);
        } else {
            if (destlen < pos / 2 + 1)
                CFF_ERROR("Buffer overflow.");
            dest[pos / 2] = (card8) (0xb0);
        }
        pos++;
    } else if (e < 0) {
        if (pos % 2) {
            dest[pos / 2] = (card8) (dest[pos / 2] + 0x0c);
        } else {
            if (destlen < pos / 2 + 1)
                CFF_ERROR("Buffer overflow.");
            dest[pos / 2] = (card8) (0xc0);
        }
        e *= -1;
        pos++;
    }

    if (e != 0) {
        sprintf(work_buffer, "%ld", e);
        for (i = 0; i < CFF_REAL_MAX_LEN; i++) {
            unsigned char ch = 0;
            if (work_buffer[i] == '\0') {
                break;
            } else if (work_buffer[i] == '.') {
                ch = 0x0a;
            } else if (work_buffer[i] >= '0' && work_buffer[i] <= '9') {
                ch = (unsigned char) (work_buffer[i] - '0');
            } else {
                CFF_ERROR("Invalid character.");
            }

            if (destlen < pos / 2 + 1)
                CFF_ERROR("Buffer overflow.");

            if (pos % 2) {
                dest[pos / 2] = (card8) (dest[pos / 2] + ch);
            } else {
                dest[pos / 2] = (card8) (ch << 4);
            }
            pos++;
        }
    }

    if (pos % 2) {
        dest[pos / 2] = (card8) (dest[pos / 2] + 0x0f);
        pos++;
    } else {
        if (destlen < pos / 2 + 1)
            CFF_ERROR("Buffer overflow.");
        dest[pos / 2] = (card8) (0xff);
        pos += 2;
    }

    return pos / 2;
}

@ @c
static long cff_dict_put_number(double value,
                                card8 * dest, long destlen, int type)
{
    long len = 0;
    double nearint;

    nearint = floor(value + 0.5);
    /* set offset to longint */
    if (type == CFF_TYPE_OFFSET) {
        long lvalue;

        lvalue = (long) value;
        if (destlen < 5)
            CFF_ERROR("Buffer overflow.");
        dest[0] = 29;
        dest[1] = (card8) ((lvalue >> 24) & 0xff);
        dest[2] = (card8) ((lvalue >> 16) & 0xff);
        dest[3] = (card8) ((lvalue >> 8) & 0xff);
        dest[4] = (card8) (lvalue & 0xff);
        len = 5;
    } else if (value > CFF_INT_MAX || value < CFF_INT_MIN || (fabs(value - nearint) > 1.0e-5)) {        /* real */
        len = pack_real(dest, destlen, value);
    } else {                    /* integer */
        len = pack_integer(dest, destlen, (long) nearint);
    }

    return len;
}

@ @c
static long put_dict_entry(cff_dict_entry * de, card8 * dest, long destlen)
{
    long len = 0;
    int i, type, id;

    if (de->count > 0) {
        id = de->id;
        if (dict_operator[id].argtype == CFF_TYPE_OFFSET ||
            dict_operator[id].argtype == CFF_TYPE_SZOFF) {
            type = CFF_TYPE_OFFSET;
        } else {
            type = CFF_TYPE_NUMBER;
        }
        for (i = 0; i < de->count; i++) {
            len += cff_dict_put_number(de->values[i],
                                       dest + len, destlen - len, type);
        }
        if (id >= 0 && id < CFF_LAST_DICT_OP1) {
            if (len + 1 > destlen)
                CFF_ERROR("Buffer overflow.");
            dest[len++] = (card8) id;
        } else if (id >= 0 && id < CFF_LAST_DICT_OP) {
            if (len + 2 > destlen)
                CFF_ERROR("in cff_dict_pack(): Buffer overflow");
            dest[len++] = 12;
            dest[len++] = (card8) (id - CFF_LAST_DICT_OP1);
        } else {
            CFF_ERROR("Invalid CFF DICT operator ID.");
        }
    }

    return len;
}

@ @c
long cff_dict_pack(cff_dict * dict, card8 * dest, long destlen)
{
    long len = 0;
    int i;

    for (i = 0; i < dict->count; i++) {
        if (!strcmp(dict->entries[i].key, "ROS")) {
            len += put_dict_entry(&dict->entries[i], dest, destlen);
            break;
        }
    }
    for (i = 0; i < dict->count; i++) {
        if (strcmp(dict->entries[i].key, "ROS")) {
            len += put_dict_entry(&dict->entries[i], dest + len, destlen - len);
        }
    }

    return len;
}


@ @c
void cff_dict_add(cff_dict * dict, const char *key, int count)
{
    int id, i;

    for (id = 0; id < CFF_LAST_DICT_OP; id++) {
        if (key && dict_operator[id].opname &&
            strcmp(dict_operator[id].opname, key) == 0)
            break;
    }

    if (id == CFF_LAST_DICT_OP)
        CFF_ERROR("Unknown CFF DICT operator.");

    for (i = 0; i < dict->count; i++) {
        if ((dict->entries)[i].id == id) {
            if ((dict->entries)[i].count != count)
                CFF_ERROR("Inconsistent DICT argument number.");
            return;
        }
    }

    if (dict->count + 1 >= dict->max) {
        dict->max += 8;
        dict->entries =
            xrealloc(dict->entries,
                     (unsigned) ((unsigned) dict->max *
                                 sizeof(cff_dict_entry)));
    }

    (dict->entries)[dict->count].id = id;
    (dict->entries)[dict->count].key = dict_operator[id].opname;
    (dict->entries)[dict->count].count = count;
    if (count > 0) {
        (dict->entries)[dict->count].values =
            xcalloc((unsigned) count, sizeof(double));
    } else {
        (dict->entries)[dict->count].values = NULL;
    }
    dict->count += 1;

    return;
}


@ @c
void cff_dict_remove(cff_dict * dict, const char *key)
{
    int i;
    for (i = 0; i < dict->count; i++) {
        if (key && strcmp(key, (dict->entries)[i].key) == 0) {
            (dict->entries)[i].count = 0;
            xfree((dict->entries)[i].values);
        }
    }
}

@ @c
void cff_dict_set(cff_dict * dict, const char *key, int idx, double value)
{
    int i;

    assert(dict && key);

    for (i = 0; i < dict->count; i++) {
        if (strcmp(key, (dict->entries)[i].key) == 0) {
            if ((dict->entries)[i].count > idx)
                (dict->entries)[i].values[idx] = value;
            else
                CFF_ERROR("Invalid index number.");
            break;
        }
    }

    if (i == dict->count)
        luatex_fail("DICT entry \"%s\" not found.", key);
}


@ Strings
@c
char *cff_get_string(cff_font * cff, s_SID id)
{
    char *result = NULL;
    size_t len;

    if (id < CFF_STDSTR_MAX) {
        len = strlen(cff_stdstr[id]);
        result = xmalloc((unsigned) (len + 1) * sizeof(char));
        memcpy(result, cff_stdstr[id], len);
        result[len] = '\0';
    } else if (cff && cff->string) {
        cff_index *strings = cff->string;
        id = (s_SID) (id - CFF_STDSTR_MAX);
        if (id < strings->count) {
            len = (strings->offset)[id + 1] - (strings->offset)[id];
            result = xmalloc((unsigned) (len + 1) * sizeof(char));
            memmove(result, strings->data + (strings->offset)[id] - 1, len);
            result[len] = '\0';
        }
    }

    return result;
}

@ @c
long cff_get_sid(cff_font * cff, const char *str)
{
    card16 i;

    if (!cff || !str)
        return -1;

    /* I search String INDEX first. */
    if (cff && cff->string) {
        cff_index *idx = cff->string;
        for (i = 0; i < idx->count; i++) {
            if (strlen(str) == (idx->offset)[i + 1] - (idx->offset)[i] &&
                !memcmp(str, (idx->data) + (idx->offset)[i] - 1, strlen(str)))
                return (i + CFF_STDSTR_MAX);
        }
    }

    for (i = 0; i < CFF_STDSTR_MAX; i++) {
        if (!strcmp(str, cff_stdstr[i]))
            return i;
    }

    return -1;
}


@ @c
void cff_update_string(cff_font * cff)
{
    if (cff == NULL)
        CFF_ERROR("CFF font not opened.");

    if (cff->string)
        cff_release_index(cff->string);
    cff->string = cff->_string;
    cff->_string = NULL;
}


@ @c
s_SID cff_add_string(cff_font * cff, const char *str)
{
    card16 idx;
    cff_index *strings;
    l_offset offset, size;

    if (cff == NULL)
        CFF_ERROR("CFF font not opened.");

    if (cff->_string == NULL)
        cff->_string = cff_new_index(0);
    strings = cff->_string;

    for (idx = 0; idx < strings->count; idx++) {
        size = strings->offset[idx + 1] - strings->offset[idx];
        offset = strings->offset[idx];
        if (size == strlen(str) &&
            !memcmp(strings->data + offset - 1, str, strlen(str)))
            return (s_SID) (idx + CFF_STDSTR_MAX);
    }

    for (idx = 0; idx < CFF_STDSTR_MAX; idx++) {
        if (cff_stdstr[idx] && !strcmp(cff_stdstr[idx], str))
            return idx;
    }
    offset = (strings->count > 0) ? strings->offset[strings->count] : 1;
    strings->offset =
        xrealloc(strings->offset,
                 (unsigned) (((unsigned) strings->count +
                              2) * sizeof(l_offset)));
    if (strings->count == 0)
        strings->offset[0] = 1;
    idx = strings->count;
    strings->count = (card16) (strings->count + 1);
    strings->offset[strings->count] = offset + strlen(str);
    strings->data =
        xrealloc(strings->data,
                 (unsigned) ((offset + strlen(str) - 1) * sizeof(card8)));
    memcpy(strings->data + offset - 1, str, strlen(str));

    return (s_SID) (idx + CFF_STDSTR_MAX);
}


@ @c
void cff_dict_update(cff_dict * dict, cff_font * cff)
{
    int i;

    for (i = 0; i < dict->count; i++) {
        if ((dict->entries)[i].count > 0) {
            char *str;
            int id;

            id = (dict->entries)[i].id;

            if (dict_operator[id].argtype == CFF_TYPE_SID) {
                str = cff_get_string(cff, (s_SID) (dict->entries)[i].values[0]);
                (dict->entries)[i].values[0] = cff_add_string(cff, str);
                xfree(str);
            } else if (dict_operator[id].argtype == CFF_TYPE_ROS) {
                str = cff_get_string(cff, (s_SID) (dict->entries)[i].values[0]);
                (dict->entries)[i].values[0] = cff_add_string(cff, str);
                xfree(str);
                str = cff_get_string(cff, (s_SID) (dict->entries)[i].values[1]);
                (dict->entries)[i].values[1] = cff_add_string(cff, str);
                xfree(str);
            }

        }
    }
}

@ charsets
@c
long cff_read_charsets(cff_font * cff)
{
    cff_charsets *charset;
    long offset, length;
    card16 count, i;

    if (cff->topdict == NULL)
        CFF_ERROR("Top DICT not available");

    if (!cff_dict_known(cff->topdict, "charset")) {
        cff->flag |= CHARSETS_ISOADOBE;
        cff->charsets = NULL;
        return 0;
    }

    offset = (long) cff_dict_get(cff->topdict, "charset", 0);

    if (offset == 0) {          /* predefined */
        cff->flag |= CHARSETS_ISOADOBE;
        cff->charsets = NULL;
        return 0;
    } else if (offset == 1) {
        cff->flag |= CHARSETS_EXPERT;
        cff->charsets = NULL;
        return 0;
    } else if (offset == 2) {
        cff->flag |= CHARSETS_EXPSUB;
        cff->charsets = NULL;
        return 0;
    }

    cff->offset = (l_offset) offset;
    cff->charsets = charset = xcalloc(1, sizeof(cff_charsets));
    charset->format = get_card8(cff);
    charset->num_entries = 0;

    count = (card16) (cff->num_glyphs - 1);
    length = 1;

    /* Not sure. Not well documented. */
    switch (charset->format) {
    case 0:
        charset->num_entries = (card16) (cff->num_glyphs - 1);  /* no .notdef */
        charset->data.glyphs =
            xmalloc((unsigned) (charset->num_entries * sizeof(s_SID)));
        length += (charset->num_entries) * 2;
        for (i = 0; i < (charset->num_entries); i++) {
            charset->data.glyphs[i] = get_card16(cff);
        }
        count = 0;
        break;
    case 1:
        {
            cff_range1 *ranges = NULL;
            while (count > 0 && charset->num_entries < cff->num_glyphs) {
                ranges =
                    xrealloc(ranges,
                             (unsigned) (((unsigned) charset->num_entries +
                                          1) * sizeof(cff_range1)));
                ranges[charset->num_entries].first = get_card16(cff);
                ranges[charset->num_entries].n_left = get_card8(cff);
                count = (card16) (count - ranges[charset->num_entries].n_left + 1);     /* no-overrap */
                charset->num_entries++;
                charset->data.range1 = ranges;
            }
            length += (charset->num_entries) * 3;
        }
        break;
    case 2:
        {
            cff_range2 *ranges = NULL;
            while (count > 0 && charset->num_entries < cff->num_glyphs) {
                ranges =
                    xrealloc(ranges,
                             (unsigned) (((unsigned) charset->num_entries +
                                          1) * sizeof(cff_range2)));
                ranges[charset->num_entries].first = get_card16(cff);
                ranges[charset->num_entries].n_left = get_card16(cff);
                count = (card16) (count - (ranges[charset->num_entries].n_left + 1));   /* non-overrapping */
                charset->num_entries++;
            }
            charset->data.range2 = ranges;
            length += (charset->num_entries) * 4;
        }
        break;
    default:
        xfree(charset);
        CFF_ERROR("Unknown Charset format");
        break;
    }

    if (count > 0) {
        /* fprintf(stdout, "count=%d\n", count); */
        WARN("Charset data possibly broken (num_glyphs too high)");
    }

    return length;
}

@ @c
long cff_pack_charsets(cff_font * cff, card8 * dest, long destlen)
{
    long len = 0;
    card16 i;
    cff_charsets *charset;

    if (cff->flag & HAVE_STANDARD_CHARSETS || cff->charsets == NULL)
        return 0;

    if (destlen < 1)
        CFF_ERROR("in cff_pack_charsets(): Buffer overflow");

    charset = cff->charsets;

    dest[len++] = charset->format;
    switch (charset->format) {
    case 0:
        if (destlen < len + (charset->num_entries) * 2)
            CFF_ERROR("in cff_pack_charsets(): Buffer overflow");
        for (i = 0; i < (charset->num_entries); i++) {
            s_SID sid = (charset->data).glyphs[i];      /* or CID */
            dest[len++] = (card8) ((sid >> 8) & 0xff);
            dest[len++] = (card8) (sid & 0xff);
        }
        break;
    case 1:
        {
            if (destlen < len + (charset->num_entries) * 3)
                CFF_ERROR("in cff_pack_charsets(): Buffer overflow");
            for (i = 0; i < (charset->num_entries); i++) {
                dest[len++] =
                    (card8) (((charset->data).range1[i].first >> 8) & 0xff);
                dest[len++] = (card8) ((charset->data).range1[i].first & 0xff);
                dest[len++] = (card8) ((charset->data).range1[i].n_left);
            }
        }
        break;
    case 2:
        {
            if (destlen < len + (charset->num_entries) * 4)
                CFF_ERROR("in cff_pack_charsets(): Buffer overflow");
            for (i = 0; i < (charset->num_entries); i++) {
                dest[len++] =
                    (card8) (((charset->data).range2[i].first >> 8) & 0xff);
                dest[len++] = (card8) ((charset->data).range2[i].first & 0xff);
                dest[len++] =
                    (card8) (((charset->data).range2[i].n_left >> 8) & 0xff);
                dest[len++] = (card8) ((charset->data).range2[i].n_left & 0xff);
            }
        }
        break;
    default:
        CFF_ERROR("Unknown Charset format");
        break;
    }

    return len;
}



@* Type 2 Charstring support.

Decode and encode Type 2 charstring

All local/global subroutine calls in a given charstring is replace by the
content of subroutine charstrings. We do this because some PostScript RIP
may have problems with sparse subroutine array. Workaround for this is to
re-order subroutine array so that no gap appears in the subroutine array,
or put dummy charstrings that contains only `return' in the gap. However,
re-ordering of subroutine is rather difficult for Type 2 charstrings due
to the bias which depends on the total number of subroutines. Replacing
callgsubr/callsubr calls with the content of the corresponding subroutine
charstring may be more efficient than putting dummy subroutines in the
case of subsetted font. Adobe distiller seems doing same thing.

And also note that subroutine numbers within subroutines can depend on the
content of operand stack as follows:

\.{  ... l m callsubr << subr \#(m+bias): n add callsubr >> ...}

I've not implemented the `random' operator which generates a pseudo-random
number in the range (0, 1] and push them into argument stack.
How pseudo-random sequences are generated is not documented in the Type 2
charstring spec..


@c
#define CS_TYPE2_DEBUG_STR "Type2 Charstring Parser"
#define CS_TYPE2_DEBUG     5

/* decoder/encoder status codes */
#define CS_BUFFER_CFF_ERROR -3
#define CS_STACK_CFF_ERROR  -2
#define CS_PARSE_CFF_ERROR  -1
#define CS_PARSE_OK      0
#define CS_PARSE_END     1
#define CS_SUBR_RETURN   2
#define CS_CHAR_END      3

static int status = CS_PARSE_CFF_ERROR;

#define DST_NEED(a,b) {if ((a) < (b)) { status = CS_BUFFER_CFF_ERROR ; return ; }}
#define SRC_NEED(a,b) {if ((a) < (b)) { status = CS_PARSE_CFF_ERROR  ; return ; }}
#define NEED(a,b)     {if ((a) < (b)) { status = CS_STACK_CFF_ERROR  ; return ; }}

/* hintmask and cntrmask need number of stem zones */
static int num_stems = 0;
static int phase = 0;

/* subroutine nesting */
static int cs2_nest = 0;

/* advance width */
static int have_width = 0;
static double width = 0.0;

@
 Standard Encoding Accented Characters:
 Optional four arguments for endchar. See, CFF spec., p.35.
  This is obsolete feature and is no longer supported.
@c
#if 0
/* adx ady bchar achar endchar */
static double seac[4] = { 0.0, 0.0, 0.0, 0.0 };
#endif

@  Operand stack and Transient array
@c
static int cs2_stack_top = 0;
static double cs2_arg_stack[CS_ARG_STACK_MAX];
static double trn_array[CS_TRANS_ARRAY_MAX];

@ Type 2 CharString encoding
@c

/*
 1-byte CharString operaotrs:
 |cs_escape| is first byte of two-byte operator
*/

/*      RESERVED      0 */
#define cs_hstem      1
/*      RESERVED      2 */
#define cs_vstem      3
#define cs_vmoveto    4
#define cs_rlineto    5
#define cs_hlineto    6
#define cs_vlineto    7
#define cs_rrcurveto  8
/*      |cs_closepath|  9  : TYPE1 */
#define cs_callsubr   10
#define cs_return     11
#define cs_escape     12
/*      |cs_hsbw|       13 : TYPE1 */
#define cs_endchar    14
/*      RESERVED      15 */
/*      RESERVED      16 */
/*      RESERVED      17 */
#define cs_hstemhm    18
#define cs_hintmask   19
#define cs_cntrmask   20
#define cs_rmoveto    21
#define cs_hmoveto    22
#define cs_vstemhm    23
#define cs_rcurveline 24
#define cs_rlinecurve 25
#define cs_vvcurveto  26
#define cs_hhcurveto  27
/*      SHORTINT      28 : first byte of shortint*/
#define cs_callgsubr  29
#define cs_vhcurveto  30
#define cs_hvcurveto  31

/*
 2-byte CharString operaotrs:
 "dotsection" is obsoleted in Type 2 charstring.
 */

#define cs_dotsection 0
/*      |cs_vstem3|     1 : TYPE1 */
/*      |cs_hstem3|     2 : TYPE1 */
#define cs_and        3
#define cs_or         4
#define cs_not        5
/*      |cs_seac|       6 : TYPE1 */
/*      |cs_sbw|        7 : TYPE1 */
/*      RESERVED      8  */
#define cs_abs        9
#define cs_add        10
#define cs_sub        11
#define cs_div        12
/*      RESERVED      13 */
#define cs_neg        14
#define cs_eq         15
/*      |cs_callothersubr| 16 : TYPE1 */
/*      |cs_pop|           17 : TYPE1 */
#define cs_drop       18
/*      RESERVED      19 */
#define cs_put        20
#define cs_get        21
#define cs_ifelse     22
#define cs_random     23
#define cs_mul        24
/*      RESERVED      25 */
#define cs_sqrt       26
#define cs_dup        27
#define cs_exch       28
#define cs_index      29
#define cs_roll       30
/*      |cs_setcurrentpoint| 31 : TYPE1 */
/*      RESERVED      32 */
/*      RESERVED      33 */
#define cs_hflex      34
#define cs_flex       35
#define cs_hflex1     36
#define cs_flex1      37

@
|clear_stack()| put all operands sotred in operand stack to dest.
@c
static void clear_stack(card8 ** dest, card8 * limit)
{
    int i;

    for (i = 0; i < cs2_stack_top; i++) {
        double value;
        long ivalue;
        value = cs2_arg_stack[i];
        /* Nearest integer value */
        ivalue = (long) floor(value + 0.5);
        if (value >= 0x8000L || value <= (-0x8000L - 1)) {
            /*
             This number cannot be represented as a single operand.
             We must use `a b mul ...' or `a c div' to represent large values.
             */
            CFF_ERROR("Argument value too large. (This is bug)");
        } else if (fabs(value - (double) ivalue) > 3.0e-5) {
            /* 16.16-bit signed fixed value  */
            DST_NEED(limit, *dest + 5);
            *(*dest)++ = 255;
            ivalue = (long) floor(value);       /* mantissa */
            *(*dest)++ = (card8) ((ivalue >> 8) & 0xff);
            *(*dest)++ = (card8) (ivalue & 0xff);
            ivalue = (long) ((value - (double) ivalue) * 0x10000l);     /* fraction */
            *(*dest)++ = (card8) ((ivalue >> 8) & 0xff);
            *(*dest)++ = (card8) (ivalue & 0xff);
            /* Everything else are integers. */
        } else if (ivalue >= -107 && ivalue <= 107) {
            DST_NEED(limit, *dest + 1);
            *(*dest)++ = (card8) (ivalue + 139);
        } else if (ivalue >= 108 && ivalue <= 1131) {
            DST_NEED(limit, *dest + 2);
            ivalue = (long) 0xf700u + ivalue - 108;
            *(*dest)++ = (card8) ((ivalue >> 8) & 0xff);
            *(*dest)++ = (card8) (ivalue & 0xff);
        } else if (ivalue >= -1131 && ivalue <= -108) {
            DST_NEED(limit, *dest + 2);
            ivalue = (long) 0xfb00u - ivalue - 108;
            *(*dest)++ = (card8) ((ivalue >> 8) & 0xff);
            *(*dest)++ = (card8) (ivalue & 0xff);
        } else if (ivalue >= -32768 && ivalue <= 32767) {       /* shortint */
            DST_NEED(limit, *dest + 3);
            *(*dest)++ = 28;
            *(*dest)++ = (card8) ((ivalue >> 8) & 0xff);
            *(*dest)++ = (card8) ((ivalue) & 0xff);
        } else {                /* Shouldn't come here */
            CFF_ERROR("Unexpected error.");
        }
    }

    cs2_stack_top = 0;          /* clear stack */

    return;
}

@
 Single byte operators:
  Path construction, Operator for finishing a path, Hint operators.

 phase:
 \item 0: inital state
 \item 1: hint declaration, first stack-clearing operator appeared
 \item 2: in path construction

@c
static void
do_operator1(card8 ** dest, card8 * limit, card8 ** data, card8 * endptr)
{
    card8 op = **data;

    *data += 1;

    switch (op) {
    case cs_hstemhm:
    case cs_vstemhm:
        /* charstring may have hintmask if above operator have seen */
    case cs_hstem:
    case cs_vstem:
        if (phase == 0 && (cs2_stack_top % 2)) {
            have_width = 1;
            width = cs2_arg_stack[0];
        }
        num_stems += cs2_stack_top / 2;
        clear_stack(dest, limit);
        DST_NEED(limit, *dest + 1);
        *(*dest)++ = op;
        phase = 1;
        break;
    case cs_hintmask:
    case cs_cntrmask:
        if (phase < 2) {
            if (phase == 0 && (cs2_stack_top % 2)) {
                have_width = 1;
                width = cs2_arg_stack[0];
            }
            num_stems += cs2_stack_top / 2;
        }
        clear_stack(dest, limit);
        DST_NEED(limit, *dest + 1);
        *(*dest)++ = op;
        if (num_stems > 0) {
            int masklen = (num_stems + 7) / 8;
            DST_NEED(limit, *dest + masklen);
            SRC_NEED(endptr, *data + masklen);
            memmove(*dest, *data, (size_t) masklen);
            *data += masklen;
            *dest += masklen;
        }
        phase = 2;
        break;
    case cs_rmoveto:
        if (phase == 0 && (cs2_stack_top % 2)) {
            have_width = 1;
            width = cs2_arg_stack[0];
        }
        clear_stack(dest, limit);
        DST_NEED(limit, *dest + 1);
        *(*dest)++ = op;
        phase = 2;
        break;
    case cs_hmoveto:
    case cs_vmoveto:
        if (phase == 0 && (cs2_stack_top % 2) == 0) {
            have_width = 1;
            width = cs2_arg_stack[0];
        }
        clear_stack(dest, limit);
        DST_NEED(limit, *dest + 1);
        *(*dest)++ = op;
        phase = 2;
        break;
    case cs_endchar:
        if (cs2_stack_top == 1) {
            have_width = 1;
            width = cs2_arg_stack[0];
            clear_stack(dest, limit);
        } else if (cs2_stack_top == 4 || cs2_stack_top == 5) {
            WARN("\"seac\" character deprecated in Type 2 charstring.");
            status = CS_PARSE_CFF_ERROR;
            return;
        } else if (cs2_stack_top > 0) {
            WARN("Operand stack not empty.");
        }
        DST_NEED(limit, *dest + 1);
        *(*dest)++ = op;
        status = CS_CHAR_END;
        break;
        /* above oprators are candidate for first stack-clearing operator */
    case cs_rlineto:
    case cs_hlineto:
    case cs_vlineto:
    case cs_rrcurveto:
    case cs_rcurveline:
    case cs_rlinecurve:
    case cs_vvcurveto:
    case cs_hhcurveto:
    case cs_vhcurveto:
    case cs_hvcurveto:
        if (phase < 2) {
            WARN("Broken Type 2 charstring.");
            status = CS_PARSE_CFF_ERROR;
            return;
        }
        clear_stack(dest, limit);
        DST_NEED(limit, *dest + 1);
        *(*dest)++ = op;
        break;
        /* all operotors above are stack-clearing operator */
        /* no output */
    case cs_return:
    case cs_callgsubr:
    case cs_callsubr:
        CFF_ERROR("Unexpected call(g)subr/return");
        break;
    default:
        /* no-op ? */
        WARN("%s: Unknown charstring operator: 0x%02x", CS_TYPE2_DEBUG_STR, op);
        status = CS_PARSE_CFF_ERROR;
        break;
    }

    return;
}

@
 Double byte operators:
 Flex, arithmetic, conditional, and storage operators.

 Following operators are not supported:
   random: How random ?

@c
static void
do_operator2(card8 ** dest, card8 * limit, card8 ** data, card8 * endptr)
{
    card8 op;

    *data += 1;

    SRC_NEED(endptr, *data + 1);

    op = **data;
    *data += 1;

    switch (op) {
    case cs_dotsection:        /* deprecated */
        WARN("Operator \"dotsection\" deprecated in Type 2 charstring.");
        status = CS_PARSE_CFF_ERROR;
        return;
        break;
    case cs_hflex:
    case cs_flex:
    case cs_hflex1:
    case cs_flex1:
        if (phase < 2) {
            WARN("%s: Broken Type 2 charstring.", CS_TYPE2_DEBUG_STR);
            status = CS_PARSE_CFF_ERROR;
            return;
        }
        clear_stack(dest, limit);
        DST_NEED(limit, *dest + 2);
        *(*dest)++ = cs_escape;
        *(*dest)++ = op;
        break;
        /* all operator above are stack-clearing */
        /* no output */
    case cs_and:
        NEED(cs2_stack_top, 2);
        cs2_stack_top--;
        if (cs2_arg_stack[cs2_stack_top] && cs2_arg_stack[cs2_stack_top - 1]) {
            cs2_arg_stack[cs2_stack_top - 1] = 1.0;
        } else {
            cs2_arg_stack[cs2_stack_top - 1] = 0.0;
        }
        break;
    case cs_or:
        NEED(cs2_stack_top, 2);
        cs2_stack_top--;
        if (cs2_arg_stack[cs2_stack_top] || cs2_arg_stack[cs2_stack_top - 1]) {
            cs2_arg_stack[cs2_stack_top - 1] = 1.0;
        } else {
            cs2_arg_stack[cs2_stack_top - 1] = 0.0;
        }
        break;
    case cs_not:
        NEED(cs2_stack_top, 1);
        if (cs2_arg_stack[cs2_stack_top - 1]) {
            cs2_arg_stack[cs2_stack_top - 1] = 0.0;
        } else {
            cs2_arg_stack[cs2_stack_top - 1] = 1.0;
        }
        break;
    case cs_abs:
        NEED(cs2_stack_top, 1);
        cs2_arg_stack[cs2_stack_top - 1] =
            fabs(cs2_arg_stack[cs2_stack_top - 1]);
        break;
    case cs_add:
        NEED(cs2_stack_top, 2);
        cs2_arg_stack[cs2_stack_top - 2] += cs2_arg_stack[cs2_stack_top - 1];
        cs2_stack_top--;
        break;
    case cs_sub:
        NEED(cs2_stack_top, 2);
        cs2_arg_stack[cs2_stack_top - 2] -= cs2_arg_stack[cs2_stack_top - 1];
        cs2_stack_top--;
        break;
    case cs_div:               /* doesn't check overflow */
        NEED(cs2_stack_top, 2);
        cs2_arg_stack[cs2_stack_top - 2] /= cs2_arg_stack[cs2_stack_top - 1];
        cs2_stack_top--;
        break;
    case cs_neg:
        NEED(cs2_stack_top, 1);
        cs2_arg_stack[cs2_stack_top - 1] *= -1.0;
        break;
    case cs_eq:
        NEED(cs2_stack_top, 2);
        cs2_stack_top--;
        if (cs2_arg_stack[cs2_stack_top] == cs2_arg_stack[cs2_stack_top - 1]) {
            cs2_arg_stack[cs2_stack_top - 1] = 1.0;
        } else {
            cs2_arg_stack[cs2_stack_top - 1] = 0.0;
        }
        break;
    case cs_drop:
        NEED(cs2_stack_top, 1);
        cs2_stack_top--;
        break;
    case cs_put:
        NEED(cs2_stack_top, 2);
        {
            int idx = (int) cs2_arg_stack[--cs2_stack_top];
            NEED(CS_TRANS_ARRAY_MAX, idx);
            trn_array[idx] = cs2_arg_stack[--cs2_stack_top];
        }
        break;
    case cs_get:
        NEED(cs2_stack_top, 1);
        {
            int idx = (int) cs2_arg_stack[cs2_stack_top - 1];
            NEED(CS_TRANS_ARRAY_MAX, idx);
            cs2_arg_stack[cs2_stack_top - 1] = trn_array[idx];
        }
        break;
    case cs_ifelse:
        NEED(cs2_stack_top, 4);
        cs2_stack_top -= 3;
        if (cs2_arg_stack[cs2_stack_top + 1] > cs2_arg_stack[cs2_stack_top + 2]) {
            cs2_arg_stack[cs2_stack_top - 1] = cs2_arg_stack[cs2_stack_top];
        }
        break;
    case cs_mul:
        NEED(cs2_stack_top, 2);
        cs2_arg_stack[cs2_stack_top - 2] =
            cs2_arg_stack[cs2_stack_top - 2] * cs2_arg_stack[cs2_stack_top - 1];
        cs2_stack_top--;
        break;
    case cs_sqrt:
        NEED(cs2_stack_top, 1);
        cs2_arg_stack[cs2_stack_top - 1] =
            sqrt(cs2_arg_stack[cs2_stack_top - 1]);
        break;
    case cs_dup:
        NEED(cs2_stack_top, 1);
        NEED(CS_ARG_STACK_MAX, cs2_stack_top + 1);
        cs2_arg_stack[cs2_stack_top] = cs2_arg_stack[cs2_stack_top - 1];
        cs2_stack_top++;
        break;
    case cs_exch:
        NEED(cs2_stack_top, 2);
        {
            double save = cs2_arg_stack[cs2_stack_top - 2];
            cs2_arg_stack[cs2_stack_top - 2] = cs2_arg_stack[cs2_stack_top - 1];
            cs2_arg_stack[cs2_stack_top - 1] = save;
        }
        break;
    case cs_index:
        NEED(cs2_stack_top, 2); /* need two arguments at least */
        {
            int idx = (int) cs2_arg_stack[cs2_stack_top - 1];
            if (idx < 0) {
                cs2_arg_stack[cs2_stack_top - 1] =
                    cs2_arg_stack[cs2_stack_top - 2];
            } else {
                NEED(cs2_stack_top, idx + 2);
                cs2_arg_stack[cs2_stack_top - 1] =
                    cs2_arg_stack[cs2_stack_top - idx - 2];
            }
        }
        break;
    case cs_roll:
        NEED(cs2_stack_top, 2);
        {
            int N, J;
            J = (int) cs2_arg_stack[--cs2_stack_top];
            N = (int) cs2_arg_stack[--cs2_stack_top];
            NEED(cs2_stack_top, N);
            if (J > 0) {
                J = J % N;
                while (J-- > 0) {
                    double save = cs2_arg_stack[cs2_stack_top - 1];
                    int i = cs2_stack_top - 1;
                    while (i > cs2_stack_top - N) {
                        cs2_arg_stack[i] = cs2_arg_stack[i - 1];
                        i--;
                    }
                    cs2_arg_stack[i] = save;
                }
            } else {
                J = (-J) % N;
                while (J-- > 0) {
                    double save = cs2_arg_stack[cs2_stack_top - N];
                    int i = cs2_stack_top - N;
                    while (i < cs2_stack_top - 1) {
                        cs2_arg_stack[i] = cs2_arg_stack[i + 1];
                        i++;
                    }
                    cs2_arg_stack[i] = save;
                }
            }
        }
        break;
    case cs_random:
        WARN("%s: Charstring operator \"random\" found.", CS_TYPE2_DEBUG_STR);
        NEED(CS_ARG_STACK_MAX, cs2_stack_top + 1);
        cs2_arg_stack[cs2_stack_top++] = 1.0;
        break;
    default:
        /* no-op ? */
        WARN("%s: Unknown charstring operator: 0x0c%02x", CS_TYPE2_DEBUG_STR,
             op);
        status = CS_PARSE_CFF_ERROR;
        break;
    }

    return;
}

@
 integer:
  exactly the same as the DICT encoding (except 29)
@c
static void cs2_get_integer(card8 ** data, card8 * endptr)
{
    long result = 0;
    card8 b0 = **data, b1, b2;

    *data += 1;

    if (b0 == 28) {             /* shortint */
        SRC_NEED(endptr, *data + 2);
        b1 = **data;
        b2 = *(*data + 1);
        result = b1 * 256 + b2;
        if (result > 0x7fff)
            result -= 0x10000L;
        *data += 2;
    } else if (b0 >= 32 && b0 <= 246) { /* int (1) */
        result = b0 - 139;
    } else if (b0 >= 247 && b0 <= 250) {        /* int (2) */
        SRC_NEED(endptr, *data + 1);
        b1 = **data;
        result = (b0 - 247) * 256 + b1 + 108;
        *data += 1;
    } else if (b0 >= 251 && b0 <= 254) {
        SRC_NEED(endptr, *data + 1);
        b1 = **data;
        result = -(b0 - 251) * 256 - b1 - 108;
        *data += 1;
    } else {
        status = CS_PARSE_CFF_ERROR;
        return;
    }

    NEED(CS_ARG_STACK_MAX, cs2_stack_top + 1);
    cs2_arg_stack[cs2_stack_top++] = (double) result;

    return;
}

@
Signed 16.16-bits fixed number for Type 2 charstring encoding
@c
static void get_fixed(card8 ** data, card8 * endptr)
{
    long ivalue;
    double rvalue;

    *data += 1;

    SRC_NEED(endptr, *data + 4);

    ivalue = *(*data) * 0x100 + *(*data + 1);
    rvalue = (double) ((ivalue > 0x7fffL) ? (ivalue - 0x10000L) : ivalue);
    ivalue = *(*data + 2) * 0x100 + *(*data + 3);
    rvalue += ((double) ivalue) / 0x10000L;

    NEED(CS_ARG_STACK_MAX, cs2_stack_top + 1);
    cs2_arg_stack[cs2_stack_top++] = rvalue;
    *data += 4;

    return;
}

@
 Subroutines:
  The bias for subroutine number is introduced in type 2 charstrings.

  subr:     set to a pointer to the subroutine charstring.
  len:      set to the length of subroutine charstring.
  |subr_idx|: CFF INDEX data that contains subroutines.
  id:       biased subroutine number.

@c
static void get_subr(card8 ** subr, long *len, cff_index * subr_idx, long id)
{
    card16 count;

    if (subr_idx == NULL)
        CFF_ERROR("%s: Subroutine called but no subroutine found.",
                  CS_TYPE2_DEBUG_STR);

    count = subr_idx->count;

    /* Adding bias number */
    if (count < 1240) {
        id += 107;
    } else if (count < 33900) {
        id += 1131;
    } else {
        id += 32768;
    }

    if (id > count)
        CFF_ERROR("%s: Invalid Subr index: %ld (max=%u)", CS_TYPE2_DEBUG_STR,
                  id, count);

    *len = (long) ((subr_idx->offset)[id + 1] - (subr_idx->offset)[id]);
    *subr = subr_idx->data + (subr_idx->offset)[id] - 1;

    return;
}


@ NOTE:
 The Type 2 interpretation of a number encoded in five-bytes (those with
 an initial byte value of 255) differs from how it is interpreted in the
 Type 1 format.

@c
static void
do_charstring(card8 ** dest, card8 * limit,
              card8 ** data, card8 * endptr,
              cff_index * gsubr_idx, cff_index * subr_idx)
{
    card8 b0 = 0, *subr;
    long len;

    if (cs2_nest > CS_SUBR_NEST_MAX)
        CFF_ERROR("%s: Subroutine nested too deeply.", CS_TYPE2_DEBUG_STR);

    cs2_nest++;

    while (*data < endptr && status == CS_PARSE_OK) {
        b0 = **data;
        if (b0 == 255) {        /* 16-bit.16-bit fixed signed number */
            get_fixed(data, endptr);
        } else if (b0 == cs_return) {
            status = CS_SUBR_RETURN;
        } else if (b0 == cs_callgsubr) {
            if (cs2_stack_top < 1) {
                status = CS_STACK_CFF_ERROR;
            } else {
                cs2_stack_top--;
                get_subr(&subr, &len, gsubr_idx,
                         (long) cs2_arg_stack[cs2_stack_top]);
                if (*dest + len > limit)
                    CFF_ERROR("%s: Possible buffer overflow.",
                              CS_TYPE2_DEBUG_STR);
                do_charstring(dest, limit, &subr, subr + len, gsubr_idx,
                              subr_idx);
                *data += 1;
            }
        } else if (b0 == cs_callsubr) {
            if (cs2_stack_top < 1) {
                status = CS_STACK_CFF_ERROR;
            } else {
                cs2_stack_top--;
                get_subr(&subr, &len, subr_idx,
                         (long) cs2_arg_stack[cs2_stack_top]);
                if (limit < *dest + len)
                    CFF_ERROR("%s: Possible buffer overflow.",
                              CS_TYPE2_DEBUG_STR);
                do_charstring(dest, limit, &subr, subr + len, gsubr_idx,
                              subr_idx);
                *data += 1;
            }
        } else if (b0 == cs_escape) {
            do_operator2(dest, limit, data, endptr);
        } else if (b0 < 32 && b0 != 28) {       /* 19, 20 need mask */
            do_operator1(dest, limit, data, endptr);
        } else if ((b0 <= 22 && b0 >= 27) || b0 == 31) {        /* reserved */
            status = CS_PARSE_CFF_ERROR;        /* not an error ? */
        } else {                /* integer */
            cs2_get_integer(data, endptr);
        }
    }

    if (status == CS_SUBR_RETURN) {
        status = CS_PARSE_OK;
    } else if (status == CS_CHAR_END && *data < endptr) {
        WARN("%s: Garbage after endchar.", CS_TYPE2_DEBUG_STR);
    } else if (status < CS_PARSE_OK) {  /* error */
        CFF_ERROR("%s: Parsing charstring failed: (status=%d, stack=%d)",
                  CS_TYPE2_DEBUG_STR, status, cs2_stack_top);
    }

    cs2_nest--;

    return;
}

@ @c
static void cs_parse_init(void)
{
    status = CS_PARSE_OK;
    cs2_nest = 0;
    phase = 0;
    num_stems = 0;
    cs2_stack_top = 0;
}


@ Not just copying...
@c
long
cs_copy_charstring(card8 * dst, long dstlen,
                   card8 * src, long srclen,
                   cff_index * gsubr, cff_index * subr,
                   double default_width, double nominal_width, cs_ginfo * ginfo)
{
    card8 *save = dst;

    cs_parse_init();

    width = 0.0;
    have_width = 0;

    /* expand call(g)subrs */
    do_charstring(&dst, dst + dstlen, &src, src + srclen, gsubr, subr);

    if (ginfo) {
        ginfo->flags = 0;       /* not used */
        if (have_width) {
            ginfo->wx = nominal_width + width;
        } else {
            ginfo->wx = default_width;
        }
    }

    return (long) (dst - save);
}

@* encodings.

@ Encoding and Charset

Encoding and Charset arrays always begin with GID = 1.

@c
long cff_read_encoding(cff_font * cff)
{
    cff_encoding *encoding;
    long offset, length;
    card8 i;

    if (cff->topdict == NULL) {
        CFF_ERROR("Top DICT data not found");
    }

    if (!cff_dict_known(cff->topdict, "Encoding")) {
        cff->flag |= ENCODING_STANDARD;
        cff->encoding = NULL;
        return 0;
    }

    offset = (long) cff_dict_get(cff->topdict, "Encoding", 0);
    if (offset == 0) {          /* predefined */
        cff->flag |= ENCODING_STANDARD;
        cff->encoding = NULL;
        return 0;
    } else if (offset == 1) {
        cff->flag |= ENCODING_EXPERT;
        cff->encoding = NULL;
        return 0;
    }

    cff->offset = (l_offset) offset;
    cff->encoding = encoding = xcalloc(1, sizeof(cff_encoding));
    encoding->format = get_card8(cff);
    length = 1;

    switch (encoding->format & (~0x80)) {
    case 0:
        encoding->num_entries = get_card8(cff);
        (encoding->data).codes = xmalloc(encoding->num_entries * sizeof(card8));
        for (i = 0; i < (encoding->num_entries); i++) {
            (encoding->data).codes[i] = get_card8(cff);
        }
        length += encoding->num_entries + 1;
        break;
    case 1:
        {
            cff_range1 *ranges;
            encoding->num_entries = get_card8(cff);
            encoding->data.range1 = ranges
                = xcalloc(encoding->num_entries, sizeof(cff_range1));
            for (i = 0; i < (encoding->num_entries); i++) {
                ranges[i].first = get_card8(cff);
                ranges[i].n_left = get_card8(cff);
            }
            length += (encoding->num_entries) * 2 + 1;
        }
        break;
    default:
        xfree(encoding);
        CFF_ERROR("Unknown Encoding format");
        break;
    }

    /* Supplementary data */
    if ((encoding->format) & 0x80) {
        cff_map *map;
        encoding->num_supps = get_card8(cff);
        encoding->supp = map = xcalloc(encoding->num_supps, sizeof(cff_map));
        for (i = 0; i < (encoding->num_supps); i++) {
            map[i].code = get_card8(cff);
            map[i].glyph = get_card16(cff);     /* SID */
        }
        length += (encoding->num_supps) * 3 + 1;
    } else {
        encoding->num_supps = 0;
        encoding->supp = NULL;
    }

    return length;
}

@ @c
long cff_pack_encoding(cff_font * cff, card8 * dest, long destlen)
{
    long len = 0;
    cff_encoding *encoding;
    card16 i;

    if (cff->flag & HAVE_STANDARD_ENCODING || cff->encoding == NULL)
        return 0;

    if (destlen < 2)
        CFF_ERROR("in cff_pack_encoding(): Buffer overflow");

    encoding = cff->encoding;

    dest[len++] = encoding->format;
    dest[len++] = encoding->num_entries;
    switch (encoding->format & (~0x80)) {
    case 0:
        if (destlen < len + encoding->num_entries)
            CFF_ERROR("in cff_pack_encoding(): Buffer overflow");
        for (i = 0; i < (encoding->num_entries); i++) {
            dest[len++] = (encoding->data).codes[i];
        }
        break;
    case 1:
        {
            if (destlen < len + (encoding->num_entries) * 2)
                CFF_ERROR("in cff_pack_encoding(): Buffer overflow");
            for (i = 0; i < (encoding->num_entries); i++) {
                dest[len++] = (card8) ((encoding->data).range1[i].first & 0xff);
                dest[len++] = (card8) ((encoding->data).range1[i].n_left);
            }
        }
        break;
    default:
        CFF_ERROR("Unknown Encoding format");
        break;
    }

    if ((encoding->format) & 0x80) {
        if (destlen < len + (encoding->num_supps) * 3 + 1)
            CFF_ERROR("in cff_pack_encoding(): Buffer overflow");
        dest[len++] = encoding->num_supps;
        for (i = 0; i < (encoding->num_supps); i++) {
            dest[len++] = (card8) ((encoding->supp)[i].code);
            dest[len++] = (card8) (((encoding->supp)[i].glyph >> 8) & 0xff);
            dest[len++] = (card8) ((encoding->supp)[i].glyph & 0xff);
        }
    }

    return len;
}

@ CID-Keyed font specific
@c
long cff_read_fdselect(cff_font * cff)
{
    cff_fdselect *fdsel;
    long offset, length;
    card16 i;

    if (cff->topdict == NULL)
        CFF_ERROR("Top DICT not available");

    if (!(cff->flag & FONTTYPE_CIDFONT))
        return 0;

    offset = (long) cff_dict_get(cff->topdict, "FDSelect", 0);
    cff->offset = (l_offset) offset;
    cff->fdselect = fdsel = xcalloc(1, sizeof(cff_fdselect));
    fdsel->format = get_card8(cff);

    length = 1;

    switch (fdsel->format) {
    case 0:
        fdsel->num_entries = cff->num_glyphs;
        (fdsel->data).fds = xmalloc(fdsel->num_entries * sizeof(card8));
        for (i = 0; i < (fdsel->num_entries); i++) {
            (fdsel->data).fds[i] = get_card8(cff);
        }
        length += fdsel->num_entries;
        break;
    case 3:
        {
            cff_range3 *ranges;
            fdsel->num_entries = get_card16(cff);
            fdsel->data.ranges = ranges =
                xcalloc(fdsel->num_entries, sizeof(cff_range3));
            for (i = 0; i < (fdsel->num_entries); i++) {
                ranges[i].first = get_card16(cff);
                ranges[i].fd = get_card8(cff);
            }
            if (ranges[0].first != 0)
                CFF_ERROR("Range not starting with 0.");
            if (cff->num_glyphs != get_card16(cff))
                CFF_ERROR("Sentinel value mismatched with number of glyphs.");
            length += (fdsel->num_entries) * 3 + 4;
        }
        break;
    default:
        xfree(fdsel);
        CFF_ERROR("Unknown FDSelect format.");
        break;
    }

    return length;
}


@ @c
long cff_pack_fdselect(cff_font * cff, card8 * dest, long destlen)
{
    cff_fdselect *fdsel;
    long len = 0;
    card16 i;

    if (cff->fdselect == NULL)
        return 0;

    if (destlen < 1)
        CFF_ERROR("in cff_pack_fdselect(): Buffur overflow");

    fdsel = cff->fdselect;

    dest[len++] = fdsel->format;
    switch (fdsel->format) {
    case 0:
        if (fdsel->num_entries != cff->num_glyphs)
            CFF_ERROR("in cff_pack_fdselect(): Invalid data");
        if (destlen < len + fdsel->num_entries)
            CFF_ERROR("in cff_pack_fdselect(): Buffer overflow");
        for (i = 0; i < fdsel->num_entries; i++) {
            dest[len++] = (fdsel->data).fds[i];
        }
        break;
    case 3:
        {
            if (destlen < len + 2)
                CFF_ERROR("in cff_pack_fdselect(): Buffer overflow");
            len += 2;
            for (i = 0; i < (fdsel->num_entries); i++) {
                if (destlen < len + 3)
                    CFF_ERROR("in cff_pack_fdselect(): Buffer overflow");
                dest[len++] =
                    (card8) (((fdsel->data).ranges[i].first >> 8) & 0xff);
                dest[len++] = (card8) ((fdsel->data).ranges[i].first & 0xff);
                dest[len++] = (card8) ((fdsel->data).ranges[i].fd);
            }
            if (destlen < len + 2)
                CFF_ERROR("in cff_pack_fdselect(): Buffer overflow");
            dest[len++] = (card8) ((cff->num_glyphs >> 8) & 0xff);
            dest[len++] = (card8) (cff->num_glyphs & 0xff);
            dest[1] = (card8) (((len / 3 - 1) >> 8) & 0xff);
            dest[2] = (card8) ((len / 3 - 1) & 0xff);
        }
        break;
    default:
        CFF_ERROR("Unknown FDSelect format.");
        break;
    }

    return len;
}



@ Create an instance of embeddable font.

@c
static void write_fontfile(PDF pdf, cff_font * cffont, char *fullname)
{
    cff_index *topdict, *fdarray, *private;
    unsigned char *dest;
    long destlen = 0, i, size;
    long offset, topdict_offset, fdarray_offset;

    /*  DICT sizes (offset set to long int) */
    topdict = cff_new_index(1);
    fdarray = cff_new_index(cffont->num_fds);
    private = cff_new_index(cffont->num_fds);

    cff_dict_remove(cffont->topdict, "UniqueID");
    cff_dict_remove(cffont->topdict, "XUID");
    cff_dict_remove(cffont->topdict, "Private");        /* some bad font may have */
    cff_dict_remove(cffont->topdict, "Encoding");       /* some bad font may have */

    topdict->offset[1] = (l_offset) cff_dict_pack(cffont->topdict,
                                                  (card8 *) work_buffer,
                                                  WORK_BUFFER_SIZE) + 1;
    for (i = 0; i < cffont->num_fds; i++) {
        size = 0;
        if (cffont->private && cffont->private[i]) {
            size = cff_dict_pack(cffont->private[i],
                                 (card8 *) work_buffer, WORK_BUFFER_SIZE);
            if (size < 1) {     /* Private had contained only Subr */
                cff_dict_remove(cffont->fdarray[i], "Private");
            }
        }
        (private->offset)[i + 1] =
            (unsigned long) ((private->offset)[i] + (unsigned) size);
        (fdarray->offset)[i + 1] =
            (unsigned long) ((fdarray->offset)[i] + (unsigned)
                             cff_dict_pack(cffont->fdarray[i],
                                           (card8 *) work_buffer,
                                           WORK_BUFFER_SIZE));
    }

    destlen = 4;                /* header size */
    destlen += cff_set_name(cffont, fullname);
    destlen += cff_index_size(topdict);
    destlen += cff_index_size(cffont->string);
    destlen += cff_index_size(cffont->gsubr);
    destlen += (cffont->charsets->num_entries) * 2 + 1; /* charset format 0 */
    destlen += (cffont->fdselect->num_entries) * 3 + 5; /* fdselect format 3 */
    destlen += cff_index_size(cffont->cstrings);
    destlen += cff_index_size(fdarray);
    destlen = (long) (destlen + (long) private->offset[private->count] - 1);    /* Private is not INDEX */

    dest = xcalloc((unsigned) destlen, sizeof(card8));

    offset = 0;
    /* Header */
    offset += cff_put_header(cffont, dest + offset, destlen - offset);
    /* Name */
    offset += cff_pack_index(cffont->name, dest + offset, destlen - offset);
    /* Top DICT */
    topdict_offset = offset;
    offset += cff_index_size(topdict);
    /* Strings */
    offset += cff_pack_index(cffont->string, dest + offset, destlen - offset);
    /* Global Subrs */
    offset += cff_pack_index(cffont->gsubr, dest + offset, destlen - offset);

    /* charset */
    cff_dict_set(cffont->topdict, "charset", 0, (double) offset);
    offset += cff_pack_charsets(cffont, dest + offset, destlen - offset);

    /* FDSelect */
    cff_dict_set(cffont->topdict, "FDSelect", 0, (double) offset);
    offset += cff_pack_fdselect(cffont, dest + offset, destlen - offset);

    /* CharStrings */
    cff_dict_set(cffont->topdict, "CharStrings", 0, (double) offset);
    offset += cff_pack_index(cffont->cstrings,
                             dest + offset, cff_index_size(cffont->cstrings));
    cff_release_index(cffont->cstrings);
    cffont->cstrings = NULL;    /* Charstrings cosumes huge memory */

    /* FDArray and Private */
    cff_dict_set(cffont->topdict, "FDArray", 0, (double) offset);
    fdarray_offset = offset;
    offset += cff_index_size(fdarray);

    fdarray->data =
        xcalloc((unsigned) (fdarray->offset[fdarray->count] - 1),
                sizeof(card8));
    for (i = 0; i < cffont->num_fds; i++) {
        size = (long) (private->offset[i + 1] - private->offset[i]);
        if (cffont->private[i] && size > 0) {
            cff_dict_pack(cffont->private[i], dest + offset, size);
            cff_dict_set(cffont->fdarray[i], "Private", 0, (double) size);
            cff_dict_set(cffont->fdarray[i], "Private", 1, (double) offset);
        }
        cff_dict_pack(cffont->fdarray[i],
                      fdarray->data + (fdarray->offset)[i] - 1,
                      (long) (fdarray->offset[fdarray->count] - 1));
        offset += size;
    }

    cff_pack_index(fdarray, dest + fdarray_offset, cff_index_size(fdarray));
    cff_release_index(fdarray);
    cff_release_index(private);

    /* Finally Top DICT */
    topdict->data =
        xcalloc((unsigned) (topdict->offset[topdict->count] - 1),
                sizeof(card8));
    cff_dict_pack(cffont->topdict, topdict->data,
                  (long) (topdict->offset[topdict->count] - 1));
    cff_pack_index(topdict, dest + topdict_offset, cff_index_size(topdict));
    cff_release_index(topdict);

    for (i = 0; i < offset; i++)
        strbuf_putchar(pdf->fb, dest[i]);

    xfree(dest);
    return;
}


@ this block is used a few times
@c
#define DO_COPY_CHARSTRING()                                                   \
  if ((avl_find(fd->gl_tree,glyph) != NULL)) {                                 \
      size = (long)(cs_idx->offset[code+1] - cs_idx->offset[code]);	\
    if (size > CS_STR_LEN_MAX) {                                               \
      luatex_fail("Charstring too long: gid=%u, %ld bytes", code, size);       \
    }                                                                          \
    if (charstring_len + CS_STR_LEN_MAX >= max_len) {                          \
	max_len = (long)(charstring_len + 2 * CS_STR_LEN_MAX);		\
	charstrings->data = xrealloc(charstrings->data, (unsigned)((unsigned)max_len*sizeof(card8))); \
    }                                                                          \
    (charstrings->offset)[gid] = (unsigned)(charstring_len + 1);	\
    cffont->offset= (l_offset)((unsigned)offset + (cs_idx->offset)[code] - 1);	\
    memcpy(data,&cffont->stream[cffont->offset],(size_t)size);		\
    charstring_len += cs_copy_charstring(charstrings->data + charstring_len,   \
                                         max_len - charstring_len,	\
                                         data, size,                           \
                                         cffont->gsubr, (cffont->subrs)[0],    \
                                         default_width, nominal_width, NULL);  \
    gid++;                                                                     \
  }

@ @c
void write_cff(PDF pdf, cff_font * cffont, fd_entry * fd)
{
    cff_index *charstrings, *cs_idx;

    long charstring_len, max_len;
    long size, offset = 0;

    card8 *data;
    card16 num_glyphs, cs_count1, code, gid, last_cid;

    double nominal_width, default_width;

    char *fullname;

    glw_entry *glyph, *found;
    struct avl_traverser t;

    fullname = xcalloc((unsigned) (8 + strlen(fd->fontname)), 1);
    sprintf(fullname, "%s+%s", fd->subset_tag, fd->fontname);

    /* finish parsing the CFF */
    cff_read_private(cffont);
    cff_read_subrs(cffont);

    /*
     Widths
     */
    if (cffont->private[0] &&
        cff_dict_known(cffont->private[0], "defaultWidthX")) {
        default_width =
            (double) cff_dict_get(cffont->private[0], "defaultWidthX", 0);
    } else {
        default_width = CFF_DEFAULTWIDTHX_DEFAULT;
    }
    if (cffont->private[0] &&
        cff_dict_known(cffont->private[0], "nominalWidthX")) {
        nominal_width =
            (double) cff_dict_get(cffont->private[0], "nominalWidthX", 0);
    } else {
        nominal_width = CFF_NOMINALWIDTHX_DEFAULT;
    }

    num_glyphs = 0;
    last_cid = 0;
    glyph = xtalloc(1, glw_entry);

    /* insert notdef */
    glyph->id = 0;
    if (avl_find(fd->gl_tree, glyph) == NULL) {
        avl_insert(fd->gl_tree, glyph);
        glyph = xtalloc(1, glw_entry);
    }

    avl_t_init(&t, fd->gl_tree);
    for (found = (glw_entry *) avl_t_first(&t, fd->gl_tree);
         found != NULL; found = (glw_entry *) avl_t_next(&t)) {
        if (found->id > last_cid)
            last_cid = (card16) found->id;
        num_glyphs++;
    }

    {
        cff_fdselect *fdselect;

        fdselect = xcalloc(1, sizeof(cff_fdselect));
        fdselect->format = 3;
        fdselect->num_entries = 1;
        fdselect->data.ranges = xcalloc(1, sizeof(cff_range3));
        fdselect->data.ranges[0].first = 0;
        fdselect->data.ranges[0].fd = 0;
        cffont->fdselect = fdselect;
    }

    {
        cff_charsets *charset;

        charset = xcalloc(1, sizeof(cff_charsets));
        charset->format = 0;
        charset->num_entries = (card16) (num_glyphs - 1);
        charset->data.glyphs = xcalloc(num_glyphs, sizeof(s_SID));

        gid = 0;

        avl_t_init(&t, fd->gl_tree);
        for (found = (glw_entry *) avl_t_first(&t, fd->gl_tree);
             found != NULL; found = (glw_entry *) avl_t_next(&t)) {
            if (found->id != 0) {
                charset->data.glyphs[gid] = (s_SID) found->id;
                gid++;
            }
        }
        cffont->charsets = charset;
    }

    cff_dict_add(cffont->topdict, "CIDCount", 1);
    cff_dict_set(cffont->topdict, "CIDCount", 0, last_cid + 1);

    cffont->fdarray = xcalloc(1, sizeof(cff_dict *));
    cffont->fdarray[0] = cff_new_dict();
    cff_dict_add(cffont->fdarray[0], "FontName", 1);
    cff_dict_set(cffont->fdarray[0], "FontName", 0, (double) cff_add_string(cffont, fullname)); /* FIXME: Skip XXXXXX+ */
    cff_dict_add(cffont->fdarray[0], "Private", 2);
    cff_dict_set(cffont->fdarray[0], "Private", 0, 0.0);
    cff_dict_set(cffont->fdarray[0], "Private", 0, 0.0);
    /* FDArray  - index offset, not known yet */
    cff_dict_add(cffont->topdict, "FDArray", 1);
    cff_dict_set(cffont->topdict, "FDArray", 0, 0.0);
    /* FDSelect - offset, not known yet */
    cff_dict_add(cffont->topdict, "FDSelect", 1);
    cff_dict_set(cffont->topdict, "FDSelect", 0, 0.0);

    cff_dict_remove(cffont->topdict, "UniqueID");
    cff_dict_remove(cffont->topdict, "XUID");
    cff_dict_remove(cffont->topdict, "Private");
    cff_dict_remove(cffont->topdict, "Encoding");

    cffont->offset = (l_offset) cff_dict_get(cffont->topdict, "CharStrings", 0);
    cs_idx = cff_get_index_header(cffont);

    offset = (long) cffont->offset;
    cs_count1 = cs_idx->count;
    if (cs_count1 < 2) {
        CFF_ERROR("No valid charstring data found.");
    }

    /* build the new charstrings entry */
    charstrings = cff_new_index((card16) (cs_count1 + 1));
    max_len = 2 * CS_STR_LEN_MAX;
    charstrings->data = xcalloc((unsigned) max_len, sizeof(card8));
    charstring_len = 0;

    gid = 0;
    data = xcalloc(CS_STR_LEN_MAX, sizeof(card8));

    {
        int i;
        for (i = 0; i < cs_count1; i++) {
            code = (card16) i;
            glyph->id = code;
            DO_COPY_CHARSTRING();
        }
    }

    /* CIDSet: a table of bits indexed by cid, bytes with high order bit first,
       each (set) bit is a (present) CID. */
    if (1) {
      int cid;
      cidset = pdf_create_obj(pdf, obj_type_others, 0);
      if (cidset != 0) {
       size_t l = (last_cid/8)+1;
       char *stream = xmalloc(l);
       memset(stream, 0, l);
       for (cid = 1; cid <= (long) last_cid; cid++) {
           glyph->id = cid;
           if (avl_find(fd->gl_tree,glyph) != NULL) {
	      stream[(cid / 8)] |= (1 << (7 - (cid % 8)));
           }
       }
       pdf_begin_obj(pdf, cidset, OBJSTM_NEVER);
       pdf_begin_dict(pdf);
       pdf_dict_add_streaminfo(pdf);
       pdf_end_dict(pdf);
       pdf_begin_stream(pdf);
       pdf_out_block(pdf, stream, l);
       pdf_end_stream(pdf);
       pdf_end_obj(pdf);
      }
    }

    /* this happens if the internal metrics do not agree with the actual disk font */
    if (gid < num_glyphs) {
        WARN("embedded subset is smaller than expected: %d instead of %d glyphs.", gid, num_glyphs);
        num_glyphs = gid;
    }

    xfree(data);
    cff_release_index(cs_idx);

    (charstrings->offset)[num_glyphs] = (l_offset) (charstring_len + 1);
    charstrings->count = num_glyphs;
    cffont->num_glyphs = num_glyphs;
    cffont->cstrings = charstrings;

    /*
     We don't use subroutines at all.
     */
    if (cffont->gsubr)
        cff_release_index(cffont->gsubr);
    cffont->gsubr = cff_new_index(0);

    if (cffont->subrs && cffont->subrs[0])
        cff_release_index(cffont->subrs[0]);
    cffont->subrs[0] = NULL;

    if (cffont->private && (cffont->private)[0]) {
        cff_dict_remove((cffont->private)[0], "Subrs"); /* no Subrs */
    }

    cff_add_string(cffont, "Adobe");
    cff_add_string(cffont, "Identity");

    cff_dict_update(cffont->topdict, cffont);
    cff_dict_update(cffont->private[0], cffont);
    cff_update_string(cffont);

    /* CFF code need to be rewrote... */
    cff_dict_add(cffont->topdict, "ROS", 3);
    cff_dict_set(cffont->topdict, "ROS", 0,
                 (double) cff_get_sid(cffont, "Adobe"));
    cff_dict_set(cffont->topdict, "ROS", 1,
                 (double) cff_get_sid(cffont, "Identity"));
    cff_dict_set(cffont->topdict, "ROS", 2, 0.0);

    write_fontfile(pdf, cffont, fullname);
    xfree(fullname);
    cff_close(cffont);

}

@ @c
#undef ERROR                    /* for mingw */
#define ERROR(a) { perror(a); return 0; }

@ Input : SID or CID (16-bit unsigned int)
 Output: glyph index

@c
card16 cff_charsets_lookup(cff_font * cff, card16 cid)
{
    card16 gid = 0;
    cff_charsets *charset;
    card16 i;

    if (cff->flag & (CHARSETS_ISOADOBE | CHARSETS_EXPERT | CHARSETS_EXPSUB)) {
        ERROR("Predefined CFF charsets not supported yet");
    } else if (cff->charsets == NULL) {
        ERROR("Charsets data not available");
    }

    if (cid == 0) {
        return 0;               /* GID 0 (.notdef) */
    }

    charset = cff->charsets;

    gid = 0;
    switch (charset->format) {
    case 0:
        for (i = 0; i < charset->num_entries; i++) {
            if (cid == charset->data.glyphs[i]) {
                gid = (card16) (i + 1);
                return gid;
            }
        }
        break;
    case 1:
        for (i = 0; i < charset->num_entries; i++) {
            if (cid >= charset->data.range1[i].first &&
                cid <=
                charset->data.range1[i].first +
                charset->data.range1[i].n_left) {
                gid = (card16) (gid + cid - charset->data.range1[i].first + 1);
                return gid;
            }
            gid = (card16) (gid + charset->data.range1[i].n_left + 1);
        }
        break;
    case 2:
        for (i = 0; i < charset->num_entries; i++) {
            if (cid >= charset->data.range2[i].first &&
                cid <=
                charset->data.range2[i].first +
                charset->data.range2[i].n_left) {
                gid = (card16) (gid + cid - charset->data.range2[i].first + 1);
                return gid;
            }
            gid = (card16) (gid + charset->data.range2[i].n_left + 1);
        }
        break;
    default:
        ERROR("Unknown Charset format");
    }

    return 0;                   /* not found */
}


@ @c
#define is_cidfont(a) ((a)->flag & FONTTYPE_CIDFONT)
#define CID_MAX 65535
void write_cid_cff(PDF pdf, cff_font * cffont, fd_entry * fd)
{
    cff_index *charstrings, *cs_idx;

    long charstring_len, max_len;
    long size, offset = 0;

    card8 *data;
    card16 num_glyphs, cs_count1, gid, last_cid;


    int fdsel, prev_fd, cid_count, cid;
    char *fullname;

    glw_entry *glyph;

    unsigned char *CIDToGIDMap = NULL;

    cff_fdselect *fdselect = NULL;
    cff_charsets *charset = NULL;


    if (!is_cidfont(cffont)) {
        perror("Not a CIDfont.");
        return;
    }

    fullname = xcalloc((unsigned) (8 + strlen(fd->fontname)), 1);
    sprintf(fullname, "%s+%s", fd->subset_tag, fd->fontname);

    /* finish parsing the CFF */

    if (cff_dict_known(cffont->topdict, "CIDCount")) {
        cid_count = (card16) cff_dict_get(cffont->topdict, "CIDCount", 0);
    } else {
        cid_count = CFF_CIDCOUNT_DEFAULT;
    }
    cff_read_charsets(cffont);
    CIDToGIDMap = xmalloc((unsigned)
                          ((2 * (unsigned) cid_count) * sizeof(unsigned char)));
    memset(CIDToGIDMap, 0, (size_t) (2 * cid_count));


    glyph = xtalloc(1, glw_entry);
    /* insert notdef */
    glyph->id = 0;
    if (avl_find(fd->gl_tree, glyph) == NULL) {
        avl_insert(fd->gl_tree, glyph);
        glyph = xtalloc(1, glw_entry);
    }

    last_cid = 0;
    num_glyphs = 0;
    for (cid = 0; cid <= CID_MAX; cid++) {
        glyph->id = (unsigned) cid;
        if (avl_find(fd->gl_tree, glyph) != NULL) {
            gid = (card16) cff_charsets_lookup(cffont, (card16) cid);
            CIDToGIDMap[2 * cid] = (unsigned char) ((gid >> 8) & 0xff);
            CIDToGIDMap[2 * cid + 1] = (unsigned char) (gid & 0xff);
            last_cid = (card16) cid;
            num_glyphs++;
        }
    }

    /* CIDSet: a table of bits indexed by cid, bytes with high order bit first,
       each (set) bit is a (present) CID. */
    if (1) {
        cidset = pdf_create_obj(pdf, obj_type_others, 0);
        if (cidset != 0) {
            size_t l = (last_cid / 8) + 1;
            char *stream = xmalloc(l);
            memset(stream, 0, l);
            for (cid = 1; cid <= (long) last_cid; cid++) {
                if (CIDToGIDMap[2 * cid] || CIDToGIDMap[2 * cid + 1]) {
                    stream[(cid / 8)] |= (1 << (7 - (cid % 8)));
                }
            }
            pdf_begin_obj(pdf, cidset, OBJSTM_NEVER);
            pdf_begin_dict(pdf);
            pdf_dict_add_streaminfo(pdf);
            pdf_end_dict(pdf);
	    pdf_begin_stream(pdf);
            pdf_out_block(pdf, stream, l);
            pdf_end_stream(pdf);
            pdf_end_obj(pdf);
        }
    }


    cff_read_fdselect(cffont);
    cff_read_fdarray(cffont);
    cff_read_private(cffont);

    cff_read_subrs(cffont);


    cffont->offset = (l_offset) cff_dict_get(cffont->topdict, "CharStrings", 0);
    cs_idx = cff_get_index_header(cffont);

    offset = (long) cffont->offset;
    cs_count1 = cs_idx->count;
    if (cs_count1 < 2) {
        CFF_ERROR("No valid charstring data found.");
    }

    charset = xcalloc(1, sizeof(cff_charsets));
    charset->format = 0;
    charset->num_entries = 0;
    charset->data.glyphs = xcalloc(num_glyphs, sizeof(s_SID));

    fdselect = xcalloc(1, sizeof(cff_fdselect));
    fdselect->format = 3;
    fdselect->num_entries = 0;
    fdselect->data.ranges = xcalloc(num_glyphs, sizeof(cff_range3));

    charstrings = cff_new_index((card16) (cs_count1 + 1));
    max_len = 2 * CS_STR_LEN_MAX;
    charstrings->data = xcalloc((unsigned) max_len, sizeof(card8));
    charstring_len = 0;

    prev_fd = -1;
    gid = 0;
    data = xcalloc(CS_STR_LEN_MAX, sizeof(card8));
    for (cid = 0; cid <= last_cid; cid++) {
        unsigned short gid_org;

        glyph->id = (unsigned) cid;
        if (avl_find(fd->gl_tree, glyph) == NULL)
            continue;

        gid_org =
            (short unsigned) ((CIDToGIDMap[2 * cid] << 8) |
                              (CIDToGIDMap[2 * cid + 1]));
        size = (long) (cs_idx->offset[gid_org + 1] - cs_idx->offset[gid_org]);
        if (size > CS_STR_LEN_MAX) {
            luatex_fail("Charstring too long: gid=%u, %ld bytes", cid, size);
        }
        if (charstring_len + CS_STR_LEN_MAX >= max_len) {
            max_len = charstring_len + 2 * CS_STR_LEN_MAX;
            charstrings->data =
                xrealloc(charstrings->data,
                         (unsigned) ((unsigned) max_len * sizeof(card8)));
        }
        (charstrings->offset)[gid] = (l_offset) (charstring_len + 1);
        cffont->offset =
            (l_offset) ((unsigned) offset + (cs_idx->offset)[gid_org] - 1);
        memcpy(data, &cffont->stream[cffont->offset], (size_t) size);
        fdsel = cff_fdselect_lookup(cffont, gid_org);
        charstring_len += cs_copy_charstring(charstrings->data + charstring_len,
                                             max_len - charstring_len,
                                             data, size,
                                             cffont->gsubr,
                                             (cffont->subrs)[fdsel], 0, 0,
                                             NULL);

        if (cid > 0 && gid_org > 0) {
            charset->data.glyphs[charset->num_entries] = (s_SID) cid;
            charset->num_entries++;
        }
        if (fdsel != prev_fd) {
            fdselect->data.ranges[fdselect->num_entries].first = gid;
            fdselect->data.ranges[fdselect->num_entries].fd = (card8) fdsel;
            fdselect->num_entries++;
            prev_fd = fdsel;
        }
        gid++;
    }

    if (gid != num_glyphs)
        CFF_ERROR("Unexpected error: %i != %i", gid, num_glyphs);
    xfree(data);
    cff_release_index(cs_idx);

    xfree(CIDToGIDMap);

    (charstrings->offset)[num_glyphs] = (l_offset) (charstring_len + 1);
    charstrings->count = num_glyphs;
    cffont->num_glyphs = num_glyphs;
    cffont->cstrings = charstrings;

    cff_release_charsets(cffont->charsets);
    cffont->charsets = charset;
    cff_release_fdselect(cffont->fdselect);
    cffont->fdselect = fdselect;

    /*
     * We don't use subroutines at all.
     */
    if (cffont->gsubr)
        cff_release_index(cffont->gsubr);
    cffont->gsubr = cff_new_index(0);

    for (fdsel = 0; fdsel < cffont->num_fds; fdsel++) {
        if (cffont->subrs && cffont->subrs[fdsel]) {
            cff_release_index(cffont->subrs[fdsel]);
            cffont->subrs[fdsel] = NULL;
        }
        if (cffont->private && (cffont->private)[fdsel]) {
            cff_dict_remove((cffont->private)[fdsel], "Subrs"); /* no Subrs */
        }
    }

    write_fontfile(pdf, cffont, fullname);
    xfree(fullname);
    cff_close(cffont);

}

@ here is a sneaky trick: fontforge knows how to convert Type1 to CFF, so
I have defined a utility function in luafflib.c that does exactly that.
If it works out ok, I will clean up this code.

@c
void writetype1w(PDF pdf, fd_entry * fd)
{
    cff_font *cff;
    int i;
    FILE *fp;
    ff_entry *ff;
    unsigned char *tfm_buffer = NULL;
    int tfm_size = 0;

    ff = check_ff_exist(fd->fm->ff_name, 0);

    fp = fopen(ff->ff_path, "rb");
    cur_file_name = ff->ff_path;

    if (!fp) {
        fprintf(stderr, "Type1: Could not open Type1 font: %s", cur_file_name);
        uexit(1);
    }
    fclose(fp);

    if (is_subsetted(fd->fm)) {
        report_start_file(filetype_subset,cur_file_name);
    } else {
        report_start_file(filetype_font,cur_file_name);
    }
    (void) ff_createcff(ff->ff_path, &tfm_buffer, &tfm_size);

    if (tfm_size > 0) {
        cff = read_cff(tfm_buffer, tfm_size, 0);
        if (cff != NULL) {
            write_cff(pdf, cff, fd);
        } else {
            for (i = 0; i < tfm_size; i++)
                strbuf_putchar(pdf->fb, tfm_buffer[i]);
        }
        fd->ff_found = 1;
    } else {
        fprintf(stderr, "Type1: Could not understand Type1 font: %s",
                cur_file_name);
        uexit(1);
    }
    if (is_subsetted(fd->fm)) {
        report_stop_file(filetype_subset);
    } else {
        report_stop_file(filetype_font);
    }
    cur_file_name = NULL;
}