1 /*
2 For general Scribus (>=1.3.2) copyright and licensing information please refer
3 to the COPYING file provided with the program. Following this notice may exist
4 a copyright and/or license notice that predates the release of Scribus 1.3.2
5 for which a new license (GPL+exception) is in place.
6 */
7 
8 #ifndef __SCPAINTER_H__
9 #define __SCPAINTER_H__
10 
11 #include <QPainterPath>
12 #include <QPainter>
13 #include <QVector>
14 #include <QStack>
15 #include <QColor>
16 #include <QTransform>
17 #include <QFont>
18 #include <QImage>
19 #include <QPointF>
20 #include "scribusapi.h"
21 #include "scconfig.h"
22 #include "sctextstruct.h"
23 #include "fpoint.h"
24 #include "fpointarray.h"
25 #include "vgradient.h"
26 #include "mesh.h"
27 
28 class ScPattern;
29 
30 typedef struct _cairo cairo_t;
31 typedef struct _cairo_surface cairo_surface_t;
32 typedef struct _cairo_pattern cairo_pattern_t;
33 
34 class SCRIBUS_API ScPainter
35 {
36 public:
37 	ScPainter(QImage *target, int w, int h, double transparency = 1.0, int blendmode = 0);
38 	virtual ~ScPainter();
39 	enum FillMode { None, Solid, Gradient, Pattern, Hatch };
40 	virtual void beginLayer(double transparency, int blendmode, FPointArray *clipArray = nullptr);
41 	virtual void endLayer();
42 	virtual void setAntialiasing(bool enable);
43 	virtual void begin();
44 	virtual void end();
45 	void clear();
46 	virtual void clear(const QColor &);
47 	virtual cairo_t* context();
48 
49 	// matrix manipulation
50 	virtual void setWorldMatrix(const QTransform &);
51 	virtual const QTransform worldMatrix();
52 	virtual void setZoomFactor(double);
zoomFactor()53 	virtual double zoomFactor() { return m_zoomFactor; }
54 	virtual void translate(double, double);
55 	virtual void translate(const QPointF& offset);
56 	virtual void rotate(double);
57 	virtual void scale(double, double);
58 
59 	// drawing
60 	virtual void moveTo(const double &, const double &);
61 	virtual void lineTo(const double &, const double &);
62 	virtual void curveTo(const FPoint& p1, const FPoint& p2, const FPoint& p3);
63 	virtual void newPath();
64 	virtual void closePath();
65 	virtual void fillPath();
66 	virtual void strokePath();
67 	virtual void setFillRule(bool fillRule);
fillRule()68 	virtual bool fillRule() { return m_fillRule; }
69 	virtual void setFillMode(int fill);
fillMode()70 	virtual int  fillMode() { return m_fillMode; }
maskMode()71 	virtual int  maskMode() { return m_maskMode; }
72 	virtual void setStrokeMode(int stroke);
strokeMode()73 	virtual int  strokeMode() { return m_strokeMode; }
74 	virtual void setGradient(VGradient::VGradientType mode, const FPoint& orig, const FPoint& vec, const FPoint& foc, double scale, double skew);
75 	virtual void setPattern(ScPattern *pattern, double scaleX, double scaleY, double offsetX, double offsetY, double rotation, double skewX, double skewY, bool mirrorX, bool mirrorY);
76 
77 	virtual void setMaskMode(int mask);
78 	virtual void setGradientMask(VGradient::VGradientType mode, const FPoint& orig, const FPoint& vec, const FPoint& foc, double scale, double skew);
79 	virtual void setPatternMask(ScPattern *pattern, double scaleX, double scaleY, double offsetX, double offsetY, double rotation, double skewX, double skewY, bool mirrorX, bool mirrorY);
80 
81 	virtual void set4ColorGeometry(const FPoint& p1, const FPoint& p2, const FPoint& p3, const FPoint& p4, const FPoint& c1, const FPoint& c2, const FPoint& c3, const FPoint& c4);
82 	virtual void set4ColorColors(const QColor& col1, const QColor& col2, const QColor& col3, const QColor& col4);
83 	virtual void setDiamondGeometry(const FPoint& p1, const FPoint& p2, const FPoint& p3, const FPoint& p4, const FPoint& c1, const FPoint& c2, const FPoint& c3, const FPoint& c4, const FPoint& c5);
84 	virtual void setMeshGradient(const FPoint& p1, const FPoint& p2, const FPoint& p3, const FPoint& p4, const QList<QList<MeshPoint> >& meshArray);
85 	virtual void setMeshGradient(const FPoint& p1, const FPoint& p2, const FPoint& p3, const FPoint& p4, const QList<meshGradientPatch>& meshPatches);
86 
87 	virtual void setHatchParameters(int mode, double distance, double angle, bool useBackground, const QColor& background, const QColor& foreground, double width, double height);
88 
89 	virtual void setClipPath();
90 
91 	virtual void drawImage(QImage *image);
92 	virtual void setupPolygon(const FPointArray *points, bool closed = true);
93 	virtual void setupSharpPolygon(const FPointArray *points, bool closed = true);
94 	virtual void sharpLineHelper(FPoint &pp);
95 	virtual void sharpLineHelper(QPointF &pp);
96 	virtual void drawPolygon();
97 	virtual void drawPolyLine();
98 	virtual void drawLine(const FPoint& start, const FPoint& end);
99 	virtual void drawLine(const QPointF& start, const QPointF& end);
100 	virtual void drawSharpLine(FPoint start, FPoint end);
101 	virtual void drawSharpLine(QPointF start, QPointF end);
102 	virtual void drawRect(double, double, double, double);
103 	virtual void drawSharpRect(double x, double y, double w, double h);
104 	virtual void drawText(QRectF area, const QString& text, bool filled = true, int align = 0);
105 	virtual void drawShadeCircle(const QRectF &re, const QColor& color, bool sunken, int lineWidth);
106 	virtual void drawShadePanel(const QRectF &r, const QColor& color, bool sunken, int lineWidth);
107 	virtual void drawUnderlinedRect(const QRectF &r, const QColor& color, int lineWidth);
108 	virtual void colorizeAlpha(const QColor& color);
109 	virtual void colorize(const QColor& color);
110 	virtual void blurAlpha(int radius);
111 	virtual void blur(int radius);
112 
113 	// pen + brush
114 	virtual QColor pen();
115 	virtual QColor brush();
116 	virtual double brushOpacity();
117 	virtual void setPen(const QColor &);
118 	virtual void setPen(const QColor &c, double w, Qt::PenStyle st, Qt::PenCapStyle ca, Qt::PenJoinStyle jo);
119 	virtual void setPenOpacity(double op);
120 	virtual void setLineWidth(double w);
121 	virtual void setDash(const QVector<double>& m_array, double ofs);
122 	virtual void setBrush(const QColor &);
123 	virtual void setBrushOpacity(double op);
124 	virtual void setOpacity(double op);
125 	virtual void setFont(const ScFace &f , double s);
126 
127 	// stack management
128 	virtual void save();
129 	virtual void restore();
130 	virtual int blendModeFill();
131 
132 
133 	virtual void setRasterOp(int blendMode);
134 	virtual void setBlendModeFill(int blendMode);
135 	virtual void setBlendModeStroke(int blendMode);
136 
137 	VGradient fill_gradient;
138 	VGradient stroke_gradient;
139 	VGradient mask_gradient;
140 	ScPattern *m_maskPattern;
141 	ScPattern *m_pattern;
142 
143 private:
144 	void fillPathHelper();
145 	void strokePathHelper();
146 
147 	cairo_t *m_cr;
148 	struct layerProp
149 	{
150 		cairo_surface_t *data;
151 		int blendmode;
152 		double tranparency;
153 		int maskMode;				// 0 = none, 1 = gradient 2 = pattern
154 		double mask_patternScaleX;
155 		double mask_patternScaleY;
156 		double mask_patternOffsetX;
157 		double mask_patternOffsetY;
158 		double mask_patternRotation;
159 		double mask_patternSkewX;
160 		double mask_patternSkewY;
161 		bool mask_patternMirrorX;
162 		bool mask_patternMirrorY;
163 		double mask_gradientScale;
164 		double mask_gradientSkew;
165 		VGradient mask_gradient;
166 		ScPattern *maskPattern;
167 		FPointArray groupClip;
168 		bool pushed;
169 		bool fillRule;
170 	};
171 	cairo_pattern_t *getMaskPattern();
172 	cairo_surface_t *m_imageMask;
173 	QImage m_imageQ;
174 
175 	QStack<layerProp> m_Layers;
176 	QStack<double> m_zoomStack;
177 	QImage *m_image;
178 	double  m_layerTransparency;
179 	int  m_blendMode;
180 	int  m_blendModeFill;
181 	int  m_blendModeStroke;
182 	unsigned int m_width;
183 	unsigned int m_height;
184 	QTransform m_matrix;
185 	ScFace m_font;
186 	double m_fontSize;
187 	bool mf_underline;
188 	bool mf_strikeout;
189 	bool mf_shadow;
190 	bool mf_outlined;
191 	/*! \brief Filling */
192 	QColor m_fill;
193 	double m_fill_trans;
194 	bool m_fillRule;
195 	int m_fillMode;				// 0 = none, 1 = solid, 2 = gradient 3 = pattern 4 = hatch
196 	double m_patternScaleX;
197 	double m_patternScaleY;
198 	double m_patternOffsetX;
199 	double m_patternOffsetY;
200 	double m_patternRotation;
201 	double m_patternSkewX;
202 	double m_patternSkewY;
203 	bool m_patternMirrorX;
204 	bool m_patternMirrorY;
205 	double m_gradientScale;
206 	double m_gradientSkew;
207 	FPoint gradPatchP1;
208 	FPoint gradPatchP2;
209 	FPoint gradPatchP3;
210 	FPoint gradPatchP4;
211 	FPoint gradControlP1;
212 	FPoint gradControlP2;
213 	FPoint gradControlP3;
214 	FPoint gradControlP4;
215 	FPoint gradControlP5;
216 	QColor gradPatchColor1;
217 	QColor gradPatchColor2;
218 	QColor gradPatchColor3;
219 	QColor gradPatchColor4;
220 	QList<QList<MeshPoint> > meshGradientArray;
221 	QList<meshGradientPatch> meshGradientPatches;
222 	double m_hatchAngle;
223 	double m_hatchDistance;
224 	int m_hatchType;				// 0 = single 1 = double 2 = triple
225 	bool m_hatchUseBackground;
226 	QColor m_hatchBackground;
227 	QColor m_hatchForeground;
228 	double m_hatchWidth;
229 	double m_hatchHeight;
230 	/*! \brief Stroking */
231 	QColor m_stroke;
232 	double m_stroke_trans;
233 	double m_LineWidth;
234 	int m_strokeMode;				// 0 = none, 1 = solid, 2 = gradient 3 = pattern
235 	int m_maskMode;				// 0 = none, 1 = gradient 2 = pattern
236 	double m_mask_patternScaleX;
237 	double m_mask_patternScaleY;
238 	double m_mask_patternOffsetX;
239 	double m_mask_patternOffsetY;
240 	double m_mask_patternRotation;
241 	double m_mask_patternSkewX;
242 	double m_mask_patternSkewY;
243 	bool m_mask_patternMirrorX;
244 	bool m_mask_patternMirrorY;
245 	double m_mask_gradientScale;
246 	double m_mask_gradientSkew;
247 
248 	/*! \brief Line End Style */
249 	Qt::PenCapStyle PLineEnd;
250   /*! \brief Line Join Style */
251 	Qt::PenJoinStyle PLineJoin;
252   /*! \brief The Dash Array */
253 	QVector<double> m_array;
254 	double m_offset;
255 	/*! \brief Zoom Factor of the Painter */
256 	double m_zoomFactor;
257 	bool m_imageMode;
258 	bool m_layeredMode;
259 	bool m_svgMode;
260 };
261 
262 #endif
263