]> www.fi.muni.cz Git - evince.git/blobdiff - pdf/xpdf/pdfinfo.cc
fixes for USE_ANNOTS_VIEW conditional.
[evince.git] / pdf / xpdf / pdfinfo.cc
index d45599bd9c8f90c3a79eb302cdb601ad989f2fc2..f856a6d41e24b728354cf903a459baf60b113495 100644 (file)
@@ -2,17 +2,21 @@
 //
 // pdfinfo.cc
 //
 //
 // pdfinfo.cc
 //
-// Copyright 1998 Derek B. Noonburg
+// Copyright 1998-2003 Glyph & Cog, LLC
 //
 //========================================================================
 
 //
 //========================================================================
 
+#include <aconf.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stddef.h>
 #include <string.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stddef.h>
 #include <string.h>
+#include <time.h>
+#include <math.h>
 #include "parseargs.h"
 #include "GString.h"
 #include "gmem.h"
 #include "parseargs.h"
 #include "GString.h"
 #include "gmem.h"
+#include "GlobalParams.h"
 #include "Object.h"
 #include "Stream.h"
 #include "Array.h"
 #include "Object.h"
 #include "Stream.h"
 #include "Array.h"
 #include "Catalog.h"
 #include "Page.h"
 #include "PDFDoc.h"
 #include "Catalog.h"
 #include "Page.h"
 #include "PDFDoc.h"
-#include "Params.h"
+#include "CharTypes.h"
+#include "UnicodeMap.h"
 #include "Error.h"
 #include "config.h"
 
 #include "Error.h"
 #include "config.h"
 
-static void printInfoString(Dict *infoDict, char *key, char *fmt);
-static void printInfoDate(Dict *infoDict, char *key, char *fmt);
+static void printInfoString(Dict *infoDict, char *key, char *text,
+                           UnicodeMap *uMap);
+static void printInfoDate(Dict *infoDict, char *key, char *text);
 
 
+static GBool printMetadata = gFalse;
+static char textEncName[128] = "";
+static char ownerPassword[33] = "";
 static char userPassword[33] = "";
 static char userPassword[33] = "";
