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

  1. //========================================================================
  2. //
  3. // GfxState.h
  4. //
  5. // Copyright 1996 Derek B. Noonburg
  6. //
  7. //========================================================================
  8. //
  9. //
  10. // Ported to EPOC by Sander van der Wal
  11. //
  12. // $Id: GfxState.h 1.3 2000-09-21 14:44:54+02 svdwal Exp svdwal $
  13. //
  14. // Added some faster forms of tranforms, especcially when one value
  15. // is known to be zero all call time
  16. //
  17.  
  18. #ifndef GFXSTATE_H
  19. #define GFXSTATE_H
  20.  
  21. #ifdef __GNUC__
  22. #pragma interface
  23. #endif
  24.  
  25. #ifdef __SYMBIAN32__
  26. #undef FIXCOLOR
  27. #define FIXCOLOR
  28. #endif
  29.  
  30. #ifndef __E32BASE_H__
  31. #include <e32base.h>
  32. #endif
  33.  
  34. #include "gtypes.h"
  35.  
  36. class Object;
  37. class Function;
  38. class GfxFont;
  39.  
  40. //------------------------------------------------------------------------
  41. // GfxColor
  42. //------------------------------------------------------------------------
  43.  
  44. #if defined(FIXCOLOR)
  45. class GfxColor {
  46. public:
  47.  
  48.   GfxColor(): r(0), g(0), b(0) {}
  49.  
  50.   // Set color.
  51.   void setGray(int gray);
  52.   void setCMYK(int c, int m, int y, int k);
  53.   void setRGB(int r1, int g1, int b1);
  54.  
  55.   // Accessors.
  56.   int getR() { return r; }
  57.   int getG() { return g; }
  58.   int getB() { return b; }
  59.   int getGray() { return (299 * r + 587 * g + 114 * b) / 1000; }
  60.  
  61. private:
  62.  
  63.   int r, g, b;
  64. };
  65. #else
  66. class GfxColor {
  67. public:
  68.  
  69.   GfxColor(): r(0), g(0), b(0) {}
  70.  
  71.   // Set color.
  72.   void setGray(double gray);
  73.   void setCMYK(double c, double m, double y, double k);
  74.   void setRGB(double r1, double g1, double b1);
  75.  
  76.   // Accessors.
  77.   double getR() { return r; }
  78.   double getG() { return g; }
  79.   double getB() { return b; }
  80.   double getGray() { return 0.299 * r + 0.587 * g + 0.114 * b; }
  81.  
  82. private:
  83.  
  84.   double r, g, b;
  85. };
  86. #endif
  87.  
  88. //------------------------------------------------------------------------
  89. // GfxColorSpace
  90. //------------------------------------------------------------------------
  91.  
  92. enum GfxColorMode {
  93.   colorGray, colorCMYK, colorRGB
  94. };
  95.  
  96. class GfxColorSpace: public CBase {
  97. public:
  98.  
  99.   // Construct a colorspace.
  100.   GfxColorSpace() {}
  101.   void ConstructL(Object *colorSpace);
  102.  
  103.   // Construct a simple colorspace: DeviceGray, DeviceCMYK, or
  104.   // DeviceRGB.
  105.   GfxColorSpace(GfxColorMode mode1);
  106.  
  107.   // Destructor.
  108.   ~GfxColorSpace();
  109.  
  110.   // Copy.
  111.   GfxColorSpace *copyL();
  112.  
  113.   // Is color space valid?
  114.   GBool isOk() { return ok; }
  115.  
  116.   // Get the color mode.
  117.   GfxColorMode getMode() { return mode; }
  118.  
  119.   // Get number of components in pixels of this colorspace.
  120.   int getNumPixelComps() { return indexed ? 1 : numComps; }
  121.  
  122.   // Get number of components in colors of this colorspace.
  123.   int getNumColorComps() { return numComps; }
  124.  
  125.   // Return true if colorspace is indexed.
  126.   GBool isIndexed() { return indexed; }
  127.  
  128.   // Get lookup table (only for indexed colorspaces).
  129.   int getIndexHigh() { return indexHigh; }
  130.   Guchar *getLookupVal(int i) { return lookup[i]; }
  131.  
  132.   // Convert a pixel to a color.
  133. #if defined(FIXCOLOR)
  134.   void getColor(int x[4], GfxColor *color);
  135. #else
  136.   void getColor(double x[4], GfxColor *color);
  137. #endif
  138.  
  139. private:
  140.  
  141.   Function *sepFunc;        // separation tint transform function
  142.   GfxColorMode mode;        // color mode
  143.   GBool indexed;        // set for indexed colorspaces
  144.   int numComps;            // number of components in colors
  145.   int indexHigh;        // max pixel for indexed colorspace
  146.   Guchar (*lookup)[4];        // lookup table (only for indexed
  147.                           //   colorspaces)
  148.   GBool ok;            // is color space valid?
  149.  
  150.   void ConstructL(GfxColorSpace *colorSpace);
  151.  
  152.   void setMode(Object *colorSpace);
  153. };
  154.  
  155. //------------------------------------------------------------------------
  156. // Function
  157. //------------------------------------------------------------------------
  158.  
  159. class Function: public CBase {
  160. public:
  161.  
  162.   // Create a PDF function object.
  163.   Function() {}
  164.   void ConstructL(Object* funcObj);
  165.  
  166.   ~Function();
  167.   
  168.   Function *copyL();
  169.  
  170.   GBool isOk() { return ok; }
  171.  
  172.   // Return size of input and output tuples.
  173.   int getInputSize() { return m; }
  174.   int getOutputSize() { return n; }
  175.  
  176.   // Transform an input tuple into an output tuple.
  177. #ifdef FIXCOLOR
  178.   void transform(int* in, int* out);
  179. #else
  180.   void transform(double *in, double *out);
  181. #endif
  182.  
  183. private:
  184.  
  185.   void ConstructL(Function *func);
  186.  
  187.   int m, n;
  188.   double domain[1][2];
  189.   double range[4][2];
  190.   int sampleSize[1];
  191.   double encode[1][2];
  192.   double decode[4][2];
  193.   double *samples;
  194.   GBool ok;
  195. };
  196.  
  197. //------------------------------------------------------------------------
  198. // GfxImageColorMap
  199. //------------------------------------------------------------------------
  200.  
  201. class GfxImageColorMap: public CBase {
  202. public:
  203.  
  204.   // Constructor.
  205.   GfxImageColorMap(GfxColorSpace *aColorSpace);
  206.   void ConstructL(int bits1, Object *decode);
  207.  
  208.   // Destructor.
  209.   ~GfxImageColorMap();
  210.  
  211.   // Is color map valid?
  212.   GBool isOk() { return ok; }
  213.  
  214.   // Get the color space.
  215.   GfxColorSpace *getColorSpace() { return colorSpace; }
  216.  
  217.   // Get stream decoding info.
  218.   int getNumPixelComps() { return numComps; }
  219.   int getBits() { return bits; }
  220.  
  221.   // Get decode table.
  222.   double getDecodeLow(int i) { return decodeLow[i]; }
  223.   double getDecodeHigh(int i) { return decodeLow[i] + decodeRange[i]; }
  224.  
  225.   // Convert a pixel to a color.
  226.   void getColor(Guchar x[4], GfxColor *color);
  227.  
  228. private:
  229.  
  230.   GfxColorSpace *colorSpace;    // the image colorspace
  231.   int bits;            // bits per component
  232.   int numComps;            // number of components in a pixel
  233.   GBool indexed;        // set for indexed color space
  234.   GfxColorMode mode;            // color mode
  235. #if defined(FIXCOLOR)
  236.   int (*lookup)[4];             // lookup table
  237.   int decodeLow[4];        // minimum values for each component
  238.   int decodeRange[4];            // max - min value for each component
  239. #else
  240.   double (*lookup)[4];        // lookup table
  241.   double decodeLow[4];        // minimum values for each component
  242.   double decodeRange[4];    // max - min value for each component
  243. #endif
  244.   GBool ok;
  245. };
  246.  
  247. //------------------------------------------------------------------------
  248. // GfxSubpath and GfxPath
  249. //------------------------------------------------------------------------
  250.  
  251. class GfxSubpath: public CBase {
  252. public:
  253.  
  254.   // Constructor.
  255.   GfxSubpath() {};
  256.   void ConstructL(double x1, double y1);
  257.  
  258.   // Destructor.
  259.   ~GfxSubpath();
  260.  
  261.   // Copy.
  262.   GfxSubpath *copyL();
  263.  
  264.   // Get points.
  265.   int getNumPoints() { return n; }
  266.   double getX(int i) { return x[i]; }
  267.   double getY(int i) { return y[i]; }
  268.   GBool getCurve(int i) { return curve[i]; }
  269.  
  270.   // Get last point.
  271.   double getLastX() { return x[n-1]; }
  272.   double getLastY() { return y[n-1]; }
  273.  
  274.   // Add a line segment.
  275.   void lineTo(double x1, double y1);
  276.  
  277.   // Add a Bezier curve.
  278.   void curveTo(double x1, double y1, double x2, double y2,
  279.            double x3, double y3);
  280.  
  281.   // Close the subpath.
  282.   void close();
  283.   GBool isClosed() { return closed; }
  284.  
  285. private:
  286.  
  287.   double *x, *y;        // points
  288.   GBool *curve;            // curve[i] => point i is a control point
  289.                 //   for a Bezier curve
  290.   int n;            // number of points
  291.   int size;            // size of x/y arrays
  292.   GBool closed;            // set if path is closed
  293.  
  294.   void ConstructL(GfxSubpath *subpath);
  295. };
  296.  
  297. class GfxPath: public CBase {
  298. public:
  299.  
  300.   // Constructor.
  301.   GfxPath() {}
  302.   void ConstructL();
  303.  
  304.   // Destructor.
  305.   ~GfxPath();
  306.  
  307.   // Copy.
  308.   GfxPath *copyL();
  309.  
  310.   // Is there a current point?
  311.   GBool isCurPt() { return n > 0 || justMoved; }
  312.  
  313.   // Is the path non-empty, i.e., is there at least one segment?
  314.   GBool isPath() { return n > 0; }
  315.  
  316.   // Get subpaths.
  317.   int getNumSubpaths() { return n; }
  318.   GfxSubpath *getSubpath(int i) { return subpaths[i]; }
  319.  
  320.   // Get last point on last subpath.
  321.   double getLastX() { return subpaths[n-1]->getLastX(); }
  322.   double getLastY() { return subpaths[n-1]->getLastY(); }
  323.  
  324.   // Move the current point.
  325.   void moveTo(double x, double y);
  326.  
  327.   // Add a segment to the last subpath.
  328.   void lineTo(double x, double y);
  329.  
  330.   // Add a Bezier curve to the last subpath
  331.   void curveTo(double x1, double y1, double x2, double y2,
  332.            double x3, double y3);
  333.  
  334.   // Close the last subpath.
  335.   void close() { subpaths[n-1]->close(); }
  336.  
  337. private:
  338.  
  339.   GBool justMoved;        // set if a new subpath was just started
  340.   double firstX, firstY;    // first point in new subpath
  341.   GfxSubpath **subpaths;    // subpaths
  342.   int n;            // number of subpaths
  343.   int size;            // size of subpaths array
  344.  
  345.   void ConstructL(GBool justMoved1, double firstX1, double firstY1,
  346.       GfxSubpath **subpaths1, int n1, int size1);
  347. };
  348.  
  349. //------------------------------------------------------------------------
  350. // GfxState
  351. //------------------------------------------------------------------------
  352.  
  353. class GfxState: public CBase {
  354. public:
  355.  
  356.   // Construct a default GfxState, for a device with resolution <dpi>,
  357.   // page box (<x1>,<y1>)-(<x2>,<y2>), page rotation <rotate>, and
  358.   // coordinate system specified by <upsideDown>.
  359.   GfxState() {}
  360.   void ConstructL(int dpi, double px1a, double py1a, double px2a, double py2a,
  361.        int rotate, GBool upsideDown);
  362.  
  363.   // Destructor.
  364.   ~GfxState();
  365.  
  366.   // Copy.
  367.   GfxState *copyL();
  368.  
  369.   // Accessors.
  370.   double *getCTM() { return ctm; }
  371.   double getX1() { return px1; }
  372.   double getY1() { return py1; }
  373.   double getX2() { return px2; }
  374.   double getY2() { return py2; }
  375.   double getPageWidth() { return pageWidth; }
  376.   double getPageHeight() { return pageHeight; }
  377.   GfxColor *getFillColor() { return &fillColor; }
  378.   GfxColor *getStrokeColor() { return &strokeColor; }
  379.   double getLineWidth() { return lineWidth; }
  380.   void getLineDash(double **dash, int *length, double *start)
  381.     { *dash = lineDash; *length = lineDashLength; *start = lineDashStart; }
  382.   int getFlatness() { return flatness; }
  383.   int getLineJoin() { return lineJoin; }
  384.   int getLineCap() { return lineCap; }
  385.   double getMiterLimit() { return miterLimit; }
  386.   GfxFont *getFont() { return font; }
  387.   double getFontSize() { return fontSize; }
  388.   double *getTextMat() { return textMat; }
  389.   double getCharSpace() { return charSpace; }
  390.   double getWordSpace() { return wordSpace; }
  391.   double getHorizScaling() { return horizScaling; }
  392.   double getLeading() { return leading; }
  393.   double getRise() { return rise; }
  394.   int getRender() { return render; }
  395.   GfxPath *getPath() { return path; }
  396.   double getCurX() { return curX; }
  397.   double getCurY() { return curY; }
  398.   double getLineX() { return lineX; }
  399.   double getLineY() { return lineY; }
  400.  
  401.   // Is there a current point/path?
  402.   GBool isCurPt() { return path->isCurPt(); }
  403.   GBool isPath() { return path->isPath(); }
  404.  
  405.   // Transforms.
  406.   void transform(double x1, double y1, double *x2, double *y2);
  407.   void transformDelta(double x1, double y1, double *x2, double *y2);
  408.   void textTransform(double x1, double y1, double *x2, double *y2);
  409.   void textTransformDelta(double x1, double y1, double *x2, double *y2);
  410.   // special when y1 == 0
  411.   void textTransformDelta(double x1, double *x2, double *y2);
  412.   double transformWidth(double w);
  413.   double getTransformedLineWidth()
  414.     { return transformWidth(lineWidth); }
  415.   double getTransformedFontSize();
  416.   void getFontTransMat(double *m11, double *m12, double *m21, double *m22);
  417.  
  418.   // Change state parameters.
  419.   void setCTM(double a, double b, double c,
  420.           double d, double e, double f);
  421.   void concatCTM(double a, double b, double c,
  422.          double d, double e, double f);
  423. #if defined(FIXCOLOR)
  424.   void setFillGray(double gray)
  425.     { fillColor.setGray(int(gray * 255.0)); }
  426.   void setFillCMYK(double c, double m, double y, double k)
  427.     { fillColor.setCMYK(int(c*255.0), int(m*255.0), int(y*255.0), int(k*255.0)); }
  428.   void setFillRGB(double r, double g, double b)
  429.     { fillColor.setRGB(int(r*255.0), int(g*255.0), int(b*255.0)); }
  430.   void setStrokeGray(double gray)
  431.     { strokeColor.setGray(int(gray*255.0)); }
  432.   void setStrokeCMYK(double c, double m, double y, double k)
  433.     { strokeColor.setCMYK(int(c*255.0), int(m*255.0), int(y*255.0), int(k*255.0)); }
  434.   void setStrokeRGB(double r, double g, double b)
  435.     { strokeColor.setRGB(int(r*255.0), int(g*255.0), int(b*255.0)); }
  436. #else
  437.   void setFillGray(double gray)
  438.     { fillColor.setGray(gray); }
  439.   void setFillCMYK(double c, double m, double y, double k)
  440.     { fillColor.setCMYK(c, m, y, k); }
  441.   void setFillRGB(double r, double g, double b)
  442.     { fillColor.setRGB(r, g, b); }
  443.   void setStrokeGray(double gray)
  444.     { strokeColor.setGray(gray); }
  445.   void setStrokeCMYK(double c, double m, double y, double k)
  446.     { strokeColor.setCMYK(c, m, y, k); }
  447.   void setStrokeRGB(double r, double g, double b)
  448.     { strokeColor.setRGB(r, g, b); }
  449. #endif
  450.   void setFillColorSpace(GfxColorSpace *colorSpace);
  451.   void setStrokeColorSpace(GfxColorSpace *colorSpace);
  452. #if defined(FIXCOLOR)
  453.   void setFillColor(int x[4])
  454.     { fillColorSpace->getColor(x, &fillColor); }
  455.   void setStrokeColor(int x[4])
  456.     { strokeColorSpace->getColor(x, &strokeColor); }
  457. #else
  458.   void setFillColor(double x[4])
  459.     { fillColorSpace->getColor(x, &fillColor); }
  460.   void setStrokeColor(double x[4])
  461.     { strokeColorSpace->getColor(x, &strokeColor); }
  462. #endif
  463.   void setLineWidth(double width)
  464.     { lineWidth = width; }
  465.   void setLineDash(double *dash, int length, double start);
  466.   void setFlatness(int flatness1) { flatness = flatness1; }
  467.   void setLineJoin(int lineJoin1) { lineJoin = lineJoin1; }
  468.   void setLineCap(int lineCap1) { lineCap = lineCap1; }
  469.   void setMiterLimit(double miterLimit1) { miterLimit = miterLimit1; }
  470.   void setFont(GfxFont *font1, double fontSize1)
  471.     { font = font1; fontSize = fontSize1; }
  472.   void setTextMat(double a, double b, double c,
  473.           double d, double e, double f);
  474.   void setCharSpace(double space)
  475.     { charSpace = space; }
  476.   void setWordSpace(double space)
  477.     { wordSpace = space; }
  478.   void setHorizScaling(double scale)
  479.     { horizScaling = 0.01 * scale; }
  480.   void setLeading(double leading1)
  481.     { leading = leading1; }
  482.   void setRise(double rise1)
  483.     { rise = rise1; }
  484.   void setRender(int render1)
  485.     { render = render1; }
  486.  
  487.   // Add to path.
  488.   void moveTo(double x, double y)
  489.     { path->moveTo(curX = x, curY = y); }
  490.   void lineTo(double x, double y)
  491.     { path->lineTo(curX = x, curY = y); }
  492.   void curveTo(double x1, double y1, double x2, double y2,
  493.            double x3, double y3)
  494.     { path->curveTo(x1, y1, x2, y2, curX = x3, curY = y3); }
  495.   void closePath()
  496.     { path->close(); curX = path->getLastX(); curY = path->getLastY(); }
  497.   void clearPath();
  498.  
  499.   // Text position.
  500.   void textMoveTo(double tx, double ty)
  501.     { lineX = tx; lineY = ty; textTransform(tx, ty, &curX, &curY); }
  502.   void textShift(double tx);
  503.   void textShift(double tx, double ty);
  504.   // Fast textshift when textTransformDelta already has been done
  505.   void textShiftFast(double dx, double dy) { curX += dx; curY += dy;};
  506.   
  507.   // Push/pop GfxState on/off stack.
  508.   GfxState *save();
  509.   GfxState *restore();
  510.   GBool hasSaves() { return saved != NULL; }
  511.  
  512. private:
  513.  
  514.   double ctm[6];        // coord transform matrix
  515.   double px1, py1, px2, py2;    // page corners (user coords)
  516.   double pageWidth, pageHeight;    // page size (pixels)
  517.  
  518.   GfxColorSpace *fillColorSpace;   // fill color space
  519.   GfxColorSpace *strokeColorSpace; // stroke color space
  520.   GfxColor fillColor;        // fill color
  521.   GfxColor strokeColor;        // stroke color
  522.  
  523.   double lineWidth;        // line width
  524.   double *lineDash;        // line dash
  525.   int lineDashLength;
  526.   double lineDashStart;
  527.   int flatness;            // curve flatness
  528.   int lineJoin;            // line join style
  529.   int lineCap;            // line cap style
  530.   double miterLimit;        // line miter limit
  531.  
  532.   GfxFont *font;        // font
  533.   double fontSize;        // font size
  534.   double textMat[6];        // text matrix
  535.   double charSpace;        // character spacing
  536.   double wordSpace;        // word spacing
  537.   double horizScaling;        // horizontal scaling
  538.   double leading;        // text leading
  539.   double rise;            // text rise
  540.   int render;            // text rendering mode
  541.  
  542.   GfxPath *path;        // array of path elements
  543.   double curX, curY;        // current point (user coords)
  544.   double lineX, lineY;        // start of current text line (text coords)
  545.  
  546.   GfxState *saved;        // next GfxState on stack
  547.  
  548.   void ConstructL(GfxState *state);
  549. };
  550.  
  551. #endif
  552.