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>
47 #include <private/qfontengine_p.h>
48 #include <private/qstatictext_p.h>
50 #include <qvarlengtharray.h>
56 /*******************************************************************************
61 QVectorPath::~QVectorPath()
63 if (m_hints & ShouldUseCacheHint) {
64 CacheEntry *e = m_cache;
67 e->cleanup(e->engine, e->data);
68 CacheEntry *n = e->next;
76 QRectF QVectorPath::controlPointRect() const
78 if (m_hints & ControlPointRect)
79 return QRectF(QPointF(m_cp_rect.x1, m_cp_rect.y1), QPointF(m_cp_rect.x2, m_cp_rect.y2));
82 m_cp_rect.x1 = m_cp_rect.x2 = m_cp_rect.y1 = m_cp_rect.y2 = 0;
83 m_hints |= ControlPointRect;
84 return QRectF(QPointF(m_cp_rect.x1, m_cp_rect.y1), QPointF(m_cp_rect.x2, m_cp_rect.y2));
86 Q_ASSERT(m_points && m_count > 0);
88 const qreal *pts = m_points;
89 m_cp_rect.x1 = m_cp_rect.x2 = *pts;
91 m_cp_rect.y1 = m_cp_rect.y2 = *pts;
94 const qreal *epts = m_points + (m_count << 1);
97 if (x < m_cp_rect.x1) m_cp_rect.x1 = x;
98 else if (x > m_cp_rect.x2) m_cp_rect.x2 = x;
102 if (y < m_cp_rect.y1) m_cp_rect.y1 = y;
103 else if (y > m_cp_rect.y2) m_cp_rect.y2 = y;
107 m_hints |= ControlPointRect;
108 return QRectF(QPointF(m_cp_rect.x1, m_cp_rect.y1), QPointF(m_cp_rect.x2, m_cp_rect.y2));
112 QVectorPath::CacheEntry *QVectorPath::addCacheData(QPaintEngineEx *engine, void *data,
113 qvectorpath_cache_cleanup cleanup) const{
114 Q_ASSERT(!lookupCacheData(engine));
115 if ((m_hints & IsCachedHint) == 0) {
117 m_hints |= IsCachedHint;
119 CacheEntry *e = new CacheEntry;
122 e->cleanup = cleanup;
129 const QVectorPath &qtVectorPathForPath(const QPainterPath &path)
131 Q_ASSERT(path.d_func());
132 return path.d_func()->vectorPath();
135 #ifndef QT_NO_DEBUG_STREAM
136 QDebug Q_GUI_EXPORT &operator<<(QDebug &s, const QVectorPath &path)
138 QRectF rf = path.controlPointRect();
139 s << "QVectorPath(size:" << path.elementCount()
140 << " hints:" << hex << path.hints()
146 /*******************************************************************************
148 * class QPaintEngineExPrivate:
153 struct StrokeHandler {
154 StrokeHandler(int reserve) : pts(reserve), types(reserve) {}
155 QDataBuffer<qreal> pts;
156 QDataBuffer<QPainterPath::ElementType> types;
160 QPaintEngineExPrivate::QPaintEngineExPrivate()
164 strokerPen(Qt::NoPen)
169 QPaintEngineExPrivate::~QPaintEngineExPrivate()
171 delete strokeHandler;
175 void QPaintEngineExPrivate::replayClipOperations()
179 QPainter *p = q->painter();
183 QList<QPainterClipInfo> clipInfo = p->d_ptr->state->clipInfo;
185 QTransform transform = q->state()->matrix;
187 for (int i = 0; i < clipInfo.size(); ++i) {
188 const QPainterClipInfo &info = clipInfo.at(i);
190 if (info.matrix != q->state()->matrix) {
191 q->state()->matrix = info.matrix;
192 q->transformChanged();
195 switch (info.clipType) {
196 case QPainterClipInfo::RegionClip:
197 q->clip(info.region, info.operation);
199 case QPainterClipInfo::PathClip:
200 q->clip(info.path, info.operation);
202 case QPainterClipInfo::RectClip:
203 q->clip(info.rect, info.operation);
205 case QPainterClipInfo::RectFClip: {
206 qreal right = info.rectf.x() + info.rectf.width();
207 qreal bottom = info.rectf.y() + info.rectf.height();
208 qreal pts[] = { info.rectf.x(), info.rectf.y(),
209 right, info.rectf.y(),
211 info.rectf.x(), bottom };
212 QVectorPath vp(pts, 4, 0, QVectorPath::RectangleHint);
213 q->clip(vp, info.operation);
219 if (transform != q->state()->matrix) {
220 q->state()->matrix = transform;
221 q->transformChanged();
226 bool QPaintEngineExPrivate::hasClipOperations() const
228 Q_Q(const QPaintEngineEx);
230 QPainter *p = q->painter();
234 QList<QPainterClipInfo> clipInfo = p->d_ptr->state->clipInfo;
236 return !clipInfo.isEmpty();
239 /*******************************************************************************
241 * class QPaintEngineEx:
245 static QPainterPath::ElementType qpaintengineex_ellipse_types[] = {
246 QPainterPath::MoveToElement,
247 QPainterPath::CurveToElement,
248 QPainterPath::CurveToDataElement,
249 QPainterPath::CurveToDataElement,
251 QPainterPath::CurveToElement,
252 QPainterPath::CurveToDataElement,
253 QPainterPath::CurveToDataElement,
255 QPainterPath::CurveToElement,
256 QPainterPath::CurveToDataElement,
257 QPainterPath::CurveToDataElement,
259 QPainterPath::CurveToElement,
260 QPainterPath::CurveToDataElement,
261 QPainterPath::CurveToDataElement
264 static QPainterPath::ElementType qpaintengineex_line_types_16[] = {
265 QPainterPath::MoveToElement, QPainterPath::LineToElement,
266 QPainterPath::MoveToElement, QPainterPath::LineToElement,
267 QPainterPath::MoveToElement, QPainterPath::LineToElement,
268 QPainterPath::MoveToElement, QPainterPath::LineToElement,
269 QPainterPath::MoveToElement, QPainterPath::LineToElement,
270 QPainterPath::MoveToElement, QPainterPath::LineToElement,
271 QPainterPath::MoveToElement, QPainterPath::LineToElement,
272 QPainterPath::MoveToElement, QPainterPath::LineToElement,
273 QPainterPath::MoveToElement, QPainterPath::LineToElement,
274 QPainterPath::MoveToElement, QPainterPath::LineToElement,
275 QPainterPath::MoveToElement, QPainterPath::LineToElement,
276 QPainterPath::MoveToElement, QPainterPath::LineToElement,
277 QPainterPath::MoveToElement, QPainterPath::LineToElement,
278 QPainterPath::MoveToElement, QPainterPath::LineToElement,
279 QPainterPath::MoveToElement, QPainterPath::LineToElement,
280 QPainterPath::MoveToElement, QPainterPath::LineToElement
283 static QPainterPath::ElementType qpaintengineex_rect4_types_32[] = {
284 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 1
285 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 2
286 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 3
287 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 4
288 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 5
289 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 6
290 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 7
291 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 8
292 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 9
293 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 10
294 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 11
295 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 12
296 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 13
297 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 14
298 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 15
299 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 16
300 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 17
301 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 18
302 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 19
303 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 20
304 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 21
305 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 22
306 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 23
307 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 24
308 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 25
309 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 26
310 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 27
311 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 28
312 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 29
313 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 30
314 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 31
315 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 32
319 static QPainterPath::ElementType qpaintengineex_roundedrect_types[] = {
320 QPainterPath::MoveToElement,
321 QPainterPath::LineToElement,
322 QPainterPath::CurveToElement,
323 QPainterPath::CurveToDataElement,
324 QPainterPath::CurveToDataElement,
325 QPainterPath::LineToElement,
326 QPainterPath::CurveToElement,
327 QPainterPath::CurveToDataElement,
328 QPainterPath::CurveToDataElement,
329 QPainterPath::LineToElement,
330 QPainterPath::CurveToElement,
331 QPainterPath::CurveToDataElement,
332 QPainterPath::CurveToDataElement,
333 QPainterPath::LineToElement,
334 QPainterPath::CurveToElement,
335 QPainterPath::CurveToDataElement,
336 QPainterPath::CurveToDataElement
341 static void qpaintengineex_moveTo(qreal x, qreal y, void *data) {
342 ((StrokeHandler *) data)->pts.add(x);
343 ((StrokeHandler *) data)->pts.add(y);
344 ((StrokeHandler *) data)->types.add(QPainterPath::MoveToElement);
347 static void qpaintengineex_lineTo(qreal x, qreal y, void *data) {
348 ((StrokeHandler *) data)->pts.add(x);
349 ((StrokeHandler *) data)->pts.add(y);
350 ((StrokeHandler *) data)->types.add(QPainterPath::LineToElement);
353 static void qpaintengineex_cubicTo(qreal c1x, qreal c1y, qreal c2x, qreal c2y, qreal ex, qreal ey, void *data) {
354 ((StrokeHandler *) data)->pts.add(c1x);
355 ((StrokeHandler *) data)->pts.add(c1y);
356 ((StrokeHandler *) data)->types.add(QPainterPath::CurveToElement);
358 ((StrokeHandler *) data)->pts.add(c2x);
359 ((StrokeHandler *) data)->pts.add(c2y);
360 ((StrokeHandler *) data)->types.add(QPainterPath::CurveToDataElement);
362 ((StrokeHandler *) data)->pts.add(ex);
363 ((StrokeHandler *) data)->pts.add(ey);
364 ((StrokeHandler *) data)->types.add(QPainterPath::CurveToDataElement);
367 QPaintEngineEx::QPaintEngineEx()
368 : QPaintEngine(*new QPaintEngineExPrivate, AllFeatures)
373 QPaintEngineEx::QPaintEngineEx(QPaintEngineExPrivate &data)
374 : QPaintEngine(data, AllFeatures)
379 QPainterState *QPaintEngineEx::createState(QPainterState *orig) const
382 return new QPainterState;
383 return new QPainterState(orig);
386 Q_GUI_EXPORT extern bool qt_scaleForTransform(const QTransform &transform, qreal *scale); // qtransform.cpp
388 void QPaintEngineEx::stroke(const QVectorPath &path, const QPen &pen)
391 qDebug() << "QPaintEngineEx::stroke()" << pen;
399 if (!d->strokeHandler) {
400 d->strokeHandler = new StrokeHandler(path.elementCount()+4);
401 d->stroker.setMoveToHook(qpaintengineex_moveTo);
402 d->stroker.setLineToHook(qpaintengineex_lineTo);
403 d->stroker.setCubicToHook(qpaintengineex_cubicTo);
406 if (!qpen_fast_equals(pen, d->strokerPen)) {
408 d->stroker.setJoinStyle(pen.joinStyle());
409 d->stroker.setCapStyle(pen.capStyle());
410 d->stroker.setMiterLimit(pen.miterLimit());
411 qreal penWidth = pen.widthF();
413 d->stroker.setStrokeWidth(1);
415 d->stroker.setStrokeWidth(penWidth);
417 Qt::PenStyle style = pen.style();
418 if (style == Qt::SolidLine) {
419 d->activeStroker = &d->stroker;
420 } else if (style == Qt::NoPen) {
421 d->activeStroker = 0;
423 d->dasher.setDashPattern(pen.dashPattern());
424 d->dasher.setDashOffset(pen.dashOffset());
425 d->activeStroker = &d->dasher;
429 if (!d->activeStroker) {
433 if (pen.style() > Qt::SolidLine) {
434 if (pen.isCosmetic()) {
435 d->activeStroker->setClipRect(d->exDeviceRect);
437 QRectF clipRect = state()->matrix.inverted().mapRect(QRectF(d->exDeviceRect));
438 d->activeStroker->setClipRect(clipRect);
442 const QPainterPath::ElementType *types = path.elements();
443 const qreal *points = path.points();
444 int pointCount = path.elementCount();
446 const qreal *lastPoint = points + (pointCount<<1);
448 d->strokeHandler->types.reset();
449 d->strokeHandler->pts.reset();
451 // Some engines might decide to optimize for the non-shape hint later on...
452 uint flags = QVectorPath::WindingFill;
454 if (path.elementCount() > 2)
455 flags |= QVectorPath::NonConvexShapeMask;
457 if (d->stroker.capStyle() == Qt::RoundCap || d->stroker.joinStyle() == Qt::RoundJoin)
458 flags |= QVectorPath::CurvedShapeMask;
460 // ### Perspective Xforms are currently not supported...
461 if (!pen.isCosmetic()) {
462 // We include cosmetic pens in this case to avoid having to
463 // change the current transform. Normal transformed,
464 // non-cosmetic pens will be transformed as part of fill
465 // later, so they are also covered here..
466 d->activeStroker->setCurveThresholdFromTransform(state()->matrix);
467 d->activeStroker->begin(d->strokeHandler);
469 while (points < lastPoint) {
471 case QPainterPath::MoveToElement:
472 d->activeStroker->moveTo(points[0], points[1]);
476 case QPainterPath::LineToElement:
477 d->activeStroker->lineTo(points[0], points[1]);
481 case QPainterPath::CurveToElement:
482 d->activeStroker->cubicTo(points[0], points[1],
483 points[2], points[3],
484 points[4], points[5]);
487 flags |= QVectorPath::CurvedShapeMask;
493 if (path.hasImplicitClose())
494 d->activeStroker->lineTo(path.points()[0], path.points()[1]);
497 d->activeStroker->moveTo(points[0], points[1]);
499 while (points < lastPoint) {
500 d->activeStroker->lineTo(points[0], points[1]);
503 if (path.hasImplicitClose())
504 d->activeStroker->lineTo(path.points()[0], path.points()[1]);
506 d->activeStroker->end();
508 if (!d->strokeHandler->types.size()) // an empty path...
511 QVectorPath strokePath(d->strokeHandler->pts.data(),
512 d->strokeHandler->types.size(),
513 d->strokeHandler->types.data(),
515 fill(strokePath, pen.brush());
517 // For cosmetic pens we need a bit of trickery... We to process xform the input points
518 if (state()->matrix.type() >= QTransform::TxProject) {
519 QPainterPath painterPath = state()->matrix.map(path.convertToPainterPath());
520 d->activeStroker->strokePath(painterPath, d->strokeHandler, QTransform());
522 d->activeStroker->setCurveThresholdFromTransform(QTransform());
523 d->activeStroker->begin(d->strokeHandler);
525 while (points < lastPoint) {
527 case QPainterPath::MoveToElement: {
528 QPointF pt = (*(QPointF *) points) * state()->matrix;
529 d->activeStroker->moveTo(pt.x(), pt.y());
534 case QPainterPath::LineToElement: {
535 QPointF pt = (*(QPointF *) points) * state()->matrix;
536 d->activeStroker->lineTo(pt.x(), pt.y());
541 case QPainterPath::CurveToElement: {
542 QPointF c1 = ((QPointF *) points)[0] * state()->matrix;
543 QPointF c2 = ((QPointF *) points)[1] * state()->matrix;
544 QPointF e = ((QPointF *) points)[2] * state()->matrix;
545 d->activeStroker->cubicTo(c1.x(), c1.y(), c2.x(), c2.y(), e.x(), e.y());
548 flags |= QVectorPath::CurvedShapeMask;
555 if (path.hasImplicitClose()) {
556 QPointF pt = * ((QPointF *) path.points()) * state()->matrix;
557 d->activeStroker->lineTo(pt.x(), pt.y());
561 QPointF p = ((QPointF *)points)[0] * state()->matrix;
562 d->activeStroker->moveTo(p.x(), p.y());
564 while (points < lastPoint) {
565 QPointF p = ((QPointF *)points)[0] * state()->matrix;
566 d->activeStroker->lineTo(p.x(), p.y());
569 if (path.hasImplicitClose())
570 d->activeStroker->lineTo(p.x(), p.y());
572 d->activeStroker->end();
575 QVectorPath strokePath(d->strokeHandler->pts.data(),
576 d->strokeHandler->types.size(),
577 d->strokeHandler->types.data(),
580 QTransform xform = state()->matrix;
581 state()->matrix = QTransform();
584 QBrush brush = pen.brush();
585 if (qbrush_style(brush) != Qt::SolidPattern)
586 brush.setTransform(brush.transform() * xform);
588 fill(strokePath, brush);
590 state()->matrix = xform;
595 void QPaintEngineEx::draw(const QVectorPath &path)
597 const QBrush &brush = state()->brush;
598 if (qbrush_style(brush) != Qt::NoBrush)
601 const QPen &pen = state()->pen;
602 if (qpen_style(pen) != Qt::NoPen && qbrush_style(qpen_brush(pen)) != Qt::NoBrush)
607 void QPaintEngineEx::clip(const QRect &r, Qt::ClipOperation op)
609 qreal right = r.x() + r.width();
610 qreal bottom = r.y() + r.height();
611 qreal pts[] = { qreal(r.x()), qreal(r.y()),
614 qreal(r.x()), bottom,
615 qreal(r.x()), qreal(r.y()) };
616 QVectorPath vp(pts, 5, 0, QVectorPath::RectangleHint);
620 void QPaintEngineEx::clip(const QRegion ®ion, Qt::ClipOperation op)
622 if (region.rectCount() == 1)
623 clip(region.boundingRect(), op);
625 QVector<QRect> rects = region.rects();
626 if (rects.size() <= 32) {
629 for (QVector<QRect>::const_iterator i = rects.constBegin(); i != rects.constEnd(); ++i) {
632 qreal x2 = i->x() + i->width();
633 qreal y2 = i->y() + i->height();
647 QVectorPath vp(pts, rects.size() * 4, qpaintengineex_rect4_types_32);
650 QVarLengthArray<qreal> pts(rects.size() * 2 * 4);
651 QVarLengthArray<QPainterPath::ElementType> types(rects.size() * 4);
655 for (QVector<QRect>::const_iterator i = rects.constBegin(); i != rects.constEnd(); ++i) {
658 qreal x2 = i->x() + i->width();
659 qreal y2 = i->y() + i->height();
673 types[tpos++] = QPainterPath::MoveToElement;
674 types[tpos++] = QPainterPath::LineToElement;
675 types[tpos++] = QPainterPath::LineToElement;
676 types[tpos++] = QPainterPath::LineToElement;
679 QVectorPath vp(pts.data(), rects.size() * 4, types.data());
685 void QPaintEngineEx::clip(const QPainterPath &path, Qt::ClipOperation op)
687 if (path.isEmpty()) {
688 QVectorPath vp(0, 0);
691 clip(qtVectorPathForPath(path), op);
695 void QPaintEngineEx::fillRect(const QRectF &r, const QBrush &brush)
697 qreal pts[] = { r.x(), r.y(), r.x() + r.width(), r.y(),
698 r.x() + r.width(), r.y() + r.height(), r.x(), r.y() + r.height() };
699 QVectorPath vp(pts, 4, 0, QVectorPath::RectangleHint);
703 void QPaintEngineEx::fillRect(const QRectF &r, const QColor &color)
705 fillRect(r, QBrush(color));
708 void QPaintEngineEx::drawRects(const QRect *rects, int rectCount)
710 for (int i=0; i<rectCount; ++i) {
711 const QRect &r = rects[i];
712 // ### Is there a one off here?
713 qreal right = r.x() + r.width();
714 qreal bottom = r.y() + r.height();
715 qreal pts[] = { qreal(r.x()), qreal(r.y()),
718 qreal(r.x()), bottom,
719 qreal(r.x()), qreal(r.y()) };
720 QVectorPath vp(pts, 5, 0, QVectorPath::RectangleHint);
725 void QPaintEngineEx::drawRects(const QRectF *rects, int rectCount)
727 for (int i=0; i<rectCount; ++i) {
728 const QRectF &r = rects[i];
729 qreal right = r.x() + r.width();
730 qreal bottom = r.y() + r.height();
731 qreal pts[] = { r.x(), r.y(),
736 QVectorPath vp(pts, 5, 0, QVectorPath::RectangleHint);
742 void QPaintEngineEx::drawRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius,
745 qreal x1 = rect.left();
746 qreal x2 = rect.right();
747 qreal y1 = rect.top();
748 qreal y2 = rect.bottom();
750 if (mode == Qt::RelativeSize) {
751 xRadius = xRadius * rect.width() / 200.;
752 yRadius = yRadius * rect.height() / 200.;
755 xRadius = qMin(xRadius, rect.width() / 2);
756 yRadius = qMin(yRadius, rect.height() / 2);
759 x1 + xRadius, y1, // MoveTo
760 x2 - xRadius, y1, // LineTo
761 x2 - (1 - KAPPA) * xRadius, y1, // CurveTo
762 x2, y1 + (1 - KAPPA) * yRadius,
764 x2, y2 - yRadius, // LineTo
765 x2, y2 - (1 - KAPPA) * yRadius, // CurveTo
766 x2 - (1 - KAPPA) * xRadius, y2,
768 x1 + xRadius, y2, // LineTo
769 x1 + (1 - KAPPA) * xRadius, y2, // CurveTo
770 x1, y2 - (1 - KAPPA) * yRadius,
772 x1, y1 + yRadius, // LineTo
773 x1, y1 + (1 - KAPPA) * yRadius, // CurveTo
774 x1 + (1 - KAPPA) * xRadius, y1,
778 QVectorPath path(pts, 17, qpaintengineex_roundedrect_types, QVectorPath::RoundedRectHint);
784 void QPaintEngineEx::drawLines(const QLine *lines, int lineCount)
786 int elementCount = lineCount << 1;
787 while (elementCount > 0) {
788 int count = qMin(elementCount, 32);
791 int count2 = count<<1;
793 for (int i=0; i<count2; i+=2) {
794 pts[i] = ((int *) lines)[i+1];
795 pts[i+1] = ((int *) lines)[i];
798 for (int i=0; i<count2; ++i)
799 pts[i] = ((int *) lines)[i];
802 QVectorPath path(pts, count, qpaintengineex_line_types_16, QVectorPath::LinesHint);
803 stroke(path, state()->pen);
810 void QPaintEngineEx::drawLines(const QLineF *lines, int lineCount)
812 int elementCount = lineCount << 1;
813 while (elementCount > 0) {
814 int count = qMin(elementCount, 32);
816 QVectorPath path((qreal *) lines, count, qpaintengineex_line_types_16,
817 QVectorPath::LinesHint);
818 stroke(path, state()->pen);
825 void QPaintEngineEx::drawEllipse(const QRectF &r)
827 qreal pts[26]; // QPointF[13] without constructors...
835 x.points[0] = qt_curves_for_arc(r, 0, -360, x.points + 1, &point_count);
836 QVectorPath vp((qreal *) pts, point_count + 1, qpaintengineex_ellipse_types, QVectorPath::EllipseHint);
840 void QPaintEngineEx::drawEllipse(const QRect &r)
842 drawEllipse(QRectF(r));
845 void QPaintEngineEx::drawPath(const QPainterPath &path)
848 draw(qtVectorPathForPath(path));
852 void QPaintEngineEx::drawPoints(const QPointF *points, int pointCount)
854 QPen pen = state()->pen;
855 if (pen.capStyle() == Qt::FlatCap)
856 pen.setCapStyle(Qt::SquareCap);
858 if (pen.brush().isOpaque()) {
859 while (pointCount > 0) {
860 int count = qMin(pointCount, 16);
863 for (int i=0; i<count; ++i) {
864 pts[++oset] = points[i].x();
865 pts[++oset] = points[i].y();
866 pts[++oset] = points[i].x() + 1/63.;
867 pts[++oset] = points[i].y();
869 QVectorPath path(pts, count * 2, qpaintengineex_line_types_16, QVectorPath::LinesHint);
875 for (int i=0; i<pointCount; ++i) {
876 qreal pts[] = { points[i].x(), points[i].y(), points[i].x() + 1/63., points[i].y() };
877 QVectorPath path(pts, 2, 0);
883 void QPaintEngineEx::drawPoints(const QPoint *points, int pointCount)
885 QPen pen = state()->pen;
886 if (pen.capStyle() == Qt::FlatCap)
887 pen.setCapStyle(Qt::SquareCap);
889 if (pen.brush().isOpaque()) {
890 while (pointCount > 0) {
891 int count = qMin(pointCount, 16);
894 for (int i=0; i<count; ++i) {
895 pts[++oset] = points[i].x();
896 pts[++oset] = points[i].y();
897 pts[++oset] = points[i].x() + 1/63.;
898 pts[++oset] = points[i].y();
900 QVectorPath path(pts, count * 2, qpaintengineex_line_types_16, QVectorPath::LinesHint);
906 for (int i=0; i<pointCount; ++i) {
907 qreal pts[] = { qreal(points[i].x()), qreal(points[i].y()),
908 qreal(points[i].x() +1/63.), qreal(points[i].y()) };
909 QVectorPath path(pts, 2, 0);
916 void QPaintEngineEx::drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode)
918 QVectorPath path((qreal *) points, pointCount, 0, QVectorPath::polygonFlags(mode));
920 if (mode == PolylineMode)
921 stroke(path, state()->pen);
926 void QPaintEngineEx::drawPolygon(const QPoint *points, int pointCount, PolygonDrawMode mode)
928 int count = pointCount<<1;
929 QVarLengthArray<qreal> pts(count);
932 for (int i=0; i<count; i+=2) {
933 pts[i] = ((int *) points)[i+1];
934 pts[i+1] = ((int *) points)[i];
937 for (int i=0; i<count; ++i)
938 pts[i] = ((int *) points)[i];
941 QVectorPath path(pts.data(), pointCount, 0, QVectorPath::polygonFlags(mode));
943 if (mode == PolylineMode)
944 stroke(path, state()->pen);
950 void QPaintEngineEx::drawPixmap(const QPointF &pos, const QPixmap &pm)
952 drawPixmap(QRectF(pos, pm.size()), pm, pm.rect());
955 void QPaintEngineEx::drawImage(const QPointF &pos, const QImage &image)
957 drawImage(QRectF(pos, image.size()), image, image.rect());
960 void QPaintEngineEx::drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPointF &s)
962 QBrush brush(state()->pen.color(), pixmap);
963 QTransform xform = QTransform::fromTranslate(r.x() - s.x(), r.y() - s.y());
964 brush.setTransform(xform);
966 qreal pts[] = { r.x(), r.y(),
967 r.x() + r.width(), r.y(),
968 r.x() + r.width(), r.y() + r.height(),
969 r.x(), r.y() + r.height() };
971 QVectorPath path(pts, 4, 0, QVectorPath::RectangleHint);
975 void QPaintEngineEx::drawPixmapFragments(const QPainter::PixmapFragment *fragments, int fragmentCount,
976 const QPixmap &pixmap, QPainter::PixmapFragmentHints /*hints*/)
981 qreal oldOpacity = state()->opacity;
982 QTransform oldTransform = state()->matrix;
984 for (int i = 0; i < fragmentCount; ++i) {
985 QTransform transform = oldTransform;
986 transform.translate(fragments[i].x, fragments[i].y);
987 transform.rotate(fragments[i].rotation);
988 state()->opacity = oldOpacity * fragments[i].opacity;
989 state()->matrix = transform;
993 qreal w = fragments[i].scaleX * fragments[i].width;
994 qreal h = fragments[i].scaleY * fragments[i].height;
995 QRectF sourceRect(fragments[i].sourceLeft, fragments[i].sourceTop,
996 fragments[i].width, fragments[i].height);
997 drawPixmap(QRectF(-0.5 * w, -0.5 * h, w, h), pixmap, sourceRect);
1000 state()->opacity = oldOpacity;
1001 state()->matrix = oldTransform;
1006 void QPaintEngineEx::setState(QPainterState *s)
1008 QPaintEngine::state = s;
1012 void QPaintEngineEx::updateState(const QPaintEngineState &)
1017 Q_GUI_EXPORT QPainterPath qt_painterPathFromVectorPath(const QVectorPath &path)
1019 const qreal *points = path.points();
1020 const QPainterPath::ElementType *types = path.elements();
1025 for (int i=0; i<path.elementCount(); ++i) {
1027 case QPainterPath::MoveToElement:
1028 p.moveTo(QPointF(points[id], points[id+1]));
1031 case QPainterPath::LineToElement:
1032 p.lineTo(QPointF(points[id], points[id+1]));
1035 case QPainterPath::CurveToElement: {
1036 QPointF p1(points[id], points[id+1]);
1037 QPointF p2(points[id+2], points[id+3]);
1038 QPointF p3(points[id+4], points[id+5]);
1039 p.cubicTo(p1, p2, p3);
1043 case QPainterPath::CurveToDataElement:
1049 p.moveTo(QPointF(points[0], points[1]));
1051 for (int i=1; i<path.elementCount(); ++i) {
1052 p.lineTo(QPointF(points[id], points[id+1]));
1056 if (path.hints() & QVectorPath::WindingFill)
1057 p.setFillRule(Qt::WindingFill);
1062 void QPaintEngineEx::drawStaticTextItem(QStaticTextItem *staticTextItem)
1066 path.setFillRule(Qt::WindingFill);
1069 if (staticTextItem->numGlyphs == 0)
1072 QFontEngine *fontEngine = staticTextItem->fontEngine();
1073 fontEngine->addGlyphsToPath(staticTextItem->glyphs, staticTextItem->glyphPositions,
1074 staticTextItem->numGlyphs, &path, 0);
1075 if (!path.isEmpty()) {
1076 QPainterState *s = state();
1077 QPainter::RenderHints oldHints = s->renderHints;
1078 bool changedHints = false;
1079 if (bool(oldHints & QPainter::TextAntialiasing)
1080 && !bool(fontEngine->fontDef.styleStrategy & QFont::NoAntialias)
1081 && !bool(oldHints & QPainter::Antialiasing)) {
1082 s->renderHints |= QPainter::Antialiasing;
1083 renderHintsChanged();
1084 changedHints = true;
1087 fill(qtVectorPathForPath(path), staticTextItem->color);
1090 s->renderHints = oldHints;
1091 renderHintsChanged();