File: psoutput.c

package info (click to toggle)
xmhtml 1.1.10-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,296 kB
  • sloc: ansic: 70,372; makefile: 480; sh: 176; perl: 36
file content (2444 lines) | stat: -rw-r--r-- 64,285 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
/*****
* psoutput.c : XmHTML Postscript output routines
*
* This file Version	$Revision$
*
* Creation date:		Tue Nov 17 13:49:18 CET 1998
* Last modification: 	$Date$
* By:					$Author$
* Current State:		$State$
*
* Authors:				Scott Gregory, gregory@sccoast.net
*						Koen D'Hondt, ripley@xs4all.nl
*						Ameet A. Raval, aar@gfdl.gov
*						Frans van Hoesel, hoesel@chem.rug.nl 
*
* Gratefully donated to XmHTML by Scott Gregory.
*
* Copyright (C) 1994-1998 by Ripley Software Development 
* All Rights Reserved
*
* Portions Copyright by Ameet A. Raval & Frans van Hoesel.
* Portions Copyright by Robert C. Tatar & Craig A. McGowan.
* Portions Copyright by John Bradley.
*
* This file is part of the XmHTML Widget Library
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* Scott's Note:
* 
* A hugely collective effort!
*
* The structure and portions of PS code uncerimoniously horked from code
* given to Mosaic by Ameet A. Raval & Frans van Hoesel. Their header reads:
*
* Institution: for Ameet A. Raval:
*					  Geophysical Fluid Dynamics Laboratory,
*					  National Oceanic and Atmospheric Administration,
*					  U.S. Department of Commerce
*					  P.O. Box 308
*					  Princeton, NJ 08542
*			  for Frans van Hoesel:
*					  Xtreme graphics software
*					  Herepoortenmolendrift 36
*					  9711 DH  Groningen
*					  The Netherlands
*
* Copyright
* This work is the product of the United States Government, and is precluded
* from copyright protection.  It is hereby released into the public domain.
*
* WE MAKE NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE FOR
* ANY PURPOSE.  IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED
* WARRANTY. WE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY THE
* USERS OF THIS SOFTWARE. 
*
* Pieces of code are taken from xvps by kind permission of John Bradley.
* 
* Extensive hacks from xwd2ps by Robert C. Tatar and Craig A. McGowan.
* 
*****/
/*****
* ChangeLog 
* $Log$
*****/ 
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef WITH_DMALLOC
#include <dmalloc.h>
#endif

#include <stdlib.h>
#ifdef __STDC__
#include <stdarg.h>
#else
#include <varargs.h>
#endif
#include <stdio.h>		/* must follow stdarg or varargs on LynxOs */
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <math.h>

/* Our private header files */
#include "toolkit.h"
#include XmHTMLPrivateHeader
#include "XmHTMLfuncs.h"

/*** External Function Prototype Declarations ***/

/*** Public Variable Declarations ***/

/*** Private Datatype Declarations ****/
/* for regular-font, bold-font, italic-font, fixed-font */
typedef enum { 
	RF, BF, IF, FR, FB, FI 
}PSFontstyle;

/* structure for holding all footnotes on a page (anchor data) */
typedef struct{
	int nmalloc;
	int count;
	char **items;
}PSFootnote;

typedef struct _PSDisplay {
	Display *dpy;			/* must be the first field!				*/

	XmHTMLWidget html;		/* the XmHTML widget id					*/
	Byte options;			/* options to XmHTMLFormatPostscript()	*/

	/* Output area, Screen definition */
	XmHTMLPaperSize screen;	/* defined output area					*/
	float Points_Pixel;		/* Postscript Pointer Per Pixel			*/
	int Pixels_Page;		/* height of page in pixels				*/
	int Pixels_This_Page;	/* useable height of page in pixels		*/

	/* Output area, positioning */
	int start_y;			/* current vertical pixel position		*/
	int offset;
	int curr_page;			/* current page counter					*/

	/* Font data */
	XmHTMLfont *font;		/* latest font							*/
	char font_style[3];		/* PS font macro name, "RF", etc.		*/
	int font_size;			/* size of the font (ascent)			*/

	/* Output buffer */
	char *string;			/* Output buffer						*/
	int size;				/* size of output buffer				*/
	int len;				/* used size of output buffer			*/ 

	/* Footnotes */
	PSFootnote footnotes;

	/* Hexadecimal conversion */
	Byte hexline[80];		/* accumulation buffer					*/
	int hexi;				/* current position						*/

	/* document fore & background colors */
	unsigned short fg[3];	/* foreground color components			*/
	unsigned short bg[3];	/* background color components			*/

	/* Following fields are unused. Might be used later	*/
	int page_offset;		/* unused								*/
	PSFontstyle oldfn;		/* unused								*/
	int fontascent;			/* unused								*/
	int oldfs;				/* unused								*/
}PSDisplay;

/*** Private Function Prototype Declarations ****/
#define max(a,b)		(a>b ? a : b)
#define min(a,b)		(a<b ? a : b)

/* MONO returns total intensity of r,g,b components .33R+ .5G+ .17B */
#define MONO(rd,gn,bl) (((rd)*11 + (gn)*16 + (bl)*5) >> 13)

/* PSconst_out outputs to the postscript buffer an array of constant strings */
#define PSconst_out(dpy, txt) do{ \
	int i, n = (sizeof txt)/(sizeof txt[0]); \
	for(i = 0; i < n; i++) PSprintf(dpy, "%s\n", txt[i]); \
}while(0)

static float GetDpi(XmHTMLWidget hw);
static void fnDestroy(PSFootnote footnote);
static int fnAdd(PSFootnote footnote, char *buf);
static int PSprintf(PSDisplay *dpy, char *format, ...);
static void PSfootnotes(PSDisplay *dpy);
static void PSfont(PSDisplay *dpy, XmHTMLfont *font, Boolean flush);
static void PSwidgetsOnPage(PSDisplay *dpy);
static void PSshowpage(PSDisplay *dpy);
static void PSnewpage(PSDisplay *dpy);
static void PSinit_latin1(PSDisplay *dpy);
static void PSinit(PSDisplay *dpy);
static void PStrailer(PSDisplay *dpy);
static void PSfootnotes(PSDisplay *dpy);
static void PStext(PSDisplay *dpy, String t, Boolean underline);
static void PScheckPage(PSDisplay *dpy, int x, int y);
static void PSshowpage(PSDisplay *dpy);
static void PSmoveto(PSDisplay *dpy, int x, int y);
/* static void PSmove_offset(PSDisplay *dpy, int offset); */
static int PSencode(unsigned char *data, unsigned char *rle, int size);
static void PSfootnotes(PSDisplay *dpy);
static int PShex(PSDisplay *dpy, Byte val, int flush);
static void PSColorImage(PSDisplay *dpy);
static void PScolormap(PSDisplay *dpy, Boolean color, int nc, 
	   Dimension *rmap, Dimension *gmap, Dimension *bmap);
static void PSrle_cmapimage(PSDisplay *dpy, int color);
static int PSImageBW(PSDisplay *dpy, Byte *data, int w, int h, Boolean inverse);
static void PSImage(PSDisplay *dpy, XmHTMLImage *image, int x, int y);
static void PSColorImage(PSDisplay *dpy);

/* Postscript TKA functions */
static ToolkitAbstraction *_CreatePostscriptTka(XmHTMLWidget html);

static int pstkSetForeground(Display *disp, XGC gc, unsigned long foreground);
static int pstkSetFont(Display *disp, XGC gc, XmHTMLfont *font);
static int pstkDrawString(struct _ToolkitAbstraction* tka,
	struct _XmHTMLFont *font, XGC gc, int x, int y, const char *string, int length);
static void pstkDrawAnchorData(Display *disp, WINDOW win, XGC gc, int x,
	int y, XmHTMLObjectTableElement data);
static int pstkDrawLine(Display *disp, WINDOW win, XGC gc, int x1, int y1,
	int x2, int y2);
static int pstkDrawLines(Display *disp, DRAWABLE win, XGC gc, XPoint *points,
	int num_points, int mode);
static int pstkDrawRectangle(Display *disp, WINDOW win, XGC gc, int x, int y,
	unsigned int width, unsigned int height);
static int pstkFillRectangle(Display *disp, WINDOW win, XGC gc, int x, int y,
	unsigned int width, unsigned int height);
static int pstkDrawArc(Display *disp, WINDOW win, XGC gc, int x, int y,
	unsigned int width, unsigned int height, int angle1, int angle2);
static int pstkFillArc(Display *disp, WINDOW win, XGC gc, int x, int y,
	unsigned int width, unsigned int height, int angle1, int angle2);
static void pstkDrawImage(XmHTMLWidget html, XmHTMLImage *image, XGC gc,
	int src_x, int src_y, unsigned int width, unsigned int height,
	int dest_x, int dest_y);
static void pstkDrawShadows(Display *disp, DRAWABLE drawable, 
	XGC top_shadow_GC, XGC bottom_shadow_GC,
#if NeedWidePrototypes
	int x, int y, int width, int height, int shadow_thickness,
#else
	Position x, Position y, Dimension width, Dimension height,
	Dimension shadow_thickness,
#endif
	unsigned int shadow_type);

/* empty wrappers */


/*** Private Variable Declarations ***/

#define CR '\015'
#define LF '\012'

#define TOP_MARGIN		dpy->screen.top_margin
#define BOT_MARGIN		dpy->screen.bottom_margin
#define LEFT_MARGIN		dpy->screen.left_margin
#define PAGE_HEIGHT		dpy->screen.height
#define PAGE_WIDTH		dpy->screen.width

#define F_FULLCOLOR		0
#define F_GREYSCALE		1
#define F_BWDITHER		2
#define F_REDUCED		3

#define L_PAREN			'('
#define R_PAREN			')'
#define B_SLASH			'\\'
#define MAX_ASCII		'\177'