+static char cfgFileName[256] = "";
 static GBool printVersion = gFalse;
 static GBool printHelp = gFalse;
 
 static ArgDesc argDesc[] = {
 static GBool printVersion = gFalse;
 static GBool printHelp = gFalse;
 
 static ArgDesc argDesc[] = {
+  {"-meta",   argFlag,     &printMetadata,    0,
+   "print the document metadata (XML)"},
+  {"-enc",    argString,   textEncName,    sizeof(textEncName),
+   "output text encoding name"},
+  {"-opw",    argString,   ownerPassword,  sizeof(ownerPassword),
+   "owner password (for encrypted files)"},
   {"-upw",    argString,   userPassword,   sizeof(userPassword),
    "user password (for encrypted files)"},
   {"-upw",    argString,   userPassword,   sizeof(userPassword),
    "user password (for encrypted files)"},
+  {"-cfg",        argString,      cfgFileName,    sizeof(cfgFileName),
+   "configuration file to use in place of .xpdfrc"},
   {"-v",      argFlag,     &printVersion,  0,
    "print copyright and version info"},
   {"-h",      argFlag,     &printHelp,     0,
    "print usage information"},
   {"-help",   argFlag,     &printHelp,     0,
    "print usage information"},
   {"-v",      argFlag,     &printVersion,  0,
    "print copyright and version info"},
   {"-h",      argFlag,     &printHelp,     0,
    "print usage information"},
   {"-help",   argFlag,     &printHelp,     0,
    "print usage information"},
+  {"--help",  argFlag,     &printHelp,     0,
+   "print usage information"},
+  {"-?",      argFlag,     &printHelp,     0,
+   "print usage information"},
   {NULL}
 };
 
 int main(int argc, char *argv[]) {
   PDFDoc *doc;
   GString *fileName;
   {NULL}
 };
 
 int main(int argc, char *argv[]) {
   PDFDoc *doc;
   GString *fileName;
-  GString *userPW;
+  GString *ownerPW, *userPW;
+  UnicodeMap *uMap;
   Object info;
   Object info;
+  double w, h, wISO, hISO;
+  FILE *f;
+  GString *metadata;
   GBool ok;
   GBool ok;
+  int exitCode;
+  int i;
+
+  exitCode = 99;
 
   // parse args
   ok = parseArgs(argDesc, &argc, argv);
 
   // parse args
   ok = parseArgs(argDesc, &argc, argv);
@@ -59,45 +89,64 @@ int main(int argc, char *argv[]) {
     if (!printVersion) {
       printUsage("pdfinfo", "<PDF-file>", argDesc);
     }
     if (!printVersion) {
       printUsage("pdfinfo", "<PDF-file>", argDesc);
     }
-    exit(1);
+    goto err0;
   }
   fileName = new GString(argv[1]);
 
   }
   fileName = new GString(argv[1]);
 
-  // init error file
-  errorInit();
-
   // read config file
   // read config file
-  initParams(xpdfConfigFile);
+  globalParams = new GlobalParams(cfgFileName);
+  if (textEncName[0]) {
+    globalParams->setTextEncoding(textEncName);
+  }
+
+  // get mapping to output encoding
+  if (!(uMap = globalParams->getTextEncoding())) {
+    error(-1, "Couldn't get text encoding");
+    delete fileName;
+    goto err1;
+  }
 
   // open PDF file
 
   // open PDF file
-  xref = NULL;
+  if (ownerPassword[0]) {
+    ownerPW = new GString(ownerPassword);
+  } else {
+    ownerPW = NULL;
+  }
   if (userPassword[0]) {
     userPW = new GString(userPassword);
   } else {
     userPW = NULL;
   }
   if (userPassword[0]) {
     userPW = new GString(userPassword);
   } else {
     userPW = NULL;
   }
-  doc = new PDFDoc(fileName, userPW);
+  doc = new PDFDoc(fileName, ownerPW, userPW);
   if (userPW) {
     delete userPW;
   }
   if (userPW) {
     delete userPW;
   }
+  if (ownerPW) {
+    delete ownerPW;
+  }
   if (!doc->isOk()) {
   if (!doc->isOk()) {
-    exit(1);
+    exitCode = 1;
+    goto err2;
   }
 
   // print doc info
   doc->getDocInfo(&info);
   if (info.isDict()) {
   }
 
   // print doc info
   doc->getDocInfo(&info);
   if (info.isDict()) {
-    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");
+    printInfoString(info.getDict(), "Title",        "Title:        ", uMap);
+    printInfoString(info.getDict(), "Subject",      "Subject:      ", uMap);
+    printInfoString(info.getDict(), "Keywords",     "Keywords:     ", uMap);
+    printInfoString(info.getDict(), "Author",       "Author:       ", uMap);
+    printInfoString(info.getDict(), "Creator",      "Creator:      ", uMap);
+    printInfoString(info.getDict(), "Producer",     "Producer:     ", uMap);
+    printInfoDate(info.getDict(),   "CreationDate", "CreationDate: ");
+    printInfoDate(info.getDict(),   "ModDate",      "ModDate:      ");
   }
   info.free();
 
   }
   info.free();
 
+  // print tagging info
+  printf("Tagged:       %s\n",
+        doc->getStructTreeRoot()->isDict() ? "yes" : "no");
+
   // print page count
   printf("Pages:        %d\n", doc->getNumPages());
 
   // print page count
   printf("Pages:        %d\n", doc->getNumPages());
 
@@ -105,66 +154,161 @@ int main(int argc, char *argv[]) {
   printf("Encrypted:    ");
   if (doc->isEncrypted()) {
     printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
   printf("Encrypted:    ");
   if (doc->isEncrypted()) {
     printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
-          doc->okToPrint() ? "yes" : "no",
-          doc->okToCopy() ? "yes" : "no",
-          doc->okToChange() ? "yes" : "no",
-          doc->okToAddNotes() ? "yes" : "no");
+          doc->okToPrint(gTrue) ? "yes" : "no",
+          doc->okToCopy(gTrue) ? "yes" : "no",
+          doc->okToChange(gTrue) ? "yes" : "no",
+          doc->okToAddNotes(gTrue) ? "yes" : "no");
   } else {
     printf("no\n");
   }
 
   } else {
     printf("no\n");
   }
 
+  // print page size
+  if (doc->getNumPages() >= 1) {
+    w = doc->getPageWidth(1);
+    h = doc->getPageHeight(1);
+    printf("Page size:    %g x %g pts", w, h);
+    if ((fabs(w - 612) < 0.1 && fabs(h - 792) < 0.1) ||
+       (fabs(w - 792) < 0.1 && fabs(h - 612) < 0.1)) {
+      printf(" (letter)");
+    } else {
+      hISO = sqrt(sqrt(2)) * 7200 / 2.54;
+      wISO = hISO / sqrt(2);
+      for (i = 0; i <= 6; ++i) {
+       if ((fabs(w - wISO) < 1 && fabs(h - hISO) < 1) ||
+           (fabs(w - hISO) < 1 && fabs(h - wISO) < 1)) {
+         printf(" (A%d)", i);
+         break;
+       }
+       hISO = wISO;
+       wISO /= sqrt(2);
+      }
+    }
+    printf("\n");
+  } 
+
+  // print file size
+#ifdef VMS
+  f = fopen(fileName->getCString(), "rb", "ctx=stm");
+#else
+  f = fopen(fileName->getCString(), "rb");
+#endif
+  if (f) {
+#if HAVE_FSEEKO
+    fseeko(f, 0, SEEK_END);
+    printf("File size:    %u bytes\n", (Guint)ftello(f));
+#elif HAVE_FSEEK64
+    fseek64(f, 0, SEEK_END);
+    printf("File size:    %u bytes\n", (Guint)ftell64(f));
+#else
+    fseek(f, 0, SEEK_END);
+    printf("File size:    %d bytes\n", (int)ftell(f));
+#endif
+    fclose(f);
+  }
+
   // print linearization info
   // print linearization info
