-
Notifications
You must be signed in to change notification settings - Fork 8
/
pdfdoc.c
2593 lines (2175 loc) · 67.6 KB
/
pdfdoc.c
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
/* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
Copyright (C) 2008-2015 by Jin-Hwan Cho, Matthias Franz, and Shunsaku Hirata,
the dvipdfmx project team.
Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]>
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)...
*/
#include "libtexpdf.h"
#if HAVE_LIBPNG
#include "pngimage.h"
#endif
#include <time.h>
#define PDFDOC_PAGES_ALLOC_SIZE 128u
#define PDFDOC_ARTICLE_ALLOC_SIZE 16
#define PDFDOC_BEAD_ALLOC_SIZE 16
/* XXX Need to eliminate statics if this is going to be reentrant! */
static int verbose = 0;
static char *thumb_basename = NULL;
static char * my_name = "libtexpdf";
void
texpdf_doc_enable_manual_thumbnails (pdf_doc* p)
{
#if HAVE_LIBPNG
p->manual_thumb_enabled = 1;
#else
WARN("Manual thumbnail is not supported without the libpng library.");
#endif
}
static pdf_obj *
read_thumbnail (pdf_doc *p, const char *thumb_filename)
{
pdf_obj *image_ref;
int xobj_id;
FILE *fp;
fp = MFOPEN(thumb_filename, FOPEN_RBIN_MODE);
if (!fp) {
WARN("Could not open thumbnail file \"%s\"", thumb_filename);
return NULL;
}
if (!texpdf_check_for_png(fp) && !texpdf_check_for_jpeg(fp)) {
WARN("Thumbnail \"%s\" not a png/jpeg file!", thumb_filename);
MFCLOSE(fp);
return NULL;
}
MFCLOSE(fp);
xobj_id = texpdf_ximage_findresource(p, thumb_filename, 0, NULL);
if (xobj_id < 0) {
WARN("Could not read thumbnail file \"%s\".", thumb_filename);
image_ref = NULL;
} else {
image_ref = texpdf_ximage_get_reference(xobj_id);
}
return image_ref;
}
void
texpdf_doc_set_verbose (void)
{
verbose++;
pdf_font_set_verbose();
texpdf_color_set_verbose();
texpdf_ximage_set_verbose();
}
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)
struct name_dict
{
const char *category;
struct ht_table *data;
};
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 = texpdf_new_dict();
texpdf_set_root(p->root.dict);
return;
}
static void
pdf_doc_close_catalog (pdf_doc *p)
{
pdf_obj *tmp;
if (p->root.viewerpref) {
tmp = texpdf_lookup_dict(p->root.dict, "ViewerPreferences");
if (!tmp) {
texpdf_add_dict(p->root.dict,
texpdf_new_name("ViewerPreferences"),
texpdf_ref_obj (p->root.viewerpref));
} else if (PDF_OBJ_DICTTYPE(tmp)) {
texpdf_merge_dict(p->root.viewerpref, tmp);
texpdf_add_dict(p->root.dict,
texpdf_new_name("ViewerPreferences"),
texpdf_ref_obj (p->root.viewerpref));
} else { /* Maybe reference */
/* What should I do? */
WARN("Could not modify ViewerPreferences.");
}
texpdf_release_obj(p->root.viewerpref);
p->root.viewerpref = NULL;
}
if (p->root.pagelabels) {
tmp = texpdf_lookup_dict(p->root.dict, "PageLabels");
if (!tmp) {
tmp = texpdf_new_dict();
texpdf_add_dict(tmp, texpdf_new_name("Nums"), texpdf_link_obj(p->root.pagelabels));
texpdf_add_dict(p->root.dict,
texpdf_new_name("PageLabels"), texpdf_ref_obj(tmp));
texpdf_release_obj(tmp);
} else { /* Maybe reference */
/* What should I do? */
WARN("Could not modify PageLabels.");
}
texpdf_release_obj(p->root.pagelabels);
p->root.pagelabels = NULL;
}
texpdf_add_dict(p->root.dict,
texpdf_new_name("Type"), texpdf_new_name("Catalog"));
texpdf_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, long size)
{
if (size > MAXPAGES(p)) {
long 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 long 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_doc *p, 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
texpdf_doc_set_bop_content (pdf_doc *p, const char *content, unsigned length)
{
ASSERT(p);
if (p->pages.bop) {
texpdf_release_obj(p->pages.bop);
p->pages.bop = NULL;
}
if (length > 0) {
p->pages.bop = texpdf_new_stream(STREAM_COMPRESS);
texpdf_add_stream(p->pages.bop, content, length);
} else {
p->pages.bop = NULL;
}
return;
}
void
texpdf_doc_set_eop_content (pdf_doc *p, const char *content, unsigned length)
{
if (p->pages.eop) {
texpdf_release_obj(p->pages.eop);
p->pages.eop = NULL;
}
if (length > 0) {
p->pages.eop = texpdf_new_stream(STREAM_COMPRESS);
texpdf_add_stream(p->pages.eop, content, length);
} else {
p->pages.eop = NULL;
}
return;
}
#ifndef HAVE_TM_GMTOFF
#ifndef HAVE_TIMEZONE
/* auxiliary function to compute timezone offset on
systems that do not support the tm_gmtoff in struct tm,
or have a timezone variable. Such as i386-solaris. */
static long
compute_timezone_offset()
{
const time_t now = time(NULL);
struct tm tm;
struct tm local;
time_t gmtoff;
localtime_r(&now, &local);
gmtime_r(&now, &tm);
return (mktime(&local) - mktime(&tm));
}
#endif /* HAVE_TIMEZONE */
#endif /* HAVE_TM_GMTOFF */
/*
* Docinfo
*/
static long
asn_date (char *date_string)
{
long tz_offset;
time_t current_time;
struct tm *bd_time;
time(¤t_time);
bd_time = localtime(¤t_time);
#ifdef HAVE_TM_GMTOFF
tz_offset = bd_time->tm_gmtoff;
#else
# ifdef HAVE_TIMEZONE
tz_offset = -timezone;
# else
tz_offset = compute_timezone_offset();
# endif /* HAVE_TIMEZONE */
#endif /* HAVE_TM_GMTOFF */
sprintf(date_string, "D:%04d%02d%02d%02d%02d%02d%c%02ld'%02ld'",
bd_time->tm_year + 1900, bd_time->tm_mon + 1, bd_time->tm_mday,
bd_time->tm_hour, bd_time->tm_min, bd_time->tm_sec,
(tz_offset > 0) ? '+' : '-', labs(tz_offset) / 3600,
(labs(tz_offset) / 60) % 60);
return strlen(date_string);
}
static void
pdf_doc_init_docinfo (pdf_doc *p)
{
p->info = texpdf_new_dict();
texpdf_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 = texpdf_lookup_dict(docinfo, keys[i]);
if (value) {
if (!PDF_OBJ_STRINGTYPE(value)) {
WARN("\"%s\" in DocInfo dictionary not string type.", keys[i]);
texpdf_remove_dict(docinfo, keys[i]);
WARN("\"%s\" removed from DocInfo.", keys[i]);
} else if (texpdf_string_length(value) == 0) {
/* The hyperref package often uses emtpy strings. */
texpdf_remove_dict(docinfo, keys[i]);
}
}
}
if (!texpdf_lookup_dict(docinfo, "Producer")) {
char *banner;
banner = NEW(strlen(my_name)+strlen(PACKAGE_VERSION)+4, char);
sprintf(banner, "%s (%s)", my_name, PACKAGE_VERSION);
texpdf_add_dict(docinfo,
texpdf_new_name("Producer"),
texpdf_new_string(banner, strlen(banner)));
RELEASE(banner);
}
if (!texpdf_lookup_dict(docinfo, "CreationDate")) {
char now[32];
asn_date(now);
texpdf_add_dict(docinfo,
texpdf_new_name ("CreationDate"),
texpdf_new_string(now, strlen(now)));
}
texpdf_release_obj(docinfo);
p->info = NULL;
return;
}
static pdf_obj *
texpdf_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 = texpdf_new_dict();
}
} else {
currentpage = LASTPAGE(p);
if (currentpage->resources) {
res_dict = currentpage->resources;
} else {
res_dict = currentpage->resources = texpdf_new_dict();
}
}
resources = texpdf_lookup_dict(res_dict, category);
if (!resources) {
resources = texpdf_new_dict();
texpdf_add_dict(res_dict, texpdf_new_name(category), resources);
}
return resources;
}
void
texpdf_doc_add_page_resource (pdf_doc *p, const char *category,
const char *resource_name, pdf_obj *resource_ref)
{
pdf_obj *resources;
pdf_obj *duplicate;
if (!PDF_OBJ_INDIRECTTYPE(resource_ref)) {
WARN("Passed non indirect reference...");
resource_ref = texpdf_ref_obj(resource_ref); /* leak */
}
resources = texpdf_doc_get_page_resources(p, category);
duplicate = texpdf_lookup_dict(resources, resource_name);
if (duplicate && pdf_compare_reference(duplicate, resource_ref)) {
WARN("Conflicting page resource found (page: %ld, category: %s, name: %s).",
texpdf_doc_current_page_number(p), category, resource_name);
WARN("Ignoring...");
texpdf_release_obj(resource_ref);
} else {
texpdf_add_dict(resources, texpdf_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;
texpdf_add_dict(page->page_obj,
texpdf_new_name("Type"), texpdf_new_name("Page"));
texpdf_add_dict(page->page_obj,
texpdf_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 = texpdf_new_array();
texpdf_add_array(mediabox,
texpdf_new_number(ROUND(page->cropbox.llx, 0.01)));
texpdf_add_array(mediabox,
texpdf_new_number(ROUND(page->cropbox.lly, 0.01)));
texpdf_add_array(mediabox,
texpdf_new_number(ROUND(page->cropbox.urx, 0.01)));
texpdf_add_array(mediabox,
texpdf_new_number(ROUND(page->cropbox.ury, 0.01)));
texpdf_add_dict(page->page_obj, texpdf_new_name("MediaBox"), mediabox);
}
count = 0;
contents_array = texpdf_new_array();
if (page->content_refs[0]) { /* global bop */
texpdf_add_array(contents_array, page->content_refs[0]);
count++;
} else if (p->pages.bop &&
pdf_stream_length(p->pages.bop) > 0) {
texpdf_add_array(contents_array, texpdf_ref_obj(p->pages.bop));
count++;
}
if (page->content_refs[1]) { /* background */
texpdf_add_array(contents_array, page->content_refs[1]);
count++;
}
if (page->content_refs[2]) { /* page body */
texpdf_add_array(contents_array, page->content_refs[2]);
count++;
}
if (page->content_refs[3]) { /* global eop */
texpdf_add_array(contents_array, page->content_refs[3]);
count++;
} else if (p->pages.eop &&
pdf_stream_length(p->pages.eop) > 0) {
texpdf_add_array(contents_array, texpdf_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;
texpdf_add_dict(page->page_obj,
texpdf_new_name("Contents"), contents_array);
if (page->annots) {
texpdf_add_dict(page->page_obj,
texpdf_new_name("Annots"), texpdf_ref_obj(page->annots));
texpdf_release_obj(page->annots);
}
if (page->beads) {
texpdf_add_dict(page->page_obj,
texpdf_new_name("B"), texpdf_ref_obj(page->beads));
texpdf_release_obj(page->beads);
}
texpdf_release_obj(page->page_obj);
texpdf_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, long num_pages,
pdf_obj *parent_ref)
{
pdf_obj *self, *self_ref, *kids;
long i;
self = texpdf_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 ? texpdf_ref_obj(self) : texpdf_ref_obj(p->root.pages);
texpdf_add_dict(self, texpdf_new_name("Type"), texpdf_new_name("Pages"));
texpdf_add_dict(self, texpdf_new_name("Count"), texpdf_new_number((double) num_pages));
if (parent_ref != NULL)
texpdf_add_dict(self, texpdf_new_name("Parent"), parent_ref);
kids = texpdf_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 = texpdf_ref_obj(page->page_obj);
texpdf_add_array (kids, texpdf_link_obj(page->page_ref));
doc_flush_page(p, page, texpdf_link_obj(self_ref));
}
} else if (num_pages > 0) {
for (i = 0; i < PAGE_CLUSTER; i++) {
long 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,
texpdf_link_obj(self_ref));
texpdf_add_array(kids, texpdf_ref_obj(subtree));
texpdf_release_obj(subtree);
} else {
pdf_page *page;
page = firstpage + start;
if (!page->page_ref)
page->page_ref = texpdf_ref_obj(page->page_obj);
texpdf_add_array (kids, texpdf_link_obj(page->page_ref));
doc_flush_page(p, page, texpdf_link_obj(self_ref));
}
}
}
texpdf_add_dict(self, texpdf_new_name("Kids"), kids);
texpdf_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 = texpdf_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;
long 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);
texpdf_release_obj(page->page_ref);
page->page_ref = NULL;
}
if (page->page_obj) {
WARN("Entry for a nonexistent page #%ld created.", page_no);
texpdf_release_obj(page->page_obj);
page->page_obj = NULL;
}
if (page->annots) {
WARN("Annotation attached to a nonexistent page #%ld.", page_no);
texpdf_release_obj(page->annots);
page->annots = NULL;
}
if (page->beads) {
WARN("Article beads attached to a nonexistent page #%ld.", page_no);
texpdf_release_obj(page->beads);
page->beads = NULL;
}
if (page->resources) {
texpdf_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);
texpdf_merge_dict (p->root.pages, page_tree_root);
texpdf_release_obj(page_tree_root);
/* They must be after build_page_tree() */
if (p->pages.bop) {
texpdf_add_stream (p->pages.bop, "\n", 1);
texpdf_release_obj(p->pages.bop);
p->pages.bop = NULL;
}
if (p->pages.eop) {
texpdf_add_stream (p->pages.eop, "\n", 1);
texpdf_release_obj(p->pages.eop);
p->pages.eop = NULL;
}
/* Create media box at root node and let the other pages inherit it. */
mediabox = texpdf_new_array();
texpdf_add_array(mediabox, texpdf_new_number(ROUND(p->pages.mediabox.llx, 0.01)));
texpdf_add_array(mediabox, texpdf_new_number(ROUND(p->pages.mediabox.lly, 0.01)));
texpdf_add_array(mediabox, texpdf_new_number(ROUND(p->pages.mediabox.urx, 0.01)));
texpdf_add_array(mediabox, texpdf_new_number(ROUND(p->pages.mediabox.ury, 0.01)));
texpdf_add_dict(p->root.pages, texpdf_new_name("MediaBox"), mediabox);
texpdf_add_dict(p->root.dict,
texpdf_new_name("Pages"),
texpdf_ref_obj (p->root.pages));
texpdf_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;
}
/*
* 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.
*/
pdf_obj *
texpdf_doc_get_page (pdf_file *pf, long page_no, long *count_p,
pdf_rect *bbox, pdf_obj **resources_p) {
pdf_obj *page_tree = NULL;
pdf_obj *resources = NULL, *box = NULL, *rotate = NULL;
pdf_obj *catalog;
catalog = pdf_file_get_catalog(pf);
page_tree = pdf_deref_obj(texpdf_lookup_dict(catalog, "Pages"));
if (!PDF_OBJ_DICTTYPE(page_tree))
goto error;
{
long count;
pdf_obj *tmp = pdf_deref_obj(texpdf_lookup_dict(page_tree, "Count"));
if (!PDF_OBJ_NUMBERTYPE(tmp)) {
if (tmp)
texpdf_release_obj(tmp);
goto error;
}
count = texpdf_number_value(tmp);
texpdf_release_obj(tmp);
if (count_p)
*count_p = count;
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 *media_box = NULL, *crop_box = NULL, *kids, *tmp;
int depth = PDF_OBJ_MAX_DEPTH;
long page_idx = page_no-1, kids_length = 1, i = 0;
while (--depth && i != kids_length) {
if ((tmp = pdf_deref_obj(texpdf_lookup_dict(page_tree, "MediaBox")))) {
if (media_box)
texpdf_release_obj(media_box);
media_box = tmp;
}
if ((tmp = pdf_deref_obj(texpdf_lookup_dict(page_tree, "CropBox")))) {
if (crop_box)
texpdf_release_obj(crop_box);
crop_box = tmp;
}
if ((tmp = pdf_deref_obj(texpdf_lookup_dict(page_tree, "Rotate")))) {
if (rotate)
texpdf_release_obj(rotate);
rotate = tmp;
}
if ((tmp = pdf_deref_obj(texpdf_lookup_dict(page_tree, "Resources")))) {
if (resources)
texpdf_release_obj(resources);
resources = tmp;
}
kids = pdf_deref_obj(texpdf_lookup_dict(page_tree, "Kids"));
if (!kids)
break;
else if (!PDF_OBJ_ARRAYTYPE(kids)) {
texpdf_release_obj(kids);
goto error;
}
kids_length = texpdf_array_length(kids);
for (i = 0; i < kids_length; i++) {
long count;
texpdf_release_obj(page_tree);
page_tree = pdf_deref_obj(texpdf_get_array(kids, i));
if (!PDF_OBJ_DICTTYPE(page_tree))
goto error;
tmp = pdf_deref_obj(texpdf_lookup_dict(page_tree, "Count"));
if (PDF_OBJ_NUMBERTYPE(tmp)) {
/* Pages object */
count = texpdf_number_value(tmp);
texpdf_release_obj(tmp);
} else if (!tmp)
/* Page object */
count = 1;
else {
texpdf_release_obj(tmp);
goto error;
}
if (page_idx < count)
break;
page_idx -= count;
}
texpdf_release_obj(kids);
}
if (!depth || kids_length == i) {
if (media_box)
texpdf_release_obj(media_box);
if (crop_box)
texpdf_release_obj(crop_box);
goto error;
}
if (crop_box)
box = crop_box;
else
if (!(box = pdf_deref_obj(texpdf_lookup_dict(page_tree, "ArtBox"))) &&
!(box = pdf_deref_obj(texpdf_lookup_dict(page_tree, "TrimBox"))) &&
!(box = pdf_deref_obj(texpdf_lookup_dict(page_tree, "BleedBox"))) &&
media_box) {
box = media_box;
media_box = NULL;
}
if (media_box)
texpdf_release_obj(media_box);
}
if (!PDF_OBJ_ARRAYTYPE(box) || texpdf_array_length(box) != 4 ||
!PDF_OBJ_DICTTYPE(resources))
goto error;
if (PDF_OBJ_NUMBERTYPE(rotate)) {
if (texpdf_number_value(rotate))
WARN("<< /Rotate %d >> found. (Not supported yet)",
(int) texpdf_number_value(rotate));
texpdf_release_obj(rotate);
rotate = NULL;
} else if (rotate)
goto error;
{
int i;
for (i = 4; i--; ) {
double x;
pdf_obj *tmp = pdf_deref_obj(texpdf_get_array(box, i));
if (!PDF_OBJ_NUMBERTYPE(tmp)) {
texpdf_release_obj(tmp);
goto error;
}
x = texpdf_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;
}
texpdf_release_obj(tmp);
}
}
texpdf_release_obj(box);
if (resources_p)
*resources_p = resources;
else if (resources)
texpdf_release_obj(resources);
return page_tree;
error:
WARN("Cannot parse document. Broken PDF file?");
error_silent:
if (box)
texpdf_release_obj(box);
if (rotate)
texpdf_release_obj(rotate);
if (resources)
texpdf_release_obj(resources);
if (page_tree)
texpdf_release_obj(page_tree);
return NULL;
}
#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 =