summaryrefslogtreecommitdiff
path: root/Build/source/texk/windvi/dvi-draw.c
blob: ffb9afe3980d2dc2a1b32c9be3e0126be3f78670 (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
/*========================================================================*\

Copyright (c) 1990-1999  Paul Vojta

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
PAUL VOJTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

NOTE:
	xdvi is based on prior work, as noted in the modification history
	in xdvi.c.

\*========================================================================*/
#define HAVE_BOOLEAN
#include "xdvi-config.h"
#include <kpathsea/c-ctype.h>
#include <kpathsea/c-fopen.h>
#include <kpathsea/c-vararg.h>
#include "dvi.h"

#ifdef TRANSFORM
extern BOOL bTransfInUse;
#endif

static	struct frame	frame0;		/* dummy head of list */
#ifdef	TEXXET
static	struct frame	*scan_frame;	/* head frame for scanning */
#endif

#ifndef	DVI_BUFFER_LEN
#define	DVI_BUFFER_LEN	512
#endif

static	ubyte	dvi_buffer[DVI_BUFFER_LEN];
static	struct frame	*current_frame;

#ifndef	TEXXET
#define	DIR	1
#else
#define	DIR	currinf.dir
#endif

/*
 *	Explanation of the following constant:
 *	offset_[xy]   << 16:	margin (defaults to one inch)
 *	shrink_factor << 16:	one pixel page border
 *	shrink_factor << 15:	rounding for pixel_conv
 */
#define OFFSET_X	(offset_x << 16) + (shrink_factor * 3 << 15)
#define OFFSET_Y	(offset_y << 16) + (shrink_factor * 3 << 15)

#if (BMBYTES == 1)
BMUNIT	bit_masks[9] = {
	0x0,	0x1,	0x3,	0x7,
	0xf,	0x1f,	0x3f,	0x7f,
	0xff
};
#else
#if (BMBYTES == 2)
BMUNIT	bit_masks[17] = {
	0x0,	0x1,	0x3,	0x7,
	0xf,	0x1f,	0x3f,	0x7f,
	0xff,	0x1ff,	0x3ff,	0x7ff,
	0xfff,	0x1fff,	0x3fff,	0x7fff,
	0xffff
};
#else	/* BMBYTES == 4 */
BMUNIT	bit_masks[33] = {
	0x0,		0x1,		0x3,		0x7,
	0xf,		0x1f,		0x3f,		0x7f,
	0xff,		0x1ff,		0x3ff,		0x7ff,
	0xfff,		0x1fff,		0x3fff,		0x7fff,
	0xffff,		0x1ffff,	0x3ffff,	0x7ffff,
	0xfffff,	0x1fffff,	0x3fffff,	0x7fffff,
	0xffffff,	0x1ffffff,	0x3ffffff,	0x7ffffff,
	0xfffffff,	0x1fffffff,	0x3fffffff,	0x7fffffff,
	0xffffffff
};
#endif
#endif

#ifdef	VMS
#define	off_t	int
#endif
#ifndef WIN32
extern	off_t	lseek();
#endif /* ! WIN32 */

#ifndef	SEEK_SET	/* if <unistd.h> is not provided (or for <X11R5) */
#define	SEEK_SET	0
#define	SEEK_CUR	1
#define	SEEK_END	2
#endif

static	void	draw_part();

#if	NeedFunctionPrototypes
#ifndef	TEXXET
static	long set_no_char(wide_ubyte ch);
#else
static	void set_no_char(wide_ubyte cmd, wide_ubyte ch);
#endif	/* TEXXET */
#else	/* !NeedFunctionPrototypes */
#ifndef	TEXXET
static	long set_no_char();
#else
static	void set_no_char();
#endif	/* TEXXET */
#endif	/* NeedFunctionPrototypes */

#ifdef TRANSFORM
extern HRGN hClipRgn;
#endif

/*
 *	X routines.
 */

/*
 *	Put a rectangle on the screen.
 */

/* HTEX needs this to be non-static -janl */
void
put_rule(x, y, w, h)
	int		x, y;
	unsigned int	w, h;
{
#ifdef WIN32
  RECT rTemp;
#ifdef HTEX
  HDC hdc = highlight? highGC : ruleGC;
#else
  HDC hdc = ruleGC;
#endif

#ifdef TRANSFORM
  POINT lppt[4];
  HRGN hNewClipRgn = NULL;
  XFORM xfrm, xfrm_temp;
  extern BOOL bTransfInUse;

  if (!allowDrawingChars)
	return;

  rTemp.left = x - currwin.base_x;
  rTemp.top = y - currwin.base_y;
  rTemp.right = rTemp.left + w;
  rTemp.bottom = rTemp.top + h;

  if (resource.use_xform && bTransfInUse) {
    RECT rTemp;
    BOOL bClipped1, bClipped2;
    extern XFORM current_transf;
    if (SetWorldTransform(hdc, &current_transf) == 0)
      Win32Error("XPutImage/SetWorldTransform/current");
    
    lppt[0].x = x - currwin.base_x;
    lppt[0].y = y - currwin.base_y;
    lppt[1].x = lppt[0].x + w;
    lppt[1].y = lppt[0].y;
    lppt[2].x = lppt[0].x + w;
    lppt[2].y = lppt[0].y + h;
    lppt[3].x = lppt[0].x;
    lppt[3].y = lppt[0].y + h;
    LPtoDP(hdc, lppt, 4);
    if ((hNewClipRgn = CreatePolygonRgn(lppt, 4, ALTERNATE)) == NULL) {
      Win32Error("XPutImage/CreatePolygonRgn");
    }
    if (SelectClipRgn(hdc, hNewClipRgn) == ERROR) {
      Win32Error("XPutImage/SelectClipRgn");
    }
    bClipped2 = RectVisible(hdc, &rTemp);
    if (!bClipped2)
      goto nothing;
  }
  else
#endif
    {
      if (!RectVisible(hdc, &rTemp))
		goto nothing;
    }
  FillRect(hdc, &rTemp, foreBrush);
 nothing:
#if TRANSFORM
  if (resource.use_xform && bTransfInUse) {
    SelectClipRgn(hdc, NULL);
    if (hNewClipRgn) DeleteObject(hNewClipRgn);
    ModifyWorldTransform(hdc, &xfrm_temp, MWT_IDENTITY);
  }
#endif
#else
	if (x < max_x && x + (int) w >= min_x
	  && y < max_y && y + (int) h >= min_y) {
	    if (--event_counter == 0) read_events(False);
#ifdef HTEX
	    XFillRectangle(DISP, currwin.win, highlight? highGC: ruleGC,
#else
	    XFillRectangle(DISP, currwin.win, ruleGC,
#endif
		x - currwin.base_x, y - currwin.base_y, w ? w : 1, h ? h : 1);
	}
#endif
}

static	void
put_bitmap(bitmap, x, y)
	struct bitmap *bitmap;
	int x, y;
{

#ifdef HTEX
	if (HTeXAnestlevel > 0) {
		htex_recordbits(x, y, bitmap->w, bitmap->h);
	}
#endif

	if (debug & DBG_BITMAP)
		Printf("X(%d,%d)\n", x - currwin.base_x, y - currwin.base_y);
#ifdef WIN32
	image->width = bitmap->w;
	image->height = bitmap->h;
	image->data = bitmap->bits;
	image->bytes_per_line = bitmap->bytes_wide;
	image->endian_permuted = &(bitmap->endian_permuted);
	image->depth = 1;
#ifdef HTEX
	XPutImage(DISP, currwin.win, HTeXreflevel > 0? highGC: foreGC, image,
		  0, 0,
		  x - currwin.base_x, y - currwin.base_y,
		  bitmap->w, bitmap->h);
#else
	XPutImage(DISP, currwin.win, foreGC, image,
		  0, 0,
		  x - currwin.base_x, y - currwin.base_y,
		  bitmap->w, bitmap->h);
#endif

#else
	if (x < max_x && x + (int) bitmap->w >= min_x &&
	    y < max_y && y + (int) bitmap->h >= min_y) {
	        if (--event_counter == 0) read_events(False);
		image->width = bitmap->w;
		image->height = bitmap->h;
		image->data = bitmap->bits;
		image->bytes_per_line = bitmap->bytes_wide;
#ifdef HTEX
		XPutImage(DISP, currwin.win, HTeXreflevel > 0? highGC: foreGC, image,
#else
		XPutImage(DISP, currwin.win, foreGC, image,
#endif
			0, 0,
			x - currwin.base_x, y - currwin.base_y,
			bitmap->w, bitmap->h);
		if (foreGC2)
		    XPutImage(DISP, currwin.win, foreGC2, image,
			0, 0,
			x - currwin.base_x, y - currwin.base_y,
			bitmap->w, bitmap->h);
	}
#endif
}

#ifdef	GREY
static	void
put_image(g, x, y)
	struct glyph *g;
	int x, y;
{
	XImage *img = g->image2;

#ifdef HTEX
	if (HTeXAnestlevel > 0) {
		htex_recordbits(x, y, img->width, img->height);
	}
#endif
#ifdef WIN32
	XPutImage(DISP, currwin.win, foreGC, img,
		  0, 0,
		  x - currwin.base_x, y - currwin.base_y,
		  (unsigned int) img->width, (unsigned int) img->height);

#else /* ! WIN32 */
	if (x < max_x && x + img->width >= min_x &&
	    y < max_y && y + img->height >= min_y) {

	    if (--event_counter == 0) read_events(False);

#ifdef HTEX
	    XPutImage(DISP, currwin.win, foreGC, img,
#else
	    XPutImage(DISP, currwin.win, foreGC, img,
#endif
		    0, 0,
		    x - currwin.base_x, y - currwin.base_y,
		    (unsigned int) img->width, (unsigned int) img->height);
	    if (foreGC2)
		XPutImage(DISP, currwin.win, foreGC2, img,
		    0, 0,
		    x - currwin.base_x, y - currwin.base_y,
		    (unsigned int) img->width, (unsigned int) img->height);
	    if (pixeltbl_t != NULL) {
		img->data = g->pixmap2_t;
		XPutImage(DISP, currwin.win, foreGC2, img,
			0, 0,
			x - currwin.base_x, y - currwin.base_y,
			(unsigned int) img->width, (unsigned int) img->height);
		img->data = g->pixmap2;
	    }
	}
#endif
}
#endif	/* GREY */

/*
 *	Draw the border of a rectangle on the screen.
 */

static	void
put_border(x, y, width, height, ourGC)
	int		x, y;
	unsigned int	width, height;
	GC		ourGC;
{

	--width;
	--height;
	/* top */
	XFillRectangle(DISP, currwin.win, ourGC, x, y, width, 1);
	/* right */
	XFillRectangle(DISP, currwin.win, ourGC, x + (int) width, y, 1, height);
	/* bottom */
	XFillRectangle(DISP, currwin.win, ourGC, x + 1, y + (int) height,
	    width, 1);
	/* left */
	XFillRectangle(DISP, currwin.win, ourGC, x, y + 1, 1, height);
}

#ifdef GRID
#ifdef WIN32
void
put_grid(struct WindowRec *win, unsigned int unit, GC gc1, GC gc2, GC gc3)
{
  int i;			     /* looping variable */
  float sep;		     /* grid separation */
  int tmp;		     /* temporary variable */
  HDC hdc;
  unsigned int offset;

  if (!allowDrawingChars)
    return;

  switch (grid_mode) {
  case 0:
    return;
    break;
  case 1:
    sep = ((float) unit);
    hdc = gc1;
    break;
  case 2:
    sep = ((float) unit) / 2.0;
    hdc = gc2;
    break;
  case 3:
    sep = ((float) unit) / 4.0;
    hdc = gc3;
    break;
  default:
    fprintf(stderr, "Warning: invalid grid_mode %d\n", grid_mode);
    return;
  }
#if 1
  fprintf(stderr, "Put_grid in base (%d, %d)  rect (%d, %d) x (%d, %d)\n",
	  win->base_x, win->base_y,
	  win->min_x, win->min_y,
	  win->max_x, win->max_y);
#endif

  /* Horizontal Grid */
  for (i = 0, offset = (unsigned int)(sep - (win->base_y+win->min_y) % (int) sep) -1;
       win->min_y + i*sep + offset <= win->max_y;
       i++)
    XFillRectangle(DISP, win->win, (hdc), win->min_x, win->min_y + i*sep + offset, 
		   win->max_x - win->min_x, 1);

  /* Vertical Grid */
  for (i = 0, offset = (unsigned int)(sep - (win->base_x+win->min_x) % (int) sep) - 1;
       win->min_x + i*sep + offset <= win->max_x;
       i++)
    XFillRectangle(DISP, win->win, (hdc), win->min_x + i*sep + offset,
		   win->min_y, 1, win->max_y - win->min_y);

}
#else /* ! WIN32 */
/* drawing grid */
void
put_grid(x, y, width, height, unit, gc1, gc2, gc3)
	int		x, y;
	unsigned int	width, height, unit;
	GC		gc1, gc2, gc3;
{
	int i;			     /* looping variable */
	float sep;		     /* grid separation */
	int tmp;		     /* temporary variable */

	--width;
	--height;

/* drawing vertial grid */
#define DRAWGRID_VER(gc) for (i=1; \
			       (tmp=x+(int) (((float) i)*sep)) < x+width; \
			       i++) \
  			  XFillRectangle(DISP, currwin.win, (gc), \
				   tmp, y, 1, height)
/* drawing horizontal grid */
#define DRAWGRID_HOR(gc) for (i=1; \
			       (tmp=y+(int) (((float) i)*sep)) < y+height; \
			       i++) \
		          XFillRectangle(DISP, currwin.win, (gc), \
				   x, tmp, width, 1)

	if ( grid_mode > 2 )	     /* third level grid */
	  {
	    sep = (float) unit / 4.0;
	    DRAWGRID_VER ( gc3 );    /* vertical grid */
	    DRAWGRID_HOR ( gc3 );    /* horizontal grid */
	  }
  
	if ( grid_mode > 1 )	     /* second */
	  {
	    sep = (float) unit / 2.0;
	    DRAWGRID_VER ( gc2 );    /* vertical grid */
	    DRAWGRID_HOR ( gc2 );    /* horizontal grid */
	  }
  
	if ( grid_mode > 0 )	     /* first level */
	  {
	    sep = (float) unit ;
	    DRAWGRID_VER ( gc1 );    /* vertical grid */
	    DRAWGRID_HOR ( gc1 );    /* horizontal grid */
	  }
}
#endif /* WIN32 */

#endif /* GRID */

/*
 *	Byte reading routines for dvi file.
 */

#define	xtell(pos)	((long) (lseek(fileno(dvi_file), 0L, SEEK_CUR) - \
			    (currinf.end - (pos))))

