home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 110 / EnigmaAmiga110CD.iso / indispensabili / utility / apdf / xpdf-0.80 / xpdf / pdfdoc.cc < prev    next >
C/C++ Source or Header  |  1999-06-20  |  4KB  |  172 lines

  1. //========================================================================
  2. //
  3. // PDFDoc.cc
  4. //
  5. // Copyright 1996 Derek B. Noonburg
  6. //
  7. //========================================================================
  8.  
  9. #ifdef __GNUC__
  10. #pragma implementation
  11. #endif
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <stddef.h>
  16. #include <string.h>
  17. #include "GString.h"
  18. #include "config.h"
  19. #include "Page.h"
  20. #include "Catalog.h"
  21. #include "XRef.h"
  22. #include "Link.h"
  23. #include "OutputDev.h"
  24. #include "Params.h"
  25. #include "Error.h"
  26. #include "PDFDoc.h"
  27.  
  28. //------------------------------------------------------------------------
  29. // PDFDoc
  30. //------------------------------------------------------------------------
  31.  
  32. PDFDoc::PDFDoc(GString *fileName1) {
  33.   FileStream *str;
  34.   Object catObj;
  35.   Object obj;
  36.   GString *fileName2;
  37.  
  38.   // setup
  39.   ok = gFalse;
  40.   catalog = NULL;
  41.   xref = NULL;
  42.   file = NULL;
  43.   links = NULL;
  44.  
  45.   // try to open file
  46.   fileName = fileName1;
  47.   fileName2 = NULL;
  48. #ifdef VMS
  49.   if (!(file = myfopen(fileName->getCString(), "rb", "ctx=stm"))) {
  50.     error(-1, "Couldn't open file '%s'", fileName->getCString());
  51.     return;
  52.   }
  53. #else
  54.   if (!(file = myfopen(fileName->getCString(), "rb"))) {
  55.     fileName2 = fileName->copy();
  56.     fileName2->lowerCase();
  57.     if (!(file = myfopen(fileName2->getCString(), "rb"))) {
  58.       fileName2->upperCase();
  59.       if (!(file = myfopen(fileName2->getCString(), "rb"))) {
  60.     error(-1, "Couldn't open file '%s'", fileName->getCString());
  61.     delete fileName2;
  62.     return;
  63.       }
  64.     }
  65.     delete fileName2;
  66.   }
  67. #endif
  68.  
  69.   // create stream
  70.   obj.initNull();
  71.   str = new FileStream(file, 0, -1, &obj);
  72.  
  73. //printf("*** check_header\n");
  74.   // check header
  75.   str->checkHeader();
  76. //printf("*** xref\n");
  77.   // read xref table
  78.   xref = new XRef(str);
  79.   delete str;
  80.   if (!xref->isOk()) {
  81.     error(-1, "Couldn't read xref table");
  82.     return;
  83.   }
  84.  
  85.   // read catalog
  86. //printf("*** catalog\n");
  87.   catalog = new Catalog(xref->getCatalog(&catObj));
  88.   catObj.free();
  89.   if (!catalog->isOk()) {
  90.     error(-1, "Couldn't read page catalog");
  91.     return;
  92.   }
  93. //printf("*** done.\n");
  94.   // done
  95.   ok = gTrue;
  96.   return;
  97. }
  98.  
  99. PDFDoc::~PDFDoc() {
  100.   if (catalog)
  101.     delete catalog;
  102.   if (xref)
  103.     delete xref;
  104.   if (file)
  105.     myfclose(file);
  106.   if (fileName)
  107.     delete fileName;
  108.   if (links)
  109.     delete links;
  110. }
  111.  
  112. void PDFDoc::displayPage(OutputDev *out, int page, int zoom, int rotate,
  113.              GBool doLinks) {
  114.   Link *link;
  115.   double x1, y1, x2, y2;
  116.   double w;
  117.   int i;
  118. //printf("*** displayPage(%d)\n",page);
  119.   if (printCommands)
  120.     printf("***** page %d *****\n", page);
  121.   catalog->getPage(page)->display(out, zoom, rotate);
  122.   if (doLinks) {
  123.     if (links)
  124.       delete links;
  125.     getLinks(page);
  126.     for (i = 0; i < links->getNumLinks(); ++i) {
  127.       link = links->getLink(i);
  128.       link->getBorder(&x1, &y1, &x2, &y2, &w);
  129.       if (w > 0)
  130.     out->drawLinkBorder(x1, y1, x2, y2, w);
  131.     }
  132.     out->dump();
  133.   }
  134. //printf("*** ...displayPage(%d)\n",page);
  135. }
  136.  
  137. void PDFDoc::displayPages(OutputDev *out, int firstPage, int lastPage,
  138.               int zoom, int rotate) {
  139.   Page *p;
  140.   int page;
  141.  
  142.   for (page = firstPage; page <= lastPage; ++page) {
  143.     if (printCommands)
  144.       printf("***** page %d *****\n", page);
  145.     p = catalog->getPage(page);
  146.     p->display(out, zoom, rotate);
  147.   }
  148. }
  149.  
  150. GBool PDFDoc::saveAs(GString *name) {
  151.   FILE *f;
  152.   char buf[4096];
  153.   int n;
  154.  
  155.   if (!(f = fopen(name->getCString(), "wb"))) {
  156.     error(-1, "Couldn't open file '%s'", name->getCString());
  157.     return gFalse;
  158.   }
  159.   myrewind(file);
  160.   while ((n = myfread(buf, 1, sizeof(buf), file)) > 0)
  161.     fwrite(buf, 1, n, f);
  162.   fclose(f);
  163.   return gTrue;
  164. }
  165.  
  166. void PDFDoc::getLinks(int page) {
  167.   Object obj;
  168.  
  169.   links = new Links(catalog->getPage(page)->getAnnots(&obj));
  170.   obj.free();
  171. }
  172.