1 // -*- C++ -*-
2 // --------------------------------------------------------------------
3 // Painter abstraction
4 // --------------------------------------------------------------------
5 /*
6 
7     This file is part of the extensible drawing editor Ipe.
8     Copyright (c) 1993-2020 Otfried Cheong
9 
10     Ipe is free software; you can redistribute it and/or modify it
11     under the terms of the GNU General Public License as published by
12     the Free Software Foundation; either version 3 of the License, or
13     (at your option) any later version.
14 
15     As a special exception, you have permission to link Ipe with the
16     CGAL library and distribute executables, as long as you follow the
17     requirements of the Gnu General Public License in regard to all of
18     the software in the executable aside from CGAL.
19 
20     Ipe is distributed in the hope that it will be useful, but WITHOUT
21     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22     or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
23     License for more details.
24 
25     You should have received a copy of the GNU General Public License
26     along with Ipe; if not, you can find it at
27     "http://www.gnu.org/copyleft/gpl.html", or write to the Free
28     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 
30 */
31 
32 #ifndef IPEPAINTER_H
33 #define IPEPAINTER_H
34 
35 #include <list>
36 
37 #include "ipeattributes.h"
38 #include "ipestyle.h"
39 #include "ipebitmap.h"
40 
41 // --------------------------------------------------------------------
42 
43 namespace ipe {
44 
45   class Painter {
46   public:
47     struct State {
48       Color iStroke;
49       Color iFill;
50       Fixed iPen;
51       String iDashStyle;
52       TLineCap iLineCap;
53       TLineJoin iLineJoin;
54       TFillRule iFillRule;
55       Color iSymStroke;
56       Color iSymFill;
57       Fixed iSymPen;
58       Fixed iOpacity;
59       Fixed iStrokeOpacity;
60       Attribute iTiling;
61       Attribute iGradient;
62     };
63 
64   public:
65     Painter(const Cascade *style);
66     virtual ~Painter();
67 
68     void setAttributeMap(const AttributeMap *map);
69     Attribute lookup(Kind kind, Attribute sym) const;
70 
71     void transform(const Matrix &m);
72     void untransform(TTransformations trans);
73     void translate(const Vector &v);
74     void push();
75     void pop();
76     void pushMatrix();
77     void popMatrix();
78 
79     void newPath();
80     void moveTo(const Vector &v);
81     void lineTo(const Vector &v);
82     void curveTo(const Vector &v1, const Vector &v2, const Vector &v3);
83     inline void curveTo(const Bezier &bezier);
84     void rect(const Rect &re);
85     void drawEllipse();
86     void drawArc(const Arc &arc);
87     void closePath();
88     void drawPath(TPathMode mode);
89     void drawBitmap(Bitmap bitmap);
90     void drawText(const Text *text);
91     void drawSymbol(Attribute symbol);
92     void addClipPath();
93 
94     void setStroke(Attribute color);
95     void setFill(Attribute color);
96     void setPen(Attribute pen);
97     void setDashStyle(Attribute dash);
98     void setLineCap(TLineCap cap);
99     void setLineJoin(TLineJoin join);
100     void setFillRule(TFillRule rule);
101     void setSymStroke(Attribute color);
102     void setSymFill(Attribute color);
103     void setSymPen(Attribute wid);
104     void setOpacity(Attribute opaq);
105     void setStrokeOpacity(Attribute opaq);
106     void setTiling(Attribute til);
107     void setGradient(Attribute grad);
108 
109     //! Return style sheet cascade.
cascade()110     inline const Cascade *cascade() const { return iCascade; }
111     //! Return current stroke color.
stroke()112     inline Color stroke() const { return iState.back().iStroke; }
113     //! Return current fill color.
fill()114     inline Color fill() const { return iState.back().iFill; }
115     //! Return current transformation matrix.
matrix()116     inline const Matrix &matrix() const { return iMatrix.back(); }
117     //! Return current pen.
pen()118     inline Fixed pen() const {return iState.back().iPen; }
119     //! Return current dash style (always absolute attribute).
dashStyle()120     inline String dashStyle() const {return iState.back().iDashStyle; }
121     void dashStyle(std::vector<double> &dashes, double &offset) const;
122     //! Return current line cap.
lineCap()123     inline TLineCap lineCap() const {return iState.back().iLineCap; }
124     //! Return current line join.
lineJoin()125     inline TLineJoin lineJoin() const {return iState.back().iLineJoin; }
126     //! Return current fill rule.
fillRule()127     inline TFillRule fillRule() const {return iState.back().iFillRule; }
128     //! Return current symbol stroke color.
symStroke()129     inline Color symStroke() const { return iState.back().iSymStroke; }
130     //! Return current symbol fill color.
symFill()131     inline Color symFill() const { return iState.back().iSymFill; }
132     //! Return current symbol pen.
symPen()133     inline Fixed symPen() const { return iState.back().iSymPen; }
134     //! Return current opacity.
opacity()135     inline Fixed opacity() const { return iState.back().iOpacity; }
136     //! Return current stroke opacity.
strokeOpacity()137     inline Fixed strokeOpacity() const { return iState.back().iStrokeOpacity; }
138     //! Return current tiling.
tiling()139     inline Attribute tiling() const { return iState.back().iTiling; }
140     //! Return current gradient fill.
gradient()141     inline Attribute gradient() const { return iState.back().iGradient; }
142 
143     //! Return full current graphics state.
state()144     inline const State & state() const { return iState.back(); }
145     void setState(const State &state);
146 
147   protected:
148     virtual void doPush();
149     virtual void doPop();
150     virtual void doNewPath();
151     virtual void doMoveTo(const Vector &v);
152     virtual void doLineTo(const Vector &v);
153     virtual void doCurveTo(const Vector &v1, const Vector &v2,
154 			   const Vector &v3);
155     virtual void doDrawArc(const Arc &arc);
156     virtual void doClosePath();
157     virtual void doDrawPath(TPathMode mode);
158     virtual void doDrawBitmap(Bitmap bitmap);
159     virtual void doDrawText(const Text *text);
160     virtual void doDrawSymbol(Attribute symbol);
161     virtual void doAddClipPath();
162 
163     void drawArcAsBezier(double alpha);
164 
165   protected:
166     std::list<State> iState;
167     std::list<Matrix> iMatrix;
168     const Cascade *iCascade;
169     const AttributeMap *iAttributeMap;
170     int iInPath;
171   };
172 
173   // --------------------------------------------------------------------
174 
175   //! Overloaded function.
176   /*! Assumes current position is \c bezier.iV[0] */
curveTo(const Bezier & bezier)177   inline void Painter::curveTo(const Bezier &bezier)
178   {
179     curveTo(bezier.iV[1], bezier.iV[2], bezier.iV[3]);
180   }
181 
182 } // namespace
183 
184 // --------------------------------------------------------------------
185 #endif
186