static	ubyte
xxone()
{
	if (currinf.virtual) {
	    ++(currinf.pos);
	    return EOP;
	}
	currinf.end = dvi_buffer +
	    read(fileno(dvi_file), (char *) (currinf.pos = dvi_buffer),
		DVI_BUFFER_LEN);
	return currinf.end > dvi_buffer ? *(currinf.pos)++ : EOF;
}

#define	xone()  (currinf.pos < currinf.end ? *(currinf.pos)++ : xxone())

static	unsigned long
xnum(size)
	ubyte size;
{
	long x = 0;

	while (size--) x = (x << 8) | xone();
	return x;
}

static	long
xsnum(size)
	ubyte size;
{
	long x;

#if	__STDC__
	x = (signed char) xone();
#else
	x = xone();
	if (x & 0x80) x -= 0x100;
#endif
	while (--size) x = (x << 8) | xone();
	return x;
}

#define	xsfour()	xsnum(4)

static	void
xskip(offset)
	long	offset;
{
	currinf.pos += offset;
	if (!currinf.virtual && currinf.pos > currinf.end)
	    (void) lseek(fileno(dvi_file), (long) (currinf.pos - currinf.end),
		SEEK_CUR);
}

#ifdef HAVE_PROTOTYPES
static	NORETURN void
tell_oops(_Xconst char *message, ...)
#else
/* VARARGS */
static	NORETURN void
tell_oops(va_alist)
	va_dcl
