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
31 //------------------------------------------------------------------------
33 //------------------------------------------------------------------------
35 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 crop box is larger than the media box, cut it down to
72 mediaBox.x1 <= cropBox.x2 &&
73 mediaBox.y1 <= cropBox.y2 &&
74 cropBox.x1 <= mediaBox.x2 &&
75 cropBox.y1 <= mediaBox.y2) {
76 if(mediaBox.x1 >= cropBox.x1) cropBox.x1 = mediaBox.x1;
77 if(mediaBox.y1 >= cropBox.y1) cropBox.y1 = mediaBox.y1;
78 if(mediaBox.x2 <= cropBox.x2) cropBox.x2 = mediaBox.x2;
79 if(mediaBox.y2 <= cropBox.y2) cropBox.y2 = mediaBox.y2;
84 readBox(dict, "BleedBox", &bleedBox);
86 readBox(dict, "TrimBox", &trimBox);
88 readBox(dict, "ArtBox", &artBox);
91 dict->lookup("Rotate", &obj1);
93 rotate = obj1.getInt();
99 while (rotate >= 360) {
104 dict->lookup("LastModified", &lastModified);
105 dict->lookup("BoxColorInfo", &boxColorInfo);
106 dict->lookup("Group", &group);
107 dict->lookup("Metadata", &metadata);
108 dict->lookup("PieceInfo", &pieceInfo);
109 dict->lookup("SeparationInfo", &separationInfo);
111 // resource dictionary
112 dict->lookup("Resources", &obj1);
115 obj1.copy(&resources);
120 PageAttrs::~PageAttrs() {
126 separationInfo.free();
130 GBool PageAttrs::readBox(Dict *dict, char *key, PDFRectangle *box) {
136 dict->lookup(key, &obj1);
137 if (obj1.isArray() && obj1.arrayGetLength() == 4) {
139 obj1.arrayGet(0, &obj2);
141 tmp.x1 = obj2.getNum();
146 obj1.arrayGet(1, &obj2);
148 tmp.y1 = obj2.getNum();
153 obj1.arrayGet(2, &obj2);
155 tmp.x2 = obj2.getNum();
160 obj1.arrayGet(3, &obj2);
162 tmp.y2 = obj2.getNum();
168 if (tmp.x1 > tmp.x2) {
169 t = tmp.x1; tmp.x1 = tmp.x2; tmp.x2 = t;
171 if (tmp.y1 > tmp.y2) {
172 t = tmp.y1; tmp.y1 = tmp.y2; tmp.y2 = t;
183 //------------------------------------------------------------------------
185 //------------------------------------------------------------------------
187 Page::Page(XRef *xrefA, int numA, Dict *pageDict, PageAttrs *attrsA) {
196 pageDict->lookupNF("Annots", &annots);
197 if (!(annots.isRef() || annots.isArray() || annots.isNull())) {
198 error(-1, "Page annotations object (page %d) is wrong type (%s)",
199 num, annots.getTypeName());
205 pageDict->lookupNF("Contents", &contents);
206 if (!(contents.isRef() || contents.isArray() ||
207 contents.isNull())) {
208 error(-1, "Page contents object (page %d) is wrong type (%s)",
209 num, contents.getTypeName());
229 void Page::display(OutputDev *out, double hDPI, double vDPI,
230 int rotate, GBool useMediaBox, GBool crop,
231 Links *links, Catalog *catalog,
232 GBool (*abortCheckCbk)(void *data),
233 void *abortCheckCbkData) {
234 displaySlice(out, hDPI, vDPI, rotate, useMediaBox, crop,
235 -1, -1, -1, -1, links, catalog,
236 abortCheckCbk, abortCheckCbkData);
239 void Page::displaySlice(OutputDev *out, double hDPI, double vDPI,
240 int rotate, GBool useMediaBox, GBool crop,
241 int sliceX, int sliceY, int sliceW, int sliceH,
242 Links *links, Catalog *catalog,
243 GBool (*abortCheckCbk)(void *data),
244 void *abortCheckCbkData) {
245 #ifndef PDF_PARSER_ONLY
246 PDFRectangle *mediaBox, *cropBox, *baseBox;
255 rotate += getRotate();
258 } else if (rotate < 0) {
262 mediaBox = getMediaBox();
263 cropBox = getCropBox();
264 if (sliceW >= 0 && sliceH >= 0) {
265 baseBox = useMediaBox ? mediaBox : cropBox;
269 if (out->upsideDown()) {
270 box.x1 = baseBox->x1 + ky * sliceY;
271 box.x2 = baseBox->x1 + ky * (sliceY + sliceH);
273 box.x1 = baseBox->x2 - ky * (sliceY + sliceH);
274 box.x2 = baseBox->x2 - ky * sliceY;
276 box.y1 = baseBox->y1 + kx * sliceX;
277 box.y2 = baseBox->y1 + kx * (sliceX + sliceW);
278 } else if (rotate == 180) {
279 box.x1 = baseBox->x2 - kx * (sliceX + sliceW);
280 box.x2 = baseBox->x2 - kx * sliceX;
281 if (out->upsideDown()) {
282 box.y1 = baseBox->y1 + ky * sliceY;
283 box.y2 = baseBox->y1 + ky * (sliceY + sliceH);
285 box.y1 = baseBox->y2 - ky * (sliceY + sliceH);
286 box.y2 = baseBox->y2 - ky * sliceY;
288 } else if (rotate == 270) {
289 if (out->upsideDown()) {
290 box.x1 = baseBox->x2 - ky * (sliceY + sliceH);
291 box.x2 = baseBox->x2 - ky * sliceY;
293 box.x1 = baseBox->x1 + ky * sliceY;
294 box.x2 = baseBox->x1 + ky * (sliceY + sliceH);
296 box.y1 = baseBox->y2 - kx * (sliceX + sliceW);
297 box.y2 = baseBox->y2 - kx * sliceX;
299 box.x1 = baseBox->x1 + kx * sliceX;
300 box.x2 = baseBox->x1 + kx * (sliceX + sliceW);
301 if (out->upsideDown()) {
302 box.y1 = baseBox->y2 - ky * (sliceY + sliceH);
303 box.y2 = baseBox->y2 - ky * sliceY;
305 box.y1 = baseBox->y1 + ky * sliceY;
306 box.y2 = baseBox->y1 + ky * (sliceY + sliceH);
309 } else if (useMediaBox) {
316 if (globalParams->getPrintCommands()) {
317 printf("***** MediaBox = ll:%g,%g ur:%g,%g\n",
318 mediaBox->x1, mediaBox->y1, mediaBox->x2, mediaBox->y2);
319 printf("***** CropBox = ll:%g,%g ur:%g,%g\n",
320 cropBox->x1, cropBox->y1, cropBox->x2, cropBox->y2);
321 printf("***** Rotate = %d\n", attrs->getRotate());
324 gfx = new Gfx(xref, out, num, attrs->getResourceDict(),
325 hDPI, vDPI, &box, crop ? cropBox : (PDFRectangle *)NULL,
326 rotate, abortCheckCbk, abortCheckCbkData);
327 contents.fetch(xref, &obj);
338 for (i = 0; i < links->getNumLinks(); ++i) {
339 link = links->getLink(i);
340 out->drawLink(link, catalog);
346 // draw non-link annotations
347 annotList = new Annots(xref, catalog, annots.fetch(xref, &obj));
349 if (annotList->getNumAnnots() > 0) {
350 if (globalParams->getPrintCommands()) {
351 printf("***** Annotations\n");
353 for (i = 0; i < annotList->getNumAnnots(); ++i) {
354 annotList->getAnnot(i)->draw(gfx);
364 void Page::getDefaultCTM(double *ctm, double hDPI, double vDPI,
365 int rotate, GBool upsideDown) {
369 rotate += getRotate();
372 } else if (rotate < 0) {
375 state = new GfxState(hDPI, vDPI, getMediaBox(), rotate, upsideDown);
376 for (i = 0; i < 6; ++i) {
377 ctm[i] = state->getCTM()[i];