~ubuntu-branches/ubuntu/vivid/emscripten/vivid

« back to all changes in this revision

Viewing changes to tests/poppler/utils/pdfinfo.cc

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2013-05-02 13:11:51 UTC
  • Revision ID: package-import@ubuntu.com-20130502131151-q8dvteqr1ef2x7xz
Tags: upstream-1.4.1~20130504~adb56cb
ImportĀ upstreamĀ versionĀ 1.4.1~20130504~adb56cb

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//========================================================================
 
2
//
 
3
// pdfinfo.cc
 
4
//
 
5
// Copyright 1998-2003 Glyph & Cog, LLC
 
6
//
 
7
//========================================================================
 
8
 
 
9
//========================================================================
 
10
//
 
11
// Modified under the Poppler project - http://poppler.freedesktop.org
 
12
//
 
13
// All changes made under the Poppler project to this file are licensed
 
14
// under GPL version 2 or later
 
15
//
 
16
// Copyright (C) 2006 Dom Lachowicz <cinamod@hotmail.com>
 
17
// Copyright (C) 2007-2010 Albert Astals Cid <aacid@kde.org>
 
18
// Copyright (C) 2010 Hib Eris <hib@hiberis.nl>
 
19
//
 
20
// To see a description of the changes please see the Changelog file that
 
21
// came with your tarball or type make ChangeLog if you are building from git
 
22
//
 
23
//========================================================================
 
24
 
 
25
#include "config.h"
 
26
#include <poppler-config.h>
 
27
#include <stdio.h>
 
28
#include <stdlib.h>
 
29
#include <stddef.h>
 
30
#include <string.h>
 
31
#include <time.h>
 
32
#include <math.h>
 
33
#include "parseargs.h"
 
34
#include "printencodings.h"
 
35
#include "goo/GooString.h"
 
36
#include "goo/gmem.h"
 
37
#include "GlobalParams.h"
 
38
#include "Object.h"
 
39
#include "Stream.h"
 
40
#include "Array.h"
 
41
#include "Dict.h"
 
42
#include "XRef.h"
 
43
#include "Catalog.h"
 
44
#include "Page.h"
 
45
#include "PDFDoc.h"
 
46
#include "PDFDocFactory.h"
 
47
#include "CharTypes.h"
 
48
#include "UnicodeMap.h"
 
49
#include "PDFDocEncoding.h"
 
50
#include "Error.h"
 
51
#include "DateInfo.h"
 
52
 
 
53
static void printInfoString(Dict *infoDict, char *key, char *text,
 
54
                            UnicodeMap *uMap);
 
55
static void printInfoDate(Dict *infoDict, char *key, char *text);
 
56
static void printBox(char *text, PDFRectangle *box);
 
57
 
 
58
static int firstPage = 1;
 
59
static int lastPage = 0;
 
60
static GBool printBoxes = gFalse;
 
61
static GBool printMetadata = gFalse;
 
62
static char textEncName[128] = "";
 
63
static char ownerPassword[33] = "\001";
 
64
static char userPassword[33] = "\001";
 
65
static GBool printVersion = gFalse;
 
66
static GBool printHelp = gFalse;
 
67
static GBool printEnc = gFalse;
 
68
 
 
69
static const ArgDesc argDesc[] = {
 
70
  {"-f",      argInt,      &firstPage,        0,
 
71
   "first page to convert"},
 
72
  {"-l",      argInt,      &lastPage,         0,
 
73
   "last page to convert"},
 
74
  {"-box",    argFlag,     &printBoxes,       0,
 
75
   "print the page bounding boxes"},
 
76
  {"-meta",   argFlag,     &printMetadata,    0,
 
77
   "print the document metadata (XML)"},
 
78
  {"-enc",    argString,   textEncName,    sizeof(textEncName),
 
79
   "output text encoding name"},
 
80
  {"-listenc",argFlag,     &printEnc,      0,
 
81
   "list available encodings"},
 
82
  {"-opw",    argString,   ownerPassword,  sizeof(ownerPassword),
 
83
   "owner password (for encrypted files)"},
 
84
  {"-upw",    argString,   userPassword,   sizeof(userPassword),
 
85
   "user password (for encrypted files)"},
 
86
  {"-v",      argFlag,     &printVersion,  0,
 
87
   "print copyright and version info"},
 
88
  {"-h",      argFlag,     &printHelp,     0,
 
89
   "print usage information"},
 
90
  {"-help",   argFlag,     &printHelp,     0,
 
91
   "print usage information"},
 
92
  {"--help",  argFlag,     &printHelp,     0,
 
93
   "print usage information"},
 
94
  {"-?",      argFlag,     &printHelp,     0,
 
95
   "print usage information"},
 
96
  {NULL}
 
97
};
 