#endif
{
#ifndef HAVE_PROTOTYPES
	_Xconst char *message;
#endif
	va_list	args;

	Fprintf(stderr, "%s: ", prog);
#ifdef HAVE_PROTOTYPES
	va_start(args, message);
#else
	va_start(args);
	message = va_arg(args, _Xconst char *);
#endif
	(void) vfprintf(stderr, message, args);
	va_end(args);
	if (currinf.virtual)
	    Fprintf(stderr, " in virtual font %s\n", currinf.virtual->fontname);
	else
	    Fprintf(stderr, ", offset %ld\n", xtell(currinf.pos - 1));
#if	PS
	ps_destroy();
#endif
	Exit(1);
}


/*
 *	Code for debugging options.
 */

static	void
print_bitmap(bitmap)
	struct bitmap *bitmap;
{
	BMUNIT *ptr = (BMUNIT *) bitmap->bits;
	int x, y, i;

	if (ptr == NULL) oops("print_bitmap called with null pointer.");
	Printf("w = %d, h = %d, bytes wide = %d\n",
	    bitmap->w, bitmap->h, bitmap->bytes_wide);
	for (y = 0; y < (int) bitmap->h; ++y) {
	    for (x = bitmap->bytes_wide; x > 0; x -= BMBYTES) {
#ifndef	WORDS_BIGENDIAN
		for (i = 0; i < BMBITS; ++i)
#else
		for (i = BMBITS - 1; i >= 0; --i)
#endif
		    Putchar((*ptr & (1 << i)) ? '@' : ' ');
		++ptr;
	    }
	    Putchar('\n');
	}
}

static	void
print_char(ch, g)
	ubyte ch;
	struct glyph *g;
{
	Printf("char %d", ch);
	if (ISPRINT(ch))
	    Printf(" (%c)", ch);
	Putchar('\n');
	Printf("x = %d, y = %d, dvi = %ld\n", g->x, g->y, g->dvi_adv);
	print_bitmap(&g->bitmap);
}

static	_Xconst	char	*dvi_table1[] = {
#ifdef Omega
	"SET1", "SET2", NULL, NULL, "SETRULE", "PUT1", "PUT2", NULL,
#else
	"SET1", NULL, NULL, NULL, "SETRULE", "PUT1", NULL, NULL,
#endif
	NULL, "PUTRULE", "NOP", "BOP", "EOP", "PUSH", "POP", "RIGHT1",
	"RIGHT2", "RIGHT3", "RIGHT4", "W0", "W1", "W2", "W3", "W4",
	"X0", "X1", "X2", "X3", "X4", "DOWN1", "DOWN2", "DOWN3",
	"DOWN4", "Y0", "Y1", "Y2", "Y3", "Y4", "Z0", "Z1",
	"Z2", "Z3", "Z4"};

static	_Xconst	char	*dvi_table2[] = {
	"FNT1", "FNT2", "FNT3", "FNT4", "XXX1", "XXX2", "XXX3", "XXX4",
	"FNTDEF1", "FNTDEF2", "FNTDEF3", "FNTDEF4", "PRE", "POST", "POSTPOST",
	"SREFL", "EREFL", NULL, NULL, NULL, NULL};

static	void
print_dvi(ch)
	ubyte ch;
{
	_Xconst	char	*s;

	Printf("%4d %4d ", PXL_H, PXL_V);
	if (ch <= (ubyte) (SETCHAR0 + 127)) {
	    Printf("SETCHAR%-3d", ch - SETCHAR0);
	    if (ISPRINT(ch))
		Printf(" (%c)", ch);
	    Putchar('\n');
	    return;
	}
	else if (ch < FNTNUM0) s = dvi_table1[ch - 128];
	else if (ch <= (ubyte) (FNTNUM0 + 63)) {
	    Printf("FNTNUM%d\n", ch - FNTNUM0);
	    return;
	}
	else s = dvi_table2[ch - (FNTNUM0 + 64)];
	if (s) Puts(s);
	else
	    tell_oops("unknown op-code %d", ch);
}


/*
 *	Count the number of set bits in a given region of the bitmap
 */

char	sample_count[]	= {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};

static	int
sample(bits, bytes_wide, bit_skip, w, h)
	BMUNIT	*bits;
	int	bytes_wide, bit_skip, w, h;
{
	BMUNIT	*ptr, *endp;
	BMUNIT *cp;
	int	bits_left;
	int n, bit_shift, wid;

	ptr = bits + bit_skip / BMBITS;
	endp = ADD(bits, h * bytes_wide);
	bits_left = w;
#ifndef	WORDS_BIGENDIAN
	bit_shift = bit_skip % BMBITS;
#else
	bit_shift = BMBITS - bit_skip % BMBITS;
#endif
	n = 0;
	while (bits_left) {
#ifndef	WORDS_BIGENDIAN
	    wid = BMBITS - bit_shift;
#else
	    wid = bit_shift;
#endif
	    if (wid > bits_left) wid = bits_left;
	    if (wid > 4) wid = 4;
#ifdef	WORDS_BIGENDIAN
	    bit_shift -= wid;
#endif
	    for (cp = ptr; cp < endp; cp = ADD(cp, bytes_wide))
		n += sample_count[(*cp >> bit_shift) & bit_masks[wid]];
#ifndef	WORDS_BIGENDIAN
	    bit_shift += wid;
	    if (bit_shift == BMBITS) {
		bit_shift = 0;
		++ptr;
	    }
#else
	    if (bit_shift == 0) {
		bit_shift = BMBITS;
		++ptr;
	    }
#endif
	    bits_left -= wid;
	}
	return n;
}

static	void
shrink_glyph(g)
	struct glyph *g;
{
	int		shrunk_bytes_wide, shrunk_height;
	int		rows_left, rows, init_cols;
	int		cols_left;
	int		cols;
	BMUNIT		*old_ptr, *new_ptr;
	BMUNIT	m, *cp;
	int	min_sample = shrink_factor * shrink_factor * density / 100;
	int     rtmp;
#ifdef WIN32
	extern PF_BSWAP bswap;

