]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/PDFDoc.cc
Merge with Xpdf 2.02 and make it build
[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   Page *p;
211
212   if (globalParams->getPrintCommands()) {
213     printf("***** page %d *****\n", page);
214   }
215   p = catalog->getPage(page);
216   if (doLinks) {
217     if (links) {
218       delete links;
219       links = NULL;
220     }
221     getLinks(p);
222     p->display(out, zoom, rotate, links, catalog,
223                abortCheckCbk, abortCheckCbkData);
224   } else {
225     p->display(out, zoom, rotate, NULL, catalog,
226                abortCheckCbk, abortCheckCbkData);
227   }
228 }
229
230 void PDFDoc::displayPages(OutputDev *out, int firstPage, int lastPage,
231                           int zoom, int rotate, GBool doLinks,
232                           GBool (*abortCheckCbk)(void *data),
233                           void *abortCheckCbkData) {
234   int page;
235
236   for (page = firstPage; page <= lastPage; ++page) {
237     displayPage(out, page, zoom, rotate, doLinks,
238                 abortCheckCbk, abortCheckCbkData);
239   }
240 }
241
242 void PDFDoc::displayPageSlice(OutputDev *out, int page, double zoom,
243                               int rotate, int sliceX, int sliceY,
244                               int sliceW, int sliceH,
245                               GBool (*abortCheckCbk)(void *data),
246                               void *abortCheckCbkData) {
247   Page *p;
248
249   p = catalog->getPage(page);
250   p->displaySlice(out, zoom, rotate, sliceX, sliceY, sliceW, sliceH,
251                   NULL, catalog, abortCheckCbk, abortCheckCbkData);
252 }
253
254 GBool PDFDoc::isLinearized() {
255   Parser *parser;
256   Object obj1, obj2, obj3, obj4, obj5;
257   GBool lin;
258
259   lin = gFalse;
260   obj1.initNull();
261   parser = new Parser(xref,
262              new Lexer(xref,
263                str->makeSubStream(str->getStart(), gFalse, 0, &obj1)));
264   parser->getObj(&obj1);
265   parser->getObj(&obj2);
266   parser->getObj(&obj3);
267   parser->getObj(&obj4);
268   if (obj1.isInt() && obj2.isInt() && obj3.isCmd("obj") &&
269       obj4.isDict()) {
270     obj4.dictLookup("Linearized", &obj5);
271     if (obj5.isNum() && obj5.getNum() > 0) {
272       lin = gTrue;
273     }
274     obj5.free();
275   }
276   obj4.free();
277   obj3.free();
278   obj2.free();
279   obj1.free();
280   delete parser;
281   return lin;
282 }
283
284 GBool PDFDoc::saveAs(GString *name) {
285   FILE *f;
286   int c;
287
288   if (!(f = fopen(name->getCString(), "wb"))) {
289     error(-1, "Couldn't open file '%s'", name->getCString());
290     return gFalse;
291   }
292   str->reset();
293   while ((c = str->getChar()) != EOF) {
294     fputc(c, f);
295   }
296   str->close();
297   fclose(f);
298   return gTrue;
299 }
300
301 void PDFDoc::getLinks(Page *page) {
302   Object obj;
303
304   links = new Links(page->getAnnots(&obj), catalog->getBaseURI());
305   obj.free();
306 }