upgrade to xpdf 3.00.
[swftools.git] / pdf2swf / xpdf / Page.cc
blobab8504d9954f845c048b73d6c0bf726e321aff54
1 //========================================================================
2 //
3 // Page.cc
4 //
5 // Copyright 1996-2003 Glyph & Cog, LLC
6 //
7 //========================================================================
9 #include <aconf.h>
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
13 #endif
15 #include <stddef.h>
16 #include "GlobalParams.h"
17 #include "Object.h"
18 #include "Array.h"
19 #include "Dict.h"
20 #include "XRef.h"
21 #include "Link.h"
22 #include "OutputDev.h"
23 #ifndef PDF_PARSER_ONLY
24 #include "Gfx.h"
25 #include "Annot.h"
26 #endif
27 #include "Error.h"
28 #include "Page.h"
30 //------------------------------------------------------------------------
31 // PageAttrs
32 //------------------------------------------------------------------------
34 PageAttrs::PageAttrs(PageAttrs *attrs, Dict *dict) {
35 Object obj1;
36 double w, h;
38 // get old/default values
39 if (attrs) {
40 mediaBox = attrs->mediaBox;
41 cropBox = attrs->cropBox;
42 haveCropBox = attrs->haveCropBox;
43 rotate = attrs->rotate;
44 attrs->resources.copy(&resources);
45 } else {
46 // set default MediaBox to 8.5" x 11" -- this shouldn't be necessary
47 // but some (non-compliant) PDF files don't specify a MediaBox
48 mediaBox.x1 = 0;
49 mediaBox.y1 = 0;
50 mediaBox.x2 = 612;
51 mediaBox.y2 = 792;
52 cropBox.x1 = cropBox.y1 = cropBox.x2 = cropBox.y2 = 0;
53 haveCropBox = gFalse;
54 rotate = 0;
55 resources.initNull();
58 // media box
59 readBox(dict, "MediaBox", &mediaBox);
61 // crop box
62 if (readBox(dict, "CropBox", &cropBox)) {
63 haveCropBox = gTrue;
65 if (!haveCropBox) {
66 cropBox = mediaBox;
69 // if the MediaBox is excessively larger than the CropBox,
70 // just use the CropBox
71 limitToCropBox = gFalse;
72 if (haveCropBox) {
73 w = 0.25 * (cropBox.x2 - cropBox.x1);
74 h = 0.25 * (cropBox.y2 - cropBox.y1);
75 if ((cropBox.x1 - mediaBox.x1) + (mediaBox.x2 - cropBox.x2) > w ||
76 (cropBox.y1 - mediaBox.y1) + (mediaBox.y2 - cropBox.y2) > h) {
77 limitToCropBox = gTrue;
81 // other boxes
82 bleedBox = cropBox;
83 readBox(dict, "BleedBox", &bleedBox);
84 trimBox = cropBox;
85 readBox(dict, "TrimBox", &trimBox);
86 artBox = cropBox;
87 readBox(dict, "ArtBox", &artBox);
89 // rotate
90 dict->lookup("Rotate", &obj1);
91 if (obj1.isInt()) {
92 rotate = obj1.getInt();
94 obj1.free();
95 while (rotate < 0) {
96 rotate += 360;
98 while (rotate >= 360) {
99 rotate -= 360;
102 // misc attributes
103 dict->lookup("LastModified", &lastModified);
104 dict->lookup("BoxColorInfo", &boxColorInfo);
105 dict->lookup("Group", &group);
106 dict->lookup("Metadata", &metadata);
107 dict->lookup("PieceInfo", &pieceInfo);
108 dict->lookup("SeparationInfo", &separationInfo);
110 // resource dictionary
111 dict->lookup("Resources", &obj1);
112 if (obj1.isDict()) {
113 resources.free();
114 obj1.copy(&resources);
116 obj1.free();
119 PageAttrs::~PageAttrs() {
120 lastModified.free();
121 boxColorInfo.free();
122 group.free();
123 metadata.free();
124 pieceInfo.free();
125 separationInfo.free();
126 resources.free();
129 GBool PageAttrs::readBox(Dict *dict, char *key, PDFRectangle *box) {
130 PDFRectangle tmp;
131 Object obj1, obj2;
132 GBool ok;
134 dict->lookup(key, &obj1);
135 if (obj1.isArray() && obj1.arrayGetLength() == 4) {
136 ok = gTrue;
137 obj1.arrayGet(0, &obj2);
138 if (obj2.isNum()) {
139 tmp.x1 = obj2.getNum();
140 } else {
141 ok = gFalse;
143 obj2.free();
144 obj1.arrayGet(1, &obj2);
145 if (obj2.isNum()) {
146 tmp.y1 = obj2.getNum();
147 } else {
148 ok = gFalse;
150 obj2.free();
151 obj1.arrayGet(2, &obj2);
152 if (obj2.isNum()) {
153 tmp.x2 = obj2.getNum();
154 } else {
155 ok = gFalse;
157 obj2.free();
158 obj1.arrayGet(3, &obj2);
159 if (obj2.isNum()) {
160 tmp.y2 = obj2.getNum();
161 } else {
162 ok = gFalse;
164 obj2.free();
165 if (ok) {
166 *box = tmp;
168 } else {
169 ok = gFalse;
171 obj1.free();
172 return ok;
175 //------------------------------------------------------------------------
176 // Page
177 //------------------------------------------------------------------------
179 Page::Page(XRef *xrefA, int numA, Dict *pageDict, PageAttrs *attrsA) {
180 ok = gTrue;
181 xref = xrefA;
182 num = numA;
184 // get attributes
185 attrs = attrsA;
187 // annotations
188 pageDict->lookupNF("Annots", &annots);
189 if (!(annots.isRef() || annots.isArray() || annots.isNull())) {
190 error(-1, "Page annotations object (page %d) is wrong type (%s)",
191 num, annots.getTypeName());
192 annots.free();
193 goto err2;
196 // contents
197 pageDict->lookupNF("Contents", &contents);
198 if (!(contents.isRef() || contents.isArray() ||
199 contents.isNull())) {
200 error(-1, "Page contents object (page %d) is wrong type (%s)",
201 num, contents.getTypeName());
202 contents.free();
203 goto err1;
206 return;
208 err2:
209 annots.initNull();
210 err1:
211 contents.initNull();
212 ok = gFalse;
215 Page::~Page() {
216 delete attrs;
217 annots.free();
218 contents.free();
221 void Page::display(OutputDev *out, double hDPI, double vDPI,
222 int rotate, GBool crop,
223 Links *links, Catalog *catalog,
224 GBool (*abortCheckCbk)(void *data),
225 void *abortCheckCbkData) {
226 displaySlice(out, hDPI, vDPI, rotate, crop, -1, -1, -1, -1, links, catalog,
227 abortCheckCbk, abortCheckCbkData);
230 void Page::displaySlice(OutputDev *out, double hDPI, double vDPI,
231 int rotate, GBool crop,
232 int sliceX, int sliceY, int sliceW, int sliceH,
233 Links *links, Catalog *catalog,
234 GBool (*abortCheckCbk)(void *data),
235 void *abortCheckCbkData) {
236 #ifndef PDF_PARSER_ONLY
237 PDFRectangle *mediaBox, *cropBox;
238 PDFRectangle box;
239 Gfx *gfx;
240 Object obj;
241 Link *link;
242 Annots *annotList;
243 double kx, ky;
244 int i;
246 rotate += getRotate();
247 if (rotate >= 360) {
248 rotate -= 360;
249 } else if (rotate < 0) {
250 rotate += 360;
253 mediaBox = getBox();
254 if (sliceW >= 0 && sliceH >= 0) {
255 kx = 72.0 / hDPI;
256 ky = 72.0 / vDPI;
257 if (rotate == 90) {
258 if (out->upsideDown()) {
259 box.x1 = mediaBox->x1 + ky * sliceY;
260 box.x2 = mediaBox->x1 + ky * (sliceY + sliceH);
261 } else {
262 box.x1 = mediaBox->x2 - ky * (sliceY + sliceH);
263 box.x2 = mediaBox->x2 - ky * sliceY;
265 box.y1 = mediaBox->y1 + kx * sliceX;
266 box.y2 = mediaBox->y1 + kx * (sliceX + sliceW);
267 } else if (rotate == 180) {
268 box.x1 = mediaBox->x2 - kx * (sliceX + sliceW);
269 box.x2 = mediaBox->x2 - kx * sliceX;
270 if (out->upsideDown()) {
271 box.y1 = mediaBox->y1 + ky * sliceY;
272 box.y2 = mediaBox->y1 + ky * (sliceY + sliceH);
273 } else {
274 box.y1 = mediaBox->y2 - ky * (sliceY + sliceH);
275 box.y2 = mediaBox->y2 - ky * sliceY;
277 } else if (rotate == 270) {
278 if (out->upsideDown()) {
279 box.x1 = mediaBox->x2 - ky * (sliceY + sliceH);
280 box.x2 = mediaBox->x2 - ky * sliceY;
281 } else {
282 box.x1 = mediaBox->x1 + ky * sliceY;
283 box.x2 = mediaBox->x1 + ky * (sliceY + sliceH);
285 box.y1 = mediaBox->y2 - kx * (sliceX + sliceW);
286 box.y2 = mediaBox->y2 - kx * sliceX;
287 } else {
288 box.x1 = mediaBox->x1 + kx * sliceX;
289 box.x2 = mediaBox->x1 + kx * (sliceX + sliceW);
290 if (out->upsideDown()) {
291 box.y1 = mediaBox->y2 - ky * (sliceY + sliceH);
292 box.y2 = mediaBox->y2 - ky * sliceY;
293 } else {
294 box.y1 = mediaBox->y1 + ky * sliceY;
295 box.y2 = mediaBox->y1 + ky * (sliceY + sliceH);
298 } else {
299 box = *mediaBox;
301 cropBox = getCropBox();
303 if (globalParams->getPrintCommands()) {
304 printf("***** MediaBox = ll:%g,%g ur:%g,%g\n",
305 box.x1, box.y1, box.x2, box.y2);
306 if (isCropped()) {
307 printf("***** CropBox = ll:%g,%g ur:%g,%g\n",
308 cropBox->x1, cropBox->y1, cropBox->x2, cropBox->y2);
310 printf("***** Rotate = %d\n", attrs->getRotate());
313 gfx = new Gfx(xref, out, num, attrs->getResourceDict(),
314 hDPI, vDPI, &box, crop && isCropped(), cropBox, rotate,
315 abortCheckCbk, abortCheckCbkData);
316 contents.fetch(xref, &obj);
317 if (!obj.isNull()) {
318 gfx->saveState();
319 gfx->display(&obj);
320 gfx->restoreState();
322 obj.free();
324 // draw links
325 if (links) {
326 gfx->saveState();
327 for (i = 0; i < links->getNumLinks(); ++i) {
328 link = links->getLink(i);
329 out->drawLink(link, catalog);
331 gfx->restoreState();
332 out->dump();
335 // draw non-link annotations
336 annotList = new Annots(xref, annots.fetch(xref, &obj));
337 obj.free();
338 if (annotList->getNumAnnots() > 0) {
339 if (globalParams->getPrintCommands()) {
340 printf("***** Annotations\n");
342 for (i = 0; i < annotList->getNumAnnots(); ++i) {
343 annotList->getAnnot(i)->draw(gfx);
345 out->dump();
347 delete annotList;
349 delete gfx;
350 #endif