	/* Ensure that the original data are not permuted */
	if (g->bitmap.endian_permuted == 1) {
	  bswap((BMUNIT*)g->bitmap.bits,
		(g->bitmap.bytes_wide * g->bitmap.h)/BMBYTES);
	  g->bitmap.endian_permuted = 0;
	}
#endif
	/* These machinations ensure that the character is shrunk according to
	   its hot point, rather than its upper left-hand corner. */
	g->x2 = g->x / shrink_factor;
	init_cols = g->x - g->x2 * shrink_factor;
	if (init_cols <= 0) init_cols += shrink_factor;
	else ++g->x2;
	g->bitmap2.w = g->x2 + ROUNDUP((int) g->bitmap.w - g->x, shrink_factor);
	/* include row zero with the positively numbered rows */
 	rtmp = g->y + 1;
 	g->y2 = rtmp / shrink_factor;
 	rows = rtmp - g->y2 * shrink_factor;
	if (rows <= 0) {
	    rows += shrink_factor;
	    --g->y2;
	}
	g->bitmap2.h = shrunk_height = g->y2 +
	    ROUNDUP((int) g->bitmap.h - rtmp, shrink_factor) + 1;
	alloc_bitmap(&g->bitmap2);
	old_ptr = (BMUNIT *) g->bitmap.bits;
	new_ptr = (BMUNIT *) g->bitmap2.bits;
	shrunk_bytes_wide = g->bitmap2.bytes_wide;
	rows_left = g->bitmap.h;
	bzero((char *) new_ptr, shrunk_bytes_wide * shrunk_height);
	while (rows_left) {
	    if (rows > rows_left) rows = rows_left;
	    cols_left = g->bitmap.w;
#ifndef	WORDS_BIGENDIAN
	    m = (1 << 0);
#else
	    m = ((BMUNIT) 1 << (BMBITS-1));
#endif
	    cp = new_ptr;
	    cols = init_cols;
	    while (cols_left) {
		if (cols > cols_left) cols = cols_left;
		if (sample(old_ptr, g->bitmap.bytes_wide,
			(int) g->bitmap.w - cols_left, cols, rows)
			>= min_sample)
		    *cp |= m;
#ifndef	WORDS_BIGENDIAN
		if (m == ((BMUNIT)1 << (BMBITS-1))) {
		    m = (1 << 0);
		    ++cp;
		}
		else m <<= 1;
#else
		if (m == (1 << 0)) {
		    m = ((BMUNIT) 1 << (BMBITS-1));
		    ++cp;
		}
		else m >>= 1;
#endif
		cols_left -= cols;
		cols = shrink_factor;
	    }
	    *((char **) &new_ptr) += shrunk_bytes_wide;
	    *((char **) &old_ptr) += rows * g->bitmap.bytes_wide;
	    rows_left -= rows;
	    rows = shrink_factor;
	}
	g->y2 = g->y / shrink_factor;
	if (debug & DBG_BITMAP)
	    print_bitmap(&g->bitmap2);
#ifdef WIN32
	g->bitmap2.endian_permuted = 0;
#endif
}

#ifdef	GREY
static	void
shrink_glyph_grey(g)
	struct glyph *g;
{
	int		rows_left, rows, init_cols;
	int		cols_left;
	int	cols;
	int		x, y;
	long		thesample;
	BMUNIT		*old_ptr;
	unsigned int	size;
	int		rtmp;
#ifdef WIN32
	extern PF_BSWAP bswap;

	/* Ensure that the original data are not permuted */
	if (g->bitmap.endian_permuted == 1) {
	  bswap((BMUNIT*)g->bitmap.bits,
		(g->bitmap.bytes_wide * g->bitmap.h)/BMBYTES);
	  g->bitmap.endian_permuted = 0;
	}
#endif
	/* These machinations ensure that the character is shrunk according to
	   its hot point, rather than its upper left-hand corner. */
	g->x2 = g->x / shrink_factor;
	init_cols = g->x - g->x2 * shrink_factor;
	if (init_cols <= 0) init_cols += shrink_factor;
	else ++g->x2;
	g->bitmap2.w = g->x2 + ROUNDUP((int) g->bitmap.w - g->x, shrink_factor);
	/* include row zero with the positively numbered rows */
	rtmp = g->y + 1;
	g->y2 = rtmp / shrink_factor;
	rows = rtmp - g->y2 * shrink_factor;
	if (rows <= 0) {
	    rows += shrink_factor;
	    --g->y2;
	}
	g->bitmap2.h = g->y2 + ROUNDUP((int) g->bitmap.h - rtmp, shrink_factor)
	    + 1;

#ifdef WIN32
	g->image2 = XCreateImage(DISP, maneDrawDC,
				 4,
				 ZPixmap, 0, (char *) NULL,
				 g->bitmap2.w, g->bitmap2.h,
				 BMBITS, 0);
	g->image2->endian_permuted = NULL;
#else
	g->image2 = XCreateImage(DISP, our_visual, our_depth, ZPixmap,
	  0, (char *) NULL, g->bitmap2.w, g->bitmap2.h, BMBITS, 0);
#endif
	size = g->image2->bytes_per_line * g->bitmap2.h;
	g->pixmap2 = g->image2->data = xmalloc(size != 0 ? size : 1);
	if (pixeltbl_t != NULL)
	    g->pixmap2_t = xmalloc(size != 0 ? size : 1);

	old_ptr = (BMUNIT *) g->bitmap.bits;
	rows_left = g->bitmap.h;
	y = 0;
	while (rows_left) {
	    x = 0;
	    if (rows > rows_left) rows = rows_left;
	    cols_left = g->bitmap.w;
	    cols = init_cols;
	    while (cols_left) {
		if (cols > cols_left) cols = cols_left;

		thesample = sample(old_ptr, g->bitmap.bytes_wide,
			(int) g->bitmap.w - cols_left, cols, rows);
		XPutPixel(g->image2, x, y, pixeltbl[thesample]);
		if (pixeltbl_t != NULL) {
		    g->image2->data = g->pixmap2_t;
		    XPutPixel(g->image2, x, y, pixeltbl_t[thesample]);
		    g->image2->data = g->pixmap2;
		}

		cols_left -= cols;
		cols = shrink_factor;
		x++;
	    }
	    *((char **) &old_ptr) += rows * g->bitmap.bytes_wide;
	    rows_left -= rows;
	    rows = shrink_factor;
	    y++;
	}

	while (y < (int) g->bitmap2.h) {
	    for (x = 0; x < (int) g->bitmap2.w; x++) {
		XPutPixel(g->image2, x, y, *pixeltbl);
		if (pixeltbl_t != NULL) {
		    g->image2->data = g->pixmap2_t;
		    XPutPixel(g->image2, x, y, *pixeltbl_t);
		    g->image2->data = g->pixmap2;
		}
	    }
	    y++;
	}

	g->y2 = g->y / shrink_factor;
#ifdef WIN32
	g->bitmap2.endian_permuted = 0;
#endif
}
#endif	/* GREY */

/*
 *	Find font #n.
 */

static	void
change_font(n)
	unsigned long n;
{
	struct tn *tnp;

	if (n < currinf.tn_table_len) currinf.fontp = currinf.tn_table[n];
	else {
	    currinf.fontp = NULL;
	    for (tnp = currinf.tn_head; tnp != NULL; tnp = tnp->next)
		if (tnp->TeXnumber == n) {
		    currinf.fontp = tnp->fontp;
		    break;
		}
	}
	if (currinf.fontp == NULL) tell_oops("non-existent font #%d", n);
	if (currinf.fontp->set_char_p == NULL) 
	  tell_oops("No procedure to set font #%d",
		    n,currinf.fontp->fontname);
	maxchar = currinf.fontp->maxchar;
	currinf.set_char_p = currinf.fontp->set_char_p;
}


/*
 *	Open a font file.
 */

static	void
open_font_file(fontp)
	struct font *fontp;
{
	if (fontp->file == NULL) {
	    fontp->file = xfopen_local(fontp->filename, OPEN_MODE);
	    if (fontp->file == NULL)
		oops("Font file disappeared:  %s", fontp->filename);
	}
}

/*
 *	Read special string.
 */

