1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "private/qpaintengine_p.h"
5#include "private/qpainter_p.h"
6#include "private/qpicture_p.h"
7#include "private/qfont_p.h"
8
9#ifndef QT_NO_PICTURE
10
11#include "qbuffer.h"
12#include "qbytearray.h"
13#include "qdatastream.h"
14#include "qmath.h"
15#include "qpaintengine_pic_p.h"
16#include "qpicture.h"
17#include "qpolygon.h"
18#include "qrect.h"
19#include <private/qtextengine_p.h>
20
21//#define QT_PICTURE_DEBUG
22#include <qdebug.h>
23
24
25QT_BEGIN_NAMESPACE
26
27class QPicturePaintEnginePrivate : public QPaintEnginePrivate
28{
29 Q_DECLARE_PUBLIC(QPicturePaintEngine)
30public:
31 QDataStream s;
32 QPainter *pt;
33 QPicturePrivate *pic_d;
34};
35
36QPicturePaintEngine::QPicturePaintEngine()
37 : QPaintEngine(*(new QPicturePaintEnginePrivate), AllFeatures)
38{
39 Q_D(QPicturePaintEngine);
40 d->pt = nullptr;
41}
42
43QPicturePaintEngine::QPicturePaintEngine(QPaintEnginePrivate &dptr)
44 : QPaintEngine(dptr, AllFeatures)
45{
46 Q_D(QPicturePaintEngine);
47 d->pt = nullptr;
48}
49
50QPicturePaintEngine::~QPicturePaintEngine()
51{
52}
53
54bool QPicturePaintEngine::begin(QPaintDevice *pd)
55{
56 Q_D(QPicturePaintEngine);
57#ifdef QT_PICTURE_DEBUG
58 qDebug("QPicturePaintEngine::begin()");
59#endif
60 Q_ASSERT(pd);
61 QPicture *pic = static_cast<QPicture *>(pd);
62
63 d->pdev = pd;
64 d->pic_d = pic->d_func();
65 Q_ASSERT(d->pic_d);
66
67 d->s.setDevice(&d->pic_d->pictb);
68 d->s.setVersion(d->pic_d->formatMajor);
69
70 d->pic_d->pictb.open(openMode: QIODevice::WriteOnly | QIODevice::Truncate);
71 d->s.writeRawData(qt_mfhdr_tag, len: 4);
72 d->s << (quint16) 0 << (quint16) d->pic_d->formatMajor << (quint16) d->pic_d->formatMinor;
73 d->s << (quint8) QPicturePrivate::PdcBegin << (quint8) sizeof(qint32);
74 d->pic_d->brect = QRect();
75 if (d->pic_d->formatMajor >= 4) {
76 QRect r = pic->boundingRect();
77 d->s << (qint32) r.left() << (qint32) r.top() << (qint32) r.width()
78 << (qint32) r.height();
79 }
80 d->pic_d->trecs = 0;
81 d->s << (quint32)d->pic_d->trecs; // total number of records
82 d->pic_d->formatOk = false;
83 setActive(true);
84 return true;
85}
86
87bool QPicturePaintEngine::end()
88{
89 Q_D(QPicturePaintEngine);
90#ifdef QT_PICTURE_DEBUG
91 qDebug("QPicturePaintEngine::end()");
92#endif
93 d->pic_d->trecs++;
94 d->s << (quint8) QPicturePrivate::PdcEnd << (quint8) 0;
95 int cs_start = sizeof(quint32); // pos of checksum word
96 int data_start = cs_start + sizeof(quint16);
97 int brect_start = data_start + 2*sizeof(qint16) + 2*sizeof(quint8);
98 int pos = d->pic_d->pictb.pos();
99 d->pic_d->pictb.seek(off: brect_start);
100 if (d->pic_d->formatMajor >= 4) { // bounding rectangle
101 QRect r = static_cast<QPicture *>(d->pdev)->boundingRect();
102 d->s << (qint32) r.left() << (qint32) r.top() << (qint32) r.width()
103 << (qint32) r.height();
104 }
105 d->s << (quint32) d->pic_d->trecs; // write number of records
106 d->pic_d->pictb.seek(off: cs_start);
107 const QByteArray buf = d->pic_d->pictb.buffer();
108 quint16 cs = (quint16) qChecksum(data: QByteArrayView(buf.constData() + data_start, pos - data_start));
109 d->s << cs; // write checksum
110 d->pic_d->pictb.close();
111 setActive(false);
112 return true;
113}
114
115#define SERIALIZE_CMD(c) \
116 d->pic_d->trecs++; \
117 d->s << (quint8) c; \
118 d->s << (quint8) 0; \
119 pos = d->pic_d->pictb.pos()
120
121void QPicturePaintEngine::updatePen(const QPen &pen)
122{
123 Q_D(QPicturePaintEngine);
124#ifdef QT_PICTURE_DEBUG
125 qDebug() << " -> updatePen(): width:" << pen.width() << "style:"
126 << pen.style() << "color:" << pen.color();
127#endif
128 int pos;
129 SERIALIZE_CMD(QPicturePrivate::PdcSetPen);
130 if (d->pic_d->in_memory_only) {
131 int index = d->pic_d->pen_list.size();
132 d->pic_d->pen_list.append(t: pen);
133 d->s << index;
134 } else {
135 d->s << pen;
136 }
137 writeCmdLength(pos, r: QRect(), corr: false);
138}
139
140void QPicturePaintEngine::updateCompositionMode(QPainter::CompositionMode cmode)
141{
142 Q_D(QPicturePaintEngine);
143#ifdef QT_PICTURE_DEBUG
144 qDebug() << " -> updateCompositionMode():" << cmode;
145#endif
146 int pos;
147 SERIALIZE_CMD(QPicturePrivate::PdcSetCompositionMode);
148 d->s << (qint32)cmode;
149 writeCmdLength(pos, r: QRectF(), corr: false);
150}
151
152void QPicturePaintEngine::updateClipEnabled(bool enabled)
153{
154 Q_D(QPicturePaintEngine);
155#ifdef QT_PICTURE_DEBUG
156 qDebug() << " -> updateClipEnabled():" << enabled;
157#endif
158 int pos;
159 SERIALIZE_CMD(QPicturePrivate::PdcSetClipEnabled);
160 d->s << enabled;
161 writeCmdLength(pos, r: QRectF(), corr: false);
162}
163
164void QPicturePaintEngine::updateOpacity(qreal opacity)
165{
166 Q_D(QPicturePaintEngine);
167#ifdef QT_PICTURE_DEBUG
168 qDebug() << " -> updateOpacity():" << opacity;
169#endif
170 int pos;
171 SERIALIZE_CMD(QPicturePrivate::PdcSetOpacity);
172 d->s << double(opacity);
173 writeCmdLength(pos, r: QRectF(), corr: false);
174}
175
176void QPicturePaintEngine::updateBrush(const QBrush &brush)
177{
178 Q_D(QPicturePaintEngine);
179#ifdef QT_PICTURE_DEBUG
180 qDebug() << " -> updateBrush(): style:" << brush.style();
181#endif
182 int pos;
183 SERIALIZE_CMD(QPicturePrivate::PdcSetBrush);
184 if (d->pic_d->in_memory_only) {
185 int index = d->pic_d->brush_list.size();
186 d->pic_d->brush_list.append(t: brush);
187 d->s << index;
188 } else {
189 d->s << brush;
190 }
191 writeCmdLength(pos, r: QRect(), corr: false);
192}
193
194void QPicturePaintEngine::updateBrushOrigin(const QPointF &p)
195{
196 Q_D(QPicturePaintEngine);
197#ifdef QT_PICTURE_DEBUG
198 qDebug() << " -> updateBrushOrigin(): " << p;
199#endif
200 int pos;
201 SERIALIZE_CMD(QPicturePrivate::PdcSetBrushOrigin);
202 d->s << p;
203 writeCmdLength(pos, r: QRect(), corr: false);
204}
205
206void QPicturePaintEngine::updateFont(const QFont &font)
207{
208 Q_D(QPicturePaintEngine);
209#ifdef QT_PICTURE_DEBUG
210 qDebug() << " -> updateFont(): pt sz:" << font.pointSize();
211#endif
212 int pos;
213 SERIALIZE_CMD(QPicturePrivate::PdcSetFont);
214 QFont fnt = font;
215 d->s << fnt;
216 writeCmdLength(pos, r: QRectF(), corr: false);
217}
218
219void QPicturePaintEngine::updateBackground(Qt::BGMode bgMode, const QBrush &bgBrush)
220{
221 Q_D(QPicturePaintEngine);
222#ifdef QT_PICTURE_DEBUG
223 qDebug() << " -> updateBackground(): mode:" << bgMode << "style:" << bgBrush.style();
224#endif
225 int pos;
226 SERIALIZE_CMD(QPicturePrivate::PdcSetBkColor);
227 d->s << bgBrush.color();
228 writeCmdLength(pos, r: QRect(), corr: false);
229
230 SERIALIZE_CMD(QPicturePrivate::PdcSetBkMode);
231 d->s << (qint8) bgMode;
232 writeCmdLength(pos, r: QRectF(), corr: false);
233}
234
235void QPicturePaintEngine::updateMatrix(const QTransform &matrix)
236{
237 Q_D(QPicturePaintEngine);
238#ifdef QT_PICTURE_DEBUG
239 qDebug() << " -> updateMatrix():" << matrix;
240#endif
241 int pos;
242 SERIALIZE_CMD(QPicturePrivate::PdcSetWMatrix);
243 d->s << matrix << (qint8) false;
244 writeCmdLength(pos, r: QRectF(), corr: false);
245}
246
247void QPicturePaintEngine::updateClipRegion(const QRegion &region, Qt::ClipOperation op)
248{
249 Q_D(QPicturePaintEngine);
250#ifdef QT_PICTURE_DEBUG
251 qDebug() << " -> updateClipRegion(): op:" << op
252 << "bounding rect:" << region.boundingRect();
253#endif
254 int pos;
255 SERIALIZE_CMD(QPicturePrivate::PdcSetClipRegion);
256 d->s << region << qint8(op);
257 writeCmdLength(pos, r: QRectF(), corr: false);
258}
259
260void QPicturePaintEngine::updateClipPath(const QPainterPath &path, Qt::ClipOperation op)
261{
262 Q_D(QPicturePaintEngine);
263#ifdef QT_PICTURE_DEBUG
264 qDebug() << " -> updateClipPath(): op:" << op
265 << "bounding rect:" << path.boundingRect();
266#endif
267 int pos;
268
269 SERIALIZE_CMD(QPicturePrivate::PdcSetClipPath);
270 d->s << path << qint8(op);
271 writeCmdLength(pos, r: QRectF(), corr: false);
272}
273
274void QPicturePaintEngine::updateRenderHints(QPainter::RenderHints hints)
275{
276 Q_D(QPicturePaintEngine);
277#ifdef QT_PICTURE_DEBUG
278 qDebug() << " -> updateRenderHints(): " << hints;
279#endif
280 int pos;
281 SERIALIZE_CMD(QPicturePrivate::PdcSetRenderHint);
282 d->s << (quint32) hints;
283 writeCmdLength(pos, r: QRect(), corr: false);
284}
285
286void QPicturePaintEngine::writeCmdLength(int pos, const QRectF &r, bool corr)
287{
288 Q_D(QPicturePaintEngine);
289 int newpos = d->pic_d->pictb.pos(); // new position
290 int length = newpos - pos;
291 QRectF br(r);
292
293 if (length < 255) { // write 8-bit length
294 d->pic_d->pictb.seek(off: pos - 1); // position to right index
295 d->s << (quint8)length;
296 } else { // write 32-bit length
297 d->s << (quint32)0; // extend the buffer
298 d->pic_d->pictb.seek(off: pos - 1); // position to right index
299 d->s << (quint8)255; // indicate 32-bit length
300 char *p = d->pic_d->pictb.buffer().data();
301 memmove(dest: p+pos+4, src: p+pos, n: length); // make room for 4 byte
302 d->s << (quint32)length;
303 newpos += 4;
304 }
305 d->pic_d->pictb.seek(off: newpos); // set to new position
306
307 if (br.width() > 0.0 || br.height() > 0.0) {
308 if (corr) { // widen bounding rect
309 int w2 = painter()->pen().width() / 2;
310 br.setCoords(xp1: br.left() - w2, yp1: br.top() - w2,
311 xp2: br.right() + w2, yp2: br.bottom() + w2);
312 }
313 br = painter()->transform().mapRect(br);
314 if (painter()->hasClipping()) {
315 QRectF cr = painter()->clipBoundingRect();
316 br &= cr;
317 }
318
319 if (br.width() > 0.0 || br.height() > 0.0) {
320 int minx = qFloor(v: br.left());
321 int miny = qFloor(v: br.top());
322 int maxx = qCeil(v: br.right());
323 int maxy = qCeil(v: br.bottom());
324
325 if (d->pic_d->brect.width() > 0 || d->pic_d->brect.height() > 0) {
326 minx = qMin(a: minx, b: d->pic_d->brect.left());
327 miny = qMin(a: miny, b: d->pic_d->brect.top());
328 maxx = qMax(a: maxx, b: d->pic_d->brect.x() + d->pic_d->brect.width());
329 maxy = qMax(a: maxy, b: d->pic_d->brect.y() + d->pic_d->brect.height());
330 d->pic_d->brect = QRect(minx, miny, maxx - minx, maxy - miny);
331 } else {
332 d->pic_d->brect = QRect(minx, miny, maxx - minx, maxy - miny);
333 }
334 }
335 }
336}
337
338void QPicturePaintEngine::drawEllipse(const QRectF &rect)
339{
340 Q_D(QPicturePaintEngine);
341#ifdef QT_PICTURE_DEBUG
342 qDebug() << " -> drawEllipse():" << rect;
343#endif
344 int pos;
345 SERIALIZE_CMD(QPicturePrivate::PdcDrawEllipse);
346 d->s << rect;
347 writeCmdLength(pos, r: rect, corr: true);
348}
349
350void QPicturePaintEngine::drawPath(const QPainterPath &path)
351{
352 Q_D(QPicturePaintEngine);
353#ifdef QT_PICTURE_DEBUG
354 qDebug() << " -> drawPath():" << path.boundingRect();
355#endif
356 int pos;
357 SERIALIZE_CMD(QPicturePrivate::PdcDrawPath);
358 d->s << path;
359 writeCmdLength(pos, r: path.boundingRect(), corr: true);
360}
361
362void QPicturePaintEngine::drawPolygon(const QPointF *points, int numPoints, PolygonDrawMode mode)
363{
364 Q_D(QPicturePaintEngine);
365#ifdef QT_PICTURE_DEBUG
366 qDebug() << " -> drawPolygon(): size=" << numPoints;
367#endif
368 int pos;
369
370 QPolygonF polygon;
371 polygon.reserve(asize: numPoints);
372 for (int i=0; i<numPoints; ++i)
373 polygon << points[i];
374
375 if (mode == PolylineMode) {
376 SERIALIZE_CMD(QPicturePrivate::PdcDrawPolyline);
377 d->s << polygon;
378 } else {
379 SERIALIZE_CMD(QPicturePrivate::PdcDrawPolygon);
380 d->s << polygon;
381 d->s << (qint8)(mode == OddEvenMode ? 0 : 1);
382 }
383
384 writeCmdLength(pos, r: polygon.boundingRect(), corr: true);
385}
386
387void QPicturePaintEngine::drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr)
388{
389 Q_D(QPicturePaintEngine);
390#ifdef QT_PICTURE_DEBUG
391 qDebug() << " -> drawPixmap():" << r;
392#endif
393 int pos;
394 SERIALIZE_CMD(QPicturePrivate::PdcDrawPixmap);
395
396 if (d->pic_d->in_memory_only) {
397 int index = d->pic_d->pixmap_list.size();
398 d->pic_d->pixmap_list.append(t: pm);
399 d->s << r << index << sr;
400 } else {
401 d->s << r << pm << sr;
402 }
403 writeCmdLength(pos, r, corr: false);
404}
405
406void QPicturePaintEngine::drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPointF &s)
407{
408 Q_D(QPicturePaintEngine);
409#ifdef QT_PICTURE_DEBUG
410 qDebug() << " -> drawTiledPixmap():" << r << s;
411#endif
412 int pos;
413 SERIALIZE_CMD(QPicturePrivate::PdcDrawTiledPixmap);
414 if (d->pic_d->in_memory_only) {
415 int index = d->pic_d->pixmap_list.size();
416 d->pic_d->pixmap_list.append(t: pixmap);
417 d->s << r << index << s;
418 } else {
419 d->s << r << pixmap << s;
420 }
421 writeCmdLength(pos, r, corr: false);
422}
423
424void QPicturePaintEngine::drawImage(const QRectF &r, const QImage &image, const QRectF &sr,
425 Qt::ImageConversionFlags flags)
426{
427 Q_D(QPicturePaintEngine);
428#ifdef QT_PICTURE_DEBUG
429 qDebug() << " -> drawImage():" << r << sr;
430#endif
431 int pos;
432 SERIALIZE_CMD(QPicturePrivate::PdcDrawImage);
433 if (d->pic_d->in_memory_only) {
434 int index = d->pic_d->image_list.size();
435 d->pic_d->image_list.append(t: image);
436 d->s << r << index << sr << (quint32) flags;
437 } else {
438 d->s << r << image << sr << (quint32) flags;
439 }
440 writeCmdLength(pos, r, corr: false);
441}
442
443void QPicturePaintEngine::drawTextItem(const QPointF &p , const QTextItem &ti)
444{
445 Q_D(QPicturePaintEngine);
446#ifdef QT_PICTURE_DEBUG
447 qDebug() << " -> drawTextItem():" << p << ti.text();
448#endif
449
450 const QTextItemInt &si = static_cast<const QTextItemInt &>(ti);
451 if (si.chars == nullptr)
452 QPaintEngine::drawTextItem(p, textItem: ti); // Draw as path
453
454 if (d->pic_d->formatMajor >= 9) {
455 int pos;
456 SERIALIZE_CMD(QPicturePrivate::PdcDrawTextItem);
457 QFont fnt = ti.font();
458 fnt.setUnderline(false);
459 fnt.setStrikeOut(false);
460 fnt.setOverline(false);
461
462 qreal justificationWidth = 0;
463 if (si.justified)
464 justificationWidth = si.width.toReal();
465
466 d->s << p << ti.text() << fnt << ti.renderFlags() << double(fnt.d->dpi)/qt_defaultDpi() << justificationWidth;
467 writeCmdLength(pos, /*brect=*/r: QRectF(), /*corr=*/false);
468 } else if (d->pic_d->formatMajor >= 8) {
469 // old old (buggy) format
470 int pos;
471 SERIALIZE_CMD(QPicturePrivate::PdcDrawTextItem);
472 d->s << QPointF(p.x(), p.y() - ti.ascent()) << ti.text() << ti.font() << ti.renderFlags();
473 writeCmdLength(pos, /*brect=*/r: QRectF(), /*corr=*/false);
474 } else {
475 // old (buggy) format
476 int pos;
477 SERIALIZE_CMD(QPicturePrivate::PdcDrawText2);
478 d->s << p << ti.text();
479 writeCmdLength(pos, r: QRectF(p, QSizeF(1,1)), corr: true);
480 }
481}
482
483void QPicturePaintEngine::updateState(const QPaintEngineState &state)
484{
485 QPaintEngine::DirtyFlags flags = state.state();
486 if (flags & DirtyPen) updatePen(pen: state.pen());
487 if (flags & DirtyBrush) updateBrush(brush: state.brush());
488 if (flags & DirtyBrushOrigin) updateBrushOrigin(p: state.brushOrigin());
489 if (flags & DirtyFont) updateFont(font: state.font());
490 if (flags & DirtyBackground) updateBackground(bgMode: state.backgroundMode(), bgBrush: state.backgroundBrush());
491 if (flags & DirtyTransform) updateMatrix(matrix: state.transform());
492 if (flags & DirtyClipEnabled) updateClipEnabled(enabled: state.isClipEnabled());
493 if (flags & DirtyClipRegion) updateClipRegion(region: state.clipRegion(), op: state.clipOperation());
494 if (flags & DirtyClipPath) updateClipPath(path: state.clipPath(), op: state.clipOperation());
495 if (flags & DirtyHints) updateRenderHints(hints: state.renderHints());
496 if (flags & DirtyCompositionMode) updateCompositionMode(cmode: state.compositionMode());
497 if (flags & DirtyOpacity) updateOpacity(opacity: state.opacity());
498}
499
500QT_END_NAMESPACE
501
502#endif // QT_NO_PICTURE
503

source code of qtbase/src/gui/image/qpaintengine_pic.cpp