Debian Codegraph @

texk/dvipdfm-x/pdfdoc.c

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