#define FOOTNOTE_ROW_HEIGHT	8		/* helvetica 8 */

/*****
* Name:			GetDpi
* Return Type: 	float
* Description: 	returns dots-per-inch of the screen. Calculates the pixel
*				density in dots per inch on the current Widget screen.
* In: 
*	html:		XmHTMLWidget id
* Returns:
*	screen density.
*****/
static float
GetDpi(XmHTMLWidget html)
{
	ToolkitAbstraction *tka = HTML_ATTR(tka);
	float dpi;

	dpi = 25.4 * tka->width/tka->widthMM;

	/* no earthly monitor does this */
	if(dpi < 1.0 || dpi > 10000.0)
		dpi = 72.0;
	return dpi;
}

/**********
***** Footnote functions. If requested, XmHTML prints the referring hyperlink
***** of each anchor as a footnote.
**********/

/*****
* Name:			fnDestroy
* Return Type: 	void
* Description: 	destroy all footnote data for this page.
* In: 
*	footnote:	array of footnotes to be destroyed.
* Returns:
*	nothing.
*****/
static void
fnDestroy(PSFootnote footnote)
{
	int i;

	if(footnote.items)
	{
		for(i = 0; footnote.items[i] != NULL; ++i)
			free(footnote.items[i]);
		free(footnote.items);
		footnote.items = NULL;
	}
	footnote.count = footnote.nmalloc = 0;
}

/*****
* Name: 		fnAdd
* Return Type: 	int
* Description:	add a footnote to the current page and return its number
* In: 
*	buf:		footnote value
* Returns:
*	footnote number.
*****/
static int
fnAdd(PSFootnote footnote, char *buf)
{
	int fnum;

	if(!footnote.items)
	{
		/* initialize current page footnotes buffer */
		footnote.nmalloc = 10;
		footnote.count = 0;
		footnote.items = (char **)malloc(sizeof(char *)*footnote.nmalloc);
		memset(footnote.items, 0, sizeof(char *)*footnote.nmalloc);
	}
	else
	{
		/*****
		* We've already got a buffer, see if it's large enough and extend
		* if required.
		*****/
		if(footnote.count >= (footnote.nmalloc-1))
		{
			footnote.nmalloc += 10;
			footnote.items = (char **)realloc(footnote.items,
								sizeof(char *)*footnote.nmalloc);
		}
	}

	/* Check if this footnote has already been added */
	for(fnum = 0; fnum < footnote.count; ++fnum)
		if(strcmp(footnote.items[fnum], buf) == 0)
			return(fnum);
			
	/* this is a new footnote, store it */
	fnum = footnote.count;
	footnote.count++;

	footnote.items[fnum] = strdup(buf);
	footnote.items[fnum+1] = NULL;

	return(fnum);
}

/*****
* Name: 		PSprintf
* Return Type: 	int
* Description: 	sprintf but can dynamically extend the destination buffer.
* In: 
*
* Returns:
*
*****/
static int
#ifdef __STDC__
PSprintf(PSDisplay *dpy, char *format, ...)
#else
PSprintf(dpy, format, va_alist)
	PSDisplay *dpy;
	String format;
	va_dcl
#endif
{
	int len;
	char *s;
#ifdef __STDC__
	va_list args;
#else
	va_alist args
#endif

	if(dpy->size - dpy->len < 1024)
	{
		dpy->size += 1024;
		s = (char*)realloc(dpy->string, dpy->size);
		dpy->string = s;
	}
#ifdef __STDC__
	va_start(args, format);
#else
	va_start(args);
#endif
	len = vsprintf(dpy->string + dpy->len, format, args);

	va_end(args);

	/* update size of destination buffer */
	if(len != 0)
		dpy->len += strlen(dpy->string + dpy->len);

	/* all done */
	return(len);
}

/*****
* Name: 		PSfootnotes
* Return Type: 	void
* Description: Display the footer and all footnotes collected during this page
* In: 
*	dpy:		current postscript output area
* Returns:
*	nothing.
*****/
static void
PSfootnotes(PSDisplay *dpy)
{
	int x, y, i;

	if(!(dpy->options & XmHTMLTEXT_ADDFOOTER))
		return;

	x = dpy->screen.left_margin;
	y = dpy->Pixels_This_Page;

	PSprintf(dpy, "%% PSfootnotes\n");
	PSprintf(dpy, "0 setgray\n");

	PSprintf(dpy, "%d -%d M %d 0 RL stroke\n", x, y,
		dpy->screen.width - dpy->screen.left_margin -
		dpy->screen.right_margin);

	/* set font */
	PSprintf(dpy, "\n/helvetica-bold %d SF\n", FOOTNOTE_ROW_HEIGHT);

	/* show page number */
	PSprintf(dpy, "newpath %d -%d M 0 -%d RL ( Page %d ) stringwidth "
		"pop neg 0 RL 0 %d RL closepath stroke\n",
		dpy->screen.width - dpy->screen.right_margin, y,
		FOOTNOTE_ROW_HEIGHT + 2, dpy->curr_page, FOOTNOTE_ROW_HEIGHT+2);
	PSprintf(dpy, "%d -%d M ( Page %d ) stringwidth pop neg -%d R "
		"(Page %d ) S\n",
		dpy->screen.width - dpy->screen.right_margin, y,
		dpy->curr_page, FOOTNOTE_ROW_HEIGHT, dpy->curr_page);

	/* print the footnotes if requested */
	if(!(dpy->options & XmHTMLTEXT_ANCHORFOOTNOTES) ||
		dpy->footnotes.count <= 0)
	{
		fnDestroy(dpy->footnotes);
		return;
	}
	for(i = 0; dpy->footnotes.items[i] != NULL; ++i)
	{
		y += 2 + FOOTNOTE_ROW_HEIGHT;

		/* set footnote number font */
		PSprintf(dpy, "/helvetica-bold %d SF\n", FOOTNOTE_ROW_HEIGHT);

		/* footnote number */
		PSprintf(dpy, "%d -%d M (%d. )S\n", x, y, i+1);

		/* set footnote content font */
		PSprintf(dpy, "/helvetica %d SF\n", FOOTNOTE_ROW_HEIGHT);

		/* footnote contents */
		PSprintf(dpy, "(%s)S\n", dpy->footnotes.items[i]);
	}

	/* all done */
	fnDestroy(dpy->footnotes);
}

/*****
* Name:			PSfont
* Return Type: 	void
* Description: 	change local font.
* In: 
*	dpy:		current postscript output area;
*	font:		master font;
*	flush:		flush font settings at end of page.
* Returns:
*	nothing.
* Note:
*	This needs some work. Ultimatly, Postscript output should use the
*	specified document fonts instead of referring to a set of default
*	fonts. This will require a lot of work as it requires XmHTML to include
*	font definitions for each font that is used in this document.
*	Ideally, the Postscript ToolkitAbstraction should have an associated
*	font cache instead of basing font selection on the fonts residing
*	in the X server.
*****/
static void
PSfont(PSDisplay *dpy, XmHTMLfont *font, Boolean flush)
{
	static XmHTMLfont *last_font=NULL;
	char fstyle[3];
	static char fstr[25]="\0";
	int i;

	/* no font change */
	if(font == last_font && font != NULL)
		return;

	/* Forced font flush or no font provided */
	if(flush || font == NULL)
	{
		/* First time entry, set initial size */
		if(last_font == NULL || fstr[0]=='\0')
		{
			PSprintf(dpy, "RF 14 SF\n");
			return;
		}
		else
		{
			/* use current font */
			PSprintf(dpy, "%s\n", fstr);
			return;
		}
	}

	/* initialize font style array */
	memset(fstyle, 0, sizeof(fstyle));

	if(font->style & FONT_SCALABLE || strstr(font->font_family, "times"))
	{
		/* scalable font */
		fstyle[1]='F';
		i = 0;
	}
	else
	{
		/* fixed font */
		fstyle[0]='F';
		i = 1;
	}

	/* set font styles */
	if(font->style & FONT_BOLD)
		fstyle[i]='B';
	else if(font->style & FONT_MEDIUM)
		fstyle[i] = 'R';
	else if(font->style & FONT_ITALIC)
		fstyle[i] = 'I';
	else
		fstyle[i] = 'R';

	/* comment stating original fontname */
	PSprintf(dpy, "%%FontStyle=0x%x %s, size = %i points\n", (int)font->style,
		font->font_name, font->ptsize);

	/* store & set size */
	sprintf(fstr, "%s %d SF", fstyle, font->ptsize);
	PSprintf(dpy, "%s\n", fstr);

	/* store font data */
	dpy->font = font;
	strcpy(dpy->font_style, fstyle);
	dpy->font_size = font->m_ascent;

	if(font)
		last_font = font;
}

