protect against broken info dictionaries
[swftools.git] / lib / pdf / pdf.cc
index 53d95cc..4bcf9b9 100644 (file)
@@ -34,6 +34,7 @@ typedef struct _pdf_doc_internal
 {
     int protect;
     PDFDoc*doc;
+    Object docinfo;
     InfoOutputDev*info;
     GFXOutputDev*outputDev;
     pdf_page_info_t*pages;
@@ -87,6 +88,8 @@ void render2(gfxpage_t*page, gfxdevice_t*output)
     pi->outputDev->setInfo(pi->info);
     pi->outputDev->setXRef(pi->doc, pi->doc->getXRef());
     pi->doc->displayPage((OutputDev*)pi->outputDev, page->nr, zoom, zoom, /*rotate*/0, true, true, /*doLinks*/(int)1);
+    pi->doc->processLinks((OutputDev*)pi->outputDev, page->nr);
+    pi->outputDev->endframe();
 }
 
     
@@ -118,6 +121,8 @@ void pdf_doc_destroy(gfxdocument_t*gfx)
 
     delete i->doc; i->doc=0;
     free(i->pages); i->pages = 0;
+
+    i->docinfo.free();
     
     if(i->info) {
        delete i->info;i->info=0;
@@ -132,16 +137,16 @@ void pdf_doc_destroy(gfxdocument_t*gfx)
     }
 }
 
-void pdf_doc_set_parameter(gfxdocument_t*gfx, char*name, char*value)
+void pdf_doc_set_parameter(gfxdocument_t*gfx, const char*name, const char*value)
 {
     pdf_doc_internal_t*i= (pdf_doc_internal_t*)gfx->internal;
+    GFXOutputDev*o = i->outputDev;
     if(!strcmp(name, "pagemap")) {
-       GFXOutputDev*o = i->outputDev;
        int pdfpage=0, outputpage=0;
        sscanf(value,"%d:%d", &pdfpage, &outputpage);
        o->preparePage(pdfpage, outputpage);
     } else {
-       msg("<warning> Ignored parameter: %s=%s", name, value);
+        o->setParameter(name, value);
     }
 }
 
@@ -168,7 +173,84 @@ gfxpage_t* pdf_doc_getpage(gfxdocument_t*doc, int page)
     return pdf_page;
 }
 
-void storeDeviceParameter(char*name, char*value)
+static char*getInfoString(Dict *infoDict, char *key)
+{
+    Object obj;
+    GString *s1, *s2;
+    int i;
+
+    if (infoDict && infoDict->lookup(key, &obj)->isString()) {
+       s1 = obj.getString();
+       if ((s1->getChar(0) & 0xff) == 0xfe &&
+           (s1->getChar(1) & 0xff) == 0xff) {
+           s2 = new GString();
+           for (i = 2; i < obj.getString()->getLength(); i += 2) {
+             if (s1->getChar(i) == '\0') {
+               s2->append(s1->getChar(i+1));
+             } else {
+               delete s2;
+               s2 = new GString("<unicode>");
+               break;
+             }
+           }
+           char*ret = strdup(s2->getCString());
+           delete s2;
+           obj.free();
+           return ret;
+       } else {
+           char*ret = strdup(s1->getCString());
+           obj.free();
+           return ret;
+       }
+    }
+    return strdup("");
+}
+
+static char*getInfoDate(Dict *infoDict, char *key) 
+{
+    Object obj;
+    char *s;
+
+    if (infoDict && infoDict->lookup(key, &obj)->isString()) {
+       s = obj.getString()->getCString();
+       if (s[0] == 'D' && s[1] == ':') {
+         s += 2;
+       }
+       char*ret = strdup(s);
+       obj.free();
+       return ret;
+    }
+    return strdup("");
+}
+
+char* pdf_doc_getinfo(gfxdocument_t*doc, const char*name)
+{
+    pdf_doc_internal_t*i= (pdf_doc_internal_t*)doc->internal;
+    if(!strcmp(name, "title")) return getInfoString(i->docinfo.getDict(), "Title");
+    else if(!strcmp(name, "subject")) return getInfoString(i->docinfo.getDict(), "Subject");
+    else if(!strcmp(name, "keywords")) return getInfoString(i->docinfo.getDict(), "Keywords");
+    else if(!strcmp(name, "author")) return getInfoString(i->docinfo.getDict(), "Author");
+    else if(!strcmp(name, "creator")) return getInfoString(i->docinfo.getDict(), "Creator");
+    else if(!strcmp(name, "producer")) return getInfoString(i->docinfo.getDict(), "Producer");
+    else if(!strcmp(name, "creationdate")) return getInfoDate(i->docinfo.getDict(), "CreationDate");
+    else if(!strcmp(name, "moddate")) return getInfoDate(i->docinfo.getDict(), "ModDate");
+    else if(!strcmp(name, "linearized")) return strdup(i->doc->isLinearized() ? "yes" : "no");
+    else if(!strcmp(name, "tagged")) return strdup(i->doc->getStructTreeRoot()->isDict() ? "yes" : "no");
+    else if(!strcmp(name, "encrypted")) return strdup(i->doc->isEncrypted() ? "yes" : "no");
+    else if(!strcmp(name, "oktoprint")) return strdup(i->doc->okToPrint() ? "yes" : "no");
+    else if(!strcmp(name, "oktocopy")) return strdup(i->doc->okToCopy() ? "yes" : "no");
+    else if(!strcmp(name, "oktochange")) return strdup(i->doc->okToChange() ? "yes" : "no");
+    else if(!strcmp(name, "oktoaddnotes")) return strdup(i->doc->okToAddNotes() ? "yes" : "no");
+    else if(!strcmp(name, "version")) { 
+        char buf[32];
+        sprintf(buf, "%.1f", i->doc->getPDFVersion());
+        return strdup(buf);
+    }
+    return 0;
+}
+
+
+static void storeDeviceParameter(const char*name, const char*value)
 {
     parameter_t*p = new parameter_t();
     p->name = strdup(name);
@@ -183,7 +265,7 @@ void storeDeviceParameter(char*name, char*value)
     }
 }
 
