PDFDoc.cc 6.67 KB
Newer Older
1 2 3 4 5 6 7 8
//========================================================================
//
// PDFDoc.cc
//
// Copyright 1996-2002 Glyph & Cog, LLC
//
//========================================================================

Wilco Greven's avatar
Wilco Greven committed
9 10 11
#include <aconf.h>

#ifdef USE_GCC_PRAGMAS
12 13 14 15 16 17 18 19 20
#pragma implementation
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include "GString.h"
#include "config.h"
Wilco Greven's avatar
Wilco Greven committed
21
#include "GlobalParams.h"
22 23 24 25 26 27 28 29 30 31
#include "Page.h"
#include "Catalog.h"
#include "Stream.h"
#include "XRef.h"
#include "Link.h"
#include "OutputDev.h"
#include "Error.h"
#include "ErrorCodes.h"
#include "Lexer.h"
#include "Parser.h"
Wilco Greven's avatar
Wilco Greven committed
32 33 34
#ifndef DISABLE_OUTLINE
#include "Outline.h"
#endif
35 36 37 38 39 40 41 42 43 44 45 46
#include "PDFDoc.h"

//------------------------------------------------------------------------

#define headerSearchSize 1024	// read this many bytes at beginning of
				//   file to look for '%PDF'

//------------------------------------------------------------------------
// PDFDoc
//------------------------------------------------------------------------

PDFDoc::PDFDoc(GString *fileNameA, GString *ownerPassword,
Wilco Greven's avatar
Wilco Greven committed
47
	       GString *userPassword) {
48
  Object obj;
Wilco Greven's avatar
Wilco Greven committed
49
  GString *fileName1, *fileName2;
50 51 52 53 54 55 56 57 58

  ok = gFalse;
  errCode = errNone;

  file = NULL;
  str = NULL;
  xref = NULL;
  catalog = NULL;
  links = NULL;
Wilco Greven's avatar
Wilco Greven committed
59 60 61
#ifndef DISABLE_OUTLINE
  outline = NULL;
#endif
62 63

  fileName = fileNameA;
Wilco Greven's avatar
Wilco Greven committed
64 65 66 67
  fileName1 = fileName;


  // try to open file
68 69
  fileName2 = NULL;
#ifdef VMS
Wilco Greven's avatar
Wilco Greven committed
70 71
  if (!(file = fopen(fileName1->getCString(), "rb", "ctx=stm"))) {
    error(-1, "Couldn't open file '%s'", fileName1->getCString());
72 73 74 75
    errCode = errOpenFile;
    return;
  }
#else
Wilco Greven's avatar
Wilco Greven committed
76
  if (!(file = fopen(fileName1->getCString(), "rb"))) {
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    fileName2 = fileName->copy();
    fileName2->lowerCase();
    if (!(file = fopen(fileName2->getCString(), "rb"))) {
      fileName2->upperCase();
      if (!(file = fopen(fileName2->getCString(), "rb"))) {
	error(-1, "Couldn't open file '%s'", fileName->getCString());
	delete fileName2;
	errCode = errOpenFile;
	return;
      }
    }
    delete fileName2;
  }
#endif

  // create stream
  obj.initNull();
  str = new FileStream(file, 0, gFalse, 0, &obj);

  ok = setup(ownerPassword, userPassword);
}

PDFDoc::PDFDoc(BaseStream *strA, GString *ownerPassword,
Wilco Greven's avatar
Wilco Greven committed
100
	       GString *userPassword) {
101 102 103 104 105 106 107 108
  ok = gFalse;
  errCode = errNone;
  fileName = NULL;
  file = NULL;
  str = strA;
  xref = NULL;
  catalog = NULL;
  links = NULL;
Wilco Greven's avatar
Wilco Greven committed
109 110 111
#ifndef DISABLE_OUTLINE
  outline = NULL;
#endif
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  ok = setup(ownerPassword, userPassword);
}

GBool PDFDoc::setup(GString *ownerPassword, GString *userPassword) {
  // check header
  checkHeader();

  // read xref table
  xref = new XRef(str, ownerPassword, userPassword);
  if (!xref->isOk()) {
    error(-1, "Couldn't read xref table");
    errCode = xref->getErrorCode();
    return gFalse;
  }

  // read catalog
Wilco Greven's avatar
Wilco Greven committed
128
  catalog = new Catalog(xref);
129 130 131 132 133 134
  if (!catalog->isOk()) {
    error(-1, "Couldn't read page catalog");
    errCode = errBadCatalog;
    return gFalse;
  }

Wilco Greven's avatar
Wilco Greven committed
135 136 137 138 139
#ifndef DISABLE_OUTLINE
  // read outline
  outline = new Outline(catalog->getOutline(), xref);
#endif

140 141 142 143 144
  // done
  return gTrue;
}

PDFDoc::~PDFDoc() {
Wilco Greven's avatar
Wilco Greven committed
145 146 147 148 149
#ifndef DISABLE_OUTLINE
  if (outline) {
    delete outline;
  }
#endif
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
  if (catalog) {
    delete catalog;
  }
  if (xref) {
    delete xref;
  }
  if (str) {
    delete str;
  }
  if (file) {
    fclose(file);
  }
  if (fileName) {
    delete fileName;
  }
  if (links) {
    delete links;
  }
}

