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

  1. //========================================================================
  2. //
  3. // Page.cc
  4. //
  5. // Copyright 1996 Derek B. Noonburg
  6. //
  7. //========================================================================
  8. //
  9. // Ported to EPOC by Sander van der Wal
  10. //
  11. // $Log: Page.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. #include "Page.h"
  21.  
  22. // --o PdfLib
  23. #include "Array.h"
  24. #include "Dict.h"
  25. #include "XRef.h"
  26.  
  27. #ifndef PDF_PARSER_ONLY
  28. #include "Gfx.h"
  29. #endif
  30.  
  31. #include "Error.h"
  32.  
  33. // --o Pdf
  34. #include "Pdf.rsg"
  35. #include "PROFILE.H"
  36.  
  37. //------------------------------------------------------------------------
  38. // PageAttrs
  39. //------------------------------------------------------------------------
  40.  
  41. void PageAttrs::ConstructL(PageAttrs *attrs, Dict *dict) {
  42.   RAutoObject obj1, obj2;
  43.   double w, h;
  44.  
  45.   // get old/default values
  46.   if (attrs) {
  47.     x1 = attrs->x1;
  48.     y1 = attrs->y1;
  49.     x2 = attrs->x2;
  50.     y2 = attrs->y2;
  51.     cropX1 = attrs->cropX1;
  52.     cropY1 = attrs->cropY1;
  53.     cropX2 = attrs->cropX2;
  54.     cropY2 = attrs->cropY2;
  55.     rotate = attrs->rotate;
  56.     attrs->resources.copyL(&resources);
  57.   } else {
  58.     // set default MediaBox to 8.5" x 11" -- this shouldn't be necessary
  59.     // but some (non-compliant) PDF files don't specify a MediaBox
  60.     x1 = 0;
  61.     y1 = 0;
  62.     x2 = 612;
  63.     y2 = 792;
  64.     cropX1 = cropY1 = cropX2 = cropY2 = 0;
  65.     rotate = 0;
  66.     resources.initNull();
  67.   }
  68.  
  69.   // media box
  70.   dict->lookupL("MediaBox", &obj1);
  71.   if (obj1.isArray() && obj1.arrayGetLength() == 4) {
  72.     obj1.arrayGetL(0, &obj2);
  73.     if (obj2.isNum())
  74.       x1 = obj2.getNum();
  75.     obj2.free();
  76.     obj1.arrayGetL(1, &obj2);
  77.     if (obj2.isNum())
  78.       y1 = obj2.getNum();
  79.     obj2.free();
  80.     obj1.arrayGetL(2, &obj2);
  81.     if (obj2.isNum())
  82.       x2 = obj2.getNum();
  83.     obj2.free();
  84.     obj1.arrayGetL(3, &obj2);
  85.     if (obj2.isNum())
  86.       y2 = obj2.getNum();
  87.     obj2.free();
  88.   }
  89.   obj1.free();
  90.  
  91.   // crop box
  92.   dict->lookupL("CropBox", &obj1);
  93.   if (obj1.isArray() && obj1.arrayGetLength() == 4) {
  94.     obj1.arrayGetL(0, &obj2);
  95.     if (obj2.isNum())
  96.       cropX1 = obj2.getNum();
  97.     obj2.free();
  98.     obj1.arrayGetL(1, &obj2);
  99.     if (obj2.isNum())
  100.       cropY1 = obj2.getNum();
  101.     obj2.free();
  102.     obj1.arrayGetL(2, &obj2);
  103.     if (obj2.isNum())
  104.       cropX2 = obj2.getNum();
  105.     obj2.free();
  106.     obj1.arrayGetL(3, &obj2);
  107.     if (obj2.isNum())
  108.       cropY2 = obj2.getNum();
  109.     obj2.free();
  110.   }
  111.   obj1.free();
  112.  
  113.   // if the MediaBox is excessively larger than the CropBox,
  114.   // just use the CropBox
  115.   limitToCropBox = gFalse;
  116.   w = 0.25 * (cropX2 - cropX1);
  117.   h = 0.25 * (cropY2 - cropY1);
  118.   if (cropX2 > cropX1 &&
  119.       (cropX1 - x1 > w || x2 - cropX2 > w ||
  120.        cropY1 - y1 > h || y2 - cropY2 > h)) {
  121.     limitToCropBox = gTrue;
  122.   }
  123.  
  124.   // rotate
  125.   dict->lookupL("Rotate", &obj1);
  126.   if (obj1.isInt())
  127.     rotate = obj1.getInt();
  128.   obj1.free();
  129.   while (rotate < 0)
  130.     rotate += 360;
  131.   while (rotate >= 360)
  132.     rotate -= 360;
  133.  
  134.   // resource dictionary
  135.   dict->lookupL("Resources", &obj1);
  136.   if (obj1.isDict()) {
  137.     resources.free();
  138.     obj1.copyL(&resources);
  139.   }
  140.   obj1.free();
  141. }
  142.  
  143. PageAttrs::~PageAttrs() {
  144.   resources.free();
  145. }
  146.  
  147. //------------------------------------------------------------------------
  148. // Page
  149. //------------------------------------------------------------------------
  150.  
  151. void Page::ConstructL(Dict *pageDict) {
  152.  
  153.   ok = gTrue;
  154.  
  155.   // annotations
  156.   pageDict->lookupNFL("Annots", &annots);
  157.   if (!(annots.isRef() || annots.isArray() || annots.isNull())) {
  158.     error(-1, R_PAGE_ANNOTATIONS_OBJECT__PAGE__D__IS_WRONG_TYPE___S_,
  159.       num, annots.getTypeName());
  160.     annots.free();
  161.     goto err2;
  162.   }
  163.  
  164.   // contents
  165.   pageDict->lookupNFL("Contents", &contents);
  166.   if (!(contents.isRef() || contents.isArray() ||
  167.     contents.isNull())) {
  168.     error(-1, R_PAGE_CONTENTS_OBJECT__PAGE__D__IS_WRONG_TYPE___S_,
  169.       num, contents.getTypeName());
  170.     contents.free();
  171.     goto err1;
  172.   }
  173.   
  174.   return;
  175.  
  176.  err2:
  177.   annots.initNull();
  178.  err1:
  179.   contents.initNull();
  180.   ok = gFalse;
  181. }
  182.  
  183. Page::~Page() {
  184.  
  185.   delete attrs;
  186.   annots.free();
  187.   contents.free();
  188. }
  189.  
  190.  
  191. void Page::displayL(OutputDev *out, int dpi, int rotate) {
  192.   
  193. #ifndef PDF_PARSER_ONLY
  194.  
  195.   PROFILE_START(PAGE__DISPLAY);
  196.  
  197.   RAutoObject obj;
  198.  
  199. #ifndef __SYMBIAN32__
  200.   if (printCommands) {
  201.     printf("***** MediaBox = ll:%g,%g ur:%g,%g\n",
  202.        getX1(), getY1(), getX2(), getY2());
  203.     if (isCropped()) {
  204.       printf("***** CropBox = ll:%g,%g ur:%g,%g\n",
  205.          getCropX1(), getCropY1(), getCropX2(), getCropY2());
  206.     }
  207.     printf("***** Rotate = %d\n", attrs->getRotate());
  208.   }
  209. #endif
  210.  
  211.   rotate += getRotate();
  212.   if (rotate >= 360)
  213.     rotate -= 360;
  214.   else if (rotate < 0)
  215.     rotate += 360;
  216.  
  217.   Gfx* gfx = new(ELeave) Gfx();
  218.   CleanupStack::PushL(gfx);
  219.   gfx->ConstructL(out, num, attrs->getResourceDict(),
  220.                   dpi, getX1(), getY1(), getX2(), getY2(), isCropped(),
  221.                   getCropX1(), getCropY1(), getCropX2(), getCropY2(), rotate);
  222.   contents.fetchL(&obj);
  223.   if (!obj.isNull())
  224.     gfx->displayL(&obj);
  225.   
  226.   obj.free();
  227.   CleanupStack::PopAndDestroy(); // gfx;
  228.   
  229.   PROFILE_STOP(PAGE__DISPLAY);
  230.  
  231. #endif
  232.  
  233. }
  234.