3
This is dvipdfm, a DVI to PDF translator.
4
Copyright (C) 1998, 1999 by Mark A. Wicks
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
The author may be contacted via the e-mail address
30
#include "pdfspecial.h"
33
pdf_obj *pdf_include_page(FILE *image_file, struct xform_info *p, char *res_name)
35
pdf_obj *trailer = NULL, *catalog = NULL, *page_tree = NULL;
36
pdf_obj *kids_ref, *kids;
37
pdf_obj *media_box = NULL, *crop_box = NULL, *resources = NULL,
38
*contents = NULL, *contents_ref = NULL;
43
if (!(trailer = pdf_open (image_file))) {
44
fprintf (stderr, "\nCorrupt PDF file?\n");
47
/* Now just lookup catalog location */
49
if (trailer && (catalog = pdf_deref_obj(pdf_lookup_dict (trailer,"Root"))) ==
51
fprintf (stderr, "\nCatalog isn't where I expect it.\n");
54
pdf_release_obj (trailer);
56
/* Lookup page tree in catalog */
58
page_tree = pdf_deref_obj (pdf_lookup_dict (catalog, "Pages"));
59
/* Should be finished with catalog */
60
pdf_release_obj (catalog);
62
/* Media box and resources can be inherited so start looking for
65
if ((tmp1 = pdf_lookup_dict (page_tree, "CropBox")))
66
crop_box = pdf_deref_obj (tmp1);
67
if ((tmp1 = pdf_lookup_dict (page_tree, "MediaBox")))
68
media_box = pdf_deref_obj (tmp1);
69
resources = pdf_deref_obj (pdf_lookup_dict (page_tree, "Resources"));
70
if (resources == NULL) {
71
resources = pdf_new_dict();
73
while ((kids_ref = pdf_lookup_dict (page_tree, "Kids")) != NULL) {
74
kids = pdf_deref_obj (kids_ref);
75
pdf_release_obj (page_tree);
76
page_tree = pdf_deref_obj (pdf_get_array(kids, 0));
77
pdf_release_obj (kids);
78
/* Replace MediaBox if it's here */
79
tmp1 = pdf_deref_obj(pdf_lookup_dict (page_tree, "MediaBox"));
80
if (tmp1 && media_box)
81
pdf_release_obj (media_box);
84
/* Do same for CropBox */
85
tmp1 = pdf_deref_obj(pdf_lookup_dict (page_tree, "CropBox"));
87
pdf_release_obj (crop_box);
90
/* Add resources if they're here */
91
tmp1 = pdf_deref_obj (pdf_lookup_dict (page_tree, "Resources"));
93
pdf_merge_dict (tmp1, resources);
94
pdf_release_obj (resources);
98
/* At this point, page_tree contains the first page. media_box,
99
crop_box, and resources should also be set. */
100
/* If there's a crop_box, replace media_box with crop_box.
101
The rest of this routine assumes crop_box has been released */
103
pdf_release_obj (media_box);
104
media_box = crop_box;
107
/* This gets bit confusing. In the following code,
108
media_box is the box the image is cropped to.
109
The bounding box is the box the image is scaled to */
110
/* If user did not supply bounding box, use media_box
111
(which may really be cropbox) as bounding box */
112
/* Set the crop box parameters in the xform_info structure */
113
p->c_llx = pdf_number_value (pdf_get_array (media_box, 0));
114
p->c_lly = pdf_number_value (pdf_get_array (media_box, 1));
115
p->c_urx = pdf_number_value (pdf_get_array (media_box, 2));
116
p->c_ury = pdf_number_value (pdf_get_array (media_box, 3));
118
/* Adjust scaling and clipping information as necessary */
121
/* Set the media box to whatever pdf_scale_image() decided
122
for the crop box (which may be unchanged) */
123
pdf_release_obj (media_box);
124
media_box = pdf_new_array ();
125
pdf_add_array (media_box, pdf_new_number (p->c_llx));
126
pdf_add_array (media_box, pdf_new_number (p->c_lly));
127
pdf_add_array (media_box, pdf_new_number (p->c_urx));
128
pdf_add_array (media_box, pdf_new_number (p->c_ury));
131
pdf_deref_obj(pdf_lookup_dict(page_tree,"Contents")))==NULL) {
132
fprintf (stderr, "\nNo Contents found\n");
135
pdf_release_obj (page_tree);
137
/* Arrays of contents must be handled very differently (not implemented) */
138
if (contents && contents -> type != PDF_ARRAY) {
139
doc_make_form_xobj (contents, media_box,
140
p->user_bbox? p->u_llx: 0.0,
141
p->user_bbox? p->u_lly: 0.0,
143
resources, res_name);
145
fprintf (stderr, "\nIgnoring stream with with multiple segments\n");
149
contents_ref = pdf_ref_obj (contents);
150
pdf_release_obj (contents);
156
return (contents_ref);