/*****
* Name:			PSwidgetsOnPage
* Return Type: 	void
* Description: 	render all HTML FORM elements residing on the current page.
* In: 
*	dpy:		current Postscript output area
* Returns:
*	nothing.
* Note:
*	Called at end of a page to draw the form widgets.
*	
*	Koen,
*		for this to work correctly, each widget would have had to have
*	been mapped at one time to get its formatted position. Then in here
*	we would have to map those that aren't, wait on the expose, then
*	grab the image and unmap it.
*	
*	Kind of academic until we can ensure all form widgets have been mapped.
*
*	Scott,
*		Since we can't rely on each widget being mapped (it's very well
*		possible the XmHTMLWidget will get mapped at all: think about a
*		HTML to Postscript converter), we just draw a grayed rectangle.
*		Later on we could decide to do it another way, say map the
*		widget to an offscreen position, take a snapshot of that and render
*		the resulting image. Come to think of it, that would be the way
*		to do it.
*****/
static void
PSwidgetsOnPage(PSDisplay *dpy)
{
	XmHTMLWidget html = dpy->html;
#if 0
	ToolkitAbstraction *tka = HTML_ATTR(tka);
#endif
	XmHTMLFormData *form;
	XmHTMLForm *entry;
	int xs, ys;

	/* XmHTMLExtObj *u; ??Should we print these also?? Not implemented yet */
	
	if((form = HTML_ATTR(form_data)) == NULL)
		return;
		
	/* walk all defined forms */
	for(; form != NULL; form = form->next)
	{
		/* check all form components */
		for(entry = form->components; entry != NULL; entry = entry->next)
		{
			/* only render visible form members */
			if(entry->w)
			{
				/*****
				* get widget position on current page
				* data->x and data->y contain the computed widget's position
				* relative to the start of the document, so to compute
				* the widget position relative to the current page we must
				* substract the region that's already behind us.
				*****/
				xs = entry->data->x - HTML_ATTR(scroll_x);
				ys = entry->data->y - HTML_ATTR(scroll_y);

				/* check if this widget is in the viewable area */
				if(xs + entry->width > 0 && xs < HTML_ATTR(work_width) &&
					ys + entry->height > 0 && ys < HTML_ATTR(work_height))
				{
					PSprintf(dpy, "%% PSwidgetsOnPage %s (%dx%d+%d+%d)\n",
						TkaWidgetName(entry->w), entry->data->width,
						entry->data->height, xs, ys);

					/* render a grayed rectangle */
#if 1
					/* Positioning might be wrong... */

					PSprintf(dpy, "%d %d translate", xs,
						-(ys - dpy->start_y) - entry->data->height);
					PSprintf(dpy, "gsave currentpoint %d sub translate ",
						entry->data->height);
					PSprintf(dpy, "%d %d scale\n", entry->data->width,
						entry->data->height);
					PSprintf(dpy, "SQ 0.9 setgray fill\ngrestore\n");
#else
					if(tka->IsRealized(entry->w) && entry->mapped)
						ImageToPs(dpy, XtDisplay((Widget)html),
							XtWindow(c->w), 0, 0, c->width, c->height,
							xs, -(ys - dpy->start_y), 1);
#endif
				}
			}
		}
	}
}

/*****
* Name:			PSshowpage
* Return Type: 	end of page function. Show current page and restore
*				any changes to the printer state.
* Description: 
* In: 
*	dpy:		current postscript output area.
* Returns:
*	nothing.
*****/
static void
PSshowpage(PSDisplay *dpy)
{
	PSwidgetsOnPage(dpy);
	
	if(dpy->curr_page > 0)
		PSfootnotes(dpy);

	dpy->Pixels_This_Page = dpy->Pixels_Page;

	PSprintf(dpy, "showpage restore\n");
}

/*****
* Name: 		PSnewpage
* Return Type: 	begin a fresh page. Increments page count and handles
*				structured comment conventions.
* Description: 
* In: 
*	dpy:		current postscript output area.
* Returns:
*	nothing.
*****/
static void
PSnewpage(PSDisplay *dpy)
{
	dpy->curr_page++;

	/*****
	* The PostScript reference Manual states that the Page: Tag
	* should have a label and a ordinal; otherwise programs like
	* psutils fail -- gustaf
	*****/
	PSprintf(dpy, "%%%%Page: %d %d\n", dpy->curr_page, dpy->curr_page);
	PSprintf(dpy, "save\nNP\n");
	PSfont(dpy, (Font)0, True); /* force re-flush of last font used */

	/* reserve footnote space (if requested) */
	if(dpy->options & XmHTMLTEXT_ADDFOOTER)
		dpy->Pixels_This_Page -= FOOTNOTE_ROW_HEIGHT;
}

/*****
* Name:			PSinit_latin1
* Return Type: 	void
* Description: 	handle ISO Latin1 encoding.
* In: 
*	dpy:		current postscript output area
* Returns:
*	nothing.
* Note:
*	This table contains the names of all characters in the ISO Latin1 font
*	encoding (191 defined characters in total). The first valid character
*	is space.
*	This table comes from the Idraw program (from Stanford's InterViews 
*	package), courtesy of Steinar Kjaernsrd, steinar@ifi.uio.no
*****/
static void
PSinit_latin1(PSDisplay *dpy)
{
	static char *txt[] = {
		"/reencodeISO {",
		"dup dup findfont dup length dict begin",
		"{ 1 index /FID ne { def }{ pop pop } ifelse } forall",
		"/Encoding ISOLatin1Encoding D",
		"currentdict end definefont",
		"} D",
		"/ISOLatin1Encoding [",
		"/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef",
		"/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef",
		"/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef",
		"/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef",
		"/space/exclam/quotedbl/numbersign/dollar/percent/ampersand/quoteright",
		"/parenleft/parenright/asterisk/plus/comma/minus/period/slash",
		"/zero/one/two/three/four/five/six/seven/eight/nine/colon/semicolon",
		"/less/equal/greater/question/at/A/B/C/D/E/F/G/H/I/J/K/L/M/N",
		"/O/P/Q/R/S/T/U/V/W/X/Y/Z/bracketleft/backslash/bracketright",
		"/asciicircum/underscore/quoteleft/a/b/c/d/e/f/g/h/i/j/k/l/m",
		"/n/o/p/q/r/s/t/u/v/w/x/y/z/braceleft/bar/braceright/asciitilde",
		"/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef",
		"/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef/.notdef",
		"/.notdef/dotlessi/grave/acute/circumflex/tilde/macron/breve",
		"/dotaccent/dieresis/.notdef/ring/cedilla/.notdef/hungarumlaut",
		"/ogonek/caron/space/exclamdown/cent/sterling/currency/yen/brokenbar",
		"/section/dieresis/copyright/ordfeminine/guillemotleft/logicalnot",
		"/hyphen/registered/macron/degree/plusminus/twosuperior/threesuperior",
		"/acute/mu/paragraph/periodcentered/cedilla/onesuperior/ordmasculine",
		"/guillemotright/onequarter/onehalf/threequarters/questiondown",
		"/Agrave/Aacute/Acircumflex/Atilde/Adieresis/Aring/AE/Ccedilla",
		"/Egrave/Eacute/Ecircumflex/Edieresis/Igrave/Iacute/Icircumflex",
		"/Idieresis/Eth/Ntilde/Ograve/Oacute/Ocircumflex/Otilde/Odieresis",
		"/multiply/Oslash/Ugrave/Uacute/Ucircumflex/Udieresis/Yacute",
		"/Thorn/germandbls/agrave/aacute/acircumflex/atilde/adieresis",
		"/aring/ae/ccedilla/egrave/eacute/ecircumflex/edieresis/igrave",
		"/iacute/icircumflex/idieresis/eth/ntilde/ograve/oacute/ocircumflex",
		"/otilde/odieresis/divide/oslash/ugrave/uacute/ucircumflex/udieresis",
		"/yacute/thorn/ydieresis",
		"] D",
		"[RF BF IF FR FB FI] {reencodeISO D} forall"
	};

	PSconst_out(dpy, txt);
}

/*****
* Name:			PSinit
* Return Type: 	void
* Description: 	initialize postscript output per HTML document.
* In: 
*	dpy:		current postscript output area
* Returns:
*	nothing.
*****/
static void
PSinit(PSDisplay *dpy)
{
	dpy->offset = dpy->hexi = dpy->page_offset = 0;
	dpy->start_y = 0;

	/* Initialize output buffer */
	dpy->size = 1024;
	dpy->string = (char*)calloc(1, sizeof(char)*dpy->size);
	dpy->len = 0;

	dpy->oldfs = 0;
	dpy->oldfn = RF;
	dpy->curr_page = 0 ;
}

/*****
* Name: 		PSheader
* Return Type: 	void
* Description: 	initialize postscript output and prints the postscript prolog.
* In: 
*	dpy:		current postscript output area
*	title:		document title (if any)
*	font:		document basefont.
* Returns:
*	nothing.
*****/
static void
PSheader(PSDisplay *dpy, char *title, int font)
{
	time_t currtime = time(NULL);

	static char *fontname[] = {
		/* in order: regular, bold, italic */
		"Times-Roman", "Times-Bold", "Times-Italic",
		"Helvetica", "Helvetica-Bold", "Helvetica-Oblique",
		"NewCenturySchlbk-Roman", "NewCenturySchlbk-Bold",
				"NewCenturySchlbk-Italic",
		/* this is a nasty trick, I have put Times in place of
		 * Lucida, because most printers don't have Lucida font
		 */
		"Times-Roman", "Times-Bold", "Times-Italic"
		/* "Lucida", "Lucida-Bold", "Lucida-Italic" */
	};

	static char *txt[] = {
		"%%EndComments",
		"save",
		"/D {def} def /E {exch} D",
		"/M {moveto} D",
		"/S {show} D",
		"/R {rmoveto} D",
		"/L {lineto} D",
		"/RL {rlineto} D",
		"/SQ {newpath 0 0 M 0 1 L 1 1 L 1 0 L closepath} D",
		"/U {gsave currentpoint currentfont /FontInfo get /UnderlinePosition get",
		" 0 E currentfont /FontMatrix get dtransform E pop add newpath moveto",
		" dup stringwidth rlineto stroke grestore S } D",
		"/B {/r E D gsave -13 0  R currentpoint ",
		"  newpath r 0 360 arc closepath fill grestore } D",
		"/OB {/r E D gsave -13 0  R currentpoint ",
		"  newpath r 0 360 arc closepath stroke grestore } D",
		"/NP {xmargin topmargin translate scalfac dup scale newpath } D",
		"/HR {/l E D gsave l 0 RL  stroke grestore } D",
		"/SF {E findfont E scalefont setfont } D",
		"/FR {/Courier } D",
		"/FB {/Courier-Bold } D",
		"/FI {/Courier-Oblique } D"
	};

	/* Postscript level */
	PSprintf(dpy, "%%!PS-Adobe-1.0\n");
	PSprintf(dpy,"%%%%Creator: %s\n", XmHTMLVERSION_STRING);

	/* set document title (if any) */
	if(title != NULL && *title != '\0')
	{
		char *tmp;
		for(tmp = title; *tmp; tmp++)
		{
			/* convert newlines to spaces */
			if(*tmp == CR || *tmp == LF)
				*tmp = ' ';
		}
		PSprintf(dpy, "%%%%Title: %s\n", title);
	}
	else
		PSprintf(dpy, "%%%%Title: (no title)\n");

	/* print remainder of header */

	/* creation data */
	PSprintf(dpy, "%%%%CreationData: %s", ctime(&currtime));

	/* tell no of pages is at the end of the document */
	PSprintf(dpy, "%%%%Pages: (atend)\n");
	PSprintf(dpy, "%%%%PageOrder: Ascend\n");

	/* set document fonts */
	PSprintf(dpy, "%%%%DocumentFonts: %s %s %s Courier Courier-Bold "
		"Courier-Oblique\n", fontname[font*3], fontname[font*3+1],
		fontname[font*3+2]);

	/* print remainder of prolog */
	PSconst_out(dpy, txt);

	PSprintf(dpy, "/RF {/%s} D\n", fontname[font*3]);
	PSprintf(dpy, "/BF {/%s} D\n", fontname[font*3+1]);
	PSprintf(dpy, "/IF {/%s} D\n", fontname[font*3+2]);

	PSinit_latin1(dpy);

	PSprintf(dpy, "/xmargin %d D\n", (int)LEFT_MARGIN);
	PSprintf(dpy, "/topmargin %d D\n", (int)TOP_MARGIN);
	PSprintf(dpy, "/scalfac %.5f D\n", dpy->Points_Pixel);
	PSprintf(dpy, "%%%%EndProlog\n");
}