static	char *
read_special(nbytes)
	long	nbytes;
{
	static	char	*spcl	= NULL;
	static	long	spcl_len = -1;
	char	*p;

	if (spcl_len < nbytes) {
	    if (spcl != NULL) free(spcl);
	    spcl = xmalloc((unsigned) nbytes + 1);
	    spcl_len = nbytes;
	}
	p = spcl;
	for (;;) {
	    int i = currinf.end - currinf.pos;

	    if (i > nbytes) i = nbytes;
	    bcopy((char *) currinf.pos, p, i);
	    currinf.pos += i;
	    p += i;
	    nbytes -= i;
	    if (nbytes == 0) break;
	    (void) xxone();
	    --(currinf.pos);
	}
	*p = '\0';
	return spcl;
}

#if	PS

/*
 *	Size of page interval for "Scanning pages xx-xx" message.
 */

#ifndef	REPORT_INCR
#define	REPORT_INCR	50
#endif

/*
 *	Table used for scanning.  If >= 0, then skip that many bytes.
 *	M1 means end of page, M2 means special, M3 means FNTDEF,
 *	M4 means unrecognizable, and M5 means doesn't belong here.
 */

#define	M1	255
#define	M2	254
#define	M3	253
#define	M4	252
#define	M5	251
#define	MM	251

static	ubyte	scantable[256] = {
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,	/* chars 0 - 127 */
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
#ifdef Omega
	1,2,			/* SET1,SET2 (128,129) */
		/* -,-,SETRULE,PUT1,PUT2,-,-,PUTRULE,NOP,BOP (130-139) */
	M4,M4,8,1,2,M4,M4,8,0,44,
#else

	1,M4,			/* SET1,- (128,129) */
			/* -,-,SETRULE,PUT1,-,-,-,PUTRULE,NOP,BOP (130-139) */
	M4,M4,8,1,M4,M4,M4,8,0,44,
#endif
	M1,0,0,1,2,3,4,0,1,2,	/* EOP,PUSH,POP,RIGHT1-4,W0M2 (140-149) */
	3,4,0,1,2,3,4,1,2,3,	/* W3-4,X0-4,DOWN1-3 (150-159) */
	4,0,1,2,3,4,0,1,2,3,	/* DOWN4,Y0-4,Z0-3 (160-169) */
	4,			/* Z4 (170) */
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,	/* change font 171 - 234 */
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
	1,2,3,4,M2,		/* FNT1-4,XXX1 (235-239) */
			/* XXX2-4,FNTDEF1-4,PRE,POST,POSTPOST (240-249) */
	M2,M2,M2,M3,M3,M3,M3,M5,M5,M5,
	0,0,M4,M4,M4,M4};	/* SREFL,EREFL,-,-,-,- (250-255) */

/*
 *	Prescanning routine for dvi file.  This looks for specials like
 *	`header=' and `!'.
 */

void
prescan()
{
	ubyte	ch;
	ubyte	n;
	long	a;
	int	nextreportpage;
	char	scanmsg[40];
#ifdef WIN32
	extern void scan_init_fore_color(void);
	extern void scan_get_last_fore_color(void);
#endif

#ifndef WIN32
	if (!resource._postscript) {
	    scanned_page = total_pages;
	    return;
	}
#else
	/* debug = DBG_PS; */
	scan_init_fore_color();
#endif
	nextreportpage = scanned_page;
	++scanned_page;
	(void) lseek(fileno(dvi_file), page_offset[scanned_page], SEEK_SET);
	currinf.pos = currinf.end = dvi_buffer;
	for (;;) {
	    if (debug & DBG_PS)
		Printf("Scanning page %d\n", scanned_page);
	    if (scanned_page > nextreportpage) {
		nextreportpage += REPORT_INCR;
		if (nextreportpage > current_page)
		    nextreportpage = current_page;
		Sprintf(scanmsg, "Scanning pages %d-%d",
		    scanned_page + pageno_correct,
		    nextreportpage + pageno_correct);
		showmessage(scanmsg);
		XFlush(DISP);
	    }
	    for (;;) {
		ch = xone();
		n = scantable[ch];
		if (n < MM)
		    while (n-- != 0)
			(void) xone();
		else if (n == M1) break;	/* end of page */
		else switch (n) {
		    case M2:	/* special */
			a = xnum(ch - XXX1 + 1);
			if (a > 0)
			    scan_special(read_special(a));
			break;
		    case M3:	/* FNTDEF */
			xskip((long) (12 + ch - FNTDEF1 + 1));
			ch = xone();
			xskip((long) ch + (long) xone());
			break;
		    case M4:	/* unrecognizable */
			tell_oops("unknown op-code %d", ch);
			break;
		    case M5:	/* doesn't belong */
			tell_oops("prescan, shouldn't happen: %s encountered",
				dvi_table2[ch - (FNTNUM0 + 64)]);
			break;
		}
	    }
#ifdef WIN32
	    scan_get_last_fore_color();
#if DEBUG_SPECIALS
	    fprintf(stderr, "Mid : scanned_page = %d, current_page = %d\n",
		    scanned_page, current_page);
#endif
#endif
	    if (scanned_page >= current_page) break;
	    ++scanned_page;
	}
#if DEBUG_SPECIALS
	fprintf(stderr, "End of prescan, scanned_page = %d\n", scanned_page);
#endif
	scanned_page_bak = scanned_page;
#ifndef WIN32
	/* I don't think this is the right place for this. */
	XClearWindow(DISP, mane.win);
#endif
	psp.endheader();
}

#endif	/* PS */

/*
 *	Routines to print characters.
 */

#ifndef	TEXXET
#define	ERRVAL	0L
#else
#define	ERRVAL
#endif

#ifndef	TEXXET
long
set_char P1C(wide_ubyte, ch)
#else
void
set_char P2C(wide_ubyte, cmd, wide_ubyte, ch)
#endif
{
	struct glyph *g;
#ifdef	TEXXET
	long	dvi_h_sav;
#endif

	if (ch > maxchar) realloc_font(currinf.fontp, WIDENINT ch);
	if ((g = &currinf.fontp->glyph[ch])->bitmap.bits == NULL) {
	    if (g->addr == 0) {
		if (!hush_chars)
		    Fprintf(stderr, "Character %d not defined in font %s\n", ch,
			currinf.fontp->fontname);
		g->addr = -1;
		return ERRVAL;
	    }
	    if (g->addr == -1)
		return ERRVAL;	/* previously flagged missing char */
	    open_font_file(currinf.fontp);
	    Fseek(currinf.fontp->file, g->addr, 0);
	    (*currinf.fontp->read_char)(currinf.fontp, ch);
	    if (debug & DBG_BITMAP) print_char((ubyte) ch, g);
	    currinf.fontp->timestamp = ++current_timestamp;
	}

#ifdef	TEXXET
	dvi_h_sav = DVI_H;
	if (currinf.dir < 0) DVI_H -= g->dvi_adv;

	if (scan_frame == NULL) {
#endif
	    if (shrink_factor == 1)
		put_bitmap(&g->bitmap, PXL_H - g->x, PXL_V - g->y);
	    else {
#ifdef	GREY
		if (use_grey) {
		    if (g->pixmap2 == NULL) {
			shrink_glyph_grey(g);
		    }
		    put_image(g, PXL_H - g->x2, PXL_V - g->y2);
		} else {
		    if (g->bitmap2.bits == NULL) {
			shrink_glyph(g);
		    }
		    put_bitmap(&g->bitmap2, PXL_H - g->x2, PXL_V - g->y2);
		}
#else
		if (g->bitmap2.bits == NULL) {
		    shrink_glyph(g);
		}
		put_bitmap(&g->bitmap2, PXL_H - g->x2, PXL_V - g->y2);
#endif
	    }
#ifndef	TEXXET
	return g->dvi_adv;
#else
	}
#ifdef Omega
	if ((cmd == PUT1) || (cmd == PUT2))
#else
	if (cmd == PUT1)
#endif
	    DVI_H = dvi_h_sav;
	else
	    if (currinf.dir > 0) DVI_H += g->dvi_adv;
#endif
}