// Check for a PDF header on this stream.  Skip past some garbage
// if necessary.
void PDFDoc::checkHeader() {
  char hdrBuf[headerSearchSize+1];
  char *p;
  int i;

  pdfVersion = 0;
  for (i = 0; i < headerSearchSize; ++i) {
    hdrBuf[i] = str->getChar();
  }
  hdrBuf[headerSearchSize] = '\0';
  for (i = 0; i < headerSearchSize - 5; ++i) {
    if (!strncmp(&hdrBuf[i], "%PDF-", 5)) {
      break;
    }
  }
  if (i >= headerSearchSize - 5) {
    error(-1, "May not be a PDF file (continuing anyway)");
    return;
  }
  str->moveStart(i);
  p = strtok(&hdrBuf[i+5], " \t\n\r");
  pdfVersion = atof(p);
  if (!(hdrBuf[i+5] >= '0' && hdrBuf[i+5] <= '9') ||
      pdfVersion > supportedPDFVersionNum + 0.0001) {
    error(-1, "PDF version %s -- xpdf supports version %s"
	  " (continuing anyway)", p, supportedPDFVersionStr);
  }
}

void PDFDoc::displayPage(OutputDev *out, int page, double zoom,
Wilco Greven's avatar
Wilco Greven committed
202 203
			 int rotate, GBool doLinks,
			 GBool (*abortCheckCbk)(void *data),
204 205 206 207
                         void *abortCheckCbkData) {

  if ( page < 0 || page >= catalog->getNumPages() ) return; // obviously invalid page

208 209
  Page *p;

Wilco Greven's avatar
Wilco Greven committed
210
  if (globalParams->getPrintCommands()) {
211 212
    printf("***** page %d *****\n", page);
  }
213

214
  p = catalog->getPage(page);
215

216 217 218 219 220
  if (doLinks) {
    if (links) {
      delete links;
    }
    getLinks(p);
Wilco Greven's avatar
Wilco Greven committed
221 222
    p->display(out, zoom, rotate, links, catalog,
	       abortCheckCbk, abortCheckCbkData);
223
  } else {
Wilco Greven's avatar
Wilco Greven committed
224 225
    p->display(out, zoom, rotate, NULL, catalog,
	       abortCheckCbk, abortCheckCbkData);
226 227 228 229
  }
}

void PDFDoc::displayPages(OutputDev *out, int firstPage, int lastPage,
Wilco Greven's avatar
Wilco Greven committed
230 231 232
			  int zoom, int rotate, GBool doLinks,
			  GBool (*abortCheckCbk)(void *data),
			  void *abortCheckCbkData) {
233 234 235
  int page;

  for (page = firstPage; page <= lastPage; ++page) {
Wilco Greven's avatar
Wilco Greven committed
236 237
    displayPage(out, page, zoom, rotate, doLinks,
		abortCheckCbk, abortCheckCbkData);
238 239 240
  }
}

Wilco Greven's avatar
Wilco Greven committed
241 242 243 244 245 246 247 248 249 250 251 252
void PDFDoc::displayPageSlice(OutputDev *out, int page, double zoom,
			      int rotate, int sliceX, int sliceY,
			      int sliceW, int sliceH,
			      GBool (*abortCheckCbk)(void *data),
			      void *abortCheckCbkData) {
  Page *p;

  p = catalog->getPage(page);
  p->displaySlice(out, zoom, rotate, sliceX, sliceY, sliceW, sliceH,
		  NULL, catalog, abortCheckCbk, abortCheckCbkData);
}

253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
GBool PDFDoc::isLinearized() {
  Parser *parser;
  Object obj1, obj2, obj3, obj4, obj5;
  GBool lin;

  lin = gFalse;
  obj1.initNull();
  parser = new Parser(xref,
	     new Lexer(xref,
	       str->makeSubStream(str->getStart(), gFalse, 0, &obj1)));
  parser->getObj(&obj1);
  parser->getObj(&obj2);
  parser->getObj(&obj3);
  parser->getObj(&obj4);
  if (obj1.isInt() && obj2.isInt() && obj3.isCmd("obj") &&
      obj4.isDict()) {
    obj4.dictLookup("Linearized", &obj5);
    if (obj5.isNum() && obj5.getNum() > 0) {
      lin = gTrue;
    }
    obj5.free();
  }
  obj4.free();
  obj3.free();
  obj2.free();
  obj1.free();
  delete parser;
  return lin;
}

GBool PDFDoc::saveAs(GString *name) {
  FILE *f;
  int c;

  if (!(f = fopen(name->getCString(), "wb"))) {
    error(-1, "Couldn't open file '%s'", name->getCString());
    return gFalse;
  }
  str->reset();
  while ((c = str->getChar()) != EOF) {
    fputc(c, f);
  }
  str->close();
  fclose(f);
  return gTrue;
}

void PDFDoc::getLinks(Page *page) {
  Object obj;

  links = new Links(page->getAnnots(&obj), catalog->getBaseURI());
  obj.free();
}