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
|
/* Copyright (C) 2001-2006 Artifex Software, Inc.
All Rights Reserved.
This software is provided AS-IS with no warranty, either express or
implied.
This software is distributed under license and may not be copied, modified
or distributed except as expressly authorized under the terms of that
license. Refer to licensing information at http://www.artifex.com/
or contact Artifex Software, Inc., 7 Mt. Lassen Drive - Suite A-134,
San Rafael, CA 94903, U.S.A., +1(415)492-9861, for further information.
*/
/* $Id: dscparse.c 9043 2008-08-28 22:48:19Z giles $ */
/*
* This is a DSC parser, based on the DSC 3.0 spec,
* with a few DSC 2.1 additions for page size.
*
* Current limitations:
* %%+ may be used after any comment in the comment or trailer,
* but is currently only supported by
* %%DocumentMedia
*
* DSC 2.1 additions (discontinued in DSC 3.0):
* %%DocumentPaperColors:
* %%DocumentPaperForms:
* %%DocumentPaperSizes:
* %%DocumentPaperWeights:
* %%PaperColor: (ignored)
* %%PaperForm: (ignored)
* %%PaperSize:
* %%PaperWeight: (ignored)
*
* Other additions for defaults or page section
% %%ViewingOrientation: xx xy yx yy
*/
#include <stdio.h> /* for sprintf(), not file I/O */
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXSTR 256
#include "dscparse.h"
/* Macros for comparing string literals
* For maximum speed, the length of the second macro argument is
* computed at compile time.
* THE SECOND MACRO ARGUMENT MUST BE A STRING LITERAL.
*/
#define COMPARE(p,str) (strncmp((const char *)(p), (str), sizeof(str)-1)==0)
#define IS_DSC(line, str) (COMPARE((line), (str)))
/* Macros for comparing the first one or two characters */
#define IS_WHITE(ch) (((ch)==' ') || ((ch)=='\t'))
#define IS_EOL(ch) (((ch)=='\r') || ((ch)=='\n'))
#define IS_WHITE_OR_EOL(ch) (IS_WHITE(ch) || IS_EOL(ch))
#define IS_BLANK(str) (IS_EOL(str[0]))
#define NOT_DSC_LINE(str) (((str)[0]!='%') || ((str)[1]!='%'))
/* Macros for document offset to start and end of line */
#define DSC_START(dsc) ((dsc)->data_offset + (dsc)->data_index - (dsc)->line_length)
#define DSC_END(dsc) ((dsc)->data_offset + (dsc)->data_index)
/* dsc_scan_SECTION() functions return one of
* CDSC_ERROR, CDSC_OK, CDSC_NOTDSC
* or one of the following
*/
/* The line should be passed on to the next section parser. */
#define CDSC_PROPAGATE 10
/* If document is DOS EPS and we haven't read 30 bytes, ask for more. */
#define CDSC_NEEDMORE 11
/* local prototypes */
static void * dsc_memalloc(CDSC *dsc, size_t size);
static void dsc_memfree(CDSC*dsc, void *ptr);
static CDSC * dsc_init2(CDSC *dsc);
static void dsc_reset(CDSC *dsc);
static void dsc_section_join(DSC_OFFSET begin, DSC_OFFSET *pend, DSC_OFFSET **pplast);
static int dsc_read_line(CDSC *dsc);
static int dsc_read_doseps(CDSC *dsc);
static int dsc_read_macbin(CDSC *dsc);
static int dsc_read_applesingle(CDSC *dsc);
static char * dsc_alloc_string(CDSC *dsc, const char *str, int len);
static char * dsc_add_line(CDSC *dsc, const char *line, unsigned int len);
static char * dsc_copy_string(char *str, unsigned int slen,
char *line, unsigned int len, unsigned int *offset);
static GSDWORD dsc_get_dword(const unsigned char *buf);
static GSWORD dsc_get_word(const unsigned char *buf);
static GSDWORD dsc_get_bigendian_dword(const unsigned char *buf);
static GSWORD dsc_get_bigendian_word(const unsigned char *buf);
static int dsc_get_int(const char *line, unsigned int len, unsigned int *offset);
static float dsc_get_real(const char *line, unsigned int len,
unsigned int *offset);
static void dsc_unknown(CDSC *dsc);
static GSBOOL dsc_is_section(char *line);
static int dsc_parse_pages(CDSC *dsc);
static int dsc_parse_bounding_box(CDSC *dsc, CDSCBBOX** pbbox, int offset);
static int dsc_parse_float_bounding_box(CDSC *dsc, CDSCFBBOX** pfbbox, int offset);
static int dsc_parse_orientation(CDSC *dsc, unsigned int *porientation,
int offset);
static int dsc_parse_order(CDSC *dsc);
static int dsc_parse_media(CDSC *dsc, const CDSCMEDIA **page_media);
static int dsc_parse_document_media(CDSC *dsc);
static int dsc_parse_viewing_orientation(CDSC *dsc, CDSCCTM **pctm);
static int dsc_parse_page(CDSC *dsc);
static void dsc_save_line(CDSC *dsc);
static int dsc_scan_type(CDSC *dsc);
static int dsc_scan_comments(CDSC *dsc);
static int dsc_scan_preview(CDSC *dsc);
static int dsc_scan_defaults(CDSC *dsc);
static int dsc_scan_prolog(CDSC *dsc);
static int dsc_scan_setup(CDSC *dsc);
static int dsc_scan_page(CDSC *dsc);
static int dsc_scan_trailer(CDSC *dsc);
static int dsc_error(CDSC *dsc, unsigned int explanation,
char *line, unsigned int line_len);
static int dsc_dcs2_fixup(CDSC *dsc);
static int dsc_parse_platefile(CDSC *dsc);
static int dsc_parse_dcs1plate(CDSC *dsc);
static CDSCCOLOUR * dsc_find_colour(CDSC *dsc, const char *colourname);
static int dsc_parse_process_colours(CDSC *dsc);
static int dsc_parse_custom_colours(CDSC *dsc);
static int dsc_parse_cmyk_custom_colour(CDSC *dsc);
static int dsc_parse_rgb_custom_colour(CDSC *dsc);
/* DSC error reporting */
static const int dsc_severity[] = {
CDSC_ERROR_WARN, /* CDSC_MESSAGE_BBOX */
CDSC_ERROR_WARN, /* CDSC_MESSAGE_EARLY_TRAILER */
CDSC_ERROR_WARN, /* CDSC_MESSAGE_EARLY_EOF */
CDSC_ERROR_ERROR, /* CDSC_MESSAGE_PAGE_IN_TRAILER */
CDSC_ERROR_ERROR, /* CDSC_MESSAGE_PAGE_ORDINAL */
CDSC_ERROR_ERROR, /* CDSC_MESSAGE_PAGES_WRONG */
CDSC_ERROR_ERROR, /* CDSC_MESSAGE_EPS_NO_BBOX */
CDSC_ERROR_ERROR, /* CDSC_MESSAGE_EPS_PAGES */
CDSC_ERROR_WARN, /* CDSC_MESSAGE_NO_MEDIA */
CDSC_ERROR_WARN, /* CDSC_MESSAGE_ATEND */
CDSC_ERROR_INFORM, /* CDSC_MESSAGE_DUP_COMMENT */
CDSC_ERROR_INFORM, /* CDSC_MESSAGE_DUP_TRAILER */
CDSC_ERROR_WARN, /* CDSC_MESSAGE_BEGIN_END */
CDSC_ERROR_INFORM, /* CDSC_MESSAGE_BAD_SECTION */
CDSC_ERROR_INFORM, /* CDSC_MESSAGE_LONG_LINE */
CDSC_ERROR_WARN, /* CDSC_MESSAGE_INCORRECT_USAGE */
0
};
#define DSC_MAX_ERROR ((sizeof(dsc_severity) / sizeof(int))-2)
const CDSCMEDIA dsc_known_media[CDSC_KNOWN_MEDIA] = {
/* These sizes taken from Ghostscript gs_statd.ps */
{"11x17", 792, 1224, 0, NULL, NULL},
{"A3", 842, 1190, 0, NULL, NULL},
{"A4", 595, 842, 0, NULL, NULL},
{"A5", 421, 595, 0, NULL, NULL},
{"B4", 709, 1002, 0, NULL, NULL}, /* ISO, but not Adobe standard */
{"B5", 501, 709, 0, NULL, NULL}, /* ISO, but not Adobe standard */
{"Ledger", 1224, 792, 0, NULL, NULL},
{"Legal", 612, 1008, 0, NULL, NULL},
{"Letter", 612, 792, 0, NULL, NULL},
{"Note", 612, 792, 0, NULL, NULL},
{NULL, 0, 0, 0, NULL, NULL}
};
/* parser state */
enum CDSC_SCAN_SECTION {
scan_none = 0,
scan_comments = 1,
scan_pre_preview = 2,
scan_preview = 3,
scan_pre_defaults = 4,
scan_defaults = 5,
scan_pre_prolog = 6,
scan_prolog = 7,
scan_pre_setup = 8,
scan_setup = 9,
scan_pre_pages = 10,
scan_pages = 11,
scan_pre_trailer = 12,
scan_trailer = 13,
scan_eof = 14
};
static const char * const dsc_scan_section_name[15] = {
"Type", "Comments",
"pre-Preview", "Preview",
"pre-Defaults", "Defaults",
"pre-Prolog", "Prolog",
"pre-Setup", "Setup",
"pre-Page", "Page",
"pre-Trailer", "Trailer",
"EOF"
};
/******************************************************************/
/* Public functions */
/******************************************************************/
/* constructor */
CDSC *
dsc_init(void *caller_data)
{
CDSC *dsc = (CDSC *)malloc(sizeof(CDSC));
if (dsc == NULL)
return NULL;
memset(dsc, 0, sizeof(CDSC));
dsc->caller_data = caller_data;
dsc->ref_count = 0;
dsc_ref(dsc);
return dsc_init2(dsc);
}
/* constructor, with caller supplied memalloc */
CDSC *
dsc_init_with_alloc(
void *caller_data,
void *(*memalloc)(size_t size, void *closure_data),
void (*memfree)(void *ptr, void *closure_data),
void *closure_data)
{
CDSC *dsc = (CDSC *)memalloc(sizeof(CDSC), closure_data);
if (dsc == NULL)
return NULL;
memset(dsc, 0, sizeof(CDSC));
dsc->caller_data = caller_data;
dsc->memalloc = memalloc;
dsc->memfree = memfree;
dsc->mem_closure_data = closure_data;
dsc->ref_count = 0;
dsc_ref(dsc);
return dsc_init2(dsc);
}
/* destructor */
void
dsc_free(CDSC *dsc)
{
if (dsc == NULL)
return;
dsc_reset(dsc);
dsc_memfree(dsc, dsc);
}
CDSC *
dsc_new(void *caller_data)
{
return dsc_init(caller_data);
}
int
dsc_ref(CDSC *dsc)
{
return ++(dsc->ref_count);
}
int
dsc_unref(CDSC *dsc)
{
if (dsc->ref_count <= 0)
return -1;
dsc->ref_count--;
if (dsc->ref_count == 0) {
dsc_free(dsc);
return 0;
}
return dsc->ref_count;
}
/* Tell DSC parser how long document will be, to allow ignoring
* of early %%Trailer and %%EOF. This is optional.
*/
void
dsc_set_length(CDSC *dsc, DSC_OFFSET len)
{
dsc->file_length = len;
}
/* Process a buffer containing DSC comments and PostScript */
/* Return value is < 0 for error, >=0 for OK.
* CDSC_ERROR
* CDSC_OK
* CDSC_NOTDSC (DSC will be ignored)
* other values indicate the last DSC comment read
*/
int
dsc_scan_data(CDSC *dsc, const char *data, int length)
{
int bytes_read;
int code = 0;
if (dsc == NULL)
return CDSC_ERROR;
if (dsc->id == CDSC_NOTDSC)
return CDSC_NOTDSC;
dsc->id = CDSC_OK;
if (dsc->eof)
return CDSC_OK; /* ignore */
if (length == 0) {
/* EOF, so process what remains */
dsc->eof = TRUE;
}
do {
if (dsc->id == CDSC_NOTDSC)
break;
if (length != 0) {
/* move existing data if needed */
if (dsc->data_length > CDSC_DATA_LENGTH/2) {
memmove(dsc->data, dsc->data + dsc->data_index,
dsc->data_length - dsc->data_index);
dsc->data_offset += dsc->data_index;
dsc->data_length -= dsc->data_index;
dsc->data_index = 0;
}
/* append to buffer */
bytes_read = min(length, (int)(CDSC_DATA_LENGTH - dsc->data_length));
memcpy(dsc->data + dsc->data_length, data, bytes_read);
dsc->data_length += bytes_read;
data += bytes_read;
length -= bytes_read;
}
if (dsc->scan_section == scan_none) {
code = dsc_scan_type(dsc);
if (code == CDSC_NEEDMORE) {
/* need more characters before we can identify type */
code = CDSC_OK;
break;
}
dsc->id = code;
}
if (code == CDSC_NOTDSC) {
dsc->id = CDSC_NOTDSC;
break;
}
while ((code = dsc_read_line(dsc)) > 0) {
if (dsc->id == CDSC_NOTDSC)
break;
if (dsc->file_length &&
(dsc->data_offset + dsc->data_index > dsc->file_length)) {
/* have read past end of where we need to parse. */
return CDSC_OK; /* ignore */
}
if (dsc->doseps_end &&
(dsc->data_offset + dsc->data_index > dsc->doseps_end)) {
/* have read past end of DOS EPS or Mac Binary
* PostScript section
*/
return CDSC_OK; /* ignore */
}
if (dsc->eof)
return CDSC_OK;
if (dsc->skip_document)
continue; /* embedded document */
if (dsc->skip_lines)
continue; /* embedded lines */
if (IS_DSC(dsc->line, "%%BeginData:"))
continue;
if (IS_DSC(dsc->line, "%%BeginBinary:"))
continue;
if (IS_DSC(dsc->line, "%%EndDocument"))
continue;
if (IS_DSC(dsc->line, "%%EndData"))
continue;
if (IS_DSC(dsc->line, "%%EndBinary"))
continue;
do {
switch (dsc->scan_section) {
case scan_comments:
code = dsc_scan_comments(dsc);
break;
case scan_pre_preview:
case scan_preview:
code = dsc_scan_preview(dsc);
break;
case scan_pre_defaults:
case scan_defaults:
code = dsc_scan_defaults(dsc);
break;
case scan_pre_prolog:
case scan_prolog:
code = dsc_scan_prolog(dsc);
break;
case scan_pre_setup:
case scan_setup:
code = dsc_scan_setup(dsc);
break;
case scan_pre_pages:
case scan_pages:
code = dsc_scan_page(dsc);
break;
case scan_pre_trailer:
case scan_trailer:
code = dsc_scan_trailer(dsc);
break;
case scan_eof:
code = CDSC_OK;
break;
default:
/* invalid state */
code = CDSC_ERROR;
}
/* repeat if line is start of next section */
} while (code == CDSC_PROPAGATE);
/* if DOS EPS header not complete, ask for more */
if (code == CDSC_NEEDMORE) {
code = CDSC_OK;
break;
}
if (code == CDSC_NOTDSC) {
dsc->id = CDSC_NOTDSC;
break;
}
}
} while (length != 0);
return (code < 0) ? code : dsc->id;
}
/* Tidy up from incorrect DSC comments */
int
dsc_fixup(CDSC *dsc)
{
unsigned int i;
char buf[32];
DSC_OFFSET *last;
if (dsc->id == CDSC_NOTDSC)
return 0;
/* flush last partial line */
dsc_scan_data(dsc, NULL, 0);
/* Fix DSC error: EOF before end of %%BeginData */
if (dsc->eof &&
(dsc->skip_lines || dsc->skip_bytes || dsc->skip_document)) {
switch (dsc->scan_section) {
case scan_comments:
dsc->endcomments = DSC_END(dsc);
break;
case scan_preview:
dsc->endpreview = DSC_END(dsc);
break;
case scan_defaults:
dsc->enddefaults = DSC_END(dsc);
break;
case scan_prolog:
dsc->endprolog = DSC_END(dsc);
break;
case scan_setup:
dsc->endsetup = DSC_END(dsc);
break;
case scan_pages:
if (dsc->page_count)
dsc->page[dsc->page_count-1].end = DSC_END(dsc);
break;
case scan_trailer:
case scan_eof:
dsc->endtrailer = DSC_END(dsc);
break;
}
}
/* Fix DSC error: code between %%EndSetup and %%Page */
if (dsc->page_count && (dsc->page[0].begin != dsc->endsetup)
&& (dsc->endsetup != dsc->beginsetup)) {
dsc->endsetup = dsc->page[0].begin;
dsc_debug_print(dsc, "Warning: code included between setup and first page\n");
}
/* Last page contained a false trailer, */
/* so extend last page to start of trailer */
if (dsc->page_count && (dsc->begintrailer != 0) &&
(dsc->page[dsc->page_count-1].end != dsc->begintrailer)) {
dsc_debug_print(dsc, "Ignoring earlier misplaced trailer\n");
dsc_debug_print(dsc, "and extending last page to start of trailer\n");
dsc->page[dsc->page_count-1].end = dsc->begintrailer;
}
/*
* Join up all sections.
* There might be extra code between them, or we might have
* missed including the \n which followed \r.
*/
last = &dsc->endcomments;
dsc_section_join(dsc->beginpreview, &dsc->endpreview, &last);
dsc_section_join(dsc->begindefaults, &dsc->enddefaults, &last);
dsc_section_join(dsc->beginprolog, &dsc->endprolog, &last);
dsc_section_join(dsc->beginsetup, &dsc->endsetup, &last);
for (i=0; i<dsc->page_count; i++)
dsc_section_join(dsc->page[i].begin, &dsc->page[i].end, &last);
if (dsc->begintrailer)
*last = dsc->begintrailer;
if ((dsc->page_pages == 0) && (dsc->page_count == 1)) {
/* don't flag an error if %%Pages absent but one %%Page found */
/* adjust incorrect page count */
dsc->page_pages = dsc->page_count;
}
/* Warnings and Errors that we can now identify */
if ((dsc->page_count != dsc->page_pages)) {
int rc = dsc_error(dsc, CDSC_MESSAGE_PAGES_WRONG, NULL, 0);
switch (rc) {
case CDSC_RESPONSE_OK:
/* adjust incorrect page count */
dsc->page_pages = dsc->page_count;
break;
case CDSC_RESPONSE_CANCEL:
break;;
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
if (dsc->epsf && (dsc->bbox == (CDSCBBOX *)NULL)) {
/* EPS files MUST include a BoundingBox */
int rc = dsc_error(dsc, CDSC_MESSAGE_EPS_NO_BBOX, NULL, 0);
switch (rc) {
case CDSC_RESPONSE_OK:
/* Assume that it is EPS */
break;
case CDSC_RESPONSE_CANCEL:
/* Is NOT an EPS file */
dsc->epsf = FALSE;
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
if (dsc->epsf && ((dsc->page_count > 1) || (dsc->page_pages > 1))) {
int rc = dsc_error(dsc, CDSC_MESSAGE_EPS_PAGES, NULL, 0);
switch (rc) {
case CDSC_RESPONSE_OK:
/* Is an EPS file */
break;
case CDSC_RESPONSE_CANCEL:
/* Is NOT an EPS file */
dsc->epsf = FALSE;
break;
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
/* convert single file DSC 2.0 into multiple pages */
dsc_dcs2_fixup(dsc);
if ((dsc->media_count == 1) && (dsc->page_media == NULL)) {
/* if one only media was specified, and default page media */
/* was not specified, assume that default is the only media. */
dsc->page_media = dsc->media[0];
}
if ((dsc->media_count != 0) && (dsc->page_media == NULL)) {
int rc = dsc_error(dsc, CDSC_MESSAGE_NO_MEDIA, NULL, 0);
switch (rc) {
case CDSC_RESPONSE_OK:
/* default media is first listed */
dsc->page_media = dsc->media[0];
break;
case CDSC_RESPONSE_CANCEL:
/* No default media */
break;
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
/* make sure all pages have a label */
for (i=0; i<dsc->page_count; i++) {
if (strlen(dsc->page[i].label) == 0) {
sprintf(buf, "%d", i+1);
if ((dsc->page[i].label = dsc_alloc_string(dsc, buf, (int)strlen(buf)))
== (char *)NULL)
return CDSC_ERROR; /* no memory */
}
}
return CDSC_OK;
}
/* Install a function to be used for displaying messages about
* DSC errors and warnings, and to request advice from user.
* Installing an error function is optional.
*/
void
dsc_set_error_function(CDSC *dsc,
int (*fn)(void *caller_data, CDSC *dsc,
unsigned int explanation, const char *line, unsigned int line_len))
{
dsc->dsc_error_fn = fn;
}
/* Install a function for printing debug messages */
/* This is optional */
void
dsc_set_debug_function(CDSC *dsc,
void (*debug_fn)(void *caller_data, const char *str))
{
dsc->debug_print_fn = debug_fn;
}
/* Doesn't need to be public for PostScript documents */
/* Made public so GSview can add pages when processing PDF files */
int
dsc_add_page(CDSC *dsc, int ordinal, char *label)
{
dsc->page[dsc->page_count].ordinal = ordinal;
dsc->page[dsc->page_count].label =
dsc_alloc_string(dsc, label, (int)strlen(label)+1);
dsc->page[dsc->page_count].begin = 0;
dsc->page[dsc->page_count].end = 0;
dsc->page[dsc->page_count].orientation = CDSC_ORIENT_UNKNOWN;
dsc->page[dsc->page_count].media = NULL;
dsc->page[dsc->page_count].bbox = NULL;
dsc->page[dsc->page_count].viewing_orientation = NULL;
dsc->page[dsc->page_count].crop_box = NULL;
dsc->page_count++;
if (dsc->page_count >= dsc->page_chunk_length) {
CDSCPAGE *new_page = (CDSCPAGE *)dsc_memalloc(dsc,
(CDSC_PAGE_CHUNK+dsc->page_count) * sizeof(CDSCPAGE));
if (new_page == NULL)
return CDSC_ERROR; /* out of memory */
memcpy(new_page, dsc->page,
dsc->page_count * sizeof(CDSCPAGE));
dsc_memfree(dsc, dsc->page);
dsc->page= new_page;
dsc->page_chunk_length = CDSC_PAGE_CHUNK+dsc->page_count;
}
return CDSC_OK;
}
/* Doesn't need to be public for PostScript documents */
/* Made public so GSview can store PDF MediaBox */
int
dsc_add_media(CDSC *dsc, CDSCMEDIA *media)
{
CDSCMEDIA **newmedia_array;
CDSCMEDIA *newmedia;
/* extend media array */
newmedia_array = (CDSCMEDIA **)dsc_memalloc(dsc,
(dsc->media_count + 1) * sizeof(CDSCMEDIA *));
if (newmedia_array == NULL)
return CDSC_ERROR; /* out of memory */
if (dsc->media != NULL) {
memcpy(newmedia_array, dsc->media,
dsc->media_count * sizeof(CDSCMEDIA *));
dsc_memfree(dsc, dsc->media);
}
dsc->media = newmedia_array;
/* allocate new media */
newmedia = dsc->media[dsc->media_count] =
(CDSCMEDIA *)dsc_memalloc(dsc, sizeof(CDSCMEDIA));
if (newmedia == NULL)
return CDSC_ERROR; /* out of memory */
newmedia->name = NULL;
newmedia->width = 595.0;
newmedia->height = 842.0;
newmedia->weight = 80.0;
newmedia->colour = NULL;
newmedia->type = NULL;
newmedia->mediabox = NULL;
dsc->media_count++;
if (media->name) {
newmedia->name = dsc_alloc_string(dsc, media->name,
(int)strlen(media->name));
if (newmedia->name == NULL)
return CDSC_ERROR; /* no memory */
}
newmedia->width = media->width;
newmedia->height = media->height;
newmedia->weight = media->weight;
if (media->colour) {
newmedia->colour = dsc_alloc_string(dsc, media->colour,
(int)strlen(media->colour));
if (newmedia->colour == NULL)
return CDSC_ERROR; /* no memory */
}
if (media->type) {
newmedia->type = dsc_alloc_string(dsc, media->type,
(int)strlen(media->type));
if (newmedia->type == NULL)
return CDSC_ERROR; /* no memory */
}
newmedia->mediabox = NULL;
if (media->mediabox) {
newmedia->mediabox = (CDSCBBOX *)dsc_memalloc(dsc, sizeof(CDSCBBOX));
if (newmedia->mediabox == NULL)
return CDSC_ERROR; /* no memory */
*newmedia->mediabox = *media->mediabox;
}
return CDSC_OK;
}
/* Doesn't need to be public for PostScript documents */
/* Made public so GSview can store PDF CropBox */
int
dsc_set_page_bbox(CDSC *dsc, unsigned int page_number,
int llx, int lly, int urx, int ury)
{
CDSCBBOX *bbox;
if (page_number >= dsc->page_count)
return CDSC_ERROR;
bbox = dsc->page[page_number].bbox;
if (bbox == NULL)
dsc->page[page_number].bbox = bbox =
(CDSCBBOX *)dsc_memalloc(dsc, sizeof(CDSCBBOX));
if (bbox == NULL)
return CDSC_ERROR;
bbox->llx = llx;
bbox->lly = lly;
bbox->urx = urx;
bbox->ury = ury;
return CDSC_OK;
}
/******************************************************************/
/* Private functions below here. */
/******************************************************************/
static void *
dsc_memalloc(CDSC *dsc, size_t size)
{
if (dsc->memalloc)
return dsc->memalloc(size, dsc->mem_closure_data);
return malloc(size);
}
static void
dsc_memfree(CDSC*dsc, void *ptr)
{
if (dsc->memfree)
dsc->memfree(ptr, dsc->mem_closure_data);
else
free(ptr);
}
/* private constructor */
static CDSC *
dsc_init2(CDSC *dsc)
{
dsc_reset(dsc);
dsc->string_head = (CDSCSTRING *)dsc_memalloc(dsc, sizeof(CDSCSTRING));
if (dsc->string_head == NULL) {
dsc_free(dsc);
return NULL; /* no memory */
}
dsc->string = dsc->string_head;
dsc->string->next = NULL;
dsc->string->data = (char *)dsc_memalloc(dsc, CDSC_STRING_CHUNK);
if (dsc->string->data == NULL) {
dsc_free(dsc);
return NULL; /* no memory */
}
dsc->string->index = 0;
dsc->string->length = CDSC_STRING_CHUNK;
dsc->page = (CDSCPAGE *)dsc_memalloc(dsc, CDSC_PAGE_CHUNK * sizeof(CDSCPAGE));
if (dsc->page == NULL) {
dsc_free(dsc);
return NULL; /* no memory */
}
dsc->page_chunk_length = CDSC_PAGE_CHUNK;
dsc->page_count = 0;
dsc->line = NULL;
dsc->data_length = 0;
dsc->data_index = dsc->data_length;
return dsc;
}
static void
dsc_reset(CDSC *dsc)
{
unsigned int i;
/* Clear public members */
dsc->dsc = FALSE;
dsc->ctrld = FALSE;
dsc->pjl = FALSE;
dsc->epsf = FALSE;
dsc->pdf = FALSE;
dsc->epsf = FALSE;
dsc->preview = CDSC_NOPREVIEW;
dsc->dsc_version = NULL; /* stored in dsc->string */
dsc->language_level = 0;
dsc->document_data = CDSC_DATA_UNKNOWN;
dsc->begincomments = 0;
dsc->endcomments = 0;
dsc->beginpreview = 0;
dsc->endpreview = 0;
dsc->begindefaults = 0;
dsc->enddefaults = 0;
dsc->beginprolog = 0;
dsc->endprolog = 0;
dsc->beginsetup = 0;
dsc->endsetup = 0;
dsc->begintrailer = 0;
dsc->endtrailer = 0;
for (i=0; i<dsc->page_count; i++) {
/* page media is pointer to an element of media or dsc_known_media */
/* do not free it. */
if (dsc->page[i].bbox)
dsc_memfree(dsc, dsc->page[i].bbox);
if (dsc->page[i].viewing_orientation)
dsc_memfree(dsc, dsc->page[i].viewing_orientation);
if (dsc->page[i].crop_box)
dsc_memfree(dsc, dsc->page[i].crop_box);
}
if (dsc->page)
dsc_memfree(dsc, dsc->page);
dsc->page = NULL;
dsc->page_count = 0;
dsc->page_pages = 0;
dsc->page_order = CDSC_ORDER_UNKNOWN;
dsc->page_orientation = CDSC_ORIENT_UNKNOWN;
if (dsc->viewing_orientation)
dsc_memfree(dsc, dsc->viewing_orientation);
dsc->viewing_orientation = NULL;
if (dsc->media) {
for (i=0; i<dsc->media_count; i++) {
if (dsc->media[i]) {
if (dsc->media[i]->mediabox)
dsc_memfree(dsc, dsc->media[i]->mediabox);
dsc_memfree(dsc, dsc->media[i]);
}
}
dsc_memfree(dsc, dsc->media);
}
dsc->media_count = 0;
dsc->media = NULL;
/* page_media is pointer to an element of media or dsc_known_media */
/* do not free it. */
dsc->page_media = NULL;
if (dsc->bbox)
dsc_memfree(dsc, dsc->bbox);
dsc->bbox = NULL;
if (dsc->page_bbox)
dsc_memfree(dsc, dsc->page_bbox);
dsc->page_bbox = NULL;
if (dsc->doseps)
dsc_memfree(dsc, dsc->doseps);
dsc->doseps = NULL;
dsc->dsc_title = NULL;
dsc->dsc_creator = NULL;
dsc->dsc_date = NULL;
dsc->dsc_for = NULL;
dsc->max_error = DSC_MAX_ERROR;
dsc->severity = dsc_severity;
/* Clear private members */
/* Don't touch dsc->caller_data */
dsc->id = CDSC_OK;
dsc->scan_section = scan_none;
dsc->doseps_end = 0;
dsc->page_chunk_length = 0;
dsc->file_length = 0;
dsc->skip_document = 0;
dsc->skip_bytes = 0;
dsc->skip_lines = 0;
dsc->skip_pjl = 0;
dsc->begin_font_count = 0;
dsc->begin_feature_count = 0;
dsc->begin_resource_count = 0;
dsc->begin_procset_count = 0;
dsc->data_length = 0;
dsc->data_index = 0;
dsc->data_offset = 0;
dsc->eof = 0;
dsc->line = 0;
dsc->line_length = 0;
dsc->eol = 0;
dsc->last_cr = FALSE;
dsc->line_count = 1;
dsc->long_line = FALSE;
memset(dsc->last_line, 0, sizeof(dsc->last_line));
dsc->string = dsc->string_head;
while (dsc->string != (CDSCSTRING *)NULL) {
if (dsc->string->data)
dsc_memfree(dsc, dsc->string->data);
dsc->string_head = dsc->string;
dsc->string = dsc->string->next;
dsc_memfree(dsc, dsc->string_head);
}
dsc->string_head = NULL;
dsc->string = NULL;
/* don't touch caller functions */
/* public data */
if (dsc->hires_bbox)
dsc_memfree(dsc, dsc->hires_bbox);
dsc->hires_bbox = NULL;
if (dsc->crop_box)
dsc_memfree(dsc, dsc->crop_box);
dsc->crop_box = NULL;
if (dsc->dcs2) {
CDCS2 *this_dcs, *next_dcs;
this_dcs = dsc->dcs2;
while (this_dcs) {
next_dcs = this_dcs->next;
/* strings have already been freed */
dsc_memfree(dsc, this_dcs);
this_dcs = next_dcs;
}
dsc->dcs2 = NULL;
}
if (dsc->colours) {
CDSCCOLOUR *this_colour, *next_colour;
this_colour = dsc->colours;
while (this_colour) {
next_colour = this_colour->next;
/* strings have already been freed */
dsc_memfree(dsc, this_colour);
this_colour = next_colour;
}
dsc->colours = NULL;
}
if (dsc->macbin)
dsc_memfree(dsc, dsc->macbin);
dsc->macbin = NULL;
}
/*
* Join up all sections.
* There might be extra code between them, or we might have
* missed including the \n which followed \r.
* begin is the start of this section
* pend is a pointer to the end of this section
* pplast is a pointer to a pointer of the end of the previous section
*/
static void
dsc_section_join(DSC_OFFSET begin, DSC_OFFSET *pend, DSC_OFFSET **pplast)
{
if (begin)
**pplast = begin;
if (*pend > begin)
*pplast = pend;
}
/* return value is 0 if no line available, or length of line */
static int
dsc_read_line(CDSC *dsc)
{
char *p, *last;
dsc->line = NULL;
if (dsc->eof) {
/* return all that remains, even if line incomplete */
dsc->line = dsc->data + dsc->data_index;
dsc->line_length = dsc->data_length - dsc->data_index;
dsc->data_index = dsc->data_length;
return dsc->line_length;
}
if (dsc->file_length &&
(dsc->data_offset + dsc->data_index >= dsc->file_length)) {
/* Have read past where we need to parse. */
/* Ignore all that remains. */
dsc->line = dsc->data + dsc->data_index;
dsc->line_length = dsc->data_length - dsc->data_index;
dsc->data_index = dsc->data_length;
return dsc->line_length;
}
if (dsc->doseps_end &&
(dsc->data_offset + dsc->data_index >= dsc->doseps_end)) {
/* Have read past end of DOS EPS PostScript section. */
/* Ignore all that remains. */
dsc->line = dsc->data + dsc->data_index;
dsc->line_length = dsc->data_length - dsc->data_index;
dsc->data_index = dsc->data_length;
return dsc->line_length;
}
/* ignore embedded bytes */
if (dsc->skip_bytes) {
int cnt = min(dsc->skip_bytes,
(int)(dsc->data_length - dsc->data_index));
dsc->skip_bytes -= cnt;
dsc->data_index += cnt;
if (dsc->skip_bytes != 0)
return 0;
}
do {
dsc->line = dsc->data + dsc->data_index;
last = dsc->data + dsc->data_length;
if (dsc->data_index == dsc->data_length) {
dsc->line_length = 0;
return 0;
}
if (dsc->eol) {
/* if previous line was complete, increment line count */
dsc->line_count++;
if (dsc->skip_lines)
dsc->skip_lines--;
}
/* skip over \n which followed \r */
if (dsc->last_cr && dsc->line[0] == '\n') {
dsc->data_index++;
dsc->line++;
}
dsc->last_cr = FALSE;
/* look for EOL */
dsc->eol = FALSE;
for (p = dsc->line; p < last; p++) {
if (*p == '\r') {
p++;
if ((p<last) && (*p == '\n'))
p++; /* include line feed also */
else
dsc->last_cr = TRUE; /* we might need to skip \n */
dsc->eol = TRUE; /* dsc->line is a complete line */
break;
}
if (*p == '\n') {
p++;
dsc->eol = TRUE; /* dsc->line is a complete line */
break;
}
if (*p == '\032') { /* MS-DOS Ctrl+Z */
dsc->eol = TRUE;
}
}
if (dsc->eol == FALSE) {
/* we haven't got a complete line yet */
if (dsc->data_length - dsc->data_index < sizeof(dsc->data)/2) {
/* buffer is less than half full, ask for some more */
dsc->line_length = 0;
return 0;
}
}
dsc->data_index += dsc->line_length = (int)(p - dsc->line);
} while (dsc->skip_lines && dsc->line_length);
if (dsc->line_length == 0)
return 0;
if ((dsc->line[0]=='%') && (dsc->line[1]=='%')) {
/* handle recursive %%BeginDocument */
if ((dsc->skip_document) && dsc->line_length &&
COMPARE(dsc->line, "%%EndDocument")) {
dsc->skip_document--;
}
/* handle embedded lines or binary data */
if (COMPARE(dsc->line, "%%BeginData:")) {
/* %%BeginData: <numberof>[ <type> [ <bytesorlines> ] ]
* <numberof> ::= <uint> (Lines or physical bytes)
* <type> ::= Hex | Binary | ASCII (Type of data)
* <bytesorlines> ::= Bytes | Lines (Read in bytes or lines)
*/
char begindata[MAXSTR+1];
int cnt;
const char *numberof, *bytesorlines;
cnt = dsc->line_length;
if (dsc->line_length > sizeof(begindata)-1)
cnt = sizeof(begindata)-1;
memcpy(begindata, dsc->line, cnt);
begindata[cnt] = '\0';
numberof = strtok(begindata+12, " \r\n");
strtok(NULL, " \r\n"); /* dump type */
bytesorlines = strtok(NULL, " \r\n");
if (bytesorlines == NULL)
bytesorlines = "Bytes";
if ( (numberof == NULL) || (bytesorlines == NULL) ) {
/* invalid usage of %%BeginData */
/* ignore that we ever saw it */
int rc = dsc_error(dsc, CDSC_MESSAGE_INCORRECT_USAGE,
dsc->line, dsc->line_length);
switch (rc) {
case CDSC_RESPONSE_OK:
case CDSC_RESPONSE_CANCEL:
break;
case CDSC_RESPONSE_IGNORE_ALL:
return 0;
}
}
else {
cnt = atoi(numberof);
if (cnt) {
if (bytesorlines && (dsc_stricmp(bytesorlines, "Lines")==0)) {
/* skip cnt lines */
if (dsc->skip_lines == 0) {
/* we are not already skipping lines */
dsc->skip_lines = cnt+1;
}
}
else {
/* byte count doesn't includes \n or \r\n */
/* or \r of %%BeginData: */
/* skip cnt bytes */
if (dsc->skip_bytes == 0) {
/* we are not already skipping lines */
dsc->skip_bytes = cnt;
}
}
}
}
}
else if (COMPARE(dsc->line, "%%BeginBinary:")) {
/* byte count doesn't includes \n or \r\n or \r of %%BeginBinary:*/
int cnt = dsc_get_int(dsc->line + 14,
dsc->line_length - 14, NULL);
if (dsc->skip_bytes == 0) {
/* we are not already skipping lines */
dsc->skip_bytes = cnt;
}
}
}
if ((dsc->line[0]=='%') && (dsc->line[1]=='%') &&
COMPARE(dsc->line, "%%BeginDocument:") ) {
/* Skip over embedded document, recursively */
dsc->skip_document++;
}
if (!dsc->long_line && (dsc->line_length > DSC_LINE_LENGTH)) {
dsc_error(dsc, CDSC_MESSAGE_LONG_LINE, dsc->line, dsc->line_length);
dsc->long_line = TRUE;
}
return dsc->line_length;
}
/* Save last DSC line, for use with %%+ */
static void
dsc_save_line(CDSC *dsc)
{
int len = min(sizeof(dsc->last_line), dsc->line_length);
memcpy(dsc->last_line, dsc->line, len);
}
/* display unknown DSC line */
static void
dsc_unknown(CDSC *dsc)
{
if (dsc->debug_print_fn) {
char line[DSC_LINE_LENGTH];
unsigned int length = min(DSC_LINE_LENGTH-1, dsc->line_length);
sprintf(line, "Unknown in %s section at line %d:\n ",
dsc_scan_section_name[dsc->scan_section], dsc->line_count);
dsc_debug_print(dsc, line);
strncpy(line, dsc->line, length);
line[length] = '\0';
dsc_debug_print(dsc, line);
dsc_debug_print(dsc, "\n");
}
}
static GSBOOL
dsc_is_section(char *line)
{
if ( !((line[0]=='%') && (line[1]=='%')) )
return FALSE;
if (IS_DSC(line, "%%BeginPreview"))
return TRUE;
if (IS_DSC(line, "%%BeginDefaults"))
return TRUE;
if (IS_DSC(line, "%%BeginProlog"))
return TRUE;
if (IS_DSC(line, "%%BeginSetup"))
return TRUE;
if (IS_DSC(line, "%%Page:"))
return TRUE;
if (IS_DSC(line, "%%Trailer"))
return TRUE;
if (IS_DSC(line, "%%EOF"))
return TRUE;
return FALSE;
}
/* Get little-endian DWORD, used for DOS EPS files */
static GSDWORD
dsc_get_dword(const unsigned char *buf)
{
GSDWORD dw;
dw = (GSDWORD)buf[0];
dw += ((GSDWORD)buf[1])<<8;
dw += ((GSDWORD)buf[2])<<16;
dw += ((GSDWORD)buf[3])<<24;
return dw;
}
static GSWORD
dsc_get_word(const unsigned char *buf)
{
GSWORD w;
w = (GSWORD)buf[0];
w |= (GSWORD)(buf[1]<<8);
return w;
}
/* Get big-endian DWORD, used for Mac Binary files */
static GSDWORD
dsc_get_bigendian_dword(const unsigned char *buf)
{
GSDWORD dw;
dw = (GSDWORD)buf[3];
dw += ((GSDWORD)buf[2])<<8;
dw += ((GSDWORD)buf[1])<<16;
dw += ((GSDWORD)buf[0])<<24;
return dw;
}
static GSWORD
dsc_get_bigendian_word(const unsigned char *buf)
{
GSWORD w;
w = (GSWORD)buf[1];
w |= (GSWORD)(buf[0]<<8);
return w;
}
static int
dsc_read_doseps(CDSC *dsc)
{
unsigned char *line = (unsigned char *)dsc->line;
if ((dsc->doseps = (CDSCDOSEPS *)dsc_memalloc(dsc, sizeof(CDSCDOSEPS))) == NULL)
return CDSC_ERROR; /* no memory */
dsc->doseps->ps_begin = dsc_get_dword(line+4);
dsc->doseps->ps_length = dsc_get_dword(line+8);
dsc->doseps->wmf_begin = dsc_get_dword(line+12);
dsc->doseps->wmf_length = dsc_get_dword(line+16);
dsc->doseps->tiff_begin = dsc_get_dword(line+20);
dsc->doseps->tiff_length = dsc_get_dword(line+24);
dsc->doseps->checksum = dsc_get_word(line+28);
if (dsc->file_length &&
(dsc->doseps->ps_begin + dsc->doseps->ps_length > dsc->file_length)) {
/* Error in DOS EPS header.
* Some files have been seen with a fixed large value as
* the length of the PostScript section.
* Correct for these erroneous files.
*/
dsc->doseps->ps_length =
(GSDWORD)(dsc->file_length - dsc->doseps->ps_begin);
}
dsc->doseps_end = dsc->doseps->ps_begin + dsc->doseps->ps_length;
/* move data_index backwards to byte after doseps header */
dsc->data_index -= dsc->line_length - 30;
/* we haven't read a line of PostScript code yet */
dsc->line_count = 0;
/* skip from current position to start of PostScript section */
dsc->skip_bytes = dsc->doseps->ps_begin - 30;
if (dsc->doseps->tiff_begin)
dsc->preview = CDSC_TIFF;
if (dsc->doseps->wmf_begin)
dsc->preview = CDSC_WMF;
return CDSC_OK;
}
static int
dsc_read_macbin(CDSC *dsc)
{
unsigned char *line = (unsigned char *)dsc->line;
if ((dsc->macbin =
(CDSCMACBIN *)dsc_memalloc(dsc, sizeof(CDSCMACBIN))) == NULL)
return CDSC_ERROR; /* no memory */
dsc->macbin->data_begin = 128;
dsc->macbin->data_length = dsc_get_bigendian_dword(line+83);
dsc->macbin->resource_begin =
(dsc->macbin->data_begin + dsc->macbin->data_length + 127 ) & ~127;
dsc->macbin->resource_length = dsc_get_bigendian_dword(line+87);
if (dsc->file_length &&
(((dsc->macbin->resource_begin + dsc->macbin->resource_length
+ 127) & ~127) > dsc->file_length)) {
return CDSC_ERROR;
}
dsc->doseps_end = dsc->macbin->data_begin + dsc->macbin->data_length;
/* move data_index to byte after Mac Binary header */
dsc->data_index -= dsc->line_length - 128;
/* we haven't read a line of PostScript code yet */
dsc->line_count = 0;
dsc->preview = CDSC_PICT;
return CDSC_OK;
}
static int
dsc_read_applesingle(CDSC *dsc)
{
GSDWORD EntryID;
GSDWORD Offset;
GSDWORD Length;
GSWORD entries;
int index;
int header;
int i;
unsigned char *line = (unsigned char *)dsc->line;
if ((dsc->macbin =
(CDSCMACBIN *)dsc_memalloc(dsc, sizeof(CDSCMACBIN))) == NULL)
return CDSC_ERROR; /* no memory */
entries = dsc_get_bigendian_word(line+24);
for (i=0; i<(int)entries; i++) {
index = 26 + i * 12;
EntryID = dsc_get_bigendian_dword(line+index);
Offset = dsc_get_bigendian_dword(line+index+4);
Length = dsc_get_bigendian_dword(line+index+8);
if (EntryID == 1) {
/* data fork */
dsc->macbin->data_begin = Offset;
dsc->macbin->data_length = Length;
}
else if (EntryID == 2) {
/* resource fork */
dsc->macbin->resource_begin = Offset;
dsc->macbin->resource_length = Length;
}
}
if (dsc->file_length &&
(dsc->macbin->resource_begin + dsc->macbin->resource_length
> dsc->file_length)) {
return CDSC_ERROR;
}
if (dsc->file_length &&
(dsc->macbin->data_begin + dsc->macbin->data_length
> dsc->file_length)) {
return CDSC_ERROR;
}
dsc->doseps_end = dsc->macbin->data_begin + dsc->macbin->data_length;
header = 26 + entries * 12;
/* move data_index to byte after AppleSingle/AppleDouble header */
dsc->data_index -= dsc->line_length - header;
/* we haven't read a line of PostScript code yet */
dsc->line_count = 0;
/* skip from current position to start of PostScript section */
dsc->skip_bytes = dsc->macbin->data_begin - header;
dsc->preview = CDSC_PICT;
return CDSC_OK;
}
static int
dsc_parse_pages(CDSC *dsc)
{
int ip, io;
unsigned int i;
char *p;
int n;
if ((dsc->page_pages != 0) && (dsc->scan_section == scan_comments)) {
int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line,
dsc->line_length);
switch (rc) {
case CDSC_RESPONSE_OK:
case CDSC_RESPONSE_CANCEL:
return CDSC_OK; /* ignore duplicate comments in header */
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
if ((dsc->page_pages != 0) && (dsc->scan_section == scan_trailer)) {
int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_TRAILER, dsc->line,
dsc->line_length);
switch (rc) {
case CDSC_RESPONSE_OK:
case CDSC_RESPONSE_CANCEL:
break; /* use duplicate comments in header */
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
n = IS_DSC(dsc->line, "%%+") ? 3 : 8;
while (IS_WHITE(dsc->line[n]))
n++;
p = dsc->line + n;
if (COMPARE(p, "atend")) {
if (dsc->scan_section != scan_comments)
dsc_unknown(dsc);
else {
int rc = dsc_error(dsc, CDSC_MESSAGE_ATEND,
dsc->line, dsc->line_length);
switch (rc) {
case CDSC_RESPONSE_OK:
/* assume (atend) */
/* we should mark it as deferred */
break;
case CDSC_RESPONSE_CANCEL:
/* ignore it */
break;
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
}
else if (COMPARE(p, "(atend)")) {
if (dsc->scan_section != scan_comments)
dsc_unknown(dsc);
/* do nothing */
/* we should mark it as deferred */
}
else {
ip = dsc_get_int(dsc->line+n, dsc->line_length-n, &i);
if (i) {
n+=i;
dsc->page_pages = ip;
io = dsc_get_int(dsc->line+n, dsc->line_length-n, &i);
if (i) {
/* DSC 2 uses extra integer to indicate page order */
/* DSC 3 uses %%PageOrder: */
if (dsc->page_order == CDSC_ORDER_UNKNOWN)
switch (io) {
case -1:
dsc->page_order = CDSC_DESCEND;
break;
case 0:
dsc->page_order = CDSC_SPECIAL;
break;
case 1:
dsc->page_order = CDSC_ASCEND;
break;
}
}
}
else {
int rc = dsc_error(dsc, CDSC_MESSAGE_INCORRECT_USAGE, dsc->line,
dsc->line_length);
switch (rc) {
case CDSC_RESPONSE_OK:
case CDSC_RESPONSE_CANCEL:
/* ignore it */
break;
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
}
return CDSC_OK;
}
static int
dsc_parse_bounding_box(CDSC *dsc, CDSCBBOX** pbbox, int offset)
{
unsigned int i, n;
int llx, lly, urx, ury;
float fllx, flly, furx, fury;
char *p;
/* Process first %%BoundingBox: in comments, and last in trailer */
if ((*pbbox != NULL) && (dsc->scan_section == scan_comments)) {
int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line,
dsc->line_length);
switch (rc) {
case CDSC_RESPONSE_OK:
case CDSC_RESPONSE_CANCEL:
return CDSC_OK; /* ignore duplicate comments in header */
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
if ((*pbbox != NULL) && (dsc->scan_section == scan_pages)) {
int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line,
dsc->line_length);
switch (rc) {
case CDSC_RESPONSE_OK:
case CDSC_RESPONSE_CANCEL:
return CDSC_OK; /* ignore duplicate comments in header */
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
if ((*pbbox != NULL) && (dsc->scan_section == scan_trailer)) {
int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_TRAILER, dsc->line,
dsc->line_length);
switch (rc) {
case CDSC_RESPONSE_OK:
case CDSC_RESPONSE_CANCEL:
break; /* use duplicate comments in trailer */
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
if (*pbbox != NULL) {
dsc_memfree(dsc, *pbbox);
*pbbox = NULL;
}
/* should only process first %%BoundingBox: */
while (IS_WHITE(dsc->line[offset]))
offset++;
p = dsc->line + offset;
if (COMPARE(p, "atend")) {
if (dsc->scan_section == scan_trailer)
dsc_unknown(dsc);
else {
int rc = dsc_error(dsc, CDSC_MESSAGE_ATEND, dsc->line,
dsc->line_length);
switch (rc) {
case CDSC_RESPONSE_OK:
/* assume (atend) */
/* we should mark it as deferred */
break;
case CDSC_RESPONSE_CANCEL:
/* ignore it */
break;
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
}
else if (COMPARE(p, "(atend)")) {
if (dsc->scan_section == scan_trailer)
dsc_unknown(dsc);
/* do nothing */
/* we should mark it as deferred */
}
else {
/* llx = */ lly = urx = ury = 0;
n = offset;
llx = dsc_get_int(dsc->line+n, dsc->line_length-n, &i);
n += i;
if (i)
lly = dsc_get_int(dsc->line+n, dsc->line_length-n, &i);
n += i;
if (i)
urx = dsc_get_int(dsc->line+n, dsc->line_length-n, &i);
n += i;
if (i)
ury = dsc_get_int(dsc->line+n, dsc->line_length-n, &i);
if (i) {
*pbbox = (CDSCBBOX *)dsc_memalloc(dsc, sizeof(CDSCBBOX));
if (*pbbox == NULL)
return CDSC_ERROR; /* no memory */
(*pbbox)->llx = llx;
(*pbbox)->lly = lly;
(*pbbox)->urx = urx;
(*pbbox)->ury = ury;
}
else {
int rc = dsc_error(dsc, CDSC_MESSAGE_BBOX, dsc->line,
dsc->line_length);
switch (rc) {
case CDSC_RESPONSE_OK:
/* fllx = */ flly = furx = fury = 0.0;
n = offset;
n += i;
fllx = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
n += i;
if (i)
flly = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
n += i;
if (i)
furx = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
n += i;
if (i)
fury = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
if (i) {
*pbbox = (CDSCBBOX *)dsc_memalloc(dsc, sizeof(CDSCBBOX));
if (*pbbox == NULL)
return CDSC_ERROR; /* no memory */
(*pbbox)->llx = (int)fllx;
(*pbbox)->lly = (int)flly;
(*pbbox)->urx = (int)(furx+0.999);
(*pbbox)->ury = (int)(fury+0.999);
}
return CDSC_OK;
case CDSC_RESPONSE_CANCEL:
return CDSC_OK;
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
}
return CDSC_OK;
}
static int
dsc_parse_float_bounding_box(CDSC *dsc, CDSCFBBOX** pbbox, int offset)
{
unsigned int i, n;
float fllx, flly, furx, fury;
char *p;
/* Process first %%HiResBoundingBox: or %%CropBox: in comments,
* and last in trailer.
*/
if ((*pbbox != NULL) && (dsc->scan_section == scan_comments)) {
int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line,
dsc->line_length);
switch (rc) {
case CDSC_RESPONSE_OK:
case CDSC_RESPONSE_CANCEL:
return CDSC_OK; /* ignore duplicate comments in header */
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
if ((*pbbox != NULL) && (dsc->scan_section == scan_pages)) {
int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line,
dsc->line_length);
switch (rc) {
case CDSC_RESPONSE_OK:
case CDSC_RESPONSE_CANCEL:
return CDSC_OK; /* ignore duplicate comments in header */
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
if ((*pbbox != NULL) && (dsc->scan_section == scan_trailer)) {
int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_TRAILER, dsc->line,
dsc->line_length);
switch (rc) {
case CDSC_RESPONSE_OK:
case CDSC_RESPONSE_CANCEL:
break; /* use duplicate comments in trailer */
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
if (*pbbox != NULL) {
dsc_memfree(dsc, *pbbox);
*pbbox = NULL;
}
/* should only process first %%BoundingBox: */
while (IS_WHITE(dsc->line[offset]))
offset++;
p = dsc->line + offset;
if (COMPARE(p, "atend")) {
if (dsc->scan_section == scan_trailer)
dsc_unknown(dsc);
else {
int rc = dsc_error(dsc, CDSC_MESSAGE_ATEND, dsc->line,
dsc->line_length);
switch (rc) {
case CDSC_RESPONSE_OK:
/* assume (atend) */
/* we should mark it as deferred */
break;
case CDSC_RESPONSE_CANCEL:
/* ignore it */
break;
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
}
else if (COMPARE(p, "(atend)")) {
if (dsc->scan_section == scan_trailer)
dsc_unknown(dsc);
/* do nothing */
/* we should mark it as deferred */
}
else {
/* fllx = */ flly = furx = fury = 0.0;
n = offset;
fllx = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
n += i;
if (i)
flly = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
n += i;
if (i)
furx = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
n += i;
if (i)
fury = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
if (i) {
*pbbox = (CDSCFBBOX *)dsc_memalloc(dsc, sizeof(CDSCFBBOX));
if (*pbbox == NULL)
return CDSC_ERROR; /* no memory */
(*pbbox)->fllx = fllx;
(*pbbox)->flly = flly;
(*pbbox)->furx = furx;
(*pbbox)->fury = fury;
}
}
return CDSC_OK;
}
static int
dsc_parse_orientation(CDSC *dsc, unsigned int *porientation, int offset)
{
char *p;
if ((dsc->page_orientation != CDSC_ORIENT_UNKNOWN) &&
(dsc->scan_section == scan_comments)) {
int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line,
dsc->line_length);
switch (rc) {
case CDSC_RESPONSE_OK:
case CDSC_RESPONSE_CANCEL:
return CDSC_OK; /* ignore duplicate comments in header */
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
if ((dsc->page_orientation != CDSC_ORIENT_UNKNOWN) &&
(dsc->scan_section == scan_trailer)) {
int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_TRAILER, dsc->line,
dsc->line_length);
switch (rc) {
case CDSC_RESPONSE_OK:
case CDSC_RESPONSE_CANCEL:
break; /* use duplicate comments in header; */
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
p = dsc->line + offset;
while (IS_WHITE(*p))
p++;
if (COMPARE(p, "atend")) {
if (dsc->scan_section == scan_trailer)
dsc_unknown(dsc);
else {
int rc = dsc_error(dsc, CDSC_MESSAGE_ATEND,
dsc->line, dsc->line_length);
switch (rc) {
case CDSC_RESPONSE_OK:
/* assume (atend) */
/* we should mark it as deferred */
break;
case CDSC_RESPONSE_CANCEL:
/* ignore it */
break;
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
}
else if (COMPARE(p, "(atend)")) {
if (dsc->scan_section == scan_trailer)
dsc_unknown(dsc);
/* do nothing */
/* we should mark it as deferred */
}
else if (COMPARE(p, "Portrait")) {
*porientation = CDSC_PORTRAIT;
}
else if (COMPARE(p, "Landscape")) {
*porientation = CDSC_LANDSCAPE;
}
else {
dsc_unknown(dsc);
}
return CDSC_OK;
}
static int
dsc_parse_order(CDSC *dsc)
{
char *p;
if ((dsc->page_order != CDSC_ORDER_UNKNOWN) &&
(dsc->scan_section == scan_comments)) {
int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_COMMENT, dsc->line,
dsc->line_length);
switch (rc) {
case CDSC_RESPONSE_OK:
case CDSC_RESPONSE_CANCEL:
return CDSC_OK; /* ignore duplicate comments in header */
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
if ((dsc->page_order != CDSC_ORDER_UNKNOWN) &&
(dsc->scan_section == scan_trailer)) {
int rc = dsc_error(dsc, CDSC_MESSAGE_DUP_TRAILER, dsc->line,
dsc->line_length);
switch (rc) {
case CDSC_RESPONSE_OK:
case CDSC_RESPONSE_CANCEL:
break; /* use duplicate comments in trailer */
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
p = dsc->line + (IS_DSC(dsc->line, "%%+") ? 3 : 13);
while (IS_WHITE(*p))
p++;
if (COMPARE(p, "atend")) {
if (dsc->scan_section == scan_trailer)
dsc_unknown(dsc);
else {
int rc = dsc_error(dsc, CDSC_MESSAGE_ATEND, dsc->line,
dsc->line_length);
switch (rc) {
case CDSC_RESPONSE_OK:
/* assume (atend) */
/* we should mark it as deferred */
break;
case CDSC_RESPONSE_CANCEL:
/* ignore it */
break;
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
}
else if (COMPARE(p, "(atend)")) {
if (dsc->scan_section == scan_trailer)
dsc_unknown(dsc);
/* do nothing */
/* we should mark it as deferred */
}
else if (COMPARE(p, "Ascend")) {
dsc->page_order = CDSC_ASCEND;
}
else if (COMPARE(p, "Descend")) {
dsc->page_order = CDSC_DESCEND;
}
else if (COMPARE(p, "Special")) {
dsc->page_order = CDSC_SPECIAL;
}
else {
dsc_unknown(dsc);
}
return CDSC_OK;
}
static int
dsc_parse_media(CDSC *dsc, const CDSCMEDIA **page_media)
{
char media_name[MAXSTR];
int n = IS_DSC(dsc->line, "%%+") ? 3 : 12; /* %%PageMedia: */
unsigned int i;
if (dsc_copy_string(media_name, sizeof(media_name)-1,
dsc->line+n, dsc->line_length-n, NULL)) {
for (i=0; i<dsc->media_count; i++) {
if (dsc->media[i]->name &&
(dsc_stricmp(media_name, dsc->media[i]->name) == 0)) {
*page_media = dsc->media[i];
return CDSC_OK;
}
}
}
dsc_unknown(dsc);
return CDSC_OK;
}
static int
dsc_parse_document_media(CDSC *dsc)
{
unsigned int i, n;
CDSCMEDIA lmedia;
GSBOOL blank_line;
if (IS_DSC(dsc->line, "%%DocumentMedia:"))
n = 16;
else if (IS_DSC(dsc->line, "%%+"))
n = 3;
else
return CDSC_ERROR; /* error */
/* check for blank remainder of line */
blank_line = TRUE;
for (i=n; i<dsc->line_length; i++) {
if (!IS_WHITE_OR_EOL(dsc->line[i])) {
blank_line = FALSE;
break;
}
}
if (!blank_line) {
char name[MAXSTR];
char colour[MAXSTR];
char type[MAXSTR];
lmedia.name = lmedia.colour = lmedia.type = (char *)NULL;
lmedia.width = lmedia.height = lmedia.weight = 0;
lmedia.mediabox = (CDSCBBOX *)NULL;
lmedia.name = dsc_copy_string(name, sizeof(name),
dsc->line+n, dsc->line_length-n, &i);
n+=i;
if (i)
lmedia.width = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
n+=i;
if (i)
lmedia.height = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
n+=i;
if (i)
lmedia.weight = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
n+=i;
if (i)
lmedia.colour = dsc_copy_string(colour, sizeof(colour),
dsc->line+n, dsc->line_length-n, &i);
n+=i;
if (i)
lmedia.type = dsc_copy_string(type, sizeof(type),
dsc->line+n, dsc->line_length-n, &i);
if (i==0)
dsc_unknown(dsc); /* we didn't get all fields */
else {
if (dsc_add_media(dsc, &lmedia))
return CDSC_ERROR; /* out of memory */
}
}
return CDSC_OK;
}
/* viewing orientation is believed to be the first four elements of
* a CTM matrix
*/
static int
dsc_parse_viewing_orientation(CDSC *dsc, CDSCCTM **pctm)
{
CDSCCTM ctm;
unsigned int i, n;
if (*pctm != NULL) {
dsc_memfree(dsc, *pctm);
*pctm = NULL;
}
n = IS_DSC(dsc->line, "%%+") ? 3 : 21; /* %%ViewingOrientation: */
while (IS_WHITE(dsc->line[n]))
n++;
/* ctm.xx = */ ctm.xy = ctm.yx = ctm.yy = 0.0;
ctm.xx = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
n += i;
if (i)
ctm.xy = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
n += i;
if (i)
ctm.yx = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
n += i;
if (i)
ctm.yy = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
if (i==0) {
dsc_unknown(dsc); /* we didn't get all fields */
}
else {
*pctm = (CDSCCTM *)dsc_memalloc(dsc, sizeof(CDSCCTM));
if (*pctm == NULL)
return CDSC_ERROR; /* no memory */
**pctm = ctm;
}
return CDSC_OK;
}
/* This is called before dsc_read_line(), since we may
* need to skip a binary header which contains a new line
* character
*/
static int
dsc_scan_type(CDSC *dsc)
{
unsigned char *p;
unsigned char *line = (unsigned char *)(dsc->data + dsc->data_index);
int length = dsc->data_length - dsc->data_index;
/* Types that should be known:
* DSC
* EPSF
* PJL + any of above
* ^D + any of above
* DOS EPS
* PDF
* non-DSC
*/
/* First process any non PostScript headers */
/* At this stage we do not have a complete line */
if (length == 0)
return CDSC_NEEDMORE;
/* If we have already found a DOS EPS header, */
/* ignore all until the PostScript section */
if (dsc->skip_bytes) {
int cnt = min(dsc->skip_bytes,
(int)(dsc->data_length - dsc->data_index));
dsc->skip_bytes -= cnt;
dsc->data_index += cnt;
length -= cnt;
line += cnt;
if (dsc->skip_bytes != 0)
return CDSC_NEEDMORE;
}
if (dsc->skip_pjl) {
/* skip until first PostScript comment */
while (length >= 2) {
while (length && !IS_EOL(line[0])) {
/* skip until EOL character */
line++;
dsc->data_index++;
length--;
}
while ((length >= 2) && IS_EOL(line[0]) && IS_EOL(line[1])) {
/* skip until EOL followed by non-EOL */
line++;
dsc->data_index++;
length--;
}
if (length < 2)
return CDSC_NEEDMORE;
if (IS_EOL(line[0]) && line[1]=='%') {
line++;
dsc->data_index++;
length--;
dsc->skip_pjl = FALSE;
break;
}
else {
line++;
dsc->data_index++;
length--;
}
}
if (dsc->skip_pjl)
return CDSC_NEEDMORE;
}
if (length == 0)
return CDSC_NEEDMORE;
if (line[0] == '\004') {
line++;
dsc->data_index++;
length--;
dsc->ctrld = TRUE;
}
if (line[0] == '\033') {
/* possibly PJL */
if (length < 9)
return CDSC_NEEDMORE;
if (COMPARE(line, "\033%-12345X")) {
dsc->skip_pjl = TRUE; /* skip until first PostScript comment */
dsc->pjl = TRUE;
dsc->data_index += 9;
return dsc_scan_type(dsc);
}
}
if ((line[0]==0x0) && (length < 2))
return CDSC_NEEDMORE; /* Could be Mac Binary EPSF */
if ((line[0]==0x0) && (line[1] >= 1) && (line[1] <= 63) && (length < 128))
return CDSC_NEEDMORE; /* Could be Mac Binary EPSF */
if ((line[0]==0x0) && (line[1] == 0x5) && (length < 4))
return CDSC_NEEDMORE; /* Could be Mac AppleSingle/AppleDouble */
if ((line[0]==0xc5) && (length < 4))
return CDSC_NEEDMORE; /* Could be DOS EPS */
if ((line[0]==0xc5) && (line[1]==0xd0) &&
(line[2]==0xd3) && (line[3]==0xc6) ) {
/* id is "EPSF" with bit 7 set */
/* read DOS EPS header, then ignore all bytes until the PS section */
if (length < 30)
return CDSC_NEEDMORE;
dsc->line = (char *)line;
if (dsc_read_doseps(dsc))
return CDSC_ERROR;
}
else if ((line[0]==0x0) && (line[1]==0x05) &&
(line[2]==0x16) && ((line[3]==0x0) || (line[3] == 0x07))) {
/* Mac AppleSingle or AppleDouble */
GSDWORD version;
GSWORD entries;
if (length < 26)
return CDSC_NEEDMORE;
version = dsc_get_bigendian_dword(line+4);
entries = dsc_get_bigendian_word(line+24);
if ((version == 0x00010000) || (version == 0x00020000)) {
if (length < (int)(26 + entries * 12))
return CDSC_NEEDMORE;
dsc->line = (char *)line;
if (dsc_read_applesingle(dsc))
return CDSC_ERROR;
}
}
else if ((line[0]==0x0) &&
(line[1] >= 1) && (line[1] <= 63) &&
(line[74]==0x0) &&
(line[65]=='E') && (line[66]=='P') &&
(line[67]=='S') && (line[68]=='F')) {
/* Mac Binary EPSF */
dsc->line = (char *)line;
if (dsc_read_macbin(dsc))
return CDSC_ERROR;
}
else {
if (length < 2)
return CDSC_NEEDMORE;
if ((line[0] == '%') && (line[1] == 'P')) {
if (length < 5)
return CDSC_NEEDMORE;
if (COMPARE(line, "%PDF-")) {
dsc->pdf = TRUE;
dsc->scan_section = scan_comments;
return CDSC_OK;
}
}
}
/* Finally process PostScript headers */
if (dsc_read_line(dsc) <= 0)
return CDSC_NEEDMORE;
dsc->dsc_version = dsc_add_line(dsc, dsc->line, dsc->line_length);
if (COMPARE(dsc->line, "%!PS-Adobe")) {
dsc->dsc = TRUE;
dsc->begincomments = DSC_START(dsc);
if (dsc->dsc_version == NULL)
return CDSC_ERROR; /* no memory */
p = (unsigned char *)dsc->line + 14;
while (IS_WHITE(*p))
p++;
if (COMPARE(p, "EPSF-"))
dsc->epsf = TRUE;
dsc->scan_section = scan_comments;
return CDSC_PSADOBE;
}
if (COMPARE(dsc->line, "%!")) {
dsc->scan_section = scan_comments;
return CDSC_NOTDSC;
}
dsc->scan_section = scan_comments;
return CDSC_NOTDSC; /* unrecognised */
}
static int
dsc_scan_comments(CDSC *dsc)
{
/* Comments section ends at */
/* %%EndComments */
/* another section */
/* line that does not start with %% */
/* Save a few important lines */
char *line = dsc->line;
GSBOOL continued = FALSE;
dsc->id = CDSC_OK;
if (IS_DSC(line, "%%EndComments")) {
dsc->id = CDSC_ENDCOMMENTS;
dsc->endcomments = DSC_END(dsc);
dsc->scan_section = scan_pre_preview;
return CDSC_OK;
}
else if (IS_DSC(line, "%%BeginComments")) {
/* ignore because we are in this section */
dsc->id = CDSC_BEGINCOMMENTS;
}
else if (dsc_is_section(line)) {
dsc->endcomments = DSC_START(dsc);
dsc->scan_section = scan_pre_preview;
return CDSC_PROPAGATE;
}
else if (line[0] == '%' && IS_WHITE_OR_EOL(line[1])) {
dsc->endcomments = DSC_START(dsc);
dsc->scan_section = scan_pre_preview;
return CDSC_PROPAGATE;
}
else if (line[0] != '%') {
dsc->id = CDSC_OK;
dsc->endcomments = DSC_START(dsc);
dsc->scan_section = scan_pre_preview;
return CDSC_PROPAGATE;
}
else if (IS_DSC(line, "%%Begin")) {
dsc->endcomments = DSC_START(dsc);
dsc->scan_section = scan_pre_preview;
return CDSC_PROPAGATE;
}
/* Handle continuation lines.
* To simply processing, we assume that contination lines
* will only occur if repeat parameters are allowed and that
* a complete set of these parameters appears on each line.
* This is more restrictive than the DSC specification, but
* is valid for the DSC comments understood by this parser
* for all documents that we have seen.
*/
if (IS_DSC(line, "%%+")) {
line = dsc->last_line;
continued = TRUE;
}
else
dsc_save_line(dsc);
if (IS_DSC(line, "%%Pages:")) {
dsc->id = CDSC_PAGES;
if (dsc_parse_pages(dsc) != 0)
return CDSC_ERROR;
}
else if (IS_DSC(line, "%%Creator:")) {
dsc->id = CDSC_CREATOR;
dsc->dsc_creator = dsc_add_line(dsc, dsc->line+10, dsc->line_length-10);
if (dsc->dsc_creator==NULL)
return CDSC_ERROR;
}
else if (IS_DSC(line, "%%CreationDate:")) {
dsc->id = CDSC_CREATIONDATE;
dsc->dsc_date = dsc_add_line(dsc, dsc->line+15, dsc->line_length-15);
if (dsc->dsc_date==NULL)
return CDSC_ERROR;
}
else if (IS_DSC(line, "%%Title:")) {
dsc->id = CDSC_TITLE;
dsc->dsc_title = dsc_add_line(dsc, dsc->line+8, dsc->line_length-8);
if (dsc->dsc_title==NULL)
return CDSC_ERROR;
}
else if (IS_DSC(line, "%%For:")) {
dsc->id = CDSC_FOR;
dsc->dsc_for = dsc_add_line(dsc, dsc->line+6, dsc->line_length-6);
if (dsc->dsc_for==NULL)
return CDSC_ERROR;
}
else if (IS_DSC(line, "%%LanguageLevel:")) {
unsigned int n = continued ? 3 : 16;
unsigned int i;
int ll;
dsc->id = CDSC_LANGUAGELEVEL;
ll = dsc_get_int(dsc->line+n, dsc->line_length-n, &i);
if (i) {
if ( (ll==1) || (ll==2) || (ll==3) )
dsc->language_level = ll;
else {
dsc_unknown(dsc);
}
}
else
dsc_unknown(dsc);
}
else if (IS_DSC(line, "%%BoundingBox:")) {
dsc->id = CDSC_BOUNDINGBOX;
if (dsc_parse_bounding_box(dsc, &(dsc->bbox), continued ? 3 : 14))
return CDSC_ERROR;
}
else if (IS_DSC(line, "%%HiResBoundingBox:")) {
dsc->id = CDSC_HIRESBOUNDINGBOX;
if (dsc_parse_float_bounding_box(dsc, &(dsc->hires_bbox),
continued ? 3 : 19))
return CDSC_ERROR;
}
else if (IS_DSC(line, "%%CropBox:")) {
dsc->id = CDSC_CROPBOX;
if (dsc_parse_float_bounding_box(dsc, &(dsc->crop_box),
continued ? 3 : 10))
return CDSC_ERROR;
}
else if (IS_DSC(line, "%%Orientation:")) {
dsc->id = CDSC_ORIENTATION;
if (dsc_parse_orientation(dsc, &(dsc->page_orientation),
continued ? 3 : 14))
return CDSC_ERROR;
}
else if (IS_DSC(line, "%%PageOrder:")) {
dsc->id = CDSC_PAGEORDER;
if (dsc_parse_order(dsc))
return CDSC_ERROR;
}
else if (IS_DSC(line, "%%DocumentMedia:")) {
dsc->id = CDSC_DOCUMENTMEDIA;
if (dsc_parse_document_media(dsc))
return CDSC_ERROR;
}
else if (IS_DSC(line, "%%DocumentPaperSizes:")) {
/* DSC 2.1 */
unsigned int n = continued ? 3 : 21;
unsigned int count = 0;
unsigned int i = 1;
char name[MAXSTR];
char *p;
dsc->id = CDSC_DOCUMENTPAPERSIZES;
while (i && (dsc->line[n]!='\r') && (dsc->line[n]!='\n')) {
p = dsc_copy_string(name, sizeof(name)-1,
dsc->line+n, dsc->line_length-n, &i);
if (i && p) {
const CDSCMEDIA *m = dsc_known_media;
if (count >= dsc->media_count) {
/* set some default values */
CDSCMEDIA lmedia;
lmedia.name = p;
lmedia.width = 595.0;
lmedia.height = 842.0;
lmedia.weight = 80.0;
lmedia.colour = NULL;
lmedia.type = NULL;
lmedia.mediabox = NULL;
if (dsc_add_media(dsc, &lmedia))
return CDSC_ERROR;
}
else
dsc->media[count]->name =
dsc_alloc_string(dsc, p, (int)strlen(p));
/* find in list of known media */
while (m && m->name) {
if (dsc_stricmp(p, m->name)==0) {
dsc->media[count]->width = m->width;
dsc->media[count]->height = m->height;
break;
}
m++;
}
}
n+=i;
count++;
}
}
else if (IS_DSC(line, "%%DocumentPaperForms:")) {
/* DSC 2.1 */
unsigned int n = continued ? 3 : 21;
unsigned int count = 0;
unsigned int i = 1;
char type[MAXSTR];
char *p;
dsc->id = CDSC_DOCUMENTPAPERFORMS;
while (i && (dsc->line[n]!='\r') && (dsc->line[n]!='\n')) {
p = dsc_copy_string(type, sizeof(type)-1,
dsc->line+n, dsc->line_length-n, &i);
if (i && p) {
if (count >= dsc->media_count) {
/* set some default values */
CDSCMEDIA lmedia;
lmedia.name = NULL;
lmedia.width = 595.0;
lmedia.height = 842.0;
lmedia.weight = 80.0;
lmedia.colour = NULL;
lmedia.type = p;
lmedia.mediabox = NULL;
if (dsc_add_media(dsc, &lmedia))
return CDSC_ERROR;
}
else
dsc->media[count]->type =
dsc_alloc_string(dsc, p, (int)strlen(p));
}
n+=i;
count++;
}
}
else if (IS_DSC(line, "%%DocumentPaperColors:")) {
/* DSC 2.1 */
unsigned int n = continued ? 3 : 22;
unsigned int count = 0;
unsigned int i = 1;
char colour[MAXSTR];
char *p;
dsc->id = CDSC_DOCUMENTPAPERCOLORS;
while (i && (dsc->line[n]!='\r') && (dsc->line[n]!='\n')) {
p = dsc_copy_string(colour, sizeof(colour)-1,
dsc->line+n, dsc->line_length-n, &i);
if (i && p) {
if (count >= dsc->media_count) {
/* set some default values */
CDSCMEDIA lmedia;
lmedia.name = NULL;
lmedia.width = 595.0;
lmedia.height = 842.0;
lmedia.weight = 80.0;
lmedia.colour = p;
lmedia.type = NULL;
lmedia.mediabox = NULL;
if (dsc_add_media(dsc, &lmedia))
return CDSC_ERROR;
}
else
dsc->media[count]->colour =
dsc_alloc_string(dsc, p, (int)strlen(p));
}
n+=i;
count++;
}
}
else if (IS_DSC(line, "%%DocumentPaperWeights:")) {
/* DSC 2.1 */
unsigned int n = continued ? 3 : 23;
unsigned int count = 0;
unsigned int i = 1;
float w;
dsc->id = CDSC_DOCUMENTPAPERWEIGHTS;
while (i && (dsc->line[n]!='\r') && (dsc->line[n]!='\n')) {
w = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
if (i) {
if (count >= dsc->media_count) {
/* set some default values */
CDSCMEDIA lmedia;
lmedia.name = NULL;
lmedia.width = 595.0;
lmedia.height = 842.0;
lmedia.weight = w;
lmedia.colour = NULL;
lmedia.type = NULL;
lmedia.mediabox = NULL;
if (dsc_add_media(dsc, &lmedia))
return CDSC_ERROR;
}
else
dsc->media[count]->weight = w;
}
n+=i;
count++;
}
}
else if (IS_DSC(line, "%%DocumentData:")) {
unsigned int n = continued ? 3 : 15;
char *p = dsc->line + n;
while (IS_WHITE(*p))
p++;
dsc->id = CDSC_DOCUMENTDATA;
if (COMPARE(p, "Clean7Bit"))
dsc->document_data = CDSC_CLEAN7BIT;
else if (COMPARE(p, "Clean8Bit"))
dsc->document_data = CDSC_CLEAN8BIT;
else if (COMPARE(p, "Binary"))
dsc->document_data = CDSC_BINARY;
else
dsc_unknown(dsc);
}
else if (IS_DSC(line, "%%Requirements:")) {
dsc->id = CDSC_REQUIREMENTS;
/* ignore */
}
else if (IS_DSC(line, "%%DocumentNeededFonts:")) {
dsc->id = CDSC_DOCUMENTNEEDEDFONTS;
/* ignore */
}
else if (IS_DSC(line, "%%DocumentSuppliedFonts:")) {
dsc->id = CDSC_DOCUMENTSUPPLIEDFONTS;
/* ignore */
}
else if (IS_DSC(line, "%%PlateFile:")) {
dsc->id = CDSC_PLATEFILE;
if (dsc_parse_platefile(dsc) != CDSC_OK)
dsc->id = CDSC_UNKNOWNDSC;
}
else if (IS_DSC(line, "%%CyanPlate:") ||
IS_DSC(line, "%%MagentaPlate:") ||
IS_DSC(line, "%%YellowPlate:") ||
IS_DSC(line, "%%BlackPlate:")) {
dsc->id = CDSC_PLATEFILE;
if (dsc_parse_dcs1plate(dsc) != CDSC_OK)
dsc->id = CDSC_UNKNOWNDSC;
}
else if (IS_DSC(line, "%%DocumentProcessColors:")) {
dsc->id = CDSC_DOCUMENTPROCESSCOLORS;
if (dsc_parse_process_colours(dsc) != CDSC_OK)
dsc->id = CDSC_UNKNOWNDSC;
}
else if (IS_DSC(line, "%%DocumentCustomColors:")) {
dsc->id = CDSC_DOCUMENTCUSTOMCOLORS;
if (dsc_parse_custom_colours(dsc) != CDSC_OK)
dsc->id = CDSC_UNKNOWNDSC;
}
else if (IS_DSC(line, "%%CMYKCustomColor:")) {
dsc->id = CDSC_CMYKCUSTOMCOLOR;
if (dsc_parse_cmyk_custom_colour(dsc) != CDSC_OK)
dsc->id = CDSC_UNKNOWNDSC;
}
else if (IS_DSC(line, "%%RGBCustomColor:")) {
dsc->id = CDSC_RGBCUSTOMCOLOR;
if (dsc_parse_rgb_custom_colour(dsc) != CDSC_OK)
dsc->id = CDSC_UNKNOWNDSC;
}
else if (dsc->line[0] == '%' && IS_WHITE_OR_EOL(dsc->line[1])) {
dsc->id = CDSC_OK;
/* ignore */
}
else {
dsc->id = CDSC_UNKNOWNDSC;
dsc_unknown(dsc);
}
dsc->endcomments = DSC_END(dsc);
return CDSC_OK;
}
static int
dsc_scan_preview(CDSC *dsc)
{
/* Preview section ends at */
/* %%EndPreview */
/* another section */
/* Preview section must start with %%BeginPreview */
char *line = dsc->line;
dsc->id = CDSC_OK;
if (dsc->scan_section == scan_pre_preview) {
if (IS_BLANK(line))
return CDSC_OK; /* ignore blank lines before preview */
else if (IS_DSC(line, "%%BeginPreview")) {
dsc->id = CDSC_BEGINPREVIEW;
dsc->beginpreview = DSC_START(dsc);
dsc->endpreview = DSC_END(dsc);
dsc->scan_section = scan_preview;
/* Don't mark the preview as EPSI if a DOS EPS header is present */
if (dsc->preview == CDSC_NOPREVIEW)
dsc->preview = CDSC_EPSI;
return CDSC_OK;
}
else {
dsc->scan_section = scan_pre_defaults;
return CDSC_PROPAGATE;
}
}
if (IS_DSC(line, "%%BeginPreview")) {
/* ignore because we are in this section */
}
else if (dsc_is_section(line)) {
dsc->endpreview = DSC_START(dsc);
dsc->scan_section = scan_pre_defaults;
return CDSC_PROPAGATE;
}
else if (IS_DSC(line, "%%EndPreview")) {
dsc->id = CDSC_ENDPREVIEW;
dsc->endpreview = DSC_END(dsc);
dsc->scan_section = scan_pre_defaults;
return CDSC_OK;
}
else if (line[0] == '%' && line[1] != '%') {
/* Ordinary comments are OK */
}
else {
dsc->id = CDSC_UNKNOWNDSC;
/* DSC comments should not occur in preview */
dsc_unknown(dsc);
}
dsc->endpreview = DSC_END(dsc);
return CDSC_OK;
}
static int
dsc_scan_defaults(CDSC *dsc)
{
/* Defaults section ends at */
/* %%EndDefaults */
/* another section */
/* Defaults section must start with %%BeginDefaults */
char *line = dsc->line;
dsc->id = CDSC_OK;
if (dsc->scan_section == scan_pre_defaults) {
if (IS_BLANK(line))
return CDSC_OK; /* ignore blank lines before defaults */
else if (IS_DSC(line, "%%BeginDefaults")) {
dsc->id = CDSC_BEGINDEFAULTS;
dsc->begindefaults = DSC_START(dsc);
dsc->enddefaults = DSC_END(dsc);
dsc->scan_section = scan_defaults;
return CDSC_OK;
}
else {
dsc->scan_section = scan_pre_prolog;
return CDSC_PROPAGATE;
}
}
if (NOT_DSC_LINE(line)) {
/* ignore */
}
else if (IS_DSC(line, "%%BeginPreview")) {
/* ignore because we have already processed this section */
}
else if (IS_DSC(line, "%%BeginDefaults")) {
/* ignore because we are in this section */
}
else if (dsc_is_section(line)) {
dsc->enddefaults = DSC_START(dsc);
dsc->scan_section = scan_pre_prolog;
return CDSC_PROPAGATE;
}
else if (IS_DSC(line, "%%EndDefaults")) {
dsc->id = CDSC_ENDDEFAULTS;
dsc->enddefaults = DSC_END(dsc);
dsc->scan_section = scan_pre_prolog;
return CDSC_OK;
}
else if (IS_DSC(line, "%%PageMedia:")) {
dsc->id = CDSC_PAGEMEDIA;
dsc_parse_media(dsc, &dsc->page_media);
}
else if (IS_DSC(line, "%%PageOrientation:")) {
dsc->id = CDSC_PAGEORIENTATION;
/* This can override %%Orientation: */
if (dsc_parse_orientation(dsc, &(dsc->page_orientation), 18))
return CDSC_ERROR;
}
else if (IS_DSC(line, "%%PageBoundingBox:")) {
dsc->id = CDSC_PAGEBOUNDINGBOX;
if (dsc_parse_bounding_box(dsc, &(dsc->page_bbox), 18))
return CDSC_ERROR;
}
else if (IS_DSC(line, "%%ViewingOrientation:")) {
dsc->id = CDSC_VIEWINGORIENTATION;
if (dsc_parse_viewing_orientation(dsc, &dsc->viewing_orientation))
return CDSC_ERROR;
}
else if (IS_DSC(line, "%%PageCropBox:")) {
dsc->id = CDSC_PAGECROPBOX;
if (dsc_parse_float_bounding_box(dsc, &dsc->crop_box, 14))
return CDSC_ERROR;
}
else {
dsc->id = CDSC_UNKNOWNDSC;
/* All other DSC comments are unknown, but not an error */
dsc_unknown(dsc);
}
dsc->enddefaults = DSC_END(dsc);
return CDSC_OK;
}
/* CDSC_RESPONSE_OK and CDSC_RESPONSE_CANCEL mean ignore the
* mismatch (default) */
static int
dsc_check_match_prompt(CDSC *dsc, const char *str, int count)
{
if (count != 0) {
char buf[MAXSTR+MAXSTR];
if (dsc->line_length < (unsigned int)(sizeof(buf)/2-1)) {
strncpy(buf, dsc->line, dsc->line_length);
buf[dsc->line_length] = '\0';
}
sprintf(buf+strlen(buf), "\n%%%%Begin%.40s: / %%%%End%.40s\n", str, str);
return dsc_error(dsc, CDSC_MESSAGE_BEGIN_END, buf, (int)strlen(buf));
}
return CDSC_RESPONSE_CANCEL;
}
static int
dsc_check_match_type(CDSC *dsc, const char *str, int count)
{
if (dsc_check_match_prompt(dsc, str, count) == CDSC_RESPONSE_IGNORE_ALL)
return CDSC_NOTDSC;
return CDSC_OK;
}
/* complain if Begin/End blocks didn't match */
/* return non-zero if we should ignore all DSC */
static int
dsc_check_match(CDSC *dsc)
{
int rc = 0;
const char *font = "Font";
const char *feature = "Feature";
const char *resource = "Resource";
const char *procset = "ProcSet";
if (!rc)
rc = dsc_check_match_type(dsc, font, dsc->begin_font_count);
if (!rc)
rc = dsc_check_match_type(dsc, feature, dsc->begin_feature_count);
if (!rc)
rc = dsc_check_match_type(dsc, resource, dsc->begin_resource_count);
if (!rc)
rc = dsc_check_match_type(dsc, procset, dsc->begin_procset_count);
dsc->begin_font_count = 0;
dsc->begin_feature_count = 0;
dsc->begin_resource_count = 0;
dsc->begin_procset_count = 0;
return rc;
}
static int
dsc_scan_prolog(CDSC *dsc)
{
/* Prolog section ends at */
/* %%EndProlog */
/* another section */
/* Prolog section may start with %%BeginProlog or non-dsc line */
char *line = dsc->line;
dsc->id = CDSC_OK;
if (dsc->scan_section == scan_pre_prolog) {
if (dsc_is_section(line) && (!IS_DSC(line, "%%BeginProlog"))) {
dsc->scan_section = scan_pre_setup;
return CDSC_PROPAGATE;
}
dsc->id = CDSC_BEGINPROLOG;
dsc->beginprolog = DSC_START(dsc);
dsc->endprolog = DSC_END(dsc);
dsc->scan_section = scan_prolog;
if (IS_DSC(line, "%%BeginProlog"))
return CDSC_OK;
}
if (NOT_DSC_LINE(line)) {
/* ignore */
}
else if (IS_DSC(line, "%%BeginPreview")) {
/* ignore because we have already processed this section */
}
else if (IS_DSC(line, "%%BeginDefaults")) {
/* ignore because we have already processed this section */
}
else if (IS_DSC(line, "%%BeginProlog")) {
/* ignore because we are in this section */
}
else if (dsc_is_section(line)) {
dsc->endprolog = DSC_START(dsc);
dsc->scan_section = scan_pre_setup;
if (dsc_check_match(dsc))
return CDSC_NOTDSC;
return CDSC_PROPAGATE;
}
else if (IS_DSC(line, "%%EndProlog")) {
dsc->id = CDSC_ENDPROLOG;
dsc->endprolog = DSC_END(dsc);
dsc->scan_section = scan_pre_setup;
if (dsc_check_match(dsc))
return CDSC_NOTDSC;
return CDSC_OK;
}
else if (IS_DSC(line, "%%BeginFont:")) {
dsc->id = CDSC_BEGINFONT;
/* ignore Begin/EndFont, apart form making sure */
/* that they are matched. */
dsc->begin_font_count++;
}
else if (IS_DSC(line, "%%EndFont")) {
dsc->id = CDSC_ENDFONT;
dsc->begin_font_count--;
}
else if (IS_DSC(line, "%%BeginFeature:")) {
dsc->id = CDSC_BEGINFEATURE;
/* ignore Begin/EndFeature, apart form making sure */
/* that they are matched. */
dsc->begin_feature_count++;
}
else if (IS_DSC(line, "%%EndFeature")) {
dsc->id = CDSC_ENDFEATURE;
dsc->begin_feature_count--;
}
else if (IS_DSC(line, "%%BeginResource:")) {
dsc->id = CDSC_BEGINRESOURCE;
/* ignore Begin/EndResource, apart form making sure */
/* that they are matched. */
dsc->begin_resource_count++;
}
else if (IS_DSC(line, "%%EndResource")) {
dsc->id = CDSC_ENDRESOURCE;
dsc->begin_resource_count--;
}
else if (IS_DSC(line, "%%BeginProcSet:")) {
dsc->id = CDSC_BEGINPROCSET;
/* ignore Begin/EndProcSet, apart form making sure */
/* that they are matched. */
dsc->begin_procset_count++;
}
else if (IS_DSC(line, "%%EndProcSet")) {
dsc->id = CDSC_ENDPROCSET;
dsc->begin_procset_count--;
}
else {
/* All other DSC comments are unknown, but not an error */
dsc->id = CDSC_UNKNOWNDSC;
dsc_unknown(dsc);
}
dsc->endprolog = DSC_END(dsc);
return CDSC_OK;
}
static int
dsc_scan_setup(CDSC *dsc)
{
/* Setup section ends at */
/* %%EndSetup */
/* another section */
/* Setup section must start with %%BeginSetup */
char *line = dsc->line;
dsc->id = CDSC_OK;
if (dsc->scan_section == scan_pre_setup) {
if (IS_BLANK(line))
return CDSC_OK; /* ignore blank lines before setup */
else if (IS_DSC(line, "%%BeginSetup")) {
dsc->id = CDSC_BEGINSETUP;
dsc->beginsetup = DSC_START(dsc);
dsc->endsetup = DSC_END(dsc);
dsc->scan_section = scan_setup;
return CDSC_OK;
}
else {
dsc->scan_section = scan_pre_pages;
return CDSC_PROPAGATE;
}
}
if (NOT_DSC_LINE(line)) {
/* ignore */
}
else if (IS_DSC(line, "%%BeginPreview")) {
/* ignore because we have already processed this section */
}
else if (IS_DSC(line, "%%BeginDefaults")) {
/* ignore because we have already processed this section */
}
else if (IS_DSC(line, "%%BeginProlog")) {
/* ignore because we have already processed this section */
}
else if (IS_DSC(line, "%%BeginSetup")) {
/* ignore because we are in this section */
}
else if (dsc_is_section(line)) {
dsc->endsetup = DSC_START(dsc);
dsc->scan_section = scan_pre_pages;
if (dsc_check_match(dsc))
return CDSC_NOTDSC;
return CDSC_PROPAGATE;
}
else if (IS_DSC(line, "%%EndSetup")) {
dsc->id = CDSC_ENDSETUP;
dsc->endsetup = DSC_END(dsc);
dsc->scan_section = scan_pre_pages;
if (dsc_check_match(dsc))
return CDSC_NOTDSC;
return CDSC_OK;
}
else if (IS_DSC(line, "%%BeginFeature:")) {
dsc->id = CDSC_BEGINFEATURE;
/* ignore Begin/EndFeature, apart form making sure */
/* that they are matched. */
dsc->begin_feature_count++;
}
else if (IS_DSC(line, "%%EndFeature")) {
dsc->id = CDSC_ENDFEATURE;
dsc->begin_feature_count--;
}
else if (IS_DSC(line, "%%Feature:")) {
dsc->id = CDSC_FEATURE;
/* ignore */
}
else if (IS_DSC(line, "%%BeginResource:")) {
dsc->id = CDSC_BEGINRESOURCE;
/* ignore Begin/EndResource, apart form making sure */
/* that they are matched. */
dsc->begin_resource_count++;
}
else if (IS_DSC(line, "%%EndResource")) {
dsc->id = CDSC_ENDRESOURCE;
dsc->begin_resource_count--;
}
else if (IS_DSC(line, "%%PaperColor:")) {
dsc->id = CDSC_PAPERCOLOR;
/* ignore */
}
else if (IS_DSC(line, "%%PaperForm:")) {
dsc->id = CDSC_PAPERFORM;
/* ignore */
}
else if (IS_DSC(line, "%%PaperWeight:")) {
dsc->id = CDSC_PAPERWEIGHT;
/* ignore */
}
else if (IS_DSC(line, "%%PaperSize:")) {
/* DSC 2.1 */
GSBOOL found_media = FALSE;
int i;
int n = 12;
char buf[MAXSTR];
buf[0] = '\0';
dsc->id = CDSC_PAPERSIZE;
dsc_copy_string(buf, sizeof(buf)-1, dsc->line+n, dsc->line_length-n,
NULL);
for (i=0; i<(int)dsc->media_count; i++) {
if (dsc->media[i] && dsc->media[i]->name &&
(dsc_stricmp(buf, dsc->media[i]->name)==0)) {
dsc->page_media = dsc->media[i];
found_media = TRUE;
break;
}
}
if (!found_media) {
/* It didn't match %%DocumentPaperSizes: */
/* Try our known media */
const CDSCMEDIA *m = dsc_known_media;
while (m->name) {
if (dsc_stricmp(buf, m->name)==0) {
dsc->page_media = m;
break;
}
m++;
}
if (m->name == NULL)
dsc_unknown(dsc);
}
}
else {
/* All other DSC comments are unknown, but not an error */
dsc->id = CDSC_UNKNOWNDSC;
dsc_unknown(dsc);
}
dsc->endsetup = DSC_END(dsc);
return CDSC_OK;
}
static int
dsc_scan_page(CDSC *dsc)
{
/* Page section ends at */
/* %%Page */
/* %%Trailer */
/* %%EOF */
char *line = dsc->line;
dsc->id = CDSC_OK;
if (dsc->scan_section == scan_pre_pages) {
if (IS_DSC(line, "%%Page:")) {
dsc->scan_section = scan_pages;
/* fall through */
}
else {
/* %%Page: didn't follow %%EndSetup
* Keep reading until reach %%Page or %%Trailer
* and add it to previous section.
*/
DSC_OFFSET *last;
if (dsc->endsetup != 0)
last = &dsc->endsetup;
else if (dsc->endprolog != 0)
last = &dsc->endprolog;
else if (dsc->enddefaults != 0)
last = &dsc->enddefaults;
else if (dsc->endpreview != 0)
last = &dsc->endpreview;
else if (dsc->endcomments != 0)
last = &dsc->endcomments;
else
last = &dsc->begincomments;
*last = DSC_START(dsc);
if (IS_DSC(line, "%%Trailer") || IS_DSC(line, "%%EOF")) {
dsc->scan_section = scan_pre_trailer;
return CDSC_PROPAGATE;
}
*last = DSC_END(dsc);
return CDSC_OK;
}
}
if (NOT_DSC_LINE(line)) {
/* ignore */
}
else if (IS_DSC(line, "%%Page:")) {
int code;
dsc->id = CDSC_PAGE;
if (dsc->page_count) {
dsc->page[dsc->page_count-1].end = DSC_START(dsc);
if (dsc_check_match(dsc))
return CDSC_NOTDSC;
}
if ( (code = dsc_parse_page(dsc)) != CDSC_OK)
return code;
if (dsc->page_count == 0)
dsc->scan_section = scan_pre_pages;
}
else if (IS_DSC(line, "%%BeginPreview")) {
/* ignore because we have already processed this section */
}
else if (IS_DSC(line, "%%BeginDefaults")) {
/* ignore because we have already processed this section */
}
else if (IS_DSC(line, "%%BeginProlog")) {
/* ignore because we have already processed this section */
}
else if (IS_DSC(line, "%%BeginSetup")) {
/* ignore because we have already processed this section */
}
else if (dsc_is_section(line)) {
if (IS_DSC(line, "%%Trailer")) {
if (dsc->page_count)
dsc->page[dsc->page_count-1].end = DSC_START(dsc);
if (dsc->file_length) {
if ((!dsc->doseps_end &&
((DSC_END(dsc) + 32768) < dsc->file_length)) ||
((dsc->doseps_end) &&
((DSC_END(dsc) + 32768) < dsc->doseps_end))) {
int rc = dsc_error(dsc, CDSC_MESSAGE_EARLY_TRAILER,
dsc->line, dsc->line_length);
switch (rc) {
case CDSC_RESPONSE_OK:
/* ignore early trailer */
break;
case CDSC_RESPONSE_CANCEL:
/* this is the trailer */
dsc->scan_section = scan_pre_trailer;
if (dsc_check_match(dsc))
return CDSC_NOTDSC;
return CDSC_PROPAGATE;
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
else {
dsc->scan_section = scan_pre_trailer;
if (dsc_check_match(dsc))
return CDSC_NOTDSC;
return CDSC_PROPAGATE;
}
}
else {
dsc->scan_section = scan_pre_trailer;
if (dsc_check_match(dsc))
return CDSC_NOTDSC;
return CDSC_PROPAGATE;
}
}
else if (IS_DSC(line, "%%EOF")) {
if (dsc->page_count)
dsc->page[dsc->page_count-1].end = DSC_START(dsc);
if (dsc->file_length) {
if ((!dsc->doseps_end &&
((DSC_END(dsc) + 100) < dsc->file_length)) ||
((dsc->doseps_end) &&
((DSC_END(dsc) + 100) < dsc->doseps_end))) {
int rc = dsc_error(dsc, CDSC_MESSAGE_EARLY_EOF,
dsc->line, dsc->line_length);
switch (rc) {
case CDSC_RESPONSE_OK:
/* %%EOF is wrong, ignore it */
break;
case CDSC_RESPONSE_CANCEL:
/* %%EOF is correct */
dsc->scan_section = scan_eof;
dsc->eof = TRUE;
if (dsc_check_match(dsc))
return CDSC_NOTDSC;
return CDSC_PROPAGATE;
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
}
else {
/* ignore it */
if (dsc_check_match(dsc))
return CDSC_NOTDSC;
return CDSC_OK;
}
}
else {
/* Section comment, probably from a badly */
/* encapsulated EPS file. */
int rc = dsc_error(dsc, CDSC_MESSAGE_BAD_SECTION,
dsc->line, dsc->line_length);
if (rc == CDSC_RESPONSE_IGNORE_ALL)
return CDSC_NOTDSC;
}
}
else if (IS_DSC(line, "%%PageTrailer")) {
dsc->id = CDSC_PAGETRAILER;
/* ignore */
}
else if (IS_DSC(line, "%%BeginPageSetup")) {
dsc->id = CDSC_BEGINPAGESETUP;
/* ignore */
}
else if (IS_DSC(line, "%%EndPageSetup")) {
dsc->id = CDSC_ENDPAGESETUP;
/* ignore */
}
else if (IS_DSC(line, "%%PageMedia:")) {
dsc->id = CDSC_PAGEMEDIA;
if (dsc->page_count)
dsc_parse_media(dsc, &(dsc->page[dsc->page_count-1].media));
}
else if (IS_DSC(line, "%%PaperColor:")) {
dsc->id = CDSC_PAPERCOLOR;
/* ignore */
}
else if (IS_DSC(line, "%%PaperForm:")) {
dsc->id = CDSC_PAPERFORM;
/* ignore */
}
else if (IS_DSC(line, "%%PaperWeight:")) {
dsc->id = CDSC_PAPERWEIGHT;
/* ignore */
}
else if (IS_DSC(line, "%%PaperSize:")) {
/* DSC 2.1 */
GSBOOL found_media = FALSE;
int i;
int n = 12;
char buf[MAXSTR];
buf[0] = '\0';
dsc_copy_string(buf, sizeof(buf)-1, dsc->line+n,
dsc->line_length-n, NULL);
for (i=0; i<(int)dsc->media_count; i++) {
if (dsc->media[i] && dsc->media[i]->name &&
(dsc_stricmp(buf, dsc->media[i]->name)==0)) {
if (dsc->page_count)
dsc->page[dsc->page_count-1].media = dsc->media[i];
found_media = TRUE;
break;
}
}
if (!found_media) {
/* It didn't match %%DocumentPaperSizes: */
/* Try our known media */
const CDSCMEDIA *m = dsc_known_media;
while (m->name) {
if (dsc_stricmp(buf, m->name)==0) {
if (dsc->page_count)
dsc->page[dsc->page_count-1].media = m;
break;
}
m++;
}
if (m->name == NULL)
dsc_unknown(dsc);
}
}
else if (IS_DSC(line, "%%PageOrientation:")) {
if (dsc->page_count) {
dsc->id = CDSC_PAGEORIENTATION;
if (dsc_parse_orientation(dsc,
&(dsc->page[dsc->page_count-1].orientation) ,18))
return CDSC_NOTDSC;
}
}
else if (IS_DSC(line, "%%PageBoundingBox:")) {
if (dsc->page_count) {
dsc->id = CDSC_PAGEBOUNDINGBOX;
if (dsc_parse_bounding_box(dsc,
&dsc->page[dsc->page_count-1].bbox, 18))
return CDSC_NOTDSC;
}
}
else if (IS_DSC(line, "%%ViewingOrientation:")) {
if (dsc->page_count) {
dsc->id = CDSC_VIEWINGORIENTATION;
if (dsc_parse_viewing_orientation(dsc,
&dsc->page[dsc->page_count-1].viewing_orientation))
return CDSC_ERROR;
}
}
else if (IS_DSC(line, "%%PageCropBox:")) {
if (dsc->page_count) {
dsc->id = CDSC_PAGECROPBOX;
if (dsc_parse_float_bounding_box(dsc,
&(dsc->page[dsc->page_count-1].crop_box), 14))
return CDSC_ERROR;
}
}
else if (IS_DSC(line, "%%BeginFont:")) {
dsc->id = CDSC_BEGINFONT;
/* ignore Begin/EndFont, apart form making sure */
/* that they are matched. */
dsc->begin_font_count++;
}
else if (IS_DSC(line, "%%EndFont")) {
dsc->id = CDSC_BEGINFONT;
dsc->begin_font_count--;
}
else if (IS_DSC(line, "%%BeginFeature:")) {
dsc->id = CDSC_BEGINFEATURE;
/* ignore Begin/EndFeature, apart form making sure */
/* that they are matched. */
dsc->begin_feature_count++;
}
else if (IS_DSC(line, "%%EndFeature")) {
dsc->id = CDSC_ENDFEATURE;
dsc->begin_feature_count--;
}
else if (IS_DSC(line, "%%BeginResource:")) {
dsc->id = CDSC_BEGINRESOURCE;
/* ignore Begin/EndResource, apart form making sure */
/* that they are matched. */
dsc->begin_resource_count++;
}
else if (IS_DSC(line, "%%EndResource")) {
dsc->id = CDSC_ENDRESOURCE;
dsc->begin_resource_count--;
}
else if (IS_DSC(line, "%%BeginProcSet:")) {
dsc->id = CDSC_BEGINPROCSET;
/* ignore Begin/EndProcSet, apart form making sure */
/* that they are matched. */
dsc->begin_procset_count++;
}
else if (IS_DSC(line, "%%EndProcSet")) {
dsc->id = CDSC_ENDPROCSET;
dsc->begin_procset_count--;
}
else if (IS_DSC(line, "%%IncludeFont:")) {
dsc->id = CDSC_INCLUDEFONT;
/* ignore */
}
else {
/* All other DSC comments are unknown, but not an error */
dsc->id = CDSC_UNKNOWNDSC;
dsc_unknown(dsc);
}
if (dsc->page_count)
dsc->page[dsc->page_count-1].end = DSC_END(dsc);
return CDSC_OK;
}
/* Valid Trailer comments are
* %%Trailer
* %%EOF
* or the following deferred with (atend)
* %%BoundingBox:
* %%DocumentCustomColors:
* %%DocumentFiles:
* %%DocumentFonts:
* %%DocumentNeededFiles:
* %%DocumentNeededFonts:
* %%DocumentNeededProcSets:
* %%DocumentNeededResources:
* %%DocumentProcSets:
* %%DocumentProcessColors:
* %%DocumentSuppliedFiles:
* %%DocumentSuppliedFonts:
* %%DocumentSuppliedProcSets:
* %%DocumentSuppliedResources:
* %%Orientation:
* %%Pages:
* %%PageOrder:
*
* Our supported subset is
* %%Trailer
* %%EOF
* %%BoundingBox:
* %%CropBox:
* %%HiResBoundingBox:
* %%DocumentCustomColors:
* %%DocumentProcessColors:
* %%Orientation:
* %%Pages:
* %%PageOrder:
* In addition to these, we support
* %%DocumentMedia:
*
* A %%PageTrailer can have the following:
* %%PageBoundingBox:
* %%PageCustomColors:
* %%PageFiles:
* %%PageFonts:
* %%PageOrientation:
* %%PageProcessColors:
* %%PageResources:
*/
static int
dsc_scan_trailer(CDSC *dsc)
{
/* Trailer section start at */
/* %%Trailer */
/* and ends at */
/* %%EOF */
char *line = dsc->line;
GSBOOL continued = FALSE;
dsc->id = CDSC_OK;
if (dsc->scan_section == scan_pre_trailer) {
if (IS_DSC(line, "%%Trailer")) {
dsc->id = CDSC_TRAILER;
dsc->begintrailer = DSC_START(dsc);
dsc->endtrailer = DSC_END(dsc);
dsc->scan_section = scan_trailer;
return CDSC_OK;
}
else if (IS_DSC(line, "%%EOF")) {
dsc->id = CDSC_EOF;
dsc->begintrailer = DSC_START(dsc);
dsc->endtrailer = DSC_END(dsc);
dsc->scan_section = scan_trailer;
/* Continue, in case we found %%EOF in an embedded document */
return CDSC_OK;
}
else {
/* %%Page: didn't follow %%EndSetup
* Keep reading until reach %%Page or %%Trailer
* and add it to setup section
*/
/* append to previous section */
if (dsc->beginsetup)
dsc->endsetup = DSC_END(dsc);
else if (dsc->beginprolog)
dsc->endprolog = DSC_END(dsc);
else {
/* horribly confused */
}
return CDSC_OK;
}
}
/* Handle continuation lines.
* See comment above about our restrictive processing of
* continuation lines
*/
if (IS_DSC(line, "%%+")) {
line = dsc->last_line;
continued = TRUE;
}
else
dsc_save_line(dsc);
if (NOT_DSC_LINE(line)) {
/* ignore */
}
else if (IS_DSC(dsc->line, "%%EOF")) {
/* Keep scanning, in case we have a false trailer */
dsc->id = CDSC_EOF;
}
else if (IS_DSC(dsc->line, "%%Trailer")) {
/* Cope with no pages with code after setup and before trailer. */
/* Last trailer is the correct one. */
dsc->id = CDSC_TRAILER;
dsc->begintrailer = DSC_START(dsc);
}
else if (IS_DSC(line, "%%Pages:")) {
dsc->id = CDSC_PAGES;
if (dsc_parse_pages(dsc) != 0)
return CDSC_ERROR;
}
else if (IS_DSC(line, "%%BoundingBox:")) {
dsc->id = CDSC_BOUNDINGBOX;
if (dsc_parse_bounding_box(dsc, &(dsc->bbox), continued ? 3 : 14))
return CDSC_ERROR;
}
else if (IS_DSC(line, "%%HiResBoundingBox:")) {
dsc->id = CDSC_HIRESBOUNDINGBOX;
if (dsc_parse_float_bounding_box(dsc, &(dsc->hires_bbox),
continued ? 3 : 19))
return CDSC_ERROR;
}
else if (IS_DSC(line, "%%CropBox:")) {
dsc->id = CDSC_CROPBOX;
if (dsc_parse_float_bounding_box(dsc, &(dsc->crop_box),
continued ? 3 : 10))
return CDSC_ERROR;
}
else if (IS_DSC(line, "%%Orientation:")) {
dsc->id = CDSC_ORIENTATION;
if (dsc_parse_orientation(dsc, &(dsc->page_orientation), continued ? 3 : 14))
return CDSC_ERROR;
}
else if (IS_DSC(line, "%%PageOrder:")) {
dsc->id = CDSC_PAGEORDER;
if (dsc_parse_order(dsc))
return CDSC_ERROR;
}
else if (IS_DSC(line, "%%DocumentMedia:")) {
dsc->id = CDSC_DOCUMENTMEDIA;
if (dsc_parse_document_media(dsc))
return CDSC_ERROR;
}
else if (IS_DSC(dsc->line, "%%Page:")) {
/* This should not occur in the trailer, but we might see
* this if a document has been incorrectly embedded.
*/
int rc = dsc_error(dsc, CDSC_MESSAGE_PAGE_IN_TRAILER,
dsc->line, dsc->line_length);
switch (rc) {
case CDSC_RESPONSE_OK:
/* Assume that we are really in the previous */
/* page, not the trailer */
dsc->scan_section = scan_pre_pages;
if (dsc->page_count)
dsc->page[dsc->page_count-1].end = DSC_START(dsc);
return CDSC_PROPAGATE; /* try again */
case CDSC_RESPONSE_CANCEL:
/* ignore pages in trailer */
break;
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
else if (IS_DSC(line, "%%DocumentNeededFonts:")) {
dsc->id = CDSC_DOCUMENTNEEDEDFONTS;
/* ignore */
}
else if (IS_DSC(line, "%%DocumentSuppliedFonts:")) {
dsc->id = CDSC_DOCUMENTSUPPLIEDFONTS;
/* ignore */
}
else if (IS_DSC(line, "%%DocumentProcessColors:")) {
dsc->id = CDSC_DOCUMENTPROCESSCOLORS;
if (dsc_parse_process_colours(dsc) != CDSC_OK)
dsc->id = CDSC_UNKNOWNDSC;
}
else if (IS_DSC(line, "%%DocumentCustomColors:")) {
dsc->id = CDSC_DOCUMENTCUSTOMCOLORS;
if (dsc_parse_custom_colours(dsc) != CDSC_OK)
dsc->id = CDSC_UNKNOWNDSC;
}
else {
/* All other DSC comments are unknown, but not an error */
dsc->id = CDSC_UNKNOWNDSC;
dsc_unknown(dsc);
}
dsc->endtrailer = DSC_END(dsc);
return CDSC_OK;
}
static char *
dsc_alloc_string(CDSC *dsc, const char *str, int len)
{
char *p;
if (dsc->string_head == NULL) {
dsc->string_head = (CDSCSTRING *)dsc_memalloc(dsc, sizeof(CDSCSTRING));
if (dsc->string_head == NULL)
return NULL; /* no memory */
dsc->string = dsc->string_head;
dsc->string->next = NULL;
dsc->string->data = (char *)dsc_memalloc(dsc, CDSC_STRING_CHUNK);
if (dsc->string->data == NULL) {
dsc_reset(dsc);
return NULL; /* no memory */
}
dsc->string->index = 0;
dsc->string->length = CDSC_STRING_CHUNK;
}
if ( dsc->string->index + len + 1 > dsc->string->length) {
/* allocate another string block */
CDSCSTRING *newstring = (CDSCSTRING *)dsc_memalloc(dsc, sizeof(CDSCSTRING));
if (newstring == NULL) {
dsc_debug_print(dsc, "Out of memory\n");
return NULL;
}
newstring->next = NULL;
newstring->length = 0;
newstring->index = 0;
newstring->data = (char *)dsc_memalloc(dsc, CDSC_STRING_CHUNK);
if (newstring->data == NULL) {
dsc_memfree(dsc, newstring);
dsc_debug_print(dsc, "Out of memory\n");
return NULL; /* no memory */
}
newstring->length = CDSC_STRING_CHUNK;
dsc->string->next = newstring;
dsc->string = newstring;
}
if ( dsc->string->index + len + 1 > dsc->string->length)
return NULL; /* failed */
p = dsc->string->data + dsc->string->index;
memcpy(p, str, len);
*(p+len) = '\0';
dsc->string->index += len + 1;
return p;
}
/* store line, ignoring leading spaces */
static char *
dsc_add_line(CDSC *dsc, const char *line, unsigned int len)
{
char *newline;
unsigned int i;
while (len && (IS_WHITE(*line))) {
len--;
line++;
}
newline = dsc_alloc_string(dsc, line, len);
if (newline == NULL)
return NULL;
for (i=0; i<len; i++) {
if (newline[i] == '\r') {
newline[i]='\0';
break;
}
if (newline[i] == '\n') {
newline[i]='\0';
break;
}
}
return newline;
}
/* Copy string on line to new allocated string str */
/* String is always null terminated */
/* String is no longer than len */
/* Return pointer to string */
/* Store number of used characters from line */
/* Don't copy enclosing () */
static char *
dsc_copy_string(char *str, unsigned int slen, char *line,
unsigned int len, unsigned int *offset)
{
int quoted = FALSE;
int instring=0;
unsigned int newlength = 0;
unsigned int i = 0;
unsigned char ch;
if (len > slen)
len = slen-1;
while ( (i<len) && IS_WHITE(line[i]))
i++; /* skip leading spaces */
if ((i < len) && (line[i]=='(')) {
quoted = TRUE;
instring++;
i++; /* don't copy outside () */
}
while (i < len) {
str[newlength] = ch = line[i];
i++;
if (quoted) {
if (ch == '(')
instring++;
if (ch == ')')
instring--;
if (instring==0)
break;
}
else if (ch == ' ')
break;
if (ch == '\r')
break;
if (ch == '\n')
break;
else if ( (ch == '\\') && (i+1 < len) ) {
ch = line[i];
if ((ch >= '0') && (ch <= '9')) {
/* octal coded character */
int j = 3;
ch = 0;
while (j && (i < len) && line[i]>='0' && line[i]<='7') {
ch = (unsigned char)((ch<<3) + (line[i]-'0'));
i++;
j--;
}
str[newlength] = ch;
}
else if (ch == '(') {
str[newlength] = ch;
i++;
}
else if (ch == ')') {
str[newlength] = ch;
i++;
}
else if (ch == 'b') {
str[newlength] = '\b';
i++;
}
else if (ch == 'f') {
str[newlength] = '\b';
i++;
}
else if (ch == 'n') {
str[newlength] = '\n';
i++;
}
else if (ch == 'r') {
str[newlength] = '\r';
i++;
}
else if (ch == 't') {
str[newlength] = '\t';
i++;
}
else if (ch == '\\') {
str[newlength] = '\\';
i++;
}
}
newlength++;
}
str[newlength] = '\0';
if (offset != (unsigned int *)NULL)
*offset = i;
return str;
}
static int
dsc_get_int(const char *line, unsigned int len, unsigned int *offset)
{
char newline[MAXSTR];
int newlength = 0;
unsigned int i = 0;
unsigned char ch;
len = min(len, sizeof(newline)-1);
while ((i<len) && IS_WHITE(line[i]))
i++; /* skip leading spaces */
while (i < len) {
newline[newlength] = ch = line[i];
if (!(isdigit(ch) || (ch=='-') || (ch=='+')))
break; /* not part of an integer number */
i++;
newlength++;
}
while ((i<len) && IS_WHITE(line[i]))
i++; /* skip trailing spaces */
newline[newlength] = '\0';
if (offset != (unsigned int *)NULL)
*offset = i;
return atoi(newline);
}
static float
dsc_get_real(const char *line, unsigned int len, unsigned int *offset)
{
char newline[MAXSTR];
int newlength = 0;
unsigned int i = 0;
unsigned char ch;
len = min(len, sizeof(newline)-1);
while ((i<len) && IS_WHITE(line[i]))
i++; /* skip leading spaces */
while (i < len) {
newline[newlength] = ch = line[i];
if (!(isdigit(ch) || (ch=='.') || (ch=='-') || (ch=='+')
|| (ch=='e') || (ch=='E')))
break; /* not part of a real number */
i++;
newlength++;
}
while ((i<len) && IS_WHITE(line[i]))
i++; /* skip trailing spaces */
newline[newlength] = '\0';
if (offset != (unsigned int *)NULL)
*offset = i;
return (float)atof(newline);
}
int
dsc_stricmp(const char *s, const char *t)
{
while (toupper((unsigned char)*s) == toupper((unsigned char)*t)) {
if (*s == '\0')
return 0;
s++;
t++;
}
return (toupper((unsigned char)*s) - toupper((unsigned char)*t));
}
static int
dsc_parse_page(CDSC *dsc)
{
char *p;
unsigned int i;
char page_label[MAXSTR];
char *pl;
int page_ordinal;
int page_number;
p = dsc->line + 7;
pl = dsc_copy_string(page_label, sizeof(page_label), p, dsc->line_length-7, &i);
if (pl == NULL)
return CDSC_ERROR;
p += i;
if (dsc->line_length - 7 - i == 0) {
/* Ordinal missing, or parentheses not matched in label */
/* Try to find ordinal at end of line */
while (i > 0) {
if (!IS_WHITE_OR_EOL(p[-1]))
break;
p--;
i--;
}
while (i > 0) {
if (!isdigit((int)p[-1]))
break;
p--;
i--;
}
}
page_ordinal = dsc_get_int(p, dsc->line_length - 7 - i, NULL);
if ( (page_ordinal == 0) || (strlen(page_label) == 0) ||
(dsc->page_count &&
(page_ordinal != dsc->page[dsc->page_count-1].ordinal+1)) ) {
int rc = dsc_error(dsc, CDSC_MESSAGE_PAGE_ORDINAL, dsc->line,
dsc->line_length);
switch (rc) {
case CDSC_RESPONSE_OK:
/* ignore this page */
return CDSC_OK;
case CDSC_RESPONSE_CANCEL:
/* accept the page */
break;
case CDSC_RESPONSE_IGNORE_ALL:
return CDSC_NOTDSC;
}
}
page_number = dsc->page_count;
dsc_add_page(dsc, page_ordinal, page_label);
dsc->page[page_number].begin = DSC_START(dsc);
dsc->page[page_number].end = DSC_START(dsc);
if (dsc->page[page_number].label == NULL)
return CDSC_ERROR; /* no memory */
return CDSC_OK;
}
/* DSC error reporting */
void
dsc_debug_print(CDSC *dsc, const char *str)
{
if (dsc->debug_print_fn)
dsc->debug_print_fn(dsc->caller_data, str);
}
/* Display a message about a problem with the DSC comments.
*
* explanation = an index to to a multiline explanation in dsc_message[]
* line = pointer to the offending DSC line (if any)
* return code =
* CDSC_RESPONSE_OK DSC was wrong, make a guess about what
* was really meant.
* CDSC_RESPONSE_CANCEL Assume DSC was correct, ignore if it
* is misplaced.
* CDSC_RESPONSE_IGNORE_ALL Ignore all DSC.
*/
/* Silent operation. Don't display errors. */
static int
dsc_error(CDSC *dsc, unsigned int explanation,
char *line, unsigned int line_len)
{
/* if error function provided, use it */
if (dsc->dsc_error_fn)
return dsc->dsc_error_fn(dsc->caller_data, dsc,
explanation, line, line_len);
/* treat DSC as being correct */
return CDSC_RESPONSE_CANCEL;
}
/* Fixup if DCS 2.0 was used */
static int
dsc_dcs2_fixup(CDSC *dsc)
{
char composite[] = "Composite";
/* If DCS 2.0 single file format found, expose the separations
* as multiple pages. Treat the initial EPS file as a single
* page without comments, prolog or trailer.
*/
if (dsc->dcs2) {
int code = CDSC_OK;
int page_number;
DSC_OFFSET *pbegin;
DSC_OFFSET *pend;
DSC_OFFSET end;
CDCS2 *pdcs = dsc->dcs2;
/* Now treat the initial EPS file as a single page without
* headers or trailer, so page extraction will fetch the
* the correct separation. */
if (dsc->page_count == 0)
code = dsc_add_page(dsc, 1, composite);
else if (dsc->page_count == 1)
dsc->page[0].label =
dsc_alloc_string(dsc, composite, (int)strlen(composite)+1);
if (code != CDSC_OK)
return code;
page_number = dsc->page_count - 1;
pbegin = &dsc->page[page_number].begin;
pend = &dsc->page[page_number].end;
if (*pbegin == *pend) {
/* no page, so force it to conform to the following sections */
*pbegin = 999999999;
*pend = 0;
}
if (dsc->begincomments != dsc->endcomments) {
*pbegin = min(dsc->begincomments, *pbegin);
dsc->begincomments = 0;
*pend = max(dsc->endcomments, *pend);
dsc->endcomments = 0;
}
if (dsc->beginpreview != dsc->endpreview) {
*pbegin = min(dsc->beginpreview, *pbegin);
dsc->beginpreview = 0;
*pend = max(dsc->endpreview, *pend);
dsc->endpreview = 0;
}
if (dsc->begindefaults != dsc->enddefaults) {
*pbegin = min(dsc->begindefaults, *pbegin);
dsc->begindefaults = 0;
*pend = max(dsc->enddefaults, *pend);
dsc->enddefaults = 0;
}
if (dsc->beginprolog != dsc->endprolog) {
*pbegin = min(dsc->beginprolog, *pbegin);
dsc->beginprolog = 0;
*pend = max(dsc->endprolog, *pend);
dsc->endprolog = 0;
}
if (dsc->beginsetup != dsc->endsetup) {
*pbegin = min(dsc->beginsetup, *pbegin);
dsc->beginsetup = 0;
*pend = max(dsc->endsetup, *pend);
dsc->endsetup = 0;
}
if (dsc->begintrailer != dsc->endtrailer) {
*pbegin = min(dsc->begintrailer, *pbegin);
dsc->begintrailer = 0;
*pend = max(dsc->endtrailer, *pend);
dsc->endtrailer = 0;
}
if (*pbegin == 999999999)
*pbegin = *pend;
end = 0; /* end of composite is start of first separation */
while (pdcs) {
page_number = dsc->page_count;
if ((pdcs->begin) && (pdcs->colourname != NULL)) {
/* Single file DCS 2.0 */
code = dsc_add_page(dsc, page_number+1, pdcs->colourname);
if (code)
return code;
dsc->page[page_number].begin = pdcs->begin;
dsc->page[page_number].end = pdcs->end;
if (end != 0)
end = min(end, pdcs->begin);
else
end = pdcs->begin; /* first separation */
}
else {
/* Multiple file DCS 2.0 */
if ((pdcs->location != NULL) &&
(pdcs->filetype != NULL) &&
(pdcs->colourname != NULL) &&
(dsc_stricmp(pdcs->location, "Local") == 0) &&
((dsc_stricmp(pdcs->filetype, "EPS") == 0) ||
(dsc_stricmp(pdcs->filetype, "EPSF") == 0))) {
code = dsc_add_page(dsc, page_number+1, pdcs->colourname);
if (code)
return code;
dsc->page[page_number].begin = 0;
dsc->page[page_number].end = 0;
}
}
pdcs = pdcs->next;
}
/* end of composite is start of first separation */
if (end != 0)
*pend = end;
/* According to the DCS2 specification, the size of the composite
* section can be determined by the smallest #offset.
* Some incorrect DCS2 files don't put the separations inside
* the DOS EPS PostScript section, and have a TIFF separation
* between the composite and the first separation. This
* contravenes the DCS2 specification. If we see one of these
* files, bring the end of the composite back to the end of
* the DOS EPS PostScript section.
*/
if (dsc->doseps_end && (*pend > dsc->doseps_end))
*pend = dsc->doseps_end;
}
return 0;
}
static int
dsc_parse_platefile(CDSC *dsc)
{
unsigned int i, n;
CDCS2 dcs2;
CDCS2 *pdcs2;
char colourname[MAXSTR];
char filetype[MAXSTR];
char location[MAXSTR];
char *filename = NULL;
int filename_length = 0;
GSBOOL blank_line;
GSBOOL single = FALSE;
if (IS_DSC(dsc->line, "%%PlateFile:"))
n = 12;
else if (IS_DSC(dsc->line, "%%+"))
n = 3;
else
return CDSC_ERROR; /* error */
memset(&dcs2, 0, sizeof(dcs2));
memset(&colourname, 0, sizeof(colourname));
memset(&filetype, 0, sizeof(filetype));
memset(&location, 0, sizeof(location));
memset(&filename, 0, sizeof(filename));
/* check for blank remainder of line */
blank_line = TRUE;
for (i=n; i<dsc->line_length; i++) {
if (!IS_WHITE_OR_EOL(dsc->line[i])) {
blank_line = FALSE;
break;
}
}
if (!blank_line) {
dsc_copy_string(colourname, sizeof(colourname),
dsc->line+n, dsc->line_length-n, &i);
n+=i;
if (i)
dsc_copy_string(filetype, sizeof(filetype),
dsc->line+n, dsc->line_length-n, &i);
n+=i;
while (IS_WHITE_OR_EOL(dsc->line[n]))
n++;
if (dsc->line[n] == '#') {
/* single file DCS 2.0 */
single = TRUE;
n++;
if (i)
dcs2.begin= dsc_get_int(dsc->line+n, dsc->line_length-n, &i);
n+=i;
if (i)
dcs2.end= dcs2.begin +
dsc_get_int(dsc->line+n, dsc->line_length-n, &i);
}
else {
/* multiple file DCS 2.0 */
if (i)
dsc_copy_string(location, sizeof(location),
dsc->line+n, dsc->line_length-n, &i);
n+=i;
if (i) {
filename = dsc->line+n;
filename_length = dsc->line_length-n;
}
}
if (i==0)
dsc_unknown(dsc); /* we didn't get all fields */
else {
/* Allocate strings */
if (strlen(colourname))
dcs2.colourname = dsc_alloc_string(dsc,
colourname, (int)strlen(colourname));
if (strlen(filetype))
dcs2.filetype = dsc_alloc_string(dsc,
filetype, (int)strlen(filetype));
if (strlen(location))
dcs2.location = dsc_alloc_string(dsc,
location, (int)strlen(location));
if (filename)
dcs2.filename = dsc_add_line(dsc, filename, filename_length);
/* Prevent parser from reading separations */
if (single)
dsc->file_length = min(dsc->file_length, dcs2.begin);
/* Allocate it */
pdcs2 = (CDCS2 *)dsc_memalloc(dsc, sizeof(CDCS2));
if (pdcs2 == NULL)
return CDSC_ERROR; /* out of memory */
memcpy(pdcs2, &dcs2, sizeof(CDCS2));
/* Then add to list of separations */
if (dsc->dcs2 == NULL)
dsc->dcs2 = pdcs2;
else {
CDCS2 *this_dcs2 = dsc->dcs2;
while (this_dcs2->next)
this_dcs2 = this_dcs2->next;
this_dcs2->next = pdcs2;
}
}
}
return CDSC_OK;
}
/* Parse a DCS 1.0 plate comment, storing like a multi file DSC 2.0 */
static int
dsc_parse_dcs1plate(CDSC *dsc)
{
unsigned int i, n = 0;
CDCS2 dcs2;
CDCS2 *pdcs2;
const char *colourname;
char filename[MAXSTR];
GSBOOL blank_line;
GSBOOL continued = FALSE;
char *line = dsc->line;
memset(&dcs2, 0, sizeof(dcs2));
memset(&filename, 0, sizeof(filename));
if (IS_DSC(line, "%%+")) {
n = 3;
line = dsc->last_line;
continued = TRUE;
}
if (IS_DSC(line, "%%CyanPlate:")) {
colourname = "Cyan";
if (!continued)
n = 12;
}
else if (IS_DSC(line, "%%MagentaPlate:")) {
colourname = "Magenta";
if (!continued)
n = 15;
}
else if (IS_DSC(line, "%%YellowPlate:")) {
colourname = "Yellow";
if (!continued)
n = 14;
}
else if (IS_DSC(line, "%%BlackPlate:")) {
colourname = "Black";
if (!continued)
n = 13;
}
else
return CDSC_ERROR; /* error */
/* check for blank remainder of line */
blank_line = TRUE;
for (i=n; i<dsc->line_length; i++) {
if (!IS_WHITE_OR_EOL(dsc->line[i])) {
blank_line = FALSE;
break;
}
}
if (!blank_line) {
dsc_copy_string(filename, sizeof(filename),
dsc->line+n, dsc->line_length-n, &i);
if (i==0)
dsc_unknown(dsc); /* we didn't get all fields */
else {
/* Allocate strings */
dcs2.colourname = dsc_alloc_string(dsc,
colourname, (int)strlen(colourname));
dcs2.filetype = dsc_alloc_string(dsc, "EPS", 3);
dcs2.location = dsc_alloc_string(dsc, "Local", 5);
if (strlen(filename))
dcs2.filename = dsc_alloc_string(dsc,
filename, (int)strlen(filename));
/* Allocate it */
pdcs2 = (CDCS2 *)dsc_memalloc(dsc, sizeof(CDCS2));
if (pdcs2 == NULL)
return CDSC_ERROR; /* out of memory */
memcpy(pdcs2, &dcs2, sizeof(CDCS2));
/* Then add to list of separations */
if (dsc->dcs2 == NULL)
dsc->dcs2 = pdcs2;
else {
CDCS2 *this_dcs2 = dsc->dcs2;
while (this_dcs2->next)
this_dcs2 = this_dcs2->next;
this_dcs2->next = pdcs2;
}
}
}
return CDSC_OK;
}
/* Find the filename which corresponds to this separation.
* Used with multiple file DCS 2.0.
* Returns NULL if there is no filename, or not DCS 2.0,
* or single file DCS 2.0.
* Caller will need to obtain the filesize from the file.
*/
const char *
dsc_find_platefile(CDSC *dsc, int page)
{
CDCS2 *pdcs = dsc->dcs2;
int i = 1;
while (pdcs) {
if (pdcs->begin != pdcs->end)
return NULL; /* Single file DCS 2.0 */
if (pdcs->location && pdcs->filetype && pdcs->colourname
&& (dsc_stricmp(pdcs->location, "Local") == 0)
&& ((dsc_stricmp(pdcs->filetype, "EPS") == 0) ||
(dsc_stricmp(pdcs->filetype, "EPSF") == 0))) {
if (i == page)
return pdcs->filename;
i++;
}
pdcs = pdcs->next;
}
return NULL;
}
static CDSCCOLOUR *
dsc_find_colour(CDSC *dsc, const char *colourname)
{
CDSCCOLOUR *colour = dsc->colours;
while (colour) {
if (colour->name && (dsc_stricmp(colour->name, colourname)==0))
return colour;
colour = colour->next;
}
return 0;
}
static int
dsc_parse_process_colours(CDSC *dsc)
{
unsigned int i, n;
CDSCCOLOUR *pcolour;
char colourname[MAXSTR];
GSBOOL blank_line;
if (IS_DSC(dsc->line, "%%DocumentProcessColors:"))
n = 24;
else if (IS_DSC(dsc->line, "%%+"))
n = 3;
else
return CDSC_ERROR; /* error */
memset(&colourname, 0, sizeof(colourname));
/* check for blank remainder of line */
blank_line = TRUE;
for (i=n; i<dsc->line_length; i++) {
if (!IS_WHITE_OR_EOL(dsc->line[i])) {
blank_line = FALSE;
break;
}
}
while (IS_WHITE(dsc->line[n]))
n++;
if (COMPARE(dsc->line+n, "(atend)")) {
if (dsc->scan_section == scan_comments)
blank_line = TRUE;
else {
dsc_unknown(dsc);
return CDSC_NOTDSC;
}
}
if (!blank_line) {
do {
dsc_copy_string(colourname, sizeof(colourname),
dsc->line+n, dsc->line_length-n, &i);
n+=i;
if (i && strlen(colourname)) {
if ((pcolour = dsc_find_colour(dsc, colourname)) == NULL) {
pcolour = (CDSCCOLOUR *)
dsc_memalloc(dsc, sizeof(CDSCCOLOUR));
if (pcolour == NULL)
return CDSC_ERROR; /* out of memory */
memset(pcolour, 0, sizeof(CDSCCOLOUR));
pcolour->custom = CDSC_CUSTOM_COLOUR_UNKNOWN;
pcolour->name = dsc_alloc_string(dsc,
colourname, (int)strlen(colourname));
if (dsc->colours == NULL)
dsc->colours = pcolour;
else {
CDSCCOLOUR *this_colour = dsc->colours;
while (this_colour->next)
this_colour = this_colour->next;
this_colour->next = pcolour;
}
}
pcolour->type = CDSC_COLOUR_PROCESS;
if (dsc_stricmp(colourname, "Cyan")==0) {
pcolour->custom = CDSC_CUSTOM_COLOUR_CMYK;
pcolour->cyan = 1.0;
pcolour->magenta = pcolour->yellow = pcolour->black = 0.0;
}
else if (dsc_stricmp(colourname, "Magenta")==0) {
pcolour->custom = CDSC_CUSTOM_COLOUR_CMYK;
pcolour->magenta = 1.0;
pcolour->cyan = pcolour->yellow = pcolour->black = 0.0;
}
else if (dsc_stricmp(colourname, "Yellow")==0) {
pcolour->custom = CDSC_CUSTOM_COLOUR_CMYK;
pcolour->yellow = 1.0;
pcolour->cyan = pcolour->magenta = pcolour->black = 0.0;
}
else if (dsc_stricmp(colourname, "Black")==0) {
pcolour->custom = CDSC_CUSTOM_COLOUR_CMYK;
pcolour->black = 1.0;
pcolour->cyan = pcolour->magenta = pcolour->yellow = 0.0;
}
else if (dsc_stricmp(colourname, "Red")==0) {
pcolour->custom = CDSC_CUSTOM_COLOUR_RGB;
pcolour->red = 1.0;
pcolour->green = pcolour->blue = 0.0;
}
else if (dsc_stricmp(colourname, "Green")==0) {
pcolour->custom = CDSC_CUSTOM_COLOUR_RGB;
pcolour->green = 1.0;
pcolour->red = pcolour->blue = 0.0;
}
else if (dsc_stricmp(colourname, "Blue")==0) {
pcolour->custom = CDSC_CUSTOM_COLOUR_RGB;
pcolour->blue = 1.0;
pcolour->red = pcolour->green = 0.0;
}
}
} while (i != 0);
}
return CDSC_OK;
}
static int
dsc_parse_custom_colours(CDSC *dsc)
{
unsigned int i, n;
CDSCCOLOUR *pcolour;
char colourname[MAXSTR];
GSBOOL blank_line;
if (IS_DSC(dsc->line, "%%DocumentCustomColors:"))
n = 23;
else if (IS_DSC(dsc->line, "%%+"))
n = 3;
else
return CDSC_ERROR; /* error */
memset(&colourname, 0, sizeof(colourname));
/* check for blank remainder of line */
blank_line = TRUE;
for (i=n; i<dsc->line_length; i++) {
if (!IS_WHITE_OR_EOL(dsc->line[i])) {
blank_line = FALSE;
break;
}
}
while (IS_WHITE(dsc->line[n]))
n++;
if (COMPARE(dsc->line+n, "(atend)")) {
if (dsc->scan_section == scan_comments)
blank_line = TRUE;
else {
dsc_unknown(dsc);
return CDSC_NOTDSC;
}
}
if (!blank_line) {
do {
dsc_copy_string(colourname, sizeof(colourname),
dsc->line+n, dsc->line_length-n, &i);
n+=i;
if (i && strlen(colourname)) {
if ((pcolour = dsc_find_colour(dsc, colourname)) == NULL) {
pcolour = (CDSCCOLOUR *)
dsc_memalloc(dsc, sizeof(CDSCCOLOUR));
if (pcolour == NULL)
return CDSC_ERROR; /* out of memory */
memset(pcolour, 0, sizeof(CDSCCOLOUR));
pcolour->name = dsc_alloc_string(dsc,
colourname, (int)strlen(colourname));
pcolour->custom = CDSC_CUSTOM_COLOUR_UNKNOWN;
if (dsc->colours == NULL)
dsc->colours = pcolour;
else {
CDSCCOLOUR *this_colour = dsc->colours;
while (this_colour->next)
this_colour = this_colour->next;
this_colour->next = pcolour;
}
}
pcolour->type = CDSC_COLOUR_CUSTOM;
}
} while (i != 0);
}
return CDSC_OK;
}
static int
dsc_parse_cmyk_custom_colour(CDSC *dsc)
{
unsigned int i, n;
CDSCCOLOUR *pcolour;
char colourname[MAXSTR];
float cyan, magenta, yellow, black;
GSBOOL blank_line;
if (IS_DSC(dsc->line, "%%CMYKCustomColor:"))
n = 18;
else if (IS_DSC(dsc->line, "%%+"))
n = 3;
else
return CDSC_ERROR; /* error */
memset(&colourname, 0, sizeof(colourname));
/* check for blank remainder of line */
do {
blank_line = TRUE;
for (i=n; i<dsc->line_length; i++) {
if (!IS_WHITE_OR_EOL(dsc->line[i])) {
blank_line = FALSE;
break;
}
}
if (blank_line)
break;
else {
cyan = magenta = yellow = black = 0.0;
cyan = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
n += i;
if (i)
magenta = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
n += i;
if (i)
yellow = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
n += i;
if (i)
black = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
n += i;
if (i)
dsc_copy_string(colourname, sizeof(colourname),
dsc->line+n, dsc->line_length-n, &i);
n+=i;
if (i && strlen(colourname)) {
if ((pcolour = dsc_find_colour(dsc, colourname)) == NULL) {
pcolour = (CDSCCOLOUR *)
dsc_memalloc(dsc, sizeof(CDSCCOLOUR));
if (pcolour == NULL)
return CDSC_ERROR; /* out of memory */
memset(pcolour, 0, sizeof(CDSCCOLOUR));
pcolour->name = dsc_alloc_string(dsc,
colourname, (int)strlen(colourname));
pcolour->type = CDSC_COLOUR_UNKNOWN;
if (dsc->colours == NULL)
dsc->colours = pcolour;
else {
CDSCCOLOUR *this_colour = dsc->colours;
while (this_colour->next)
this_colour = this_colour->next;
this_colour->next = pcolour;
}
}
pcolour->custom = CDSC_CUSTOM_COLOUR_CMYK;
pcolour->cyan = cyan;
pcolour->magenta = magenta;
pcolour->yellow = yellow;
pcolour->black = black;
}
}
} while (i != 0);
return CDSC_OK;
}
static int
dsc_parse_rgb_custom_colour(CDSC *dsc)
{
unsigned int i, n;
CDSCCOLOUR *pcolour;
char colourname[MAXSTR];
float red, green, blue;
GSBOOL blank_line;
if (IS_DSC(dsc->line, "%%RGBCustomColor:"))
n = 17;
else if (IS_DSC(dsc->line, "%%+"))
n = 3;
else
return CDSC_ERROR; /* error */
memset(&colourname, 0, sizeof(colourname));
/* check for blank remainder of line */
do {
blank_line = TRUE;
for (i=n; i<dsc->line_length; i++) {
if (!IS_WHITE_OR_EOL(dsc->line[i])) {
blank_line = FALSE;
break;
}
}
if (blank_line)
break;
else {
red = green = blue = 0.0;
red = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
n += i;
if (i)
green = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
n += i;
if (i)
blue = dsc_get_real(dsc->line+n, dsc->line_length-n, &i);
n += i;
if (i)
dsc_copy_string(colourname, sizeof(colourname),
dsc->line+n, dsc->line_length-n, &i);
n+=i;
if (i && strlen(colourname)) {
if ((pcolour = dsc_find_colour(dsc, colourname)) == NULL) {
pcolour = (CDSCCOLOUR *)
dsc_memalloc(dsc, sizeof(CDSCCOLOUR));
if (pcolour == NULL)
return CDSC_ERROR; /* out of memory */
memset(pcolour, 0, sizeof(CDSCCOLOUR));
pcolour->name = dsc_alloc_string(dsc,
colourname, (int)strlen(colourname));
pcolour->type = CDSC_COLOUR_UNKNOWN;
if (dsc->colours == NULL)
dsc->colours = pcolour;
else {
CDSCCOLOUR *this_colour = dsc->colours;
while (this_colour->next)
this_colour = this_colour->next;
this_colour->next = pcolour;
}
}
pcolour->custom = CDSC_CUSTOM_COLOUR_RGB;
pcolour->red = red;
pcolour->green = green;
pcolour->blue = blue;
}
}
} while (i != 0);
return CDSC_OK;
}
|