1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | #ifndef PAINTCOMMANDS_H |
29 | #define PAINTCOMMANDS_H |
30 | |
31 | #include <qcolor.h> |
32 | #include <qmap.h> |
33 | #include <qpainterpath.h> |
34 | #include <qregion.h> |
35 | #include <qregularexpression.h> |
36 | #include <qstringlist.h> |
37 | #include <qpixmap.h> |
38 | #include <qbrush.h> |
39 | #include <qhash.h> |
40 | |
41 | QT_FORWARD_DECLARE_CLASS(QPainter) |
42 | #ifndef QT_NO_OPENGL |
43 | QT_FORWARD_DECLARE_CLASS(QOpenGLFramebufferObject) |
44 | QT_FORWARD_DECLARE_CLASS(QOpenGLPaintDevice) |
45 | QT_FORWARD_DECLARE_CLASS(QOpenGLContext) |
46 | #endif |
47 | |
48 | enum DeviceType { |
49 | WidgetType, |
50 | BitmapType, |
51 | PixmapType, |
52 | ImageType, |
53 | ImageMonoType, |
54 | OpenGLType, |
55 | OpenGLBufferType, |
56 | PictureType, |
57 | PrinterType, |
58 | PdfType, |
59 | PsType, |
60 | GrabType, |
61 | CustomDeviceType, |
62 | CustomWidgetType, |
63 | ImageWidgetType |
64 | }; |
65 | |
66 | /************************************************************************/ |
67 | class PaintCommands |
68 | { |
69 | public: |
70 | // construction / initialization |
71 | PaintCommands(const QStringList &cmds, int w, int h, QImage::Format format) |
72 | : m_painter(0) |
73 | , m_surface_painter(0) |
74 | , m_format(format) |
75 | , m_commands(cmds) |
76 | , m_gradientSpread(QGradient::PadSpread) |
77 | , m_gradientCoordinate(QGradient::LogicalMode) |
78 | , m_width(w) |
79 | , m_height(h) |
80 | , m_verboseMode(false) |
81 | , m_type(WidgetType) |
82 | , m_checkers_background(true) |
83 | , m_shouldDrawText(true) |
84 | #ifndef QT_NO_OPENGL |
85 | , m_default_glcontext(0) |
86 | , m_surface_glcontext(0) |
87 | , m_surface_glbuffer(0) |
88 | , m_surface_glpaintdevice(0) |
89 | #endif |
90 | { staticInit(); } |
91 | |
92 | public: |
93 | void setCheckersBackground(bool b) { staticInit(); m_checkers_background = b; } |
94 | void setContents(const QStringList &cmds) { |
95 | staticInit(); |
96 | m_blockMap.clear(); |
97 | m_pathMap.clear(); |
98 | m_pixmapMap.clear(); |
99 | m_imageMap.clear(); |
100 | m_regionMap.clear(); |
101 | m_gradientStops.clear(); |
102 | m_controlPoints.clear(); |
103 | m_gradientSpread = QGradient::PadSpread; |
104 | m_gradientCoordinate = QGradient::LogicalMode; |
105 | m_commands = cmds; |
106 | |
107 | |
108 | } |
109 | void setPainter(QPainter *pt) { staticInit(); m_painter = pt; } |
110 | void setType(DeviceType t) { staticInit(); m_type = t; } |
111 | void setFilePath(const QString &path) { staticInit(); m_filepath = path; } |
112 | void setControlPoints(const QVector<QPointF> &points) { staticInit(); m_controlPoints = points; } |
113 | void setVerboseMode(bool v) { staticInit(); m_verboseMode = v; } |
114 | void insertAt(int commandIndex, const QStringList &newCommands); |
115 | void setShouldDrawText(bool drawText) { m_shouldDrawText = drawText; } |
116 | |
117 | // run |
118 | void runCommands(); |
119 | |
120 | private: |
121 | // run |
122 | void runCommand(const QString &scriptLine); |
123 | |
124 | // conversion methods |
125 | int convertToInt(const QString &str); |
126 | double convertToDouble(const QString &str); |
127 | float convertToFloat(const QString &str); |
128 | QColor convertToColor(const QString &str); |
129 | |
130 | // commands: comments |
131 | void command_comment(QRegularExpressionMatch re); |
132 | |
133 | // commands: importer |
134 | void command_import(QRegularExpressionMatch re); |
135 | |
136 | // commands: blocks |
137 | void command_begin_block(QRegularExpressionMatch re); |
138 | void command_end_block(QRegularExpressionMatch re); |
139 | void command_repeat_block(QRegularExpressionMatch re); |
140 | |
141 | // commands: misc |
142 | void command_textlayout_draw(QRegularExpressionMatch re); |
143 | void command_abort(QRegularExpressionMatch re); |
144 | |
145 | // commands: noops |
146 | void command_noop(QRegularExpressionMatch re); |
147 | |
148 | // commands: setters |
149 | void command_setBgMode(QRegularExpressionMatch re); |
150 | void command_setBackground(QRegularExpressionMatch re); |
151 | void command_setOpacity(QRegularExpressionMatch re); |
152 | void command_path_setFillRule(QRegularExpressionMatch re); |
153 | void command_setBrush(QRegularExpressionMatch re); |
154 | void command_setBrushOrigin(QRegularExpressionMatch re); |
155 | void command_brushTranslate(QRegularExpressionMatch re); |
156 | void command_brushRotate(QRegularExpressionMatch re); |
157 | void command_brushScale(QRegularExpressionMatch re); |
158 | void command_brushShear(QRegularExpressionMatch re); |
159 | void command_setClipPath(QRegularExpressionMatch re); |
160 | void command_setClipRect(QRegularExpressionMatch re); |
161 | void command_setClipRectangle(QRegularExpressionMatch re); |
162 | void command_setClipRegion(QRegularExpressionMatch re); |
163 | void command_setClipping(QRegularExpressionMatch re); |
164 | void command_setCompositionMode(QRegularExpressionMatch re); |
165 | void command_setFont(QRegularExpressionMatch re); |
166 | void command_setPen(QRegularExpressionMatch re); |
167 | void command_setPen2(QRegularExpressionMatch re); |
168 | void command_pen_setDashOffset(QRegularExpressionMatch re); |
169 | void command_pen_setDashPattern(QRegularExpressionMatch re); |
170 | void command_pen_setCosmetic(QRegularExpressionMatch re); |
171 | void command_setRenderHint(QRegularExpressionMatch re); |
172 | void command_clearRenderHint(QRegularExpressionMatch re); |
173 | void command_gradient_appendStop(QRegularExpressionMatch re); |
174 | void command_gradient_clearStops(QRegularExpressionMatch re); |
175 | void command_gradient_setConical(QRegularExpressionMatch re); |
176 | void command_gradient_setLinear(QRegularExpressionMatch re); |
177 | void command_gradient_setRadial(QRegularExpressionMatch re); |
178 | void command_gradient_setRadialExtended(QRegularExpressionMatch re); |
179 | void command_gradient_setLinearPen(QRegularExpressionMatch re); |
180 | void command_gradient_setSpread(QRegularExpressionMatch re); |
181 | void command_gradient_setCoordinateMode(QRegularExpressionMatch re); |
182 | |
183 | // commands: drawing ops |
184 | void command_drawArc(QRegularExpressionMatch re); |
185 | void command_drawChord(QRegularExpressionMatch re); |
186 | void command_drawConvexPolygon(QRegularExpressionMatch re); |
187 | void command_drawEllipse(QRegularExpressionMatch re); |
188 | void command_drawImage(QRegularExpressionMatch re); |
189 | void command_drawLine(QRegularExpressionMatch re); |
190 | void command_drawLines(QRegularExpressionMatch re); |
191 | void command_drawPath(QRegularExpressionMatch re); |
192 | void command_drawPie(QRegularExpressionMatch re); |
193 | void command_drawPixmap(QRegularExpressionMatch re); |
194 | void command_drawPoint(QRegularExpressionMatch re); |
195 | void command_drawPolygon(QRegularExpressionMatch re); |
196 | void command_drawPolyline(QRegularExpressionMatch re); |
197 | void command_drawRect(QRegularExpressionMatch re); |
198 | void command_drawRoundedRect(QRegularExpressionMatch re); |
199 | void command_drawRoundRect(QRegularExpressionMatch re); |
200 | void command_drawText(QRegularExpressionMatch re); |
201 | void command_drawStaticText(QRegularExpressionMatch re); |
202 | void command_drawTextDocument(QRegularExpressionMatch re); |
203 | void command_drawTiledPixmap(QRegularExpressionMatch re); |
204 | void command_fillRect(QRegularExpressionMatch re); |
205 | void command_fillRectF(QRegularExpressionMatch re); |
206 | |
207 | // paths |
208 | void command_path_addEllipse(QRegularExpressionMatch re); |
209 | void command_path_addPolygon(QRegularExpressionMatch re); |
210 | void command_path_addRect(QRegularExpressionMatch re); |
211 | void command_path_addText(QRegularExpressionMatch re); |
212 | void command_path_arcTo(QRegularExpressionMatch re); |
213 | void command_path_closeSubpath(QRegularExpressionMatch re); |
214 | void command_path_createOutline(QRegularExpressionMatch re); |
215 | void command_path_cubicTo(QRegularExpressionMatch re); |
216 | void command_path_debugPrint(QRegularExpressionMatch re); |
217 | void command_path_lineTo(QRegularExpressionMatch re); |
218 | void command_path_moveTo(QRegularExpressionMatch re); |
219 | void command_region_addEllipse(QRegularExpressionMatch re); |
220 | void command_region_addRect(QRegularExpressionMatch re); |
221 | |
222 | // getters |
223 | void command_region_getClipRegion(QRegularExpressionMatch re); |
224 | void command_path_getClipPath(QRegularExpressionMatch re); |
225 | |
226 | // commands: surface begin/end |
227 | void command_surface_begin(QRegularExpressionMatch re); |
228 | void command_surface_end(QRegularExpressionMatch re); |
229 | |
230 | // commands: save/restore painter state |
231 | void command_restore(QRegularExpressionMatch re); |
232 | void command_save(QRegularExpressionMatch re); |
233 | |
234 | // commands: pixmap/image |
235 | void command_pixmap_load(QRegularExpressionMatch re); |
236 | void command_pixmap_setMask(QRegularExpressionMatch re); |
237 | void command_bitmap_load(QRegularExpressionMatch re); |
238 | void command_pixmap_setDevicePixelRatio(QRegularExpressionMatch re); |
239 | void command_image_convertToFormat(QRegularExpressionMatch re); |
240 | void command_image_load(QRegularExpressionMatch re); |
241 | void command_image_setColor(QRegularExpressionMatch re); |
242 | void command_image_setColorCount(QRegularExpressionMatch re); |
243 | void command_image_setDevicePixelRatio(QRegularExpressionMatch re); |
244 | |
245 | // commands: transformation |
246 | void command_resetMatrix(QRegularExpressionMatch re); |
247 | void command_translate(QRegularExpressionMatch re); |
248 | void command_rotate(QRegularExpressionMatch re); |
249 | void command_rotate_x(QRegularExpressionMatch re); |
250 | void command_rotate_y(QRegularExpressionMatch re); |
251 | void command_scale(QRegularExpressionMatch re); |
252 | void command_mapQuadToQuad(QRegularExpressionMatch re); |
253 | void command_setMatrix(QRegularExpressionMatch re); |
254 | |
255 | // attributes |
256 | QPainter *m_painter; |
257 | QPainter *m_surface_painter; |
258 | QImage::Format m_format; |
259 | QImage m_surface_image; |
260 | QRectF m_surface_rect; |
261 | QStringList m_commands; |
262 | QString m_currentCommand; |
263 | int m_currentCommandIndex; |
264 | QString m_filepath; |
265 | QMap<QString, QStringList> m_blockMap; |
266 | QMap<QString, QPainterPath> m_pathMap; |
267 | QMap<QString, QPixmap> m_pixmapMap; |
268 | QMap<QString, QImage> m_imageMap; |
269 | QMap<QString, QRegion> m_regionMap; |
270 | QGradientStops m_gradientStops; |
271 | QGradient::Spread m_gradientSpread; |
272 | QGradient::CoordinateMode m_gradientCoordinate; |
273 | bool m_abort; |
274 | int m_width; |
275 | int m_height; |
276 | |
277 | bool m_verboseMode; |
278 | DeviceType m_type; |
279 | bool m_checkers_background; |
280 | bool m_shouldDrawText; |
281 | |
282 | QVector<QPointF> m_controlPoints; |
283 | |
284 | #ifndef QT_NO_OPENGL |
285 | QOpenGLContext *m_default_glcontext; |
286 | QOpenGLContext *m_surface_glcontext; |
287 | QOpenGLFramebufferObject *m_surface_glbuffer; |
288 | QOpenGLPaintDevice *m_surface_glpaintdevice; |
289 | #endif |
290 | |
291 | // painter functionalities string tables |
292 | static const char *brushStyleTable[]; |
293 | static const char *penStyleTable[]; |
294 | static const char *fontWeightTable[]; |
295 | static const char *fontHintingTable[]; |
296 | static const char *clipOperationTable[]; |
297 | static const char *spreadMethodTable[]; |
298 | static const char *coordinateMethodTable[]; |
299 | static const char *compositionModeTable[]; |
300 | static const char *imageFormatTable[]; |
301 | static const char *sizeModeTable[]; |
302 | static int translateEnum(const char *table[], const QString &pattern, int limit); |
303 | |
304 | // utility |
305 | template <typename T> T image_load(const QString &filepath); |
306 | |
307 | // commands dictionary management |
308 | static void staticInit(); |
309 | |
310 | public: |
311 | struct PaintCommandInfos |
312 | { |
313 | PaintCommandInfos(QString id, void (PaintCommands::*p)(QRegularExpressionMatch), QRegularExpression r, QString sy, QString sa) |
314 | : identifier(id) |
315 | , regExp(r) |
316 | , syntax(sy) |
317 | , sample(sa) |
318 | , paintMethod(p) |
319 | {} |
320 | PaintCommandInfos(QString title) |
321 | : identifier(title), paintMethod(0) {} |
322 | bool isSectionHeader() const { return paintMethod == 0; } |
323 | QString identifier; |
324 | QRegularExpression regExp; |
325 | QString syntax; |
326 | QString sample; |
327 | void (PaintCommands::*paintMethod)(QRegularExpressionMatch); |
328 | }; |
329 | |
330 | static PaintCommandInfos *findCommandById(const QString &identifier) { |
331 | for (int i=0; i<s_commandInfoTable.size(); i++) |
332 | if (s_commandInfoTable[i].identifier == identifier) |
333 | return &s_commandInfoTable[i]; |
334 | return 0; |
335 | } |
336 | |
337 | static QList<PaintCommandInfos> s_commandInfoTable; |
338 | static QList<QPair<QString,QStringList> > s_enumsTable; |
339 | static QMultiHash<QString, int> s_commandHash; |
340 | }; |
341 | |
342 | #endif // PAINTCOMMANDS_H |
343 | |