1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
|
/* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
Copyright (C) 2008-2019 by Jin-Hwan Cho, Matthias Franz, and Shunsaku Hirata,
the dvipdfmx project team.
Copyright (C) 1998, 1999 by Mark A. Wicks <mwicks@kettering.edu>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
/*
* TODO: Many things...
* {begin,end}_{bead,article}, box stack, name tree (not limited to dests)...
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "system.h"
#include "mem.h"
#include "error.h"
#include "mfileio.h"
#include "dpxconf.h"
#include "numbers.h"
#include "pdfobj.h"
#include "pdfparse.h"
#include "pdfnames.h"
#include "pdfencrypt.h"
#include "dvipdfmx.h"
#include "pdfdev.h"
#include "pdfdraw.h"
#include "pdfcolor.h"
#include "pdfresource.h"
#include "pdffont.h"
#include "pdfximage.h"
#include "pdflimits.h"
#if HAVE_LIBPNG
#include "pngimage.h"
#endif
#include "jpegimage.h"
#include "pdfdoc.h"
#define PDFDOC_PAGES_ALLOC_SIZE 128u
#define PDFDOC_ARTICLE_ALLOC_SIZE 16
#define PDFDOC_BEAD_ALLOC_SIZE 16
static char manual_thumb_enabled = 0;
static char *thumb_basename = NULL;
void
pdf_doc_enable_manual_thumbnails (void)
{
#if HAVE_LIBPNG
manual_thumb_enabled = 1;
#else
WARN("Manual thumbnail is not supported without the libpng library.");
#endif
}
static pdf_obj *
read_thumbnail (const char *thumb_filename)
{
pdf_obj *image_ref;
int xobj_id;
FILE *fp;
load_options options = {1, 0, NULL};
fp = MFOPEN(thumb_filename, FOPEN_RBIN_MODE);
if (!fp) {
WARN("Could not open thumbnail file \"%s\"", thumb_filename);
return NULL;
}
if (!check_for_png(fp) && !check_for_jpeg(fp)) {
WARN("Thumbnail \"%s\" not a png/jpeg file!", thumb_filename);
MFCLOSE(fp);
return NULL;
}
MFCLOSE(fp);
xobj_id = pdf_ximage_findresource(thumb_filename, options);
if (xobj_id < 0) {
WARN("Could not read thumbnail file \"%s\".", thumb_filename);
image_ref = NULL;
} else {
image_ref = pdf_ximage_get_reference(xobj_id);
}
return image_ref;
}
typedef struct pdf_form
{
char *ident;
pdf_tmatrix matrix;
pdf_rect cropbox;
pdf_obj *resources;
pdf_obj *contents;
} pdf_form;
struct form_list_node
{
int q_depth;
pdf_form form;
struct form_list_node *prev;
};
#define USE_MY_MEDIABOX (1 << 0)
typedef struct pdf_page
{
pdf_obj *page_obj;
pdf_obj *page_ref;
int flags;
double ref_x, ref_y;
pdf_rect cropbox;
pdf_obj *resources;
/* Contents */
pdf_obj *background;
pdf_obj *contents;
/* global bop, background, contents, global eop */
pdf_obj *content_refs[4];
pdf_obj *annots;
pdf_obj *beads;
} pdf_page;
typedef struct pdf_olitem
{
pdf_obj *dict;
int is_open;
struct pdf_olitem *first;
struct pdf_olitem *parent;
struct pdf_olitem *next;
} pdf_olitem;
typedef struct pdf_bead
{
char *id;
int page_no;
pdf_rect rect;
} pdf_bead;
typedef struct pdf_article
{
char *id;
pdf_obj *info;
int num_beads;
int max_beads;
pdf_bead *beads;
} pdf_article;
struct name_dict
{
const char *category;
struct ht_table *data;
};
typedef struct pdf_doc
{
struct {
pdf_obj *dict;
pdf_obj *viewerpref;
pdf_obj *pagelabels;
pdf_obj *pages;
pdf_obj *names;
pdf_obj *threads;
} root;
pdf_obj *info;
struct {
pdf_rect mediabox;
pdf_obj *bop, *eop;
int num_entries; /* This is not actually total number of pages. */
int max_entries;
pdf_page *entries;
} pages;
struct {
pdf_olitem *first;
pdf_olitem *current;
int current_depth;
} outlines;
struct {
int num_entries;
int max_entries;
pdf_article *entries;
} articles;
struct name_dict *names;
int check_gotos;
struct ht_table gotos;
struct {
int outline_open_depth;
double annot_grow;
} opt;
struct form_list_node *pending_forms;
} pdf_doc;
static pdf_doc pdoc;
static void
pdf_doc_init_catalog (pdf_doc *p)
{
p->root.viewerpref = NULL;
p->root.pagelabels = NULL;
p->root.pages = NULL;
p->root.names = NULL;
p->root.threads = NULL;
p->root.dict = pdf_new_dict();
pdf_set_root(p->root.dict);
return;
}
static void
pdf_doc_close_catalog (pdf_doc *p)
{
pdf_obj *tmp;
if (p->root.viewerpref) {
tmp = pdf_lookup_dict(p->root.dict, "ViewerPreferences");
if (!tmp) {
pdf_add_dict(p->root.dict,
pdf_new_name("ViewerPreferences"),
pdf_ref_obj (p->root.viewerpref));
} else if (PDF_OBJ_DICTTYPE(tmp)) {
pdf_merge_dict(p->root.viewerpref, tmp);
pdf_add_dict(p->root.dict,
pdf_new_name("ViewerPreferences"),
pdf_ref_obj (p->root.viewerpref));
} else { /* Maybe reference */
/* What should I do? */
WARN("Could not modify ViewerPreferences.");
}
pdf_release_obj(p->root.viewerpref);
p->root.viewerpref = NULL;
}
if (p->root.pagelabels) {
tmp = pdf_lookup_dict(p->root.dict, "PageLabels");
if (!tmp) {
tmp = pdf_new_dict();
pdf_add_dict(tmp, pdf_new_name("Nums"), pdf_link_obj(p->root.pagelabels));
pdf_add_dict(p->root.dict,
pdf_new_name("PageLabels"), pdf_ref_obj(tmp));
pdf_release_obj(tmp);
} else { /* Maybe reference */
/* What should I do? */
WARN("Could not modify PageLabels.");
}
pdf_release_obj(p->root.pagelabels);
p->root.pagelabels = NULL;
}
pdf_add_dict(p->root.dict,
pdf_new_name("Type"), pdf_new_name("Catalog"));
pdf_release_obj(p->root.dict);
p->root.dict = NULL;
return;
}
/*
* Pages are starting at 1.
* The page count does not increase until the page is finished.
*/
#define LASTPAGE(p) (&(p->pages.entries[p->pages.num_entries]))
#define FIRSTPAGE(p) (&(p->pages.entries[0]))
#define PAGECOUNT(p) (p->pages.num_entries)
#define MAXPAGES(p) (p->pages.max_entries)
static void
doc_resize_page_entries (pdf_doc *p, int size)
{
if (size > MAXPAGES(p)) {
int i;
p->pages.entries = RENEW(p->pages.entries, size, struct pdf_page);
for (i = p->pages.max_entries; i < size; i++) {
p->pages.entries[i].page_obj = NULL;
p->pages.entries[i].page_ref = NULL;
p->pages.entries[i].flags = 0;
p->pages.entries[i].resources = NULL;
p->pages.entries[i].background = NULL;
p->pages.entries[i].contents = NULL;
p->pages.entries[i].content_refs[0] = NULL; /* global bop */
p->pages.entries[i].content_refs[1] = NULL; /* background */
p->pages.entries[i].content_refs[2] = NULL; /* page body */
p->pages.entries[i].content_refs[3] = NULL; /* global eop */
p->pages.entries[i].annots = NULL;
p->pages.entries[i].beads = NULL;
}
p->pages.max_entries = size;
}
return;
}
static pdf_page *
doc_get_page_entry (pdf_doc *p, unsigned int page_no)
{
pdf_page *page;
if (page_no > 65535ul) {
ERROR("Page number %ul too large!", page_no);
} else if (page_no == 0) {
ERROR("Invalid Page number %ul.", page_no);
}
if (page_no > MAXPAGES(p)) {
doc_resize_page_entries(p, page_no + PDFDOC_PAGES_ALLOC_SIZE);
}
page = &(p->pages.entries[page_no - 1]);
return page;
}
static void pdf_doc_init_page_tree (pdf_doc *p, double media_width, double media_height);
static void pdf_doc_close_page_tree (pdf_doc *p);
static void pdf_doc_init_names (pdf_doc *p, int check_gotos);
static void pdf_doc_close_names (pdf_doc *p);
static void pdf_doc_add_goto (pdf_obj *annot_dict);
static void pdf_doc_init_docinfo (pdf_doc *p);
static void pdf_doc_close_docinfo (pdf_doc *p);
static void pdf_doc_init_articles (pdf_doc *p);
static void pdf_doc_close_articles (pdf_doc *p);
static void pdf_doc_init_bookmarks (pdf_doc *p, int bm_open_depth);
static void pdf_doc_close_bookmarks (pdf_doc *p);
void
pdf_doc_set_bop_content (const char *content, unsigned length)
{
pdf_doc *p = &pdoc;
ASSERT(p);
if (p->pages.bop) {
pdf_release_obj(p->pages.bop);
p->pages.bop = NULL;
}
if (length > 0) {
p->pages.bop = pdf_new_stream(STREAM_COMPRESS);
pdf_add_stream(p->pages.bop, content, length);
} else {
p->pages.bop = NULL;
}
return;
}
void
pdf_doc_set_eop_content (const char *content, unsigned length)
{
pdf_doc *p = &pdoc;
if (p->pages.eop) {
pdf_release_obj(p->pages.eop);
p->pages.eop = NULL;
}
if (length > 0) {
p->pages.eop = pdf_new_stream(STREAM_COMPRESS);
pdf_add_stream(p->pages.eop, content, length);
} else {
p->pages.eop = NULL;
}
return;
}
static void
pdf_doc_init_docinfo (pdf_doc *p)
{
p->info = pdf_new_dict();
pdf_set_info(p->info);
return;
}
static void
pdf_doc_close_docinfo (pdf_doc *p)
{
pdf_obj *docinfo = p->info;
/*
* Excerpt from PDF Reference 4th ed., sec. 10.2.1.
*
* Any entry whose value is not known should be omitted from the dictionary,
* rather than included with an empty string as its value.
*
* ....
*
* Note: Although viewer applications can store custom metadata in the document
* information dictionary, it is inappropriate to store private content or
* structural information there; such information should be stored in the
* document catalog instead (see Section 3.6.1, Document Catalog ).
*/
const char *keys[] = {
"Title", "Author", "Subject", "Keywords", "Creator", "Producer",
"CreationDate", "ModDate", /* Date */
NULL
};
pdf_obj *value;
int i;
for (i = 0; keys[i] != NULL; i++) {
value = pdf_lookup_dict(docinfo, keys[i]);
if (value) {
if (!PDF_OBJ_STRINGTYPE(value)) {
WARN("\"%s\" in DocInfo dictionary not string type.", keys[i]);
pdf_remove_dict(docinfo, keys[i]);
WARN("\"%s\" removed from DocInfo.", keys[i]);
} else if (pdf_string_length(value) == 0) {
/* The hyperref package often uses emtpy strings. */
pdf_remove_dict(docinfo, keys[i]);
}
}
}
if (!pdf_lookup_dict(docinfo, "Producer")) {
char *banner;
banner = NEW(strlen(my_name)+strlen(VERSION)+4, char);
sprintf(banner, "%s (%s)", my_name, VERSION);
pdf_add_dict(docinfo,
pdf_new_name("Producer"),
pdf_new_string(banner, strlen(banner)));
RELEASE(banner);
}
if (!pdf_lookup_dict(docinfo, "CreationDate")) {
char now[32];
dpx_util_format_asn_date(now, 1);
pdf_add_dict(docinfo,
pdf_new_name ("CreationDate"),
pdf_new_string(now, strlen(now)));
}
pdf_release_obj(docinfo);
p->info = NULL;
return;
}
static pdf_obj *
pdf_doc_get_page_resources (pdf_doc *p, const char *category)
{
pdf_obj *resources;
pdf_page *currentpage;
pdf_obj *res_dict;
if (!p || !category) {
return NULL;
}
if (p->pending_forms) {
if (p->pending_forms->form.resources) {
res_dict = p->pending_forms->form.resources;
} else {
res_dict = p->pending_forms->form.resources = pdf_new_dict();
}
} else {
currentpage = LASTPAGE(p);
if (currentpage->resources) {
res_dict = currentpage->resources;
} else {
res_dict = currentpage->resources = pdf_new_dict();
}
}
resources = pdf_lookup_dict(res_dict, category);
if (!resources) {
resources = pdf_new_dict();
pdf_add_dict(res_dict, pdf_new_name(category), resources);
}
return resources;
}
void
pdf_doc_add_page_resource (const char *category,
const char *resource_name, pdf_obj *resource_ref)
{
pdf_doc *p = &pdoc;
pdf_obj *resources;
pdf_obj *duplicate;
if (!PDF_OBJ_INDIRECTTYPE(resource_ref)) {
WARN("Passed non indirect reference...");
resource_ref = pdf_ref_obj(resource_ref); /* leak */
}
resources = pdf_doc_get_page_resources(p, category);
duplicate = pdf_lookup_dict(resources, resource_name);
if (duplicate && pdf_compare_reference(duplicate, resource_ref)) {
WARN("Conflicting page resource found (page: %d, category: %s, name: %s).",
pdf_doc_current_page_number(), category, resource_name);
WARN("Ignoring...");
pdf_release_obj(resource_ref);
} else {
pdf_add_dict(resources, pdf_new_name(resource_name), resource_ref);
}
return;
}
static void
doc_flush_page (pdf_doc *p, pdf_page *page, pdf_obj *parent_ref)
{
pdf_obj *contents_array;
int count;
pdf_add_dict(page->page_obj,
pdf_new_name("Type"), pdf_new_name("Page"));
pdf_add_dict(page->page_obj,
pdf_new_name("Parent"), parent_ref);
/*
* Clipping area specified by CropBox is affected by MediaBox which
* might be inherit from parent node. If MediaBox of the root node
* does not have enough size to cover all page's imaging area, using
* CropBox here gives incorrect result.
*/
if (page->flags & USE_MY_MEDIABOX) {
pdf_obj *mediabox;
mediabox = pdf_new_array();
pdf_add_array(mediabox,
pdf_new_number(ROUND(page->cropbox.llx, 0.01)));
pdf_add_array(mediabox,
pdf_new_number(ROUND(page->cropbox.lly, 0.01)));
pdf_add_array(mediabox,
pdf_new_number(ROUND(page->cropbox.urx, 0.01)));
pdf_add_array(mediabox,
pdf_new_number(ROUND(page->cropbox.ury, 0.01)));
pdf_add_dict(page->page_obj, pdf_new_name("MediaBox"), mediabox);
}
count = 0;
contents_array = pdf_new_array();
if (page->content_refs[0]) { /* global bop */
pdf_add_array(contents_array, page->content_refs[0]);
count++;
} else if (p->pages.bop &&
pdf_stream_length(p->pages.bop) > 0) {
pdf_add_array(contents_array, pdf_ref_obj(p->pages.bop));
count++;
}
if (page->content_refs[1]) { /* background */
pdf_add_array(contents_array, page->content_refs[1]);
count++;
}
if (page->content_refs[2]) { /* page body */
pdf_add_array(contents_array, page->content_refs[2]);
count++;
}
if (page->content_refs[3]) { /* global eop */
pdf_add_array(contents_array, page->content_refs[3]);
count++;
} else if (p->pages.eop &&
pdf_stream_length(p->pages.eop) > 0) {
pdf_add_array(contents_array, pdf_ref_obj(p->pages.eop));
count++;
}
if (count == 0) {
WARN("Page with empty content found!!!");
}
page->content_refs[0] = NULL;
page->content_refs[1] = NULL;
page->content_refs[2] = NULL;
page->content_refs[3] = NULL;
pdf_add_dict(page->page_obj,
pdf_new_name("Contents"), contents_array);
if (page->annots) {
pdf_add_dict(page->page_obj,
pdf_new_name("Annots"), pdf_ref_obj(page->annots));
pdf_release_obj(page->annots);
}
if (page->beads) {
pdf_add_dict(page->page_obj,
pdf_new_name("B"), pdf_ref_obj(page->beads));
pdf_release_obj(page->beads);
}
pdf_release_obj(page->page_obj);
pdf_release_obj(page->page_ref);
page->page_obj = NULL;
page->page_ref = NULL;
page->annots = NULL;
page->beads = NULL;
return;
}
/* B-tree? */
#define PAGE_CLUSTER 4
static pdf_obj *
build_page_tree (pdf_doc *p,
pdf_page *firstpage, int num_pages,
pdf_obj *parent_ref)
{
pdf_obj *self, *self_ref, *kids;
int i;
self = pdf_new_dict();
/*
* This is a slight kludge which allow the subtree dictionary
* generated by this routine to be merged with the real
* page_tree dictionary, while keeping the indirect object
* references right.
*/
self_ref = parent_ref ? pdf_ref_obj(self) : pdf_ref_obj(p->root.pages);
pdf_add_dict(self, pdf_new_name("Type"), pdf_new_name("Pages"));
pdf_add_dict(self, pdf_new_name("Count"), pdf_new_number((double) num_pages));
if (parent_ref != NULL)
pdf_add_dict(self, pdf_new_name("Parent"), parent_ref);
kids = pdf_new_array();
if (num_pages > 0 && num_pages <= PAGE_CLUSTER) {
for (i = 0; i < num_pages; i++) {
pdf_page *page;
page = firstpage + i;
if (!page->page_ref)
page->page_ref = pdf_ref_obj(page->page_obj);
pdf_add_array (kids, pdf_link_obj(page->page_ref));
doc_flush_page(p, page, pdf_link_obj(self_ref));
}
} else if (num_pages > 0) {
for (i = 0; i < PAGE_CLUSTER; i++) {
int start, end;
start = (i*num_pages)/PAGE_CLUSTER;
end = ((i+1)*num_pages)/PAGE_CLUSTER;
if (end - start > 1) {
pdf_obj *subtree;
subtree = build_page_tree(p, firstpage + start, end - start,
pdf_link_obj(self_ref));
pdf_add_array(kids, pdf_ref_obj(subtree));
pdf_release_obj(subtree);
} else {
pdf_page *page;
page = firstpage + start;
if (!page->page_ref)
page->page_ref = pdf_ref_obj(page->page_obj);
pdf_add_array (kids, pdf_link_obj(page->page_ref));
doc_flush_page(p, page, pdf_link_obj(self_ref));
}
}
}
pdf_add_dict(self, pdf_new_name("Kids"), kids);
pdf_release_obj(self_ref);
return self;
}
static void
pdf_doc_init_page_tree (pdf_doc *p, double media_width, double media_height)
{
/*
* Create empty page tree.
* The docroot.pages is kept open until the document is closed.
* This allows the user to write to pages if he so choses.
*/
p->root.pages = pdf_new_dict();
p->pages.num_entries = 0;
p->pages.max_entries = 0;
p->pages.entries = NULL;
p->pages.bop = NULL;
p->pages.eop = NULL;
p->pages.mediabox.llx = 0.0;
p->pages.mediabox.lly = 0.0;
p->pages.mediabox.urx = media_width;
p->pages.mediabox.ury = media_height;
return;
}
static void
pdf_doc_close_page_tree (pdf_doc *p)
{
pdf_obj *page_tree_root;
pdf_obj *mediabox;
int page_no;
/*
* Do consistency check on forward references to pages.
*/
for (page_no = PAGECOUNT(p) + 1; page_no <= MAXPAGES(p); page_no++) {
pdf_page *page;
page = doc_get_page_entry(p, page_no);
if (page->page_obj) {
WARN("Nonexistent page #%ld refered.", page_no);
pdf_release_obj(page->page_ref);
page->page_ref = NULL;
}
if (page->page_obj) {
WARN("Entry for a nonexistent page #%ld created.", page_no);
pdf_release_obj(page->page_obj);
page->page_obj = NULL;
}
if (page->annots) {
WARN("Annotation attached to a nonexistent page #%ld.", page_no);
pdf_release_obj(page->annots);
page->annots = NULL;
}
if (page->beads) {
WARN("Article beads attached to a nonexistent page #%ld.", page_no);
pdf_release_obj(page->beads);
page->beads = NULL;
}
if (page->resources) {
pdf_release_obj(page->resources);
page->resources = NULL;
}
}
/*
* Connect page tree to root node.
*/
page_tree_root = build_page_tree(p, FIRSTPAGE(p), PAGECOUNT(p), NULL);
pdf_merge_dict (p->root.pages, page_tree_root);
pdf_release_obj(page_tree_root);
/* They must be after build_page_tree() */
if (p->pages.bop) {
pdf_add_stream (p->pages.bop, "\n", 1);
pdf_release_obj(p->pages.bop);
p->pages.bop = NULL;
}
if (p->pages.eop) {
pdf_add_stream (p->pages.eop, "\n", 1);
pdf_release_obj(p->pages.eop);
p->pages.eop = NULL;
}
/* Create media box at root node and let the other pages inherit it. */
mediabox = pdf_new_array();
pdf_add_array(mediabox, pdf_new_number(ROUND(p->pages.mediabox.llx, 0.01)));
pdf_add_array(mediabox, pdf_new_number(ROUND(p->pages.mediabox.lly, 0.01)));
pdf_add_array(mediabox, pdf_new_number(ROUND(p->pages.mediabox.urx, 0.01)));
pdf_add_array(mediabox, pdf_new_number(ROUND(p->pages.mediabox.ury, 0.01)));
pdf_add_dict(p->root.pages, pdf_new_name("MediaBox"), mediabox);
pdf_add_dict(p->root.dict,
pdf_new_name("Pages"),
pdf_ref_obj (p->root.pages));
pdf_release_obj(p->root.pages);
p->root.pages = NULL;
RELEASE(p->pages.entries);
p->pages.entries = NULL;
p->pages.num_entries = 0;
p->pages.max_entries = 0;
return;
}
int
pdf_doc_get_page_count (pdf_file *pf)
{
int count = 0;
pdf_obj *page_tree = NULL;
pdf_obj *catalog;
catalog = pdf_file_get_catalog(pf);
page_tree = pdf_deref_obj(pdf_lookup_dict(catalog, "Pages"));
if (!PDF_OBJ_DICTTYPE(page_tree)) {
return 0;
}
{
pdf_obj *tmp = pdf_deref_obj(pdf_lookup_dict(page_tree, "Count"));
if (!PDF_OBJ_NUMBERTYPE(tmp)) {
if (tmp)
pdf_release_obj(tmp);
return 0;
}
count = pdf_number_value(tmp);
pdf_release_obj(tmp);
}
return count;
}
static int
set_bounding_box (pdf_rect *bbox, enum pdf_page_boundary opt_bbox,
pdf_obj *media_box, pdf_obj *crop_box,
pdf_obj *art_box, pdf_obj *trim_box, pdf_obj *bleed_box)
{
pdf_obj *box = NULL;
if (!media_box) {
WARN("MediaBox not found in included PDF...");
return -1;
}
#define VALIDATE_BOX(o) if ((o)) {\
if (!PDF_OBJ_ARRAYTYPE((o)) || pdf_array_length((o)) != 4) \
return -1;\
}
VALIDATE_BOX(media_box);
VALIDATE_BOX(crop_box);
VALIDATE_BOX(art_box);
VALIDATE_BOX(trim_box);
VALIDATE_BOX(bleed_box);
if (opt_bbox == pdf_page_boundary__auto) {
if (crop_box)
box = pdf_link_obj(crop_box);
else if (art_box)
box = pdf_link_obj(art_box);
else if (trim_box)
box = pdf_link_obj(trim_box);
else if (bleed_box)
box = pdf_link_obj(bleed_box);
else {
box = pdf_link_obj(media_box);
}
} else {
if (!crop_box) {
crop_box = pdf_link_obj(media_box);
}
if (!art_box) {
art_box = pdf_link_obj(crop_box);
}
if (!trim_box) {
trim_box = pdf_link_obj(crop_box);
}
if (!bleed_box) {
bleed_box = pdf_link_obj(crop_box);
}
/* At this point all boxes must be defined. */
switch (opt_bbox) {
case pdf_page_boundary_cropbox:
box = pdf_link_obj(crop_box);
break;
case pdf_page_boundary_mediabox:
box = pdf_link_obj(media_box);
break;
case pdf_page_boundary_artbox:
box = pdf_link_obj(art_box);
break;
case pdf_page_boundary_trimbox:
box = pdf_link_obj(trim_box);
break;
case pdf_page_boundary_bleedbox:
box = pdf_link_obj(bleed_box);
break;
default:
box = pdf_link_obj(crop_box);
break;
}
}
if (!box) {
/* Impossible */
WARN("No appropriate page boudary box found???");
return -1;
} else {
int i;
for (i = 4; i--; ) {
double x;
pdf_obj *tmp = pdf_deref_obj(pdf_get_array(box, i));
if (!PDF_OBJ_NUMBERTYPE(tmp)) {
pdf_release_obj(tmp);
pdf_release_obj(box);
return -1;
}
x = pdf_number_value(tmp);
switch (i) {
case 0: bbox->llx = x; break;
case 1: bbox->lly = x; break;
case 2: bbox->urx = x; break;
case 3: bbox->ury = x; break;
}
pdf_release_obj(tmp);
}
/* New scheme only for XDV files */
if (dpx_conf.compat_mode == dpx_mode_xdv_mode ||
opt_bbox != pdf_page_boundary__auto) {
for (i = 4; i--; ) {
double x;
pdf_obj *tmp = pdf_deref_obj(pdf_get_array(media_box, i));
if (!PDF_OBJ_NUMBERTYPE(tmp)) {
pdf_release_obj(tmp);
pdf_release_obj(box);
return -1;
}
x = pdf_number_value(tmp);
switch (i) {
case 0: if (bbox->llx < x) bbox->llx = x; break;
case 1: if (bbox->lly < x) bbox->lly = x; break;
case 2: if (bbox->urx > x) bbox->urx = x; break;
case 3: if (bbox->ury > x) bbox->ury = x; break;
}
pdf_release_obj(tmp);
}
}
}
pdf_release_obj(box);
return 0;
}
static int
set_transform_matrix (pdf_tmatrix *matrix, pdf_rect *bbox, pdf_obj *rotate)
{
double deg;
int rot;
matrix->a = matrix->d = 1.0;
matrix->b = matrix->c = 0.0;
matrix->e = matrix->f = 0.0;
/* Handle Rotate */
if (rotate) {
if (!PDF_OBJ_NUMBERTYPE(rotate)) {
return -1;
} else {
deg = pdf_number_value(rotate);
if (deg - (int)deg != 0.0) {
WARN("Invalid value specified for /Rotate: %f", deg);
return -1;
} else if (deg != 0.0) {
rot = (int) deg;
if (rot % 90 != 0.0) {
WARN("Invalid value specified for /Rotate: %f", deg);
} else {
rot = rot % 360;
if (rot < 0) rot += 360;
switch (rot) {
case 90:
matrix->a = matrix->d = 0;
matrix->b = -1;
matrix->c = 1;
matrix->e = bbox->llx - bbox->lly;
matrix->f = bbox->lly + bbox->urx;
break;
case 180:
matrix->a = matrix->d = -1;
matrix->b = matrix->c = 0;
matrix->e = bbox->llx + bbox->urx;
matrix->f = bbox->lly + bbox->ury;
break;
case 270:
matrix->a = matrix->d = 0;
matrix->b = 1;
matrix->c = -1;
matrix->e = bbox->llx + bbox->ury;
matrix->f = bbox->lly - bbox->llx;
break;
default:
WARN("Invalid value specified for /Rotate: %f", deg);
break;
}
}
}
}
}
return 0;
}
/*
* From PDFReference15_v6.pdf (p.119 and p.834)
*
* MediaBox rectangle (Required; inheritable)
*
* The media box defines the boundaries of the physical medium on which the
* page is to be printed. It may include any extended area surrounding the
* finished page for bleed, printing marks, or other such purposes. It may
* also include areas close to the edges of the medium that cannot be marked
* because of physical limitations of the output device. Content falling
* outside this boundary can safely be discarded without affecting the
* meaning of the PDF file.
*
* CropBox rectangle (Optional; inheritable)
*
* The crop box defines the region to which the contents of the page are to be
* clipped (cropped) when displayed or printed. Unlike the other boxes, the
* crop box has no defined meaning in terms of physical page geometry or
* intended use; it merely imposes clipping on the page contents. However,
* in the absence of additional information (such as imposition instructions
* specified in a JDF or PJTF job ticket), the crop box will determine how
* the page's contents are to be positioned on the output medium. The default
* value is the page's media box.
*
* BleedBox rectangle (Optional; PDF 1.3)
*
* The bleed box (PDF 1.3) defines the region to which the contents of the
* page should be clipped when output in a production environment. This may
* include any extra "bleed area" needed to accommodate the physical
* limitations of cutting, folding, and trimming equipment. The actual printed
* page may include printing marks that fall outside the bleed box.
* The default value is the page's crop box.
*
* TrimBox rectangle (Optional; PDF 1.3)
*
* The trim box (PDF 1.3) defines the intended dimensions of the finished page
* after trimming. It may be smaller than the media box, to allow for
* production-related content such as printing instructions, cut marks, or
* color bars. The default value is the page's crop box.
*
* ArtBox rectangle (Optional; PDF 1.3)
*
* The art box (PDF 1.3) defines the extent of the page's meaningful content
* (including potential white space) as intended by the page's creator.
* The default value is the page's crop box.
*
* Rotate integer (Optional; inheritable)
*
* The number of degrees by which the page should be rotated clockwise when
* displayed or printed. The value must be a multiple of 90. Default value: 0.
*/
/* count_p removed: Please use different interface if you want to get total page
* number. pdf_doc_get_page() is obviously not an interface to do such.
*/
pdf_obj *
pdf_doc_get_page (pdf_file *pf,
int page_no, enum pdf_page_boundary opt_bbox, /* load options */
pdf_rect *bbox, pdf_tmatrix *matrix, /* returned value */
pdf_obj **resources_p /* returned values */
) {
pdf_obj *catalog = NULL, *page_tree = NULL;
pdf_obj *resources = NULL, *rotate = NULL;
pdf_obj *art_box = NULL, *trim_box = NULL, *bleed_box = NULL;
pdf_obj *media_box = NULL, *crop_box = NULL;
int error = 0;
catalog = pdf_file_get_catalog(pf);
page_tree = pdf_deref_obj(pdf_lookup_dict(catalog, "Pages"));
if (!PDF_OBJ_DICTTYPE(page_tree))
goto error_exit;
{
int count;
pdf_obj *tmp = pdf_deref_obj(pdf_lookup_dict(page_tree, "Count"));
if (!PDF_OBJ_NUMBERTYPE(tmp)) {
if (tmp)
pdf_release_obj(tmp);
goto error_exit;
}
count = pdf_number_value(tmp);
pdf_release_obj(tmp);
if (page_no <= 0 || page_no > count) {
WARN("Page %ld does not exist.", page_no);
goto error_silent;
}
}
/*
* Seek correct page. Get MediaBox, CropBox and Resources.
* (Note that these entries can be inherited.)
*/
{
pdf_obj *kids, *tmp;
int depth = PDF_OBJ_MAX_DEPTH;
int page_idx = page_no - 1, kids_length = 1, i = 0;
while (--depth && i != kids_length) {
if ((tmp = pdf_deref_obj(pdf_lookup_dict(page_tree, "MediaBox")))) {
if (media_box)
pdf_release_obj(media_box);
media_box = tmp;
}
if ((tmp = pdf_deref_obj(pdf_lookup_dict(page_tree, "CropBox")))) {
if (crop_box)
pdf_release_obj(crop_box);
crop_box = tmp;
}
if ((tmp = pdf_deref_obj(pdf_lookup_dict(page_tree, "ArtBox")))) {
if (art_box)
pdf_release_obj(art_box);
art_box = tmp;
}
if ((tmp = pdf_deref_obj(pdf_lookup_dict(page_tree, "TrimBox")))) {
if (trim_box)
pdf_release_obj(trim_box);
trim_box = tmp;
}
if ((tmp = pdf_deref_obj(pdf_lookup_dict(page_tree, "BleedBox")))) {
if (bleed_box)
pdf_release_obj(bleed_box);
bleed_box = tmp;
}
if ((tmp = pdf_deref_obj(pdf_lookup_dict(page_tree, "Rotate")))) {
if (rotate)
pdf_release_obj(rotate);
rotate = tmp;
}
if ((tmp = pdf_deref_obj(pdf_lookup_dict(page_tree, "Resources")))) {
if (resources)
pdf_release_obj(resources);
resources = tmp;
}
kids = pdf_deref_obj(pdf_lookup_dict(page_tree, "Kids"));
if (!kids)
break;
else if (!PDF_OBJ_ARRAYTYPE(kids)) {
pdf_release_obj(kids);
goto error_exit;
}
kids_length = pdf_array_length(kids);
for (i = 0; i < kids_length; i++) {
int count;
pdf_release_obj(page_tree);
page_tree = pdf_deref_obj(pdf_get_array(kids, i));
if (!PDF_OBJ_DICTTYPE(page_tree))
goto error_exit;
tmp = pdf_deref_obj(pdf_lookup_dict(page_tree, "Count"));
if (PDF_OBJ_NUMBERTYPE(tmp)) {
/* Pages object */
count = pdf_number_value(tmp);
pdf_release_obj(tmp);
} else if (!tmp)
/* Page object */
count = 1;
else {
pdf_release_obj(tmp);
goto error_exit;
}
if (page_idx < count)
break;
page_idx -= count;
}
pdf_release_obj(kids);
}
if (!depth || kids_length == i)
goto error_exit;
}
if (!PDF_OBJ_DICTTYPE(resources))
goto error_exit;
if (resources_p)
*resources_p = pdf_link_obj(resources);
/* Select page boundary box */
error = set_bounding_box(bbox, opt_bbox, media_box, crop_box, art_box, trim_box, bleed_box);
if (error)
goto error_exit;
/* Set transformation matrix */
error = set_transform_matrix(matrix, bbox, rotate);
if (error)
goto error_exit;
goto clean_exit; /* Success */
error_exit:
WARN("Error found in including PDF image.");
error_silent:
if (page_tree)
pdf_release_obj(page_tree);
page_tree = NULL;
clean_exit:
if (crop_box)
pdf_release_obj(crop_box);
if (bleed_box)
pdf_release_obj(bleed_box);
if (trim_box)
pdf_release_obj(trim_box);
if (art_box)
pdf_release_obj(art_box);
if (media_box)
pdf_release_obj(media_box);
if (rotate)
pdf_release_obj(rotate);
if (resources)
pdf_release_obj(resources);
return page_tree;
}
#ifndef BOOKMARKS_OPEN_DEFAULT
#define BOOKMARKS_OPEN_DEFAULT 0
#endif
static int clean_bookmarks (pdf_olitem *item);
static int flush_bookmarks (pdf_olitem *item,
pdf_obj *parent_ref,
pdf_obj *parent_dict);
static void
pdf_doc_init_bookmarks (pdf_doc *p, int bm_open_depth)
{
pdf_olitem *item;
#define MAX_OUTLINE_DEPTH 256u
p->opt.outline_open_depth =
((bm_open_depth >= 0) ?
bm_open_depth : MAX_OUTLINE_DEPTH - bm_open_depth);
p->outlines.current_depth = 1;
item = NEW(1, pdf_olitem);
item->dict = NULL;
item->next = NULL;
item->first = NULL;
item->parent = NULL;
item->is_open = 1;
p->outlines.current = item;
p->outlines.first = item;
return;
}
static int
clean_bookmarks (pdf_olitem *item)
{
pdf_olitem *next;
while (item) {
next = item->next;
if (item->dict)
pdf_release_obj(item->dict);
if (item->first)
clean_bookmarks(item->first);
RELEASE(item);
item = next;
}
return 0;
}
static int
flush_bookmarks (pdf_olitem *node,
pdf_obj *parent_ref, pdf_obj *parent_dict)
{
int retval;
int count;
pdf_olitem *item;
pdf_obj *this_ref, *prev_ref, *next_ref;
ASSERT(node->dict);
this_ref = pdf_ref_obj(node->dict);
pdf_add_dict(parent_dict,
pdf_new_name("First"), pdf_link_obj(this_ref));
retval = 0;
for (item = node, prev_ref = NULL;
item && item->dict; item = item->next) {
if (item->first && item->first->dict) {
count = flush_bookmarks(item->first, this_ref, item->dict);
if (item->is_open) {
pdf_add_dict(item->dict,
pdf_new_name("Count"),
pdf_new_number(count));
retval += count;
} else {
pdf_add_dict(item->dict,
pdf_new_name("Count"),
pdf_new_number(-count));
}
}
pdf_add_dict(item->dict,
pdf_new_name("Parent"),
pdf_link_obj(parent_ref));
if (prev_ref) {
pdf_add_dict(item->dict,
pdf_new_name("Prev"),
prev_ref);
}
if (item->next && item->next->dict) {
next_ref = pdf_ref_obj(item->next->dict);
pdf_add_dict(item->dict,
pdf_new_name("Next"),
pdf_link_obj(next_ref));
} else {
next_ref = NULL;
}
pdf_release_obj(item->dict);
item->dict = NULL;
prev_ref = this_ref;
this_ref = next_ref;
retval++;
}
pdf_add_dict(parent_dict,
pdf_new_name("Last"),
pdf_link_obj(prev_ref));
pdf_release_obj(prev_ref);
pdf_release_obj(node->dict);
node->dict = NULL;
return retval;
}
int
pdf_doc_bookmarks_up (void)
{
pdf_doc *p = &pdoc;
pdf_olitem *parent, *item;
item = p->outlines.current;
if (!item || !item->parent) {
WARN("Can't go up above the bookmark root node!");
return -1;
}
parent = item->parent;
item = parent->next;
if (!parent->next) {
parent->next = item = NEW(1, pdf_olitem);
item->dict = NULL;
item->first = NULL;
item->next = NULL;
item->is_open = 0;
item->parent = parent->parent;
}
p->outlines.current = item;
p->outlines.current_depth--;
return 0;
}
int
pdf_doc_bookmarks_down (void)
{
pdf_doc *p = &pdoc;
pdf_olitem *item, *first;
item = p->outlines.current;
if (!item->dict) {
pdf_obj *tcolor, *action;
WARN("Empty bookmark node!");
WARN("You have tried to jump more than 1 level.");
item->dict = pdf_new_dict();
#define TITLE_STRING "<No Title>"
pdf_add_dict(item->dict,
pdf_new_name("Title"),
pdf_new_string(TITLE_STRING, strlen(TITLE_STRING)));
tcolor = pdf_new_array();
pdf_add_array(tcolor, pdf_new_number(1.0));
pdf_add_array(tcolor, pdf_new_number(0.0));
pdf_add_array(tcolor, pdf_new_number(0.0));
pdf_add_dict (item->dict,
pdf_new_name("C"), pdf_link_obj(tcolor));
pdf_release_obj(tcolor);
pdf_add_dict (item->dict,
pdf_new_name("F"), pdf_new_number(1.0));
#define JS_CODE "app.alert(\"The author of this document made this bookmark item empty!\", 3, 0)"
action = pdf_new_dict();
pdf_add_dict(action,
pdf_new_name("S"), pdf_new_name("JavaScript"));
pdf_add_dict(action,
pdf_new_name("JS"), pdf_new_string(JS_CODE, strlen(JS_CODE)));
pdf_add_dict(item->dict,
pdf_new_name("A"), pdf_link_obj(action));
pdf_release_obj(action);
}
item->first = first = NEW(1, pdf_olitem);
first->dict = NULL;
first->is_open = 0;
first->parent = item;
first->next = NULL;
first->first = NULL;
p->outlines.current = first;
p->outlines.current_depth++;
return 0;
}
int
pdf_doc_bookmarks_depth (void)
{
pdf_doc *p = &pdoc;
return p->outlines.current_depth;
}
void
pdf_doc_bookmarks_add (pdf_obj *dict, int is_open)
{
pdf_doc *p = &pdoc;
pdf_olitem *item, *next;
ASSERT(p && dict);
item = p->outlines.current;
if (!item) {
item = NEW(1, pdf_olitem);
item->parent = NULL;
p->outlines.first = item;
} else if (item->dict) { /* go to next item */
item = item->next;
}
#define BMOPEN(b,p) (((b) < 0) ? (((p)->outlines.current_depth > (p)->opt.outline_open_depth) ? 0 : 1) : (b))
#if 0
item->dict = pdf_link_obj(dict);
#endif
item->dict = dict;
item->first = NULL;
item->is_open = BMOPEN(is_open, p);
item->next = next = NEW(1, pdf_olitem);
next->dict = NULL;
next->parent = item->parent;
next->first = NULL;
next->is_open = -1;
next->next = NULL;
p->outlines.current = item;
pdf_doc_add_goto(dict);
return;
}
static void
pdf_doc_close_bookmarks (pdf_doc *p)
{
pdf_obj *catalog = p->root.dict;
pdf_olitem *item;
int count;
pdf_obj *bm_root, *bm_root_ref;
item = p->outlines.first;
if (item->dict) {
bm_root = pdf_new_dict();
bm_root_ref = pdf_ref_obj(bm_root);
count = flush_bookmarks(item, bm_root_ref, bm_root);
pdf_add_dict(bm_root,
pdf_new_name("Count"),
pdf_new_number(count));
pdf_add_dict(catalog,
pdf_new_name("Outlines"),
bm_root_ref);
pdf_release_obj(bm_root);
}
clean_bookmarks(item);
p->outlines.first = NULL;
p->outlines.current = NULL;
p->outlines.current_depth = 0;
return;
}
static const char *name_dict_categories[] = {
"Dests", "AP", "JavaScript", "Pages",
"Templates", "IDS", "URLS", "EmbeddedFiles",
"AlternatePresentations", "Renditions"
};
#define NUM_NAME_CATEGORY (sizeof(name_dict_categories)/sizeof(name_dict_categories[0]))
static void
pdf_doc_init_names (pdf_doc *p, int check_gotos)
{
int i;
p->root.names = NULL;
p->names = NEW(NUM_NAME_CATEGORY + 1, struct name_dict);
for (i = 0; i < NUM_NAME_CATEGORY; i++) {
p->names[i].category = name_dict_categories[i];
p->names[i].data = strcmp(name_dict_categories[i], "Dests") ?
NULL : pdf_new_name_tree();
/*
* We need a non-null entry for PDF destinations in order to find
* broken links even if no destination is defined in the DVI file.
*/
}
p->names[NUM_NAME_CATEGORY].category = NULL;
p->names[NUM_NAME_CATEGORY].data = NULL;
p->check_gotos = check_gotos;
ht_init_table(&p->gotos, (void (*) (void *)) pdf_release_obj);
return;
}
int
pdf_doc_add_names (const char *category,
const void *key, int keylen, pdf_obj *value)
{
pdf_doc *p = &pdoc;
int i;
for (i = 0; p->names[i].category != NULL; i++) {
if (!strcmp(p->names[i].category, category)) {
break;
}
}
if (p->names[i].category == NULL) {
WARN("Unknown name dictionary category \"%s\".", category);
return -1;
}
if (!p->names[i].data) {
p->names[i].data = pdf_new_name_tree();
}
return pdf_names_add_object(p->names[i].data, key, keylen, value);
}
static void
pdf_doc_add_goto (pdf_obj *annot_dict)
{
pdf_obj *subtype = NULL, *A = NULL, *S = NULL, *D = NULL, *D_new, *dict;
const char *dest, *key;
int destlen = 0;
if (!pdoc.check_gotos)
return;
/*
* An annotation dictionary coming from an annotation special
* must have a "Subtype". An annotation dictionary coming from
* an outline special has none.
*/
subtype = pdf_deref_obj(pdf_lookup_dict(annot_dict, "Subtype"));
if (subtype) {
if (PDF_OBJ_UNDEFINED(subtype))
goto undefined;
else if (!PDF_OBJ_NAMETYPE(subtype))
goto error;
else if (strcmp(pdf_name_value(subtype), "Link"))
goto cleanup;
}
dict = annot_dict;
key = "Dest";
D = pdf_deref_obj(pdf_lookup_dict(annot_dict, key));
if (PDF_OBJ_UNDEFINED(D))
goto undefined;
A = pdf_deref_obj(pdf_lookup_dict(annot_dict, "A"));
if (A) {
if (PDF_OBJ_UNDEFINED(A))
goto undefined;
else if (D || !PDF_OBJ_DICTTYPE(A))
goto error;
else {
S = pdf_deref_obj(pdf_lookup_dict(A, "S"));
if (PDF_OBJ_UNDEFINED(S))
goto undefined;
else if (!PDF_OBJ_NAMETYPE(S))
goto error;
else if (strcmp(pdf_name_value(S), "GoTo"))
goto cleanup;
dict = A;
key = "D";
D = pdf_deref_obj(pdf_lookup_dict(A, key));
}
}
if (PDF_OBJ_STRINGTYPE(D)) {
dest = (char *) pdf_string_value(D);
destlen = pdf_string_length(D);
}
#if 0
/* Names as destinations are not supported by dvipdfmx */
else if (PDF_OBJ_NAMETYPE(D))
dest = pdf_name_value(D);
#endif
else if (PDF_OBJ_ARRAYTYPE(D))
goto cleanup;
else if (PDF_OBJ_UNDEFINED(D))
goto undefined;
else
goto error;
D_new = ht_lookup_table(&pdoc.gotos, dest, destlen);
if (!D_new) {
char buf[10];
/* We use hexadecimal notation for our numeric destinations.
* Other bases (e.g., 10+26 or 10+2*26) would be more efficient.
*/
sprintf(buf, "%x", ht_table_size(&pdoc.gotos));
D_new = pdf_new_string(buf, strlen(buf));
ht_append_table(&pdoc.gotos, dest, destlen, D_new);
}
{
pdf_obj *key_obj = pdf_new_name(key);
if (!pdf_add_dict(dict, key_obj, pdf_link_obj(D_new)))
pdf_release_obj(key_obj);
}
cleanup:
if (subtype)
pdf_release_obj(subtype);
if (A)
pdf_release_obj(A);
if (S)
pdf_release_obj(S);
if (D)
pdf_release_obj(D);
return;
error:
WARN("Unknown PDF annotation format. Output file may be broken.");
goto cleanup;
undefined:
WARN("Cannot optimize PDF annotations. Output file may be broken."
" Please restart with option \"-C 0x10\"\n");
goto cleanup;
}
static void
warn_undef_dests (struct ht_table *dests, struct ht_table *gotos)
{
struct ht_iter iter;
if (ht_set_iter(gotos, &iter) < 0)
return;
do {
int keylen;
char *key = ht_iter_getkey(&iter, &keylen);
if (!ht_lookup_table(dests, key, keylen)) {
char *dest = NEW(keylen+1, char);
memcpy(dest, key, keylen);
dest[keylen] = 0;
WARN("PDF destination \"%s\" not defined.", dest);
RELEASE(dest);
}
} while (ht_iter_next(&iter) >= 0);
ht_clear_iter(&iter);
}
static void
pdf_doc_close_names (pdf_doc *p)
{
pdf_obj *tmp;
int i;
for (i = 0; p->names[i].category != NULL; i++) {
if (p->names[i].data) {
struct ht_table *data = p->names[i].data;
pdf_obj *name_tree;
int count;
if (!pdoc.check_gotos || strcmp(p->names[i].category, "Dests"))
name_tree = pdf_names_create_tree(data, &count, NULL);
else {
name_tree = pdf_names_create_tree(data, &count, &pdoc.gotos);
if (dpx_conf.verbose_level > 0 && count < data->count)
MESG("\nRemoved %ld unused PDF destinations\n", data->count-count);
if (count < pdoc.gotos.count)
warn_undef_dests(data, &pdoc.gotos);
}
if (name_tree) {
if (!p->root.names)
p->root.names = pdf_new_dict();
pdf_add_dict(p->root.names,
pdf_new_name(p->names[i].category),
pdf_ref_obj(name_tree));
pdf_release_obj(name_tree);
}
pdf_delete_name_tree(&p->names[i].data);
}
}
if (p->root.names) {
tmp = pdf_lookup_dict(p->root.dict, "Names");
if (!tmp) {
pdf_add_dict(p->root.dict,
pdf_new_name("Names"),
pdf_ref_obj (p->root.names));
} else if (PDF_OBJ_DICTTYPE(tmp)) {
pdf_merge_dict(p->root.names, tmp);
pdf_add_dict(p->root.dict,
pdf_new_name("Names"),
pdf_ref_obj (p->root.names));
} else { /* Maybe reference */
/* What should I do? */
WARN("Could not modify Names dictionary.");
}
pdf_release_obj(p->root.names);
p->root.names = NULL;
}
RELEASE(p->names);
p->names = NULL;
ht_clear_table(&p->gotos);
return;
}
static void pdf_doc_get_mediabox (unsigned page_no, pdf_rect *mediabox);
void
pdf_doc_add_annot (unsigned page_no, const pdf_rect *rect,
pdf_obj *annot_dict, int new_annot)
{
pdf_doc *p = &pdoc;
pdf_page *page;
pdf_obj *rect_array;
double xpos, ypos;
pdf_rect annbox;
page = doc_get_page_entry(p, page_no);
if (!page->annots)
page->annots = pdf_new_array();
{
pdf_rect mediabox;
pdf_doc_get_mediabox(page_no, &mediabox);
pdf_dev_get_coord(&xpos, &ypos);
annbox.llx = rect->llx - xpos; annbox.lly = rect->lly - ypos;
annbox.urx = rect->urx - xpos; annbox.ury = rect->ury - ypos;
if (annbox.llx < mediabox.llx || annbox.urx > mediabox.urx ||
annbox.lly < mediabox.lly || annbox.ury > mediabox.ury) {
WARN("Annotation out of page boundary.");
WARN("Current page's MediaBox: [%g %g %g %g]",
mediabox.llx, mediabox.lly, mediabox.urx, mediabox.ury);
WARN("Annotation: [%g %g %g %g]",
annbox.llx, annbox.lly, annbox.urx, annbox.ury);
WARN("Maybe incorrect paper size specified.");
}
if (annbox.llx > annbox.urx || annbox.lly > annbox.ury) {
WARN("Rectangle with negative width/height: [%g %g %g %g]",
annbox.llx, annbox.lly, annbox.urx, annbox.ury);
}
}
rect_array = pdf_new_array();
pdf_add_array(rect_array, pdf_new_number(ROUND(annbox.llx, 0.001)));
pdf_add_array(rect_array, pdf_new_number(ROUND(annbox.lly, 0.001)));
pdf_add_array(rect_array, pdf_new_number(ROUND(annbox.urx, 0.001)));
pdf_add_array(rect_array, pdf_new_number(ROUND(annbox.ury, 0.001)));
pdf_add_dict (annot_dict, pdf_new_name("Rect"), rect_array);
pdf_add_array(page->annots, pdf_ref_obj(annot_dict));
if (new_annot)
pdf_doc_add_goto(annot_dict);
return;
}
/*
* PDF Article Thread
*/
static void
pdf_doc_init_articles (pdf_doc *p)
{
p->root.threads = NULL;
p->articles.num_entries = 0;
p->articles.max_entries = 0;
p->articles.entries = NULL;
return;
}
void
pdf_doc_begin_article (const char *article_id, pdf_obj *article_info)
{
pdf_doc *p = &pdoc;
pdf_article *article;
if (article_id == NULL || strlen(article_id) == 0)
ERROR("Article thread without internal identifier.");
if (p->articles.num_entries >= p->articles.max_entries) {
p->articles.max_entries += PDFDOC_ARTICLE_ALLOC_SIZE;
p->articles.entries = RENEW(p->articles.entries,
p->articles.max_entries, struct pdf_article);
}
article = &(p->articles.entries[p->articles.num_entries]);
article->id = NEW(strlen(article_id)+1, char);
strcpy(article->id, article_id);
article->info = article_info;
article->num_beads = 0;
article->max_beads = 0;
article->beads = NULL;
p->articles.num_entries++;
return;
}
static pdf_bead *
find_bead (pdf_article *article, const char *bead_id)
{
pdf_bead *bead;
int i;
bead = NULL;
for (i = 0; i < article->num_beads; i++) {
if (!strcmp(article->beads[i].id, bead_id)) {
bead = &(article->beads[i]);
break;
}
}
return bead;
}
void
pdf_doc_add_bead (const char *article_id,
const char *bead_id, int page_no, const pdf_rect *rect)
{
pdf_doc *p = &pdoc;
pdf_article *article;
pdf_bead *bead;
int i;
if (!article_id) {
ERROR("No article identifier specified.");
}
article = NULL;
for (i = 0; i < p->articles.num_entries; i++) {
if (!strcmp(p->articles.entries[i].id, article_id)) {
article = &(p->articles.entries[i]);
break;
}
}
if (!article) {
ERROR("Specified article thread that doesn't exist.");
return;
}
bead = bead_id ? find_bead(article, bead_id) : NULL;
if (!bead) {
if (article->num_beads >= article->max_beads) {
article->max_beads += PDFDOC_BEAD_ALLOC_SIZE;
article->beads = RENEW(article->beads,
article->max_beads, struct pdf_bead);
for (i = article->num_beads; i < article->max_beads; i++) {
article->beads[i].id = NULL;
article->beads[i].page_no = -1;
}
}
bead = &(article->beads[article->num_beads]);
if (bead_id) {
bead->id = NEW(strlen(bead_id)+1, char);
strcpy(bead->id, bead_id);
} else {
bead->id = NULL;
}
article->num_beads++;
}
bead->rect.llx = rect->llx;
bead->rect.lly = rect->lly;
bead->rect.urx = rect->urx;
bead->rect.ury = rect->ury;
bead->page_no = page_no;
return;
}
static pdf_obj *
make_article (pdf_doc *p,
pdf_article *article,
const char **bead_ids, int num_beads,
pdf_obj *article_info)
{
pdf_obj *art_dict;
pdf_obj *first, *prev, *last;
int i, n;
if (!article)
return NULL;
art_dict = pdf_new_dict();
first = prev = last = NULL;
/*
* The bead_ids represents logical order of beads in an article thread.
* If bead_ids is not given, we create an article thread in the order of
* beads appeared.
*/
n = bead_ids ? num_beads : article->num_beads;
for (i = 0; i < n; i++) {
pdf_bead *bead;
bead = bead_ids ? find_bead(article, bead_ids[i]) : &(article->beads[i]);
if (!bead || bead->page_no < 0) {
continue;
}
last = pdf_new_dict();
if (prev == NULL) {
first = last;
pdf_add_dict(first,
pdf_new_name("T"), pdf_ref_obj(art_dict));
} else {
pdf_add_dict(prev,
pdf_new_name("N"), pdf_ref_obj(last));
pdf_add_dict(last,
pdf_new_name("V"), pdf_ref_obj(prev));
/* We must link first to last. */
if (prev != first)
pdf_release_obj(prev);
}
/* Realize bead now. */
{
pdf_page *page;
pdf_obj *rect;
page = doc_get_page_entry(p, bead->page_no);
if (!page->beads) {
page->beads = pdf_new_array();
}
pdf_add_dict(last, pdf_new_name("P"), pdf_link_obj(page->page_ref));
rect = pdf_new_array();
pdf_add_array(rect, pdf_new_number(ROUND(bead->rect.llx, 0.01)));
pdf_add_array(rect, pdf_new_number(ROUND(bead->rect.lly, 0.01)));
pdf_add_array(rect, pdf_new_number(ROUND(bead->rect.urx, 0.01)));
pdf_add_array(rect, pdf_new_number(ROUND(bead->rect.ury, 0.01)));
pdf_add_dict (last, pdf_new_name("R"), rect);
pdf_add_array(page->beads, pdf_ref_obj(last));
}
prev = last;
}
if (first && last) {
pdf_add_dict(last,
pdf_new_name("N"), pdf_ref_obj(first));
pdf_add_dict(first,
pdf_new_name("V"), pdf_ref_obj(last));
if (first != last) {
pdf_release_obj(last);
}
pdf_add_dict(art_dict,
pdf_new_name("F"), pdf_ref_obj(first));
/* If article_info is supplied, we override article->info. */
if (article_info) {
pdf_add_dict(art_dict,
pdf_new_name("I"), article_info);
} else if (article->info) {
pdf_add_dict(art_dict,
pdf_new_name("I"), pdf_ref_obj(article->info));
pdf_release_obj(article->info);
article->info = NULL; /* We do not write as object reference. */
}
pdf_release_obj(first);
} else {
pdf_release_obj(art_dict);
art_dict = NULL;
}
return art_dict;
}
static void
clean_article (pdf_article *article)
{
if (!article)
return;
if (article->beads) {
int i;
for (i = 0; i < article->num_beads; i++) {
if (article->beads[i].id)
RELEASE(article->beads[i].id);
}
RELEASE(article->beads);
article->beads = NULL;
}
if (article->id)
RELEASE(article->id);
article->id = NULL;
article->num_beads = 0;
article->max_beads = 0;
return;
}
static void
pdf_doc_close_articles (pdf_doc *p)
{
int i;
for (i = 0; i < p->articles.num_entries; i++) {
pdf_article *article;
article = &(p->articles.entries[i]);
if (article->beads) {
pdf_obj *art_dict;
art_dict = make_article(p, article, NULL, 0, NULL);
if (!p->root.threads) {
p->root.threads = pdf_new_array();
}
pdf_add_array(p->root.threads, pdf_ref_obj(art_dict));
pdf_release_obj(art_dict);
}
clean_article(article);
}
RELEASE(p->articles.entries);
p->articles.entries = NULL;
p->articles.num_entries = 0;
p->articles.max_entries = 0;
if (p->root.threads) {
pdf_add_dict(p->root.dict,
pdf_new_name("Threads"),
pdf_ref_obj (p->root.threads));
pdf_release_obj(p->root.threads);
p->root.threads = NULL;
}
return;
}
/* page_no = 0 for root page tree node. */
void
pdf_doc_set_mediabox (unsigned page_no, const pdf_rect *mediabox)
{
pdf_doc *p = &pdoc;
pdf_page *page;
if (page_no == 0) {
p->pages.mediabox.llx = mediabox->llx;
p->pages.mediabox.lly = mediabox->lly;
p->pages.mediabox.urx = mediabox->urx;
p->pages.mediabox.ury = mediabox->ury;
} else {
page = doc_get_page_entry(p, page_no);
page->cropbox.llx = mediabox->llx;
page->cropbox.lly = mediabox->lly;
page->cropbox.urx = mediabox->urx;
page->cropbox.ury = mediabox->ury;
page->flags |= USE_MY_MEDIABOX;
}
return;
}
static void
pdf_doc_get_mediabox (unsigned page_no, pdf_rect *mediabox)
{
pdf_doc *p = &pdoc;
pdf_page *page;
if (page_no == 0) {
mediabox->llx = p->pages.mediabox.llx;
mediabox->lly = p->pages.mediabox.lly;
mediabox->urx = p->pages.mediabox.urx;
mediabox->ury = p->pages.mediabox.ury;
} else {
page = doc_get_page_entry(p, page_no);
if (page->flags & USE_MY_MEDIABOX) {
mediabox->llx = page->cropbox.llx;
mediabox->lly = page->cropbox.lly;
mediabox->urx = page->cropbox.urx;
mediabox->ury = page->cropbox.ury;
} else {
mediabox->llx = p->pages.mediabox.llx;
mediabox->lly = p->pages.mediabox.lly;
mediabox->urx = p->pages.mediabox.urx;
mediabox->ury = p->pages.mediabox.ury;
}
}
return;
}
pdf_obj *
pdf_doc_current_page_resources (void)
{
pdf_obj *resources;
pdf_doc *p = &pdoc;
pdf_page *currentpage;
if (p->pending_forms) {
if (p->pending_forms->form.resources) {
resources = p->pending_forms->form.resources;
} else {
resources = p->pending_forms->form.resources = pdf_new_dict();
}
} else {
currentpage = LASTPAGE(p);
if (currentpage->resources) {
resources = currentpage->resources;
} else {
resources = currentpage->resources = pdf_new_dict();
}
}
return resources;
}
pdf_obj *
pdf_doc_get_dictionary (const char *category)
{
pdf_doc *p = &pdoc;
pdf_obj *dict = NULL;
ASSERT(category);
if (!strcmp(category, "Names")) {
if (!p->root.names)
p->root.names = pdf_new_dict();
dict = p->root.names;
} else if (!strcmp(category, "Pages")) {
if (!p->root.pages)
p->root.pages = pdf_new_dict();
dict = p->root.pages;
} else if (!strcmp(category, "Catalog")) {
if (!p->root.dict)
p->root.dict = pdf_new_dict();
dict = p->root.dict;
} else if (!strcmp(category, "Info")) {
if (!p->info)
p->info = pdf_new_dict();
dict = p->info;
} else if (!strcmp(category, "@THISPAGE")) {
/* Sorry for this... */
pdf_page *currentpage;
currentpage = LASTPAGE(p);
dict = currentpage->page_obj;
}
if (!dict) {
ERROR("Document dict. \"%s\" not exist. ", category);
}
return dict;
}
int
pdf_doc_current_page_number (void)
{
pdf_doc *p = &pdoc;
return (int) (PAGECOUNT(p) + 1);
}
pdf_obj *
pdf_doc_ref_page (unsigned page_no)
{
pdf_doc *p = &pdoc;
pdf_page *page;
page = doc_get_page_entry(p, page_no);
if (!page->page_obj) {
page->page_obj = pdf_new_dict();
page->page_ref = pdf_ref_obj(page->page_obj);
}
return pdf_link_obj(page->page_ref);
}
pdf_obj *
pdf_doc_get_reference (const char *category)
{
pdf_obj *ref = NULL;
int page_no;
ASSERT(category);
page_no = pdf_doc_current_page_number();
if (!strcmp(category, "@THISPAGE")) {
ref = pdf_doc_ref_page(page_no);
} else if (!strcmp(category, "@PREVPAGE")) {
if (page_no <= 1) {
ERROR("Reference to previous page, but no pages have been completed yet.");
}
ref = pdf_doc_ref_page(page_no - 1);
} else if (!strcmp(category, "@NEXTPAGE")) {
ref = pdf_doc_ref_page(page_no + 1);
}
if (!ref) {
ERROR("Reference to \"%s\" not exist. ", category);
}
return ref;
}
static void
pdf_doc_new_page (pdf_doc *p)
{
pdf_page *currentpage;
if (PAGECOUNT(p) >= MAXPAGES(p)) {
doc_resize_page_entries(p, MAXPAGES(p) + PDFDOC_PAGES_ALLOC_SIZE);
}
/*
* This is confusing. pdf_doc_finish_page() have increased page count!
*/
currentpage = LASTPAGE(p);
/* Was this page already instantiated by a forward reference to it? */
if (!currentpage->page_ref) {
currentpage->page_obj = pdf_new_dict();
currentpage->page_ref = pdf_ref_obj(currentpage->page_obj);
}
currentpage->background = NULL;
currentpage->contents = pdf_new_stream(STREAM_COMPRESS);
currentpage->resources = pdf_new_dict();
currentpage->annots = NULL;
currentpage->beads = NULL;
return;
}
/* This only closes contents and resources. */
static void
pdf_doc_finish_page (pdf_doc *p)
{
pdf_page *currentpage;
if (p->pending_forms) {
ERROR("A pending form XObject at the end of page.");
}
currentpage = LASTPAGE(p);
if (!currentpage->page_obj)
currentpage->page_obj = pdf_new_dict();
/*
* Make Contents array.
*/
/*
* Global BOP content stream.
* pdf_ref_obj() returns reference itself when the object is
* indirect reference, not reference to the indirect reference.
* We keep bop itself but not reference to it since it is
* expected to be small.
*/
if (p->pages.bop &&
pdf_stream_length(p->pages.bop) > 0) {
currentpage->content_refs[0] = pdf_ref_obj(p->pages.bop);
} else {
currentpage->content_refs[0] = NULL;
}
/*
* Current page background content stream.
*/
if (currentpage->background) {
if (pdf_stream_length(currentpage->background) > 0) {
currentpage->content_refs[1] = pdf_ref_obj(currentpage->background);
pdf_add_stream (currentpage->background, "\n", 1);
}
pdf_release_obj(currentpage->background);
currentpage->background = NULL;
} else {
currentpage->content_refs[1] = NULL;
}
/* Content body of current page */
currentpage->content_refs[2] = pdf_ref_obj(currentpage->contents);
pdf_add_stream (currentpage->contents, "\n", 1);
pdf_release_obj(currentpage->contents);
currentpage->contents = NULL;
/*
* Global EOP content stream.
*/
if (p->pages.eop &&
pdf_stream_length(p->pages.eop) > 0) {
currentpage->content_refs[3] = pdf_ref_obj(p->pages.eop);
} else {
currentpage->content_refs[3] = NULL;
}
/*
* Page resources.
*/
if (currentpage->resources) {
pdf_obj *procset;
/*
* ProcSet is obsolete in PDF-1.4 but recommended for compatibility.
*/
procset = pdf_new_array ();
pdf_add_array(procset, pdf_new_name("PDF"));
pdf_add_array(procset, pdf_new_name("Text"));
pdf_add_array(procset, pdf_new_name("ImageC"));
pdf_add_array(procset, pdf_new_name("ImageB"));
pdf_add_array(procset, pdf_new_name("ImageI"));
pdf_add_dict(currentpage->resources, pdf_new_name("ProcSet"), procset);
pdf_add_dict(currentpage->page_obj,
pdf_new_name("Resources"),
pdf_ref_obj(currentpage->resources));
pdf_release_obj(currentpage->resources);
currentpage->resources = NULL;
}
if (manual_thumb_enabled) {
char *thumb_filename;
pdf_obj *thumb_ref;
thumb_filename = NEW(strlen(thumb_basename)+7, char);
sprintf(thumb_filename, "%s.%ld",
thumb_basename, (p->pages.num_entries % 99999) + 1L);
thumb_ref = read_thumbnail(thumb_filename);
RELEASE(thumb_filename);
if (thumb_ref)
pdf_add_dict(currentpage->page_obj, pdf_new_name("Thumb"), thumb_ref);
}
p->pages.num_entries++;
return;
}
static pdf_color bgcolor = { 1, NULL, { 1.0 } };
void
pdf_doc_set_bgcolor (const pdf_color *color)
{
if (color)
pdf_color_copycolor(&bgcolor, color);
else { /* as clear... */
pdf_color_white(&bgcolor);
}
}
static void
doc_fill_page_background (pdf_doc *p)
{
pdf_page *currentpage;
pdf_rect r;
int cm;
pdf_obj *saved_content;
cm = pdf_dev_get_param(PDF_DEV_PARAM_COLORMODE);
if (!cm || pdf_color_is_white(&bgcolor)) {
return;
}
pdf_doc_get_mediabox(pdf_doc_current_page_number(), &r);
currentpage = LASTPAGE(p);
ASSERT(currentpage);
if (!currentpage->background)
currentpage->background = pdf_new_stream(STREAM_COMPRESS);
saved_content = currentpage->contents;
currentpage->contents = currentpage->background;
pdf_dev_gsave();
pdf_dev_set_nonstrokingcolor(&bgcolor);
pdf_dev_rectfill(r.llx, r.lly, r.urx - r.llx, r.ury - r.lly);
pdf_dev_grestore();
currentpage->contents = saved_content;
return;
}
void
pdf_doc_begin_page (double scale, double x_origin, double y_origin)
{
pdf_doc *p = &pdoc;
pdf_tmatrix M;
M.a = scale; M.b = 0.0;
M.c = 0.0 ; M.d = scale;
M.e = x_origin;
M.f = y_origin;
/* pdf_doc_new_page() allocates page content stream. */
pdf_doc_new_page(p);
pdf_dev_bop(&M);
return;
}
void
pdf_doc_end_page (void)
{
pdf_doc *p = &pdoc;
pdf_dev_eop();
doc_fill_page_background(p);
pdf_doc_finish_page(p);
return;
}
void
pdf_doc_add_page_content (const char *buffer, unsigned length)
{
pdf_doc *p = &pdoc;
pdf_page *currentpage;
if (p->pending_forms) {
pdf_add_stream(p->pending_forms->form.contents, buffer, length);
} else {
currentpage = LASTPAGE(p);
pdf_add_stream(currentpage->contents, buffer, length);
}
return;
}
void
pdf_open_document (const char *filename,
const char *creator, const unsigned char *id1, const unsigned char *id2,
struct pdf_setting settings)
{
pdf_doc *p = &pdoc;
if (settings.enable_encrypt)
pdf_init_encryption(settings.encrypt, id1);
pdf_out_init(filename, settings.enable_encrypt,
settings.object.enable_objstm, settings.object.enable_predictor);
pdf_files_init();
pdf_doc_init_catalog(p);
p->opt.annot_grow = settings.annot_grow_amount;
p->opt.outline_open_depth = settings.outline_open_depth;
pdf_init_resources();
pdf_init_colors();
pdf_init_fonts();
/* Thumbnail want this to be initialized... */
pdf_init_images();
pdf_doc_init_docinfo(p);
if (creator) {
pdf_add_dict(p->info,
pdf_new_name("Creator"),
pdf_new_string(creator, strlen(creator)));
}
pdf_doc_init_bookmarks(p, settings.outline_open_depth);
pdf_doc_init_articles (p);
pdf_doc_init_names (p, settings.check_gotos);
pdf_doc_init_page_tree(p, settings.media_width, settings.media_height);
pdf_doc_set_bgcolor(NULL);
if (settings.enable_encrypt) {
pdf_obj *encrypt = pdf_encrypt_obj();
pdf_set_encrypt(encrypt);
pdf_release_obj(encrypt);
}
if (id1 && id2) {
pdf_obj *id_obj = pdf_new_array();
pdf_add_array(id_obj, pdf_new_string(id1, 16));
pdf_add_array(id_obj, pdf_new_string(id2, 16));
pdf_set_id(id_obj);
}
/* Create a default name for thumbnail image files */
if (manual_thumb_enabled) {
if (strlen(filename) > 4 &&
!strncmp(".pdf", filename + strlen(filename) - 4, 4)) {
thumb_basename = NEW(strlen(filename)-4+1, char);
strncpy(thumb_basename, filename, strlen(filename)-4);
thumb_basename[strlen(filename)-4] = 0;
} else {
thumb_basename = NEW(strlen(filename)+1, char);
strcpy(thumb_basename, filename);
}
}
p->pending_forms = NULL;
pdf_init_device(settings.device.dvi2pts, settings.device.precision,
settings.device.ignore_colors);
return;
}
void
pdf_close_document (void)
{
pdf_doc *p = &pdoc;
pdf_close_device();
/*
* Following things were kept around so user can add dictionary items.
*/
pdf_doc_close_articles (p);
pdf_doc_close_names (p);
pdf_doc_close_bookmarks(p);
pdf_doc_close_page_tree(p);
pdf_doc_close_docinfo (p);
pdf_doc_close_catalog (p);
pdf_close_images();
pdf_close_fonts ();
pdf_close_colors();
pdf_close_resources(); /* Should be at last. */
pdf_files_close();
pdf_out_flush();
if (thumb_basename)
RELEASE(thumb_basename);
return;
}
/*
* All this routine does is give the form a name and add a unity scaling matrix.
* It fills in required fields. The caller must initialize the stream.
*/
static void
pdf_doc_make_xform (pdf_obj *xform,
pdf_rect *bbox,
pdf_tmatrix *matrix,
pdf_obj *resources,
pdf_obj *attrib)
{
pdf_obj *xform_dict;
pdf_obj *tmp;
xform_dict = pdf_stream_dict(xform);
pdf_add_dict(xform_dict,
pdf_new_name("Type"), pdf_new_name("XObject"));
pdf_add_dict(xform_dict,
pdf_new_name("Subtype"), pdf_new_name("Form"));
pdf_add_dict(xform_dict,
pdf_new_name("FormType"), pdf_new_number(1.0));
if (!bbox)
ERROR("No BoundingBox supplied.");
tmp = pdf_new_array();
pdf_add_array(tmp, pdf_new_number(ROUND(bbox->llx, .001)));
pdf_add_array(tmp, pdf_new_number(ROUND(bbox->lly, .001)));
pdf_add_array(tmp, pdf_new_number(ROUND(bbox->urx, .001)));
pdf_add_array(tmp, pdf_new_number(ROUND(bbox->ury, .001)));
pdf_add_dict(xform_dict, pdf_new_name("BBox"), tmp);
if (matrix) {
tmp = pdf_new_array();
pdf_add_array(tmp, pdf_new_number(ROUND(matrix->a, .00001)));
pdf_add_array(tmp, pdf_new_number(ROUND(matrix->b, .00001)));
pdf_add_array(tmp, pdf_new_number(ROUND(matrix->c, .00001)));
pdf_add_array(tmp, pdf_new_number(ROUND(matrix->d, .00001)));
pdf_add_array(tmp, pdf_new_number(ROUND(matrix->e, .001 )));
pdf_add_array(tmp, pdf_new_number(ROUND(matrix->f, .001 )));
pdf_add_dict(xform_dict, pdf_new_name("Matrix"), tmp);
}
if (attrib) {
pdf_merge_dict(xform_dict, attrib);
}
pdf_add_dict(xform_dict, pdf_new_name("Resources"), resources);
return;
}
/*
* begin_form_xobj creates an xobject with its "origin" at
* xpos and ypos that is clipped to the specified bbox. Note
* that the origin is not the lower left corner of the bbox.
*/
int
pdf_doc_begin_grabbing (const char *ident,
double ref_x, double ref_y, const pdf_rect *cropbox)
{
int xobj_id = -1;
pdf_doc *p = &pdoc;
pdf_form *form;
struct form_list_node *fnode;
xform_info info;
pdf_dev_push_gstate();
fnode = NEW(1, struct form_list_node);
fnode->prev = p->pending_forms;
fnode->q_depth = pdf_dev_current_depth();
form = &fnode->form;
/*
* The reference point of an Xobject is at the lower left corner
* of the bounding box. Since we would like to have an arbitrary
* reference point, we use a transformation matrix, translating
* the reference point to (0,0).
*/
form->matrix.a = 1.0; form->matrix.b = 0.0;
form->matrix.c = 0.0; form->matrix.d = 1.0;
form->matrix.e = -ref_x;
form->matrix.f = -ref_y;
form->cropbox.llx = ref_x + cropbox->llx;
form->cropbox.lly = ref_y + cropbox->lly;
form->cropbox.urx = ref_x + cropbox->urx;
form->cropbox.ury = ref_y + cropbox->ury;
form->contents = pdf_new_stream(STREAM_COMPRESS);
form->resources = pdf_new_dict();
pdf_ximage_init_form_info(&info);
info.matrix.a = 1.0; info.matrix.b = 0.0;
info.matrix.c = 0.0; info.matrix.d = 1.0;
info.matrix.e = -ref_x;
info.matrix.f = -ref_y;
info.bbox.llx = cropbox->llx;
info.bbox.lly = cropbox->lly;
info.bbox.urx = cropbox->urx;
info.bbox.ury = cropbox->ury;
/* Use reference since content itself isn't available yet. */
xobj_id = pdf_ximage_defineresource(ident,
PDF_XOBJECT_TYPE_FORM,
&info, pdf_ref_obj(form->contents));
p->pending_forms = fnode;
/*
* Make sure the object is self-contained by adding the
* current font and color to the object stream.
*/
pdf_dev_reset_fonts(1);
pdf_dev_reset_color(1); /* force color operators to be added to stream */
return xobj_id;
}
void
pdf_doc_end_grabbing (pdf_obj *attrib)
{
pdf_form *form;
pdf_obj *procset;
pdf_doc *p = &pdoc;
struct form_list_node *fnode;
if (!p->pending_forms) {
WARN("Tried to close a nonexistent form XOject.");
return;
}
fnode = p->pending_forms;
form = &fnode->form;
pdf_dev_grestore_to(fnode->q_depth);
/*
* ProcSet is obsolete in PDF-1.4 but recommended for compatibility.
*/
procset = pdf_new_array();
pdf_add_array(procset, pdf_new_name("PDF"));
pdf_add_array(procset, pdf_new_name("Text"));
pdf_add_array(procset, pdf_new_name("ImageC"));
pdf_add_array(procset, pdf_new_name("ImageB"));
pdf_add_array(procset, pdf_new_name("ImageI"));
pdf_add_dict (form->resources, pdf_new_name("ProcSet"), procset);
pdf_doc_make_xform(form->contents,
&form->cropbox, &form->matrix,
pdf_ref_obj(form->resources), attrib);
pdf_release_obj(form->resources);
pdf_release_obj(form->contents);
if (attrib) pdf_release_obj(attrib);
p->pending_forms = fnode->prev;
pdf_dev_pop_gstate();
pdf_dev_reset_fonts(1);
pdf_dev_reset_color(0);
RELEASE(fnode);
return;
}
static struct
{
int dirty;
int broken;
pdf_obj *annot_dict;
pdf_rect rect;
} breaking_state = {0, 0, NULL, {0.0, 0.0, 0.0, 0.0}};
static void
reset_box (void)
{
breaking_state.rect.llx = breaking_state.rect.lly = HUGE_VAL;
breaking_state.rect.urx = breaking_state.rect.ury = -HUGE_VAL;
breaking_state.dirty = 0;
}
void
pdf_doc_begin_annot (pdf_obj *dict)
{
breaking_state.annot_dict = dict;
breaking_state.broken = 0;
reset_box();
}
void
pdf_doc_end_annot (void)
{
pdf_doc_break_annot();
breaking_state.annot_dict = NULL;
}
void
pdf_doc_break_annot (void)
{
pdf_doc *p = &pdoc;
double g = p->opt.annot_grow;
if (breaking_state.dirty) {
pdf_obj *annot_dict;
pdf_rect rect;
/* Copy dict */
annot_dict = pdf_new_dict();
pdf_merge_dict(annot_dict, breaking_state.annot_dict);
rect = breaking_state.rect;
rect.llx -= g;
rect.lly -= g;
rect.urx += g;
rect.ury += g;
pdf_doc_add_annot(pdf_doc_current_page_number(), &rect,
annot_dict, !breaking_state.broken);
pdf_release_obj(annot_dict);
breaking_state.broken = 1;
}
reset_box();
}
void
pdf_doc_expand_box (const pdf_rect *rect)
{
breaking_state.rect.llx = MIN(breaking_state.rect.llx, rect->llx);
breaking_state.rect.lly = MIN(breaking_state.rect.lly, rect->lly);
breaking_state.rect.urx = MAX(breaking_state.rect.urx, rect->urx);
breaking_state.rect.ury = MAX(breaking_state.rect.ury, rect->ury);
breaking_state.dirty = 1;
}
#if 0
/* This should be number tree */
void
pdf_doc_set_pagelabel (int pg_start,
const char *type,
const void *prefix, int prfx_len, int start)
{
pdf_doc *p = &pdoc;
pdf_obj *label_dict;
if (!p->root.pagelabels)
p->root.pagelabels = pdf_new_array();
label_dict = pdf_new_dict();
if (!type || type[0] == '\0') /* Set back to default. */
pdf_add_dict(label_dict, pdf_new_name("S"), pdf_new_name("D"));
else {
if (type)
pdf_add_dict(label_dict, pdf_new_name("S"), pdf_new_name(type));
if (prefix && prfx_len > 0)
pdf_add_dict(label_dict,
pdf_new_name("P"),
pdf_new_string(prefix, prfx_len));
if (start != 1)
pdf_add_dict(label_dict,
pdf_new_name("St"), pdf_new_number(start));
}
pdf_add_array(p->root.pagelabels, pdf_new_number(pg_start));
pdf_add_array(p->root.pagelabels, label_dict);
return;
}
#endif
|