summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/omegaware/odvicopy.web
blob: cda08cc427faf0642554b22a354e736bd6ffc43d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
% odvicopy.web: Omega version of file dvicopy.web
% 
% This file is part of the Omega project, which
% is based in the web2c distribution of TeX.
%
% Copyright (c) 1994--1998 John Plaice and Yannis Haralambous
% applies only to the changes to the original dvicopy.web.
%
% This program is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation; either version 1, or (at your option)
% any later version.
%
% You should have received a copy of the GNU General Public License
% along with this program; if not, write to the Free Software
% Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
%
% Version 0.9 was finished May 21, 1990.
% Version 1.0 pixel rounding for real devices (August 6, 1990).
% Version 1.1 major rearrangements for DVIprint (October 7, 1990).
% Version 1.2 fixed some bugs, page selection (February 13, 1991).
% Version 1.3 several more changes, command line options,
%             don't load fonts that are never used (August 25, 1992).
% Version 1.4 fixed a typo (March 28, 1995).
% Version 1.5 avoided cur_name_length identifier conflict (October 15, 1995).

% Here is TeX material that gets inserted after \input webmac
\def\hang{\hangindent 3em\indent\ignorespaces}
\font\ninerm=cmr9
\let\mc=\ninerm % medium caps for names like SAIL
\def\PASCAL{Pascal}
\font\logo=manfnt % font used for the METAFONT logo
\def\MF{{\logo META}\-{\logo FONT}}
\mathchardef\RA="3221 % right arrow

