home *** CD-ROM | disk | FTP | other *** search
/ Enigma Amiga Life 110 / EnigmaAmiga110CD.iso / indispensabili / utility / apdf / xpdf-0.80 / xpdf / gfxstate.h < prev    next >
C/C++ Source or Header  |  1998-11-27  |  14KB  |  473 lines

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