#ifndef	TEXXET
static	long set_empty_char P1C(wide_ubyte, ch)
{ return 0; }
#else
static	void set_empty_char P2C(wide_ubyte, cmd, wide_ubyte, ch)
{ return; }
#endif

#ifndef	TEXXET
long load_n_set_char P1C(wide_ubyte, ch)
#else
void load_n_set_char P2C(wide_ubyte, cmd, wide_ubyte, ch)
#endif
{
	if (load_font(currinf.fontp)) {	/* if not found */
	    Fputs("Character(s) will be left blank.\n", stderr);
	    currinf.set_char_p = currinf.fontp->set_char_p = set_empty_char;
	    currinf.fontp->glyph = NULL;        /* init for read_postamble */
#ifndef	TEXXET
	    return 0;
#else
	    return;
#endif
	}
	maxchar = currinf.fontp->maxchar;
	currinf.set_char_p = currinf.fontp->set_char_p;
#ifndef	TEXXET
	return (*currinf.set_char_p)(ch);
#else
	(*currinf.set_char_p)(cmd, ch);
	return;
#endif
}


#ifndef	TEXXET
long
set_vf_char P1C(wide_ubyte, ch)
#else
void
set_vf_char P2C(wide_ubyte, cmd, wide_ubyte, ch)
#endif
{
	struct macro *m;
	struct drawinf	oldinfo;
#ifdef Omega
	wide_ubyte	oldmaxchar;
#else
	ubyte	oldmaxchar;
#endif
	static	ubyte	c;
#ifdef	TEXXET
	long	dvi_h_sav;
#endif

	if (ch > maxchar) realloc_virtual_font(currinf.fontp, ch);
	if ((m = &currinf.fontp->macro[ch])->pos == NULL) {
	    if (!hush_chars)
		Fprintf(stderr, "Character %d not defined in font %s\n", ch,
		    currinf.fontp->fontname);
	    m->pos = m->end = &c;
	    return ERRVAL;
	}
#ifdef	TEXXET
	dvi_h_sav = DVI_H;
	if (currinf.dir < 0) DVI_H -= m->dvi_adv;
	if (scan_frame == NULL) {
#endif
	    oldinfo = currinf;
	    oldmaxchar = maxchar;
	    WW = XX = YY = ZZ = 0;
	    currinf.tn_table_len = VFTABLELEN;
	    currinf.tn_table = currinf.fontp->vf_table;
	    currinf.tn_head = currinf.fontp->vf_chain;
	    currinf.pos = m->pos;
	    currinf.end = m->end;
	    currinf.virtual = currinf.fontp;
	    draw_part(current_frame, currinf.fontp->dimconv);
	    if (currinf.pos != currinf.end + 1)
		tell_oops("virtual character macro does not end correctly");
	    currinf = oldinfo;
	    maxchar = oldmaxchar;
#ifndef	TEXXET
	return m->dvi_adv;
#else
	}
#ifdef Omega
	if ((cmd == PUT1) || (cmd == PUT2))
#else
	if (cmd == PUT1)
#endif
	    DVI_H = dvi_h_sav;
	else
	    if (currinf.dir > 0) DVI_H += m->dvi_adv;
#endif
}


#ifndef	TEXXET
static	long
set_no_char P1C(wide_ubyte, ch)
#else
static	void
set_no_char P2C(wide_ubyte, cmd, wide_ubyte, ch)
#endif
{
	if (currinf.virtual) {
	    currinf.fontp = currinf.virtual->first_font;
	    if (currinf.fontp != NULL) {
		maxchar = currinf.fontp->maxchar;
		currinf.set_char_p = currinf.fontp->set_char_p;
#ifndef	TEXXET
		return (*currinf.set_char_p)(ch);
#else
		(*currinf.set_char_p)(cmd, ch);
		return;
#endif
	    }
	}
	tell_oops("attempt to set character of unknown font");
	/* NOTREACHED */
}


/*
 *	Set rule.  Arguments are coordinates of lower left corner.
 */

static	void
set_rule(h, w)
	int h, w;
{
#ifndef	TEXXET
	put_rule(PXL_H, PXL_V - h + 1, (unsigned int) w, (unsigned int) h);
#else
	put_rule(PXL_H - (currinf.dir < 0 ? w - 1 : 0), PXL_V - h + 1,
	    (unsigned int) w, (unsigned int) h);
#endif
}

#define	xspell_conv(n)	spell_conv0(n, current_dimconv)

