upgrade to xpdf 3.00.
[swftools.git] / pdf2swf / xpdf / PDFDoc.cc
index ae55d23..b5981d9 100644 (file)
@@ -2,11 +2,13 @@
 //
 // PDFDoc.cc
 //
-// Copyright 1996 Derek B. Noonburg
+// Copyright 1996-2003 Glyph & Cog, LLC
 //
 //========================================================================
 
-#ifdef __GNUC__
+#include <aconf.h>
+
+#ifdef USE_GCC_PRAGMAS
 #pragma implementation
 #endif
 
 #include <string.h>
 #include "GString.h"
 #include "config.h"
+#include "GlobalParams.h"
 #include "Page.h"
 #include "Catalog.h"
 #include "Stream.h"
 #include "XRef.h"
 #include "Link.h"
 #include "OutputDev.h"
-#include "Params.h"
 #include "Error.h"
+#include "ErrorCodes.h"
 #include "Lexer.h"
 #include "Parser.h"
+#ifndef DISABLE_OUTLINE
+#include "Outline.h"
+#endif
 #include "PDFDoc.h"
 
 //------------------------------------------------------------------------
 // PDFDoc
 //------------------------------------------------------------------------
 
-PDFDoc::PDFDoc(GString *fileName1, GString *userPassword) {
+PDFDoc::PDFDoc(GString *fileNameA, GString *ownerPassword,
+              GString *userPassword) {
   Object obj;
-  GString *fileName2;
+  GString *fileName1, *fileName2;
 
   ok = gFalse;
+  errCode = errNone;
 
   file = NULL;
   str = NULL;
   xref = NULL;
   catalog = NULL;
   links = NULL;
+#ifndef DISABLE_OUTLINE
+  outline = NULL;
+#endif
+
+  fileName = fileNameA;
+  fileName1 = fileName;
+
 
   // try to open file
-  fileName = fileName1;
   fileName2 = NULL;
 #ifdef VMS
-  if (!(file = fopen(fileName->getCString(), "rb", "ctx=stm"))) {
-    error(-1, "Couldn't open file '%s'", fileName->getCString());
+  if (!(file = fopen(fileName1->getCString(), "rb", "ctx=stm"))) {
+    error(-1, "Couldn't open file '%s'", fileName1->getCString());
+    errCode = errOpenFile;
     return;
   }
 #else
-  if (!(file = fopen(fileName->getCString(), "rb"))) {
+  if (!(file = fopen(fileName1->getCString(), "rb"))) {
     fileName2 = fileName->copy();
     fileName2->lowerCase();
     if (!(file = fopen(fileName2->getCString(), "rb"))) {
@@ -66,6 +81,7 @@ PDFDoc::PDFDoc(GString *fileName1, GString *userPassword) {
       if (!(file = fopen(fileName2->getCString(), "rb"))) {
        error(-1, "Couldn't open file '%s'", fileName->getCString());
        delete fileName2;
+       errCode = errOpenFile;
        return;
       }
     }
@@ -75,48 +91,64 @@ PDFDoc::PDFDoc(GString *fileName1, GString *userPassword) {
 
   // create stream
   obj.initNull();
-  str = new FileStream(file, 0, -1, &obj);
+  str = new FileStream(file, 0, gFalse, 0, &obj);
 
-  ok = setup(userPassword);
+  ok = setup(ownerPassword, userPassword);
 }
 
-PDFDoc::PDFDoc(BaseStream *str, GString *userPassword) {
+PDFDoc::PDFDoc(BaseStream *strA, GString *ownerPassword,
+              GString *userPassword) {
   ok = gFalse;
+  errCode = errNone;
   fileName = NULL;
   file = NULL;
-  this->str = str;
+  str = strA;
   xref = NULL;
   catalog = NULL;
   links = NULL;
-  ok = setup(userPassword);
+#ifndef DISABLE_OUTLINE
+  outline = NULL;
+#endif
+  ok = setup(ownerPassword, userPassword);
 }
 
-GBool PDFDoc::setup(GString *userPassword) {
-  Object catObj;
+GBool PDFDoc::setup(GString *ownerPassword, GString *userPassword) {
+  str->reset();
 
   // check header
   checkHeader();
 
   // read xref table
-  xref = new XRef(str, userPassword);
+  xref = new XRef(str, ownerPassword, userPassword);
   if (!xref->isOk()) {
     error(-1, "Couldn't read xref table");
+    errCode = xref->getErrorCode();
     return gFalse;
   }
 
   // read catalog
-  catalog = new Catalog(xref->getCatalog(&catObj));
-  catObj.free();
+  catalog = new Catalog(xref);
   if (!catalog->isOk()) {
     error(-1, "Couldn't read page catalog");
+    errCode = errBadCatalog;
     return gFalse;
   }
 
+#ifndef DISABLE_OUTLINE
+  // read outline
+  outline = new Outline(catalog->getOutline(), xref);
+#endif
+
   // done
   return gTrue;
 }
 
 PDFDoc::~PDFDoc() {
+#ifndef DISABLE_OUTLINE
+  if (outline) {
+    delete outline;
+  }
+#endif
   if (catalog) {
     delete catalog;
   }
@@ -168,11 +200,13 @@ void PDFDoc::checkHeader() {
   }
 }
 
-void PDFDoc::displayPage(OutputDev *out, int page, double zoom,
-                        int rotate, GBool doLinks) {
+void PDFDoc::displayPage(OutputDev *out, int page, double hDPI, double vDPI,
+                        int rotate, GBool crop, GBool doLinks,
+                        GBool (*abortCheckCbk)(void *data),
+                        void *abortCheckCbkData) {
   Page *p;
 
-  if (printCommands) {
+  if (globalParams->getPrintCommands()) {
     printf("***** page %d *****\n", page);
   }
   p = catalog->getPage(page);
@@ -181,21 +215,41 @@ void PDFDoc::displayPage(OutputDev *out, int page, double zoom,
       delete links;
     }
     getLinks(p);
-    p->display(out, zoom, rotate, links, catalog);
+    p->display(out, hDPI, vDPI, rotate, crop, links, catalog,
+              abortCheckCbk, abortCheckCbkData);
   } else {
-    p->display(out, zoom, rotate, NULL, catalog);
+    p->display(out, hDPI, vDPI, rotate, crop, NULL, catalog,
+              abortCheckCbk, abortCheckCbkData);
   }
 }
 
 void PDFDoc::displayPages(OutputDev *out, int firstPage, int lastPage,
-                         int zoom, int rotate, GBool doLinks) {
+                         double hDPI, double vDPI, int rotate,
+                         GBool crop, GBool doLinks,
+                         GBool (*abortCheckCbk)(void *data),
+                         void *abortCheckCbkData) {
   int page;
 
   for (page = firstPage; page <= lastPage; ++page) {
-    displayPage(out, page, zoom, rotate, doLinks);
+    displayPage(out, page, hDPI, vDPI, rotate, crop, doLinks,
+               abortCheckCbk, abortCheckCbkData);
   }
 }
 
+void PDFDoc::displayPageSlice(OutputDev *out, int page,
+                             double hDPI, double vDPI,
+                             int rotate, GBool crop,
+                             int sliceX, int sliceY, int sliceW, int sliceH,
+                             GBool (*abortCheckCbk)(void *data),
+                             void *abortCheckCbkData) {
+  Page *p;
+
+  p = catalog->getPage(page);
+  p->displaySlice(out, hDPI, vDPI, rotate, crop,
+                 sliceX, sliceY, sliceW, sliceH,
+                 NULL, catalog, abortCheckCbk, abortCheckCbkData);
+}
+
 GBool PDFDoc::isLinearized() {
   Parser *parser;
   Object obj1, obj2, obj3, obj4, obj5;
@@ -203,8 +257,9 @@ GBool PDFDoc::isLinearized() {
 
   lin = gFalse;
   obj1.initNull();
-  parser = new Parser(new Lexer(str->makeSubStream(str->getStart(),
-                                                  -1, &obj1)));
+  parser = new Parser(xref,
+            new Lexer(xref,
+              str->makeSubStream(str->getStart(), gFalse, 0, &obj1)));
   parser->getObj(&obj1);
   parser->getObj(&obj2);
   parser->getObj(&obj3);
@@ -248,4 +303,3 @@ void PDFDoc::getLinks(Page *page) {
   links = new Links(page->getAnnots(&obj), catalog->getBaseURI());
   obj.free();
 }
-