/*****
* Name:			PStrailer
* Return Type: 	void
* Description: 	write document trailer.
* In: 
*	dpy:		current postscript output area
* Returns:
*	nothing.
*****/
static void
PStrailer(PSDisplay *dpy)
{
	PSprintf(dpy, "%%%%Trailer\n");
	PSprintf(dpy, "restore\n");
	PSprintf(dpy, "%%%%Pages: %d\n", dpy->curr_page);
}

/*****
* Name: 		PStext
* Return Type: 	void
* Description: 	output text, renders the given text, protects special
*				characters and underlines words if required.
* In: 
*	dpy:		current postscript output area
*	t:			text to be drawn
*	underline:	if non-zero, text is drawn underlined.
* Returns:
*	nothing.
*****/
static void
PStext(PSDisplay *dpy, String t, Boolean underline)
{
	String chPtr, t2;					/* temporary buffers */
	int nspecial = 0, nisochar = 0;		/* special character counters */

	chPtr = t;

	/* Count number of special characters in the given text */
	while(*chPtr != '\0')
	{
		if(*chPtr == L_PAREN || *chPtr == R_PAREN || *chPtr == B_SLASH)
			nspecial++;
		else if(*(Byte *)chPtr > (Byte )MAX_ASCII)
			nisochar++;
		chPtr++;
	}

	if(nspecial == 0 && nisochar == 0)
	{
		/* no special characters found, display as is */
		PSprintf(dpy, "(%s)%c\n", t, (underline) ? 'U' : 'S');
		return;
	}

	/*****
	* Special characters found. Create a new output buffer that will
	* contain the original text and any special characters that need to
	* be protected.
	*
	* We don't need to check the return value: all memory allocation
	* routines are actually defined as macros to functions that perform
	* return value checking.
	*****/

	t2 = (String)malloc((chPtr - t) + nspecial + 3*nisochar + 1);

	/*****
	* For each character in the provided text, check if it is a special char.
	* If we have a special, escape it by inserting a backslash into t2, then
	* insert the actual char. Ordinary characters are copied as is.
	*****/
	chPtr = t2;
	while(*t != '\0')
	{
		if(*t == L_PAREN || *t == R_PAREN || *t == B_SLASH)
		{
			/* parenthesis and backslash must be escaped */
			*(chPtr++) = B_SLASH;
			*(chPtr++) = *t;
		}
		else
		{
			/* non-ascii character, escape and convert to octal */
			if(*(Byte*)t > (Byte)MAX_ASCII)
			{
				/*  convert to octal */
				*(chPtr++) = B_SLASH;
				*(chPtr++) = ((int)(*(Byte*)t)>>6 & 007) + '0';
				*(chPtr++) = ((int)(*(Byte*)t)>>3 & 007) + '0';
				*(chPtr++) = ((int)(*(Byte*)t) & 007) + '0';
			}
			else
				*(chPtr++) = *t;
		}

		t++;
	}
	/* terminate */
	*(chPtr) = '\0';

	/* output */
	PSprintf(dpy, "(%s)%c\n", t2, (underline) ? 'U' : 'S');

	/* all done */
	free(t2);
}

/*****
* Name:			PScheckPage
* Return Type: 	void
* Description: 	check if a new new page should be started.
* In: 
*	dpy:		current postscript output area
*	x:			current horizontal pixel position
*	y:			current vertical pixel position
* Returns:
*	nothing
*****/
static void
PScheckPage(PSDisplay *dpy, int x, int y)
{
	if(y > dpy->start_y + dpy->Pixels_This_Page)
	{
		PSshowpage(dpy);
		dpy->start_y = y;
		PSnewpage(dpy);
	}
	dpy->offset = 0;
}

/*****
* Name: 		PSmoveto
* Return Type: 	void
* Description: 	move to output vector to a new x,y location. If the given
*				y value does not fit within the current page, start a new
*				page.
* In: 
*	dpy:		current postscript output area
*	x:			current horizontal pixel position
*	y:			current vertical pixel position
* Returns:
*	nothing.
*****/
static void
PSmoveto(PSDisplay *dpy, int x, int y)
{
	PScheckPage(dpy, x, y);
	dpy->offset = 0;
	PSprintf(dpy, "%d %d M\n", x, -(y - dpy->start_y));
}

/*****
* Name: 		PSmove_offset
* Return Type: 	void
* Description: 	set y-offset. Performs a relative vertical move whenever
*				the offset changes.
* In: 
*	dpy:		current postscript output area
*	offset:		vertical (pixel) offset.
* Returns:
*	nothing.
*****/
/*
static void
PSmove_offset(PSDisplay *dpy, int offset)
{
	if(offset != dpy->offset)
	{
		PSprintf(dpy, "0 %d R\n", dpy->offset - offset );
		dpy->offset = offset;
	}
}
*/

/*****************************************************************************
****************** Image & Color conversion Routines *************************
*****************************************************************************/

/*****
* Name:			PScolormap
* Return Type: 	void
* Description: 	writes a colormap. Produces code for the colormap of
*				any images following. If this is a grayscale image, a
*				grayscale colormap is produced.
* In: 
*	dpy:		current postscript output area
*	color:		indicates color or grayscale.
*	nc:			number of colors
*	rmap:		red color components;
*	gmap:		green color components;
*	bmap:		blue color components;
* Returns:
*	nothing.
*****/
static void
PScolormap(PSDisplay *dpy, Boolean color, int nc,
	Dimension *rmap, Dimension *gmap, Dimension *bmap)
{
	int i;

	/* define the colormap */
	PSprintf(dpy, "/cmap %d string def\n\n\n", nc * ((color) ? 3 : 1));

	/* load it */
	PSprintf(dpy, "currentfile cmap readhexstring\n");

	/* write out each color */
	for(i = 0; i < nc; i++)
	{
		if(color)	/* downscale to 0-255 */
			PSprintf(dpy, "%02x%02x%02x ", rmap[i]>>8, gmap[i]>>8, bmap[i]>>8);
		else 
			PSprintf(dpy, "%02x ", MONO(rmap[i], gmap[i], bmap[i]));

		/* make it look nice */
		if((i % 10) == 9)
			PSprintf(dpy, "\n");
	}
	PSprintf(dpy, "\n");
	PSprintf(dpy, "pop pop\n"); /* lose return values from readhexstring */
}

/*****
* Name:			PSencode
* Return Type: 	int
* Description: 	perform run length encoding.
*				RLE is used to reduce file size (and thus reduce the time
*				required to send it to the printer). The disadvantage is
*				increased processing time.
* In: 
*	data:		data to be rle'd
*	rle:		return buffer.
*	size:		size of data.
* Returns:
*	size of return buffer.
* Note:
*	rle is encoded as such:
*	<count> <value>			# 'run' of count+1 equal pixels
*	<count | 0x80> <count+1 data bytes>	# count+1 non-equal pixels
*	count can range between 0 and 127
*****/
static int
PSencode(unsigned char *data, unsigned char *rle, int size) 
{
    int i, j, blocklen, isrun, rlen;
    unsigned char block[256], pix;

    blocklen = isrun = rlen = 0;

    for(i = 0; i < size; i++) 
    {
		/*****
		* 5 possible states:
		*   0: block empty.
		*   1: block is a run, current pix == previous pix
		*   2: block is a run, current pix != previous pix
		*   3: block not a run, current pix == previous pix
		*   4: block not a run, current pix != previous pix
		*****/

		pix = data[i];

		if(!blocklen) 
		{
		    /* case 0:  empty */
		    block[blocklen++] = pix;
		    isrun = 1;
		}
		else if(isrun) 
		{
		    if(pix == block[blocklen-1])
		    { 
				/*  case 1:  isrun, prev==cur */
				block[blocklen++] = pix;
		    }
		    else 
		    {
				/*  case 2:  isrun, prev!=cur */
				if(blocklen>1) 
				{
				    /*  we have a run block to flush */
					rle[rlen++] = blocklen-1;
					rle[rlen++] = block[0];
					/*  start new run block with pix */
					block[0] = pix;
					blocklen = 1;
				}
				else
				{
					/*  blocklen<=1, turn into non-run */
					isrun = 0;
					block[blocklen++] = pix;
				}
			}
		}
		else
		{ 
			/* not a run */
			if(pix == block[blocklen-1]) 
			{
				/* case 3: non-run, prev==cur */
				if(blocklen>1) 
				{
					/*  have a non-run block to flush */
					rle[rlen++] = (blocklen-1) | 0x80;
					for(j = 0; j < blocklen; j++)
						rle[rlen++] = block[j];
					/*  start new run block with pix */
					block[0] = pix;
					blocklen = isrun = 1;
				} 
				else
				{
					/*  blocklen<=1 turn into a run */
					isrun = 1;
					block[blocklen++] = pix;
				}
			}
			else 
			{
				/* case 4:  non-run, prev!=cur */
				block[blocklen++] = pix;
			}
		}
	
		/* max block length.  flush */
		if(blocklen == 128)
		{
			if(isrun) 
			{
				rle[rlen++] = blocklen-1;
				rle[rlen++] = block[0];
			}
			else
			{
				rle[rlen++] = (blocklen-1) | 0x80;
				for(j = 0; j < blocklen; j++)
					rle[rlen++] = block[j];
			}
			blocklen = 0;
		}
	}

	/* flush last block */
	if(blocklen)
	{
		if(isrun)
		{
			rle[rlen++] = blocklen-1;
			rle[rlen++] = block[0];
		}
		else
		{
			rle[rlen++] = (blocklen-1) | 0x80;
			for(j = 0; j < blocklen; j++)
				rle[rlen++] = block[j];
		}
	}
	return(rlen);
}

