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) {
123 xref = new XRef(str, ownerPassword, userPassword);
125 error(-1, "Couldn't read xref table");
126 errCode = xref->getErrorCode();
131 catalog = new Catalog(xref);
132 if (!catalog->isOk()) {
133 error(-1, "Couldn't read page catalog");
134 errCode = errBadCatalog;
138 #ifndef DISABLE_OUTLINE
140 outline = new Outline(catalog->getOutline(), xref);
148 #ifndef DISABLE_OUTLINE
173 // Check for a PDF header on this stream. Skip past some garbage
175 void PDFDoc::checkHeader() {
176 char hdrBuf[headerSearchSize+1];
181 for (i = 0; i < headerSearchSize; ++i) {
182 hdrBuf[i] = str->getChar();
184 hdrBuf[headerSearchSize] = '\0';
185 for (i = 0; i < headerSearchSize - 5; ++i) {
186 if (!strncmp(&hdrBuf[i], "%PDF-", 5)) {
190 if (i >= headerSearchSize - 5) {
191 error(-1, "May not be a PDF file (continuing anyway)");
195 p = strtok(&hdrBuf[i+5], " \t\n\r");
197 char *theLocale = setlocale(LC_NUMERIC, "C");
198 pdfVersion = atof(p);
199 setlocale(LC_NUMERIC, theLocale);
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);
208 void PDFDoc::displayPage(OutputDev *out, int page, double hDPI, double vDPI,
209 int rotate, GBool doLinks,
210 GBool (*abortCheckCbk)(void *data),
211 void *abortCheckCbkData,
212 GBool (*annotDisplayDecideCbk)(Annot *annot, void *user_data),
213 void *annotDisplayDecideCbkData) {
216 if (globalParams->getPrintCommands()) {
217 printf("***** page %d *****\n", page);
219 p = catalog->getPage(page);
226 p->display(out, hDPI, vDPI, rotate, links, catalog,
227 abortCheckCbk, abortCheckCbkData,
228 annotDisplayDecideCbk, annotDisplayDecideCbkData);
230 p->display(out, hDPI, vDPI, rotate, NULL, catalog,
231 abortCheckCbk, abortCheckCbkData,
232 annotDisplayDecideCbk, annotDisplayDecideCbkData);
236 void PDFDoc::displayPages(OutputDev *out, int firstPage, int lastPage,
237 double hDPI, double vDPI, int rotate, GBool doLinks,
238 GBool (*abortCheckCbk)(void *data),
239 void *abortCheckCbkData,
240 GBool (*annotDisplayDecideCbk)(Annot *annot, void *user_data),
241 void *annotDisplayDecideCbkData) {
244 for (page = firstPage; page <= lastPage; ++page) {
245 displayPage(out, page, hDPI, vDPI, rotate, doLinks,
246 abortCheckCbk, abortCheckCbkData,
247 annotDisplayDecideCbk, annotDisplayDecideCbkData);
251 void PDFDoc::displayPageSlice(OutputDev *out, int page,
252 double hDPI, double vDPI,
253 int rotate, int sliceX, int sliceY,
254 int sliceW, int sliceH,
255 GBool (*abortCheckCbk)(void *data),
256 void *abortCheckCbkData,
257 GBool (*annotDisplayDecideCbk)(Annot *annot, void *user_data),
258 void *annotDisplayDecideCbkData) {
261 p = catalog->getPage(page);
262 p->displaySlice(out, hDPI, vDPI, rotate, sliceX, sliceY, sliceW, sliceH,
264 abortCheckCbk, abortCheckCbkData,
265 annotDisplayDecideCbk, annotDisplayDecideCbkData);
268 GBool PDFDoc::isLinearized() {
270 Object obj1, obj2, obj3, obj4, obj5;
275 parser = new Parser(xref,
277 str->makeSubStream(str->getStart(), gFalse, 0, &obj1)));
278 parser->getObj(&obj1);
279 parser->getObj(&obj2);
280 parser->getObj(&obj3);
281 parser->getObj(&obj4);
282 if (obj1.isInt() && obj2.isInt() && obj3.isCmd("obj") &&
284 obj4.dictLookup("Linearized", &obj5);
285 if (obj5.isNum() && obj5.getNum() > 0) {
298 GBool PDFDoc::saveAs(GString *name) {
302 if (!(f = fopen(name->getCString(), "wb"))) {
303 error(-1, "Couldn't open file '%s'", name->getCString());
307 while ((c = str->getChar()) != EOF) {
315 void PDFDoc::getLinks(Page *page) {
318 links = new Links(page->getAnnots(&obj), catalog->getBaseURI());