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