home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga 12 / MA_Cover_12.iso / source / xpdf-0.80 / xpdf / page.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1998-11-27  |  4.8 KB  |  215 lines

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