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
|
// Copyright (C) 2004-2024 Artifex Software, Inc.
//
// This file is part of MuPDF.
//
// MuPDF is free software: you can redistribute it and/or modify it under the
// terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
// details.
//
// You should have received a copy of the GNU Affero General Public License
// along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
//
// Alternative licensing terms are available from the licensor.
// For commercial licensing, see <https://www.artifex.com/> or contact
// Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
// CA 94129, USA, for further information.
#include "mupdf/fitz.h"
#include "pdf-annot-imp.h"
#include <string.h>
/* Must be kept in sync with definitions in pdf_util.js */
enum
{
Display_Visible,
Display_Hidden,
Display_NoPrint,
Display_NoView
};
enum
{
SigFlag_SignaturesExist = 1,
SigFlag_AppendOnly = 2
};
const char *pdf_field_value(fz_context *ctx, pdf_obj *field)
{
pdf_obj *v = pdf_dict_get_inheritable(ctx, field, PDF_NAME(V));
if (pdf_is_name(ctx, v))
return pdf_to_name(ctx, v);
if (pdf_is_stream(ctx, v))
{
// FIXME: pdf_dict_put_inheritable...
char *str = pdf_new_utf8_from_pdf_stream_obj(ctx, v);
fz_try(ctx)
pdf_dict_put_text_string(ctx, field, PDF_NAME(V), str);
fz_always(ctx)
fz_free(ctx, str);
fz_catch(ctx)
fz_rethrow(ctx);
v = pdf_dict_get(ctx, field, PDF_NAME(V));
}
return pdf_to_text_string(ctx, v);
}
int pdf_field_flags(fz_context *ctx, pdf_obj *obj)
{
return pdf_dict_get_inheritable_int(ctx, obj, PDF_NAME(Ff));
}
int pdf_field_type(fz_context *ctx, pdf_obj *obj)
{
pdf_obj *type = pdf_dict_get_inheritable(ctx, obj, PDF_NAME(FT));
int flags = pdf_field_flags(ctx, obj);
if (pdf_name_eq(ctx, type, PDF_NAME(Btn)))
{
if (flags & PDF_BTN_FIELD_IS_PUSHBUTTON)
return PDF_WIDGET_TYPE_BUTTON;
else if (flags & PDF_BTN_FIELD_IS_RADIO)
return PDF_WIDGET_TYPE_RADIOBUTTON;
else
return PDF_WIDGET_TYPE_CHECKBOX;
}
else if (pdf_name_eq(ctx, type, PDF_NAME(Tx)))
return PDF_WIDGET_TYPE_TEXT;
else if (pdf_name_eq(ctx, type, PDF_NAME(Ch)))
{
if (flags & PDF_CH_FIELD_IS_COMBO)
return PDF_WIDGET_TYPE_COMBOBOX;
else
return PDF_WIDGET_TYPE_LISTBOX;
}
else if (pdf_name_eq(ctx, type, PDF_NAME(Sig)))
return PDF_WIDGET_TYPE_SIGNATURE;
else
return PDF_WIDGET_TYPE_BUTTON;
}
const char *pdf_field_type_string(fz_context *ctx, pdf_obj *obj)
{
switch (pdf_field_type(ctx, obj))
{
default:
case PDF_WIDGET_TYPE_BUTTON: return "button";
case PDF_WIDGET_TYPE_CHECKBOX: return "checkbox";
case PDF_WIDGET_TYPE_COMBOBOX: return "combobox";
case PDF_WIDGET_TYPE_LISTBOX: return "listbox";
case PDF_WIDGET_TYPE_RADIOBUTTON: return "radiobutton";
case PDF_WIDGET_TYPE_SIGNATURE: return "signature";
case PDF_WIDGET_TYPE_TEXT: return "text";
}
}
/* Find the point in a field hierarchy where all descendants
* share the same name */
static pdf_obj *find_head_of_field_group(fz_context *ctx, pdf_obj *obj)
{
if (obj == NULL || pdf_dict_get(ctx, obj, PDF_NAME(T)))
return obj;
else
return find_head_of_field_group(ctx, pdf_dict_get(ctx, obj, PDF_NAME(Parent)));
}
static void pdf_field_mark_dirty(fz_context *ctx, pdf_obj *field)
{
pdf_document *doc = pdf_get_bound_document(ctx, field);
pdf_obj *kids = pdf_dict_get(ctx, field, PDF_NAME(Kids));
if (kids)
{
int i, n = pdf_array_len(ctx, kids);
for (i = 0; i < n; i++)
pdf_field_mark_dirty(ctx, pdf_array_get(ctx, kids, i));
}
pdf_dirty_obj(ctx, field);
if (doc)
doc->resynth_required = 1;
}
static void update_field_value(fz_context *ctx, pdf_document *doc, pdf_obj *obj, const char *text)
{
const char *old_text;
pdf_obj *grp;
if (!text)
text = "";
/* All fields of the same name should be updated, so
* set the value at the head of the group */
grp = find_head_of_field_group(ctx, obj);
if (grp)
obj = grp;
/* Only update if we change the actual value. */
old_text = pdf_dict_get_text_string(ctx, obj, PDF_NAME(V));
if (old_text && !strcmp(old_text, text))
return;
pdf_dict_put_text_string(ctx, obj, PDF_NAME(V), text);
pdf_field_mark_dirty(ctx, obj);
}
static pdf_obj *
pdf_lookup_field_imp(fz_context *ctx, pdf_obj *arr, const char *str, pdf_cycle_list *cycle_up);
static pdf_obj *
lookup_field_sub(fz_context *ctx, pdf_obj *dict, const char *str, pdf_cycle_list *cycle_up)
{
pdf_obj *kids;
pdf_obj *name;
name = pdf_dict_get(ctx, dict, PDF_NAME(T));
/* If we have a name, check it matches. If it matches, consume that
* portion of str. If not, exit. */
if (name)
{
const char *match = pdf_to_text_string(ctx, name);
const char *e = str;
size_t len;
while (*e && *e != '.')
e++;
len = e-str;
if (strncmp(str, match, len) != 0 || (match[len] != 0 && match[len] != '.'))
/* name doesn't match. */
return NULL;
str = e;
if (*str == '.')
str++;
}
/* If there is a kids array, but the search string is not empty, we have
encountered an internal field which represents a set of terminal fields. */
/* If there is a kids array and the search string is not empty,
walk those looking for the appropriate one. */
kids = pdf_dict_get(ctx, dict, PDF_NAME(Kids));
if (kids && *str != 0)
return pdf_lookup_field_imp(ctx, kids, str, cycle_up);
/* The field may be a terminal or an internal field at this point.
Accept it as the match if the match string is exhausted. */
if (*str == 0)
return dict;
return NULL;
}
static pdf_obj *
pdf_lookup_field_imp(fz_context *ctx, pdf_obj *arr, const char *str, pdf_cycle_list *cycle_up)
{
pdf_cycle_list cycle;
int len = pdf_array_len(ctx, arr);
int i;
for (i = 0; i < len; i++)
{
pdf_obj *k = pdf_array_get(ctx, arr, i);
pdf_obj *found;
if (pdf_cycle(ctx, &cycle, cycle_up, k))
fz_throw(ctx, FZ_ERROR_FORMAT, "cycle in fields");
found = lookup_field_sub(ctx, k, str, &cycle);
if (found)
return found;
}
return NULL;
}
pdf_obj *
pdf_lookup_field(fz_context *ctx, pdf_obj *arr, const char *str)
{
return pdf_lookup_field_imp(ctx, arr, str, NULL);
}
static void reset_form_field(fz_context *ctx, pdf_document *doc, pdf_obj *field)
{
/* Set V to DV wherever DV is present, and delete V where DV is not.
* FIXME: we assume for now that V has not been set unequal
* to DV higher in the hierarchy than "field".
*
* At the bottom of the hierarchy we may find widget annotations
* that aren't also fields, but DV and V will not be present in their
* dictionaries, and attempts to remove V will be harmless. */
pdf_obj *dv = pdf_dict_get(ctx, field, PDF_NAME(DV));
pdf_obj *kids = pdf_dict_get(ctx, field, PDF_NAME(Kids));
if (dv)
pdf_dict_put(ctx, field, PDF_NAME(V), dv);
else
pdf_dict_del(ctx, field, PDF_NAME(V));
if (kids == NULL)
{
/* The leaves of the tree are widget annotations
* In some cases we need to update the appearance state;
* in others we need to mark the field as dirty so that
* the appearance stream will be regenerated. */
switch (pdf_field_type(ctx, field))
{
case PDF_WIDGET_TYPE_CHECKBOX:
case PDF_WIDGET_TYPE_RADIOBUTTON:
{
pdf_obj *leafv = pdf_dict_get_inheritable(ctx, field, PDF_NAME(V));
pdf_obj *ap = pdf_dict_get(ctx, field, PDF_NAME(AP));
pdf_obj *n = pdf_dict_get(ctx, ap, PDF_NAME(N));
/* Value does not refer to any appearance state in the
normal appearance stream dictionary, default to Off instead. */
if (pdf_is_dict(ctx, n) && !pdf_dict_get(ctx, n, leafv))
leafv = NULL;
if (!pdf_is_name(ctx, leafv))
leafv = PDF_NAME(Off);
pdf_dict_put(ctx, field, PDF_NAME(AS), leafv);
}
pdf_field_mark_dirty(ctx, field);
break;
case PDF_WIDGET_TYPE_BUTTON:
case PDF_WIDGET_TYPE_SIGNATURE:
/* Pushbuttons and signatures have no value to reset. */
break;
default:
pdf_field_mark_dirty(ctx, field);
break;
}
}
}
void pdf_field_reset(fz_context *ctx, pdf_document *doc, pdf_obj *field)
{
pdf_obj *kids = pdf_dict_get(ctx, field, PDF_NAME(Kids));
reset_form_field(ctx, doc, field);
if (kids)
{
int i, n = pdf_array_len(ctx, kids);
for (i = 0; i < n; i++)
pdf_field_reset(ctx, doc, pdf_array_get(ctx, kids, i));
}
}
static void add_field_hierarchy_to_array(fz_context *ctx, pdf_obj *array, pdf_obj *field, pdf_obj *fields, int exclude)
{
pdf_obj *kids = pdf_dict_get(ctx, field, PDF_NAME(Kids));
char *needle = pdf_load_field_name(ctx, field);
int i, n;
fz_try(ctx)
{
n = pdf_array_len(ctx, fields);
for (i = 0; i < n; i++)
{
char *name = pdf_load_field_name(ctx, pdf_array_get(ctx, fields, i));
int found = !strcmp(needle, name);
fz_free(ctx, name);
if (found)
break;
}
}
fz_always(ctx)
fz_free(ctx, needle);
fz_catch(ctx)
fz_rethrow(ctx);
if ((exclude && i < n) || (!exclude && i == n))
return;
pdf_array_push(ctx, array, field);
if (kids)
{
int i, n = pdf_array_len(ctx, kids);
for (i = 0; i < n; i++)
add_field_hierarchy_to_array(ctx, array, pdf_array_get(ctx, kids, i), fields, exclude);
}
}
/*
When resetting or submitting a form, the fields to act upon are defined
by an array of either field references or field names, plus a flag determining
whether to act upon the fields in the array, or all fields other than those in
the array. specified_fields interprets this information and produces the array
of fields to be acted upon.
*/
static pdf_obj *specified_fields(fz_context *ctx, pdf_document *doc, pdf_obj *fields, int exclude)
{
pdf_obj *form = pdf_dict_getl(ctx, pdf_trailer(ctx, doc), PDF_NAME(Root), PDF_NAME(AcroForm), PDF_NAME(Fields), NULL);
int i, n;
pdf_obj *result = pdf_new_array(ctx, doc, 0);
fz_try(ctx)
{
n = pdf_array_len(ctx, fields);
for (i = 0; i < n; i++)
{
pdf_obj *field = pdf_array_get(ctx, fields, i);
if (pdf_is_string(ctx, field))
field = pdf_lookup_field(ctx, form, pdf_to_str_buf(ctx, field));
if (field)
add_field_hierarchy_to_array(ctx, result, field, fields, exclude);
}
}
fz_catch(ctx)
{
pdf_drop_obj(ctx, result);
fz_rethrow(ctx);
}
return result;
}
void pdf_reset_form(fz_context *ctx, pdf_document *doc, pdf_obj *fields, int exclude)
{
pdf_obj *sfields = specified_fields(ctx, doc, fields, exclude);
fz_try(ctx)
{
int i, n = pdf_array_len(ctx, sfields);
for (i = 0; i < n; i++)
reset_form_field(ctx, doc, pdf_array_get(ctx, sfields, i));
doc->recalculate = 1;
}
fz_always(ctx)
pdf_drop_obj(ctx, sfields);
fz_catch(ctx)
fz_rethrow(ctx);
}
typedef struct
{
pdf_obj *pageobj;
pdf_obj *chk;
} lookup_state;
static void *find_widget_on_page(fz_context *ctx, fz_page *page_, void *state_)
{
lookup_state *state = (lookup_state *) state_;
pdf_page *page = (pdf_page *) page_;
pdf_annot *widget;
if (state->pageobj && pdf_objcmp_resolve(ctx, state->pageobj, page->obj))
return NULL;
for (widget = pdf_first_widget(ctx, page); widget != NULL; widget = pdf_next_widget(ctx, widget))
{
if (!pdf_objcmp_resolve(ctx, state->chk, widget->obj))
return widget;
}
return NULL;
}
static pdf_annot *find_widget(fz_context *ctx, pdf_document *doc, pdf_obj *chk)
{
lookup_state state;
state.pageobj = pdf_dict_get(ctx, chk, PDF_NAME(P));
state.chk = chk;
return fz_process_opened_pages(ctx, (fz_document *) doc, find_widget_on_page, &state);
}
static void set_check(fz_context *ctx, pdf_document *doc, pdf_obj *chk, pdf_obj *name)
{
pdf_obj *n = pdf_dict_getp(ctx, chk, "AP/N");
pdf_obj *val;
/* If name is a possible value of this check
* box then use it, otherwise use "Off" */
if (pdf_dict_get(ctx, n, name))
val = name;
else
val = PDF_NAME(Off);
if (pdf_name_eq(ctx, pdf_dict_get(ctx, chk, PDF_NAME(AS)), val))
return;
pdf_dict_put(ctx, chk, PDF_NAME(AS), val);
pdf_set_annot_has_changed(ctx, find_widget(ctx, doc, chk));
}
/* Set the values of all fields in a group defined by a node
* in the hierarchy */
static void set_check_grp(fz_context *ctx, pdf_document *doc, pdf_obj *grp, pdf_obj *val)
{
pdf_obj *kids = pdf_dict_get(ctx, grp, PDF_NAME(Kids));
if (kids == NULL)
{
set_check(ctx, doc, grp, val);
}
else
{
int i, n = pdf_array_len(ctx, kids);
for (i = 0; i < n; i++)
set_check_grp(ctx, doc, pdf_array_get(ctx, kids, i), val);
}
}
void pdf_calculate_form(fz_context *ctx, pdf_document *doc)
{
if (doc->js)
{
fz_try(ctx)
{
pdf_obj *co = pdf_dict_getp(ctx, pdf_trailer(ctx, doc), "Root/AcroForm/CO");
int i, n = pdf_array_len(ctx, co);
for (i = 0; i < n; i++)
{
pdf_obj *field = pdf_array_get(ctx, co, i);
pdf_field_event_calculate(ctx, doc, field);
}
}
fz_always(ctx)
doc->recalculate = 0;
fz_catch(ctx)
fz_rethrow(ctx);
}
}
static pdf_obj *find_on_state(fz_context *ctx, pdf_obj *dict)
{
int i, n = pdf_dict_len(ctx, dict);
for (i = 0; i < n; ++i)
{
pdf_obj *key = pdf_dict_get_key(ctx, dict, i);
if (key != PDF_NAME(Off))
return key;
}
return NULL;
}
pdf_obj *pdf_button_field_on_state(fz_context *ctx, pdf_obj *field)
{
pdf_obj *ap = pdf_dict_get(ctx, field, PDF_NAME(AP));
pdf_obj *on = find_on_state(ctx, pdf_dict_get(ctx, ap, PDF_NAME(N)));
if (!on) on = find_on_state(ctx, pdf_dict_get(ctx, ap, PDF_NAME(D)));
if (!on) on = PDF_NAME(Yes);
return on;
}
static void
begin_annot_op(fz_context *ctx, pdf_annot *annot, const char *op)
{
if (!annot->page)
fz_throw(ctx, FZ_ERROR_ARGUMENT, "annotation not bound to any page");
pdf_begin_operation(ctx, annot->page->doc, op);
}
static void
end_annot_op(fz_context *ctx, pdf_annot *annot)
{
pdf_end_operation(ctx, annot->page->doc);
}
static void
abandon_annot_op(fz_context *ctx, pdf_annot *annot)
{
pdf_abandon_operation(ctx, annot->page->doc);
}
static void toggle_check_box(fz_context *ctx, pdf_annot *annot)
{
pdf_document *doc = annot->page->doc;
begin_annot_op(ctx, annot, "Toggle checkbox");
fz_try(ctx)
{
pdf_obj *field = annot->obj;
int ff = pdf_field_flags(ctx, field);
int is_radio = (ff & PDF_BTN_FIELD_IS_RADIO);
int is_no_toggle_to_off = (ff & PDF_BTN_FIELD_IS_NO_TOGGLE_TO_OFF);
pdf_obj *grp, *as, *val;
grp = find_head_of_field_group(ctx, field);
if (!grp)
grp = field;
/* TODO: check V value as well as or instead of AS? */
as = pdf_dict_get(ctx, field, PDF_NAME(AS));
if (as && as != PDF_NAME(Off))
{
if (is_radio && is_no_toggle_to_off)
{
end_annot_op(ctx, annot);
break;
}
val = PDF_NAME(Off);
}
else
{
val = pdf_button_field_on_state(ctx, field);
}
pdf_dict_put(ctx, grp, PDF_NAME(V), val);
set_check_grp(ctx, doc, grp, val);
doc->recalculate = 1;
end_annot_op(ctx, annot);
}
fz_catch(ctx)
{
abandon_annot_op(ctx, annot);
fz_rethrow(ctx);
}
pdf_set_annot_has_changed(ctx, annot);
}
int pdf_has_unsaved_changes(fz_context *ctx, pdf_document *doc)
{
int i;
if (doc->num_incremental_sections == 0)
return 0;
for (i = 0; i < doc->xref_sections->num_objects; i++)
if (doc->xref_sections->subsec->table[i].type != 0)
break;
return i != doc->xref_sections->num_objects;
}
int pdf_was_repaired(fz_context *ctx, pdf_document *doc)
{
return doc->repair_attempted;
}
int pdf_toggle_widget(fz_context *ctx, pdf_annot *widget)
{
switch (pdf_widget_type(ctx, widget))
{
default:
return 0;
case PDF_WIDGET_TYPE_CHECKBOX:
case PDF_WIDGET_TYPE_RADIOBUTTON:
toggle_check_box(ctx, widget);
return 1;
}
return 0;
}
int
pdf_update_page(fz_context *ctx, pdf_page *page)
{
pdf_annot *annot;
pdf_annot *widget;
int changed = 0;
fz_try(ctx)
{
pdf_begin_implicit_operation(ctx, page->doc);
if (page->doc->recalculate)
pdf_calculate_form(ctx, page->doc);
for (annot = page->annots; annot; annot = annot->next)
if (pdf_update_annot(ctx, annot))
changed = 1;
for (widget = page->widgets; widget; widget = widget->next)
if (pdf_update_annot(ctx, widget))
changed = 1;
pdf_end_operation(ctx, page->doc);
}
fz_catch(ctx)
{
pdf_abandon_operation(ctx, page->doc);
fz_rethrow(ctx);
}
return changed;
}
pdf_annot *pdf_first_widget(fz_context *ctx, pdf_page *page)
{
return page->widgets;
}
pdf_annot *pdf_next_widget(fz_context *ctx, pdf_annot *widget)
{
return widget->next;
}
enum pdf_widget_type pdf_widget_type(fz_context *ctx, pdf_annot *widget)
{
enum pdf_widget_type ret = PDF_WIDGET_TYPE_BUTTON;
pdf_annot_push_local_xref(ctx, widget);
fz_try(ctx)
{
pdf_obj *subtype = pdf_dict_get(ctx, widget->obj, PDF_NAME(Subtype));
if (pdf_name_eq(ctx, subtype, PDF_NAME(Widget)))
ret = pdf_field_type(ctx, widget->obj);
}
fz_always(ctx)
pdf_annot_pop_local_xref(ctx, widget);
fz_catch(ctx)
fz_rethrow(ctx);
return ret;
}
static int set_validated_field_value(fz_context *ctx, pdf_document *doc, pdf_obj *field, const char *text, int ignore_trigger_events)
{
char *newtext = NULL;
if (!ignore_trigger_events)
{
if (!pdf_field_event_validate(ctx, doc, field, text, &newtext))
return 0;
}
update_field_value(ctx, doc, field, newtext ? newtext : text);
fz_free(ctx, newtext);
return 1;
}
static void update_checkbox_selector(fz_context *ctx, pdf_document *doc, pdf_obj *field, const char *val)
{
pdf_obj *kids = pdf_dict_get(ctx, field, PDF_NAME(Kids));
if (kids)
{
int i, n = pdf_array_len(ctx, kids);
for (i = 0; i < n; i++)
update_checkbox_selector(ctx, doc, pdf_array_get(ctx, kids, i), val);
}
else
{
pdf_obj *n = pdf_dict_getp(ctx, field, "AP/N");
pdf_obj *oval;
if (pdf_dict_gets(ctx, n, val))
oval = pdf_new_name(ctx, val);
else
oval = PDF_NAME(Off);
pdf_dict_put_drop(ctx, field, PDF_NAME(AS), oval);
}
}
static int set_checkbox_value(fz_context *ctx, pdf_document *doc, pdf_obj *field, const char *val)
{
update_checkbox_selector(ctx, doc, field, val);
update_field_value(ctx, doc, field, val);
return 1;
}
int pdf_set_field_value(fz_context *ctx, pdf_document *doc, pdf_obj *field, const char *text, int ignore_trigger_events)
{
int accepted = 0;
switch (pdf_field_type(ctx, field))
{
case PDF_WIDGET_TYPE_TEXT:
case PDF_WIDGET_TYPE_COMBOBOX:
case PDF_WIDGET_TYPE_LISTBOX:
accepted = set_validated_field_value(ctx, doc, field, text, ignore_trigger_events);
break;
case PDF_WIDGET_TYPE_CHECKBOX:
case PDF_WIDGET_TYPE_RADIOBUTTON:
accepted = set_checkbox_value(ctx, doc, field, text);
break;
default:
update_field_value(ctx, doc, field, text);
accepted = 1;
break;
}
if (!ignore_trigger_events)
doc->recalculate = 1;
return accepted;
}
char *pdf_field_border_style(fz_context *ctx, pdf_obj *field)
{
const char *bs = pdf_to_name(ctx, pdf_dict_getl(ctx, field, PDF_NAME(BS), PDF_NAME(S), NULL));
switch (*bs)
{
case 'S': return "Solid";
case 'D': return "Dashed";
case 'B': return "Beveled";
case 'I': return "Inset";
case 'U': return "Underline";
}
return "Solid";
}
void pdf_field_set_border_style(fz_context *ctx, pdf_obj *field, const char *text)
{
pdf_obj *val;
if (!strcmp(text, "Solid"))
val = PDF_NAME(S);
else if (!strcmp(text, "Dashed"))
val = PDF_NAME(D);
else if (!strcmp(text, "Beveled"))
val = PDF_NAME(B);
else if (!strcmp(text, "Inset"))
val = PDF_NAME(I);
else if (!strcmp(text, "Underline"))
val = PDF_NAME(U);
else
return;
pdf_dict_putl_drop(ctx, field, val, PDF_NAME(BS), PDF_NAME(S), NULL);
pdf_field_mark_dirty(ctx, field);
}
void pdf_field_set_button_caption(fz_context *ctx, pdf_obj *field, const char *text)
{
if (pdf_field_type(ctx, field) == PDF_WIDGET_TYPE_BUTTON)
{
pdf_obj *val = pdf_new_text_string(ctx, text);
pdf_dict_putl_drop(ctx, field, val, PDF_NAME(MK), PDF_NAME(CA), NULL);
pdf_field_mark_dirty(ctx, field);
}
}
int pdf_field_display(fz_context *ctx, pdf_obj *field)
{
pdf_obj *kids;
int f, res = Display_Visible;
/* Base response on first of children. Not ideal,
* but not clear how to handle children with
* differing values */
while ((kids = pdf_dict_get(ctx, field, PDF_NAME(Kids))) != NULL)
field = pdf_array_get(ctx, kids, 0);
f = pdf_dict_get_int(ctx, field, PDF_NAME(F));
if (f & PDF_ANNOT_IS_HIDDEN)
{
res = Display_Hidden;
}
else if (f & PDF_ANNOT_IS_PRINT)
{
if (f & PDF_ANNOT_IS_NO_VIEW)
res = Display_NoView;
}
else
{
if (f & PDF_ANNOT_IS_NO_VIEW)
res = Display_Hidden;
else
res = Display_NoPrint;
}
return res;
}
/*
* get the field name in a char buffer that has spare room to
* add more characters at the end.
*/
static char *load_field_name(fz_context *ctx, pdf_obj *field, int spare, pdf_cycle_list *cycle_up)
{
pdf_cycle_list cycle;
char *res = NULL;
pdf_obj *parent;
const char *lname;
int llen;
if (pdf_cycle(ctx, &cycle, cycle_up, field))
fz_throw(ctx, FZ_ERROR_FORMAT, "Cycle in field parents");
parent = pdf_dict_get(ctx, field, PDF_NAME(Parent));
lname = pdf_dict_get_text_string(ctx, field, PDF_NAME(T));
llen = (int)strlen(lname);
// Limit fields to 16K
if (llen > (16 << 10) || llen + spare > (16 << 10))
fz_throw(ctx, FZ_ERROR_LIMIT, "Field name too long");
/*
* If we found a name at this point in the field hierarchy
* then we'll need extra space for it and a dot
*/
if (llen)
spare += llen+1;
if (parent)
{
res = load_field_name(ctx, parent, spare, &cycle);
}
else
{
res = Memento_label(fz_malloc(ctx, spare+1), "form_field_name");
res[0] = 0;
}
if (llen)
{
if (res[0])
strcat(res, ".");
strcat(res, lname);
}
return res;
}
char *pdf_load_field_name(fz_context *ctx, pdf_obj *field)
{
return load_field_name(ctx, field, 0, NULL);
}
void pdf_create_field_name(fz_context *ctx, pdf_document *doc, const char *prefix, char *buf, size_t len)
{
pdf_obj *form = pdf_dict_getl(ctx, pdf_trailer(ctx, doc),
PDF_NAME(Root), PDF_NAME(AcroForm), PDF_NAME(Fields), NULL);
int i;
for (i = 0; i < 65536; ++i) {
fz_snprintf(buf, len, "%s%d", prefix, i);
if (!pdf_lookup_field(ctx, form, buf))
return;
}
fz_throw(ctx, FZ_ERROR_LIMIT, "Could not create unique field name.");
}
const char *pdf_field_label(fz_context *ctx, pdf_obj *field)
{
pdf_obj *label = pdf_dict_get_inheritable(ctx, field, PDF_NAME(TU));
if (!label)
label = pdf_dict_get_inheritable(ctx, field, PDF_NAME(T));
if (label)
return pdf_to_text_string(ctx, label);
return "Unnamed";
}
void pdf_field_set_display(fz_context *ctx, pdf_obj *field, int d)
{
pdf_obj *kids = pdf_dict_get(ctx, field, PDF_NAME(Kids));
if (!kids)
{
int mask = (PDF_ANNOT_IS_HIDDEN|PDF_ANNOT_IS_PRINT|PDF_ANNOT_IS_NO_VIEW);
int f = pdf_dict_get_int(ctx, field, PDF_NAME(F)) & ~mask;
switch (d)
{
case Display_Visible:
f |= PDF_ANNOT_IS_PRINT;
break;
case Display_Hidden:
f |= PDF_ANNOT_IS_HIDDEN;
break;
case Display_NoView:
f |= (PDF_ANNOT_IS_PRINT|PDF_ANNOT_IS_NO_VIEW);
break;
case Display_NoPrint:
break;
}
pdf_dict_put_int(ctx, field, PDF_NAME(F), f);
}
else
{
int i, n = pdf_array_len(ctx, kids);
for (i = 0; i < n; i++)
pdf_field_set_display(ctx, pdf_array_get(ctx, kids, i), d);
}
}
void pdf_field_set_fill_color(fz_context *ctx, pdf_obj *field, pdf_obj *col)
{
/* col == NULL mean transparent, but we can simply pass it on as with
* non-NULL values because pdf_dict_putp interprets a NULL value as
* delete */
pdf_dict_putl(ctx, field, col, PDF_NAME(MK), PDF_NAME(BG), NULL);
pdf_field_mark_dirty(ctx, field);
}
void pdf_field_set_text_color(fz_context *ctx, pdf_obj *field, pdf_obj *col)
{
char buf[100];
const char *font;
float size, color[4];
/* TODO? */
const char *da = pdf_to_str_buf(ctx, pdf_dict_get_inheritable(ctx, field, PDF_NAME(DA)));
int n;
pdf_parse_default_appearance(ctx, da, &font, &size, &n, color);
switch (pdf_array_len(ctx, col))
{
default:
n = 0;
color[0] = color[1] = color[2] = color[3] = 0;
break;
case 1:
n = 1;
color[0] = pdf_array_get_real(ctx, col, 0);
break;
case 3:
n = 3;
color[0] = pdf_array_get_real(ctx, col, 0);
color[1] = pdf_array_get_real(ctx, col, 1);
color[2] = pdf_array_get_real(ctx, col, 2);
break;
case 4:
n = 4;
color[0] = pdf_array_get_real(ctx, col, 0);
color[1] = pdf_array_get_real(ctx, col, 1);
color[2] = pdf_array_get_real(ctx, col, 2);
color[3] = pdf_array_get_real(ctx, col, 3);
break;
}
pdf_print_default_appearance(ctx, buf, sizeof buf, font, size, n, color);
pdf_dict_put_string(ctx, field, PDF_NAME(DA), buf, strlen(buf));
pdf_field_mark_dirty(ctx, field);
}
pdf_annot *
pdf_keep_widget(fz_context *ctx, pdf_annot *widget)
{
return pdf_keep_annot(ctx, widget);
}
void
pdf_drop_widget(fz_context *ctx, pdf_annot *widget)
{
pdf_drop_annot(ctx, widget);
}
void
pdf_drop_widgets(fz_context *ctx, pdf_annot *widget)
{
while (widget)
{
pdf_annot *next = widget->next;
pdf_drop_widget(ctx, widget);
widget = next;
}
}
pdf_annot *
pdf_create_signature_widget(fz_context *ctx, pdf_page *page, char *name)
{
fz_rect rect = { 12, 12, 12+100, 12+50 };
pdf_annot *annot;
pdf_begin_operation(ctx, page->doc, "Create signature");
annot = pdf_create_annot_raw(ctx, page, PDF_ANNOT_WIDGET);
fz_try(ctx)
{
pdf_obj *obj = annot->obj;
pdf_obj *root = pdf_dict_get(ctx, pdf_trailer(ctx, page->doc), PDF_NAME(Root));
pdf_obj *acroform = pdf_dict_get(ctx, root, PDF_NAME(AcroForm));
pdf_obj *fields, *lock;
if (!acroform)
{
acroform = pdf_new_dict(ctx, page->doc, 1);
pdf_dict_put_drop(ctx, root, PDF_NAME(AcroForm), acroform);
}
fields = pdf_dict_get(ctx, acroform, PDF_NAME(Fields));
if (!fields)
{
fields = pdf_new_array(ctx, page->doc, 1);
pdf_dict_put_drop(ctx, acroform, PDF_NAME(Fields), fields);
}
pdf_set_annot_rect(ctx, annot, rect);
pdf_dict_put(ctx, obj, PDF_NAME(FT), PDF_NAME(Sig));
pdf_dict_put_int(ctx, obj, PDF_NAME(F), PDF_ANNOT_IS_PRINT);
pdf_dict_put_text_string(ctx, obj, PDF_NAME(DA), "/Helv 0 Tf 0 g");
pdf_dict_put_text_string(ctx, obj, PDF_NAME(T), name);
pdf_array_push(ctx, fields, obj);
lock = pdf_dict_put_dict(ctx, obj, PDF_NAME(Lock), 1);
pdf_dict_put(ctx, lock, PDF_NAME(Action), PDF_NAME(All));
pdf_end_operation(ctx, page->doc);
}
fz_catch(ctx)
{
pdf_abandon_operation(ctx, page->doc);
pdf_delete_annot(ctx, page, annot);
}
return (pdf_annot *)annot;
}
fz_rect
pdf_bound_widget(fz_context *ctx, pdf_annot *widget)
{
return pdf_bound_annot(ctx, widget);
}
int
pdf_update_widget(fz_context *ctx, pdf_annot *widget)
{
return pdf_update_annot(ctx, widget);
}
int pdf_text_widget_max_len(fz_context *ctx, pdf_annot *tw)
{
pdf_annot *annot = (pdf_annot *)tw;
return pdf_dict_get_inheritable_int(ctx, annot->obj, PDF_NAME(MaxLen));
}
int pdf_text_widget_format(fz_context *ctx, pdf_annot *tw)
{
pdf_annot *annot = (pdf_annot *)tw;
int type = PDF_WIDGET_TX_FORMAT_NONE;
pdf_obj *js = pdf_dict_getl(ctx, annot->obj, PDF_NAME(AA), PDF_NAME(F), PDF_NAME(JS), NULL);
if (js)
{
char *code = pdf_load_stream_or_string_as_utf8(ctx, js);
if (strstr(code, "AFNumber_Format"))
type = PDF_WIDGET_TX_FORMAT_NUMBER;
else if (strstr(code, "AFSpecial_Format"))
type = PDF_WIDGET_TX_FORMAT_SPECIAL;
else if (strstr(code, "AFDate_FormatEx"))
type = PDF_WIDGET_TX_FORMAT_DATE;
else if (strstr(code, "AFTime_FormatEx"))
type = PDF_WIDGET_TX_FORMAT_TIME;
fz_free(ctx, code);
}
return type;
}
static char *
merge_changes(fz_context *ctx, const char *value, int start, int end, const char *change)
{
int changelen = change ? (int)strlen(change) : 0;
int valuelen = value ? (int)strlen(value) : 0;
int prelen = (start >= 0 ? (start < valuelen ? start : valuelen) : 0);
int postlen = (end >= 0 && end <= valuelen ? valuelen - end : 0);
int newlen = prelen + changelen + postlen + 1;
char *merged = fz_malloc(ctx, newlen);
char *m = merged;
if (prelen)
{
memcpy(m, value, prelen);
m += prelen;
}
if (changelen)
{
memcpy(m, change, changelen);
m += changelen;
}
if (postlen)
{
memcpy(m, &value[end], postlen);
m += postlen;
}
*m = 0;
return merged;
}
int pdf_set_text_field_value(fz_context *ctx, pdf_annot *widget, const char *update)
{
pdf_document *doc;
pdf_keystroke_event evt = { 0 };
char *new_change = NULL;
char *new_value = NULL;
char *merged_value = NULL;
int rc = 1;
begin_annot_op(ctx, widget, "Edit text field");
doc = widget->page->doc;
fz_var(new_value);
fz_var(new_change);
fz_var(merged_value);
fz_try(ctx)
{
if (!widget->ignore_trigger_events)
{
evt.value = pdf_annot_field_value(ctx, widget);
evt.change = update;
evt.selStart = 0;
evt.selEnd = (int)strlen(evt.value);
evt.willCommit = 0;
rc = pdf_annot_field_event_keystroke(ctx, doc, widget, &evt);
new_change = evt.newChange;
new_value = evt.newValue;
evt.newValue = NULL;
evt.newChange = NULL;
if (rc)
{
merged_value = merge_changes(ctx, new_value, evt.selStart, evt.selEnd, new_change);
evt.value = merged_value;
evt.change = "";
evt.selStart = -1;
evt.selEnd = -1;
evt.willCommit = 1;
rc = pdf_annot_field_event_keystroke(ctx, doc, widget, &evt);
if (rc)
rc = pdf_set_annot_field_value(ctx, doc, widget, evt.newValue, 0);
}
}
else
{
rc = pdf_set_annot_field_value(ctx, doc, widget, update, 1);
}
end_annot_op(ctx, widget);
}
fz_always(ctx)
{
fz_free(ctx, new_value);
fz_free(ctx, evt.newValue);
fz_free(ctx, new_change);
fz_free(ctx, evt.newChange);
fz_free(ctx, merged_value);
}
fz_catch(ctx)
{
abandon_annot_op(ctx, widget);
fz_warn(ctx, "could not set widget text");
rc = 0;
}
return rc;
}
int pdf_edit_text_field_value(fz_context *ctx, pdf_annot *widget, const char *value, const char *change, int *selStart, int *selEnd, char **result)
{
pdf_document *doc = widget->page->doc;
pdf_keystroke_event evt = {0};
int rc = 1;
pdf_begin_operation(ctx, doc, "Text field keystroke");
fz_try(ctx)
{
if (!widget->ignore_trigger_events)
{
evt.value = value;
evt.change = change;
evt.selStart = *selStart;
evt.selEnd = *selEnd;
evt.willCommit = 0;
rc = pdf_annot_field_event_keystroke(ctx, doc, widget, &evt);
if (rc)
{
*result = merge_changes(ctx, evt.newValue, evt.selStart, evt.selEnd, evt.newChange);
*selStart = evt.selStart + (int)strlen(evt.newChange);
*selEnd = *selStart;
}
}
else
{
*result = merge_changes(ctx, value, *selStart, *selEnd, change);
*selStart = evt.selStart + (int)strlen(change);
*selEnd = *selStart;
}
pdf_end_operation(ctx, doc);
}
fz_always(ctx)
{
fz_free(ctx, evt.newValue);
fz_free(ctx, evt.newChange);
}
fz_catch(ctx)
{
pdf_abandon_operation(ctx, doc);
fz_warn(ctx, "could not process text widget keystroke");
rc = 0;
}
return rc;
}
int pdf_set_choice_field_value(fz_context *ctx, pdf_annot *widget, const char *new_value)
{
/* Choice widgets use almost the same keystroke processing as text fields. */
return pdf_set_text_field_value(ctx, widget, new_value);
}
int pdf_choice_widget_options(fz_context *ctx, pdf_annot *tw, int exportval, const char *opts[])
{
pdf_annot *annot = (pdf_annot *)tw;
pdf_obj *optarr;
int i, n, m;
optarr = pdf_dict_get_inheritable(ctx, annot->obj, PDF_NAME(Opt));
n = pdf_array_len(ctx, optarr);
if (opts)
{
for (i = 0; i < n; i++)
{
m = pdf_array_len(ctx, pdf_array_get(ctx, optarr, i));
/* If it is a two element array, the second item is the one that we want if we want the listing value. */
if (m == 2)
if (exportval)
opts[i] = pdf_array_get_text_string(ctx, pdf_array_get(ctx, optarr, i), 0);
else
opts[i] = pdf_array_get_text_string(ctx, pdf_array_get(ctx, optarr, i), 1);
else
opts[i] = pdf_array_get_text_string(ctx, optarr, i);
}
}
return n;
}
int pdf_choice_field_option_count(fz_context *ctx, pdf_obj *field)
{
pdf_obj *opt = pdf_dict_get_inheritable(ctx, field, PDF_NAME(Opt));
return pdf_array_len(ctx, opt);
}
const char *pdf_choice_field_option(fz_context *ctx, pdf_obj *field, int export, int i)
{
pdf_obj *opt = pdf_dict_get_inheritable(ctx, field, PDF_NAME(Opt));
pdf_obj *ent = pdf_array_get(ctx, opt, i);
if (pdf_array_len(ctx, ent) == 2)
return pdf_array_get_text_string(ctx, ent, export ? 0 : 1);
else
return pdf_to_text_string(ctx, ent);
}
int pdf_choice_widget_is_multiselect(fz_context *ctx, pdf_annot *tw)
{
pdf_annot *annot = (pdf_annot *)tw;
if (!annot) return 0;
switch (pdf_field_type(ctx, annot->obj))
{
case PDF_WIDGET_TYPE_LISTBOX:
return (pdf_field_flags(ctx, annot->obj) & PDF_CH_FIELD_IS_MULTI_SELECT) != 0;
default:
return 0;
}
}
int pdf_choice_widget_value(fz_context *ctx, pdf_annot *tw, const char *opts[])
{
pdf_annot *annot = (pdf_annot *)tw;
pdf_obj *optarr;
int i, n;
if (!annot)
return 0;
optarr = pdf_dict_get(ctx, annot->obj, PDF_NAME(V));
if (pdf_is_string(ctx, optarr))
{
if (opts)
opts[0] = pdf_to_text_string(ctx, optarr);
return 1;
}
else
{
n = pdf_array_len(ctx, optarr);
if (opts)
{
for (i = 0; i < n; i++)
{
pdf_obj *elem = pdf_array_get(ctx, optarr, i);
if (pdf_is_array(ctx, elem))
elem = pdf_array_get(ctx, elem, 1);
opts[i] = pdf_to_text_string(ctx, elem);
}
}
return n;
}
}
void pdf_choice_widget_set_value(fz_context *ctx, pdf_annot *tw, int n, const char *opts[])
{
pdf_annot *annot = (pdf_annot *)tw;
pdf_obj *optarr = NULL;
int i;
if (!annot)
return;
begin_annot_op(ctx, annot, "Set choice");
fz_var(optarr);
fz_try(ctx)
{
if (n != 1)
{
optarr = pdf_new_array(ctx, annot->page->doc, n);
for (i = 0; i < n; i++)
pdf_array_push_text_string(ctx, optarr, opts[i]);
pdf_dict_put_drop(ctx, annot->obj, PDF_NAME(V), optarr);
}
else
pdf_dict_put_text_string(ctx, annot->obj, PDF_NAME(V), opts[0]);
/* FIXME: when n > 1, we should be regenerating the indexes */
pdf_dict_del(ctx, annot->obj, PDF_NAME(I));
pdf_field_mark_dirty(ctx, annot->obj);
end_annot_op(ctx, annot);
}
fz_catch(ctx)
{
abandon_annot_op(ctx, annot);
pdf_drop_obj(ctx, optarr);
fz_rethrow(ctx);
}
}
int pdf_signature_byte_range(fz_context *ctx, pdf_document *doc, pdf_obj *signature, fz_range *byte_range)
{
pdf_obj *br = pdf_dict_getl(ctx, signature, PDF_NAME(V), PDF_NAME(ByteRange), NULL);
int i, n = pdf_array_len(ctx, br)/2;
if (byte_range)
{
for (i = 0; i < n; i++)
{
int64_t offset = pdf_array_get_int(ctx, br, 2*i);
int length = pdf_array_get_int(ctx, br, 2*i+1);
if (offset < 0 || offset > doc->file_size)
fz_throw(ctx, FZ_ERROR_FORMAT, "offset of signature byte range outside of file");
else if (length < 0)
fz_throw(ctx, FZ_ERROR_FORMAT, "length of signature byte range negative");
else if (offset + length > doc->file_size)
fz_throw(ctx, FZ_ERROR_FORMAT, "signature byte range extends past end of file");
byte_range[i].offset = offset;
byte_range[i].length = length;
}
}
return n;
}
static int is_white(int c)
{
return c == '\x00' || c == '\x09' || c == '\x0a' || c == '\x0c' || c == '\x0d' || c == '\x20';
}
static int is_hex_or_white(int c)
{
return (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f') || (c >= '0' && c <= '9') || is_white(c);
}
static void validate_certificate_data(fz_context *ctx, pdf_document *doc, fz_range *hole)
{
fz_stream *stm;
int c;
stm = fz_open_range_filter(ctx, doc->file, hole, 1);
fz_try(ctx)
{
while (is_white((c = fz_read_byte(ctx, stm))))
;
if (c == '<')
c = fz_read_byte(ctx, stm);
while (is_hex_or_white(c))
c = fz_read_byte(ctx, stm);
if (c == '>')
c = fz_read_byte(ctx, stm);
while (is_white(c))
c = fz_read_byte(ctx, stm);
if (c != EOF)
fz_throw(ctx, FZ_ERROR_FORMAT, "signature certificate data contains invalid character");
if ((size_t)fz_tell(ctx, stm) != hole->length)
fz_throw(ctx, FZ_ERROR_FORMAT, "premature end of signature certificate data");
}
fz_always(ctx)
fz_drop_stream(ctx, stm);
fz_catch(ctx)
fz_rethrow(ctx);
}
static int rangecmp(const void *a_, const void *b_)
{
const fz_range *a = (const fz_range *) a_;
const fz_range *b = (const fz_range *) b_;
return (int) (a->offset - b->offset);
}
static void validate_byte_ranges(fz_context *ctx, pdf_document *doc, fz_range *unsorted, int nranges)
{
int64_t offset = 0;
fz_range *sorted;
int i;
sorted = fz_calloc(ctx, nranges, sizeof(*sorted));
memcpy(sorted, unsorted, nranges * sizeof(*sorted));
qsort(sorted, nranges, sizeof(*sorted), rangecmp);
fz_try(ctx)
{
offset = 0;
for (i = 0; i < nranges; i++)
{
if (sorted[i].offset > offset)
{
fz_range hole;
hole.offset = offset;
hole.length = sorted[i].offset - offset;
validate_certificate_data(ctx, doc, &hole);
}
offset = fz_maxi64(offset, sorted[i].offset + sorted[i].length);
}
}
fz_always(ctx)
fz_free(ctx, sorted);
fz_catch(ctx)
fz_rethrow(ctx);
}
fz_stream *pdf_signature_hash_bytes(fz_context *ctx, pdf_document *doc, pdf_obj *signature)
{
fz_range *byte_range = NULL;
int byte_range_len;
fz_stream *bytes = NULL;
fz_var(byte_range);
fz_try(ctx)
{
byte_range_len = pdf_signature_byte_range(ctx, doc, signature, NULL);
if (byte_range_len)
{
byte_range = fz_calloc(ctx, byte_range_len, sizeof(*byte_range));
pdf_signature_byte_range(ctx, doc, signature, byte_range);
}
validate_byte_ranges(ctx, doc, byte_range, byte_range_len);
bytes = fz_open_range_filter(ctx, doc->file, byte_range, byte_range_len);
}
fz_always(ctx)
{
fz_free(ctx, byte_range);
}
fz_catch(ctx)
{
fz_rethrow(ctx);
}
return bytes;
}
int pdf_incremental_change_since_signing_widget(fz_context *ctx, pdf_annot *widget)
{
if (!widget->page)
fz_throw(ctx, FZ_ERROR_ARGUMENT, "annotation not bound to any page");
return pdf_signature_incremental_change_since_signing(ctx, widget->page->doc, widget->obj);
}
int pdf_signature_incremental_change_since_signing(fz_context *ctx, pdf_document *doc, pdf_obj *signature)
{
fz_range *byte_range = NULL;
int byte_range_len;
int changed = 0;
if (pdf_dict_get_inheritable(ctx, signature, PDF_NAME(FT)) != PDF_NAME(Sig))
fz_throw(ctx, FZ_ERROR_ARGUMENT, "annotation is not a signature widget");
if (!pdf_signature_is_signed(ctx, doc, signature))
return 0;
fz_var(byte_range);
fz_try(ctx)
{
byte_range_len = pdf_signature_byte_range(ctx, doc, signature, NULL);
if (byte_range_len)
{
fz_range *last_range;
int64_t end_of_range;
byte_range = fz_calloc(ctx, byte_range_len, sizeof(*byte_range));
pdf_signature_byte_range(ctx, doc, signature, byte_range);
last_range = &byte_range[byte_range_len -1];
end_of_range = last_range->offset + last_range->length;
/* We can see how long the document was when signed by inspecting the byte
* ranges of the signature. The document, when read in, may have already
* had changes tagged on to it, past its extent when signed, or we may have
* made changes since reading it, which will be held in a new incremental
* xref section. */
if (doc->file_size > end_of_range || doc->num_incremental_sections > 0)
changed = 1;
}
}
fz_always(ctx)
{
fz_free(ctx, byte_range);
}
fz_catch(ctx)
{
fz_rethrow(ctx);
}
return changed;
}
int pdf_signature_is_signed(fz_context *ctx, pdf_document *doc, pdf_obj *field)
{
pdf_obj *v;
pdf_obj* vtype;
if (pdf_dict_get_inheritable(ctx, field, PDF_NAME(FT)) != PDF_NAME(Sig))
return 0;
/* Signatures can only be signed if the value is a dictionary,
* and if the value has a Type, it should be Sig. */
v = pdf_dict_get_inheritable(ctx, field, PDF_NAME(V));
vtype = pdf_dict_get(ctx, v, PDF_NAME(Type));
return pdf_is_dict(ctx, v) && (vtype ? pdf_name_eq(ctx, vtype, PDF_NAME(Sig)) : 1);
}
int pdf_widget_is_signed(fz_context *ctx, pdf_annot *widget)
{
if (widget == NULL)
return 0;
if (!widget->page)
fz_throw(ctx, FZ_ERROR_ARGUMENT, "annotation not bound to any page");
return pdf_signature_is_signed(ctx, widget->page->doc, widget->obj);
}
int pdf_widget_is_readonly(fz_context *ctx, pdf_annot *widget)
{
int fflags;
if (widget == NULL)
return 0;
fflags = pdf_field_flags(ctx, ((pdf_annot *) widget)->obj);
return fflags & PDF_FIELD_IS_READ_ONLY;
}
size_t pdf_signature_contents(fz_context *ctx, pdf_document *doc, pdf_obj *signature, char **contents)
{
pdf_obj *v_ref = pdf_dict_get_inheritable(ctx, signature, PDF_NAME(V));
pdf_obj *v_obj = pdf_load_unencrypted_object(ctx, doc, pdf_to_num(ctx, v_ref));
char *copy = NULL;
size_t len;
fz_var(copy);
fz_try(ctx)
{
pdf_obj *c = pdf_dict_get(ctx, v_obj, PDF_NAME(Contents));
char *s;
s = pdf_to_str_buf(ctx, c);
len = pdf_to_str_len(ctx, c);
if (contents)
{
copy = Memento_label(fz_malloc(ctx, len), "sig_contents");
memcpy(copy, s, len);
}
}
fz_always(ctx)
pdf_drop_obj(ctx, v_obj);
fz_catch(ctx)
{
fz_free(ctx, copy);
fz_rethrow(ctx);
}
if (contents)
*contents = copy;
return len;
}
static fz_xml_doc *load_xfa(fz_context *ctx, pdf_document *doc)
{
pdf_obj *xfa;
fz_buffer *buf = NULL;
fz_buffer *packet = NULL;
int i;
if (doc->xfa)
return doc->xfa; /* Already loaded, and present. */
xfa = pdf_dict_getp(ctx, pdf_trailer(ctx, doc), "Root/AcroForm/XFA");
if (!pdf_is_array(ctx, xfa) && !pdf_is_stream(ctx, xfa))
return NULL; /* No XFA */
fz_var(buf);
fz_var(packet);
fz_try(ctx)
{
if (pdf_is_stream(ctx, xfa))
{
/* Load entire XFA resource */
buf = pdf_load_stream(ctx, xfa);
}
else
{
/* Concatenate packets to create entire XFA resource */
buf = fz_new_buffer(ctx, 1024);
for(i = 0; i < pdf_array_len(ctx, xfa); ++i)
{
pdf_obj *ref = pdf_array_get(ctx, xfa, i);
if (pdf_is_stream(ctx, ref))
{
packet = pdf_load_stream(ctx, ref);
fz_append_buffer(ctx, buf, packet);
fz_drop_buffer(ctx, packet);
packet = NULL;
}
}
}
/* Parse and stow away XFA resource in document */
doc->xfa = fz_parse_xml(ctx, buf, 0);
}
fz_always(ctx)
{
fz_drop_buffer(ctx, packet);
fz_drop_buffer(ctx, buf);
}
fz_catch(ctx)
{
fz_rethrow(ctx);
}
return doc->xfa;
}
static fz_xml *
get_xfa_resource(fz_context *ctx, pdf_document *doc, const char *str)
{
fz_xml_doc *xfa;
xfa = load_xfa(ctx, doc);
if (!xfa)
return NULL;
return fz_xml_find_down(fz_xml_root(xfa), str);
}
static int
find_name_component(char **np, char **sp, char **ep)
{
char *n = *np;
char *s, *e;
int idx = 0;
if (*n == '.')
n++;
/* Find the next name we are looking for. */
s = e = n;
while (*e && *e != '[' && *e != '.')
e++;
/* So the next name is s..e */
n = e;
if (*n == '[')
{
n++;
while (*n >= '0' && *n <= '9')
idx = idx*10 + *n++ - '0';
while (*n && *n != ']')
n++;
if (*n == ']')
n++;
}
*np = n;
*sp = s;
*ep = e;
return idx;
}
static pdf_obj *
annot_from_name(fz_context *ctx, pdf_document *doc, const char *str)
{
pdf_obj *fields = pdf_dict_getp(ctx, pdf_trailer(ctx, doc), "Root/AcroForm/Fields");
if (strncmp(str, "xfa[0].", 7) == 0)
str += 7;
if (strncmp(str, "template[0].", 12) == 0)
str += 12;
return pdf_lookup_field(ctx, fields, str);
}
static pdf_obj *
get_locked_fields_from_xfa(fz_context *ctx, pdf_document *doc, pdf_obj *field)
{
char *name = pdf_load_field_name(ctx, field);
char *n = name;
const char *use;
fz_xml *node;
if (name == NULL)
return NULL;
fz_try(ctx)
{
node = get_xfa_resource(ctx, doc, "template");
do
{
char c, *s, *e;
int idx = 0;
char *key;
idx = find_name_component(&n, &s, &e);
/* We want the idx'th occurrence of s..e */
/* Hacky */
c = *e;
*e = 0;
key = *n ? "subform" : "field";
node = fz_xml_find_down_match(node, key, "name", s);
while (node && idx > 0)
{
node = fz_xml_find_next_match(node, key, "name", s);
idx--;
}
*e = c;
}
while (node && *n == '.');
}
fz_always(ctx)
fz_free(ctx, name);
fz_catch(ctx)
fz_rethrow(ctx);
if (node == NULL)
return NULL;
node = fz_xml_find_down(node, "ui");
node = fz_xml_find_down(node, "signature");
node = fz_xml_find_down(node, "manifest");
use = fz_xml_att(node, "use");
if (use == NULL)
return NULL;
if (*use == '#')
use++;
/* Now look for a variables entry in a subform that defines this. */
while (node)
{
fz_xml *variables, *manifest, *ref;
pdf_obj *arr;
/* Find the enclosing subform */
do {
node = fz_xml_up(node);
} while (node && strcmp(fz_xml_tag(node), "subform"));
/* Look for a variables within that. */
variables = fz_xml_find_down(node, "variables");
if (variables == NULL)
continue;
manifest = fz_xml_find_down_match(variables, "manifest", "id", use);
if (manifest == NULL)
continue;
arr = pdf_new_array(ctx, doc, 16);
fz_try(ctx)
{
ref = fz_xml_find_down(manifest, "ref");
while (ref)
{
const char *s = fz_xml_text(fz_xml_down(ref));
pdf_array_push(ctx, arr, annot_from_name(ctx, doc, s));
ref = fz_xml_find_next(ref, "ref");
}
}
fz_catch(ctx)
{
pdf_drop_obj(ctx, arr);
fz_rethrow(ctx);
}
return arr;
}
return NULL;
}
static void
lock_field(fz_context *ctx, pdf_obj *f)
{
int ff = pdf_dict_get_inheritable_int(ctx, f, PDF_NAME(Ff));
if ((ff & PDF_FIELD_IS_READ_ONLY) ||
!pdf_name_eq(ctx, pdf_dict_get(ctx, f, PDF_NAME(Type)), PDF_NAME(Annot)) ||
!pdf_name_eq(ctx, pdf_dict_get(ctx, f, PDF_NAME(Subtype)), PDF_NAME(Widget)))
return;
pdf_dict_put_int(ctx, f, PDF_NAME(Ff), ff | PDF_FIELD_IS_READ_ONLY);
}
static void
lock_xfa_locked_fields(fz_context *ctx, pdf_obj *a)
{
int i;
int len = pdf_array_len(ctx, a);
for (i = 0; i < len; i++)
{
lock_field(ctx, pdf_array_get(ctx, a, i));
}
}
void pdf_signature_set_value(fz_context *ctx, pdf_document *doc, pdf_obj *field, pdf_pkcs7_signer *signer, int64_t stime)
{
pdf_obj *v = NULL;
pdf_obj *o = NULL;
pdf_obj *r = NULL;
pdf_obj *t = NULL;
pdf_obj *a = NULL;
pdf_obj *b = NULL;
pdf_obj *l = NULL;
pdf_obj *indv;
int vnum;
size_t max_digest_size;
char *buf = NULL;
vnum = pdf_create_object(ctx, doc);
indv = pdf_new_indirect(ctx, doc, vnum, 0);
pdf_dict_put_drop(ctx, field, PDF_NAME(V), indv);
max_digest_size = signer->max_digest_size(ctx, signer);
fz_var(v);
fz_var(o);
fz_var(r);
fz_var(t);
fz_var(a);
fz_var(b);
fz_var(l);
fz_var(buf);
fz_try(ctx)
{
v = pdf_new_dict(ctx, doc, 4);
pdf_update_object(ctx, doc, vnum, v);
buf = fz_calloc(ctx, max_digest_size, 1);
/* Ensure that the /Filter entry is the first entry in the
dictionary after the digest contents since we look for
this tag when completing signatures in pdf-write.c in order
to generate the correct byte range. */
pdf_dict_put_array(ctx, v, PDF_NAME(ByteRange), 4);
pdf_dict_put_string(ctx, v, PDF_NAME(Contents), buf, max_digest_size);
pdf_dict_put(ctx, v, PDF_NAME(Filter), PDF_NAME(Adobe_PPKLite));
pdf_dict_put(ctx, v, PDF_NAME(SubFilter), PDF_NAME(adbe_pkcs7_detached));
pdf_dict_put(ctx, v, PDF_NAME(Type), PDF_NAME(Sig));
pdf_dict_put_date(ctx, v, PDF_NAME(M), stime);
o = pdf_dict_put_array(ctx, v, PDF_NAME(Reference), 1);
r = pdf_array_put_dict(ctx, o, 0, 4);
pdf_dict_put(ctx, r, PDF_NAME(Data), pdf_dict_get(ctx, pdf_trailer(ctx, doc), PDF_NAME(Root)));
pdf_dict_put(ctx, r, PDF_NAME(TransformMethod), PDF_NAME(FieldMDP));
pdf_dict_put(ctx, r, PDF_NAME(Type), PDF_NAME(SigRef));
t = pdf_dict_put_dict(ctx, r, PDF_NAME(TransformParams), 5);
l = pdf_dict_getp(ctx, field, "Lock/Action");
if (l)
{
a = pdf_dict_getp(ctx, field, "Lock/Fields");
}
else
{
/* Lock action wasn't specified so we need to encode an Include.
* Before we just use an empty array, check in the XFA for locking
* details. */
a = get_locked_fields_from_xfa(ctx, doc, field);
if (a)
lock_xfa_locked_fields(ctx, a);
/* If we don't get a result from the XFA, just encode an empty array
* (leave a == NULL), even if Lock/Fields exists because we don't really
* know what to do with the information if the action isn't defined. */
l = PDF_NAME(Include);
}
pdf_dict_put(ctx, t, PDF_NAME(Action), l);
if (pdf_name_eq(ctx, l, PDF_NAME(Include)) || pdf_name_eq(ctx, l, PDF_NAME(Exclude)))
{
/* For action Include and Exclude, we need to encode a Fields array */
if (!a)
{
/* If one wasn't defined or we chose to ignore it because no action
* was defined then use an empty one. */
b = pdf_new_array(ctx, doc, 0);
a = b;
}
pdf_dict_put_drop(ctx, t, PDF_NAME(Fields), pdf_copy_array(ctx, a));
}
pdf_dict_put(ctx, t, PDF_NAME(Type), PDF_NAME(TransformParams));
pdf_dict_put(ctx, t, PDF_NAME(V), PDF_NAME(1_2));
/* Record details within the document structure so that contents
* and byte_range can be updated with their correct values at
* saving time */
pdf_xref_store_unsaved_signature(ctx, doc, field, signer);
}
fz_always(ctx)
{
pdf_drop_obj(ctx, v);
pdf_drop_obj(ctx, b);
fz_free(ctx, buf);
}
fz_catch(ctx)
{
fz_rethrow(ctx);
}
}
void pdf_set_widget_editing_state(fz_context *ctx, pdf_annot *widget, int editing)
{
widget->ignore_trigger_events = editing;
}
int pdf_get_widget_editing_state(fz_context *ctx, pdf_annot *widget)
{
return widget->ignore_trigger_events;
}
static void pdf_execute_js_action(fz_context *ctx, pdf_document *doc, pdf_obj *target, const char *path, pdf_obj *js)
{
if (js)
{
char *code = pdf_load_stream_or_string_as_utf8(ctx, js);
int in_op = 0;
fz_var(in_op);
fz_try(ctx)
{
char buf[100];
fz_snprintf(buf, sizeof buf, "%d/%s", pdf_to_num(ctx, target), path);
pdf_begin_operation(ctx, doc, "Javascript Event");
in_op = 1;
pdf_js_execute(doc->js, buf, code, NULL);
pdf_end_operation(ctx, doc);
}
fz_always(ctx)
{
fz_free(ctx, code);
}
fz_catch(ctx)
{
if (in_op)
pdf_abandon_operation(ctx, doc);
fz_rethrow(ctx);
}
}
}
static void pdf_execute_action_imp(fz_context *ctx, pdf_document *doc, pdf_obj *target, const char *path, pdf_obj *action)
{
pdf_obj *S = pdf_dict_get(ctx, action, PDF_NAME(S));
if (pdf_name_eq(ctx, S, PDF_NAME(JavaScript)))
{
if (doc->js)
pdf_execute_js_action(ctx, doc, target, path, pdf_dict_get(ctx, action, PDF_NAME(JS)));
}
if (pdf_name_eq(ctx, S, PDF_NAME(ResetForm)))
{
pdf_obj *fields = pdf_dict_get(ctx, action, PDF_NAME(Fields));
int flags = pdf_dict_get_int(ctx, action, PDF_NAME(Flags));
pdf_reset_form(ctx, doc, fields, flags & 1);
}
}
static void pdf_execute_action_chain(fz_context *ctx, pdf_document *doc, pdf_obj *target, const char *path, pdf_obj *action, pdf_cycle_list *cycle_up)
{
pdf_cycle_list cycle;
pdf_obj *next;
if (pdf_cycle(ctx, &cycle, cycle_up, action))
fz_throw(ctx, FZ_ERROR_FORMAT, "cycle in action chain");
if (pdf_is_array(ctx, action))
{
int i, n = pdf_array_len(ctx, action);
for (i = 0; i < n; ++i)
pdf_execute_action_chain(ctx, doc, target, path, pdf_array_get(ctx, action, i), &cycle);
}
else
{
pdf_execute_action_imp(ctx, doc, target, path, action);
next = pdf_dict_get(ctx, action, PDF_NAME(Next));
if (next)
pdf_execute_action_chain(ctx, doc, target, path, next, &cycle);
}
}
static void pdf_execute_action(fz_context *ctx, pdf_document *doc, pdf_obj *target, const char *path)
{
pdf_obj *action = pdf_dict_getp_inheritable(ctx, target, path);
if (action)
pdf_execute_action_chain(ctx, doc, target, path, action, NULL);
}
void pdf_document_event_will_close(fz_context *ctx, pdf_document *doc)
{
pdf_execute_action(ctx, doc, pdf_trailer(ctx, doc), "Root/AA/WC");
}
void pdf_document_event_will_save(fz_context *ctx, pdf_document *doc)
{
pdf_execute_action(ctx, doc, pdf_trailer(ctx, doc), "Root/AA/WS");
}
void pdf_document_event_did_save(fz_context *ctx, pdf_document *doc)
{
pdf_execute_action(ctx, doc, pdf_trailer(ctx, doc), "Root/AA/DS");
}
void pdf_document_event_will_print(fz_context *ctx, pdf_document *doc)
{
pdf_execute_action(ctx, doc, pdf_trailer(ctx, doc), "Root/AA/WP");
}
void pdf_document_event_did_print(fz_context *ctx, pdf_document *doc)
{
pdf_execute_action(ctx, doc, pdf_trailer(ctx, doc), "Root/AA/DP");
}
void pdf_page_event_open(fz_context *ctx, pdf_page *page)
{
pdf_execute_action(ctx, page->doc, page->obj, "AA/O");
}
void pdf_page_event_close(fz_context *ctx, pdf_page *page)
{
pdf_execute_action(ctx, page->doc, page->obj, "AA/C");
}
static void
annot_execute_action(fz_context *ctx, pdf_annot *annot, const char *act)
{
begin_annot_op(ctx, annot, "JavaScript action");
fz_try(ctx)
{
pdf_execute_action(ctx, annot->page->doc, annot->obj, act);
end_annot_op(ctx, annot);
}
fz_catch(ctx)
{
abandon_annot_op(ctx, annot);
fz_rethrow(ctx);
}
}
void pdf_annot_event_enter(fz_context *ctx, pdf_annot *annot)
{
annot_execute_action(ctx, annot, "AA/E");
}
void pdf_annot_event_exit(fz_context *ctx, pdf_annot *annot)
{
annot_execute_action(ctx, annot, "AA/X");
}
void pdf_annot_event_down(fz_context *ctx, pdf_annot *annot)
{
annot_execute_action(ctx, annot, "AA/D");
}
void pdf_annot_event_up(fz_context *ctx, pdf_annot *annot)
{
pdf_obj *action;
begin_annot_op(ctx, annot, "JavaScript action");
fz_try(ctx)
{
action = pdf_dict_get(ctx, annot->obj, PDF_NAME(A));
if (action)
pdf_execute_action_chain(ctx, annot->page->doc, annot->obj, "A", action, NULL);
else
pdf_execute_action(ctx, annot->page->doc, annot->obj, "AA/U");
end_annot_op(ctx, annot);
}
fz_catch(ctx)
{
abandon_annot_op(ctx, annot);
fz_rethrow(ctx);
}
}
void pdf_annot_event_focus(fz_context *ctx, pdf_annot *annot)
{
annot_execute_action(ctx, annot, "AA/Fo");
}
void pdf_annot_event_blur(fz_context *ctx, pdf_annot *annot)
{
annot_execute_action(ctx, annot, "AA/Bl");
}
void pdf_annot_event_page_open(fz_context *ctx, pdf_annot *annot)
{
annot_execute_action(ctx, annot, "AA/PO");
}
void pdf_annot_event_page_close(fz_context *ctx, pdf_annot *annot)
{
annot_execute_action(ctx, annot, "AA/PC");
}
void pdf_annot_event_page_visible(fz_context *ctx, pdf_annot *annot)
{
annot_execute_action(ctx, annot, "AA/PV");
}
void pdf_annot_event_page_invisible(fz_context *ctx, pdf_annot *annot)
{
annot_execute_action(ctx, annot, "AA/PI");
}
int pdf_field_event_keystroke(fz_context *ctx, pdf_document *doc, pdf_obj *field, pdf_keystroke_event *evt)
{
pdf_js *js = doc->js;
if (js)
{
pdf_obj *action = pdf_dict_getp_inheritable(ctx, field, "AA/K/JS");
if (action)
{
pdf_js_event_init_keystroke(js, field, evt);
pdf_execute_js_action(ctx, doc, field, "AA/K/JS", action);
return pdf_js_event_result_keystroke(js, evt);
}
}
evt->newChange = fz_strdup(ctx, evt->change);
evt->newValue = fz_strdup(ctx, evt->value);
return 1;
}
int pdf_annot_field_event_keystroke(fz_context *ctx, pdf_document *doc, pdf_annot *annot, pdf_keystroke_event *evt)
{
int ret;
pdf_annot_push_local_xref(ctx, annot);
fz_try(ctx)
ret = pdf_field_event_keystroke(ctx, doc, annot->obj, evt);
fz_always(ctx)
pdf_annot_pop_local_xref(ctx, annot);
fz_catch(ctx)
fz_rethrow(ctx);
return ret;
}
char *pdf_field_event_format(fz_context *ctx, pdf_document *doc, pdf_obj *field)
{
pdf_js *js = doc->js;
if (js)
{
pdf_obj *action = pdf_dict_getp_inheritable(ctx, field, "AA/F/JS");
if (action)
{
const char *value = pdf_field_value(ctx, field);
pdf_js_event_init(js, field, value, 1);
pdf_execute_js_action(ctx, doc, field, "AA/F/JS", action);
return pdf_js_event_value(js);
}
}
return NULL;
}
int pdf_field_event_validate(fz_context *ctx, pdf_document *doc, pdf_obj *field, const char *value, char **newvalue)
{
pdf_js *js = doc->js;
*newvalue = NULL;
if (js)
{
pdf_obj *action = pdf_dict_getp_inheritable(ctx, field, "AA/V/JS");
if (action)
{
pdf_js_event_init(js, field, value, 1);
pdf_execute_js_action(ctx, doc, field, "AA/V/JS", action);
return pdf_js_event_result_validate(js, newvalue);
}
}
return 1;
}
void pdf_field_event_calculate(fz_context *ctx, pdf_document *doc, pdf_obj *field)
{
pdf_js *js = doc->js;
if (js)
{
pdf_obj *action = pdf_dict_getp_inheritable(ctx, field, "AA/C/JS");
if (action)
{
char *old_value = fz_strdup(ctx, pdf_field_value(ctx, field));
char *new_value = NULL;
fz_var(new_value);
fz_try(ctx)
{
pdf_js_event_init(js, field, old_value, 1);
pdf_execute_js_action(ctx, doc, field, "AA/C/JS", action);
if (pdf_js_event_result(js))
{
new_value = pdf_js_event_value(js);
if (strcmp(old_value, new_value))
pdf_set_field_value(ctx, doc, field, new_value, 0);
}
}
fz_always(ctx)
{
fz_free(ctx, old_value);
fz_free(ctx, new_value);
}
fz_catch(ctx)
fz_rethrow(ctx);
}
}
}
static void
count_sigs(fz_context *ctx, pdf_obj *field, void *arg, pdf_obj **ft)
{
int *n = (int *)arg;
if (!pdf_name_eq(ctx, pdf_dict_get(ctx, field, PDF_NAME(Type)), PDF_NAME(Annot)) ||
!pdf_name_eq(ctx, pdf_dict_get(ctx, field, PDF_NAME(Subtype)), PDF_NAME(Widget)) ||
!pdf_name_eq(ctx, *ft, PDF_NAME(Sig)))
return;
(*n)++;
}
static pdf_obj *ft_name[2] = { PDF_NAME(FT), NULL };
int pdf_count_signatures(fz_context *ctx, pdf_document *doc)
{
int n = 0;
pdf_obj *ft = NULL;
pdf_obj *form_fields = pdf_dict_getp(ctx, pdf_trailer(ctx, doc), "Root/AcroForm/Fields");
pdf_walk_tree(ctx, form_fields, PDF_NAME(Kids), count_sigs, NULL, &n, ft_name, &ft);
return n;
}
/*
* Bake interactive form fields into static content.
*/
static pdf_obj *get_annot_ap(fz_context *ctx, pdf_obj *annot)
{
pdf_obj *ap = pdf_dict_get(ctx, annot, PDF_NAME(AP));
pdf_obj *as = pdf_dict_get(ctx, annot, PDF_NAME(AS));
if (ap)
{
ap = pdf_dict_get(ctx, ap, PDF_NAME(N));
if (pdf_is_stream(ctx, ap))
return ap;
ap = pdf_dict_get(ctx, ap, as);
if (pdf_is_stream(ctx, ap))
return ap;
}
return NULL;
}
static fz_matrix get_annot_transform(fz_context *ctx, pdf_obj *annot, pdf_obj *ap)
{
float w, h, x, y;
fz_matrix transform;
fz_rect bbox;
fz_rect rect;
rect = pdf_dict_get_rect(ctx, annot, PDF_NAME(Rect));
bbox = pdf_dict_get_rect(ctx, ap, PDF_NAME(BBox));
transform = pdf_dict_get_matrix(ctx, ap, PDF_NAME(Matrix));
bbox = fz_transform_rect(bbox, transform);
w = (rect.x1 - rect.x0) / (bbox.x1 - bbox.x0);
h = (rect.y1 - rect.y0) / (bbox.y1 - bbox.y0);
x = rect.x0 - bbox.x0 * w;
y = rect.y0 - bbox.y0 * h;
return fz_make_matrix(w, 0, 0, h, x, y);
}
static void pdf_bake_annot(fz_context *ctx, fz_buffer *buf, pdf_document *doc, pdf_obj *page, pdf_obj *res_xobj, pdf_obj *annot)
{
fz_matrix m;
pdf_obj *ap;
char name[20];
ap = get_annot_ap(ctx, annot);
if (ap)
{
fz_snprintf(name, sizeof name, "Annot%d", pdf_to_num(ctx, annot));
pdf_dict_puts(ctx, res_xobj, name, ap);
pdf_dict_put(ctx, ap, PDF_NAME(Type), PDF_NAME(XObject));
pdf_dict_put(ctx, ap, PDF_NAME(Subtype), PDF_NAME(Form));
m = get_annot_transform(ctx, annot, ap);
fz_append_printf(ctx, buf,
"q\n%g %g %g %g %g %g cm\n/%s Do\nQ\n",
m.a, m.b, m.c, m.d, m.e, m.f,
name
);
}
}
static void pdf_bake_page(fz_context *ctx, pdf_document *doc, pdf_obj *page, int bake_annots, int bake_widgets)
{
pdf_obj *res;
pdf_obj *res_xobj;
pdf_obj *contents;
pdf_obj *new_contents = NULL;
pdf_obj *annots;
pdf_obj *annot;
pdf_obj *subtype;
pdf_obj *prologue = NULL;
fz_buffer *buf = NULL;
int prepend, append;
int i;
fz_var(buf);
fz_var(prologue);
fz_var(new_contents);
annots = pdf_dict_get(ctx, page, PDF_NAME(Annots));
if (pdf_array_len(ctx, annots) == 0)
return;
res = pdf_dict_get(ctx, page, PDF_NAME(Resources));
if (!res)
res = pdf_dict_put_dict(ctx, page, PDF_NAME(Resources), 4);
res_xobj = pdf_dict_get(ctx, res, PDF_NAME(XObject));
if (!res_xobj)
res_xobj = pdf_dict_put_dict(ctx, res, PDF_NAME(XObject), 8);
fz_try(ctx)
{
// Ensure that the graphics state is balanced.
contents = pdf_dict_get(ctx, page, PDF_NAME(Contents));
pdf_count_q_balance(ctx, doc, res, contents, &prepend, &append);
if (prepend)
{
// Prepend enough 'q' to ensure we can get back to initial state.
buf = fz_new_buffer(ctx, 1024);
while (prepend-- > 0)
fz_append_string(ctx, buf, "q\n");
prologue = pdf_add_stream(ctx, doc, buf, NULL, 0);
fz_drop_buffer(ctx, buf);
buf = NULL;
}
// Append enough 'Q' to get back to initial state.
buf = fz_new_buffer(ctx, 1024);
while (append-- > 0)
fz_append_string(ctx, buf, "Q\n");
for (i = 0; i < pdf_array_len(ctx, annots); )
{
annot = pdf_array_get(ctx, annots, i);
subtype = pdf_dict_get(ctx, annot, PDF_NAME(Subtype));
if (subtype == PDF_NAME(Link))
{
++i;
}
else if (subtype == PDF_NAME(Widget))
{
if (bake_widgets)
{
pdf_bake_annot(ctx, buf, doc, page, res_xobj, annot);
pdf_array_delete(ctx, annots, i);
}
else
{
++i;
}
}
else
{
if (bake_annots)
{
pdf_bake_annot(ctx, buf, doc, page, res_xobj, annot);
pdf_array_delete(ctx, annots, i);
}
else
{
++i;
}
}
}
if (!pdf_is_array(ctx, contents))
{
new_contents = pdf_new_array(ctx, doc, 10);
if (prologue)
pdf_array_push(ctx, new_contents, prologue);
if (contents)
pdf_array_push(ctx, new_contents, contents);
pdf_dict_put(ctx, page, PDF_NAME(Contents), new_contents);
pdf_drop_obj(ctx, new_contents);
contents = new_contents;
new_contents = NULL;
}
else if (prologue)
{
pdf_array_insert(ctx, contents, prologue, 0);
}
pdf_array_push_drop(ctx, contents, pdf_add_stream(ctx, doc, buf, NULL, 0));
}
fz_always(ctx)
{
fz_drop_buffer(ctx, buf);
pdf_drop_obj(ctx, prologue);
pdf_drop_obj(ctx, new_contents);
}
fz_catch(ctx)
{
fz_rethrow(ctx);
}
}
void pdf_bake_document(fz_context *ctx, pdf_document *doc, int bake_annots, int bake_widgets)
{
pdf_page *page = NULL;
pdf_annot *annot;
int i, n;
fz_var(page);
pdf_begin_operation(ctx, doc, "Bake interactive content");
fz_try(ctx)
{
n = pdf_count_pages(ctx, doc);
for (i = 0; i < n; ++i)
{
page = pdf_load_page(ctx, doc, i);
if (bake_annots)
for (annot = pdf_first_annot(ctx, page); annot; annot = pdf_next_annot(ctx, annot))
pdf_annot_request_synthesis(ctx, annot);
if (bake_widgets)
for (annot = pdf_first_widget(ctx, page); annot; annot = pdf_next_widget(ctx, annot))
pdf_annot_request_synthesis(ctx, annot);
pdf_update_page(ctx, page);
pdf_bake_page(ctx, doc, page->obj, bake_annots, bake_widgets);
fz_drop_page(ctx, (fz_page*)page);
page = NULL;
}
if (bake_widgets)
{
pdf_obj *trailer = pdf_trailer(ctx, doc);
pdf_obj *root = pdf_dict_get(ctx, trailer, PDF_NAME(Root));
pdf_dict_del(ctx, root, PDF_NAME(AcroForm));
}
pdf_end_operation(ctx, doc);
}
fz_always(ctx)
{
fz_drop_page(ctx, (fz_page*)page);
}
fz_catch(ctx)
{
pdf_abandon_operation(ctx, doc);
}
}
|