]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/PDFDoc.cc
Added annots display CB.
[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   // check header
118   checkHeader();
119
120   // read xref table
121   xref = new XRef(str, ownerPassword, userPassword);
122   if (!xref->isOk()) {
123     error(-1, "Couldn't read xref table");
124     errCode = xref->getErrorCode();
125     return gFalse;
126   }
127
128   // read catalog
129   catalog = new Catalog(xref);
130   if (!catalog->isOk()) {
131     error(-1, "Couldn't read page catalog");
132     errCode = errBadCatalog;
133     return gFalse;
134   }
135
136 #ifndef DISABLE_OUTLINE
137   // read outline
138   outline = new Outline(catalog->getOutline(), xref);
139 #endif
140
141   // done
142   return gTrue;
143 }
144
145 PDFDoc::~PDFDoc() {
146 #ifndef DISABLE_OUTLINE
147   if (outline) {
148     delete outline;
149   }
150 #endif
151   if (catalog) {
152     delete catalog;
153   }
154   if (xref) {
155     delete xref;
156   }
157   if (str) {
158     delete str;
159   }
160   if (file) {
161     fclose(file);
162   }
163   if (fileName) {
164     delete fileName;
165   }
166   if (links) {
167     delete links;
168   }
169 }
170
171 // Check for a PDF header on this stream.  Skip past some garbage
172 // if necessary.
173 void PDFDoc::checkHeader() {
174   char hdrBuf[headerSearchSize+1];
175   char *p;
176   int i;
177
178   pdfVersion = 0;
179   for (i = 0; i < headerSearchSize; ++i) {
180     hdrBuf[i] = str->getChar();
181   }
182   hdrBuf[headerSearchSize] = '\0';
183   for (i = 0; i < headerSearchSize - 5; ++i) {
184     if (!strncmp(&hdrBuf[i], "%PDF-", 5)) {
185       break;
186     }
187   }
188   if (i >= headerSearchSize - 5) {
189     error(-1, "May not be a PDF file (continuing anyway)");
190     return;
191   }
192   str->moveStart(i);
193   p = strtok(&hdrBuf[i+5], " \t\n\r");
194   {
195     char *theLocale = setlocale(LC_NUMERIC, "C");
196     pdfVersion = atof(p);
197     setlocale(LC_NUMERIC, theLocale);
198   }
199   if (!(hdrBuf[i+5] >= '0' && hdrBuf[i+5] <= '9') ||
200       pdfVersion > supportedPDFVersionNum + 0.0001) {
201     error(-1, "PDF version %s -- xpdf supports version %s"
202           " (continuing anyway)", p, supportedPDFVersionStr);
203   }
204 }
205
206 void PDFDoc::displayPage(OutputDev *out, int page, double zoom,
207                          int rotate, GBool doLinks,
208                          GBool (*abortCheckCbk)(void *data),
209                          void *abortCheckCbkData,
210                          GBool (*annotDisplayDecideCbk)(Annot *annot, void *user_data),
211                          void *annotDisplayDecideCbkData) {
212   Page *p;
213
214   if (globalParams->getPrintCommands()) {
215     printf("***** page %d *****\n", page);
216   }
217   p = catalog->getPage(page);
218   if (doLinks) {
219     if (links) {
220       delete links;
221       links = NULL;
222     }
223     getLinks(p);
224     p->display(out, zoom, rotate, links, catalog,
225                abortCheckCbk, abortCheckCbkData,
226                annotDisplayDecideCbk, annotDisplayDecideCbkData);
227   } else {
228     p->display(out, zoom, rotate, NULL, catalog,
229                abortCheckCbk, abortCheckCbkData,
230                annotDisplayDecideCbk, annotDisplayDecideCbkData);
231   }
232 }
233
234 void PDFDoc::displayPages(OutputDev *out, int firstPage, int lastPage,
235                           int zoom, int rotate, GBool doLinks,
236                           GBool (*abortCheckCbk)(void *data),
237                           void *abortCheckCbkData,
238                           GBool (*annotDisplayDecideCbk)(Annot *annot, void *user_data),
239                           void *annotDisplayDecideCbkData) {
240   int page;
241
242   for (page = firstPage; page <= lastPage; ++page) {
243     displayPage(out, page, zoom, rotate, doLinks,
244                 abortCheckCbk, abortCheckCbkData,
245                 annotDisplayDecideCbk, annotDisplayDecideCbkData);
246   }
247 }
248
249 void PDFDoc::displayPageSlice(OutputDev *out, int page, double zoom,
250                               int rotate, int sliceX, int sliceY,
251                               int sliceW, int sliceH,
252                               GBool (*abortCheckCbk)(void *data),
253                               void *abortCheckCbkData,
254                               GBool (*annotDisplayDecideCbk)(Annot *annot, void *user_data),
255                               void *annotDisplayDecideCbkData) {
256   Page *p;
257
258   p = catalog->getPage(page);
259   p->displaySlice(out, zoom, rotate, sliceX, sliceY, sliceW, sliceH,
260                   NULL, catalog,
261                   abortCheckCbk, abortCheckCbkData,
262                   annotDisplayDecideCbk, annotDisplayDecideCbkData);
263 }
264
265 GBool PDFDoc::isLinearized() {
266   Parser *parser;
267   Object obj1, obj2, obj3, obj4, obj5;
268   GBool lin;
269
270   lin = gFalse;
271   obj1.initNull();
272   parser = new Parser(xref,
273              new Lexer(xref,
274                str->makeSubStream(str->getStart(), gFalse, 0, &obj1)));
275   parser->getObj(&obj1);
276   parser->getObj(&obj2);
277   parser->getObj(&obj3);
278   parser->getObj(&obj4);
279   if (obj1.isInt() && obj2.isInt() && obj3.isCmd("obj") &&
280       obj4.isDict()) {
281     obj4.dictLookup("Linearized", &obj5);
282     if (obj5.isNum() && obj5.getNum() > 0) {
283       lin = gTrue;
284     }
285     obj5.free();
286   }
287   obj4.free();
288   obj3.free();
289   obj2.free();
290   obj1.free();
291   delete parser;
292   return lin;
293 }
294
295 GBool PDFDoc::saveAs(GString *name) {
296   FILE *f;
297   int c;
298
299   if (!(f = fopen(name->getCString(), "wb"))) {
300     error(-1, "Couldn't open file '%s'", name->getCString());
301     return gFalse;
302   }
303   str->reset();
304   while ((c = str->getChar()) != EOF) {
305     fputc(c, f);
306   }
307   str->close();
308   fclose(f);
309   return gTrue;
310 }
311
312 void PDFDoc::getLinks(Page *page) {
313   Object obj;
314
315   links = new Links(page->getAnnots(&obj), catalog->getBaseURI());
316   obj.free();
317 }