1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include "qpaintengineex_p.h"
43 #include "qpainter_p.h"
44 #include "qstroker_p.h"
45 #include "qbezier_p.h"
46 #include <private/qpainterpath_p.h>
48 #include <qvarlengtharray.h>
54 /*******************************************************************************
59 QVectorPath::~QVectorPath()
61 if (m_hints & ShouldUseCacheHint) {
62 CacheEntry *e = m_cache;
65 e->cleanup(e->engine, e->data);
66 CacheEntry *n = e->next;
74 QRectF QVectorPath::controlPointRect() const
76 if (m_hints & ControlPointRect)
77 return QRectF(QPointF(m_cp_rect.x1, m_cp_rect.y1), QPointF(m_cp_rect.x2, m_cp_rect.y2));
80 m_cp_rect.x1 = m_cp_rect.x2 = m_cp_rect.y1 = m_cp_rect.y2 = 0;
81 m_hints |= ControlPointRect;
82 return QRectF(QPointF(m_cp_rect.x1, m_cp_rect.y1), QPointF(m_cp_rect.x2, m_cp_rect.y2));
84 Q_ASSERT(m_points && m_count > 0);
86 const qreal *pts = m_points;
87 m_cp_rect.x1 = m_cp_rect.x2 = *pts;
89 m_cp_rect.y1 = m_cp_rect.y2 = *pts;
92 const qreal *epts = m_points + (m_count << 1);
95 if (x < m_cp_rect.x1) m_cp_rect.x1 = x;
96 else if (x > m_cp_rect.x2) m_cp_rect.x2 = x;
100 if (y < m_cp_rect.y1) m_cp_rect.y1 = y;
101 else if (y > m_cp_rect.y2) m_cp_rect.y2 = y;
105 m_hints |= ControlPointRect;
106 return QRectF(QPointF(m_cp_rect.x1, m_cp_rect.y1), QPointF(m_cp_rect.x2, m_cp_rect.y2));
110 QVectorPath::CacheEntry *QVectorPath::addCacheData(QPaintEngineEx *engine, void *data,
111 qvectorpath_cache_cleanup cleanup) const{
112 Q_ASSERT(!lookupCacheData(engine));
113 if ((m_hints & IsCachedHint) == 0) {
115 m_hints |= IsCachedHint;
117 CacheEntry *e = new CacheEntry;
120 e->cleanup = cleanup;
127 const QVectorPath &qtVectorPathForPath(const QPainterPath &path)
129 Q_ASSERT(path.d_func());
130 return path.d_func()->vectorPath();
133 #ifndef QT_NO_DEBUG_STREAM
134 QDebug Q_GUI_EXPORT &operator<<(QDebug &s, const QVectorPath &path)
136 QRectF rf = path.controlPointRect();
137 s << "QVectorPath(size:" << path.elementCount()
138 << " hints:" << hex << path.hints()
144 /*******************************************************************************
146 * class QPaintEngineExPrivate:
151 struct StrokeHandler {
152 StrokeHandler(int reserve) : pts(reserve), types(reserve) {}
153 QDataBuffer<qreal> pts;
154 QDataBuffer<QPainterPath::ElementType> types;
158 QPaintEngineExPrivate::QPaintEngineExPrivate()
162 strokerPen(Qt::NoPen)
167 QPaintEngineExPrivate::~QPaintEngineExPrivate()
169 delete strokeHandler;
173 void QPaintEngineExPrivate::replayClipOperations()
177 QPainter *p = q->painter();
181 QList<QPainterClipInfo> clipInfo = p->d_ptr->state->clipInfo;
183 QTransform transform = q->state()->matrix;
185 for (int i = 0; i < clipInfo.size(); ++i) {
186 const QPainterClipInfo &info = clipInfo.at(i);
188 if (info.matrix != q->state()->matrix) {
189 q->state()->matrix = info.matrix;
190 q->transformChanged();
193 switch (info.clipType) {
194 case QPainterClipInfo::RegionClip:
195 q->clip(info.region, info.operation);
197 case QPainterClipInfo::PathClip:
198 q->clip(info.path, info.operation);
200 case QPainterClipInfo::RectClip:
201 q->clip(info.rect, info.operation);
203 case QPainterClipInfo::RectFClip: {
204 qreal right = info.rectf.x() + info.rectf.width();
205 qreal bottom = info.rectf.y() + info.rectf.height();
206 qreal pts[] = { info.rectf.x(), info.rectf.y(),
207 right, info.rectf.y(),
209 info.rectf.x(), bottom };
210 QVectorPath vp(pts, 4, 0, QVectorPath::RectangleHint);
211 q->clip(vp, info.operation);
217 if (transform != q->state()->matrix) {
218 q->state()->matrix = transform;
219 q->transformChanged();
224 bool QPaintEngineExPrivate::hasClipOperations() const
226 Q_Q(const QPaintEngineEx);
228 QPainter *p = q->painter();
232 QList<QPainterClipInfo> clipInfo = p->d_ptr->state->clipInfo;
234 return !clipInfo.isEmpty();
237 /*******************************************************************************
239 * class QPaintEngineEx:
243 static QPainterPath::ElementType qpaintengineex_ellipse_types[] = {
244 QPainterPath::MoveToElement,
245 QPainterPath::CurveToElement,
246 QPainterPath::CurveToDataElement,
247 QPainterPath::CurveToDataElement,
249 QPainterPath::CurveToElement,
250 QPainterPath::CurveToDataElement,
251 QPainterPath::CurveToDataElement,
253 QPainterPath::CurveToElement,
254 QPainterPath::CurveToDataElement,
255 QPainterPath::CurveToDataElement,
257 QPainterPath::CurveToElement,
258 QPainterPath::CurveToDataElement,
259 QPainterPath::CurveToDataElement
262 static QPainterPath::ElementType qpaintengineex_line_types_16[] = {
263 QPainterPath::MoveToElement, QPainterPath::LineToElement,
264 QPainterPath::MoveToElement, QPainterPath::LineToElement,
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
281 static QPainterPath::ElementType qpaintengineex_rect4_types_32[] = {
282 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 1
283 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 2
284 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 3
285 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 4
286 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 5
287 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 6
288 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 7
289 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 8
290 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 9
291 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 10
292 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 11
293 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 12
294 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 13
295 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 14
296 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 15
297 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 16
298 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 17
299 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 18
300 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 19
301 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 20
302 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 21
303 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 22
304 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 23
305 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 24
306 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 25
307 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 26
308 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 27
309 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 28
310 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 29
311 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 30
312 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 31
313 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 32
317 static QPainterPath::ElementType qpaintengineex_roundedrect_types[] = {
318 QPainterPath::MoveToElement,
319 QPainterPath::LineToElement,
320 QPainterPath::CurveToElement,
321 QPainterPath::CurveToDataElement,
322 QPainterPath::CurveToDataElement,
323 QPainterPath::LineToElement,
324 QPainterPath::CurveToElement,
325 QPainterPath::CurveToDataElement,
326 QPainterPath::CurveToDataElement,
327 QPainterPath::LineToElement,
328 QPainterPath::CurveToElement,
329 QPainterPath::CurveToDataElement,
330 QPainterPath::CurveToDataElement,
331 QPainterPath::LineToElement,
332 QPainterPath::CurveToElement,
333 QPainterPath::CurveToDataElement,
334 QPainterPath::CurveToDataElement
339 static void qpaintengineex_moveTo(qreal x, qreal y, void *data) {
340 ((StrokeHandler *) data)->pts.add(x);
341 ((StrokeHandler *) data)->pts.add(y);
342 ((StrokeHandler *) data)->types.add(QPainterPath::MoveToElement);
345 static void qpaintengineex_lineTo(qreal x, qreal y, void *data) {
346 ((StrokeHandler *) data)->pts.add(x);
347 ((StrokeHandler *) data)->pts.add(y);
348 ((StrokeHandler *) data)->types.add(QPainterPath::LineToElement);
351 static void qpaintengineex_cubicTo(qreal c1x, qreal c1y, qreal c2x, qreal c2y, qreal ex, qreal ey, void *data) {
352 ((StrokeHandler *) data)->pts.add(c1x);
353 ((StrokeHandler *) data)->pts.add(c1y);
354 ((StrokeHandler *) data)->types.add(QPainterPath::CurveToElement);
356 ((StrokeHandler *) data)->pts.add(c2x);
357 ((StrokeHandler *) data)->pts.add(c2y);
358 ((StrokeHandler *) data)->types.add(QPainterPath::CurveToDataElement);
360 ((StrokeHandler *) data)->pts.add(ex);
361 ((StrokeHandler *) data)->pts.add(ey);
362 ((StrokeHandler *) data)->types.add(QPainterPath::CurveToDataElement);
365 QPaintEngineEx::QPaintEngineEx()
366 : QPaintEngine(*new QPaintEngineExPrivate, AllFeatures)
371 QPaintEngineEx::QPaintEngineEx(QPaintEngineExPrivate &data)
372 : QPaintEngine(data, AllFeatures)
377 QPainterState *QPaintEngineEx::createState(QPainterState *orig) const
380 return new QPainterState;
381 return new QPainterState(orig);
384 Q_GUI_EXPORT extern bool qt_scaleForTransform(const QTransform &transform, qreal *scale); // qtransform.cpp
386 void QPaintEngineEx::stroke(const QVectorPath &path, const QPen &pen)
389 qDebug() << "QPaintEngineEx::stroke()" << pen;
397 if (!d->strokeHandler) {
398 d->strokeHandler = new StrokeHandler(path.elementCount()+4);
399 d->stroker.setMoveToHook(qpaintengineex_moveTo);
400 d->stroker.setLineToHook(qpaintengineex_lineTo);
401 d->stroker.setCubicToHook(qpaintengineex_cubicTo);
404 if (!qpen_fast_equals(pen, d->strokerPen)) {
406 d->stroker.setJoinStyle(pen.joinStyle());
407 d->stroker.setCapStyle(pen.capStyle());
408 d->stroker.setMiterLimit(pen.miterLimit());
409 qreal penWidth = pen.widthF();
411 d->stroker.setStrokeWidth(1);
413 d->stroker.setStrokeWidth(penWidth);
415 Qt::PenStyle style = pen.style();
416 if (style == Qt::SolidLine) {
417 d->activeStroker = &d->stroker;
418 } else if (style == Qt::NoPen) {
419 d->activeStroker = 0;
421 d->dasher.setDashPattern(pen.dashPattern());
422 d->dasher.setDashOffset(pen.dashOffset());
423 d->activeStroker = &d->dasher;
427 if (!d->activeStroker) {
431 if (pen.style() > Qt::SolidLine) {
432 if (pen.isCosmetic()) {
433 d->activeStroker->setClipRect(d->exDeviceRect);
435 QRectF clipRect = state()->matrix.inverted().mapRect(QRectF(d->exDeviceRect));
436 d->activeStroker->setClipRect(clipRect);
440 const QPainterPath::ElementType *types = path.elements();
441 const qreal *points = path.points();
442 int pointCount = path.elementCount();
444 const qreal *lastPoint = points + (pointCount<<1);
446 d->strokeHandler->types.reset();
447 d->strokeHandler->pts.reset();
449 // Some engines might decide to optimize for the non-shape hint later on...
450 uint flags = QVectorPath::WindingFill;
452 if (path.elementCount() > 2)
453 flags |= QVectorPath::NonConvexShapeMask;
455 if (d->stroker.capStyle() == Qt::RoundCap || d->stroker.joinStyle() == Qt::RoundJoin)
456 flags |= QVectorPath::CurvedShapeMask;
458 // ### Perspective Xforms are currently not supported...
459 if (!pen.isCosmetic()) {
460 // We include cosmetic pens in this case to avoid having to
461 // change the current transform. Normal transformed,
462 // non-cosmetic pens will be transformed as part of fill
463 // later, so they are also covered here..
464 d->activeStroker->setCurveThresholdFromTransform(state()->matrix);
465 d->activeStroker->begin(d->strokeHandler);
467 while (points < lastPoint) {
469 case QPainterPath::MoveToElement:
470 d->activeStroker->moveTo(points[0], points[1]);
474 case QPainterPath::LineToElement:
475 d->activeStroker->lineTo(points[0], points[1]);
479 case QPainterPath::CurveToElement:
480 d->activeStroker->cubicTo(points[0], points[1],
481 points[2], points[3],
482 points[4], points[5]);
485 flags |= QVectorPath::CurvedShapeMask;
491 if (path.hasImplicitClose())
492 d->activeStroker->lineTo(path.points()[0], path.points()[1]);
495 d->activeStroker->moveTo(points[0], points[1]);
497 while (points < lastPoint) {
498 d->activeStroker->lineTo(points[0], points[1]);
501 if (path.hasImplicitClose())
502 d->activeStroker->lineTo(path.points()[0], path.points()[1]);
504 d->activeStroker->end();
506 if (!d->strokeHandler->types.size()) // an empty path...
509 QVectorPath strokePath(d->strokeHandler->pts.data(),
510 d->strokeHandler->types.size(),
511 d->strokeHandler->types.data(),
513 fill(strokePath, pen.brush());
515 // For cosmetic pens we need a bit of trickery... We to process xform the input points
516 if (state()->matrix.type() >= QTransform::TxProject) {
517 QPainterPath painterPath = state()->matrix.map(path.convertToPainterPath());
518 d->activeStroker->strokePath(painterPath, d->strokeHandler, QTransform());
520 d->activeStroker->setCurveThresholdFromTransform(QTransform());
521 d->activeStroker->begin(d->strokeHandler);
523 while (points < lastPoint) {
525 case QPainterPath::MoveToElement: {
526 QPointF pt = (*(QPointF *) points) * state()->matrix;
527 d->activeStroker->moveTo(pt.x(), pt.y());
532 case QPainterPath::LineToElement: {
533 QPointF pt = (*(QPointF *) points) * state()->matrix;
534 d->activeStroker->lineTo(pt.x(), pt.y());
539 case QPainterPath::CurveToElement: {
540 QPointF c1 = ((QPointF *) points)[0] * state()->matrix;
541 QPointF c2 = ((QPointF *) points)[1] * state()->matrix;
542 QPointF e = ((QPointF *) points)[2] * state()->matrix;
543 d->activeStroker->cubicTo(c1.x(), c1.y(), c2.x(), c2.y(), e.x(), e.y());
546 flags |= QVectorPath::CurvedShapeMask;
553 if (path.hasImplicitClose()) {
554 QPointF pt = * ((QPointF *) path.points()) * state()->matrix;
555 d->activeStroker->lineTo(pt.x(), pt.y());
559 QPointF p = ((QPointF *)points)[0] * state()->matrix;
560 d->activeStroker->moveTo(p.x(), p.y());
562 while (points < lastPoint) {
563 QPointF p = ((QPointF *)points)[0] * state()->matrix;
564 d->activeStroker->lineTo(p.x(), p.y());
567 if (path.hasImplicitClose())
568 d->activeStroker->lineTo(p.x(), p.y());
570 d->activeStroker->end();
573 QVectorPath strokePath(d->strokeHandler->pts.data(),
574 d->strokeHandler->types.size(),
575 d->strokeHandler->types.data(),
578 QTransform xform = state()->matrix;
579 state()->matrix = QTransform();
582 QBrush brush = pen.brush();
583 if (qbrush_style(brush) != Qt::SolidPattern)
584 brush.setTransform(brush.transform() * xform);
586 fill(strokePath, brush);
588 state()->matrix = xform;
593 void QPaintEngineEx::draw(const QVectorPath &path)
595 const QBrush &brush = state()->brush;
596 if (qbrush_style(brush) != Qt::NoBrush)
599 const QPen &pen = state()->pen;
600 if (qpen_style(pen) != Qt::NoPen && qbrush_style(qpen_brush(pen)) != Qt::NoBrush)
605 void QPaintEngineEx::clip(const QRect &r, Qt::ClipOperation op)
607 qreal right = r.x() + r.width();
608 qreal bottom = r.y() + r.height();
609 qreal pts[] = { qreal(r.x()), qreal(r.y()),
612 qreal(r.x()), bottom,
613 qreal(r.x()), qreal(r.y()) };
614 QVectorPath vp(pts, 5, 0, QVectorPath::RectangleHint);
618 void QPaintEngineEx::clip(const QRegion ®ion, Qt::ClipOperation op)
620 if (region.rectCount() == 1)
621 clip(region.boundingRect(), op);
623 QVector<QRect> rects = region.rects();
624 if (rects.size() <= 32) {
627 for (QVector<QRect>::const_iterator i = rects.constBegin(); i != rects.constEnd(); ++i) {
630 qreal x2 = i->x() + i->width();
631 qreal y2 = i->y() + i->height();
645 QVectorPath vp(pts, rects.size() * 4, qpaintengineex_rect4_types_32);
648 QVarLengthArray<qreal> pts(rects.size() * 2 * 4);
649 QVarLengthArray<QPainterPath::ElementType> types(rects.size() * 4);
653 for (QVector<QRect>::const_iterator i = rects.constBegin(); i != rects.constEnd(); ++i) {
656 qreal x2 = i->x() + i->width();
657 qreal y2 = i->y() + i->height();
671 types[tpos++] = QPainterPath::MoveToElement;
672 types[tpos++] = QPainterPath::LineToElement;
673 types[tpos++] = QPainterPath::LineToElement;
674 types[tpos++] = QPainterPath::LineToElement;
677 QVectorPath vp(pts.data(), rects.size() * 4, types.data());
683 void QPaintEngineEx::clip(const QPainterPath &path, Qt::ClipOperation op)
685 if (path.isEmpty()) {
686 QVectorPath vp(0, 0);
689 clip(qtVectorPathForPath(path), op);
693 void QPaintEngineEx::fillRect(const QRectF &r, const QBrush &brush)
695 qreal pts[] = { r.x(), r.y(), r.x() + r.width(), r.y(),
696 r.x() + r.width(), r.y() + r.height(), r.x(), r.y() + r.height() };
697 QVectorPath vp(pts, 4, 0, QVectorPath::RectangleHint);
701 void QPaintEngineEx::fillRect(const QRectF &r, const QColor &color)
703 fillRect(r, QBrush(color));
706 void QPaintEngineEx::drawRects(const QRect *rects, int rectCount)
708 for (int i=0; i<rectCount; ++i) {
709 const QRect &r = rects[i];
710 // ### Is there a one off here?
711 qreal right = r.x() + r.width();
712 qreal bottom = r.y() + r.height();
713 qreal pts[] = { qreal(r.x()), qreal(r.y()),
716 qreal(r.x()), bottom,
717 qreal(r.x()), qreal(r.y()) };
718 QVectorPath vp(pts, 5, 0, QVectorPath::RectangleHint);
723 void QPaintEngineEx::drawRects(const QRectF *rects, int rectCount)
725 for (int i=0; i<rectCount; ++i) {
726 const QRectF &r = rects[i];
727 qreal right = r.x() + r.width();
728 qreal bottom = r.y() + r.height();
729 qreal pts[] = { r.x(), r.y(),
734 QVectorPath vp(pts, 5, 0, QVectorPath::RectangleHint);
740 void QPaintEngineEx::drawRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius,
743 qreal x1 = rect.left();
744 qreal x2 = rect.right();
745 qreal y1 = rect.top();
746 qreal y2 = rect.bottom();
748 if (mode == Qt::RelativeSize) {
749 xRadius = xRadius * rect.width() / 200.;
750 yRadius = yRadius * rect.height() / 200.;
753 xRadius = qMin(xRadius, rect.width() / 2);
754 yRadius = qMin(yRadius, rect.height() / 2);
757 x1 + xRadius, y1, // MoveTo
758 x2 - xRadius, y1, // LineTo
759 x2 - (1 - KAPPA) * xRadius, y1, // CurveTo
760 x2, y1 + (1 - KAPPA) * yRadius,
762 x2, y2 - yRadius, // LineTo
763 x2, y2 - (1 - KAPPA) * yRadius, // CurveTo
764 x2 - (1 - KAPPA) * xRadius, y2,
766 x1 + xRadius, y2, // LineTo
767 x1 + (1 - KAPPA) * xRadius, y2, // CurveTo
768 x1, y2 - (1 - KAPPA) * yRadius,
770 x1, y1 + yRadius, // LineTo
771 x1, y1 + (1 - KAPPA) * yRadius, // CurveTo
772 x1 + (1 - KAPPA) * xRadius, y1,
776 QVectorPath path(pts, 17, qpaintengineex_roundedrect_types, QVectorPath::RoundedRectHint);
782 void QPaintEngineEx::drawLines(const QLine *lines, int lineCount)
784 int elementCount = lineCount << 1;
785 while (elementCount > 0) {
786 int count = qMin(elementCount, 32);
789 int count2 = count<<1;
791 for (int i=0; i<count2; i+=2) {
792 pts[i] = ((int *) lines)[i+1];
793 pts[i+1] = ((int *) lines)[i];
796 for (int i=0; i<count2; ++i)
797 pts[i] = ((int *) lines)[i];
800 QVectorPath path(pts, count, qpaintengineex_line_types_16, QVectorPath::LinesHint);
801 stroke(path, state()->pen);
808 void QPaintEngineEx::drawLines(const QLineF *lines, int lineCount)
810 int elementCount = lineCount << 1;
811 while (elementCount > 0) {
812 int count = qMin(elementCount, 32);
814 QVectorPath path((qreal *) lines, count, qpaintengineex_line_types_16,
815 QVectorPath::LinesHint);
816 stroke(path, state()->pen);
823 void QPaintEngineEx::drawEllipse(const QRectF &r)
825 qreal pts[26]; // QPointF[13] without constructors...
833 x.points[0] = qt_curves_for_arc(r, 0, -360, x.points + 1, &point_count);
834 QVectorPath vp((qreal *) pts, point_count, qpaintengineex_ellipse_types, QVectorPath::EllipseHint);
838 void QPaintEngineEx::drawEllipse(const QRect &r)
840 drawEllipse(QRectF(r));
843 void QPaintEngineEx::drawPath(const QPainterPath &path)
846 draw(qtVectorPathForPath(path));
850 void QPaintEngineEx::drawPoints(const QPointF *points, int pointCount)
852 QPen pen = state()->pen;
853 if (pen.capStyle() == Qt::FlatCap)
854 pen.setCapStyle(Qt::SquareCap);
856 if (pen.brush().isOpaque()) {
857 while (pointCount > 0) {
858 int count = qMin(pointCount, 16);
861 for (int i=0; i<count; ++i) {
862 pts[++oset] = points[i].x();
863 pts[++oset] = points[i].y();
864 pts[++oset] = points[i].x() + 1/63.;
865 pts[++oset] = points[i].y();
867 QVectorPath path(pts, count * 2, qpaintengineex_line_types_16, QVectorPath::LinesHint);
873 for (int i=0; i<pointCount; ++i) {
874 qreal pts[] = { points[i].x(), points[i].y(), points[i].x() + 1/63., points[i].y() };
875 QVectorPath path(pts, 2, 0);
881 void QPaintEngineEx::drawPoints(const QPoint *points, int pointCount)
883 QPen pen = state()->pen;
884 if (pen.capStyle() == Qt::FlatCap)
885 pen.setCapStyle(Qt::SquareCap);
887 if (pen.brush().isOpaque()) {
888 while (pointCount > 0) {
889 int count = qMin(pointCount, 16);
892 for (int i=0; i<count; ++i) {
893 pts[++oset] = points[i].x();
894 pts[++oset] = points[i].y();
895 pts[++oset] = points[i].x() + 1/63.;
896 pts[++oset] = points[i].y();
898 QVectorPath path(pts, count * 2, qpaintengineex_line_types_16, QVectorPath::LinesHint);
904 for (int i=0; i<pointCount; ++i) {
905 qreal pts[] = { qreal(points[i].x()), qreal(points[i].y()),
906 qreal(points[i].x() +1/63.), qreal(points[i].y()) };
907 QVectorPath path(pts, 2, 0);
914 void QPaintEngineEx::drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode)
916 QVectorPath path((qreal *) points, pointCount, 0, QVectorPath::polygonFlags(mode));
918 if (mode == PolylineMode)
919 stroke(path, state()->pen);
924 void QPaintEngineEx::drawPolygon(const QPoint *points, int pointCount, PolygonDrawMode mode)
926 int count = pointCount<<1;
927 QVarLengthArray<qreal> pts(count);
930 for (int i=0; i<count; i+=2) {
931 pts[i] = ((int *) points)[i+1];
932 pts[i+1] = ((int *) points)[i];
935 for (int i=0; i<count; ++i)
936 pts[i] = ((int *) points)[i];
939 QVectorPath path(pts.data(), pointCount, 0, QVectorPath::polygonFlags(mode));
941 if (mode == PolylineMode)
942 stroke(path, state()->pen);
948 void QPaintEngineEx::drawPixmap(const QPointF &pos, const QPixmap &pm)
950 drawPixmap(QRectF(pos, pm.size()), pm, pm.rect());
953 void QPaintEngineEx::drawImage(const QPointF &pos, const QImage &image)
955 drawImage(QRectF(pos, image.size()), image, image.rect());
958 void QPaintEngineEx::drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPointF &s)
960 QBrush brush(state()->pen.color(), pixmap);
961 QTransform xform = QTransform::fromTranslate(r.x() - s.x(), r.y() - s.y());
962 brush.setTransform(xform);
964 qreal pts[] = { r.x(), r.y(),
965 r.x() + r.width(), r.y(),
966 r.x() + r.width(), r.y() + r.height(),
967 r.x(), r.y() + r.height() };
969 QVectorPath path(pts, 4, 0, QVectorPath::RectangleHint);
973 void QPaintEngineEx::drawPixmapFragments(const QPainter::PixmapFragment *fragments, int fragmentCount,
974 const QPixmap &pixmap, QPainter::PixmapFragmentHints /*hints*/)
979 qreal oldOpacity = state()->opacity;
980 QTransform oldTransform = state()->matrix;
982 for (int i = 0; i < fragmentCount; ++i) {
983 QTransform transform = oldTransform;
984 transform.translate(fragments[i].x, fragments[i].y);
985 transform.rotate(fragments[i].rotation);
986 state()->opacity = oldOpacity * fragments[i].opacity;
987 state()->matrix = transform;
991 qreal w = fragments[i].scaleX * fragments[i].width;
992 qreal h = fragments[i].scaleY * fragments[i].height;
993 QRectF sourceRect(fragments[i].sourceLeft, fragments[i].sourceTop,
994 fragments[i].width, fragments[i].height);
995 drawPixmap(QRectF(-0.5 * w, -0.5 * h, w, h), pixmap, sourceRect);
998 state()->opacity = oldOpacity;
999 state()->matrix = oldTransform;
1004 void QPaintEngineEx::setState(QPainterState *s)
1006 QPaintEngine::state = s;
1010 void QPaintEngineEx::updateState(const QPaintEngineState &)