/*****
* Name: 		PShex
* Return Type: 	output a hexadecimal value.
* Description: 
* In: 
*	dpy:		current postscript output area
*	val:		value to be converted
*	flush:		if True, flush out the current buffer.
* Returns:
*	EOF on failure, something else otherwise.
*****/
static int 
PShex(PSDisplay *dpy, Byte val, int flush)
{
	static char digit[] = "0123456789abcdef";

	/* convert to hexadecimal and collect */
	if(!flush) 
	{
		dpy->hexline[dpy->hexi++] = (char) digit[((unsigned) val >>
			(unsigned) 4) & (unsigned) 0x0f];
		dpy->hexline[dpy->hexi++] = (char) digit[(unsigned) val &
			(unsigned) 0x0f];
	}

	/* flush requested or buffer full */
	if((flush && dpy->hexi) || (dpy->hexi > 77)) 
	{
		dpy->hexline[dpy->hexi] = '\0';
		dpy->hexi = 0;
		return(PSprintf(dpy, "%s\n", dpy->hexline));
	}
	return(0);
}

/*****
* Name: 		PSColorImage
* Return Type:	void
* Description:	created postscript colorimage operator 
*				Adds code that checks if the PostScript device in question
*				knows about the 'colorimage' operator.  If it doesn't, it
*				defines 'colorimage' in terms of image (ie, generates a
*				greyscale image from RGB data)
* In: 
*	dpy:		current postscript output area
* Returns:
*	nothing.
*****/
static void 
PSColorImage(PSDisplay *dpy) 
{
    static char *txt[] = {
	"% define 'colorimage' if it isn't defined",
	"%   ('colortogray' and 'mergeprocs' come from xwd2ps",
	"%	 via xgrab)",
	"/colorimage where   % do we know about 'colorimage'?",
	"  { pop }		   % yes: pop off the 'dict' returned",
	"  {				 % no:  define one",
	"	/colortogray {  % define an RGB->I function",
	"	  /rgbdata exch store	% call input 'rgbdata'",
	"	  rgbdata length 3 idiv",
	"	  /npixls exch store",
	"	  /rgbindx 0 store",
	"	  /grays npixls string store  % str to hold the result",
	"	  0 1 npixls 1 sub {",
	"		grays exch",
	"		rgbdata rgbindx	   get 20 mul	% Red",
	"		rgbdata rgbindx 1 add get 32 mul	% Green",
	"		rgbdata rgbindx 2 add get 12 mul	% Blue",
	"		add add 64 idiv	  % I = .5G + .31R + .18B",
	"		put",
	"		/rgbindx rgbindx 3 add store",
	"	  } for",
	"	  grays",
	"	} bind def\n",
	/* Utility procedure for colorimage operator.
	 * This procedure takes two procedures off the
	 * stack and merges them into a single procedure
	 */
	"	/mergeprocs { % def",
	"	  dup length",
	"	  3 -1 roll dup length dup 5 1 roll",
	"	  3 -1 roll add array cvx dup",
	"	  3 -1 roll 0 exch putinterval",
	"	  dup 4 2 roll putinterval",
	"	} bind def\n",
	"	/colorimage { % def",
	/* remove 'false 3' operands */
	"	  pop pop",
	"	  {colortogray} mergeprocs",
	"	  image",
	"	} bind def",
	/* end of 'false' case */
	"  } ifelse"
    };

    PSconst_out(dpy, txt);
}

/*****
* Name:			PSrle_cmapimage
* Return Type: 	void
* Description: 	define rlecmapimage operator
* In: 
*	dpy:		current postscript output area
*	color:		indicates color or mono.
* Returns:
*	nothing.
*****/
static void
PSrle_cmapimage(PSDisplay *dpy, int color) 
{
	/* prolog */
	static char *txt[] = {
		/* rlecmapimage expects to have 'w h bits matrix' on stack */
		"/rlecmapimage {",
		"  /buffer 1 string def",
		"  /rgbval 3 string def",
		"  /block  384 string def",
		"  { currentfile buffer readhexstring pop",
		"	/bcount exch 0 get store",
		"	bcount 128 ge",
		"	{ ",
		"	  0 1 bcount 128 sub",
		"	{ currentfile buffer readhexstring pop pop"
	};

	/* color operator */
	static char *txt_color[] = {
		"		/rgbval cmap buffer 0 get 3 mul 3 getinterval store",
		"		block exch 3 mul rgbval putinterval",
		"	  } for",
		"	  block  0  bcount 127 sub 3 mul  getinterval",
		"	}",
		"	{ ",
		"	  currentfile buffer readhexstring pop pop",
		"	  /rgbval cmap buffer 0 get 3 mul 3 getinterval store",
		"	  0 1 bcount { block exch 3 mul rgbval putinterval } for",
		"	  block 0 bcount 1 add 3 mul getinterval",
		"	} ifelse",
		"  }",
		"  false 3 colorimage",
		"} bind def"
	};

	/* grayscale operator */
	static char *txt_gray[] = {
		"		/rgbval cmap buffer 0 get 1 getinterval store",
		"		block exch rgbval putinterval",
		"	  } for",
		"	  block  0  bcount 127 sub  getinterval",
		"	}",
		"	{ ",
		"	  currentfile buffer readhexstring pop pop",
		"	  /rgbval cmap buffer 0 get 1 getinterval store",
		"	  0 1 bcount { block exch rgbval putinterval } for",
		"	  block 0 bcount 1 add getinterval",
		"	} ifelse",
		"  }",
		"  image",
		"} bind def"
	};

	/* put prolog */
	PSconst_out(dpy, txt);

	if(color) 
		PSconst_out(dpy, txt_color);
	else 
		PSconst_out(dpy, txt_gray);
}

/*****
* Name: 		PSImageBW
* Return Type: 	int
* Description: 	writes out a Black & White image
* In: 
*	dpy:		current postscript output area
*	data:		image data
*	w:			scanline width
*	h:			no of scanlines
*	inverse:	indicates color reversal
* Returns:
*	0 on success, EOF if write failed.
* Note
*	Write the given image array 'pic' (B/W stippled, 1 byte per pixel,
*	0=blk,1=wht) out as hexadecimal, max of 72 hex chars per line.  If
*	inverse is True, then white = 0 and black = 1.
*****/
static int
PSImageBW(PSDisplay *dpy, Byte *data, int w, int h, Boolean inverse)
{
	int	i, j;
	int	err = 0;
	Byte outbyte, bitnum, bit;
    
	outbyte = bitnum = 0;

	/* from left to right, top to bottom */
	for(i = 0; i < h && err != EOF; i++)
	{
		for(j = 0; j < w && err != EOF; j++)
		{
			bit = *(data++);
			outbyte = (outbyte<<1) | ((bit)&0x01);
			bitnum++;
	    
			if(bitnum == 8)
			{
				if(inverse)
					outbyte = ~outbyte & 0xff;
				err = PShex(dpy, outbyte, False);
				outbyte = bitnum = 0;
			}
		}
		if(bitnum)
		{	/*  few bits left over in this row */
			outbyte <<= 8-bitnum;
			if(inverse)
				outbyte = ~outbyte & 0xff;
			err = PShex(dpy, outbyte, False);
			outbyte = bitnum = 0;
		}
	}
	err = PShex(dpy, '\0', True);	/*  Flush the hex buffer if needed */

	return(err);
}

/*****
* Name:			PSImage
* Return Type: 	void
* Description: 	convert image to postscript.
* In: 
*	dpy:		current postscript output area
*	image:		image data
* Returns:
*
*****/
static void 
PSImage(PSDisplay *dpy, XmHTMLImage *image, int x, int y)
{
	XmImageInfo *info = image->html_image;
	Byte *data = info->data;
	int nc = info->ncolors;
	int i, j;
	int w = info->width;
	int h = info->height;
	int slen, colortype, bits;
	int err=0;
	int dest_x = x, dest_y = 0;
	Boolean isanchor = False;
	Boolean colorps = False;

	/* set image name as a comment */
	PSprintf(dpy, "%% PSImage, URL=%s, width = %i, height = %i\n",
		image->url ? image->url : "(unknown)", image->width, image->height);

    /* Isgray returns true if the nth color index is a gray value */
#define Isgray(i,n) (i->reds[n] == i->greens[n] && i->reds[n] == i->blues[n])

    /* Is_bg returns true if the nth color index is the screen background */
#define Is_bg(i,n)  (i->reds[n] == dpy->bg[0] && \
		i->greens[n] == dpy->bg[1] && i->blues[n]== dpy->bg[2])

    /* Is_fg returns true if the nth color index is the screen foreground */
