home *** CD-ROM | disk | FTP | other *** search
/ PSION CD 2 / PsionCDVol2.iso / Programs / 720 / PDF090B4-SorceCode / pdf / PDFDoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-30  |  3.8 KB  |  182 lines

  1. //========================================================================
  2. //
  3. // PDFDoc.cc
  4. //
  5. // Copyright 1996 Derek B. Noonburg
  6. //
  7. //========================================================================
  8. //
  9. // Ported to EPOC by Sander van der Wal
  10. //
  11. // $Log: PDFDoc.cpp $
  12. // Revision 1.2  2000-09-17 13:38:21+02  svdwal
  13. // Ported
  14. //
  15.  
  16. #ifdef __GNUC__
  17. #pragma implementation
  18. #endif
  19.  
  20. #ifndef __E32DEF_H__
  21. #include <e32base.h> // remove warning about NULL redefinition
  22. #endif
  23.  
  24. #include "GString.h"
  25.  
  26. #include "config.h"
  27. #include "Page.h"
  28. #include "Catalog.h"
  29. #include "XRef.h"
  30. #include "Link.h"
  31. #include "OutputDev.h"
  32. #include "Error.h"
  33. #include "PDFDoc.h"
  34.  
  35. #include "Pdf.rsg"
  36.  
  37. //------------------------------------------------------------------------
  38. // PDFDoc
  39. //------------------------------------------------------------------------
  40.  
  41. void PDFDoc::ConstructL(RFs& aFsSession, const TFileName& aFileName)
  42. {
  43.   FileStream *str;
  44.   RAutoObject catObj;
  45.   RAutoObject obj;
  46.  
  47.   ok = gFalse;
  48.  
  49.   fileName = aFileName;
  50.  
  51.   TInt err = file.Open(aFsSession, fileName, EFileRead|EFileStream|EFileShareReadersOnly);
  52.   switch (err) {
  53.   case KErrNone:
  54.     break;
  55.   case KErrNoMemory:
  56.     User::LeaveNoMemory(); break;
  57.   default:
  58.     error(-1, R_COULDN_T_OPEN_FILE___S_, (const char*)fileName.Ptr());
  59.     return;
  60.   }
  61.   
  62.   // create stream
  63.  
  64.   obj.initNull();
  65.   str = new(ELeave) FileStream(file, 0, -1, &obj);
  66.   CleanupStack::PushL(str);
  67.  
  68.   // check header
  69.   str->checkHeader();
  70.  
  71.   // read xref table
  72.   xref = new(ELeave) XRef();
  73.   xref->ConstructL(str);
  74.   CleanupStack::PopAndDestroy(); // delete str;
  75.   if (!xref->isOk()) {
  76.     error(-1, R_COULDN_T_READ_XREF_TABLE);
  77.     return;
  78.   }
  79.  
  80.   // read catalog
  81.   catalog = new(ELeave) Catalog();
  82.   catalog->ConstructL(xref->getCatalogL(&catObj));
  83.   catObj.free();
  84.   if (!catalog->isOk()) {
  85.     error(-1, R_COULDN_T_READ_PAGE_CATALOG);
  86.     return;
  87.   }
  88.  
  89.   // done
  90.   ok = gTrue;
  91. }
  92.  
  93.  
  94. PDFDoc::~PDFDoc() {
  95.   delete catalog;
  96.   delete xref;
  97.   file.Close();
  98.   delete links;
  99. }
  100.  
  101. void PDFDoc::displayPageL(OutputDev *out, int page, int zoom, int rotate,
  102.               GBool doLinks) {
  103.  
  104. #ifndef __SYMBIAN32__
  105.   if (printCommands)
  106.     printf("***** page %d *****\n", page);
  107. #endif
  108.  
  109.   if (doLinks)
  110.     getLinksL(page);
  111.   catalog->getPage(page)->displayL(out, zoom, rotate);
  112.   if (doLinks)
  113.     DisplayLinks(out, page);
  114.  
  115. }
  116.  
  117.  
  118. void PDFDoc::DisplayLinks(OutputDev *out, int /* page */) {
  119.   Link *link;
  120.   double x1, y1, x2, y2;
  121.   double w;
  122.   int i;
  123.  
  124.   for (i = 0; i < links->getNumLinks(); ++i) {
  125.     link = links->getLink(i);
  126.     link->getBorder(&x1, &y1, &x2, &y2, &w);
  127.     if (w > 0)
  128.       out->drawLinkBorder(x1, y1, x2, y2, w);
  129.   }
  130.   out->dump();
  131. }
  132.  
  133.  
  134. void PDFDoc::displayPagesL(OutputDev *out, int firstPage, int lastPage,
  135.               int zoom, int rotate) {
  136.   Page *p;
  137.   int page;
  138.  
  139.   for (page = firstPage; page <= lastPage; ++page) {
  140. #ifndef __SYMBIAN32__
  141.     if (printCommands)
  142.       printf("***** page %d *****\n", page);
  143. #endif
  144.     p = catalog->getPage(page);
  145.     p->displayL(out, zoom, rotate);
  146.   }
  147. }
  148.  
  149.  
  150. #if 0
  151. GBool PDFDoc::saveAs(GString *name) {
  152.   char buf[256];
  153.   int n;
  154.   FILE *f = fopen(name->getCString(), "wb");
  155.   if (!f) {
  156.     error(-1, R_COULDN_T_OPEN_FILE___S_, name->getCString());
  157.     return gFalse;
  158.   }
  159.   rewind(file);
  160.   while ((n = fread(buf, 1, sizeof(buf), file)) > 0)
  161.     fwrite(buf, 1, n, f);
  162.   fclose(f);
  163.   return gTrue;
  164. }
  165. #endif
  166.  
  167.  
  168. void PDFDoc::getLinksL(int page) {
  169.   RAutoObject obj;
  170.  
  171.   // cleanup-safe way of creating new links
  172.   Links* l = new(ELeave) Links();
  173.   CleanupStack::PushL(l);
  174.   l->ConstructL(catalog->getPage(page)->getAnnotsL(&obj),
  175.                 catalog->getBaseURI());
  176.   CleanupStack::Pop(); // l
  177.   obj.free();
  178.  
  179.   delete links;
  180.   links = l;  
  181. }
  182.