upgrade to xpdf 1.01.
[swftools.git] / pdf2swf / xpdf / Page.cc
index c2ac6b1..c601857 100644 (file)
@@ -2,7 +2,7 @@
 //
 // Page.cc
 //
-// Copyright 1996 Derek B. Noonburg
+// Copyright 1996-2002 Glyph & Cog, LLC
 //
 //========================================================================
 
@@ -10,6 +10,7 @@
 #pragma implementation
 #endif
 
+#include <aconf.h>
 #include <stddef.h>
 #include "Object.h"
 #include "Array.h"
 #include "OutputDev.h"
 #ifndef PDF_PARSER_ONLY
 #include "Gfx.h"
-#include "FormWidget.h"
+#include "Annot.h"
 #endif
 #include "Error.h"
-
-#include "Params.h"
 #include "Page.h"
 
 //------------------------------------------------------------------------
 //------------------------------------------------------------------------
 
 PageAttrs::PageAttrs(PageAttrs *attrs, Dict *dict) {
-  Object obj1, obj2;
+  Object obj1;
   double w, h;
 
   // get old/default values
   if (attrs) {
-    x1 = attrs->x1;
-    y1 = attrs->y1;
-    x2 = attrs->x2;
-    y2 = attrs->y2;
-    cropX1 = attrs->cropX1;
-    cropY1 = attrs->cropY1;
-    cropX2 = attrs->cropX2;
-    cropY2 = attrs->cropY2;
+    mediaBox = attrs->mediaBox;
+    cropBox = attrs->cropBox;
+    haveCropBox = attrs->haveCropBox;
     rotate = attrs->rotate;
     attrs->resources.copy(&resources);
   } else {
     // set default MediaBox to 8.5" x 11" -- this shouldn't be necessary
     // but some (non-compliant) PDF files don't specify a MediaBox
-    x1 = 0;
-    y1 = 0;
-    x2 = 612;
-    y2 = 792;
-    cropX1 = cropY1 = cropX2 = cropY2 = 0;
+    mediaBox.x1 = 0;
+    mediaBox.y1 = 0;
+    mediaBox.x2 = 612;
+    mediaBox.y2 = 792;
+    cropBox.x1 = cropBox.y1 = cropBox.x2 = cropBox.y2 = 0;
+    haveCropBox = gFalse;
     rotate = 0;
     resources.initNull();
   }
 
   // media box
-  dict->lookup("MediaBox", &obj1);
-  if (obj1.isArray() && obj1.arrayGetLength() == 4) {
-    obj1.arrayGet(0, &obj2);
-    if (obj2.isNum())
-      x1 = obj2.getNum();
-    obj2.free();
-    obj1.arrayGet(1, &obj2);
-    if (obj2.isNum())
-      y1 = obj2.getNum();
-    obj2.free();
-    obj1.arrayGet(2, &obj2);
-    if (obj2.isNum())
-      x2 = obj2.getNum();
-    obj2.free();
-    obj1.arrayGet(3, &obj2);
-    if (obj2.isNum())
-      y2 = obj2.getNum();
-    obj2.free();
-  }
-  obj1.free();
+  readBox(dict, "MediaBox", &mediaBox);
 
   // crop box
-  dict->lookup("CropBox", &obj1);
-  if (obj1.isArray() && obj1.arrayGetLength() == 4) {
-    obj1.arrayGet(0, &obj2);
-    if (obj2.isNum())
-      cropX1 = obj2.getNum();
-    obj2.free();
-    obj1.arrayGet(1, &obj2);
-    if (obj2.isNum())
-      cropY1 = obj2.getNum();
-    obj2.free();
-    obj1.arrayGet(2, &obj2);
-    if (obj2.isNum())
-      cropX2 = obj2.getNum();
-    obj2.free();
-    obj1.arrayGet(3, &obj2);
-    if (obj2.isNum())
-      cropY2 = obj2.getNum();
-    obj2.free();
-  }
-  obj1.free();
+  cropBox = mediaBox;
+  haveCropBox = readBox(dict, "CropBox", &cropBox);
 
   // if the MediaBox is excessively larger than the CropBox,
   // just use the CropBox
   limitToCropBox = gFalse;
-  w = 0.25 * (cropX2 - cropX1);
-  h = 0.25 * (cropY2 - cropY1);
-  if (cropX2 > cropX1 &&
-      ((cropX1 - x1) + (x2 - cropX2) > w ||
-       (cropY1 - y1) + (y2 - cropY2) > h)) {
-    limitToCropBox = gTrue;
+  if (haveCropBox) {
+    w = 0.25 * (cropBox.x2 - cropBox.x1);
+    h = 0.25 * (cropBox.y2 - cropBox.y1);
+    if ((cropBox.x1 - mediaBox.x1) + (mediaBox.x2 - cropBox.x2) > w ||
+       (cropBox.y1 - mediaBox.y1) + (mediaBox.y2 - cropBox.y2) > h) {
+      limitToCropBox = gTrue;
+    }
   }
 
+  // other boxes
+  bleedBox = cropBox;
+  readBox(dict, "BleedBox", &bleedBox);
+  trimBox = cropBox;
+  readBox(dict, "TrimBox", &trimBox);
+  artBox = cropBox;
+  readBox(dict, "ArtBox", &artBox);
+
   // rotate
   dict->lookup("Rotate", &obj1);
-  if (obj1.isInt())
+  if (obj1.isInt()) {
     rotate = obj1.getInt();
+  }
   obj1.free();
-  while (rotate < 0)
+  while (rotate < 0) {
     rotate += 360;
-  while (rotate >= 360)
+  }
+  while (rotate >= 360) {
     rotate -= 360;
+  }
+
+  // misc attributes
+  dict->lookup("LastModified", &lastModified);
+  dict->lookup("BoxColorInfo", &boxColorInfo);
+  dict->lookup("Group", &group);
+  dict->lookup("Metadata", &metadata);
+  dict->lookup("PieceInfo", &pieceInfo);
+  dict->lookup("SeparationInfo", &separationInfo);
 
   // resource dictionary
   dict->lookup("Resources", &obj1);
@@ -133,20 +111,75 @@ PageAttrs::PageAttrs(PageAttrs *attrs, Dict *dict) {
 }
 
 PageAttrs::~PageAttrs() {
+  lastModified.free();
+  boxColorInfo.free();
+  group.free();
+  metadata.free();
+  pieceInfo.free();
+  separationInfo.free();
   resources.free();
 }
 
+GBool PageAttrs::readBox(Dict *dict, char *key, PDFRectangle *box) {
+  PDFRectangle tmp;
+  Object obj1, obj2;
+  GBool ok;
+
+  dict->lookup(key, &obj1);
+  if (obj1.isArray() && obj1.arrayGetLength() == 4) {
+    ok = gTrue;
+    obj1.arrayGet(0, &obj2);
+    if (obj2.isNum()) {
+      tmp.x1 = obj2.getNum();
+    } else {
+      ok = gFalse;
+    }
+    obj2.free();
+    obj1.arrayGet(1, &obj2);
+    if (obj2.isNum()) {
+      tmp.y1 = obj2.getNum();
+    } else {
+      ok = gFalse;
+    }
+    obj2.free();
+    obj1.arrayGet(2, &obj2);
+    if (obj2.isNum()) {
+      tmp.x2 = obj2.getNum();
+    } else {
+      ok = gFalse;
+    }
+    obj2.free();
+    obj1.arrayGet(3, &obj2);
+    if (obj2.isNum()) {
+      tmp.y2 = obj2.getNum();
+    } else {
+      ok = gFalse;
+    }
+    obj2.free();
+    if (ok) {
+      *box = tmp;
+    }
+  } else {
+    ok = gFalse;
+  }
+  obj1.free();
+  return ok;
+}
+
 //------------------------------------------------------------------------
 // Page
 //------------------------------------------------------------------------
 
-Page::Page(int num1, Dict *pageDict, PageAttrs *attrs1) {
+Page::Page(XRef *xrefA, int numA, Dict *pageDict, PageAttrs *attrsA,
+          GBool printCommandsA) {
 
   ok = gTrue;
-  num = num1;
+  xref = xrefA;
+  num = numA;
+  printCommands = printCommandsA;
 
   // get attributes
-  attrs = attrs1;
+  attrs = attrsA;
 
   // annotations
   pageDict->lookupNF("Annots", &annots);
@@ -185,18 +218,22 @@ Page::~Page() {
 void Page::display(OutputDev *out, double dpi, int rotate,
                   Links *links, Catalog *catalog) {
 #ifndef PDF_PARSER_ONLY
+  PDFRectangle *box, *cropBox;
   Gfx *gfx;
   Object obj;
   Link *link;
   int i;
-  FormWidgets *formWidgets;
+  Annots *annotList;
+
+  box = getBox();
+  cropBox = getCropBox();
 
   if (printCommands) {
     printf("***** MediaBox = ll:%g,%g ur:%g,%g\n",
-          getX1(), getY1(), getX2(), getY2());
+          box->x1, box->y1, box->x2, box->y2);
     if (isCropped()) {
       printf("***** CropBox = ll:%g,%g ur:%g,%g\n",
-            getCropX1(), getCropY1(), getCropX2(), getCropY2());
+            cropBox->x1, cropBox->y1, cropBox->x2, cropBox->y2);
     }
     printf("***** Rotate = %d\n", attrs->getRotate());
   }
@@ -207,10 +244,9 @@ void Page::display(OutputDev *out, double dpi, int rotate,
   } else if (rotate < 0) {
     rotate += 360;
   }
-  gfx = new Gfx(out, num, attrs->getResourceDict(),
-               dpi, getX1(), getY1(), getX2(), getY2(), isCropped(),
-               getCropX1(), getCropY1(), getCropX2(), getCropY2(), rotate);
-  contents.fetch(&obj);
+  gfx = new Gfx(xref, out, num, attrs->getResourceDict(),
+               dpi, box, isCropped(), cropBox, rotate, printCommands);
+  contents.fetch(xref, &obj);
   if (!obj.isNull()) {
     gfx->display(&obj);
   }
@@ -225,20 +261,20 @@ void Page::display(OutputDev *out, double dpi, int rotate,
     out->dump();
   }
 
-  // draw AcroForm widgets
+  // draw non-link annotations
   //~ need to reset CTM ???
-  formWidgets = new FormWidgets(annots.fetch(&obj));
+  annotList = new Annots(xref, annots.fetch(xref, &obj));
   obj.free();
-  if (printCommands && formWidgets->getNumWidgets() > 0) {
-    printf("***** AcroForm widgets\n");
-  }
-  for (i = 0; i < formWidgets->getNumWidgets(); ++i) {
-    formWidgets->getWidget(i)->draw(gfx);
-  }
-  if (formWidgets->getNumWidgets() > 0) {
+  if (annotList->getNumAnnots() > 0) {
+    if (printCommands) {
+      printf("***** Annotations\n");
+    }
+    for (i = 0; i < annotList->getNumAnnots(); ++i) {
+      annotList->getAnnot(i)->draw(gfx);
+    }
     out->dump();
   }
-  delete formWidgets;
+  delete annotList;
 
   delete gfx;
 #endif