summaryrefslogtreecommitdiff
path: root/dviware/dvgtk/dvireader.c
blob: 8e59cb79a8390a48ad52bb3e4530140a43a785fa (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
/* dvireader.c - functions for reading DVI files
   $Id: dvireader.c,v 0.2 1997/03/28 03:16:30 tjchol01 Exp $
   Authors: Andrew Trevorrow, Ian Dall, Geoffrey Tobin, Tomasz J. Cholewo
 */

#include "dvgt.h"
#include "dvireader.h"
#include "screenio.h"
#include "fontreader.h"
#include "options.h"

/* Variables Exported, via "dvireader.h". */
int DVImag;
int totalpages, totalfonts, totalrules;
int currDVIpage;
TeXcounters currTeXpage;
ruleinfo *rulelist, *ruletail;
fontinfo *fontlist, *currfont;
specialinfo *speciallist;
boolean pageempty;
int minhp, minvp, maxhp, maxvp;

time_t  dvi_time;               /* last mod. time for dvi file */
/*----------------------------------------------------------------------
   DECLARATIONS FOR INTERPRETING A DVI PAGE

   The commands between the bop and eop bytes for a particular page need to be
   translated (based on the method used by DVITYPE) before we can determine the
   the position and shape of all rules on that page, as well as the position
   of all characters and which fonts they belong to.
*/

/* Use symbolic names for the opcodes of DVI commands:                     */

#define setchar0        0
    /* setchar1..setchar127 = 1..127                     */
#define set1            128
    /* set2,set3,set4 = 129,130,131                      */
#define setrule         132
#define put1            133
    /* put2,put3,put4 = 134,135,136                      */
#define putrule         137
#define nop             138
#define bop             139
#define eop             140
#define push            141
#define pop             142
#define right1          143
#define w0              147
#define x0              152
#define down1           157
#define y0_             161
#define z0              166
#define right2          144
#define w1              148
#define x1              153
#define down2           158
#define y1_             162
#define z1              167
#define right3          145
#define w2              149
#define x2              154
#define down3           159
#define y2              163
#define z2              168
#define right4          146
#define w3              150
#define x3              155
#define down4           160
#define y3              164
#define z3              169
#define w4              151
#define x4              156
#define y4              165
#define z4              170
#define fntnum0         171
    /* fntnum1..fntnum63 = 172..234                      */
#define fnt1            235
    /* fnt2,fnt3,fnt4 = 236,237,238                      */
#define xxx1            239
    /* xxx2,xxx3,xxx4 = 240,241,242                      */
#define fntdef1         243
    /* fntdef2,fntdef3,fntdef4 = 244,245,246             */
#define pre             247
#define post            248
#define postpost        249
/* undefined commands = 250..255 */

#define maxstacksize    100	/* maximum stack size for state values   */

/*  According to the DVI Driver Standard, Level 0, version 0.5,
   section 2.6.2, max_drift should depend on the output device:

   2  if a "device unit" <= 0.005 in (0.127 mm),
   1  if a "device unit" >  0.005 in (0.127 mm)
   but <= 0.01 in (0.254 mm),
   0  if a "device unit" >  0.01 in (0.254 mm).

   So, the value given here for max_drift implies a device with
   a resolution of at least 200 dpi.

   Note:  max_drift is a number of pixels.
 */

#define max_drift        2	/* prevent hh & vv from drifting too far */


/*----------------------------------------------------------------------
   DECLARATIONS FOR ACCESSING BYTES IN A DVI FILE

   A DVI file is a stream of 8-bit bytes.  Once opened, we can access any byte
   by setting DVIoffset to the required position and then calling 
GetDVIByte.
*/

static int DVIfile;		/* DVI file descriptor                  */
static int DVIoffset;		/* current byte offset in DVIfile       */
static int currDVIbuff;		/* starting byte offset in buffer       */
static buffer DVIbuffer;	/* input buffer                         */
static int postpostid;		/* offset of postpost's id byte         */

/*----------------------------------------------------------------------
   DECLARATIONS FOR GETTING TO A DVI PAGE

   The user can select a particular page by specifying a DVI page
   number (from 1 to totalpages), or a TeX page number (based on the
   values of \count0,\count1,...,\count9), or simply requesting the next page
   in the DVI file (which depends on whether we are ascending or not).
   We will often need to follow the DVI backpointers to locate the bop byte
   of a selected page.
*/

static int curreop;		/* position of eop byte of current page   */
static int currbop;		/* position of bop byte of current page   */
static int lastbop;		/* position of last bop byte              */
static int prevbop;		/* position of bop byte of previous page;
				   note that prevbop of first page = -1   */

static unsigned char DVIcommand;	/* holds next DVI command     */

static int maxstack;		/* max pushes over pops in DVI file      */
static int num = 0;		/* DVI numerator                         */
static int den = 0;		/* DVI denominator                       */
static double xconv = 0.0;	/* converts DVI units to X pixels        */
static double yconv = 0.0;	/* converts DVI units to Y pixels        */
static int h, v;		/* current pos on page in DVI units      */
static int w, x;		/* horizontal increments in DVI units    */
static int y, z;		/* vertical increments in DVI units      */
static int hh, vv;		/* h and v in pixels (approx)            */
static int hhh, vvv;		/* h and v rounded to nearest pixel      */
static int hstack[maxstacksize], vstack[maxstacksize];
    /* push down stacks for state values     */
static int wstack[maxstacksize], xstack[maxstacksize], ystack[maxstacksize],
  zstack[maxstacksize], hhstack[maxstacksize], vvstack[maxstacksize];
static int stackpos;
/* stacks empty when stackpos = 0, i.e., top of stacks = stackpos - 1 */
static int fontspace;		/* used in DoRight and DoDown            */


/*--------------------------------------------------------------------*/

/* Here are the functions used to get byte/s from DVIfile.
   They are essentially the same as those used in DVITYPE.
 */

static int 
GetDVIByte ()
{
  /* Returns the value (unsigned) of the byte at DVIoffset and
     advances DVIoffset for the next GetDVIByte.
   */

  int Result, buffstart, result;

  /* bufflen is a #define-d constant in "dvgt.h" */

  buffstart = DVIoffset / bufflen * bufflen;	/* 0, bufflen, 2*bufflen... */
  if (buffstart != currDVIbuff)
    {
      currDVIbuff = buffstart;
      result = lseek (DVIfile, buffstart, SEEK_SET);

      if (result != buffstart)
	{
	  FATAL ("lseek failed in GetDVIByte!");
	}
      result = read (DVIfile, DVIbuffer, bufflen);

      if (result == -1)
	{
	  FATAL ("read failed in GetDVIByte!");
	}
    }
  Result = DVIbuffer[DVIoffset - buffstart];
  DVIoffset++;
  return Result;
}
/* GetDVIByte */

/*--------------------------------------------------------------------*/

static int 
SignedDVIByte ()
{
  /* the next byte, signed */
  int b;

  b = GetDVIByte ();
  if (b < 128)
    return b;
  else
    return (b - 256);
}
/* SignedDVIByte */

/*--------------------------------------------------------------------*/

static int 
GetTwoDVIBytes ()
{
  /* the next 2 bytes, unsigned */
  int a, b;

  a = GetDVIByte ();
  b = GetDVIByte ();
  return (a * 256 + b);
}
/* GetTwoDVIBytes */

/*--------------------------------------------------------------------*/

static int 
SignedDVIPair ()
{
  /* the next 2 bytes, signed */
  int a, b;

  a = GetDVIByte ();
  b = GetDVIByte ();
  if (a < 128)
    return (a * 256 + b);
  else
    return ((a - 256) * 256 + b);
}
/* SignedDVIPair */

/*--------------------------------------------------------------------*/

static int 
GetThreeDVIBytes ()
{
  /* the next 3 bytes, unsigned */
  int a, b, c;

  a = GetDVIByte ();
  b = GetDVIByte ();
  c = GetDVIByte ();
  return ((a * 256 + b) * 256 + c);
}
/* GetThreeDVIBytes */

/*--------------------------------------------------------------------*/

static int 
SignedDVITrio ()
{
  /* the next 3 bytes, signed */
  int a, b, c;

  a = GetDVIByte ();
  b = GetDVIByte ();
  c = GetDVIByte ();
  if (a < 128)
    return ((a * 256 + b) * 256 + c);
  else
    return (((a - 256) * 256 + b) * 256 + c);
}
/* SignedDVIOqTrio */


/*--------------------------------------------------------------------*/

static int 
SignedDVIQuad ()
{
  /* the next 4 bytes, signed */
  int w;
  byte3 (w) = GetDVIByte ();
  byte2 (w) = GetDVIByte ();
  byte1 (w) = GetDVIByte ();
  byte0 (w) = GetDVIByte ();
  return (w);
}
/* SignedDVIQuad */

/*--------------------------------------------------------------------*/

static void 
ProcessPostamble ()
{
  /* Having successfully opened the DVI file, we find the postamble
     and initialize these global variables:
     lastbop, num, den, DVImag, maxstack, totalpages.
     The font definitions are read by ProcessFontDefs.
   */

  int postamblepos, postamble, pagehtplusdp, pagewidth;

  DVIoffset = postpostid - 4;
  postamblepos = SignedDVIQuad ();	/* get post_post's postamble ptr */
  DVIoffset = postamblepos;
  postamble = GetDVIByte ();
  lastbop = SignedDVIQuad ();
  num = SignedDVIQuad ();
  den = SignedDVIQuad ();
  DVImag = SignedDVIQuad ();
  pagehtplusdp = SignedDVIQuad ();
  pagewidth = SignedDVIQuad ();
  maxstack = SignedDVIPair ();
  totalpages = SignedDVIPair ();
  if (maxstack <= maxstacksize)
    return;

  FATAL ("Stack capacity exceeded!");
  /* now we don't need to test for stack overflow in DoPush */
}
/* ProcessPostamble */

/*--------------------------------------------------------------------*/

static void 
ProcessFontDefs ()
{
  /* Read the fntdef commands in the postamble (fntdef commands in the DVI
     pages will be skipped) and store the information in the font list.
     (Note that complete fontspecs are NOT built here because DVIReader does not
     want to know about the format or naming conventions of font files.)
     Since ProcessPostamble ended by reading the totalpages parameter, the
     next GetDVIByte should return nop or first fntdef.
   */

  int f = 0, c, s, d, a, l;	/* hold fntdef parameters */
  int i;
  char ch;			/* for getting farea and fname */
  String farea, fname;		/* a and l bytes long respectively */

  totalfonts = 0;		/* number of nodes in font list */
  fontlist = (fontinfo *) NULL;
  do
    {
      DVIcommand = GetDVIByte ();
      if (DVIcommand >= fntdef1 && DVIcommand <= fntdef1 + 3)
	{
	  switch (DVIcommand - fntdef1)
	    {
	    case 0:
	      f = GetDVIByte ();
	      break;

	    case 1:
	      f = GetTwoDVIBytes ();
	      break;

	    case 2:
	      f = GetThreeDVIBytes ();
	      break;

	    case 3:
	      f = SignedDVIQuad ();
	      break;
	    }
	  c = SignedDVIQuad ();	/* checksum; ignore it */
	  s = SignedDVIQuad ();	/* scaled size */
	  d = SignedDVIQuad ();	/* design size */
	  a = GetDVIByte ();	/* length of font area */
	  l = GetDVIByte ();	/* length of font name */

	  /* read and store font area */
	  for (i = 0; i < a; i++)
	    {
	      ch = GetDVIByte ();
	      if (i < maxfontspec)
		farea[i] = ch;
	    }
	  farea[i <= maxfontspec ? i : maxfontspec] = '\0';

	  /* read and store font name */
	  for (i = 0; i < l; i++)
	    {
	      ch = GetDVIByte ();
	      if (i < maxfontspec)
		fname[i] = ch;
	    }

	  fname[i <= maxfontspec ? i : maxfontspec] = '\0';

	  currfont = (fontinfo *) Malloc (sizeof (fontinfo));
	  currfont->fontused = false;
	  currfont->fontnum = f;
	  currfont->scaledsize = s;
	  currfont->designsize = d;
	  strncpy (currfont->fontarea, farea, maxstring);
	  currfont->fontarea[maxstring] = '\0';		/* to be careful */
	  currfont->fontarealen = a;
	  strncpy (currfont->fontname, fname, maxstring);
	  currfont->fontname[maxstring] = '\0';		/* to be careful */
	  currfont->fontnamelen = l;
	  currfont->fontspec[0] = '\0';
	  currfont->fontspeclen = 0;	/* fontspec is built in FontReader */
	  currfont->fontexists = false;		/* becomes TRUE if fontspec can be opened */
	  currfont->totalchars = 0;

	  /* first node allocated in DoFont */
	  currfont->charlist = (charinfo *) NULL;

	  /* nodes are added to tail of char list */
	  currfont->chartail = (charinfo *) NULL;

	  /* allocated once per font; see DoFont */
	  currfont->pixelptr = (_REC_pixeltable *) NULL;

	  currfont->nextfont = fontlist;
	  fontlist = currfont;	/* add new font to head of list */
	  totalfonts++;
	}
      else if (DVIcommand != nop)
	{
	  if (DVIcommand != postpost)
	    FATAL1 ("Unexpected DVI command in postamble = %d", DVIcommand);
	  /* we have reached end of postamble */
	}
    }
  while (DVIcommand != postpost);

  /* nop commands can occur between DVI commands */
}
/* ProcessFontDefs */

/*--------------------------------------------------------------------*/

void 
OpenDVIFile (char *name)
{
  /* If the given file can be opened and is a valid TeX82 DVI file then
     the following global variables are initialized:

     DVImag      := magnification value stored in DVI file (TeX's \mag)
     totalpages  := total number of pages in DVI file
     currDVIpage := 0      (and remains so until a page is selected)
     currTeXpage := ten 0s (ditto)
     totalfonts  := total number of fonts in DVI file (= nodes in font list)
     fontlist->  (nodes are added to head of list)
     fontused   := FALSE
     fontnum    := internal DVI font number
     scaledsize := scaled size of font (in DVI units)
     designsize := design size of font (in DVI units)
     fontarea   := a string of min (fontarealen, maxfontspec) characters
     fontname   := a string of min (fontnamelen, maxfontspec) characters
     fontspec   := a null string (fontspeclen := 0)
     fontexists := FALSE
     totalchars := 0
     charlist   := NIL
     chartail   := NIL
     pixelptr   := NIL
     nextfont   := next node in font list (if not NIL)
   */

#if 0    
  struct stat fstatbuf;
#endif

  currDVIbuff = -1;		/* impossible value for first GetDVIByte */
  DVIfile = open (name, O_RDONLY, 0);
  if (DVIfile >= 0)
    {
      int i;
#if 0
      fstat (DVIfile, &fstatbuf);
      if (S_ISDIR(fstatbuf.st_mode))
          FATAL1 ("`%s' is a directory\n", name);
      dvi_time = fstatbuf.st_mtime;
#endif

      /* get offset of last DVI byte */
      DVIoffset = lseek (DVIfile, 0, SEEK_END);
      if (DVIoffset == -1)
	FATAL ("Failed to find end of file!");
      if (DVIoffset == 0)
	FATAL ("DVI file is empty!");
      DVIoffset--;

      /* In the following, DVIoffset == 1 signifies the start of the file. */

      /* skip any NULs */
      while (GetDVIByte () == 0 && DVIoffset != 1)
	{
	  DVIoffset -= 2;	/* GetDVIByte increments */
	}
      DVIoffset--;

      /* skip 223s */
      while (GetDVIByte () == 223 && DVIoffset != 1)
	{
	  DVIoffset -= 2;	/* GetDVIByte increments */
	}

      DVIoffset--;
      postpostid = DVIoffset;	/* remember offset of id byte */
      if (GetDVIByte () != 2)
	{
	  FATAL ("Not a valid DVI file!");
	  /*NOTREACHED */
	  return;
	}
      ProcessPostamble ();	/* get DVImag, totalpages, etc */
      ProcessFontDefs ();	/* build and initialize font list */
      currDVIpage = 0;		/* we haven't processed a page yet */
      for (i = 0; i <= 9; i++)
	currTeXpage[i] = 0;

      return;
    }
  else
    FATAL1 ("Couldn't open DVI file: `%s'\n", name);
}
/* OpenDVIFile */
/*--------------------------------------------------------------------*/
#if 0
boolean
check_dvi_file (char *DVIname)
{
    struct stat statbuf;
    if (DVIname == NULL || stat (DVIname, &statbuf) != 0 || statbuf.st_mtime != dvi_time) {
        if (DVIname) {
            Fclose(DVIname);
            DVIname = NULL;
            if (list_fonts) Putchar('\n');

        }
    }
}
#endif
/*--------------------------------------------------------------------*/
static void 
SkipFntdef (int which)
{
  /* Read past a fntdef command without interpreting it. */

  int dummy, a, l, i;

  switch (which)
    {				/* which = DVIcommand - fntdef1 */
    case 0:
      dummy = GetDVIByte ();
      break;
    case 1:
      dummy = GetTwoDVIBytes ();
      break;
    case 2:
      dummy = GetThreeDVIBytes ();
      break;
    case 3:
      dummy = SignedDVIQuad ();
      break;
    }
  dummy = SignedDVIQuad ();
  dummy = SignedDVIQuad ();
  dummy = SignedDVIQuad ();

  a = GetDVIByte ();		/* length of directory */
  l = GetDVIByte ();		/* length of font name */
  for (i = 1; i <= l + a; i++)
    dummy = GetDVIByte ();
}
/* SkipFntdef */

/*--------------------------------------------------------------------*/

static void 
ReadFirstBop ()
{

  /* Read first bop by skipping past preamble; update currbop and currDVIpage. */
  int k, i, dummy;

  DVIoffset = 14;		/* position of preamble's k parameter */
  k = GetDVIByte ();		/* length of x parameter */
  for (i = 1; i <= k; i++)	/* skip preamble comment */
    dummy = GetDVIByte ();
  do
    {				/* skip any nops and fntdefs */
      DVIcommand = GetDVIByte ();
      if (DVIcommand != nop && DVIcommand != bop)
	{
	  if (DVIcommand >= fntdef1 && DVIcommand <= fntdef1 + 3)
	    SkipFntdef (DVIcommand - fntdef1);
	  else
	    FATAL1 ("Unexpected DVI command before first bop = %d.",
		    DVIcommand);
	}
      /* do nothing */
    }
  while (DVIcommand != bop);
  currbop = DVIoffset - 1;	/* position in DVI file of first bop */
  currDVIpage = 1;
}
/* ReadFirstBop */

/*--------------------------------------------------------------------*/

static void 
ReadNextBop ()
{
  /* We are currently positioned after an eop byte which we know is not the
     last.  This routine positions us after the next bop byte and updates
     currbop and currDVIpage.
   */

  /* skip any nops and fntdefs */
  do
    {
      DVIcommand = GetDVIByte ();
      if (DVIcommand != nop && DVIcommand != bop)
	{
	  if (DVIcommand >= fntdef1 && DVIcommand <= fntdef1 + 3)
	    SkipFntdef (DVIcommand - fntdef1);
	  else
	    FATAL1 ("Unexpected DVI command between eop and bop = %d.",
		    DVIcommand);
	}
      /* do nothing */
    }
  while (DVIcommand != bop);
  currbop = DVIoffset - 1;	/* position in DVI file of this bop */
  currDVIpage++;
}
/* ReadNextBop */

/*--------------------------------------------------------------------*/

static void 
ReadBopParameters ()
{

  /* We should now be positioned after the bop of desired page, so read
     the 10 TeX counters and update currTeXpage and prevbop.
     At the end of this routine we will be at the byte after currbop's parameters
     and ready to InterpretPage.
   */
  int i;

  for (i = 0; i <= 9; i++)
    currTeXpage[i] = SignedDVIQuad ();
  prevbop = SignedDVIQuad ();	/* position of previous bop in DVI file */
}
/* ReadBopParameters */

/*--------------------------------------------------------------------*/

void 
MoveToNextPage (boolean ascending)
{
  /* MoveToNextPage will select the next page, depending on the current page
     and the specified direction.  If the value of currDVIpage is 0 (set in
     OpenDVIFile), then MoveToNextPage will select the first page if ascending is
     TRUE and the last page if ascending is FALSE.  If currDVIpage is > 0 then
     MoveToNextPage will select currDVIpage+1 if ascending (unless currDVIpage =
     totalpages, in which case it does nothing), or currDVIpage-1 if descending
     (unless currDVIpage = 0).

     Before calling InterpretPage, DVItoVDU must position DVIReader to the
     desired page by calling one of the MoveTo... routines.
     The global variables updated if a page is located by any MoveTo... call are:
     currDVIpage := the current DVI page (1..totalpages)
     currTeXpage := the ten TeX counter values stored with this page
     Note that currDVIpage is initially 0 until one of these routines succeeds.
   */

  /* keep within limits of document */
  if ((currDVIpage != 1 && !ascending)
      || (currDVIpage != totalpages && ascending))
    {
      if (currDVIpage == 0)
	{			/* special value */
	  /* we haven't processed a page yet */
	  if (ascending)	/* get first page */
	    ReadFirstBop ();
	  else
	    {			/* get last page */
	      currbop = lastbop;
	      DVIoffset = currbop + 1;
	      currDVIpage = totalpages;
	    }
	}
      else
	{
	  if (ascending)
	    {
	      /* currently positioned after eop of currDVIpage, so get next bop */
	      ReadNextBop ();
	    }
	  else
	    {
	      /* move to bop pointed to by currbop's backpointer */
	      currbop = prevbop;
	      DVIoffset = currbop + 1;	/* move to byte after previous bop */
	      currDVIpage--;
	    }
	}
      ReadBopParameters ();	/* update currTeXpage and prevbop */
    }
}
/* MoveToNextPage */

/*--------------------------------------------------------------------*/

void 
MoveToDVIPage (int n)
{
  /* Move to nth DVI page; n should be in 1..totalpages. */

  if (n < 1 || n > totalpages)	/* do nothing */
    goto _L999;

  if (n == 1)
    {
      /* Note that this test must come before next test so that we avoid any
         problems when currDVIpage initially = 0. */
      ReadFirstBop ();
    }
  else if (n == currDVIpage + 1)
    {
      ReadNextBop ();
    }
  else
    {
      if (n < currDVIpage)
	{
	  currbop = prevbop;	/* start searching backwards from previous page */
	  currDVIpage--;
	}
      else if (n > currDVIpage)
	{
	  currbop = lastbop;	/* start searching backwards from last page */
	  currDVIpage = totalpages;
	}
      /* if n = currDVIpage we'll just move back to currbop */
      /* n is now <= currDVIpage so search by following backpointers */
      while (true)
	{
	  if (n == currDVIpage)
	    {
	      DVIoffset = currbop + 1;	/* move to byte after currbop */
	      break;		/* escape from "infinite" loop */
	    }
	  DVIoffset = currbop + 41;	/* move to location of backpointer */
	  currbop = SignedDVIQuad ();	/* get location of previous page */
	  currDVIpage--;
	}			/* while */
    }				/* if */
  ReadBopParameters ();		/* update currTeXpage and prevbop */
_L999:;
}
/* MoveToDVIPage */

/*--------------------------------------------------------------------*/

static boolean 
CurrMatchesNew (TeXpageinfo * newTeXpage)
{
  /* Return TRUE iff currTeXpage matches newTeXpage. */

  boolean Result;
  int i, FORLIM;

  Result = true;
  FORLIM = newTeXpage->lastvalue;
  for (i = 0; i <= FORLIM; i++)
    {
      if (newTeXpage->present[i])
	{
	  if (newTeXpage->value[i] != currTeXpage[i])
	    Result = false;
	}
    }
  return Result;
}
/* CurrMatchesNew */

/*--------------------------------------------------------------------*/

boolean 
MoveToTeXPage (TeXpageinfo * newTeXpage)
{
  /* MoveToTeXPage will search for the lowest DVI page matching the given
     TeX page specification.

     (TeX stores the values of \count0,\count1, ..., \count9 with every DVI
     page.  Plain TeX uses \count0 to control page numbering.)

     newTeXpage is a VAR parameter only for efficiency; it won't be changed.
     The value array stores the requested counter values, the present array
     indicates which counters are relevant and lastvalue indicates
     the position (0..9) of the last relevant counter.

     DVItoVDU converts a more friendly representation
     of a TeX page request into the TeXpageinfo format.  For example,
     [2..5] would be converted to:     and [] would be converted to:
     value     = [2,?,5,?,?,?,?,?,?,?]     value     = [?,?,?,?,?,?,?,?,?,?]
     present   = [T,F,T,?,?,?,?,?,?,?]     present   = [F,?,?,?,?,?,?,?,?,?]
     lastvalue =      2                    lastvalue =  0
     MoveToTeXPage returns TRUE iff the requested TeX page is located.
   */

  boolean Result;

  int savecurrbop = 0, savecurrDVIpage, nextbop, i;
  boolean atleastone;

  /* save away current page and DVI position */
  savecurrDVIpage = currDVIpage;
  if (currDVIpage != 0)		/* only if we've processed a page */
    savecurrbop = currbop;
  /* note that curreop is saved in last InterpretPage */
  /* search backwards through all DVI pages for lowest matching page */
  atleastone = false;
  nextbop = lastbop;
  for (i = totalpages; i >= 1; i--)
    {
      DVIoffset = nextbop + 1;
      ReadBopParameters ();	/* update currTeXpage and prevbop */
      if (CurrMatchesNew (newTeXpage))
	{
	  currbop = nextbop;
	  currDVIpage = i;
	  atleastone = true;
	}
      nextbop = prevbop;
    }
  if (!atleastone)
    {				/* no match, so restore currDVIpage */
      currDVIpage = savecurrDVIpage;
      if (currDVIpage != 0)
	{			/* restore page and positioning info */
	  currbop = savecurrbop;
	  DVIoffset = currbop + 1;
	  ReadBopParameters ();	/* restore currTeXpage and prevbop */
	  DVIoffset = curreop + 1;
	  /* we should now be after the eop byte of the original page */
	}
      Result = false;
      goto _L999;
    }
  DVIoffset = currbop + 1;
  ReadBopParameters ();		/* update currTeXpage and prevbop */
  Result = true;
_L999:
  return Result;

  /* we found lowest matching page */
}
/* MoveToTeXPage */

/*--------------------------------------------------------------------*/

void 
SetConversionFactors (double xres, double yres, double magnification)
{
/* Arguments:
   xres:  horizontal resolution of output device in dots per inch.
   yres:  vertical resolution.
   magnification:  magnification of document;  1 means normal size.
 */
  /* This routine must be called before the first InterpretPage call.
     DVIReader needs to know the resolution and magnification values
     before it attempts to convert DVI units into pixel values.
   */

  /* GT - The "254000.0" corresponds to ten thousand units per mm. */
  /* Refer to TUG's DVI Driver standard. */

  xconv = (num / 254000.0 * xres / den * magnification);
  yconv = (num / 254000.0 * yres / den * magnification);
}

/* SetConversionFactors */

/*--------------------------------------------------------------------*/

static int 
Sign (double d)
{
  return (d < 0.0 ? -1 : 1);
}
/* Sign */

/*--------------------------------------------------------------------*/

static void 
InitStateValues ()
{
  /* Initialize state values and stack. */

  /* This is the only place in "dvireader.c" */
  /* where hoffset, voffset appear.          */
  /* The offsets are set in "options.c". */
  /* The other extern variables are static in "dvireader.c". */

  double hox, voy;		/* offsets divided by conversion factors */

  hox = hoffset / xconv;
  voy = voffset / yconv;

  hh = hoffset;			/* 0 if no horizontal shift specified */
  vv = voffset;			/* 0 if no vertical shift specified */
  h = Sign (hox) * (int) ((hox >= 0.0 ? hox : -hox) + 0.5);
  v = Sign (voy) * (int) ((voy >= 0.0 ? voy : -voy) + 0.5);
  w = 0;
  x = 0;
  y = 0;
  z = 0;
  stackpos = 0;
  fontspace = 0;		/* for DoRight and DoDown before a DoFont call */
}

/* InitStateValues */

/*--------------------------------------------------------------------*/

static void 
InitPage ()
{
  /* Initialize page so that there are no fonts, chars, rules, specials. */

  pageempty = true;		/* gt - assumed until a char or rule is found */

  /* page edges will change if there is at least one char or rule on page */

  /* gt - these shouldn't be necessary, but to be on the safe side ... */
  minhp = 0;
  minvp = 0;
  maxhp = 0;
  maxvp = 0;

  currfont = fontlist;
  while (currfont != (fontinfo *) NULL)
    {
      if (currfont->fontused)
	{			/* only reset those fonts used in last page */
	  currfont->fontused = false;

	  /* deallocate char list completely; DoFont will allocate first node */
	  currfont->totalchars = 0;
	  while (currfont->charlist != (charinfo *) NULL)
	    {
	      charinfo *thischar;	/* temporary pointer to node in charlist */
	      thischar = currfont->charlist;
	      currfont->charlist = thischar->nextchar;
	      Free (thischar, charinfo);
	    }
	  currfont->chartail = (charinfo *) NULL;
	  /* pixel table remains allocated */
	}
      currfont = currfont->nextfont;
    }
  currfont = (fontinfo *) NULL;	/* current font's undefined at start of page */

  /* deallocate rule information except for one node (for DoSet/PutRule) */
  totalrules = 0;
  while (rulelist != ruletail)
    {
      ruleinfo *thisrule;	/* temporary pointer to node in rulelist */
      thisrule = rulelist;
      rulelist = thisrule->nextrule;
      Free (thisrule, ruleinfo);
    }
  rulelist->rulecount = 0;	/* no rules in this node */
  rulelist->nextrule = (ruleinfo *) NULL;

  /* deallocate \special information */
  while (speciallist != (specialinfo *) NULL)
    {
      specialinfo *thisspecial;	/* temporary ptr to node in speciallist */
      thisspecial = speciallist;
      speciallist = speciallist->nextspecial;
      Free (thisspecial, specialinfo);
    }
}
/* InitPage */

/*--------------------------------------------------------------------*/

int 
XPixelRound (int DVIunits)
{
  /* Return the nearest number of X pixels in the given DVI X dimension. */

  /* According to the DVI Driver Standard, Level 0, draft 0.05, */
  /* section 2.6.2, "Changes in position due to characters and  */
  /* rules",                                                    */
  /* pixel_round (n) = sign (Kn) * floor (abs (Kn) + 0.5),      */
  /* where sign (i) = -1 if i < 0 and = 1 if i >= 0,            */
  /* and K is xconv for horizontal and yconv for vertical       */
  /* dimensions.                                                */

  double Kn = xconv * (double) DVIunits;
  return (Sign (Kn) * (int) ((Kn >= 0.0 ? Kn : -Kn) + 0.5));
}

/* XPixelRound */

/*--------------------------------------------------------------------*/

int 
YPixelRound (int DVIunits)
{
  /* Return the nearest number of Y pixels in the given DVI Y dimension. */

  /* According to the DVI Driver Standard, Level 0, draft 0.05, */
  /* section 2.6.2, "Changes in position due to characters and  */
  /* rules",                                                    */
  /* pixel_round (n) = sign (Kn) * floor (abs (Kn) + 0.5),      */
  /* where sign (i) = -1 if i < 0 and = 1 if i >= 0,            */
  /* and K is xconv for horizontal and yconv for vertical       */
  /* dimensions.                                                */

  double Kn = yconv * (double) DVIunits;
  return (Sign (Kn) * (int) ((Kn >= 0.0 ? Kn : -Kn) + 0.5));
}

/* YPixelRound */

/*--------------------------------------------------------------------*/

static void 
DoSetPutChar (int ch, boolean set)
{
  /* Add char info to current chartable, update our horizontal
     position on the page and check the page edges.
   */
  /* putchar: Exactly the same as setchar, but we DON'T update the horizontal
     position on the page.  (We still have to check page edges.)
   */

  if (ch > maxTeXchar)
    {				/* ignore ch */
      String codestr;
      sprintf (codestr, "%d", ch);
      MesgString ("Unknown character (code ");
      MesgString (codestr);
      MesgString (") from ");
      MesgString (currfont->fontspec);
    }
  else
    {				/* ch is valid */
      charinfo *char_info = currfont->chartail;		/* may be new chartable */
      _REC_chartable *char_tab = (_REC_chartable *) NULL;
      _REC_pixeltable *pix_tab = (_REC_pixeltable *) NULL;

      if (char_info->charcount == chartablesize)
	{
	  char_info->nextchar = (charinfo *) Malloc (sizeof (charinfo));
	  /* add new node to end of char list */
	  char_info->nextchar->charcount = 0;	/* reset charcount */
	  char_info->nextchar->nextchar = (charinfo *) NULL;
	  currfont->chartail = char_info->nextchar;
	}
      /* allocate a new chartable */

      char_info = currfont->chartail;
      char_tab = &char_info->chartable[char_info->charcount];
      char_tab->hp = hh;
      char_tab->vp = vv;
      char_tab->code = ch;

      pix_tab = &currfont->pixelptr[ch];

      /* do page edges increase? */

      if (pageempty || hh - pix_tab->xo < minhp)
	minhp = hh - pix_tab->xo;
      if (pageempty || vv - pix_tab->yo < minvp)
	minvp = vv - pix_tab->yo;
      if (pageempty || hh + pix_tab->wd - pix_tab->xo - 1 > maxhp)
	maxhp = hh + pix_tab->wd - pix_tab->xo - 1;
      if (pageempty || vv + pix_tab->ht - pix_tab->yo - 1 > maxvp)
	maxvp = vv + pix_tab->ht - pix_tab->yo - 1;

      /* the above checks ensure that page edges include all black
         pixels in glyph, but we also want to include reference point */

      if (pageempty || hh < minhp)
	minhp = hh;
      if (pageempty || vv < minvp)
	minvp = vv;
      if (pageempty || hh > maxhp)
	maxhp = hh;
      if (pageempty || vv > maxvp)
	maxvp = vv;

      if (set)
	{
	  /* add DVI width calculated in PixelTableRoutine */
	  h += pix_tab->dwidth;

	  /* add pixel width calculated in PixelTableRoutine */
	  hh += pix_tab->pwidth;

	  /* use hhh and max_drift to prevent hh drifting too far from h */

	  hhh = XPixelRound (h);

	  if (abs (hhh - hh) > max_drift)
	    {
	      /* Adjust hh to be just within the allowed tolerance from h.  */
	      hh = hhh + (hhh > hh ? -max_drift : max_drift);
	    }
	}
      currfont->totalchars++;
      char_info->charcount++;
      pageempty = false;
    }
}
/* DoSetChar */


/*--------------------------------------------------------------------*/

static void 
DoPush ()
{
  /* Push state values onto stack.
     No need to test for stack overflow since we compare maxstack and
     maxstacksize in ProcessPostamble.
   */

  hstack[stackpos] = h;
  vstack[stackpos] = v;
  wstack[stackpos] = w;
  xstack[stackpos] = x;
  ystack[stackpos] = y;
  zstack[stackpos] = z;
  hhstack[stackpos] = hh;
  vvstack[stackpos] = vv;
  stackpos++;
}

/* DoPush */

/*--------------------------------------------------------------------*/

static void 
DoPop ()
{
  /* Pop state values from top of stack. */

  if (stackpos == 0)
    {
      FATAL ("Stack empty!");
    }
  stackpos--;
  h = hstack[stackpos];
  v = vstack[stackpos];
  w = wstack[stackpos];
  x = xstack[stackpos];
  y = ystack[stackpos];
  z = zstack[stackpos];
  hh = hhstack[stackpos];
  vv = vvstack[stackpos];
}
/* DoPop */

/*--------------------------------------------------------------------*/

static void 
DoRight (int amount)
{
  /* Move the reference point horizontally by given amount (usually +ve).
     When the amount is small, like a kern, hh changes by rounding
     the amount; but when the amount is large, hh changes by rounding
     the true position h so that accumulated rounding errors disappear.
   */

  h += amount;

  /* GT - Wrong?  DVI Driver Standard specifies:
     "if (processor uses TFM)
     word_space = space - space_shrink;
     else
     quad = magnification * (design size of font);
     word_space = 0.2 * quad;
     fi;

     back_space = 0.9 * quad;

     if (amount < word_space && amount > -back_space)".

     So,     TFM -> space, space_shrink;
     else -> design size.

     Now, how do we learn those quantities?
   */

  if (amount < fontspace && amount > -fontspace * 4)
    {
      hh += XPixelRound (amount);

      /* use hhh and max_drift to prevent hh drifting too far from h */

      hhh = XPixelRound (h);
      if (abs (hhh - hh) > max_drift)
	{
	  /* Adjust hh to be just within the allowed tolerance from h.  */
	  hh = hhh + (hhh > hh ? -max_drift : max_drift);
	}
    }
  else
    {
      hh = XPixelRound (h);
    }
}
/* DoRight */

/*--------------------------------------------------------------------*/

static void 
DoDown (int amount)
{
  /* Move the reference point vertically by given amount (usually +ve).
     Rounding is done similarly to DoRight but with the threshold between
     small and large amounts increased by a factor of 5.
   */

  v += amount;

  /* GT - Wrong?  DVI Driver Standard specifies:
     "if (abs (y) < 0.8 * quad)".
   */

  if (abs (amount) < fontspace * 5)
    {
      vv += YPixelRound (amount);

      /* use vvv and max_drift to prevent vv drifting too far from v */

      vvv = YPixelRound (v);
      if (abs (vvv - vv) > max_drift)
	{
	  /* Adjust vv to be just within the allowed tolerance from v.  */
	  vv = vvv + (vvv > vv ? -max_drift : max_drift);
	}
    }
  else
    {
      vv = YPixelRound (v);
    }
}
/* DoDown */

/*--------------------------------------------------------------------*/

static int 
RulePixels (double conv, int DVIunits)
{
  /* Return the number of pixels in the given height or width of a rule
     using the method recommended in DVITYPE.
   */

  int n = (int) (conv * DVIunits);
  if (n < conv * DVIunits)
    return (n + 1);
  else
    return n;
}
/* RulePixels */

/*--------------------------------------------------------------------*/

static void 
DoPutSetRule (int height, int width, unsigned char DVIcommand)
{
  /* Exactly the same as DoSetRule, but we DON'T update the horizontal
     position on the page.  (We still have to check page edges.)
   */
  _REC_ruletable *ruletab;

  if (height > 0 && width > 0)
    {

      /* may be new ruletable */
      if (ruletail->rulecount == ruletablesize)
	{
	  ruletail->nextrule = (ruleinfo *) Malloc (sizeof (ruleinfo));
	  /* add new node to end of rule list */
	  ruletail->nextrule->rulecount = 0;	/* reset rulecount */
	  ruletail->nextrule->nextrule = (ruleinfo *) NULL;
	  ruletail = ruletail->nextrule;
	}
      /* allocate a new ruletable */
      ruletab = &ruletail->ruletable[ruletail->rulecount];
      ruletab->hp = hh;
      ruletab->vp = vv;
      ruletab->wd = RulePixels (xconv, width);
      ruletab->ht = RulePixels (yconv, height);

      /* do page edges increase? */

      /* ref pt of rule is bottom left black pixel */

      if (pageempty || hh < minhp)
	minhp = hh;
      if (pageempty || vv > maxvp)
	maxvp = vv;

      if (pageempty || hh + ruletab->wd - 1 > maxhp)
	maxhp = hh + ruletab->wd - 1;
      if (pageempty || vv - ruletab->ht + 1 < minvp)
	minvp = vv - ruletab->ht + 1;

      totalrules++;
      ruletail->rulecount++;
      pageempty = false;
    }

  if (DVIcommand == setrule)
    {
      hh += RulePixels (xconv, width);
      h += width;

      /* use hhh and max_drift to prevent hh drifting too far from h */

      hhh = XPixelRound (h);

      if (abs (hhh - hh) > max_drift)
	{
	  /* Adjust hh to be just within the allowed tolerance from h.  */
	  hh = hhh + (hhh > hh ? -max_drift : max_drift);
	}
    }
}				/* DoPutSetRule */

/*--------------------------------------------------------------------*/

static void 
DoFont (int externf)
{
  /* Search font list for externf, setting currfont and fontspace.
     If this is the first time we've seen this font (on current page) then
     we need to allocate the first chartable.
     If this is the first time we've seen this font used at all then we
     allocate a pixeltable and call routine to fill it in.
   */

  charinfo *char_info = (charinfo *) NULL;

  currfont = fontlist;
  while (currfont != (fontinfo *) NULL && currfont->fontnum != externf)
    {
      currfont = currfont->nextfont;
    }

  if (currfont == NULL)
    FATAL1 ("Failed to find font #%d.", externf);
  if (!currfont->fontused)
    {
      currfont->fontused = true;
      currfont->charlist = (charinfo *) Malloc (sizeof (charinfo));
      /* allocate first chartable */
      char_info = currfont->charlist;
      char_info->charcount = 0;	/* for DoSet/PutChar */
      char_info->nextchar = (charinfo *) NULL;	/* this node is also last */
      currfont->chartail = currfont->charlist;
      if (currfont->pixelptr == (_REC_pixeltable *) NULL)
	{			/* first time we've seen this font */
	  currfont->pixelptr = (_REC_pixeltable *) Malloc (sizeof (pixeltable));
	  PixelTableRoutine (currfont);
	}
    }
  /* do nothing since we've already used this font on this page */

  fontspace = currfont->scaledsize / 6;

  /* See DVITYPE; a 3-unit thin space.
     Note that a thin space is 1/6 of a quad, where a quad is
     1 em in the current font and usually equals the design size.
   */

}
/* DoFont */

/*--------------------------------------------------------------------*/

static void 
DoSpecial (int hpos, int vpos, int totalbytes)
{
  /* in */

  /* DVIReader has seen a \special command while interpreting the current page.
     It will pass the current page position and number of bytes in the command.
     We save the info away in speciallist for later use by the main program.
   */
  int i;
  specialinfo *temp;

  temp = (specialinfo *) Malloc (sizeof (specialinfo));
  for (i = 0; i < totalbytes; i++)
    {
      int temp1;
      temp1 = GetDVIByte ();
      if (i < maxspeciallen)
	temp->special[i] = temp1;
    }
  temp->special[i <= maxspeciallen ? i : maxspeciallen] = '\0';

  temp->hp = hpos;
  temp->vp = vpos;
  temp->nextspecial = speciallist;
  speciallist = temp;		/* add new info to head of list */
}
/* DoSpecial */

/*--------------------------------------------------------------------*/

void 
InterpretPage ()
{
  /* When this routine is called we are positioned after the bytes of a bop
     command (i.e., at currbop + 45).  At the end of this routine we will be
     positioned after the eop byte for the current page.  In between we carry
     out the important task of translating the DVI description of this page
     and filling in the following global data structures:

     totalrules := number of rules on page
     rulelist-> (nodes are added to tail of rule list)
     rulecount  := number of rules in this ruletable
     ruletable[0..rulecount-1].
     hp, vp  := reference point of a rule
     wd, ht  := pixel dimensions of a rule (both > 0)
     nextrule   := next node in rule list (if not NIL)
     ruletail   := pointer to last node in rule list

     speciallist-> (nodes are added to head of this list)
     hp, vp      := reference point on page
     special     := \special bytes
     nextspecial := next node in list (if not NIL)

     fontlist->
     (the following fontinfo is relevant only if fontused is TRUE)
     totalchars := number of chars on page from this font
     charlist-> (nodes are added to tail of char list)
     charcount  := number of chars in this chartable
     chartable[0..charcount-1].
     hp, vp  := reference point of a character
     code    := TeX character code (and index into pixel table)
     nextchar   := next node in char list (if not NIL)
     chartail   := pointer to last node in char list
     pixelptr^[0..maxTeXchar].
     (filled in by FontReader's PixelTableRoutine)
     wd, ht  := glyph width and height in pixels
     xo, yo  := offsets from the character's reference point
     dwidth  := advance width in DVI units
     pwidth  := advance width in pixels
     mapadr  := offset in fontspec of the glyph's bitmap info
     bitmap  := NIL
     nextfont   := next node in font list (if not NIL)

     pageempty  := TRUE iff the page has no rules and no characters
     minhp, minvp, maxhp, maxvp
     := the edges of the page (undefined if pageempty is TRUE)
     They define the smallest rectangle containing all black
     pixels on the page;(minhp,minvp) is the top left corner.

     Reference points for rules and characters are stored as a pair of
     horizontal and vertical pixel coordinates.  The point (0,0) is assumed
     to be the pixel 1 inch in from the top and left edges of an imaginary sheet
     of paper.  Horizontal coordinates increase to the right and vertical
     coordinates increase down the paper.
     The numbers of pixels per inch in the X and Y directions are defined by
     the xres and yres resolution parameters given to SetConversionFactors.
   */

  int param = 0, ht, wd;

  InitStateValues ();
  InitPage ();
  do
    {
      DVIcommand = GetDVIByte ();
      /* For efficiency reasons the most frequent commands should be tested 1st.
         The following order is the result of frequency testing on typical
         DVI files.  Note that the most frequent commands in the DVI file
         generated by TeX 1.3 for the Dec. 1983 LaTeX manual were:
         <set1, w0, right3, push/pop, x0, w3, y0, fntnum25, right2, fntnum31,
         down3, x2, right4, w2, x3, down4, z0, fntnum8, setrule, y3, etc.
       */
      if (DVIcommand < set1)	/* 0..127 */
	DoSetPutChar (DVIcommand, true);
      else if (DVIcommand == w0)
	DoRight (w);
      else if (DVIcommand == right3)
	DoRight (SignedDVITrio ());
      else if (DVIcommand == push)
	DoPush ();
      else if (DVIcommand == pop)
	DoPop ();
      else if (DVIcommand == x0)
	DoRight (x);
      else if (DVIcommand == w3)
	{
	  w = SignedDVITrio ();
	  DoRight (w);
	}
      else if (DVIcommand == y0_)
	DoDown (y);
      else if (DVIcommand > z4 && DVIcommand < fnt1)
	{
	  /* catch all the remaining movement commands */
	  DoFont (DVIcommand - fntnum0);
	}
      else if (DVIcommand > pop && DVIcommand < fntnum0)
	{
	  if (DVIcommand == right2)
	    DoRight (SignedDVIPair ());
	  else if (DVIcommand == right4)
	    DoRight (SignedDVIQuad ());
	  else if (DVIcommand == x2)
	    {
	      x = SignedDVIPair ();
	      DoRight (x);
	    }
	  else if (DVIcommand == x3)
	    {
	      x = SignedDVITrio ();
	      DoRight (x);
	    }
	  else if (DVIcommand == down3)
	    DoDown (SignedDVITrio ());
	  else if (DVIcommand == down4)
	    DoDown (SignedDVIQuad ());
	  else if (DVIcommand == w2)
	    {
	      w = SignedDVIPair ();
	      DoRight (w);
	    }
	  else if (DVIcommand == z0)
	    DoDown (z);
	  else if (DVIcommand == y3)
	    {
	      y = SignedDVITrio ();
	      DoDown (y);
	    }
	  else if (DVIcommand == z3)
	    {
	      z = SignedDVITrio ();
	      DoDown (z);
	    }
	  else if (DVIcommand == down2)
	    {
	      /* the next DVI commands are used very rarely (by TeX 1.3 at least) */
	      DoDown (SignedDVIPair ());
	    }
	  else if (DVIcommand == w1)
	    {
	      w = SignedDVIByte ();
	      DoRight (w);
	    }
	  else if (DVIcommand == w4)
	    {
	      w = SignedDVIQuad ();
	      DoRight (w);
	    }
	  else if (DVIcommand == x1)
	    {
	      x = SignedDVIByte ();
	      DoRight (x);
	    }
	  else if (DVIcommand == x4)
	    {
	      x = SignedDVIQuad ();
	      DoRight (x);
	    }
	  else if (DVIcommand == y1_)
	    {
	      y = SignedDVIByte ();
	      DoDown (y);
	    }
	  else if (DVIcommand == y2)
	    {
	      y = SignedDVIPair ();
	      DoDown (y);
	    }
	  else if (DVIcommand == y4)
	    {
	      y = SignedDVIQuad ();
	      DoDown (y);
	    }
	  else if (DVIcommand == z1)
	    {
	      z = SignedDVIByte ();
	      DoDown (z);
	    }
	  else if (DVIcommand == z2)
	    {
	      z = SignedDVIPair ();
	      DoDown (z);
	    }
	  else if (DVIcommand == z4)
	    {
	      z = SignedDVIQuad ();
	      DoDown (z);
	    }
	  else if (DVIcommand == right1)
	    DoRight (SignedDVIByte ());
	  else if (DVIcommand == down1)
	    DoDown (SignedDVIByte ());
	  else
	    FATAL ("Bug in InterpretPage!");
	}
      else if (DVIcommand == setrule || DVIcommand == putrule)
	{
	  ht = SignedDVIQuad ();
	  wd = SignedDVIQuad ();
	  DoPutSetRule (ht, wd, DVIcommand);
	}
      else if (DVIcommand >= put1 && DVIcommand <= put1 + 3)
	{
	  switch (DVIcommand - put1)
	    {
	    case 0:
	      DoSetPutChar (GetDVIByte (), false);
	      break;
	    case 1:
	      DoSetPutChar (GetTwoDVIBytes (), false);
	      break;
	    case 2:
	      DoSetPutChar (GetThreeDVIBytes (), false);
	      break;
	    case 3:
	      DoSetPutChar (SignedDVIQuad (), false);
	      break;
	    }

	}
      else if (DVIcommand >= set1 && DVIcommand <= set1 + 3)
	{
	  switch (DVIcommand - set1)
	    {
	    case 0:
	      DoSetPutChar (GetDVIByte (), true);
	      break;
	    case 1:
	      DoSetPutChar (GetTwoDVIBytes (), true);
	      break;
	    case 2:
	      DoSetPutChar (GetThreeDVIBytes (), true);
	      break;
	    case 3:
	      DoSetPutChar (SignedDVIQuad (), true);
	      break;
	    }

	}
      else if (DVIcommand >= fnt1 && DVIcommand <= fnt1 + 3)
	{
	  switch (DVIcommand - fnt1)
	    {
	    case 0:
	      DoFont (GetDVIByte ());
	      break;
	    case 1:
	      DoFont (GetTwoDVIBytes ());
	      break;
	    case 2:
	      DoFont (GetThreeDVIBytes ());
	      break;
	    case 3:
	      DoFont (SignedDVIQuad ());
	      break;
	    }

	}
      else if (DVIcommand >= xxx1 && DVIcommand <= xxx1 + 3)
	{
	  switch (DVIcommand - xxx1)
	    {
	    case 0:
	      param = GetDVIByte ();
	      break;
	    case 1:
	      param = GetTwoDVIBytes ();
	      break;
	    case 2:
	      param = GetThreeDVIBytes ();
	      break;
	    case 3:
	      param = SignedDVIQuad ();
	      break;
	    }
	  /* pass current pixel position and number of bytes */
	  DoSpecial (hh, vv, param);
	}
      else if (DVIcommand >= fntdef1 && DVIcommand <= fntdef1 + 3)
	SkipFntdef (DVIcommand - fntdef1);
      else if (DVIcommand != nop)
	{
	  if (DVIcommand != eop)	/* do nothing */
	    FATAL1 ("Unexpected DVI command while interpreting page = %d.",
		    DVIcommand);
	}
    }
  while (DVIcommand != eop);

  /* save position of eop byte for use in MoveToTeXPage */
  curreop = DVIoffset - 1;

  if (stackpos != 0)
    FATAL ("Stack not empty at eop!");
  /* InitPage values */


  /* fntnum0..fntnum63 */


  /* skip fntdef command since we've got this info from postamble */

  /* do nothing */
}
/* InterpretPage */

/*--------------------------------------------------------------------*/

void 
SortFonts (fontinfo ** unusedlist)
{
  /* out */

  /* Sort fontlist in order of ascending totalchars.
     Fonts with least characters can then be accessed first.
     Since the number of fonts used on a typical page is quite small, a simple
     sorting algorithm should be good enough.  Note that unused fonts are moved
     to the end of the list and unusedlist points to the first such node.
     DVItoVDU need only process fonts up to (but excluding) unusedlist
     and does not have to worry about checking the fontused flag.
     If unusedlist is NIL then either 1) all fonts are used on the current page
     or 2) fontlist is also NIL (totalfonts = 0).
   */
  fontinfo *newfontlist, *prevfont, *largest, *prevlargest;
  int mostchars;

  newfontlist = (fontinfo *) NULL;
  /* go thru fontlist once and move all unused fonts to head of newfontlist */
  prevfont = (fontinfo *) NULL;
  currfont = fontlist;
  while (currfont != (fontinfo *) NULL)
    {
      if (currfont->fontused)
	{
	  prevfont = currfont;	/* remember previous node */
	  currfont = currfont->nextfont;
	  continue;
	}
      /* move node from fontlist to head of newfontlist
         and don't change prevfont
       */
      if (prevfont == (fontinfo *) NULL)
	{
	  fontlist = currfont->nextfont;	/* remove first node in fontlist */
	  currfont->nextfont = newfontlist;
	  newfontlist = currfont;
	  currfont = fontlist;
	}
      else
	{
	  prevfont->nextfont = currfont->nextfont;
	  currfont->nextfont = newfontlist;
	  newfontlist = currfont;
	  currfont = prevfont->nextfont;
	}
    }

  /* unusedlist will be last unused font moved to newfontlist.  It will be NIL
     if either fontlist is NIL or all fonts are used.
   */

  *unusedlist = newfontlist;

  /* Now go thru fontlist repeatedly moving node with max totalchars to
     head of newfontlist until fontlist is exhausted.
   */

  while (fontlist != (fontinfo *) NULL)
    {
      prevfont = (fontinfo *) NULL;
      currfont = fontlist;
      prevlargest = (fontinfo *) NULL;
      largest = fontlist;
      mostchars = 0;

      /* search for largest totalchars */
      while (currfont != (fontinfo *) NULL)
	{
	  if (currfont->totalchars > mostchars)
	    {
	      prevlargest = prevfont;
	      largest = currfont;
	      mostchars = currfont->totalchars;
	    }
	  prevfont = currfont;
	  currfont = currfont->nextfont;
	}
      /* move largest node from fontlist to head of newfontlist */
      if (prevlargest == (fontinfo *) NULL)
	fontlist = largest->nextfont;	/* remove first node in fontlist */
      else
	prevlargest->nextfont = largest->nextfont;
      largest->nextfont = newfontlist;
      newfontlist = largest;
    }
  fontlist = newfontlist;
  /* used fonts now sorted and unused fonts at end */
}
/* SortFonts */

/*--------------------------------------------------------------------*/

void 
CloseDVIFile ()
{
  /* Close the currently open DVI file,
     and deallocate dynamic data structures. */

  int result;

  result = close (DVIfile);
  while (fontlist != (fontinfo *) NULL)
    {
      currfont = fontlist;
      while (currfont->charlist != (charinfo *) NULL)
	{
	  charinfo *thischar;	/* temporary pointer to node in charlist */
	  thischar = currfont->charlist;
	  currfont->charlist = thischar->nextchar;
	  Free (thischar, charinfo);	/* deallocate char list */
	}
      if (currfont->pixelptr != (_REC_pixeltable *) NULL)
	Free (currfont->pixelptr, _REC_pixeltable);	/* deallocate pixel table */
      fontlist = currfont->nextfont;
      Free (currfont, fontinfo);	/* deallocate font information */
    }
  /* Deallocate rule information except for one node
     (in case DVItoVDU ever opens another DVI file).
   */
  while (rulelist != ruletail)
    {
      ruleinfo *thisrule;	/* temporary pointer to node in rulelist */
      thisrule = rulelist;
      rulelist = thisrule->nextrule;
      Free (thisrule, ruleinfo);
    }
  while (speciallist != (specialinfo *) NULL)
    {
      specialinfo *thisspecial;	/* temporary ptr to node in speciallist */
      thisspecial = speciallist;
      speciallist = speciallist->nextspecial;
      Free (thisspecial, specialinfo);
    }
}
/* CloseDVIFile */

/*--------------------------------------------------------------------*/

void 
InitDVIReader ()
{
  totalrules = 0;
  rulelist = (ruleinfo *) Malloc (sizeof (ruleinfo));	/* for first InitPage */
  ruletail = rulelist;		/* ditto */
  speciallist = (specialinfo *) NULL;	/* ditto */
  fontlist = (fontinfo *) NULL;	/* safer for CloseDVIFile */
}
/* InitDVIReader */

/*--------------------------------------------------------------------*/

/* end dvireader.c */