summaryrefslogtreecommitdiff
path: root/Master/tlpkg/tlgs/Resource/Init/pdf_font.ps
blob: 1be2871b105ab72af9cd7b0f2b9877a145bd0fb5 (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
% Copyright (C) 2001-2022 Artifex Software, Inc.
% All Rights Reserved.
%
% This software is provided AS-IS with no warranty, either express or
% implied.
%
% This software is distributed under license and may not be copied,
% modified or distributed except as expressly authorized under the terms
% of the license contained in the file LICENSE in this distribution.
%
% Refer to licensing information at http://www.artifex.com or contact
% Artifex Software, Inc.,  1305 Grant Avenue - Suite 200, Novato,
% CA 94945, U.S.A., +1(415)492-9861, for further information.
%

% PDF font operations.

% Since PDF font are not unique and can collide with external font resources
% or each other, use font dictionaries obtained from PDF directly, never
% register them as resources or look them up by name. Use findfont oparator
% for non-embedded fonts only. CIDFont resources still use the old logic
% described below.

% Finding a font by name can't give a proper result when PDF font names aren't unique.
% But it is only the way to obtain a font in Postscript after a font file is executed.
% Therefore using a FontName (and findfont) is allowed only
% immediately after a font file is executed.
% In all other cases the font to be found by a pointer through PDF structures.
%
% This ideal logics can't work for documents,
% which define a font resource with an embedded font,
% and another font resource with same BaseFont but with no embedded font
% (and possibly with no font descriptor).
% Our testbase does contain such examples.
% In this case we do find font by FontName (with findfont),
% since there is no other way to get a reasonable result.

/.setlanguagelevel where { pop 2 .setlanguagelevel } if
.currentglobal //true .setglobal
/GS_PDF_ProcSet load begin	% from userdict at this point
pdfdict begin

% We cache the PostScript font in an additional element of the
% font resource dictionary, called PSFont.

% ---------------- Encodings ---------------- %

/.notdefEncoding 256 { /.notdef } repeat 256 packedarray def

% Apply a list of differences to an Encoding.
% Note that the differences may cause the array to grow.
/updateencoding {	% <encoding|null> <differences> updateencoding <enc'>
        % Calculate the length of the result.
  % in case the incoming Encoding is null, use .notdefEncoding
  exch dup //null eq { pop .notdefEncoding } if
  0 0 3 index {
    dup type /nametype ne { exch pop oforce } { pop 1 add } ifelse
    % Differences list may not be in order, update the largest_index
    % stack: <Differences> <encoding> <largest_index> <at_index>
    2 copy lt { exch pop dup } if	% at_index is new largest
  } forall
  pop 1 index length .max array dup 0 4 -1 roll putinterval
  exch 0 exch {
                % Stack: enc' code element
    dup type /nametype ne
      { exch pop oforce }
      { 3 copy put pop 1 add }
    ifelse
  } forall pop
} bind executeonly def

/good_encoding_names <<
  /MacRomanEncoding 0 /MacExpertEncoding 0 /WinAnsiEncoding 0
>> readonly def

/known_symbolic_fonts <<
  /Wingdings2 0
  /ZapfDingbats 0
>> readonly def

% Get the Encoding for a font.
/getencoding		% <base-encoding> <font-resource> getencoding <enc>
 { dup /Encoding knownoget
    { dup type /nametype eq
       {
         % The published PDF specification says the Encoding name
         % "must be" one of the 3 predefined Encodings, implying
         % that an error should occur if it isn't.  However, Acrobat
         % Reader simply ignores unknown names, and since there are
         % some buggy applications that rely on this, we do the same.

         //good_encoding_names 1 index known {
           exch /BaseFont knownoget not { 0 } if
           % Ignore named encodings for known symbolic fonts. See bug 690135.
           //known_symbolic_fonts exch known {
             pop
           } {
             exch pop findencoding
           } ifelse
         } {
           pop pop
         } ifelse
       }
       { exch pop
         dup type /arraytype eq
         { exch pop
           (   **** Warning: Encoding is an array, not name or dictionary.\n)  pdfformatwarning
         }
         { dup /BaseEncoding knownoget
             {
               %% Originally we ignored the specific invalid name '/' here from Bug #687786, however
               %% Bug #696942 contains an invalid BaseEncoding '/utf-8', so now we check all BaseEncoding
               %% against the permitted names, just as we do for Encodings.
               //good_encoding_names 1 index known{
                 findencoding 3 -1 roll pop exch
               }{
                 (   **** Error: Invalid BaseEncoding name ") print 256 string cvs print (" ignoring BaseEncoding.\n)  pdfformaterror
                 (               Output may be incorrect.\n) pdfformaterror
               }ifelse
             } if
           /Differences knownoget { updateencoding } if
         }
         ifelse
       }
       ifelse
     } {
       pop
       (   **** Error: Encoding not present.\n)  pdfformaterror
       (                 Output may be incorrect.\n) pdfformaterror
     }
     ifelse
 } bind executeonly def

currentdict /good_encoding_names undef
currentdict /known_symbolic_fonts undef

