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
|
//C- -*- C++ -*-
//C- -------------------------------------------------------------------
//C- DjVuLibre-3.5
//C- Copyright (c) 2002-2003 Leon Bottou and Yann Le Cun.
//C- Copyright (c) 2001 AT&T
//C-
//C- This software is subject to, and may be distributed under, the
//C- GNU General Public License, Version 2. The license should have
//C- accompanied the software or you may obtain a copy of the license
//C- from the Free Software Foundation at http://www.fsf.org .
//C-
//C- This program is distributed in the hope that it will be useful,
//C- but WITHOUT ANY WARRANTY; without even the implied warranty of
//C- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//C- GNU General Public License for more details.
//C-
//C- DjVuLibre-3.5 is derived from the DjVu(r) Reference Library
//C- distributed by Lizardtech Software. On July 19th 2002, Lizardtech
//C- Software authorized us to replace the original DjVu(r) Reference
//C- Library notice by the following text (see doc/lizard2002.djvu):
//C-
//C- ------------------------------------------------------------------
//C- | DjVu (r) Reference Library (v. 3.5)
//C- | Copyright (c) 1999-2001 LizardTech, Inc. All Rights Reserved.
//C- | The DjVu Reference Library is protected by U.S. Pat. No.
//C- | 6,058,214 and patents pending.
//C- |
//C- | This software is subject to, and may be distributed under, the
//C- | GNU General Public License, Version 2. The license should have
//C- | accompanied the software or you may obtain a copy of the license
//C- | from the Free Software Foundation at http://www.fsf.org .
//C- |
//C- | The computer code originally released by LizardTech under this
//C- | license and unmodified by other parties is deemed "the LIZARDTECH
//C- | ORIGINAL CODE." Subject to any third party intellectual property
//C- | claims, LizardTech grants recipient a worldwide, royalty-free,
//C- | non-exclusive license to make, use, sell, or otherwise dispose of
//C- | the LIZARDTECH ORIGINAL CODE or of programs derived from the
//C- | LIZARDTECH ORIGINAL CODE in compliance with the terms of the GNU
//C- | General Public License. This grant only confers the right to
//C- | infringe patent claims underlying the LIZARDTECH ORIGINAL CODE to
//C- | the extent such infringement is reasonably necessary to enable
//C- | recipient to make, have made, practice, sell, or otherwise dispose
//C- | of the LIZARDTECH ORIGINAL CODE (or portions thereof) and not to
//C- | any greater extent that may be necessary to utilize further
//C- | modifications or combinations.
//C- |
//C- | The LIZARDTECH ORIGINAL CODE is provided "AS IS" WITHOUT WARRANTY
//C- | OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
//C- | TO ANY WARRANTY OF NON-INFRINGEMENT, OR ANY IMPLIED WARRANTY OF
//C- | MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//C- +------------------------------------------------------------------
//
// $Id: DjVuToPS.cpp,v 1.23 2003/11/07 22:08:21 leonb Exp $
// $Name: release_3_5_15 $
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#if NEED_GNUG_PRAGMAS
# pragma implementation
#endif
#include "DjVuToPS.h"
#include "IFFByteStream.h"
#include "BSByteStream.h"
#include "DjVuImage.h"
#include "DjVuText.h"
#include "DataPool.h"
#include "IW44Image.h"
#include "JB2Image.h"
#include "GBitmap.h"
#include "GPixmap.h"
#include "debug.h"
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#ifdef UNIX
#include <pwd.h>
#include <grp.h>
#include <unistd.h>
#endif
#ifdef HAVE_NAMESPACES
namespace DJVU {
# ifdef NOT_DEFINED // Just to fool emacs c++ mode
}
#endif
#endif
static const size_t ps_string_size=15000;
// ***************************************************************************
// ****************************** Options ************************************
// ***************************************************************************
DjVuToPS::Options::
Options(void)
: format(PS),
level(2),
orientation(AUTO),
mode(COLOR),
zoom(0),
color(true),
calibrate(true),
text(false),
gamma((double)2.2),
copies(1),
frame(false),
cropmarks(false),
bookletmode(OFF),
bookletmax(0),
bookletalign(0),
bookletfold(18),
bookletxfold(200)
{}
void
DjVuToPS::Options::
set_format(Format xformat)
{
if (xformat != EPS && xformat != PS)
G_THROW(ERR_MSG("DjVuToPS.bad_format"));
format=xformat;
}
void
DjVuToPS::Options::
set_level(int xlevel)
{
if (xlevel<1 || xlevel>3)
G_THROW(ERR_MSG("DjVuToPS.bad_level")
+ GUTF8String("\t") + GUTF8String(xlevel));
level=xlevel;
}
void
DjVuToPS::Options::
set_orientation(Orientation xorientation)
{
if (xorientation!=PORTRAIT &&
xorientation!=LANDSCAPE &&
xorientation!=AUTO )
G_THROW(ERR_MSG("DjVuToPS.bad_orient"));
orientation=xorientation;
}
void
DjVuToPS::Options::
set_mode(Mode xmode)
{
if (xmode!=COLOR && xmode!=FORE && xmode!=BACK && xmode!=BW)
G_THROW(ERR_MSG("DjVuToPS.bad_mode"));
mode=xmode;
}
void
DjVuToPS::Options::
set_zoom(int xzoom)
{
if (xzoom!=0 && !(xzoom>=5 && xzoom<=999))
G_THROW(ERR_MSG("DjVuToPS.bad_zoom"));
zoom=xzoom;
}
void
DjVuToPS::Options::
set_color(bool xcolor)
{
color=xcolor;
}
void
DjVuToPS::Options::
set_sRGB(bool xcalibrate)
{
calibrate=xcalibrate;
}
void
DjVuToPS::Options::
set_gamma(double xgamma)
{
if (xgamma<(double)(0.3-0.0001) || xgamma>(double)(5.0+0.0001))
G_THROW(ERR_MSG("DjVuToPS.bad_gamma"));
gamma=xgamma;
}
void
DjVuToPS::Options::
set_copies(int xcopies)
{
if (xcopies<=0)
G_THROW(ERR_MSG("DjVuToPS.bad_number"));
copies=xcopies;
}
void
DjVuToPS::Options::
set_frame(bool xframe)
{
frame=xframe;
}
void
DjVuToPS::Options::
set_cropmarks(bool xmarks)
{
cropmarks=xmarks;
}
void
DjVuToPS::Options::
set_text(bool xtext)
{
text=xtext;
}
void
DjVuToPS::Options::
set_bookletmode(BookletMode m)
{
bookletmode = m;
}
void
DjVuToPS::Options::
set_bookletmax(int m)
{
bookletmax = 0;
if (m > 0)
bookletmax = (m+3)/4;
bookletmax *= 4;
}
void
DjVuToPS::Options::
set_bookletalign(int m)
{
bookletalign = m;
}
void
DjVuToPS::Options::
set_bookletfold(int fold, int xfold)
{
if (fold >= 0)
bookletfold = fold;
if (xfold >= 0)
bookletxfold = xfold;
}
// ***************************************************************************
// ******************************* DjVuToPS **********************************
// ***************************************************************************
static char bin2hex[256][2];
DjVuToPS::DjVuToPS(void)
{
DEBUG_MSG("DjVuToPS::DjVuToPS(): initializing...\n");
DEBUG_MAKE_INDENT(3);
DEBUG_MSG("Initializing dig2hex[]\n");
// Creating tables for bin=>text translation
static char * dig2hex="0123456789ABCDEF";
int i;
for(i=0;i<256;i++)
{
bin2hex[i][0]=dig2hex[i/16];
bin2hex[i][1]=dig2hex[i%16];
}
refresh_cb=0;
refresh_cl_data=0;
prn_progress_cb=0;
prn_progress_cl_data=0;
dec_progress_cb=0;
dec_progress_cl_data=0;
info_cb=0;
info_cl_data=0;
}
#ifdef __GNUC__
static void
write(ByteStream &str, const char *format, ...)
__attribute__((format (printf, 2, 3)));
#endif
static void
write(ByteStream &str, const char *format, ...)
{
/* Will output the formated string to the specified \Ref{ByteStream}
like #fprintf# would do it for a #FILE#. */
va_list args;
va_start(args, format);
GUTF8String tmp;
tmp.vformat(format, args);
str.writall((const char *) tmp, tmp.length());
}
// ************************* DOCUMENT LEVEL *********************************
void
DjVuToPS::
store_doc_prolog(ByteStream &str, int pages, int dpi, GRect *grect)
{
/* Will store the {\em document prolog}, which is basically a
block of document-level comments in PS DSC 3.0 format.
@param str Stream where PostScript data should be written
@param pages Total number of pages
@param dpi (EPS mode only)
@param grect (EPS mode only) */
DEBUG_MSG("storing the document prolog\n");
DEBUG_MAKE_INDENT(3);
if (options.get_format()==Options::EPS)
write(str,
"%%!PS-Adobe-3.0 EPSF 3.0\n"
"%%%%BoundingBox: 0 0 %d %d\n",
(grect->width()*100+dpi-1)/dpi,
(grect->height()*100+dpi-1)/dpi );
else
write(str, "%%!PS-Adobe-3.0\n");
write(str,
"%%%%Title: DjVu PostScript document\n"
"%%%%Copyright: Copyright (c) 1998-1999 AT&T\n"
"%%%%Creator: DjVu (code by Andrei Erofeev)\n"
"%%%%DocumentData: Clean7Bit\n");
// Date
time_t tm=time(0);
write(str, "%%%%CreationDate: %s", ctime(&tm));
// For
#ifdef UNIX
passwd *pswd = getpwuid(getuid());
if (pswd)
{
char *s = strchr(pswd->pw_gecos, ',');
if (s)
*s = 0;
s = 0;
if (pswd->pw_gecos && strlen(pswd->pw_gecos))
s = pswd->pw_gecos;
else if (pswd->pw_name && strlen(pswd->pw_name))
s = pswd->pw_name;
if (s)
write(str, "%%%%For: %s\n", s);
}
#endif
// Language
write(str, "%%%%LanguageLevel: %d\n", options.get_level());
if (options.get_level()<2 && options.get_color())
write(str, "%%%%Extensions: CMYK\n");
// Pages
write(str, "%%%%Pages: %d\n",pages );
write(str, "%%%%PageOrder: Ascend\n");
// Orientation
if (options.get_orientation() != Options::AUTO)
write(str, "%%%%Orientation: %s\n",
options.get_orientation()==Options::PORTRAIT ?
"Portrait" : "Landscape" );
// Requirements
if (options.get_format() == Options::PS)
{
write(str, "%%%%Requirements:");
if (options.get_color())
write(str, " color");
if (options.get_copies()>1)
write(str, " numcopies(%d)", options.get_copies());
if (options.get_level()>=2)
{
if (options.get_copies()>1)
write(str, " collate");
if (options.get_bookletmode() == Options::RECTOVERSO)
write(str, " duplex(tumble)");
}
write(str, "\n");
}
// End
write(str,
"%%%%EndComments\n"
"%%%%EndProlog\n"
"\n");
}
void
DjVuToPS::
store_doc_setup(ByteStream &str)
{
/* Will store the {\em document setup}, which is a set of
PostScript commands and functions used to inspect and prepare
the PostScript interpreter environment before displaying images. */
write(str,
"%%%%BeginSetup\n"
"/doc-origstate save def\n");
if (options.get_level()>=2)
{
if (options.get_format() == Options::PS)
{
if (options.get_copies()>1)
write(str,
"[{\n"
"%%%%BeginFeature: NumCopies %d\n"
"<< /NumCopies %d >> setpagedevice\n"
"%%%%EndFeature\n"
"} stopped cleartomark\n"
"[{\n"
"%%%%BeginFeature: Collate\n"
"<< /Collate true >> setpagedevice\n"
"%%%%EndFeature\n"
"} stopped cleartomark\n",
options.get_copies(),
options.get_copies() );
if (options.get_bookletmode()==Options::RECTOVERSO)
write(str,
"[{\n"
"%%%%BeginFeature: Duplex DuplexTumble\n"
"<< /Duplex true /Tumble true >> setpagedevice\n"
"%%%%EndFeature\n"
"} stopped cleartomark\n");
}
if (options.get_color())
write(str,
"%% -- procs for reading color image\n"
"/readR () def\n"
"/readG () def\n"
"/readB () def\n"
"/ReadData {\n"
" currentfile /ASCII85Decode filter dup\n"
" /RunLengthDecode filter\n"
" bufferR readstring pop /readR exch def\n"
" dup status { flushfile } { pop } ifelse\n"
" currentfile /ASCII85Decode filter dup\n"
" /RunLengthDecode filter\n"
" bufferG readstring pop /readG exch def\n"
" dup status { flushfile } { pop } ifelse\n"
" currentfile /ASCII85Decode filter dup\n"
" /RunLengthDecode filter\n"
" bufferB readstring pop /readB exch def\n"
" dup status { flushfile } { pop } ifelse\n"
"} bind def\n"
"/ReadR {\n"
" readR length 0 eq { ReadData } if\n"
" readR /readR () def\n"
"} bind def\n"
"/ReadG {\n"
" readG length 0 eq { ReadData } if\n"
" readG /readG () def\n"
"} bind def\n"
"/ReadB {\n"
" readB length 0 eq { ReadData } if\n"
" readB /readB () def\n"
"} bind def\n");
write(str,
"%% -- procs for foreground layer\n"
"/g {gsave 0 0 0 0 5 index 5 index setcachedevice\n"
" true [1 0 0 1 0 0] 5 4 roll imagemask grestore\n"
"} bind def\n"
"/gn {gsave 0 0 0 0 6 index 6 index setcachedevice\n"
" true [1 0 0 1 0 0] 3 2 roll 5 1 roll \n"
" { 1 sub 0 index 2 add 1 index 1 add roll\n"
" } imagemask grestore pop \n"
"} bind def\n"
"/c {setcolor rmoveto glyphshow} bind def\n"
"/s {rmoveto glyphshow} bind def\n"
"/S {rmoveto gsave show grestore} bind def\n"
"/F {(Helvetica) findfont exch scalefont setfont} bind def\n"
"%% -- emulations\n"
"systemdict /rectstroke known not {\n"
" /rectstroke %% stack : x y width height \n"
" { newpath 4 2 roll moveto 1 index 0 rlineto\n"
" 0 exch rlineto neg 0 rlineto closepath stroke\n"
" } bind def } if\n"
"systemdict /rectclip known not {\n"
" /rectclip %% stack : x y width height \n"
" { newpath 4 2 roll moveto 1 index 0 rlineto\n"
" 0 exch rlineto neg 0 rlineto closepath clip\n"
" } bind def } if\n"
"%% -- color space\n" );
if (options.get_sRGB())
write(str,
"/DjVuColorSpace [ %s\n"
"<< /DecodeLMN [ { dup 0.03928 le {\n"
" 12.92321 div\n"
" } {\n"
" 0.055 add 1.055 div 2.4 exp\n"
" } ifelse } bind dup dup ]\n"
" /MatrixLMN [\n"
" 0.412457 0.212673 0.019334\n"
" 0.357576 0.715152 0.119192\n"
" 0.180437 0.072175 0.950301 ]\n"
" /WhitePoint [ 0.9505 1 1.0890 ] %% D65 \n"
" /BlackPoint[0 0 0] >> ] def\n",
(options.get_color()) ? "/CIEBasedABC" : "/CIEBasedA" );
else if (options.get_color())
write(str,"/DjVuColorSpace /DeviceRGB def\n");
else
write(str,"/DjVuColorSpace /DeviceGray def\n");
}
else
{
// level<2
if (options.get_format() == Options::PS)
if (options.get_copies() > 1)
write(str,"/#copies %d def\n", options.get_copies());
if (options.get_color())
write(str,
"%% -- buffers for reading image\n"
"/buffer8 () def\n"
"/buffer24 () def\n"
"%% -- colorimage emulation\n"
"systemdict /colorimage known {\n"
" /ColorProc {\n"
" currentfile buffer24 readhexstring pop\n"
" } bind def\n"
" /ColorImage {\n"
" colorimage\n"
" } bind def\n"
"} {\n"
" /ColorProc {\n"
" currentfile buffer24 readhexstring pop\n"
" /data exch def /datalen data length def\n"
" /cnt 0 def\n"
" 0 1 datalen 3 idiv 1 sub {\n"
" buffer8 exch\n"
" data cnt get 20 mul /cnt cnt 1 add def\n"
" data cnt get 32 mul /cnt cnt 1 add def\n"
" data cnt get 12 mul /cnt cnt 1 add def\n"
" add add 64 idiv put\n"
" } for\n"
" buffer8 0 datalen 3 idiv getinterval\n"
" } bind def\n"
" /ColorImage {\n"
" pop pop image\n"
" } bind def\n"
"} ifelse\n");
} // level<2
write(str, "%%%%EndSetup\n\n");
}
void
DjVuToPS::
store_doc_trailer(ByteStream &str)
{
/* Will store the {\em document trailer}, which is a clean-up code
used to return the PostScript interpeter back to the state, in which
it was before displaying this document. */
write(str,
"%%%%Trailer\n"
"doc-origstate restore\n"
"%%%%EOF\n");
}
// ***********************************************************************
// ***************************** PAGE LEVEL ******************************
// ***********************************************************************
static unsigned char *
ASCII85_encode(unsigned char * dst,
const unsigned char * src_start,
const unsigned char * src_end)
{
/* Will read data between #src_start# and #src_end# pointers (excluding byte
pointed by #src_end#), encode it using {\bf ASCII85} algorithm, and
output the result into the destination buffer pointed by #dst#. The
function returns pointer to the first unused byte in the destination
buffer. */
int symbols=0;
const unsigned char * ptr;
for(ptr=src_start;ptr<src_end;ptr+=4)
{
unsigned int num=0;
if (ptr+3<src_end)
{
num |= ptr[0] << 24;
num |= ptr[1] << 16;
num |= ptr[2] << 8;
num |= ptr[3];
}
else
{
num |= ptr[0] << 24;
if (ptr+1<src_end)
num |= ptr[1] << 16;
if (ptr+2<src_end)
num |= ptr[2] << 8;
}
int a1, a2, a3, a4, a5;
a5=num % 85; num/=85;
a4=num % 85; num/=85;
a3=num % 85; num/=85;
a2=num % 85;
a1=num / 85;
*dst++ = a1+33;
*dst++ = a2+33;
if (ptr+1<src_end)
*dst++ = a3+33;
if (ptr+2<src_end)
*dst++ = a4+33;
if (ptr+3<src_end)
*dst++ = a5+33;
symbols += 5;
if (symbols > 70 && ptr+4<src_end)
{
*dst++='\n';
symbols=0;
}
}
return dst;
}
static unsigned char *
RLE_encode(unsigned char * dst,
const unsigned char * src_start,
const unsigned char * src_end)
{
/* Will read data between #src_start# and #src_end# pointers (excluding byte
pointed by #src_end#), RLE encode it, and output the result into the
destination buffer pointed by #dst#. #counter# is used to count the
number of output bytes. The function returns pointer to the first unused
byte in the destination buffer. */
const unsigned char * ptr;
for(ptr=src_start;ptr<src_end;ptr++)
{
if (ptr==src_end-1)
{
*dst++=0; *dst++=*ptr;
}
else if (ptr[0]!=ptr[1])
{
// Guess how many non repeating bytes we have
const unsigned char * ptr1;
for(ptr1=ptr+1;ptr1<src_end-1;ptr1++)
if (ptr1[0]==ptr1[1] || ptr1-ptr>=128) break;
int pixels=ptr1-ptr;
*dst++=pixels-1;
for(int cnt=0;cnt<pixels;cnt++)
*dst++=*ptr++;
ptr--;
}
else
{
// Get the number of repeating bytes
const unsigned char * ptr1;
for(ptr1=ptr+1;ptr1<src_end-1;ptr1++)
if (ptr1[0]!=ptr1[1] || ptr1-ptr+1>=128) break;
int pixels=ptr1-ptr+1;
*dst++=257-pixels;
*dst++=*ptr;
ptr=ptr1;
}
}
return dst;
}
#define GRAY(r,g,b) (((r)*20+(g)*32+(b)*12)/64)
void
DjVuToPS::
store_page_setup(ByteStream &str,
int dpi,
const GRect &grect,
int align )
{
/* Will store PostScript code necessary to prepare page for
the coming \Ref{DjVuImage}. This is basically a scaling
code plus initialization of some buffers. */
if (options.get_format() == Options::EPS)
write(str,
"/page-origstate save def\n"
"%% -- coordinate system\n"
"/image-dpi %d def\n"
"/image-x 0 def\n"
"/image-y 0 def\n"
"/image-width %d def\n"
"/image-height %d def\n"
"/coeff 100 image-dpi div def\n"
"/a11 coeff def\n"
"/a12 0 def\n"
"/a13 0 def\n"
"/a21 0 def\n"
"/a22 coeff def\n"
"/a23 0 def\n"
"[a11 a21 a12 a22 a13 a23] concat\n"
"gsave 0 0 image-width image-height rectclip\n"
"%% -- begin printing\n",
dpi, grect.width(), grect.height() );
else
{
int margin = 0;
const char *xauto = "false";
const char *xportrait = "false";
const char *xfit = "false";
if (options.get_orientation()==Options::AUTO)
xauto = "true";
if (options.get_orientation()==Options::PORTRAIT)
xportrait = "true";
if (options.get_zoom()<=0)
xfit = "true";
if (options.get_cropmarks())
margin = 36;
else if (options.get_frame())
margin = 6;
write(str,
"/page-origstate save def\n"
"%% -- coordinate system\n"
"/auto-orient %s def\n"
"/portrait %s def\n"
"/fit-page %s def\n"
"/zoom %d def\n"
"/image-dpi %d def\n"
"clippath pathbbox newpath\n"
"2 index sub exch 3 index sub\n"
"/page-width exch def\n"
"/page-height exch def\n"
"/page-y exch def\n"
"/page-x exch def\n"
"/image-x 0 def\n"
"/image-y 0 def\n"
"/image-width %d def\n"
"/image-height %d def\n"
"/margin %d def\n"
"/halign %d def\n"
"/valign 0 def\n",
xauto, xportrait, xfit, options.get_zoom(),
dpi, grect.width(), grect.height(),
margin, align );
write(str,
"%% -- position page\n"
"auto-orient {\n"
" image-height image-width sub\n"
" page-height page-width sub\n"
" mul 0 ge /portrait exch def\n"
"} if\n"
"fit-page {\n"
" /page-width page-width margin sub\n"
" halign 0 eq { margin sub } if def\n"
" /page-height page-height margin sub\n"
" valign 0 eq { margin sub } if def\n"
" /page-x page-x halign 0 ge { margin add } if def\n"
" /page-y page-y valign 0 ge { margin add } if def\n"
"} if\n"
"portrait {\n"
" fit-page {\n"
" image-height page-height div\n"
" image-width page-width div\n"
" gt {\n"
" page-height image-height div /coeff exch def\n"
" } {\n"
" page-width image-width div /coeff exch def\n"
" } ifelse\n"
" } {\n"
" /coeff 72 image-dpi div zoom mul 100 div def\n"
" } ifelse\n"
" /start-x page-x page-width image-width\n"
" coeff mul sub 2 div halign 1 add mul add def\n"
" /start-y page-y page-height image-height\n"
" coeff mul sub 2 div valign 1 add mul add def\n"
" /a11 coeff def\n"
" /a12 0 def\n"
" /a13 start-x def\n"
" /a21 0 def\n"
" /a22 coeff def\n"
" /a23 start-y def\n"
"} { %% landscape\n"
" fit-page {\n"
" image-height page-width div\n"
" image-width page-height div\n"
" gt {\n"
" page-width image-height div /coeff exch def\n"
" } {\n"
" page-height image-width div /coeff exch def\n"
" } ifelse\n"
" } {\n"
" /coeff 72 image-dpi div zoom mul 100 div def\n"
" } ifelse\n"
" /start-x page-x page-width add page-width image-height\n"
" coeff mul sub 2 div valign 1 add mul sub def\n"
" /start-y page-y page-height image-width\n"
" coeff mul sub 2 div halign 1 add mul add def\n"
" /a11 0 def\n"
" /a12 coeff neg def\n"
" /a13 start-x image-y coeff neg mul sub def\n"
" /a21 coeff def\n"
" /a22 0 def\n"
" /a23 start-y image-x coeff mul add def \n"
"} ifelse\n"
"[a11 a21 a12 a22 a13 a23] concat\n"
"gsave 0 0 image-width image-height rectclip\n"
"%% -- begin print\n");
}
}
void
DjVuToPS::
store_page_trailer(ByteStream &str)
{
write(str,
"%% -- end print\n"
"grestore\n");
if (options.get_frame())
write(str,
"%% Drawing frame\n"
"gsave 0.7 setgray 0.5 coeff div setlinewidth 0 0\n"
"image-width image-height rectstroke\n"
"grestore\n");
if (options.get_cropmarks() &&
options.get_format() != Options::EPS )
write(str,
"%% Drawing crop marks\n"
"/cm { gsave translate rotate 1 coeff div dup scale\n"
" 0 setgray 0.5 setlinewidth -36 0 moveto 0 0 lineto\n"
" 0 -36 lineto stroke grestore } bind def\n"
"0 0 0 cm 180 image-width image-height cm\n"
"90 image-width 0 cm 270 0 image-height cm\n");
write(str,
"page-origstate restore\n");
}
static int
compute_red(int w, int h, int rw, int rh)
{
for (int red=1; red<16; red++)
if (((w+red-1)/red==rw) && ((h+red-1)/red==rh))
return red;
return 16;
}
static int
get_bg_red(GP<DjVuImage> dimg)
{
GP<GPixmap> pm = 0;
// Access image size
int width = dimg->get_width();
int height = dimg->get_height();
if (width<=0 || height<=0) return 0;
// CASE1: Incremental BG IW44Image
GP<IW44Image> bg44 = dimg->get_bg44();
if (bg44)
{
int w = bg44->get_width();
int h = bg44->get_height();
// Avoid silly cases
if (w==0 || h==0 || width==0 || height==0)
return 0;
return compute_red(width,height,w,h);
}
// CASE 2: Raw background pixmap
GP<GPixmap> bgpm = dimg->get_bgpm();
if (bgpm)
{
int w = bgpm->columns();
int h = bgpm->rows();
// Avoid silly cases
if (w==0 || h==0 || width==0 || height==0)
return 0;
return compute_red(width,height,w,h);
}
return 0;
}
static GP<GPixmap>
get_bg_pixmap(GP<DjVuImage> dimg, const GRect &rect)
{
GP<GPixmap> pm = 0;
// Access image size
int width = dimg->get_width();
int height = dimg->get_height();
GP<DjVuInfo> info = dimg->get_info();
if (width<=0 || height<=0 || !info) return 0;
// CASE1: Incremental BG IW44Image
GP<IW44Image> bg44 = dimg->get_bg44();
if (bg44)
{
int w = bg44->get_width();
int h = bg44->get_height();
// Avoid silly cases
if (w==0 || h==0 || width==0 || height==0)
return 0;
pm = bg44->get_pixmap(1,rect);
return pm;
}
// CASE 2: Raw background pixmap
GP<GPixmap> bgpm = dimg->get_bgpm();
if (bgpm)
{
int w = bgpm->columns();
int h = bgpm->rows();
// Avoid silly cases
if (w==0 || h==0 || width==0 || height==0)
return 0;
pm->init(*bgpm, rect);
return pm;
}
// FAILURE
return 0;
}
void
DjVuToPS::
make_gamma_ramp(GP<DjVuImage> dimg)
{
double targetgamma = options.get_gamma();
double whitepoint = (options.get_sRGB() ? 255 : 280);
for (int i=0; i<256; i++)
ramp[i] = i;
if (! dimg->get_info())
return;
if (targetgamma < 0.1)
return;
double filegamma = dimg->get_info()->gamma;
double correction = filegamma / targetgamma;
if (correction<0.1 || correction>10)
return;
{
for (int i=0; i<256; i++)
{
double x = (double)(i)/255.0;
if (correction != 1.0)
x = pow(x, correction);
int j = (int) floor(whitepoint * x + 0.5);
ramp[i] = (j>255) ? 255 : (j<0) ? 0 : j;
}
}
}
void
DjVuToPS::
print_fg_2layer(ByteStream &str,
GP<DjVuImage> dimg,
const GRect &prn_rect,
unsigned char *blit_list)
{
// Pure-jb2 or color-jb2 case.
GPixel p;
int currentx=0;
int currenty=0;
GP<DjVuPalette> pal = dimg->get_fgbc();
GP<JB2Image> jb2 = dimg->get_fgjb();
if (! jb2) return;
int num_blits = jb2->get_blit_count();
int current_blit;
for(current_blit=0; current_blit<num_blits; current_blit++)
{
if (blit_list[current_blit])
{
JB2Blit *blit = jb2->get_blit(current_blit);
if ((pal) && !(options.get_mode()==Options::BW))
{
pal->index_to_color(pal->colordata[current_blit], p);
if (options.get_color())
{
write(str,"/%d %d %d %f %f %f c\n",
blit->shapeno,
blit->left-currentx, blit->bottom-currenty,
ramp[p.r]/255.0, ramp[p.g]/255.0, ramp[p.b]/255.0);
}
else
{
write(str,"/%d %d %d %f c\n",
blit->shapeno,
blit->left-currentx, blit->bottom-currenty,
ramp[GRAY(p.r, p.g, p.b)]/255.0);
}
}
else
{
write(str,"/%d %d %d s\n",
blit->shapeno,
blit->left-currentx, blit->bottom-currenty);
}
currentx = blit->left;
currenty = blit->bottom;
}
}
}
void
DjVuToPS::
print_fg_3layer(ByteStream &str,
GP<DjVuImage> dimg,
const GRect &cprn_rect,
unsigned char *blit_list )
{
GRect prn_rect;
GP<GPixmap> brush = dimg->get_fgpm();
if (! brush) return;
int br = brush->rows();
int bc = brush->columns();
int red = compute_red(dimg->get_width(),dimg->get_height(),bc,br);
prn_rect.ymin = (cprn_rect.ymin)/red;
prn_rect.xmin = (cprn_rect.xmin)/red;
prn_rect.ymax = (cprn_rect.ymax+red-1)/red;
prn_rect.xmax = (cprn_rect.xmax+red-1)/red;
int color_nb = ((options.get_color()) ? 3 : 1);
GP<JB2Image> jb2 = dimg->get_fgjb();
if (! jb2) return;
int pw = bc;
int ph = 2;
write(str,
"/P {\n"
" 11 dict dup begin 4 1 roll\n"
" /PatternType 1 def\n"
" /PaintType 1 def\n"
" /TilingType 1 def\n"
" /H exch def\n"
" /W exch def\n"
" /Red %d def\n"
" /PatternString exch def\n"
" /XStep W Red mul def\n"
" /YStep H Red mul def\n"
" /BBox [0 0 XStep YStep] def\n"
" /PaintProc { begin\n"
" Red dup scale\n"
" << /ImageType 1 /Width W /Height H\n"
" /BitsPerComponent 8 /Interpolate false\n"
" /Decode [%s] /ImageMatrix [1 0 0 1 0 0]\n"
" /DataSource PatternString >> image\n"
" end } bind def\n"
" 0 0 XStep YStep rectclip\n"
" end matrix makepattern\n"
" /Pattern setcolorspace setpattern\n"
" 0 0 moveto\n"
"} def\n", red, (color_nb == 1) ? "0 1" : "0 1 0 1 0 1" );
unsigned char *s;
GPBuffer<unsigned char> gs(s,pw*ph*color_nb);
unsigned char *s_ascii_encoded;
GPBuffer<unsigned char> gs_ascii_encoded(s_ascii_encoded,pw*ph*2*color_nb);
{
for (int y=prn_rect.ymin; y<prn_rect.ymax; y+=ph)
for (int x=prn_rect.xmin; x<prn_rect.xmax; x+=pw)
{
int w = ((x+pw > prn_rect.xmax) ? prn_rect.xmax-x : pw);
int h = ((y+ph > prn_rect.ymax) ? prn_rect.ymax-y : ph);
int currentx = x * red;
int currenty = y * red;
// Find first intersecting blit
int current_blit;
int num_blits = jb2->get_blit_count();
GRect rect1(currentx,currenty, w*red, h*red);
for(current_blit=0; current_blit<num_blits; current_blit++)
if (blit_list[current_blit])
{
JB2Blit *blit = jb2->get_blit(current_blit);
GRect rect2(blit->left, blit->bottom,
jb2->get_shape(blit->shapeno).bits->columns(),
jb2->get_shape(blit->shapeno).bits->rows());
if (rect2.intersect(rect1,rect2))
break;
}
if (current_blit >= num_blits)
continue;
// Setup pattern
write(str,"gsave %d %d translate\n", currentx, currenty);
write(str,"<~");
unsigned char *q = s;
for(int current_row = y; current_row<y+h; current_row++)
{
GPixel *row_pix = (*brush)[current_row];
for(int current_col = x; current_col<x+w; current_col++)
{
GPixel &p = row_pix[current_col];
if (color_nb>1)
{
*q++ = ramp[p.r];
*q++ = ramp[p.g];
*q++ = ramp[p.b];
}
else
{
*q++ = ramp[GRAY(p.r,p.g,p.b)];
}
}
}
unsigned char *stop_ascii =
ASCII85_encode(s_ascii_encoded,s,s+w*h*color_nb);
*stop_ascii++='\0';
write(str,"%s",s_ascii_encoded);
write(str,"~> %d %d P\n", w, h);
// Keep performing blits
for(; current_blit<num_blits; current_blit++)
if (blit_list[current_blit])
{
JB2Blit *blit = jb2->get_blit(current_blit);
GRect rect2(blit->left, blit->bottom,
jb2->get_shape(blit->shapeno).bits->columns(),
jb2->get_shape(blit->shapeno).bits->rows());
if (rect2.intersect(rect1,rect2))
{
write(str,"/%d %d %d s\n",
blit->shapeno,
blit->left-currentx, blit->bottom-currenty);
currentx = blit->left;
currenty = blit->bottom;
}
}
write(str,"grestore\n");
}
// Cleanup
}
}
void
DjVuToPS::
print_fg(ByteStream &str,
GP<DjVuImage> dimg,
const GRect &prn_rect )
{
GP<JB2Image> jb2=dimg->get_fgjb();
if (! jb2) return;
int num_blits = jb2->get_blit_count();
int num_shapes = jb2->get_shape_count();
unsigned char *dict_shapes = 0;
unsigned char *blit_list = 0;
GPBuffer<unsigned char> gdict_shapes(dict_shapes,num_shapes);
GPBuffer<unsigned char> gblit_list(blit_list,num_blits);
for(int i=0; i<num_shapes; i++)
{
dict_shapes[i]=0;
}
for(int current_blit=0; current_blit<num_blits; current_blit++)
{
JB2Blit *blit = jb2->get_blit(current_blit);
JB2Shape *shape = & jb2->get_shape(blit->shapeno);
blit_list[current_blit] = 0;
if (! shape->bits)
continue;
GRect rect2(blit->left, blit->bottom,
shape->bits->columns(), shape->bits->rows());
if (rect2.intersect(rect2, prn_rect))
{
dict_shapes[blit->shapeno] = 1;
blit_list[current_blit] = 1;
}
}
write(str,
"%% --- now doing the foreground\n"
"gsave DjVuColorSpace setcolorspace\n" );
// Define font
write(str,
"/$DjVuLocalFont 7 dict def\n"
"$DjVuLocalFont begin\n"
"/FontType 3 def \n"
"/FontMatrix [1 0 0 1 0 0] def\n"
"/FontBBox [0 0 1 .5] def\n"
"/CharStrings %d dict def\n"
"/Encoding 2 array def\n"
"0 1 1 {Encoding exch /.notdef put} for \n"
"CharStrings begin\n"
"/.notdef {} def\n",
num_shapes+1);
for(int current_shape=0; current_shape<num_shapes; current_shape++)
{
if (dict_shapes[current_shape])
{
JB2Shape *shape = & jb2->get_shape(current_shape);
GP<GBitmap> bitmap = shape->bits;
int rows = bitmap->rows();
int columns = bitmap->columns();
int nbytes = (columns+7)/8*rows+1;
int nrows = rows;
int nstrings=0;
if (nbytes>(int)ps_string_size) //max string length
{
nrows=ps_string_size/((columns+7)/8);
nbytes=(columns+7)/8*nrows+1;
}
unsigned char *s_start;
GPBuffer<unsigned char> gs_start(s_start,nbytes);
unsigned char *s_ascii;
GPBuffer<unsigned char> gs_ascii(s_ascii,nbytes*2);
write(str,"/%d {",current_shape);
unsigned char *s = s_start;
for(int current_row=0; current_row<rows; current_row++)
{
unsigned char * row_bits = (*bitmap)[current_row];
unsigned char acc = 0;
unsigned char mask = 0;
for(int current_col=0; current_col<columns; current_col++)
{
if (mask == 0)
mask = 0x80;
if (row_bits[current_col])
acc |= mask;
mask >>= 1;
if (mask == 0)
{
*s=acc;
s++;
acc = mask = 0;
}
}
if (mask != 0)
{
*s=acc;
s++;
}
if (!((current_row+1)%nrows))
{
unsigned char *stop_ascii = ASCII85_encode(s_ascii,s_start,s);
*stop_ascii++='\0';
write(str,"<~%s~> ",s_ascii);
s=s_start;
nstrings++;
}
}
if (s!=s_start)
{
unsigned char *stop_ascii = ASCII85_encode(s_ascii,s_start,s);
*stop_ascii++='\0';
write(str,"<~%s~> ",s_ascii);
nstrings++;
}
if (nstrings==1)
write(str," %d %d g} def\n", columns, rows);
else
write(str," %d %d %d gn} def\n", columns, rows,nstrings);
}
}
write(str,
"end\n"
"/BuildGlyph {\n"
" exch /CharStrings get exch\n"
" 2 copy known not\n"
" {pop /.notdef} if\n"
" get exec \n"
"} bind def\n"
"end\n"
"/LocalDjVuFont $DjVuLocalFont definefont pop\n"
"/LocalDjVuFont findfont setfont\n" );
write(str,
"-%d -%d translate\n"
"0 0 moveto\n",
prn_rect.xmin, prn_rect.ymin);
// Print the foreground layer
if (dimg->get_fgpm() && !(options.get_mode()==Options::BW))
print_fg_3layer(str, dimg, prn_rect, blit_list);
else
print_fg_2layer(str, dimg, prn_rect, blit_list);
write(str, "/LocalDjVuFont undefinefont grestore\n");
}
void
DjVuToPS::
print_bg(ByteStream &str,
GP<DjVuImage> dimg,
const GRect &cprn_rect)
{
GP<GPixmap> pm;
GRect prn_rect;
double print_done = 0;
int red = 0;
write(str, "%% --- now doing the background\n");
if (! (red = get_bg_red(dimg)))
return;
write(str,
"gsave -%d -%d translate\n"
"/bgred %d def bgred bgred scale\n",
cprn_rect.xmin % red,
cprn_rect.ymin % red,
red);
prn_rect.ymin = (cprn_rect.ymin)/red;
prn_rect.ymax = (cprn_rect.ymax+red-1)/red;
prn_rect.xmin = (cprn_rect.xmin)/red;
prn_rect.xmax = (cprn_rect.xmax+red-1)/red;
// Display image
int band_bytes = 125000;
int band_height = band_bytes/prn_rect.width();
int buffer_size = band_height*prn_rect.width();
int ps_chunk_height = 30960/prn_rect.width()+1;
buffer_size = buffer_size*23/10;
bool do_color = options.get_color();
if (!dimg->is_legal_photo() &&
!dimg->is_legal_compound() ||
options.get_mode()==Options::BW)
do_color = false;
if (do_color)
buffer_size *= 3;
if (do_color)
write(str,
"/bufferR %d string def\n"
"/bufferG %d string def\n"
"/bufferB %d string def\n"
"DjVuColorSpace setcolorspace\n"
"<< /ImageType 1\n"
" /Width %d\n"
" /Height %d\n"
" /BitsPerComponent 8\n"
" /Decode [0 1 0 1 0 1]\n"
" /ImageMatrix [1 0 0 1 0 0]\n"
" /MultipleDataSources true\n"
" /DataSource [ { ReadR } { ReadG } { ReadB } ]\n"
" /Interpolate false >> image\n",
ps_chunk_height*prn_rect.width(),
ps_chunk_height*prn_rect.width(),
ps_chunk_height*prn_rect.width(),
prn_rect.width(), prn_rect.height());
else
write(str,
"DjVuColorSpace setcolorspace\n"
"<< /ImageType 1\n"
" /Width %d\n"
" /Height %d\n"
" /BitsPerComponent 8\n"
" /Decode [0 1]\n"
" /ImageMatrix [1 0 0 1 0 0]\n"
" /DataSource currentfile /ASCII85Decode\n"
" filter /RunLengthDecode filter\n"
" /Interpolate false >> image\n",
prn_rect.width(), prn_rect.height());
unsigned char *buffer;
GPBuffer<unsigned char> gbuffer(buffer,buffer_size);
unsigned char *rle_in;
GPBuffer<unsigned char> grle_in(rle_in,ps_chunk_height*prn_rect.width());
unsigned char *rle_out;
GPBuffer<unsigned char> grle_out(rle_out,2*ps_chunk_height*prn_rect.width());
{
// Start storing image in bands
unsigned char * rle_out_end = rle_out;
GRect grectBand = prn_rect;
grectBand.ymax = grectBand.ymin;
while(grectBand.ymax < prn_rect.ymax)
{
GP<GPixmap> pm = 0;
// Compute next band
grectBand.ymin=grectBand.ymax;
grectBand.ymax=grectBand.ymin+band_bytes/grectBand.width();
if (grectBand.ymax>prn_rect.ymax)
grectBand.ymax=prn_rect.ymax;
pm = get_bg_pixmap(dimg, grectBand);
unsigned char *buf_ptr = buffer;
if (pm)
{
if (do_color)
{
int y=0;
while(y<grectBand.height())
{
int row, y1;
unsigned char *ptr, *ptr1;
// Doing R component of current chunk
for (row=0,ptr=rle_in,y1=y;
row<ps_chunk_height && y1<grectBand.height();
row++,y1++)
{
GPixel *pix = (*pm)[y1];
for (int x=grectBand.width(); x>0; x--,pix++)
*ptr++ = ramp[pix->r];
}
ptr1 = RLE_encode(rle_out, rle_in, ptr);
*ptr1++ = 0x80;
buf_ptr = ASCII85_encode(buf_ptr, rle_out, ptr1);
*buf_ptr++ = '~'; *buf_ptr++ = '>'; *buf_ptr++ = '\n';
// Doing G component of current chunk
for (row=0,ptr=rle_in,y1=y;
row<ps_chunk_height && y1<grectBand.height();
row++,y1++)
{
GPixel *pix = (*pm)[y1];
for (int x=grectBand.width(); x>0; x--,pix++)
*ptr++ = ramp[pix->g];
}
ptr1 = RLE_encode(rle_out, rle_in, ptr);
*ptr1++ = 0x80;
buf_ptr = ASCII85_encode(buf_ptr, rle_out, ptr1);
*buf_ptr++ = '~';
*buf_ptr++ = '>';
*buf_ptr++ = '\n';
// Doing B component of current chunk
for (row=0, ptr=rle_in, y1=y;
row<ps_chunk_height && y1<grectBand.height();
row++,y1++)
{
GPixel *pix = (*pm)[y1];
for (int x=grectBand.width(); x>0; x--,pix++)
*ptr++ = ramp[pix->b];
}
ptr1 = RLE_encode(rle_out, rle_in, ptr);
*ptr1++ = 0x80;
buf_ptr = ASCII85_encode(buf_ptr, rle_out, ptr1);
*buf_ptr++ = '~';
*buf_ptr++ = '>';
*buf_ptr++ = '\n';
y=y1;
if (refresh_cb)
refresh_cb(refresh_cl_data);
} //while (y>=0)
}
else
{
// Don't use color
int y=0;
while(y<grectBand.height())
{
unsigned char *ptr = rle_in;
for(int row=0;
row<ps_chunk_height && y<grectBand.height();
row++,y++)
{
GPixel *pix = (*pm)[y];
for (int x=grectBand.width(); x>0; x--,pix++)
*ptr++ = ramp[GRAY(pix->r,pix->g,pix->b)];
}
rle_out_end = RLE_encode(rle_out_end, rle_in, ptr);
unsigned char *encode_to
= rle_out+(rle_out_end-rle_out)/4*4;
int bytes_left = rle_out_end-encode_to;
buf_ptr = ASCII85_encode(buf_ptr, rle_out, encode_to);
*buf_ptr++ = '\n';
memcpy(rle_out, encode_to, bytes_left);
rle_out_end = rle_out+bytes_left;
if (refresh_cb)
refresh_cb(refresh_cl_data);
}
}
} // if (pm)
str.writall(buffer, buf_ptr-buffer);
if (prn_progress_cb)
{
double done=(double)(grectBand.ymax
- prn_rect.ymin)/prn_rect.height();
if ((int) (20*print_done)!=(int) (20*done))
{
print_done=done;
prn_progress_cb(done, prn_progress_cl_data);
}
}
} // while(grectBand.yax<grect.ymax)
if (! do_color)
{
unsigned char * buf_ptr = buffer;
*rle_out_end++ = 0x80;
buf_ptr = ASCII85_encode(buf_ptr, rle_out, rle_out_end);
*buf_ptr++='~';
*buf_ptr++='>';
*buf_ptr++='\n';
str.writall(buffer, buf_ptr-buffer);
}
}
//restore the scaling
write(str, "grestore\n");
}
void
DjVuToPS::
print_image_lev1(ByteStream &str,
GP<DjVuImage> dimg,
const GRect &prn_rect)
{
double print_done=0;
GRect all(0,0, dimg->get_width(),dimg->get_height());
GP<GPixmap> pm;
GP<GBitmap> bm;
GRect test(0,0,1,1);
if (options.get_mode() == Options::FORE)
pm = dimg->get_fg_pixmap(test, all);
else if (options.get_mode() == Options::BACK)
pm = dimg->get_bg_pixmap(test, all);
else if (options.get_mode() != Options::BW)
pm = dimg->get_pixmap(test, all);
if (! pm)
bm = dimg->get_bitmap(test,all);
if (! pm && ! bm)
return;
write(str,
"%% --- now doing a level 1 image\n"
"gsave\n");
// Display image
int band_bytes=125000;
int band_height = band_bytes/prn_rect.width();
int buffer_size = band_height*prn_rect.width();
buffer_size = buffer_size*21/10;
bool do_color = false;
bool do_color_or_gray = false;
if (pm && (options.get_mode() != Options::BW))
do_color_or_gray = true;
if (do_color_or_gray && options.get_color())
do_color = true;
if (do_color)
buffer_size *= 3;
if (do_color)
write(str, "/buffer24 %d string def\n", 3*prn_rect.width());
if (do_color_or_gray)
write(str, "/buffer8 %d string def\n", prn_rect.width());
else
write(str, "/buffer8 %d string def\n", (prn_rect.width()+7)/8);
if (do_color)
{
write(str,
"%d %d 8 [ 1 0 0 1 0 0 ]\n"
"{ ColorProc } false 3 ColorImage\n",
prn_rect.width(), prn_rect.height());
}
else if (do_color_or_gray)
{
write(str,
"%d %d 8 [ 1 0 0 1 0 0 ]\n"
"{ currentfile buffer8 readhexstring pop } image\n",
prn_rect.width(), prn_rect.height());
}
else
{
write(str,
"%d %d 1 [ 1 0 0 1 0 0 ]\n"
"{ currentfile buffer8 readhexstring pop } image\n",
prn_rect.width(), prn_rect.height());
}
unsigned char * buffer;
GPBuffer<unsigned char> gbuffer(buffer,buffer_size);
{
// Start storing image in bands
GRect grectBand = prn_rect;
grectBand.ymax = grectBand.ymin;
while(grectBand.ymax < prn_rect.ymax)
{
// Compute next band
grectBand.ymin = grectBand.ymax;
grectBand.ymax = grectBand.ymin+band_bytes/grectBand.width();
if (grectBand.ymax > prn_rect.ymax)
grectBand.ymax = prn_rect.ymax;
GRect all(0,0, dimg->get_width(),dimg->get_height());
pm = 0;
bm = 0;
if (do_color_or_gray)
{
if (options.get_mode() == Options::FORE)
pm = dimg->get_fg_pixmap(grectBand, all);
else if (options.get_mode() == Options::BACK)
pm = dimg->get_bg_pixmap(grectBand, all);
else
pm = dimg->get_pixmap(grectBand, all);
}
else
{
bm = dimg->get_bitmap(grectBand, all);
}
// Store next band
unsigned char *buf_ptr = buffer;
int symbols=0;
for (int y=0; y<grectBand.height(); y++)
{
if (pm && do_color_or_gray)
{
GPixel *pix = (*pm)[y];
for (int x=grectBand.width(); x>0; x--, pix++)
{
if (do_color)
{
char *data;
data = bin2hex[ramp[pix->r]];
*buf_ptr++ = data[0];
*buf_ptr++ = data[1];
data = bin2hex[ramp[pix->g]];
*buf_ptr++ = data[0];
*buf_ptr++ = data[1];
data = bin2hex[ramp[pix->b]];
*buf_ptr++ = data[0];
*buf_ptr++ = data[1];
symbols += 6;
}
else
{
char *data;
data = bin2hex[ramp[GRAY(pix->r,pix->g,pix->b)]];
*buf_ptr++ = data[0];
*buf_ptr++ = data[1];
symbols += 2;
}
if (symbols>70)
{
*buf_ptr++ = '\n';
symbols=0;
}
}
}
else if (bm)
{
unsigned char *pix = (*bm)[y];
unsigned char acc = 0;
unsigned char mask = 0;
char *data;
for (int x=grectBand.width(); x>0; x--, pix++)
{
if (mask == 0)
mask = 0x80;
if (! *pix)
acc |= mask;
mask >>= 1;
if (mask == 0)
{
data = bin2hex[acc];
acc = 0;
*buf_ptr++ = data[0];
*buf_ptr++ = data[1];
symbols += 2;
if (symbols>70)
{
*buf_ptr++ = '\n';
symbols = 0;
}
}
}
if (mask != 0)
{
data = bin2hex[acc];
*buf_ptr++ = data[0];
*buf_ptr++ = data[1];
symbols += 2;
}
}
if (refresh_cb)
refresh_cb(refresh_cl_data);
}
str.writall(buffer, buf_ptr-buffer);
if (prn_progress_cb)
{
double done=(double) (grectBand.ymax
- prn_rect.ymin)/prn_rect.height();
if ((int) (20*print_done)!=(int) (20*done))
{
print_done=done;
prn_progress_cb(done, prn_progress_cl_data);
}
}
}
write(str, "\n");
}
write(str, "grestore\n");
}
void
DjVuToPS::
print_image_lev2(ByteStream &str,
GP<DjVuImage> dimg,
const GRect &prn_rect)
{
double print_done=0;
GRect all(0,0, dimg->get_width(),dimg->get_height());
GP<GPixmap> pm;
GRect test(0,0,1,1);
if (options.get_mode() == Options::FORE)
pm = dimg->get_fg_pixmap(test, all);
else if (options.get_mode() == Options::BACK)
pm = dimg->get_bg_pixmap(test, all);
else if (options.get_mode() != Options::BW)
pm = dimg->get_pixmap(test, all);
if (! pm)
return;
write(str,
"%% --- now doing a level 2 image\n"
"gsave\n");
// Display image
int band_bytes=125000;
int band_height = band_bytes/prn_rect.width();
int buffer_size = band_height*prn_rect.width();
int ps_chunk_height = 30960/prn_rect.width()+1;
buffer_size = buffer_size*21/10 + 32;
bool do_color = options.get_color();
if (do_color)
{
buffer_size *= 3;
write(str,
"/bufferR %d string def\n"
"/bufferG %d string def\n"
"/bufferB %d string def\n"
"DjVuColorSpace setcolorspace\n"
"<< /ImageType 1\n"
" /Width %d\n"
" /Height %d\n"
" /BitsPerComponent 8\n"
" /Decode [0 1 0 1 0 1]\n"
" /ImageMatrix [1 0 0 1 0 0]\n"
" /MultipleDataSources true\n"
" /DataSource [ { ReadR } { ReadG } { ReadB } ]\n"
" /Interpolate false >> image\n",
ps_chunk_height*prn_rect.width(),
ps_chunk_height*prn_rect.width(),
ps_chunk_height*prn_rect.width(),
prn_rect.width(), prn_rect.height());
}
else
{
write(str,
"DjVuColorSpace setcolorspace\n"
"<< /ImageType 1\n"
" /Width %d\n"
" /Height %d\n"
" /BitsPerComponent 8\n"
" /Decode [0 1]\n"
" /ImageMatrix [1 0 0 1 0 0]\n"
" /DataSource currentfile /ASCII85Decode\n"
" filter /RunLengthDecode filter\n"
" /Interpolate false >> image\n",
prn_rect.width(), prn_rect.height());
}
unsigned char *buffer;
GPBuffer<unsigned char> gbuffer(buffer,buffer_size);
unsigned char *rle_in;
GPBuffer<unsigned char> grle_in(rle_in,ps_chunk_height*prn_rect.width());
unsigned char *rle_out;
GPBuffer<unsigned char> grle_out(rle_out,2*ps_chunk_height*prn_rect.width());
{
// Start storing image in bands
unsigned char * rle_out_end = rle_out;
GRect grectBand = prn_rect;
grectBand.ymax = grectBand.ymin;
while(grectBand.ymax < prn_rect.ymax)
{
// Compute next band
grectBand.ymin = grectBand.ymax;
grectBand.ymax = grectBand.ymin+band_bytes/grectBand.width();
if (grectBand.ymax > prn_rect.ymax)
grectBand.ymax = prn_rect.ymax;
GRect all(0,0, dimg->get_width(),dimg->get_height());
pm = 0;
if (options.get_mode() == Options::FORE)
pm = dimg->get_fg_pixmap(grectBand, all);
else if (options.get_mode() == Options::BACK)
pm = dimg->get_bg_pixmap(grectBand, all);
else
pm = dimg->get_pixmap(grectBand, all);
// Store next band
unsigned char *buf_ptr = buffer;
if (do_color && pm)
{
int y=0;
while(y<grectBand.height())
{
int row, y1;
unsigned char *ptr, *ptr1;
// Doing R component of current chunk
for (row=0,ptr=rle_in,y1=y;
row<ps_chunk_height && y1<grectBand.height();
row++,y1++)
{
GPixel *pix = (*pm)[y1];
for (int x=grectBand.width(); x>0; x--,pix++)
*ptr++ = ramp[pix->r];
}
ptr1 = RLE_encode(rle_out, rle_in, ptr);
*ptr1++ = 0x80;
buf_ptr = ASCII85_encode(buf_ptr, rle_out, ptr1);
*buf_ptr++ = '~'; *buf_ptr++ = '>'; *buf_ptr++ = '\n';
// Doing G component of current chunk
for (row=0,ptr=rle_in,y1=y;
row<ps_chunk_height && y1<grectBand.height();
row++,y1++)
{
GPixel *pix = (*pm)[y1];
for (int x=grectBand.width(); x>0; x--,pix++)
*ptr++ = ramp[pix->g];
}
ptr1 = RLE_encode(rle_out, rle_in, ptr);
*ptr1++ = 0x80;
buf_ptr = ASCII85_encode(buf_ptr, rle_out, ptr1);
*buf_ptr++ = '~';
*buf_ptr++ = '>';
*buf_ptr++ = '\n';
// Doing B component of current chunk
for (row=0, ptr=rle_in, y1=y;
row<ps_chunk_height && y1<grectBand.height();
row++,y1++)
{
GPixel *pix = (*pm)[y1];
for (int x=grectBand.width(); x>0; x--,pix++)
*ptr++ = ramp[pix->b];
}
ptr1 = RLE_encode(rle_out, rle_in, ptr);
*ptr1++ = 0x80;
buf_ptr = ASCII85_encode(buf_ptr, rle_out, ptr1);
*buf_ptr++ = '~';
*buf_ptr++ = '>';
*buf_ptr++ = '\n';
y=y1;
if (refresh_cb)
refresh_cb(refresh_cl_data);
} //while (y>=0)
}
else if (pm)
{
// Don't use color
int y=0;
while(y<grectBand.height())
{
unsigned char *ptr = rle_in;
for(int row=0;
row<ps_chunk_height && y<grectBand.height();
row++,y++)
{
GPixel *pix = (*pm)[y];
for (int x=grectBand.width(); x>0; x--,pix++)
*ptr++ = ramp[GRAY(pix->r,pix->g,pix->b)];
}
rle_out_end = RLE_encode(rle_out_end, rle_in, ptr);
unsigned char *encode_to = rle_out
+ (rle_out_end-rle_out)/4*4;
int bytes_left = rle_out_end-encode_to;
buf_ptr = ASCII85_encode(buf_ptr, rle_out, encode_to);
*buf_ptr++ = '\n';
memcpy(rle_out, encode_to, bytes_left);
rle_out_end = rle_out+bytes_left;
if (refresh_cb)
refresh_cb(refresh_cl_data);
}
if (grectBand.ymax >= prn_rect.ymax)
{
*rle_out_end++ = 0x80; // Add EOF marker
buf_ptr = ASCII85_encode(buf_ptr, rle_out, rle_out_end);
*buf_ptr++ = '~';
*buf_ptr++ = '>';
*buf_ptr++ = '\n';
}
}
str.writall(buffer, buf_ptr-buffer);
if (prn_progress_cb)
{
double done=(double) (grectBand.ymax
- prn_rect.ymin)/prn_rect.height();
if ((int) (20*print_done)!=(int) (20*done))
{
print_done=done;
prn_progress_cb(done, prn_progress_cl_data);
}
}
}
write(str, "\n");
}
write(str, "grestore\n");
}
static void
get_anno_sub(IFFByteStream &iff, IFFByteStream &out)
{
GUTF8String chkid;
while (iff.get_chunk(chkid))
{
if (iff.composite())
get_anno_sub(iff, out);
else if (chkid == "ANTa" || chkid == "ANTz" ||
chkid == "TXTa" || chkid == "TXTz" )
{
out.put_chunk(chkid);
out.copy(*iff.get_bytestream());
out.close_chunk();
}
iff.close_chunk();
}
}
static GP<ByteStream>
get_anno(GP<DjVuFile> f)
{
if (! f->anno)
{
GP<ByteStream> bs = f->get_init_data_pool()->get_stream();
GP<ByteStream> anno = ByteStream::create();
GP<IFFByteStream> in = IFFByteStream::create(bs);
GP<IFFByteStream> out = IFFByteStream::create(anno);
get_anno_sub(*in, *out);
f->anno = anno;
}
f->anno->seek(0);
return f->anno;
}
static GP<DjVuTXT>
get_text(GP<DjVuFile> file)
{
GUTF8String chkid;
GP<IFFByteStream> iff = IFFByteStream::create(get_anno(file));
while (iff->get_chunk(chkid))
{
if (chkid == "TXTa")
{
GP<DjVuTXT> txt = DjVuTXT::create();
txt->decode(iff->get_bytestream());
return txt;
}
else if (chkid == "TXTz")
{
GP<DjVuTXT> txt = DjVuTXT::create();
GP<ByteStream> bsiff = BSByteStream::create(iff->get_bytestream());
txt->decode(bsiff);
return txt;
}
iff->close_chunk();
}
return 0;
}
static void
print_ps_string(const char *data, int length, ByteStream &out)
{
while (*data && length>0)
{
int span = 0;
while (span<length && data[span]>=0x20 && data[span]<0x7f
&& data[span]!='(' && data[span]!=')' && data[span]!='\\' )
span++;
if (span > 0)
{
out.write(data, span);
data += span;
length -= span;
}
else
{
char buffer[5];
sprintf(buffer,"\\%03o", *data);
out.write(buffer,4);
data += 1;
length -= 1;
}
}
}
static void
print_txt_sub(DjVuTXT &txt, DjVuTXT::Zone &zone,
ByteStream &out,int &lastx,int &lasty)
{
// Get separator
char separator = 0;
switch(zone.ztype)
{
case DjVuTXT::COLUMN:
separator = DjVuTXT::end_of_column; break;
case DjVuTXT::REGION:
separator = DjVuTXT::end_of_region; break;
case DjVuTXT::PARAGRAPH:
separator = DjVuTXT::end_of_paragraph; break;
case DjVuTXT::LINE:
separator = DjVuTXT::end_of_line; break;
case DjVuTXT::WORD:
separator = ' '; break;
default:
separator = 0; break;
}
// Zone children
if (zone.children.isempty())
{
const char *data = (const char*)txt.textUTF8 + zone.text_start;
int length = zone.text_length;
if (data[length-1] == separator)
length -= 1;
out.write("( ",2);
print_ps_string(data,length,out);
out.write(")",1);
GUTF8String message;
int tmpx= zone.rect.xmin-lastx;
int tmpy= zone.rect.ymin-lasty;
message.format(" %d %d S \n", tmpx, tmpy);
lastx=zone.rect.xmin;
lasty=zone.rect.ymin;
out.write((const char*)message, message.length());
}
else
{
if (zone.ztype==DjVuTXT::LINE)
{
GUTF8String message;
message.format("%d F\n",zone.rect.ymax-zone.rect.ymin);
out.write((const char*)message,message.length());
}
for (GPosition pos=zone.children; pos; ++pos)
print_txt_sub(txt, zone.children[pos], out,lastx,lasty);
}
}
static void
print_txt(GP<DjVuTXT> txt,
ByteStream &out )
{
if (txt)
{
int lastx=0;
int lasty=0;
GUTF8String message =
"%% -- now doing hidden text\n"
"gsave -1 -1 0 0 clip 0 0 moveto\n";
out.write((const char*)message,message.length());
print_txt_sub(*txt, txt->page_zone, out,lastx,lasty);
message =
"grestore \n";
out.write((const char*)message,message.length());
}
}
void
DjVuToPS::
print_image(ByteStream &str,
GP<DjVuImage> dimg,
const GRect &prn_rect,
GP<DjVuTXT> txt)
{
/* Just outputs the specified image. The function assumes, that
all add-ons (like {\em document setup}, {\em page setup}) are
already there. It will just output the image. Since
output of this function will generate PostScript errors when
used without output of auxiliary functions, it should be
used carefully. */
DEBUG_MSG("DjVuToPS::print_image(): Printing DjVuImage to a stream\n");
DEBUG_MAKE_INDENT(3);
if (!dimg)
G_THROW(ERR_MSG("DjVuToPS.empty_image"));
if (prn_rect.isempty())
G_THROW(ERR_MSG("DjVuToPS.empty_rect"));
if (prn_progress_cb)
prn_progress_cb(0, prn_progress_cl_data);
// Compute information for chosen display mode
print_txt(txt, str);
make_gamma_ramp(dimg);
if (options.get_level() < 2)
{
print_image_lev1(str, dimg, prn_rect);
}
else if (options.get_level() < 3 && dimg->get_fgpm())
{
switch(options.get_mode())
{
case Options::COLOR:
case Options::FORE:
print_image_lev2(str, dimg, prn_rect);
break;
case Options::BW:
print_fg(str, dimg, prn_rect);
break;
case Options::BACK:
print_bg(str, dimg, prn_rect);
break;
}
}
else
{
switch(options.get_mode())
{
case Options::COLOR:
print_bg(str, dimg, prn_rect);
print_fg(str, dimg, prn_rect);
break;
case Options::FORE:
case Options::BW:
print_fg(str, dimg, prn_rect);
break;
case Options::BACK:
print_bg(str, dimg, prn_rect);
break;
}
}
if (prn_progress_cb)
prn_progress_cb(1, prn_progress_cl_data);
}
// ***********************************************************************
// ******* PUBLIC FUNCTION FOR PRINTING A SINGLE PAGE ********************
// ***********************************************************************
void
DjVuToPS::
print(ByteStream &str,
GP<DjVuImage> dimg,
const GRect &prn_rect_in,
const GRect &img_rect,
int override_dpi)
{
DEBUG_MSG("DjVuToPS::print(): Printing DjVu page to a stream\n");
DEBUG_MAKE_INDENT(3);
GRect prn_rect;
prn_rect.intersect(prn_rect_in, img_rect);
DEBUG_MSG("prn_rect=(" << prn_rect.xmin << ", " << prn_rect.ymin << ", " <<
prn_rect.width() << ", " << prn_rect.height() << ")\n");
DEBUG_MSG("img_rect=(" << img_rect.xmin << ", " << img_rect.ymin << ", " <<
img_rect.width() << ", " << img_rect.height() << ")\n");
if (!dimg)
G_THROW(ERR_MSG("DjVuToPS.empty_image"));
if (prn_rect.isempty())
G_THROW(ERR_MSG("DjVuToPS.empty_rect"));
if (img_rect.isempty())
G_THROW(ERR_MSG("DjVuToPS.bad_scale"));
GRectMapper mapper;
mapper.set_input(img_rect);
GRect full_rect(0, 0, dimg->get_width(), dimg->get_height());
mapper.set_output(full_rect);
mapper.map(prn_rect);
int image_dpi = dimg->get_dpi();
if (override_dpi>0)
image_dpi = override_dpi;
if (image_dpi <= 0)
image_dpi = 300;
store_doc_prolog(str, 1, (int)(image_dpi), &prn_rect);
store_doc_setup(str);
write(str,"%%%%Page: 1 1\n");
store_page_setup(str, (int)(image_dpi), prn_rect);
print_image(str, dimg, prn_rect, 0);
store_page_trailer(str);
write(str,"showpage\n");
store_doc_trailer(str);
}
// ***********************************************************************
// *************************** DOCUMENT LEVEL ****************************
// ***********************************************************************
void
DjVuToPS::
parse_range(GP<DjVuDocument> doc,
GUTF8String page_range,
GList<int> &pages_todo)
{
int doc_pages = doc->get_pages_num();
if (!page_range.length())
page_range.format("1-%d", doc_pages);
DEBUG_MSG("page_range='" << (const char *)page_range << "'\n");
int spec = 0;
int both = 1;
int start_page = 1;
int end_page = doc_pages;
const char *q = (const char*)page_range;
char *p = (char*)q;
while (*p)
{
while (*p==' ')
p += 1;
if (! *p)
break;
if (*p>='0' && *p<='9')
{
end_page = strtol(p, &p, 10);
spec = 1;
}
else if (*p=='$')
{
spec = 1;
end_page = doc_pages;
p += 1;
}
else if (both)
{
end_page = 1;
}
else
{
end_page = doc_pages;
}
while (*p==' ')
p += 1;
if (both)
{
start_page = end_page;
if (*p == '-')
{
p += 1;
both = 0;
continue;
}
}
both = 1;
while (*p==' ')
p += 1;
if (*p && *p != ',')
G_THROW(ERR_MSG("DjVuToPS.bad_range")
+ GUTF8String("\t") + GUTF8String(p) );
if (*p == ',')
p += 1;
if (! spec)
G_THROW(ERR_MSG("DjVuToPS.bad_range")
+ GUTF8String("\t") + page_range );
spec = 0;
if (end_page < 0)
end_page = 0;
if (start_page < 0)
start_page = 0;
if (end_page > doc_pages)
end_page = doc_pages;
if (start_page > doc_pages)
start_page = doc_pages;
if (start_page <= end_page)
for(int page_num=start_page; page_num<=end_page; page_num++)
pages_todo.append(page_num-1);
else
for(int page_num=start_page; page_num>=end_page; page_num--)
pages_todo.append(page_num-1);
}
}
class DjVuToPS::DecodePort : public DjVuPort
{
protected:
DecodePort(void);
public:
static GP<DecodePort> create(void);
GEvent decode_event;
bool decode_event_received;
double decode_done;
GURL decode_page_url;
virtual void notify_file_flags_changed(const DjVuFile*,long,long);
virtual void notify_decode_progress(const DjVuPort*,double);
};
DjVuToPS::DecodePort::
DecodePort(void)
: decode_event_received(false),
decode_done((double)0)
{
}
GP<DjVuToPS::DecodePort>
DjVuToPS::DecodePort::
create(void)
{
return new DecodePort;
}
void
DjVuToPS::DecodePort::
notify_file_flags_changed(const DjVuFile *source,
long set_mask, long clr_mask)
{
// WARNING! This function is called from another thread
if (set_mask & (DjVuFile::DECODE_OK |
DjVuFile::DECODE_FAILED |
DjVuFile::DECODE_STOPPED ))
{
if (source->get_url() == decode_page_url)
{
decode_event_received=true;
decode_event.set();
}
}
}
void
DjVuToPS::DecodePort::
notify_decode_progress(const DjVuPort *source, double done)
{
// WARNING! This function is called from another thread
if (source->inherits("DjVuFile"))
{
DjVuFile * file=(DjVuFile *) source;
if (file->get_url()==decode_page_url)
if ((int) (decode_done*20)!=(int) (done*20))
{
decode_done=done;
decode_event_received=true;
decode_event.set();
}
}
}
void
DjVuToPS::
set_refresh_cb(void (*_refresh_cb)(void*), void *_refresh_cl_data)
{
refresh_cb = _refresh_cb;
refresh_cl_data = _refresh_cl_data;
}
void
DjVuToPS::
set_prn_progress_cb(void (*_prn_progress_cb)(double, void *),
void *_prn_progress_cl_data)
{
prn_progress_cb=_prn_progress_cb;
prn_progress_cl_data=_prn_progress_cl_data;
}
void
DjVuToPS::
set_dec_progress_cb(void (*_dec_progress_cb)(double, void *),
void *_dec_progress_cl_data)
{
dec_progress_cb=_dec_progress_cb;
dec_progress_cl_data=_dec_progress_cl_data;
}
void
DjVuToPS::
set_info_cb(void (*_info_cb)(int, int, int, Stage, void*),
void *_info_cl_data)
{
info_cb=_info_cb;
info_cl_data=_info_cl_data;
}
GP<DjVuImage>
DjVuToPS::
decode_page(GP<DjVuDocument> doc,
int page_num, int cnt, int todo)
{
DEBUG_MSG("processing page #" << page_num << "\n");
if (! port)
{
port = DecodePort::create();
DjVuPort::get_portcaster()->add_route((DjVuDocument*)doc, port);
}
port->decode_event_received = false;
port->decode_done = 0;
GP<DjVuFile> djvu_file;
GP<DjVuImage> dimg;
if (page_num >= 0 && page_num < doc->get_pages_num())
djvu_file = doc->get_djvu_file(page_num);
if (! djvu_file )
return 0;
if (djvu_file->is_decode_ok())
return doc->get_page(page_num, false);
// This is the best place to call info_cb(). Note, that
// get_page() will start decoding if necessary, and will not
// return until the decoding is over in a single threaded
// environment. That's why we call get_djvu_file() first.
if (info_cb)
info_cb(page_num, cnt, todo, DECODING, info_cl_data);
// Do NOT decode the page synchronously here!!!
// The plugin will deadlock otherwise.
dimg = doc->get_page(page_num, false);
djvu_file = dimg->get_djvu_file();
port->decode_page_url = djvu_file->get_url();
if (djvu_file->is_decode_ok())
return dimg;
DEBUG_MSG("decoding\n");
if (dec_progress_cb)
dec_progress_cb(0, dec_progress_cl_data);
while(! djvu_file->is_decode_ok())
{
while(!port->decode_event_received &&
!djvu_file->is_decode_ok())
{
port->decode_event.wait(250);
if (refresh_cb)
refresh_cb(refresh_cl_data);
}
port->decode_event_received = false;
if (djvu_file->is_decode_failed() ||
djvu_file->is_decode_stopped())
G_THROW(ERR_MSG("DjVuToPS.no_image")
+ GUTF8String("\t")
+ GUTF8String(page_num));
if (dec_progress_cb)
dec_progress_cb(port->decode_done, dec_progress_cl_data);
}
if (dec_progress_cb)
dec_progress_cb(1, dec_progress_cl_data);
return dimg;
}
void
DjVuToPS::
process_single_page(ByteStream &str,
GP<DjVuDocument> doc,
int page_num, int cnt, int todo,
int magic)
{
GP<DjVuTXT> txt;
GP<DjVuImage> dimg;
dimg = decode_page(doc, page_num, cnt, todo);
if (options.get_text())
txt = get_text(dimg->get_djvu_file());
if (info_cb)
info_cb(page_num, cnt, todo, PRINTING, info_cl_data);
if (!magic)
write(str, "%%%%Page: %d %d\n", page_num+1, cnt+1);
if (dimg)
{
int dpi = dimg->get_dpi();
dpi = ((dpi <= 0) ? 300 : dpi);
GRect img_rect(0, 0, dimg->get_width(), dimg->get_height());
store_page_setup(str, dpi, img_rect, magic);
print_image(str, dimg, img_rect,txt);
store_page_trailer(str);
}
if (!magic)
write(str,"showpage\n");
}
struct pdata {
int page1, page2;
int smax, spos;
int offset;
};
void
DjVuToPS::
process_double_page(ByteStream &str,
GP<DjVuDocument> doc,
void *v, int cnt, int todo)
{
const pdata *inf = (const pdata*)v;
int off = abs(inf->offset);
write(str,
"%%%%Page: (%d,%d) %d\n"
"gsave\n"
"/fold-dict 8 dict dup 3 1 roll def begin\n"
" clippath pathbbox newpath pop pop translate\n"
" clippath pathbbox newpath 4 2 roll pop pop\n"
" /ph exch def\n"
" /pw exch def\n"
" /w ph %d sub 2 div def\n"
" /m1 %d def\n"
" /m2 %d def\n"
"end\n",
inf->page1 + 1, inf->page2 + 1, cnt,
2 * (off + options.get_bookletfold(inf->smax-1)),
inf->offset + options.get_bookletfold(inf->spos),
inf->offset - options.get_bookletfold(inf->spos));
if (options.get_cropmarks())
write(str,
"%% -- folding marks\n"
"fold-dict begin\n"
" 0 setgray 0.5 setlinewidth\n"
" ph m1 m2 add add 2 div dup\n"
" 0 exch moveto 36 0 rlineto stroke\n"
" pw exch moveto -36 0 rlineto stroke\n"
"end\n");
write(str,
"%% -- first page\n"
"gsave fold-dict begin\n"
" 0 ph 2 div w add m1 add translate 270 rotate\n"
" 0 0 w pw rectclip end\n");
if (inf->page1 >= 0)
process_single_page(str, doc, inf->page1, cnt*2, todo*2, +1);
write(str,
"grestore\n"
"%% -- second page\n"
"gsave fold-dict begin\n"
" 0 ph 2 div m2 add translate 270 rotate\n"
" 0 0 w pw rectclip end\n");
if (inf->page2 >= 0)
process_single_page(str, doc, inf->page2, cnt*2+1, todo*2, -1);
write(str,
"grestore\n"
"grestore\n"
"showpage\n");
}
static void
booklet_order(GList<int>& pages, int smax)
{
// -- make a multiple of four
while (pages.size() & 0x3)
pages.append(-1);
// -- copy to array
int i = 0;
int n = pages.size();
GTArray<int> p(0,n-1);
for (GPosition pos=pages; pos; ++pos)
p[i++] = pages[pos];
// -- rebuild
pages.empty();
for (i=0; i<n; i+=smax)
{
int lo = i;
int hi = i+smax-1;
if (hi >= n)
hi = n-1;
while (lo < hi)
{
pages.append(p[hi--]);
pages.append(p[lo++]);
pages.append(p[lo++]);
pages.append(p[hi--]);
}
}
}
// ***********************************************************************
// ******* PUBLIC FUNCTIONS FOR PRINTING MULTIPLE PAGES ******************
// ***********************************************************************
void
DjVuToPS::
print(ByteStream &str,
GP<DjVuDocument> doc,
GUTF8String page_range)
{
DEBUG_MSG("DjVuToPS::print(): Printing DjVu document\n");
DEBUG_MAKE_INDENT(3);
// Get page range
GList<int> pages_todo;
parse_range(doc, page_range, pages_todo);
int todo = pages_todo.size();
if (options.get_format()==Options::EPS)
{
/* Encapsulated Postscript mode */
if (todo != 1)
G_THROW(ERR_MSG("DjVuToPS.only_one_page"));
GPosition pos = pages_todo;
int page_num = pages_todo[pos];
GP<DjVuImage> dimg = decode_page(doc,page_num,0,todo);
if (! dimg)
G_THROW(ERR_MSG("DjVuToPS.no_image") + GUTF8String("\t1"));
GRect bbox(0, 0, dimg->get_width(), dimg->get_height());
store_doc_prolog(str, 1, dimg->get_dpi(), &bbox);
store_doc_setup(str);
process_single_page(str, doc, page_num, 0, todo, 0);
}
else if (options.get_bookletmode()==Options::OFF)
{
/* Normal mode */
int cnt = 0;
store_doc_prolog(str, todo, 0, 0);
store_doc_setup(str);
for(GPosition pos = pages_todo; pos; ++pos)
process_single_page(str,doc,pages_todo[pos],cnt++,todo,0);
store_doc_trailer(str);
}
else
{
/* Booklet mode */
int sheets_left = (todo+3)/4;
int sides_todo = sheets_left;
if (options.get_bookletmode() == Options::RECTOVERSO)
sides_todo *= 2;
int sheets_max = (options.get_bookletmax()+3)/4;
if (! sheets_max)
sheets_max = sheets_left;
// -- reorder pages
booklet_order(pages_todo, sheets_max*4);
// -- print
int sides = 0;
int sheetpos = sheets_max;
store_doc_prolog(str, sides_todo, 0, 0);
store_doc_setup(str);
for (GPosition p=pages_todo; p; ++p)
{
struct pdata inf;
inf.page1 = pages_todo[p];
inf.page2 = pages_todo[++p];
inf.smax = sheets_max;
inf.spos = --sheetpos;
inf.offset = options.get_bookletalign();
if (options.get_bookletmode() != Options::VERSO)
process_double_page(str,doc,(void*)&inf,sides++,sides_todo);
inf.page1 = pages_todo[++p];
inf.page2 = pages_todo[++p];
inf.offset = -inf.offset;
if (options.get_bookletmode() != Options::RECTO)
process_double_page(str,doc,(void*)&inf,sides++,sides_todo);
sheets_left -= 1;
if (sheetpos <= 0)
sheetpos = ((sheets_max<sheets_left) ? sheets_max : sheets_left);
}
store_doc_trailer(str);
}
}
void
DjVuToPS::
print(ByteStream &str, GP<DjVuDocument> doc)
{
GUTF8String dummy;
print(str,doc,dummy);
}
#ifdef HAVE_NAMESPACES
}
# ifndef NOT_USING_DJVU_NAMESPACE
using namespace DJVU;
# endif
#endif
|