Coverage Report

Created: 2026-09-14 07:15

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