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 QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
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 Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qpaintengineex_p.h"
41#include "qpainter_p.h"
42#include "qstroker_p.h"
43#include "qbezier_p.h"
44#include <private/qpainterpath_p.h>
45#include <private/qfontengine_p.h>
46#include <private/qstatictext_p.h>
47
48#include <qvarlengtharray.h>
49#include <qdebug.h>
50
51
52QT_BEGIN_NAMESPACE
53
54#if !defined(QT_MAX_CACHED_GLYPH_SIZE)
55# define QT_MAX_CACHED_GLYPH_SIZE 64
56#endif
57
58/*******************************************************************************
59 *
60 * class QVectorPath
61 *
62 */
63QVectorPath::~QVectorPath()
64{
65 if (m_hints & ShouldUseCacheHint) {
66 CacheEntry *e = m_cache;
67 while (e) {
68 if (e->data)
69 e->cleanup(e->engine, e->data);
70 CacheEntry *n = e->next;
71 delete e;
72 e = n;
73 }
74 }
75}
76
77
78QRectF QVectorPath::controlPointRect() const
79{
80 if (m_hints & ControlPointRect)
81 return QRectF(QPointF(m_cp_rect.x1, m_cp_rect.y1), QPointF(m_cp_rect.x2, m_cp_rect.y2));
82
83 if (m_count == 0) {
84 m_cp_rect.x1 = m_cp_rect.x2 = m_cp_rect.y1 = m_cp_rect.y2 = 0;
85 m_hints |= ControlPointRect;
86 return QRectF(QPointF(m_cp_rect.x1, m_cp_rect.y1), QPointF(m_cp_rect.x2, m_cp_rect.y2));
87 }
88 Q_ASSERT(m_points && m_count > 0);
89
90 const qreal *pts = m_points;
91 m_cp_rect.x1 = m_cp_rect.x2 = *pts;
92 ++pts;
93 m_cp_rect.y1 = m_cp_rect.y2 = *pts;
94 ++pts;
95
96 const qreal *epts = m_points + (m_count << 1);
97 while (pts < epts) {
98 qreal x = *pts;
99 if (x < m_cp_rect.x1) m_cp_rect.x1 = x;
100 else if (x > m_cp_rect.x2) m_cp_rect.x2 = x;
101 ++pts;
102
103 qreal y = *pts;
104 if (y < m_cp_rect.y1) m_cp_rect.y1 = y;
105 else if (y > m_cp_rect.y2) m_cp_rect.y2 = y;
106 ++pts;
107 }
108
109 m_hints |= ControlPointRect;
110 return QRectF(QPointF(m_cp_rect.x1, m_cp_rect.y1), QPointF(m_cp_rect.x2, m_cp_rect.y2));
111}
112
113
114QVectorPath::CacheEntry *QVectorPath::addCacheData(QPaintEngineEx *engine, void *data,
115 qvectorpath_cache_cleanup cleanup) const{
116 Q_ASSERT(!lookupCacheData(engine));
117 if ((m_hints & IsCachedHint) == 0) {
118 m_cache = 0;
119 m_hints |= IsCachedHint;
120 }
121 CacheEntry *e = new CacheEntry;
122 e->engine = engine;
123 e->data = data;
124 e->cleanup = cleanup;
125 e->next = m_cache;
126 m_cache = e;
127 return m_cache;
128}
129
130
131const QVectorPath &qtVectorPathForPath(const QPainterPath &path)
132{
133 Q_ASSERT(path.d_func());
134 return path.d_func()->vectorPath();
135}
136
137#ifndef QT_NO_DEBUG_STREAM
138QDebug Q_GUI_EXPORT &operator<<(QDebug &s, const QVectorPath &path)
139{
140 QDebugStateSaver saver(s);
141 QRectF rf = path.controlPointRect();
142 s << "QVectorPath(size:" << path.elementCount()
143 << " hints:" << Qt::hex << path.hints()
144 << rf << ')';
145 return s;
146}
147#endif
148
149/*******************************************************************************
150 *
151 * class QPaintEngineExPrivate:
152 *
153 */
154
155
156struct StrokeHandler {
157 StrokeHandler(int reserve) : pts(reserve), types(reserve) {}
158 QDataBuffer<qreal> pts;
159 QDataBuffer<QPainterPath::ElementType> types;
160};
161
162
163QPaintEngineExPrivate::QPaintEngineExPrivate()
164 : dasher(&stroker),
165 strokeHandler(0),
166 activeStroker(0),
167 strokerPen(Qt::NoPen)
168{
169}
170
171
172QPaintEngineExPrivate::~QPaintEngineExPrivate()
173{
174 delete strokeHandler;
175}
176
177
178void QPaintEngineExPrivate::replayClipOperations()
179{
180 Q_Q(QPaintEngineEx);
181
182 QPainter *p = q->painter();
183 if (!p || !p->d_ptr)
184 return;
185
186 const QVector<QPainterClipInfo> &clipInfo = p->d_ptr->state->clipInfo;
187
188 QTransform transform = q->state()->matrix;
189
190 for (const QPainterClipInfo &info : clipInfo) {
191
192 if (info.matrix != q->state()->matrix) {
193 q->state()->matrix = info.matrix;
194 q->transformChanged();
195 }
196
197 switch (info.clipType) {
198 case QPainterClipInfo::RegionClip:
199 q->clip(info.region, info.operation);
200 break;
201 case QPainterClipInfo::PathClip:
202 q->clip(info.path, info.operation);
203 break;
204 case QPainterClipInfo::RectClip:
205 q->clip(info.rect, info.operation);
206 break;
207 case QPainterClipInfo::RectFClip: {
208 qreal right = info.rectf.x() + info.rectf.width();
209 qreal bottom = info.rectf.y() + info.rectf.height();
210 qreal pts[] = { info.rectf.x(), info.rectf.y(),
211 right, info.rectf.y(),
212 right, bottom,
213 info.rectf.x(), bottom };
214 QVectorPath vp(pts, 4, 0, QVectorPath::RectangleHint);
215 q->clip(vp, info.operation);
216 break;
217 }
218 }
219 }
220
221 if (transform != q->state()->matrix) {
222 q->state()->matrix = transform;
223 q->transformChanged();
224 }
225}
226
227
228bool QPaintEngineExPrivate::hasClipOperations() const
229{
230 Q_Q(const QPaintEngineEx);
231
232 QPainter *p = q->painter();
233 if (!p || !p->d_ptr)
234 return false;
235
236 return !p->d_ptr->state->clipInfo.isEmpty();
237}
238
239/*******************************************************************************
240 *
241 * class QPaintEngineEx:
242 *
243 */
244
245static const QPainterPath::ElementType qpaintengineex_ellipse_types[] = {
246 QPainterPath::MoveToElement,
247 QPainterPath::CurveToElement,
248 QPainterPath::CurveToDataElement,
249 QPainterPath::CurveToDataElement,
250
251 QPainterPath::CurveToElement,
252 QPainterPath::CurveToDataElement,
253 QPainterPath::CurveToDataElement,
254
255 QPainterPath::CurveToElement,
256 QPainterPath::CurveToDataElement,
257 QPainterPath::CurveToDataElement,
258
259 QPainterPath::CurveToElement,
260 QPainterPath::CurveToDataElement,
261 QPainterPath::CurveToDataElement
262};
263
264static const QPainterPath::ElementType qpaintengineex_line_types_16[] = {
265 QPainterPath::MoveToElement, QPainterPath::LineToElement,
266 QPainterPath::MoveToElement, QPainterPath::LineToElement,
267 QPainterPath::MoveToElement, QPainterPath::LineToElement,
268 QPainterPath::MoveToElement, QPainterPath::LineToElement,
269 QPainterPath::MoveToElement, QPainterPath::LineToElement,
270 QPainterPath::MoveToElement, QPainterPath::LineToElement,
271 QPainterPath::MoveToElement, QPainterPath::LineToElement,
272 QPainterPath::MoveToElement, QPainterPath::LineToElement,
273 QPainterPath::MoveToElement, QPainterPath::LineToElement,
274 QPainterPath::MoveToElement, QPainterPath::LineToElement,
275 QPainterPath::MoveToElement, QPainterPath::LineToElement,
276 QPainterPath::MoveToElement, QPainterPath::LineToElement,
277 QPainterPath::MoveToElement, QPainterPath::LineToElement,
278 QPainterPath::MoveToElement, QPainterPath::LineToElement,
279 QPainterPath::MoveToElement, QPainterPath::LineToElement,
280 QPainterPath::MoveToElement, QPainterPath::LineToElement
281};
282
283static const QPainterPath::ElementType qpaintengineex_rect4_types_32[] = {
284 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 1
285 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 2
286 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 3
287 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 4
288 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 5
289 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 6
290 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 7
291 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 8
292 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 9
293 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 10
294 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 11
295 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 12
296 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 13
297 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 14
298 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 15
299 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 16
300 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 17
301 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 18
302 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 19
303 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 20
304 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 21
305 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 22
306 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 23
307 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 24
308 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 25
309 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 26
310 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 27
311 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 28
312 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 29
313 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 30
314 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 31
315 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 32
316};
317
318
319static const QPainterPath::ElementType qpaintengineex_roundedrect_types[] = {
320 QPainterPath::MoveToElement,
321 QPainterPath::LineToElement,
322 QPainterPath::CurveToElement,
323 QPainterPath::CurveToDataElement,
324 QPainterPath::CurveToDataElement,
325 QPainterPath::LineToElement,
326 QPainterPath::CurveToElement,
327 QPainterPath::CurveToDataElement,
328 QPainterPath::CurveToDataElement,
329 QPainterPath::LineToElement,
330 QPainterPath::CurveToElement,
331 QPainterPath::CurveToDataElement,
332 QPainterPath::CurveToDataElement,
333 QPainterPath::LineToElement,
334 QPainterPath::CurveToElement,
335 QPainterPath::CurveToDataElement,
336 QPainterPath::CurveToDataElement
337};
338
339
340
341static void qpaintengineex_moveTo(qreal x, qreal y, void *data) {
342 ((StrokeHandler *) data)->pts.add(x);
343 ((StrokeHandler *) data)->pts.add(y);
344 ((StrokeHandler *) data)->types.add(QPainterPath::MoveToElement);
345}
346
347static void qpaintengineex_lineTo(qreal x, qreal y, void *data) {
348 ((StrokeHandler *) data)->pts.add(x);
349 ((StrokeHandler *) data)->pts.add(y);
350 ((StrokeHandler *) data)->types.add(QPainterPath::LineToElement);
351}
352
353static void qpaintengineex_cubicTo(qreal c1x, qreal c1y, qreal c2x, qreal c2y, qreal ex, qreal ey, void *data) {
354 ((StrokeHandler *) data)->pts.add(c1x);
355 ((StrokeHandler *) data)->pts.add(c1y);
356 ((StrokeHandler *) data)->types.add(QPainterPath::CurveToElement);
357
358 ((StrokeHandler *) data)->pts.add(c2x);
359 ((StrokeHandler *) data)->pts.add(c2y);
360 ((StrokeHandler *) data)->types.add(QPainterPath::CurveToDataElement);
361
362 ((StrokeHandler *) data)->pts.add(ex);
363 ((StrokeHandler *) data)->pts.add(ey);
364 ((StrokeHandler *) data)->types.add(QPainterPath::CurveToDataElement);
365}
366
367QPaintEngineEx::QPaintEngineEx()
368 : QPaintEngine(*new QPaintEngineExPrivate, AllFeatures)
369{
370 extended = true;
371}
372
373QPaintEngineEx::QPaintEngineEx(QPaintEngineExPrivate &data)
374 : QPaintEngine(data, AllFeatures)
375{
376 extended = true;
377}
378
379QPainterState *QPaintEngineEx::createState(QPainterState *orig) const
380{
381 if (!orig)
382 return new QPainterState;
383 return new QPainterState(orig);
384}
385
386Q_GUI_EXPORT extern bool qt_scaleForTransform(const QTransform &transform, qreal *scale); // qtransform.cpp
387
388void QPaintEngineEx::stroke(const QVectorPath &path, const QPen &pen)
389{
390#ifdef QT_DEBUG_DRAW
391 qDebug() << "QPaintEngineEx::stroke()" << pen;
392#endif
393
394 Q_D(QPaintEngineEx);
395
396 if (path.isEmpty())
397 return;
398
399 if (!d->strokeHandler) {
400 d->strokeHandler = new StrokeHandler(path.elementCount()+4);
401 d->stroker.setMoveToHook(qpaintengineex_moveTo);
402 d->stroker.setLineToHook(qpaintengineex_lineTo);
403 d->stroker.setCubicToHook(qpaintengineex_cubicTo);
404 }
405
406 if (!qpen_fast_equals(pen, d->strokerPen)) {
407 d->strokerPen = pen;
408 d->stroker.setJoinStyle(pen.joinStyle());
409 d->stroker.setCapStyle(pen.capStyle());
410 d->stroker.setMiterLimit(pen.miterLimit());
411 qreal penWidth = pen.widthF();
412 if (penWidth == 0)
413 d->stroker.setStrokeWidth(1);
414 else
415 d->stroker.setStrokeWidth(penWidth);
416
417 Qt::PenStyle style = pen.style();
418 if (style == Qt::SolidLine) {
419 d->activeStroker = &d->stroker;
420 } else if (style == Qt::NoPen) {
421 d->activeStroker = 0;
422 } else {
423 d->dasher.setDashPattern(pen.dashPattern());
424 d->dasher.setDashOffset(pen.dashOffset());
425 d->activeStroker = &d->dasher;
426 }
427 }
428
429 if (!d->activeStroker) {
430 return;
431 }
432
433 if (pen.style() > Qt::SolidLine) {
434 if (qt_pen_is_cosmetic(pen, state()->renderHints)){
435 d->activeStroker->setClipRect(d->exDeviceRect);
436 } else {
437 QRectF clipRect = state()->matrix.inverted().mapRect(QRectF(d->exDeviceRect));
438 d->activeStroker->setClipRect(clipRect);
439 }
440 }
441
442 if (d->activeStroker == &d->stroker)
443 d->stroker.setForceOpen(path.hasExplicitOpen());
444
445 const QPainterPath::ElementType *types = path.elements();
446 const qreal *points = path.points();
447 int pointCount = path.elementCount();
448
449 const qreal *lastPoint = points + (pointCount<<1);
450
451 d->strokeHandler->types.reset();
452 d->strokeHandler->pts.reset();
453
454 // Some engines might decide to optimize for the non-shape hint later on...
455 uint flags = QVectorPath::WindingFill;
456
457 if (path.elementCount() > 2)
458 flags |= QVectorPath::NonConvexShapeMask;
459
460 if (d->stroker.capStyle() == Qt::RoundCap || d->stroker.joinStyle() == Qt::RoundJoin)
461 flags |= QVectorPath::CurvedShapeMask;
462
463 // ### Perspective Xforms are currently not supported...
464 if (!qt_pen_is_cosmetic(pen, state()->renderHints)) {
465 // We include cosmetic pens in this case to avoid having to
466 // change the current transform. Normal transformed,
467 // non-cosmetic pens will be transformed as part of fill
468 // later, so they are also covered here..
469 d->activeStroker->setCurveThresholdFromTransform(state()->matrix);
470 d->activeStroker->begin(d->strokeHandler);
471 if (types) {
472 while (points < lastPoint) {
473 switch (*types) {
474 case QPainterPath::MoveToElement:
475 d->activeStroker->moveTo(points[0], points[1]);
476 points += 2;
477 ++types;
478 break;
479 case QPainterPath::LineToElement:
480 d->activeStroker->lineTo(points[0], points[1]);
481 points += 2;
482 ++types;
483 break;
484 case QPainterPath::CurveToElement:
485 d->activeStroker->cubicTo(points[0], points[1],
486 points[2], points[3],
487 points[4], points[5]);
488 points += 6;
489 types += 3;
490 flags |= QVectorPath::CurvedShapeMask;
491 break;
492 default:
493 break;
494 }
495 }
496 if (path.hasImplicitClose())
497 d->activeStroker->lineTo(path.points()[0], path.points()[1]);
498
499 } else {
500 d->activeStroker->moveTo(points[0], points[1]);
501 points += 2;
502 while (points < lastPoint) {
503 d->activeStroker->lineTo(points[0], points[1]);
504 points += 2;
505 }
506 if (path.hasImplicitClose())
507 d->activeStroker->lineTo(path.points()[0], path.points()[1]);
508 }
509 d->activeStroker->end();
510
511 if (!d->strokeHandler->types.size()) // an empty path...
512 return;
513
514 QVectorPath strokePath(d->strokeHandler->pts.data(),
515 d->strokeHandler->types.size(),
516 d->strokeHandler->types.data(),
517 flags);
518 fill(strokePath, pen.brush());
519 } else {
520 // For cosmetic pens we need a bit of trickery... We to process xform the input points
521 if (state()->matrix.type() >= QTransform::TxProject) {
522 QPainterPath painterPath = state()->matrix.map(path.convertToPainterPath());
523 d->activeStroker->strokePath(painterPath, d->strokeHandler, QTransform());
524 } else {
525 d->activeStroker->setCurveThresholdFromTransform(QTransform());
526 d->activeStroker->begin(d->strokeHandler);
527 if (types) {
528 while (points < lastPoint) {
529 switch (*types) {
530 case QPainterPath::MoveToElement: {
531 QPointF pt = (*(const QPointF *) points) * state()->matrix;
532 d->activeStroker->moveTo(pt.x(), pt.y());
533 points += 2;
534 ++types;
535 break;
536 }
537 case QPainterPath::LineToElement: {
538 QPointF pt = (*(const QPointF *) points) * state()->matrix;
539 d->activeStroker->lineTo(pt.x(), pt.y());
540 points += 2;
541 ++types;
542 break;
543 }
544 case QPainterPath::CurveToElement: {
545 QPointF c1 = ((const QPointF *) points)[0] * state()->matrix;
546 QPointF c2 = ((const QPointF *) points)[1] * state()->matrix;
547 QPointF e = ((const QPointF *) points)[2] * state()->matrix;
548 d->activeStroker->cubicTo(c1.x(), c1.y(), c2.x(), c2.y(), e.x(), e.y());
549 points += 6;
550 types += 3;
551 flags |= QVectorPath::CurvedShapeMask;
552 break;
553 }
554 default:
555 break;
556 }
557 }
558 if (path.hasImplicitClose()) {
559 QPointF pt = * ((const QPointF *) path.points()) * state()->matrix;
560 d->activeStroker->lineTo(pt.x(), pt.y());
561 }
562
563 } else {
564 QPointF p = ((const QPointF *)points)[0] * state()->matrix;
565 d->activeStroker->moveTo(p.x(), p.y());
566 points += 2;
567 while (points < lastPoint) {
568 QPointF p = ((const QPointF *)points)[0] * state()->matrix;
569 d->activeStroker->lineTo(p.x(), p.y());
570 points += 2;
571 }
572 if (path.hasImplicitClose())
573 d->activeStroker->lineTo(p.x(), p.y());
574 }
575 d->activeStroker->end();
576 }
577
578 QVectorPath strokePath(d->strokeHandler->pts.data(),
579 d->strokeHandler->types.size(),
580 d->strokeHandler->types.data(),
581 flags);
582
583 QTransform xform = state()->matrix;
584 state()->matrix = QTransform();
585 transformChanged();
586
587 QBrush brush = pen.brush();
588 if (qbrush_style(brush) != Qt::SolidPattern)
589 brush.setTransform(brush.transform() * xform);
590
591 fill(strokePath, brush);
592
593 state()->matrix = xform;
594 transformChanged();
595 }
596}
597
598void QPaintEngineEx::draw(const QVectorPath &path)
599{
600 const QBrush &brush = state()->brush;
601 if (qbrush_style(brush) != Qt::NoBrush)
602 fill(path, brush);
603
604 const QPen &pen = state()->pen;
605 if (qpen_style(pen) != Qt::NoPen && qbrush_style(qpen_brush(pen)) != Qt::NoBrush)
606 stroke(path, pen);
607}
608
609
610void QPaintEngineEx::clip(const QRect &r, Qt::ClipOperation op)
611{
612 qreal right = r.x() + r.width();
613 qreal bottom = r.y() + r.height();
614 qreal pts[] = { qreal(r.x()), qreal(r.y()),
615 right, qreal(r.y()),
616 right, bottom,
617 qreal(r.x()), bottom,
618 qreal(r.x()), qreal(r.y()) };
619 QVectorPath vp(pts, 5, 0, QVectorPath::RectangleHint);
620 clip(vp, op);
621}
622
623void QPaintEngineEx::clip(const QRegion &region, Qt::ClipOperation op)
624{
625 const auto rectsInRegion = region.rectCount();
626 if (rectsInRegion == 1) {
627 clip(*region.begin(), op);
628 } else if (rectsInRegion <= 32) {
629 qreal pts[2*32*4];
630 int pos = 0;
631 for (QRect r : region) {
632 qreal x1 = r.x();
633 qreal y1 = r.y();
634 qreal x2 = r.x() + r.width();
635 qreal y2 = r.y() + r.height();
636
637 pts[pos++] = x1;
638 pts[pos++] = y1;
639
640 pts[pos++] = x2;
641 pts[pos++] = y1;
642
643 pts[pos++] = x2;
644 pts[pos++] = y2;
645
646 pts[pos++] = x1;
647 pts[pos++] = y2;
648 }
649 QVectorPath vp(pts, rectsInRegion * 4, qpaintengineex_rect4_types_32);
650 clip(vp, op);
651 } else {
652 QVarLengthArray<qreal> pts(rectsInRegion * 2 * 4);
653 QVarLengthArray<QPainterPath::ElementType> types(rectsInRegion * 4);
654 int ppos = 0;
655 int tpos = 0;
656
657 for (QRect r : region) {
658 qreal x1 = r.x();
659 qreal y1 = r.y();
660 qreal x2 = r.x() + r.width();
661 qreal y2 = r.y() + r.height();
662
663 pts[ppos++] = x1;
664 pts[ppos++] = y1;
665
666 pts[ppos++] = x2;
667 pts[ppos++] = y1;
668
669 pts[ppos++] = x2;
670 pts[ppos++] = y2;
671
672 pts[ppos++] = x1;
673 pts[ppos++] = y2;
674
675 types[tpos++] = QPainterPath::MoveToElement;
676 types[tpos++] = QPainterPath::LineToElement;
677 types[tpos++] = QPainterPath::LineToElement;
678 types[tpos++] = QPainterPath::LineToElement;
679 }
680
681 QVectorPath vp(pts.data(), rectsInRegion * 4, types.data());
682 clip(vp, op);
683 }
684
685}
686
687void QPaintEngineEx::clip(const QPainterPath &path, Qt::ClipOperation op)
688{
689 if (path.isEmpty()) {
690 QVectorPath vp(0, 0);
691 clip(vp, op);
692 } else {
693 clip(qtVectorPathForPath(path), op);
694 }
695}
696
697void QPaintEngineEx::fillRect(const QRectF &r, const QBrush &brush)
698{
699 qreal pts[] = { r.x(), r.y(), r.x() + r.width(), r.y(),
700 r.x() + r.width(), r.y() + r.height(), r.x(), r.y() + r.height() };
701 QVectorPath vp(pts, 4, 0, QVectorPath::RectangleHint);
702 fill(vp, brush);
703}
704
705void QPaintEngineEx::fillRect(const QRectF &r, const QColor &color)
706{
707 fillRect(r, QBrush(color));
708}
709
710void QPaintEngineEx::drawRects(const QRect *rects, int rectCount)
711{
712 for (int i=0; i<rectCount; ++i) {
713 const QRect &r = rects[i];
714 // ### Is there a one off here?
715 qreal right = r.x() + r.width();
716 qreal bottom = r.y() + r.height();
717 qreal pts[] = { qreal(r.x()), qreal(r.y()),
718 right, qreal(r.y()),
719 right, bottom,
720 qreal(r.x()), bottom,
721 qreal(r.x()), qreal(r.y()) };
722 QVectorPath vp(pts, 5, 0, QVectorPath::RectangleHint);
723 draw(vp);
724 }
725}
726
727void QPaintEngineEx::drawRects(const QRectF *rects, int rectCount)
728{
729 for (int i=0; i<rectCount; ++i) {
730 const QRectF &r = rects[i];
731 qreal right = r.x() + r.width();
732 qreal bottom = r.y() + r.height();
733 qreal pts[] = { r.x(), r.y(),
734 right, r.y(),
735 right, bottom,
736 r.x(), bottom,
737 r.x(), r.y() };
738 QVectorPath vp(pts, 5, 0, QVectorPath::RectangleHint);
739 draw(vp);
740 }
741}
742
743
744void QPaintEngineEx::drawRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius,
745 Qt::SizeMode mode)
746{
747 qreal x1 = rect.left();
748 qreal x2 = rect.right();
749 qreal y1 = rect.top();
750 qreal y2 = rect.bottom();
751
752 if (mode == Qt::RelativeSize) {
753 xRadius = xRadius * rect.width() / 200.;
754 yRadius = yRadius * rect.height() / 200.;
755 }
756
757 xRadius = qMin(xRadius, rect.width() / 2);
758 yRadius = qMin(yRadius, rect.height() / 2);
759
760 qreal pts[] = {
761 x1 + xRadius, y1, // MoveTo
762 x2 - xRadius, y1, // LineTo
763 x2 - (1 - KAPPA) * xRadius, y1, // CurveTo
764 x2, y1 + (1 - KAPPA) * yRadius,
765 x2, y1 + yRadius,
766 x2, y2 - yRadius, // LineTo
767 x2, y2 - (1 - KAPPA) * yRadius, // CurveTo
768 x2 - (1 - KAPPA) * xRadius, y2,
769 x2 - xRadius, y2,
770 x1 + xRadius, y2, // LineTo
771 x1 + (1 - KAPPA) * xRadius, y2, // CurveTo
772 x1, y2 - (1 - KAPPA) * yRadius,
773 x1, y2 - yRadius,
774 x1, y1 + yRadius, // LineTo
775 x1, y1 + (1 - KAPPA) * yRadius, // CurveTo
776 x1 + (1 - KAPPA) * xRadius, y1,
777 x1 + xRadius, y1
778 };
779
780 QVectorPath path(pts, 17, qpaintengineex_roundedrect_types, QVectorPath::RoundedRectHint);
781 draw(path);
782}
783
784
785
786void QPaintEngineEx::drawLines(const QLine *lines, int lineCount)
787{
788 int elementCount = lineCount << 1;
789 while (elementCount > 0) {
790 int count = qMin(elementCount, 32);
791
792 qreal pts[64];
793 int count2 = count<<1;
794 for (int i=0; i<count2; ++i)
795 pts[i] = ((const int *) lines)[i];
796
797 QVectorPath path(pts, count, qpaintengineex_line_types_16, QVectorPath::LinesHint);
798 stroke(path, state()->pen);
799
800 elementCount -= 32;
801 lines += 16;
802 }
803}
804
805void QPaintEngineEx::drawLines(const QLineF *lines, int lineCount)
806{
807 int elementCount = lineCount << 1;
808 while (elementCount > 0) {
809 int count = qMin(elementCount, 32);
810
811 QVectorPath path((const qreal *) lines, count, qpaintengineex_line_types_16,
812 QVectorPath::LinesHint);
813 stroke(path, state()->pen);
814
815 elementCount -= 32;
816 lines += 16;
817 }
818}
819
820void QPaintEngineEx::drawEllipse(const QRectF &r)
821{
822 qreal pts[26]; // QPointF[13] without constructors...
823 union {
824 qreal *ptr;
825 QPointF *points;
826 } x;
827 x.ptr = pts;
828
829 int point_count = 0;
830 x.points[0] = qt_curves_for_arc(r, 0, -360, x.points + 1, &point_count);
831 if (point_count == 0)
832 return;
833 QVectorPath vp((qreal *) pts, point_count + 1, qpaintengineex_ellipse_types, QVectorPath::EllipseHint);
834 draw(vp);
835}
836
837void QPaintEngineEx::drawEllipse(const QRect &r)
838{
839 drawEllipse(QRectF(r));
840}
841
842void QPaintEngineEx::drawPath(const QPainterPath &path)
843{
844 if (!path.isEmpty())
845 draw(qtVectorPathForPath(path));
846}
847
848
849void QPaintEngineEx::drawPoints(const QPointF *points, int pointCount)
850{
851 QPen pen = state()->pen;
852 if (pen.capStyle() == Qt::FlatCap)
853 pen.setCapStyle(Qt::SquareCap);
854
855 if (pen.brush().isOpaque()) {
856 while (pointCount > 0) {
857 int count = qMin(pointCount, 16);
858 qreal pts[64];
859 int oset = -1;
860 for (int i=0; i<count; ++i) {
861 pts[++oset] = points[i].x();
862 pts[++oset] = points[i].y();
863 pts[++oset] = points[i].x() + 1/63.;
864 pts[++oset] = points[i].y();
865 }
866 QVectorPath path(pts, count * 2, qpaintengineex_line_types_16, QVectorPath::LinesHint);
867 stroke(path, pen);
868 pointCount -= 16;
869 points += 16;
870 }
871 } else {
872 for (int i=0; i<pointCount; ++i) {
873 qreal pts[] = { points[i].x(), points[i].y(), points[i].x() + qreal(1/63.), points[i].y() };
874 QVectorPath path(pts, 2, 0);
875 stroke(path, pen);
876 }
877 }
878}
879
880void QPaintEngineEx::drawPoints(const QPoint *points, int pointCount)
881{
882 QPen pen = state()->pen;
883 if (pen.capStyle() == Qt::FlatCap)
884 pen.setCapStyle(Qt::SquareCap);
885
886 if (pen.brush().isOpaque()) {
887 while (pointCount > 0) {
888 int count = qMin(pointCount, 16);
889 qreal pts[64];
890 int oset = -1;
891 for (int i=0; i<count; ++i) {
892 pts[++oset] = points[i].x();
893 pts[++oset] = points[i].y();
894 pts[++oset] = points[i].x() + 1/63.;
895 pts[++oset] = points[i].y();
896 }
897 QVectorPath path(pts, count * 2, qpaintengineex_line_types_16, QVectorPath::LinesHint);
898 stroke(path, pen);
899 pointCount -= 16;
900 points += 16;
901 }
902 } else {
903 for (int i=0; i<pointCount; ++i) {
904 qreal pts[] = { qreal(points[i].x()), qreal(points[i].y()),
905 qreal(points[i].x() +1/63.), qreal(points[i].y()) };
906 QVectorPath path(pts, 2, 0);
907 stroke(path, pen);
908 }
909 }
910}
911
912
913void QPaintEngineEx::drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode)
914{
915 QVectorPath path((const qreal *) points, pointCount, 0, QVectorPath::polygonFlags(mode));
916
917 if (mode == PolylineMode)
918 stroke(path, state()->pen);
919 else
920 draw(path);
921}
922
923void QPaintEngineEx::drawPolygon(const QPoint *points, int pointCount, PolygonDrawMode mode)
924{
925 int count = pointCount<<1;
926 QVarLengthArray<qreal> pts(count);
927
928 for (int i=0; i<count; ++i)
929 pts[i] = ((const int *) points)[i];
930
931 QVectorPath path(pts.data(), pointCount, 0, QVectorPath::polygonFlags(mode));
932
933 if (mode == PolylineMode)
934 stroke(path, state()->pen);
935 else
936 draw(path);
937
938}
939
940void QPaintEngineEx::drawPixmap(const QPointF &pos, const QPixmap &pm)
941{
942 drawPixmap(QRectF(pos, pm.size() / pm.devicePixelRatio()), pm, pm.rect());
943}
944
945void QPaintEngineEx::drawImage(const QPointF &pos, const QImage &image)
946{
947 drawImage(QRectF(pos, image.size() / image.devicePixelRatio()), image, image.rect());
948}
949
950void QPaintEngineEx::drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPointF &s)
951{
952 QBrush brush(state()->pen.color(), pixmap);
953 QTransform xform = QTransform::fromTranslate(r.x() - s.x(), r.y() - s.y());
954 if (!qFuzzyCompare(pixmap.devicePixelRatioF(), 1.0))
955 xform.scale(1.0/pixmap.devicePixelRatioF(), 1.0/pixmap.devicePixelRatioF());
956 brush.setTransform(xform);
957
958 qreal pts[] = { r.x(), r.y(),
959 r.x() + r.width(), r.y(),
960 r.x() + r.width(), r.y() + r.height(),
961 r.x(), r.y() + r.height() };
962
963 QVectorPath path(pts, 4, 0, QVectorPath::RectangleHint);
964 fill(path, brush);
965}
966
967void QPaintEngineEx::drawPixmapFragments(const QPainter::PixmapFragment *fragments, int fragmentCount,
968 const QPixmap &pixmap, QPainter::PixmapFragmentHints /*hints*/)
969{
970 if (pixmap.isNull())
971 return;
972
973 qreal oldOpacity = state()->opacity;
974 QTransform oldTransform = state()->matrix;
975
976 for (int i = 0; i < fragmentCount; ++i) {
977 QTransform transform = oldTransform;
978 transform.translate(fragments[i].x, fragments[i].y);
979 transform.rotate(fragments[i].rotation);
980 state()->opacity = oldOpacity * fragments[i].opacity;
981 state()->matrix = transform;
982 opacityChanged();
983 transformChanged();
984
985 qreal w = fragments[i].scaleX * fragments[i].width;
986 qreal h = fragments[i].scaleY * fragments[i].height;
987 QRectF sourceRect(fragments[i].sourceLeft, fragments[i].sourceTop,
988 fragments[i].width, fragments[i].height);
989 drawPixmap(QRectF(-0.5 * w, -0.5 * h, w, h), pixmap, sourceRect);
990 }
991
992 state()->opacity = oldOpacity;
993 state()->matrix = oldTransform;
994 opacityChanged();
995 transformChanged();
996}
997
998void QPaintEngineEx::setState(QPainterState *s)
999{
1000 QPaintEngine::state = s;
1001}
1002
1003
1004void QPaintEngineEx::updateState(const QPaintEngineState &)
1005{
1006 // do nothing...
1007}
1008
1009Q_GUI_EXPORT QPainterPath qt_painterPathFromVectorPath(const QVectorPath &path)
1010{
1011 const qreal *points = path.points();
1012 const QPainterPath::ElementType *types = path.elements();
1013
1014 QPainterPath p;
1015 if (types) {
1016 int id = 0;
1017 for (int i=0; i<path.elementCount(); ++i) {
1018 switch(types[i]) {
1019 case QPainterPath::MoveToElement:
1020 p.moveTo(QPointF(points[id], points[id+1]));
1021 id+=2;
1022 break;
1023 case QPainterPath::LineToElement:
1024 p.lineTo(QPointF(points[id], points[id+1]));
1025 id+=2;
1026 break;
1027 case QPainterPath::CurveToElement: {
1028 QPointF p1(points[id], points[id+1]);
1029 QPointF p2(points[id+2], points[id+3]);
1030 QPointF p3(points[id+4], points[id+5]);
1031 p.cubicTo(p1, p2, p3);
1032 id+=6;
1033 break;
1034 }
1035 case QPainterPath::CurveToDataElement:
1036 ;
1037 break;
1038 }
1039 }
1040 } else {
1041 p.moveTo(QPointF(points[0], points[1]));
1042 int id = 2;
1043 for (int i=1; i<path.elementCount(); ++i) {
1044 p.lineTo(QPointF(points[id], points[id+1]));
1045 id+=2;
1046 }
1047 }
1048 if (path.hints() & QVectorPath::WindingFill)
1049 p.setFillRule(Qt::WindingFill);
1050
1051 return p;
1052}
1053
1054void QPaintEngineEx::drawStaticTextItem(QStaticTextItem *staticTextItem)
1055{
1056 QPainterPath path;
1057 path.setFillRule(Qt::WindingFill);
1058
1059 if (staticTextItem->numGlyphs == 0)
1060 return;
1061
1062 QFontEngine *fontEngine = staticTextItem->fontEngine();
1063 fontEngine->addGlyphsToPath(staticTextItem->glyphs, staticTextItem->glyphPositions,
1064 staticTextItem->numGlyphs, &path, 0);
1065 if (!path.isEmpty()) {
1066 QPainterState *s = state();
1067 QPainter::RenderHints oldHints = s->renderHints;
1068 bool changedHints = false;
1069 if (bool(oldHints & QPainter::TextAntialiasing)
1070 && !bool(fontEngine->fontDef.styleStrategy & QFont::NoAntialias)
1071 && !bool(oldHints & QPainter::Antialiasing)) {
1072 s->renderHints |= QPainter::Antialiasing;
1073 renderHintsChanged();
1074 changedHints = true;
1075 }
1076
1077 fill(qtVectorPathForPath(path), s->pen.brush());
1078
1079 if (changedHints) {
1080 s->renderHints = oldHints;
1081 renderHintsChanged();
1082 }
1083 }
1084}
1085
1086bool QPaintEngineEx::requiresPretransformedGlyphPositions(QFontEngine *, const QTransform &) const
1087{
1088 return false;
1089}
1090
1091bool QPaintEngineEx::shouldDrawCachedGlyphs(QFontEngine *fontEngine, const QTransform &m) const
1092{
1093 if (fontEngine->glyphFormat == QFontEngine::Format_ARGB)
1094 return true;
1095
1096 static const int maxCachedGlyphSizeSquared = std::pow([]{
1097 if (int env = qEnvironmentVariableIntValue("QT_MAX_CACHED_GLYPH_SIZE"))
1098 return env;
1099 return QT_MAX_CACHED_GLYPH_SIZE;
1100 }(), 2);
1101
1102 qreal pixelSize = fontEngine->fontDef.pixelSize;
1103 return (pixelSize * pixelSize * qAbs(m.determinant())) <= maxCachedGlyphSizeSquared;
1104}
1105
1106QT_END_NAMESPACE
1107