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