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 <qtprintsupportglobal.h> |
5 | |
6 | #ifndef QT_NO_PRINTER |
7 | #include <qdebug.h> |
8 | #include "private/qpaintengine_alpha_p.h" |
9 | |
10 | #include "private/qpainter_p.h" |
11 | #include "private/qpicture_p.h" |
12 | #include "private/qfont_p.h" |
13 | #include "QtGui/qpicture.h" |
14 | |
15 | QT_BEGIN_NAMESPACE |
16 | |
17 | QAlphaPaintEngine::QAlphaPaintEngine(QAlphaPaintEnginePrivate &data, PaintEngineFeatures devcaps) |
18 | : QPaintEngine(data, devcaps) |
19 | { |
20 | |
21 | } |
22 | |
23 | QAlphaPaintEngine::~QAlphaPaintEngine() |
24 | { |
25 | |
26 | } |
27 | |
28 | bool QAlphaPaintEngine::begin(QPaintDevice *pdev) |
29 | { |
30 | Q_D(QAlphaPaintEngine); |
31 | |
32 | d->m_continueCall = true; |
33 | if (d->m_pass != 0) { |
34 | return true; |
35 | } |
36 | |
37 | d->m_savedcaps = gccaps; |
38 | d->m_pdev = pdev; |
39 | |
40 | d->m_alphaPen = false; |
41 | d->m_alphaBrush = false; |
42 | d->m_alphaOpacity = false; |
43 | d->m_hasalpha = false; |
44 | d->m_advancedPen = false; |
45 | d->m_advancedBrush = false; |
46 | d->m_complexTransform = false; |
47 | d->m_emulateProjectiveTransforms = false; |
48 | |
49 | // clear alpha region |
50 | d->m_alphargn = QRegion(); |
51 | d->m_cliprgn = QRegion(); |
52 | d->m_pen = QPen(); |
53 | d->m_transform = QTransform(); |
54 | |
55 | flushAndInit(); |
56 | |
57 | return true; |
58 | } |
59 | |
60 | bool QAlphaPaintEngine::end() |
61 | { |
62 | Q_D(QAlphaPaintEngine); |
63 | |
64 | d->m_continueCall = true; |
65 | if (d->m_pass != 0) { |
66 | return true; |
67 | } |
68 | |
69 | flushAndInit(init: false); |
70 | return true; |
71 | } |
72 | |
73 | void QAlphaPaintEngine::updateState(const QPaintEngineState &state) |
74 | { |
75 | Q_D(QAlphaPaintEngine); |
76 | |
77 | DirtyFlags flags = state.state(); |
78 | if (flags & QPaintEngine::DirtyTransform) { |
79 | d->m_transform = state.transform(); |
80 | d->m_complexTransform = (d->m_transform.type() > QTransform::TxScale); |
81 | d->m_emulateProjectiveTransforms = !(d->m_savedcaps & QPaintEngine::PerspectiveTransform) |
82 | && !(d->m_savedcaps & QPaintEngine::AlphaBlend) |
83 | && (d->m_transform.type() >= QTransform::TxProject); |
84 | } |
85 | if (flags & QPaintEngine::DirtyPen) { |
86 | d->m_pen = state.pen(); |
87 | if (d->m_pen.style() == Qt::NoPen) { |
88 | d->m_advancedPen = false; |
89 | d->m_alphaPen = false; |
90 | } else { |
91 | d->m_advancedPen = (d->m_pen.brush().style() != Qt::SolidPattern); |
92 | d->m_alphaPen = !d->m_pen.brush().isOpaque(); |
93 | } |
94 | } |
95 | |
96 | if (d->m_pass != 0) { |
97 | d->m_continueCall = true; |
98 | return; |
99 | } |
100 | d->m_continueCall = false; |
101 | |
102 | if (flags & QPaintEngine::DirtyOpacity) { |
103 | d->m_alphaOpacity = (state.opacity() != 1.0f); |
104 | } |
105 | |
106 | if (flags & QPaintEngine::DirtyBrush) { |
107 | if (state.brush().style() == Qt::NoBrush) { |
108 | d->m_advancedBrush = false; |
109 | d->m_alphaBrush = false; |
110 | } else { |
111 | d->m_advancedBrush = (state.brush().style() != Qt::SolidPattern); |
112 | d->m_alphaBrush = !state.brush().isOpaque(); |
113 | } |
114 | } |
115 | |
116 | |
117 | d->m_hasalpha = d->m_alphaOpacity || d->m_alphaBrush || d->m_alphaPen; |
118 | |
119 | if (d->m_picengine) { |
120 | const QPainter *p = painter(); |
121 | d->m_picpainter->setPen(p->pen()); |
122 | d->m_picpainter->setBrush(p->brush()); |
123 | d->m_picpainter->setBrushOrigin(p->brushOrigin()); |
124 | d->m_picpainter->setFont(p->font()); |
125 | d->m_picpainter->setOpacity(p->opacity()); |
126 | d->m_picpainter->setTransform(transform: p->combinedTransform()); |
127 | d->m_picengine->updateState(state); |
128 | } |
129 | } |
130 | |
131 | void QAlphaPaintEngine::drawPath(const QPainterPath &path) |
132 | { |
133 | Q_D(QAlphaPaintEngine); |
134 | |
135 | QRectF tr = d->addPenWidth(path); |
136 | |
137 | if (d->m_pass == 0) { |
138 | d->m_continueCall = false; |
139 | if (d->canSeeTroughBackground(somethingInRectHasAlpha: d->m_hasalpha, rect: tr) || d->m_advancedPen || d->m_advancedBrush |
140 | || d->m_emulateProjectiveTransforms) |
141 | { |
142 | d->addAlphaRect(rect: tr); |
143 | } |
144 | |
145 | d->addDirtyRect(rect: tr); |
146 | |
147 | if (d->m_picengine) |
148 | d->m_picengine->drawPath(path); |
149 | } else { |
150 | d->m_continueCall = !d->fullyContained(rect: tr); |
151 | } |
152 | } |
153 | |
154 | void QAlphaPaintEngine::drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode) |
155 | { |
156 | Q_D(QAlphaPaintEngine); |
157 | |
158 | QPolygonF poly; |
159 | poly.reserve(asize: pointCount); |
160 | for (int i = 0; i < pointCount; ++i) |
161 | poly.append(t: points[i]); |
162 | |
163 | QPainterPath path; |
164 | path.addPolygon(polygon: poly); |
165 | QRectF tr = d->addPenWidth(path); |
166 | |
167 | if (d->m_pass == 0) { |
168 | d->m_continueCall = false; |
169 | if (d->canSeeTroughBackground(somethingInRectHasAlpha: d->m_hasalpha, rect: tr) || d->m_advancedPen || d->m_advancedBrush |
170 | || d->m_emulateProjectiveTransforms) |
171 | { |
172 | d->addAlphaRect(rect: tr); |
173 | } |
174 | |
175 | d->addDirtyRect(rect: tr); |
176 | |
177 | if (d->m_picengine) |
178 | d->m_picengine->drawPolygon(points, pointCount, mode); |
179 | } else { |
180 | d->m_continueCall = !d->fullyContained(rect: tr); |
181 | } |
182 | } |
183 | |
184 | void QAlphaPaintEngine::drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr) |
185 | { |
186 | Q_D(QAlphaPaintEngine); |
187 | |
188 | QRectF tr = d->m_transform.mapRect(r); |
189 | if (d->m_pass == 0) { |
190 | d->m_continueCall = false; |
191 | if (d->canSeeTroughBackground(somethingInRectHasAlpha: pm.hasAlpha() || d->m_alphaOpacity, rect: tr) || d->m_complexTransform || pm.isQBitmap()) { |
192 | d->addAlphaRect(rect: tr); |
193 | } |
194 | |
195 | d->addDirtyRect(rect: tr); |
196 | |
197 | if (d->m_picengine) |
198 | d->m_picengine->drawPixmap(r, pm, sr); |
199 | |
200 | } else { |
201 | d->m_continueCall = !d->fullyContained(rect: tr); |
202 | } |
203 | } |
204 | |
205 | void QAlphaPaintEngine::drawTextItem(const QPointF &p, const QTextItem &textItem) |
206 | { |
207 | Q_D(QAlphaPaintEngine); |
208 | |
209 | QRectF tr(p.x(), p.y() - textItem.ascent(), textItem.width() + 5, textItem.ascent() + textItem.descent() + 5); |
210 | tr = d->m_transform.mapRect(tr); |
211 | |
212 | if (d->m_pass == 0) { |
213 | d->m_continueCall = false; |
214 | if (d->canSeeTroughBackground(somethingInRectHasAlpha: d->m_alphaPen || d->m_alphaOpacity, rect: tr) || d->m_advancedPen) { |
215 | d->addAlphaRect(rect: tr); |
216 | } |
217 | |
218 | d->addDirtyRect(rect: tr); |
219 | |
220 | if (d->m_picengine) { |
221 | d->m_picengine->drawTextItem(p, textItem); |
222 | } |
223 | } else { |
224 | d->m_continueCall = !d->fullyContained(rect: tr); |
225 | } |
226 | } |
227 | |
228 | void QAlphaPaintEngine::drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPointF &s) |
229 | { |
230 | Q_D(QAlphaPaintEngine); |
231 | |
232 | QRectF brect = d->m_transform.mapRect(r); |
233 | |
234 | if (d->m_pass == 0) { |
235 | d->m_continueCall = false; |
236 | if (d->canSeeTroughBackground(somethingInRectHasAlpha: pixmap.hasAlpha() || d->m_alphaOpacity, rect: brect) || d->m_complexTransform || pixmap.isQBitmap()) { |
237 | d->addAlphaRect(rect: brect); |
238 | } |
239 | |
240 | d->addDirtyRect(rect: brect); |
241 | |
242 | if (d->m_picengine) |
243 | d->m_picengine->drawTiledPixmap(r, pixmap, s); |
244 | } else { |
245 | d->m_continueCall = !d->fullyContained(rect: brect); |
246 | } |
247 | } |
248 | |
249 | QRegion QAlphaPaintEngine::alphaClipping() const |
250 | { |
251 | Q_D(const QAlphaPaintEngine); |
252 | return d->m_cliprgn; |
253 | } |
254 | |
255 | bool QAlphaPaintEngine::continueCall() const |
256 | { |
257 | Q_D(const QAlphaPaintEngine); |
258 | return d->m_continueCall; |
259 | } |
260 | |
261 | void QAlphaPaintEngine::flushAndInit(bool init) |
262 | { |
263 | Q_D(QAlphaPaintEngine); |
264 | Q_ASSERT(d->m_pass == 0); |
265 | |
266 | if (d->m_pic) { |
267 | d->m_picpainter->end(); |
268 | |
269 | // set clip region |
270 | d->m_alphargn = d->m_alphargn.intersected(r: QRect(0, 0, d->m_pdev->width(), d->m_pdev->height())); |
271 | |
272 | // just use the bounding rect if it's a complex region.. |
273 | if (d->m_alphargn.rectCount() > 10) { |
274 | QRect br = d->m_alphargn.boundingRect(); |
275 | d->m_alphargn = QRegion(br); |
276 | } |
277 | |
278 | const auto oldAlphaRegion = d->m_cliprgn = d->m_alphargn; |
279 | |
280 | // now replay the QPicture |
281 | ++d->m_pass; // we are now doing pass #2 |
282 | |
283 | // reset states |
284 | gccaps = d->m_savedcaps; |
285 | |
286 | painter()->save(); |
287 | d->resetState(p: painter()); |
288 | |
289 | // make sure the output from QPicture is unscaled |
290 | QTransform mtx; |
291 | mtx.scale(sx: 1.0f / (qreal(d->m_pdev->logicalDpiX()) / qreal(qt_defaultDpiX())), |
292 | sy: 1.0f / (qreal(d->m_pdev->logicalDpiY()) / qreal(qt_defaultDpiY()))); |
293 | painter()->setTransform(transform: mtx); |
294 | painter()->drawPicture(x: 0, y: 0, p: *d->m_pic); |
295 | |
296 | d->m_cliprgn = QRegion(); |
297 | d->resetState(p: painter()); |
298 | |
299 | // fill in the alpha images |
300 | for (const auto &rect : oldAlphaRegion) |
301 | d->drawAlphaImage(rect); |
302 | |
303 | d->m_alphargn = QRegion(); |
304 | |
305 | painter()->restore(); |
306 | |
307 | --d->m_pass; // pass #2 finished |
308 | |
309 | cleanUp(); |
310 | } |
311 | |
312 | if (init) { |
313 | gccaps = PaintEngineFeatures(AllFeatures & ~QPaintEngine::ObjectBoundingModeGradients); |
314 | |
315 | d->m_pic = new QPicture(); |
316 | d->m_pic->d_ptr->in_memory_only = true; |
317 | d->m_picpainter = new QPainter(d->m_pic); |
318 | d->m_picengine = d->m_picpainter->paintEngine(); |
319 | |
320 | // When newPage() is called and the m_picpainter is recreated |
321 | // we have to copy the current state of the original printer |
322 | // painter back to the m_picpainter |
323 | d->m_picpainter->setPen(painter()->pen()); |
324 | d->m_picpainter->setBrush(painter()->brush()); |
325 | d->m_picpainter->setBrushOrigin(painter()->brushOrigin()); |
326 | d->m_picpainter->setFont(painter()->font()); |
327 | d->m_picpainter->setOpacity(painter()->opacity()); |
328 | d->m_picpainter->setTransform(transform: painter()->combinedTransform()); |
329 | d->m_picengine->syncState(); |
330 | QPainterState &state = *d->m_picpainter->d_func()->state; |
331 | QPainter *oldPainter = state.painter; |
332 | state = *painter()->d_func()->state; |
333 | state.painter = oldPainter; |
334 | } |
335 | } |
336 | |
337 | void QAlphaPaintEngine::cleanUp() |
338 | { |
339 | Q_D(QAlphaPaintEngine); |
340 | |
341 | delete d->m_picpainter; |
342 | delete d->m_pic; |
343 | |
344 | d->m_picpainter = nullptr; |
345 | d->m_pic = nullptr; |
346 | d->m_picengine = nullptr; |
347 | } |
348 | |
349 | QAlphaPaintEnginePrivate::QAlphaPaintEnginePrivate() |
350 | : m_pass(0), |
351 | m_pic(nullptr), |
352 | m_picengine(nullptr), |
353 | m_picpainter(nullptr), |
354 | m_numberOfCachedRects(0), |
355 | m_hasalpha(false), |
356 | m_alphaPen(false), |
357 | m_alphaBrush(false), |
358 | m_alphaOpacity(false), |
359 | m_advancedPen(false), |
360 | m_advancedBrush(false), |
361 | m_complexTransform(false) |
362 | { |
363 | |
364 | } |
365 | |
366 | QAlphaPaintEnginePrivate::~QAlphaPaintEnginePrivate() |
367 | { |
368 | delete m_picpainter; |
369 | delete m_pic; |
370 | } |
371 | |
372 | QRectF QAlphaPaintEnginePrivate::addPenWidth(const QPainterPath &path) |
373 | { |
374 | QPainterPath tmp = path; |
375 | |
376 | if (m_pen.style() == Qt::NoPen) |
377 | return (path.controlPointRect() * m_transform).boundingRect(); |
378 | bool cosmetic = m_pen.isCosmetic(); |
379 | if (cosmetic) |
380 | tmp = path * m_transform; |
381 | |
382 | QPainterPathStroker stroker; |
383 | if (m_pen.widthF() == 0.0f) |
384 | stroker.setWidth(1.0); |
385 | else |
386 | stroker.setWidth(m_pen.widthF()); |
387 | stroker.setJoinStyle(m_pen.joinStyle()); |
388 | stroker.setCapStyle(m_pen.capStyle()); |
389 | tmp = stroker.createStroke(path: tmp); |
390 | if (cosmetic) |
391 | return tmp.controlPointRect(); |
392 | |
393 | return (tmp.controlPointRect() * m_transform).boundingRect(); |
394 | } |
395 | |
396 | void QAlphaPaintEnginePrivate::addAlphaRect(const QRectF &rect) |
397 | { |
398 | m_alphargn |= rect.toAlignedRect(); |
399 | } |
400 | |
401 | bool QAlphaPaintEnginePrivate::canSeeTroughBackground(bool somethingInRectHasAlpha, const QRectF &rect) const |
402 | { |
403 | if (somethingInRectHasAlpha) { |
404 | if (m_dirtyRects.size() != m_numberOfCachedRects) { |
405 | m_cachedDirtyRgn.setRects(rect: m_dirtyRects.constData(), num: m_dirtyRects.size()); |
406 | m_numberOfCachedRects = m_dirtyRects.size(); |
407 | } |
408 | return m_cachedDirtyRgn.intersects(r: rect.toAlignedRect()); |
409 | } |
410 | return false; |
411 | } |
412 | |
413 | void QAlphaPaintEnginePrivate::drawAlphaImage(const QRectF &rect) |
414 | { |
415 | Q_Q(QAlphaPaintEngine); |
416 | |
417 | qreal dpiX = qMax(a: m_pdev->logicalDpiX(), b: 300); |
418 | qreal dpiY = qMax(a: m_pdev->logicalDpiY(), b: 300); |
419 | qreal xscale = (dpiX / m_pdev->logicalDpiX()); |
420 | qreal yscale = (dpiY / m_pdev->logicalDpiY()); |
421 | |
422 | QTransform picscale; |
423 | picscale.scale(sx: xscale, sy: yscale); |
424 | |
425 | const int tileSize = 2048; |
426 | QSize size((int(rect.width() * xscale)), int(rect.height() * yscale)); |
427 | int divw = (size.width() / tileSize); |
428 | int divh = (size.height() / tileSize); |
429 | divw += 1; |
430 | divh += 1; |
431 | |
432 | int incx = int(rect.width() / divw); |
433 | int incy = int(rect.height() / divh); |
434 | |
435 | for (int y=0; y<divh; ++y) { |
436 | int ypos = int((incy * y) + rect.y()); |
437 | int height = int((y == (divh - 1)) ? (rect.height() - (incy * y)) : incy) + 1; |
438 | |
439 | for (int x=0; x<divw; ++x) { |
440 | int xpos = int((incx * x) + rect.x()); |
441 | int width = int((x == (divw - 1)) ? (rect.width() - (incx * x)) : incx) + 1; |
442 | |
443 | QSize imgsize((int)(width * xscale), (int)(height * yscale)); |
444 | QImage img(imgsize, QImage::Format_RGB32); |
445 | img.fill(pixel: 0xffffffff); |
446 | |
447 | QPainter imgpainter(&img); |
448 | imgpainter.setTransform(transform: picscale); |
449 | QPointF picpos(qreal(-xpos), qreal(-ypos)); |
450 | imgpainter.drawPicture(p: picpos, picture: *m_pic); |
451 | imgpainter.end(); |
452 | |
453 | q->painter()->setTransform(transform: QTransform()); |
454 | QRect r(xpos, ypos, width, height); |
455 | q->painter()->drawImage(r, image: img); |
456 | } |
457 | } |
458 | } |
459 | |
460 | bool QAlphaPaintEnginePrivate::fullyContained(const QRectF &rect) const |
461 | { |
462 | QRegion r(rect.toAlignedRect()); |
463 | return (m_cliprgn.intersected(r) == r); |
464 | } |
465 | |
466 | void QAlphaPaintEnginePrivate::resetState(QPainter *p) |
467 | { |
468 | p->setPen(QPen()); |
469 | p->setBrush(QBrush()); |
470 | p->setBrushOrigin(x: 0,y: 0); |
471 | p->setBackground(QBrush()); |
472 | p->setFont(QFont()); |
473 | p->setTransform(transform: QTransform()); |
474 | // The view transform is already recorded and included in the |
475 | // picture we're about to replay. If we don't turn if off, |
476 | // the view matrix will be applied twice. |
477 | p->setViewTransformEnabled(false); |
478 | p->setClipRegion(QRegion(), op: Qt::NoClip); |
479 | p->setClipPath(path: QPainterPath(), op: Qt::NoClip); |
480 | p->setClipping(false); |
481 | p->setOpacity(1.0f); |
482 | } |
483 | |
484 | |
485 | QT_END_NAMESPACE |
486 | |
487 | #endif // QT_NO_PRINTER |
488 | |