1 //========================================================================
5 // Copyright 1996-2003 Glyph & Cog, LLC
7 //========================================================================
11 #ifdef USE_GCC_PRAGMAS
12 #pragma implementation
22 #include "GlobalParams.h"
28 #include "OutputDev.h"
30 #include "ErrorCodes.h"
33 #ifndef DISABLE_OUTLINE
38 //------------------------------------------------------------------------
40 #define headerSearchSize 1024 // read this many bytes at beginning of
41 // file to look for '%PDF'
43 //------------------------------------------------------------------------
45 //------------------------------------------------------------------------
47 PDFDoc::PDFDoc(GString *fileNameA, GString *ownerPassword,
48 GString *userPassword) {
50 GString *fileName1, *fileName2;
60 #ifndef DISABLE_OUTLINE
71 if (!(file = fopen(fileName1->getCString(), "rb", "ctx=stm"))) {
72 error(-1, "Couldn't open file '%s'", fileName1->getCString());
73 errCode = errOpenFile;
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());
85 errCode = errOpenFile;
95 str = new FileStream(file, 0, gFalse, 0, &obj);
97 ok = setup(ownerPassword, userPassword);
100 PDFDoc::PDFDoc(BaseStream *strA, GString *ownerPassword,
101 GString *userPassword) {
110 #ifndef DISABLE_OUTLINE
113 ok = setup(ownerPassword, userPassword);
116 GBool PDFDoc::setup(GString *ownerPassword, GString *userPassword) {
121 xref = new XRef(str, ownerPassword, userPassword);
123 error(-1, "Couldn't read xref table");
124 errCode = xref->getErrorCode();
129 catalog = new Catalog(xref);
130 if (!catalog->isOk()) {
131 error(-1, "Couldn't read page catalog");
132 errCode = errBadCatalog;
136 #ifndef DISABLE_OUTLINE
138 outline = new Outline(catalog->getOutline(), xref);
146 #ifndef DISABLE_OUTLINE
171 // Check for a PDF header on this stream. Skip past some garbage
173 void PDFDoc::checkHeader() {
174 char hdrBuf[headerSearchSize+1];
179 for (i = 0; i < headerSearchSize; ++i) {
180 hdrBuf[i] = str->getChar();
182 hdrBuf[headerSearchSize] = '\0';
183 for (i = 0; i < headerSearchSize - 5; ++i) {
184 if (!strncmp(&hdrBuf[i], "%PDF-", 5)) {
188 if (i >= headerSearchSize - 5) {
189 error(-1, "May not be a PDF file (continuing anyway)");
193 p = strtok(&hdrBuf[i+5], " \t\n\r");
195 char *theLocale = setlocale(LC_NUMERIC, "C");
196 pdfVersion = atof(p);
197 setlocale(LC_NUMERIC, theLocale);
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);
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) {
214 if (globalParams->getPrintCommands()) {
215 printf("***** page %d *****\n", page);
217 p = catalog->getPage(page);
224 p->display(out, zoom, rotate, links, catalog,
225 abortCheckCbk, abortCheckCbkData,
226 annotDisplayDecideCbk, annotDisplayDecideCbkData);
228 p->display(out, zoom, rotate, NULL, catalog,
229 abortCheckCbk, abortCheckCbkData,
230 annotDisplayDecideCbk, annotDisplayDecideCbkData);
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) {
242 for (page = firstPage; page <= lastPage; ++page) {
243 displayPage(out, page, zoom, rotate, doLinks,
244 abortCheckCbk, abortCheckCbkData,
245 annotDisplayDecideCbk, annotDisplayDecideCbkData);
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) {
258 p = catalog->getPage(page);
259 p->displaySlice(out, zoom, rotate, sliceX, sliceY, sliceW, sliceH,
261 abortCheckCbk, abortCheckCbkData,
262 annotDisplayDecideCbk, annotDisplayDecideCbkData);
265 GBool PDFDoc::isLinearized() {
267 Object obj1, obj2, obj3, obj4, obj5;
272 parser = new Parser(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") &&
281 obj4.dictLookup("Linearized", &obj5);
282 if (obj5.isNum() && obj5.getNum() > 0) {
295 GBool PDFDoc::saveAs(GString *name) {
299 if (!(f = fopen(name->getCString(), "wb"))) {
300 error(-1, "Couldn't open file '%s'", name->getCString());
304 while ((c = str->getChar()) != EOF) {
312 void PDFDoc::getLinks(Page *page) {
315 links = new Links(page->getAnnots(&obj), catalog->getBaseURI());