1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678
|
/*
* Copyright (C) 1997 and 1998 WIDE Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* $Id: draw.c,v 1.246 2009/02/15 11:35:19 nishida Exp $
*/
#include "mgp.h"
#ifdef USE_IMLIB
#include <Imlib2.h>
#endif
/* state associated with the window - how should we treat this? */
static struct ctrl *bg_ctl, *bg_ctl_last, *bg_ctl_cache;
static int bgindex = 0;
struct render_state cache_state;
static u_short kinsokutable[] = {
0x2121, 0x2122, 0x2123, 0x2124, 0x2125, 0x2126, 0x2127, 0x2128,
0x2129, 0x212a, 0x212b, 0x212c, 0x212d, 0x212e, 0x212f, 0x2130,
0x2133, 0x2134, 0x2135, 0x2136, 0x213c, 0x2147, 0x2149, 0x214b,
0x214d, 0x214f, 0x2151, 0x2153, 0x2155, 0x2157, 0x2159, 0x216b,
0x2242, 0x2244, 0
};
static struct pcache {
u_int flag;
u_int page;
u_int mgpflag;
u_int mode;
u_int effect;
u_int value;
} pcache;
#define COMPLEX_BGIMAGE \
(bg_ctl \
&& ((bg_ctl->ct_op == CTL_BIMAGE) \
|| bg_ctl->ct_op == CTL_BGRAD))
#define COMPLEX_BGIMAGE2 (0)
#define POSY(size) (-(int)((size)/2))
static void process_direc __P((struct render_state *, int *));
static int set_position __P((struct render_state *));
static void draw_line_output __P((struct render_state *, char *));
static void cutin __P((struct render_state *, int, int, int));
#if 0
static void shrink __P((char *, u_int));
#endif
static void draw_string __P((struct render_state *, char *));
static char *draw_fragment __P((struct render_state *, u_char *, u_int, char *, int));
static int iskinsokuchar __P((u_int));
static struct render_object *obj_alloc __P((struct render_state *state));
static void obj_free __P((struct render_state *, struct render_object *));
static int obj_new_xfont __P((struct render_state *, int, int, int,
u_int, char *));
static int obj_new_image __P((struct render_state *, int, int, Image *, int, int));
#ifdef USE_IMLIB
Imlib_Image *search_imdata __P((char *));
static int obj_new_image2 __P((struct render_state *, int, int, Image *, int, int, Imlib_Image *, int));
#endif
static int obj_new_icon __P((struct render_state *, int, int, u_int, u_int, u_long, u_int, XPoint *));
static Pixel obj_image_color __P((Image *, Image *, Pixel, int *));
static Image *obj_image_trans __P((Image *, u_int, u_int));
static void obj_draw_image __P((Drawable, u_int, u_int, struct render_object *, int));
static void obj_draw __P((struct render_state *, Drawable, u_int, u_int));
#ifdef VFLIB
static int obj_new_vfont __P((struct render_state *, int, int, struct vfont *,
int));
static u_int draw_onechar_vf __P((struct render_state *, u_int, int, int, u_int, u_int));
#endif
#ifdef FREETYPE
static u_int draw_onechar_tf __P((struct render_state *, u_int, int, int,
u_int, char *, int, int));
#endif
static char *x_fontname __P((char *, int, char *, int, char *));
static int x_parsefont __P((char *, int *, int*));
static XFontStruct *x_setfont __P((char *, u_int, char *, int *));
static u_int draw_onechar_x __P((struct render_state *, u_int, int, int, int,
char *, int));
static void back_gradation __P((struct render_state *, struct ctrl_grad *));
#if 1 /* by h.kakugawa@computer.org */
static void image_load __P((struct render_state *, char *, int, int, int, int, int, int, int, int, int));
static void image_load_ps __P((struct render_state *, char *, int, int, int, int, int, int, int, int, int));
#else
static void image_load __P((struct render_state *, char *, int, int, int, int, int, int));
static void image_load_ps __P((struct render_state *, char *, int, int, int, int, int, int));
#endif
static void process_icon __P((struct render_state *, struct ctrl *));
static void draw_bar __P((struct render_state *, struct ctrl *));
static void process_system __P((struct render_state *, struct ctrl *));
static void process_xsystem __P((struct render_state *, struct ctrl *));
static void process_tsystem __P((struct render_state *, struct ctrl *));
static Window search_child_window __P(());
static Window tsearch_child_window __P((const char *name));
static Window getNamedWindow __P((const char *name, Window top));
static void reparent_child_window __P((Window, int, int));
static char *epstoimage __P((struct render_state *, char *, int, int, int,
int, int, int));
static void image_setcolor __P((struct render_state *));
static void x_registerseed __P((struct render_state *, char *, char *));
static char *x_findseed __P((struct render_state *, char *));
static void XClearPixmap __P((Display *, Drawable));
static void cache_page __P((struct render_state *, int));
static void cache_effect1 __P((void));
static void cache_effect2 __P((void));
static void set_from_cache __P((struct render_state *));
static void pcache_process __P((int));
static void predraw __P((struct render_state *));
static void set_background_pixmap __P((struct ctrl *));
static void get_background_pixmap __P((struct ctrl *, struct render_state *));
static void regist_background_pixmap __P((XImageInfo *, Image *));
#ifdef MNG
static void process_anim __P((struct render_state *, struct ctrl *));
static void obj_draw_anim __P((struct render_state *,
u_int, u_int, struct render_object *));
#endif
static int valign = VL_BOTTOM;
#define CHECK_CACHE do {if (caching) {caching = -1; return; }} while (0)
#ifdef USE_XFT2
static void set_xrender_color __P((long, int));
static XftDraw * xft_getdraw __P((Drawable));
static char *xft_draw_fragment __P((struct render_state *,
u_char *, u_int, char *, int));
static int obj_new_xftfont __P((struct render_state *, int, int, char *,
int, char *, char *, int, int, XftFont *));
static XftFont * xft_setfont __P((char *, int, char *));
XftFont *xft_font;
XftDraw *xft_draw[2];
Drawable xft_xdraw[2];
XftColor xft_forecolor;
XRenderColor xft_render_color;
#endif
#ifdef USE_IMLIB
static void regist_zimage_position __P((struct render_object *, int, int, int, int, int));
static void clear_zimage __P((int));
static void clear_region __P((int, int, int, int));
#define ZIMAGENUM 100
static Imlib_Image *zimage[ZIMAGENUM];
static int zonzoom[ZIMAGENUM];
static int zpage[ZIMAGENUM];
static int zx[ZIMAGENUM];
static int zx[ZIMAGENUM];
static int zy[ZIMAGENUM];
static int zwidth[ZIMAGENUM];
static int zheight[ZIMAGENUM];
#endif
extern int zoomin;
static int
ispsfilename(p0)
char *p0;
{
char *p;
p = p0;
while (*p)
p++;
if (4 < p - p0 && strcasecmp(p - 4, ".eps") == 0)
return 1;
if (3 < p - p0 && strcasecmp(p - 3, ".ps") == 0)
return 1;
if (6 < p - p0 && strcasecmp(p - 6, ".idraw") == 0)
return 1;
return 0;
}
/*
* state management.
*/
void
state_goto(state, page, repaint)
struct render_state *state;
u_int page;
int repaint;
{
if (!repaint) {
purgechild(state->page);
#ifdef USE_IMLIB
clear_zimage(state->page);
#endif
}
state->page = page;
state->line = 0;
state->cp = NULL;
state->phase = P_NONE;
free_alloc_colors(&image_clr);
free_alloc_colors(&font_clr);
#ifdef COLOR_BUGFIX
colormap = XCopyColormapAndFree(display, colormap);
#endif
predraw(state);
}
void
state_next(state)
struct render_state *state;
{
switch (state->phase) {
case P_NONE:
fprintf(stderr, "internal error\n");
break;
case P_DEFAULT:
if (state->cp)
state->cp = state->cp->ct_next;
if (!state->cp) {
state->cp = page_control[state->page][state->line];
state->phase = P_PAGE;
}
break;
case P_PAGE:
if (state->cp)
state->cp = state->cp->ct_next;
if (!state->cp) {
state->line++;
state->cp = NULL;
state->phase = P_NONE;
state_init(state);
}
break;
case P_END:
/*nothing*/
break;
}
/* next page */
if (page_attribute[state->page].pg_linenum < state->line) {
if (state->page < maxpage) {
purgechild(state->page);
#ifdef USE_IMLIB
clear_zimage(state->page);
#endif
if (mgp_flag & FL_FRDCACHE &&
cached_page == state->page + 1) {
/* Hit cache */
set_from_cache(state);
pcache_process(state->page);
cache_hit = 1;
} else {
state->phase = P_NONE;
state->page++;
state->line = 0;
state_newpage(state);
state_init(state);
}
} else
state->phase = P_END;
}
}
void
state_init(state)
struct render_state *state;
{
assert(state);
if (state->phase == P_NONE || !state->cp) {
#if 0
if (!(page_attribute[state->page].pg_flag & PGFLAG_NODEF)) {
state->cp = default_control[state->line];
state->phase = P_DEFAULT;
} else
#endif
{
state->cp = page_control[state->page][state->line];
state->phase = P_PAGE;
}
}
}
void
state_newpage(state)
struct render_state *state;
{
state->ypos = 0;
state->have_mark = 0;
state->charoff = 0;
char_size[caching] = nonscaled_size[caching];
free_alloc_colors(&image_clr);
free_alloc_colors(&font_clr);
#ifdef COLOR_BUGFIX
colormap = XCopyColormapAndFree(display, colormap);
#endif
predraw(state);
}
/*
* page management.
*/
void
draw_page(state, lastcp)
struct render_state *state;
struct ctrl *lastcp;
{
u_int end_line;
int pause;
assert(state);
/* initialize the state, if required. */
if (state->phase != P_END && (state->phase == P_NONE || !state->cp)) {
state_newpage(state);
state_init(state);
}
end_line = page_attribute[state->page].pg_linenum;
while (1) {
switch (state->phase) {
case P_NONE:
fprintf(stderr, "internal error\n");
cleanup(-1);
case P_DEFAULT:
case P_PAGE:
pause = 0;
if (state->cp)
process_direc(state, &pause);
if (caching == -1) {
/* caching failed */
caching = 0;
return;
}
if (lastcp && state->cp == lastcp)
goto done;
if (pause) {
if (state->cp
&& state->cp->ct_op == CTL_PAUSE
&& state->cp->cti_value) {
goto done;
}
}
break;
case P_END:
goto done;
}
#if 0
XFlush(display);
#endif
state_next(state);
}
done:
XFlush(display);
}
Bool
draw_one(state, e)
struct render_state *state;
XEvent *e;
{
u_int end_line;
int pause;
fd_set fds;
int xfd;
struct timeval tout;
long emask;
#ifdef TTY_KEYINPUT
KeySym ks;
char c;
extern volatile int ttykey_enable;
#endif
assert(state);
/* initialize the state, if required. */
if (state->phase != P_END && (state->phase == P_NONE || !state->cp)) {
state_newpage(state);
state_init(state);
}
end_line = page_attribute[state->page].pg_linenum;
switch (state->phase) {
case P_DEFAULT:
case P_PAGE:
pause = 0;
if (state->cp)
process_direc(state, &pause);
break;
case P_END:
break;
case P_NONE:
default:
fprintf(stderr, "internal error\n");
cleanup(-1);
}
xfd = ConnectionNumber(display);
if (state->phase != P_END && !pause)
emask = xeventmask;
else
emask = ~NoEventMask;
for (;;) {
if (XCheckMaskEvent(display, emask, e) == True) {
/* we got some event in the queue*/
if (2 <= parse_debug) {
fprintf(stderr,
"interrupted and "
"got X11 event type=%d\n",
e->type);
}
got_event:
if (state->phase == P_END)
XFlush(display);
else if (!pause)
state_next(state);
return True;
}
#ifdef TTY_KEYINPUT
if (ttykey_enable) {
FD_ZERO(&fds);
FD_SET(0, &fds);
tout.tv_sec = tout.tv_usec = 0;
if (select(1, &fds, NULL, NULL, &tout) > 0
&& read(0, &c, sizeof(c)) == sizeof(c)) {
if (c > 0 && c < ' ')
ks = 0xff00 | c;
else if (c >= ' ' && c < '\177')
ks = c;
else if (c == '\177')
ks = XK_Delete;
else
continue;
e->xkey.display = display;
e->xkey.type = KeyPress;
e->xkey.keycode = XKeysymToKeycode(display, ks);
if (e->xkey.keycode == 0)
continue;
goto got_event;
}
}
#endif
if (state->phase != P_END && !pause) {
state_next(state);
return False;
}
FD_ZERO(&fds);
FD_SET(xfd, &fds);
#ifdef TTY_KEYINPUT
if (ttykey_enable)
FD_SET(0, &fds);
#endif
remapchild();
/* always cache next page */
if ((mgp_flag & FL_FRDCACHE) && cache_mode) {
#if 1/*ONOE*/
if (XCheckMaskEvent(display, emask, e) == True)
goto got_event;
#endif
cache_page(&cache_state, state->page +1);
/* check if we got some events during caching */
if (XCheckMaskEvent(display, emask, e) == True)
goto got_event;
}
/* wait for something */
tout.tv_sec = 2;
tout.tv_usec = 0;
(void)select(xfd + 1, &fds, NULL, NULL, &tout);
#ifdef TTY_KEYINPUT
if (!(mgp_flag & FL_NOSTDIN) && !ttykey_enable)
try_enable_ttykey();
#endif
/* we have no event in 2sec, so..*/
if (!FD_ISSET(xfd, &fds)) {
if ((mgp_flag & FL_FRDCACHE) && !cache_mode)
cache_page(&cache_state, state->page +1);
timebar(state);
e->type = 0;
return True;
}
}
/*NOTREACHED*/
}
static void
process_direc(state, seenpause)
struct render_state *state;
int *seenpause;
{
struct ctrl *cp;
if (seenpause)
*seenpause = 0;
cp = state->cp;
if (2 <= parse_debug) {
fprintf(stderr, "p%d/l%d: ", state->page, state->line);
debug0(cp);
}
switch(cp->ct_op) {
case CTL_SUP:
if (sup_scale > 1.0 || sup_scale < 0.1) {
sup_scale = DEFAULT_SUPSCALE;
}
if (sup_off > 1.0 || sup_scale < 0.1) {
sup_off = DEFAULT_SUPOFF;
}
state->charoff = -sup_off * nonscaled_size[caching];
char_size[caching] = (int)(nonscaled_size[caching] * sup_scale);
break;
case CTL_SUB:
if (sup_scale > 1.0 || sup_scale < 0.1) {
sup_scale = DEFAULT_SUPSCALE;
}
if (sub_off > 1.0 || sub_off < 0.1){
sub_off = DEFAULT_SUBOFF;
}
state->charoff = sub_off * nonscaled_size[caching];
char_size[caching] = (int)(nonscaled_size[caching] * sup_scale);
break;
case CTL_SETSUP:
if (cp->cti3_value1 > 100 || cp->cti3_value1 < 10){
sup_off = DEFAULT_SUPOFF;
} else {
sup_off = cp->cti3_value1 / 100.;
}
if (cp->cti3_value2 > 100 || cp->cti3_value2 < 10){
sub_off = DEFAULT_SUBOFF;
} else {
sub_off = cp->cti3_value2 / 100.;
}
if (cp->cti3_value3 > 100 || cp->cti3_value3 < 10){
sup_scale = DEFAULT_SUPSCALE;
} else {
sup_scale = cp->cti3_value3 / 100.;
}
break;
case CTL_SIZE:
nonscaled_size[caching] = state->height * cp->ctf_value / 100;
char_size[caching] = nonscaled_size[caching];
#ifdef FREETYPE
tfc_setsize(char_size[caching]);
#endif
break;
case CTL_VGAP:
vert_gap[caching] = cp->cti_value;
break;
case CTL_HGAP:
horiz_gap[caching] = cp->cti_value;
break;
case CTL_GAP:
vert_gap[caching] = horiz_gap[caching] = cp->cti_value;
break;
case CTL_QUALITY:
if (!quality_flag)
b_quality[caching] = cp->cti_value;
break;
case CTL_PAUSE:
CHECK_CACHE;
if (seenpause)
*seenpause = 1;
break;
case CTL_AGAIN:
CHECK_CACHE;
if (state->have_mark)
state->ypos = state->mark_ypos;
state->have_mark = 0;
break;
case CTL_FORE:
fore_color[caching] = cp->ctl_value;
#ifdef USE_M17N
if (! (mgp_flag & FL_NOM17N))
{
M17N_set_color (cp->ctl_value);
break;
}
#endif
XSetForeground(display, gcfore, fore_color[caching]);
break;
case CTL_BACK:
if (state->line){
fprintf(stderr, "warning: %%back directive should be put in the first line of the page. ignored.\n");
break;
}
back_color[caching] = cp->ctl_value;
bg_ctl = cp; /*update later*/
break;
case CTL_CCOLOR:
ctrl_color[caching] = cp->ctl_value;
break;
case CTL_CENTER:
state->align = AL_CENTER;
break;
case CTL_LEFT:
state->align = AL_LEFT;
break;
case CTL_LEFTFILL:
state->align = AL_LEFTFILL0;
break;
case CTL_RIGHT:
state->align = AL_RIGHT;
break;
case CTL_CONT:
state->charoff = 0;
char_size[caching] = nonscaled_size[caching];
break;
#ifdef VFLIB
case CTL_VFONT:
vfc_setfont(cp->ctc_value);
break;
#endif /*VFLIB*/
#ifdef FREETYPE
case CTL_TFONT:
tfc_setfont(cp->ctc2_value1, 0, cp->ctc2_value2);
break;
case CTL_TMFONT:
tfc_setfont(cp->ctc_value, 1, NULL);
break;
#endif /*FREETYPE*/
case CTL_XFONT2:
#ifdef USE_M17N
if (! (mgp_flag & FL_NOM17N))
{
M17N_set_font (cp->ctc2_value1, cp->ctc2_value2);
break;
}
#endif
x_registerseed(state, cp->ctc2_value1, cp->ctc2_value2);
break;
case CTL_BAR:
draw_bar(state, cp);
break;
case CTL_IMAGE:
{
if (state->align == AL_LEFTFILL0) {
state->align = AL_LEFTFILL1;
state->leftfillpos = state->linewidth;
}
/* quickhack for postscript */
if (ispsfilename(cp->ctm_fname)) {
#if 1 /* by h.kakugawa@computer.org */
image_load_ps(state, cp->ctm_fname, cp->ctm_numcolor,
cp->ctm_ximagesize, cp->ctm_yimagesize, 0,
cp->ctm_zoomflag, 0, cp->ctm_raise, cp->ctm_rotate, cp->ctm_zoomonclk);
#else
image_load_ps(state, cp->ctm_fname, cp->ctm_numcolor,
cp->ctm_ximagesize, cp->ctm_yimagesize, 0,
cp->ctm_zoomflag, 0);
#endif
} else {
#if 1 /* by h.kakugawa@computer.org */
image_load(state, cp->ctm_fname, cp->ctm_numcolor,
cp->ctm_ximagesize, cp->ctm_yimagesize, 0,
cp->ctm_zoomflag, 0, cp->ctm_raise, cp->ctm_rotate, cp->ctm_zoomonclk);
#else
image_load(state, cp->ctm_fname, cp->ctm_numcolor,
cp->ctm_ximagesize, cp->ctm_yimagesize, 0,
cp->ctm_zoomflag, 0);
#endif
}
state->brankline = 0;
}
break;
case CTL_BIMAGE:
if (mgp_flag & FL_BIMAGE)
break;
bg_ctl = cp; /*update later*/
break;
case CTL_BGRAD:
if (mgp_flag & FL_BIMAGE)
break;
bg_ctl = cp; /*update later*/
break;
case CTL_LCUTIN:
CHECK_CACHE;
state->special = SP_LCUTIN;
break;
case CTL_RCUTIN:
CHECK_CACHE;
state->special = SP_RCUTIN;
break;
case CTL_SHRINK:
CHECK_CACHE;
state->special = SP_SHRINK;
break;
case CTL_PREFIX:
state->curprefix = cp->ctc_value;
break;
case CTL_PREFIXN:
state->xprefix = state->width * cp->ctf_value / 100;
break;
case CTL_TABPREFIX:
state->tabprefix = cp->ctc_value;
break;
case CTL_TABPREFIXN:
state->tabxprefix = state->width * cp->ctf_value / 100;
break;
case CTL_PREFIXPOS:
{
char *p;
p = (state->tabprefix) ? state->tabprefix : state->curprefix;
if (!p)
break;
#ifdef USE_M17N
if (! (mgp_flag & FL_NOM17N))
{
cp->ct_op = CTL_TEXT;
cp->ctc_value = p;
M17N_draw_string (state, cp);
break;
}
#endif
draw_line_output(state, p);
break;
}
case CTL_TEXT:
if (!cp->ctc_value)
break;
if (state->align == AL_LEFTFILL0) {
state->align = AL_LEFTFILL1;
state->leftfillpos = state->linewidth;
}
#ifdef USE_M17N
if (! (mgp_flag & FL_NOM17N))
{
M17N_draw_string (state, cp);
break;
}
#endif
draw_line_output(state, cp->ctc_value);
break;
case CTL_LINESTART:
state->charoff = 0;
char_size[caching] = nonscaled_size[caching];
if (state->line == 0) {
/*
* set background of target
*/
if (bg_ctl) {
if (!caching){
/* target is window, so we need care bg_ctl_last */
if (bg_ctl_last && !ctlcmp(bg_ctl, bg_ctl_last)){
/* same as last time, we do nothing */
;
} else {
/* we have to change background */
get_background_pixmap(bg_ctl, state);
/* set window background */
set_background_pixmap(bg_ctl);
bg_ctl_last = bg_ctl;
}
XClearWindow(display, state->target);
} else {
get_background_pixmap(bg_ctl, state);
bg_ctl_cache = bg_ctl;
XClearPixmap(display, state->target);
}
} else {
if (!caching)
XClearWindow(display, state->target);
else
XClearPixmap(display, state->target);
}
if (t_fin)
timebar(state);
}
draw_line_start(state);
break;
case CTL_LINEEND:
/* blank lines */
if (state->brankline) { /*XXX*/
state->max_lineascent = char_size[caching];
state->maxascent = char_size[caching];
state->maxdescent = VERT_GAP(char_size[caching]);
}
draw_line_end(state);
/* reset single-line oriented state */
state->tabprefix = NULL;
state->tabxprefix = 0;
state->special = 0;
if (state->align == AL_LEFTFILL1) {
state->align = AL_LEFTFILL0;
state->leftfillpos = 0;
}
break;
case CTL_MARK:
state->have_mark = 1;
state->mark_ypos = state->ypos;
break;
case CTL_SYSTEM:
CHECK_CACHE;
process_system(state, cp);
break;
case CTL_XSYSTEM:
CHECK_CACHE;
process_xsystem(state, cp);
break;
case CTL_TSYSTEM:
CHECK_CACHE;
process_tsystem(state, cp);
break;
case CTL_ICON:
process_icon(state, cp);
break;
#ifdef VFLIB
case CTL_VFCAP:
vfcap_name = cp->ctc_value;
break;
#endif
#ifdef FREETYPE
case CTL_TFDIR:
freetypefontdir = cp->ctc_value;
break;
case CTL_TFONT0:
freetypefont0 = cp->ctc_value;
break;
case CTL_TMFONT0:
freetypemfont0 = cp->ctc_value;
break;
#endif
case CTL_NOOP:
case CTL_NODEF:
break;
case CTL_XFONT:
/* obsolete directives */
fprintf(stderr, "internal error: obsolete directive "
"\"%s\"\n", ctl_words[cp->ct_op].ctl_string);
exit(1);
/*NOTREACHED*/
case CTL_PCACHE:
if (!caching) {
if (cp->ctch_flag)
mgp_flag |= FL_FRDCACHE;
else
mgp_flag ^= FL_FRDCACHE;
cache_mode = cp->ctch_mode;
cache_effect = cp->ctch_effect;
cache_value = cp->ctch_value;
} else {
pcache.flag = 1;
pcache.page = state->page;
pcache.mgpflag = cp->ctch_flag;
pcache.mode = cp->ctch_mode;
pcache.effect = cp->ctch_effect;
pcache.value = cp->ctch_value;
}
break;
case CTL_CHARSET:
if (get_regid(cp->ctc_value) < 0){
fprintf(stderr, "invalid charset \"%s\". ignored\n",
cp->ctc_value);
break;
}
strcpy(mgp_charset, cp->ctc_value);
break;
#ifdef MNG
case CTL_ANIM:
if (state->align == AL_LEFTFILL0) {
state->align = AL_LEFTFILL1;
state->leftfillpos = state->linewidth;
}
process_anim(state, cp);
break;
#endif
case CTL_VALIGN:
valign = cp->cti_value;
break;
case CTL_AREA:
state->width = window_width * cp->ctar_width / 100;
state->height = window_height * cp->ctar_height / 100;
state->xoff = window_width * cp->ctar_xoff / 100;
state->yoff = window_height * cp->ctar_yoff / 100;
state->ypos = 0;
break;
case CTL_OPAQUE:
#ifdef USE_XFT2
if (cp->cti_value > 100){
fprintf(stderr, "%%opaque: value should be 0-100\n");
cp->cti_value = 100;
}
state->opaque = cp->cti_value;
if (mgp_flag & FL_NOXFT && verbose){
fprintf(stderr, "ignored %%opaque.\n");
}
#else
printf("this mgp cannot use %%opaque, needs to be built with xft2\n");
#endif
break;
case CTL_M17N:
#ifdef USE_M17N
M17N_process_direc(cp->ctc2_value1, cp->ctc2_value2);
#else
fprintf(stderr, "this mgp cannot use %%m17n, needs to be built with m17n-lib\n");
#endif
break;
case CTL_PSFONT:
break;
default:
fprintf(stderr,
"undefined directive %d at page %d line %d:\n\t",
cp->ct_op, state->page, state->line);
debug0(cp);
break;
}
}
/*
* line management.
*/
static int
set_position(state)
struct render_state *state;
{
int x;
x = 0;
switch (state->align) {
case AL_CENTER:
x = (state->width - state->linewidth)/ 2;
break;
case AL_LEFT:
case AL_LEFTFILL0:
case AL_LEFTFILL1:
x = 0;
break;
case AL_RIGHT:
x = state->width - state->linewidth;
break;
}
return x;
}
void
draw_line_start(state)
struct render_state *state;
{
struct render_object *obj;
state->max_lineascent = 0;
state->max_linedescent = 0;
state->maxascent = 0;
state->maxdescent = 0;
state->linewidth = 0;
state->brankline = 1;
while ((obj = state->obj))
obj_free(state, obj);
}
void
draw_line_itemsize(state, ascent, descent, flheight)
struct render_state *state;
int ascent;
int descent;
int flheight;
{
ascent -= state->charoff;
descent += state->charoff;
if (ascent > state->maxascent)
state->maxascent = ascent;
if (descent > state->maxdescent)
state->maxdescent = descent;
/*
* calculation for the height of a line should ignore
* character offset
*/
if (state->charoff == 0) {
if (ascent > state->max_lineascent)
state->max_lineascent = ascent;
if (descent > state->max_linedescent)
state->max_linedescent = descent;
}
if (flheight > state->maxflheight)
state->maxflheight = flheight;
}
static void
draw_line_output(state, data)
struct render_state *state;
char *data;
{
draw_string(state, data);
}
void
draw_line_end(state)
struct render_state *state;
{
int xpos;
xpos = set_position(state);
/* process the special attribute. */
switch (state->special) {
#if 0
case SP_SHRINK:
shrink(data, page, xpos);
break;
#endif
case SP_LCUTIN:
cutin(state, xpos, state->ypos, 1);
break;
case SP_RCUTIN:
cutin(state, xpos, state->ypos, -1);
break;
default:
break;
}
if (state->obj) {
obj_draw(state, state->target, xpos, state->ypos);
while (state->obj)
obj_free(state, state->obj);
}
state->ypos += state->max_lineascent;
/*
* we should ignore height of images to calculate line gap.
* suggested by Toru Terao
*/
if (VERT_GAP(char_size[caching]) < state->max_linedescent)
state->ypos += state->max_linedescent;
else
state->ypos += VERT_GAP(char_size[caching]);
state->ypos += 2;
}
#define min(x, y) (x < y ? x: y)
static void
cutin(state, lx, ly, dir)
struct render_state *state;
int lx;
int ly;
int dir;
{
u_int step, x, xoff, yoff;
int i, sx, round;
int root_x, root_y, use_copy;
Window cutinWin, junkwin;
XImage *copywin;
static XWindowAttributes xa;
XWindowAttributes wa;
Pixmap ghostWin;
GC saveGC = gc_cache;
XGetWindowAttributes(display, window, &wa);
ghostWin = XCreatePixmap(display, window, wa.width, wa.height, wa.depth);
/* all drawing should be done on the image */
gc_cache = XCreateGC(display, ghostWin, 0, 0);
XCopyArea(display, state->target, ghostWin, gc_cache,
0, 0, wa.width, wa.height, 0, 0);
if (state->repaint)
return;
if (!state->linewidth)
return;
if (!xa.width)
XGetWindowAttributes(display, DefaultRootWindow(display), &xa);
XTranslateCoordinates(display, window, DefaultRootWindow(display),
0, 0, &root_x ,&root_y, &junkwin);
use_copy = 1;
if ((root_x + window_width > xa.width) || (root_y + window_height > xa.height) ||
(root_x < 0 || root_y < 0)) use_copy = 1;
sx = (0 < dir) ? 0 : state->width - state->linewidth;
round = 20; /*XXX*/
#ifndef abs
#define abs(a) (((a) < 0) ? -(a) : (a))
#endif
if (abs(lx - sx) < round){
round = abs(lx - sx);
if (!round) round = 1;
}
step = (lx - sx) / round;
if (!use_copy){
cutinWin = XCreateSimpleWindow(display, state->target,
sx, ly, state->linewidth, state->maxascent + state->maxdescent,
0, fore_color[caching], back_color[caching]);
XSetWindowBackgroundPixmap(display, cutinWin, None);
XMapSubwindows(display, state->target);
} else {
copywin = XGetImage(display, window, state->xoff + min(sx, lx), ly + state->yoff, state->linewidth + abs(lx - sx),
state->maxascent + state->maxdescent, AllPlanes, ZPixmap);
}
xoff = state->xoff;
yoff = state->yoff;
state->xoff = state->yoff = 0;
if (state->obj && !use_copy) {
obj_draw(state, cutinWin, 0, 0);
}
XFlush(display);
x = sx;
for (i = 0; i < round; i++) {
if (use_copy && state->obj) {
obj_draw(state, ghostWin, x + xoff, ly + yoff);
XCopyArea(display, ghostWin, state->target,
saveGC,
xoff + min(sx, lx),
ly + yoff,
state->linewidth + abs(lx - sx),
state->maxascent + state->maxdescent,
xoff + min(sx, lx),
ly + yoff);
} else
XMoveWindow(display, cutinWin, x + xoff, ly + yoff);
XFlush(display);
usleep(CUTIN_DELAY);
if (use_copy && state->obj) {
XPutImage(display, ghostWin, gc_cache, copywin,
x - min(sx, lx) , 0, x + xoff, ly + yoff,
state->linewidth, state->maxascent + state->maxdescent);
}
x = sx + ((i+1)*(lx - sx)) / round;
}
XCopyArea(display, ghostWin, state->target, saveGC,
0, 0, wa.width, wa.height, 0, 0);
if (!use_copy) XDestroyWindow(display, cutinWin);
state->xoff = xoff;
state->yoff = yoff;
/* freeing images */
if(use_copy) XFree(copywin);
/* restoring tho old GC */
XFreeGC(display, gc_cache);
XFreePixmap(display, ghostWin);
gc_cache = saveGC;
}
#if 0
static void
shrink(data, page)
char *data;
u_int page;
{
u_int min_csize = char_size;
u_int max_csize = state->height / 4;
u_int csize, i, x;
u_int step = (max_csize - min_csize) / 3;
if (!step)
step = 1;
if (state->align != AL_CENTER) {
fprintf(stderr, "align is not center: \n");
return;
}
csize = char_size;
for (i = max_csize; i > min_csize; i -= step) {
char_size = i;
draw_string(state, data);
x = (state->width - state->linewidth) / 2;
XCopyArea(display, maskpix, state->target, gc,
0, 0, state->linewidth, char_size, x, state->ypos);
XCopyArea(display, pixmap, state->target, gcor,
0, 0, state->linewidth, char_size, x, state->ypos);
XFlush(display);
usleep(SHRINK_DELAY);
XFillRectangle(display, pixmap, gcall,
0, 0, state->width, char_size);
XFillRectangle(display, maskpix, gcall,
0, 0, state->width, char_size);
XClearArea(display, state->target, x, state->ypos,
state->linewidth, char_size, 0);
}
char_size = csize;
}
#endif
/*
* render characters.
*/
static void
draw_string(state, data)
struct render_state *state;
char *data;
{
u_char *p, *q;
char *registry = NULL;
u_int code2;
static char *rtab96[] = {
NULL, /* ESC - @ */
"iso8859-1", /* ESC - A */
"iso8859-2", /* ESC - B */
"iso8859-3", /* ESC - C */
"iso8859-4", /* ESC - D */
};
#define RTAB96_MAX (sizeof(rtab96)/sizeof(rtab96[0]))
static char *rtab9494[] = {
"jisx0208.1978-*", /* ESC $ @ or ESC $ ( @ */
"gb2312.1980-*", /* ESC $ A or ESC $ ( A */
"jisx0208.1983-*", /* ESC $ B or ESC $ ( B */
"ksc5601.1987-*", /* ESC $ ( C */
NULL, /* D */
NULL, /* E */
NULL, /* F */
NULL, /* G */
NULL, /* H */
NULL, /* I */
NULL, /* J */
NULL, /* K */
NULL, /* L */
NULL, /* M */
NULL, /* N */
"jisx0213.2000-1", /* ESC $ ( O */
"jisx0213.2000-2", /* ESC $ ( P */
};
#define RTAB9494_MAX (sizeof(rtab9494)/sizeof(rtab9494[0]))
int charset16 = 0;
p = (u_char *)data;
while (*p && *p != '\n') {
/* 94x94 charset */
if (p[0] == 0x1b && p[1] == '$' &&
'@' <= p[2] && p[2] < 'C' && rtab9494[p[2] - '@']) {
registry = rtab9494[p[2] - '@'];
charset16 = 1;
p += 3;
continue;
}
if (p[0] == 0x1b && p[1] == '$' && p[2] == '(' &&
'@' <= p[3] && p[3] < '@' + RTAB9494_MAX &&
rtab9494[p[3] - '@']) {
registry = rtab9494[p[3] - '@'];
charset16 = 1;
p += 4;
continue;
}
/* ascii (or JIS roman) */
if (p[0] == 0x1b && p[1] == '(' &&
(p[2] == 'B' || p[2] == 'J')) {
registry = NULL;
charset16 = 0;
p += 3;
continue;
}
/* 96 charset */
if (p[0] == 0x1b && p[1] == '-' &&
'@' < p[2] && p[2] < '@' + RTAB96_MAX &&
rtab96[p[2] - '@']) {
registry = rtab96[p[2] - '@'];
charset16 = 0;
p += 3;
continue;
}
if (!registry && isspace(p[0])) {
draw_fragment(state, p, 1, registry, 0);
p++;
continue;
}
if (charset16) {
for (q = p + 2; 0x21 <= *q && *q <= 0x7e; q += 2) {
code2 = q[0] * 256 + q[1];
if (strncmp(registry, "jisx0208", 8) == 0
&& !iskinsokuchar(code2)) {
break;
}
}
} else {
q = p;
while (*q && isprint(*q) && !isspace(*q))
q++;
if (q == p)
q++;
else {
/*
* append spaces to the end of the word.
* fragments in the following line:
* "this is test"
* are:
* "this_" "is_" "test"
*/
while (*q && isspace(*q))
q++;
}
}
q = draw_fragment(state, p, q - p, registry, charset16);
p = q;
}
}
static char *
draw_fragment(state, p, len, registry, charset16)
struct render_state *state;
u_char *p;
u_int len;
char *registry;
int charset16; /*2-octet charset?*/
{
u_int char_len, i;
u_short code;
struct render_object *tail;
struct render_object *thisline;
struct render_object *thislineend;
u_int startwidth;
struct render_state backup0, backup;
enum { MODE_UNKNOWN, MODE_X, MODE_VFLIB, MODE_FREETYPE }
mode = MODE_UNKNOWN;
char *p0;
#ifdef USE_XFT2
if (!(mgp_flag & FL_NOXFT)){
p0 = xft_draw_fragment(state, p, len, registry, charset16);
if (p0) return p0;
}
#endif
if (state->obj)
tail = state->objlast;
else
tail = NULL;
startwidth = state->linewidth;
while (len) {
code = charset16 ? p[0] * 256 + p[1] : p[0];
if (code != ' ')
state->brankline = 0; /* This isn't brankline */
#if 0
if (code == ' ') {
char_len = char_size[caching] / 2;
p++;
len--;
state->linewidth += HORIZ_STEP(char_size[caching], char_len);
continue;
}
#endif
if (code == '\t') {
char_len = char_size[caching] / 2;
p++;
len--;
char_len = HORIZ_STEP(char_size[caching], char_len) * 8;/*XXX*/
state->linewidth = (state->linewidth + char_len) / char_len * char_len;
continue;
}
/*
* decide which font to use.
* Japanese font:
* VFlib - optional
* then X.
* Western font:
* If possible, freetype. (in the future) - optional
* X if truely scalable.
* VFlib if it is larger than some size - optional
* otherwise, X.
*/
mode = MODE_UNKNOWN;
if (charset16) {
#ifdef VFLIB
if (!(mgp_flag & FL_NOVFLIB)
&& strncmp(registry, "jisx0208.1983-", 14) == 0)
mode = MODE_VFLIB;
#endif
#ifdef FREETYPE_CHARSET16
if (!(mgp_flag & FL_NOFREETYPE)
&& (strncmp(registry, "jisx0208.1983-", 14) == 0 ||
strncmp(registry, "jisx0213.2000-", 14) == 0)) {
if (tfc_get(code, char_size[caching], 1, registry,
charset16)){
mode = MODE_FREETYPE;
}
}
#endif
if (mode == MODE_UNKNOWN)
mode = MODE_X;
} else {
#ifdef FREETYPE
if (!(mgp_flag & FL_NOFREETYPE)) {
if (tfc_get(code, char_size[caching], 1, registry,
charset16)) {
mode = MODE_FREETYPE;
}
}
#endif
if (mode == MODE_UNKNOWN) {
/*
* if we can have X font that is exactly
* matches the required size, we use that.
*/
XFontStruct *xfontstruct;
int ts;
xfontstruct = x_setfont(
x_findseed(state, registry),
char_size[caching], registry, &ts);
if (ts)
mode = MODE_X;
}
#ifdef VFLIB
# ifdef USE_XDRAWSTRING_ONLY_SMALL
if (!(mgp_flag & FL_NOVFLIB) && mode == MODE_UNKNOWN) {
if (25 < char_size)
mode = MODE_VFLIB;
}
# endif /* USE_XDRAWSTRING_ONLY_SMALL */
#endif
/* last resort: use X font. */
if (mode == MODE_UNKNOWN)
mode = MODE_X;
}
/* back it up before drawing anything */
memcpy(&backup0, state, sizeof(struct render_state));
switch (mode) {
#ifdef VFLIB
case MODE_VFLIB:
char_len = draw_onechar_vf(state, code,
state->linewidth, state->charoff,
registry ? char_size[caching]
: (char_size[caching] * 4 / 5), /*XXX*/
char_size[caching]);
break;
#endif
#ifdef FREETYPE
case MODE_FREETYPE:
/*
* NOTE: width and height parameter (4th and 5th)
* are meaningless for FreeType, since we use
* metric info derived from TrueType font file.
*/
char_len = draw_onechar_tf(state, code,
state->linewidth, state->charoff,
char_size[caching], registry,
(len == (charset16 ? 2 : 1)) ? 1 : 0,
charset16);
break;
#endif
default:
fprintf(stderr, "invalid drawing mode %d for %04x "
"- fallback to X11\n", mode, code);
/* fall through */
case MODE_UNKNOWN:
case MODE_X:
char_len = draw_onechar_x(state, code,
state->linewidth, state->charoff, char_size[caching],
registry, (len == (charset16 ? 2 : 1)) ? 1 : 0);
if (char_len == 0) {
fprintf(stderr, "can't load font size %d "
"(nor font in similar size) for "
"font <%s:%d:%s>, glyph 0x%04x\n",
char_size[caching], x_findseed(state, registry),
char_size[caching], registry?registry:"NULL", code);
}
break;
}
p += (charset16 ? 2 : 1);
len -= (charset16 ? 2 : 1);
state->linewidth += HORIZ_STEP(char_size[caching], char_len);
/* ukai */
if (!charset16 && state->linewidth + HORIZ_STEP(char_size[caching],
char_len) > state->width) {
if (len >= 20) break; /* too long word */
for (i = 0; i < len; i ++){
if (isspace(*(p +i))) break;
}
if (i == len) break;
}
}
if (state->width - state->leftfillpos / 2 < state->linewidth
#if 0
&& state->align == AL_LEFTFILL1
#endif
) {
memcpy(&backup, state, sizeof(struct render_state));
/* strip off the last fragment we wrote. */
if (tail) {
thisline = tail->next;
thislineend = state->objlast;
tail->next = NULL;
state->objlast = tail;
state->maxascent = backup0.maxascent;
state->maxdescent = backup0.maxdescent;
} else {
thisline = state->obj;
thislineend = state->objlast;
state->obj = state->objlast = NULL;
state->maxascent = backup0.maxascent;
state->maxdescent = backup0.maxdescent;
}
#if 0
state->align = AL_LEFT;
#endif
state->linewidth = startwidth;
draw_line_end(state); /* flush the line. */
/* start the new line with the last fragment we wrote. */
draw_line_start(state);
state->linewidth = state->leftfillpos;
state->linewidth += (backup.linewidth - startwidth);
if (state->obj && state->objlast)
state->objlast->next = thisline;
else
state->obj = thisline;
state->objlast = thislineend;
state->align = backup.align;
/* fix up x position and maxascent. */
for (tail = state->obj; tail; tail = tail->next) {
tail->x -= startwidth;
tail->x += state->leftfillpos;
draw_line_itemsize(state, tail->ascent, tail->descent, 0);
}
}
return p;
}
static int
iskinsokuchar(code)
u_int code;
{
u_short *kinsoku;
for (kinsoku = kinsokutable; *kinsoku; kinsoku++) {
if (code == *kinsoku)
return 1;
}
return 0;
}
static struct render_object *
obj_alloc(state)
struct render_state *state;
{
struct render_object *obj;
obj = malloc(sizeof(*obj));
if (obj == NULL)
return NULL;
obj->next = NULL;
if (state->obj == NULL)
state->obj = obj;
else
state->objlast->next = obj;
state->objlast = obj;
return obj;
}
static void
obj_free(state, obj)
struct render_state *state;
struct render_object *obj;
{
struct render_object *o;
if (state->obj == obj)
state->obj = obj->next;
else {
for (o = state->obj; o; o = o->next)
if (o->next == obj)
break;
/* ASSERT(o != NULL); */
o->next = obj->next;
}
if (state->objlast == obj)
state->objlast = obj->next;
switch (obj->type) {
#ifdef VFLIB
case O_VFONT:
obj->data.vfc->ref--;
break;
#endif /* VFLIB */
#ifdef FREETYPE
case O_TFONT:
obj->data.tfc->ref--;
break;
#endif /* FREETYPE */
case O_IMAGE:
freeImage(obj->data.image.image);
break;
case O_XFONT:
free(obj->data.xfont.xfont);
break;
case O_ICON:
if (obj->data.icon.xpoint)
free(obj->data.icon.xpoint);
break;
#ifdef USE_XFT2
case O_XTFONT:
if (obj->data.xftfont.data)
free(obj->data.xftfont.data);
if (obj->data.xftfont.fontname)
free(obj->data.xftfont.fontname);
if (obj->data.xftfont.registry)
free(obj->data.xftfont.registry);
break;
#endif
#ifdef USE_M17N
case O_M17NTEXT:
/* XXX we need to add free function for mtext data here! */
m17n_object_unref(obj->data.m17ntext.mt);
break;
#endif
#ifdef MNG
case O_ANIM:
break;
#endif /* MNG */
}
free(obj);
}
#ifdef VFLIB
static int
obj_new_vfont(state, x, y, vfc, size)
struct render_state *state;
int x, y;
struct vfont *vfc;
int size;
{
struct render_object *obj;
obj = obj_alloc(state);
if (obj == NULL)
return 0;
obj->x = x;
obj->y = y;
obj->fore = fore_color[caching];
obj->type = O_VFONT;
obj->data.vfc = vfc;
obj->data.vfc->size = size;
obj->ascent = obj->data.vfc->ascent - y;
obj->descent = obj->data.vfc->descent + y;
obj->vertloc = VL_BASE;
vfc->ref++;
return 1;
}
#endif /* VFLIB */
#ifdef FREETYPE
static int
obj_new_tfont(state, x, y, tfc)
struct render_state *state;
int x, y;
struct tfont *tfc;
{
struct render_object *obj;
obj = obj_alloc(state);
if (obj == NULL)
return 0;
obj->x = x;
obj->y = y;
obj->fore = fore_color[caching];
obj->type = O_TFONT;
obj->data.tfc = tfc;
obj->ascent = obj->data.tfc->ascent - y;
obj->descent = obj->data.tfc->descent + y;
obj->vertloc = VL_BASE;
tfc->ref++;
return 1;
}
#endif /* FREETYPE */
static int
obj_new_xfont(state, x, y, size, code, registry)
struct render_state *state;
int x, y;
int size;
u_int code;
char *registry;
{
struct render_object *obj;
obj = obj_alloc(state);
if (obj == NULL)
return 0;
obj->x = x;
obj->y = y;
obj->fore = fore_color[caching];
obj->type = O_XFONT;
obj->data.xfont.xfont = strdup(x_findseed(state, registry));
obj->data.xfont.csize = size;
obj->data.xfont.code = code;
obj->data.xfont.registry = registry;
obj->ascent = size - y; /*XXX*/
obj->descent = -y; /*XXX*/
obj->vertloc = VL_BASE;
return 1;
}
static int
obj_new_image(state, x, y, image, xzoom, yzoom)
struct render_state *state;
int x, y;
Image *image;
int xzoom, yzoom;
{
struct render_object *obj;
obj = obj_alloc(state);
if (obj == NULL)
return 0;
obj->x = x;
obj->y = y;
obj->type = O_IMAGE;
obj->data.image.image = image;
obj->data.image.xzoom = xzoom;
obj->data.image.yzoom = yzoom;
obj->ascent = 0; /*XXX*/
obj->descent = image->height * yzoom / 100; /*XXX*/
obj->vertloc = VL_TOP;
return 1;
}
#ifdef USE_IMLIB
static int
obj_new_image2(state, x, y, image, xzoom, yzoom, imimage, zoomonclk)
struct render_state *state;
int x, y;
Image *image;
int xzoom, yzoom;
Imlib_Image *imimage;
int zoomonclk;
{
struct render_object *obj;
obj = obj_alloc(state);
if (obj == NULL)
return 0;
obj->x = x;
obj->y = y;
obj->type = O_IMAGE;
obj->data.image.image = image;
obj->data.image.xzoom = xzoom;
obj->data.image.yzoom = yzoom;
obj->ascent = 0; /*XXX*/
obj->descent = image->height * yzoom / 100; /*XXX*/
obj->vertloc = VL_TOP;
obj->data.image.imimage = imimage;
obj->data.image.zoomonclk = zoomonclk;
return 1;
}
#endif
static int
obj_new_icon(state, x, y, itype, isize, color, npoint, xpoint)
struct render_state *state;
int x, y;
u_int itype, isize;
u_long color;
u_int npoint;
XPoint *xpoint;
{
struct render_object *obj;
int i;
obj = obj_alloc(state);
if (obj == NULL)
return 0;
obj->x = x;
obj->y = y;
obj->fore = color;
obj->type = O_ICON;
obj->data.icon.itype = itype;
obj->data.icon.isize = isize;
obj->data.icon.npoint = npoint;
if (npoint) {
obj->data.icon.xpoint = malloc(sizeof(XPoint) * npoint);
if (obj->data.icon.xpoint == NULL) {
obj_free(state, obj);
return 0;
}
for (i = 0; i < npoint; i++)
obj->data.icon.xpoint[i] = xpoint[i];
} else
obj->data.icon.xpoint = NULL;
obj->ascent = 0; /*XXX*/
obj->descent = isize; /*XXX*/
obj->vertloc = VL_CENTER;
#ifdef USE_M17N
// Adjust icon position for line folding function
if (mgp_flag & FL_NOM17N)
obj->vertloc = VL_CENTER;
else
obj->vertloc = VL_ICENTER;
#endif
return 1;
}
static Pixel
obj_image_color(image, bimage, d, inithist)
Image *image, *bimage;
Pixel d;
int *inithist;
{
int i, j;
RGBMap rgb;
int r, g, b;
static char hist[256];
byte *p;
switch (bimage->type) {
case IBITMAP:
r = g = b = d ? 0xffff : 0;
break;
case IRGB:
r = bimage->rgb.red[d];
g = bimage->rgb.green[d];
b = bimage->rgb.blue[d];
break;
case ITRUE:
r = TRUE_RED(d) << 8;
g = TRUE_GREEN(d) << 8;
b = TRUE_BLUE(d) << 8;
break;
default:
return 0;
}
if (image->type == ITRUE)
return RGB_TO_TRUE(r, g, b);
for (i = 0; i < image->rgb.used; i++) {
if (image->rgb.red[i] == r &&
image->rgb.green[i] == g &&
image->rgb.blue[i] == b)
return i;
}
if (i >= image->rgb.size) {
if (i >= 256) {
/* search a free slot */
if (image->rgb.size == 256) {
if (!*inithist) {
*inithist = 1;
memset(hist, 0, sizeof(hist));
p = image->data;
for (j = 0; j < image->height; j++)
for (i = 0; i < image->width; i++)
hist[*p++] = 1;
}
for (i = 0; i < 256; i++) {
if (hist[i] == 0) {
hist[i] = 1;
goto freeslot;
}
}
}
return -1;
}
image->depth = 8;
newRGBMapData(&rgb, depthToColors(image->depth));
for (i = 0; i < image->rgb.used; i++) {
rgb.red[i] = image->rgb.red[i];
rgb.green[i] = image->rgb.green[i];
rgb.blue[i] = image->rgb.blue[i];
}
rgb.used = i;
freeRGBMapData(&image->rgb);
image->rgb = rgb;
}
freeslot:
image->rgb.red[i] = r;
image->rgb.green[i] = g;
image->rgb.blue[i] = b;
if (image->rgb.used < i + 1)
image->rgb.used = i + 1;
return i;
}
static Image *
obj_image_trans(image, x, y)
Image *image;
u_int x, y;
{
Image *timage;
int i, j;
byte *p, *b;
Pixel d, n, pd;
static XColor xcol;
int pl, bpl;
int trans;
u_int bw, bh, bx, by;
int inithist;
if (!COMPLEX_BGIMAGE) {
if (back_color[caching] != xcol.pixel) {
xcol.pixel = back_color[caching];
xcol.flags = DoRed|DoGreen|DoBlue;
XQueryColor(display, colormap, &xcol);
}
switch (image->type) {
case IBITMAP:
case IRGB:
image->rgb.red[image->trans] = xcol.red;
image->rgb.green[image->trans] = xcol.green;
image->rgb.blue[image->trans] = xcol.blue;
break;
case ITRUE:
d = image->trans;
n = RGB_TO_TRUE(xcol.red, xcol.green, xcol.blue);
pl = image->pixlen;
p = image->data;
for (j = 0; j < image->height; j++) {
for (i = 0; i < image->width; i++, p += pl) {
if (memToVal(p, pl) == d)
valToMem(n, p, pl);
}
}
break;
}
bw = bh = 0; /* for lint */
goto end;
}
bh = bgpixmap[bgindex].image->height;
bw = bgpixmap[bgindex].image->width;
j = 0;
if (image->type == IBITMAP) {
expand:
timage = image;
if (verbose)
fprintf(stderr, "obj_image_trans: expanding image\n");
image = expand(image);
if (image != timage)
freeImage(timage);
}
pl = image->pixlen;
p = image->data + image->width * j * pl;
bpl = bgpixmap[bgindex].image->pixlen;
pd = -1;
n = 0; /* for lint */
trans = image->trans;
inithist = 0;
for ( ; j < image->height; j++) {
by = (y + j) % bh;
bx = x % bw;
b = bgpixmap[bgindex].image->data +
(bgpixmap[bgindex].image->width * by + bx) * bpl;
for (i = 0; i < image->width; i++, p += pl, b += bpl, bx++) {
if (bx == bw) {
bx = 0;
b = bgpixmap[bgindex].image->data +
bgpixmap[bgindex].image->width * by * bpl;
}
if (memToVal(p, pl) != trans)
continue;
d = memToVal(b, bpl);
if (d != pd) {
pd = d;
n = obj_image_color(image,
bgpixmap[bgindex].image, d, &inithist);
if (n == -1)
goto expand;
}
valToMem(n, p, pl);
}
}
end:
if (verbose) {
char *p;
switch (image->type) {
case IBITMAP: p = "bitmap"; break;
case IRGB: p = "rgb"; break;
default: p = "true"; break;
}
fprintf(stderr, "obj_image_trans: %s: "
"trans=%d, rgb_used=%d, rgb_size=%d\n",
p, image->trans, image->rgb.used, image->rgb.size);
fprintf(stderr, " image=%dx%d+%d+%d",
image->width, image->height, x, y);
if (COMPLEX_BGIMAGE)
fprintf(stderr, " bgpixmap[bgindex].image=%dx%d", bw, bh);
fprintf(stderr, "\n");
}
image->trans = -1; /* XXX: need recalculation to redraw? */
return image;
}
static void
obj_draw_image(target, x, y, obj, page)
Drawable target;
u_int x, y;
struct render_object *obj;
int page;
{
Image *image, *timage;
XImageInfo *ximageinfo;
XImage *xim;
int private = mgp_flag & FL_PRIVATE;
image = obj->data.image.image;
if (obj->data.image.xzoom != 100.0 || obj->data.image.yzoom != 100.0) {
timage = image;
image = zoom(image,
obj->data.image.xzoom, obj->data.image.yzoom, verbose);
if (!image) {
fprintf(stderr, "image zoom (%0.2fx%0.2f) failed in obj_draw_image\n",
obj->data.image.xzoom, obj->data.image.yzoom);
exit(1);
}
freeImage(timage);
}
if (image->trans >= 0)
image = obj_image_trans(image, x, y);
obj->data.image.image = image; /* to free later */
ximageinfo= imageToXImage(display, screen, visual, depth, image,
private, 0,0, verbose);
if (ximageinfo == NULL) {
fprintf(stderr, "Cannot convert Image to XImage\n");
cleanup(-1);
}
xim = ximageinfo->ximage;
if (xim->format == XYBitmap)
XSetBackground(display, gcfore, back_color[caching]);
XPutImage(display, target, gcfore, xim, 0, 0,
x, y, xim->width, xim->height);
#ifdef USE_IMLIB
if (obj->data.image.zoomonclk) {
regist_zimage_position(obj, x, y, xim->width, xim->height, page);
}
#endif
freeXImage(image, ximageinfo);
}
static void
obj_draw(state, target, xpos, ypos)
struct render_state *state;
Drawable target;
u_int xpos, ypos;
{
struct render_object *obj;
int x = 0, y = 0;
int width, height, xwidth, xheight;
u_long fore;
u_int code;
char *registry;
XChar2b kch[2];
#define MAXDRAWAREA 1024
struct {
int x, y, width, height;
} drawarea[MAXDRAWAREA];
int areaindex = 0;
#define addarea(X) \
{\
if (areaindex == MAXDRAWAREA){\
fprintf(stderr, "too many drawarea (increase MAXDRAWAREA)\n");\
exit(1);\
}\
drawarea[areaindex].x = x;\
drawarea[areaindex].y = y - obj->data.X->ascent;\
drawarea[areaindex].width = obj->data.X->xmax+1;\
drawarea[areaindex].height = obj->data.X->height+1;\
areaindex ++;\
}
#if 0
char ch[2];
#endif
u_int isize;
int i;
int lineoff; /* ypos correction for lines with superscripts */
#ifdef RASTERLIB
XImage *bim, *xim;
u_long bcolor;
#endif /* RASTERLIB */
/*
* very complicated...
*
* xpos, ypos x/y position of the target,
* leftmost and uppermost dot.
* state->ypos absolute y position in main window.
*/
xpos += state->tabxprefix ? state->tabxprefix : state->xprefix;
width = (state->linewidth <= state->width - xpos)
? state->linewidth
: state->width - xpos;
height = state->maxascent + state->maxdescent + 1;
xpos += state->xoff;
ypos += state->yoff;
fore = fore_color[caching];
/*
* only used with superscript offset for calculating the
* exact line position (ypos correction)
*/
lineoff = state->maxascent - state->max_lineascent;
#ifdef RASTERLIB
bcolor = back_color[caching];
for (obj = state->obj; obj; obj = obj->next) {
#ifdef VFLIB
if (obj->type == O_VFONT){
xwidth = obj->data.vfc->width;
xheight = obj->data.vfc->height;
break;
}
#endif /* VFLIB */
#ifdef FREETYPE
if (obj->type == O_TFONT){
xwidth = obj->data.tfc->width;
xheight = obj->data.tfc->height;
break;
}
#endif /* FREETYPE */
}
if (obj != NULL) { /* VFONT exist */
xim = XCreateImage(display, visual, depth, ZPixmap,
0, NULL, width, height,
(depth <= 8) ? 8 : (depth <= 16) ? 16 : 32, 0);
xim->data = malloc(xim->bytes_per_line * height);
if (COMPLEX_BGIMAGE) {
u_int bw, bh, bx, by, ox, oy;
u_long p;
u_long r, g, b;
byte *bp;
int bpl;
XColor col;
bim = bgpixmap[bgindex].ximageinfo->ximage;
bw = bim->width;
bh = bim->height;
ox = xpos;
oy = state->ypos + state->yoff;
bcolor = (u_long)-1; /* tell vfc_image() to calculate */
by = oy % bh;
if (bw == 1) {
r = g = b = 0;
bpl = bgpixmap[bgindex].image->pixlen;
bp = bgpixmap[bgindex].image->data + by * bpl;
for (y = 0;
y < height;
y++, by++, bp += bpl) {
if (by == bh)
by = 0;
p = memToVal(bp, bpl);
if (TRUEP(bgpixmap[bgindex].image)) {
r += TRUE_RED(p) << 8;
g += TRUE_GREEN(p) << 8;
b += TRUE_BLUE(p) << 8;
} else {
r += bgpixmap[bgindex].image->rgb.red[p];
g += bgpixmap[bgindex].image->rgb.green[p];
b += bgpixmap[bgindex].image->rgb.blue[p];
}
p = XGetPixel(bim, 0, by);
for (x = 0; x < width; x++)
XPutPixel(xim, x, y, p);
}
col.red = r / height;
col.green = g / height;
col.blue = b / height;
col.flags = DoRed|DoGreen|DoBlue;
/* XXX:actually we don't need to allocate. */
if (XAllocColor(display, colormap, &col)) {
regist_alloc_colors(&font_clr,
&col.pixel, 1);
bcolor = col.pixel;
}
#if 0
fprintf(stderr, "bim=%dx%d, r=%x, g=%x, b=%x, "
"bcolor=%x\n",
bgpixmap[bgindex].image->width, bgpixmap[bgindex].image->height,
col.red, col.green, col.blue, bcolor);
#endif
} else {
for (y = 0; y < height; y++, by++) {
if (by == bh)
by = 0;
for (x = 0, bx = ox % bw; x < width; x++, bx++) {
if (bx == bw)
bx = 0;
p = XGetPixel(bim, bx, by);
XPutPixel(xim, x, y, p);
}
}
}
} else {
memset(xim->data, 0, xim->bytes_per_line * height);
XAddPixel(xim, bcolor);
}
for ( ; obj; obj = obj->next) {
x = obj->x;
switch (obj->vertloc) {
case VL_BASE:
y = state->maxascent;
break;
case VL_CENTER:
y = (state->maxascent + state->maxdescent) / 2;
y += (obj->ascent - obj->descent) / 2;
break;
case VL_TOP:
y = obj->ascent;
break;
case VL_BOTTOM:
y = state->maxascent + state->maxdescent;
y -= obj->descent;
break;
}
y += obj->y;
#ifdef VFLIB
if (obj->type == O_VFONT) {
(void)vfc_image(obj->data.vfc,
obj->fore, bcolor, xim, x, y);
addarea(vfc);
}
#endif /* VFLIB */
#ifdef FREETYPE
if (obj->type == O_TFONT) {
(void)tfc_image(obj->data.tfc,
obj->fore, bcolor, xim, x, y);
addarea(tfc);
}
#endif /* FREETYPE */
}
#if 0
XPutImage(display, target, gcfore, xim, 0, 0,
xpos, ypos, width, height);
#else
for (i = 0; i < areaindex; i ++)
XPutImage(display, target, gcfore, xim,
drawarea[i].x, drawarea[i].y,
drawarea[i].x + xpos,
ypos + drawarea[i].y - lineoff,
drawarea[i].width, drawarea[i].height);
#endif
XDestroyImage(xim);
if (mgp_flag & FL_GLYPHEDGE) {
XDrawLine(display, target, gcfore, state->xoff, ypos,
state->xoff + state->width - 1, ypos);
XDrawLine(display, target, gcfore,
state->xoff, ypos + state->maxascent,
state->xoff + state->width - 1,
ypos + state->maxascent);
XDrawLine(display, target, gcgreen,
state->xoff,
ypos + state->maxascent + state->maxdescent,
state->xoff + state->width - 1,
ypos + state->maxascent + state->maxdescent);
XDrawLine(display, target, gcred,
state->xoff, ypos + height,
state->xoff + state->width - 1, ypos + height);
}
}
#endif /* RASTERLIB */
for (obj = state->obj; obj; obj = obj->next) {
#if 0
x = obj->x + offx;
y = obj->y + offy;
#else
x = obj->x;
switch (obj->vertloc) {
case VL_BASE:
y = state->maxascent;
break;
case VL_ICENTER:
if (state->maxflheight){
y = (state->maxascent + state->maxflheight) / 2;
} else
y = (state->maxascent + state->maxdescent) / 2;
y += (obj->ascent - obj->descent) / 2;
break;
case VL_CENTER:
y = (state->maxascent + state->maxdescent) / 2;
y += (obj->ascent - obj->descent) / 2;
break;
case VL_TOP:
y = obj->ascent;
break;
case VL_BOTTOM:
y = state->maxascent + state->maxdescent;
y -= obj->descent;
break;
}
x += xpos;
y += ypos;
#endif
switch (obj->type) {
#ifdef MNG
case O_ANIM:
obj_draw_anim(state, x, y, obj);
break;
#endif /* MNG */
case O_IMAGE:
obj_draw_image(target, x, y, obj, state->page);
break;
#ifdef USE_XFT2
case O_XTFONT:
y += obj->y;
set_xrender_color(obj->fore, state->opaque);
xft_font = xft_setfont(obj->data.xftfont.fontname,
obj->data.xftfont.size,
obj->data.xftfont.registry);
XftDraw *dummy = xft_getdraw(target);
if (obj->data.xftfont.charset16){
XftDrawStringUtf8(dummy,
&xft_forecolor, xft_font,
x, y - lineoff,
obj->data.xftfont.data,
obj->data.xftfont.len);
} else
XftDrawString8(dummy,
&xft_forecolor, xft_font,
x, y - lineoff,
obj->data.xftfont.data,
obj->data.xftfont.len);
XftDrawDestroy(dummy);
break;
#endif
#ifdef USE_M17N
case O_M17NTEXT:
y += obj->y;
M17N_draw_object(obj, target, x, y);
break;
#endif
case O_XFONT:
y += obj->y;
code = obj->data.xfont.code;
registry = obj->data.xfont.registry;
(void)x_setfont(obj->data.xfont.xfont,
obj->data.xfont.csize,
registry, NULL);
if (obj->fore != fore) {
fore = obj->fore;
XSetForeground(display, gcfore, fore);
}
#if 1
/* is it always okay? */
kch[0].byte1 = (code >> 8) & 0xff;
kch[0].byte2 = code & 0xff;
XDrawString16(display, target, gcfore,
x, y - lineoff, kch, 1);
#else
if (registry) {
kch[0].byte1 = (code >> 8) & 0xff;
kch[0].byte2 = code & 0xff;
XDrawString16(display, target, gcfore,
x, y - lineoff, kch, 1);
} else {
ch[0] = code & 0xff;
XDrawString(display, target, gcfore,
x, y - lineoff, ch, 1);
}
#endif
break;
case O_ICON:
if (obj->fore != fore) {
fore = obj->fore;
XSetForeground(display, gcfore, fore);
}
isize = obj->data.icon.isize;
switch (obj->data.icon.itype) {
case 1: /* this is box */
XFillRectangle(display, target, gcfore, x, y,
isize, isize);
break;
case 2: /* this is arc */
XFillArc(display, target, gcfore, x, y,
isize, isize, 0, 360 * 64);
break;
case 3: case 4: case 5: case 6:
case 7:
for (i = 0; i < obj->data.icon.npoint; i++) {
obj->data.icon.xpoint[i].x += x;
obj->data.icon.xpoint[i].y += y;
}
XFillPolygon(display, target, gcfore,
obj->data.icon.xpoint,
obj->data.icon.npoint,
Convex, CoordModeOrigin);
break;
}
break;
default:
break;
}
}
if (fore != fore_color[caching]){
XSetForeground(display, gcfore, fore_color[caching]);
}
/* ASSERT(state->obj == NULL); */
/* ASSERT(state->objlast == NULL); */
}
#ifdef VFLIB
static u_int
draw_onechar_vf(state, code, x, y, width, height)
struct render_state *state;
u_int code;
int x, y;
u_int width, height;
{
struct vfont *vfc;
vfc = vfc_get(code, width, height, 1);
draw_line_itemsize(state, vfc->ascent, vfc->descent, 0);
obj_new_vfont(state, x, y, vfc, height);
return vfc->charlen;
}
#endif /* VFLIB */
static char *
x_fontname(buf, bufsiz, seed, siz, registry)
char *buf;
int bufsiz;
char *seed;
int siz;
char *registry; /* already canonicalized */
{
int hyphen;
char *p;
char tmp[BUFSIZ];
char tmp2[BUFSIZ];
char **fontlist;
int count;
if (!registry)
registry = "iso8859-1";
if (siz < 0)
strcpy(tmp2, "*");
else
sprintf(tmp2, "%d", siz);
hyphen = 0;
for (p = seed; *p; p++) {
if (*p == '-')
hyphen++;
}
switch (hyphen) {
case 0:
/* for "a14", "5x8", or such an short names */
if ((fontlist = XListFonts(display, seed, 1, &count))) {
XFreeFontNames(fontlist);
strcpy(buf, seed);
break;
}
sprintf(tmp, "%s-*-*", seed);
sprintf(buf, FONT_FORMAT, tmp, tmp2, registry);
break;
case 2:
sprintf(buf, FONT_FORMAT, seed, tmp2, registry);
break;
case XLFD_HYPHEN:
/* as is */
strcpy(buf, seed);
break;
case 1: /* should not happen */
fprintf(stderr, "internal error: invalid seed <%s>\n", seed);
exit(1);
}
if (mgp_flag & FL_VERBOSE) {
fprintf(stderr, "fontname: seed=<%s> siz=<%d> reg=<%s> "
"result=<%s>\n",
seed, siz, registry, buf);
}
return buf;
}
static int
x_parsefont(xfont, pixel, truescalable)
char *xfont;
int *pixel;
int *truescalable;
{
char *p;
int fsize;
int i;
/* go toward pixel size */
p = xfont;
for (i = 0; *p && i < 7; i++) {
/* go toward minus sign */
while (*p && *p != '-')
p++;
/* skip minus sign */
if (*p)
p++;
}
if (!*p)
return -1;
fsize = atoi(p);
if (pixel)
*pixel = fsize;
/* skip pixel size */
while (*p && (isdigit(*p) || *p == '*'))
p++;
if (*p == '-')
p++;
else
return -1;
/* skip point size */
while (*p && (isdigit(*p) || *p == '*'))
p++;
if (*p == '-')
p++;
else
return -1;
if (truescalable) {
if (fsize == 0 && (p[0] == '0' || p[0] == '*') && p[1] == '-')
*truescalable = 1;
else
*truescalable = 0;
}
return 0;
}
static XFontStruct *
x_setfont(xfont, csize, registry, truescalable)
char *xfont;
u_int csize;
char *registry;
int *truescalable;
{
static XFontStruct *xfontstruct;
int i, fsize;
char fontstring[BUFSIZ];
#define FONTTYPEMAX 10 /* number of used fontlist type (in cache) */
#define FONTLISTMAX 20 /* number of list for specified font type */
#define FONTALLOWMAX 105 /* % of desired font */
#define FONTALLOWMIN 90 /* % of desired font */
char **fontlist, **font;
u_int error;
int best, freeindex, count;
int maxsize, minsize;
int scalable, tscalable, tsflag;
static struct {
char *xlfd;
char **list;
int count;
} fontnames[FONTTYPEMAX];
#define FONTCACHEMAX 200 /* number of used font type (in cache) */
static struct {
char *xfont;
u_int csize;
char *registry;
char *xlfd;
XFontStruct *xfontstruct;
} fonts[FONTCACHEMAX];
/*
* Check font cache first.
*/
for (i = 0; i < FONTCACHEMAX; i++) {
if (!fonts[i].xfontstruct)
continue;
if (fonts[i].csize != csize || fonts[i].registry != registry
|| strcmp(fonts[i].xfont, xfont) != 0) {
continue;
}
#if 0
if (verbose) {
fprintf(stderr, "font cache hit: entry %d <%s>\n",
i, fonts[i].xlfd);
}
#endif
XSetFont(display, gcfore, fonts[i].xfontstruct->fid);
return fonts[i].xfontstruct;
}
/*
* load new font.
*/
if (csize < 5) {
xfontstruct = XLoadQueryFont(display, "nil2");
goto gotfont;
}
if (verbose) {
fprintf(stderr, "need font <%s:%d:%s>\n",
xfont, csize, registry?registry:"NULL");
}
/*
* Look for the best font possible.
* 1. Check for a font that is smaller than the required one.
* By using smaller font, we won't make the screen garbled.
* 2. If 1. is impossible, look for slightly larger font than
* the required one.
*/
fontlist = NULL;
freeindex = -1;
x_fontname(fontstring, sizeof(fontstring), xfont, -1, registry);
if (verbose)
fprintf(stderr, "fontstring <%s>\n", fontstring);
for (i = 0; i < FONTTYPEMAX; i++) {
if (fontnames[i].xlfd == NULL) {
if (freeindex < 0)
freeindex = i;
continue;
}
if (strcmp(fontnames[i].xlfd, fontstring) == 0) {
fontlist = fontnames[i].list;
count = fontnames[i].count;
freeindex = i;
break;
}
}
if (fontlist == NULL) {
fontlist = XListFonts(display, fontstring, FONTLISTMAX, &count);
if (fontlist == NULL)
return NULL;
if (freeindex >= 0) {
if (fontnames[freeindex].xlfd)
free(fontnames[freeindex].xlfd);
fontnames[freeindex].xlfd = strdup(fontstring);
fontnames[freeindex].list = fontlist;
fontnames[freeindex].count = count;
}
}
error = (u_int)-1;
best = -1;
maxsize = csize * FONTALLOWMAX / 100; /* truncate */
minsize = (csize * FONTALLOWMIN + 99) / 100; /* roundup */
if (verbose)
fprintf(stderr, "checking %d to %d\n", minsize, maxsize);
scalable = tscalable = -1;
if (truescalable)
*truescalable = 0;
for (i = 0, font = fontlist; i < count; i++, font++) {
if (x_parsefont(*font, &fsize, &tsflag) < 0) {
#if 1
if (verbose) {
fprintf(stderr, " [%d] <%s>: nosize\n",
i, *font);
}
#endif
continue;
}
if (fsize == 0) {
if (scalable < 0)
scalable = i;
if (tsflag) {
tscalable = i;
if (truescalable)
*truescalable = 1;
}
if (verbose) {
fprintf(stderr, " [%d] <%s>: scalable (%d)\n",
i, *font, tsflag);
}
continue;
} else if (fsize > maxsize || fsize < minsize) {
#if 0
if (verbose) {
fprintf(stderr, " [%d] <%s>: reject\n",
i, *font);
}
#endif
continue;
}
if (fsize > csize) {
fsize = fsize - csize + 100;
/* penalty for larger font */
} else
fsize = csize - fsize;
if (error > fsize) {
error = fsize;
best = i;
if (verbose) {
fprintf(stderr, " [%d] <%s>: score %d best\n",
i, *font, error);
}
} else {
if (verbose) {
fprintf(stderr, " [%d] <%s>: score %d\n",
i, *font, error);
}
}
}
if (best >= 0) {
if (verbose) {
fprintf(stderr, "using best [%d] <%s>\n",
best, fontlist[best]);
}
strlcpy(fontstring, fontlist[best], sizeof(fontstring));
} else if (scalable >= 0 || tscalable >= 0) {
x_fontname(fontstring, sizeof(fontstring), xfont, csize,
registry);
if (verbose) {
fprintf(stderr, "using %sscalable <%s>\n",
tscalable >= 0 ? "true" : "", fontstring);
}
}
xfontstruct = XLoadQueryFont(display, fontstring);
if (freeindex < 0)
XFreeFontNames(fontlist);
/*
* Fill font cache.
*/
for (i = 0; i < FONTCACHEMAX; i++) {
if (!fonts[i].xfontstruct)
break;
}
if (FONTTYPEMAX <= i) {
/* last resort. always cache the font recently used */
i = FONTTYPEMAX - 1;
XFreeFont(display, fonts[i].xfontstruct);
free(fonts[i].xfont);
free(fonts[i].xlfd);
}
#if 0
if (verbose) {
fprintf(stderr, "fill font cache: entry %d <%s>\n",
i, fontstring);
}
#endif
fonts[i].csize = csize;
fonts[i].registry = registry;
fonts[i].xfont = strdup(xfont);
fonts[i].xlfd = strdup(fontstring);
fonts[i].xfontstruct = xfontstruct;
gotfont:
if (xfontstruct == NULL)
return NULL;
XSetFont(display, gcfore, xfontstruct->fid);
return xfontstruct;
}
static u_int
draw_onechar_x(state, code, x, y, size, argregistry, lastchar)
struct render_state *state;
u_int code;
int x, y;
int size;
char *argregistry;
int lastchar;
{
u_int charlen;
static XFontStruct *xfontstruct;
int coffset;
XCharStruct *cs;
char *metricsource;
char *seed;
char *registry;
if (code >= 0xa0 && ((!argregistry || !argregistry[0]) && mgp_charset))
registry = mgp_charset;
else
registry = argregistry;
seed = x_findseed(state, registry);
xfontstruct = x_setfont(seed, char_size[caching], registry, NULL);
if (xfontstruct == NULL)
return 0;
if (!xfontstruct->per_char) {
metricsource = "max_bounds";
coffset = 0;
cs = &xfontstruct->max_bounds;
} else if (!xfontstruct->min_byte1 && !xfontstruct->max_byte1) {
metricsource = "bytewise offset";
coffset = (code & 0xff) - xfontstruct->min_char_or_byte2;
cs = &xfontstruct->per_char[coffset];
} else {
metricsource = "wordwise offset";
coffset = (code & 0xff) - xfontstruct->min_char_or_byte2;
coffset += (((code >> 8) & 0xff) - xfontstruct->min_byte1)
* (xfontstruct->max_char_or_byte2 - xfontstruct->min_char_or_byte2);
cs = &xfontstruct->per_char[coffset];
}
/*
* It looks that there are some Japanese X11 fonts with bogus
* font metric (cs->width == 0). This is a workaround for that.
* (or is there any mistake in above "coffset" computation?)
*
* TODO: report the X/Open group, or some other guys, about this.
*/
if (!cs->width) {
if (verbose) {
fprintf(stderr, "X11 font %s:%d:%s has bogus "
"font metric for glyph 0x%04x\n"
"\tcs->width=%d, source=%s, coffset=0x%04x\n",
seed, char_size, registry?registry:"NULL",
code, cs->width, metricsource, coffset);
}
cs = &xfontstruct->max_bounds;
}
draw_line_itemsize(state, cs->ascent, cs->descent, 0);
/* usually */
charlen = cs->width;
/*
* for the very first char on the line, the char may goes over the
* edge at the lefthand side. offset the image to the right so that
* whole part of the bitmap appears on the screen.
* beware the sign-ness of cs->lbearing.
*/
if (x + cs->lbearing < 0) {
x -= cs->lbearing;
charlen -= cs->lbearing;
}
/*
* For the last char, make sure that the whole part of the bitmap
* appears on the screen.
*/
if (lastchar && cs->width < cs->rbearing)
charlen += cs->rbearing - cs->width;
obj_new_xfont(state, x, y, size, code, registry);
return charlen;
}
/*
* render misc items.
*/
static void
back_gradation(state, cg0)
struct render_state *state;
struct ctrl_grad *cg0;
{
struct ctrl_grad cg1;
struct ctrl_grad *cg;
int srcwidth, srcheight;
int dstwidth, dstheight;
int dir, numcolor;
float xzoomrate, yzoomrate;
int hquality, vquality;
Image *myimage, *image;
Pixmap mypixmap;
XImageInfo *ximageinfo;
byte *pic;
int private = mgp_flag & FL_PRIVATE;
static Cursor curs;
/* okay, please wait for a while... */
if (!curs)
curs = XCreateFontCursor(display, XC_watch);
XDefineCursor(display, window, curs);
XFlush(display);
/* just for safety */
memcpy(&cg1, cg0, sizeof(struct ctrl_grad));
cg = &cg1;
/* grab parameters */
dir = cg->ct_direction;
numcolor = cg->ct_numcolor;
hquality = b_quality[caching];
vquality = b_quality[caching];
/*
* XXX zoomflag is too complex to understand.
*/
if (!cg->ct_zoomflag) {
int t;
int i;
dstwidth = window_width * cg->ct_width / 100;
dstheight = window_height * cg->ct_height / 100;
srcwidth = dstwidth;
srcheight = dstheight;
/*
* apply quality factor if srcwidth/height are large enough.
*/
#define TOOSMALLFACTOR 8
t = srcwidth;
for (i = 100; hquality < i; i--) {
t = srcwidth * i / 100;
if (t < cg->ct_g_colors * TOOSMALLFACTOR)
break;
}
srcwidth = t;
t = srcheight;
for (i = 100; vquality < i; i--) {
t = srcheight * i / 100;
if (t < cg->ct_g_colors * TOOSMALLFACTOR)
break;
}
srcheight = t;
#undef TOOSMALLFACTOR
} else {
dstwidth = window_width;
dstheight = window_height;
srcwidth = state->width * cg->ct_width / 100;
srcheight = state->height * cg->ct_height / 100;
/*
* we don't apply quality factor here, since srcwidth/height
* is already smaller than dstwidth/height.
*/
}
#if 0
if (srcwidth * hquality / 100 < cg->ct_g_colors * TOOSMALLFACTOR
|| srcheight * vquality / 100 < cg->ct_g_colors * TOOSMALLFACTOR) {
srcwidth = srcwidth * hquality / 100;
srcheight = srcheight * vquality / 100;
}
#endif
xzoomrate = 100.0 * dstwidth / srcwidth;
yzoomrate = 100.0 * dstheight / srcheight;
/* performace enhance hack for special case */
if (dir % 90 == 0) {
float *q;
int *p, *r;
/*
* 0 or 180: reduce width
* 90 or 270: reduce height
*/
p = (dir % 180 == 0) ? &srcwidth : &srcheight;
q = (dir % 180 == 0) ? &xzoomrate : &yzoomrate;
r = (dir % 180 == 0) ? &dstwidth : &dstheight;
/* rely upon use X11 background image tiling. */
*q = (float) 100.0;
#ifndef DITHERED_BGRAD
*p = 1;
*r = 1;
#else
*p = 3;
*r = 3;
#endif
}
if (verbose) {
fprintf(stderr, "raw: %d,%d qu: %d,%d "
"dst: %d,%d src: %d,%d zoom: %0.2f,%0.2f\n",
cg->ct_width, cg->ct_height,
hquality, vquality,
dstwidth, dstheight, srcwidth, srcheight,
xzoomrate, yzoomrate);
}
screen = DefaultScreen(display);
/* make gradation image */
pic = draw_gradation(srcwidth, srcheight, cg);
myimage = make_XImage(pic, srcwidth, srcheight);
if (numcolor < 64)
myimage = reduce(myimage, numcolor, verbose);
if (verbose) {
fprintf(stderr, "background zoomrate: (%0.2f,%0.2f)\n",
xzoomrate, yzoomrate);
fprintf(stderr, "background zoom mode %d: "
"(%d, %d)->(%d, %d)[%d]\n", cg->ct_zoomflag,
srcwidth, srcheight, dstwidth, dstheight, b_quality);
}
if (xzoomrate != 100.0 || yzoomrate != 100.0) {
image = myimage;
myimage = zoom(image, xzoomrate, yzoomrate, verbose);
if (!image) {
fprintf(stderr, "image zoom (%0.2fx%0.2f) failed in back_gradataion\n",
xzoomrate, yzoomrate);
exit(1);
}
freeImage(image);
}
#ifndef COLOR_BUGFIX
if (private) free_alloc_colors(&back_clr);
#endif
ximageinfo = imageToXImage(display, screen, visual, depth, myimage,
private, 0, 1, verbose);
if (!ximageinfo) {
fprintf(stderr, "Cannot convert Image to XImage\n");
cleanup(-1);
}
regist_background_pixmap(ximageinfo, myimage);
XUndefineCursor(display, window);
XFlush(display);
}
/* !TODO: move rotation code into some library */
/* rotate image by 90 degrees (counter clockwise) */
static void rotate_image_p90(image)
Image *image;
{
unsigned int row, column, pl = image->pixlen;
unsigned int new_height = image->width, new_width = image->height, new_linelen = new_width * pl;
byte *src, *tgt, *col_head;
Pixel d;
/* allocate buffer for new image */
byte *rot_data = lmalloc(new_linelen * new_height);
/* do the rotation */
for (row = 0, src = image->data, col_head = rot_data + (new_height - 1) * new_linelen;
row < image->height;
row++, col_head += pl) {
for (column = 0, tgt = col_head;
column < image->width;
column++, src += pl, tgt -= new_linelen) {
d = memToVal(src, pl);
valToMem(d, tgt, pl);
}
}
/* swap to rotated image, exchange height and width
and point to rotated data */
image->height = new_height;
image->width = new_width;
lfree(image->data);
image->data = rot_data;
}
/* rotate image by -90 degrees (clockwise) */
static void rotate_image_m90(image)
Image *image;
{
unsigned int row, column, pl = image->pixlen;
unsigned int new_height = image->width, new_width = image->height, new_linelen = new_width * pl;
byte *src, *tgt;
Pixel d;
/* allocate buffer for new image */
byte *rot_data = lmalloc(new_linelen * new_height);
/* do the rotation */
for (row = 0, src = image->data; row < image->height; row++) {
for (column = 0, tgt = rot_data + new_linelen - (row + 1) * pl;
column < image->width;
column++, src += pl, tgt += new_linelen) {
d = memToVal(src, pl);
valToMem(d, tgt, pl);
}
}
/* swap to rotated image, exchange height and width
and point to rotated data */
image->height = new_height;
image->width = new_width;
lfree(image->data);
image->data = rot_data;
return;
}
/* rotate image by 180 degrees */
static void rotate_image_180(image)
Image *image;
{
unsigned int row, column, pl = image->pixlen;
unsigned int new_height = image->height, new_width = image->width, new_linelen = new_width * pl;
byte *src, *tgt;
Pixel d;
/* allocate buffer for new image */
byte *rot_data = lmalloc(new_linelen * new_height);
/* do the rotation */
for (row = 0, src = image->data; row < image->height; row++) {
for (column = 0, tgt = rot_data + (new_height - row) * new_linelen - pl;
column < image->width;
column++, src += pl, tgt -= pl) {
d = memToVal(src, pl);
valToMem(d, tgt, pl);
}
}
/* swap to rotated image, exchange height and width
and point to rotated data */
image->height = new_height;
image->width = new_width;
lfree(image->data);
image->data = rot_data;
return;
}
static void
image_load(state, filename, numcolor, ximagesize, yimagesize, backflag, zoomflag, centerflag, raise, rotate, zoomonclk)
struct render_state *state;
char *filename;
int numcolor;
int ximagesize;
int yimagesize;
int backflag;
int zoomflag;
int centerflag;
int raise;
int rotate;
int zoomonclk;
{
Image *image, *myimage;
Pixmap mypixmap;
XImageInfo *ximageinfo;
u_int image_posx;
int width, height, yoffset;
float xzoomrate, yzoomrate;
int private = mgp_flag & FL_PRIVATE;
static Cursor curs;
static char backfile[MAXPATHLEN];
static int backzoom, backnumcolor, backx, backy;
#ifdef USE_IMLIB
Imlib_Image *imimage;
#endif
if (!caching){
if (!curs)
curs = XCreateFontCursor(display, XC_watch);
XDefineCursor(display, state->target, curs);
XFlush(display);
}
if ((myimage = loadImage(filename, verbose)) == NULL) {
fprintf(stderr, "failed to load image file\n");
cleanup(-1);
}
switch (rotate) {
case 0:
/* Do nothing */
break;
case -90:
case 270:
rotate_image_m90(myimage);
break;
case 90:
rotate_image_p90(myimage);
break;
case -180:
case 180:
rotate_image_180(myimage);
break;
default:
fprintf(stderr, "rotation by %d degrees not supported.\n", rotate);
cleanup(-1);
}
width = myimage->width;
height = myimage->height;
if (myimage->depth == 1 && myimage->trans < 0) {
XColor xc;
xc.flags = DoRed | DoGreen | DoBlue;
xc.pixel = fore_color[caching];
XQueryColor(display, colormap, &xc);
*(myimage->rgb.red + 1) = xc.red;
*(myimage->rgb.green + 1) = xc.green;
*(myimage->rgb.blue + 1) = xc.blue;
myimage->trans = 0; /* call obj_image_trans() later */
}
if (numcolor)
myimage = reduce(myimage, numcolor, verbose);
#if 0
if (zoomflag == 2) {
/*
* auto resize according to physical and desired screen size.
* allow 5% error for '-o' option.
*/
if (ximagesize == 0 || ximagesize == state->width)
ximagesize = 100;
else
ximagesize = state->width * 100 / ximagesize;
if (yimagesize == 0 || yimagesize == state->height)
yimagesize = 100;
else
yimagesize = state->height * 100 / yimagesize;
if (ximagesize > 95 && ximagesize < 105 &&
yimagesize > 95 && yimagesize < 105)
ximagesize = yimagesize = 0;
}
if (ximagesize != 0) {
if (!zoomflag)
xzoomrate = state->width * ximagesize / width;
else
xzoomrate = ximagesize;
} else
xzoomrate = 100.0;
if (yimagesize != 0) {
if (!zoomflag)
yzoomrate = state->height * yimagesize / height;
else
yzoomrate = yimagesize;
} else
yzoomrate = 100.0;
#else
if (!ximagesize) ximagesize = 100;
if (!yimagesize) yimagesize = 100;
xzoomrate = (float) ximagesize;
yzoomrate = (float) yimagesize;
image_zoomratio(state, &xzoomrate, &yzoomrate, zoomflag, width, height);
#endif
if (backflag) {
if (xzoomrate != 100 || yzoomrate != 100) {
image = myimage;
myimage = zoom(image, xzoomrate, yzoomrate, verbose);
if (!image) {
fprintf(stderr, "image zoom (%dx%d) failed in image_load\n",
xzoomrate, yzoomrate);
exit(1);
}
freeImage(image);
}
#ifndef COLOR_BUGFIX
if (private) free_alloc_colors(&back_clr);
#endif
ximageinfo= imageToXImage(display, screen, visual, depth,
myimage, private, 0, 1, verbose);
if (ximageinfo == NULL) {
fprintf(stderr, "Cannot convert Image to XImage\n");
cleanup(-1);
}
regist_background_pixmap(ximageinfo, myimage);
goto end;
}
#if 1 /* by h.kakugawa@computer.org */
switch(valign){
case VL_TOP:
draw_line_itemsize(state,
(height * raise) * yzoomrate / 10000,
height * (100 + raise) * yzoomrate / 10000, 0);
break;
case VL_BOTTOM:
draw_line_itemsize(state,
height * (100 + raise) * yzoomrate / 10000,
(height * raise) * yzoomrate / 10000, 0);
break;
case VL_CENTER:
draw_line_itemsize(state,
height * (100 + raise) * yzoomrate / 20000,
height * (100 + raise) * yzoomrate / 20000, 0);
break;
}
#else
switch(valign){
case VL_TOP:
draw_line_itemsize(state, 0, height * yzoomrate / 100, 0);
break;
case VL_BOTTOM:
draw_line_itemsize(state, height * yzoomrate / 100, 0, 0);
break;
case VL_CENTER:
draw_line_itemsize(state, height * yzoomrate / 200,
height * yzoomrate / 200, 0);
break;
}
#endif
if (centerflag)
image_posx = char_size[caching] / 2 - (width * xzoomrate / 100) / 2;
else
image_posx = 0;
#ifdef USE_IMLIB
imimage = search_imdata(filename);
obj_new_image2(state, state->linewidth + image_posx,
- height * yzoomrate / 100 / 2,
myimage, xzoomrate, yzoomrate, imimage, zoomonclk);
#else
obj_new_image(state, state->linewidth + image_posx,
- height * yzoomrate / 100 / 2,
myimage, xzoomrate, yzoomrate);
#endif
state->linewidth += (width * xzoomrate / 100);
end:
if (!caching){
XUndefineCursor(display, state->target);
XFlush(display);
}
}
static void
image_load_ps(state, filename, numcolor, ximagesize, yimagesize, backflag, zoomflag, centerflag, raise, rotate,zoomonclk)
struct render_state *state;
char *filename;
int numcolor;
int ximagesize;
int yimagesize;
int backflag;
int zoomflag;
int centerflag;
int raise;
int rotate;
int zoomonclk;
{
int x1, y1, x2, y2;
static Cursor curs;
char fullname[MAXPATHLEN];
char *imagefile;
int width, height;
float xzoom, yzoom, zratio;
char *p;
/* wait for a while, please. */
if (!curs)
curs = XCreateFontCursor(display, XC_watch);
XDefineCursor(display, window, curs);
XFlush(display);
if (findImage(filename, fullname) < 0) {
fprintf(stderr, "image file %s not found in path\n", filename);
cleanup(-1);
}
if (ps_boundingbox(fullname, &x1, &y1, &x2, &y2) < 0) {
/* error message generated in ps_boundingbox() */
cleanup(-1);
}
width = x2 - x1 + 1;
height = y2 - y1 + 1;
#if 0
if (zoomflag == 2) {
/* screen relative */
if (ximagesize == 0 || ximagesize == state->width)
ximagesize = 100;
else
ximagesize = state->width * 100 / ximagesize;
if (yimagesize == 0 || yimagesize == state->height)
yimagesize = 100;
else
yimagesize = state->height * 100 / yimagesize;
if (ximagesize > 95 && ximagesize < 105 &&
yimagesize > 95 && yimagesize < 105)
ximagesize = yimagesize = 0;
}
if (ximagesize != 0) {
if (!zoomflag)
xzoom = ((float) state->width * ximagesize) / width;
else
xzoom = (float) ximagesize;
width = width * xzoom / 100;
} else
xzoom = 100.0;
if (yimagesize != 0) {
if (!zoomflag)
yzoom = ((float) state->height * yimagesize) / height;
else
yzoom = yimagesize;
height = height * yzoom / 100;
} else
yzoom = 100.0;
#else
xzoom = (float) ximagesize;
yzoom = (float) yimagesize;
image_zoomratio(state, &xzoom, &yzoom, zoomflag, width, height);
width = width * xzoom / 100;
height = height * yzoom / 100;
#endif
#ifndef USE_IMLIB
imagefile = epstoimage(state, fullname, x1, y1, width, height, xzoom,
yzoom);
#else
if (zoomonclk)
zratio = (float) zoomonclk / 100.0 * window_width / width;
else
zratio = 1.0;
imagefile = epstoimage(state, fullname, x1, y1,
width * zratio, height * zratio, xzoom * zratio, yzoom * zratio);
#endif
if (imagefile == NULL) {
fprintf(stderr, "WARN: cannot generate %s file from %s\n",
gsdevice, filename);
XUndefineCursor(display, window);
XFlush(display);
return;
}
if (mgp_flag & FL_VERBOSE) {
fprintf(stderr, "image_load_ps: %s: %s file = %s\n",
filename, gsdevice, imagefile);
}
#ifndef USE_IMLIB
image_load(state, imagefile, numcolor, 100, 100, backflag,
Z_NORMAL | (Z_NORMAL << Z_YSHIFT), centerflag, raise, rotate, zoomonclk);
#else
image_load(state, imagefile, numcolor, 100.0 /zratio, 100.0/zratio, backflag,
Z_NORMAL | (Z_NORMAL << Z_YSHIFT), centerflag, raise, rotate, zoomonclk);
#endif
/* XXX: unlink imagefile in /tmp */
if ((p = strrchr(imagefile, '/')) != NULL)
p++;
else
p = imagefile;
if (strncmp(p, ".gscache", sizeof(".gscache") - 1) != 0)
unlink(imagefile);
if (!backflag)
image_setcolor(state);
}
void
timebar(state)
struct render_state *state;
{
int pos, n, p, barlen;
GC pgc;
if (t_start == 0 || tbar_mode == 0 || caching)
return;
pos = (window_width - 2) * (state->page - 1) / (maxpage - 1);
p = time(NULL) - t_start;
barlen = window_width - window_width * p / t_fin / 60;
if (window_width / 2 < barlen)
pgc = gcgreen;
else if (window_width / 3 < barlen)
pgc = gcyellow;
else
pgc = gcred;
if (barlen > 0) {
XClearArea(display, state->target, 0, window_height - 2,
window_width, 2, 0);
XFillRectangle(display, state->target, pgc,
window_width - barlen, window_height - 1, barlen, 1);
XFillRectangle(display, state->target, pgc,
pos, window_height - 5, 2, 5);
} else if (barlen < 0) {
barlen = - barlen;
n = p / t_fin / 60;
if (n > window_height - 1)
n = window_height - 1;
if (n)
XFillRectangle(display, state->target, gcred,
0, window_height - n,
barlen, n);
XClearArea(display, state->target, 0, window_height - (n + 2),
window_width, n + 2, 0);
XFillRectangle(display, state->target, gcred,
0, window_height - (n + 1),
barlen % window_width, n + 1);
XFillRectangle(display, state->target, gcred,
pos, window_height - (n + 1 + 4),
2, 5);
}
}
static void
process_icon(state, cp)
struct render_state *state;
struct ctrl *cp;
{
u_int i, icon_type, icon_size, icon_x, icon_y, index;
u_long tmp_color;
static struct ctl_words icon_words[] = {
{ 1, 'x', "box", 3 },
{ 2, 'x', "arc", 3 },
{ 3, 'x', "delta1", 6 },
{ 4, 'x', "delta2", 6 },
{ 5, 'x', "delta3", 6 },
{ 6, 'x', "delta4", 6 },
{ 7, 'x', "dia", 3 },
{ 0, 'x', NULL, 0 }
};
XPoint xpoint[4];
static struct icon_point {
int point_num;
XPoint xpoint[4];
} icon_point[] = {{ 3, {{1, 0}, {0, 2}, {2, 2}, {0, 0}}},
{ 3, {{0, 0}, {2, 0}, {1, 2}, {0, 0}}},
{ 3, {{0, 0}, {0, 2}, {2, 1}, {0, 0}}},
{ 3, {{2, 0}, {2, 2}, {0, 1}, {0, 0}}},
{ 4, {{1, 0}, {0, 1}, {1, 2}, {2, 1}}}};
for (i = 0; icon_words[i].ctl_strlen != 0; i++) {
if (!strncasecmp(cp->ctic_value, icon_words[i].ctl_string,
strlen(cp->ctic_value))) {
break;
}
}
icon_type = icon_words[i].ctl_type; /* may be 0 */
icon_size = char_size[caching] * cp->ctic_size / 100;
switch(icon_type){
case 0:
/* this is image */
icon_x = icon_size * 100 / state->width;
icon_y = icon_size * 100 / state->height;
if (icon_x == 0) icon_x = 1;
if (icon_y == 0) icon_y = 1;
tmp_color = fore_color[caching];
fore_color[caching] = cp->ctic_color;
image_load(state, cp->ctic_value, 0, icon_x, icon_y, 0, 0, 1, 0, 0, 0);
fore_color[caching] = tmp_color;
break;
case 1:
/* this is box */
obj_new_icon(state,
state->linewidth + char_size[caching]/2 - icon_size/2,
POSY(icon_size), icon_type, icon_size,
cp->ctic_color, 0, NULL);
state->linewidth += char_size[caching];
break;
case 2:
/* this is arc */
obj_new_icon(state,
state->linewidth + char_size[caching]/2 - icon_size/2,
POSY(icon_size), icon_type, icon_size,
cp->ctic_color, 0, NULL);
state->linewidth += char_size[caching];
break;
case 3:
case 4:
case 5:
case 6:
case 7:
index = icon_type - 3;
icon_x = state->linewidth + (char_size[caching] - icon_size) / 2;
#if 0
icon_y = POSY(icon_size);
#else
icon_y = 0;
#endif
for (i = 0; i < icon_point[index].point_num; i ++){
xpoint[i].x = icon_x +
icon_point[index].xpoint[i].x * icon_size / 2;
xpoint[i].y = icon_y +
icon_point[index].xpoint[i].y * icon_size / 2;
}
obj_new_icon(state, 0, 0, icon_type, icon_size,
cp->ctic_color, icon_point[index].point_num, xpoint);
state->linewidth += char_size[caching];
break;
default:
break;
}
cp = NULL;
state->brankline = 0;
}
static void
draw_bar(state, cp)
struct render_state *state;
struct ctrl *cp;
{
u_int width, swidth, st, len;
XColor col, scol;
static GC gcbar, gcsbar;
static u_long prevcolor = -1;
if (!gcbar) {
gcbar = XCreateGC(display, state->target, 0, 0);
XSetFunction(display, gcbar, GXcopy);
gcsbar = XCreateGC(display, state->target, 0, 0);
XSetFunction(display, gcsbar, GXcopy);
}
col.pixel = cp->ctb_color;
if (col.pixel == -1)
col.pixel = fore_color[caching];
if (col.pixel != prevcolor) {
prevcolor = col.pixel;
col.flags = DoRed|DoGreen|DoBlue;
XQueryColor(display, colormap, &col);
scol.red = col.red / 2;
scol.green = col.green / 2;
scol.blue = col.blue / 2;
if (!XAllocColor(display, colormap, &scol))
scol.pixel = col.pixel;
XSetForeground(display, gcbar, col.pixel);
XSetForeground(display, gcsbar, scol.pixel);
}
width = cp->ctb_width * state->height / 1000;
swidth = width / 2;
width -= swidth;
st = cp->ctb_start * state->width / 100 + state->xoff;
len = cp->ctb_length * state->width / 100;
XFillRectangle(display, state->target, gcbar, st, state->ypos + state->yoff, len, width);
XFillRectangle(display, state->target, gcsbar, st, state->ypos + state->yoff + width, len, swidth);
state->ypos += width + swidth + VERT_GAP(char_size[caching]) / 2;
if (state->maxascent < width + swidth)
state->maxascent = width + swidth;
state->brankline = 0;
}
static void
process_system(state, cp)
struct render_state *state;
struct ctrl *cp;
{
pid_t pid;
int i;
char **argv;
char buf[BUFSIZ];
if (state->repaint) {
if (mgp_flag & FL_VERBOSE) {
fprintf(stderr, "WARN: %%system directive skipping during repaint of same page\n");
}
return; /* don't relaunch on repaint */
}
if (mgp_flag & FL_NOFORK) {
if (mgp_flag & FL_VERBOSE) {
fprintf(stderr, "WARN: %%system ");
for (i = 0; i < cp->cta_argc; i++) {
fprintf(stderr, "%c%s", (i == 0) ? '"' : ' ',
cp->cta_argv[i]);
}
fprintf(stderr, "\": directive skipped\n");
}
return;
}
if (checkchild(cp) != (pid_t)-1)
return; /*already running*/
/*
* edit argument.
* if we have X11 geometry string
*/
argv = (char **)cp->cta_argv;
for (i = 0; i < cp->cta_argc; i++) {
if (*(argv[i]) == '%')
break;
}
if (i < cp->cta_argc) {
char *p;
char *q;
int myxpos, myypos;
int rootxsiz, rootysiz;
int xsiz, ysiz;
int xloc, yloc;
int mode;
{
XWindowAttributes wa;
Window junkwin;
int junk;
XGetWindowAttributes(display, window, &wa);
XTranslateCoordinates(display, window, wa.root,
-wa.border_width, -wa.border_width,
&myxpos, &myypos, &junkwin);
XGetGeometry(display, wa.root, &junkwin, &junk, &junk,
&rootxsiz, &rootysiz, &junk, &junk);
}
argv = (char **)malloc((cp->cta_argc + 1) * sizeof(char *));
memcpy(argv, cp->cta_argv, (cp->cta_argc + 1) * sizeof(char *));
p = argv[i];
p++; /*drop percent char*/
q = buf;
*q = '\0';
mode = XParseGeometry(p, &xloc, &yloc, &xsiz, &ysiz);
if (mode == 0)
goto fail;
if ((mode & WidthValue) && (mode & HeightValue)) {
sprintf(q, "%dx%d", xsiz * state->width / 100,
ysiz * state->height / 100);
q += strlen(q);
}
if ((mode & XValue) && (mode & YValue)) {
xloc = xloc * state->width / 100;
yloc = yloc * state->height / 100;
if (mode & XNegative)
xloc = rootxsiz - myxpos + state->width - xloc;
else
xloc += myxpos;
if (mode & YNegative)
yloc = rootysiz - myypos + state->height - yloc;
else
yloc += myypos;
sprintf(q, "+%d+%d", xloc + state->xoff, yloc + state->yoff);
}
if (mgp_flag & FL_VERBOSE) {
fprintf(stderr, "relative geometry: "
"%s (presentation %dx%d+%d+%d)\n",
argv[i], state->width, state->height,
myxpos, myypos);
fprintf(stderr, "\t-> %s\n", buf);
}
argv[i] = buf;
if (0) {
fail:
if (mgp_flag & FL_VERBOSE) {
fprintf(stderr,
"relative geometry: %s failed\n",
argv[i]);
}
}
}
pid = fork();
if (pid < 0) {
perror("fork");
cleanup(-1);
} else if (pid == 0) {
execvp(argv[0], argv);
perror(argv[0]);
_exit(1);
}
if (!cp->cta_flag) /*will be purged at the end of page*/
regchild(pid, cp, -1, state->page);
else
regchild(pid, cp, -1, cp->cta_flag);
}
static void
process_xsystem(state, cp)
struct render_state *state;
struct ctrl *cp;
{
pid_t pid;
int i, dumint;
int xloc, yloc;
int xsiz, ysiz;
char **argv;
char buf[BUFSIZ];
Window window_id, dumwin;
if (state->repaint) {
if (mgp_flag & FL_VERBOSE) {
fprintf(stderr, "WARN: %%system directive skipping during repaint of same page\n");
}
return; /* don't relaunch on repaint */
}
if (mgp_flag & FL_NOFORK) {
if (mgp_flag & FL_VERBOSE) {
fprintf(stderr, "WARN: %%system ");
for (i = 0; i < cp->cta_argc; i++) {
fprintf(stderr, "%c%s", (i == 0) ? '"' : ' ',
cp->cta_argv[i]);
}
fprintf(stderr, "\": directive skipped\n");
}
return;
}
/*
* edit argument.
* if we have X11 geometry string
*/
argv = (char **)cp->cta_argv;
for (i = 0; i < cp->cta_argc; i++) {
if (!strncmp(argv[i], "-geom", 5))
break;
}
i ++;
if (i < cp->cta_argc) {
char *p;
char *q;
int mode;
argv = (char **)malloc((cp->cta_argc + 1) * sizeof(char *)); /* XXX seems to be never freed */
memcpy(argv, cp->cta_argv, (cp->cta_argc + 1) * sizeof(char *));
p = argv[i];
if (*p == '%') p++; /*drop percent char*/
q = buf;
*q = '\0';
mode = XParseGeometry(p, &xloc, &yloc, &xsiz, &ysiz);
if (mode == 0)
goto fail;
if ((mode & WidthValue) && (mode & HeightValue)) {
xsiz = xsiz * state->width / 100;
ysiz = ysiz * state->height / 100;
sprintf(q, "%dx%d", xsiz, ysiz);
q += strlen(q);
}
/* make window raise outside of display */
sprintf(q, "+%d+%d", DisplayWidth(display, DefaultScreen(display)),
DisplayHeight(display, DefaultScreen(display)));
if (mgp_flag & FL_VERBOSE) {
fprintf(stderr, "relative geometry: "
"%s (presentation %dx%d+%d+%d)\n",
argv[i], state->width, state->height,
xloc, yloc);
fprintf(stderr, "\t-> %s\n", buf);
}
argv[i] = buf;
if (0) {
fail:
if (mgp_flag & FL_VERBOSE) {
fprintf(stderr,
"relative geometry: %s failed\n",
argv[i]);
}
}
} else {
char geom_arg1[] = {"-geometry"};
char geom_arg2[512];
sprintf(geom_arg2, "+%d+%d", DisplayWidth(display,
DefaultScreen(display)),
DisplayHeight(display, DefaultScreen(display)));
argv[cp->cta_argc] = geom_arg1;
argv[cp->cta_argc+1] = geom_arg2;
/*
** XXX argv is now not generally NULL-terminated
** the maximal allowed size of argv is ganatied to be
** argc+2 so no NULL can appended
*/
}
if ((window_id = checkchildwin(cp)) != (Window)-1)
goto finish; /*already running*/
if (checkchild(cp) != (pid_t)-1)
return; /*already running*/
pid = fork();
if (pid < 0) {
perror("fork");
cleanup(-1);
} else if (pid == 0){
usleep(EXEC_DELAY);
execvp(argv[0], argv);
perror(argv[0]);
_exit(1);
}
window_id = search_child_window();
if (!cp->cta_flag) /*will be purged at the end of page*/
regchild(pid, cp, window_id, state->page);
else
regchild(pid, cp, window_id, cp->cta_flag);
if (window_id != -1)
reparent_child_window(window_id, window_width, window_height);
else {
if (mgp_flag & FL_VERBOSE) {
fprintf(stderr, "WARN: %%xsystem cannot find child window:");
for (i = 0; i < cp->cta_argc; i++) {
fprintf(stderr, "%c%s", (i == 0) ? '"' : ' ',
cp->cta_argv[i]);
}
fprintf(stderr, "\"\n");
}
return;
}
finish:
XGetGeometry(display, window_id, &dumwin,
&xloc, &yloc, &xsiz, &ysiz, &dumint, &dumint);
state->linewidth = xsiz;
xloc = set_position(state) + state->xoff
+ (state->tabxprefix ? state->tabxprefix : state->xprefix);
yloc = state->ypos + state->yoff;
XMoveWindow(display, window_id, xloc, yloc);
state->ypos += ysiz;
#if 0 /* not implemented yet */
state->linewidth += xsiz;
state->maxascent += ysiz;
#endif
}
/*
* tsystem does mostly the same like xsystem, but identifies the created
* window by its name
*
* this hack is done because at some windowmanagers occures additional
* xreparentevents, which cause xsystem to fail
*
* it is possible, that the title of some applications is reseted, than
* tsystem will fail
*/
static void
process_tsystem(state, cp)
struct render_state *state;
struct ctrl *cp;
{
pid_t pid;
int i, dumint, argc;
int xloc, yloc;
int xsiz, ysiz;
char **argv;
char buf[BUFSIZ];
char title_arg1[] = "-title";
char title_arg2[BUFSIZ];
static unsigned int magicCnt=0;
Window window_id, dumwin;
if (mgp_flag & FL_NOFORK) {
if (mgp_flag & FL_VERBOSE) {
fprintf(stderr, "WARN: %%system ");
for (i = 0; i < cp->cta_argc; i++) {
fprintf(stderr, "%c%s", (i == 0) ? '"' : ' ',
cp->cta_argv[i]);
}
fprintf(stderr, "\": directive skipped\n");
}
return;
}
/*
* edit argument.
* allways copy the argument vector, for adding -title magictitle
* it's assumed, that there is not -title in the argument vector
*/
argc=cp->cta_argc;
argv = (char **)malloc((argc + 5) * sizeof(char *));
/* +5 for NULL, title and potentally geometry */
memcpy(argv, cp->cta_argv, (argc + 1) * sizeof(char *));
/*
* search for X11 geometry string
*/
for (i = 0; i < argc; i++) {
if (!strncmp(argv[i], "-geom", 5))
break;
}
i ++;
if (i < argc) {
/*
* we have X11 geometry string
*/
char *p;
char *q;
int mode;
p = argv[i];
if (*p == '%') p++; /*drop percent char*/
q = buf;
*q = '\0';
mode = XParseGeometry(p, &xloc, &yloc, &xsiz, &ysiz);
if (mode == 0)
goto fail;
if ((mode & WidthValue) && (mode & HeightValue)) {
xsiz = xsiz * state->width / 100;
ysiz = ysiz * state->height / 100;
sprintf(q, "%dx%d", xsiz, ysiz);
q += strlen(q);
}
/* make window raise outside of display */
/* XXX potentially overflow, but BUFSIZ should be alway large enough*/
sprintf(q, "+%d+%d", DisplayWidth(display, DefaultScreen(display)),
DisplayHeight(display, DefaultScreen(display)));
if (mgp_flag & FL_VERBOSE) {
fprintf(stderr, "relative geometry: "
"%s (presentation %dx%d+%d+%d)\n",
argv[i], state->width, state->height,
xloc, yloc);
fprintf(stderr, "\t-> %s\n", buf);
}
argv[i] = buf;
if (0) {
fail:
if (mgp_flag & FL_VERBOSE) {
fprintf(stderr,
"relative geometry: %s failed\n",
argv[i]);
}
}
} else {
/*
* we do not have X11 geometry string
*/
char geom_arg1[] = "-geometry";
char geom_arg2[512];
sprintf(geom_arg2, "+%d+%d", DisplayWidth(display,
DefaultScreen(display)),
DisplayHeight(display, DefaultScreen(display)));
argv[argc] = geom_arg1;
argv[argc+1] = geom_arg2;
argc += 2;
}
/*
* adding magic title and incrementing magicCnt
* guaranteeing the NULL-termination of argv
*/
snprintf(title_arg2, BUFSIZ, "magictitle %u", magicCnt++);
argv[argc] = title_arg1;
argv[argc+1] = title_arg2;
argv[argc+2] = NULL;
argc += 2; /* seems not to be nessesary */
if ((window_id = checkchildwin(cp)) != (Window)-1)
goto finish; /*already running*/
if (checkchild(cp) != (pid_t)-1) {
free(argv);
return; /*already running*/
}
/*
* using vfork() to first run the child
*/
pid = vfork();
if (pid < 0) {
perror("fork");
cleanup(-1);
} else if (pid == 0){
execvp(argv[0], argv);
perror(argv[0]);
_exit(1);
}
window_id = tsearch_child_window(title_arg2);
if (!cp->cta_flag) /*will be purged at the end of page*/
regchild(pid, cp, window_id, state->page);
else
regchild(pid, cp, window_id, cp->cta_flag);
if (0 == window_id) {
if (mgp_flag & FL_VERBOSE) {
fprintf(stderr, "WARN: %%tsystem can not find child window:");
for (i = 0; i < cp->cta_argc; i++) {
fprintf(stderr, "%c%s", (i == 0) ? '"' : ' ',
cp->cta_argv[i]);
}
fprintf(stderr, "\"\n");
}
return;
}
finish:
{
Window root, par, *child;
int newxloc, newyloc;
unsigned int nchild;
XGetGeometry(display, window_id, &dumwin,
&xloc, &yloc, &xsiz, &ysiz, &dumint, &dumint);
XQueryTree(display, window_id, &root, &par, &child, &nchild);
if(child) XFree(child);
state->linewidth = xsiz;
newxloc = set_position(state) + state->xoff
+ (state->tabxprefix ? state->tabxprefix : state->xprefix);
newyloc = state->ypos + state->yoff;
while((par!=window) || (xloc != newxloc)) {
/*
* this hack should correct not moved windows
* if found, that XMoveWindow, XReparentWindow returns success,
* but the window is sometimes not moved etc in ion
*/
XReparentWindow(display, window_id, window, newxloc, newyloc);
XGetGeometry(display, window_id, &dumwin,
&xloc, &yloc, &xsiz, &ysiz, &dumint, &dumint);
XQueryTree(display, window_id, &root, &par, &child, &nchild);
if(child) XFree(child);
}
}
state->ypos += ysiz;
free(argv);
#if 0 /* not implemented yet */
state->linewidth += xsiz;
state->maxascent += ysiz;
#endif
}
Window
search_child_window()
{
XEvent e;
int fd, found = 0;
fd_set fdset, dumfdset;
struct timeval timeout;
fd = ConnectionNumber(display);
/* waiting for 2 second */
timeout.tv_sec = 2;
timeout.tv_usec = 0;
/* get all client's ReparentNotify event */
XSelectInput(display, DefaultRootWindow(display),
SubstructureNotifyMask);
while (!found) {
while (XEventsQueued(display, QueuedAfterFlush) > 0) {
XNextEvent(display, &e);
if (e.type == ReparentNotify){
found = 1;
break;
}
}
if (found) break;
FD_ZERO(&fdset);
FD_SET(fd, &fdset);
FD_ZERO(&dumfdset);
if (!select(fd+1, &fdset, &dumfdset, &dumfdset, &timeout))
break;
}
XSelectInput(display, DefaultRootWindow(display), NoEventMask);
if (found == 1)
return e.xreparent.window;
else
return (Window)-1;
}
/*
** looks for a window with the specified name
** return (Window)0 if not found
*/
Window
tsearch_child_window(const char *name)
{
/* 100 ms between two searches for the specified window */
#define WAITTIME 100000
/* maximal wait time = 1 minute */
#define WAITCYCLES 60000000 / WAITTIME
int maxWait=WAITCYCLES;
Window w=0;
while(maxWait--)
{
if((w = getNamedWindow(name, DefaultRootWindow(display))))
break;
usleep(WAITTIME);
}
return w;
#undef WAITCYCLES
#undef WAITTIME
}
Window
getNamedWindow(const char *name, Window top)
{
Window w=0;
Window *child;
Window dum;
unsigned int nchild,i;
char *w_name;
if(XFetchName(display, top, &w_name) && (!strcmp(w_name, name)))
return top;
if(!XQueryTree(display, top, &dum, &dum, &child, &nchild))
return (Window)0;
for(i=0; i<nchild; ++i)
{
if((w = getNamedWindow(name, child[i])))
break;
}
if(child) XFree((char *)child);
return w;
}
void
reparent_child_window(child_window, x, y)
Window child_window;
int x,y;
{
Window dummyroot, *dummywin;
Window target, parent;
u_int dumint;
target = child_window;
while (1) {
XQueryTree(display, target, &dummyroot, &parent, &dummywin,
&dumint);
if (parent == dummyroot)
break;
XFree(dummywin);
target = parent;
}
XReparentWindow(display, child_window, window, x, y);
#if 0
XDestroyWindow(display, target);
#endif
}
void
draw_reinit(state)
struct render_state *state;
{
/* invalidate the background image cache */
bg_ctl_last = bg_ctl_cache = NULL;
x_registerseed(state, NULL, NULL);
}
static char *
epstoimage(state, epsfile, x, y, width, height, xzoom, yzoom)
struct render_state *state;
char *epsfile;
int x, y, width, height, xzoom, yzoom;
{
int fd, pfd[3][2];
int i, j;
FILE *fp;
int status;
pid_t pid = 0, gspid;
char *cp;
int scale = 1;
struct stat stbuf;
XColor back;
char geom[32], device[64], scalebuf[32];
static char imagefile[MAXPATHLEN];
void (*sigpipe_handler)();
fd = -1;
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++)
pfd[i][j] = -1;
}
strcpy(imagefile, epsfile);
if ((cp = strrchr(imagefile, '/')) != NULL)
cp++;
else
cp = imagefile;
sprintf(cp, ".gscache.%s.%dx%d", epsfile + (cp - imagefile),
width, height);
if (verbose)
fprintf(stderr, "gs cache filename: %s\n", imagefile);
/* check if we got any cached image file already. */
if (stat(imagefile, &stbuf) == 0) {
time_t cachetime;
off_t cachesize;
cachetime = stbuf.st_mtime;
cachesize = stbuf.st_size;
if (stat(epsfile, &stbuf) == 0) {
if (stbuf.st_mtime < cachetime && cachesize > 0) {
if (verbose) {
fprintf(stderr, "gs cache valid, "
"using it \n");
}
return imagefile;
}
}
if (verbose) {
fprintf(stderr, "gs cache looks older than source, "
"generate again\n");
}
} else {
if (verbose) {
fprintf(stderr, "gs cache not found, convert eps\n");
}
}
if (verbose)
fprintf(stderr, "converting eps file...\n");
/* convert eps file into readable form. */
sprintf(device, "-sDEVICE=%s", gsdevice);
/*
* a suffix of +scale in the device tipe means produce a larger
* image that can be scaled later for better antialiasing.
*/
if ((cp = strchr(device, '+')) != NULL) {
*cp++ = '\0';
scale = atoi(cp);
if (scale <= 0)
scale = 2;
xzoom *= scale;
yzoom *= scale;
width *= scale;
height *= scale;
}
if (width == 0 || height == 0) {
fprintf(stderr, "WARN: epstoimage: scale=%d, xzoom=%d, "
"yzoom=%d, width=%d, height=%d\n",
scale, xzoom, yzoom, width, height);
return NULL;
}
if (scale != 1)
sprintf(scalebuf, "%f", 1. / (double)scale);
sprintf(geom, "-g%dx%d", width, height);
/* generate cache file. */
fd = open(imagefile, O_RDWR|O_CREAT|O_TRUNC, 0600);
if (fd < 0) {
/* last resort: generate output onto /tmp. */
if ((cp = getenv("TMPDIR")) == NULL)
cp = "/tmp";
if (verbose) {
fprintf(stderr, "could not write to \"%s\", using "
"%s\n", imagefile, cp);
}
strcpy(imagefile, cp);
strcat(imagefile, "/mgp.XXXXXXXX");
if ((fd = mkstemp(imagefile)) < 0) {
perror(imagefile);
return NULL;
}
}
if (scale != 1) {
if (pipe(pfd[2]) < 0) {
perror("pipe");
goto error;
}
if ((pid = vfork()) == 0) {
close(pfd[2][1]);
dup2(pfd[2][0], 0); close(pfd[2][0]);
dup2(fd, 1); close(fd);
if (verbose)
fprintf(stderr, "epstoimage: \"pnmdepth 256\"\n");
close(2); /* XXX suppress message */
execlp("pnmdepth", "pnmdepth", "255", NULL);
perror("pnmdepth");
_exit(1);
}
if (pid < 0) {
perror("vfork");
goto error;
}
close(pfd[2][0]); pfd[2][0] = -1;
close(fd); fd = -1;
if (pipe(pfd[1]) < 0) {
perror("pipe");
goto error;
}
if ((gspid = vfork()) == 0) {
close(pfd[1][1]);
dup2(pfd[1][0], 0); close(pfd[1][0]);
dup2(pfd[2][1], 1); close(pfd[2][1]);
if (verbose)
fprintf(stderr, "epstoimage: \"pnmscale %s\"\n", scalebuf);
close(2); /* XXX suppress message */
execlp("pnmscale", "pnmscale", scalebuf, NULL);
perror("pnmscale");
_exit(1);
}
if (gspid < 0) {
perror("vfork");
goto error;
}
close(pfd[2][1]); pfd[2][1] = -1;
close(pfd[1][0]); pfd[1][0] = -1;
fd = pfd[1][1]; pfd[1][1] = -1;
}
if (pipe(pfd[0]) < 0) {
perror("pipe");
goto error;
}
if ((gspid = vfork()) == 0) {
close(pfd[0][1]);
dup2(pfd[0][0], 0); close(pfd[0][0]);
dup2(fd, 1); close(fd);
if (verbose)
fprintf(stderr, "epstoimage: \"gs %s %s -sOutputFile=- -q -\"\n", geom, device);
execlp("gs", "gs", geom, device, "-sOutputFile=-", "-q", "-", NULL);
perror("gs");
_exit(1);
}
close(fd); fd = -1;
close(pfd[0][0]); pfd[0][0] = -1;
if ((fp = fdopen(pfd[0][1], "w")) == NULL) {
fprintf(stderr, "fdopen failed\n");
goto error;
}
sigpipe_handler = signal(SIGPIPE, SIG_IGN); /* XXX: avoid SIGPIPE */
pfd[0][1] = -1;
fprintf(fp, "%f %f scale\n", (double)xzoom/100., (double)yzoom/100.);
fprintf(fp, "%d %d translate\n", -1 * x, -1 * y);
fprintf(fp, "(%s) run\n", epsfile);
fprintf(fp, "showpage\n");
fprintf(fp, "quit\n");
fflush(fp);
fclose(fp);
signal(SIGPIPE, sigpipe_handler);
if (!pid)
pid = gspid;
while (waitpid(pid, &status, 0) < 0) {
if (errno != EINTR)
break;
}
if (stat(imagefile, &stbuf) == 0 && stbuf.st_size > 0)
return imagefile;
error:
if (fd >= 0) close(fd);
for (i = 0; i < 3; i++) {
for (j = 0; j < 2; j++)
if (pfd[i][j] >= 0)
close(pfd[i][j]);
}
if (imagefile[0])
unlink(imagefile);
return NULL;
}
static void
image_setcolor(state)
struct render_state *state;
{
struct render_object *obj;
Image *image;
int i;
Intensity *red, *green, *blue;
XColor fore, back;
obj = state->objlast;
if (obj->type != O_IMAGE)
return;
image = obj->data.image.image;
if (image->trans >= 0)
return;
switch (image->type) {
case IBITMAP:
/*
* XXX: Actually, no one comes here.
* This translation for IBITMAP was done by image_load().
*/
fore.pixel = fore_color[caching];
fore.flags = DoRed | DoGreen | DoBlue;
XQueryColor(display, colormap, &fore);
image->rgb.red [1] = fore.red;
image->rgb.green[1] = fore.green;
image->rgb.blue [1] = fore.blue;
image->trans = 0;
break;
case IRGB:
red = image->rgb.red;
green = image->rgb.green;
blue = image->rgb.blue;
for (i = 0; i < image->rgb.used; i++) {
if (red[i] != green[i] || red[i] != blue[i])
return;
}
/* grayscale */
fore.pixel = fore_color[caching];
fore.flags = DoRed | DoGreen | DoBlue;
XQueryColor(display, colormap, &fore);
if (!COMPLEX_BGIMAGE) {
back.pixel = back_color[caching];
back.flags = DoRed | DoGreen | DoBlue;
XQueryColor(display, colormap, &back);
} else {
int x, y, bpl;
byte *p;
Pixel d;
/* XXX: use background color of center position */
x = (obj->x + image->width/2) % bgpixmap[bgindex].image->width;
y = (state->ypos + image->height/2)
% bgpixmap[bgindex].image->height;
bpl = bgpixmap[bgindex].image->pixlen;
p = bgpixmap[bgindex].image->data
+ (bgpixmap[bgindex].image->width * y + x) * bpl;
d = memToVal(p, bpl);
if (bgpixmap[bgindex].image->type == ITRUE) {
back.red = TRUE_RED(d) << 8;
back.green = TRUE_GREEN(d) << 8;
back.blue = TRUE_BLUE(d) << 8;
} else {
back.red = bgpixmap[bgindex].image->rgb.red [d];
back.green = bgpixmap[bgindex].image->rgb.green[d];
back.blue = bgpixmap[bgindex].image->rgb.blue [d];
}
}
for (i = 0; i < image->rgb.used; i++) {
if (red[i] >= 65000) /*XXX*/
image->trans = i;
red[i] = (back.red * red [i]
+ fore.red * (65535-red [i])) / 65535;
green[i] = (back.green * green[i]
+ fore.green * (65535-green[i])) / 65535;
blue[i] = (back.blue * blue [i]
+ fore.blue * (65535-blue [i])) / 65535;
}
break;
case ITRUE:
/* XXX: assume background color is on the left right corner */
image->trans = memToVal(image->data, image->pixlen);
}
}
#ifdef FREETYPE
static u_int
draw_onechar_tf(state, code, x, y, size, registry, lastchar, charset16)
struct render_state *state;
u_int code;
int x, y;
u_int size;
char *registry;
int lastchar;
int charset16;
{
struct tfont *tfc;
int charlen;
tfc = tfc_get(code, size, 1, registry, charset16);
draw_line_itemsize(state, tfc->ascent, tfc->descent, 0);
/* usually */
charlen = tfc->charlen;
/*
* for the very first char on the line, the char may goes over the
* edge at the lefthand side. offset the image to the right so that
* whole part of the bitmap appears on the screen.
* beware the sign-ness of tfc->xoff.
*/
if (x + tfc->xoff < 0) {
x -= tfc->xoff;
charlen -= tfc->xoff;
}
/*
* For the last char, make sure that the whole part of the bitmap
* appears on the screen.
*/
if (lastchar && tfc->charlen < tfc->xoff + tfc->width)
charlen += tfc->xoff + tfc->width - tfc->charlen;
/*
* (x, y): left side, baseline of the font (FreeType font origin)
*/
obj_new_tfont(state, x, y, tfc);
return charlen;
}
#endif /* FREETYPE */
static void
x_registerseed(state, seed, registry)
struct render_state *state;
char *seed;
char *registry;
{
char tmp1[BUFSIZ], tmp2[BUFSIZ];
char *p;
struct ctrl *cp;
int hyphen;
/* if both of arguments are NULL, initialize */
if (!seed && !registry) {
if (state->xfont)
ctlfree(state->xfont);
state->xfont = NULL;
return;
}
if (!registry)
registry = "iso8859-1";
/* canonicalize seed */
hyphen = 0;
for (p = seed; *p; p++) {
if (*p == '-')
hyphen++;
if (*p == ':') {
hyphen = 0;
break;
}
}
switch (hyphen) {
case 0:
/* maybe alias, don't canonicalize */
break;
case 1:
sprintf(tmp1, "%s-*", seed);
seed = tmp1;
break;
case 2:
case XLFD_HYPHEN:
/* as is */
break;
default:
fprintf(stderr, "invalid XFONT seed <%s>\n", seed);
break;
}
/* canonicalize registry */
if (!registry)
registry = "iso8859-1";
hyphen = 0;
for (p = registry; *p; p++) {
if (*p == '-')
hyphen++;
}
switch (hyphen) {
case 0:
sprintf(tmp2, "%s-*", registry);
registry = tmp2;
break;
case 1:
/* as is */
break;
default:
fprintf(stderr, "invalid XFONT registry <%s>\n", registry);
exit(1);
}
cp = NULL;
for (cp = state->xfont; cp; cp = cp->ct_next) {
if (!cp->ctc2_value2) continue;
if (strcmp(cp->ctc2_value2, registry) == 0)
break;
}
if (cp) {
if (!strcmp(cp->ctc2_value1, seed)) return;
free(cp->ctc2_value1);
cp->ctc2_value1 = strdup(seed);
} else {
cp = ctlalloc1(CTL_XFONT2);
cp->ctc2_value1 = strdup(seed);
cp->ctc2_value2 = strdup(registry);
cp->ct_next = state->xfont;
state->xfont = cp;
}
}
static char *
x_findseed(state, registry)
struct render_state *state;
char *registry;
{
struct ctrl *cp;
if (!registry)
registry = "iso8859-1";
for (cp = state->xfont; cp; cp = cp->ct_next) {
if (strcmp(cp->ctc2_value2, registry) == 0) {
return cp->ctc2_value1;
}
}
return "*-*-*"; /*anything, canonicalized*/
}
/* cache specified page */
static void
cache_page(state, page)
struct render_state *state;
int page;
{
struct ctrl *tmp_bg_ctl;
int tmp_bgindex;
/* we don't need caching */
if (cached_page == page || page > maxpage || page <= 0)
return;
if (!page_attribute[page].pg_linenum) return;
XFlush(display);
memset(state, 0, sizeof(struct render_state));
state->target = cachewin; /*XXX*/
state->width = window_width;
state->height = window_height;
state->page = page;
caching = 1;
tmp_bg_ctl = bg_ctl;
tmp_bgindex = bgindex;
if (verbose){
printf("now caching %d page ...\n", page);
fflush(stdout);
}
draw_page(state, NULL);
if (verbose){
printf("caching done \n");
}
caching = 0;
cached_page = page;
bg_ctl = tmp_bg_ctl;
bgindex = tmp_bgindex;
}
static void
set_from_cache(state)
struct render_state *state;
{
int i;
char_size[0] = char_size[1];
horiz_gap[0] = horiz_gap[1];
vert_gap[0] = vert_gap[1];
fore_color[0] = fore_color[1];
back_color[0] = back_color[1];
ctrl_color[0] = ctrl_color[1];
b_quality[0] = b_quality[1];
memcpy(state, &cache_state, sizeof(struct render_state));
state->target = window;
XSetForeground(display, gcfore, fore_color[0]);
XSetBackground(display, gcfore, back_color[0]);
bg_ctl = bg_ctl_last = bg_ctl_cache;
if (bg_ctl){
for (i = 0; i < MAXBGPIXMAP; i ++){
if (bgpixmap[i].ctl && ctlcmp(bg_ctl, bgpixmap[i].ctl) == 0)
bgindex = i;
}
set_background_pixmap(bg_ctl);
}
switch(cache_effect){
case 1:
cache_effect1();
break;
case 2:
cache_effect2();
break;
default:
break;
}
XCopyArea(display, cachewin, window, gc_cache,
0, 0, window_width, window_height, 0, 0);
XFlush(display);
}
void
reset_background_pixmap()
{
int i = 0;
bg_ctl_last = NULL;
bg_ctl_cache = NULL;
for (i = 0; i < MAXBGPIXMAP; i ++) {
if (bgpixmap[i].image){
XFreePixmap(display, bgpixmap[i].pixmap);
freeXImage(bgpixmap[i].image, bgpixmap[i].ximageinfo);
freeImage(bgpixmap[i].image);
}
bgpixmap[i].ctl = NULL;
bgpixmap[i].image = NULL;
bgpixmap[i].ximageinfo = NULL;
}
}
static void
cache_effect1()
{
int x, step;
step = cache_value ? window_width / cache_value : 1;
if (!step) step = 1;
for (x = window_width; x > step; x -= step){
XCopyArea(display, window, window, gc_cache,
step, 0, window_width - step, window_height, 0, 0);
XCopyArea(display, cachewin, window, gc_cache,
window_width - x, 0, step, window_height,
window_width - step, 0);
#if 1
XSync(display, False);
#else
XFlush(display);
#endif
#if 1/*ONOE*/
{ XEvent e;
if (XCheckMaskEvent(display, ~NoEventMask, &e) == True) {
printf("event type=%d\n", e.type);
XPutBackEvent(display, &e);
break;
}
}
#endif
}
}
static void
cache_effect2()
{
int x, step;
step = cache_value ? window_width / (cache_value * 2) : 1;
if (!step) step = 1;
for (x = 0; x < window_width; x += step){
XCopyArea(display, window, window, gc_cache,
x, 0, window_width - step -x , window_height, x + step, 0);
XCopyArea(display, cachewin, window, gc_cache,
x, 0, step, window_height, x, 0);
XFlush(display);
}
}
/*
pcache directive process
*/
static void
pcache_process(page)
int page;
{
if (!pcache.flag)
return;
if (pcache.page != page)
return;
if (pcache.mgpflag)
mgp_flag |= FL_FRDCACHE;
else
mgp_flag ^= FL_FRDCACHE;
cache_mode = pcache.mode;
cache_effect = pcache.effect;
cache_value = pcache.value;
pcache.flag = 0;
}
/*
predraw: if this page contains texts only,
draw page in pixmap once, then copy to window.
*/
static void
predraw(state)
struct render_state *state;
{
if (!caching && cached_page != state->page
&& page_attribute[state->page].pg_text
&& page_attribute[state->page].pg_linenum){
cache_page(&cache_state, state->page);
set_from_cache(state);
pcache_process(state->page);
}
}
static void
get_background_pixmap(ctl, state)
struct ctrl *ctl;
struct render_state *state;
{
int i;
/*
* check if background is already cached
*/
for (i = 0; i < MAXBGPIXMAP; i ++){
if (bgpixmap[i].ctl && ctlcmp(ctl, bgpixmap[i].ctl) == 0){
bgindex = i;
return;
}
}
if (i == MAXBGPIXMAP){
/* this background is not cached, we have to generate one */
switch(ctl->ct_op){
case CTL_BIMAGE:
image_load(state, ctl->ctm_fname, ctl->ctm_numcolor,
ctl->ctm_ximagesize, ctl->ctm_yimagesize, 1,
ctl->ctm_zoomflag, 0, 0, ctl->ctm_rotate, 0);
break;
case CTL_BGRAD:
back_gradation(state, &ctl->ct_val.ctrl_grad);
break;
case CTL_BACK:
break;
default:
fprintf(stderr, "fatal error in get_background_pixmap()\n");
cleanup(-1);
break;
}
}
}
static void
regist_background_pixmap(ximageinfo, image)
XImageInfo *ximageinfo;
Image *image;
{
Pixmap pixmap;
int i, j;
/* search empty slot */
for (i = 0; i < MAXBGPIXMAP; i ++){
if (bgpixmap[i].ctl == NULL)
break;
}
if (i == MAXBGPIXMAP){
/* no empty slot, we need to make one */
XFreePixmap(display, bgpixmap[MAXBGPIXMAP -1].pixmap);
freeXImage(bgpixmap[MAXBGPIXMAP -1].image,
bgpixmap[MAXBGPIXMAP -1].ximageinfo);
freeImage(bgpixmap[MAXBGPIXMAP -1].image);
for (j = MAXBGPIXMAP -2; j >= 0; j --){
bgpixmap[j +1].ctl = bgpixmap[j].ctl;
bgpixmap[j +1].pixmap = bgpixmap[j].pixmap;
bgpixmap[j +1].image = bgpixmap[j].image;
bgpixmap[j +1].ximageinfo = bgpixmap[j].ximageinfo;
}
bg_ctl_last = NULL;
i = 0;
}
pixmap = ximageToPixmap(display,
RootWindow(display, screen), ximageinfo);
bgpixmap[i].ctl = bg_ctl;
bgpixmap[i].pixmap = pixmap;
bgpixmap[i].image = image;
bgpixmap[i].ximageinfo = ximageinfo;
bgindex = i;
}
static void
set_background_pixmap(ctl)
struct ctrl *ctl;
{
int i;
switch(ctl->ct_op){
case CTL_BIMAGE:
case CTL_BGRAD:
for (i = 0; i < MAXBGPIXMAP; i ++){
if (bgpixmap[i].ctl && ctlcmp(ctl, bgpixmap[i].ctl) == 0)
break;
}
if (i == MAXBGPIXMAP){
fprintf(stderr, "fatal error in set_background_pixmap()\n");
cleanup(-1);
}
XSetWindowBackgroundPixmap(display, window, bgpixmap[i].pixmap);
break;
case CTL_BACK:
XSetWindowBackground(display, window, ctl->ctl_value);
break;
default:
fprintf(stderr, "fatal error in set_background_pixmap() op=%d\n",
ctl->ct_op);
cleanup(-1);
break;
}
}
/*
* Clear target pixmap
*/
static void
XClearPixmap(display, target)
Display *display;
Drawable target;
{
int i;
int x, y, width, height;
XImage *xim;
switch(bg_ctl->ct_op){
case CTL_BIMAGE:
case CTL_BGRAD:
for (i = 0; i < MAXBGPIXMAP; i ++){
if (bgpixmap[i].ctl && ctlcmp(bg_ctl, bgpixmap[i].ctl) == 0)
break;
}
if (i == MAXBGPIXMAP){
fprintf(stderr, "fatal error in XClearPixmap()\n");
cleanup(-1);
}
xim = bgpixmap[i].ximageinfo->ximage;
for (y = 0; y < window_height; y += xim->height)
for (x = 0; x < window_width; x += xim->width)
XPutImage(display, target, gc_cache,
xim, 0, 0, x, y,
xim->width, xim->height);
break;
case CTL_BACK:
XSetForeground(display, gc_cache, bg_ctl->ctl_value);
XFillRectangle(display, target,
gc_cache, 0, 0, window_width, window_height);
break;
default:
fprintf(stderr, "fatal error in XClearPixmap()\n");
cleanup(-1);
break;
}
}
int
get_regid(registry)
char *registry;
{
char *p;
if (!registry || registry[0] == '\0') return 0;
if (strlen(registry) == 9 && !strncmp("iso8859-", registry, 8) &&
registry[8] >= '1' && registry[8] <= '4') {
p = registry + 8;
return atoi(p) -1;
} else
return -1;
}
#ifdef MNG
int
obj_new_anim(state, x, y, width, height, filename, key)
struct render_state *state;
int x, y;
int width, height;
char *filename;
void *key;
{
struct render_object *obj;
obj = obj_alloc(state);
if (obj == NULL)
return 0;
obj->x = x;
obj->y = y;
obj->type = O_ANIM;
obj->data.anim.width = width;
obj->data.anim.height = height;
obj->data.anim.filename = strdup(filename);
obj->data.anim.key = key; /* for regchild */
obj->ascent = 0; /*XXX*/
obj->descent = height; /*XXX*/
obj->vertloc = VL_CENTER;
return 1;
}
static void
obj_draw_anim(state, x, y, obj)
struct render_state *state;
u_int x, y;
struct render_object *obj;
{
pid_t pid;
if (!(pid = fork())){
mngload(obj->data.anim.filename, x, y,
obj->data.anim.width, obj->data.anim.height);
while(1) sleep(1);
}
regchild(pid, obj->data.anim.key, -1, state->page);
}
static void
process_anim(state, cp)
struct render_state *state;
struct ctrl *cp;
{
int width, height;
width = 200;
height = 100;
/*
* we support only mng so far
*/
mngpreload(state, cp->ctc_value, &width, &height);
obj_new_anim(state, state->linewidth, - height,
width, height, cp->ctc_value, cp);
switch(valign){
case VL_TOP:
draw_line_itemsize(state, 0, height, 0);
break;
case VL_BOTTOM:
draw_line_itemsize(state, height, 0, 0);
break;
case VL_CENTER:
draw_line_itemsize(state, height /2 , height /2, 0);
break;
}
state->linewidth += width;
state->brankline = 0;
}
#endif
#ifdef USE_XFT2
void
set_xrender_color(value, opaque)
long value;
int opaque;
{
XColor xc;
XRenderColor render_color;
xft_forecolor.color.alpha = 65535 * opaque / 100;
if (value == xft_forecolor.pixel) return;
xc.flags = DoRed | DoGreen | DoBlue;
xc.pixel = value;
XQueryColor(display, colormap, &xc);
xft_forecolor.pixel = value;
xft_forecolor.color.red = xc.red;
xft_forecolor.color.green = xc.green;
xft_forecolor.color.blue = xc.blue;
}
static char *
xft_draw_fragment(state, p, len, registry, charset16)
struct render_state *state;
u_char *p;
u_int len;
char *registry;
int charset16; /*2-octet charset?*/
{
XGlyphInfo extents;
struct ctrl *cp;
char *fontname = NULL;
int i;
static char etab[3][20] = { "iso-2022-jp", "gb2312", "ksc5601"};
static char rtab[3][20] = { "jisx208", "gb2312", "ksc5601"};
static char prefix[3][20] = { "\033$B", "\033$A", "\033$(C"};
char buf16[1024], *p16;
char out16[1024], *o16;
int ileft, oleft;
#ifdef HAVE_ICONV
static iconv_t icv[3];
#endif
for (i = 0; i < len; i ++){
if (!isspace(*(p + i))) state->brankline = 0; /* This isn't brankline */
}
if (!registry) registry = "iso8859-1";
for (cp = state->xfont; cp; cp = cp->ct_next) {
if (!cp->ctc2_value2) continue;
if (strcmp(cp->ctc2_value2, registry) == 0) {
fontname = cp->ctc2_value1;
break;
}
}
if (!fontname) return NULL;
if (!(xft_font = xft_setfont(fontname, char_size[caching], registry))) return NULL;
if (charset16) {
#ifdef HAVE_ICONV
for (i = 0; i < 3; i ++) {
if (!strncmp(registry, rtab[i], 3)) break;
}
if (i == 3) return NULL; /* cannot find codeset */
sprintf(buf16, "%s%s\0", prefix[i], p);
if (icv[i] == (iconv_t)0) icv[i] = iconv_open("UTF-8", etab[i]);
if (icv[i] == (iconv_t)-1) {
fprintf(stderr, "your iconv doesn't support %s\n",
etab[i]);
return NULL;
}
p16 = buf16; o16 = out16;
ileft = len + strlen(prefix[i]); oleft = sizeof(out16);
if (iconv(icv[i], &p16, &ileft, &o16, &oleft) < 0) {
perror("iconv");
return NULL;
}
/* measure drawing are */
XftTextExtentsUtf8(display, xft_font, (XftChar8 *)out16,
sizeof(out16) - oleft, &extents);
/* line folding check */
if (state->width - state->leftfillpos / 2 - state->linewidth <
extents.xOff) {
draw_line_end(state);
draw_line_start(state);
state->linewidth = state->leftfillpos;
}
draw_line_itemsize(state, xft_font->ascent, xft_font->descent, 0);
if (obj_new_xftfont(state, state->linewidth, 0, out16,
sizeof(out16) - oleft, fontname, registry,
char_size[caching], charset16, xft_font)) {
state->linewidth += extents.xOff;
return p + len;
} else
#endif
return NULL;
}
XftTextExtents8(display, xft_font, (XftChar8 *)p, len, &extents);
/* line folding check */
if (state->width - state->leftfillpos / 2 - state->linewidth < extents.xOff) {
if (isspace(*(p + len -1))) {
XftTextExtents8(display, xft_font, (XftChar8 *)p, len -1, &extents);
if (state->width - state->leftfillpos / 2 - state->linewidth >= extents.xOff) goto nofolding;
draw_line_end(state);
draw_line_start(state);
state->linewidth = state->leftfillpos;
return p;
}
for (i = 2; i < len; i ++){
XftTextExtents8(display, xft_font, (XftChar8 *)p, len -i, &extents);
if (state->width - state->leftfillpos / 2 - state->linewidth >= extents.xOff){
len -= i;
break;
}
}
draw_line_itemsize(state, xft_font->ascent, xft_font->descent, 0);
if (obj_new_xftfont(state, state->linewidth, state->charoff, p, len, fontname,
registry, char_size[caching], charset16, xft_font)) {
draw_line_end(state);
draw_line_start(state);
state->linewidth = state->leftfillpos;
return p +len;
} else
return NULL;
}
nofolding:
#if 1
draw_line_itemsize(state, xft_font->ascent, xft_font->descent, 0);
#else
draw_line_itemsize(state, extents.y, extents.height - extents.y, 0);
#endif
if (obj_new_xftfont(state, state->linewidth, state->charoff, p, len, fontname,
registry, char_size[caching], charset16, xft_font)) {
state->linewidth += extents.xOff;
return p + len;
} else
return NULL;
}
static int
obj_new_xftfont(state, x, y, p, len, fontname, registry, size, charset16, xft_font)
struct render_state *state;
int x, y;
char *p;
int len;
char *fontname;
char *registry;
int size;
int charset16;
XftFont *xft_font;
{
struct render_object *obj;
char buf[65535], *p1;
p1 = buf;
bzero(buf, sizeof(buf));
if (sizeof(buf) > len)
memcpy(buf, p, len);
else
return 0;
obj = obj_alloc(state);
if (obj == NULL)
return 0;
obj->x = x;
obj->y = y;
obj->fore = fore_color[caching];
obj->type = O_XTFONT;
obj->data.xftfont.data = strdup(p1);
obj->data.xftfont.fontname = strdup(fontname);
obj->data.xftfont.registry = strdup(registry);
obj->data.xftfont.len = len;
obj->data.xftfont.size = size;
obj->data.xftfont.charset16 = charset16;
obj->ascent = xft_font->ascent;
obj->descent = xft_font->descent;
obj->vertloc = VL_BASE;
return 1;
}
static XftDraw *
xft_getdraw(Drawable drawable)
{
int i;
for (i = 0; i < 2; i ++) {
if (xft_xdraw[i] == drawable)
return xft_draw[i];
}
for (i = 0; i < 2; i ++) {
if (!xft_xdraw[i])
xft_draw[i] = XftDrawCreate(display, drawable, visual,
colormap);
return xft_draw[i];
}
return NULL; /* should not happen */
}
static
XftFont *
xft_setfont(xfontarg, csize, registry)
char *xfontarg;
int csize;
char *registry;
{
char *xfont;
static XftFont *last_xftfont;
static char lastfont[100];
static int lastsize = 0;
XftFont *xftfont;
char *p, *p2;
char style[100];
char font[100];
bzero(style, sizeof(style));
bzero(font, sizeof(font));
xfont = strdup(xfontarg);
if (!xfont)
return NULL;
if (!strcmp(xfont, lastfont) && lastsize == csize) {
free(xfont);
return last_xftfont;
}
if ((p = strchr(xfont, ':')) != NULL) {
/*
* if xfont contsins ":", we believe this is a Xft font name
* with the style expression.
*/
p2 = p + 1;
/* allow to use ":style=" syntax */
if ((strstr(p2, "style=") != NULL) || (strstr(p2, "STYLE=") != NULL))
p2 += 6;
*p = '\0';
strlcpy(font, xfont, sizeof(font));
strlcpy(style, p2, sizeof(style));
} else if ((p = strchr(xfont, '-')) != NULL) {
/*
* if xfont contains "-", we believe this is a conventional
* xfont name and try to convert it for xft
*/
*p++ = 0;
strlcpy(font, xfont, sizeof(font));
if (strncmp(p, "bold-i", 6) == 0)
strlcpy(style, "Bold Italic", sizeof(style));
else if (strncmp(p, "bold-", 5) == 0)
strlcpy(style, "Bold", sizeof(style));
else if ((p = strchr(p, '-')) != NULL && p[1] == 'i')
strlcpy(style, "Italic", sizeof(style));
} else
strlcpy(font, xfont, sizeof(font));
if (style[0]) {
xftfont = XftFontOpen(display, screen,
XFT_FAMILY, XftTypeString, font,
XFT_ENCODING, XftTypeString, registry,
XFT_STYLE, XftTypeString, style,
XFT_PIXEL_SIZE, XftTypeDouble, (float)csize, (char*)0);
} else {
xftfont = XftFontOpen(display, screen,
XFT_FAMILY, XftTypeString, font,
XFT_ENCODING, XftTypeString, registry,
XFT_PIXEL_SIZE, XftTypeDouble, (float)csize, (char*)0);
}
if (xftfont == 0) {
free(xfont);
return NULL;
}
if (style[0])
snprintf(lastfont, sizeof(lastfont), "%s:%s", font, style);
else
snprintf(lastfont, sizeof(lastfont), "%s", font);
if (verbose) {
fprintf(stderr, "using xftfont [%s] size: %d\n", lastfont,
csize);
}
lastsize = csize;
last_xftfont = xftfont;
free(xfont);
return last_xftfont;
}
#endif
#ifdef USE_M17N
obj_new_mtext(state, x, y, mt, from, to, drawframe, ascent, descent)
struct render_state *state;
int x, y;
MText *mt;
int from, to;
MFrame *drawframe;
int ascent, descent;
{
struct render_object *obj;
obj = obj_alloc(state);
if (obj == NULL) return 0;
obj->x = x;
obj->y = y;
obj->fore = fore_color[caching]; /* we don't need this */
obj->type = O_M17NTEXT;
obj->ascent = ascent;
obj->descent = descent;
obj->vertloc = VL_BASE;
obj->data.m17ntext.mt = mt;
m17n_object_ref (mt);
obj->data.m17ntext.drawframe = drawframe;
obj->data.m17ntext.from = from;
obj->data.m17ntext.to = to;
return 1;
}
#endif
#ifdef USE_IMLIB
void
regist_zimage_position(obj, x, y, width, height, page)
struct render_object *obj;
int x, y, width, height, page;
{
int i;
for (i = 0; i < ZIMAGENUM; i ++){
/* already registered */
if (zimage[i] == obj->data.image.imimage) return;
}
for (i = 0; i < ZIMAGENUM; i ++){
if (!zimage[i]) break;
}
if (i == ZIMAGENUM) {
fprintf(stderr, "Warning: too many images\n");
return;
}
zimage[i] = obj->data.image.imimage;
zonzoom[i] = obj->data.image.zoomonclk;
zx[i] = x;
zy[i] = y;
zwidth[i] = width;
zheight[i] = height;
zpage[i] = page;
}
static void
clear_zimage(page)
int page;
{
int i;
zoomin = 0;
manage_pixmap((Pixmap)NULL, 0, page);
for (i = 0; i < ZIMAGENUM; i ++){
if (zpage[i] == page) zimage[i] = 0;
}
}
int
search_zimage(x, y, page)
int x, y, page;
{
int i;
for (i = 0; i < ZIMAGENUM; i ++){
if (!zimage[i]) continue;
if (zx[i] <= x && zx[i] + zwidth[i] >= x &&
zy[i] <= y && zy[i] + zheight[i] >= y && zpage[i] == page) {
return i;
}
}
return -1;
}
void
zoomin_zimage(id)
int id;
{
Pixmap pixmap;
int i, w, h, x, y, xf, yf;
int ratio = 10;
float zstep = (window_width * zonzoom[id] / 100.0 - zwidth[id]) / (float)ratio;
float xstep;
float ystep;
float xyratio = (float)zheight[id] / zwidth[id];
xf = window_width * (100 - zonzoom[id]) / 200.0;
yf = (window_height - (window_width * zonzoom[id] / 100.0 * xyratio)) / 2;
xstep = (float)(xf - zx[id]) / ratio;
ystep = (float)(yf - zy[id]) / ratio;
for (i = 0; i <= ratio; i ++) {
w = zstep * i + zwidth[id];
h = w * xyratio+1;
x = zx[id] + xstep * i;
y = zy[id] + ystep * i;
pixmap = pixmap_fromimimage(zimage[id], w, h);
manage_pixmap(pixmap, 1, zpage[id]);
if (i > 0) clear_region(id, i-1, i, 0);
XCopyArea(display, pixmap, window, gcfore, 0,0, w, h, x, y);
XFlush(display);
if (i < ratio) usleep(10000);
}
}
void
zoomout_zimage(id)
int id;
{
Pixmap pixmap;
int i, w, h, x, y, xf, yf;
int ratio = 10;
float zstep = (window_width * zonzoom[id] / 100.0 - zwidth[id]) / (float)ratio;
float xstep;
float ystep;
float xyratio = (float)zheight[id] / zwidth[id];
xf = window_width * (100 - zonzoom[id]) / 200.0;
yf = (window_height - (window_width * zonzoom[id] / 100.0 * xyratio)) / 2;
xstep = (float)(xf - zx[id]) / ratio;
ystep = (float)(yf - zy[id]) / ratio;
for (i = ratio-1; i >= 0; i --) {
w = zstep * i + zwidth[id];
h = w * xyratio+1;
x = zx[id] + xstep * i;
y = zy[id] + ystep * i;
pixmap = pixmap_fromimimage(zimage[id], w, h);
manage_pixmap(pixmap, 1, zpage[id]);
if (i < ratio) clear_region(id, i+1, i, 0);
XCopyArea(display, pixmap, window, gcfore, 0, 0, w, h, x, y);
XFlush(display);
if (i > 0) usleep(10000);
}
clear_region(id, ratio, 1, 1);
}
void
clear_region(id, prev, cur, clear)
int id, prev, cur, clear;
{
int i, w, h, x, y, xf, yf;
int x1, x2, y1, y2, w1, w2, h1, h2;
int ratio = 10;
float zstep = (window_width * zonzoom[id] / 100.0 - zwidth[id]) / (float)ratio;
float xstep;
float ystep;
float xyratio = (float)zheight[id] / zwidth[id];
if (prev > ratio) return;
xf = window_width * (100 - zonzoom[id]) / 200.0;
yf = (window_height - (window_width * zonzoom[id] / 100.0 * xyratio)) / 2;
xstep = (float)(xf - zx[id]) / ratio;
ystep = (float)(yf - zy[id]) / ratio;
x1 = zx[id] + xstep * prev;
y1 = zy[id] + ystep * prev;
w1 = zstep * prev + zwidth[id];
h1 = w1 * xyratio+1;
x2 = zx[id] + xstep * cur;
y2 = zy[id] + ystep * cur;
w2 = zstep * cur + zwidth[id];
h2 = w2 * xyratio+1;
if (x2 > x1) XClearArea(display, window, x1-1, y1, x2 - x1, h1, clear);
if (y2 > y1) XClearArea(display, window, x1, y1, w1, y2 -y1, clear);
if (x2 + w2 < x1 + w1) XClearArea(display, window, x2 + w2, y1, x1 + w1 - x2 - w2, h1, clear);
if (y2 + h2 < y1 + h1) XClearArea(display, window, x1, y2 + h2, w1, y1 + h1 - y2 - h2, clear);
}
#endif
|