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