-void pdf_set_parameter(char*name, char*value)
+static void pdf_set_parameter(const char*name, const char*value)
 {
     msg("<verbose> setting parameter %s to \"%s\"", name, value);
     if(!strncmp(name, "fontdir", strlen("fontdir"))) {
@@ -216,51 +298,7 @@ void pdf_set_parameter(char*name, char*value)
     }
 }
 
-static void printInfoString(Dict *infoDict, char *key, char *fmt) {
-  Object obj;
-  GString *s1, *s2;
-  int i;
-
-  if (infoDict->lookup(key, &obj)->isString()) {
-    s1 = obj.getString();
-    if ((s1->getChar(0) & 0xff) == 0xfe &&
-       (s1->getChar(1) & 0xff) == 0xff) {
-      s2 = new GString();
-      for (i = 2; i < obj.getString()->getLength(); i += 2) {
-       if (s1->getChar(i) == '\0') {
-         s2->append(s1->getChar(i+1));
-       } else {
-         delete s2;
-         s2 = new GString("<unicode>");
-         break;
-       }
-      }
-      printf(fmt, s2->getCString());
-      delete s2;
-    } else {
-      printf(fmt, s1->getCString());
-    }
-  }
-  obj.free();
-}
-
-static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
-  Object obj;
-  char *s;
-
-  if (infoDict->lookup(key, &obj)->isString()) {
-    s = obj.getString()->getCString();
-    if (s[0] == 'D' && s[1] == ':') {
-      s += 2;
-    }
-    printf(fmt, s);
-  }
-  obj.free();
-}
-
-
-
-gfxdocument_t*pdf_open(char*filename)
+static gfxdocument_t*pdf_open(const char*filename)
 {
     gfxdocument_t*pdf_doc = (gfxdocument_t*)malloc(sizeof(gfxdocument_t));
     memset(pdf_doc, 0, sizeof(gfxdocument_t));
@@ -279,7 +317,6 @@ gfxdocument_t*pdf_open(char*filename)
     
     GString *fileName = new GString(filename);
     GString *userPW;
-    Object info;
 
     // read config file
     if(!globalParams)
@@ -300,33 +337,9 @@ gfxdocument_t*pdf_open(char*filename)
         return 0;
     }
 
-    // print doc info
-    i->doc->getDocInfo(&info);
-    if (info.isDict() &&
-      (getScreenLogLevel()>=LOGLEVEL_NOTICE)) {
-      printInfoString(info.getDict(), "Title",        "Title:        %s\n");
-      printInfoString(info.getDict(), "Subject",      "Subject:      %s\n");
-      printInfoString(info.getDict(), "Keywords",     "Keywords:     %s\n");
-      printInfoString(info.getDict(), "Author",       "Author:       %s\n");
-      printInfoString(info.getDict(), "Creator",      "Creator:      %s\n");
-      printInfoString(info.getDict(), "Producer",     "Producer:     %s\n");
-      printInfoDate(info.getDict(),   "CreationDate", "CreationDate: %s\n");
-      printInfoDate(info.getDict(),   "ModDate",      "ModDate:      %s\n");
-      printf("Pages:        %d\n", i->doc->getNumPages());
-      printf("Linearized:   %s\n", i->doc->isLinearized() ? "yes" : "no");
-      printf("Encrypted:    ");
-      if (i->doc->isEncrypted()) {
-        printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
-               i->doc->okToPrint() ? "yes" : "no",
-               i->doc->okToCopy() ? "yes" : "no",
-               i->doc->okToChange() ? "yes" : "no",
-               i->doc->okToAddNotes() ? "yes" : "no");
-      } else {
-        printf("no\n");
-      }
-    }
-    info.free();
-                   
+    // get doc info
+    i->doc->getDocInfo(&i->docinfo);
+    
     pdf_doc->num_pages = i->doc->getNumPages();
     i->protect = 0;
     if (i->doc->isEncrypted()) {
@@ -338,16 +351,14 @@ gfxdocument_t*pdf_open(char*filename)
               i->protect = 1;
     }
 
-    InfoOutputDev*io = new InfoOutputDev();
+    InfoOutputDev*io = new InfoOutputDev(i->doc->getXRef());
     int t;
     i->pages = (pdf_page_info_t*)malloc(sizeof(pdf_page_info_t)*pdf_doc->num_pages);
     memset(i->pages,0,sizeof(pdf_page_info_t)*pdf_doc->num_pages);
     for(t=1;t<=pdf_doc->num_pages;t++) {
        if(!global_page_range || is_in_range(t, global_page_range)) {
            i->doc->displayPage((OutputDev*)io, t, zoom, zoom, /*rotate*/0, /*usemediabox*/true, /*crop*/true, /*doLinks*/(int)1);
-#if xpdfUpdateVersion >= 16
            i->doc->processLinks((OutputDev*)io, t);
-#endif
            i->pages[t-1].xMin = io->x1;
            i->pages[t-1].yMin = io->y1;
            i->pages[t-1].xMax = io->x2;
@@ -366,6 +377,7 @@ gfxdocument_t*pdf_open(char*filename)
     pdf_doc->get = 0;
     pdf_doc->destroy = pdf_doc_destroy;
     pdf_doc->set_parameter = pdf_doc_set_parameter;
+    pdf_doc->getinfo = pdf_doc_getinfo;
     pdf_doc->getpage = pdf_doc_getpage;