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
|
/*========================================================================*\
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:
This module is based on prior work as noted below.
\*========================================================================*/
/*
* Support drawing routines for TeXsun and TeX
*
* Copyright, (C) 1987, 1988 Tim Morgan, UC Irvine
* Adapted for xdvi by Jeffrey Lee, U. of Toronto
*
* At the time these routines are called, the values of hh and vv should
* have been updated to the upper left corner of the graph (the position
* the \special appears at in the dvi file). Then the coordinates in the
* graphics commands are in terms of a virtual page with axes oriented the
* same as the Imagen and the SUN normally have:
*
* 0,0
* +-----------> +x
* |
* |
* |
* \ /
* +y
*
* Angles are measured in the conventional way, from +x towards +y.
* Unfortunately, that reverses the meaning of "counterclockwise"
* from what it's normally thought of.
*
* A lot of floating point arithmetic has been converted to integer
* arithmetic for speed. In some places, this is kind-of kludgy, but
* it's worth it.
*/
#ifdef WIN32
#define WIN32_LEAN_AND_MEAN 1
#include "wingui.h"
#endif
#undef HAVE_BOOLEAN
#include "xdvi-config.h"
#include <kpathsea/c-fopen.h>
#include <kpathsea/c-ctype.h>
#include <kpathsea/c-pathmx.h>
#include <kpathsea/c-stat.h>
#include <kpathsea/line.h>
#include <kpathsea/tex-file.h>
#ifndef S_IRUSR
#define S_IRUSR 0400
#endif
#ifndef S_IWUSR
#define S_IWUSR 0200
#endif
#include "dvi.h" // added SU
#ifdef WIN32
#include <zlib.h>
#include <bzlib.h>
#else
# if HAVE_SYS_WAIT_H
# include <sys/wait.h>
# endif
# ifndef WEXITSTATUS
# define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
# endif
# ifndef WIFEXITED
# define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
# endif
extern char *tempnam ARGS((_Xconst char *, _Xconst char *));
extern double floor ARGS((double));
#endif
#define MAXPOINTS 300 /* Max points in a path */
#define MAX_PEN_SIZE 72 /* Max pixels of pen width */
#define TWOPI (3.14159265359 * 2.0)
#define rint(x) floor((x) + 0.5)
static double shadetp = 0.5;
/* shading level, initialized as requested by tpic 2.0 -- MJ */
static int xx[MAXPOINTS], yy[MAXPOINTS]; /* Path in milli-inches */
static int path_len = 0; /* # points in current path */
/* static */ int pen_size = 1; /* Pixel width of lines drawn */
static Boolean whiten = False;
static Boolean shade = False;
static Boolean blacken = False;
static Boolean begin_path = False;
/* Unfortunately, these values also appear in dvisun.c */
#define xRESOLUTION (pixels_per_inch/shrink_factor)
#define yRESOLUTION (pixels_per_inch/shrink_factor)
# if HAVE_VFORK_H
# include <vfork.h>
# endif
#define CMD(x, y) ((x) << 8 | (y))
/*
* Issue warning messages
*/
static void
Warning(fmt, msg)
char *fmt, *msg;
{
Fprintf(stderr, "%s: ", prog);
Fprintf(stderr, fmt, msg);
(void) fputc('\n', stderr);
}
/*
* X drawing routines
*/
#define toint(x) ((int) ((x) + 0.5))
#define xconv(x) (toint(tpic_conv*(x))/shrink_factor + PXL_H)
#define yconv(y) (toint(tpic_conv*(y))/shrink_factor + PXL_V)
/*
* Draw a line from (fx,fy) to (tx,ty).
* Right now, we ignore pen_size.
*/
static void
line_btw(fx, fy, tx, ty)
int fx, fy, tx, ty;
{
int fcx = xconv(fx),
tcx = xconv(tx),
fcy = yconv(fy),
tcy = yconv(ty);
HPEN oldPen;
oldPen = SelectObject(ruleGC, foreTPicPen);
if ((fcx < max_x || tcx < max_x) && (fcx >= min_x || tcx >= min_x) &&
(fcy < max_y || tcy < max_y) && (fcy >= min_y || tcy >= min_y))
XDrawLine(DISP, currwin.win, ruleGC,
fcx - currwin.base_x, fcy - currwin.base_y,
tcx - currwin.base_x, tcy - currwin.base_y);
SelectObject(ruleGC, oldPen);
}
/*
* Draw a dot at (x,y)
*/
static void
dot_at(x, y)
int x, y;
{
int cx = xconv(x),
cy = yconv(y);
HPEN oldPen;
oldPen = SelectObject(ruleGC, foreTPicPen);
if (cx < max_x && cx >= min_x && cy < max_y && cy >= min_y)
XDrawPoint(DISP, currwin.win, ruleGC,
cx - currwin.base_x, cy - currwin.base_y);
SelectObject(ruleGC, oldPen);
}
/*
* Apply the requested attributes to the last path (box) drawn.
* Attributes are reset.
* (Not currently implemented.)
*/
/* ARGSUSED */
static void
do_attribute_path(last_min_x, last_max_x, last_min_y, last_max_y)
int last_min_x, last_max_x, last_min_y, last_max_y;
{
HPEN oldPen;
HBRUSH oldBrush;
if (!begin_path) {
Warning("%s", "Tpic specials: no Beginpath encountered!\n");
return;
}
begin_path = False;
#if DEBUG_TPIC
fprintf(stderr, "Flushing path\n");
#endif
if (EndPath(ruleGC) == 0) {
Win32Error("do_attribute_path/EndPath");
return;
}
oldPen = SelectObject(ruleGC, foreTPicPen);
oldBrush = SelectObject(ruleGC, foreTPicBrush);
if (blacken || whiten || shade) {
#if DEBUG_TPIC
fprintf(stderr, "Filling ...\n");
#endif
blacken = whiten = shade = False;
if (StrokeAndFillPath(ruleGC) == 0) {
Win32Error("do_attribute_path/StrokeAndFillPath");
}
}
else {
#if DEBUG_TPIC
fprintf(stderr, "Empty ...\n");
#endif
if (StrokePath(ruleGC) == 0) {
Win32Error("do_attribute_path/StrokePath");
}
}
SelectObject(ruleGC, oldPen);
SelectObject(ruleGC, oldBrush);
}
/*
* Set the size of the virtual pen used to draw in milli-inches
*/
/* ARGSUSED */
static void
set_pen_size(cp)
char *cp;
{
int ps;
HPEN newForePen, newBackPen;
if (sscanf(cp, " %d ", &ps) != 1) {
Warning("invalid .ps command format: %s", cp);
return;
}
pen_size = ((float)ps * ((float)pixels_per_inch / 1000.0) + 0.5) / shrink_factor;
#if DEBUG_TPIC
fprintf(stderr, "Pen size is %d\n", pen_size);
#endif
if (pen_size < 1) pen_size = 1;
else if (pen_size > MAX_PEN_SIZE) pen_size = MAX_PEN_SIZE;
if (foreTPicPen)
if (!DeleteObject(foreTPicPen))
Win32Error("set_pen_size/DeleteObject/foreTPicPen");
foreTPicPen = CreatePen(PS_SOLID, pen_size, fore_Pixel);
if (backTPicPen)
if (!DeleteObject(backTPicPen))
Win32Error("set_pen_size/DeleteObject/backTPicPen");
backTPicPen = CreatePen(PS_NULL, pen_size, back_Pixel);
#if DEBUG_TPIC
fprintf(stderr, "Pen size set to %d points\n", pen_size);
#endif
}
/*
* Print the line defined by previous path commands
*/
static void
flush_path()
{
int i;
int last_min_x, last_max_x, last_min_y, last_max_y;
#if 0
HPEN oldPen;
HBRUSH oldBrush;
oldPen = SelectObject(ruleGC, foreTPicPen);
oldBrush = SelectObject(ruleGC, foreTPicBrush);
#endif
last_min_x = 30000;
last_min_y = 30000;
last_max_x = -30000;
last_max_y = -30000;
for (i = 1; i < path_len; i++) {
if (xx[i] > last_max_x) last_max_x = xx[i];
if (xx[i] < last_min_x) last_min_x = xx[i];
if (yy[i] > last_max_y) last_max_y = yy[i];
if (yy[i] < last_min_y) last_min_y = yy[i];
line_btw(xx[i], yy[i], xx[i+1], yy[i+1]);
}
if (xx[path_len] > last_max_x) last_max_x = xx[path_len];
if (xx[path_len] < last_min_x) last_min_x = xx[path_len];
if (yy[path_len] > last_max_y) last_max_y = yy[path_len];
if (yy[path_len] < last_min_y) last_min_y = yy[path_len];
path_len = 0;
do_attribute_path(last_min_x, last_max_x, last_min_y, last_max_y);
#if 0
SelectObject(ruleGC, oldPen);
SelectObject(ruleGC, oldBrush);
#endif
}
/*
* Print a dashed line along the previously defined path, with
* the dashes/inch defined.
*/
static void
flush_dashed(cp, dotted)
char *cp;
Boolean dotted;
{
int i, k;
int numdots;
int lx0, ly0, lx1, ly1;
int cx0, cy0, cx1, cy1;
float inchesperdash;
double d, spacesize, a, b, dx, dy, milliperdash;
int last_min_x, last_max_x, last_min_y, last_max_y;
HPEN oldPen;
if (sscanf(cp, " %f ", &inchesperdash) != 1) {
Warning("invalid format for dotted/dashed line: %s", cp);
return;
}
if (path_len <= 1 || inchesperdash <= 0.0) {
Warning("invalid conditions for dotted/dashed line", "");
return;
}
if (begin_path) {
begin_path = False;
EndPath(ruleGC);
}
oldPen = SelectObject(ruleGC, backTPicPen);
if (whiten || blacken || shade) {
/* FIXME : add checks to win32 calls ! */
BeginPath(ruleGC);
for (i = 1; i < path_len; i++) {
line_btw(xx[i], yy[i], xx[i+1], yy[i+1]);
}
EndPath(ruleGC);
FillPath(ruleGC);
whiten = blacken = shade = False;
}
SelectObject(ruleGC, foreTPicPen);
milliperdash = inchesperdash * 1000.0;
for (k = 1; k < path_len; k++) {
lx0 = xx[k]; ly0 = yy[k];
lx1 = xx[k+1];ly1 = yy[k+1];
dx = lx1 - lx0;
dy = ly1 - ly0;
if (dotted) {
numdots = sqrt(dx*dx + dy*dy) / milliperdash + 0.5;
if (numdots == 0) numdots = 1;
for (i = 0; i <= numdots; i++) {
a = (float) i / (float) numdots;
cx0 = lx0 + a * dx + 0.5;
cy0 = ly0 + a * dy + 0.5;
dot_at(cx0, cy0);
}
}
else {
d = sqrt(dx*dx + dy*dy);
numdots = d / (2.0 * milliperdash) + 1.0;
if (numdots <= 1)
line_btw(lx0, ly0, lx1, ly1);
else {
spacesize = (d - numdots * milliperdash) / (numdots - 1);
for (i = 0; i < numdots - 1; i++) {
a = i * (milliperdash + spacesize) / d;
b = a + milliperdash / d;
cx0 = lx0 + a * dx + 0.5;
cy0 = ly0 + a * dy + 0.5;
cx1 = lx0 + b * dx + 0.5;
cy1 = ly0 + b * dy + 0.5;
line_btw(cx0, cy0, cx1, cy1);
b += spacesize / d;
}
cx0 = lx0 + b * dx + 0.5;
cy0 = ly0 + b * dy + 0.5;
line_btw(cx0, cy0, lx1, ly1);
}
}
}
path_len = 0;
SelectObject(ruleGC, oldPen);
}
/*
* Add a point to the current path
*/
static void
add_path(cp)
char *cp;
{
int pathx, pathy;
HPEN oldPen;
#if 0
oldPen = SelectObject(ruleGC, foreTPicPen);
#endif
if (path_len == 0 && !begin_path) {
BeginPath(ruleGC);
begin_path = True;
}
if (++path_len >= MAXPOINTS) oops("Too many points");
if (sscanf(cp, " %d %d ", &pathx, &pathy) != 2)
oops("Malformed path command");
xx[path_len] = pathx;
yy[path_len] = pathy;
#if 0
SelectObject(ruleGC, oldPen);
#endif
}
/*
* Draw to a floating point position
*/
static void
im_fdraw(x, y)
double x,y;
{
if (++path_len >= MAXPOINTS) oops("Too many arc points");
xx[path_len] = x + 0.5;
yy[path_len] = y + 0.5;
}
/*
* Draw an ellipse with the indicated center and radices.
*/
static void
draw_ellipse(xc, yc, xr, yr)
int xc, yc, xr, yr;
{
double angle, theta;
int n;
int px0, py0, px1, py1;
HPEN oldPen;
oldPen = SelectObject(ruleGC, foreTPicPen);
angle = (xr + yr) / 2.0;
theta = sqrt(1.0 / angle);
n = TWOPI / theta + 0.5;
if (n < 12) n = 12;
else if (n > 80) n = 80;
n /= 2;
theta = TWOPI / n;
angle = 0.0;
px0 = xc + xr; /* cos(0) = 1 */
py0 = yc; /* sin(0) = 0 */
while ((angle += theta) <= TWOPI) {
px1 = xc + xr*cos(angle) + 0.5;
py1 = yc + yr*sin(angle) + 0.5;
line_btw(px0, py0, px1, py1);
px0 = px1;
py0 = py1;
}
line_btw(px0, py0, xc + xr, yc);
flush_path();
SelectObject(ruleGC, oldPen);
}
/*
* Draw an arc
*/
static void
arc(cp, invis)
char *cp;
Boolean invis;
{
int xc, yc, xrad, yrad, n;
float start_angle, end_angle, angle, theta, r;
double xradius, yradius, xcenter, ycenter;
HPEN oldPen;
HBRUSH oldBrush;
if (sscanf(cp, " %d %d %d %d %f %f ", &xc, &yc, &xrad, &yrad,
&start_angle, &end_angle) != 6) {
Warning("invalid arc specification: %s", cp);
return;
}
/* FIXME: the interior might need to be filled in */
if (invis) return;
oldPen = SelectObject(ruleGC, foreTPicPen);
oldBrush = SelectObject(ruleGC, foreTPicBrush);
if (!begin_path) {
BeginPath(ruleGC);
begin_path = True;
}
/* We have a specialized fast way to draw closed circles/ellipses */
if (start_angle <= 0.0 && end_angle >= 6.282) {
draw_ellipse(xc, yc, xrad, yrad);
return;
}
xcenter = xc;
ycenter = yc;
xradius = xrad;
yradius = yrad;
r = (xradius + yradius) / 2.0;
theta = sqrt(1.0 / r);
n = 0.3 * TWOPI / theta + 0.5;
if (n < 12) n = 12;
else if (n > 80) n = 80;
n /= 2;
theta = TWOPI / n;
flush_path();
if (!begin_path) {
BeginPath(ruleGC);
begin_path = True;
}
im_fdraw(xcenter + xradius * cos(start_angle),
ycenter + yradius * sin(start_angle));
angle = start_angle + theta;
if (end_angle < start_angle) end_angle += TWOPI;
while (angle < end_angle) {
im_fdraw(xcenter + xradius * cos(angle),
ycenter + yradius * sin(angle));
angle += theta;
}
im_fdraw(xcenter + xradius * cos(end_angle),
ycenter + yradius * sin(end_angle));
flush_path();
SelectObject(ruleGC, oldPen);
SelectObject(ruleGC, oldBrush);
}
/*
* APPROXIMATE integer distance between two points
*/
#define dist(x0, y0, x1, y1) (abs(x0 - x1) + abs(y0 - y1))
/*
* Draw a spline along the previously defined path
*/
static void
flush_spline(char *cp)
{
int xp, yp;
int N;
int lastx, lasty;
Boolean lastvalid = False;
int t1, t2, t3;
int steps;
int j;
int i, w;
int last_min_x, last_max_x, last_min_y, last_max_y;
HPEN oldPen;
HBRUSH oldBrush;
oldPen = SelectObject(ruleGC, foreTPicPen);
oldBrush = SelectObject(ruleGC, foreTPicBrush);
#ifdef lint
lastx = lasty = -1;
#endif
last_min_x = 30000;
last_min_y = 30000;
last_max_x = -30000;
last_max_y = -30000;
for (i = 1; i <= path_len; i++) {
if (xx[i] > last_max_x) last_max_x = xx[i];
if (xx[i] < last_min_x) last_min_x = xx[i];
if (yy[i] > last_max_y) last_max_y = yy[i];
if (yy[i] < last_min_y) last_min_y = yy[i];
}
N = path_len + 1;
xx[0] = xx[1];
yy[0] = yy[1];
xx[N] = xx[N-1];
yy[N] = yy[N-1];
for (i = 0; i < N - 1; i++) { /* interval */
steps = (dist(xx[i], yy[i], xx[i+1], yy[i+1]) +
dist(xx[i+1], yy[i+1], xx[i+2], yy[i+2])) / 80;
for (j = 0; j < steps; j++) { /* points within */
w = (j * 1000 + 500) / steps;
t1 = w * w / 20;
w -= 500;
t2 = (750000 - w * w) / 10;
w -= 500;
t3 = w * w / 20;
xp = (t1*xx[i+2] + t2*xx[i+1] + t3*xx[i] + 50000) / 100000;
yp = (t1*yy[i+2] + t2*yy[i+1] + t3*yy[i] + 50000) / 100000;
if (lastvalid) line_btw(lastx, lasty, xp, yp);
lastx = xp;
lasty = yp;
lastvalid = True;
}
}
path_len = 0;
do_attribute_path(last_min_x, last_max_x, last_min_y, last_max_y);
SelectObject(ruleGC, oldPen);
SelectObject(ruleGC, oldBrush);
}
/*
* Shade the last box, circle, or ellipse
*/
static void
shade_last(char *cp)
{
blacken = whiten = False;
shade = True;
if (*cp) {
float tempShadetp;
if (sscanf(cp, "%f ", &tempShadetp) != 1) {
Warning ("Illegal format for shade level: %s", cp);
return;
}
else if (tempShadetp < 0.0 || tempShadetp > 1.0) {
Warning ("Invalid shade level: %s", cp);
return;
}
else
/* if "sh" has an argument we can safely assume that tpic 2.0 is used
so that all subsequent "sh" commands will come with an explicit
argument. Hence we may overwrite shadetp's old value */
/* Also note the inversion of gray levels for tpic 2.0 (0 = white,
1 = black) w.r.t. PostScript (0 = black, 1 = white) */
shadetp = 1.0 - tempShadetp;
}
if (foreTPicBrush)
if (!DeleteObject(foreTPicBrush))
Win32Error("shade_last/DeleteObject/foreTPicBrush");
foreTPicBrush = CreateSolidBrush(RGB((shadetp*255), (shadetp*255), (shadetp*255)));
}
/*
* Make the last box, circle, or ellipse, white inside (shade with white)
*/
static void
whiten_last()
{
whiten = True;
blacken = shade = False;
if (foreTPicBrush)
if (!DeleteObject(foreTPicBrush))
Win32Error("whiten_last/DeleteObject/foreTPicBrush");
foreTPicBrush = CreateSolidBrush(back_Pixel);
}
/*
* Make last box, etc, black inside
*/
static void
blacken_last()
{
blacken = True;
whiten = shade = False;
if (foreTPicBrush)
if (!DeleteObject(foreTPicBrush))
Win32Error("blacken_last/DeleteObject/foreTPicBrush");
foreTPicBrush = CreateSolidBrush(fore_Pixel);
}
/*
* Set current shading factor
*/
/* Taken from dvips ! */
/* set shading level (used with Fig 1.4-TFX). priol@irisa.fr, 89/04 */
/* A better trial at setting shading level -- jourdan@minos.inria.fr */
/* Count number of black bits in the pattern together with total number, */
/* compute the average and use this as the PostScript gray level */
static void
set_shade (char *cp)
{
int blackbits = 0, totalbits = 0;
while (*cp) {
switch (*cp) {
case '0':
totalbits += 4;
break;
case '1':
case '2':
case '4':
case '8':
blackbits += 1;
totalbits += 4;
break;
case '3':
case '5':
case '6':
case '9':
case 'a':
case 'A':
case 'c':
case 'C':
blackbits += 2;
totalbits += 4;
break;
case '7':
case 'b':
case 'B':
case 'd':
case 'D':
case 'e':
case 'E':
blackbits += 3;
totalbits += 4;
break;
case 'f':
case 'F':
blackbits += 4;
totalbits += 4;
break;
case ' ':
break;
default:
Warning("Invalid character in .tx patte: %s\n", cp);
return;
break;
}
cp++;
}
shadetp = 1.0 - ((double) blackbits / (double) totalbits);
if (foreTPicBrush)
if (!DeleteObject(foreTPicBrush))
Win32Error("set_shade/DeleteObject/foreTPicBrush");
foreTPicBrush = CreateSolidBrush(RGB((shadetp*255), (shadetp*255), (shadetp*255)));
} /* end of SetShade */
/*
* Code for PostScript<tm> specials begins here.
*/
#if PS
/*
* Information on how to search for PS header and figure files.
*/
static _Xconst char no_f_str_ps[] = "/%f";
static void ps_startup ARGS((int, int, _Xconst char *));
static void ps_startup2 ARGS((void));
void NullProc ARGS((void)) {}
/* ARGSUSED */
static void NullProc2 ARGS((_Xconst char *));
struct psprocs psp = { /* used for lazy startup of the ps machinery */
/* toggle */ NullProc,
/* destroy */ NullProc,
/* interrupt */ NullProc,
/* endpage */ NullProc,
/* drawbegin */ ps_startup,
/* drawraw */ NullProc2,
/* drawfile */ NULL,
/* drawend */ NullProc2,
/* beginheader */ ps_startup2,
/* endheader */ NullProc,
/* newdoc */ NullProc};
struct psprocs no_ps_procs = { /* used if postscript is unavailable */
/* toggle */ NullProc,
/* destroy */ NullProc,
/* interrupt */ NullProc,
/* endpage */ NullProc,
/* drawbegin */ drawbegin_none,
/* drawraw */ NullProc2,
/* drawfile */ NULL,
/* drawend */ NullProc2,
/* beginheader */ NullProc,
/* endheader */ NullProc,
/* newdoc */ NullProc};
#endif /* PS */
static Boolean bbox_valid;
static unsigned int bbox_width;
static unsigned int bbox_height;
static int bbox_angle;
static int bbox_voffset;
void
draw_bbox()
{
int xcorner, ycorner;
if (bbox_valid) {
xcorner = PXL_H - currwin.base_x;
ycorner = PXL_V - currwin.base_y;
if (bbox_angle == 0) {
ycorner -= bbox_voffset;
XDrawLine(DISP, currwin.win, ruleGC,
xcorner, ycorner,
xcorner + bbox_width, ycorner);
XDrawLine(DISP, currwin.win, ruleGC,
xcorner + bbox_width, ycorner,
xcorner + bbox_width, ycorner + bbox_height);
XDrawLine(DISP, currwin.win, ruleGC,
xcorner + bbox_width, ycorner + bbox_height,
xcorner, ycorner + bbox_height);
XDrawLine(DISP, currwin.win, ruleGC,
xcorner, ycorner + bbox_height,
xcorner, ycorner);
}
else {
float sin_a = sin(bbox_angle * (TWOPI / 360));
float cos_a = cos(bbox_angle * (TWOPI / 360));
float a, b, c, d;
a = cos_a * bbox_width;
b = -sin_a * bbox_width;
c = -sin_a * bbox_height;
d = -cos_a * bbox_height;
XDrawLine(DISP, currwin.win, ruleGC,
xcorner, ycorner,
xcorner + (int) rint(a), ycorner + (int) rint(b));
XDrawLine(DISP, currwin.win, ruleGC,
xcorner + (int) rint(a), ycorner + (int) rint(b),
xcorner + (int) rint(a + c), ycorner + (int) rint(b + d));
XDrawLine(DISP, currwin.win, ruleGC,
xcorner + (int) rint(a + c), ycorner + (int) rint(b + d),
xcorner + (int) rint(c), ycorner + (int) rint(d));
XDrawLine(DISP, currwin.win, ruleGC,
xcorner + (int) rint(c), ycorner + (int) rint(d),
xcorner, ycorner);
}
bbox_valid = False;
}
}
#if PS
static void
actual_startup()
{
/*
* Figure out what we want to use to display postscript figures
* and set at most one of the following to True:
* resource.useGS, resource.useDPS, resource.useNeWS
*
* Choose DPS then NEWS then Ghostscript if they are available
*/
if (!(
#ifdef PS_DPS
(resource.useDPS && initDPS())
#if defined(PS_NEWS) || defined(PS_GS)
||
#endif
#endif /* PS_DPS */
#ifdef PS_NEWS
(resource.useNeWS && initNeWS())
#ifdef PS_GS
||
#endif
#endif /* PS_NEWS */
#ifdef PS_GS
(resource.useGS && initGS())
#endif
))
psp = no_ps_procs;
}
static void
ps_startup(xul, yul, cp)
int xul, yul;
_Xconst char *cp;
{
if (!resource._postscript) {
psp.toggle = actual_startup;
draw_bbox();
return;
}
actual_startup();
psp.drawbegin(xul, yul, cp);
}
static void
ps_startup2()
{
actual_startup();
psp.beginheader();
}
/* ARGSUSED */
static void
NullProc2(cp)
_Xconst char *cp;
{}
static void
ps_parseraw(PostScript_cmd)
_Xconst char *PostScript_cmd;
{
_Xconst char *p;
/* dumb parsing of PostScript - search for rotation H. Zeller 1/97 */
bbox_angle = 0;
p = strstr(PostScript_cmd, "rotate");
if (p != NULL) {
while (*p != '\0' && !isdigit(*p)) --p;
while (*p != '\0' && isdigit(*p)) --p;
if (*p != '+' && *p != '-') ++p;
sscanf(p, "%d neg rotate", &bbox_angle);
}
}
/* ARGSUSED */
void
#if NeedFunctionPrototypes
drawbegin_none(int xul, int yul, _Xconst char *cp)
#else /* !NeedFunctionPrototypes */
drawbegin_none(xul, yul, cp)
int xul, yul;
_Xconst char *cp;
#endif /* NeedFunctionPrototypes */
{
draw_bbox();
}
struct tickrec {
struct tickrec *next;
int pageno;
char *command;
_Xconst char *tmpname;
};
static struct tickrec *tickhead = NULL; /* head of linked list of */
/* cached information */
static int nticks = 0; /* number of records total */
#ifndef TICKCACHESIZE
#define TICKCACHESIZE 3
#endif
#ifndef TICKTMP
#define TICKTMP "/tmp"
#endif
#ifdef WIN32
char tick_tmp[PATH_MAX];
#endif
static struct tickrec *
cachetick(filename, pathinfo, fp)
_Xconst char *filename;
kpse_file_format_type pathinfo;
FILE **fp;
{
struct tickrec **linkp;
struct tickrec *tikp;
struct tickrec **freerecp;
static int fileno=0;
linkp = &tickhead;
freerecp = NULL;
for (;;) { /* see if we have it already */
tikp = *linkp;
if (tikp == NULL) { /* if end of list */
if (nticks >= TICKCACHESIZE && freerecp != NULL) {
tikp = *freerecp;
*freerecp = tikp->next;
free(tikp->command);
unlink(tikp->tmpname);
}
else {
/* This will overflow when we get 10^9 temporary files.
If we have to worry I worry */
char *buffer=malloc(strlen(temporary_dir)+10);
tikp = (struct tickrec *)
xmalloc(sizeof(struct tickrec));
sprintf(buffer,"%s/%d",temporary_dir,fileno);
fileno++;
tikp->tmpname = buffer;
tikp->pageno = -1;
if (tikp->tmpname == NULL) {
Fputs("Cannot create temporary file name.\n", stderr);
free((char *) tikp);
return NULL;
}
++nticks;
}
tikp->command = xmalloc((unsigned) strlen(filename) + 1);
Strcpy(tikp->command, filename);
*fp = NULL;
break;
}
if (strcmp(filename, tikp->command) == 0) { /* found it */
*linkp = tikp->next; /* unlink it */
*fp = xfopen(tikp->tmpname, OPEN_MODE);
if (*fp == NULL) {
fprintf(stderr,"%s: ("__FILE__" at line %d) ", prog, __LINE__);
perror(tikp->tmpname);
}
break;
}
if (tikp->pageno != current_page) freerecp = linkp;
linkp = &tikp->next;
}
tikp->next = tickhead; /* link it in */
tickhead = tikp;
tikp->pageno =
pathinfo != kpse_tex_ps_header_format ? current_page : -1;
return tikp;
}
#ifndef UNCOMPRESS
#define UNCOMPRESS "uncompress"
#endif
#ifndef GUNZIP
#define GUNZIP "gunzip"
#endif
#ifndef BUNZIP2
#define BUNZIP2 "bunzip2"
#endif
#ifdef WIN32
/* ===========================================================================
* Uncompress input to output then close both files.
*/
void gz_uncompress(gzFile in, FILE *out)
{
char buf[16384];
int len;
int err;
for (;;) {
len = gzread(in, buf, sizeof(buf));
if (len < 0) {
fprintf(stderr, "gunzip: %s\n", (gzerror(in, &err)));
return;
}
if (len == 0) break;
if ((int)fwrite(buf, 1, (unsigned)len, out) != len) {
fprintf(stderr, "gunzip: failed fwrite\n");
return;
}
}
if (fclose(out)) {
fprintf(stderr, "gunzip: failed fclose\n");
return;
}
if (gzclose(in) != Z_OK) {
fprintf(stderr, "gunzip: failed gzclose\n");
return;
}
}
#endif
#ifdef USE_POPEN
static char buf_popen[1024];
#endif
static void
send_ps_file(filename, pathinfo)
_Xconst char *filename;
kpse_file_format_type pathinfo;
{
FILE *f;
static _Xconst char *argv[] = {NULL, "-c", NULL, NULL};
_Xconst char *bufp;
struct tickrec *tikp;
int len;
char magic1, magic2, magic3;
if (psp.drawfile == NULL || !resource._postscript) return;
#ifdef HTEX
filename=urlocalize(filename);
if (((URLbase != NULL) && htex_is_url(urlocalize(URLbase))) ||
(htex_is_url(filename))) {
/* xfopen knows how to fetch url things */
URL_aware=1;
f = xfopen(filename, OPEN_MODE);
bufp=filelist[lastwwwopen].file;
URL_aware=0;
} else
#endif
if (filename[0] == '`') {
if (!resource.allow_shell) {
if (warn_spec_now)
Fprintf(stderr,
"%s: shell escape disallowed for special \"%s\"\n",
prog, filename);
return;
}
tikp = cachetick(filename, pathinfo, &f);
if (tikp == NULL)
return;
if (f == NULL) {
#ifdef USE_POPEN
FILE *in, *out;
int nb_read, nb_written;
in = popen(filename+1, OPEN_MODE);
out = xfopen(tikp->tmpname, "wb");
if (!in) {
fclose(out);
fprintf(stderr, "Can't popen %s\n", filename+1);
return;
}
/* Copying the result of popen to tmpname */
for ( ; !feof(in); ) {
nb_read = fread(buf_popen, 1, sizeof(buf_popen), in);
if (ferror(in)) {
perror(filename+1);
break;
}
nb_written = fwrite(buf_popen, 1, nb_read, out);
if (ferror(out)) {
perror(tikp->tmpname);
break;
}
}
pclose(in);
fclose(out);
#else
len = strlen(filename) + strlen(tikp->tmpname) + (4 - 1);
if (len > ffline_len)
expandline(len);
Sprintf(ffline, "%s > %s", filename + 1, tikp->tmpname);
(void) system(ffline);
#endif
f = xfopen(tikp->tmpname, OPEN_MODE);
if (f == NULL) {
fprintf(stderr,"%s: ("__FILE__" at line %d) ", prog, __LINE__);
perror(tikp->tmpname);
return;
}
}
bufp = tikp->tmpname;
}
else {
bufp = kpse_find_file (filename, pathinfo, True);
f = bufp ? xfopen (bufp, OPEN_MODE) : NULL;
/* if still no luck, complain */
if (f == NULL) {
Fprintf(stderr, "%s: cannot find PS file `%s'.\n",
prog, filename);
draw_bbox();
return;
}
#ifdef WIN32
/* check for compressed files */
len = strlen(filename);
magic1 = '\037';
magic3 = '\0';
if ((len > 2 && _stricmp(filename + len - 2, ".Z") == 0 && (magic2 = '\235', True))
|| (len > 3 && _stricmp(filename + len - 3, ".gz") == 0 && (magic2 = '\213', True))) {
/* Uncompress or Gzip */
if (getc(f) != magic1 || (char) getc(f) != magic2)
rewind(f);
else {
Fclose(f);
++n_files_left;
tikp = cachetick(filename, pathinfo, &f);
if (tikp == NULL)
return;
if (f == NULL) {
FILE *outfile;
gzFile *infile;
infile = gzopen(bufp, "rb");
if (infile == NULL) {
fprintf(stderr, "%s: can't gzopen %s\n", prog, bufp);
return;
}
/* FIXME: Check that this is correct wrt to the unix case,
tmpname are reused. */
if ((outfile = fopen(tikp->tmpname, "wb")) == NULL) {
perror(tikp->tmpname);
return;
}
gz_uncompress(infile, outfile);
/* Files closing is cared of by gz_uncompress ! */
f = xfopen(tikp->tmpname, OPEN_MODE);
if (f == NULL) {
fprintf(stderr,"%s: ("__FILE__" at line %d) ",
prog, __LINE__);
perror(tikp->tmpname);
return;
}
}
bufp = tikp->tmpname;
}
}
else if (len > 4 && _stricmp(filename + len - 4, ".bz2") == 0) {
/* Bunzip2 */
magic1 = 'B';
magic2 = 'Z';
magic3 = 'h';
if (getc(f) != magic1 || (char) getc(f) != magic2
|| getc(f) != magic3)
rewind(f);
else {
Fclose(f);
++n_files_left;
tikp = cachetick(filename, pathinfo, &f);
if (tikp == NULL)
return;
if (f == NULL) {
FILE *outfile;
BZFILE *infile;
unsigned char buf[4096];
size_t len;
infile = BZ2_bzopen(bufp, "rb");
if (infile == NULL) {
fprintf(stderr, "%s: can't BZ2_bzopen %s\n", prog, bufp);
return;
}
/* FIXME: Check that this is correct wrt to the unix case,
tmpname are reused. */
if ((outfile = fopen(tikp->tmpname, "wb")) == NULL) {
perror(tikp->tmpname);
return;
}
while ((len = BZ2_bzread(infile, buf, sizeof(buf))) > 0)
fwrite (buf, 1, len, outfile);
BZ2_bzclose (infile);
fclose(outfile);
f = xfopen(tikp->tmpname, OPEN_MODE);
if (f == NULL) {
fprintf(stderr,"%s: ("__FILE__" at line %d) ",
prog, __LINE__);
perror(tikp->tmpname);
return;
}
}
bufp = tikp->tmpname;
}
}
else if (len > 3 && _stricmp(filename + len - 2, ".zip") == 0) {
fprintf(stderr, "%s: Zip files are not supported (%s)\n", prog, filename);
Fclose(f);
return;
}
else {
/* Not a compressed file ? */
rewind(f);
}
}
#else
/* check for compressed files */
len = strlen(filename);
magic1 = '\037';
magic3 = '\0';
if ((len > 2 && strcmp(filename + len - 2, ".Z") == 0
&& (argv[0] = UNCOMPRESS, magic2 = '\235', True))
|| (len > 3 && strcmp(filename + len - 3, ".gz") == 0
&& (argv[0] = GUNZIP, magic2 = '\213', True))
|| (len > 4 && strcmp(filename + len - 4, ".bz2") == 0
&& (argv[0] = BUNZIP2, magic1 = 'B', magic2 = 'Z',
magic3 = 'h', True))) {
if (getc(f) != magic1 || (char) getc(f) != magic2
|| (magic3 != '\0' && getc(f) != magic3))
rewind(f);
else {
Fclose(f);
++n_files_left;
tikp = cachetick(filename, pathinfo, &f);
if (tikp == NULL)
return;
if (f == NULL) {
pid_t pid;
int handle;
int status;
argv[2] = bufp;
handle = open(tikp->tmpname, O_RDWR | O_CREAT | O_EXCL,
S_IRUSR | S_IWUSR);
if (handle == -1 && errno == EEXIST) {
/* The tmpnames are reused for each page so
unlink the tmpname file from the previous page */
unlink(tikp->tmpname);
handle = open(tikp->tmpname, O_RDWR | O_CREAT |
O_EXCL, S_IRUSR | S_IWUSR);
}
if (handle == -1) {
fprintf(stderr,"%s: ("__FILE__" at line %d) ", prog, __LINE__);
perror(tikp->tmpname);
return;
}
Fflush(stderr); /* avoid double flushing */
pid = vfork();
if (pid == 0) { /* if child */
(void) dup2(handle, 1);
(void) execvp(argv[0], (char **) argv);
Fprintf(stderr, "Execvp of %s failed.\n", argv[0]);
Fflush(stderr);
_exit(1);
}
(void) close(handle);
for (;;) {
#if HAVE_WAITPID
if (waitpid(pid, &status, 0) != -1) break;
#else
# if HAVE_WAIT4
if (wait4(pid, &status, 0, (struct rusage *) NULL)
!= -1)
break;
# else
int retval;
retval = wait(&status);
if (retval == pid) break;
if (retval != -1) continue;
# endif /* HAVE_WAIT4 */
#endif /* HAVE_WAITPID */
if (errno == EINTR) continue;
perror("[xdvi] waitpid");
return;
}
f = xfopen(tikp->tmpname, OPEN_MODE);
if (f == NULL) {
fprintf(stderr,"%s: ("__FILE__" at line %d) ",
prog, __LINE__);
perror(tikp->tmpname);
return;
}
}
bufp = tikp->tmpname;
}
}
}
#endif
/* Success! */
psp.drawfile(bufp, f, filename); /* this is supposed to close the file */
}
void
ps_newdoc()
{
struct tickrec *tikp;
scanned_page = scanned_page_bak = scanned_page_reset =
resource.prescan ? -1 : total_pages;
for (tikp = tickhead; tikp != NULL; tikp = tikp->next)
tikp->pageno = -1;
psp.newdoc();
}
void
ps_destroy()
{
struct tickrec *tikp;
/* Note: old NeXT systems (at least) lack atexit/on_exit. */
psp.destroy();
for (tikp = tickhead; tikp != NULL; tikp = tikp->next)
if (unlink(tikp->tmpname) < 0) {
fprintf(stderr,"%s: ("__FILE__" at line %d) ", prog, __LINE__);
perror(tikp->tmpname);
}
}
#endif /* PS */
static void
psfig_special(cp)
char *cp;
{
char *filename;
int raww, rawh;
if (strncmp(cp, ":[begin]", 8) == 0) {
cp += 8;
bbox_valid = False;
bbox_angle = 0;
if (sscanf(cp,"%d %d\n", &raww, &rawh) >= 2) {
bbox_valid = True;
bbox_width = pixel_conv(spell_conv(raww));
bbox_height = pixel_conv(spell_conv(rawh));
bbox_voffset = 0;
}
if (currwin.win == mane.win)
#if PS
if (isPrinting)
psp.drawbegin(PXL_H, PXL_V,
cp);
else
psp.drawbegin(PXL_H - currwin.base_x, PXL_V - currwin.base_y,
cp);
#else
draw_bbox();
#endif
psfig_begun = True;
} else if (strncmp(cp, " plotfile ", 10) == 0) {
cp += 10;
while (isspace(*cp)) cp++;
/* handle "`zcat file". Borrowed from dvipsk...*/
if (*cp == '"') {
cp++;
for (filename = cp; *cp && (*cp != '"'); ++cp);
} else {
for (filename = cp; *cp && !isspace(*cp); ++cp);
}
*cp = '\0';
#if PS
if (currwin.win == mane.win)
send_ps_file(filename, kpse_pict_format);
#endif
} else if (strncmp(cp, ":[end]", 6) == 0) {
cp += 6;
#if PS
if (currwin.win == mane.win) psp.drawend(cp);
#endif
bbox_valid = False;
psfig_begun = False;
} else { /* I am going to send some raw postscript stuff */
if (*cp == ':')
++cp; /* skip second colon in ps:: */
#if PS
if (currwin.win == mane.win) {
ps_parseraw(cp);
if (psfig_begun) psp.drawraw(cp);
else {
if (isPrinting)
psp.drawbegin(PXL_H, PXL_V, cp);
else
psp.drawbegin(PXL_H - currwin.base_x,
PXL_V - currwin.base_y, cp);
psp.drawend("");
}
}
#endif
}
}
/* Keys for epsf specials */
static _Xconst char *keytab[] = {"clip",
"llx",
"lly",
"urx",
"ury",
"rwi",
"rhi",
"hsize",
"vsize",
"hoffset",
"voffset",
"hscale",
"vscale",
"angle"};
#define KEY_LLX keyval[0]
#define KEY_LLY keyval[1]
#define KEY_URX keyval[2]
#define KEY_URY keyval[3]
#define KEY_RWI keyval[4]
#define KEY_RHI keyval[5]
#define NKEYS (sizeof(keytab)/sizeof(*keytab))
#define N_ARGLESS_KEYS 1
static void
epsf_special(cp)
char *cp;
{
char *filename;
static char *buffer;
static unsigned int buflen = 0;
unsigned int len;
char *q;
int flags = 0;
double keyval[6];
filename = cp;
if (*cp == '\'' || *cp == '"') {
do ++cp;
while (*cp != '\0' && *cp != *filename);
++filename;
}
else
while (*cp != '\0' && *cp != ' ' && *cp != '\t') ++cp;
if (*cp != '\0') *cp++ = '\0';
while (*cp == ' ' || *cp == '\t') ++cp;
len = strlen(cp) + NKEYS + 30;
if (buflen < len) {
if (buflen != 0) free(buffer);
buflen = len;
buffer = xmalloc(buflen);
}
Strcpy(buffer, "@beginspecial");
q = buffer + strlen(buffer);
while (*cp != '\0') {
char *p1 = cp;
int keyno;
while (*p1 != '=' && !isspace(*p1) && *p1 != '\0') ++p1;
for (keyno = 0;; ++keyno) {
if (keyno >= NKEYS) {
if (warn_spec_now)
Fprintf(stderr,
"%s: unknown keyword (%*s) in \\special will be ignored\n",
prog, (int) (p1 - cp), cp);
break;
}
if (memcmp(cp, keytab[keyno], p1 - cp) == 0) {
if (keyno >= N_ARGLESS_KEYS) {
while (isspace(*p1)) ++p1;
if (*p1 == '=') {
++p1;
while (isspace(*p1)) ++p1;
}
if (keyno < N_ARGLESS_KEYS + 6) {
keyval[keyno - N_ARGLESS_KEYS] = atof(p1);
flags |= (1 << (keyno - N_ARGLESS_KEYS));
}
*q++ = ' ';
while (!isspace(*p1) && *p1 != '\0') *q++ = *p1++;
}
*q++ = ' ';
*q++ = '@';
Strcpy(q, keytab[keyno]);
q += strlen(q);
break;
}
}
cp = p1;
while (!isspace(*cp) && *cp != '\0') ++cp;
while (isspace(*cp)) ++cp;
}
Strcpy(q, " @setspecial\n");
bbox_valid = False;
if ((flags & 0x30) == 0x30 || ((flags & 0x30) && (flags & 0xf) == 0xf)){
bbox_valid = True;
bbox_width = 0.1 * ((flags & 0x10) ? KEY_RWI
: KEY_RHI * (KEY_URX - KEY_LLX) / (KEY_URY - KEY_LLY))
* dimconv / shrink_factor + 0.5;
bbox_voffset = bbox_height = 0.1 * ((flags & 0x20) ? KEY_RHI
: KEY_RWI * (KEY_URY - KEY_LLY) / (KEY_URX - KEY_LLX))
* dimconv / shrink_factor + 0.5;
}
if (filename && currwin.win == mane.win) {
#if PS
if (isPrinting)
psp.drawbegin(PXL_H, PXL_V, buffer);
else
psp.drawbegin(PXL_H - currwin.base_x, PXL_V - currwin.base_y,
buffer);
/* talk directly with the DPSHandler here */
send_ps_file(filename, kpse_pict_format);
psp.drawend(" @endspecial");
#else
draw_bbox();
#endif
}
bbox_valid = False;
}
static void
quote_special(cp)
char *cp;
{
bbox_valid = False;
#if PS
if (currwin.win == mane.win) {
if (isPrinting)
psp.drawbegin(PXL_H, PXL_V,
"@beginspecial @setspecial ");
else
psp.drawbegin(PXL_H - currwin.base_x, PXL_V - currwin.base_y,
"@beginspecial @setspecial ");
/* talk directly with the DPSHandler here */
psp.drawraw(cp + 1);
psp.drawend(" @endspecial");
}
#endif
/* nothing else to do--there's no bbox here */
}
#if PS
static void
scan_header(cp)
char *cp;
{
char *filename;
filename = cp;
if (*cp == '\'' || *cp == '"') {
do ++cp;
while (*cp != '\0' && *cp != *filename);
*cp = '\0';
++filename;
}
psp.beginheader();
send_ps_file(filename, kpse_tex_ps_header_format);
}
static void
scan_bang(cp)
char *cp;
{
psp.beginheader();
psp.drawraw(cp + 1);
}
#endif /* PS */
/*
Color support.
Basically, the primitives supported by dvips.
Still do not work when printing. Colors are mixed up because we can't do
transparent blt directly on the printer dc.
*/
void
background P1C(char *, p)
{
/* this should be valid only for the current page */
while ( *p <= ' ' ) p++ ;
#if DEBUG_COLOR
fprintf(stderr, "Special : background %s\n", p);
#endif
SetBackColor(string_to_colorref(p));
}
void
pushcolor P2C(char *, p, Boolean, outtops)
{
COLORREF c = string_to_colorref(p);
#if DEBUG_COLOR
fprintf(stderr, "Special : push color %s %8x\n", p, c);
#endif
CRefPush(color_stack, c);
SetForeColor(c);
}
void
popcolor P1C(Boolean, outtops)
{
CRefPop(color_stack);
SetForeColor(CRefTop(color_stack));
#if DEBUG_COLOR
fprintf(stderr, "Special : pop color %8x\n", fore_Pixel);
#endif
}
void
resetcolorstack P2C(char *, p, int, outtops)
{
COLORREF c = string_to_colorref(p);
CRefResetInit(color_stack, c);
SetForeColor(c);
}
/*
* The following copyright message applies to the rest of this file. --PV
*/
/*
* This program is Copyright (C) 1987 by the Board of Trustees of the
* University of Illinois, and by the author Dirk Grunwald.
*
* This program may be freely copied, as long as this copyright
* message remaines affixed. It may not be sold, although it may
* be distributed with other software which is sold. If the
* software is distributed, the source code must be made available.
*
* No warranty, expressed or implied, is given with this software.
* It is presented in the hope that it will prove useful.
*
* Hacked in ignorance and desperation by jonah@db.toronto.edu
*/
/*
* The code to handle the \specials generated by tpic was modified
* by Dirk Grunwald using the code Tim Morgan at Univ. of Calif, Irvine
* wrote for TeXsun.
*/
static char *
endofcommand(cp)
char *cp;
{
while (isspace(*cp)) ++cp;
if (*cp != '=') return NULL;
do ++cp; while (isspace(*cp));
return cp;
}
// addition SU: copied from dvi-draw.c
#define xtell(pos) ((long) (lseek(fileno(dvi_file), 0L, SEEK_CUR) - \
(currinf.end - (pos))))
void
applicationDoSpecial(cp, maybe_src)
char *cp;
int maybe_src;
{
char *p;
boolean gs_allowed;
struct drawinf oldinfo;
ubyte ch;
float pt_size;
off_t file_pos;
static Position remember_H, remember_V;
#ifdef TRANSFORM
extern XFORM *gs_getmatrix(void);
extern void xfrm_record(XFORM *);
extern void xfrm_apply(void);
#endif
/* Skip white space */
while (*cp == ' ' || *cp == '\t') ++cp;
if (allowDrawingChars && maybe_src) {
/* BEGIN CHUNK special.c 1 */
#ifdef SRC_SPECIALS
if (memicmp(cp, "src:", 4) == 0) {
/* oldinfo = currinf;
file_pos = xtell(currinf.pos);
pt_size = src_look_forward(dimconv);
fprintf(stderr, "returning.\n");
currinf = oldinfo;
(void) lseek(fileno(dvi_file), file_pos, SEEK_SET);
src_eval_special(cp + 4, PXL_H, PXL_V, pt_size); */
#if 0
fprintf(stderr, "executing %d, %d for %s\n", remember_H, remember_V, cp + 4);
#endif
src_eval_special(cp + 4, remember_H, remember_V);
delay_src = False;
}
return;
#endif
/* END CHUNK special.c 1 */
}
/* PostScript specials */
gs_allowed = !allowDrawingChars && ((mane.shrinkfactor > 1) || isPrinting);
#if 0
fprintf(stderr, "special found at %ld: %s, isPrinting = %d, gs_allowed = %d\n",
currinf.pos, cp, isPrinting, gs_allowed);
#endif
/* We do not allow to use gs at magnification 1 */
if (*cp == '"') {
if (gs_allowed) {
quote_special(cp);
#ifdef TRANSFORM
xfrm_record(gs_getmatrix());
#endif
}
#ifdef TRANSFORM
else if (allowDrawingChars) {
xfrm_apply();
}
#endif
return;
}
if (memicmp(cp, "ps:", 3) == 0) {
if (gs_allowed) {
psfig_special(cp + 3);
#ifdef TRANSFORM
xfrm_record(gs_getmatrix());
#endif
}
#ifdef TRANSFORM
else if (allowDrawingChars) {
xfrm_apply();
}
#endif
return;
}
if (memicmp(cp, "src:", 4) == 0) {
if (allowDrawingChars) {
#ifdef SRC_SPECIALS
remember_V = PXL_V;
remember_H = PXL_H;
#if 0
fprintf(stderr, "remembering %d, %d for %s\n", remember_H, remember_V, cp + 4);
#endif
delay_src = True;
#else
fprintf(stderr, "special not handled: %s\n", cp);
#endif
}
return;
}
if (memicmp(cp, "psfile", 6) == 0
&& (p = endofcommand(cp + 6)) != NULL) {
if (gs_allowed) {
epsf_special(p);
#ifdef TRANSFORM
xfrm_record(gs_getmatrix());
#endif
}
#ifdef TRANSFORM
else if (allowDrawingChars) {
xfrm_apply();
}
#endif
return;
}
/* these should have been scanned */
if (*cp == '!'
|| (memicmp(cp, "header", 6) == 0
&& endofcommand(cp + 6) != NULL)) {
#if PS
if (resource._postscript && scanned_page_reset >= 0) {
/* turn on scanning and redraw the page */
scanned_page = scanned_page_bak = scanned_page_reset = -1;
psp.interrupt();
canit = True;
/* longjmp(canit_env, 1); */
SendMessage(hWndDraw, WM_COMMAND, IDM_REDRAWPAGE, 0);
}
#endif
return;
}
/* Color specials */
if (memicmp(cp, "background", 10) == 0) {
#if 0
if (scanned_page_reset >= 0) {
p = cp + 11;
while ( *p <= ' ' ) p++ ;
background(p) ;
scanned_page = scanned_page_bak = scanned_page_reset = -1;
if (! isPrinting)
SendMessage(hWndDraw, WM_COMMAND, IDM_REDRAWPAGE, 0);
}
#else
p = cp + 11;
while ( *p <= ' ' ) p++ ;
background(p) ;
#endif
return ;
}
if (memicmp(cp, "landscape", 9) == 0) {
fprintf(stderr, "special not handled: %s\n", cp);
return;
}
if (memicmp(cp, "papersize", 9) == 0) {
fprintf(stderr, "special not handled: %s\n", cp);
return;
}
if (memicmp(cp, "pos:", 4) == 0) {
/* ignore positional specials */
return;
}
if (memicmp(cp, "xtex:", 5) == 0) {
return;
}
if (memicmp(cp, "em:", 3) == 0) {
if (currwin.win == hWndDraw)
emspecial(cp+3, PXL_H, PXL_V);
return;
}
if (memicmp(cp, "color", 5) == 0) {
p = cp + 6;
while ( *p <= ' ' ) p++ ;
if (memicmp(p, "push", 4) == 0) {
p += 5;
while ( *p <= ' ' ) p++ ;
pushcolor(p, 0) ;
} else if (memicmp(p, "pop", 3) == 0) {
popcolor(0) ;
} else {
resetcolorstack(p,0) ;
}
return;
} /* IBM: color - end changes */
#ifdef HTEX
if (checkHyperTeX(cp, current_page)) return;
#endif
/* tpic specials */
if (*cp >= 'a' && *cp <= 'z' && cp[1] >= 'a' && cp[1] <= 'z' &&
(isspace(cp[2]) || cp[2] == '\0')) {
#if DEBUG_TPIC
fprintf(stderr, "Tpic special : %s\n", cp);
#endif
switch (CMD(*cp, cp[1])) {
case CMD('p','n'): set_pen_size(cp + 2); return;
case CMD('f','p'): flush_path(); return;
case CMD('d','a'): flush_dashed(cp + 2, False); return;
case CMD('d','t'): flush_dashed(cp + 2, True); return;
case CMD('p','a'): add_path(cp + 2); return;
case CMD('a','r'): arc(cp + 2, False); return;
case CMD('i','a'): arc(cp + 2, True); return;
case CMD('s','p'): flush_spline(cp+2); return;
case CMD('s','h'): shade_last(cp+2); return;
case CMD('w','h'): whiten_last(); return;
case CMD('b','k'): blacken_last(); return;
case CMD('i','p'): /* throw away the path -- jansteen */
path_len = 0; return;
case CMD('t','x'): set_shade(cp+2); return;
}
}
if (warn_spec_now)
Fprintf(stderr, "%s: special \"%s\" not implemented\n", prog, cp);
}
#undef CMD
#if PS
void
scan_special(cp)
char *cp;
{
char *p;
/* Skip white space */
while (*cp == ' ' || *cp == '\t') ++cp;
if (debug & DBG_PS)
Printf("Scanning special `%s'.\n", cp);
if (*cp == '"'
|| memicmp(cp, "ps:", 3) == 0
|| (memicmp(cp, "psfile", 6) == 0 && (p = endofcommand(cp + 6)) != NULL)
|| ((memicmp(cp, "header", 6) == 0) && ((p = endofcommand(cp + 6)) != NULL))) {
psToDisplay = TRUE;
}
/* these should have been scanned */
if (*cp == '!')
scan_bang(cp);
else if ((memicmp(cp, "header", 6) == 0)
&& ((p = endofcommand(cp + 6)) != NULL))
scan_header(p);
else if (memicmp(cp, "background", 10) == 0
|| memicmp(cp, "color", 5) == 0) {
scan_colors(cp);
}
}
#endif /* PS */
|