-  printf("Linearized:   %s\n", doc->isLinearized() ? "yes" : "no");
+  printf("Optimized:    %s\n", doc->isLinearized() ? "yes" : "no");
+
+  // print PDF version
+  printf("PDF version:  %.1f\n", doc->getPDFVersion());
+
+  // print the metadata
+  if (printMetadata && (metadata = doc->readMetadata())) {
+    fputs("Metadata:\n", stdout);
+    fputs(metadata->getCString(), stdout);
+    fputc('\n', stdout);
+    delete metadata;
+  }
+
+  exitCode = 0;
 
   // clean up
 
   // clean up
+ err2:
+  uMap->decRefCnt();
   delete doc;
   delete doc;
-  freeParams();
+ err1:
+  delete globalParams;
+ err0:
 
   // check for memory leaks
   Object::memCheck(stderr);
   gMemReport(stderr);
 
 
   // check for memory leaks
   Object::memCheck(stderr);
   gMemReport(stderr);
 
-  return 0;
+  return exitCode;
 }
 
 }
 
-static void printInfoString(Dict *infoDict, char *key, char *fmt) {
+static void printInfoString(Dict *infoDict, char *key, char *text,
+                           UnicodeMap *uMap) {
   Object obj;
   Object obj;
-  GString *s1, *s2;
-  int i;
+  GString *s1;
+  GBool isUnicode;
+  Unicode u;
+  char buf[8];
+  int i, n;
 
   if (infoDict->lookup(key, &obj)->isString()) {
 
   if (infoDict->lookup(key, &obj)->isString()) {
+    fputs(text, stdout);
     s1 = obj.getString();
     if ((s1->getChar(0) & 0xff) == 0xfe &&
        (s1->getChar(1) & 0xff) == 0xff) {
     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;
+      isUnicode = gTrue;
+      i = 2;
     } else {
     } else {
-      printf(fmt, s1->getCString());
+      isUnicode = gFalse;
+      i = 0;
+    }
+    while (i < obj.getString()->getLength()) {
+      if (isUnicode) {
+       u = ((s1->getChar(i) & 0xff) << 8) |
+           (s1->getChar(i+1) & 0xff);
+       i += 2;
+      } else {
+       u = s1->getChar(i) & 0xff;
+       ++i;
+      }
+      n = uMap->mapUnicode(u, buf, sizeof(buf));
+      fwrite(buf, 1, n, stdout);
     }
     }
+    fputc('\n', stdout);
   }
   obj.free();
 }
 
   }
   obj.free();
 }
 
-static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
+static void printInfoDate(Dict *infoDict, char *key, char *text) {
   Object obj;
   char *s;
   Object obj;
   char *s;
+  int year, mon, day, hour, min, sec;
+  struct tm tmStruct;
+  char buf[256];
 
   if (infoDict->lookup(key, &obj)->isString()) {
 
   if (infoDict->lookup(key, &obj)->isString()) {
+    fputs(text, stdout);
     s = obj.getString()->getCString();
     if (s[0] == 'D' && s[1] == ':') {
       s += 2;
     }
     s = obj.getString()->getCString();
     if (s[0] == 'D' && s[1] == ':') {
       s += 2;
     }
-    printf(fmt, s);
+    if (sscanf(s, "%4d%2d%2d%2d%2d%2d",
+              &year, &mon, &day, &hour, &min, &sec) == 6) {
+      tmStruct.tm_year = year - 1900;
+      tmStruct.tm_mon = mon - 1;
+      tmStruct.tm_mday = day;
+      tmStruct.tm_hour = hour;
+      tmStruct.tm_min = min;
+      tmStruct.tm_sec = sec;
+      tmStruct.tm_wday = -1;
+      tmStruct.tm_yday = -1;
+      tmStruct.tm_isdst = -1;
+      // compute the tm_wday and tm_yday fields
+      if (mktime(&tmStruct) != (time_t)-1 &&
+         strftime(buf, sizeof(buf), "%c", &tmStruct)) {
+       fputs(buf, stdout);
+      } else {
+       fputs(s, stdout);
+      }
+    } else {
+      fputs(s, stdout);
+    }
+    fputc('\n', stdout);
   }
   obj.free();
 }
   }
   obj.free();
 }