98
 
 
99
int main(int argc, char *argv[]) {
 
100
  PDFDoc *doc;
 
101
  GooString *fileName;
 
102
  GooString *ownerPW, *userPW;
 
103
  UnicodeMap *uMap;
 
104
  Page *page;
 
105
  Object info;
 
106
  char buf[256];
 
107
  double w, h, wISO, hISO;
 
108
  FILE *f;
 
109
  GooString *metadata;
 
110
  GBool ok;
 
111
  int exitCode;
 
112
  int pg, i;
 
113
  GBool multiPage;
 
114
 
 
115
  exitCode = 99;
 
116
 
 
117
  // parse args
 
118
  ok = parseArgs(argDesc, &argc, argv);
 
119
  if (!ok || (argc != 2 && !printEnc) || printVersion || printHelp) {
 
120
    fprintf(stderr, "pdfinfo version %s\n", PACKAGE_VERSION);
 
121
    fprintf(stderr, "%s\n", popplerCopyright);
 
122
    fprintf(stderr, "%s\n", xpdfCopyright);
 
123
    if (!printVersion) {
 
124
      printUsage("pdfinfo", "<PDF-file>", argDesc);
 
125
    }
 
126
    if (printVersion || printHelp)
 
127
      exitCode = 0;
 
128
    goto err0;
 
129
  }
 
130
 
 
131
  // read config file
 
132
  globalParams = new GlobalParams();
 
133
 
 
134
  if (printEnc) {
 
135
    printEncodings();
 
136
    delete globalParams;
 
137
    exitCode = 0;
 
138
    goto err0;
 
139
  }
 
140
 
 
141
  fileName = new GooString(argv[1]);
 
142
 
 
143
  if (textEncName[0]) {
 
144
    globalParams->setTextEncoding(textEncName);
 
145
  }
 
146
 
 
147
  // get mapping to output encoding
 
148
  if (!(uMap = globalParams->getTextEncoding())) {
 
149
    error(-1, "Couldn't get text encoding");
 
150
    delete fileName;
 
151
    goto err1;
 
152
  }
 
153
 
 
154
  // open PDF file
 
155
  if (ownerPassword[0] != '\001') {
 
156
    ownerPW = new GooString(ownerPassword);
 
157
  } else {
 
158
    ownerPW = NULL;
 
159
  }
 
160
  if (userPassword[0] != '\001') {
 
161
    userPW = new GooString(userPassword);
 
162
  } else {
 
163
    userPW = NULL;
 
164
  }
 
165
 
 
166
  if (fileName->cmp("-") == 0) {
 
167
      delete fileName;
 
168
      fileName = new GooString("fd://0");
 
169
  }
 
170
 
 
171
  doc = PDFDocFactory().createPDFDoc(*fileName, ownerPW, userPW);
 
172
 
 
173
  if (userPW) {
 
174
    delete userPW;
 
175
  }
 
176
  if (ownerPW) {
 
177
    delete ownerPW;
 
178
  }
 
179
  if (!doc->isOk()) {
 
180
    exitCode = 1;
 
181
    goto err2;
 
182
  }
 
183
 
 
184
  // get page range
 
185
  if (firstPage < 1) {
 
186
    firstPage = 1;
 
187
  }
 
188
  if (lastPage == 0) {
 
189
    multiPage = gFalse;
 
190
    lastPage = 1;
 
191
  } else {
 
192
    multiPage = gTrue;
 
193
  }
 
194
  if (lastPage < 1 || lastPage > doc->getNumPages()) {
 
195
    lastPage = doc->getNumPages();
 
196
  }
 
197
 
 
198
  // print doc info
 
199
  doc->getDocInfo(&info);
 
200
  if (info.isDict()) {
 
201
    printInfoString(info.getDict(), "Title",        "Title:          ", uMap);
 
202
    printInfoString(info.getDict(), "Subject",      "Subject:        ", uMap);
 
203
    printInfoString(info.getDict(), "Keywords",     "Keywords:       ", uMap);
 
204
    printInfoString(info.getDict(), "Author",       "Author:         ", uMap);
 
205
    printInfoString(info.getDict(), "Creator",      "Creator:        ", uMap);
 
206
    printInfoString(info.getDict(), "Producer",     "Producer:       ", uMap);
 
207
    printInfoDate(info.getDict(),   "CreationDate", "CreationDate:   ");
 
208
    printInfoDate(info.getDict(),   "ModDate",      "ModDate:        ");
 
209
  }
 
210
  info.free();
 
211
 
 
212
  // print tagging info
 
213
  printf("Tagged:         %s\n",
 
214
         doc->getStructTreeRoot()->isDict() ? "yes" : "no");
 
215
 
 
216
  // print page count
 
217
  printf("Pages:          %d\n", doc->getNumPages());
 
218
 
 
219
  // print encryption info
 
220
  printf("Encrypted:      ");
 
221
  if (doc->isEncrypted()) {
 
222
    printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
 
223
           doc->okToPrint(gTrue) ? "yes" : "no",
 
224
           doc->okToCopy(gTrue) ? "yes" : "no",
 
225
           doc->okToChange(gTrue) ? "yes" : "no",
 
226
           doc->okToAddNotes(gTrue) ? "yes" : "no");
 
227
  } else {
 
228
    printf("no\n");
 
229
  }
 