/checkGlyphNames2Unicode % <dict> checkGlyphNames2Unicode -
{
  PDFDEBUG {
    dup /FontInfo .knownget {
      /GlyphNames2Unicode .knownget {
        (Has GlyphNames2Unicode) =
        pop % { exch //== exec //== exec} forall
      } if
    } if
  } if
  pop
} bind executeonly def

% Define a font using it's FontName as the key.
% Adjust a font according to the Encoding and Widths in the font resource.
/adjustfont {		% <font-resource> <font> adjustfont <font'>
  getfontencoding
  3 copy .processToUnicode
  getfontmetrics 5 -1 roll pop .updatefont {
    %% Bug #696306 has a broken type 1 font where the eexec encrypted portion simply ends
    %% leaving a partial glyph description and, more importantly, the Private dictionary
    %% on the operand stack. Ths means the font dictonary does not contain a Private
    %% dictionary which causes .buildfont1 to throw an error. .completefont calls .buildfont1
    %% (which is obscured by a definition in gs_type1.ps) so we need to check the reault
    %% of .completefont. If it fails we must discard the font dictionary and use a fallback.
    {.completefont} stopped {
      (\n *** ERROR: The font ) print /FontName get 256 string cvs print ( is damaged and cannot be used. Switching to a\n) pdfformaterror
      (            last-ditch fallback, text may not render correctly, or at all.\n\n) pdfformaterror
      /Helvetica findfont
    }if
  } if
} bind executeonly def

% Get the (possibly modified) encoding of a font.
/getfontencoding {	% <font-resource> <font> getfontencoding
                        %   <font-resource> <font> <Encoding|null>
  % Ignore encoding when TrueType is requested
  % and loaded from an embedded stream.
  % All other cases are left unchanged.
  dup /FontType get 42 eq 1 index /PathLoad known not and
  2 index /Subtype get /TrueType eq and {
    //null
  } {
    % Acrobat appears to be always using StandardEcoding as the
    % default encoding for some non-symbolic fonts (rather than,
    % as the spec states, the encoding specified by the font).
    % As this is contrary to spec, and we only have a CFF
    % example, I've restricted this to fonts with Subtype Type1C
    % *and* that result in a Type 2 PS font.
    1 index /FontDescriptor knownoget
    {
      dup
      % Unfortunately, Acrobat doesn't enforce the meaning of
      % FontFile[23] so whilst, in theory, Type1C should only
      % occur in FontFile3, we can't rely on that.
      dup /FontFile knownoget not
      {
        dup /FontFile2 knownoget not
        {
          dup /FontFile3 knownoget not {1 dict} if
        } if
      } if
      exch pop /Subtype knownoget {/Type1C eq}{//false} ifelse
      exch /Flags knownoget {4 and 4 eq not}{//false}ifelse and
    } {//false} ifelse

    1 index /FontType get 2 eq and
    {
        2 index /Encoding knownoget {pop StandardEncoding 2 index getencoding} { //null } ifelse
    }
    {
      2 index /Encoding known {
        dup /Encoding knownoget { 2 index getencoding } { //null } ifelse
      } {
        //null
      } ifelse
    } ifelse
  } ifelse
} bind executeonly def

% Returns true if the current glyph is in the Differences array at
% the specified index value. This is needed because the Widths
% array may map to the same glyph at different positions from the
% Encoding. We want to use the Width that was associated with the
% one specified in the Encoding::Differences list.
/match_in_diff     % <Differences> <index> <glyphname> match_in_diff <bool>
{ //false 4 1 roll 0 4 -1 roll	% stack: false index glyphname at_index==0 Differences
  { exch 1 index type /nametype ne {
      % stack: false index glyphname Diff_element at_index
      pop	% Diff_element is new at_index
    } {
      % stack: false index glyphname Diff_element at_index
      exch 2 index eq {
        % stack: false index glyphname at_index
        dup 3 index eq {
          //true 5 1 roll % stack: true false index glyphname at_index
          pop exit
        } if
      } if
      1 add		% at_index++ stack: false index glyphname at_index'
    } ifelse
  } forall
  % stack: true  false index     glyphname
  %  or  : false index glyphname at_index
  pop pop pop
} bind executeonly def

/unique_name {  % <dict> </root> unique_name </unique>
  %
  %  Note : this function interacts with pdf_write_encoding in src/gdevpdtw.c
  %  and with copied_drop_extension_glyphs in src\gxfcopy.c
  %  by adding a reserved substring (~GS~).
  %
  .namestring       % <<>> (root)
  0 1 65535 {
    5 string cvs    % <<>> (root) (0)
    (~GS~) exch concatstrings
    1 index exch    % <<>> (root) (root) (~GS~0)
    concatstrings   % <<>> (root) (root~GS~0)
    dup             % <<>> (root) (root~GS~0) (root~GS~0)
    3 index exch    % <<>> (root) (root~GS~0) <<>> (root~GS~0)
    known not {
      exch pop exit % <<>> (root~GS~0)
    } if
    pop
  } for
  exch pop cvn      % /root0
} bind executeonly def

% Get the metrics of a font, if specified.
/getfontmetrics {       % <font-resource> <font> <Encoding|null> getfontmetrics
                        %   <font-resource> <font> <Encoding|null>
                        %   <Metrics|null> <GlyphMap|null>
  2 index /Widths oknown {
    dup //null eq { pop dup /Encoding get } if
    7 dict begin
      dup length dict
      /Metrics exch def
      /Encoding exch def
      /GlyphMap //null def
      exch
      dup /Widths oget /Widths exch def
                % Stack: font font-res
                % Note that widths are always based on a 1000-unit
                % character space, but the FontMatrix may specify
                % some other scale factor.  Compensate for this here,
                % by scaling the Widths if necessary.
      0.001 2 index /FontMatrix get 0 get
      dup 0 eq {
                % FontMatrix.xx == 0, so we cannot scale down by xx.
                % - FontMatrix[0] == 0 implies either FontMatrix[1] <> 0 or
                %   FontMatrix cannot be inverted. In the 1st case we have
                %   FontMatrixNonHV == true and will render text with cshow + xshow.
                %   In the 2nd case, metrics in the PDF Font object cannot be enforced
                %   [by altering metrics in PS glyph space].
                % HACK:
                % - we scale down by FontMatrix[1];
                % - given the format of Metrics entries we use, wy = 0 in glyph space;
                % - as a result, the cshow procedure receives as wy the value we
                %   need for wx (all of this in PS user space).
        pop
        2 index /FontMatrix get 1 get
        dup 0 eq { pop 1 } if % sorry, no way to enforce PDF metrics by altering the font
      } if
      div
                % Stack: font font-res mscale
      /FirstChar 2 index /FirstChar knownoget not {
        (   **** Warning: /FirstChar attribute is missing, assuming 0.\n) pdfformatwarning
        0
      } if def
      /LastChar  2 index /LastChar  knownoget not {
        (   **** Warning: /LastChar attribute is missing, assuming 255.\n) pdfformatwarning
        255
      } if def

      Encoding length LastChar le {
         (   **** Warning: Font Encoding array size is smaller than character range.\n)
         pdfformatwarning
      } if

      1 index /FontDescriptor knownoget {
           /MissingWidth knownoget not { 0 } if
        }
        { 1000
        }
      ifelse /MissingWidth exch def

      Widths length LastChar FirstChar sub le {
         (   **** Warning: Font Widths array size is smaller than character range.\n)
         pdfformatwarning
         /Widths [Widths aload length LastChar FirstChar sub exch sub MissingWidth exch {dup} repeat] def
      } if
      FirstChar

      0 Encoding
       {        % Stack: font font-res mscale first-char index charname

         1 index FirstChar lt { MissingWidth } {
           1 index LastChar gt { MissingWidth } { Widths 2 index FirstChar sub get } ifelse
         } ifelse
         oforce

                % Stack: font font-res mscale first-char index charname width
         4 index mul
                % The following 'loop' is only context for 'exit'.
         {
                % Work around a bug in pdfTeX, which can generate Encoding
                % vectors containing nulls :
           1 index //null eq { exit } if
           Metrics 2 index .knownget {
             1 index ne
           } {
             //false
           } ifelse {
             % Two or more Encoding elements refer same glyph name,
             % and Widths specify different wihts for it.
             % Since a Postscript font can't have different
             % Metrics for same glyph name,
             % we generate an unique name, and create a new
             % Charstrings entry with same glyph value.
             GlyphMap //null eq {
               /Encoding Encoding dup length array copy def
               /GlyphMap 4 dict def
             } if
             % To prevent too many new names, check whether
             % we can use one already created for same glyph.
             //true
             GlyphMap {                           % f r s c i n w b n1 n2
               4 index eq {                       % f r s c i n w b n1
                 dup Metrics exch get             % f r s c i n w b n1 w1
                 3 index eq {                     % f r s c i n w b n1
                   4 3 roll pop                   % f r s c i w b n1
                   3 1 roll pop                   % f r s c i n1 w
                   Encoding 3 index 3 index put
                   //false                        % f r s c i n1 w b
                   exit
                 } {
                   pop
                 } ifelse
               } {                                % f r s c i n w b n1
                 pop
               } ifelse
             } forall                             % f r s c i n w b
             { % Do create a new name.
               Metrics 2 index //unique_name exec % f r s c i n w nn
               Encoding 4 index 2 index put
               GlyphMap 1 index 5 -1 roll put     % f r s c i w nn
               exch
                  % Stack: font font-res mscale first-char index new_name width
             } if
           } if
           2 copy Metrics 3 1 roll put
           exit
         } loop
         pop pop
         1 add
       }
     forall pop pop pop
     exch Encoding Metrics GlyphMap end
  } {
    //null //null
  } ifelse
} bind executeonly def

currentdict /unique_name undef
currentdict /match_in_diff undef

/ToUnicodeCMapReader 4 dict begin
  /defineresource % <name> <dict> <cat-name> defineresource <dict>
  {
    pop
    dup userdict exch /.lastToUnicode exch put
    exch pop
  } bind executeonly def

  /CIDSystemInfo
  {
    (   **** Warning: ToUnicode CMap has invalid syntax near CIDSystemInfo.\n)  pdfformatwarning
    /CIDSystemInfo
  } bind executeonly def % A work around a bug in Altona.Page_3.2002-09-27.pdf - a slash is missed.

  /CMapName
  {
    (   **** Warning: ToUnicode CMap has no CMapName.\n\
        See the comment to revision 6201 in gs/doc/ps2pdf.htm#Problems .\n) pdfformatwarning
    /CMap1   % arbitrary, PDF defineresource tolerates non-unique names
  } bind executeonly def % A work around incorrect ToUnicode CMap generated by GS before rev. 6201.

  % Split large ranges into smaller chunks to stay withing the limits
  % of various PS% objects and speed up operand stack manipulation,
  %  esp. "counttomatk -3 roll". Bug 691908.
  /beginbfrange {
    { pop
      mark
       256 {
        currentfile token not { /endbfrange exit } if
        dup /endbfrange eq { exit } if
        currentfile token not { pop /endbfrange } if
        currentfile token not { pop pop /endbfrange } if
        dup ([) eq {
          pop [
          { currentfile token not { exit } if
            dup (]) eq { pop exit } if
          } loop ]
        } if
      } repeat
      dup /endbfrange eq {
        pop endbfrange exit
      } {
        endbfrange
        mark
      } ifelse
    } loop
  } bind executeonly def
currentdict end readonly def

/string2number     % <string> string2number <number>
{ 0 exch dup 0 exch 1 exch length 1 sub {     % n () i
    1 index exch get                          % n () v
    3 2 roll 256 mul add exch                 % v+n*256 ()
  } for
  pop                                         % N
} bind executeonly def

/copy&def    % <key> <value> <bool> copy&def -
{
  { //true
  } {
    currentdict gcheck {
      dup gcheck not
    } {
      //false
    } ifelse
  } ifelse
  { currentglobal currentdict gcheck setglobal
    exch dup length string copy exch
    setglobal
  } if
  def
} bind executeonly def

/.convert_ToUnicode-into-g2u % <GlyphNames2Unicode> <Encoding|null> <CMap> .convert_ToUnicode-into-g2u -
{
  CMAPDEBUG {
    (.convert_ToUnicode-into-g2u beg) =
  } if
  3 2 roll begin
  /.CodeMapData get % About the data format see gs_cmap.ps, the comment after "CMap operators".
  1 get % code maps
  {
    CMAPDEBUG {
      dup //== exec
    } if
    dup length 1 sub 0 exch 5 exch {           % e [] i
      2 copy get                               % e [] i (prefix)
      string2number                            % e [] i prefix
      2 index 2 index 1 add get                % e [] i prefix (key_size,?is_range,value_type,value_size)
      dup 0 get 8 mul                          % e [] i prefix (key_size,?is_range,value_type,value_size) key_size*8
      3 2 roll exch bitshift exch              % e [] i prefix<<key_size*8 (key_size,?is_range,value_type,value_size)
      dup 0 get exch 3 get                     % e [] i offset key_size value_size
      4 index 4 index 2 add get                % e [] i offset key_size value_size (keys)
      5 index 5 index 3 add get                % e [] i offset key_size value_size (keys) (values)
      CMAPDEBUG {
        ( offset=) print 4 index =string cvs print
        ( key_size=) print 3 index =string cvs print
        ( value_size=) print 2 index =
        ( keys=)   print 1 index //== exec
        ( values=) print dup //== exec
      } if
      1 index length 0 eq {
        % A single pair.
        exch pop exch pop exch pop exch        % e [] i (values) offset
        4 index //null ne {
          4 index length 1 index gt {
            4 index exch get
          } if
        } if                                   % e [] i (values) cid|name
        exch
        CMAPDEBUG {
          ( defined single: ) print 1 index =string cvs print ( ) print dup //== exec
        } if
        //false copy&def                       % e [] i
        pop                                    % e []
      } {
        % A range.                             % e [] i offset key_size value_size (keys) (values)
        dup length string copy % protect the original string from modifications below.
        0 4 index 2 mul 3 index length 1 sub { % e [] i offset key_size value_size (keys) (values) j
          2 index 1 index 6 index getinterval
          string2number                        % e [] i offset key_size value_size (keys) (values) j keyL
          CMAPDEBUG {
              ( keyL=) print dup =string cvs print
          } if
          3 index 2 index 7 index add 7 index getinterval
          string2number                        % e [] i offset key_size value_size (keys) (values) j keyL keyH
          CMAPDEBUG {
              ( keyH=) print dup =
          } if
          3 2 roll 6 index idiv 5 index mul    % e [] i offset key_size value_size (keys) (values) keyL keyH J
          3 index exch 6 index getinterval     % e [] i offset key_size value_size (keys) (values) keyL keyH (valueL)
          3 1 roll 1 exch {                    % e [] i offset key_size value_size (keys) (values) (value) k
            9 index //null ne {
              9 index exch get                 % e [] i offset key_size value_size (keys) (values) (value) name
            } if                               % e [] i offset key_size value_size (keys) (values) (value) cid|name
            1 index                            % e [] i offset key_size value_size (keys) (values) (value) cid|name (value)
            CMAPDEBUG {
              ( defined from range: ) print 1 index =string cvs print ( ) print dup //== exec
            } if
            //true copy&def                    % e [] i offset key_size value_size (keys) (values) (value)

            %% Calculate the value from the last string
            dup dup length 1 sub 0 exch 0 1 3 -1 roll
            {
              % (string) value index
              exch 256 mul exch
              2 index exch get add
            } for

            %% and increment the value for the next string
            1 add

            %% Now convert the value into a string of bytes.
            1 index length 1 sub 1 exch 0 1 3 -1 roll
            {
              % (string) value divisor index
              2 index 2 index idiv 255 and % (string) value divisor index byte
              exch 4 index length 1 sub exch sub % (string) value divisor byte string_position
              5 index                      % (string) value divisor byte string_position (string)
              exch 3 -1 roll put           % (string) value divisor
              256 mul
            } for
            pop pop pop
          } for                                % e [] i offset key_size value_size (keys) (values) (value)
        } for
        pop pop pop pop pop pop pop            % e []
      } ifelse
    } for
    pop                                        % e
  } forall
  end
  pop                                          %
  CMAPDEBUG {
    (.convert_ToUnicode-into-g2u end) =
  } if
} bind executeonly def

/.DoToUnicode?
{
  /IgnoreToUnicode where
  {/IgnoreToUnicode get not}
  {//true} ifelse
} bind executeonly def

/.processToUnicode   % <font-resource> <font-dict> <encoding|null> .processToUnicode -
{
  //.DoToUnicode? exec
  {
    currentdict count 1 sub /.stackdepth exch .forceput
    currentdict countdictstack /.dstackdepth exch .forceput
    {
      1 index /FontType get 0 eq
      {
        1 index /FDepVector get 0 get
        dup /FontType .knownget not
        {
          dup /CIDFontType .knownget
          { dup 2 eq {pop 11} if }
          {-1} % just some value that's not a valid font type
          ifelse
        }if
        11 eq
        { /Path known}
       {pop //false}
        ifelse
      }
      {//false} ifelse
      % Currently pdfwrite is only device which can handle GlyphNames2Unicoide to
      % generate a ToUnicode CMaps. So don't bother with other devices.
      /WantsToUnicode /GetDeviceParam .special_op {
        exch pop
      }{
        //false
      }ifelse
      or
      {
        PDFDEBUG {
          (.processToUnicode beg) =
        } if

        2 index /ToUnicode knownoget
        {
            dup type /nametype eq {
              % This is contrary to the specification but it seems that Acrobat at least will accept
              % a ToUnicode with a value of Identity-H *and* will use that for search, copy/paste.
              % We can't pass through a name, so the best we can do is build a GlyphNames2Unicode
              % map matching that which would have been generated by a full 16-bit Identity CMap
              %
              % See bug numbers 701003 and 687351
              %
              dup /Identity-H eq 1 index /Identity-V eq or{
                pop
                1 index /FontInfo .knownget not {
                  currentglobal 2 index dup gcheck setglobal
                  /FontInfo 5 dict dup 5 1 roll .forceput
                  setglobal
                } executeonly if
                dup /GlyphNames2Unicode .knownget not {
                  //true                        % No existing G2U, make one
                } {
                  dup wcheck {
                    //false                     % Existing, writeable G2U, don't make new one
                  } {
                    pop //true                          % Existing read only G2U, make new one
                  } ifelse
                } ifelse
                {
                  currentglobal exch dup gcheck setglobal
                  dup /GlyphNames2Unicode 100 dict dup 4 1 roll .forceput
                  3 2 roll setglobal
                } if                                 % font-res font-dict encoding|null font-info g2u

                0 1 65535{
                                                        % g2u index
                  dup dup 256 mod exch 256 idiv         % g2u index lo-byte hi-byte
                  2 string dup 0 4 -1 roll              % g2u index lo-byte () () 0 hi-byte
                  put                                   % g2u index lo-byte (x)
                  dup 1                                 % g2u index lo-byte (x) (x) 1
                  4 -1 roll put                         % g2u index (x) (x) 1 lo-byte -> dict index (xx)
                  2 index                               % g2u index (xx) dict
                  3 1 roll                              % g2u g2u index (xx)
                  put                                   % g2u
                } for
                pop                                     % font-res font-dict encoding|null font-info
                pop                                     % font-res font-dict encoding|null
                //false                                 % We built a GlyphNames2Unicode table, don't need to process further
              } executeonly
              {
                //true                                  % name is not Identity-V or H, fail by falling through
              }ifelse
          } executeonly
          {
            //true
          } ifelse                                      % not a name, try as a dictionary (as specified)

          % If the ToUnicode isn't a name, or the name isn't Identity-V or -H then follow the specification
          % If its not a dictionary type throw an error, otherwise decode it and build a GlyphNames2Unicode
          %
          {
            dup type /dicttype eq { dup /File known not } { //true } ifelse {
              % We undefine wrong /Length and define /File in stream dictionaries.
              % Bug687351.pdf defines /ToUnicode /Identity-H, what is incorrect.
              (   **** Warning: Ignoring bad ToUnicode CMap.\n)  pdfformatwarning
              pop
            } {
              /PDFScanRules .getuserparam dup //null eq {
                pop //PDFScanRules_null
              } {
                1 dict dup /PDFScanRules 4 -1 roll put
              } ifelse
              //PDFScanRules_true setuserparams
              PDFfile fileposition
              3 -1 roll
              count 1 sub
              countdictstack
              { //false resolvestream
                % Following Acrobat we ignore everything outside
                %   begincodespacerange .. endcmap.
                dup 0 (begincodespacerange) /SubFileDecode filter flushfile
                /CIDInit /ProcSet findresource begin
                //ToUnicodeCMapReader begin
                12 dict begin
                /CMapType 2 def
                mark exch % emulate 'begincodespacerange'
                0 (endcmap) /SubFileDecode filter cvx /begincmap cvx exch 2 .execn
                endcmap
                userdict /.lastToUnicode currentdict put
                end end end
              }

              PDFSTOPONERROR {
                { exec } 0 get
                //false
                5 -2 roll
                5
              } {
                { stopped } 0 get
                4 2 roll
                4
              } ifelse
              array astore cvx exec

              countdictstack exch sub 0 .max { end } repeat
              count exch sub 2 sub 0 .max { exch pop } repeat
              3 1 roll                     % Stach the stop flag.
              PDFfile exch setfileposition
              setuserparams
              {
                (   **** Warning: Failed to read ToUnicode CMap.\n)  pdfformatwarning
              } {
                1 index /FontInfo .knownget not {
                  currentglobal 2 index dup gcheck setglobal
                  /FontInfo 5 dict dup 5 1 roll .forceput
                  setglobal
                } if
                dup /GlyphNames2Unicode .knownget not {
                  //true                        % No existing G2U, make one
                } {
                  dup wcheck {
                    //false                     % Existing, writeable G2U, don't make new one
                  } {
                    pop //true                          % Existing read only G2U, make new one
                  } ifelse
                } ifelse
                {
                  currentglobal exch dup gcheck setglobal
                  dup /GlyphNames2Unicode 100 dict dup 4 1 roll .forceput
                  3 2 roll setglobal
                } if                                 % font-res font-dict encoding|null font-info g2u
                exch pop exch                        % font-res font-dict g2u encoding|null
                userdict /.lastToUnicode get         % font-res font-dict g2u Encoding|null CMap
                .convert_ToUnicode-into-g2u          % font-res font-dict
                //null                               % font-res font-dict //null
              } ifelse
            } ifelse
          } if
          PDFDEBUG {
            (.processToUnicode end) =
          } if
        } executeonly if
      } executeonly if
    } executeonly stopped
    {
      .dstackdepth 1 countdictstack 1 sub
      {pop end} for
      .stackdepth 1 count 3 sub
      {pop pop} for
    } if
    pop pop pop
    currentdict /.stackdepth .forceundef
    currentdict /.dstackdepth .forceundef
  } executeonly
  {pop pop pop}
  ifelse

} bind executeonly odef

currentdict /.DoToUnicode? .forceundef

% ---------------- Descriptors ---------------- %

% Partial descriptors for the 14 built-in fonts.  Note that
% from PDF 1.1 to PDF 1.2, the meaning of the Flag 6 in the FontDescriptor
% object has undergone a subtle change in its meaning which has serious
% consequences for searching with Acrobat:
% In PDF 1.1, the flag meant: Font has StandardEncoding
% In PDF 1.2, the flag means: Font has (subset of) StandardRomanCharacterSet
/standardfontdescriptors mark
  /Courier mark /Flags 16#23 .dicttomark
  /Courier-Oblique 1 index
  /Courier-Bold 1 index
  /Courier-BoldOblique 1 index
  /Helvetica mark /Flags 16#20 .dicttomark
  /Helvetica-Oblique 1 index
  /Helvetica-Bold 1 index
  /Helvetica-BoldOblique 1 index
  /Times-Roman mark /Flags 16#22 .dicttomark
  /Times-Bold 1 index
  /Times-Italic mark /Flags 16#62 .dicttomark
  /Times-BoldItalic 1 index
  /Symbol mark /Flags 16#4 .dicttomark
  /ZapfDingbats 1 index
.dicttomark readonly def

% ---------------- Utilities ---------------- %

/.pdforigfontcache_g 20 dict def
currentglobal //false setglobal
systemdict /.pdforigfontcache_l 20 dict .forceput
setglobal

% Find an original font, using cache to prevent adjustfont to accumulate changes.
/pdffindcachedfont {   % <font_name> pdffindcachedfont <font>
  dup //.pdforigfontcache_g exch .knownget {
    exch pop
  } {
    dup .pdforigfontcache_l exch .knownget {
      exch pop
    } {
      dup findfont dup
      dup gcheck { //.pdforigfontcache_g } { .pdforigfontcache_l } ifelse
                     % Stack : font_name font font cache
      4 2 roll .growput
    } ifelse
  } ifelse
} bind executeonly def

% Add original font to cache to prevent adjustfont to accumulate changes.
/pdfaddcachedfont {   % <font_name> pdfaddcachedfont <font>
  dup findfont dup                   % name font font
  dup gcheck { //.pdforigfontcache_g } {.pdforigfontcache_l} ifelse
  4 2 roll                           % font d name font
  put                                % font
} bind executeonly def

/.remove_font_name_prefix {  % <name>  .remove_font_name_prefix <name>
  dup .namestring (+) search {
    //true exch
    { dup 65 lt exch 90 gt or {
        pop //false exit
      } if
    } forall
    { pop exch pop cvn
    } {
      pop pop
    } ifelse
  } {
    pop
  } ifelse
} bind executeonly def

% Find a font (except for embedded ones), and adjust its encoding if necessary.
/.pdfdfndict mark
  /defaultfontname /Helvetica
.dicttomark readonly def

% This code attempts to use the FontName and FontDescriptor to find a
% 'better' match for a missing font than the default font. There
% are a list of fonts and mappings here (TTfonts) and in gs_font.ps
% (.substitutefaces) which we can use to map from a requested font
% family name to a substitute family. If that fails we check the
% FontDescriptor Flags to attempt a (very slightly) better match.
% We use Times-Roman for serif fonts and Helvetica for non-serif.
% This all seems pretty poor to me, but its long-standing, so we
% won't attempt to meddle with it.
/pdfsubstitutefont {
  % Stack: font-res fontname fontdesc

  dup /Flags oget
  dup 16#40 and -6 bitshift		% 1, oblique/italic
  1 index 16#40000 and -17 bitshift add	% 2, bold
  exch 16#2 and 2 bitshift add	% 8, serif

  % We should look at the fixed flag, too.
  % Stack: font-res fontname fontdesc properties

  % Even though /FontName is a required key in FontDescriptor dict
  % (As of the PDF 1.4 Reference Manual), In the case of missing
  % /FontName key, we substitue /BaseFont for the value of /FontName.
  % Yet another case of broken PDF's that Adobe Reader accepts.
  1 index dup /FontName known
  {
    /FontName oget
    dup type /nametype ne
    {
      (   **** Error: /FontName in FontDescriptor is not a name.\n)
      pdfformaterror
      (               Output may be incorrect.\n) pdfformaterror
      cvn
    } if
  }
  {
    (   **** Error: FontDescriptor missing required /FontName key. BaseFont name used.\n)
    pdfformaterror
    (               Output may be incorrect.\n) pdfformaterror
    pop 2 index		% grab the BaseFont from the stack.
  } ifelse
  .remove_font_name_prefix
  exch

  % Analyzes font name and extract "Bold" and "Narrow" properties
  % which are not described by the FontDescriptor Flags.
  % We also allow the font name analysis to override the Flags setting
  % for Italic/Oblique as this gives us results more consistent with
  % Acrobat.
  0 2 index //.fontnameproperties exec 7 and or

  % Rebind the default font name to Helvetica so that
  % fonts with no properties are handled correctly.
  //.pdfdfndict begin .substitutefontname end

  % Stack: font-res fontname fontdesc substname|null
  Fontmap 1 index known
  {//false}
  { .buildnativefontmap pop NativeFontmap 1 index known not}
  ifelse

  {
    % No available good substitution, use the standard one.
    pop 1 index .substitutefont
  } if

  dup 3 index ne
  {
    QUIET not
    {
      (Substituting font ) print dup =only
      ( for ) print 2 index =only (.) = flush
    } if
    % Send a warning to the device, in case its pdfwrite and we are in PDF/A mode
    % In that case the substituted font may not (probably doesn't) have glyphs which
    % match the /Widths of the original font, and that will cause a fault with PDF/A
    % so we need to let the device know.
    /SubstitutedFont /EventInfo .special_op
  } if

  pdffindcachedfont
  % Stack: font-res fontname fontdesc font

  % Some non-compliant files are missing FirstChar/LastChar,
  % despite referencing a non-base14 font
  3 index /FirstChar knownoget
  {
    0                                       % push an initial value for accumating the Widths
    0 string                                % and an empty string to interrogate the font
    7 index /Widths knownoget
    {
      0 1 2 index length 1 sub
      {
        dup 2 index exch get
        dup type /packedarraytype eq {exec} if % Handle entries which are indirect references (seriously ? !!)
        dup 0 gt                              % We ignore entries of zero in the widths array
        {
          exch 5 index add                    % add FirstChar to the idx to get a char code
          dup 64 gt 1 index 91 lt and         % we only want to consider A~Z and....
          1 index 96 gt 2 index 123 lt and or % ... a~z
          {
            exch
            5 -1 roll add 4 1 roll            % add the width to the accumulator on the stack
            1 string dup 0 4 -1 roll put
            3 -1 roll concatstrings exch      % add the char code to the string
          }
          {
            pop pop
          } ifelse
        }
        {
           pop pop
        } ifelse
      } for
      pop           % get rid of the Widths array
      3 -1 roll pop % get rid of the FirstChar value
      dup length dup 0 gt
      {
        3 -1 roll exch div  % calculate an average width from the Widths array
        gsave
        2 index 10 scalefont setfont
        exch dup length exch stringwidth
        grestore
        pop 100 mul exch div div
        % Only make the font smaller/narrower, not larger/wider
        dup 1.0 lt
        {
          0 0 2 index 0 0 6 array astore
          exch //true .copyfontdict
          dup /FontMatrix get 3 -1 roll matrix concatmatrix
          exch dup 3 -1 roll /FontMatrix exch put
          % we don't need to definfont here, we can leave that to .completefont below
        }
        {
          pop
        } ifelse
      }
      {
        % no suitable Widths entries - carry on and hope for the best......
        pop pop pop
      } ifelse
    }
    {
      % Broken file: no Widths array for non-base14 font - carry on and hope for the best......
      (   **** Warning: Widths array missing in FontDescriptor for non-base14 font\n)
      pdfformatwarning
      pop pop pop
    } ifelse
  }
  {
    (   **** Warning: FirstChar value missing in FontDescriptor for non-base14 font\n)
    pdfformatwarning
  } ifelse
  % Stack: font-res fontname fontdesc font

  % If this is a small-caps font, replace the CharString
  % entries for a..z.
  exch /Flags oget 16#20000 and 0 ne
  {
    //true .copyfontdict
    dup /CharStrings 2 copy get dup length dict .copydict
    % stack: font-res fontname font font /CharStrings CharStringsdict
    5 index /FirstChar get 97 .max
    6 index /LastChar get 122 .min 1 exch
    {

      % Stack: font-res fontname font' font' /CharStrings charstrings code
      % Note that this only remaps a-z, not accented characters.
      6 index /Widths oget 1 index 8 index /FirstChar get sub oget
      1 string dup 0 5 -1 roll put

      % Stack: font-res font' font' /CharStrings charstrings code
      %   width (x)
      2 index exch dup cvn exch
      dup 0 2 copy get 32 sub put 4 -1 roll
      {
        % Stack: operand (X) width
        0 setcharwidth exch pop
        currentfont /FontMatrix get matrix invertmatrix concat
        0.7 dup scale 0 0 moveto show
      } /exec cvx 4 packedarray cvx put
    } for put
  } if

  dup /FontName get 2 index ne
  {
    //true .copyfontdict
    2 copy exch /FontName exch put
  } if
  exch pop .completefont
} bind executeonly def

/pdffindfont {		% <font-resource> <fontname> pdffindfont <font>
  % If the font isn't available, synthesize one based on
  % its descriptor.
  dup /Font resourcestatus
  {
    pop pop pdffindcachedfont
  }
  {
    1 index /FontDescriptor knownoget {
      /SUBSTFONT where not
      {
        pdfsubstitutefont
      }
      {
        % User has defined SUBSTFONT, use the defined substitute font, do not
        % try to be clever
        pop                 % the dictionary containing SUBSTFONT
        pop                 % the FontDescriptor
        pdffindcachedfont
      }ifelse
    }
    {
      % No descriptor available, use the default algorithm.
      pdffindcachedfont
    } ifelse
  } ifelse
  exch pop
} bind executeonly def

% ---------------- Type 1 fonts ---------------- %

/buildType1		% <Type1-font-resource> buildType1 <font>
 { dup /BaseFont get pdffindfont
 } bind executeonly def

% Read an embedded Type 1 font.
/readfontfilter {	% <proc> readfontfilter <filter>
  0 () /SubFileDecode filter
} bind executeonly def

% Adobe Acrobat doesn't skip space characters after eexec
/eexec_pdf_param_dict mark
  .eexec_param_dict {} forall
  /keep_spaces //true
.dicttomark readonly def

% When Type 1 font reading procedure is executing, a copy of this dictionary is current.
% topFontDict allows us to *attempt* to handle the case where a Type 1 font definition
% does not start by creating it's own dictionary (i.e. a number of the keys end up in
% the currentdict rather than the font dictionary).
% The whole type1 stream can be executed directly. There's no need to process
% Length1, 2, 3 keys.

/readtype1dict 10 dict dup begin
  /prev_definefont /definefont load def

  /definefont {
    % if a font defines any keys outside it's own dictionary,
    % this attempts to cope by copying them into the font dictionary
    [
      /FontType    % in PLRM order
      /FontMatrix
      /FontName
      /FontInfo
      /WMode
      /Encoding
      /FontBBox
      /UniqueID
      /XUID
      /PaintType
      /StrokeWidth
      /Metrics
      /Metrics2
      /CDevProc
      /CharStrings
      /Private
      /WeightVector
    ]
    {
      2 copy known
      {pop}
      {
        dup where
        {
          1 index get
          2 index
          3 1 roll put
          dup /MisplacedKey 0 put
        }
        {pop} ifelse
      } ifelse
    } forall
    dup /MisplacedKey known {
      (   **** Warning: Type 1 font defines some required keys outside the font dictionary.\n)
      pdfformatwarning
    } if

    dup /CharStrings get /.notdef known not {
      dup /CharStrings get
        /.notdef <9E35CED7FFD3622F09> put
        (   **** Warning: Type 1 font has no /.notdef entry in /CharString dictionary.\n)
        pdfformatwarning
    } if
    dup /UniqueID .knownget {
      dup dup 0 lt exch 16#ffffff gt or {
        (   **** Warning: Ignoring invalid /UniqueID = ) exch =string cvs
        concatstrings (\n) concatstrings pdfformatwarning
        dup /UniqueID undef
      } {
        pop
      } ifelse
    } if
    prev_definefont
  } bind executeonly def

  /undef_proc_warning {
    /Repaired //true store     % flag that we have warnings
    UndefProcList exch 2 copy .knownget { 1 add } { 1 } ifelse put
  } bind executeonly def

  /missing-type1-procs 6 dict begin
    /-| { string currentfile exch readstring pop /-| //undef_proc_warning exec } executeonly bdef
    /RD { string currentfile exch readstring pop /RD //undef_proc_warning exec } executeonly bdef
    /|- { noaccess def /|- //undef_proc_warning exec } executeonly bdef
    /ND { noaccess def /ND //undef_proc_warning exec } executeonly bdef
    /|  { noaccess put /|  //undef_proc_warning exec } executeonly bdef
    /NP { noaccess put /NP //undef_proc_warning exec } executeonly bdef
  currentdict end readonly def

  /eexec {
        % Assume the font dictionary is directly below the file on the stack
    count 0 gt { /topFontDict 2 index cvlit store } if
    //eexec_pdf_param_dict /eexecDecode filter

    //missing-type1-procs begin
    /userdict .systemvar begin
    //systemdict begin
    readtype1dictcopy begin cvx stopped

    currentfile flushfile % Skip the trailer after return from eexec, bug 690701.
    { currentdict end //missing-type1-procs eq { exit } if } loop
    { stop } if
  } bind executeonly def

  /readonly-op-dict <<
    /stringtype 0
    /arraytype 0
    /packedarraytype 0
    /dicttype 0
  >> readonly def

  /readonly { % bug 689617
    dup type //readonly-op-dict exch known not {
      (   **** Warning: Type 1 font applies operator readonly to an invalid object type.\n)
      pdfformatwarning
    } if
  } bind executeonly def

  /prev_get /get load def

  /get {
    dup /FontName eq {
      % No warning, probably FontName is defined elsewhere; see definefont above.
      .knownget not { /Missing } if
    } {
      dup /UniqueID eq {
        % We don't want fonts to check /UniqueID and do 'restore'.
        pop pop 16#FEDCBA98  % Arbitrary and invalid value
      } {
        prev_get
      } ifelse
    } ifelse
  } bind executeonly def

  /prev_begin /begin load def

  /begin {
    dup //systemdict eq { pop 0 dict } if
    prev_begin
  } bind executeonly def

  { /undef_proc_warning /missing-type1-procs /readonly-op-dict }
  { currentdict exch undef } forall

  % Bug703454.pdf contains a number of fonts with multiple definitions
  % of a given glyph name in the CharStrings dict - i.e. two entries
  % for /a - the first of which is "correct" the second is "wrong".
  % Normal Postscript behaviour replaces the first value with the second
  % for the given key/value pair.
  % To handle this, *only* for Type 1 fonts embedded in PDFs, we
  % have a special definition of "def" which won't overwrite existing
  % values in the CharStrings dict.
  /def
  {
    3 index /CharStrings eq
    currentdict 3 index known and
    { pop pop }
    { systemdict /def get exec }
    ifelse
  } bind executeonly def

end readonly def

currentdict /eexec_pdf_param_dict .undef

/readtype1 {		% <font-resource> <stream-dict> readtype1 <font|null>
   PDFfile fileposition 3 1 roll      % pos res stream
   dup /PFB known exch                % pos res pfb? stream
   //true resolvestream               % pos res pfb? file
   exch {
     //false /PFBDecode filter        % pos res file'
   } if
                % Some buggy embedded fonts leave extra junk on the stack,
                % so we have to make a closure that records the stack depth
                % in a fail-safe way. This code also removes the mark when
                % the implied cleartomark is not executed, i.e. Length3 == 0.
                % Also restore dictstack depth.
                %
   //systemdict begin
   //readtype1dict dup length 5 add dict copy begin % pos res file'
   /file-position 4 -1 roll def       % res file'
   /stack-count count 3 sub def
   1 index /BaseFont oget /savedFontName exch def
   /topFontDict //null def
   /readtype1dictcopy currentdict def
   cvx stopped
   { currentdict /topFontDict known not { end } { exit } ifelse } loop
   { /topFontDict //null def } if
   PDFfile file-position setfileposition
   count stack-count sub { pop } repeat
   topFontDict end end
 } bind executeonly def

% ---------------- Type 3 fonts ---------------- %

/buildType3 {		% <Type3-font-resource> buildType3 <font>
  8 dict begin
    /FontType 3 def
                % If the font does not contain a Resources entry, then we use
                % the resources from our current context.  Page 391 of the PDF
                % 1.6 spec says that the Resources dict is optional and if not
                % present then we should use the Resources for the page.
                % However we have a test file (687989) which uses a Type3 font
                % inside a form XObject and the desired Resources are in the
                % XObject dict and not in the Page dict.  So we are going to
                % the parent object to find resources instead of only going to
                % the page dict when a font does not specify its required
                % resources.
    /Resources 1 index /Resources knownoget {
       oforce
    } {
      0 dict
    } ifelse def
    /FontBBox 1 index /FontBBox get cvx def
    /FontMatrix 1 index /FontMatrix oget def
    /CharProcs 1 index /CharProcs oget def
    1 index /Widths knownoget {
      /Widths exch def
      1 index /FirstChar known 2 index /LastChar known and {
        /FirstChar 1 index /FirstChar oget def
        /LastChar 1 index /LastChar oget def
      } {
       (   **** Error: Requireed entry FirstChar or LastChar is missing from Font.\n)
       pdfformaterror
       (               Output may be incorrect.\n) pdfformaterror
        /FirstChar 0 def
        /LastChar Widths length def
      }ifelse
    } if
    /FontName 1 index /Name knownoget not { /PDFType3Untitled } if def
    /Encoding .notdefEncoding 2 index getencoding def
                % We have to define BuildChar rather than BuildGlyph:
                % there is no PDF equivalent of glyphshow, and we need
                % the character code to access the Widths.
    /BuildChar {
                % Stack: font charcode
      1 index begin 4 dict begin
      /Font 3 -1 roll def /CharCode 1 index def
      Encoding exch get CharProcs exch knownoget {
        dup /Length get 0 eq {
          pop
          %% Detect a 0 length stream. If this occurs
          %% then we won't execute d0 or d1, and so won't adjust the
          %% character width. So, pretend we got a
          %% null d1, just so that we can adjust using the /Widths
          %% (Bug 697805)
          0 0 0 0 0 0
          pdfopdict /d1 get exec
        }{
          {
            dup //false resolvestream
                % Stack: filepos streamdict stream
                % Don't let setgcolor set the color inside the BuildGlyph
                % procedure, because this causes an /undefined error.
            q
            %% This is (currently) needed to make sure that the current dictionary, which is
            %% 'nodict' because we did a gsave, is writeable. If we don't execute gput
            %% to make a writeable copy then it is a no access dicitonary and the 'def'
            %% below will fail. It would be nice to fix the cases of this someday, but
            %% we use 'nodict'[ to store other PDF state stuff that isn't gstate, and we
            %% probably always will, so it may well be more trouble than its worth.
            /Dummy //false gput
            Font /Resources get  % Stack: filepos streamdict stream resdict
            dup length 0 eq {
              3 -1 roll /Resources knownoget {
                exch pop
                (   **** Warning: Type 3 font has resource dictionary in glyph stream.\n)
                pdfformatwarning
              } if
            } {
              3 -1 roll pop
            } ifelse
            exch pdfopdict
            /BuildCharDictDepth countdictstack def
            .pdfruncontext
            countdictstack BuildCharDictDepth sub
            {
              //pdfdict /.Qqwarning_issued .knownget {not}{//true} ifelse
              {
                (\n   **** Warning: Type 3 glyph has unbalanced q/Q operators \(too many q's\)\n               Output may be incorrect.\n)
                pdfformatwarning
                //pdfdict /.Qqwarning_issued //true .forceput
              } executeonly if
              Q
            } executeonly repeat
            Q
          } executeonly PDFfile fileposition 2 .execn % Keep pdfcount valid.
          PDFfile exch setfileposition
        } executeonly ifelse
      } executeonly
      {
        % PDF Type 3 fonts don't use .notdef
        % d1 implementation adjusts the width as needed
        0 0 0 0 0 0
        pdfopdict /d1 get exec
      } ifelse
      end end
    } executeonly bdef
    dup currentdict Encoding .processToUnicode
    currentdict end .completefont exch pop
} bind executeonly odef
/.adjustcharwidth {	% <wx> <wy> .adjustcharwidth <wx'> <wy'>
  % Enforce the metrics, in glyph space, to the values found in the PDF Font object
  % - force wy == 0 (assumed, and not stored in the PDF font)
  %   Even though PDF1.3-1.7 specifications state that this must be 0,
  %   Distiller sometimes creates Type3 fonts with non-zero wy. We set
  %   it to 0 since this is apparently what Acrobat Reader 4 and 5 do.
  %   PDF1.2 does not mention this restriction, it only says
  %   "see setcharwidth/ setcachedevice in the PostScript Reference".
  % - get wx from the Widths array (do nothing if not present)
  pop 0
  /Widths where {
    begin
    CharCode FirstChar ge CharCode LastChar le and {
      exch pop Widths CharCode FirstChar sub get exch
    } if end
  } if
} bind executeonly def

% ---------------- TrueType fonts ---------------- %

/TTfonts mark
  /Arial /Helvetica
  /Arial,Italic /Helvetica-Oblique
  /Arial,Bold /Helvetica-Bold
  /Arial,BoldItalic /Helvetica-BoldOblique
  /CourierNew /Courier
  /CourierNew,Bold /Courier-Bold
  /TimesNewRoman /Times-Roman
  /TimesNewRoman,Italic /Times-Italic
  /TimesNewRoman,Bold /Times-Bold
  /TimesNewRoman,BoldItalic /Times-BoldItalic
.dicttomark readonly def

/buildTrueType {	% <TrueType-font-resource> buildTrueType <font>
  dup /BaseFont oget
  dup /font_name gput

  1 index /FontDescriptor knownoget
  {
    /Flags knownoget
    {
      4 and 0 ne
      { //true } %  symbolic
      { 1 index /Encoding oknown not}  % no encoding => symbolic
      ifelse
    }
    {//false}
    ifelse
  }
  {//false}
  ifelse

  /is_symbolic gput
  1 index /Encoding oknown {
    StandardEncoding
    3 index getencoding
  } {
    //null
  } ifelse
  /prebuilt_encoding gput
  //false /.render_notdef gput % set the default

  dup /Font resourcestatus dup { exch pop exch pop } if not
  TTfonts 2 index known and {
    dup TTfonts exch get
    QUIET not {
      (Substituting font ) print dup =only
      ( for ) print 1 index =only (.) = flush
    } if
    exch 3 1 roll pdffindfont
    //true .copyfontdict
    2 copy exch /FontName exch put
    exch pop .completefont
  } {
    pdffindfont
  } ifelse
} bind executeonly def

% Read an embedded TrueType font.
/readtruetype {		% <font-resource> <stream-dict> readtruetype <font>
  1 index exch
  PDFfile fileposition 3 1 roll
  //true resolvestream readfontfilter
                % Stack: filepos fontres stream
  1 index /CIDSystemInfo oknown {
    1 index /CIDSystemInfo get dup type /packedarraytype eq {exec}if
    dup /Registry known not {
      (   **** Warning: a CIDFont's CIDSystemInfo is missing the required Registry entry\n) pdfformatwarning
      dup /Registry (Adobe) put
    } if
    dup /Ordering known not {
      (   **** Warning: a CIDFont's CIDSystemInfo is missing the required Ordering entry\n) pdfformatwarning
      dup /Ordering (Identity) put
    } if
    dup /Supplement known not {
      (   **** Warning: a CIDFont's CIDSystemInfo is missing the required Supplement entry\n) pdfformatwarning
      dup /Supplement 0 put
    } if
    3 1 roll
    1 index /Subtype knownoget {
      /CIDFontType2 ne {
        (   **** Error: Subtype of a TT CID font is not /CIDFontType2\n) pdfformaterror
        1 index /Subtype /CIDFontType2 put
        (               Output may be incorrect.\n) pdfformaterror
      } if
    } {
      (   **** Error: Subtype of a TT CID font is missing.\n) pdfformaterror
      1 index /Subtype /CIDFontType2 put
      (               Output may be incorrect.\n) pdfformaterror
    } ifelse

    1 index /BaseFont get                 % Use the BaseFont name for the font. Otherwise we
                                          % would use the name table, or a manufactured name.
    .loadttcidfont
                                          % Stack: filepos fontres cidfont
  } {
                                          % filepos fontres stream
    1 index /FontDescriptor oget
    /Flags oget dup                       % only believe the symbolic flag if the non-symbolic flag is not also set!
    4 and 0 ne
    exch 32 and 0 eq and {
       //true  %  symbolic
    } {
       1 index /Encoding oknown not  % no encoding => symbolic
    } ifelse

    2 index /Encoding oknown {
      StandardEncoding
      3 index getencoding
    } {
      //null
    } ifelse

    dup 4 index exch                      % filepos fontres stream is_symbolic Encoding fontres Encoding
    /prebuilt_encoding exch put           % filepos fontres stream is_symbolic Encoding
    5 index /BaseFont get                 % Use the BaseFont name for the font. Otherwise we
                                          % would use the name table, or a manufactured name.
    .loadpdfttfont
  } ifelse
  exch pop
  PDFfile 3 -1 roll setfileposition
                % Ignore both the Encoding and the Widths.
  exch pop
} bind executeonly def

% ---------------- Type 0 fonts ---------------- %

% Predefine the known CMaps, but only create them on demand.
/knownCMaps mark
  /Identity-H { /Identity-H 0 makeIdentityCMap }
  /Identity-V { /Identity-V 1 makeIdentityCMap }
.dicttomark def

/makeIdentityCMap {		% <cmapname> <wmode> .makeIdentityCMap -
  .currentglobal //true .setglobal 3 1 roll
  /CIDInit /ProcSet findresource begin
  12 dict begin
    begincmap
    /WMode exch def
    /CMapName exch def
    /CIDSystemInfo 3 dict dup begin
      /Registry (Adobe) def
      /Ordering (Identity) def
      /Supplement 0 def
    end def
    %/CMapName (see above)
    /CMapVersion 1 def
    /CMapType 1 def
    %WMode (see above)
        % The PDF documentation says that these CMaps map CIDs
        % "1 to 65,536".  This is a misprint for 0 to 65,535.
    1 begincodespacerange
        % <0001> <00ff>  <0100> <ffff>
      <0000> <ffff>
    endcodespacerange
    1 begincidrange
        % <0001> <00ff> 1   <0100> <ffff> 256
      <0000> <ffff> 0
    endcidrange
    endcmap
    CMapName currentdict /CMap defineresource
    knownCMaps CMapName 2 index put
  end		% CMap
  end		% CIDInit ProcSet
  exch .setglobal
} bind executeonly def

/CMap_read_dict 3 dict begin
  /defineresource % <name> <dict> <cat-name> defineresource <dict>
  {
    pop
    /.last_CMap_def 1 index store
    exch pop
  } bind executeonly def
  /CIDSystemInfo
  {
    (   **** Error: CMap has invalid syntax near CIDSystemInfo.\n)  pdfformaterror
    (               Output may be incorrect.\n) pdfformaterror
    /CIDSystemInfo
  } bind executeonly def % A work around a bug in Altona.Page_3.2002-09-27.pdf - a slash is missed.
  /CMapName
  {
    (   **** Error: CMap has no CMapName.\n\
        See the comment to revision 6201 in gs/doc/ps2pdf.htm#Problems .\n) pdfformaterror
    (               Output may be incorrect.\n) pdfformaterror
    /CMap1   % arbitrary, PDF defineresource tolerates non-unique names
  } bind executeonly def % A work around incorrect ToUnicode CMap generated by GS before rev. 6201.
currentdict end readonly def

% Read an embedded stream that we *hope* is a PostScript CMap. According
% to the spec, ths isn't legal, but 'Acrobat opens it'....
%
/read_CMap {
  dup mark exch 0 (begincodespacerange) /SubFileDecode filter cvx exec
  cleartomark
  {currentdict dup /CMapType known not {end}{pop exit} ifelse} loop
  /CMapType 1 def
  mark exch % emulate 'begincodespacerange'
  0 (endcmap) /SubFileDecode filter cvx /begincmap cvx exch 2 .execn
  endcmap
  currentdict end
  dup /CMapName get exch
  /CMap defineresource end
} bind executeonly def

% Read embedded CMap stream.
% if the <wmode> parameter is null, it indicates there is no WMode, and we
% should not add one.
% Following Acrobat we ignore everything outside
% begincodespacerange .. endcmap.
/read_CMap_stream {  % <info> <wmode> <name> <stream> read_CMap <CMap>
  //CMap_read_dict begin
  /CIDInit /ProcSet findresource begin
  12 dict begin
  /o.endmapvalue /.endmapvalue load def
  /.endmapvalue
  {
    dup 65535 gt
    {
      (   **** Warning: CMap: CID out of valid range (0-65535).\n)  pdfformatwarning
      pop 65535
    } if
    o.endmapvalue
  } bind def
  4 2 roll
  dup //null eq { pop } { /WMode exch def } ifelse
  dup //null eq { pop } { /CIDSystemInfo exch def } ifelse
  /CMapType 1 def
  /.last_CMap_def currentdict def % establish binding

  % The stream may not be seekable, push a ReusableStream to make it seekable.
  /ReusableStreamDecode filter

  % Try to skip past the 'header' portion of the PDF file to the body
  % If the CMap doesn't have a begincodespacerange (eg it uses /UseCMap)
  % then this will leave us at EOF.
  dup 0 (begincodespacerange) /SubFileDecode filter flushfile
  % See if we have anything left in the file
  dup bytesavailable 0 eq
  {
    % We discarded all the file contents, so there was no begincodespacerange
    % Rewind the file to the beginning
    dup 0 setfileposition
    % And try again, this time only discarding to the 'begincmap'
    dup 0 (begincmap) /SubFileDecode filter flushfile
  }
  {
    % Everything worked as expected, because we have consumed the
    % begincodespacerange from the file, emulate it
    mark exch
  } ifelse

  0 (endcmap) /SubFileDecode filter cvx /begincmap cvx exch 2 .execn
  currentdict /o.endmapvalue undef
  endcmap
  /.last_CMap_def load
  { currentdict end //CMap_read_dict eq { exit } if } loop
  dup /.last_CMap_def undef
  /CMap defineresource
} bind executeonly def

currentdict /CMap_read_dict undef

/buildType0 {		% <Type0-font-resource> buildType0 <font>
  dup /BaseFont get	% FontName
  1 index /Encoding oget
  dup type /nametype eq {
    dup /CMap resourcestatus {
        pop pop /CMap findresource
    } {
        knownCMaps 1 index .knownget
          { exch pop exec } { /undefined signalerror } ifelse
    } ifelse
  } {
    %% Bug #696449 Acrobat will read an embedded PostScript
    %% CMap, whcih according to the spec is not valid, it needs
    %% to be a PDF CMap stream. Here we check and see if it looks like a CMap
    %% dictionary. If it is we proceed, if its some other kind of dictionary,
    %% throw an error, if its neither, treat it as a stream and hope for the best.
    dup /Type known {
      dup /Type get /CMap eq {
      PDFfile fileposition exch
      dup /CIDSystemInfo knownoget {
        << exch { oforce } forall >>
      } {
        //null
      } ifelse exch
      dup /WMode knownoget not {//null} if exch
      dup /CMapName knownoget not {/null} if exch
      //true resolvestream
      read_CMap_stream
      exch PDFfile exch setfileposition
      } {
        PDFSTOPONERROR {
          /buildType0 cvx /syntaxerror signalerror
        } if
      } ifelse
    }{
      PDFfile fileposition exch
      //true resolvestream
      read_CMap
      exch PDFfile exch setfileposition
    } ifelse
  } ifelse		% CMap
  [
    3 index /DescendantFonts oget { exec resourcefont } forall
  ]			% subfonts
  .composefontdict  % composefont must insert FontInfo dictionary - see gs_cmap.ps .
                    % Stack: fontres name font
  3 copy exch pop //null .processToUnicode
  exch pop .completefont % Stack: fontres font

  1 index /FontMatrix knownoget {
    dup aload pop //true {0 0 1 0 0 1} {3 -1 roll eq and} forall {
      1 index exch makefont exch /FontName get exch
      exch pop .completefont
    } {
      pop
    } ifelse
  } if exch pop
} bind executeonly def

% ---------------- CIDFontType0/2 fonts ---------------- %

% Insert metrics into a CIDFont, by saving the PDF W, W2, DW, and DW2
% arrays and using a (currently very inefficient) CDevProc.
% For detail, refer "PDF Reference" 2nd ed., p314 "5.6.3 CIDFonts".
% It notes default DW is 0, but Acrobat Reader uses 1000 as default.
% If DW is 0, currentpoint does not move by default in rendering text
% horizontally, the result is unreadable. You can check it by Acrobat.

/.pdfDefaultDW  1000 def
/.pdfDefaultDW2 [ 880 -1000 ] def

/addCIDmetrics {	% <CIDFont-resource> <CIDFont> addCIDmetrics <fontdict>
  dup length 5 add dict .copydict
  dup /FID undef
  dup /UniqueID undef
  dup /XUID undef
        % Insert the widths into the font.

        % Stack: pdfresource newfont

  1 index /DW knownoget {
    1 index /DW 3 -1 roll put
  } {
    dup /DW .pdfDefaultDW put
  } ifelse

  1 index /W knownoget {
    //false 1 index { xcheck or } forall {
      oforce_array
    } if
    dup 2 index /W 3 -1 roll put
    .pdfMakeInternalW 1 index /.internalW 3 -1 roll put
  } if

  1 index /DW2 knownoget {
    1 index /DW2 3 -1 roll put
  } {
    dup /DW2 .pdfDefaultDW2 put
  } ifelse

  1 index /W2 knownoget {
    //false 1 index { xcheck or } forall {
      oforce_array
    } if
    dup 2 index /W2 3 -1 roll put
    .pdfMakeInternalW2 1 index /.internalW2 3 -1 roll put
  } if
  dup /CDevProc 1 index /CIDWProc load /exec load 3 packedarray cvx put
  exch pop
} bind executeonly def

/.pdfMakeInternalMTXArray { % <mtx_array> <item_size> .pdfConvertInternalW <mtx_array'>
  % convert /W or /W2 to internal expression
  %
  %   mtx_array: original /W or /W2 array
  %   item_size: number of metrics values per CID
  %
  %   for detail of the metrics list format in PDF,
  %   refer PDF Ref. p.317 "Glyph Metrics in CIDFonts".
  %
  %   format of single entry in internal expression
  %
  %     [
  %       [cid_begin cid_end]
  %       value_is_varied (bool)
  %       [ [values for cid_begin...]
  %         [values for cid_begin + 1]
  %         ... ]
  %     ]
  %

  7 dict
  begin
    /itemSize exch def
    /M exch def			% original /W or /W2
    /Msize M length def
    /Mi { M i get } def		% W[i]
    /Mi1 { M i 1 add get } def	% W[i + 1]
    /putMTXEntry <<
      /arraytype   {
         [
           [Mi Mi Mi1 length itemSize idiv add 1 sub]
           //true
           [
             0 itemSize Mi1 length 1 sub {
               [ exch 1 1 index itemSize add 1 sub { Mi1 exch get } for ]
             } for
           ]
         ]
         /i i 2 add def
      }
      /integertype {
         [
           [Mi Mi1]
           //false
           [[ i 2 add 1 i 1 add itemSize add { M exch get } for ]]
         ]
         /i i 2 add itemSize add def
      }
    >> def

    /i 0 def

    [ { putMTXEntry Mi1 type get exec i Msize ge { exit } if } loop ]
  end
} executeonly def

/.pdfMakeInternalW  { dup length 0 gt { oforce_recursive 1 .pdfMakeInternalMTXArray } if } executeonly def
/.pdfMakeInternalW2 { dup length 0 gt { oforce_recursive 3 .pdfMakeInternalMTXArray } if } executeonly def

/.pdfGetMTXByCID { % <internalMTXArray> <cid>
                   %     .pdfGetMTXByCID
                   %         { <MTXEntry> true | false }

  % get values for given CID from internal format of /W or /W2

  exch
  {
    {
      dup 0 get {} forall      % Stack: <cid> <entry> <cid_0> <cid_1>
      3 index lt { pop pop //false exit } if
      2 index exch sub dup 0 lt { pop pop //false exit } if
      1 index 1 get not { pop 0 } if
      exch 2 get exch get //true exit
    } loop
    { exit } if
  } forall
  dup type /arraytype eq { exch pop //true } { pop //false } ifelse
} executeonly def

% Apply the [D]W[2] metrics to a character before displaying.
/CIDWProc {		% <w0x> <w0y> <llx> <lly> <urx> <ury>
                        %   <w1x> <w1y> <vx> <vy> <cid> <font> CIDWproc
                        %   <w0x'> ... <vy'>
  begin % push <font> to currentdict
    % <w1x> <w1y> <vx> <vy> won't be used and replaced, discard now
    5 1 roll pop pop pop pop

    % get FontMatrix that applies to this CID
    % (needs to convert [D]W[2] values to glyph space)
    currentdict /FontMatrix get
    % if the currentfont is a CIDFontType 0 CIDFont,
    % we need FDArray font's FontMatrix too
    FontType 9 eq {
        currentdict 2 index .type9mapcid
        % <w0x> <w0y> <llx> <lly> <urx> <ury> <cid> <FontMatrix> (charstring) <FDArray#>
        exch pop currentdict /FDArray get exch get /FontMatrix get
        % <w0x> <w0y> <llx> <lly> <urx> <ury> <cid> <FontMatrix> <FontMatrix2>
        matrix concatmatrix
    } if
    % preserve this FontMatrix at bottom of the stack
    8 1 roll
    % Stack: <FontMatrix> <w0x> <w0y> <llx> <lly> <urx> <ury> <cid>

    {
      currentdict /DW .knownget not {   % no DW
        .pdfDefaultDW exit              % replace <w0x> by defaultDW
      } if

      currentdict /.internalW .knownget not {    % no W
        exit                            % use already-stacked DW
      } if

      dup length 0 eq {                 % W is null array
        pop                             % discard unusable W
        exit                            % use already-stacked DW
      } if

      % W is finite array, try to get W_cid
      2 index .pdfGetMTXByCID {           % got W, discard DW
        exch pop {} forall
        exit
      } if

      exit
    } loop
    0                           % <w0y'>

    % Stack: <FontMatrix> <w0x> <w0y> <llx> <lly> <urx> <ury> <cid> <w0x'> <w0y'>
    9 -2 roll pop pop           % discard <w0x> <w0y>
    7  2 roll                   % put <w0x'> <w0y'>

    % Stack: <FontMatrix> <w0x'> <w0y'> <llx> <lly> <urx> <ury> <cid>
    0                           % <w1x'>
    exch                        % put <w1x'>

    % Stack: <FontMatrix> <w0x'> <w0y'> <llx> <lly> <urx> <ury> <w1x'> <cid>
    {
      currentdict /DW2 .knownget not {  % no DW2, use defaultDW2
        .pdfDefaultDW2 exit
      } if

      currentdict /.internalW2 .knownget not {   % has DW2, no W2
        exit                            % use already-stacked DW2
      } if

      dup length 0 eq {                 % W2 is null array
        pop                             % discard unusable W2
        exit                            % use already-stacked DW2
      } if

      2 index .pdfGetMTXByCID {		% got W2_cid, discard DW2
        exch pop
        exit
      } if

      % could not get W2_cid
      exit

    } loop

    exch pop                            % discard <cid>

    % Stack: <FontMatrix> <w0x'> <w0y'> <llx> <lly> <urx> <ury>
    %                           <w1x'> { [<vy'> <w1y'>] | [<w1y'> <vx'> <vy'>] }
    dup length 2 eq {                   % this is DW2
      aload pop
      exch 8 index 2 div                % <vx'> = <w0x'> / 2
      exch
    } {                                 % assume W2
      aload pop
    } ifelse
    % Stack: <FontMatrix> <w0x'> <w0y'> <llx> <lly> <urx> <ury> <w1x'> <w1y'> <vx'> <vy'>

    % now, convert each metrics valude (1000th of text space) to glyph space
    10 -2 roll
    % Stack: <FontMatrix> <llx> <lly> <urx> <ury> <w1x'> <w1y'> <vx'> <vy'> <w0x'> <w0y'>
    3 {
        10 index idtransform exch 1000 div exch 1000 div
        6 2 roll
    } repeat
    10 2 roll
    % Stack: <FontMatrix> <w0x'> <w0y'> <llx> <lly> <urx> <ury> <w1x'> <w1y'> <vx'> <vy'>
    11 -1 roll pop
    % Stack: <w0x'> <w0y'> <llx> <lly> <urx> <ury> <w1x'> <w1y'> <vx'> <vy'>
  end                                   % recover currentdict
} bind executeonly def

% <string> <match> tailmatch ==> <pre> true
%                            ==> <string> false
/tailmatch {
  2 copy length 1 index length .min
  dup 2 index length exch sub exch getinterval
  1 index eq {
    length 1 index length exch sub
    0 exch getinterval //true
  } {
    pop //false
  } ifelse
} bind executeonly def

/makeboldfont {
  16 dict begin
    /strokewidth exch def
    /basecidfont exch def
    /FontMatrix [ 1 0 0 1 0 0 ] def

    /CIDFontName /.boldfont def
    /CIDFontType 1 def

    /basefont-H /.basefont-H /Identity-H [ basecidfont ] composefont def
    /basefont-V /.basefont-V /Identity-V [ basecidfont ] composefont def

    /CIDSystemInfo dup basecidfont exch get def
    /FontBBox [ basecidfont /FontBBox get cvx exec
      4 2 roll basecidfont /FontMatrix get transform
      4 2 roll basecidfont /FontMatrix get transform
    ] def

    /tmpstr 2 string def
    /BuildGlyph {
      gsave
      exch begin
        dup 256 idiv tmpstr exch 0 exch put
        256 mod tmpstr exch 1 exch put
        rootfont /WMode known { rootfont /WMode get 1 eq } { //false } ifelse
        { basefont-V } { basefont-H } ifelse setfont
        strokewidth setlinewidth
        1 setlinejoin
        newpath
        0 0 moveto tmpstr //false charpath stroke
        0 0 moveto tmpstr show
        currentpoint setcharwidth
      end
      grestore
    } bind def

   currentdict
  end
  dup /CIDFontName get exch /CIDFont defineresource
} bind executeonly def

% <CIDFont-resource> <CIDFontName> findCIDFont <CIDFont-resource> <font>
%   CIDFont-resource is not modified.
% In various places we do:
% "findCIDFont dup /OrigFont .knownget {exch pop} if"
% because the CIDFont we find may have been "patched" to apply metrics from
% the PDF file (W, W2, etc). We want to avoid applying those metrics more
% than once, hence retrieving the original, unpatched CIDFont dictionary,
% if it exists.
/findCIDFont {
  {
    dup /CIDFont resourcestatus {
      pop pop dup /CIDFont findresource
      dup /Path known
      1 index /ResourcePath known or
      { exch pop exit }
      { pop dup /CIDFont undefineresource }
      ifelse
    } if
    .remove_font_name_prefix
    dup dup length string cvs

    (,Bold) tailmatch {
      exch pop
      % If we're substituting a standard weight font for a
      % "Light,Bold" font, don't artificially bolden further
      dup (Light) tailmatch
      {
        pop cvn findCIDFont
        dup /OrigFont .knownget {exch pop} if
        dup /FontName .knownget
        {
          (Light) tailmatch exch pop
        }
        {
          //true
        } ifelse

        {
          0.03 makeboldfont
        } if
      }
      {
        pop
        cvn findCIDFont
        dup /OrigFont .knownget {exch pop} if
        0.03 makeboldfont
      } ifelse
      exit
    } if
    (,Italic) tailmatch {
      exch pop
      cvn findCIDFont
      dup /OrigFont .knownget {exch pop} if
      [ 1 0 0.3 1 0 0 ] makefont
      exit
    } if
    (,BoldItalic) tailmatch {
      exch pop
      % see above
      dup (Light) tailmatch
      {
        pop cvn findCIDFont
        dup /OrigFont .knownget {exch pop} if
        dup /FontName .knownget
        {
          (Light) tailmatch exch pop
        }
        {
          //true
        } ifelse

        {
          0.03 makeboldfont
        } if
      }
      {
        pop
        cvn findCIDFont
        dup /OrigFont .knownget {exch pop} if
        0.03 makeboldfont
      } ifelse
      [ 1 0 0.3 1 0 0 ] makefont
      exit
    } if
    QUIET not {
      (Can't find CID font ") print dup =string cvs print (".) = flush
    } if
    pop

    1 index /CIDSystemInfo oget begin Registry (-) Ordering end
    concatstrings concatstrings
    cvn
    QUIET not {
      (Attempting to substitute CID font ) print dup ==only
      ( for ) print 1 index ==only (, see doc/Use.htm#CIDFontSubstitution.) = flush
    } if
    exch pop
    dup /CIDFont resourcestatus {
      pop pop /CIDFont { findresource } stopped
      {pop}{exit} ifelse
    } if
    QUIET not {
      (The substitute CID font ") print dup =string cvs print
      (" is not provided either. ) print
    } if

    PDFNOCIDFALLBACK {
      PDFSTOPONERROR {
        (Will exit with error.) = flush
      }
      {
        (Will attempt to continue, but content may be missing.) = flush
      } ifelse
      exit
    } if

    QUIET not {
      (attempting to use fallback CIDFont.) print
      (See doc/Use.htm#CIDFontSubstitution.) = flush
    } if

    PDFNOCIDFALLBACK not {
      % This previously worked by loading the substitute font, and using that
      % as a "template" to create a new CIDFont dictionary with the CIDSystemInfo
      % dict appropriate for the requested font.
      % This, however, caused problems with the CIDMap and thus broke pdfwrite.
      % Hence, we now modify the CIDFont resource .map dictionary on the fly
      % so it includes an appropriate mapping, so the subsitute font is loaded
      % from the beginning with the correct ordering and supplement settings.
      /CIDFont /Category findresource /.map get dup /CIDFallBack .knownget {
        dup type /dicttype eq {
          dup /FileType get /TrueType eq {
            currentglobal
            4 1 roll
            dup gcheck setglobal
            dup length dict copy dup
            6 index /CIDSystemInfo oget dup /Ordering oget exch /Supplement get
            % we have to copy the string, for VM correctness
            exch dup length string copy exch 2 array astore
            /CSI exch put 2 index exch .forceput
            exch setglobal
            {
              % This *should* not execute arbitrary Postscript, so we ought to
              % be safe running in a stopped context, in knowledge we know how
              % to clean up afterwards - this would not be safe if CIDFallBack
              % were a "real" CIDFont.
              /CIDFont findresource
            }
            % if the above fails, try just using the CIDFallBack font directly
            stopped
            {
              pop
              % First, since it didn't work, remove the entry from the CIDFont category
              % .map dictionary.
              currentglobal exch
              /CIDFont /Category findresource /.map get
              dup gcheck setglobal
              exch undef
              setglobal

              (Warning: falling back to Identity ordering\n) print flush
              /CIDFallBack /CIDFont findresource
            } if
            exit
          } executeonly if
        } if
      } if

      pop pop pop
      /CIDFallBack

      QUIET not {
        (The fallback CID font ") print dup =string cvs print
        (" is not provided. ) print
        ( Finally attempting to use ArtifexBullet.\n) print flush
      }if

      % We'll just use Identity for this, we have only one glyph anyway.
      /ArtifexBullet /CIDFont resourcestatus {
        pop pop pop
        /ArtifexBullet /CIDFont findresource
        exit
      }
      {
        pop /ArtifexBullet
      } ifelse

      QUIET not {
        (The CID font ") print dup =string cvs print
        (" is not provided. ) print
        PDFSTOPONERROR
        {
          (Will exit with error.) = flush
        }
        {
          (Will continue, but content may be missing.) = flush
        } ifelse
      } if
    } executeonly if
    /findresource cvx /undefined signalerror
  } executeonly loop
} bind executeonly odef

/buildCIDType0 {	% <CIDFontType0-font-resource> buildCIDType0 <font>
  dup /BaseFont get findCIDFont exch pop
} bind executeonly def

/buildCIDType2 {	% <CIDFontType2-font-resource> buildCIDType2 <font>
  dup /BaseFont get findCIDFont exch pop
} bind executeonly def

/processCIDToGIDMap { % <fontres> <cidfont> processCIDToGIDMap <fontres> <cidfont>
  1 index /CIDToGIDMap knownoget {
    PDFfile fileposition 4 1 roll
    dup /Identity eq {
      pop
    } {
      //true resolvestream
                % Stack: filepos fontres font mapstream
      [
      { counttomark 1 add index 32768 string readstring not {
          counttomark 1 eq {
            exch pop dup length
          } {
            ] dup 0 exch { length add } forall
          } ifelse
          exit
        } if
      } loop
      2 idiv
        % Stack: filepos fontres font mapstream array/string CIDCount
      dup 0 ne {
        3 index exch /CIDCount exch put
        exch closefile exch
        dup /CIDMap 4 -1 roll put
      }{
        % If CIDCount is zero, ignore it
        pop pop closefile
      }ifelse
    } ifelse
    3 2 roll PDFfile exch setfileposition
  } if
} bind executeonly def

% Adjust a CIDFontType0 DW[2] in the font resource.
/adjustCIDType0 {		% <font-resource> <font> adjustfont <font'>
  dup /FontType get 1 eq {
    (   **** Error: Font resource has a wrong /Subtype.\n)  pdfformaterror
    (               Output may be incorrect.\n) pdfformaterror
    adjustfont
  } {
    addCIDmetrics
    dup /CIDFontName get exch /CIDFont defineresource
  } ifelse
} bind executeonly def

% Adjust a CIDFontType2 DW[2] and CIDToGIDMap in the font resource.
/adjustCIDType2 {		% <font-resource> <font> adjustfont <font'>
  dup /FontType get 1 eq {
    (   **** Error: Font resource has a wrong /Subtype.\n)  pdfformaterror
    (               Output may be incorrect.\n) pdfformaterror
    adjustfont
  } {
    addCIDmetrics
    dup /CIDFontType get 2 eq {   % OpenType CFF font converts to CIDFontType 0
      processCIDToGIDMap          %  that ignores CIDMap.
    } if
    dup /CIDFontName get exch /CIDFont defineresource
  } ifelse
} bind executeonly def

% ---------------- Other embedded fonts ---------------- %

% Read an embedded compressed font.
/readType1C {		% <font-resource> <stream-dict> readType1C <font>
  1 index exch
  PDFfile fileposition 3 1 roll
  dup //true resolvestream dup readfontfilter
                % Stack: pos resource streamdict stream filter
  3 index /FontDescriptor oget /FontName oget
  1 index
  /FontSetInit /ProcSet findresource begin //true //false ReadData
  %% We need to be careful not to corrupt the stack if something went wrong.
  %% Previously, if the dict was length 0 (an error occured) we would end up
  %% unable to recover the stack in the calling procedure.
  %% Bug #695819.
  dup length 0 ne {
    { exch pop exit } forall
    7 1 roll
  }{
    /invalidfont signalerror
  } ifelse
  closefile closefile pop
  PDFfile 3 -1 roll setfileposition
  pop pop
} bind executeonly def

% Read an embedded CFF CIDFont.
/readCIDFontType0C {  % <font-resource> <stream-dict> readCIDFontType0C <font>
  PDFfile fileposition 3 1 roll
  dup //true resolvestream dup readfontfilter
                % Stack: pos resource streamdict stream filter
  3 index /FontDescriptor oget /FontName oget
  1 index
  /FontSetInit /ProcSet findresource begin //true //true ReadData pop
  closefile closefile pop
  PDFfile 3 -1 roll setfileposition
                % Some broken Adobe software produces PDF files in which
                % the FontName of the CFF font and the FontName in the
                % FontDescriptor don't match the BaseFont in the font.
                % Use the FontName, rather than the BaseFont, here.
  dup /FontDescriptor oget /FontName oget /CIDFont findresource
  addCIDmetrics dup /CIDFontName get exch /CIDFont defineresource
} bind executeonly def

% Read an embedded OpenType font.
/readOTTOfont {		% <font-resource> <stream-dict> readOTTOfont <font>
  1 index exch                    % res res strdict
  PDFfile fileposition 3 1 roll   % res pos res strdict
  dup //true resolvestream        % res pos res strdict stream
  dup readfontfilter              % res pos res strdict stream filter
  3 index /FontDescriptor oget
  /FontName oget                  % res pos res strdict stream filter /name
  1 index .init_otto_font_file    % res pos res strdict stream filter /name filter'
  //true
  6 index /CIDSystemInfo known    % res pos res strdict stream filter /name filter' bool bool
  ReadData                        % res pos res strdict stream filter fontset
  { exch pop exit } forall        % res pos res strdict stream filter font
  dup /FontType get 9 eq  {
    % OpenType may contain simple CFF font, which is accesed as a CIDFont by PDF.
    % The font is converted to Type 9 CIDFont resource, which ignores CIDMap attribute.
    % The following code just shuffles GlyphDirectory to the same effect.
    4 index /CIDToGIDMap knownoget {
      dup type /dicttype eq {
        1 index /GlyphDirectory get exch       % res pos res strdict stream filter font dir c2g
        //true resolvestream                   % res pos res strdict stream filter font dir c2g_file
        256 dict begin
        0 2 index 0 get def  % copy .notdef
        0 1 16#7fffffff {
          1 index read not { pop exit } if     % res pos res strdict stream filter font dir c2g_file cid hi
          256 mul                              % res pos res strdict stream filter font dir c2g_file cid hi
          2 index read not { pop pop exit } if % res pos res strdict stream filter font dir c2g_file cid hi lo
          add                                  % res pos res strdict stream filter font dir c2g_file cid gid
          dup 0 ne {
            dup 4 index length lt {
              3 index exch get                 % res pos res strdict stream filter font dir c2g_file cid charstr
              def                              % res pos res strdict stream filter font dir c2g_file
            } {
              pop pop
            } ifelse
          } {
            pop pop
          } ifelse
        } for
        closefile pop                          % res pos res strdict stream filter font
        dup length dict copy                   % res pos res strdict stream filter font'
        dup /GlyphDirectory currentdict put    % res pos res strdict stream filter font'
        end
        dup /GlyphDirectory get 0 exch {
          pop .max
        } forall
        1 index exch /CIDCount exch 1 add put
      } {
      pop
      } ifelse
    } if
  } if
  7 1 roll                        % font res pos res strdict stream filter
  closefile closefile pop pop     % font res pos
  PDFfile exch setfileposition    % font res
  pop                             % font
} bind executeonly def

% ---------------- Font lookup ---------------- %

% Some PDF files mis-identify font type of the embedded font streams or
% include raw PFB font streams. Length1, Length2, Length3 may be wrong or
% missing. Adobe Acrobat corrects these errors transparently to the user.
%
% We ignore the font type keys and recognize the font type by the 1st 4 bytes
% of the font stream. The PFB stream is recognized by the 1st 2 bytes.

/fonttypeprocs mark		% <font-resource> -proc- <font>
  /Type0 //buildType0
  /Type1 //buildType1
  /MMType1 //buildType1
  /Type3 /buildType3 load
  /TrueType //buildTrueType
  /CIDFontType0 //buildCIDType0
  /CIDFontType2 //buildCIDType2
.dicttomark readonly def

/adjustfonttypes mark
  /Type1 //adjustfont
  /MMType1 //adjustfont
  /TrueType //adjustfont
  /CIDFontType0 //adjustCIDType0
  /CIDFontType2 //adjustCIDType2
.dicttomark readonly def

% Bind a proc and define n names
% /name ... /name {proc} n bndef -
/bndef
  { exch bind executeonly exch
    { dup 3 1 roll def } repeat
    pop
  } bind executeonly def

% <res> run-fonttypeproc <font>
/run-fonttypeproc {
  dup /Subtype knownoget not { 0 } if % res /subtype
  //fonttypeprocs 1 index known not {
    (   **** Error: Font /Subtype attribute is wrong or missing, /Type1 assumed.\n)
    pdfformaterror
    (               Output may be incorrect.\n) pdfformaterror
    pop /Type1
    2 copy /Subtype exch put
  } if
  //fonttypeprocs exch get exec
} bind executeonly def

% Prototype for all procedures: <res> <desc> <stream> foo <font>
/font_tag_dict 13 dict begin

  % When the font stream is absent or cannot be read we load the font by the name.
  /no_stream
    { pop pop
      run-fonttypeproc
    } bind executeonly def

  /bad_stream
    { (   **** Error: Error reading font stream, attempting to load the font using its name\n)
      pdfformaterror
      (               Output may be incorrect.\n) pdfformaterror
      //no_stream exec
    } bind executeonly def

  <8001>          % PFB
    { dup /PFB //true put
      exch pop readtype1
    } bind executeonly def

  (%!PS) (%!Fo)   % Type1
    { exch pop readtype1
    } 2 bndef

  <01000401> % Type 1C
    {
      exch pop

      1 index /Subtype knownoget {
        dup /CIDFontType2 eq{
          pop
          (   **** Error: Subtype of a Type 1 CID font is not /CIDFontType0\n) pdfformaterror
          (               Output may be incorrect.\n) pdfformaterror
          1 index /Subtype /CIDFontType0 put
          readCIDFontType0C
        }
        {
          /CIDFontType0 eq {
            readCIDFontType0C
          }
          {
            readType1C
          } ifelse
        } ifelse
      } {
        readType1C
      } ifelse
    } bind executeonly def

  <00010000> (true) (typ1) (ttcf)  % TrueType OpenType
    { exch pop readtruetype
    } 4 bndef

  (OTTO)
    { exch pop
      readOTTOfont
    } bind executeonly def

currentdict end readonly def
currentdict /bndef undef

% Used to create a hash for the XUID, ths combines the input PDF file name and the
% object number (currently the object number of the FontDescriptor). NB we *must*
% use an object number, as otherwise all fonts will have the same
% XUID and Ghostscript will think they are the same font. Ths routine must not
% be used unless we have an object number which is believed to be uniqe to
% a particular font instance.
/.CRCHashFilenameAndObject % object_number (filename) CRCHasFilename integer
{
    dup                     % object_number (filename) (filename)
    length                  % object_number (filename) length
    0 exch                  % object_number (filename) hash length
    1 sub                   % object_number (filename) hash length-1
    0 1 3 -1 roll           % object_number (filename) hash 0 1 length-1
    {                       % object_number (filename) hash index
      2 index exch get      % object_number (filename) hash char
      exch                  % object_number (filename) char hash
      dup 16#F8000000 and   % object_number (filename) char hash top5bits
      -27 bitshift          % object_number (filename) char hash top5>>27
      exch 5 bitshift       % object_number (filename) char top5>>27 hash<<5
      16#7FFFFFFF and
      xor                   % object_number (filename) char CRC<hash>
      xor                   % object_number (filename) newhash
    } for
    exch pop              % object_number newhash
    dup 16#F8000000 and   % object_number newhash top5bits
    -27 bitshift          % object_number newhash top5>>27
    exch 5 bitshift       % object_number top5>>27 newhash<<5
    16#7FFFFFFF and
    xor                   % object_number CRC<newhash>
    xor                   % final hashed value
}bind executeonly def

% Either patch or create a new XUID based on the existing
% XUID or UniqueID and incorporating the PDF object number
% so we get an XUID we can use to improve cache efficiency
% in multipage documents.
/patch_font_XUID     % <font dict> <patch> patch_font_XUID <font dict'>
{
  dup //null eq
  {pop}
  {
    //pdfdict /InputPDFFileName .knownget {.CRCHashFilenameAndObject} if
    exch dup /.OrigUniqueIDXUID .knownget not
    {
      dup /XUID .knownget not
      {
        dup /UniqueID .knownget
        {1 array astore}{ [] } ifelse
      } if
    } if

    dup //null eq
    {
          pop exch pop
    }
    {
      [ 1000000 3 -1 roll 4 index exch {exch} forall ]
      exch //true .copyfontdict
      dup 3 -1 roll /XUID exch put
      exch pop
      .completefont
    } ifelse
  } ifelse
} bind executeonly def

/resourcefont                   % <font-resource> resourcefont <font>
{
  dup /PSFont .knownget dup {
    pop /FID knownoget dup { pop type /fonttype eq } if
  } if {
    /PSFont get
  } {

    dup /Subtype get /Type0 eq {
      % if the font was defined in-line, we won't have a object number
      dup /.gs.pdfobj# .knownget not { //null } if
    } {
          % if the font was defined in-line, we won't have a object number
      dup /.gs.pdfobj# .knownget not { //null }if
      dup //null eq not {
         1 index /FontDescriptor knownoget {
           dup /FontFile knownoget not {
           dup /FontFile2 knownoget not {
              dup /FontFile3 knownoget not {
                pop pop //null
              } {pop pop}ifelse
            } {pop pop}ifelse
           }{pop pop}ifelse
         }
         {
           pop //null
         } ifelse
      }if
    } ifelse
    3 1 roll

    % In the event we have a Type 0 dictionary with a spurious
    % FontDescriptor object, remove it here to prevent confusion
    % later on.
    % Bug 691589
    dup /Subtype get /Type0 eq 1 index /FontDescriptor known and
    {
        dup /FontDescriptor undef
    }if
    dup /CIDSystemInfo .knownget
    {
      oforce_recursive
      1 index exch /CIDSystemInfo exch put
    } if

    dup dup /FontDescriptor knownoget {
                                          % font-res font-res font-desc
      % The same font descriptor can be reused in a CID and non-CID contexts.
      % Store CID and non-CID fonts under different keys. Bug 689301
      1 index /Subtype knownoget dup {
        pop dup /CIDFontType0 eq exch /CIDFontType2 eq or
      } if { /CIDFontObject } { /FontObject } ifelse
                                          % font-res font-res font-desc /key
      2 index 1 index .knownget {
        4 1 roll pop pop pop              % font-res obj
      } {
        4 1 roll                          % /key font-res font-res font-desc

        dup /FontFile knownoget not {
          dup /FontFile2 knownoget not {
            dup /FontFile3 knownoget not {
              //null
            } if
          } if
        } if
                                          % /key res res desc stream
        dup //null ne {
          PDFfile fileposition
          mark {
            2 index //true resolvestream dup
            4 string readstring pop
            exch closefile
          } stopped {
            cleartomark /bad_stream
          } {
            exch pop
          } ifelse
          PDFfile 3 -1 roll setfileposition
          dup length 4 lt { pop /bad_stream } if
        } {
          /no_stream
        } ifelse
                                          % /key res res desc stream tag

        //font_tag_dict 1 index known not {
          %
          % The tag is the first 4 (?) bytes frm the font stream, if we get here then
          % it didn't match any of the keys in the dictionary. There seems to be
          % a possibility that a PFB font could be undetected (because its only 2 bytes?)
          % but the main problem is CFF fonts. These lack a magic number, and the header
          % can vary in the last 3 of the 4 bytes. We've now found 7 different possible
          % values for these bytes, adding more seems pointless and isn't future proof.
          % Instead check the dictionary for its Subtype.
          %
          dup 0 2 getinterval <8001> eq {
            0 2 getinterval               % /key res res desc stream pfb_tag
          } {
            1 index /Subtype known {
              1 index /Subtype get
                dup /Type1C eq 1 index /CIDFontType0C eq or exch /CIDFontType1C eq or {
                pop <01000401>            % discard the data we read and pretend we read the original Type1C tag
                //false
              }{
                //true
              }ifelse
            } {
              //true                      % no /Subtype, give up.
            }ifelse

            {
              (12345678>\n) dup /ASCIIHexEncode filter dup 4 -1 roll writestring closefile
              (   **** Error: unrecognized font file starts with <) exch concatstrings
              pdfformaterror
              (               Output may be incorrect.\n) pdfformaterror
              /no_stream                    % /key res res desc stream unknown_tag
            }if
          } ifelse
        } if
        //font_tag_dict exch get          % /key res res desc stream {proc}
        { stopped } 0 get                 % /key res res desc stream {proc} --stopped--
        count                             % /key res res desc stream {proc} --stopped-- #
        countdictstack                    % /key res res desc stream {proc} --stopped-- # ##
        PDFfile fileposition              % /key res res desc stream {proc} --stopped-- # ## ###
        5 array astore cvx                % /key res res desc stream {{proc} --stopped-- # ## ###}
        exec
        4 -1 roll {
          PDFSTOPONERROR not {
            PDFfile exch setfileposition
            countdictstack exch sub 0 .max { end } repeat
            count 4 add exch sub 0 .max { pop } repeat
            //null
          } {
            /resourcefont cvx /undefinederror signalerror
          } ifelse
        } {
          PDFfile exch setfileposition
          countdictstack exch sub 0 .max { end } repeat
          count 3 sub exch sub 0 .max { pop } repeat
        } ifelse
        dup //null eq {
          PDFSTOPONERROR not {
            pop
            (   **** Error: can't process embedded font stream,\n        attempting to load the font using its name.\n)
            pdfformaterror
            (               Output may be incorrect.\n) pdfformaterror
            dup run-fonttypeproc            % /key res font
          } {
            /resourcefont cvx /undefinederror signalerror
          } ifelse
        } if
        1 index                           % /key res font res
        4 -1 roll 2 index                 % res font desc /key font
        put  % Save pointer to the font.
      } ifelse
    } {
      run-fonttypeproc
    } ifelse
        % Stack: font-res font
    1 index exch
    1 index /Subtype get
    //adjustfonttypes exch .knownget { exec } { exch pop } ifelse
    4 -1 roll patch_font_XUID
    dup 3 1 roll /PSFont exch put
  } ifelse
  dup checkGlyphNames2Unicode
} bind executeonly def

currentdict /font_tag_dict .undef
currentdict /fonttypeprocs .undef
currentdict /adjustfonttypes .undef

drawopdict begin
  /d0 {
    currentcolor currentcolorspace .swapcolors setcolorspace setcolor .swapcolors
    .adjustcharwidth setcharwidth
  } bind executeonly def
  /d1 {
    2 copy                           % ... llx lly urx ury | urx ury
    0 ne exch 0 ne                   % ... llx lly urx ury | ury!=0 urx!=0
    3 index 6 index eq and           % ... llx lly urx ury | ury!=0 (urx!=0 && llx==urx)
    exch 2 index 5 index eq and or { % ... llx lly urx ury | (urx!=0 && llx==urx) || (ury!=0 && lly==ury)
      % The bounding box is empty and likely incorrect. Don't cache.
      pop pop pop pop
      //d0 exec
    } {
      currentcolor currentcolorspace .swapcolors setcolorspace setcolor .swapcolors
      6 -2 roll .adjustcharwidth 6 2 roll setcachedevice
    } ifelse
  } bind executeonly def
  /Tf {
    1 index Page /Font rget {
      dup type /dicttype eq {
        resourcefont exch
        Tf pop
      }
      {
        PDFSTOPONERROR
        {
          /typecheck signalerror
        }
        {
          % Bug 689450
          type
          (   **** Error: Tf refers to a resource key with an invalid value type: ) pdfformaterror
          .namestring pdfformaterror
          (. Assuming resource key: ) pdfformaterror
          1 index .namestring pdfformaterror
          ( is a font name.\n) pdfformaterror
          (               Output may be incorrect.\n) pdfformaterror
         Tf
        } ifelse
      }ifelse
    }
    {
      PDFSTOPONERROR
      {
          pop /undefined signalerror
      }
      {
        % Bug 689037
        (   **** Error: Tf refers to an unknown resource name: ) pdfformaterror
        1 index .namestring pdfformaterror
        ( Assuming it's a font name.\n) pdfformaterror
        (               Output may be incorrect.\n) pdfformaterror
        Tf
      } ifelse
    } ifelse
  } bind executeonly def
end

end			% pdfdict
end			% GS_PDF_ProcSet
.setglobal