#define Is_fg(i,n)  (i->reds[n] == dpy->fg[0] && \
		i->greens[n] == dpy->fg[1] && i->blues[n] == dpy->fg[2])

	dest_y = -(y - dpy->start_y) - image->height;

	if(data == NULL) 
	{
		/*****
		*  image was not available, draw an empty square instead
		*****/
		PSprintf(dpy, "gsave\n%i %i translate\n%d %d scale\n",
			dest_x, dest_y, w, h);
		PSprintf(dpy, "0.9 setgray SQ fill\n");
		PSprintf(dpy, "grestore\n\n");
		return;
	}

	if(image->owner && image->owner->anchor != NULL &&
		image->owner->anchor->url_type != ANCHOR_NAMED)
		isanchor = True;

	/* draw outline if this is an anchored image */
	if(isanchor) 
	{
		PSprintf(dpy, "gsave\n%i %i translate\n%d %d scale\n",
			dest_x-2, dest_y-2, w+4, h+4);
		PSprintf(dpy, "SQ fill\n");
		PSprintf(dpy, "grestore\n");
	}
	
	/*****
	* This is a hack to see if the image is Black & White, 
	* Greyscale or 8 bit color
	* assume it's bw if it has only one or two colors, both some grey's
	* assume it's greyscale if all the colors (>2) are grey
	* Images with only one color do occur too.
	*****/
    
	if(((nc == 2) 
			&& ((Isgray(info,0) && Isgray(info,1))
			|| (Is_bg(info,0) && Is_fg(info,1)) 
			|| (Is_fg(info,0) && Is_bg(info,1)) ))
		|| ((nc == 1)
			&& (Isgray(info,0) || Is_bg(info,0) || Is_fg(info,0))))
	{
		/* this is a black & white image */
		colortype = F_BWDITHER;
		slen = (w+7)/8;
		bits = 1;
		colorps = False;
	}
	else
	{
		/* assume grayscale unless proven otherwise */
		colortype = F_GREYSCALE;
		slen = w;
		bits = 8;
		colorps = False;
		for(i = 0; i < nc; i++)
		{
			if(!Isgray(info,i))
			{
				/* it's color */
				colortype = F_REDUCED;
				slen = w*3;
				bits = 8;
				colorps = True;
				break;
			}
		}
	}
	
	/*  build a temporary dictionary */
	PSprintf(dpy, "20 dict begin\n\n");

	/*  define string to hold a scanline's worth of data */
	PSprintf(dpy, "/pix %d string def\n\n", slen);

	/*  position and scaling */
	PSprintf(dpy, "gsave\n");
    
	if(colortype == F_BWDITHER) 
	{
		/*  1-bit dither code uses 'image' */
		Boolean inverse = False;
	
		/*  set if color#0 is 'white' */
		if((nc == 2 &&
			MONO(info->reds[0], info->greens[0], info->blues[0]) >
				MONO(info->reds[1], info->greens[1], info->blues[1])) ||
			(nc == 1 && 
				MONO(info->reds[0], info->greens[0],info->blues[0]) >
				MONO(127, 127, 127)))
		{
			inverse = True;
		}
	
		/*  dimensions of data */
		PSprintf(dpy, "%d %d %d\n", w, h, bits);
	
		/*  mapping matrix */
		PSprintf(dpy, "[%d 0 0 %d 0 %d]\n\n", w, -h, h);

		/* Position and scaling */
		PSprintf(dpy, "%i %i translate\n%d %d scale\n", dest_x, dest_y, w, h);
	
		PSprintf(dpy, "{currentfile pix readhexstring pop}\n");
		PSprintf(dpy, "image\n");

		/*  write the actual image data */
		err = PSImageBW(dpy, data, w, h, inverse);
	} 
	else
	{
		/*  all other formats */
		unsigned char *rleline = (unsigned char *) NULL;
		int rlen;
	
		/*  if we're using color, make sure 'colorimage' is defined */
		if(colorps)
			PSColorImage(dpy);
	
		PScolormap(dpy, colorps, nc, info->reds, info->greens, info->blues);
		PSrle_cmapimage(dpy, colorps);

		/*  dimensions of data */
		PSprintf(dpy, "%d %d %d\n", w, h, bits);

		/*  mapping matrix */
		PSprintf(dpy, "[%d 0 0 %d 0 %d]\n", w, -h, h);

		/* Position and scaling */
		PSprintf(dpy, "%i %i translate\n%d %d scale\n", dest_x, dest_y, w, h);

		PSprintf(dpy, "rlecmapimage\n");
	
		rleline = (unsigned char *) malloc(w * 2);
		if(!rleline) 
			return;

		for(i = 0; i < h && err != EOF; i++) 
		{
		    rlen = PSencode(data, rleline, w);
		    data += w;
		    for(j = 0; j < rlen && err != EOF; j++)
				err=PShex(dpy, rleline[j], False);
		    err=PShex(dpy, '\0', True);	/*  Flush the hex buffer */
		}
		free(rleline);
	}
	
	/*  stop using temporary dictionary */
	PSprintf(dpy, "end\n");
	PSprintf(dpy, "grestore\n\n");
	
#undef Isgray
#undef Is_fg
#undef Is_bg
}


/************************************************************************/
/***********************								 ****************/
/*********************** XmHTML ToolkitAbstrion Routines ****************/
/***********************								 ****************/
/************************************************************************/

/*****
* Name:			pstkSetForeground
* Return Type: 	int
* Description: 	sets foreground color
* In: 
*	dpy:		current postscript output area
*	gc:			graphics context, unused
*	foreground:	foreground color to set
* Returns:
*	PSprintf return value (ignored by caller)
*****/
static int
pstkSetForeground(Display *disp, XGC gc, unsigned long foreground)
{
	PSDisplay *dpy = (PSDisplay*)disp;
	XmHTMLWidget html = dpy->html;
	unsigned short red, green, blue;

	XCCGetColor(HTML_ATTR(xcc), foreground, &red, &green, &blue);

	return(PSprintf(dpy, "%u %u %u setrgbcolor\n", red, green, blue));
}

static GC
pstkCreateGC(struct _ToolkitAbstraction* tka, DRAWABLE win, unsigned long valuemask,
	XGCValues *values)
{
	return(NULL);
}

static int
pstkFreeGC(Display *disp, XGC gc)
{
	return(1);
}

static int
pstkCopyGC(Display *disp, XGC src, unsigned long valuemask, XGC dest)
{
	return(1);
}

static int
pstkSetFunction(Display *disp, XGC gc, int function)
{
	return(1);
}

static int
pstkSetClipOriginAndMask(struct _ToolkitAbstraction* tka, XGC gc, 
	int clip_x_origin, int clip_y_origin,
	PIXMAP pixmap)
{
	return(1);
}

static int
pstkSetTile(Display *disp, XGC gc, PIXMAP tile)
{
	return(1);
}

static int
pstkSetTSOrigin(Display *disp, XGC gc, int ts_x_origin, int ts_y_origin)
{
	return(1);
}

static int
pstkSetFillStyle(Display *disp, XGC gc, int fill_style)
{
	return(1);
}

static int
pstkSetLineAttributes(Display *disp, XGC gc, unsigned int line_width,
	int line_style, int cap_style, int join_style)
{
	return(1);
}

static int
pstkSetBackground(Display *disp, XGC gc, unsigned long background)
{
	return(0);
}

/*****
* Name:			pstkSetFont
* Return Type: 	int
* Description: 	sets the requested font
* In: 
*	dpy:		current postscript output area
*	gc:			graphics context, unused
*	font:		font to set
* Returns:
*	always 1 (ignored by caller)
*****/
static int
pstkSetFont(Display *disp, XGC gc, XmHTMLfont *font)
{
	PSDisplay *dpy = (PSDisplay*)disp;
	PSfont(dpy, font, False);
	return(1);
}

/*****
* Name:			pstkTextWidth
* Return Type: 	int
* Description: 	Postscript textwidth function.
* In: 
*	font:		font to be used when computing pixel width;
*	string:		string for which to compute pixel width;
*	count:		no of characters in string.
* Returns:
*	pixel width of given string.
*****/
/*
static int
pstkTextWidth(XmHTMLfont *font, const char* string, int count)
{
}
*/

#if 0
/*****
* Name:			pstkCopyArea
* Return Type: 	int
* Description: 	postscript XCopyArea.
* In: 
*	too many
* Returns:
*	always 1 (ignored by caller)
*****/
static int
pstkCopyArea(Display *disp, DRAWABLE src, DRAWABLE dest, XGC gc, int src_x,
	int src_y, unsigned int width, unsigned int height, int dest_x, int dest_y)
{
	PSDisplay *dpy = (PSDisplay*)disp;

	PSprintf(dpy, "%% pstkCopyArea\n");

	PScheckPage(dpy, dest_x, dest_y);
	ImageToPs(XtDisplay(dpy->html), src, src_x, src_y,
		width, height, dest_x, -(dest_y - dpy->start_y), 1);

	return(1);
}
#endif