230
 
 
231
  // print page size
 
232
  for (pg = firstPage; pg <= lastPage; ++pg) {
 
233
    w = doc->getPageCropWidth(pg);
 
234
    h = doc->getPageCropHeight(pg);
 
235
    if (multiPage) {
 
236
      printf("Page %4d size: %g x %g pts", pg, w, h);
 
237
    } else {
 
238
      printf("Page size:      %g x %g pts", w, h);
 
239
    }
 
240
    if ((fabs(w - 612) < 0.1 && fabs(h - 792) < 0.1) ||
 
241
        (fabs(w - 792) < 0.1 && fabs(h - 612) < 0.1)) {
 
242
      printf(" (letter)");
 
243
    } else {
 
244
      hISO = sqrt(sqrt(2.0)) * 7200 / 2.54;
 
245
      wISO = hISO / sqrt(2.0);
 
246
      for (i = 0; i <= 6; ++i) {
 
247
        if ((fabs(w - wISO) < 1 && fabs(h - hISO) < 1) ||
 
248
            (fabs(w - hISO) < 1 && fabs(h - wISO) < 1)) {
 
249
          printf(" (A%d)", i);
 
250
          break;
 
251
        }
 
252
        hISO = wISO;
 
253
        wISO /= sqrt(2.0);
 
254
      }
 
255
    }
 
256
    printf("\n");
 
257
  } 
 
258
 
 
259
  // print the boxes
 
260
  if (printBoxes) {
 
261
    if (multiPage) {
 
262
      for (pg = firstPage; pg <= lastPage; ++pg) {
 
263
        page = doc->getPage(pg);
 
264
        if (!page) {
 
265
          error(-1, "Failed to print boxes for page %d", pg);
 
266
          continue;
 
267
        }
 
268
        sprintf(buf, "Page %4d MediaBox: ", pg);
 
269
        printBox(buf, page->getMediaBox());
 
270
        sprintf(buf, "Page %4d CropBox:  ", pg);
 
271
        printBox(buf, page->getCropBox());
 
272
        sprintf(buf, "Page %4d BleedBox: ", pg);
 
273
        printBox(buf, page->getBleedBox());
 
274
        sprintf(buf, "Page %4d TrimBox:  ", pg);
 
275
        printBox(buf, page->getTrimBox());
 
276
        sprintf(buf, "Page %4d ArtBox:   ", pg);
 
277
        printBox(buf, page->getArtBox());
 
278
      }
 
279
    } else {
 
280
      page = doc->getPage(firstPage);
 
281
      if (!page) {
 
282
        error(-1, "Failed to print boxes for page %d", firstPage);
 
283
      } else {
 
284
        printBox("MediaBox:       ", page->getMediaBox());
 
285
        printBox("CropBox:        ", page->getCropBox());
 
286
        printBox("BleedBox:       ", page->getBleedBox());
 
287
        printBox("TrimBox:        ", page->getTrimBox());
 
288
        printBox("ArtBox:         ", page->getArtBox());
 
289
      }
 
290
    }
 
291
  }
 
292
 
 
293
  // print file size
 
294
#ifdef VMS
 
295
  f = fopen(fileName->getCString(), "rb", "ctx=stm");
 
296
#else
 
297
  f = fopen(fileName->getCString(), "rb");
 
298
#endif
 
299
  if (f) {
 
300
#if HAVE_FSEEKO
 
301
    fseeko(f, 0, SEEK_END);
 
302
    printf("File size:      %u bytes\n", (Guint)ftello(f));
 
303
#elif HAVE_FSEEK64
 
304
    fseek64(f, 0, SEEK_END);
 
305
    printf("File size:      %u bytes\n", (Guint)ftell64(f));
 
306
#else
 
307
    fseek(f, 0, SEEK_END);
 
308
    printf("File size:      %d bytes\n", (int)ftell(f));
 
309
#endif
 
310
    fclose(f);
 
311
  }
 
