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
|
#include "sys-defines.h"
#include "extern.h"
/* Original author: Keith Packard, MIT X Consortium.
Hacked by Robert S. Maier, 1998-99. */
/* This module contains the miWideLine() and miWideDash() functions. They
rasterize wide polylines (usually just called `wide lines'), either
solid or dashed.
Any wide line is treated as a polygon to be painted. (If it is dashed,
each dash is treated as a polygon.) The painting follows libxmi's
policy on painting of `edge' pixels, i.e., pixels that lie exactly on a
boundary. A pixel is not painted if it lies on a `right' or `bottom'
edge of a polygon.
All painting goes through the low-level MI_PAINT_SPANS() macro. */
#include "xmi.h"
#include "mi_spans.h"
#include "mi_gc.h"
#include "mi_api.h"
#include "mi_widelin.h"
/* undefine if hypot is available (it's X_OPEN, but not ANSI or POSIX) */
#define hypot(x, y) sqrt((x)*(x) + (y)*(y))
/* internal functions that do painting of pixels */
static void miFillPolyHelper ____P((miPaintedSet *paintedSet, miPixel pixel, int y, unsigned int overall_height, PolyEdge *left, PolyEdge *right, int left_count, int right_count));
static void miFillRectPolyHelper ____P((miPaintedSet *paintedSet, miPixel pixel, int x, int y, unsigned int w, unsigned int h));
static void miLineArc ____P((miPaintedSet *paintedSet, miPixel pixel, const miGC *pGC, LineFace *leftFace, LineFace *rightFace, double xorg, double yorg, bool isInt));
static void miLineJoin ____P((miPaintedSet *paintedSet, miPixel pixel, const miGC *pGC, LineFace *pLeft, LineFace *pRight));
static void miLineProjectingCap ____P((miPaintedSet *paintedSet, miPixel pixel, const miGC *pGC, const LineFace *face, bool isLeft, bool isInt));
static void miWideDashSegment ____P((miPaintedSet *paintedSet, const miGC *pGC, int *pDashNum, int *pDashIndex, int *pDashOffset, int x1, int y1, int x2, int y2, bool projectLeft, bool projectRight, LineFace *leftFace, LineFace *rightFace));
static void miWideSegment ____P((miPaintedSet *paintedSet, miPixel pixel, const miGC *pGC, int x1, int y1, int x2, int y2, bool projectLeft, bool projectRight, LineFace *leftFace, LineFace *rightFace));
/* internal functions that don't do painting of pixels */
static int miLineArcD ____P((const miGC *pGC, double xorg, double yorg, miPoint *points, unsigned int *widths, PolyEdge *edge1, int edgey1, bool edgeleft1, PolyEdge *edge2, int edgey2, bool edgeleft2));
static int miLineArcI ____P((const miGC *pGC, int xorg, int yorg, miPoint *points, unsigned int *widths));
static int miPolyBuildEdge ____P((double x0, double y0, double k, int dx, int dy, int xi, int yi, bool left, PolyEdge *edge));
static int miPolyBuildPoly ____P((const PolyVertex *vertices, const PolySlope *slopes, int count, int xi, int yi, PolyEdge *left, PolyEdge *right, int *pnleft, int *pnright, unsigned int *h));
static int miRoundCapClip ____P((const LineFace *face, bool isInt, PolyEdge *edge, bool *leftEdge));
static int miRoundJoinFace ____P((const LineFace *face, PolyEdge *edge, bool *leftEdge));
static void miRoundJoinClip ____P((LineFace *pLeft, LineFace *pRight, PolyEdge *edge1, PolyEdge *edge2, int *y1, int *y2, bool *left1, bool *left2));
/* Spans-based convex polygon filler. Paints a convex polygon, supplied as
lists of `left' and `right' edges. Used for painting polygonal line
caps and line joins.
This implements libxmi's policy on painting `edge' pixels, i.e., pixels
that lie exactly on the boundary of a polygon. A pixel is not painted
if it lies on a `right' or `bottom' edge of the polygon. */
static void
#ifdef _HAVE_PROTOS
miFillPolyHelper (miPaintedSet *paintedSet, miPixel pixel, int y, unsigned int overall_height, PolyEdge *left, PolyEdge *right, int left_count, int right_count)
#else
miFillPolyHelper (paintedSet, pixel, y, overall_height, left, right, left_count, right_count)
miPaintedSet *paintedSet;
miPixel pixel;
int y; /* starting y coordinate */
unsigned int overall_height; /* height of entire segment */
PolyEdge *left, *right;
int left_count, right_count;
#endif
{
int left_x = 0, left_e = 0;
int left_stepx = 0;
int left_signdx = 0;
int left_dy = 0, left_dx = 0;
int right_x = 0, right_e = 0;
int right_stepx = 0;
int right_signdx = 0;
int right_dy = 0, right_dx = 0;
unsigned int left_height = 0, right_height = 0;
miPoint *ppt;
miPoint *pptInit = (miPoint *)NULL;
unsigned int *pwidth;
unsigned int *pwidthInit = (unsigned int *)NULL;
pptInit = (miPoint *)mi_xmalloc(overall_height * sizeof(miPoint));
pwidthInit = (unsigned int *)mi_xmalloc(overall_height * sizeof(unsigned int));
ppt = pptInit;
pwidth = pwidthInit;
while ((left_count || left_height) && (right_count || right_height))
{
unsigned int height;
/* load fields from next left edge, right edge */
MIPOLYRELOADLEFT
MIPOLYRELOADRIGHT
height = UMIN (left_height, right_height);
left_height -= height;
right_height -= height;
/* walk down to end of left or right edge, whichever comes first */
while (height--)
{
if (right_x >= left_x)
/* generate a span (omitting point on right end, see above) */
{
ppt->x = left_x;
ppt->y = y;
ppt++;
*pwidth++ = (unsigned int)(right_x - left_x + 1);
}
y++;
/* update left_x, right_x by stepping along left and right edges,
using midpoint line algorithm */
MIPOLYSTEPLEFT
MIPOLYSTEPRIGHT
}
}
MI_PAINT_SPANS(paintedSet, pixel, ppt - pptInit, pptInit, pwidthInit)
}
/* Rectangle filler. Policy mentioned above (no painting of right or
bottom edges) is followed. */
static void
#ifdef _HAVE_PROTOS
miFillRectPolyHelper (miPaintedSet *paintedSet, miPixel pixel, int x, int y, unsigned int w, unsigned int h)
#else
miFillRectPolyHelper (paintedSet, pixel, x, y, w, h)
miPaintedSet *paintedSet;
miPixel pixel;
int x, y;
unsigned int w, h;
#endif
{
miPoint *ppt, *pptInit;
unsigned int *pwidth, *pwidthInit;
pptInit = (miPoint *)mi_xmalloc (h * sizeof(miPoint));
pwidthInit = (unsigned int *)mi_xmalloc (h * sizeof(unsigned int));
ppt = pptInit;
pwidth = pwidthInit;
while (h--)
{
*pwidth++ = w;
ppt->x = x;
ppt->y = y;
ppt++;
y++;
}
MI_PAINT_SPANS(paintedSet, pixel, ppt - pptInit, pptInit, pwidthInit)
}
/* Build a single polygon edge (either a left edge or a right edge).
I.e. compute integer edge data that can be used by the midpoint line
algorithm. The edge will be traversed downward (on entry, dy != 0 is
assumed). Returns starting value for y, i.e. integer y-value for top of
edge. */
/* Supplied: an integer offset (xi,yi), the exact floating-point edge start
(x0,y0), its (rational) slope dy/dx, and the quantity k = x0*dy-y0*dx,
which is a measure of distance of (x0,y0) from the parallel line segment
that passes through (0,0). k will be transformed into the initial value
for e, the integer decision variable for the midpoint line algorithm. */
/* The integer edge data that are computed do not include the `height'
field, i.e. the number of scanlines to process. */
static int
#ifdef _HAVE_PROTOS
miPolyBuildEdge (double x0, double y0, double k, int dx, int dy, int xi, int yi, bool left, PolyEdge *edge)
#else
miPolyBuildEdge (x0, y0, k, dx, dy, xi, yi, left, edge)
double x0, y0; /* starting point of edge (rel. to (xi,yi)) */
double k; /* x0 * dy - y0 * dx */
int dx, dy; /* edge has rational slope dy/dx */
int xi, yi; /* integer offset for coordinate system */
bool left; /* left edge, not right edge? */
PolyEdge *edge; /* integer edge data, to be filled in */
#endif
{
int x, y, e;
int xady;
/* make dy positive, since edge will be traversed downward */
if (dy < 0)
{
dy = -dy;
dx = -dx;
k = -k;
}
#if 0
{
double realk, kerror;
realk = x0 * dy - y0 * dx;
kerror = fabs (realk - k);
if (kerror > .1)
printf ("realk: %g\t k: %g\n", realk, k);
}
#endif
/* integer starting value for y: round up the floating-point value */
y = ICEIL (y0);
/* work out integer starting value for x */
xady = ICEIL (k) + y * dx;
if (xady <= 0)
x = - (-xady / dy) - 1;
else
x = (xady - 1) / dy;
/* start working out initial value of decision variable */
e = xady - x * dy; /* i.e. ICEIL(k) - (x * dy - y * dx) */
/* work out optional and non-optional x increment, for algorithm */
if (dx >= 0)
{
edge->signdx = 1; /* optional step */
edge->stepx = dx / dy; /* non-optional step, 0 if dx<dy in mag. */
edge->dx = dx % dy;
}
else
{
edge->signdx = -1; /* optional step */
edge->stepx = - (-dx / dy); /* non-optional step, 0 if dx<dy in mag. */
edge->dx = -dx % dy;
e = dy - e + 1;
}
edge->dy = dy;
edge->x = x + (left == true ? 1 : 0) + xi; /* starting value for x */
edge->e = e - dy; /* bias: initial value for e */
/* return integer starting value for y, i.e. top of edge */
return y + yi;
}
/* add incr to v, cyclically; always stay in range 0..max */
#define StepAround(v, incr, max) (((v) + (incr) < 0) ? (max - 1) : ((v) + (incr) == max) ? 0 : ((v) + (incr)))
/* Build lists of right and left polygon edges, from an array of vertex
coordinates (floating-point), a precomputed array of PolySlopes
(including a `k' value for each edge), and an integer offset vector.
Also return overall vertical range and top (starting) y value. */
static int
#ifdef _HAVE_PROTOS
miPolyBuildPoly (const PolyVertex *vertices, const PolySlope *slopes, int count, int xi, int yi, PolyEdge *left, PolyEdge *right, int *pnleft, int *pnright, unsigned int *h)
#else
miPolyBuildPoly (vertices, slopes, count, xi, yi, left, right, pnleft, pnright, h)
const PolyVertex *vertices;
const PolySlope *slopes;
int count;
int xi, yi; /* (xi,yi) = integer offset for polygon */
PolyEdge *left, *right;
int *pnleft, *pnright;
unsigned int *h;
#endif
{
int top, bottom;
double miny, maxy;
int i;
int j;
int clockwise;
int slopeoff;
int s;
int nright, nleft;
int y, lasty = 0, bottomy, topy = 0;
/* compute min, max y values for polygon (floating-point); also location
of corresponding vertices in vertex array */
maxy = miny = vertices[0].y;
bottom = top = 0;
for (i = 1; i < count; i++)
{
if (vertices[i].y < miny)
{
top = i;
miny = vertices[i].y;
}
if (vertices[i].y >= maxy)
{
bottom = i;
maxy = vertices[i].y;
}
}
/* compute integer y-value for bottom of polygon (round up) */
bottomy = ICEIL (maxy) + yi;
/* determine whether should go `clockwise' or `counterclockwise'
to move down the right side of the polygon */
i = top;
j = StepAround (top, -1, count);
clockwise = 1;
slopeoff = 0;
if (slopes[j].dy * slopes[i].dx > slopes[i].dy * slopes[j].dx)
{
clockwise = -1;
slopeoff = -1;
}
/* step around right side of polygon from top to bottom, building array
of `right' edges (horizontal edges are ignored) */
i = top;
s = StepAround (top, slopeoff, count);
nright = 0;
while (i != bottom)
{
if (slopes[s].dy != 0)
{
y = miPolyBuildEdge (vertices[i].x, vertices[i].y,
slopes[s].k, slopes[s].dx, slopes[s].dy,
xi, yi, false,
&right[nright]);
if (nright != 0)
right[nright-1].height = y - lasty;
else /* y is top of first edge */
topy = y;
nright++;
lasty = y;
}
i = StepAround (i, clockwise, count);
s = StepAround (s, clockwise, count);
}
if (nright != 0)
right[nright-1].height = bottomy - lasty;
/* step around left side of polygon from top to bottom, building array of
`left' edges (horizontal edges are ignored) */
if (slopeoff == 0)
slopeoff = -1;
else
slopeoff = 0;
i = top;
s = StepAround (top, slopeoff, count);
nleft = 0;
while (i != bottom)
{
if (slopes[s].dy != 0)
{
y = miPolyBuildEdge (vertices[i].x, vertices[i].y,
slopes[s].k, slopes[s].dx, slopes[s].dy,
xi, yi, true,
&left[nleft]);
if (nleft != 0)
left[nleft-1].height = y - lasty;
nleft++;
lasty = y;
}
i = StepAround (i, -clockwise, count);
s = StepAround (s, -clockwise, count);
}
if (nleft != 0)
left[nleft-1].height = bottomy - lasty;
/* return number of left-side and right-side edges; also height (vertical
range, an unsigned int) and the vertical location of the top vertex
(an integer) */
*pnleft = nleft;
*pnright = nright;
*h = bottomy - topy;
return topy;
}
/* Paint all types of line join: round/miter/bevel/triangular. Called by
both miWideLine() and miWideDash(). Left and right line faces are
supplied, each with its own value of k. They may be modified. */
static void
#ifdef _HAVE_PROTOS
miLineJoin (miPaintedSet *paintedSet, miPixel pixel, const miGC *pGC, LineFace *pLeft, LineFace *pRight)
#else
miLineJoin (paintedSet, pixel, pGC, pLeft, pRight)
miPaintedSet *paintedSet;
miPixel pixel;
const miGC *pGC;
LineFace *pLeft, *pRight;
#endif
{
double mx = 0.0, my = 0.0;
int denom = 0; /* avoid compiler warnings */
PolyVertex vertices[4];
PolySlope slopes[4];
int edgecount;
PolyEdge left[4], right[4];
int nleft, nright;
int y;
unsigned int height;
bool swapslopes;
int joinStyle = (int)pGC->joinStyle;
int lw = (int)(pGC->lineWidth);
if (joinStyle == (int)MI_JOIN_ROUND)
{
/* invoke miLineArc to fill the round join, isInt = true */
miLineArc (paintedSet, pixel, pGC,
pLeft, pRight, (double)0.0, (double)0.0, true);
return;
}
denom = - pLeft->dx * pRight->dy + pRight->dx * pLeft->dy;
if (denom == 0)
return; /* no join to draw */
/* Now must handle cases where line join is a small polygon to be filled;
specify its vertices clockwise. */
/* swap slopes if cross product of line faces has wrong sign */
if (denom > 0)
{
swapslopes = false;
pLeft->xa = -pLeft->xa;
pLeft->ya = -pLeft->ya;
pLeft->dx = -pLeft->dx;
pLeft->dy = -pLeft->dy;
}
else
{
swapslopes = true;
pRight->xa = -pRight->xa;
pRight->ya = -pRight->ya;
pRight->dx = -pRight->dx;
pRight->dy = -pRight->dy;
}
/* vertex #0 is at the right end of the right face */
vertices[0].x = pRight->xa;
vertices[0].y = pRight->ya;
slopes[0].dx = -pRight->dy;
slopes[0].dy = pRight->dx;
slopes[0].k = 0;
/* vertex #1 is the nominal join point (i.e. halfway across both the
right face and the left face) */
vertices[1].x = 0;
vertices[1].y = 0;
slopes[1].dx = pLeft->dy;
slopes[1].dy = -pLeft->dx;
slopes[1].k = 0;
/* vertex #2 is at the left end of the left face */
vertices[2].x = pLeft->xa;
vertices[2].y = pLeft->ya;
if (joinStyle == (int)MI_JOIN_MITER)
{
double miterlimit = pGC->miterLimit;
/* compute vertex (mx,my) of miter quadrilateral */
my = (pLeft->dy * (pRight->xa * pRight->dy - pRight->ya * pRight->dx) -
pRight->dy * (pLeft->xa * pLeft->dy - pLeft->ya * pLeft->dx )) /
(double) denom;
if (pLeft->dy != 0)
mx = pLeft->xa + (my - pLeft->ya) *
(double) pLeft->dx / (double) pLeft->dy;
else
mx = pRight->xa + (my - pRight->ya) *
(double) pRight->dx / (double) pRight->dy;
/* if miter limit violated, switch to bevelled join */
if ((mx * mx + my * my) * 4 > miterlimit * miterlimit * lw * lw)
joinStyle = (int)MI_JOIN_BEVEL;
}
switch ((int)joinStyle)
{
double scale, dx, dy, adx, ady;
case (int)MI_JOIN_MITER:
default:
/* join by adding a quadrilateral */
edgecount = 4;
slopes[2].dx = pLeft->dx;
slopes[2].dy = pLeft->dy;
slopes[2].k = pLeft->k;
if (swapslopes)
{
slopes[2].dx = -slopes[2].dx;
slopes[2].dy = -slopes[2].dy;
slopes[2].k = -slopes[2].k;
}
/* vertex #3 is miter vertex (mx,my) */
vertices[3].x = mx;
vertices[3].y = my;
slopes[3].dx = pRight->dx;
slopes[3].dy = pRight->dy;
slopes[3].k = pRight->k;
if (swapslopes)
{
slopes[3].dx = -slopes[3].dx;
slopes[3].dy = -slopes[3].dy;
slopes[3].k = -slopes[3].k;
}
break;
case (int)MI_JOIN_BEVEL:
/* join by adding a triangle */
{
PolyVertex midpoint;
edgecount = 3;
/* third edge of triangle will pass through midpoint */
midpoint.x = 0.5 * (pLeft->xa + pRight->xa);
midpoint.y = 0.5 * (pLeft->ya + pRight->ya);
/* vector along third edge of triangle */
dx = pRight->xa - pLeft->xa;
dy = pRight->ya - pLeft->ya;
/* compute scale = max(|dx|,|dy|) */
adx = dx;
ady = dy;
if (adx < 0)
adx = -adx;
if (ady < 0)
ady = -ady;
scale = ady;
if (adx > ady)
scale = adx;
/* use integer dx, dy in range -65536..65536 */
slopes[2].dx = (int)((dx * 65536) / scale);
slopes[2].dy = (int)((dy * 65536) / scale);
slopes[2].k = midpoint.x * slopes[2].dy - midpoint.y * slopes[2].dx;
}
break;
case (int)MI_JOIN_TRIANGULAR:
/* join by adding a stubby quadrilateral */
{
PolyVertex midpoint, newpoint;
double mid2, mid, dx2, dy2, dx3, dy3;
edgecount = 4;
/* compute additional vertex, offset by linewidth/2 */
midpoint.x = 0.5 * (pLeft->xa + pRight->xa);
midpoint.y = 0.5 * (pLeft->ya + pRight->ya);
mid2 = midpoint.x * midpoint.x + midpoint.y * midpoint.y;
mid = sqrt (mid2);
newpoint.x = 0.5 * lw * midpoint.x / mid;
newpoint.y = 0.5 * lw * midpoint.y / mid;
vertices[3] = newpoint;
/* offset from vertices[2] to vertices[3] */
dx2 = vertices[3].x - vertices[2].x;
dy2 = vertices[3].y - vertices[2].y;
/* offset from vertices[3] back to vertices[0] */
dx3 = vertices[0].x - vertices[3].x;
dy3 = vertices[0].y - vertices[3].y;
/* compute scale = max(|dx|,|dy|), where (dx,dy) is offset between
the two corners, i.e. vertices[0] and vertices[2] */
dx = pRight->xa - pLeft->xa;
dy = pRight->ya - pLeft->ya;
adx = dx;
ady = dy;
if (adx < 0)
adx = -adx;
if (ady < 0)
ady = -ady;
scale = ady;
if (adx > ady)
scale = adx;
/* use integer dx, dy in range -65536..65536 */
slopes[2].dx = (int)((dx2 * 65536) / scale);
slopes[2].dy = (int)((dy2 * 65536) / scale);
slopes[2].k = newpoint.x * slopes[2].dy - newpoint.y * slopes[2].dx;
/* use integer dx, dy in range -65536..65536 */
slopes[3].dx = (int)((dx3 * 65536) / scale);
slopes[3].dy = (int)((dy3 * 65536) / scale);
slopes[3].k = newpoint.x * slopes[3].dy - newpoint.y * slopes[3].dx;
}
break;
} /* end of switch */
/* compute lists of left and right edges for the small polygon, using the
just-computed slopes array */
y = miPolyBuildPoly (vertices, slopes, edgecount, pLeft->x, pLeft->y,
left, right, &nleft, &nright, &height);
/* fill the small polygon */
miFillPolyHelper (paintedSet, pixel,
y, height, left, right, nleft, nright);
}
/* Paint either (1) a round cap on a line face or (2) a pie-wedge between
two line faces. Used for round capping and round joining respectively.
One or two line faces are supplied. They may be modified. */
static void
#ifdef _HAVE_PROTOS
miLineArc (miPaintedSet *paintedSet, miPixel pixel, const miGC *pGC, LineFace *leftFace, LineFace *rightFace, double xorg, double yorg, bool isInt)
#else
miLineArc (paintedSet, pixel, pGC, leftFace, rightFace, xorg, yorg, isInt)
miPaintedSet *paintedSet;
miPixel pixel;
const miGC *pGC;
LineFace *leftFace, *rightFace;
double xorg, yorg;
bool isInt;
#endif
{
miPoint *points;
unsigned int *widths;
int xorgi = 0, yorgi = 0;
int n;
PolyEdge edge1, edge2;
int edgey1, edgey2;
bool edgeleft1, edgeleft2;
if (isInt)
/* in integer case, take (xorgi,yorgi) from face; otherwise (0,0) */
{
xorgi = leftFace ? leftFace->x : rightFace->x;
yorgi = leftFace ? leftFace->y : rightFace->y;
}
edgey1 = INT_MAX;
edgey2 = INT_MAX;
edge1.x = 0; /* not used, keep memory checkers happy */
edge1.dy = -1;
edge2.x = 0; /* not used, keep memory checkers happy */
edge2.dy = -1;
edgeleft1 = false;
edgeleft2 = false;
if ((pGC->lineStyle != (int)MI_LINE_SOLID || pGC->lineWidth > 2)
&&
((pGC->capStyle == (int)MI_CAP_ROUND && pGC->joinStyle != (int)MI_JOIN_ROUND)
||
(pGC->joinStyle == (int)MI_JOIN_ROUND && pGC->capStyle == (int)MI_CAP_BUTT)))
/* construct clipping edges from the passed line faces (otherwise,
ignore them; will just draw a disk) */
{
if (isInt)
{
xorg = (double) xorgi;
yorg = (double) yorgi;
}
if (leftFace && rightFace)
/* have two faces, so construct clipping edges for pie wedge */
miRoundJoinClip (leftFace, rightFace, &edge1, &edge2,
&edgey1, &edgey2, &edgeleft1, &edgeleft2);
else if (leftFace)
/* will draw half-disk on left face, so construct clipping edge */
edgey1 = miRoundCapClip (leftFace, isInt, &edge1, &edgeleft1);
else if (rightFace)
/* will draw half-disk on right face, so construct clipping edge */
edgey2 = miRoundCapClip (rightFace, isInt, &edge2, &edgeleft2);
/* due to clipping, switch to using floating-point coordinates */
isInt = false;
}
points = (miPoint *)mi_xmalloc(sizeof(miPoint) * pGC->lineWidth);
widths = (unsigned int *)mi_xmalloc(sizeof(unsigned int) * pGC->lineWidth);
/* construct a Spans by calling integer or floating point routine */
if (isInt)
/* integer routine, no clipping: just draw a disk */
n = miLineArcI (pGC, xorgi, yorgi, points, widths);
else
/* call floating point routine, supporting clipping by edge(s) */
n = miLineArcD (pGC, xorg, yorg, points, widths,
&edge1, edgey1, edgeleft1,
&edge2, edgey2, edgeleft2);
MI_PAINT_SPANS(paintedSet, pixel, n, points, widths)
}
/* Draw a filled disk, of diameter equal to the linewidth, as a Spans.
This is used for round caps or round joins, if the clipping by one or
two edges can be ignored. Integer coordinates only are used. Returns
number of spans in the Spans. */
static int
#ifdef _HAVE_PROTOS
miLineArcI (const miGC *pGC, int xorg, int yorg, miPoint *points, unsigned int *widths)
#else
miLineArcI (pGC, xorg, yorg, points, widths)
const miGC *pGC;
int xorg, yorg;
miPoint *points;
unsigned int *widths;
#endif
{
miPoint *tpts, *bpts;
unsigned int *twids, *bwids;
int x, y, e, ex;
int slw;
tpts = points;
twids = widths;
slw = (int)(pGC->lineWidth);
if (slw == 1)
/* `disk' is a single pixel */
{
tpts->x = xorg;
tpts->y = yorg;
*twids = 1;
return 1;
}
/* otherwise, draw the disk scanline by scanline */
bpts = tpts + slw;
bwids = twids + slw;
y = (slw >> 1) + 1;
if (slw & 1)
e = - ((y << 2) + 3);
else
e = - (y << 3);
ex = -4;
x = 0;
while (y)
{
e += (y << 3) - 4;
while (e >= 0)
{
x++;
e += (ex = -((x << 3) + 4));
}
y--;
slw = (x << 1) + 1;
if ((e == ex) && (slw > 1))
slw--;
tpts->x = xorg - x;
tpts->y = yorg - y;
tpts++;
*twids++ = slw;
if ((y != 0) && ((slw > 1) || (e != ex)))
{
bpts--;
bpts->x = xorg - x;
bpts->y = yorg + y;
*--bwids = slw;
}
}
/* return linewidth (no. of spans in the Spans) */
return (int)(pGC->lineWidth);
}
#define CLIPSTEPEDGE(edgey,edge,edgeleft) \
if (ybase == edgey) \
{ \
if (edgeleft) \
{ \
if (edge->x > xcl) \
xcl = edge->x; \
} \
else \
{ \
if (edge->x < xcr) \
xcr = edge->x; \
} \
edgey++; \
edge->x += edge->stepx; \
edge->e += edge->dx; \
if (edge->e > 0) \
{ \
edge->x += edge->signdx; \
edge->e -= edge->dy; \
} \
}
/* Draw as a Spans a filled disk of diameter equal to the linewidth, paying
attention to one or two clipping edges. This is used for round caps and
round joins, respectively (it respectively yields a half-disk or a pie
wedge). Floating point coordinates are used. Returns number of spans
in the Spans. The clipping edges may be modified. */
static int
#ifdef _HAVE_PROTOS
miLineArcD (const miGC *pGC, double xorg, double yorg, miPoint *points, unsigned int *widths, PolyEdge *edge1, int edgey1, bool edgeleft1, PolyEdge *edge2, int edgey2, bool edgeleft2)
#else
miLineArcD (pGC, xorg, yorg, points, widths, edge1, edgey1, edgeleft1, edge2, edgey2, edgeleft2)
const miGC *pGC;
double xorg, yorg;
miPoint *points;
unsigned int *widths;
PolyEdge *edge1;
int edgey1;
bool edgeleft1;
PolyEdge *edge2;
int edgey2;
bool edgeleft2;
#endif
{
miPoint *pts;
unsigned int *wids;
double radius, x0, y0, el, er, yk, xlk, xrk, k;
int xbase, ybase, y, boty, xl, xr, xcl, xcr;
int ymin, ymax;
bool edge1IsMin, edge2IsMin;
int ymin1, ymin2;
pts = points;
wids = widths;
xbase = (int)(floor(xorg));
x0 = xorg - xbase;
ybase = ICEIL (yorg);
y0 = yorg - ybase;
xlk = x0 + x0 + 1.0;
xrk = x0 + x0 - 1.0;
yk = y0 + y0 - 1.0;
radius = 0.5 * ((double)pGC->lineWidth);
y = (int)(floor(radius - y0 + 1.0));
ybase -= y;
ymin = ybase;
ymax = INT_MAX;
edge1IsMin = false;
ymin1 = edgey1;
if (edge1->dy >= 0)
{
if (!edge1->dy)
{
if (edgeleft1)
edge1IsMin = true;
else
ymax = edgey1;
edgey1 = INT_MAX;
}
else
{
if ((edge1->signdx < 0) == edgeleft1)
edge1IsMin = true;
}
}
edge2IsMin = false;
ymin2 = edgey2;
if (edge2->dy >= 0)
{
if (!edge2->dy)
{
if (edgeleft2)
edge2IsMin = true;
else
ymax = edgey2;
edgey2 = INT_MAX;
}
else
{
if ((edge2->signdx < 0) == edgeleft2)
edge2IsMin = true;
}
}
if (edge1IsMin)
{
ymin = ymin1;
if (edge2IsMin && ymin1 > ymin2)
ymin = ymin2;
}
else if (edge2IsMin)
ymin = ymin2;
el = radius * radius - ((y + y0) * (y + y0)) - (x0 * x0);
er = el + xrk;
xl = 1;
xr = 0;
if (x0 < 0.5)
{
xl = 0;
el -= xlk;
}
boty = (y0 < -0.5) ? 1 : 0;
if (ybase + y - boty > ymax)
boty = ymax - ybase - y;
while (y > boty)
{
k = (y << 1) + yk;
er += k;
while (er > 0.0)
{
xr++;
er += xrk - (xr << 1);
}
el += k;
while (el >= 0.0)
{
xl--;
el += (xl << 1) - xlk;
}
y--;
ybase++;
if (ybase < ymin)
continue;
xcl = xl + xbase;
xcr = xr + xbase;
CLIPSTEPEDGE(edgey1, edge1, edgeleft1);
CLIPSTEPEDGE(edgey2, edge2, edgeleft2);
if (xcr >= xcl)
{
pts->x = xcl;
pts->y = ybase;
pts++;
*wids++ = (unsigned int)(xcr - xcl + 1);
}
}
er = xrk - (xr << 1) - er;
el = (xl << 1) - xlk - el;
boty = (int)(floor(-y0 - radius + 1.0));
if (ybase + y - boty > ymax)
boty = ymax - ybase - y;
while (y > boty)
{
k = (y << 1) + yk;
er -= k;
while ((er >= 0.0) && (xr >= 0))
{
xr--;
er += xrk - (xr << 1);
}
el -= k;
while ((el > 0.0) && (xl <= 0))
{
xl++;
el += (xl << 1) - xlk;
}
y--;
ybase++;
if (ybase < ymin)
continue;
xcl = xl + xbase;
xcr = xr + xbase;
CLIPSTEPEDGE(edgey1, edge1, edgeleft1);
CLIPSTEPEDGE(edgey2, edge2, edgeleft2);
if (xcr >= xcl)
{
pts->x = xcl;
pts->y = ybase;
pts++;
*wids++ = (unsigned int)(xcr - xcl + 1);
}
}
/* return number of spans in the Spans */
return (pts - points);
}
/* From two line faces, construct clipping edges that will be used by
miLineArcD when drawing a pie wedge. The line faces may be modified. */
static void
#ifdef _HAVE_PROTOS
miRoundJoinClip (LineFace *pLeft, LineFace *pRight, PolyEdge *edge1, PolyEdge *edge2, int *y1, int *y2, bool *left1, bool *left2)
#else
miRoundJoinClip (pLeft, pRight, edge1, edge2, y1, y2, left1, left2)
LineFace *pLeft, *pRight;
PolyEdge *edge1, *edge2;
int *y1, *y2;
bool *left1, *left2;
#endif
{
int denom;
denom = - pLeft->dx * pRight->dy + pRight->dx * pLeft->dy;
if (denom >= 0)
{
pLeft->xa = -pLeft->xa;
pLeft->ya = -pLeft->ya;
}
else
{
pRight->xa = -pRight->xa;
pRight->ya = -pRight->ya;
}
*y1 = miRoundJoinFace (pLeft, edge1, left1);
*y2 = miRoundJoinFace (pRight, edge2, left2);
}
/* helper function called by the preceding */
static int
#ifdef _HAVE_PROTOS
miRoundJoinFace (const LineFace *face, PolyEdge *edge, bool *leftEdge)
#else
miRoundJoinFace (face, edge, leftEdge)
const LineFace *face;
PolyEdge *edge;
bool *leftEdge;
#endif
{
int y;
int dx, dy;
double xa, ya;
bool left;
dx = -face->dy;
dy = face->dx;
xa = face->xa;
ya = face->ya;
left = true;
if (ya > 0)
{
ya = 0.0;
xa = 0.0;
}
if (dy < 0 || (dy == 0 && dx > 0))
{
dx = -dx;
dy = -dy;
left = (left ? false : true);
}
if (dx == 0 && dy == 0)
dy = 1;
if (dy == 0)
{
y = ICEIL (face->ya) + face->y;
edge->x = INT_MIN;
edge->stepx = 0;
edge->signdx = 0;
edge->e = -1;
edge->dy = 0;
edge->dx = 0;
edge->height = 0;
}
else
{
y = miPolyBuildEdge (xa, ya,
0.0, dx, dy,
face->x, face->y, (left ? false : true), edge);
edge->height = UINT_MAX; /* number of scanlines to process */
}
*leftEdge = (left ? false : true);
return y;
}
/* From a line face, construct a clipping edge that will be used by
miLineArcD when drawing a half-disk. */
static int
#ifdef _HAVE_PROTOS
miRoundCapClip (const LineFace *face, bool isInt, PolyEdge *edge, bool *leftEdge)
#else
miRoundCapClip (face, isInt, edge, leftEdge)
const LineFace *face;
bool isInt;
PolyEdge *edge;
bool *leftEdge;
#endif
{
int y;
int dx, dy;
double xa, ya, k;
bool left;
dx = -face->dy;
dy = face->dx;
xa = face->xa;
ya = face->ya;
k = 0.0;
if (!isInt)
k = face->k;
left = true;
if (dy < 0 || (dy == 0 && dx > 0))
{
dx = -dx;
dy = -dy;
xa = -xa;
ya = -ya;
left = (left ? false : true);
}
if (dx == 0 && dy == 0)
dy = 1;
if (dy == 0)
{
y = ICEIL (face->ya) + face->y;
edge->x = INT_MIN;
edge->stepx = 0;
edge->signdx = 0;
edge->e = -1;
edge->dy = 0;
edge->dx = 0;
edge->height = 0;
}
else
{
y = miPolyBuildEdge (xa, ya,
k, dx, dy,
face->x, face->y, (left ? false : true), edge);
edge->height = UINT_MAX; /* number of scanlines to process */
}
*leftEdge = (left ? false : true);
return y;
}
/* Paint a projecting rectangular cap on a line face. Called only by
miWideDash (with isInt = true); not by miWideLine. */
static void
#ifdef _HAVE_PROTOS
miLineProjectingCap (miPaintedSet *paintedSet, miPixel pixel, const miGC *pGC, const LineFace *face, bool isLeft, bool isInt)
#else
miLineProjectingCap (paintedSet, pixel, pGC, face, isLeft, isInt)
miPaintedSet *paintedSet;
miPixel pixel;
const miGC *pGC;
const LineFace *face;
bool isLeft;
bool isInt;
#endif
{
int xorgi = 0, yorgi = 0;
int lw;
PolyEdge lefts[2], rights[2];
int lefty, righty, topy, bottomy;
PolyEdge *left, *right;
PolyEdge *top, *bottom;
double xa,ya;
double k;
double xap, yap;
int dx, dy;
double projectXoff, projectYoff;
double maxy;
int finaly;
if (isInt)
/* in integer case, take (xorgi,yorgi) from face; otherwise (0,0) */
{
xorgi = face->x;
yorgi = face->y;
}
lw = (int)(pGC->lineWidth);
dx = face->dx;
dy = face->dy;
k = face->k;
if (dy == 0)
/* special case: line face is horizontal */
{
lefts[0].height = (unsigned int)lw;
lefts[0].x = xorgi;
if (isLeft)
lefts[0].x -= (lw >> 1);
lefts[0].stepx = 0;
lefts[0].signdx = 1;
lefts[0].e = -lw;
lefts[0].dx = 0;
lefts[0].dy = lw;
rights[0].height = (unsigned int)lw;
rights[0].x = xorgi;
if (!isLeft)
rights[0].x += ((lw + 1) >> 1);
rights[0].stepx = 0;
rights[0].signdx = 1;
rights[0].e = -lw;
rights[0].dx = 0;
rights[0].dy = lw;
/* fill the rectangle (1 left edge, 1 right edge) */
miFillPolyHelper (paintedSet, pixel,
yorgi - (lw >> 1), (unsigned int)lw,
lefts, rights, 1, 1);
}
else if (dx == 0)
/* special case: line face is vertical */
{
topy = yorgi;
bottomy = yorgi + dy;
if (isLeft)
topy -= (lw >> 1);
else
bottomy += (lw >> 1);
lefts[0].height = (unsigned int)(bottomy - topy);
lefts[0].x = xorgi - (lw >> 1);
lefts[0].stepx = 0;
lefts[0].signdx = 1;
lefts[0].e = -dy;
lefts[0].dx = dx;
lefts[0].dy = dy;
rights[0].height = (unsigned int)(bottomy - topy);
rights[0].x = lefts[0].x + (lw - 1);
rights[0].stepx = 0;
rights[0].signdx = 1;
rights[0].e = -dy;
rights[0].dx = dx;
rights[0].dy = dy;
/* fill the rectangle (1 left edge, 1 right edge) */
miFillPolyHelper (paintedSet, pixel, topy,
(unsigned int)(bottomy - topy), lefts, rights, 1, 1);
}
else
/* general case: line face is neither horizontal nor vertical */
{
xa = face->xa;
ya = face->ya;
projectXoff = -ya;
projectYoff = xa;
if (dx < 0)
{
right = &rights[1];
left = &lefts[0];
top = &rights[0];
bottom = &lefts[1];
}
else
{
right = &rights[0];
left = &lefts[1];
top = &lefts[0];
bottom = &rights[1];
}
if (isLeft)
/* cap goes left; build four edges */
{
righty = miPolyBuildEdge (xa, ya,
k, dx, dy,
xorgi, yorgi, false, right);
xa = -xa;
ya = -ya;
k = -k;
lefty = miPolyBuildEdge (xa - projectXoff, ya - projectYoff,
k, dx, dy,
xorgi, yorgi, true, left);
if (dx > 0)
{
ya = -ya;
xa = -xa;
}
xap = xa - projectXoff;
yap = ya - projectYoff;
topy = miPolyBuildEdge (xap, yap,
xap * dx + yap * dy, -dy, dx,
xorgi, yorgi, (dx > 0 ? true : false), top);
bottomy = miPolyBuildEdge (xa, ya,
0.0, -dy, dx,
xorgi, yorgi, (dx < 0 ? true : false), bottom);
maxy = -ya;
}
else
/* cap goes right; build four edges */
{
righty = miPolyBuildEdge (xa - projectXoff, ya - projectYoff,
k, dx, dy,
xorgi, yorgi, false, right);
xa = -xa;
ya = -ya;
k = -k;
lefty = miPolyBuildEdge (xa, ya,
k, dx, dy,
xorgi, yorgi, true, left);
if (dx > 0)
{
ya = -ya;
xa = -xa;
}
xap = xa - projectXoff;
yap = ya - projectYoff;
topy = miPolyBuildEdge (xa, ya,
0.0, -dy, dx,
xorgi, xorgi, (dx > 0 ? true : false), top);
bottomy = miPolyBuildEdge (xap, yap,
xap * dx + yap * dy, -dy, dx,
xorgi, xorgi, (dx < 0 ? true : false), bottom);
maxy = -ya + projectYoff;
}
finaly = ICEIL(maxy) + yorgi;
if (dx < 0)
{
left->height = (unsigned int)(bottomy - lefty);
right->height = (unsigned int)(finaly - righty);
top->height = (unsigned int)(righty - topy);
}
else
{
right->height = (unsigned int)(bottomy - righty);
left->height = (unsigned int)(finaly - lefty);
top->height = (unsigned int)(lefty - topy);
}
bottom->height = (unsigned int)(finaly - bottomy);
/* fill the rectangle (2 left edges, 2 right edges) */
miFillPolyHelper (paintedSet, pixel, topy,
(unsigned int)(bottom->height + bottomy - topy),
lefts, rights, 2, 2);
}
}
/* Draw a wide, dashed polyline, by dashing each line segment and joining
appropriately. miWideDashSegment() is called to dash each line
segment. */
void
#ifdef _HAVE_PROTOS
miWideDash (miPaintedSet *paintedSet, const miGC *pGC, miCoordMode mode, int npt, const miPoint *pPts)
#else
miWideDash (paintedSet, pGC, mode, npt, pPts)
miPaintedSet *paintedSet;
const miGC *pGC;
miCoordMode mode;
int npt;
const miPoint *pPts;
#endif
{
int x1, y1, x2, y2;
int dashNum; /* absolute number of dash, starts with 0 */
int dashIndex; /* index into array (i.e. dashNum % length) */
int dashOffset; /* offset into selected dash */
int startPaintType, endPaintType = 0, prevEndPaintType = 0;
int firstPaintType = 0; /* used only for closed polylines; will be 1 */
int numPixels;
bool selfJoin; /* polyline is closed? */
bool first; /* first line segment of polyline */
bool somethingDrawn = false;
bool projectLeft, projectRight;
LineFace leftFace, rightFace, prevRightFace;
LineFace firstFace;
miPixel pixel;
/* ensure we have >=1 points */
if (npt <= 0)
return;
/* width 0 lines are handled specially; invoke Bresenham routine in
mi_zerolin.c */
if (pGC->lineWidth == 0)
{
miZeroDash (paintedSet, pGC, mode, npt, pPts);
return;
}
x2 = pPts->x;
y2 = pPts->y;
first = true; /* first line segment of polyline */
/* determine whether polyline is closed */
selfJoin = false;
if (mode == MI_COORD_MODE_PREVIOUS)
{
int nptTmp;
const miPoint *pPtsTmp;
x1 = x2;
y1 = y2;
nptTmp = npt;
pPtsTmp = pPts + 1;
while (--nptTmp)
{
x1 += pPtsTmp->x;
y1 += pPtsTmp->y;
++pPtsTmp;
}
if (x2 == x1 && y2 == y1)
selfJoin = true;
}
else if (x2 == pPts[npt-1].x && y2 == pPts[npt-1].y)
selfJoin = true;
/* dash segments (except for the last) will not project right; and
(except for the first) will not project left */
projectLeft =
(pGC->capStyle == (int)MI_CAP_PROJECTING && !selfJoin) ? true : false;
projectRight = false;
/* perform initial offsetting into the dash sequence */
dashNum = 0; /* absolute number of dash */
dashIndex = 0; /* index into dash array */
dashOffset = 0; /* index into selected dash */
miStepDash (pGC->dashOffset, &dashNum, &dashIndex,
pGC->dash, pGC->numInDashList, &dashOffset);
/* How many paint types? (Will cycle through 0..numPixels-1, beginning
with 1, with `off' dashes defined as those with paint type #0.) */
numPixels = pGC->numPixels;
/* iterate through points, drawing a dashed segment for each line segment
of nonzero length */
while (--npt)
{
x1 = x2;
y1 = y2;
++pPts;
x2 = pPts->x;
y2 = pPts->y;
if (mode == MI_COORD_MODE_PREVIOUS)
{
x2 += x1;
y2 += y1;
}
if (x1 != x2 || y1 != y2)
/* have a line segment of nonzero length */
{
int prevDashNum, lastPaintedDashNum;
if (npt == 1 && pGC->capStyle == (int)MI_CAP_PROJECTING
&& (!selfJoin || (firstPaintType == 0)))
/* final point; and need a projecting cap here */
projectRight = true;
prevDashNum = dashNum;
/* draw dashed segment, updating dashNum, dashIndex and
dashOffset, returning faces */
miWideDashSegment (paintedSet, pGC,
&dashNum, &dashIndex, &dashOffset,
x1, y1, x2, y2,
projectLeft, projectRight, &leftFace, &rightFace);
/* determine paint types used at start and end of just-drawn
segment */
startPaintType = ((dashNum & 1) ?
0 : 1 + ((dashNum / 2) % (numPixels - 1)));
lastPaintedDashNum = (dashOffset != 0 ? dashNum : dashNum - 1);
endPaintType = ((lastPaintedDashNum & 1) ?
0 : 1 + ((dashNum / 2) % (numPixels - 1)));
/* add round cap or line join at left end of just-drawn segment;
if OnOffDash, do so only if segment began with an `on' dash */
if (pGC->lineStyle == (int)MI_LINE_DOUBLE_DASH || (startPaintType != 0))
{
pixel = pGC->pixels[startPaintType];
if (first || (pGC->lineStyle == (int)MI_LINE_ON_OFF_DASH
&& prevEndPaintType == 0))
/* draw cap at left end, unless this is first segment of a
closed polyline */
{
if (first && selfJoin)
{
firstFace = leftFace;
firstPaintType = startPaintType;
}
else if (pGC->capStyle == (int)MI_CAP_ROUND
|| pGC->capStyle == (int)MI_CAP_TRIANGULAR)
/* invoke miLineArc to draw round cap, isInt = true */
miLineArc (paintedSet, pixel, pGC,
&leftFace, (LineFace *)NULL,
(double)0.0, (double)0.0, true);
}
else
/* draw join at left end */
miLineJoin (paintedSet, pixel, pGC,
&leftFace, &prevRightFace);
}
somethingDrawn = true;
first = false;
prevRightFace = rightFace;
prevEndPaintType = endPaintType;
projectLeft = false;
}
if (npt == 1 && somethingDrawn)
/* last point of a nonempty polyline, so add line join or round cap
if appropriate, i.e. if we're doing OnOffDash and ended on an
`on' dash, or if we're doing DoubleDash */
{
if (pGC->lineStyle == (int)MI_LINE_DOUBLE_DASH || (endPaintType != 0))
{
pixel = pGC->pixels[endPaintType];
if (selfJoin && (pGC->lineStyle == (int)MI_LINE_DOUBLE_DASH
|| (firstPaintType != 0)))
/* closed, so draw a join */
miLineJoin (paintedSet, pixel, pGC,
&firstFace, &rightFace);
else
{
if (pGC->capStyle == (int)MI_CAP_ROUND
|| pGC->capStyle == (int)MI_CAP_TRIANGULAR)
/* invoke miLineArc, isInt = true, to draw a round cap */
miLineArc (paintedSet, pixel, pGC,
(LineFace *)NULL, &rightFace,
(double)0.0, (double)0.0, true);
}
}
else
/* we're doing OnOffDash, and final segment of polyline ended
with an (undrawn) `off' dash */
{
if (selfJoin && (firstPaintType != 0))
/* closed; if projecting or round caps are being used, draw
one on the first face */
{
pixel = pGC->pixels[firstPaintType];
if (pGC->capStyle == (int)MI_CAP_PROJECTING)
miLineProjectingCap (paintedSet, pixel, pGC,
&firstFace, true, true);
else if (pGC->capStyle == (int)MI_CAP_ROUND
|| pGC->capStyle == (int)MI_CAP_TRIANGULAR)
/* invoke miLineArc, isInt = true, to draw a round cap */
miLineArc (paintedSet, pixel, pGC,
&firstFace, (LineFace *)NULL,
(double)0.0, (double)0.0, true);
}
}
}
}
/* handle `all points coincident' crock, nothing yet drawn */
if (!somethingDrawn
&& (pGC->lineStyle == (int)MI_LINE_DOUBLE_DASH || !(dashNum & 1)))
{
unsigned int w1;
pixel = (dashNum & 1) ? pGC->pixels[0] : pGC->pixels[1];
switch ((int)pGC->capStyle)
{
case (int)MI_CAP_ROUND:
case (int)MI_CAP_TRIANGULAR:
/* invoke miLineArc, isInt = false, to draw a round disk */
miLineArc (paintedSet, pixel, pGC,
(LineFace *)NULL, (LineFace *)NULL,
(double)x2, (double)y2,
false);
break;
case (int)MI_CAP_PROJECTING:
/* draw a square box with edge size equal to line width */
w1 = pGC->lineWidth;
miFillRectPolyHelper (paintedSet, pixel,
(int)(x2 - (w1 >> 1)), (int)(y2 - (w1 >> 1)),
w1, w1);
break;
case (int)MI_CAP_BUTT:
default:
break;
}
}
}
#define V_TOP 0
#define V_RIGHT 1
#define V_BOTTOM 2
#define V_LEFT 3
/* Helper function, called by miWideDash(). Draw a single dashed line
segment, i.e. a sequence of dashes including a possible final incomplete
dash, and step DashNum, DashIndex and DashOffset appropriately. Also
pass back left and right faces for the line segment, for possible use in
adding caps or joins. If the LineOnOffDash line style is used, each
dash will be given a round cap if lines are drawn in the rounded cap
style, and a projecting cap if lines are drawn in the projecting cap
style. */
static void
#ifdef _HAVE_PROTOS
miWideDashSegment (miPaintedSet *paintedSet, const miGC *pGC, int *pDashNum, int *pDashIndex, int *pDashOffset, int x1, int y1, int x2, int y2, bool projectLeft, bool projectRight, LineFace *leftFace, LineFace *rightFace)
#else
miWideDashSegment (paintedSet, pGC, pDashNum, pDashIndex, pDashOffset, x1, y1, x2, y2, projectLeft, projectRight, leftFace, rightFace)
miPaintedSet *paintedSet;
const miGC *pGC;
int *pDashNum; /* absolute number of dash */
int *pDashIndex; /* index into array (i.e. dashNum % length) */
int *pDashOffset; /* offset into selected dash */
int x1, y1, x2, y2;
bool projectLeft, projectRight;
LineFace *leftFace, *rightFace;
#endif
{
int dashNum, dashIndex, dashRemain;
unsigned int *pDash;
double L, l;
double k;
PolyVertex vertices[4];
PolyVertex saveRight, saveBottom;
PolySlope slopes[4];
PolyEdge left[2], right[2];
LineFace lcapFace, rcapFace;
int nleft, nright;
unsigned int h;
int y;
int dy, dx;
double LRemain;
double r;
double rdx, rdy;
double dashDx, dashDy;
double saveK = 0.0;
bool first = true;
double lcenterx, lcentery, rcenterx = 0.0, rcentery = 0.0;
miPixel pixel;
int numPixels, paintType;
dx = x2 - x1;
dy = y2 - y1;
dashNum = *pDashNum;
dashIndex = *pDashIndex;
pDash = pGC->dash;
/* determine portion of current dash remaining (i.e. the portion after
the current offset */
dashRemain = (int)(pDash[dashIndex]) - *pDashOffset;
/* compute color of current dash */
numPixels = pGC->numPixels;
paintType = (dashNum & 1) ? 0 : 1 + ((dashNum / 2) % (numPixels - 1));
pixel = pGC->pixels[paintType];
/* compute e.g. L, the distance to go (for dashing) */
l = 0.5 * ((double) pGC->lineWidth);
if (dx == 0) /* vertical segment */
{
L = dy;
rdx = 0;
rdy = l;
if (dy < 0)
{
L = -dy;
rdy = -l;
}
}
else if (dy == 0) /* horizontal segment */
{
L = dx;
rdx = l;
rdy = 0;
if (dx < 0)
{
L = -dx;
rdx = -l;
}
}
else /* neither horizontal nor vertical */
{
L = hypot ((double) dx, (double) dy);
r = l / L; /* this is ell / L, not 1 / L */
rdx = r * dx;
rdy = r * dy;
}
k = l * L; /* this is ell * L, not 1 * L */
/* All position comments are relative to a line with dx and dy > 0,
* but the code does not depend on this. */
/* top */
slopes[V_TOP].dx = dx;
slopes[V_TOP].dy = dy;
slopes[V_TOP].k = k;
/* right */
slopes[V_RIGHT].dx = -dy;
slopes[V_RIGHT].dy = dx;
slopes[V_RIGHT].k = 0;
/* bottom */
slopes[V_BOTTOM].dx = -dx;
slopes[V_BOTTOM].dy = -dy;
slopes[V_BOTTOM].k = k;
/* left */
slopes[V_LEFT].dx = dy;
slopes[V_LEFT].dy = -dx;
slopes[V_LEFT].k = 0;
/* preload the start coordinates */
vertices[V_RIGHT].x = vertices[V_TOP].x = rdy;
vertices[V_RIGHT].y = vertices[V_TOP].y = -rdx;
vertices[V_BOTTOM].x = vertices[V_LEFT].x = -rdy;
vertices[V_BOTTOM].y = vertices[V_LEFT].y = rdx;
if (projectLeft)
/* offset the vertices appropriately */
{
vertices[V_TOP].x -= rdx;
vertices[V_TOP].y -= rdy;
vertices[V_LEFT].x -= rdx;
vertices[V_LEFT].y -= rdy;
slopes[V_LEFT].k = rdx * dx + rdy * dy;
}
/* starting point for first dash (floating point) */
lcenterx = x1;
lcentery = y1;
if (pGC->capStyle == (int)MI_CAP_ROUND
|| pGC->capStyle == (int)MI_CAP_TRIANGULAR)
/* keep track of starting face (need only in OnOff case) */
{
lcapFace.dx = dx;
lcapFace.dy = dy;
lcapFace.x = x1;
lcapFace.y = y1;
rcapFace.dx = -dx;
rcapFace.dy = -dy;
rcapFace.x = x1;
rcapFace.y = y1;
}
/* draw dashes until end of line segment is reached, and no additional
(complete) dash can be drawn */
LRemain = L;
while (LRemain > dashRemain)
{
dashDx = (dashRemain * dx) / L;
dashDy = (dashRemain * dy) / L;
/* ending point for dash */
rcenterx = lcenterx + dashDx;
rcentery = lcentery + dashDy;
vertices[V_RIGHT].x += dashDx;
vertices[V_RIGHT].y += dashDy;
vertices[V_BOTTOM].x += dashDx;
vertices[V_BOTTOM].y += dashDy;
slopes[V_RIGHT].k = vertices[V_RIGHT].x * dx + vertices[V_RIGHT].y * dy;
/* draw dash (if OnOffDash, don't draw `off' dashes) */
if (pGC->lineStyle == (int)MI_LINE_DOUBLE_DASH || !(paintType == 0))
{
if (pGC->lineStyle == (int)MI_LINE_ON_OFF_DASH &&
pGC->capStyle == (int)MI_CAP_PROJECTING)
/* will draw projecting caps, so save vertices for later use */
{
saveRight = vertices[V_RIGHT];
saveBottom = vertices[V_BOTTOM];
saveK = slopes[V_RIGHT].k;
if (!first)
{
vertices[V_TOP].x -= rdx;
vertices[V_TOP].y -= rdy;
vertices[V_LEFT].x -= rdx;
vertices[V_LEFT].y -= rdy;
slopes[V_LEFT].k = vertices[V_LEFT].x *
slopes[V_LEFT].dy -
vertices[V_LEFT].y *
slopes[V_LEFT].dx;
}
vertices[V_RIGHT].x += rdx;
vertices[V_RIGHT].y += rdy;
vertices[V_BOTTOM].x += rdx;
vertices[V_BOTTOM].y += rdy;
slopes[V_RIGHT].k = vertices[V_RIGHT].x *
slopes[V_RIGHT].dy -
vertices[V_RIGHT].y *
slopes[V_RIGHT].dx;
}
/* build lists of left and right edges for the dash, using the
just-computed array of slopes */
y = miPolyBuildPoly (vertices, slopes, 4, x1, y1,
left, right, &nleft, &nright, &h);
/* fill the dash, with either fg or bg color (alternates) */
miFillPolyHelper (paintedSet, pixel,
y, h, left, right, nleft, nright);
if (pGC->lineStyle == (int)MI_LINE_ON_OFF_DASH)
/* if doing OnOffDash, add caps if any */
{
switch ((int)pGC->capStyle)
{
case (int)MI_CAP_BUTT:
default:
break;
case (int)MI_CAP_PROJECTING:
/* use saved vertices */
vertices[V_BOTTOM] = saveBottom;
vertices[V_RIGHT] = saveRight;
slopes[V_RIGHT].k = saveK;
break;
case (int)MI_CAP_ROUND:
case (int)MI_CAP_TRIANGULAR:
if (!first)
{
if (dx < 0)
{
lcapFace.xa = -vertices[V_LEFT].x;
lcapFace.ya = -vertices[V_LEFT].y;
lcapFace.k = slopes[V_LEFT].k;
}
else
{
lcapFace.xa = vertices[V_TOP].x;
lcapFace.ya = vertices[V_TOP].y;
lcapFace.k = -slopes[V_LEFT].k;
}
/* invoke miLineArc, isInt = false, to draw half-disk
on left end of dash (only if dash is not first) */
miLineArc (paintedSet, pixel, pGC,
&lcapFace, (LineFace *) NULL,
lcenterx, lcentery, false);
}
if (dx < 0)
{
rcapFace.xa = vertices[V_BOTTOM].x;
rcapFace.ya = vertices[V_BOTTOM].y;
rcapFace.k = slopes[V_RIGHT].k;
}
else
{
rcapFace.xa = -vertices[V_RIGHT].x;
rcapFace.ya = -vertices[V_RIGHT].y;
rcapFace.k = -slopes[V_RIGHT].k;
}
/* invoke miLineArc, isInt = false, to draw half-disk on
right end of dash */
miLineArc (paintedSet, pixel, pGC,
(LineFace *)NULL, &rcapFace,
rcenterx, rcentery, false);
break;
}
}
}
/* we just drew a dash, or (in the OnOff case) we either drew a dash
or we didn't */
LRemain -= dashRemain; /* decrement float by int (distance over
which we just drew, i.e. the remainder
of current dash) */
/* bump absolute dash number, and index of dash in array (cyclically) */
++dashNum;
++dashIndex;
if (dashIndex == pGC->numInDashList)
dashIndex = 0;
dashRemain = (int)(pDash[dashIndex]); /* whole new dash now `remains' */
/* compute color of next dash */
paintType = (dashNum & 1) ? 0 : 1 + ((dashNum / 2) % (numPixels - 1));
pixel = pGC->pixels[paintType];
/* next dash will start where previous one ended */
lcenterx = rcenterx;
lcentery = rcentery;
vertices[V_TOP] = vertices[V_RIGHT];
vertices[V_LEFT] = vertices[V_BOTTOM];
slopes[V_LEFT].k = -slopes[V_RIGHT].k;
first = false; /* no longer first dash of line segment */
}
/* final portion of segment is dashed specially, with an incomplete dash */
if (pGC->lineStyle == (int)MI_LINE_DOUBLE_DASH || !(paintType == 0))
{
vertices[V_TOP].x -= dx;
vertices[V_TOP].y -= dy;
vertices[V_LEFT].x -= dx;
vertices[V_LEFT].y -= dy;
vertices[V_RIGHT].x = rdy;
vertices[V_RIGHT].y = -rdx;
vertices[V_BOTTOM].x = -rdy;
vertices[V_BOTTOM].y = rdx;
if (projectRight)
/* offset appropriately */
{
vertices[V_RIGHT].x += rdx;
vertices[V_RIGHT].y += rdy;
vertices[V_BOTTOM].x += rdx;
vertices[V_BOTTOM].y += rdy;
slopes[V_RIGHT].k = vertices[V_RIGHT].x *
slopes[V_RIGHT].dy -
vertices[V_RIGHT].y *
slopes[V_RIGHT].dx;
}
else
slopes[V_RIGHT].k = 0;
/* if OnOffDash line style and cap mode is projecting, offset the
face, so as to draw a projecting cap */
if (!first && pGC->lineStyle == (int)MI_LINE_ON_OFF_DASH
&& pGC->capStyle == (int)MI_CAP_PROJECTING)
{
vertices[V_TOP].x -= rdx;
vertices[V_TOP].y -= rdy;
vertices[V_LEFT].x -= rdx;
vertices[V_LEFT].y -= rdy;
slopes[V_LEFT].k = vertices[V_LEFT].x *
slopes[V_LEFT].dy -
vertices[V_LEFT].y *
slopes[V_LEFT].dx;
}
else
slopes[V_LEFT].k += dx * dx + dy * dy;
/* build lists of left and right edges for the final incomplete dash,
using the just-computed vertices and slopes */
y = miPolyBuildPoly (vertices, slopes, 4, x2, y2,
left, right, &nleft, &nright, &h);
/* fill the final dash */
miFillPolyHelper (paintedSet, pixel,
y, h, left, right, nleft, nright);
/* if OnOffDash line style and cap mode is round, draw a round cap */
if (!first && pGC->lineStyle == (int)MI_LINE_ON_OFF_DASH
&& (pGC->capStyle == (int)MI_CAP_ROUND
|| pGC->capStyle == (int)MI_CAP_TRIANGULAR))
{
lcapFace.x = x2;
lcapFace.y = y2;
if (dx < 0)
{
lcapFace.xa = -vertices[V_LEFT].x;
lcapFace.ya = -vertices[V_LEFT].y;
lcapFace.k = slopes[V_LEFT].k;
}
else
{
lcapFace.xa = vertices[V_TOP].x;
lcapFace.ya = vertices[V_TOP].y;
lcapFace.k = -slopes[V_LEFT].k;
}
/* invoke miLineArc, isInt = false, to draw disk on end */
miLineArc (paintedSet, pixel, pGC,
&lcapFace, (LineFace *) NULL,
rcenterx, rcentery, false);
}
}
/* work out left and right faces of the dashed segment, to pass back */
leftFace->x = x1;
leftFace->y = y1;
leftFace->dx = dx;
leftFace->dy = dy;
leftFace->xa = rdy;
leftFace->ya = -rdx;
leftFace->k = k;
rightFace->x = x2;
rightFace->y = y2;
rightFace->dx = -dx;
rightFace->dy = -dy;
rightFace->xa = -rdy;
rightFace->ya = rdx;
rightFace->k = k;
/* update absolute dash number, dash index, dash offset */
dashRemain = (int)(((double) dashRemain) - LRemain);
if (dashRemain == 0) /* on to next dash in array */
{
dashNum++; /* bump absolute dash number */
dashIndex++;
if (dashIndex == pGC->numInDashList) /* wrap */
dashIndex = 0;
dashRemain = (int)(pDash[dashIndex]);
}
*pDashNum = dashNum;
*pDashIndex = dashIndex;
*pDashOffset = (int)(pDash[dashIndex]) - dashRemain;
}
/* Helper function, called by miWideDash() above and also by miZeroPolyArc
(in mi_zerarc.c) and miZeroDash (in mi_zerolin.c) to perform initial
offsetting into the dash array, before dash #0 is drawn. In all cases,
dashNum=0, dashIndex=0 and dashOffset=0. */
/* FIXME: a negative offset is not supported, and if it is, then some
places elsewhere in the code, which assume dashNum>=0, may need to be
fixed too. */
void
#ifdef _HAVE_PROTOS
miStepDash (int dist, int *pDashNum, int *pDashIndex, const unsigned int *pDash, int numInDashList, int *pDashOffset)
#else
miStepDash (dist, pDashNum, pDashIndex, pDash, numInDashList, pDashOffset)
int dist; /* additional offset (assumed >= 0) */
int *pDashNum; /* dash number */
int *pDashIndex; /* current dash */
const unsigned int *pDash; /* dash list */
int numInDashList; /* dashlist length */
int *pDashOffset; /* offset into current dash */
#endif
{
int dashNum, dashIndex, dashOffset;
int totallen;
int i;
dashNum = *pDashNum;
dashIndex = *pDashIndex;
dashOffset = *pDashOffset;
if (dashOffset + dist < (int)(pDash[dashIndex]))
/* offset won't take us beyond end of present dash */
{
*pDashOffset = dashOffset + dist;
return;
}
/* move to next dash */
dist -= (int)(pDash[dashIndex]) - dashOffset;
dashNum++;
dashIndex++;
if (dashIndex == numInDashList)
/* wrap to beginning of dash list */
dashIndex = 0;
/* make it easy on ourselves: work modulo iteration interval */
totallen = 0;
for (i = 0; i < numInDashList; i++)
totallen += (int)(pDash[i]);
if (totallen <= dist)
dist = dist % totallen;
while (dist >= (int)(pDash[dashIndex]))
{
dist -= (int)(pDash[dashIndex]);
dashNum++;
dashIndex++;
if (dashIndex == numInDashList)
/* wrap to beginning of dash list */
dashIndex = 0;
}
*pDashNum = dashNum;
*pDashIndex = dashIndex;
*pDashOffset = dist;
}
/* Draw a wide polyline, undashed (if width=0, this simply calls
miZeroLine) */
void
#ifdef _HAVE_PROTOS
miWideLine (miPaintedSet *paintedSet, const miGC *pGC, miCoordMode mode, int npt, const miPoint *pPts)
#else
miWideLine (paintedSet, pGC, mode, npt, pPts)
miPaintedSet *paintedSet;
const miGC *pGC;
miCoordMode mode;
int npt;
const miPoint *pPts;
#endif
{
int x1, y1, x2, y2;
bool projectLeft, projectRight;
LineFace leftFace, rightFace, prevRightFace;
LineFace firstFace;
int first;
bool somethingDrawn = false;
bool selfJoin;
/* ensure we have >=1 points */
if (npt <= 0)
return;
/* width 0 lines are handled specially */
if (pGC->lineWidth == 0)
{
miZeroLine (paintedSet, pGC, mode, npt, pPts);
return;
}
x2 = pPts->x;
y2 = pPts->y;
first = true;
/* determine whether polyline is closed */
selfJoin = false;
if (npt > 1)
{
if (mode == MI_COORD_MODE_PREVIOUS)
{
int nptTmp;
const miPoint *pPtsTmp;
x1 = x2;
y1 = y2;
nptTmp = npt;
pPtsTmp = pPts + 1;
while (--nptTmp)
{
x1 += pPtsTmp->x;
y1 += pPtsTmp->y;
++pPtsTmp;
}
if (x2 == x1 && y2 == y1)
selfJoin = true;
}
else if (x2 == pPts[npt-1].x && y2 == pPts[npt-1].y)
selfJoin = true;
}
/* line segments (except for the last) will not project right; they'll
project left if the cap mode is "projecting" */
projectLeft =
(pGC->capStyle == (int)MI_CAP_PROJECTING && !selfJoin) ? true : false;
projectRight = false;
/* iterate through points, drawing all line segments of nonzero length */
while (--npt)
{
x1 = x2;
y1 = y2;
++pPts;
x2 = pPts->x;
y2 = pPts->y;
if (mode == MI_COORD_MODE_PREVIOUS)
{
x2 += x1;
y2 += y1;
}
if (x1 != x2 || y1 != y2)
/* nonzero length */
{
somethingDrawn = true;
if (npt == 1 && pGC->capStyle == (int)MI_CAP_PROJECTING && !selfJoin)
/* last point; and need a projecting cap here */
projectRight = true;
/* draw segment (pixel=1), returning faces */
miWideSegment (paintedSet, pGC->pixels[1], pGC,
x1, y1, x2, y2,
projectLeft, projectRight, &leftFace, &rightFace);
if (first)
/* first line segment, draw round cap if needed */
{
if (selfJoin)
firstFace = leftFace;
else if (pGC->capStyle == (int)MI_CAP_ROUND
|| pGC->capStyle == (int)MI_CAP_TRIANGULAR)
/* invoke miLineArc, isInt = true, to draw a round cap
on left face in paint type #1 */
miLineArc (paintedSet, pGC->pixels[1], pGC,
&leftFace, (LineFace *)NULL,
(double)0.0, (double)0.0,
true);
}
else
/* general case: draw join at beginning of segment (pixel=1) */
miLineJoin (paintedSet, pGC->pixels[1], pGC,
&leftFace, &prevRightFace);
prevRightFace = rightFace;
first = false;
projectLeft = false;
}
/* final point of polyline */
if (npt == 1 && somethingDrawn)
{
if (selfJoin)
/* add line join to close the polyline, pixel=1 */
miLineJoin (paintedSet, pGC->pixels[1], pGC,
&firstFace, &rightFace);
else if (pGC->capStyle == (int)MI_CAP_ROUND
|| pGC->capStyle == (int)MI_CAP_TRIANGULAR)
/* invoke miLineArc, isInt = true, to draw round cap
on right face, pixel=1 */
miLineArc (paintedSet, pGC->pixels[1], pGC,
(LineFace *)NULL, &rightFace,
(double)0.0, (double)0.0,
true);
}
}
/* handle crock where all points are coincident */
if (!somethingDrawn)
{
projectLeft = (pGC->capStyle == (int)MI_CAP_PROJECTING) ? true : false;
miWideSegment (paintedSet, pGC->pixels[1], pGC, /* pixel=1 */
x2, y2, x2, y2, projectLeft, projectLeft,
&leftFace, &rightFace);
if (pGC->capStyle == (int)MI_CAP_ROUND
|| pGC->capStyle == (int)MI_CAP_TRIANGULAR)
{
/* invoke miLineArc, isInt = true, to draw round cap
in paint type #1 */
miLineArc (paintedSet, pGC->pixels[1], pGC,
&leftFace, (LineFace *)NULL,
(double)0.0, (double)0.0,
true);
/* invoke miLineArc, isInt = true, to draw other round cap
in paint type #1 */
rightFace.dx = -1; /* sleazy hack to make it work */
miLineArc (paintedSet, pGC->pixels[1], pGC,
(LineFace *) NULL, &rightFace,
(double)0.0, (double)0.0,
true);
}
}
}
/* Helper function, called by miWideLine() with pixel=1. Draw a single
segment of a wide polyline, taking into account projecting caps, but not
round caps. Also pass back left and right faces for the line segment,
for possible use in adding caps or joins. */
static void
#ifdef _HAVE_PROTOS
miWideSegment (miPaintedSet *paintedSet, miPixel pixel, const miGC *pGC, int x1, int y1, int x2, int y2, bool projectLeft, bool projectRight, LineFace *leftFace, LineFace *rightFace)
#else
miWideSegment (paintedSet, pixel, pGC, x1, y1, x2, y2, projectLeft, projectRight, leftFace, rightFace)
miPaintedSet *paintedSet;
miPixel pixel;
const miGC *pGC;
int x1, y1, x2, y2;
bool projectLeft, projectRight;
LineFace *leftFace, *rightFace;
#endif
{
double l, L, r;
double xa, ya;
double projectXoff = 0.0, projectYoff = 0.0;
double k;
double maxy;
int dx, dy;
int x, y;
int finaly;
PolyEdge *left, *right;
PolyEdge *top, *bottom;
int lefty, righty, topy, bottomy;
int signdx;
PolyEdge lefts[2], rights[2];
int lw = (int)(pGC->lineWidth);
if (y2 < y1 || (y2 == y1 && x2 < x1))
/* interchange, so as always to draw top-to-bottom, or left-to-right if
horizontal */
{
int tx, ty;
bool tbool;
LineFace *tface;
tx = x1;
x1 = x2;
x2 = tx;
ty = y1;
y1 = y2;
y2 = ty;
tbool = projectLeft;
projectLeft = projectRight;
projectRight = tbool;
tface = leftFace;
leftFace = rightFace;
rightFace = tface;
}
dy = y2 - y1;
signdx = 1;
dx = x2 - x1;
if (dx < 0)
signdx = -1;
leftFace->x = x1;
leftFace->y = y1;
leftFace->dx = dx;
leftFace->dy = dy;
rightFace->x = x2;
rightFace->y = y2;
rightFace->dx = -dx; /* for faces, (dx,dy) points _into_ line */
rightFace->dy = -dy;
if (dy == 0)
/* segment is horizontal */
{
rightFace->xa = 0;
rightFace->ya = 0.5 * (double)lw;
rightFace->k = -0.5 * (double)(lw * dx); /* k = xa * dy - ya * dx */
leftFace->xa = 0;
leftFace->ya = -rightFace->ya;
leftFace->k = rightFace->k; /* k = xa * dy - ya * dx */
x = x1;
if (projectLeft)
x -= (lw >> 1);
y = y1 - (lw >> 1);
dx = x2 - x;
if (projectRight)
dx += ((lw + 1) >> 1);
dy = lw;
miFillRectPolyHelper (paintedSet, pixel,
x, y, (unsigned int)dx, (unsigned int)dy);
}
else if (dx == 0)
/* segment is vertical */
{
leftFace->xa = 0.5 * (double)lw;
leftFace->ya = 0;
leftFace->k = 0.5 * (double)(lw * dy); /* k = xa * dy - ya * dx */
rightFace->xa = -leftFace->xa;
rightFace->ya = 0;
rightFace->k = leftFace->k; /* k = xa * dy - ya * dx */
y = y1;
if (projectLeft)
y -= lw >> 1;
x = x1 - (lw >> 1);
dy = y2 - y;
if (projectRight)
dy += ((lw + 1) >> 1);
dx = lw;
miFillRectPolyHelper (paintedSet, pixel,
x, y, (unsigned int)dx, (unsigned int)dy);
}
else
/* general case: segment is neither horizontal nor vertical */
{
l = 0.5 * ((double) lw);
L = hypot ((double) dx, (double) dy);
if (dx < 0)
{
right = &rights[1];
left = &lefts[0];
top = &rights[0];
bottom = &lefts[1];
}
else
{
right = &rights[0];
left = &lefts[1];
top = &lefts[0];
bottom = &rights[1];
}
r = l / L; /* this is ell / L, not 1 / L */
ya = -r * dx;
xa = r * dy;
if (projectLeft | projectRight)
{
projectXoff = -ya;
projectYoff = xa;
}
/* build first long edge */
k = l * L; /* xa * dy - ya * dx */
leftFace->xa = xa;
leftFace->ya = ya;
leftFace->k = k;
rightFace->xa = -xa;
rightFace->ya = -ya;
rightFace->k = k;
if (projectLeft)
righty = miPolyBuildEdge (xa - projectXoff, ya - projectYoff,
k, dx, dy,
x1, y1, false, right);
else
righty = miPolyBuildEdge (xa, ya,
k, dx, dy,
x1, y1, false, right);
/* build second long edge */
ya = -ya;
xa = -xa;
k = -k; /* xa * dy - ya * dx */
if (projectLeft)
lefty = miPolyBuildEdge (xa - projectXoff, ya - projectYoff,
k, dx, dy,
x1, y1, true, left);
else
lefty = miPolyBuildEdge (xa, ya,
k, dx, dy,
x1, y1, true, left);
/* build first short edge, on left end */
if (signdx > 0)
{
ya = -ya;
xa = -xa;
}
if (projectLeft)
{
double xap = xa - projectXoff;
double yap = ya - projectYoff;
topy = miPolyBuildEdge (xap, yap,
xap * dx + yap * dy, -dy, dx,
x1, y1, (dx > 0 ? true : false), top);
}
else
topy = miPolyBuildEdge (xa, ya,
0.0, -dy, dx,
x1, y1, (dx > 0 ? true : false), top);
/* build second short edge, on right end */
if (projectRight)
{
double xap = xa + projectXoff;
double yap = ya + projectYoff;
bottomy = miPolyBuildEdge (xap, yap,
xap * dx + yap * dy, -dy, dx,
x2, y2, (dx < 0 ? true : false), bottom);
maxy = -ya + projectYoff;
}
else
{
bottomy = miPolyBuildEdge (xa, ya,
0.0, -dy, dx,
x2, y2, (dx < 0 ? true : false), bottom);
maxy = -ya;
}
finaly = ICEIL (maxy) + y2;
if (dx < 0)
{
left->height = (unsigned int)(bottomy - lefty);
right->height = (unsigned int)(finaly - righty);
top->height = (unsigned int)(righty - topy);
}
else
{
right->height = (unsigned int)(bottomy - righty);
left->height = (unsigned int)(finaly - lefty);
top->height = (unsigned int)(lefty - topy);
}
bottom->height = (unsigned int)(finaly - bottomy);
/* fill the rectangle (2 left edges, 2 right edges) */
miFillPolyHelper (paintedSet, pixel, topy,
(unsigned int)(bottom->height + bottomy - topy),
lefts, rights, 2, 2);
}
}
|