/*****
* Name:			pstkDrawString
* Return Type: 	int
* Description: 	renders text
* In: 
*	too many
* Returns:
*	always 1 (ignored by caller)
*****/
static int
pstkDrawString( struct _ToolkitAbstraction* tka, struct _XmHTMLFont *font,
	XGC gc, int x, int y, const char *string, int length)
{
	Display *disp = tka->dpy;
	PSDisplay *dpy = (PSDisplay*)disp;
	static char *last_ep=NULL;
	char *ep = string+strlen(string);

	/*****
	* XmHTML sends preformatted(<PRE>) text as multiple words within 'str'.
	* Then makes successive calls to this routine adjusting the pointer to
	* the start of the next word in the same string. The problem is, what
	* XmHTML (actually the X server's adobe fonts) thinks  is the width of a
	* space is not what my postscript printer thinks it is. So, until XmHTML
	* changes how it draws <PRE> text, or until I can figure a better way,
	* we'll just ignore successive calls on the rest of the string.
	* -- scott
	*
	* The only correct way would be to add full postscript font handling:
	* one of the properties contained in the XmHTMLfont structure is the
	* width of a single space. I guess we need a real postscript wizard
	* for this as I haven't got *any* clue on how postscript fonts are defined
	* let alone what properties it contains. -- kdh
	*****/
	if(last_ep && last_ep==ep)
		return(1);
	last_ep = ep;
	PSmoveto(dpy, x, y);
	PSfont(dpy, font, False);
	PStext(dpy, string, False);

	return(1);
}

/*****
* Name:			pstkDrawAnchorData
* Return Type: 	void
* Description: 
* In: 
*
* Returns:
*	nothing
*****/
static void
pstkDrawAnchorData(Display *disp, WINDOW win, XGC gc, int x, int y,
	XmHTMLObjectTableElement data)
{
	PSDisplay *dpy = (PSDisplay*)disp;
	int fnum, offset;
	static char *last_href;

	/*****
	* Check if this is a real anchor
	* We don't footnote internal anchors. seems sane to me.
	* might want to exclude other types as well. -- scott
	*****/
	if(!data->anchor || !data->anchor->href || data->anchor->href[0]=='\0' ||
		data->anchor->href[0] == '#' || !dpy->font)
		return;

	if(last_href == data->anchor->href)
		return;
	last_href = data->anchor->href;

	if(y > dpy->start_y + dpy->Pixels_This_Page)
		return;	/* won't fit on page! */

	/* add a footnote marker */
	PSprintf(dpy, "%d %d M\n", x, -(y - dpy->start_y));
	offset = dpy->font_size - 6; /* 6 point font for footnote number */
	fnum = fnAdd(dpy->footnotes, data->anchor->href);
	PSprintf(dpy, "/helvetica 6 SF\n");
	PSprintf(dpy, "2 %d R\n(%d)S\n", offset, fnum+1);
	PSprintf(dpy, "%s %d SF\n", dpy->font_style, dpy->font_size);

	/* reserve room for this footnote */
	dpy->Pixels_This_Page -= FOOTNOTE_ROW_HEIGHT+2;
}

/*****
* Name:			pstkDrawLine
* Return Type: 	int
* Description: 	renders a single line
* In: 
*	too many
* Returns:
*	always 1 (ignored by caller)
*****/
static int
pstkDrawLine(Display *disp, WINDOW win, XGC gc, int x1, int y1, int x2, int y2)
{
	PSDisplay *dpy = (PSDisplay*)disp;
	int yd = y2 - y1;
	int xd = x2 - x1;

	PSprintf(dpy, "%% pstkDrawLine (%d, %d) (%d, %d)\n", x1, y1, x2, y2);
	PSmoveto(dpy, x1, y1);
	PSprintf(dpy, "%d %d RL stroke\n", xd, yd);

	return(1);
}

/*****
* Name:			pstkDrawLines
* Return Type: 	void
* Description: 	draws a collection of lines
* In: 
*	too many
* Returns:
*	always 1 (ignored by caller)
*****/
static int
pstkDrawLines(Display *disp, DRAWABLE win, XGC gc, XPoint *points,
	int num_points, int mode)
{
	PSDisplay *dpy = (PSDisplay*)disp;
	int i;

	PSprintf(dpy, "%% pstkDrawLines\n");
	for(i = 0; i < num_points - 1; ++i)
	{
		pstkDrawLine(disp, win, gc,
			points[i].x, points[i].y, points[i+1].x, points[i+1].y);
	}
	return(1);
}

/*****
* Name:			pstkDrawRectangle
* Return Type: 	int
* Description: 	renders a plain rectangle
* In: 
*	too many
* Returns:
*	always 1 (ignored by caller)
*****/
static int
pstkDrawRectangle(Display *disp, WINDOW win, XGC gc, int x, int y,
	unsigned int width, unsigned int height)
{
	PSDisplay *dpy = (PSDisplay*)disp;
	int ny = y + height;

	PSprintf(dpy, "%% pstkDrawRectangle\n");
	PScheckPage(dpy, x, ny);
	PSprintf(dpy, "newpath %d %d M %u 0 RL 0 -%u RL -%u 0 RL 0 %u RL "
		"closepath stroke\n", x, -(y - dpy->start_y),
		width, height, width, height);

	return(1);
}

/*****
* Name:			pstkFillRectangle
* Return Type: 	int
* Description: 	renders a filled rectangle.
* In: 
*	too many
* Returns:
*	always 1 (ignored by caller)
*****/
static int
pstkFillRectangle(Display *disp, WINDOW win, XGC gc, int x, int y,
	unsigned int width, unsigned int height)
{
	PSDisplay *dpy = (PSDisplay*)disp;
	int ny = y + height;

	PSprintf(dpy, "%% pstkFillRectangle\n");
	PScheckPage(dpy, x, ny);
	PSprintf(dpy, "newpath %d %d M %d 0 RL 0 -%d RL -%d 0 RL 0 %d RL "
		"closepath fill stroke\n", x, -(y - dpy->start_y),
		width, height, width, height);

	return(1);
}

/*****
* Name:			pstkDrawShadows
* Return Type: 	void
* Description: 	draws a shadow rectangle
* In: 
*	too many
* Returns:
*	nothing
* Note:
*	Uses setgray instead of setrgbcolor for the shadow colors. This ensures
*	that shadows will always be drawn.
*****/
static void
pstkDrawShadows(Display *disp, DRAWABLE drawable, 
	XGC top_shadow_GC, XGC bottom_shadow_GC,
#if NeedWidePrototypes
	int x, int y, int width, int height, int shadow_thickness,
#else
	Position x, Position y, Dimension width, Dimension height,
	Dimension shadow_thickness,
#endif
	unsigned int shadow_type)
{
	PSDisplay *dpy = (PSDisplay*)disp;
	int ts=8, bs=4;

	switch(shadow_type)
	{
		case XmSHADOW_IN:
			/* top & left border */
			PSprintf(dpy, ".%d setgray\n", bs);
			pstkFillRectangle(disp, drawable, bottom_shadow_GC, x, y,
				width, 1);
			pstkFillRectangle(disp, drawable, bottom_shadow_GC, x, y,
				1, height-1);

			/* bottom & right border */
			PSprintf(dpy, ".%d setgray\n", ts);
			pstkFillRectangle(disp, drawable, top_shadow_GC, x + 1,
				y + height - 1, width - 1, 1);
			pstkFillRectangle(disp, drawable, top_shadow_GC, x - 1, y + 1, 1,
				height - 2);
			break;
		case XmSHADOW_OUT:
			/* top & left border */
			PSprintf(dpy, ".%d setgray\n", ts);
			pstkFillRectangle(disp, drawable, top_shadow_GC, x, y, width, 1);
			pstkFillRectangle(disp, drawable, top_shadow_GC, x, y, 1, height-1);

			/* bottom & right border */
			PSprintf(dpy, ".%d setgray\n", bs);
			pstkFillRectangle(disp, drawable, bottom_shadow_GC, x + 1,
				y + height - 1, width - 1, 1);
			pstkFillRectangle(disp, drawable, bottom_shadow_GC, x - 1,
				y + 1, 1, height - 2);
			break;
		default:
			break;
	}
	PSprintf(dpy, "1 setgray\n");
}

/*****
* Name:			pstkDrawArc
* Return Type: 	int
* Description: 	renders an (unfilled) arc.
* In: 
*	too many
* Returns:
*	always 1 (ignored by caller)
*****/
static int
pstkDrawArc(Display *disp, WINDOW win, XGC gc, int x, int y,
	unsigned int width, unsigned int height, int angle1, int angle2)
{
	int ny = y+height;
	int x0, y0;		/* center */
	int r;			/* radius */
	PSDisplay *dpy = (PSDisplay*)disp;

	PSprintf(dpy, "%% pstkDrawArc (%ux%u+%d+%d)\n", width, height, x, y);
	PScheckPage(dpy, x, ny);

	r = height/2;
	x0 = x + r;
	y0 = -(y - dpy->start_y + r);

	PSprintf(dpy, "newpath %d %d M %d %d %d %d %d arc closepath\n",
		x0, y0, x, y, r, angle1, angle2);

	return(1);
}

/*****
* Name:			pstkFillArc
* Return Type: 	int
* Description: 	renders a filled arc.
* In: 
*	too many args
* Returns:
*	always 1 (ignored by caller)
*****/
static int
pstkFillArc(Display *disp, WINDOW win, XGC gc, int x, int y,
	unsigned int width, unsigned int height, int angle1, int angle2)
{
	int ny = y+height;
	int x0, y0;		/* center */
	int r;			/* radius */
	PSDisplay *dpy = (PSDisplay*)disp;

	PSprintf(dpy, "%% pstkFillArc (%ux%u+%d+%d) %d\n", width, height, x, y,
		dpy->start_y);

	PScheckPage(dpy, x, ny);

	r = height/2;
	x0 = x + r;
	y0 = -(y - dpy->start_y + r);

	PSprintf(dpy, "newpath %d %d M %d %d %d %d %d arc fill closepath\n",
		x0, y0, x0, y0, r, angle1, angle2);

	return(1);
}

static void
pstkDrawImage(XmHTMLWidget html, XmHTMLImage *image, XGC gc,
	int src_x, int src_y, unsigned int width, unsigned int height,
	int dest_x, int dest_y)
{
	ToolkitAbstraction *tka = HTML_ATTR(tka);
	PSDisplay *dpy = (PSDisplay*)tka->dpy;

	PSImage(dpy, image, dest_x, dest_y);
}