static	void
draw_part(minframe, current_dimconv)
	struct frame	*minframe;
	double		current_dimconv;
{
	ubyte ch;
#ifdef	TEXXET
	struct drawinf	oldinfo;
	ubyte	oldmaxchar;
	off_t	file_pos;
	int	refl_count;
#endif
	float curr_pt_size; // added SU
	static char *spc_bak = NULL;

	currinf.fontp = NULL;
	currinf.set_char_p = set_no_char;
#ifdef	TEXXET
	currinf.dir = 1;
	scan_frame = NULL;	/* indicates we're not scanning */
#endif
	//	delay_src = False;
	for (;;) {
	    ch = xone();
	    if (debug & DBG_DVI)
		print_dvi(ch);
	    if (ch <= (ubyte) (SETCHAR0 + 127)) {
#ifndef	TEXXET
	      DVI_H += (*currinf.set_char_p)(ch);
#else
	      (*currinf.set_char_p)(ch, ch);
#endif
		  /* check if there are delayed src specials */
		  if (delay_src) {
			//			fprintf(stderr, "now calling src spec \n");
			applicationDoSpecial(spc_bak, 1);
			//			delay_src = false;
		  }
		  else {
			//			fprintf(stderr, "NOT calling src spec \n");
		  }
		}
	    else if (FNTNUM0 <= ch && ch <= (ubyte) (FNTNUM0 + 63)) {
		  change_font((unsigned long) (ch - FNTNUM0));
		  //		  fprintf(stderr, "changing font\n");
		}
	    else {
		long a, b;

		switch (ch) {
		    case SET1:
		    case PUT1:
#ifndef	TEXXET
			a = (*currinf.set_char_p)(xone());
			if (ch != PUT1) DVI_H += a;
#else
			(*currinf.set_char_p)(ch, xone());
#endif
			break;

#ifdef Omega
                  case SET2:
                  case PUT2:
#ifndef       TEXXET
                        a = (*currinf.set_char_p)(xnum(2));
                        if (ch != PUT2) DVI_H += a;
#else
                        (*currinf.set_char_p)(ch, xnum(2));
#endif
                        break;
#endif /* Omega */

		    case SETRULE:
			/* Be careful, dvicopy outputs rules with
			   height = 0x80000000.  We don't want any
			   SIGFPE here. */
			a = xsfour();
			b = xspell_conv(xsfour());
#ifndef	TEXXET
			if (a > 0 && b > 0)
#else
			if (a > 0 && b > 0 && scan_frame == NULL)
#endif
			    set_rule(pixel_round(xspell_conv(a)),
				pixel_round(b));
			DVI_H += DIR * b;
			break;

		    case PUTRULE:
			a = xspell_conv(xsfour());
			b = xspell_conv(xsfour());
#ifndef	TEXXET
			if (a > 0 && b > 0)
#else
			if (a > 0 && b > 0 && scan_frame == NULL)
#endif
			    set_rule(pixel_round(a), pixel_round(b));
			break;

		    case NOP:
			break;

		    case BOP:
			xskip((long) 11 * 4);
			DVI_H = OFFSET_X;
			DVI_V = OFFSET_Y;
			PXL_V = pixel_conv(DVI_V);
			WW = XX = YY = ZZ = 0;
			break;

		    case EOP:
			if (current_frame != minframe)
			    tell_oops("stack not empty at EOP");
			return;

		    case PUSH:
			if (current_frame->next == NULL) {
			    struct frame *newp = xmalloc(sizeof(struct frame));
			    current_frame->next = newp;
			    newp->prev = current_frame;
			    newp->next = NULL;
			}
			current_frame = current_frame->next;
			current_frame->data = currinf.data;
			break;

		    case POP:
			if (current_frame == minframe)
			    tell_oops("more POPs than PUSHes");
			currinf.data = current_frame->data;
			current_frame = current_frame->prev;
			break;

#ifdef	TEXXET
		    case SREFL:
			if (scan_frame == NULL) {
			    /* we're not scanning:  save some info. */
			    oldinfo = currinf;
			    oldmaxchar = maxchar;
			    if (!currinf.virtual)
				file_pos = xtell(currinf.pos);
			    scan_frame = current_frame; /* now we're scanning */
			    refl_count = 0;
			    break;
			}
			/* we are scanning */
			if (current_frame == scan_frame) ++refl_count;
			break;

		    case EREFL:
			if (scan_frame != NULL) {	/* if we're scanning */
			    if (current_frame == scan_frame && --refl_count < 0)
			    {
				/* we've hit the end of our scan */
				scan_frame = NULL;
				/* first:  push */
				if (current_frame->next == NULL) {
				    struct frame *newp = 
				      xmalloc(sizeof(struct frame));
				    current_frame->next = newp;
				    newp->prev = current_frame;
				    newp->next = NULL;
				}
				current_frame = current_frame->next;
				current_frame->data = currinf.data;
				/* next:  restore old file position, XX, etc. */
				if (!currinf.virtual) {
				    off_t bgn_pos = xtell(dvi_buffer);

				    if (file_pos >= bgn_pos) {
					oldinfo.pos = dvi_buffer
					    + (file_pos - bgn_pos);
					oldinfo.end = currinf.end;
				    }
				    else {
					(void) lseek(fileno(dvi_file), file_pos,
					    SEEK_SET);
					oldinfo.pos = oldinfo.end;
				    }
				}
				currinf = oldinfo;
				maxchar = oldmaxchar;
				/* and then:  recover position info. */
				DVI_H = current_frame->data.dvi_h;
				DVI_V = current_frame->data.dvi_v;
				PXL_V = current_frame->data.pxl_v;
				/* and finally, reverse direction */
				currinf.dir = -currinf.dir;
			    }
			    break;
			}
			/* we're not scanning, */
			/* so just reverse direction and then pop */
			currinf.dir = -currinf.dir;
			currinf.data = current_frame->data;
			current_frame = current_frame->prev;
			break;
#endif	/* TEXXET */

		    case RIGHT1:
		    case RIGHT2:
		    case RIGHT3:
		    case RIGHT4:
			DVI_H += DIR * xspell_conv(xsnum(ch - RIGHT1 + 1));
			break;

		    case W1:
		    case W2:
		    case W3:
		    case W4:
			WW = xspell_conv(xsnum(ch - W0));
		    case W0:
			DVI_H += DIR * WW;
			break;

		    case X1:
		    case X2:
		    case X3:
		    case X4:
			XX = xspell_conv(xsnum(ch - X0));
		    case X0:
			DVI_H += DIR * XX;
			break;

		    case DOWN1:
		    case DOWN2:
		    case DOWN3:
		    case DOWN4:
			DVI_V += xspell_conv(xsnum(ch - DOWN1 + 1));
			PXL_V = pixel_conv(DVI_V);
			break;

		    case Y1:
		    case Y2:
		    case Y3:
		    case Y4:
			YY = xspell_conv(xsnum(ch - Y0));
		    case Y0:
			DVI_V += YY;
			PXL_V = pixel_conv(DVI_V);
			break;

		    case Z1:
		    case Z2:
		    case Z3:
		    case Z4:
			ZZ = xspell_conv(xsnum(ch - Z0));
		    case Z0:
			DVI_V += ZZ;
			PXL_V = pixel_conv(DVI_V);
			break;

		    case FNT1:
		    case FNT2:
		    case FNT3:
		    case FNT4:
			change_font(xnum(ch - FNT1 + 1));
			break;

		    case XXX1:
		    case XXX2:
		    case XXX3:
		    case XXX4:
			a = xnum(ch - XXX1 + 1);
			if (a > 0) {
			  spc_bak = read_special(a);
			  applicationDoSpecial(spc_bak, 0);
			}
			break;

		    case FNTDEF1:
		    case FNTDEF2:
		    case FNTDEF3:
		    case FNTDEF4:
			xskip((long) (12 + ch - FNTDEF1 + 1));
			a = (long) xone();
			xskip(a + (long) xone());
			break;

#ifndef	TEXXET
		    case SREFL:
		    case EREFL:
#endif
		    case PRE:
		    case POST:
		    case POSTPOST:
			tell_oops("shouldn't happen: %s encountered",
				dvi_table2[ch - (FNTNUM0 + 64)]);
			break;

		    default:
			tell_oops("unknown op-code %d", ch);
		} /* end switch*/
	    } /* end else (ch not a SETCHAR or FNTNUM) */
	} /* end for */
}

#undef	xspell_conv

#ifdef HTEX 
static void
check_for_anchor() /* see if anchor requested, if so go to it */
{
	if (anchor_name != NULL) {
		htex_reinit(); /* Set up hypertext stuff */
		htex_do_loc(anchor_name);
		free(anchor_name);
		anchor_name = NULL;
	}
}
#endif /* HTEX */