\def\(#1){} % this is used to make section names sort themselves better
\def\9#1{} % this is used for sort keys in the index

\def\title{ODVI\lowercase{copy}} % don't change this line!
\def\contentspagenumber{1}
\def\topofcontents{\null
  \def\titlepage{F} % include headline on the contents page
  \def\rheader{\mainfont\hfil \contentspagenumber}
  \vfill
  \centerline{\titlefont The {\ttitlefont ODVIcopy} processor}
  \vskip 5pt
  \centerline{Copyright (C) 1990,95 Peter Breitenlohner,
1994,97 John Plaice and Yannis Haralambous}
  \centerline{Distributed under terms of GNU General Public License}
  \vskip 15pt
  \centerline{(Version 1.5, March 1997)}
  \vfill}
\def\botofcontents{\vfill
  \centerline{\hsize 5in\baselineskip9pt
    \vbox{\ninerm\noindent
    This program was developed at the Max-Planck-Institut f\"ur Physik
    (Werner-Heisenberg-Institut), Munich, Germany.
    And modified at the Universit\'e Laval, Qu\'ebec, Canada.
    `\TeX' is a trademark of the American Mathematical Society.
    `{\logo hijklmnj}\kern1pt' is a trademark of Addison-Wesley
    Publishing Company.}}}
\pageno=\contentspagenumber \advance\pageno by 1

@* Introduction.
The \.{ODVIcopy} utility program copies (selected pages of) binary
device-independent (``\.{DVI}'') files that are produced by document
compilers such as \TeX and $\Omega$,
and replaces all references to characters from
virtual fonts by the typesetting instructions specified for them in
binary virtual-font (``\.{VF}'' or ``OVF'') files.
This program has two chief purposes: (1)~It can be used as preprocessor
for existing \.{DVI}-related software in cases where this software is
unable to handle virtual fonts or (given suitable \.{VF} or \.{OVF} files)
where this software cannot handle fonts with more than 128~characters;
and (2)~it serves as an example of a program that reads \.{DVI},
\.{VF} and \.{OVF} files correctly, for system programmers who are developing
\.{DVI}-related software.

Goal number (1) is important since quite a few existing programs have
to be adapted to the extened capabilities of Version~3 of \TeX\ which
will require some time. Moreover some existing programs are `as is' and
the source code is, unfortunately, not available.
Goal number (2) needs perhaps a bit more explanation. Programs for
typesetting need to be especially careful about how they do arithmetic; if
rounding errors accumulate, margins won't be straight, vertical rules
won't line up, and so on (see the documentaion of \.{DVItype} for more
details). This program is written as if it were a \.{DVI}-driver for a
hypothetical typesetting device |out_file|, the output file receiving
the copy of the input |dvi_file|. In addition all code related to
|out_file| is concentrated in two chapters at the end of this program
and quite independent of the rest of the code concerned with the
decoding of \.{DVI}, \.{VF} and \.{OVF} files and with font substitutions. Thus
it should be relatively easy to replace the device dependent code of
this program by the corresponding code required for a real typesetting
device. Having this in mind \.{DVItype}'s pixel rounding algorithms are
included as conditional code not used by \.{ODVIcopy}.

The \.{ODVIcopy} program is an extension of \.{DVIcopy} that allows
the use of \.{OVF} and \.{OFM} files, which are used by the $\Omega$
typesetting system.  These files allows fonts with 65536 characters,
unlike ordinary \.{VF} and \.{TFM} files, which only allow 256
characters. The definition for \.{OVF} files is identical to the one for
\.{VF} files.  On the other hand, \.{OFM} files are different;  their
documentation can be found in the $\Omega$ web.

Unless the contrary is specified below, all references to \.{TFM} files
also refer to \.{OFM} files, similarly for \.{VF} and \.{OVF} files.
References to \.{DVIcopy} also apply to \.{ODVIcopy}.


The |banner| and |preamble_comment| strings defined here should be
changed whenever \.{ODVIcopy} gets modified.

@d banner=='This is ODVIcopy, Version 1.5' {printed when the program starts}
@d title=='ODVIcopy' {the name of this program, used in some messages}
@d copyright==
   '(C) 1990,95 P. Breitenlohner, 1994,97 J. Plaice and Y. Haralambous'
@#
@d preamble_comment=='ODVIcopy 1.5 output from '
@d comm_length=25 {length of |preamble_comment|}
@d from_length=6 {length of its |' from '| part}

@ This program is written in standard \PASCAL, except where it is necessary
to use extensions; for example, \.{DVIcopy} must read files whose names
are dynamically specified, and that would be impossible in pure \PASCAL.
All places where nonstandard constructions are used have been listed in
the index under ``system dependencies.''
@!@^system dependencies@>

One of the extensions to standard \PASCAL\ that we shall deal with is the
ability to move to a random place in a binary file; another is to
determine the length of a binary file. Such extensions are not necessary
for reading \.{DVI} files; since \.{DVIcopy} is (a model for) a
production program it should, however, be made as efficient as possible
for a particular system. If \.{DVIcopy} is being used with
\PASCAL s for which random file positioning is not efficiently available,
the following definition should be changed from |true| to |false|; in such
cases, \.{DVIcopy} will not include the optional feature that reads the
postamble first.

@d random_reading==true {should we skip around in the file?}

@ The program begins with a fairly normal header, made up of pieces that
@^system dependencies@>
will mostly be filled in later. The \.{DVI} input comes from file
|dvi_file|, the \.{DVI} output goes to file |out_file|, and messages
go to \PASCAL's standard |output| file.
The \.{TFM} and \.{VF} files are defined later since their external names
are determined dynamically.

If it is necessary to abort the job because of a fatal error, the program
calls the `|jump_out|' procedure, which goes to the label |final_end|.

@d final_end = 9999 {go here to wrap it up}

@p @t\4@>@<Compiler directives@>@/
program ODVI_copy(@!dvi_file,@!out_file,@!output);
label final_end;
const @<Constants in the outer block@>@/
type @<Types in the outer block@>@/
var @<Globals in the outer block@>@/
@<Error handling procedures@>@/
procedure initialize; {this procedure gets things started properly}
  var @<Local variables for initialization@>@/
  begin print_ln(banner);@/
  print_ln(copyright);
  print_ln('Distributed under terms of GNU General Public License');@/
  @<Set initial values@>@/
  end;

@ The definition of |max_font_type| should be adapted to the number of
font types used by the program; the first three values have a fixed
meaning:  |defined_font=0| indicates that a font has been defined,
|loaded_font=1| indicates that the \.{TFM} file has been loaded but the
font has not yet been used, and |vf_font_type=2| indicates a virtual
font.  Font type values |>=real_font=3| indicate real fonts and
different font types are used to distinguish various kinds of font files
(\.{GF} or \.{PK} or \.{PXL}).  \.{DVIcopy} uses |out_font_type=3| for
fonts that appear in the output \.{DVI} file.
@!@^font types@>

@d defined_font=0 {this font has been defined}
@d loaded_font=1 {this font has been defined and loaded}
@d vf_font_type=2 {this font is a virtual font}
@d real_font=3 {smallest font type for real fonts}
@#
@d out_font_type=3 {this font appears in the output file}
@d max_font_type=3

@ The following parameters can be changed at compile time to extend or
reduce \.{DVIcopy}'s capacity.

@d max_select=10 {maximum number of page selection ranges}

@<Constants...@>=
@!max_fonts=100; {maximum number of distinct fonts}
@!max_chars=10000; {maximum number of different characters among all fonts}
@!max_widths=3000; {maximum number of different characters widths}
@!max_packets=5000; {maximum number of different characters packets;
  must be less than 65536}
@!max_bytes=30000; {maximum number of bytes for characters packets}
@!max_recursion=10; {\.{VF} files shouldn't recurse beyond this level}
@!stack_size=100; {\.{DVI} files shouldn't |push| beyond this depth}
@!terminal_line_length=150; {maximum number of characters input in a single
  line of input from the terminal}
@!name_length=50; {a file name shouldn't be longer than this}
@!neg_max_chars=-10000; {maximum number of different characters among all fonts}

@ As mentioned above, \.{DVIcopy} has two chief purposes: (1)~It produces
a copy of the input \.{DVI} file with all references to characters from
virtual fonts replaced by their expansion as specified in the character
packets of \.{VF} files; and (2)~it serves as an example of a program
that reads \.{DVI} and \.{VF} files correctly, for system programmers
who are developing \.{DVI}-related software.

In fact, a very large section of code (starting with the second chapter
`Introduction (continued)' and ending with the fifteenth chapter
`The main program') is used in identical form in \.{DVIcopy} and in
\.{DVIprint}, a prototype \.{DVI}-driver.  This has been made possible
mostly by using several \.{WEB} coding tricks, such as not to make the
resulting \PASCAL\ program inefficient in any way.

Parts of the program that are needed in \.{DVIprint} but not in
\.{DVIcopy} are delimited by the codewords `$|device|\ldots|ecived|$';
these are mostly the pixel rounding algorithms used to convert the
\.{DVI} units of a \.{DVI} file to the raster units of a real output
device and have been copied more or less verbatim from \.{DVItype}.

@d device==@{ {change this to `$\\{device}\equiv\null$' when output
  for a real device is produced}
@d ecived==@t@>@} {change this to `$\\{ecived}\equiv\null$' when output
  for a real device is produced}
@f device==begin
@f ecived==end

@* Introduction (continued).
On some systems it is necessary to use various integer subrange types
in order to make \.{\title} efficient; this is true in particular for
frequently used variables such as loop indices. Consider an integer
variable |x| with values in the range |0..255|: on most small systems
|x| should be a one or two byte integer whereas on most large systems
|x| should be a four byte integer.
Clearly the author of a program knows best which range of values is
required for each variable; thus \.{\title} never uses \PASCAL's |integer|
type. All integer variables are declared as one of the integer subrange
types defined below as \.{WEB} macros or \PASCAL\ types; these definitions
can be used without system-dependent changes, provided the signed 32~bit
integers are a subset of the standard type |integer|, and the compiler
automatically uses the optimal representation for integer subranges
(both conditions need not be satisfied for a particular system).
@^system dependencies@>

The complementary problem of storing large arrays of integer type
variables as compactly as possible is addressed differently; here
\.{\title} uses a \PASCAL\ |type|~declaration for each kind of array
element.

Note that the primary purpose of these definitions is optimizations, not
range checking. All places where optimization for a particular system is
highly desirable have been listed in the index under ``optimization.''
@!@^optimization@>

@d int_32 == integer {signed 32~bit integers}

@<Types...@>=
@!int_31 = 0..@"7FFFFFFF; {unsigned 31~bit integer}
@!int_24u = 0..@"FFFFFF; {unsigned 24~bit integer}
@!int_24 = -@"800000..@"7FFFFF; {signed 24~bit integer}
@!int_23 = 0..@"7FFFFF; {unsigned 23~bit integer}
@!int_16u = 0..@"FFFF; {unsigned 16~bit integer}
@!int_16 = -@"8000..@"7FFF; {signed 16~bit integer}
@!int_15 = 0..@"7FFF; {unsigned 15~bit integer}
@!int_8u = 0..@"FF; {unsigned 8~bit integer}
@!int_8 = -@"80..@"7F; {signed 8~bit integer}
@!int_7 = 0..@"7F; {unsigned 7~bit integer}

@ Some of this code is optional for use when debugging only;
such material is enclosed between the delimiters |debug| and $|gubed|$.
Other parts, delimited by |stat| and $|tats|$, are optionally included
if statistics about \.{\title}'s memory usage are desired.

@d debug==@{ {change this to `$\\{debug}\equiv\null$' when debugging}
@d gubed==@t@>@} {change this to `$\\{gubed}\equiv\null$' when debugging}
@f debug==begin
@f gubed==end
@#
@d stat==@{ {change this to `$\\{stat}\equiv\null$'
  when gathering usage statistics}
@d tats==@t@>@} {change this to `$\\{tats}\equiv\null$'
  when gathering usage statistics}
@f stat==begin
@f tats==end

@ The \PASCAL\ compiler used to develop this program has ``compiler
directives'' that can appear in comments whose first character is a dollar sign.
In production versions of \.{\title} these directives tell the compiler that
@^system dependencies@>
it is safe to avoid range checks and to leave out the extra code it inserts
for the \PASCAL\ debugger's benefit, although interrupts will occur if
there is arithmetic overflow.

@<Compiler directives@>=
@{@&$C-,A+,D-@} {no range check, catch arithmetic overflow, no debug overhead}
@!debug @{@&$C+,D+@}@+ gubed {but turn everything on when debugging}

@ Labels are given symbolic names by the following definitions. We insert
the label `|exit|:' just before the `\ignorespaces|end|\unskip' of a
procedure in which we have used the `|return|' statement defined below;
the label `|restart|' is occasionally used at the very beginning of a
procedure; and the label `|reswitch|' is occasionally used just prior to
a \&{case} statement in which some cases change the conditions and we wish to
branch to the newly applicable case.
Loops that are set up with the \&{loop} construction defined below are
commonly exited by going to `|done|' or to `|found|' or to `|not_found|',
and they are sometimes repeated by going to `|continue|'.

@d exit=10 {go here to leave a procedure}
@d restart=20 {go here to start a procedure again}
@d reswitch=21 {go here to start a case statement again}
@d continue=22 {go here to resume a loop}
@d done=30 {go here to exit a loop}
@d found=31 {go here when you've found it}
@d not_found=32 {go here when you've found something else}

@ The term |print| is used instead of |write| when this program writes on
|output|, so that all such output could easily be redirected if desired;
the term |d_print| is used for conditional output if we are debugging.

@d print(#)==write(output,#)
@d print_ln(#)==write_ln(output,#)
@d new_line==write_ln(output) {start new line}
@d print_nl(#)==  {print information starting on a new line}
  begin new_line; print(#);
  end
@#
@d d_print(#)==@!debug print(#) @; @+ gubed
@d d_print_ln(#)==@! debug print_ln(#) @; @+ gubed

@ Here are some macros for common programming idioms.

@d incr(#) == #:=#+1 {increase a variable by unity}
@d decr(#) == #:=#-1 {decrease a variable by unity}
@#
@d Incr_Decr_end(#)==#
@d Incr(#)==#:=#+Incr_Decr_end {we use |Incr(a)(b)| to increase \dots}
@d Decr(#)==#:=#-Incr_Decr_end {\dots\ and |Decr(a)(b)| to decrease
  variable |a| by |b|; this can be optimized for some compilers}
@#
@d loop == @+ while true do@+ {repeat over and over until a |goto| happens}
@d do_nothing == {empty statement}
@d return == goto exit {terminate a procedure call}
@f return == nil
@f loop == xclause

@ We assume that |case| statements may include a default case that applies
if no matching label is found. Thus, we shall use constructions like
@^system dependencies@>
$$\vbox{\halign{#\hfil\cr
|case x of|\cr
1: $\langle\,$code for $x=1\,\rangle$;\cr
3: $\langle\,$code for $x=3\,\rangle$;\cr
|othercases| $\langle\,$code for |x<>1| and |x<>3|$\,\rangle$\cr
|endcases|\cr}}$$
since most \PASCAL\ compilers have plugged this hole in the language by
incorporating some sort of default mechanism. For example, the compiler
used to develop \.{WEB} and \TeX\ allows `|others|:' as a default label,
and other \PASCAL s allow syntaxes like `\ignorespaces|else|\unskip' or
`\&{otherwise}' or `\\{otherwise}:', etc. The definitions of |othercases|
and |endcases| should be changed to agree with local conventions. (Of
course, if no default mechanism is available, the |case| statements of
this program must be extended by listing all remaining cases.
Donald~E. Knuth, the author of the \.{WEB} system program \.{TANGLE},
@^Knuth, Donald Ervin@>
would have taken the trouble to modify \.{TANGLE} so that such extensions
were done automatically, if he had not wanted to encourage \PASCAL\
compiler writers to make this important change in \PASCAL, where it belongs.)

@d othercases == others: {default for cases not listed explicitly}
@d endcases == @+end {follows the default case in an extended |case| statement}
@f othercases == else
@f endcases == end

@* The character set.
Like all programs written with the  \.{WEB} system, \.{\title} can be
used with any character set. But it uses ASCII code internally, because
the programming for portable input-output is easier when a fixed internal
code is used, and because \.{DVI} and \.{VF} files use ASCII code for
file names and certain other strings.

The next few sections of \.{\title} have therefore been copied from the
analogous ones in the \.{WEB} system routines. They have been considerably
simplified, since \.{\title} need not deal with the controversial
ASCII codes less than @'40 or greater than @'176.
If such codes appear in the \.{DVI} file,
they will be printed as question marks.

@<Types...@>=
@!ASCII_code=" ".."~"; {a subrange of the integers}

@ The original \PASCAL\ compiler was designed in the late 60s, when six-bit
character sets were common, so it did not make provision for lower case
letters. Nowadays, of course, we need to deal with both upper and lower case
alphabets in a convenient way, especially in a program like \.{\title}.
So we shall assume that the \PASCAL\ system being used for \.{\title}
has a character set containing at least the standard visible characters
of ASCII code (|"!"| through |"~"|).

Some \PASCAL\ compilers use the original name |char| for the data type
associated with the characters in text files, while other \PASCAL s
consider |char| to be a 64-element subrange of a larger data type that has
some other name.  In order to accommodate this difference, we shall use
the name |text_char| to stand for the data type of the characters in the
output file.  We shall also assume that |text_char| consists of
the elements |chr(first_text_char)| through |chr(last_text_char)|,
inclusive. The following definitions should be adjusted if necessary.
@^system dependencies@>

@d text_char == char {the data type of characters in text files}
@d first_text_char=0 {ordinal number of the smallest element of |text_char|}
@d last_text_char=127 {ordinal number of the largest element of |text_char|}

@<Types...@>=
@!text_file=packed file of text_char;

@ @<Local variables for init...@>=
@!i:int_16; {loop index for initializations}

@ The \.{\title} processor converts between ASCII code and
the user's external character set by means of arrays |xord| and |xchr|
that are analogous to \PASCAL's |ord| and |chr| functions.

@<Globals...@>=
@!xord: array [text_char] of ASCII_code;
  {specifies conversion of input characters}
@!xchr: array [0..255] of text_char;
  {specifies conversion of output characters}

@ Under our assumption that the visible characters of standard ASCII are
all present, the following assignment statements initialize the
|xchr| array properly, without needing any system-dependent changes.

@<Set init...@>=
for i:=0 to @'37 do xchr[i]:='?';
xchr[@'40]:=' ';
xchr[@'41]:='!';
xchr[@'42]:='"';
xchr[@'43]:='#';
xchr[@'44]:='$';
xchr[@'45]:='%';
xchr[@'46]:='&';
xchr[@'47]:='''';@/
xchr[@'50]:='(';
xchr[@'51]:=')';
xchr[@'52]:='*';
xchr[@'53]:='+';
xchr[@'54]:=',';
xchr[@'55]:='-';
xchr[@'56]:='.';
xchr[@'57]:='/';@/
xchr[@'60]:='0';
xchr[@'61]:='1';
xchr[@'62]:='2';
xchr[@'63]:='3';
xchr[@'64]:='4';
xchr[@'65]:='5';
xchr[@'66]:='6';
xchr[@'67]:='7';@/
xchr[@'70]:='8';
xchr[@'71]:='9';
xchr[@'72]:=':';
xchr[@'73]:=';';
xchr[@'74]:='<';
xchr[@'75]:='=';
xchr[@'76]:='>';
xchr[@'77]:='?';@/
xchr[@'100]:='@@';
xchr[@'101]:='A';
xchr[@'102]:='B';
xchr[@'103]:='C';
xchr[@'104]:='D';
xchr[@'105]:='E';
xchr[@'106]:='F';
xchr[@'107]:='G';@/
xchr[@'110]:='H';
xchr[@'111]:='I';
xchr[@'112]:='J';
xchr[@'113]:='K';
xchr[@'114]:='L';
xchr[@'115]:='M';
xchr[@'116]:='N';
xchr[@'117]:='O';@/
xchr[@'120]:='P';
xchr[@'121]:='Q';
xchr[@'122]:='R';
xchr[@'123]:='S';
xchr[@'124]:='T';
xchr[@'125]:='U';
xchr[@'126]:='V';
xchr[@'127]:='W';@/
xchr[@'130]:='X';
xchr[@'131]:='Y';
xchr[@'132]:='Z';
xchr[@'133]:='[';
xchr[@'134]:='\';
xchr[@'135]:=']';
xchr[@'136]:='^';
xchr[@'137]:='_';@/
xchr[@'140]:='`';
xchr[@'141]:='a';
xchr[@'142]:='b';
xchr[@'143]:='c';
xchr[@'144]:='d';
xchr[@'145]:='e';
xchr[@'146]:='f';
xchr[@'147]:='g';@/
xchr[@'150]:='h';
xchr[@'151]:='i';
xchr[@'152]:='j';
xchr[@'153]:='k';
xchr[@'154]:='l';
xchr[@'155]:='m';
xchr[@'156]:='n';
xchr[@'157]:='o';@/
xchr[@'160]:='p';
xchr[@'161]:='q';
xchr[@'162]:='r';
xchr[@'163]:='s';
xchr[@'164]:='t';
xchr[@'165]:='u';
xchr[@'166]:='v';
xchr[@'167]:='w';@/
xchr[@'170]:='x';
xchr[@'171]:='y';
xchr[@'172]:='z';
xchr[@'173]:='{';
xchr[@'174]:='|';
xchr[@'175]:='}';
xchr[@'176]:='~';
for i:=@'177 to 255 do xchr[i]:='?';

@ The following system-independent code makes the |xord| array contain a
suitable inverse to the information in |xchr|.

@<Set init...@>=
for i:=first_text_char to last_text_char do xord[chr(i)]:=@'40;
for i:=" " to "~" do xord[xchr[i]]:=i;

@* Reporting errors to the user.
The \.{\title} processor does not verify that every single bit read from
one of its binary input files is meaningful and consistent; there are
other programs, e.g., \.{DVItype}, \.{TFtoPL}, and \.{VFtoPL}, specially
designed for that purpose.

On the other hand, \.{\title} is designed to avoid unpredictable results
due to undetected arithmetic overflow, or due to violation of integer
subranges or array bounds under {\it all\/} circumstances. Thus a fair
amount of checking is done when reading and analyzing the input data,
even in cases where such checking reduces the efficiency of the program
to some extent.

@ A global variable called |history| will contain one of four values
at the end of every run: |spotless| means that no unusual messages were
printed; |harmless_message| means that a message of possible interest
was printed but no serious errors were detected; |error_message| means that
at least one error was found; |fatal_message| means that the program
terminated abnormally. The value of |history| does not influence the
behavior of the program; it is simply computed for the convenience
of systems that might want to use such information.

@d spotless=0 {|history| value for normal jobs}
@d harmless_message=1 {|history| value when non-serious info was printed}
@d error_message=2 {|history| value when an error was noted}
@d fatal_message=3 {|history| value when we had to stop prematurely}
@#
@d mark_harmless==@t@>@+if history=spotless then history:=harmless_message
@d mark_error==history:=error_message
@d mark_fatal==history:=fatal_message

@<Glob...@>=@!history:spotless..fatal_message; {how bad was this run?}

@ @<Set init...@>=history:=spotless;

@ If an input (\.{DVI}, \.{TFM}, \.{VF}, or other) file is badly malformed,
the whole process must be aborted; \.{\title} will give up, after issuing
an error message about what caused the error. These messages will, however,
in most cases just indicate which input file caused the error. One of the
programs \.{DVItype}, \.{TFtoPL} or \.{VFtoVP} should then be used to
diagnose the error in full detail.

Such errors might be discovered inside of subroutines inside of subroutines,
so a procedure called |jump_out| has been introduced. This procedure, which
transfers control to the label |final_end| at the end of the program,
contains the only non-local |@!goto| statement in \.{\title}.
@^system dependencies@>
Some \PASCAL\ compilers do not implement non-local |goto| statements. In
such cases the |goto final_end| in |jump_out| should simply be replaced
by a call on some system procedure that quietly terminates the program.
@^system dependencies@>

@d abort(#)==begin print_ln(' ',#,'.'); jump_out;
    end

@<Error handling...@>=
@<Basic printing procedures@>@;
procedure close_files_and_terminate; forward;
@#
procedure jump_out;
begin mark_fatal; close_files_and_terminate;
goto final_end;
end;

@ Sometimes the program's behavior is far different from what it should
be, and \.{\title} prints an error message that is really for the
\.{\title} maintenance person, not the user. In such cases the program
says |confusion(|indication of where we are|)|.

@<Error handling...@>=
procedure confusion(@!p:pckt_pointer);
begin print(' !This can''t happen ('); print_packet(p); print_ln(').');
@.This can't happen@>
jump_out;
end;

@ An overflow stop occurs if \.{\title}'s tables aren't large enough.

@<Error handling...@>=
procedure overflow(@!p:pckt_pointer;@!n:int_16u);
begin print(' !Sorry, ',title,' capacity exceeded ['); print_packet(p);
@.Sorry, {\title} capacity exceeded@>
print_ln('=',n:1,'].');
jump_out;
end;

@* Binary data and binary files.
A detailed description of the \.{DVI} file format can be found in the
documentation of \TeX, \.{DVItype}, or \.{GFtoDVI}; here we just define
symbolic names for some of the \.{DVI} command bytes.

@d set_char_0=0 {typeset character 0 and move right}
@d set1=128 {typeset a character and move right}
@d set_rule=132 {typeset a rule and move right}
@d put1=133 {typeset a character}
@d put_rule=137 {typeset a rule}
@d nop=138 {no operation}
@d bop=139 {beginning of page}
@d eop=140 {ending of page}
@d push=141 {save the current positions}
@d pop=142 {restore previous positions}
@d right1=143 {move right}
@d w0=147 {move right by |w|}
@d w1=148 {move right and set |w|}
@d x0=152 {move right by |x|}
@d x1=153 {move right and set |x|}
@d down1=157 {move down}
@d y0=161 {move down by |y|}
@d y1=162 {move down and set |y|}
@d z0=166 {move down by |z|}
@d z1=167 {move down and set |z|}
@d fnt_num_0=171 {set current font to 0}
@d fnt1=235 {set current font}
@d xxx1=239 {extension to \.{DVI} primitives}
@d xxx4=242 {potentially long extension to \.{DVI} primitives}
@d fnt_def1=243 {define the meaning of a font number}
@d pre=247 {preamble}
@d post=248 {postamble beginning}
@d post_post=249 {postamble ending}
@#
@d dvi_id=2 {identifies \.{DVI} files}
@d dvi_pad=223 {pad bytes at end of \.{DVI} file}

@ A \.{DVI}, \.{VF}, or \.{TFM} file is a sequence of 8-bit bytes.
The bytes appear physically in what is called a `|packed file of 0..255|'
in \PASCAL\ lingo. One, two, three, or four consecutive bytes are often
interpreted as (signed or unsigned) integers.
We might as well define the corresponding data types.
@!@^system dependencies@>

@<Types...@>=
@!signed_byte=-@"80..@"7F; {signed one-byte quantity}
@!eight_bits=0..@"FF; {unsigned one-byte quantity}
@!signed_pair=-@"8000..@"7FFF; {signed two-byte quantity}
@!sixteen_bits=0..@"FFFF; {unsigned two-byte quantity}
@!signed_trio=-@"800000..@"7FFFFF; {signed three-byte quantity}
@!twentyfour_bits=0..@"FFFFFF; {unsigned three-byte quantity}
@!signed_quad=int_32; {signed four-byte quantity}

@ Packing is system dependent, and many \PASCAL\ systems fail to implement
such files in a sensible way (at least, from the viewpoint of producing
good production software).  For example, some systems treat all
byte-oriented files as text, looking for end-of-line marks and such
things. Therefore some system-dependent code is often needed to deal with
binary files, even though most of the program in this section of
\.{\title} is written in standard \PASCAL.
@^system dependencies@>

One common way to solve the problem is to consider files of |integer|
numbers, and to convert an integer in the range $-2^{31}\L x<2^{31}$ to
a sequence of four bytes $(a,b,c,d)$ using the following code, which
avoids the controversial integer division of negative numbers:
$$\vbox{\halign{#\hfil\cr
|if x>=0 then a:=x div @'100000000|\cr
|else begin x:=(x+@'10000000000)+@'10000000000; a:=x div @'100000000+128;|\cr
\quad|end|\cr
|x:=x mod @'100000000;|\cr
|b:=x div @'200000; x:=x mod @'200000;|\cr
|c:=x div @'400; d:=x mod @'400;|\cr}}$$
The four bytes are then kept in a buffer and output one by one. (On 36-bit
computers, an additional division by 16 is necessary at the beginning.
Another way to separate an integer into four bytes is to use/abuse
\PASCAL's variant records, storing an integer and retrieving bytes that are
packed in the same place; {\sl caveat implementor!\/}) It is also desirable
in some cases to read a hundred or so integers at a time, maintaining a
larger buffer.

@ We shall stick to simple \PASCAL\ in the standard version of this program,
for reasons of clarity, even if such simplicity is sometimes unrealistic.

@<Types...@>=
@!byte_file=packed file of eight_bits; {files that contain binary data}

@ For some operating systems it may be convenient or even necessary to
close the input files.

@d close_in(#)==do_nothing {close an input file}

@ Character packets extracted from \.{VF} files will be stored in a large
array |byte_mem|. Other packets of bytes, e.g., character packets
extracted from a \.{GF} or \.{PK} or \.{PXL} file could be stored in the
same way. A `|pckt_pointer|' variable, which signifies a packet,
is an index into another array |pckt_start|. The actual sequence of bytes
in the packet pointed to by |p| appears in positions |pckt_start[p]| to
|pckt_start[p+1]-1|, inclusive, in |byte_mem|.

Packets will also be used to store sequences of |ASCII_code|s; in this
respect the |byte_mem| array is very similar to \TeX's string pool and
part of the following code has, in fact, been copied more or less
verbatim from \TeX.

In other respects the packets resemble the identifiers used by
\.{TANGLE} and \.{WEAVE} (also stored in an array called |byte_mem|)
since there is, in general, at most one packet with a given contents;
thus part of the code below has been adapted from the corresponding code
in these programs.

Some \PASCAL\ compilers won't pack integers into a single byte unless the
integers lie in the range |-128..127|. To accommodate such systems we
access the array |byte_mem| only via macros that can easily be redefined.
@^system dependencies@>

@d bi(#) == # {convert from |eight_bits| to |packed_byte|}
@d bo(#) == # {convert from |packed_byte| to |eight_bits|}

@<Types...@>=
@!packed_byte = eight_bits; {elements of |byte_mem| array}
@!byte_pointer = 0..max_bytes; {an index into |byte_mem|}
@!pckt_pointer = 0..max_packets; {an index into |pckt_start|}

@ The global variable |byte_ptr| points to the first unused location in
|byte_mem| and |pckt_ptr| points to the first unused location in
|pckt_start|.

@<Globals...@>=
@!byte_mem: packed array [byte_pointer] of packed_byte; {bytes of packets}
@!pckt_start: array [pckt_pointer] of byte_pointer;
  {directory into |byte_mem|}
@!byte_ptr: byte_pointer;
@!pckt_ptr: pckt_pointer;

@ Several of the elementary operations with packets are performed using
\.{WEB} macros instead of \PASCAL\ procedures, because many of the
operations are done quite frequently and we want to avoid the
overhead of procedure calls. For example, here is
a simple macro that computes the length of a packet.
@.WEB@>

@d pckt_length(#)==(pckt_start[#+1]-pckt_start[#]) {the number of bytes
  in packet number \#}

@ Packets are created by appending bytes to |byte_mem|.
The |append_byte| macro, defined here, does not check to see if the
value of |byte_ptr| has gotten too high; this test is supposed to be
made before |append_byte| is used. There is also a |flush_byte|
macro, which erases the last byte appended.

To test if there is room to append |l| more bytes to |byte_mem|,
we shall write |pckt_room(l)|, which aborts \.{\title} and gives an
apologetic error message if there isn't enough room.

@d append_byte(#) == {put byte \# at the end of |byte_mem|}
begin byte_mem[byte_ptr]:=bi(#); incr(byte_ptr);
end
@d flush_byte == decr(byte_ptr) {forget the last byte in |byte_mem|}
@d pckt_room(#) == {make sure that |byte_mem| hasn't overflowed}
  if max_bytes-byte_ptr<# then overflow(str_bytes,max_bytes)
@#
@d append_one(#) ==
begin pckt_room(1); append_byte(#);
end

@ The length of the current packet is called |cur_pckt_length|:

@d cur_pckt_length == (byte_ptr - pckt_start[pckt_ptr])

@ Once a sequence of bytes has been appended to |byte_mem|, it
officially becomes a packet when the |make_packet| function is called.
This function returns as its value the identification number of either
an existing packet with the same contents or, if no such packet exists,
of the new packet. Thus two packets have the same contents if and only
if they have the same identification number. In order to locate the
packet with a given contents, or to find out that no such packet exists,
we need a hash table. The hash table is kept by the method of simple
chaining, where the heads of the individual lists appear in the |p_hash|
array. If |h| is a hash code, the hash table list starts at |p_hash[h]|
and proceeds through |p_link| pointers.

@d hash_size=353 {should be prime, must be |>256|}

@<Types...@>=
@!hash_code=0..hash_size;

@ @<Glob...@>=
@!p_link:array[pckt_pointer] of pckt_pointer; {hash table}
@!p_hash:array[hash_code] of pckt_pointer;

@ Initially |byte_mem| and all the hash lists are empty; |empty_packet|
is the empty packet.

@d empty_packet=0 {the empty packet}
@d invalid_packet==max_packets {used when there is no packet}

@<Set init...@>=
pckt_ptr:=1; byte_ptr:=1;
pckt_start[0]:=1; pckt_start[1]:=1;
for h:=0 to hash_size-1 do p_hash[h]:=0;

@ @<Local variables for init...@>=
@!h:hash_code; {index into hash-head arrays}

@ Here now is the |make_packet| function used to create packets (and
strings).

@p function make_packet:pckt_pointer;
label found;
var i,@!k:byte_pointer; {indices into |byte_mem|}
@!h:hash_code; {hash code}
@!s,@!l:byte_pointer; {start and length of the given packet}
@!p:pckt_pointer; {where the packet is being sought}
begin s:=pckt_start[pckt_ptr]; l:=byte_ptr-s; {compute start and length}
if l=0 then p:=empty_packet
else  begin @<Compute the packet hash code |h|@>;
  @<Compute the packet location |p|@>;
  if pckt_ptr=max_packets then overflow(str_packets,max_packets);
  incr(pckt_ptr); pckt_start[pckt_ptr]:=byte_ptr;
  end;
found:make_packet:=p;
end;

@ A simple hash code is used: If the sequence of bytes is
$b_1b_2\ldots b_n$, its hash value will be
$$(2^{n-1}b_1+2^{n-2}b_2+\cdots+b_n)\,\bmod\,|hash_size|.$$

@<Compute the packet hash...@>=
h:=bo(byte_mem[s]); i:=s+1;
while i<byte_ptr do
  begin h:=(h+h+bo(byte_mem[i])) mod hash_size; incr(i);
  end

@ If the packet is new, it will be placed in position |p=pckt_ptr|,
otherwise |p| will point to its existing location.

@<Compute the packet location...@>=
p:=p_hash[h];
while p<>0 do
  begin if pckt_length(p)=l then
      @<Compare packet |p| with current packet, |goto found| if equal@>;
  p:=p_link[p];
  end;
p:=pckt_ptr; {the current packet is new}
p_link[p]:=p_hash[h]; p_hash[h]:=p {insert |p| at beginning of hash list}

@ @<Compare packet |p|...@>=
begin i:=s; k:=pckt_start[p];
while (i<byte_ptr)and(byte_mem[i]=byte_mem[k]) do
  begin incr(i); incr(k);
  end;
if i=byte_ptr then {all bytes agree}
  begin byte_ptr:=pckt_start[pckt_ptr]; goto found;
  end;
end

@ Some packets are initialized with predefined strings of |ASCII_code|s;
a few macros permit us to do the initialization with a compact program.
Since this initialization is done when |byte_mem| is still empty, and
since |byte_mem| is supposed to be large enough for all the predefined
strings, |pckt_room| is used only if we are debugging.

@d pid0(#)==#:=make_packet
@d pid1(#)==byte_mem[byte_ptr-1]:=bi(#); pid0
@d pid2(#)==byte_mem[byte_ptr-2]:=bi(#); pid1
@d pid3(#)==byte_mem[byte_ptr-3]:=bi(#); pid2
@d pid4(#)==byte_mem[byte_ptr-4]:=bi(#); pid3
@d pid5(#)==byte_mem[byte_ptr-5]:=bi(#); pid4
@d pid6(#)==byte_mem[byte_ptr-6]:=bi(#); pid5
@d pid7(#)==byte_mem[byte_ptr-7]:=bi(#); pid6
@d pid8(#)==byte_mem[byte_ptr-8]:=bi(#); pid7
@d pid9(#)==byte_mem[byte_ptr-9]:=bi(#); pid8
@d pid10(#)==byte_mem[byte_ptr-10]:=bi(#); pid9
@#
@d pid_init(#)==
  @!debug pckt_room(#); @+ gubed @;
  Incr(byte_ptr)(#)
@#
@d id1==pid_init(1); pid1
@d id2==pid_init(2); pid2
@d id3==pid_init(3); pid3
@d id4==pid_init(4); pid4
@d id5==pid_init(5); pid5
@d id6==pid_init(6); pid6
@d id7==pid_init(7); pid7
@d id8==pid_init(8); pid8
@d id9==pid_init(9); pid9
@d id10==pid_init(10); pid10

@ Here we initialize some strings used as argument of the |overflow| and
|confusion| procedures.

@<Initialize predefined strings@>=
id5("f")("o")("n")("t")("s")(str_fonts);
id5("c")("h")("a")("r")("s")(str_chars);
id6("w")("i")("d")("t")("h")("s")(str_widths);
id7("p")("a")("c")("k")("e")("t")("s")(str_packets);
id5("b")("y")("t")("e")("s")(str_bytes);
id9("r")("e")("c")("u")("r")("s")("i")("o")("n")(str_recursion);
id5("s")("t")("a")("c")("k")(str_stack);
id10("n")("a")("m")("e")("l")("e")("n")("g")("t")("h")(str_name_length);

@ @<Glob...@>=
@!str_fonts,@!str_chars,@!str_widths,@!str_packets,@!str_bytes,
@!str_recursion,@!str_stack,@!str_name_length:pckt_pointer;

@ Some packets, e.g., the preamble comments of \.{DVI} and \.{VF} files,
are needed only temporarily. In such cases |new_packet| is used to
create a packet (which might duplicate an existing packet) and
|flush_packet| is used to discard it; the calls to |new_packet| and
|flush_packet| must occur in balanced pairs, without any intervening
calls to |make_packet|.

@p function new_packet: pckt_pointer;
begin if pckt_ptr=max_packets then overflow(str_packets,max_packets);
new_packet:=pckt_ptr; incr(pckt_ptr); pckt_start[pckt_ptr]:=byte_ptr;
end;
@#
procedure flush_packet;
begin decr(pckt_ptr); byte_ptr:=pckt_start[pckt_ptr];
end;

@ The |print_packet| procedure prints the contents of a packet; such a
packets should, of course, consists of a sequence of |ASCII_code|s.

@<Basic printing...@>=
procedure print_packet(p:pckt_pointer);
var k:byte_pointer;
begin for k:=pckt_start[p] to pckt_start[p+1]-1 do
  print(xchr[bo(byte_mem[k])]);
end;

@ When we interpret a packet we will use two (global or local) variables:
|cur_loc| will point to the byte to be used next, and |cur_limit| will
point to the start of the next packet. The macro |pckt_extract| will be
used to extract one byte; it should, however, never be used with
|cur_loc>=cur_limit|.

@d pckt_extract(#) ==
@!debug if cur_loc>=cur_limit then confusion(str_packets) @+ else @/
gubed @;
  begin #:=bo(byte_mem[cur_loc]); incr(cur_loc); @+ end

@<Globals...@>=
@!cur_pckt: pckt_pointer; {the current packet}
@!cur_loc: byte_pointer; {current location in a packet}
@!cur_limit: byte_pointer; {start of next packet}

@ We will need routines to extract one, two, three, or four bytes from
|byte_mem|, from the \.{DVI} file, or from a \.{VF} file and assemble
them into (signed or unsigned) integers and these routines should be
optimized for efficiency. Here we define \.{WEB} macros to be used for
the body of these routines; thus the changes for system dependent
optimization have to be applied only once.
@^system dependencies@>
@^optimization@>

In addition we demonstrates how these macros can be used to define
functions that extract one, two, three, or four bytes from a character
packet and assemble them into signed or unsigned integers (assuming that
|cur_loc| and |cur_limit| are initialized suitably).

@d begin_byte(#) ==
var a:eight_bits;
begin #(a)
@d comp_sbyte(#) == if a<128 then #:=a @+ else #:=a-256
@d comp_ubyte(#) == #:=a
@f begin_byte == begin

@p function pckt_sbyte:int_8; {returns the next byte, signed}
@!begin_byte(pckt_extract); comp_sbyte(pckt_sbyte);
end;
@#
function pckt_ubyte:int_8u; {returns the next byte, unsigned}
@!begin_byte(pckt_extract); comp_ubyte(pckt_ubyte);
end;

@ @d begin_pair(#) ==
var a,@!b:eight_bits;
begin #(a); #(b)
@d comp_spair(#) == if a<128 then #:=a*256+b @+ else #:=(a-256)*256+b
@d comp_upair(#) == #:=a*256+b
@f begin_pair == begin

@p function pckt_spair:int_16; {returns the next two bytes, signed}
@!begin_pair(pckt_extract); comp_spair(pckt_spair);
end;
@#
function pckt_upair:int_16u; {returns the next two bytes, unsigned}
@!begin_pair(pckt_extract); comp_upair(pckt_upair);
end;

@ @d begin_trio(#) ==
var a,@!b,@!c:eight_bits;
begin #(a); #(b); #(c)
@d comp_strio(#) ==
if a<128 then #:=(a*256+b)*256+c @+ else #:=((a-256)*256+b)*256+c
@d comp_utrio(#) == #:=(a*256+b)*256+c
@f begin_trio == begin

@p function pckt_strio:int_24; {returns the next three bytes, signed}
@!begin_trio(pckt_extract); comp_strio(pckt_strio);
end;
@#
function pckt_utrio:int_24u; {returns the next three bytes, unsigned}
@!begin_trio(pckt_extract); comp_utrio(pckt_utrio);
end;

@ @d begin_quad(#) ==
var a,@!b,@!c,@!d:eight_bits;
begin #(a); #(b); #(c); #(d)
@d comp_squad(#) ==
if a<128 then #:=((a*256+b)*256+c)*256+d
else #:=(((a-256)*256+b)*256+c)*256+d
@f begin_quad == begin

@p function pckt_squad:int_32; {returns the next four bytes, signed}
@!begin_quad(pckt_extract); comp_squad(pckt_squad);
end;

@ A similar set of routines is needed for the inverse task of
decomposing a \.{DVI} command into a sequence of bytes to be appended
to |byte_mem| or, in the case of \.{DVIcopy}, to be written to the
output file. Again we define \.{WEB} macros to be used for the body
of these routines; thus the changes for system dependent optimization
have to be applied only once.
@^system dependencies@>
@^optimization@>

First, the |pckt_one| outputs one byte, negative values are represented
in two's complement notation.

@d begin_one == begin
@d comp_one(#) ==
if x<0 then Incr(x)(256);
#(x)
@f begin_one == begin

@p @!device
procedure pckt_one(@!x:int_32); {output one byte}
@!begin_one; pckt_room(1); comp_one(append_byte);
end;
ecived

@ The |pckt_two| outputs two bytes, negative values are represented in
two's complement notation.

@d begin_two == begin
@d comp_two(#) ==
if x<0 then Incr(x)(@"10000);
#(x div @"100); #(x mod @"100)
@f begin_two == begin

@p @!device
procedure pckt_two(@!x:int_32); {output two byte}
@!begin_two; pckt_room(2); comp_two(append_byte);
end;
ecived

@ The |pckt_four| procedure outputs four bytes in two's complement
notation, without risking arithmetic overflow.

@d begin_four == begin
@d comp_four(#) ==
if x>=0 then #(x div @"1000000)
else  begin Incr(x)(@"40000000); Incr(x)(@"40000000);
  #((x div @"1000000) + 128);
  end;
x:=x mod @"1000000; #(x div @"10000);
x:=x mod @"10000; #(x div @"100);
#(x mod @"100)
@f begin_four == begin

@p procedure pckt_four(@!x:int_32); {output four bytes}
@!begin_four; pckt_room(4); comp_four(append_byte);
end;

@ Next, the |pckt_char| procedure outputs a |set_char| or \\{set} command
or, if |upd=false|, a |put| command.

@d begin_char ==
var o:eight_bits; {|set1| or |put1|}
begin
@d comp_char(#) ==
if (res>=0) and (res<@"100) then begin
  if (not upd)or(res>127)or(ext<>0) then
    begin o:=dvi_char_cmd[upd]; {|set1| or |put1|}
    if ext<0 then Incr(ext)(@"1000000);
    if ext=0 then #(o) @+ else @;
      begin if ext<@"100 then #(o+1) @+ else @;
        begin if ext<@"10000 then #(o+2) @+ else @;
          begin #(o+3); #(ext div @"10000); ext:=ext mod @"10000;
          end;
        #(ext div @"100); ext:=ext mod @"100;
        end;
      #(ext);
      end;
    end;
  #(res)
  end
else
  begin if (res>=0) and (res<@"10000) then #(o+1) @+ else @;
    begin if (res>=0) and (res<@"1000000) then #(o+2) @+ else @;
      begin #(o+3);
        if res>=0 then #(res div @"1000000)
        else begin Incr(res)(@"40000000); Incr(res)(@"40000000);
          #((res div @"1000000) + 128); res:=res mod @"1000000;
          end;
        res:=res mod @"1000000
      end;
      #(res div @"10000); res:= res mod @"10000
    end;
  #(res div @"100); res:=res mod @"100; #(res)
  end

@f begin_char == begin

@p procedure pckt_char(@!upd:boolean;@!ext:int_32;@!res:int_32);
  {output \\{set} or |put|}
@!begin_char; pckt_room(5); comp_char(append_byte);
end;

@ Then, the |pckt_unsigned| procedure outputs a |fnt| or |xxx|
command with its first parameter (normally unsigned); a |fnt| command
is converted into |fnt_num| whenever this is possible.

@d begin_unsigned == begin
@d comp_unsigned(#) ==
if (x<@"100)and(x>=0) then
  if (o=fnt1)and(x<64) then Incr(x)(fnt_num_0) @+ else #(o)
else
  begin if (x<@"10000)and(x>=0) then #(o+1) @+ else @;
    begin if (x<@"1000000)and(x>=0) then #(o+2) @+ else @;
      begin #(o+3);
      if x>=0 then #(x div @"1000000)
      else  begin Incr(x)(@"40000000); Incr(x)(@"40000000);
        #((x div @"1000000) + 128);
        end;
      x:=x mod @"1000000;
      end;
    #(x div @"10000); x:=x mod @"10000;
    end;
  #(x div @"100); x:=x mod @"100;
  end;
#(x)
@f begin_unsigned == begin

@p procedure pckt_unsigned(@!o:eight_bits;@!x:int_32);
  {output |fnt_num|, |fnt|, or |xxx|}
@!begin_unsigned; pckt_room(5); comp_unsigned(append_byte);
end;

@ Finally, the |pckt_signed| procedure outputs a movement (|right|, |w|,
|x|, |down|, |y|, or |z|) command with its (signed) parameter.

@d begin_signed ==
var xx:int_31; {`absolute value' of |x|}
begin
@d comp_signed(#) ==
if x>=0 then xx:=x @+ else xx:=-(x+1);
if xx<@"80 then
  begin #(o); @+ if x<0 then Incr(x)(@"100); @+ end
else  begin if xx<@"8000 then
    begin #(o+1); @+ if x<0 then Incr(x)(@"10000); @+ end
  else  begin if xx<@"800000 then
      begin #(o+2); @+ if x<0 then Incr(x)(@"1000000); @+ end
    else  begin #(o+3);
      if x>=0 then #(x div @"1000000)
      else  begin x:=@"7FFFFFFF-xx; #((x div @"1000000) + 128); @+ end;
      x:=x mod @"1000000;
      end;
    #(x div @"10000); x:=x mod @"10000;
    end;
  #(x div @"100); x:=x mod @"100;
  end;
#(x)
@f begin_signed == begin

@p procedure pckt_signed(@!o:eight_bits;@!x:int_32);
  {output |right|, |w|, |x|, |down|, |y|, or |z|}
@!begin_signed; pckt_room(5); comp_signed(append_byte);
end;

@ The |hex_packet| procedure prints the contents of a packet in
hexadecimal form.

@<Basic printing...@>=
@!debug procedure hex_packet(@!p:pckt_pointer); {prints a packet in hex}
var j,@!k,@!l:byte_pointer; {indices into |byte_mem|}
@!d:int_8u;
begin j:=pckt_start[p]-1; k:=pckt_start[p+1]-1;
print_ln(' packet=',p:1,' start=',j+1:1,' length=',k-j:1);
for l:=j+1 to k do
  begin d:=(bo(byte_mem[l])) div 16;
  if d<10 then print(xchr[d+"0"]) @+ else print(xchr[d-10+"A"]);
  d:=(bo(byte_mem[l])) mod 16;
  if d<10 then print(xchr[d+"0"]) @+ else print(xchr[d-10+"A"]);
  if (l=k)or(((l-j) mod 16)=0) then new_line
  else if ((l-j) mod 4)=0 then print('  ')
  else print(' ');
  end;
end;
gubed

@* File names.
The structure of file names is different for different systems; therefore
this part of the program will, in most cases, require system dependent
modifications. Here we assume that a file name consists of three parts:
an area or directory specifying where the file can be found, a name
proper and an extension; \.{\title} assumes that these three parts appear
in order stated above but this need not be true in all cases.

The font names extracted from \.{DVI} and \.{VF} files consist of an area
part and a name proper; these are stored as packets consisting of the
length of the area part followed by the area and the name proper.
When we print an external font name we simple print the area and the name
contained in the `file name packet' without delimiter between them.
This may need to be modified for some systems.
@^system dependencies@>

@<Basic printing...@>=
procedure print_font(@!f:font_number);
var p:pckt_pointer; {the font name packet}
@!k:byte_pointer; {index into |byte_mem|}
@!m:int_31; {font magnification}
begin print(' = '); p:=font_name(f);
for k:=pckt_start[p]+1 to pckt_start[p+1]-1 do
  print(xchr[bo(byte_mem[k])]);
m:=round((font_scaled(f)/font_design(f))*out_mag);
if m<>1000 then print(' scaled ',m:1);
end;

@ Before a font file can be opened for input we must build a string
with its external name.

@<Glob...@>=
@!cur_name:packed array[1..name_length] of char; {external name,
  with no lower case letters}
@!l_cur_name:int_15; {this many characters are actually relevant in
  |cur_name|}

@ For \.{TFM} and \.{VF} files we just append the apropriate extension
to the file name packet; in addition a system dependent area part
(usually different for \.{TFM} and \.{VF} files) is prepended if
the file name packet contains no area part.
@^system dependencies@>

@d append_to_name(#)==
  if l_cur_name<name_length then
    begin incr(l_cur_name); cur_name[l_cur_name]:=#;
    end
  else overflow(str_name_length,name_length)
@d make_font_name_end(#)==
  append_to_name(#[l]); make_name
@d make_font_name(#)==
  l_cur_name:=0; for l:=1 to # do make_font_name_end

@ For files with character raster data (e.g., \.{GF} or \.{PK} files) the
the extension and\slash or area part will in most cases depend on the
resolution of the output device (corrected for font magnification).
If the special character |res_char| occurs in the extension and\slash or
default area, a character string representing the device resolution will
be substituted.
@^system dependencies@>

@d res_char=='?' {character to be replaced by font resolution}
@d res_ASCII="?" {|xord[res_char]|}
@#
@d append_res_to_name(#)==
  begin c:=#;
  @!device if c=res_char then
    for ll:=n_res_digits downto 1 do append_to_name(res_digits[ll])
  else ecived@;@/
  append_to_name(c);
  end
@d make_font_res_end(#)==
  append_res_to_name(#[l]); make_name
@d make_font_res(#)==
  make_res; l_cur_name:=0; for l:=1 to # do make_font_res_end

@ @<Glob...@>=
@!device
@!f_res:int_16u; {font resolution}
@!res_digits:array [1..5] of char;
@!n_res_digits:int_7; {number of significant characters in |res_digits|}
ecived

@ The |make_res| procedure creates a sequence of characters representing
to the font resolution |f_res|.

@p @!device procedure make_res;
var r:int_16u;
begin n_res_digits:=0; r:=f_res;
repeat incr(n_res_digits);
  res_digits[n_res_digits]:=xchr["0"+(r mod 10)]; r:=r div 10;
until r=0;
end;
ecived

@ The |make_name| procedure used to build the external file name. The
global variable |l_cur_name| contains the length of a default area
which has been copied to |cur_name| before |make_name| is called.
@^system dependencies@>

@p procedure make_name(@!e:pckt_pointer);
var b:eight_bits; {a byte extracted from |byte_mem|}
@!n:pckt_pointer; {file name packet}
@!cur_loc,@!cur_limit:byte_pointer; {indices into |byte_mem|}
@!device
@!ll:int_15; {loop index}
ecived@;@/
@!c:char; {a character to be appended to |cur_name|}
begin n:=font_name(cur_fnt);
cur_loc:=pckt_start[n]; cur_limit:=pckt_start[n+1];
pckt_extract(b); {length of area part}
if b>0 then l_cur_name:=0;
while cur_loc<cur_limit do
  begin pckt_extract(b);
  if (b>="a")and(b<="z") then Decr(b)("a"-"A"); {convert to upper case}
  append_to_name(xchr[b]);
  end;
cur_loc:=pckt_start[e]; cur_limit:=pckt_start[e+1];
while cur_loc<cur_limit do
  begin pckt_extract(b); append_res_to_name(xchr[b]);
  end;
while l_cur_name<name_length do
  begin incr(l_cur_name); cur_name[l_cur_name]:=' ';
  end;
end;

@* Font data.
@ \.{DVI} file format does not include information about character widths, since
that would tend to make the files a lot longer. But a program that reads
a \.{DVI} file is supposed to know the widths of the characters that appear
in \\{set\_char} commands. Therefore \.{\title} looks at the font metric
(\.{TFM}) files for the fonts that are involved.
@.TFM {\rm files}@>
@.OFM {\rm files}@>

The character-width data appears also in other files (e.g., in \.{VF} files
or in \.{GF} and \.{PK} files that specify bit patterns for digitized
characters); thus, it is usually possible for \.{DVI} reading programs
to get by with accessing only one file per font. For \.{VF} reading
programs there is, however, a problem: (1)~when reading the character
packets from a \.{VF} file the \.{TFM} width for its local fonts should
be known in order to analyze and optimize the packets (e.g., determine
if a packet must indeed be enclosed with |push| and |pop| as implied by
the \.{VF} format); and (2)~ in order to avoid infinite recursion such
programs must not try to read a \.{VF} file for a font before a
character from that font is actually used. Thus \.{\title} reads the
\.{TFM} file whenever a new font is encountered and delays the decision
whether this is a virtual font or not.

@ First of all we need to know for each font~|f| such things as its
external name, design and scaled size, and the approximate size of
inter-word spaces. In addition we need to know the range |bc..ec| of
valid characters for this font, and for each character~|c| in~|f|  we
need to know if this character exists and if so what is the width of~|c|.
Depending on the font type of~|f| we may want to know a few other things
about character~|c| in~|f| such as the character packet from a \.{VF}
file or the raster data from a \.{PK} file.
@^font types@>

In \.{\title} we want to be able to handle the full range
|@t$-2^{31}$@><=c<@t$2^{31}$@>| of character codes; each character code
is decomposed into a character residue |0<=res<256| and character
extension |@t$-2^{23}$@><=ext<@t$2^{23}$@>| such that |c=256*ext+res|.
At present \.{VFtoVP}, \.{VPtoVF}, and the standard version of \TeX\ use
only characters in the range |0<=c<256| (i.e., |ext=0|), there are,
however, extensions of \TeX\ which use characters with |ext<>0|.
In any case characters with |ext<>0| will be used rather infrequently
and we want to handle this possibility without too much overhead.

Some of the data for each character~|c| depend only on its residue:
first of all its width and escapement; others, such as \.{VF} packets or
raster data will also depend on its extension. The later will be stored
as packets in |byte_mem|, and the packets for characters with the same
residue but different extension will be chained.

Thus we have to maintain several variables for each character
residue~|bc<=res<=ec| from each font~|f|; we store each type of variable
in a large array such that the array index |font_chars(f)+res| points to
the value for characters with residue |res| from font~|f|.

Although \TeX\ was designed to be used with 256 characters, $\Omega$
has no such restrictions.  Therefore when \.{OVF} and \.{OFM} files
are being used, |ext| will remain 0 and |res| will vary over the full
range of values.

@ Quite often a particular width value is shared by several characters in
a font or even by characters from different fonts; the later will
probably occur in particular for virtual fonts and the local fonts used
by them. Thus the array |widths| is used to store all different \.{TFM}
width values of all legal characters in all fonts; a variable of type
|width_pointer| is an index into |widths| or is zero if a characters does
not exist.

In order to locate a given width value we use again a hash
table with simple chaining; this time the heads of the individual lists
appear in the |w_hash| array and the lists proceed through |w_link|
pointers.

@<Types...@>=
@!width_pointer=0..max_widths; {an index into |widths|}

@ @<Glob...@>=
@!widths:array[width_pointer] of int_32; {the different width values}
@!w_link:array[width_pointer] of width_pointer; {hash table}
@!w_hash:array[hash_code] of width_pointer;
@!n_widths:width_pointer; {first unoccupied position in |widths|}

@ Initially the |widths| array and all the hash lists are empty, except
for one entry: the width value zero; in addition we set |widths[0]:=0|.

@d invalid_width=0 {width pointer for invalid characters}
@d zero_width=1 {a width pointer to the value zero}

@<Set init...@>=
w_hash[0]:=1; w_link[1]:=0; widths[0]:=0; widths[1]:=0; n_widths:=2;
for h:=1 to hash_size-1 do w_hash[h]:=0;

@ The |make_width| function returns an index into |widths| and, if
necessary, adds a new width value; thus two characters will have the
same |width_pointer| if and only if their widths agree.

@p function make_width(@!w:int_32):width_pointer;
label found;
var h:hash_code; {hash code}
@!p:width_pointer; {where the identifier is being sought}
@!x:int_16; {intermediate value}
begin widths[n_widths]:=w;
@<Compute the width hash code |h|@>;
@<Compute the width location |p|, |goto| found unless the value is new@>;
if n_widths=max_widths then overflow(str_widths,max_widths);
incr(n_widths);
found:make_width:=p;
end;

@ A simple hash code is used: If the width value consists of the four
bytes $b_0b_1b_2b_3$, its hash value will be
$$(8*b_0+4*b_1+2*b_2+b_3)\,\bmod\,|hash_size|.$$

@<Compute the width hash...@>=
if w>=0 then x:=w div @"1000000
else  begin w:=w+@"40000000; w:=w+@"40000000; x:=(w div @"1000000)+@"80;
  end;
w:=w mod @"1000000; x:=x+x+(w div @"10000);
w:=w mod @"10000; x:=x+x+(w div @"100);
h:=(x+x+(w mod @"100)) mod hash_size

@ If the width is new, it has been placed into position |p=n_widths|,
otherwise |p| will point to its existing location.

@<Compute the width location...@>=
p:=w_hash[h];
while p<>0 do
  begin if widths[p]=widths[n_widths] then goto found;
  p:=w_link[p];
  end;
p:=n_widths; {the current width is new}
w_link[p]:=w_hash[h]; w_hash[h]:=p {insert |p| at beginning of hash list}

@ The |char_widths| array is used to store the |width_pointer|s for all
different characters among all fonts.  The |char_packets| array is used
to store the |pckt_pointer|s for all different characters among all
fonts; they can point to character packets from \.{VF} files or, e.g.,
raster packets from \.{PK} files.

@<Types...@>=
@!char_offset=neg_max_chars..max_chars; {|char_pointer| offset for a font}
@!char_pointer=0..max_chars; {index into |char_widths| or similar arrays}

@ @<Glob...@>=
@!char_widths:array[char_pointer] of width_pointer; {width pointers}
@!char_packets:array[char_pointer] of pckt_pointer; {packet pointers}
@!n_chars:char_pointer; {first unused position in |char_widths|}

@ @<Set init...@>=
n_chars:=0;

@ The current number of known fonts is |nf|; each known font has an
internal number |f|, where |0<=f<nf|.  For the moment we need for each
known font:  |font_check|, |font_scaled|, |font_design|, |font_name|,
|font_bc|, |font_ec|, |font_chars|, and |font_type|.  Here |font_scaled|
and |font_design| are measured in \.{DVI} units and |font_chars| is of
type |char_offset|:  the width pointer for character~|c| of the font is
stored in |char_widths[char_offset+c]| (for |font_bc<=c<=font_ec|).
Later on we will need additional information depending on the font type:
\.{VF} or real (\.{GF}, \.{PK} or \.{PXL}).

@<Types...@>=
@!f_type=defined_font..max_font_type; {type of a font}
@!font_number=0..max_fonts;

@ @<Glob...@>=
@!nf:font_number;

@ These data are stored in several arrays and we use \.{WEB} macros
to access the various fields. Thus it would be simple to store the
data in an array of record structures and adapt the \.{WEB} macros
accordingly.

We will say, e.g., |font_name(f)| for the name field of font~|f|, and
|font_width(f)(c)| for the width pointer of character~|c| in font~|f|
and |font_packet(f)(c)| for its character packet (this character
exists provided |font_bc(f)<=c<=font_ec(f)| and
|font_width(f)(c)<>invalid_width|). The actual width of character~|c| in
font~|f| is stored in |widths[font_width(f)(c)]|.

@d font_check(#)==fnt_check[#] {checksum}
@d font_scaled(#)==fnt_scaled[#] {scaled or `at' size}
@d font_design(#)==fnt_design[#] {design size}
@d font_name(#)==fnt_name[#] {area plus name packet}
@d font_bc(#)==fnt_bc[#] {first character}
@d font_ec(#)==fnt_ec[#] {last character}
@d font_chars(#)==fnt_chars[#] {character info offset}
@d font_type(#)==fnt_type[#] {type of this font}
@d font_font(#)==fnt_font[#] {use depends on |font_type|}
@#
@d font_width_end(#)==#]
@d font_width(#)==char_widths[font_chars(#)+font_width_end
@d font_packet(#)==char_packets[font_chars(#)+font_width_end
@d font_extend(#)==fnt_extended[#]

@<Glob...@>=
@!fnt_check:array [font_number] of int_32; {checksum}
@!fnt_scaled:array [font_number] of int_31; {scaled size}
@!fnt_design:array [font_number] of int_31; {design size}
@!device @<Declare device dependent font data arrays@>@; @+ ecived @; @/
@!fnt_name:array [font_number] of pckt_pointer; {pointer to area plus
  name packet}
@!fnt_bc:array [font_number] of int_31; {first character}
@!fnt_ec:array [font_number] of int_31; {last character}
@!fnt_chars:array [font_number] of char_offset; {character info offset}
@!fnt_type:array [font_number] of f_type; {type of font}
@!fnt_font:array [font_number] of font_number; {use depends on |font_type|}
@!fnt_extended:array [font_number] of boolean; {\.{TFM} or \.{OFM} file}

@ @d invalid_font==max_fonts {used when there is no valid font}

@<Set init...@>=
@!device @<Initialize device dependent font data@>@; @+ ecived @;@/
nf:=0;

@ A \.{VF}, or \.{GF}, or \.{PK} file may contain information for
several characters with the same residue but with different extension;
all except the first of the corresponding packets in |byte_mem| will
contain a pointer to the previous one and |font_packet(f)(res)|
identifies the last such packet.

A character packet in |byte_mem| starts with a flag byte
$$\hbox{|flag=@"40*ext_flag+@"20*chain_flag+type_flag|}$$
with |0<=ext_flag<=3|, |0<=chain_flag<=1|, |0<=type_flag<=@"1F|,
followed by |ext_flag| bytes with the character extension for this
packet and, if |chain_flag=1|, by a two byte packet pointer to the
previous packet for the same font and character residue. The actual
character packet follows after these header bytes and the
interpretation of the |type_flag| depends on whether this is a \.{VF}
packet or a packet for raster data.

The empty packet is interpreted as a special case of a packet with
|flag=0|.

@d ext_flag=@"40
@d chain_flag=@"20

@<Types...@>=
@!type_flag=0..chain_flag-1; {the range of values for the |type_flag|}

@ The global variable |cur_fnt| is the internal font number of the
currently selected font, or equals |invalid_font| if no font has
been selected; |cur_res| and |cur_ext| are the residue and extension
part of the current character code. The type of a character packet
located by the |find_packet| function defined below is |cur_type|.
While building a character packet for a character, |pckt_ext| and
|pckt_res| are the extension and residue of this character; |pckt_dup|
indicates whether a packet for this extension exists already.

@<Glob...@>=
@!cur_fnt:font_number; {the currently selected font}
@!cur_ext:int_24; {the current character extension}
@!cur_res:int_32; {the current character residue}
@!cur_type:type_flag; {type of the current character packet}
@!pckt_ext:int_24; {character extension for the current character packet}
@!pckt_res:int_32; {character residue for the current character packet}
@!pckt_dup:boolean; {is there a previous packet for the same extension?}
@!pckt_prev:pckt_pointer; {a previous packet for the same extension}
@!pckt_m_msg,@!pckt_s_msg,@!pckt_d_msg:int_7; {counts for various character
  packet error messages}

@ @<Set init...@>=
cur_fnt:=invalid_font; pckt_m_msg:=0; pckt_s_msg:=0; pckt_d_msg:=0;

@ The |find_packet| functions is used to locate the character packet for
the character with residue~|cur_res| and extension~|cur_ext| from
font~|cur_fnt| and returns |false| if no packet exists for any extension;
otherwise the result is |true| and the global variables |cur_packet|,
|cur_type|, |cur_loc|, and |cur_limit| are initialized. In case none of
the character packets has the correct extension, the last one in the
chain (the one defined first) is used instead and |cur_ext| is changed
accordingly.

@p function find_packet:boolean;
label found,exit;
var p,@!q:pckt_pointer; {current and next packet}
@!f:eight_bits; {a flag byte}
@!e:int_24; {extension for a packet}
begin @<Locate a character packet and |goto found| if found@>;
if font_packet(cur_fnt)(cur_res)=invalid_packet then
  begin if pckt_m_msg<10 then {stop telling after first 10 times}
    begin print_ln('---missing character packet for character ',cur_res:1,
@.missing character packet...@>
      ' font ',cur_fnt:1);
    incr(pckt_m_msg); mark_error;
    if pckt_m_msg=10 then print_ln('---further messages suppressed.');
    end;
  find_packet:=false; return;
  end;
if pckt_s_msg<10 then {stop telling after first 10 times}
  begin print_ln('---substituted character packet with extension ',
@.substituted character packet...@>
    e:1,' instead of ',cur_ext:1,' for character ',cur_res:1,
    ' font ',cur_fnt:1);
  incr(pckt_s_msg); mark_error;
  if pckt_s_msg=10 then print_ln('---further messages suppressed.');
  end;
cur_ext:=e;
found: cur_pckt:=p; cur_type:=f; find_packet:=true;
exit: end;

@ @<Locate a character packet and |goto found| if found@>=
q:=font_packet(cur_fnt)(cur_res);
while q<>invalid_packet do
  begin p:=q; q:=invalid_packet;
  cur_loc:=pckt_start[p]; cur_limit:=pckt_start[p+1];
  if p=empty_packet then
    begin e:=0; f:=0;
    end
  else  begin pckt_extract(f);
    case (f div ext_flag) of
    0: e:=0;
    1: e:=pckt_ubyte;
    2: e:=pckt_upair;
    3: e:=pckt_strio;
    end; {there are no other cases}
    if (f mod ext_flag)>=chain_flag then q:=pckt_upair;
    f:=f mod chain_flag;
    end;
  if e=cur_ext then goto found;
  end

@ The |start_packet| procedure is used to create the header bytes of a
character packet for the character with residue~|cur_res| and
extension~|cur_ext| from font~|cur_fnt|; if a previous such packet
exists, we try to build an exact duplicate, i.e., use the chain field of
that previous packet.

@p procedure start_packet(@!t:type_flag);
label found,not_found;
var p,@!q:pckt_pointer; {current and next packet}
@!f:int_8u; {a flag byte}
@!e:int_32; {extension for a packet}
@!cur_loc: byte_pointer; {current location in a packet}
@!cur_limit: byte_pointer; {start of next packet}
begin @<Locate a character packet and |goto found| if found@>;
q:=font_packet(cur_fnt)(cur_res); pckt_dup:=false; goto not_found;
found: pckt_dup:=true; pckt_prev:=p;
not_found: pckt_ext:=cur_ext; pckt_res:=cur_res; pckt_room(6);
@!debug if byte_ptr<>pckt_start[pckt_ptr] then confusion(str_packets);
gubed @;@/
if q=invalid_packet then f:=t @+ else f:=t+chain_flag;
e:=cur_ext;
if e<0 then Incr(e)(@"1000000);
if e=0 then append_byte(f) @+ else @;
  begin if e<@"100 then append_byte(f+ext_flag) @+ else @;
    begin if e<@"10000 then append_byte(f+ext_flag+ext_flag) @+ else @;
      begin append_byte(f+ext_flag+ext_flag+ext_flag);
      append_byte(e div @"10000); e:=e mod @"10000;
      end;
    append_byte(e div @"100); e:=e mod @"100;
    end;
  append_byte(e);
  end;
if q<>invalid_packet then
  begin append_byte(q div @"100); append_byte(q mod @"100);
  end;
end;

@ The |build_packet| procedure is used to finish a character packet.
If a previous packet for the same character extension exists, the new
one is discarded; if the two packets are identical, as it occasionally
occurs for raster files, this is done without an error message.

@p procedure build_packet;
var k,@!l:byte_pointer; {indices into |byte_mem|}
begin if pckt_dup then
  begin k:=pckt_start[pckt_prev+1]; l:=pckt_start[pckt_ptr];
  if (byte_ptr-l)<>(k-pckt_start[pckt_prev]) then pckt_dup:=false;
  while pckt_dup and(byte_ptr>l) do
    begin flush_byte; decr(k);
    if byte_mem[byte_ptr]<>byte_mem[k] then pckt_dup:=false;
    end;
  if (not pckt_dup)and(pckt_d_msg<10) then {stop telling after first 10 times}
    begin print('---duplicate packet for character ',pckt_res:1);
@.duplicate packet for character...@>
    if pckt_ext<>0 then print('.',pckt_ext:1);
    print_ln(' font ',cur_fnt:1);
    incr(pckt_d_msg); mark_error;
    if pckt_d_msg=10 then print_ln('---further messages suppressed.');
    end;
  byte_ptr:=l;
  end
else font_packet(cur_fnt)(pckt_res):=make_packet;
end;

@* Defining fonts.
A detailed description of the \.{TFM} file format can be found in the
documentation of \TeX, \MF, or \.{TFtoPL}.  In order to read \.{TFM}
files the program uses the binary file variable |tfm_file|.

@<Glob...@>=
@!tfm_file:byte_file; {a \.{TFM} file}
@!tfm_ext:pckt_pointer; {extension for \.{TFM} files}
@!ofm_ext:pckt_pointer; {extension for \.{OFM} files}

@ @<Initialize predefined strings@>=
id4(".")("T")("F")("M")(tfm_ext); {file name extension for \.{TFM} files}
id4(".")("O")("F")("M")(ofm_ext); {file name extension for \.{OFM} files}

@ If no font directory has been specified, \.{\title} is supposed to use
the default \.{TFM} directory, which is a system-dependent place where
the \.{TFM} files for standard fonts are kept.
The string variable |TFM_default_area| contains the name of this area.
@^system dependencies@>

@d TFM_default_area_name=='TeXfonts:' {change this to the correct name}
@d OFM_default_area_name=='TeXfonts:' {change this to the correct name}
@d TFM_default_area_name_length=9 {change this to the correct length}
@d OFM_default_area_name_length=9 {change this to the correct length}

@<Glob...@>=
@!TFM_default_area:packed array[1..TFM_default_area_name_length] of char;
@!OFM_default_area:packed array[1..OFM_default_area_name_length] of char;

@ @<Set init...@>=
TFM_default_area:=TFM_default_area_name;
OFM_default_area:=OFM_default_area_name;

@ If a \.{TFM} file is badly malformed, we say |bad_font|; for a \.{TFM}
file the |bad_tfm| procedure is used to give an error message which
refers the user to \.{TFtoPL} and \.{PLtoTF}, and terminates \.{\title}.

@<Error handling...@>=
procedure bad_tfm;
begin print('Bad TFM or OFM file'); print_font(cur_fnt); print_ln('!');
@.Bad TFM or OFM file@>
abort('Use OFM2OPL/OPL2OFM/TFtoPL/PLtoTF to diagnose and correct the problem');
@.Use OFM2OPL/OPL2OFM/TFtoPL/PLtoTF@>
end;
@#
procedure bad_font;
begin new_line;
case font_type(cur_fnt) of
  defined_font: confusion(str_fonts);
  loaded_font: bad_tfm;
  @<Cases for |bad_font|@>@;@/
  end; {there are no other cases}
end;

@ To prepare |tfm_file| for input we |reset| it.

@<TFM: Open |tfm_file|@>=
make_font_name(TFM_default_area_name_length)(TFM_default_area)(tfm_ext);
reset(tfm_file,cur_name);
if eof(tfm_file) then begin
  make_font_name(OFM_default_area_name_length)(OFM_default_area)(ofm_ext);
  reset(tfm_file,cur_name);
  if eof(tfm_file) then
@^system dependencies@>
    abort('---not loaded, TFM or OFM file can''t be opened!')
  else font_extend(cur_fnt):=true
@.TFM or OFM file can\'t be opened@>
  end
else font_extend(cur_fnt):=false

@ It turns out to be convenient to read four bytes at a time, when we
are inputting from \.{TFM} files. The input goes into global variables
|tfm_b0|, |tfm_b1|, |tfm_b2|, and |tfm_b3|, with |tfm_b0| getting
the first byte and |tfm_b3| the fourth.

@<Glob...@>=
@!tfm_b0,@!tfm_b1,@!tfm_b2,@!tfm_b3: eight_bits; {four bytes input at once}

@ Reading a \.{TFM} file should be done as efficient as possible for a
particular system; on many systems this means that a large number of
bytes from |tfm_file| is read into a buffer and will then be extracted
from that buffer. In order to simplify such system dependent changes
we use the \.{WEB} macro |tfm_byte| to extract the next \.{TFM} or \.{OFM}
byte; this macro and |eof(tfm_file)| are used only in the |read_tfm_word|
procedure which sets |tfm_b0| through |tfm_b3| to the next four bytes
in the current \.{TFM} file. Here we give simple-minded definitions in
terms of standard \PASCAL.
@^system dependencies@>
@^optimization@>

@d tfm_byte(#)==read(tfm_file,#) {read next \.{TFM} byte}

@p procedure read_tfm_word;
begin tfm_byte(tfm_b0); tfm_byte(tfm_b1);
tfm_byte(tfm_b2); tfm_byte(tfm_b3);
if eof(tfm_file) then bad_font;
end;

@ Here are three procedures used to check the consistency of font files:
First, the |check_check_sum| procedure compares two check sum values: a
warning is given if they differ and are both non-zero; if the second
value is not zero it may replace the first one.
Next, the |check_design_size| procedure compares two design size
values: a warning is given if they differ by more than a small amount.
Finally, the |check_width| function compares the character width value
for character |cur_res| read from a \.{VF} or raster file for font
|cur_fnt| with the value previously read from the \.{TFM} file and
returns the width pointer for that value; a warning is given if the two
values differ.

@p procedure check_check_sum(@!c:int_32;@!u:boolean);
  {compare |font_check(cur_fnt)| with |c|}
begin if (c<>font_check(cur_fnt))and(c<>0) then
  begin
  if font_check(cur_fnt)<>0 then
    begin new_line; print_ln('---beware: check sums do not agree!   (',
@.beware: check sums do not agree@>
@.check sums do not agree@>
      c:1,' vs. ',font_check(cur_fnt):1,')');
    mark_harmless;
    end;
  if u then font_check(cur_fnt):=c;
  end;
end;
@#
procedure check_design_size(@!d:int_32);
  {compare |font_design(cur_fnt)| with |d|}
begin if abs(d-font_design(cur_fnt))>2 then
  begin new_line; print_ln('---beware: design sizes do not agree!   (',
@.beware: design sizes do not agree@>
@.design sizes do not agree@>
    d:1,' vs. ',font_design(cur_fnt):1,')');
  mark_error;
  end;
end;
@#
procedure print_hex(@!num:int_31);
var c:int_31;
begin print('"');
c:=num div @"10000000;
if (c<10) then print(xchr[c+'0']) else print(xchr[c-10+'a']);
num:=num mod @"10000000;
c:=num div @"1000000;
if (c<10) then print(xchr[c+'0']) else print(xchr[c-10+'a']);
num:=num mod @"1000000;
c:=num div @"100000;
if (c<10) then print(xchr[c+'0']) else print(xchr[c-10+'a']);
num:=num mod @"100000;
c:=num div @"10000;
if (c<10) then print(xchr[c+'0']) else print(xchr[c-10+'a']);
num:=num mod @"10000;
c:=num div @"1000;
if (c<10) then print(xchr[c+'0']) else print(xchr[c-10+'a']);
num:=num mod @"1000;
c:=num div @"100;
if (c<10) then print(xchr[c+'0']) else print(xchr[c-10+'a']);
num:=num mod @"100;
c:=num div @"10;
if (c<10) then print(xchr[c+'0']) else print(xchr[c-10+'a']);
num:=num mod @"10;
c:=num;
if (c<10) then print(xchr[c+'0']) else print(xchr[c-10+'a']);
end;
@#
function check_width(w:int_32):width_pointer;
  {compare |widths[font_width(cur_fnt)(cur_res)]| with |w|}
var wp:width_pointer; {pointer to \.{TFM} width value}
begin if (cur_res>=font_bc(cur_fnt))and(cur_res<=font_ec(cur_fnt)) then
  wp:=font_width(cur_fnt)(cur_res)
else wp:=invalid_width;
if wp=invalid_width then
  begin print_nl('Bad char ',cur_res:1);
@.Bad char c@>
  if cur_ext<>0 then print('.',cur_ext:1);
  print(' font ',cur_fnt:1); print_font(cur_fnt);
  abort(' (compare TFM or OFM file)');
  end;
if w<>widths[wp] then
  begin
  print_hex(cur_ext);
  print(' ');
  print_hex(cur_res);
  print(': char widths do not agree! (');
@.beware: char widths do not agree@>
@.char widths do not agree@>
  print_hex(w);
  print(' vs. ');
  print_hex(widths[wp]);
  print_ln(')');
  mark_error;
  end;
check_width:=wp;
end;

@ The |load_font| procedure reads the \.{TFM} file for a font and puts
the data extracted into position |cur_fnt| of the font data arrays.

@p procedure load_font; {reads a \.{TFM} file}
var l,j,lprime:int_32; {loop index}
@!p:char_pointer; {index into |char_widths|}
@!q:width_pointer; {index into |widths|}
@!bc,@!ec:int_31; {first and last character in this font}
@!lf:int_31; {length of file in four byte words}
@!lh:int_31; {length of header in four byte words}
@!nw:int_31; {number of words in width table}
@!w:int_32; {a four byte integer}
@!first_two:int_31;
@!ofm_level:int_32;
@!nco,@!extra_words:int_31;
@!tfm_width:int_31;
@<Variables for scaling computation@>@;
begin print('TFM: font ',cur_fnt:1); print_font(cur_fnt);
font_type(cur_fnt):=loaded_font;
@<TFM: Open |tfm_file|@>;
@<TFM: Read past the header data@>;
@<TFM: Store character-width indices@>;
@<TFM: Read and convert the width values@>;
@<TFM: Convert character-width indices to character-width pointers@>;
close_in(tfm_file);
@!device @<Initialize device dependent data for a font@>@; @+ ecived @; @/
d_print(' loaded at ',font_scaled(cur_fnt):1,' DVI units');
print_ln('.');
end;

@ @<Glob...@>=
@!tfm_conv:real; {\.{DVI} units per absolute \.{TFM} unit}

@ We will use the following \.{WEB} macros to construct integers from
two or four of the four bytes read by |read_tfm_word|.
@^system dependencies@>

@d tfm_b03(#)== {|tfm_b0..tfm_b3| as non-negative integer}
if tfm_b0>127 then bad_font
else #:=tfm_b0*@"1000000+tfm_b1*@"10000+tfm_b2*@"100+tfm_b3

@d tfm_b01(#)== {|tfm_b0..tfm_b1| as non-negative integer}
if tfm_b0>127 then bad_font
else #:=tfm_b0*256+tfm_b1
@d tfm_b23(#)== {|tfm_b2..tfm_b3| as non-negative integer}
if tfm_b2>127 then bad_font
else #:=tfm_b2*256+tfm_b3
@d tfm_squad(#)== {|tfm_b0..tfm_b3| as signed integer}
if tfm_b0<128 then #:=((tfm_b0*256+tfm_b1)*256+tfm_b2)*256+tfm_b3
else #:=(((tfm_b0-256)*256+tfm_b1)*256+tfm_b2)*256+tfm_b3
@d tfm_uquad== {|tfm_b0..tfm_b3| as unsigned integer}
(((tfm_b0*256+tfm_b1)*256+tfm_b2)*256+tfm_b3)

@d read_tfm_width(#)==begin
read_tfm_word;
if first_two<>0 then tfm_width:=tfm_b0
else begin
  if # then read_tfm_word;
  tfm_b01(tfm_width);
  end
end

@<TFM: Read past the header data@>=
read_tfm_word; tfm_b01(first_two);
if (first_two<>0) then begin
  ofm_level:=-1;
  tfm_b23(lh);
  read_tfm_word; tfm_b01(bc); tfm_b23(ec);
  if ec<bc then
    begin bc:=1; ec:=0;
    end
  else if ec>255 then bad_font;
  read_tfm_word; tfm_b01(nw);
  if (nw=0)or(nw>256) then bad_font;
  for l:=-2 to lh do
    begin read_tfm_word;
    if l=1 then
      begin tfm_squad(w); check_check_sum(w,true);
      end
    else if l=2 then
      begin if tfm_b0>127 then bad_font;
      check_design_size(round(tfm_conv*tfm_uquad));
      end
    end
  end
else begin
  tfm_b23(ofm_level);
  read_tfm_word; tfm_b03(lf);
  read_tfm_word; tfm_b03(lh);
  read_tfm_word; tfm_b03(bc);
  read_tfm_word; tfm_b03(ec);
  if ec<bc then begin
    bc:=1; ec:=0;
    end
  else if ec>65535 then bad_font;
  read_tfm_word; tfm_b03(nw);
  if (nw=0)or(nw>65536) then bad_font;
  for l:=1 to 8 do
    begin if eof(tfm_file) then bad_font;
    read_tfm_word;
    end;
  if ofm_level=1 then begin
    read_tfm_word;
    nco:=(((tfm_b0*256+tfm_b1)*256+tfm_b2)*256+tfm_b3) div 2;
    read_tfm_word; read_tfm_word;
    extra_words:=(((tfm_b0*256+tfm_b1)*256+tfm_b2)*256+tfm_b3) div 2;
    for l:=1 to 12 do
      begin if eof(tfm_file) then bad_font;
      read_tfm_word;
      end;
    end;
  for l:=1 to lh do begin
    read_tfm_word;
    if l=1 then begin
      tfm_squad(w); check_check_sum(w,true);
      end
    else if l=2 then begin
      if tfm_b0>127 then bad_font;
      check_design_size(round(tfm_conv*tfm_uquad));
      end
    end;
  if ofm_level>0 then
    for l:=1 to (nco-29-lh) do
      read_tfm_word;
  end

@ The width indices for the characters are stored in positions |n_chars|
through |n_chars-bc+ec| of the |char_widths| array; if characters on
either end of the range |bc..ec| do not exist, they are ignored and the
range is adjusted accordingly.

@<TFM: Store character-width indices@>=
if ofm_level<=0 then begin
  read_tfm_width(false);
  while (tfm_width=0)and(bc<=ec) do
    begin incr(bc); read_tfm_width(true);
    end;
  font_bc(cur_fnt):=bc; font_chars(cur_fnt):=n_chars-bc;
  if ec>=max_chars-font_chars(cur_fnt) then overflow(str_chars,max_chars);
  for l:=bc to ec do
    begin char_widths[n_chars]:=tfm_width; incr(n_chars); read_tfm_width(true);
    end;
  while (char_widths[n_chars-1]=0)and(ec>=bc) do
    begin decr(n_chars); decr(ec);
    end;
  font_ec(cur_fnt):=ec
  end
else begin
  font_bc(cur_fnt):=bc; font_chars(cur_fnt):=n_chars-bc;
  if ec>=max_chars-font_chars(cur_fnt) then overflow(str_chars,max_chars);
  font_ec(cur_fnt):=ec;
  l:=bc;
  while l<ec do begin
    read_tfm_word;
    tfm_width:=tfm_b0*256+tfm_b1;
    char_widths[n_chars]:=tfm_width; incr(n_chars);
    read_tfm_word; read_tfm_word;
    lprime:=l+(tfm_b0*256+tfm_b1);
    for j:=l+1 to lprime do begin
      char_widths[n_chars]:=tfm_width; incr(n_chars);
      end;
    l:=lprime;
    for j:=1 to extra_words do
      read_tfm_word;
    end;
    read_tfm_word;
  end

@ The most important part of |load_font| is the width computation, which
involves multiplying the relative widths in the \.{TFM} file by the
scaling factor in the \.{DVI} file. A similar computation is used for
dimensions read from \.{VF} files. This fixed-point multiplication must
be done with precisely the same accuracy by all \.{DVI}-reading programs,
in order to validate the assumptions made by \.{DVI}-writing programs
like \TeX82.

Let us therefore summarize what needs to be done. Each width in a \.{TFM}
file appears as a four-byte quantity called a |fix_word|.  A |fix_word|
whose respective bytes are $(a,b,c,d)$ represents the number
$$x=\left\{\vcenter{\halign{$#$,\hfil\qquad&if $#$\hfil\cr
b\cdot2^{-4}+c\cdot2^{-12}+d\cdot2^{-20}&a=0;\cr
-16+b\cdot2^{-4}+c\cdot2^{-12}+d\cdot2^{-20}&a=255.\cr}}\right.$$
(No other choices of $a$ are allowed, since the magnitude of a \.{TFM}
dimension must be less than 16.)  We want to multiply this quantity by the
integer~|z|, which is known to be less than $2^{27}$.
If $|z|<2^{23}$, the individual multiplications $b\cdot z$, $c\cdot z$,
$d\cdot z$ cannot overflow; otherwise we will divide |z| by 2, 4, 8, or
16, to obtain a multiplier less than $2^{23}$, and we can compensate for
this later. If |z| has thereby been replaced by $|z|^\prime=|z|/2^e$, let
$\beta=2^{4-e}$; we shall compute
$$\lfloor(b+c\cdot2^{-8}+d\cdot2^{-16})\,z^\prime/\beta\rfloor$$ if $a=0$,
or the same quantity minus $\alpha=2^{4+e}z^\prime$ if $a=255$.
This calculation must be done exactly, for the reasons stated above; the
following program does the job in a system-independent way, assuming
that arithmetic is exact on numbers less than $2^{31}$ in magnitude. We
use \.{WEB} macros for various versions of this computation.
@^system dependencies@>
@^optimization@>

@d tfm_fix3u== {convert |tfm_b1..tfm_b3| to an unsigned scaled dimension}
(((((tfm_b3*z)div@'400)+(tfm_b2*z))div@'400)+(tfm_b1*z))div beta
@#
@d tfm_fix4(#)== {convert |tfm_b0..tfm_b3| to a scaled dimension}
  #:=tfm_fix3u;
  if tfm_b0>0 then if tfm_b0=255 then Decr(#)(alpha) else bad_font
@d tfm_fix3(#)== {convert |tfm_b1..tfm_b3| to a scaled dimension}
  #:=tfm_fix3u; @+ if tfm_b1>127 then Decr(#)(alpha)
@d tfm_fix2== {convert |tfm_b2..tfm_b3| to a scaled dimension}
  if tfm_b2>127 then tfm_b1:=255 else tfm_b1:=0;
  tfm_fix3
@d tfm_fix1== {convert |tfm_b3| to a scaled dimension}
  if tfm_b3>127 then tfm_b1:=255 else tfm_b1:=0;
  tfm_b2:=tfm_b1; tfm_fix3

@<Variables for scaling computation@>=
@!z:int_32; {multiplier}
@!alpha:int_32; {correction for negative values}
@!beta:int_15; {divisor}

@ @<Replace |z| by $|z|^\prime$ and compute $\alpha,\beta$@>=
alpha:=16;
while z>=@'40000000 do
  begin z:=z div 2; alpha:=alpha+alpha;
  end;
beta:=256 div alpha; alpha:=alpha*z

@ The first width value, which indicates that a character does not exist
and which must vanish, is converted to |invalid_width|; the other width
values are scaled by |font_scaled(cur_fnt)| and converted to width
pointers by |make_width|. The resulting width pointers are stored
temporarily in the |char_widths| array, following the with indices.

@<TFM: Read and convert the width values@>=
if nw-1>max_chars-n_chars then overflow(str_chars,max_chars);
if (tfm_b0<>0)or(tfm_b1<>0)or(tfm_b2<>0)or(tfm_b3<>0) then bad_font
  else char_widths[n_chars]:=invalid_width;
z:=font_scaled(cur_fnt);
@<Replace |z|...@>;
for p:=n_chars+1 to n_chars+nw-1 do
  begin read_tfm_word; tfm_fix4(w);
  char_widths[p]:=make_width(w);
  end

@ We simply translate the width indices into width pointers. In addition
we initialize the character packets with the invalid packet.

@<TFM: Convert character-width indices to character-width pointers@>=
for p:=font_chars(cur_fnt)+bc to n_chars-1 do
  begin q:=char_widths[n_chars+char_widths[p]]; char_widths[p]:=q;
  char_packets[p]:=invalid_packet;
  end

@ When processing a font definition we put the data extracted from the
\.{DVI} or \.{VF} file into position |nf| of the font data arrays and
call |define_font| to obtain the internal font number for this font.
The parameter |load| is true if the \.{TFM} file should be loaded.

@p function define_font(@!load:boolean):font_number;
var save_fnt:font_number; {used to save |cur_fnt|}
begin save_fnt:=cur_fnt; {save}
cur_fnt:=0;
while (font_name(cur_fnt)<>font_name(nf))or@|
  (font_scaled(cur_fnt)<>font_scaled(nf)) do incr(cur_fnt);
d_print(' => ',cur_fnt:1); print_font(cur_fnt);
if cur_fnt<nf then
  begin check_check_sum(font_check(nf),true);
  check_design_size(font_design(nf));
  @!debug if font_type(cur_fnt)=defined_font then print(' defined')
  else print(' loaded');
  print(' previously');
  gubed@;
  end
else  begin if nf=max_fonts then overflow(str_fonts,max_fonts);
  incr(nf); font_font(cur_fnt):=invalid_font;
  font_type(cur_fnt):=defined_font;
  d_print(' defined');
  end;
print_ln('.');
if load and(font_type(cur_fnt)=defined_font) then load_font;
define_font:=cur_fnt;
cur_fnt:=save_fnt; {restore}
end;

@* Low-level DVI input routines.
The program uses the binary file variable |dvi_file| for its main input
file; |dvi_loc| is the number of the byte about to be read next from
|dvi_file|.

@<Glob...@>=
@!dvi_file:byte_file; {the stuff we are \.{\title}ing}
@!dvi_loc:int_32; {where we are about to look, in |dvi_file|}

@ If the \.{DVI} file is badly malformed, we say |bad_dvi|; this
procedure gives an error message which refers the user to \.{DVItype},
and terminates \.{\title}.

@<Error handling...@>=
procedure bad_dvi;
begin new_line; print_ln('Bad DVI file: loc=',dvi_loc:1,'!');
@.Bad DVI file@>
print(' Use DVItype with output level');
@.Use DVItype@>
if random_reading then print('=4') @+ else print('<4');
abort('to diagnose the problem');
end;

@ To prepare |dvi_file| for input, we |reset| it.

@<Open input file(s)@>=
reset(dvi_file); {prepares to read packed bytes from |dvi_file|}
dvi_loc:=0;

@ Reading the \.{DVI} file should be done as efficient as possible for a
particular system; on many systems this means that a large number of
bytes from |dvi_file| is read into a buffer and will then be extracted
from that buffer. In order to simplify such system dependent changes
we use a pair of \.{WEB} macros: |dvi_byte| extracts the next \.{DVI}
byte and |dvi_eof| is |true| if we have reached the end of the \.{DVI}
file. Here we give simple minded definitions for these macros in terms
of standard \PASCAL.
@^system dependencies@>
@^optimization@>

@d dvi_eof == eof(dvi_file) {has the \.{DVI} file been exhausted?}
@d dvi_byte(#) ==
  if dvi_eof then bad_dvi
  else read(dvi_file,#) {obtain next \.{DVI} byte}

@ Next we come to the routines that are used only if |random_reading|    is
|true|. The driver program below needs two such routines: |dvi_length| should
compute the total number of bytes in |dvi_file|, possibly also
causing |eof(dvi_file)| to be true; and |dvi_move(n)| should position
|dvi_file| so that the next |dvi_byte| will read byte |n|, starting with
|n=0| for the first byte in the file.
@^system dependencies@>

Such routines are, of course, highly system dependent. They are implemented
here in terms of two assumed system routines called |set_pos| and |cur_pos|.
The call |set_pos(f,n)| moves to item |n| in file |f|, unless |n| is
negative or larger than the total number of items in |f|; in the latter
case, |set_pos(f,n)| moves to the end of file |f|.
The call |cur_pos(f)| gives the total number of items in |f|, if
|eof(f)| is true; we use |cur_pos| only in such a situation.

@p function dvi_length:int_32;
begin set_pos(dvi_file,-1); dvi_length:=cur_pos(dvi_file);
end;
@#
procedure dvi_move(@!n:int_32);
begin set_pos(dvi_file,n); dvi_loc:=n;
end;

@ We need seven simple functions to read the next byte or bytes
from |dvi_file|.

@p function dvi_sbyte:int_8; {returns the next byte, signed}
@!begin_byte(dvi_byte); incr(dvi_loc); comp_sbyte(dvi_sbyte);
end;
@#
function dvi_ubyte:int_8u; {returns the next byte, unsigned}
@!begin_byte(dvi_byte); incr(dvi_loc); comp_ubyte(dvi_ubyte);
end;
@#
function dvi_spair:int_16; {returns the next two bytes, signed}
@!begin_pair(dvi_byte); Incr(dvi_loc)(2); comp_spair(dvi_spair);
end;
@#
function dvi_upair:int_16u; {returns the next two bytes, unsigned}
@!begin_pair(dvi_byte); Incr(dvi_loc)(2); comp_upair(dvi_upair);
end;
@#
function dvi_strio:int_24; {returns the next three bytes, signed}
@!begin_trio(dvi_byte); Incr(dvi_loc)(3); comp_strio(dvi_strio);
end;
@#
function dvi_utrio:int_24u; {returns the next three bytes, unsigned}
@!begin_trio(dvi_byte); Incr(dvi_loc)(3); comp_utrio(dvi_utrio);
end;
@#
function dvi_squad:int_32; {returns the next four bytes, signed}
@!begin_quad(dvi_byte); Incr(dvi_loc)(4); comp_squad(dvi_squad);
end;

@ Three other functions are used in cases where a four byte integer
(which is always signed) must have a non-negative value, a positive
value, or is a pointer which must be either positive or |=-1|.

@p function dvi_uquad:int_31; {result must be non-negative}
var x:int_32;
begin x:=dvi_squad; if x<0 then bad_dvi
else dvi_uquad:=x;
end;
@#
function dvi_pquad:int_31; {result must be positive}
var x:int_32;
begin x:=dvi_squad; if x<=0 then bad_dvi
else dvi_pquad:=x;
end;
@#
function dvi_pointer:int_32; {result must be positive or |=-1|}
var x:int_32;
begin x:=dvi_squad; if (x<=0)and(x<>-1) then bad_dvi
else dvi_pointer:=x;
end;

@ Given the structure of the \.{DVI} commands it is fairly obvious
that their interpretation consists of two steps: First zero to four
bytes are read in order to obtain the value of the first parameter
(e.g., zero bytes for |set_char_0|, four bytes for |set4|); then,
depending on the command class, a specific action is performed (e.g.,
typeset a character but don't move the reference point for |put1..put4|).

The \.{DVItype} program uses large case statements for both steps;
unfortunately some \PASCAL\ compilers fail to implement large case
statements efficiently -- in particular those as the one used in the
|first_par| function of \.{DVItype}. Here we use a pair of look up tables:
|dvi_par| determines how to obtain the value of the first parameter, and
|dvi_cl| determines the command class.

A slight complication arises from the fact that we want to decompose the
character code of each character to be typeset into a residue
|0<=char_res<256| and extension: |char_code=char_res+256*char_ext|;
the \.{TFM} widths as well as the pixel widths for a given resolution
are the same for all characters in a font with the same residue.
For \.{OFM} files, |char_res| can have any value and |char_ext=0|.

@d two_cases(#)==#,#+1
@d three_cases(#)==#,#+1,#+2
@d five_cases(#)==#,#+1,#+2,#+3,#+4

@ First we define the values used as array elements of |dvi_par|; we
distinguish between pure numbers and dimensions because dimensions read
from a \.{VF} file must be scaled.

@d char_par=0 {character for \\{set} and |put|}
@d no_par=1 {no parameter}
@d dim1_par=2 {one-byte signed dimension}
@d num1_par=3 {one-byte unsigned number}
@d dim2_par=4 {two-byte signed dimension}
@d num2_par=5 {two-byte unsigned number}
@d dim3_par=6 {three-byte signed dimension}
@d num3_par=7 {three-byte unsigned number}
@d dim4_par=8 {four-byte signed dimension}
@d num4_par=9 {four-byte signed number}
@d numu_par=10 {four-byte non-negative number}
@d rule_par=11 {dimensions for |set_rule| and |put_rule|}
@d fnt_par=12 {font for |fnt_num| commands}
@d max_par=12 {largest possible value}

@<Types...@>=
@!cmd_par=char_par..max_par;

@ Here we declare the array |dvi_par|.

@<Globals...@>=
@!dvi_par:packed array [eight_bits] of cmd_par;

@ And here we initialize it.

@<Set init...@>=
for i:=0 to put1+3 do dvi_par[i]:=char_par;@/
for i:=nop to 255 do dvi_par[i]:=no_par;@/
dvi_par[set_rule]:=rule_par; dvi_par[put_rule]:=rule_par;@/
dvi_par[right1]:=dim1_par; dvi_par[right1+1]:=dim2_par;
dvi_par[right1+2]:=dim3_par; dvi_par[right1+3]:=dim4_par;@/
for i:=fnt_num_0 to fnt_num_0+63 do dvi_par[i]:=fnt_par;@/
dvi_par[fnt1]:=num1_par; dvi_par[fnt1+1]:=num2_par;
dvi_par[fnt1+2]:=num3_par; dvi_par[fnt1+3]:=num4_par;@/
dvi_par[xxx1]:=num1_par; dvi_par[xxx1+1]:=num2_par;
dvi_par[xxx1+2]:=num3_par; dvi_par[xxx1+3]:=numu_par;@/
for i:=0 to 3 do
  begin dvi_par[i+w1]:=dvi_par[i+right1];
  dvi_par[i+x1]:=dvi_par[i+right1];
  dvi_par[i+down1]:=dvi_par[i+right1];
  dvi_par[i+y1]:=dvi_par[i+right1];
  dvi_par[i+z1]:=dvi_par[i+right1];
  dvi_par[i+fnt_def1]:=dvi_par[i+fnt1];
  end;

@ Next we define the values used as array elements of |dvi_cl|;
several \.{DVI} commands (e.g., |nop|, |bop|, |eop|, |pre|, |post|) will
always be treated separately and are therfore assigned to the invalid
class here.

@d char_cl=0
@d rule_cl=char_cl+1
@d xxx_cl=char_cl+2
@d push_cl=3
@d pop_cl=4
@d w0_cl=5
@d x0_cl=w0_cl+1
@d right_cl=w0_cl+2
@d w_cl=w0_cl+3
@d x_cl=w0_cl+4
@d y0_cl=10
@d z0_cl=y0_cl+1
@d down_cl=y0_cl+2
@d y_cl=y0_cl+3
@d z_cl=y0_cl+4
@d fnt_cl=15
@d fnt_def_cl=16
@d invalid_cl=17
@d max_cl=invalid_cl {largest possible value}

@<Types...@>=
@!cmd_cl=char_cl..max_cl;

@ Here we declare the array |dvi_cl|.

@<Globals...@>=
@!dvi_cl:packed array [eight_bits] of cmd_cl;

@ And here we initialize it.

@<Set init...@>=
for i:=set_char_0 to put1+3 do dvi_cl[i]:=char_cl;
dvi_cl[set_rule]:=rule_cl; dvi_cl[put_rule]:=rule_cl;@/
dvi_cl[nop]:=invalid_cl;
dvi_cl[bop]:=invalid_cl; dvi_cl[eop]:=invalid_cl;@/
dvi_cl[push]:=push_cl; dvi_cl[pop]:=pop_cl;@/
dvi_cl[w0]:=w0_cl; dvi_cl[x0]:=x0_cl;@/
dvi_cl[y0]:=y0_cl; dvi_cl[z0]:=z0_cl;@/
for i:=0 to 3 do
  begin dvi_cl[i+right1]:=right_cl;
  dvi_cl[i+w1]:=w_cl;
  dvi_cl[i+x1]:=x_cl;@/
  dvi_cl[i+down1]:=down_cl;
  dvi_cl[i+y1]:=y_cl;
  dvi_cl[i+z1]:=z_cl;@/
  dvi_cl[i+xxx1]:=xxx_cl;
  dvi_cl[i+fnt_def1]:=fnt_def_cl;
  end;
for i:=fnt_num_0 to fnt1+3 do dvi_cl[i]:=fnt_cl;
for i:=pre to 255 do dvi_cl[i]:=invalid_cl;

@ A few small arrays are used to generate \.{DVI} commands.

@<Glob...@>=
@!dvi_char_cmd:array[boolean] of eight_bits; {|put1| and |set1|}
@!dvi_rule_cmd:array[boolean] of eight_bits; {|put_rule| and |set_rule|}
@!dvi_right_cmd:array[right_cl..x_cl] of eight_bits; {|right1|, |w1|, and |x1|}
@!dvi_down_cmd:array[down_cl..z_cl] of eight_bits; {|down1|, |y1|, and |z1|}

@ @<Set init...@>=
dvi_char_cmd[false]:=put1;
dvi_char_cmd[true]:=set1;@/
dvi_rule_cmd[false]:=put_rule;
dvi_rule_cmd[true]:=set_rule;@/
dvi_right_cmd[right_cl]:=right1;
dvi_right_cmd[w_cl]:=w1;
dvi_right_cmd[x_cl]:=x1;@/
dvi_down_cmd[down_cl]:=down1;
dvi_down_cmd[y_cl]:=y1;
dvi_down_cmd[z_cl]:=z1;

@ The global variables |cur_cmd|, |cur_parm| and |cur_class| are used
for the current \.{DVI} command, its first parameter (if any), and its
command class respectively.

@<Glob...@>=
@!cur_cmd:eight_bits; {current \.{DVI} command byte}
@!cur_parm:int_32; {its first parameter (if any)}
@!cur_class:cmd_cl; {its class}

@ When typesetting a character or rule, the boolean variable |cur_upd|
is |true| for \\{set} commands, |false| for |put| commands.

@<Glob...@>=
@!cur_cp:char_pointer; {|char_widths| index for the current character}
@!cur_wp:width_pointer; {width pointer of the current character}
@!cur_upd:boolean; {is this a \\{set} or |set_rule| command ?}
@!cur_v_dimen:int_32; {a vertical dimension}
@!cur_h_dimen:int_32; {a horizontal dimension}

@ @<Set init...@>=
cur_cp:=0; cur_wp:=invalid_width; {so they can be saved and restored!}

@ The |dvi_first_par| procedure first reads \.{DVI} command bytes into
|cur_cmd| until |cur_cmd<>nop|; then |cur_parm| is set to the value of
the first parameter (if any) and |cur_class| to the command class.

@d set_cur_char(#)== {set up |cur_res|, |cur_ext|, and |cur_upd|}
begin cur_ext:=0;
if cur_cmd<set1 then
  begin cur_res:=cur_cmd; cur_upd:=true
  end
else  begin cur_res:=#; cur_upd:=(cur_cmd<put1);
  Decr(cur_cmd)(dvi_char_cmd[cur_upd]);
  if font_extend(cur_fnt) then
    begin
    cur_ext:=0;
    while cur_cmd>0 do
      begin
      cur_res:=cur_res*256+#; decr(cur_cmd);
      end
    end
  else
    while cur_cmd>0 do
      begin if cur_cmd=3 then if cur_res>127 then cur_ext:=-1;
      cur_ext:=cur_ext*256+cur_res; cur_res:=#; decr(cur_cmd);
      end;
  end;
end

@p procedure dvi_first_par;
begin repeat cur_cmd:=dvi_ubyte;
until cur_cmd<>nop; {skip over |nop|s}
case dvi_par[cur_cmd] of
char_par: set_cur_char(dvi_ubyte);
no_par: do_nothing;
dim1_par: cur_parm:=dvi_sbyte;
num1_par: cur_parm:=dvi_ubyte;
dim2_par: cur_parm:=dvi_spair;
num2_par: cur_parm:=dvi_upair;
dim3_par: cur_parm:=dvi_strio;
num3_par: cur_parm:=dvi_utrio;
two_cases(dim4_par): cur_parm:=dvi_squad; {|dim4_par| and |num4_par|}
numu_par: cur_parm:=dvi_uquad;
rule_par:
  begin cur_v_dimen:=dvi_squad; cur_h_dimen:=dvi_squad;
  cur_upd:=(cur_cmd=set_rule);
  end;
fnt_par:cur_parm:=cur_cmd-fnt_num_0;
end; {there are no other cases}
cur_class:=dvi_cl[cur_cmd];
end;

@ The global variable |dvi_nf| is used for the number of different
\.{DVI} fonts defined so far; their external font numbers (as extracted
from the \.{DVI} file) are stored in the array |dvi_e_fnts|, the
corresponding internal font numbers used internally by \.{\title} are
stored in the array |dvi_i_fnts|.

@<Glob...@>=
@!dvi_e_fnts:array[font_number] of int_32; {external font numbers}
@!dvi_i_fnts:array[font_number] of font_number; {corresponding
  internal font numbers}
@!dvi_nf:font_number; {number of \.{DVI} fonts defined so far}

@ @<Set ini...@>=
dvi_nf:=0;

@ The |dvi_font| procedure sets |cur_fnt| to the internal font number
corresponding to the external font number |cur_parm| (or aborts the
program if such a font was never defined).

@p procedure dvi_font; {computes |cur_fnt| corresponding to |cur_parm|}
var f:font_number; {where the font is sought}
begin @<DVI: Locate font |cur_parm|@>;
if f=dvi_nf then bad_dvi;
cur_fnt:=dvi_i_fnts[f];
if font_type(cur_fnt)=defined_font then load_font;
end;

@ @<DVI: Locate font |cur_parm|@>=
f:=0; dvi_e_fnts[dvi_nf]:=cur_parm;
while cur_parm<>dvi_e_fnts[f] do incr(f)

@ Finally the |dvi_do_font| procedure is called when one of the command
|fnt_def1..fnt_def4| and its first parameter have been read from the
\.{DVI} file; the argument indicates whether this should be the second
definition of the font (|true|) or not (|false|).

@p procedure dvi_do_font(@!second:boolean);
var f:font_number; {where the font is sought}
@!k:int_15; {general purpose variable}
begin print('DVI: font ',cur_parm:1);
@<DVI: Locate font |cur_parm|@>;
if (f=dvi_nf)=second then bad_dvi;
font_check(nf):=dvi_squad;
font_scaled(nf):=dvi_pquad;
font_design(nf):=dvi_pquad;
k:=dvi_ubyte; pckt_room(1); append_byte(k);
Incr(k)(dvi_ubyte); pckt_room(k);
while k>0 do  begin append_byte(dvi_ubyte); decr(k);
  end;
font_name(nf):=make_packet; {the font area plus name}
dvi_i_fnts[dvi_nf]:=define_font(false);
if not second then
  begin if dvi_nf=max_fonts then overflow(str_fonts,max_fonts);
  incr(dvi_nf);
  end
else if dvi_i_fnts[f]<>dvi_i_fnts[dvi_nf] then bad_dvi;
end;

@* Low-level VF input routines.
A detailed description of the \.{VF} file format can be found in
the documentation of \.{VFtoVP}; here we just define symbolic names for
some of the \.{VF} command bytes.

@d long_char=242 {\.{VF} command for general character packet}
@#
@d vf_id=202 {identifies \.{VF} files}

@ The program uses the binary file variable |vf_file| for input from
\.{VF} files; |vf_loc| is the number of the byte about to be read next
from |vf_file|.

@<Glob...@>=
@!vf_file:byte_file; {a \.{VF} file}
@!vf_loc:int_32; {where we are about to look, in |vf_file|}
@!vf_limit:int_32; {value of |vf_loc| at end of a character packet}
@!vf_ext:pckt_pointer; {extension for \.{VF} files}
@!ovf_ext:pckt_pointer; {extension for \.{OVF} files}
@!vf_cur_fnt:font_number; {current font number in a \.{VF} file}

@ @<Initialize predefined strings@>=
id3(".")("V")("F")(vf_ext); {file name extension for \.{VF} files}
id4(".")("O")("V")("F")(ovf_ext); {file name extension for \.{OVF} files}

@ If a \.{VF} file is badly malformed, we say |bad_font|; this procedure
gives an error message which refers the user to \.{VFtoVP} and
\.{OVPtoOVF}, and terminates \.{\title}.

@<Cases for |bad_font|@>=
vf_font_type: begin print('Bad (O)VF file'); print_font(cur_fnt);
@.Bad (O)VF file@>
  print_ln(' loc=',vf_loc:1);
  abort(
  'Use OVF2OVP/OVP2OVF/VFtoVP/VPtoVF to diagnose and correct the problem');
@.Use OVF2OVP/OVP2OVF/VFtoVP/VPtoVF@>
  end;

@ If no font directory has been specified, \.{\title} is supposed to use
the default \.{VF} directory, which is a system-dependent place where
the \.{VF} files for standard fonts are kept.
The string variable |VF_default_area| contains the name of this area.
@^system dependencies@>

@d VF_default_area_name=='TeXvfonts:' {change this to the correct name}
@d VF_default_area_name_length=10 {change this to the correct length}
@d OVF_default_area_name=='TeXvfonts:' {change this to the correct name}
@d OVF_default_area_name_length=10 {change this to the correct length}

@<Glob...@>=
@!VF_default_area:packed array[1..VF_default_area_name_length] of char;
@!OVF_default_area:packed array[1..OVF_default_area_name_length] of char;

@ @<Set init...@>=
VF_default_area:=VF_default_area_name;
OVF_default_area:=OVF_default_area_name;

@ To prepare |vf_file| for input we |reset| it.

@<VF: Open |vf_file| or |goto not_found|@>=
make_font_name(VF_default_area_name_length)(VF_default_area)(vf_ext);
reset(vf_file,cur_name);
if eof(vf_file) then begin
  make_font_name(OVF_default_area_name_length)(OVF_default_area)(ovf_ext);
  reset(vf_file,cur_name);
  if eof(vf_file) then
@^system dependencies@>
    goto not_found
  end;
vf_loc:=0

@ Reading a \.{VF} file should be done as efficient as possible for a
particular system; on many systems this means that a large number of
bytes from |vf_file| is read into a buffer and will then be extracted
from that buffer. In order to simplify such system dependent changes
we use a pair of \.{WEB} macros: |vf_byte| extracts the next \.{VF}
byte and |vf_eof| is |true| if we have reached the end of the \.{VF}
file. Here we give simple minded definitions for these macros in terms
of standard \PASCAL.
@^system dependencies@>
@^optimization@>

@d vf_eof == eof(vf_file) {has the \.{VF} file been exhausted?}
@d vf_byte(#) ==
  if vf_eof then bad_font
  else read(vf_file,#) {obtain next \.{VF} byte}

@ We need several simple functions to read the next byte or bytes
from |vf_file|.

@p function vf_ubyte:int_8u; {returns the next byte, unsigned}
@!begin_byte(vf_byte); incr(vf_loc); comp_ubyte(vf_ubyte);
end;
@#
function vf_upair:int_16u; {returns the next two bytes, unsigned}
@!begin_pair(vf_byte); Incr(vf_loc)(2); comp_upair(vf_upair);
end;
@#
function vf_strio:int_24; {returns the next three bytes, signed}
@!begin_trio(vf_byte); Incr(vf_loc)(3); comp_strio(vf_strio);
end;
@#
function vf_utrio:int_24u; {returns the next three bytes, unsigned}
@!begin_trio(vf_byte); Incr(vf_loc)(3); comp_utrio(vf_utrio);
end;
@#
function vf_squad:int_32; {returns the next four bytes, signed}
@!begin_quad(vf_byte); Incr(vf_loc)(4); comp_squad(vf_squad);
end;

@ All dimensions in a \.{VF} file, except the design sizes of a virtual
font and its local fonts, are |fix_word|s that must be scaled in exactly
the same way as the character widths from a \.{TFM} file; we can use the
same code, but this time |z|, |alpha|, and |beta| are global variables.

@<Glob...@>=
@<Variables for scaling computation@>@;

@ We need five functions to read the next byte or bytes and convert a
|fix_word| to a scaled dimension.

@p function vf_fix1:int_32; {returns the next byte as scaled value}
var x:int_32; {accumulator}
begin vf_byte(tfm_b3); incr(vf_loc);
tfm_fix1(x); vf_fix1:=x;
end;
@#
function vf_fix2:int_32; {returns the next two bytes as scaled value}
var x:int_32; {accumulator}
begin vf_byte(tfm_b2); vf_byte(tfm_b3); Incr(vf_loc)(2);
tfm_fix2(x); vf_fix2:=x;
end;
@#
function vf_fix3:int_32; {returns the next three bytes as scaled value}
var x:int_32; {accumulator}
begin vf_byte(tfm_b1); vf_byte(tfm_b2); vf_byte(tfm_b3);
Incr(vf_loc)(3);@/
tfm_fix3(x); vf_fix3:=x;
end;
@#
function vf_fix3u:int_32; {returns the next three bytes as scaled value}
begin vf_byte(tfm_b1); vf_byte(tfm_b2); vf_byte(tfm_b3);
Incr(vf_loc)(3);@/
vf_fix3u:=tfm_fix3u;
end;
@#
function vf_fix4:int_32; {returns the next four bytes as scaled value}
var x:int_32; {accumulator}
begin vf_byte(tfm_b0); vf_byte(tfm_b1); vf_byte(tfm_b2); vf_byte(tfm_b3);
Incr(vf_loc)(4);@/
tfm_fix4(x); vf_fix4:=x;
end;

@ Three other functions are used in cases where the result must have a
non-negative value or a positive value.

@p function vf_uquad:int_31; {result must be non-negative}
var x:int_32;
begin x:=vf_squad; if x<0 then bad_font @+ else vf_uquad:=x;
end;
@#
function vf_pquad:int_31; {result must be positive}
var x:int_32;
begin x:=vf_squad; if x<=0 then bad_font @+ else vf_pquad:=x;
end;
@#
function vf_fixp:int_31; {result must be positive}
var x:int_32; {accumulator}
begin vf_byte(tfm_b0); vf_byte(tfm_b1); vf_byte(tfm_b2); vf_byte(tfm_b3);
Incr(vf_loc)(4);@/
if tfm_b0>0 then bad_font;
vf_fixp:=tfm_fix3u;
end;

@ The |vf_first_par| procedure first reads a \.{VF} command byte into
|cur_cmd|; then |cur_parm| is set to the value of the first parameter
(if any) and |cur_class| to the command class.

@d set_cur_wp_end(#)== if cur_wp=invalid_width then #
@d set_cur_wp(#)== {set |cur_wp| to the char's width pointer}
cur_wp:=invalid_width;
if #<>invalid_font then
  if (cur_res>=font_bc(#))and(cur_res<=font_ec(#)) then
    begin cur_cp:=font_chars(#)+cur_res; cur_wp:=char_widths[cur_cp];
    end;
set_cur_wp_end

@p procedure vf_first_par;
begin cur_cmd:=vf_ubyte;
case dvi_par[cur_cmd] of
char_par:
  begin set_cur_char(vf_ubyte); set_cur_wp(vf_cur_fnt)(bad_font);
  end;
no_par: do_nothing;
dim1_par: cur_parm:=vf_fix1;
num1_par: cur_parm:=vf_ubyte;
dim2_par: cur_parm:=vf_fix2;
num2_par: cur_parm:=vf_upair;
dim3_par: cur_parm:=vf_fix3;
num3_par: cur_parm:=vf_utrio;
dim4_par: cur_parm:=vf_fix4;
num4_par: cur_parm:=vf_squad;
numu_par: cur_parm:=vf_uquad;
rule_par:
  begin cur_v_dimen:=vf_fix4; cur_h_dimen:=vf_fix4;
  cur_upd:=(cur_cmd=set_rule);
  end;
fnt_par:cur_parm:=cur_cmd-fnt_num_0;
end; {there are no other cases}
cur_class:=dvi_cl[cur_cmd];
end;

@ For a virtual font we set |font_type(f):=vf_font_type|; in this case
|font_font(f)| is the default font for character packets from virtual
font~|f|.
@^font types@>

The global variable |vf_nf| is used for the number of different local
fonts defined in a \.{VF} file so far; their external font numbers
(as extracted from the \.{VF} file) are stored in the array |vf_e_fnts|,
the corresponding internal font numbers used internally by \.{\title} are
stored in the array |vf_i_fnts|.

@<Glob...@>=
@!vf_e_fnts:array[font_number] of int_32; {external font numbers}
@!vf_i_fnts:array[font_number] of font_number; {corresponding
  internal font numbers}
@!vf_nf:font_number; {number of local fonts defined so far}
@!lcl_nf:font_number; {largest |vf_nf| value for any \.{VF} file}

@ @<Set init...@>=
lcl_nf:=0;

@ The |vf_font| procedure sets |vf_cur_fnt| to the internal font number
corresponding to the external font number |cur_parm| (or aborts the
program if such a font was never defined).

@p procedure vf_font; {computes |vf_cur_fnt| corresponding to |cur_parm|}
var f:font_number; {where the font is sought}
begin @<VF: Locate font |cur_parm|@>;
if f=vf_nf then bad_font;
vf_cur_fnt:=vf_i_fnts[f];
end;

@ @<VF: Locate font |cur_parm|@>=
f:=0; vf_e_fnts[vf_nf]:=cur_parm;
while cur_parm<>vf_e_fnts[f] do incr(f)

@ Finally the |vf_do_font| procedure is called when one of the command
|fnt_def1..fnt_def4| and its first parameter have been read from the
\.{VF} file.

@p procedure vf_do_font;
var f:font_number; {where the font is sought}
@!k:int_31; {general purpose variable}
begin
print('VF: font ',cur_parm:1);@/
@<VF: Locate font |cur_parm|@>;
if f<>vf_nf then bad_font;
font_check(nf):=vf_squad;
font_scaled(nf):=vf_fixp;
font_design(nf):=round(tfm_conv*vf_pquad);
k:=vf_ubyte; pckt_room(1); append_byte(k);
Incr(k)(vf_ubyte); pckt_room(k);
while k>0 do  begin append_byte(vf_ubyte); decr(k);
  end;
font_name(nf):=make_packet; {the font area plus name}
vf_i_fnts[vf_nf]:=define_font(true);
if vf_nf=lcl_nf then
  if lcl_nf=max_fonts then overflow(str_fonts,max_fonts)
  else incr(lcl_nf);
incr(vf_nf);
end;

@* Reading VF and OVF files.
The |do_vf| function attempts to read the \.{VF} file for a font and
returns |false| if the \.{VF} file could not be found; otherwise the
font type is changed to |vf_font_type|.

@p function do_vf:boolean; {read a \.{VF} file}
label reswitch,done,not_found,exit;
var temp_int:int_32; {integer for temporary variables}
@!temp_byte:int_8u; {byte for temporary variables}
@!k:byte_pointer; {index into |byte_mem|}
@!l:int_15; {general purpose variable}
@!save_ext:int_24; {used to save |cur_ext|}
@!save_res:int_32; {used to save |cur_res|}
@!save_cp:width_pointer; {used to save |cur_cp|}
@!save_wp:width_pointer; {used to save |cur_wp|}
@!save_upd:boolean; {used to save |cur_upd|}
@!vf_wp:width_pointer; {width pointer for the current character packet}
@!vf_fnt:font_number; {current font in the current character packet}
@!move_zero:boolean; {|true| if rule 1 is used}
@!last_pop:boolean; {|true| if final |pop| has been manufactured}
begin @<VF: Open |vf_file| or |goto not_found|@>;
save_ext:=cur_ext; save_res:=cur_res; save_cp:=cur_cp; save_wp:=cur_wp;
save_upd:=cur_upd; {save}
font_type(cur_fnt):=vf_font_type;@/
@<VF: Process the preamble@>;@/
@<VF: Process the font definitions@>;@/
while cur_cmd<=long_char do @<VF: Build a character packet@>;
if cur_cmd<>post then bad_font;
@!debug print('VF file for font ',cur_fnt:1); print_font(cur_fnt);
print_ln(' loaded.');
gubed @;@/
close_in(vf_file);
cur_ext:=save_ext; cur_res:=save_res; cur_cp:=save_cp; cur_wp:=save_wp;
cur_upd:=save_upd; {restore}
do_vf:=true; return;
not_found:do_vf:=false;
exit:end;

@ @<VF: Process the preamble@>=
if vf_ubyte<>pre then bad_font;
if vf_ubyte<>vf_id then bad_font;
temp_byte:=vf_ubyte; pckt_room(temp_byte);
for l:=1 to temp_byte do append_byte(vf_ubyte);
if font_extend(cur_fnt) then print('O');
print('VF file: '''); print_packet(new_packet); print(''',');
flush_packet;@/
check_check_sum(vf_squad,false);
check_design_size(round(tfm_conv*vf_pquad));@/
z:=font_scaled(cur_fnt);
@<Replace |z|...@>;@/
print_nl('   for font ',cur_fnt:1); print_font(cur_fnt); print_ln('.')

@ @<VF: Process the font definitions@>=
vf_i_fnts[0]:=invalid_font; vf_nf:=0;@/
cur_cmd:=vf_ubyte;
while (cur_cmd>=fnt_def1)and(cur_cmd<=fnt_def1+3) do
  begin case cur_cmd-fnt_def1 of
  0: cur_parm:=vf_ubyte;
  1: cur_parm:=vf_upair;
  2: cur_parm:=vf_utrio;
  3: cur_parm:=vf_squad;
  end; {there are no other cases}
  vf_do_font;
  cur_cmd:=vf_ubyte;
  end;
font_font(cur_fnt):=vf_i_fnts[0]

@ The \.{VF} format specifies that the interpretation of each packet
begins with |w=x=y=z=0|; any |w0|, |x0|, |y0|, or |z0| command using
these initial values will be ignored.

@<Types...@>=
@!vf_state=array[0..1,0..1] of boolean; {state of |w|, |x|, |y|, and |z|}

@ As implied by the \.{VF} format the \.{DVI} commands read from the \.{VF}
file are enclosed by |push| and |pop|; as we read \.{DVI}
commands and append them to |byte_mem|, we perform a set of
transformations in order to simplify the resulting packet: Let |zero| be
any of the commands |put|, |put_rule|, |fnt_num|, |fnt|, or |xxx| which
all leave the current position on the page unchanged, let |move| be any
of the horizontal or vertical movement commands |right1..z4|, and let
|any| be any sequence of commands containing |push| and |pop| in
properly nested pairs; whenever possible we apply one of the following
transformation rules: $$\def\n#1:{\hbox to 3cm{\hfil#1:}}
\leqalignno{
\hbox{|push| |zero|}&\RA\hbox{|zero| |push|}&\n1:\cr
\hbox{|move| |pop|}&\RA\hbox{|pop|}&\n2:\cr
\hbox{|push| |pop|}&\RA{}&\n3:\cr
\hbox{|push| |set_char| |pop|}&\RA\hbox{|put|}&\n4a:\cr
\hbox{|push| \\{set} |pop|}&\RA\hbox{|put|}&\n4b:\cr
\hbox{|push| |set_rule| |pop|}&\RA\hbox{|put_rule|}&\n4c:\cr
\hbox{|push| |push| |any| |pop|}&\RA\hbox{|push| |any| |pop| |push|}&\n5:\cr
\hbox{|push| |any| |pop| |pop|}&\RA\hbox{|any| |pop|}&\n6:\cr
}$$

@ In order to perform these transformations we need a stack which is
indexed by |vf_ptr|, the number of |push| commands without corresponding
|pop| in the packet we are building; the |vf_push_loc| array contains
the locations in |byte_mem| following such |push| commands.
In view of rule~5 consecutive |push| commands are never stored, the
|vf_push_num| array is used to count them.
The |vf_last| array indicates the type of the last non-discardable item:
a character, a rule, or a group enclosed by |push| and |pop|;
the |vf_last_end| array points to the ending locations and, if
|vf_last<>vf_other|, the |vf_last_loc| array points to the starting
locations of these items.

@d vf_set=0 {|vf_set=char_cl|, last item is a |set_char| or \\{set}}
@d vf_rule=1 {|vf_rule=rule_cl|, last item is a |set_rule|}
@d vf_group=2 {last item is a group enclosed by |push| and |pop|}
@d vf_put=3 {last item is a |put|}
@d vf_other=4 {last item (if any) is none of the above}

@<Types...@>=
@!vf_type=vf_set..vf_other;

@ @<Glob...@>=
@!vf_move: array[stack_pointer] of vf_state; {state of |w|, |x|, |y|, and |z|}
@!vf_push_loc: array[stack_pointer] of byte_pointer; {end of a |push|}
@!vf_last_loc: array[stack_pointer] of byte_pointer; {start of an item}
@!vf_last_end: array[stack_pointer] of byte_pointer; {end of an item}
@!vf_push_num: array[stack_pointer] of eight_bits; {|push| count}
@!vf_last: array[stack_pointer] of vf_type; {type of last item}
@!vf_ptr:stack_pointer; {current number of unfinished groups}
@!stack_used:stack_pointer; {largest |vf_ptr| or |stack_ptr| value}

@ We use two small arrays to determine the item type of a character or a
rule.

@<Glob...@>=
@!vf_char_type:array[boolean] of vf_type;
@!vf_rule_type:array[boolean] of vf_type;

@ @<Set init...@>=
vf_move[0][0][0]:=false; vf_move[0][0][1]:=false;
vf_move[0][1][0]:=false; vf_move[0][1][1]:=false;@/
stack_used:=0;@/
vf_char_type[false]:=vf_put; vf_char_type[true]:=vf_set;@/
vf_rule_type[false]:=vf_other; vf_rule_type[true]:=vf_rule;

@ Here we read the first bytes of a character packet from the \.{VF} or \.{OVF}
file and initialize the packet being built in |byte_mem|; the start of
the whole packet is stored in |vf_push_loc[0]|. When the character
packet is finished, a type is be assigned to it: |vf_simple| if the
packet ends with a character of the correct width, or |vf_complex|
otherwise. Moreover, if such a packet for a character with
extension zero consists of just one character with extension zero and
the same residue, and if there is no previous packet, the whole packet
is replaced by the empty packet.

@d vf_simple=0 {the packet ends with a character of the correct width}
@d vf_complex=vf_simple+1 {otherwise}

@<VF: Build a character packet@>=
begin if cur_cmd<long_char then
  begin vf_limit:=cur_cmd;
  cur_ext:=0; cur_res:=vf_ubyte; vf_wp:=check_width(vf_fix3u);
  end
else  if font_extend(cur_fnt) then
  begin vf_limit:=vf_uquad;
  cur_ext:=0; cur_res:=vf_squad; vf_wp:=check_width(vf_fix4);
  end
else begin vf_limit:=vf_uquad;
  cur_ext:=vf_strio; cur_res:=vf_ubyte; vf_wp:=check_width(vf_fix4);
  end;
Incr(vf_limit)(vf_loc);
vf_push_loc[0]:=byte_ptr; vf_last_end[0]:=byte_ptr;
vf_last[0]:=vf_other; vf_ptr:=0;@/
start_packet(vf_complex);
@<VF: Append \.{DVI} commands to the character packet@>;@/
k:=pckt_start[pckt_ptr];
if vf_last[0]=vf_put then if cur_wp=vf_wp then
  begin decr(byte_mem[k]); {change |vf_complex| into |vf_simple|}
  if (byte_mem[k]=bi(0))and@|(vf_push_loc[0]=vf_last_loc[0])and@|
    (cur_ext=0)and@|(cur_res=pckt_res) then byte_ptr:=k;
  end;
build_packet;
cur_cmd:=vf_ubyte;
end

@ For every \.{DVI} command read from the \.{VF} file some action is
performed; in addition the initial |push| and the final |pop| are
manufactured here.

@<VF: Append \.{DVI} commands to the character packet@>=
vf_cur_fnt:=font_font(cur_fnt); vf_fnt:=vf_cur_fnt;@/
last_pop:=false; cur_class:=push_cl; {initial |push|}
loop  begin
reswitch:case cur_class of
  three_cases(char_cl): @<VF: Do a |char|, |rule|, or |xxx|@>;
  push_cl: @<VF: Do a |push|@>;
  pop_cl: @<VF: Do a |pop|@>;
  two_cases(w0_cl):
    if vf_move[vf_ptr][0][cur_class-w0_cl] then append_one(cur_cmd);
  three_cases(right_cl):
    begin pckt_signed(dvi_right_cmd[cur_class],cur_parm);
    if cur_class>=w_cl then vf_move[vf_ptr][0][cur_class-w_cl]:=true;
    end;
  two_cases(y0_cl):
    if vf_move[vf_ptr][1][cur_class-y0_cl] then append_one(cur_cmd);
  three_cases(down_cl):
    begin pckt_signed(dvi_down_cmd[cur_class],cur_parm);
    if cur_class>=y_cl then vf_move[vf_ptr][1][cur_class-y_cl]:=true;
    end;
  fnt_cl: vf_font;
  fnt_def_cl: bad_font;
  invalid_cl: if cur_cmd<>nop then bad_font;
  end; {there are no other cases}
  if vf_loc<vf_limit then vf_first_par
  else if last_pop then goto done
  else  begin cur_class:=pop_cl; last_pop:=true; {final |pop|}
    end;
  end;
done:if (vf_ptr<>0)or(vf_loc<>vf_limit) then bad_font

@ For a |push| we either increase |vf_push_num| or start a new level and
append a |push|.

@d incr_stack(#)==
if #=stack_used then
  if stack_used=stack_size then overflow(str_stack,stack_size)
  else incr(stack_used);
incr(#)

@<VF: Do a |push|@>=
if (vf_ptr>0)and(vf_push_loc[vf_ptr]=byte_ptr) then
  begin if vf_push_num[vf_ptr]=255 then overflow(str_stack,255);
  incr(vf_push_num[vf_ptr]);
  end
else  begin incr_stack(vf_ptr);
  @<VF: Start a new level@>;
  vf_push_num[vf_ptr]:=0;
  end

@ @<VF: Start a new level@>=
append_one(push);
vf_move[vf_ptr]:=vf_move[vf_ptr-1];
vf_push_loc[vf_ptr]:=byte_ptr;
vf_last_end[vf_ptr]:=byte_ptr;
vf_last[vf_ptr]:=vf_other

@ When a character, a rule, or an |xxx| is appended, transformation
rule~1 might be applicable.

@<VF: Do a |char|, |rule|, or |xxx|@>=
begin if (vf_ptr=0)or(byte_ptr>vf_push_loc[vf_ptr]) then move_zero:=false
else case cur_class of
char_cl: move_zero:=(not cur_upd)or(vf_cur_fnt<>vf_fnt);
rule_cl: move_zero:=not cur_upd;
xxx_cl: move_zero:=true;
end; {there are no other cases}
if move_zero then
  begin decr(byte_ptr); decr(vf_ptr);
  end;
case cur_class of
char_cl: @<VF: Do a |fnt|, a |char|, or both@>;
rule_cl: @<VF: Do a |rule|@>;
xxx_cl: @<VF: Do an |xxx|@>;
end; {there are no other cases}
vf_last_end[vf_ptr]:=byte_ptr;
if move_zero then
  begin incr(vf_ptr); append_one(push); vf_push_loc[vf_ptr]:=byte_ptr;
  vf_last_end[vf_ptr]:=byte_ptr;
  if cur_class=char_cl then if cur_upd then goto reswitch;
  end;
end

@ A special situation arises if transformation rule~1 is applied to a
|fnt_num| of |fnt| command, but not to the |set_char| or \\{set} command
following it; in this case |cur_upd| and |move_zero| are both |true| and
the |set_char| or \\{set} command will be appended later.

@<VF: Do a |fnt|, a |char|, or both@>=
begin if vf_cur_fnt<>vf_fnt then
  begin vf_last[vf_ptr]:=vf_other;
  pckt_unsigned(fnt1,vf_cur_fnt); vf_fnt:=vf_cur_fnt;
  end;
if (not move_zero)or(not cur_upd) then
  begin vf_last[vf_ptr]:=vf_char_type[cur_upd];
  vf_last_loc[vf_ptr]:=byte_ptr;
  pckt_char(cur_upd,cur_ext,cur_res);
  end;
end

@ @<VF: Do a |rule|@>=
begin vf_last[vf_ptr]:=vf_rule_type[cur_upd];
vf_last_loc[vf_ptr]:=byte_ptr;
append_one(dvi_rule_cmd[cur_upd]);
pckt_four(cur_v_dimen); pckt_four(cur_h_dimen);
end

@ @<VF: Do an |xxx|@>=
begin vf_last[vf_ptr]:=vf_other;
pckt_unsigned(xxx1,cur_parm); pckt_room(cur_parm);
while cur_parm>0 do
  begin append_byte(vf_ubyte); decr(cur_parm);
  end;
end

@ Transformation rules 2--6 are triggered by a |pop|, either read from
the \.{VF} file or manufactured at the end of the packet.

@<VF: Do a |pop|@>=
begin if vf_ptr<1 then bad_font;
byte_ptr:=vf_last_end[vf_ptr]; {this is rule 2}
if vf_last[vf_ptr]<=vf_rule then
 if vf_last_loc[vf_ptr]=vf_push_loc[vf_ptr] then
  @<VF: Prepare for rule 4@>;
if byte_ptr=vf_push_loc[vf_ptr] then @<VF: Apply rule 3 or 4@>
else  begin if vf_last[vf_ptr]=vf_group then @<VF: Apply rule 6@>;
  append_one(pop); decr(vf_ptr); vf_last[vf_ptr]:=vf_group;
  vf_last_loc[vf_ptr]:=vf_push_loc[vf_ptr+1]-1;
  vf_last_end[vf_ptr]:=byte_ptr;
  if vf_push_num[vf_ptr+1]>0 then @<VF: Apply rule 5@>;
  end;
end

@ In order to implement transformation rule~4, we cancel the |set_char|,
\\{set}, or |set_rule|, append a |pop|, and insert a |put| or |put_rule|
with the old parameters.

@<VF: Prepare for rule 4@>=
begin cur_class:=vf_last[vf_ptr]; cur_upd:=false;
byte_ptr:=vf_push_loc[vf_ptr];
end

@ @<VF: Apply rule 3 or 4@>=
begin if vf_push_num[vf_ptr]>0 then
  begin decr(vf_push_num[vf_ptr]);
  vf_move[vf_ptr]:=vf_move[vf_ptr-1];
  end
else  begin decr(byte_ptr); decr(vf_ptr);
  end;
if cur_class<>pop_cl then goto reswitch; {this is rule 4}
end

@ @<VF: Apply rule 6@>=
begin Decr(byte_ptr)(2);
for k:=vf_last_loc[vf_ptr]+1 to byte_ptr do byte_mem[k-1]:=byte_mem[k];
vf_last[vf_ptr]:=vf_other; vf_last_end[vf_ptr]:=byte_ptr;
end

@ @<VF: Apply rule 5@>=
begin incr(vf_ptr);
@<VF: Start a new level@>;
decr(vf_push_num[vf_ptr]);
end

@ The \.{VF} formats specify that after a character packet invoked by a
|set_char| or \\{set} command, ``|h|~is increased by the \.{TFM} width
(properly scaled)---just as if a simple character had been typeset'';
for |vf_simple| packets this is achieved by changing the final |put|
command into |set_char| or \\{set}, but for |vf_complex| packets an
explicit movement must be done. This poses a problem for programs,
such as \.{DVIcopy}, which write a new \.{DVI} file with all references
to characters from virtual fonts replaced by their character packets:
The \.{DVItype} program specifies that the horizontal movements after a
|set_char| or \\{set} command, after a |set_rule| command, and after one
of the commands |right1..x4|, are all treated differently when \.{DVI}
units are converted to pixels.

Thus we introduce a slight extension of \.{DVItype}'s pixel rounding
algorithm and hope that this extension will become part of the standard
\.{DVItype} program in the near future: If a \.{DVI} file contains a
|set_rule| command for a rule with the negative height |width_dimen|,
then this rule shall be treated in exactly the same way as a ficticious
character whose width is the width of that rule; as value of |width_dimen|
we choose $-2^{31}$, the smallest signed 32-bit integer.

@<Glob...@>=
@!width_dimen:int_32; {vertical dimension of special rules}

@ When initializing |width_dimen| we are careful to avoid arithmetic
overflow.

@<Set init...@>=
width_dimen:=-@"40000000; Decr(width_dimen)(@"40000000);

@* Terminal communication.
When \.{\title} begins, it engages the user in a brief dialog so that
various options may be specified. This part of \.{\title} requires
nonstandard \PASCAL\ constructions to handle the online interaction; so
it may be preferable in some cases to omit the dialog and simply to
stick to the default options. On other hand, the system-dependent
routines that are needed are not complicated, so it will not be terribly
difficult to introduce them; furthermore they are similar to those in
\.{DVItype}.

It may be desirable to (optionally) specify all the options in the
command line and skip the dialog with the user, provided the operating
system permits this. Here we just define the system-indepent part of the
code required for this possibility. Since a complete option (a keyword
possibly followed by one or several parameters) may have embedded blanks
it might be necessary to replace these blanks by some other separator,
e.g., by a '/'. Using, e.g., \.{UNIX} style options one might then say
$$\.{\title\space-mag/2000 -sel/17.3/5 -sel/47 ...}$$
to override the magnification factor that is stated in the \.{DVI} file,
and to select five pages starting with the page numbered~17.3 as well as
all remaining pages starting with the one numbered~47; alternatively one
might simply say
$$\.{\title\space- ...}$$
to skip the dialog and use the default options.

The system-dependent initialization code should set the |n_opt| variable
to the number of options found in the command line.  If |n_opt=0| the
|input_ln| procedure defined below will promt the user for options.  If
|n_opt>0| the |k_opt| variable will be incremented and another piece of
system-dependent code is invoked instead of the dialog; that code should
place the value of command line option number |k_opt| as temporary
string into the |byte-mem| array.  This process will be repeated until
|k_opt=n_opt|, indicating that all command line options have been
processed.
@^system dependencies@>

@d opt_separator="/" {acts as blank when scanning (command line) options}

@<Set init...@>=
n_opt:=0; {change this to indicate the presence of command line options}
k_opt:=0; {just in case}
 
@ The |input_ln| routine waits for the user to type a line at his or her
terminal; then it puts ASCII-code equivalents for the characters on that
line into the |byte_mem| array as a temporary string. \PASCAL's
standard |input| file is used for terminal input, as |output| is used
for terminal output.

Since the terminal is being used for both input and output, some systems
need a special routine to make sure that the user can see a prompt message
before waiting for input based on that message. (Otherwise the message
may just be sitting in a hidden buffer somewhere, and the user will have
no idea what the program is waiting for.) We shall invoke a system-dependent
subroutine |update_terminal| in order to avoid this problem.
@^system dependencies@>

@d update_terminal == break(output) {empty the terminal output buffer}
@#
@d scan_blank(#)== {tests for `blank' when scanning (command line) options}
  ((byte_mem[#]=bi(" "))or(byte_mem[#]=bi(opt_separator)))
@d scan_skip== {skip `blanks'}
  while scan_blank(scan_ptr)and(scan_ptr<byte_ptr) do incr(scan_ptr)
@d scan_init== {initialize |scan_ptr|}
  byte_mem[byte_ptr]:=bi(" "); scan_ptr:=pckt_start[pckt_ptr-1]; scan_skip

@<Action procedures for |dialog|@>=
procedure input_ln; {inputs a line from the terminal}
var k:0..terminal_line_length;
begin if n_opt=0 then
  begin print('Enter option: '); update_terminal; reset(input);
  if eoln(input) then read_ln(input);
  k:=0; pckt_room(terminal_line_length);
  while (k<terminal_line_length)and not eoln(input) do
    begin append_byte(xord[input^]); incr(k); get(input);
    end;
  end
else if k_opt<n_opt then
  begin incr(k_opt);
  {Copy command line option number |k_opt| into |byte_mem| array!}
  end;
end;

@ The global variable |scan_ptr| is used while scanning the temporary
packet; it points to the next byte in |byte_mem| to be examined.

@<Glob...@>=
@!n_opt:int_16; {number of options found in command line}
@!k_opt:int_16; {number of command line options processed}
@!scan_ptr:byte_pointer; {pointer to next byte to be examined}
@!sep_char:text_char; {|' '| or |xchr[opt_separator]|}

@ The |scan_keyword| function is used to test for keywords in a character
string stored as temporary packet in |byte_mem|; the result is |true|
(and |scan_ptr| is updated) if the characters starting at position
|scan_ptr| are an abbreviation of a given keyword followed by at least
one blank.

@<Action procedures for |dialog|@>=
function scan_keyword(@!p:pckt_pointer;@!l:int_7):boolean;
var i,@!j,@!k:byte_pointer; {indices into |byte_mem|}
begin i:=pckt_start[p]; j:=pckt_start[p+1]; k:=scan_ptr;
while (i<j)and((byte_mem[k]=byte_mem[i])or(byte_mem[k]=byte_mem[i]-"a"+"A")) do
  begin incr(i); incr(k);
  end;
if scan_blank(k)and(i-pckt_start[p]>=l) then
  begin scan_ptr:=k; scan_skip; scan_keyword:=true;
  end
else scan_keyword:=false;
end;

@ Here is a routine that scans a (possibly signed) integer and computes
the decimal value. If no decimal integer starts at |scan_ptr|, the
value~0 is returned. The integer should be less than $2^{31}$ in
absolute value.

@<Action procedures for |dialog|@>=
function scan_int:int_32;
var x:int_32; {accumulates the value}
@!negative:boolean; {should the value be negated?}
begin if byte_mem[scan_ptr]="-" then
  begin negative:=true; incr(scan_ptr);
  end
else negative:=false;
x:=0;
while (byte_mem[scan_ptr]>="0")and(byte_mem[scan_ptr]<="9") do
  begin x:=10*x+byte_mem[scan_ptr]-"0"; incr(scan_ptr);
  end;
scan_skip;
if negative then scan_int:=-x @+ else scan_int:=x;
end;

@ The selected options are put into global variables by the |dialog|
procedure, which is called just as \.{\title} begins.
@^system dependencies@>

@p @<Action procedures for |dialog|@>@;
procedure dialog;
label exit;
var p:pckt_pointer; {packet being created}
begin @<Initialize options@>@;
loop  begin input_ln; p:=new_packet; scan_init;
  if scan_ptr=byte_ptr then
    begin flush_packet; return;
    end@;@/
  @<Cases for options@>@;@/
  else  begin if n_opt=0 then sep_char:=' '
    else sep_char:=xchr[opt_separator];
    print_options;
    if n_opt>0 then
      begin print('Bad command line option: ');
      print_packet(p); abort('---run terminated');
      end;
    end;
  flush_packet;
  end;
exit:end;

@ The |print_options| procedure might be used in a `Usage message'
displaying the command line syntax.

@<Basic printing...@>=
procedure print_options;
begin print_ln('Valid options are:');
@<Print valid options@>@;
end;

@* Subroutines for typesetting commands.
This is the central part of the whole \.{\title} program:
When a typesetting command from the \.{DVI} file or from a \.{VF} packet
has been decoded, one of the typesetting routines defined below is
invoked to execute the command; apart from the necessary book keeping,
these routines invoke device dependent code defined later.

@p @<Declare typesetting procedures@>

@ These typesetting routines communicate with the rest of the program
through global variables.

@<Glob...@>=
@!type_setting:boolean; {|true| while typesetting a page}

@ @<Set init...@>=
type_setting:=false;

@ The user may select up to |max_select| ranges of consecutive pages to
be processed. Each starting page specification is recorded in two global
arrays called |start_count| and |start_there|. For example, `\.{1.*.-5}'
is represented by |start_there[0]=true|, |start_count[0]=1|,
|start_there[1]=false|, |start_there[2]=true|, |start_count[2]=-5|. We
also set |start_vals=2|, to indicate that count 2 was the last one
mentioned. The other values of |start_count| and |start_there| are not
important, in this example. The number of pages is recorded in
|max_pages|; a non positive value indicates that there is no limit.

@d start_count==select_count[cur_select] {count values to select
  starting page}
@d start_there==select_there[cur_select] {is the |start_count| value
  relevant?}
@d start_vals==select_vals[cur_select] {the last count considered
  significant}
@d max_pages==select_max[cur_select] {at most this many |bop..eop| pages
  will be printed}

@<Glob...@>=
@!select_count:array[0..max_select-1,0..9] of int_32;
@!select_there:array[0..max_select-1,0..9] of boolean;
@!select_vals:array[0..max_select-1] of 0..9;
@!select_max:array[0..max_select-1] of int_32;
@!out_mag:int_32; {output maginfication}
@!count:array[0..9] of int_32; {the count values on the current page}
@!num_select:0..max_select; {number of page selection ranges specified}
@!cur_select:0..max_select; {current page selection range}
@!selected:boolean; {has starting page been found?}
@!all_done:boolean; {have all selected pages been processed?}
@!str_mag,@!str_select:pckt_pointer;

@ Here is a simple subroutine that tests if the current page might be the
starting page.

@p function start_match:boolean; {does |count| match the starting spec?}
var k:0..9;  {loop index}
@!match:boolean; {does everything match so far?}
begin match:=true;
for k:=0 to start_vals do
  if start_there[k]and(start_count[k]<>count[k]) then match:=false;
start_match:=match;
end;

@ @<Initialize options@>=
out_mag:=0; cur_select:=0; max_pages:=0; selected:=true;

@ @<Print valid options@>=
print_ln('  mag',sep_char,'<new_mag>');
print_ln('  select',sep_char,'<start_count>',sep_char,
  '[<max_pages>]  (up to ',max_select:1,' ranges)');

@ @<Action procedures for |dialog|@>=
procedure scan_count; {scan a |start_count| value}
begin if byte_mem[scan_ptr]=bi("*") then
  begin start_there[start_vals]:=false; incr(scan_ptr); scan_skip;
  end
else  begin start_there[start_vals]:=true;
  start_count[start_vals]:=scan_int;
  if cur_select=0 then selected:=false; {don't start at first page}
  end;
end;

@ @<Cases for options@>=
else if scan_keyword(str_mag,3) then out_mag:=scan_int
else if scan_keyword(str_select,3) then
  if cur_select=max_select then print_ln('Too many page selections')
  else  begin start_vals:=0; scan_count;
    while (start_vals<9)and(byte_mem[scan_ptr]=bi(".")) do
      begin incr(start_vals); incr(scan_ptr); scan_count;
      end;
    max_pages:=scan_int; incr(cur_select);
    end

@ @<Initialize predefined strings@>=
id3("m")("a")("g")(str_mag);
id6("s")("e")("l")("e")("c")("t")(str_select);

@ A stack is used to keep track of the current horizonal and vertical
position, |h| and |v|, and the four registers |w|, |x|, |y|, and |z|;
the register pairs |(w,x)| and |(y,z)| are maintained as arrays.

@<Types...@>=
@!device @<Declare device dependend types@>@; @+ ecived @; @/
@!stack_pointer=0..stack_size;@/
@!stack_index=1..stack_size;@/
@!pair_32=array[0..1] of int_32; {a pair of |int_32| variables}
@!stack_record=record@;@/
  @!h_field:int_32; {horizontal position |h|}
  @!v_field:int_32; {vertical position |v|}
  @!w_x_field:pair_32; {|w| and |x| register for horizontal movements}
  @!y_z_field:pair_32; {|y| and |z| register for vertical movements}
  @!device @<Device dependent stack record fields@>@; @+ ecived @; @/
  end;

@ The current values are kept in |cur_stack|; they are pushed onto and
popped from |stack|. We use \.{WEB} macros to access the current values.

@d cur_h==cur_stack.h_field {the current |@!h| value}
@d cur_v==cur_stack.v_field {the current |@!v| value}
@d cur_w_x==cur_stack.w_x_field {the current |@!w| and |@!x| value}
@d cur_y_z==cur_stack.y_z_field {the current |@!y| and |@!z| value}

@<Glob...@>=
@!stack:array[stack_index] of stack_record; {the pushed values}
@!cur_stack:stack_record; {the current values}
@!zero_stack:stack_record; {initial values}
@!stack_ptr:stack_pointer; {last used position in |stack|}

@ @<Set init...@>=
zero_stack.h_field:=0; zero_stack.v_field:=0;
for i:=0 to 1 do
  begin zero_stack.w_x_field[i]:=0; zero_stack.y_z_field[i]:=0;
  end;
@!device @<Initialize device dependent stack record fields@>@; @+ ecived @; @/

@ When typesetting for a real device we must convert the current
position from \.{DVI} units to pixels, i.e., |cur_h| and |cur_v| into
|cur_hh| and |cur_vv|.  This might be a good place to collect everything
related to the conversion from \.{DVI} units to pixels and in particular
all the pixel rounding algorithms.

@d font_space(#)==fnt_space[#] {boundary between ``small'' and ``large''
  spaces}

@<Declare device dependent font data arrays@>=
@!fnt_space:array [font_number] of int_32; {boundary between ``small''
  and ``large'' spaces}

@ @<Initialize device dependent font data@>=
font_space(invalid_font):=0;

@ @<Initialize device dependent data for a font@>=
font_space(cur_fnt):=font_scaled(cur_fnt) div 6;
  {this is a 3-unit ``thin space''}

@ The |char_pixels| array is used to store the horizontal character
escapements:  for \.{PK} or \.{GF} files we use the values given there,
otherwise we must convert the character widths to (horizontal) pixels.
The horizontal escapement of character~|c| in font~|f| is given by
|font_pixel(f)(c)|.

@d font_pixel(#)==char_pixels[font_chars(#)+font_width_end
@#
@d max_pix_value==@"7FFF {largest allowed pixel value; this range may not
  suffice for high resolution output devices}

@<Declare device dependend types@>=
@!pix_value=-max_pix_value..max_pix_value; {a pixel coordinate or displacement}

@ @<Glob...@>=
@!device
@!char_pixels:array[char_pointer] of pix_value; {character escapements}
@!h_pixels:pix_value; {a horizontal dimension in pixels}
@!v_pixels:pix_value; {a vertical dimension in pixels}
@!temp_pix:pix_value; {temporary value for pixel rounding}
ecived

@ @d cur_hh==cur_stack.hh_field {the current |@!hh| value}
@d cur_vv==cur_stack.vv_field {the current |@!vv| value}

@<Device dependent stack record fields@>=
@!hh_field:pix_value; {horizontal pixel position |hh|}
@!vv_field:pix_value; {vertical pixel position |vv|}

@ @<Initialize device dependent stack record fields@>=
zero_stack.hh_field:=0; zero_stack.vv_field:=0;

@ For small movements we round the increment in position, for large
movements we round the incremented position.  The same applies to rule
dimensions with the only difference that they will always be rounded
towards larger values.  For characters we increment the horizontal
position by the escapement values obtained, e.g., from a \.{PK} file or
by the \.{TFM} width converted to pixels.

@d h_pixel_round(#)==round(h_conv*(#))
@d v_pixel_round(#)==round(v_conv*(#))
@^system dependencies@>
@#
@d large_h_space(#)==(#>=font_space(cur_fnt))or(#<=-4*font_space(cur_fnt))
  {is this a ``large'' horizontal distance?}
@d large_v_space(#)==(abs(#)>=5*font_space(cur_fnt))
  {is this a ``large'' vertical distance?}
@#
@d h_rule_pixels== {converts the rule width |cur_h_dimen| to pixels}
@!device if large_h_space(cur_h_dimen) then
  begin h_pixels:=h_pixel_round(cur_h+cur_h_dimen)-cur_hh;
  if h_pixels<=0 then if cur_h_dimen>0 then h_pixels:=1;
  end
else  begin h_pixels:=trunc(h_conv*cur_h_dimen);
  if h_pixels<h_conv*cur_h_dimen then incr(h_pixels);
  end;
ecived
@#
@d v_rule_pixels== {converts the rule height |cur_v_dimen| to pixels}
@!device if large_v_space(cur_v_dimen) then
  begin v_pixels:=cur_vv-v_pixel_round(cur_v-cur_v_dimen);
  if v_pixels<=0 then v_pixels:=1; {used only for |cur_v_dimen>0|}
  end
else  begin v_pixels:=trunc(v_conv*cur_v_dimen);
  if v_pixels<v_conv*cur_v_dimen then incr(v_pixels);
  end;
ecived

@ A sequence of consecutive rules, or consecutive characters in a
fixed-width font whose width is not an integer number of pixels, can
cause |hh| to drift far away from a correctly rounded value.  \.{\title}
ensures that the amount of drift will never exceed |max_h_drift| pixels;
similarly |vv| shall never drift away from the correctly rounded value
by more than |max_v_drift| pixels.

@d h_upd_end(#)== {check for proper horizontal pixel rounding}
begin Incr(cur_hh)(#); temp_pix:=h_pixel_round(cur_h);
if abs(temp_pix-cur_hh)>max_h_drift then
  if temp_pix>cur_hh then cur_hh:=temp_pix-max_h_drift
  else cur_hh:=temp_pix+max_h_drift;
end @+ ecived
@d h_upd_char(#)==Incr(cur_h)(#)@;
  @!device; h_upd_end
@d h_upd_move(#)==Incr(cur_h)(#)@;
  @!device; if large_h_space(#) then cur_hh:=h_pixel_round(cur_h)
  else h_upd_end
@#
@d v_upd_end(#)== {check for proper vertical pixel rounding}
begin Incr(cur_vv)(#); temp_pix:=v_pixel_round(cur_v);
if abs(temp_pix-cur_vv)>max_v_drift then
  if temp_pix>cur_vv then cur_vv:=temp_pix-max_v_drift
  else cur_vv:=temp_pix+max_v_drift;
end @+ ecived
@d v_upd_move(#)==Incr(cur_v)(#)@;
  @!device; if large_v_space(#) then cur_vv:=v_pixel_round(cur_v)
  else v_upd_end

@ The routines defined below use sections named `Declare local variables
(if any) for \dots' or `Declare additional local variables for \dots';
the former may declare variables (including the keyword \&{var}), whereas
the later must at least contain the keyword \&{var}. In general, both may
start with the declaration of labels, constants, and\slash or types.

Let us start with the simple cases:
The |do_pre| procedure is called when the preamble has been read from
the \.{DVI} file; the preamble comment has just been converted into a
temporary packet with the |new_packet| procedure.

@p procedure do_pre;@/
@<OUT: Declare local variables (if any) for |do_pre|@>@;
begin all_done:=false; num_select:=cur_select; cur_select:=0;
if num_select=0 then max_pages:=0;
@!device
h_conv:=(dvi_num/254000.0)*(h_resolution/dvi_den)*(out_mag/1000.0);
v_conv:=(dvi_num/254000.0)*(v_resolution/dvi_den)*(out_mag/1000.0);
ecived @; @/
@<OUT: Process the |pre|@>@;@/
end;

@ The |do_bop| procedure is called when a |bop| has been read. This
routine determines whether a page shall be processed or skipped and sets
the variable |type_setting| accordingly.

@p procedure do_bop;@/
@<OUT: Declare additional local variables |do_bop|@>@;
@!i,@!j:0..9; {indices into |count|}
begin @<Determine whether this page should be processed or skipped@>;
print('DVI: ');
if type_setting then print('process') @+ else print('skipp');
print('ing page ',count[0]:1); j:=9;
while (j>0)and(count[j]=0) do decr(j);
for i:=1 to j do print('.',count[i]:1);
d_print(' at ',dvi_loc-45:1);
print_ln('.');
if type_setting then
  begin stack_ptr:=0; cur_stack:=zero_stack; cur_fnt:=invalid_font;@/
  @<OUT: Process a |bop|@>@;@/
  end;
end;

@ Note that the device dependent code `OUT: Process a |bop|' may choose
to set |type_setting| to false even if |selected| is true.

@<Determine whether this page...@>=
if not selected then selected:=start_match;
type_setting:=selected

@ The |do_eop| procedure is called in order to process an |eop|; the
stack should be empty.

@p procedure do_eop;@/
@<OUT: Declare local variables (if any) for |do_eop|@>@;
begin if stack_ptr<>0 then bad_dvi;
@<OUT: Process an |eop|@>@;
if max_pages>0 then
  begin decr(max_pages);
  if max_pages=0 then
    begin selected:=false; incr(cur_select);
   if cur_select=num_select then all_done:=true;
    end;
  end;
type_setting:=false;
end;

@ The procedures |do_push| and |do_pop| are called in order to process
|push| and |pop| commands; |do_push| must check for stack overflow,
|do_pop| should never be called when the stack is empty.

@p procedure do_push; {push onto stack}
@<OUT: Declare local variables (if any) for |do_push|@>@;
begin incr_stack(stack_ptr); stack[stack_ptr]:=cur_stack;@/
@<OUT: Process a |push|@>@;
end;
@#
procedure do_pop; {pop from stack}
@<OUT: Declare local variables (if any) for |do_pop|@>@;
begin if stack_ptr=0 then bad_dvi;
cur_stack:=stack[stack_ptr]; decr(stack_ptr);
@<OUT: Process a |pop|@>@;@/
end;

@ The |do_xxx| procedure is called in order to process a special command.
The bytes of the special string have been put into |byte_mem| as the
current string. They are converted to a temporary packet and discarded
again.

@p procedure do_xxx;@/
@<OUT: Declare additional local variables for |do_xxx|@>@;
@!p:pckt_pointer; {temporary packet}
begin p:=new_packet;@/
@<OUT: Process an |xxx|@>@;@/
flush_packet;
end;

@ Next are the movement commands:
The |do_right| procedure is called in order to process the horizontal
movement commands |right|, |w|, and |x|.


@p procedure do_right;@/
@<OUT: Declare local variables (if any) for |do_right|@>@;
begin if cur_class>=w_cl then cur_w_x[cur_class-w_cl]:=cur_parm
else if cur_class<right_cl then cur_parm:=cur_w_x[cur_class-w0_cl];
@<OUT: Process a |right| or |w| or |x|@>@;@/
h_upd_move(cur_parm)(h_pixel_round(cur_parm));
@<OUT: Move right@>@;
end;

@ The |do_down| procedure is called in order to process the vertical
movement commands |down|, |y|, and |z|.

@p procedure do_down;@/
@<OUT: Declare local variables (if any) for |do_down|@>@;
begin if cur_class>=y_cl then cur_y_z[cur_class-y_cl]:=cur_parm
else if cur_class<down_cl then cur_parm:=cur_y_z[cur_class-y0_cl];
@<OUT: Process a |down| or |y| or |z|@>@;@/
v_upd_move(cur_parm)(v_pixel_round(cur_parm));
@<OUT: Move down@>@;
end;

@ The |do_width| procedure, or actually the |do_a_width| macro, is
called in order to increase the current horizontal position |cur_h| by
|cur_h_dimen| in exactly the same way as if a character of width
|cur_h_dimen| had been typeset.

@d do_a_width(#)==
  begin @!device h_pixels:=#; @+ ecived @; @+ do_width;
  end

@p procedure do_width;@/
@<OUT: Declare local variables (if any) for |do_width|@>@;
begin @<OUT: Typeset a |width|@>@;@/
h_upd_char(cur_h_dimen)(h_pixels);
@<OUT: Move right@>@;
end;

@ Finally we have the commands for the typesetting of rules and characters;
the global variable |cur_upd| is |true| if the horizontal position shall
be updated (\\{set} commands).

The |do_rule| procedure is called in order to typeset a rule.

@p procedure do_rule;@/
@<OUT: Declare additional local variables |do_rule|@>@;
@!visible:boolean;
begin h_rule_pixels@;
if (cur_h_dimen>0)and(cur_v_dimen>0) then
  begin visible:=true; v_rule_pixels@;
  @<OUT: Typeset a visible |rule|@>@;
  end
else  begin visible:=false;
  @<OUT: Typeset an invisible |rule|@>@;
  end;
if cur_upd then
  begin h_upd_move(cur_h_dimen)(h_pixels);
  @<OUT: Move right@>@;
  end;
end;

@ Last not least the |do_char| procedure is called in order to typeset
character~|cur_res| with extension~|cur_ext| from the real font~|cur_fnt|.

@p procedure do_char;@/
@<OUT: Declare local variables (if any) for |do_char|@>@;
begin @<OUT: Typeset a |char|@>@;
if cur_upd then
  begin h_upd_char(widths[cur_wp])(char_pixels[cur_cp]);
  @<OUT: Move right@>@;
  end;
end;

@ If the program terminates abnormally, the following code may be
invoked in the middle of a page.

@<Finish output file(s)@>=
begin if type_setting then @<OUT: Finish incomplete page@>;
@<OUT: Finish output file(s)@>@;
end

@ When the first character of font~|cur_fnt| is about to be typeset, the
|do_font| procedure is called in order to decide whether this is a
virtual font or a real font.

One step in this decision is the attempt to find and read the \.{VF}
file for this font; other attempts to locate a font file may be
performed before and after that, depending on the nature of the output
device and on the structure of the file system at a particular
installation.  For a real device we convert the character widths to
(horizontal) pixels.

In any case |do_font| must change |font_type(cur_fnt)| to a value
|>defined_font|; as a last resort one might use the \.{TFM} width data
and draw boxes or leave blank spaces in the output.

@p procedure do_font;@/
label done;@/
@<OUT: Declare additional local variables for |do_font|@>@;
@!p:char_pointer; {index into |char_widths| and |char_pixels|}
begin @!debug if font_type(cur_fnt)=defined_font then confusion(str_fonts);
gubed@;
@!device for p:=font_chars(cur_fnt)+font_bc(cur_fnt)
  to font_chars(cur_fnt)+font_ec(cur_fnt) do
    char_pixels[p]:=h_pixel_round(widths[char_widths[p]]);
ecived@;
@<OUT: Look for a font file before trying to read the \.{VF} file;
  if found |goto done|@>@;@/
if do_vf then goto done; {try to read the \.{VF} file}
@<OUT: Look for a font file after trying to read the \.{VF} file@>@;@/
done:
@!debug if font_type(cur_fnt)<=loaded_font then confusion(str_fonts);
gubed@;
end;

@ Before a character of font~|cur_fnt| is typeset the following piece of
code ensures that the font is ready to be used.

@<Prepare to use font |cur_fnt|@>=
@<OUT: Prepare to use font |cur_fnt|@>@;
if font_type(cur_fnt)<=loaded_font then do_font {|cur_fnt| was not yet used}

@* Interpreting VF packets.
The |pckt_first_par| procedure first reads a \.{DVI} command byte from
the packet into |cur_cmd|; then |cur_parm| is set to the value of the
first parameter (if any) and |cur_class| to the command class.

@p procedure pckt_first_par;
begin cur_cmd:=pckt_ubyte;
case dvi_par[cur_cmd] of
char_par: set_cur_char(pckt_ubyte);
no_par: do_nothing;
dim1_par: cur_parm:=pckt_sbyte;
num1_par: cur_parm:=pckt_ubyte;
dim2_par: cur_parm:=pckt_spair;
num2_par: cur_parm:=pckt_upair;
dim3_par: cur_parm:=pckt_strio;
num3_par: cur_parm:=pckt_utrio;
three_cases(dim4_par): cur_parm:=pckt_squad; {|dim4|, |num4|, or |numu|}
rule_par:
  begin cur_v_dimen:=pckt_squad; cur_h_dimen:=pckt_squad;
  cur_upd:=(cur_cmd=set_rule);
  end;
fnt_par:cur_parm:=cur_cmd-fnt_num_0;
end; {there are no other cases}
cur_class:=dvi_cl[cur_cmd];
end;

@ The |do_vf_packet| procedure is called in order to interpret the
character packet for a virtual character. Such a packet may contain the
instruction to typeset a character from the same or an other virtual
font; in such cases |do_vf_packet| calls itself recursively. The
recursion level, i.e., the number of times this has happened, is kept
in the global variable |n_recur| and should not exceed |max_recursion|.
@^recursion@>

@<Types...@>=
@!recur_pointer=0..max_recursion;

@ The \.{\title} processor should detect an infinite recursion caused by
bad \.{VF} files; thus a new recursion level is entered even in cases
where this could be avoided without difficulty.

If the recursion level exceeds the allowed maximum, we want to give
a traceback how this has happened; thus some of the global variables
used in different invocations of |do_vf_packet| are saved in a stack,
others are saved as local variables of |do_vf_packet|.

@<Glob...@>=
@!recur_fnt:array[recur_pointer] of font_number; {this packet's font}
@!recur_ext:array[recur_pointer] of int_24; {this packet's extension}
@!recur_res:array[recur_pointer] of int_32; {this packet's residue}
@!recur_pckt:array[recur_pointer] of pckt_pointer; {the packet}
@!recur_loc:array[recur_pointer] of byte_pointer; {next byte of packet}
@!n_recur:recur_pointer; {current recursion level}
@!recur_used:recur_pointer; {highest recursion level used so far}

@ @<Set init...@>=
n_recur:=0; recur_used:=0;

@ Here now is the |do_vf_packet| procedure.

@p procedure do_vf_packet;
label continue,found,done;
var k:recur_pointer; {loop index}
@!f:int_8u; {packet type flag}
@!save_upd:boolean; {used to save |cur_upd|}
@!save_cp:width_pointer; {used to save |cur_cp|}
@!save_wp:width_pointer; {used to save |cur_wp|}
@!save_limit:byte_pointer; {used to save |cur_limit|}
begin @<VF: Save values on entry to |do_vf_packet|@>;@/
@<VF: Interpret the \.{DVI} commands in the packet@>@;@/
if save_upd then
  begin cur_h_dimen:=widths[save_wp]; do_a_width(char_pixels[save_cp]);
  end;
@<VF: Restore values on exit from |do_vf_packet|@>;@/
end;

@ On entry to |do_vf_packet| several values must be saved.

@<VF: Save values on entry to |do_vf_packet|@>=
save_upd:=cur_upd; save_cp:=cur_cp; save_wp:=cur_wp;@/
recur_fnt[n_recur]:=cur_fnt;
recur_ext[n_recur]:=cur_ext;
recur_res[n_recur]:=cur_res

@ Some of these values must be restored on exit from |do_vf_packet|.

@<VF: Restore values on exit from |do_vf_packet|@>=
cur_fnt:=recur_fnt[n_recur]

@ If |cur_pckt| is the empty packet, we manufacture a |put| command;
otherwise we read and interpret \.{DVI} commands from the packet.

@<VF: Interpret the \.{DVI} commands in the packet@>=
if find_packet then f:=cur_type @+ else goto done;
recur_pckt[n_recur]:=cur_pckt;
save_limit:=cur_limit;
cur_fnt:=font_font(cur_fnt);
if cur_pckt=empty_packet then
  begin cur_class:=char_cl; goto found;
  end;
if cur_loc>=cur_limit then goto done;
continue: pckt_first_par;
found: case cur_class of
char_cl: @<VF: Typeset a |char|@>;
rule_cl: do_rule;
xxx_cl:
  begin pckt_room(cur_parm);
  while cur_parm>0 do
    begin append_byte(pckt_ubyte); decr(cur_parm);
    end;
  do_xxx;
  end;
push_cl: do_push;
pop_cl: do_pop;
five_cases(w0_cl): do_right; {|right|, |w|, or |x|}
five_cases(y0_cl): do_down; {|down|, |y|, or |z|}
fnt_cl: cur_fnt:=cur_parm;
othercases confusion(str_packets); {font definition or invalid}
endcases;
if cur_loc<cur_limit then goto continue;
done:

@ The final |put| of a simple packet may be changed into |set_char| or
\\{set}.

@<VF: Typeset a |char|@>=
begin @<Prepare to use font |cur_fnt|@>;
cur_cp:=font_chars(cur_fnt)+cur_res; cur_wp:=char_widths[cur_cp];
if (cur_loc=cur_limit)and(f=vf_simple) and save_upd then
  begin save_upd:=false; cur_upd:=true;
  end;
if font_type(cur_fnt)=vf_font_type then
  @<VF: Enter a new recursion level@>
else do_char;
end

@ Before entering a new recursion level we must test for overflow; in
addition a few variables must be saved and restored.
A |set_char| or \\{set} followed by |pop| is changed into |put|.

@<VF: Enter a new recursion level@>=
begin recur_loc[n_recur]:=cur_loc; {save}
if cur_loc<cur_limit then
  if byte_mem[cur_loc]=bi(pop) then cur_upd:=false;
if n_recur=recur_used then
  if recur_used=max_recursion then
    @<VF: Display the recursion traceback and terminate@>
  else incr(recur_used);@/
incr(n_recur); do_vf_packet; decr(n_recur); {recurse}
cur_loc:=recur_loc[n_recur]; cur_limit:=save_limit; {restore}
end

@ @<VF: Display the recursion traceback and terminate@>=
begin print_ln(' !Infinite VF recursion?');
@.Infinite VF recursion?@>
for k:=max_recursion downto 0 do
  begin print('level=',k:1,' font');
  d_print('=',recur_fnt[k]:1);
  print_font(recur_fnt[k]);
  print(' char=',recur_res[k]:1);
  if recur_ext[k]<>0 then print('.',recur_ext[k]:1);
  new_line;
  @!debug hex_packet(recur_pckt[k]); print_ln('loc=',recur_loc[k]:1);
  gubed@;
  end;
overflow(str_recursion,max_recursion);
end

@* Interpreting the DVI file.
The |do_dvi| procedure reads the entire \.{DVI} file and initiates
whatever actions may be necessary.

@p procedure do_dvi;
label done,exit;
var temp_byte:int_8u; {byte for temporary variables}
@!temp_int:int_32; {integer for temporary variables}
@!dvi_start:int_32; {starting location}
@!dvi_bop_post:int_32; {location of |bop| or |post|}
@!dvi_back:int_32; {a back pointer}
@!k:int_15; {general purpose variable}
begin @<DVI: Process the preamble@>;
if random_reading then @<DVI: Process the postamble@>;
repeat dvi_first_par;
  while cur_class=fnt_def_cl do
    begin dvi_do_font(random_reading); dvi_first_par;
    end;
  if cur_cmd=bop then @<DVI: Process one page@>;
until cur_cmd<>eop;
if cur_cmd<>post then bad_dvi;
exit:end;

@ @<DVI: Process the preamble@>=
if dvi_ubyte<>pre then bad_dvi;
if dvi_ubyte<>dvi_id then bad_dvi;
dvi_num:=dvi_pquad; dvi_den:=dvi_pquad; dvi_mag:=dvi_pquad;
tfm_conv:=(25400000.0/dvi_num)*(dvi_den/473628672)/16.0;
temp_byte:=dvi_ubyte; pckt_room(temp_byte);
for k:=1 to temp_byte do append_byte(dvi_ubyte);
print('DVI file: '''); print_packet(new_packet); print_ln(''',');
print('   num=',dvi_num:1,', den=',dvi_den:1,', mag=',dvi_mag:1);
if out_mag<=0 then out_mag:=dvi_mag @+ else print(' => ',out_mag:1);
print_ln('.');
do_pre; flush_packet

@ @<Glob...@>=
@!dvi_num:int_31; {numerator}
@!dvi_den:int_31; {denominator}
@!dvi_mag:int_31; {magnification}

@ @<DVI: Process the postamble@>=
begin dvi_start:=dvi_loc; {remember start of first page}
@<DVI: Find the postamble@>;
d_print_ln('DVI: postamble at ',dvi_bop_post:1);
dvi_back:=dvi_pointer;
if dvi_num<>dvi_pquad then bad_dvi;
if dvi_den<>dvi_pquad then bad_dvi;
if dvi_mag<>dvi_pquad then bad_dvi;
temp_int:=dvi_squad; temp_int:=dvi_squad;
if stack_size<dvi_upair then overflow(str_stack,stack_size);
temp_int:=dvi_upair;
dvi_first_par;
while cur_class=fnt_def_cl do
  begin dvi_do_font(false); dvi_first_par;
  end;
if cur_cmd<>post_post then bad_dvi;
if not selected then @<DVI: Find the starting page@>;
dvi_move(dvi_start); {go to first or starting page}
end

@ @<DVI: Find the postamble@>=
temp_int:=dvi_length-5;
repeat if temp_int<49 then bad_dvi;
dvi_move(temp_int); temp_byte:=dvi_ubyte; decr(temp_int);
until temp_byte<>dvi_pad;
if temp_byte<>dvi_id then bad_dvi;
dvi_move(temp_int-4); if dvi_ubyte<>post_post then bad_dvi;
dvi_bop_post:=dvi_pointer;
if (dvi_bop_post<15)or(dvi_bop_post>dvi_loc-34) then bad_dvi;
dvi_move(dvi_bop_post); if dvi_ubyte<>post then bad_dvi

@ @<DVI: Find the starting page@>=
begin dvi_start:=dvi_bop_post; {just in case}
while dvi_back<>-1 do
  begin if (dvi_back<15)or(dvi_back>dvi_bop_post-46) then bad_dvi;
  dvi_bop_post:=dvi_back; dvi_move(dvi_back);
  if dvi_ubyte<>bop then bad_dvi;
  for k:=0 to 9 do count[k]:=dvi_squad;
  if start_match then dvi_start:=dvi_bop_post;
  dvi_back:=dvi_pointer;
  end;
end

@ When a |bop| has been read, the \.{DVI} commands for one page are
interpreted until an |eop| is found.

@<DVI: Process one page@>=
begin for k:=0 to 9 do count[k]:=dvi_squad;
temp_int:=dvi_pointer; do_bop;
dvi_first_par;
if type_setting then @<DVI: Process a page; then |goto done|@>
else @<DVI: Skip a page; then |goto done|@>;
done:if cur_cmd<>eop then bad_dvi;
if selected then
  begin do_eop;
  if all_done then return;
  end;
end

@ All \.{DVI} commands are processed, as long as |cur_class<>invalid_cl|;
then we should have found an |eop|.

@<DVI: Process a page; then |goto done|@>=
loop begin
  case cur_class of
  char_cl: @<DVI: Typeset a |char|@>;
  rule_cl:
    if cur_upd and(cur_v_dimen=width_dimen) then
      do_a_width(h_pixel_round(cur_h_dimen))
    else do_rule;
  xxx_cl:
    begin pckt_room(cur_parm);
    while cur_parm>0 do
      begin append_byte(dvi_ubyte); decr(cur_parm);
      end;
    do_xxx;
    end;
  push_cl: do_push;
  pop_cl: do_pop;
  five_cases(w0_cl): do_right; {|right|, |w|, or |x|}
  five_cases(y0_cl): do_down; {|down|, |y|, or |z|}
  fnt_cl: dvi_font;
  fnt_def_cl: dvi_do_font(random_reading);
  invalid_cl: goto done;
  end; {there are no other cases}
dvi_first_par; {get the next command}
end

@ While skipping a page all commands other than font definitions are
ignored.

@<DVI: Skip a page; then |goto done|@>=
loop begin
  case cur_class of
  xxx_cl: while cur_parm>0 do
    begin temp_byte:=dvi_ubyte; decr(cur_parm);
    end;
  fnt_def_cl: dvi_do_font(random_reading);
  invalid_cl: goto done;
  othercases do_nothing;
  endcases;
dvi_first_par; {get the next command}
end

@ @<DVI: Typeset a |char|@>=
begin @<Prepare to use font |cur_fnt|@>;
set_cur_wp(cur_fnt)(bad_dvi);
if font_type(cur_fnt)=vf_font_type then do_vf_packet @+ else do_char;
end

@* The main program.
The code for real devices is still rather incomplete.
Moreover several branches of the program have not been tested because
they are never used with \.{DVI} files made by \TeX\ and \.{VF} files
made by \.{VPtoVF}.  The same holds true for~$\Omega$.

@ At the end of the program the output file(s) have to be finished and
on some systems it may be necessary to close input and\slash or output
files.
@^system dependencies@>

@p procedure close_files_and_terminate;
var k:@!int_15; {general purpose index}
begin close_in(dvi_file);
if history<fatal_message then @<Finish output file(s)@>;
stat @<Print memory usage statistics@>;@+tats@;@/
@<Close output file(s)@>@;
@<Print the job |history|@>;
end;

@ Now we are ready to put it all together.
Here is where \.{\title} starts, and where it ends.
@^system dependencies@>

@p begin initialize; {get all variables initialized}
@<Initialize predefined strings@>@;
dialog; {get options}
@<Open input file(s)@>@;
@<Open output file(s)@>@;
do_dvi; {process the entire \.{DVI} file}
close_files_and_terminate;
final_end:end.

@ @<Print memory usage statistics@>=
print_ln('Memory usage statistics:');
print(dvi_nf:1,' dvi, ',lcl_nf:1,' local, ');
@<Print more font usage statistics@>@;@/
print_ln('and ',nf:1,' internal fonts of ',max_fonts:1);
print_ln(n_widths:1,' widths of ',max_widths:1,' for ',
  n_chars:1,' characters of ',max_chars:1);
print_ln(pckt_ptr:1,' byte packets of ',max_packets:1,' with ',
  byte_ptr:1,' bytes of ',max_bytes:1);
@<Print more memory usage statistics@>@;@/
print_ln(stack_used:1,' of ',stack_size:1,' stack and ',
  recur_used:1,' of ',max_recursion:1,' recursion levels.')

@ Some implementations may wish to pass the |history| value to the
operating system so that it can be used to govern whether or not other
programs are started. Here we simply report the history to the user.
@^system dependencies@>

@<Print the job |history|@>=
case history of
spotless: print_ln('(No errors were found.)');
harmless_message: print_ln('(Did you see the warning message above?)');
error_message: print_ln('(Pardon me, but I think I spotted something wrong.)');
fatal_message: print_ln('(That was a fatal error, my friend.)');
end {there are no other cases}

@* Low-level output routines.
The program uses the binary file variable |out_file| for its main output
file; |out_loc| is the number of the byte about to be written next on
|out_file|.

@<Glob...@>=
@!out_file:byte_file; {the \.{DVI} file we are writing}
@!out_loc:int_32; {where we are about to write, in |out_file|}
@!out_back:int_32; {a back pointer}
@!out_max_v:int_31; {maximum |v| value so far}
@!out_max_h:int_31; {maximum |h| value so far}
@!out_stack:int_16u; {maximum stack depth}
@!out_pages:int_16u; {total number of pages}

@ @<Set ini...@>=
out_loc:=0; out_back:=-1;
out_max_v:=0; out_max_h:=0;
out_stack:=0; out_pages:=0;

@ To prepare |out_file| for output, we |rewrite| it.

@<Open output file(s)@>=
rewrite(out_file); {prepares to write packed bytes to |out_file|}

@ For some operating systems it may be necessary to close |out_file|.

@<Close output file(s)@>=

@ Writing the |out_file| should be done as efficient as possible for a
particular system; on many systems this means that a large number of
bytes will be accumulated in a buffer and is then written from that
buffer to |out_file|. In order to simplify such system dependent changes
we use the \.{WEB} macro |out_byte| to write the next \.{DVI} byte. Here
we give a simple minded definition for this macro in terms of standard
\PASCAL.
@^system dependencies@>
@^optimization@>

@d out_byte(#) == write(out_file,#) {write next \.{DVI} byte}

@ The \.{WEB} macro |out_one| is used to write one byte and to update
|out_loc|.

@d out_one(#) == begin out_byte(#); incr(out_loc); @+ end

@ First the |out_packet| procedure copies a packet to |out_file|.

@<Declare typesetting procedures@>=
procedure out_packet(@!p:pckt_pointer);
var k:byte_pointer; {index into |byte_mem|}
begin Incr(out_loc)(pckt_length(p));
for k:=pckt_start[p] to pckt_start[p+1]-1 do out_byte(bo(byte_mem[k]));
end;

@ Next are the procedures used to write integer numbers or even complete
\.{DVI} commands to |out_file|; they all keep |out_loc| up to date.

The |out_four| procedure outputs four bytes in two's complement notation,
without risking arithmetic overflow.

@<Declare typesetting procedures@>=
procedure out_four(@!x:int_32); {output four bytes}
@!begin_four; comp_four(out_byte); Incr(out_loc)(4);
end;

@ The |out_char| procedure outputs a |set_char| or \\{set} command or, if
|upd=false|, a |put| command.

@<Declare typesetting procedures@>=
procedure out_char(@!upd:boolean;@!ext:int_32;@!res:int_32);
  {output \\{set} or |put|}
@!begin_char; comp_char(out_one);
end;

@ The |out_unsigned| procedure outputs a |fnt|, |xxx|, or |fnt_def|
command with its first parameter (normally unsigned); a |fnt| command
is converted into |fnt_num| whenever this is possible.

@<Declare typesetting procedures@>=
procedure out_unsigned(@!o:eight_bits;@!x:int_32);
  {output |fnt_num|, |fnt|, |xxx|, or |fnt_def|}
@!begin_unsigned; comp_unsigned(out_one);
end;

@ The |out_signed| procedure outputs a movement (|right|, |w|,
|x|, |down|, |y|, or |z|) command with its (signed) parameter.

@<Declare typesetting procedures@>=
procedure out_signed(@!o:eight_bits;@!x:int_32);
  {output |right|, |w|, |x|, |down|, |y|, or |z|}
@!begin_signed; comp_signed(out_one);
end;

@ For an output font we set |font_type(f):=out_font_type|; in this case
|font_font(f)| is the font number used for font~|f| in |out_file|.
@^font types@>

The global variable |out_nf| is the number of fonts already used in
|out_file| and the array |out_fnts| contains their internal font numbers;
the current font in |out_file| is called |out_fnt|.

@<Glob...@>=
@!out_fnts:array[font_number] of font_number; {internal font numbers}
@!out_nf:font_number; {number of fonts used in |out_file|}
@!out_fnt:font_number; {internal font number of current output font}

@ @<Set init...@>=
out_nf:=0;

@ @<Print more font usage statistics@>=
print(out_nf:1,' out, ');

@ The |out_fnt_def| procedure outputs a complete font definition
command.

@<Declare typesetting procedures@>=
procedure out_fnt_def(@!f:font_number);
var p:pckt_pointer; {the font name packet}
@!k,@!l:byte_pointer; {indices into |byte_mem|}
@!a:eight_bits; {length of area part}
begin out_unsigned(fnt_def1,font_font(f)); out_four(font_check(f));
out_four(font_scaled(f)); out_four(font_design(f));@/
p:=font_name(f); k:=pckt_start[p]; l:=pckt_start[p+1]-1;
a:=bo(byte_mem[k]);@/
Incr(out_loc)(l-k+2); out_byte(a); out_byte(l-k-a);
while k<l do
  begin incr(k); out_byte(bo(byte_mem[k]));
  end;
end;

@* Writing the output file.
Here we define the device dependent parts of the typesetting routines
described earlier in this program.

First we define a few quantities required by the device dependent code
for a real output device in order to demonstrate how they might be
defined and in order to be able to compile \.{DVIcopy} with the device
dependent code included.

@d h_resolution==300 {horizontal resolution in pixels per inch (dpi)}
@d v_resolution==300 {vertical resolution in pixels per inch (dpi)}

@d max_h_drift==2 {we insist that |abs(hh-h_pixel_round(h))<=max_h_drift|}
@d max_v_drift==2 {we insist that |abs(vv-v_pixel_round(v))<=max_v_drift|}

@<Glob...@>=
@!device
@!h_conv:real; {converts \.{DVI} units to horizontal pixels}
@!v_conv:real; {converts \.{DVI} units to vertical pixels}
ecived

@ These are the local variables (if any) needed for |do_pre|.

@<OUT: Declare local variables (if any) for |do_pre|@>=
var k:int_15; {general purpose variable}
@!p,@!q,@!r:byte_pointer; {indices into |byte_mem|}
@!comment:packed array[1..comm_length] of char; {preamble comment prefix}

@ And here is the device dependent code for |do_pre|; the \.{DVI} preamble
comment written to |out_file| is similar to the one produced by \.{GFtoPK},
but we want to apply our preamble comment prefix only once.

@<OUT: Process the |pre|@>=
out_one(pre); out_one(dvi_id);
out_four(dvi_num); out_four(dvi_den); out_four(out_mag);@/
p:=pckt_start[pckt_ptr-1]; q:=byte_ptr; {location of old \.{DVI} comment}
comment:=preamble_comment; pckt_room(comm_length);
for k:=1 to comm_length do append_byte(xord[comment[k]]);
while byte_mem[p]=bi(" ") do incr(p); {remove leading blanks}
if p=q then Decr(byte_ptr)(from_length)
else begin k:=0;
  while (k<comm_length)and(byte_mem[p+k]=byte_mem[q+k]) do incr(k);
  if k=comm_length then Incr(p)(comm_length);
  end;
k:=byte_ptr-p; {total length}
if k>255 then
  begin k:=255; q:=p+255-comm_length; {at most 255 bytes}
  end;
out_one(k); out_packet(new_packet); flush_packet;
for r:=p to q-1 do out_one(bo(byte_mem[r]));

@ These are the additional local variables (if any) needed for |do_bop|;
the variables |@!i| and |@!j| are already declared.

@<OUT: Declare additional local variables |do_bop|@>=
var

@ And here is the device dependent code for |do_bop|.

@<OUT: Process a |bop|@>=
out_one(bop); incr(out_pages);
for i:=0 to 9 do out_four(count[i]);
out_four(out_back); out_back:=out_loc-45;
out_fnt:=invalid_font;

@ These are the local variables (if any) needed for |do_eop|.

@<OUT: Declare local variables (if any) for |do_eop|@>=

@ And here is the device dependent code for |do_eop|.

@<OUT: Process an |eop|@>=
out_one(eop);

@ These are the local variables (if any) needed for |do_push|.

@<OUT: Declare local variables (if any) for |do_push|@>=

@ And here is the device dependent code for |do_push|.

@<OUT: Process a |push|@>=
if stack_ptr>out_stack then out_stack:=stack_ptr;
out_one(push);

@ These are the local variables (if any) needed for |do_pop|.

@<OUT: Declare local variables (if any) for |do_pop|@>=

@ And here is the device dependent code for |do_pop|.

@<OUT: Process a |pop|@>=
out_one(pop);

@ These are the additional local variables (if any) needed for |do_xxx|;
the variable |@!p|, the pointer to the packet containing the special
string, is already declared.

@<OUT: Declare additional local variables for |do_xxx|@>=
var

@ And here is the device dependent code for |do_xxx|.

@<OUT: Process an |xxx|@>=
out_unsigned(xxx1,pckt_length(p)); out_packet(p);

@ These are the local variables (if any) needed for |do_right|.

@<OUT: Declare local variables (if any) for |do_right|@>=

@ And here is the device dependent code for |do_right|.

@<OUT: Process a |right| or |w| or |x|@>=
if cur_class<right_cl then out_one(cur_cmd) {|w0| or |x0|}
else out_signed(dvi_right_cmd[cur_class],cur_parm); {|right|, |w|, or |x|}

@ Here we update the |out_max_h| value.

@<OUT: Move right@>=
if abs(cur_h)>out_max_h then out_max_h:=abs(cur_h);

@ These are the local variables (if any) needed for |do_down|.

@<OUT: Declare local variables (if any) for |do_down|@>=

@ And here is the device dependent code for |do_down|.

@<OUT: Process a |down| or |y| or |z|@>=
if cur_class<down_cl then out_one(cur_cmd) {|y0| or |z0|}
else out_signed(dvi_down_cmd[cur_class],cur_parm); {|down|, |y|, or |z|}

@ Here we update the |out_max_v| value.

@<OUT: Move down@>=
if abs(cur_v)>out_max_v then out_max_v:=abs(cur_v);

@ These are the local variables (if any) needed for |do_width|.

@<OUT: Declare local variables (if any) for |do_width|@>=

@ And here is the device dependent code for |do_width|.

@<OUT: Typeset a |width|@>=
out_one(set_rule);
out_four(width_dimen); out_four(cur_h_dimen);

@ These are the additional local variables (if any) needed for |do_rule|;
the variable |@!visible| is already declared.

@<OUT: Declare additional local variables |do_rule|@>=
var

@ And here is the device dependent code for |do_rule|.

@<OUT: Typeset a visible |rule|@>=
out_one(dvi_rule_cmd[cur_upd]);
out_four(cur_v_dimen); out_four(cur_h_dimen);

@ @<OUT: Typeset an invisible |rule|@>=
@<OUT: Typeset a visible |rule|@>

@ These are the additional local variables (if any) needed for |do_font|;
the variable |@!p| is already declared.

@<OUT: Declare additional local variables for |do_font|@>=
var

@ And here is the device dependent code for |do_font|; if the \.{VF} file
for a font could not be found, we simply assume this must be a real font.

@<OUT: Look for a font file before trying to read the \.{VF} file;
  if found |goto done|@>=

@ @<OUT: Look for a font file after trying to read the \.{VF} file@>=
if(out_nf>=max_fonts) then overflow(str_fonts,max_fonts);
print('OUT: font ',cur_fnt:1); d_print(' => ',out_nf:1);
print_font(cur_fnt);
d_print(' at ',font_scaled(cur_fnt):1,' DVI units'); print_ln('.');
font_type(cur_fnt):=out_font_type; font_font(cur_fnt):=out_nf;
out_fnts[out_nf]:=cur_fnt; incr(out_nf);
out_fnt_def(cur_fnt);

@ And here is some device dependent code used before each character.

@<OUT: Prepare to use font |cur_fnt|@>=

@ These are the local variables (if any) needed for |do_char|.

@<OUT: Declare local variables (if any) for |do_char|@>=

@ And here is the device dependent code for |do_char|.

@<OUT: Typeset a |char|@>=
@!debug if font_type(cur_fnt)<>out_font_type then confusion(str_fonts);
gubed @;
if cur_fnt<>out_fnt then
  begin out_unsigned(fnt1,font_font(cur_fnt)); out_fnt:=cur_fnt;
  end;
out_char(cur_upd,cur_ext,cur_res);

@ If the program terminates in the middle of a page, we write as many
|pop|s as necessary and one |eop|.

@<OUT: Finish incomplete page@>=
begin while stack_ptr>0 do
  begin out_one(pop); decr(stack_ptr);
  end;
  out_one(eop);
end

@ If the output file has been started, we write the postamble; in
addition we print the number of bytes and pages written to |out_file|.

@<OUT: Finish output file(s)@>=
if out_loc>0 then
  begin @<OUT: Write the postamble@>;
  k:=7-((out_loc-1) mod 4); {the number of |dvi_pad| bytes}
  while k>0 do
    begin out_one(dvi_pad); decr(k);
    end;
  print('OUT file: ',out_loc:1,' bytes, ',out_pages:1,' page');
  if out_pages<>1 then print('s');
  end
else print('OUT file: no output');
print_ln(' written.');
if out_pages=0 then mark_harmless;

@ Here we simply write the values accumulated during the \.{DVI} output.

@<OUT: Write the postamble@>=
out_one(post); out_four(out_back); out_back:=out_loc-5;@/
out_four(dvi_num); out_four(dvi_den); out_four(out_mag);@/
out_four(out_max_v); out_four(out_max_h);@/
out_one(out_stack div @"100); out_one(out_stack mod @"100);@/
out_one(out_pages div @"100); out_one(out_pages mod @"100);@/
k:=out_nf;
while k>0 do
  begin decr(k); out_fnt_def(out_fnts[k]);
  end;
out_one(post_post); out_four(out_back);@/
out_one(dvi_id)

@ Here we could print more memory usage statistics; this possibility is,
however, not used for \.{DVIcopy}.

@<Print more memory usage statistics@>=

@* System-dependent changes.
This section should be replaced, if necessary, by changes to the program
that are necessary to make \.{DVIcopy} work at a particular installation.
It is usually best to design your change file so that all changes to
previous sections preserve the section numbering; then everybody's version
will be consistent with the printed program. More extensive changes,
which introduce new sections, can be inserted here; then only the index
itself will get a new section number.
@^system dependencies@>

@* Index.
Pointers to error messages appear here together with the section numbers
where each ident\-i\-fier is used.