/*****
* Name:			_CreatePostscriptTka
* Return Type: 	ToolkitAbstraction
* Description: 	Creates the tka required for Postscript output
* In: 
*	html:		current XmHTMLWidget id
* Returns:
*	A new tka, based upon the current tka as found in the widget
*****/
static ToolkitAbstraction*
_CreatePostscriptTka(XmHTMLWidget html)
{
	static ToolkitAbstraction *tka;

	/* copy current tka and override the necessary functions */
	tka = XmHTMLTkaCopy(HTML_ATTR(tka));

	/* GC functions */
	tka->CreateGC      = pstkCreateGC;
	tka->FreeGC        = pstkFreeGC;
	tka->CopyGC        = pstkCopyGC;
	tka->SetFunction   = pstkSetFunction;
	tka->SetClipOriginAndMask   = pstkSetClipOriginAndMask;
	tka->SetTile       = pstkSetTile;
	tka->SetTSOrigin   = pstkSetTSOrigin;
	tka->SetFillStyle  = pstkSetFillStyle;
	tka->SetFont       = pstkSetFont;
	tka->SetForeground = pstkSetForeground;
	tka->SetBackground = pstkSetBackground;
	tka->SetLineAttributes = pstkSetLineAttributes;

	/* Font Allocation functions are not used by postscript output */

	/* Cursor & pointer functions are not used by postscript output */

	/* Color functions are not used by postscript output */

	/* Pixmap functions are not used by postscript output */

	/* XImage functions are not used by postscript output */

	/* misc. render functions */
	tka->DrawImage     = pstkDrawImage;
	tka->DrawAnchorData= pstkDrawAnchorData;

	/* string/text functions are not used by postscript output */

	/* Render functions */
	tka->DrawString     = pstkDrawString;
	tka->DrawLine       = pstkDrawLine;
	tka->DrawLines      = pstkDrawLines;
	tka->DrawRectangle  = pstkDrawRectangle;
	tka->FillRectangle  = pstkFillRectangle;
	tka->DrawArc        = pstkDrawArc;
	tka->FillArc        = pstkFillArc;

	/* misc. functions are not used by postscript output */

	/* X Intrinsic wrappers are not used by postscript output */

	/* Motif Wrappers */
	tka->DrawShadows    = pstkDrawShadows;

	return(tka);
}

/*****
* Name:			_XmHTMLTextGetPS
* Return Type:	String
* Description:	Formats the current HTML into postscript output.
* In:
*	html:		XmHTML Widget id;
*	pdef:		defines the paper size;
*	start/end:	ignored until XmHTML can do selections;
*   options:	An OR of:
*				  XmHTMLTEXT_ADDFOOTER - prints the page number.
*				  XmHTMLTEXT_ANCHORFOOTNOTES - footnotes each anchor on the
*					 the page and provides their HREF's at the bottom.
* Returns:
*	A malloc'd buffer containing postscript output. Calling routine must
*   free the returned buffer.
*****/	
String
_XmHTMLTextGetPS(XmHTMLWidget html, XmHTMLPaperSize *pdef,
	XmHTMLObjectTableElement start, XmHTMLObjectTableElement end,
	Byte options)
{
	ToolkitAbstraction *tka_orig, *tka;
	XmHTMLObjectTableElement pstart, pend;
	int pagewidth;
	Dimension paint_w, paint_h, margin_w, margin_h, work_w;
	Position paint_x, paint_y, scroll_x, scroll_y;
	Boolean anchorb;
	String title;
	PSDisplay *dpy;
	unsigned short red, green, blue;
	String psbuf;

	/*****
	* Sanity check, Postscript output requires a papersize definition 
	* in points.
	*****/
	if(pdef->unit_type != XmHTML_POINT)
	{
		_XmHTMLWarning(__WFUNC__(html, "_XmHTMLTextGetPS"),
			XMHTML_MSG_88, "POINT");
		return(NULL);
	}

	/* Create a Postscript output area */
	dpy = (PSDisplay *)calloc(1, sizeof(PSDisplay));

	/* initialize the output area */
	dpy->html = html;
	dpy->options = options;

	/* copy paper definition into output screen */
	memcpy((void*)&dpy->screen, pdef, sizeof(XmHTMLPaperSize));

	/*****
	* Postscript top margin seems to behave somewhat differently...
	*
	* Scott, I replaced 11*72 (which I assume is the height of Letter in
	* inches) by the height of the provided papersize (which is already
	* in points). -- kdh.
	*****/
	dpy->screen.top_margin = (pdef->height - pdef->top_margin);
	dpy->screen.height = (dpy->screen.top_margin - dpy->screen.bottom_margin);

	/*****
	* Calculate the number of Postscript points per pixel of current screen,
	* and the height of the page in pixels (used in figuring when we've hit
	* the bottom of the page) and for computing correct text widths.
	*****/
	dpy->Points_Pixel = 72.0 / GetDpi(html);

	pagewidth = pdef->width;

	/*****
	* Reduce the scaling if the width used for formatting is greater than
	* 8 * 72 pixels (8 inch). In theory, this is not what you want for A4
	* paper (only 8.27 inch wide), but I guess that the hw->html.doc_width
	* includes some left and right margins, so it seems to work in practice.
	*
	* Scott, does this test work? Above you set pagewidth to pdef->width
	* and upon entry of this routine, screen.width was set to pdef->width?
	*****/
	if(pagewidth > PAGE_WIDTH)
		dpy->Points_Pixel = dpy->Points_Pixel * (float)PAGE_WIDTH/pagewidth;

	dpy->Pixels_This_Page = (int)(PAGE_HEIGHT/dpy->Points_Pixel);
	dpy->Pixels_Page = dpy->Pixels_This_Page;

	/*****
	* Get the foreground and background colors so we can check later
	* for black&white documents
	*****/
	XCCGetColor(HTML_ATTR(xcc), HTML_ATTR(body_fg), &red, &green, &blue);
	dpy->fg[0] = red;
	dpy->fg[1] = green;
	dpy->fg[2] = blue;
	XCCGetColor(HTML_ATTR(xcc), HTML_ATTR(body_bg), &red, &green, &blue);
	dpy->bg[0] = red;
	dpy->bg[1] = green;
	dpy->bg[2] = blue;

	fnDestroy(dpy->footnotes); /* clear footnotes */

	PSinit(dpy);

	title = XmHTMLGetTitle((Widget)html);

	PSheader(dpy, title ? title : "", 0);

	PSnewpage(dpy);

	/* save all settings that will get altered */
	scroll_x = HTML_ATTR(scroll_x);
	scroll_y = HTML_ATTR(scroll_y);
	paint_y  = HTML_ATTR(paint_y);
	paint_h  = HTML_ATTR(paint_height);
	paint_x  = HTML_ATTR(paint_x);
	paint_w  = HTML_ATTR(paint_width);
	pstart   = HTML_ATTR(paint_start);
	pend	 = HTML_ATTR(paint_end);
	margin_w = HTML_ATTR(margin_width);
	margin_h = HTML_ATTR(margin_height);
	work_w   = HTML_ATTR(work_width);
	anchorb  = HTML_ATTR(anchor_buttons);

	/* reset and set paper properties */
	HTML_ATTR(scroll_x) = 0;
	HTML_ATTR(scroll_y) = 0;
	HTML_ATTR(paint_y) = 0;
	HTML_ATTR(paint_x) = 0;
	HTML_ATTR(paint_width) = pdef->width;
	HTML_ATTR(paint_height) = pdef->height - pdef->bottom_margin;
	HTML_ATTR(paint_start)  = NULL;
	HTML_ATTR(paint_end)  = NULL;
	HTML_ATTR(margin_width) = pdef->left_margin;
	HTML_ATTR(margin_height) = pdef->top_margin;
	HTML_ATTR(work_width) = pdef->width - pdef->left_margin;
	HTML_ATTR(anchor_buttons) = False;		/* no button anchors, looks ugly */

	dpy->start_y = 0;

	/* Recompute layout for new paper definition before we set tka */
	_XmHTMLComputeLayout(html);

	HTML_ATTR(paint_height) = max(HTML_ATTR(paint_height),
		HTML_ATTR(formatted_height));

	/* temporarily set the ToolkitAbstractions for postscript */

	/* save current tka */
	tka_orig = HTML_ATTR(tka);
	tka = _CreatePostscriptTka(html);
	HTML_ATTR(tka) = tka;

	/* save original display pointer */
	dpy->dpy = tka->dpy;

	/* store new one */
	tka->dpy = (Display*)dpy;
	tka->win = tka_orig->win;

	/* render as postscript */
	_XmHTMLPaint(html, HTML_ATTR(formatted), NULL);

	PSshowpage(dpy);
	PStrailer(dpy);

	/* All done. Get return buffer */
	psbuf = dpy->string;

	/* all done, release dpy */
	fnDestroy(dpy->footnotes);
	free(dpy);
	XmHTMLTkaDestroy(tka);

	HTML_ATTR(tka) = tka_orig;

	/* reset everything */
	HTML_ATTR(scroll_x) = scroll_x;
	HTML_ATTR(scroll_y) = scroll_y;
	HTML_ATTR(paint_y) = paint_y;
	HTML_ATTR(paint_x) = paint_x;
	HTML_ATTR(paint_width) = paint_w;
	HTML_ATTR(paint_height) = paint_h;
	HTML_ATTR(paint_start)  = pstart;
	HTML_ATTR(paint_end)  = pend;
	HTML_ATTR(margin_width) = margin_w;
	HTML_ATTR(margin_height) = margin_h;
	HTML_ATTR(work_width) = work_w;
	HTML_ATTR(anchor_buttons) = anchorb;

	/* Redisplay to restore everything correctly */
	XmHTMLRedisplay((Widget)html);

	return(psbuf);
}