312
 
 
313
  // print linearization info
 
314
  printf("Optimized:      %s\n", doc->isLinearized() ? "yes" : "no");
 
315
 
 
316
  // print PDF version
 
317
  printf("PDF version:    %d.%d\n", doc->getPDFMajorVersion(), doc->getPDFMinorVersion());
 
318
 
 
319
  // print the metadata
 
320
  if (printMetadata && (metadata = doc->readMetadata())) {
 
321
    fputs("Metadata:\n", stdout);
 
322
    fputs(metadata->getCString(), stdout);
 
323
    fputc('\n', stdout);
 
324
    delete metadata;
 
325
  }
 
326
 
 
327
  exitCode = 0;
 
328
 
 
329
  // clean up
 
330
 err2:
 
331
  uMap->decRefCnt();
 
332
  delete doc;
 
333
  delete fileName;
 
334
 err1:
 
335
  delete globalParams;
 
336
 err0:
 
337
 
 
338
  // check for memory leaks
 
339
  Object::memCheck(stderr);
 
340
  gMemReport(stderr);
 
341
 
 
342
  return exitCode;
 
343
}
 
344
 
 
345
static void printInfoString(Dict *infoDict, char *key, char *text,
 
346
                            UnicodeMap *uMap) {
 
347
  Object obj;
 
348
  GooString *s1;
 
349
  GBool isUnicode;
 
350
  Unicode u;
 
351
  char buf[8];
 
352
  int i, n;
 
353
 
 
354
  if (infoDict->lookup(key, &obj)->isString()) {
 
355
    fputs(text, stdout);
 
356
    s1 = obj.getString();
 
357
    if ((s1->getChar(0) & 0xff) == 0xfe &&
 
358
        (s1->getChar(1) & 0xff) == 0xff) {
 
359
      isUnicode = gTrue;
 
360
      i = 2;
 
361
    } else {
 
362
      isUnicode = gFalse;
 
363
      i = 0;
 
364
    }
 
365
    while (i < obj.getString()->getLength()) {
 
366
      if (isUnicode) {
 
367
        u = ((s1->getChar(i) & 0xff) << 8) |
 
368
            (s1->getChar(i+1) & 0xff);
 
369
        i += 2;
 
370
      } else {
 
371
        u = pdfDocEncoding[s1->getChar(i) & 0xff];
 
372
        ++i;
 
373
      }
 
374
      n = uMap->mapUnicode(u, buf, sizeof(buf));
 
375
      fwrite(buf, 1, n, stdout);
 
376
    }
 
377
    fputc('\n', stdout);
 
378
  }
 
379
  obj.free();
 
380
}
 
381
 
 
382
static void printInfoDate(Dict *infoDict, char *key, char *text) {
 
383
  Object obj;
 
384
  char *s;
 
385
  int year, mon, day, hour, min, sec, tz_hour, tz_minute;
 
386
  char tz;
 
387
  struct tm tmStruct;
 
388
  char buf[256];
 
389
 
 
390
  if (infoDict->lookup(key, &obj)->isString()) {
 
391
    fputs(text, stdout);
 
392
    s = obj.getString()->getCString();
 
393
    // TODO do something with the timezone info
 
394
    if ( parseDateString( s, &year, &mon, &day, &hour, &min, &sec, &tz, &tz_hour, &tz_minute ) ) {
 
395
      tmStruct.tm_year = year - 1900;
 
396
      tmStruct.tm_mon = mon - 1;
 
397
      tmStruct.tm_mday = day;
 
398
      tmStruct.tm_hour = hour;
 
399
      tmStruct.tm_min = min;
 
400
      tmStruct.tm_sec = sec;
 
401
      tmStruct.tm_wday = -1;
 
402
      tmStruct.tm_yday = -1;
 
403
      tmStruct.tm_isdst = -1;
 
404
      // compute the tm_wday and tm_yday fields
 
405
      if (mktime(&tmStruct) != (time_t)-1 &&
 
406
          strftime(buf, sizeof(buf), "%c", &tmStruct)) {
 
407
        fputs(buf, stdout);
 
408
      } else {
 
409
        fputs(s, stdout);
 
410
      }
 
411
    } else {
 
412
      fputs(s, stdout);
 
413
    }
 
414
    fputc('\n', stdout);
 
415
  }
 
416
  obj.free();
 
417
}
 
418
 
 
419
static void printBox(char *text, PDFRectangle *box) {
 
420
  printf("%s%8.2f %8.2f %8.2f %8.2f\n",
 
421
         text, box->x1, box->y1, box->x2, box->y2);
 
422
}