void draw_page P1C()
{
  extern HDC magDC, magMemDC;

	/* Check for changes in dvi file. */
	if (!check_dvi_file()) return;

#ifdef WIN32
	if (! allowDrawingChars && !isPrinting)
	  bColorPage = FALSE;
#endif
#if	PS
	if (scanned_page < current_page) prescan();
#ifdef WIN32
	{
	  if (!bMagDisp && !allowDrawingChars && !isPrinting) {
	    psToDisplay = False;
	  }
	}
#endif
#endif

	if (!allowDrawingChars && !isPrinting) {
	  RECT r;
	  COLORREF new_back_Pixel = get_back_color(current_page);
#if 0
 	  extern void dump_colors(char *);
	  dump_colors("draw_page()");
#endif
	  if ( back_Pixel != new_back_Pixel ) {
		back_Pixel = new_back_Pixel;
		if (backBrush)
		  DeleteObject(backBrush);
#if 0 /* DEBUG_COLOR */
		fprintf(stderr, "Creating back brush %-8x\n", back_Pixel);
#endif
		backBrush = CreateSolidBrush(back_Pixel);
	  }
#if 0
	  fprintf(stderr, "draw_page filling bg with color %-8x\n", back_Pixel);
#endif
	  r.left = min_x - currwin.base_x;
	  r.top = min_y - currwin.base_y;
	  r.right = min(page_w - currwin.base_x, max_x);
	  r.bottom = min(page_h - currwin.base_y, max_y);
	  FillRect(foreGC, &r, backBrush);
	}

#ifdef WIN32
	if (!isPrinting) {
	  /* FIXME : we should ensure that base_x and base_y
	     do correspond to Scroll variables everywhere ! */
	  extern int xCurrentScroll, yCurrentScroll;
	  if (1) {
	    put_border(-currwin.base_x, -currwin.base_y,
		       ROUNDUP(unshrunk_paper_w, shrink_factor) + 2,
		       ROUNDUP(unshrunk_paper_h, shrink_factor) + 2, highGC);
	    }
	  else {
	    put_border(-xCurrentScroll, -yCurrentScroll,
		       ROUNDUP(unshrunk_paper_w, shrink_factor) + 2,
		       ROUNDUP(unshrunk_paper_h, shrink_factor) + 2, highGC);
	  }
	}
#else
	put_border(-currwin.base_x, -currwin.base_y,
	    ROUNDUP(unshrunk_paper_w, shrink_factor) + 2,
	    ROUNDUP(unshrunk_paper_h, shrink_factor) + 2, highGC);
#endif
#if 0
#ifdef GRID
	if ( grid_mode > 0 )	/* grid is wanted */
	  put_grid(-currwin.base_x, -currwin.base_y,
		   ROUNDUP(unshrunk_paper_w, shrink_factor) + 2,
		   ROUNDUP(unshrunk_paper_h, shrink_factor) + 2, 
		   ROUNDUP(unshrunk_paper_unit, shrink_factor), 
		   grid1GC, grid2GC, grid3GC);
#endif /* GRID */
#endif
	(void) lseek(fileno(dvi_file), page_offset[current_page], SEEK_SET);

	bzero((char *) &currinf.data, sizeof(currinf.data));
	currinf.tn_table_len = TNTABLELEN;
	currinf.tn_table = tn_table;
	currinf.tn_head = tn_head;
	currinf.pos = currinf.end = dvi_buffer;
	currinf.virtual = NULL;
#ifdef HTEX
 	htex_initpage(); /* Start up the hypertext stuff for this page */
#endif
	psfig_begun = False;
	draw_part(current_frame = &frame0, dimconv);
#ifdef HTEX
 	htex_donepage(current_page, 1); /* Finish up the hypertext stuff for this page */
 	check_for_anchor(); /* see if a new anchor was requested */
#endif
#if	PS
	psp.endpage();
#endif
#ifdef WIN32
#ifdef GRID
	if ( grid_mode > 0 ) {	/* grid is wanted */
	  extern unsigned int xCurrentScroll, yCurrentScroll;
	  put_grid(&currwin,
		   ROUNDUP(unshrunk_paper_unit, shrink_factor), 
#if 0
		   grid1GC, grid2GC, grid3GC
#else
		   highGC, highGC, highGC
#endif
		   );
	}
#endif /* GRID */
#endif
}

#ifdef HTEX
extern int *htex_parsedpages; /* List of pages parsed */

/* Parse dvi pages not yet scanned by xdvi, searching for html anchors */
void htex_parsepages()
{
	int i;

	for (i=0; i < total_pages; i++) {
		if (htex_parsedpages[i] != 0) continue;
		htex_parse_page(i);
	}
	/* Return to original position in dvi file? */
}

void htex_parse_page(i)
int i;
{
	/* Skip to beginning of page i */
/* Stuff from draw_page in dvi_draw.c: */
	(void) lseek(fileno(dvi_file), page_offset[i], SEEK_SET);
	bzero((char *) &currinf.data, sizeof(currinf.data));
	currinf.tn_head = tn_head;
	currinf.pos = currinf.end = dvi_buffer;
	currinf.virtual = NULL;
	htex_initpage();
	htex_scanpage(i);
	htex_donepage(i, 0);
}


/* HTeX parse page without drawing it function: */
/* Basically the same as is done by draw_part, except we don't
	do anything other than record html anchors */
/* Assumes page has been set up properly as above */
void htex_scanpage(pageno)
int pageno;
{
	ubyte ch;
	long a;

	/* Read in page, searching for XXX1 to XXX4 (\special)
		sections and ignoring everything else */
	for (;;) {
	    ch = xone();
	    if (ch <= SETCHAR0 + 127) { /* It's a character - do nothing */
	    } else if (FNTNUM0 <= ch && ch <= FNTNUM0 + 63) {
	    	/* It's a previously numbered font - again do nothing */
	    } else {
	    	switch(ch) {
		    case PUSH: /* No args */
		    case POP: /* No args */
	    	    case NOP: /* Nothing! */
	    	    	break;
	    	    case SET1:
	    	    case PUT1:
	    	    	xskip((long) 1); /* Set or put 1 char - skip it */
	    	    	break;
#ifdef Omega
                    case SET2:
                    case PUT2:
                        xskip((long) 2); /* Set or put 16-bit char - skip it */
                        break;
#endif
		    case SETRULE:
		    case PUTRULE:
		    	xskip((long) 8); /* two integers a and b */
		    	break;
		    case BOP: /* Beginning of page stuff */
		    	xskip((long) 11*4);
		    	break;
		    case EOP: /* End of the page! */
		    	return;
		    case SREFL:
		    case EREFL:
		    	break;
		    case RIGHT1:
		    case RIGHT2:
		    case RIGHT3:
		    case RIGHT4:
		    	xskip(ch - RIGHT1 + 1);
		    	break;
		    case W1:
		    case W2:
		    case W3:
		    case W4:
		    	xskip(ch - W0);
		    case W0:
		    	break;
		    case X1:
		    case X2:
		    case X3:
		    case X4:
		    	xskip(ch - X0);
		    case X0:
		    	break;
		    case DOWN1:
		    case DOWN2:
		    case DOWN3:
		    case DOWN4:
		    	xskip(ch - DOWN1 + 1);
		    	break;
		    case Y1:
		    case Y2:
		    case Y3:
		    case Y4:
		    	xskip(ch - Y0);
		    case Y0:
		    	break;
		    case Z1:
		    case Z2:
		    case Z3:
		    case Z4:
		    	xskip(ch - Z0);
		    case Z0:
		    	break;
		    case FNT1:
		    case FNT2:
		    case FNT3:
		    case FNT4:
		    	xskip(ch - FNT1 + 1);
		    	break;
		    case XXX1:
		    case XXX2:
		    case XXX3:
		    case XXX4: /* Only thing we pay attention to! */
		    	a = xnum(ch - XXX1 + 1);
		    	if (a > 0)
		    		htex_dospecial(a, pageno);
			break;
		    case FNTDEF1:
		    case FNTDEF2:
		    case FNTDEF3:
		    case FNTDEF4:
		    	xskip((long) (12 + ch - FNTDEF1 + 1));
		    	xskip((long) xone() + (long) xone());
		    	break;
		    case PRE:
		    case POST:
		    case POSTPOST:
		    default:
		    	break;
	    	}
	    }
	}
}

/* Do the same same stuff as special() in dvi_draw.c */
/* But only check HTeX specials */
void htex_dospecial(nbytes, pageno)
long nbytes;
int pageno;
{
	static char *cmd = NULL;
	static long cmdlen = -1;
	char *p;

	if (cmdlen < nbytes) {
		if (cmd) free(cmd);
		cmd = xmalloc((unsigned) nbytes+1);
		cmdlen = nbytes;
	}
	p = cmd;
	for (;;) {
		int i = currinf.end - currinf.pos;
		if (i > nbytes) i = nbytes;
		bcopy((_Xconst char *) currinf.pos, p, i);
		currinf.pos += i;
		p += i;
		nbytes -= i;
		if (nbytes == 0) break;
		(void) xxone();
		--(currinf.pos);
	}
	*p = '\0';
	p = cmd;
	while (isspace(*p)) ++p;
	checkHyperTeX(p, pageno);
}
#endif /* HTEX */