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 crop, 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, crop, links, catalog,
227 abortCheckCbk, abortCheckCbkData,
228 annotDisplayDecideCbk, annotDisplayDecideCbkData);
230 p->display(out, hDPI, vDPI, rotate, crop, 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,
238 GBool crop, GBool doLinks,
239 GBool (*abortCheckCbk)(void *data),
240 void *abortCheckCbkData,
241 GBool (*annotDisplayDecideCbk)(Annot *annot, void *user_data),
242 void *annotDisplayDecideCbkData) {
245 for (page = firstPage; page <= lastPage; ++page) {
246 displayPage(out, page, hDPI, vDPI, rotate, crop, doLinks,
247 abortCheckCbk, abortCheckCbkData,
248 annotDisplayDecideCbk, annotDisplayDecideCbkData);
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) {
262 p = catalog->getPage(page);
263 p->displaySlice(out, hDPI, vDPI, rotate, crop,
264 sliceX, sliceY, sliceW, sliceH,
266 abortCheckCbk, abortCheckCbkData,
267 annotDisplayDecideCbk, annotDisplayDecideCbkData);
270 GBool PDFDoc::isLinearized() {
272 Object obj1, obj2, obj3, obj4, obj5;
277 parser = new Parser(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") &&
286 obj4.dictLookup("Linearized", &obj5);
287 if (obj5.isNum() && obj5.getNum() > 0) {
300 GBool PDFDoc::saveAs(GString *name) {
304 if (!(f = fopen(name->getCString(), "wb"))) {
305 error(-1, "Couldn't open file '%s'", name->getCString());
309 while ((c = str->getChar()) != EOF) {
317 void PDFDoc::getLinks(Page *page) {
320 links = new Links(page->getAnnots(&obj), catalog->getBaseURI());