1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the painting module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include "private/qpaintengine_p.h"
30
#include "private/qpainter_p.h"
31
#include "private/qpicture_p.h"
34
#include "qbytearray.h"
35
#include "qdatastream.h"
36
#include "qpaintengine_pic_p.h"
41
// #define QT_PICTURE_DEBUG
44
class QPicturePaintEnginePrivate : public QPaintEnginePrivate
46
Q_DECLARE_PUBLIC(QPicturePaintEngine)
50
QPicturePrivate *pic_d;
53
QPicturePaintEngine::QPicturePaintEngine()
54
: QPaintEngine(*(new QPicturePaintEnginePrivate), AllFeatures)
56
Q_D(QPicturePaintEngine);
60
QPicturePaintEngine::QPicturePaintEngine(QPaintEnginePrivate &dptr)
61
: QPaintEngine(dptr, AllFeatures)
63
Q_D(QPicturePaintEngine);
67
QPicturePaintEngine::~QPicturePaintEngine()
71
bool QPicturePaintEngine::begin(QPaintDevice *pd)
73
Q_D(QPicturePaintEngine);
74
#ifdef QT_PICTURE_DEBUG
75
qDebug() << "QPicturePaintEngine::begin()";
78
QPicture *pic = static_cast<QPicture *>(pd);
81
d->pic_d = pic->d_func();
84
d->s.setDevice(&d->pic_d->pictb);
85
d->s.setVersion(d->pic_d->formatMajor);
87
d->pic_d->pictb.open(QIODevice::WriteOnly | QIODevice::Truncate);
88
d->s.writeRawData(qt_mfhdr_tag, 4);
89
d->s << (quint16) 0 << (quint16) d->pic_d->formatMajor << (quint16) d->pic_d->formatMinor;
90
d->s << (quint8) QPicturePrivate::PdcBegin << (quint8) sizeof(qint32);
91
d->pic_d->brect = QRect();
92
if (d->pic_d->formatMajor >= 4) {
93
QRect r = d->pic_d->brect;
94
d->s << (qint32) r.left() << (qint32) r.top() << (qint32) r.width()
95
<< (qint32) r.height();
98
d->s << (quint32)d->pic_d->trecs; // total number of records
99
d->pic_d->formatOk = false;
104
bool QPicturePaintEngine::end()
106
Q_D(QPicturePaintEngine);
107
#ifdef QT_PICTURE_DEBUG
108
qDebug() << "QPicturePaintEngine::end()";
112
d->s << (quint8) QPicturePrivate::PdcEnd << (quint8) 0;
113
int cs_start = sizeof(quint32); // pos of checksum word
114
int data_start = cs_start + sizeof(quint16);
115
int brect_start = data_start + 2*sizeof(qint16) + 2*sizeof(quint8);
116
int pos = d->pic_d->pictb.pos();
117
d->pic_d->pictb.seek(brect_start);
118
if (d->pic_d->formatMajor >= 4) { // bounding rectangle
119
QRect r = d->pic_d->brect;
120
d->s << (qint32) r.left() << (qint32) r.top() << (qint32) r.width()
121
<< (qint32) r.height();
123
d->s << (quint32) d->pic_d->trecs; // write number of records
124
d->pic_d->pictb.seek(cs_start);
125
QByteArray buf = d->pic_d->pictb.buffer();
126
quint16 cs = (quint16) qChecksum(buf.constData() + data_start, pos - data_start);
127
d->s << cs; // write checksum
128
d->pic_d->pictb.close();
133
#define SERIALIZE_CMD(c) \
135
d->s << (quint8) c; \
136
d->s << (quint8) 0; \
137
pos = d->pic_d->pictb.pos()
139
void QPicturePaintEngine::updatePen(const QPen &pen)
141
Q_D(QPicturePaintEngine);
142
#ifdef QT_PICTURE_DEBUG
143
qDebug() << " -> updatePen(): width:" << pen.width() << "style:"
144
<< pen.style() << "color:" << pen.color();
147
SERIALIZE_CMD(QPicturePrivate::PdcSetPen);
149
writeCmdLength(pos, QRect(), false);
152
void QPicturePaintEngine::updateBrush(const QBrush &brush, const QPointF &)
154
Q_D(QPicturePaintEngine);
155
#ifdef QT_PICTURE_DEBUG
156
qDebug() << " -> updateBrush(): style:" << brush.style();
159
SERIALIZE_CMD(QPicturePrivate::PdcSetBrush);
161
writeCmdLength(pos, QRect(), false);
164
void QPicturePaintEngine::updateFont(const QFont &font)
166
Q_D(QPicturePaintEngine);
167
#ifdef QT_PICTURE_DEBUG
168
qDebug() << " -> updateFont(): pt sz:" << font.pointSize();
171
SERIALIZE_CMD(QPicturePrivate::PdcSetFont);
174
writeCmdLength(pos, QRectF(), false);
177
void QPicturePaintEngine::updateBackground(Qt::BGMode bgMode, const QBrush &bgBrush)
179
Q_D(QPicturePaintEngine);
180
#ifdef QT_PICTURE_DEBUG
181
qDebug() << " -> updateBackground(): mode:" << bgMode << "style:" << bgBrush.style();
184
SERIALIZE_CMD(QPicturePrivate::PdcSetBkColor);
185
d->s << bgBrush.color();
186
writeCmdLength(pos, QRect(), false);
188
SERIALIZE_CMD(QPicturePrivate::PdcSetBkMode);
189
d->s << (qint8) bgMode;
190
writeCmdLength(pos, QRectF(), false);
193
void QPicturePaintEngine::updateMatrix(const QMatrix &matrix)
195
Q_D(QPicturePaintEngine);
196
#ifdef QT_PICTURE_DEBUG
197
qDebug() << " -> updateMatrix():" << matrix;
200
SERIALIZE_CMD(QPicturePrivate::PdcSetWMatrix);
201
d->s << matrix << (qint8) false;
202
writeCmdLength(pos, QRectF(), false);
205
void QPicturePaintEngine::updateClipRegion(const QRegion ®ion, Qt::ClipOperation op)
207
Q_D(QPicturePaintEngine);
208
#ifdef QT_PICTURE_DEBUG
209
qDebug() << " -> updateClipRegion(): op:" << op
210
<< "bounding rect:" << region.boundingRect();
214
SERIALIZE_CMD(QPicturePrivate::PdcSetClipRegion);
215
d->s << region << qint8(0);
216
writeCmdLength(pos, QRectF(), false);
218
SERIALIZE_CMD(QPicturePrivate::PdcSetClip);
219
d->s << (qint8) !region.isEmpty();
220
writeCmdLength(pos, QRectF(), false);
223
void QPicturePaintEngine::updateClipPath(const QPainterPath &path, Qt::ClipOperation op)
225
Q_D(QPicturePaintEngine);
226
#ifdef QT_PICTURE_DEBUG
227
qDebug() << " -> updateClipPath(): op:" << op
228
<< "bounding rect:" << path.boundingRect();
233
SERIALIZE_CMD(QPicturePrivate::PdcSetClipPath);
234
d->s << path << (qint8) op;
235
writeCmdLength(pos, QRectF(), false);
238
void QPicturePaintEngine::updateRenderHints(QPainter::RenderHints hints)
240
Q_D(QPicturePaintEngine);
241
#ifdef QT_PICTURE_DEBUG
242
qDebug() << " -> updateRenderHints(): " << hints;
245
SERIALIZE_CMD(QPicturePrivate::PdcSetRenderHint);
246
d->s << (quint32) hints;
247
writeCmdLength(pos, QRect(), false);
250
void QPicturePaintEngine::writeCmdLength(int pos, const QRectF &r, bool corr)
252
Q_D(QPicturePaintEngine);
253
int newpos = d->pic_d->pictb.pos(); // new position
254
int length = newpos - pos;
255
QRect br = r.toRect();
257
if (length < 255) { // write 8-bit length
258
d->pic_d->pictb.seek(pos - 1); // position to right index
259
d->s << (quint8)length;
260
} else { // write 32-bit length
261
d->s << (quint32)0; // extend the buffer
262
d->pic_d->pictb.seek(pos - 1); // position to right index
263
d->s << (quint8)255; // indicate 32-bit length
264
char *p = d->pic_d->pictb.buffer().data();
265
memmove(p+pos+4, p+pos, length); // make room for 4 byte
266
d->s << (quint32)length;
269
d->pic_d->pictb.seek(newpos); // set to new position
272
if (corr) { // widen bounding rect
273
int w2 = painter()->pen().width() / 2;
274
br.setCoords(br.left() - w2, br.top() - w2,
275
br.right() + w2, br.bottom() + w2);
277
#ifndef QT_NO_TRANSFORMATIONS
278
br = painter()->matrix().mapRect(br);
280
if (painter()->hasClipping()) {
281
QRect cr = painter()->clipRegion().boundingRect();
285
d->pic_d->brect |= br; // merge with existing rect
289
void QPicturePaintEngine::drawPath(const QPainterPath &path)
291
Q_D(QPicturePaintEngine);
292
#ifdef QT_PICTURE_DEBUG
293
qDebug() << " -> drawPath():" << path.boundingRect();
296
SERIALIZE_CMD(QPicturePrivate::PdcDrawPath);
298
writeCmdLength(pos, path.boundingRect(), true);
301
void QPicturePaintEngine::drawPolygon(const QPointF *points, int numPoints, PolygonDrawMode mode)
303
Q_D(QPicturePaintEngine);
304
#ifdef QT_PICTURE_DEBUG
305
qDebug() << " -> drawPolygon(): size=" << numPoints;
310
for (int i=0; i<numPoints; ++i)
311
polygon << points[i];
313
if (mode == PolylineMode) {
314
SERIALIZE_CMD(QPicturePrivate::PdcDrawPolyline);
317
SERIALIZE_CMD(QPicturePrivate::PdcDrawPolygon);
319
d->s << (qint8)(mode == OddEvenMode ? 0 : 1);
322
writeCmdLength(pos, polygon.boundingRect(), true);
325
void QPicturePaintEngine::drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr)
327
Q_D(QPicturePaintEngine);
328
#ifdef QT_PICTURE_DEBUG
329
qDebug() << " -> drawPixmap():" << r;
332
SERIALIZE_CMD(QPicturePrivate::PdcDrawPixmap);
333
d->s << r << pm << sr;
334
writeCmdLength(pos, r, false);
337
void QPicturePaintEngine::drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPointF &s)
339
Q_D(QPicturePaintEngine);
340
#ifdef QT_PICTURE_DEBUG
341
qDebug() << " -> drawTiledPixmap():" << r << s;
344
SERIALIZE_CMD(QPicturePrivate::PdcDrawTiledPixmap);
345
d->s << r << pixmap << s;
346
writeCmdLength(pos, r, false);
349
void QPicturePaintEngine::drawTextItem(const QPointF &p , const QTextItem &ti)
351
Q_D(QPicturePaintEngine);
352
#ifdef QT_PICTURE_DEBUG
353
qDebug() << " -> drawTextItem():" << p << QString(ti.chars, ti.num_chars);
356
SERIALIZE_CMD(QPicturePrivate::PdcDrawText2);
357
d->s << p << ti.text();
358
writeCmdLength(pos, QRectF(p, QSizeF(1,1)), true);
361
void QPicturePaintEngine::updateState(const QPaintEngineState &state)
363
QPaintEngine::DirtyFlags flags = state.state();
364
if (flags & DirtyPen) updatePen(state.pen());
365
if (flags & DirtyBrush) updateBrush(state.brush(), state.brushOrigin());
366
if (flags & DirtyBackground) updateBackground(state.backgroundMode(), state.backgroundBrush());
367
if (flags & DirtyFont) updateFont(state.font());
368
if (flags & DirtyTransform) updateMatrix(state.matrix());
369
if (flags & DirtyClipPath) updateClipPath(state.clipPath(), state.clipOperation());
370
if (flags & DirtyClipRegion) updateClipRegion(state.clipRegion(), state.clipOperation());
371
if (flags & DirtyHints) updateRenderHints(state.renderHints());