1 //========================================================================
5 // Copyright 1996-2003 Glyph & Cog, LLC
7 //========================================================================
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
16 #include "GlobalParams.h"
22 #include "OutputDev.h"
23 #ifndef PDF_PARSER_ONLY
30 //------------------------------------------------------------------------
32 //------------------------------------------------------------------------
34 PageAttrs::PageAttrs(PageAttrs *attrs, Dict *dict) {
38 // get old/default values
40 mediaBox = attrs->mediaBox;
41 cropBox = attrs->cropBox;
42 haveCropBox = attrs->haveCropBox;
43 rotate = attrs->rotate;
44 attrs->resources.copy(&resources);
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
52 cropBox.x1 = cropBox.y1 = cropBox.x2 = cropBox.y2 = 0;
59 readBox(dict, "MediaBox", &mediaBox);
62 if (readBox(dict, "CropBox", &cropBox)) {
69 // if the MediaBox is excessively larger than the CropBox,
70 // just use the CropBox
71 limitToCropBox = gFalse;
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 /* if the crop box is larger than the media box, cut it down to
84 mediaBox.x1 <= cropBox.x2 &&
85 mediaBox.y1 <= cropBox.y2 &&
86 cropBox.x1 <= mediaBox.x2 &&
87 cropBox.y1 <= mediaBox.y2) {
88 if(mediaBox.x1 >= cropBox.x1) cropBox.x1 = mediaBox.x1;
89 if(mediaBox.y1 >= cropBox.y1) cropBox.y1 = mediaBox.y1;
90 if(mediaBox.x2 <= cropBox.x2) cropBox.x2 = mediaBox.x2;
91 if(mediaBox.y2 <= cropBox.y2) cropBox.y2 = mediaBox.y2;
96 readBox(dict, "BleedBox", &bleedBox);
98 readBox(dict, "TrimBox", &trimBox);
100 readBox(dict, "ArtBox", &artBox);
103 dict->lookup("Rotate", &obj1);
105 rotate = obj1.getInt();
111 while (rotate >= 360) {
116 dict->lookup("LastModified", &lastModified);
117 dict->lookup("BoxColorInfo", &boxColorInfo);
118 dict->lookup("Group", &group);
119 dict->lookup("Metadata", &metadata);
120 dict->lookup("PieceInfo", &pieceInfo);
121 dict->lookup("SeparationInfo", &separationInfo);
123 // resource dictionary
124 dict->lookup("Resources", &obj1);
127 obj1.copy(&resources);
132 PageAttrs::~PageAttrs() {
138 separationInfo.free();
142 GBool PageAttrs::readBox(Dict *dict, char *key, PDFRectangle *box) {
147 dict->lookup(key, &obj1);
148 if (obj1.isArray() && obj1.arrayGetLength() == 4) {
150 obj1.arrayGet(0, &obj2);
152 tmp.x1 = obj2.getNum();
157 obj1.arrayGet(1, &obj2);
159 tmp.y1 = obj2.getNum();
164 obj1.arrayGet(2, &obj2);
166 tmp.x2 = obj2.getNum();
171 obj1.arrayGet(3, &obj2);
173 tmp.y2 = obj2.getNum();
188 //------------------------------------------------------------------------
190 //------------------------------------------------------------------------
192 Page::Page(XRef *xrefA, int numA, Dict *pageDict, PageAttrs *attrsA) {
201 pageDict->lookupNF("Annots", &annots);
202 if (!(annots.isRef() || annots.isArray() || annots.isNull())) {
203 error(-1, "Page annotations object (page %d) is wrong type (%s)",
204 num, annots.getTypeName());
210 pageDict->lookupNF("Contents", &contents);
211 if (!(contents.isRef() || contents.isArray() ||
212 contents.isNull())) {
213 error(-1, "Page contents object (page %d) is wrong type (%s)",
214 num, contents.getTypeName());
234 void Page::display(OutputDev *out, double hDPI, double vDPI,
235 int rotate, GBool crop,
236 Links *links, Catalog *catalog,
237 GBool (*abortCheckCbk)(void *data),
238 void *abortCheckCbkData) {
239 displaySlice(out, hDPI, vDPI, rotate, crop, -1, -1, -1, -1, links, catalog,
240 abortCheckCbk, abortCheckCbkData);
243 void Page::displaySlice(OutputDev *out, double hDPI, double vDPI,
244 int rotate, GBool crop,
245 int sliceX, int sliceY, int sliceW, int sliceH,
246 Links *links, Catalog *catalog,
247 GBool (*abortCheckCbk)(void *data),
248 void *abortCheckCbkData) {
249 #ifndef PDF_PARSER_ONLY
250 PDFRectangle *mediaBox, *cropBox;
259 rotate += getRotate();
262 } else if (rotate < 0) {
267 if (sliceW >= 0 && sliceH >= 0) {
271 if (out->upsideDown()) {
272 box.x1 = mediaBox->x1 + ky * sliceY;
273 box.x2 = mediaBox->x1 + ky * (sliceY + sliceH);
275 box.x1 = mediaBox->x2 - ky * (sliceY + sliceH);
276 box.x2 = mediaBox->x2 - ky * sliceY;
278 box.y1 = mediaBox->y1 + kx * sliceX;
279 box.y2 = mediaBox->y1 + kx * (sliceX + sliceW);
280 } else if (rotate == 180) {
281 box.x1 = mediaBox->x2 - kx * (sliceX + sliceW);
282 box.x2 = mediaBox->x2 - kx * sliceX;
283 if (out->upsideDown()) {
284 box.y1 = mediaBox->y1 + ky * sliceY;
285 box.y2 = mediaBox->y1 + ky * (sliceY + sliceH);
287 box.y1 = mediaBox->y2 - ky * (sliceY + sliceH);
288 box.y2 = mediaBox->y2 - ky * sliceY;
290 } else if (rotate == 270) {
291 if (out->upsideDown()) {
292 box.x1 = mediaBox->x2 - ky * (sliceY + sliceH);
293 box.x2 = mediaBox->x2 - ky * sliceY;
295 box.x1 = mediaBox->x1 + ky * sliceY;
296 box.x2 = mediaBox->x1 + ky * (sliceY + sliceH);
298 box.y1 = mediaBox->y2 - kx * (sliceX + sliceW);
299 box.y2 = mediaBox->y2 - kx * sliceX;
301 box.x1 = mediaBox->x1 + kx * sliceX;
302 box.x2 = mediaBox->x1 + kx * (sliceX + sliceW);
303 if (out->upsideDown()) {
304 box.y1 = mediaBox->y2 - ky * (sliceY + sliceH);
305 box.y2 = mediaBox->y2 - ky * sliceY;
307 box.y1 = mediaBox->y1 + ky * sliceY;
308 box.y2 = mediaBox->y1 + ky * (sliceY + sliceH);
314 cropBox = getCropBox();
316 if (globalParams->getPrintCommands()) {
317 printf("***** MediaBox = ll:%g,%g ur:%g,%g\n",
318 box.x1, box.y1, box.x2, box.y2);
320 printf("***** CropBox = ll:%g,%g ur:%g,%g\n",
321 cropBox->x1, cropBox->y1, cropBox->x2, cropBox->y2);
323 printf("***** Rotate = %d\n", attrs->getRotate());
326 gfx = new Gfx(xref, out, num, attrs->getResourceDict(),
327 hDPI, vDPI, &box, crop && isCropped(), cropBox, rotate,
328 abortCheckCbk, abortCheckCbkData);
329 contents.fetch(xref, &obj);
340 for (i = 0; i < links->getNumLinks(); ++i) {
341 link = links->getLink(i);
342 out->drawLink(link, catalog);
348 // draw non-link annotations
349 annotList = new Annots(xref, annots.fetch(xref, &obj));
351 if (annotList->getNumAnnots() > 0) {
352 if (globalParams->getPrintCommands()) {
353 printf("***** Annotations\n");
355 for (i = 0; i < annotList->getNumAnnots(); ++i) {
356 annotList->getAnnot(i)->draw(gfx);