]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/PDFDoc.cc
new widget: table with labels displaying properties of PDFs
[evince.git] / pdf / xpdf / PDFDoc.cc
1 //========================================================================
2 //
3 // PDFDoc.cc
4 //
5 // Copyright 1996-2002 Glyph & Cog, LLC
6 //
7 //========================================================================
8
9 #ifdef __GNUC__
10 #pragma implementation
11 #endif
12
13 #include <aconf.h>
14 #include <locale.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <stddef.h>
18 #include <string.h>
19 #include "GString.h"
20 #include "config.h"
21 #include "Page.h"
22 #include "Catalog.h"
23 #include "Stream.h"
24 #include "XRef.h"
25 #include "Link.h"
26 #include "OutputDev.h"
27 #include "Error.h"
28 #include "ErrorCodes.h"
29 #include "Lexer.h"
30 #include "Parser.h"
31 #include "PDFDoc.h"
32
33 //------------------------------------------------------------------------
34
35 #define headerSearchSize 1024   // read this many bytes at beginning of
36                                 //   file to look for '%PDF'
37
38 //------------------------------------------------------------------------
39 // PDFDoc
40 //------------------------------------------------------------------------
41
42 PDFDoc::PDFDoc(GString *fileNameA, GString *ownerPassword,
43                GString *userPassword, GBool printCommandsA) {
44   Object obj;
45   GString *fileName2;
46
47   ok = gFalse;
48   errCode = errNone;
49
50   file = NULL;
51   str = NULL;
52   xref = NULL;
53   catalog = NULL;
54   links = NULL;
55   printCommands = printCommandsA;
56
57   // try to open file
58   fileName = fileNameA;
59   fileName2 = NULL;
60 #ifdef VMS
61   if (!(file = fopen(fileName->getCString(), "rb", "ctx=stm"))) {
62     error(-1, "Couldn't open file '%s'", fileName->getCString());
63     errCode = errOpenFile;
64     return;
65   }
66 #else
67   if (!(file = fopen(fileName->getCString(), "rb"))) {
68     fileName2 = fileName->copy();
69     fileName2->lowerCase();
70     if (!(file = fopen(fileName2->getCString(), "rb"))) {
71       fileName2->upperCase();
72       if (!(file = fopen(fileName2->getCString(), "rb"))) {
73         error(-1, "Couldn't open file '%s'", fileName->getCString());
74         delete fileName2;
75         errCode = errOpenFile;
76         return;
77       }
78     }
79     delete fileName2;
80   }
81 #endif
82
83   // create stream
84   obj.initNull();
85   str = new FileStream(file, 0, gFalse, 0, &obj);
86
87   ok = setup(ownerPassword, userPassword);
88 }
89
90 PDFDoc::PDFDoc(BaseStream *strA, GString *ownerPassword,
91                GString *userPassword, GBool printCommandsA) {
92   ok = gFalse;
93   errCode = errNone;
94   fileName = NULL;
95   file = NULL;
96   str = strA;
97   xref = NULL;
98   catalog = NULL;
99   links = NULL;
100   printCommands = printCommandsA;
101   ok = setup(ownerPassword, userPassword);
102 }
103
104 GBool PDFDoc::setup(GString *ownerPassword, GString *userPassword) {
105   // check header
106   checkHeader();
107
108   // read xref table
109   xref = new XRef(str, ownerPassword, userPassword);
110   if (!xref->isOk()) {
111     error(-1, "Couldn't read xref table");
112     errCode = xref->getErrorCode();
113     return gFalse;
114   }
115
116   // read catalog
117   catalog = new Catalog(xref, printCommands);
118   if (!catalog->isOk()) {
119     error(-1, "Couldn't read page catalog");
120     errCode = errBadCatalog;
121     return gFalse;
122   }
123
124   // done
125   return gTrue;
126 }
127
128 PDFDoc::~PDFDoc() {
129   if (catalog) {
130     delete catalog;
131   }
132   if (xref) {
133     delete xref;
134   }
135   if (str) {
136     delete str;
137   }
138   if (file) {
139     fclose(file);
140   }
141   if (fileName) {
142     delete fileName;
143   }
144   if (links) {
145     delete links;
146   }
147 }
148
149 // Check for a PDF header on this stream.  Skip past some garbage
150 // if necessary.
151 void PDFDoc::checkHeader() {
152   char hdrBuf[headerSearchSize+1];
153   char *p;
154   int i;
155
156   pdfVersion = 0;
157   for (i = 0; i < headerSearchSize; ++i) {
158     hdrBuf[i] = str->getChar();
159   }
160   hdrBuf[headerSearchSize] = '\0';
161   for (i = 0; i < headerSearchSize - 5; ++i) {
162     if (!strncmp(&hdrBuf[i], "%PDF-", 5)) {
163       break;
164     }
165   }
166   if (i >= headerSearchSize - 5) {
167     error(-1, "May not be a PDF file (continuing anyway)");
168     return;
169   }
170   str->moveStart(i);
171   p = strtok(&hdrBuf[i+5], " \t\n\r");
172   {
173     char *theLocale = setlocale(LC_NUMERIC, "C");
174     pdfVersion = atof(p);
175     setlocale(LC_NUMERIC, theLocale);
176   }
177   if (!(hdrBuf[i+5] >= '0' && hdrBuf[i+5] <= '9') ||
178       pdfVersion > supportedPDFVersionNum + 0.0001) {
179     error(-1, "PDF version %s -- xpdf supports version %s"
180           " (continuing anyway)", p, supportedPDFVersionStr);
181   }
182 }
183
184 void PDFDoc::displayPage(OutputDev *out, int page, double zoom,
185                          int rotate, GBool doLinks) {
186   Page *p;
187
188   if (printCommands) {
189     printf("***** page %d *****\n", page);
190   }
191   p = catalog->getPage(page);
192   if (doLinks) {
193     if (links) {
194       delete links;
195       links = NULL;
196     }
197     getLinks(p);
198     p->display(out, zoom, rotate, links, catalog);
199   } else {
200     p->display(out, zoom, rotate, NULL, catalog);
201   }
202 }
203
204 void PDFDoc::displayPages(OutputDev *out, int firstPage, int lastPage,
205                           int zoom, int rotate, GBool doLinks) {
206   int page;
207
208   for (page = firstPage; page <= lastPage; ++page) {
209     displayPage(out, page, zoom, rotate, doLinks);
210   }
211 }
212
213 GBool PDFDoc::isLinearized() {
214   Parser *parser;
215   Object obj1, obj2, obj3, obj4, obj5;
216   GBool lin;
217
218   lin = gFalse;
219   obj1.initNull();
220   parser = new Parser(xref,
221              new Lexer(xref,
222                str->makeSubStream(str->getStart(), gFalse, 0, &obj1)));
223   parser->getObj(&obj1);
224   parser->getObj(&obj2);
225   parser->getObj(&obj3);
226   parser->getObj(&obj4);
227   if (obj1.isInt() && obj2.isInt() && obj3.isCmd("obj") &&
228       obj4.isDict()) {
229     obj4.dictLookup("Linearized", &obj5);
230     if (obj5.isNum() && obj5.getNum() > 0) {
231       lin = gTrue;
232     }
233     obj5.free();
234   }
235   obj4.free();
236   obj3.free();
237   obj2.free();
238   obj1.free();
239   delete parser;
240   return lin;
241 }
242
243 GBool PDFDoc::saveAs(GString *name) {
244   FILE *f;
245   int c;
246
247   if (!(f = fopen(name->getCString(), "wb"))) {
248     error(-1, "Couldn't open file '%s'", name->getCString());
249     return gFalse;
250   }
251   str->reset();
252   while ((c = str->getChar()) != EOF) {
253     fputc(c, f);
254   }
255   str->close();
256   fclose(f);
257   return gTrue;
258 }
259
260 void PDFDoc::getLinks(Page *page) {
261   Object obj;
262
263   links = new Links(page->getAnnots(&obj), catalog->getBaseURI());
264   obj.free();
265 }