1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtGui module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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 #if !defined(QT_MAX_CACHED_GLYPH_SIZE)
57 # define QT_MAX_CACHED_GLYPH_SIZE 64
60 /*******************************************************************************
65 QVectorPath::~QVectorPath()
67 if (m_hints & ShouldUseCacheHint) {
68 CacheEntry *e = m_cache;
71 e->cleanup(e->engine, e->data);
72 CacheEntry *n = e->next;
80 QRectF QVectorPath::controlPointRect() const
82 if (m_hints & ControlPointRect)
83 return QRectF(QPointF(m_cp_rect.x1, m_cp_rect.y1), QPointF(m_cp_rect.x2, m_cp_rect.y2));
86 m_cp_rect.x1 = m_cp_rect.x2 = m_cp_rect.y1 = m_cp_rect.y2 = 0;
87 m_hints |= ControlPointRect;
88 return QRectF(QPointF(m_cp_rect.x1, m_cp_rect.y1), QPointF(m_cp_rect.x2, m_cp_rect.y2));
90 Q_ASSERT(m_points && m_count > 0);
92 const qreal *pts = m_points;
93 m_cp_rect.x1 = m_cp_rect.x2 = *pts;
95 m_cp_rect.y1 = m_cp_rect.y2 = *pts;
98 const qreal *epts = m_points + (m_count << 1);
101 if (x < m_cp_rect.x1) m_cp_rect.x1 = x;
102 else if (x > m_cp_rect.x2) m_cp_rect.x2 = x;
106 if (y < m_cp_rect.y1) m_cp_rect.y1 = y;
107 else if (y > m_cp_rect.y2) m_cp_rect.y2 = y;
111 m_hints |= ControlPointRect;
112 return QRectF(QPointF(m_cp_rect.x1, m_cp_rect.y1), QPointF(m_cp_rect.x2, m_cp_rect.y2));
116 QVectorPath::CacheEntry *QVectorPath::addCacheData(QPaintEngineEx *engine, void *data,
117 qvectorpath_cache_cleanup cleanup) const{
118 Q_ASSERT(!lookupCacheData(engine));
119 if ((m_hints & IsCachedHint) == 0) {
121 m_hints |= IsCachedHint;
123 CacheEntry *e = new CacheEntry;
126 e->cleanup = cleanup;
133 const QVectorPath &qtVectorPathForPath(const QPainterPath &path)
135 Q_ASSERT(path.d_func());
136 return path.d_func()->vectorPath();
139 #ifndef QT_NO_DEBUG_STREAM
140 QDebug Q_GUI_EXPORT &operator<<(QDebug &s, const QVectorPath &path)
142 QRectF rf = path.controlPointRect();
143 s << "QVectorPath(size:" << path.elementCount()
144 << " hints:" << hex << path.hints()
150 /*******************************************************************************
152 * class QPaintEngineExPrivate:
157 struct StrokeHandler {
158 StrokeHandler(int reserve) : pts(reserve), types(reserve) {}
159 QDataBuffer<qreal> pts;
160 QDataBuffer<QPainterPath::ElementType> types;
164 QPaintEngineExPrivate::QPaintEngineExPrivate()
168 strokerPen(Qt::NoPen)
173 QPaintEngineExPrivate::~QPaintEngineExPrivate()
175 delete strokeHandler;
179 void QPaintEngineExPrivate::replayClipOperations()
183 QPainter *p = q->painter();
187 QList<QPainterClipInfo> clipInfo = p->d_ptr->state->clipInfo;
189 QTransform transform = q->state()->matrix;
191 for (int i = 0; i < clipInfo.size(); ++i) {
192 const QPainterClipInfo &info = clipInfo.at(i);
194 if (info.matrix != q->state()->matrix) {
195 q->state()->matrix = info.matrix;
196 q->transformChanged();
199 switch (info.clipType) {
200 case QPainterClipInfo::RegionClip:
201 q->clip(info.region, info.operation);
203 case QPainterClipInfo::PathClip:
204 q->clip(info.path, info.operation);
206 case QPainterClipInfo::RectClip:
207 q->clip(info.rect, info.operation);
209 case QPainterClipInfo::RectFClip: {
210 qreal right = info.rectf.x() + info.rectf.width();
211 qreal bottom = info.rectf.y() + info.rectf.height();
212 qreal pts[] = { info.rectf.x(), info.rectf.y(),
213 right, info.rectf.y(),
215 info.rectf.x(), bottom };
216 QVectorPath vp(pts, 4, 0, QVectorPath::RectangleHint);
217 q->clip(vp, info.operation);
223 if (transform != q->state()->matrix) {
224 q->state()->matrix = transform;
225 q->transformChanged();
230 bool QPaintEngineExPrivate::hasClipOperations() const
232 Q_Q(const QPaintEngineEx);
234 QPainter *p = q->painter();
238 QList<QPainterClipInfo> clipInfo = p->d_ptr->state->clipInfo;
240 return !clipInfo.isEmpty();
243 /*******************************************************************************
245 * class QPaintEngineEx:
249 static QPainterPath::ElementType qpaintengineex_ellipse_types[] = {
250 QPainterPath::MoveToElement,
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,
263 QPainterPath::CurveToElement,
264 QPainterPath::CurveToDataElement,
265 QPainterPath::CurveToDataElement
268 static QPainterPath::ElementType qpaintengineex_line_types_16[] = {
269 QPainterPath::MoveToElement, QPainterPath::LineToElement,
270 QPainterPath::MoveToElement, QPainterPath::LineToElement,
271 QPainterPath::MoveToElement, QPainterPath::LineToElement,
272 QPainterPath::MoveToElement, QPainterPath::LineToElement,
273 QPainterPath::MoveToElement, QPainterPath::LineToElement,
274 QPainterPath::MoveToElement, QPainterPath::LineToElement,
275 QPainterPath::MoveToElement, QPainterPath::LineToElement,
276 QPainterPath::MoveToElement, QPainterPath::LineToElement,
277 QPainterPath::MoveToElement, QPainterPath::LineToElement,
278 QPainterPath::MoveToElement, QPainterPath::LineToElement,
279 QPainterPath::MoveToElement, QPainterPath::LineToElement,
280 QPainterPath::MoveToElement, QPainterPath::LineToElement,
281 QPainterPath::MoveToElement, QPainterPath::LineToElement,
282 QPainterPath::MoveToElement, QPainterPath::LineToElement,
283 QPainterPath::MoveToElement, QPainterPath::LineToElement,
284 QPainterPath::MoveToElement, QPainterPath::LineToElement
287 static QPainterPath::ElementType qpaintengineex_rect4_types_32[] = {
288 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 1
289 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 2
290 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 3
291 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 4
292 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 5
293 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 6
294 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 7
295 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 8
296 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 9
297 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 10
298 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 11
299 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 12
300 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 13
301 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 14
302 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 15
303 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 16
304 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 17
305 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 18
306 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 19
307 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 20
308 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 21
309 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 22
310 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 23
311 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 24
312 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 25
313 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 26
314 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 27
315 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 28
316 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 29
317 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 30
318 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 31
319 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 32
323 static QPainterPath::ElementType qpaintengineex_roundedrect_types[] = {
324 QPainterPath::MoveToElement,
325 QPainterPath::LineToElement,
326 QPainterPath::CurveToElement,
327 QPainterPath::CurveToDataElement,
328 QPainterPath::CurveToDataElement,
329 QPainterPath::LineToElement,
330 QPainterPath::CurveToElement,
331 QPainterPath::CurveToDataElement,
332 QPainterPath::CurveToDataElement,
333 QPainterPath::LineToElement,
334 QPainterPath::CurveToElement,
335 QPainterPath::CurveToDataElement,
336 QPainterPath::CurveToDataElement,
337 QPainterPath::LineToElement,
338 QPainterPath::CurveToElement,
339 QPainterPath::CurveToDataElement,
340 QPainterPath::CurveToDataElement
345 static void qpaintengineex_moveTo(qreal x, qreal y, void *data) {
346 ((StrokeHandler *) data)->pts.add(x);
347 ((StrokeHandler *) data)->pts.add(y);
348 ((StrokeHandler *) data)->types.add(QPainterPath::MoveToElement);
351 static void qpaintengineex_lineTo(qreal x, qreal y, void *data) {
352 ((StrokeHandler *) data)->pts.add(x);
353 ((StrokeHandler *) data)->pts.add(y);
354 ((StrokeHandler *) data)->types.add(QPainterPath::LineToElement);
357 static void qpaintengineex_cubicTo(qreal c1x, qreal c1y, qreal c2x, qreal c2y, qreal ex, qreal ey, void *data) {
358 ((StrokeHandler *) data)->pts.add(c1x);
359 ((StrokeHandler *) data)->pts.add(c1y);
360 ((StrokeHandler *) data)->types.add(QPainterPath::CurveToElement);
362 ((StrokeHandler *) data)->pts.add(c2x);
363 ((StrokeHandler *) data)->pts.add(c2y);
364 ((StrokeHandler *) data)->types.add(QPainterPath::CurveToDataElement);
366 ((StrokeHandler *) data)->pts.add(ex);
367 ((StrokeHandler *) data)->pts.add(ey);
368 ((StrokeHandler *) data)->types.add(QPainterPath::CurveToDataElement);
371 QPaintEngineEx::QPaintEngineEx()
372 : QPaintEngine(*new QPaintEngineExPrivate, AllFeatures)
377 QPaintEngineEx::QPaintEngineEx(QPaintEngineExPrivate &data)
378 : QPaintEngine(data, AllFeatures)
383 QPainterState *QPaintEngineEx::createState(QPainterState *orig) const
386 return new QPainterState;
387 return new QPainterState(orig);
390 Q_GUI_EXPORT extern bool qt_scaleForTransform(const QTransform &transform, qreal *scale); // qtransform.cpp
392 void QPaintEngineEx::stroke(const QVectorPath &path, const QPen &pen)
395 qDebug() << "QPaintEngineEx::stroke()" << pen;
403 if (!d->strokeHandler) {
404 d->strokeHandler = new StrokeHandler(path.elementCount()+4);
405 d->stroker.setMoveToHook(qpaintengineex_moveTo);
406 d->stroker.setLineToHook(qpaintengineex_lineTo);
407 d->stroker.setCubicToHook(qpaintengineex_cubicTo);
410 if (!qpen_fast_equals(pen, d->strokerPen)) {
412 d->stroker.setJoinStyle(pen.joinStyle());
413 d->stroker.setCapStyle(pen.capStyle());
414 d->stroker.setMiterLimit(pen.miterLimit());
415 qreal penWidth = pen.widthF();
417 d->stroker.setStrokeWidth(1);
419 d->stroker.setStrokeWidth(penWidth);
421 Qt::PenStyle style = pen.style();
422 if (style == Qt::SolidLine) {
423 d->activeStroker = &d->stroker;
424 } else if (style == Qt::NoPen) {
425 d->activeStroker = 0;
427 d->dasher.setDashPattern(pen.dashPattern());
428 d->dasher.setDashOffset(pen.dashOffset());
429 d->activeStroker = &d->dasher;
433 if (!d->activeStroker) {
437 if (pen.style() > Qt::SolidLine) {
438 if (pen.isCosmetic()) {
439 d->activeStroker->setClipRect(d->exDeviceRect);
441 QRectF clipRect = state()->matrix.inverted().mapRect(QRectF(d->exDeviceRect));
442 d->activeStroker->setClipRect(clipRect);
446 const QPainterPath::ElementType *types = path.elements();
447 const qreal *points = path.points();
448 int pointCount = path.elementCount();
450 const qreal *lastPoint = points + (pointCount<<1);
452 d->strokeHandler->types.reset();
453 d->strokeHandler->pts.reset();
455 // Some engines might decide to optimize for the non-shape hint later on...
456 uint flags = QVectorPath::WindingFill;
458 if (path.elementCount() > 2)
459 flags |= QVectorPath::NonConvexShapeMask;
461 if (d->stroker.capStyle() == Qt::RoundCap || d->stroker.joinStyle() == Qt::RoundJoin)
462 flags |= QVectorPath::CurvedShapeMask;
464 // ### Perspective Xforms are currently not supported...
465 if (!pen.isCosmetic()) {
466 // We include cosmetic pens in this case to avoid having to
467 // change the current transform. Normal transformed,
468 // non-cosmetic pens will be transformed as part of fill
469 // later, so they are also covered here..
470 d->activeStroker->setCurveThresholdFromTransform(state()->matrix);
471 d->activeStroker->begin(d->strokeHandler);
473 while (points < lastPoint) {
475 case QPainterPath::MoveToElement:
476 d->activeStroker->moveTo(points[0], points[1]);
480 case QPainterPath::LineToElement:
481 d->activeStroker->lineTo(points[0], points[1]);
485 case QPainterPath::CurveToElement:
486 d->activeStroker->cubicTo(points[0], points[1],
487 points[2], points[3],
488 points[4], points[5]);
491 flags |= QVectorPath::CurvedShapeMask;
497 if (path.hasImplicitClose())
498 d->activeStroker->lineTo(path.points()[0], path.points()[1]);
501 d->activeStroker->moveTo(points[0], points[1]);
503 while (points < lastPoint) {
504 d->activeStroker->lineTo(points[0], points[1]);
507 if (path.hasImplicitClose())
508 d->activeStroker->lineTo(path.points()[0], path.points()[1]);
510 d->activeStroker->end();
512 if (!d->strokeHandler->types.size()) // an empty path...
515 QVectorPath strokePath(d->strokeHandler->pts.data(),
516 d->strokeHandler->types.size(),
517 d->strokeHandler->types.data(),
519 fill(strokePath, pen.brush());
521 // For cosmetic pens we need a bit of trickery... We to process xform the input points
522 if (state()->matrix.type() >= QTransform::TxProject) {
523 QPainterPath painterPath = state()->matrix.map(path.convertToPainterPath());
524 d->activeStroker->strokePath(painterPath, d->strokeHandler, QTransform());
526 d->activeStroker->setCurveThresholdFromTransform(QTransform());
527 d->activeStroker->begin(d->strokeHandler);
529 while (points < lastPoint) {
531 case QPainterPath::MoveToElement: {
532 QPointF pt = (*(QPointF *) points) * state()->matrix;
533 d->activeStroker->moveTo(pt.x(), pt.y());
538 case QPainterPath::LineToElement: {
539 QPointF pt = (*(QPointF *) points) * state()->matrix;
540 d->activeStroker->lineTo(pt.x(), pt.y());
545 case QPainterPath::CurveToElement: {
546 QPointF c1 = ((QPointF *) points)[0] * state()->matrix;
547 QPointF c2 = ((QPointF *) points)[1] * state()->matrix;
548 QPointF e = ((QPointF *) points)[2] * state()->matrix;
549 d->activeStroker->cubicTo(c1.x(), c1.y(), c2.x(), c2.y(), e.x(), e.y());
552 flags |= QVectorPath::CurvedShapeMask;
559 if (path.hasImplicitClose()) {
560 QPointF pt = * ((QPointF *) path.points()) * state()->matrix;
561 d->activeStroker->lineTo(pt.x(), pt.y());
565 QPointF p = ((QPointF *)points)[0] * state()->matrix;
566 d->activeStroker->moveTo(p.x(), p.y());
568 while (points < lastPoint) {
569 QPointF p = ((QPointF *)points)[0] * state()->matrix;
570 d->activeStroker->lineTo(p.x(), p.y());
573 if (path.hasImplicitClose())
574 d->activeStroker->lineTo(p.x(), p.y());
576 d->activeStroker->end();
579 QVectorPath strokePath(d->strokeHandler->pts.data(),
580 d->strokeHandler->types.size(),
581 d->strokeHandler->types.data(),
584 QTransform xform = state()->matrix;
585 state()->matrix = QTransform();
588 QBrush brush = pen.brush();
589 if (qbrush_style(brush) != Qt::SolidPattern)
590 brush.setTransform(brush.transform() * xform);
592 fill(strokePath, brush);
594 state()->matrix = xform;
599 void QPaintEngineEx::draw(const QVectorPath &path)
601 const QBrush &brush = state()->brush;
602 if (qbrush_style(brush) != Qt::NoBrush)
605 const QPen &pen = state()->pen;
606 if (qpen_style(pen) != Qt::NoPen && qbrush_style(qpen_brush(pen)) != Qt::NoBrush)
611 void QPaintEngineEx::clip(const QRect &r, Qt::ClipOperation op)
613 qreal right = r.x() + r.width();
614 qreal bottom = r.y() + r.height();
615 qreal pts[] = { qreal(r.x()), qreal(r.y()),
618 qreal(r.x()), bottom,
619 qreal(r.x()), qreal(r.y()) };
620 QVectorPath vp(pts, 5, 0, QVectorPath::RectangleHint);
624 void QPaintEngineEx::clip(const QRegion ®ion, Qt::ClipOperation op)
626 if (region.rectCount() == 1)
627 clip(region.boundingRect(), op);
629 QVector<QRect> rects = region.rects();
630 if (rects.size() <= 32) {
633 for (QVector<QRect>::const_iterator i = rects.constBegin(); i != rects.constEnd(); ++i) {
636 qreal x2 = i->x() + i->width();
637 qreal y2 = i->y() + i->height();
651 QVectorPath vp(pts, rects.size() * 4, qpaintengineex_rect4_types_32);
654 QVarLengthArray<qreal> pts(rects.size() * 2 * 4);
655 QVarLengthArray<QPainterPath::ElementType> types(rects.size() * 4);
659 for (QVector<QRect>::const_iterator i = rects.constBegin(); i != rects.constEnd(); ++i) {
662 qreal x2 = i->x() + i->width();
663 qreal y2 = i->y() + i->height();
677 types[tpos++] = QPainterPath::MoveToElement;
678 types[tpos++] = QPainterPath::LineToElement;
679 types[tpos++] = QPainterPath::LineToElement;
680 types[tpos++] = QPainterPath::LineToElement;
683 QVectorPath vp(pts.data(), rects.size() * 4, types.data());
689 void QPaintEngineEx::clip(const QPainterPath &path, Qt::ClipOperation op)
691 if (path.isEmpty()) {
692 QVectorPath vp(0, 0);
695 clip(qtVectorPathForPath(path), op);
699 void QPaintEngineEx::fillRect(const QRectF &r, const QBrush &brush)
701 qreal pts[] = { r.x(), r.y(), r.x() + r.width(), r.y(),
702 r.x() + r.width(), r.y() + r.height(), r.x(), r.y() + r.height() };
703 QVectorPath vp(pts, 4, 0, QVectorPath::RectangleHint);
707 void QPaintEngineEx::fillRect(const QRectF &r, const QColor &color)
709 fillRect(r, QBrush(color));
712 void QPaintEngineEx::drawRects(const QRect *rects, int rectCount)
714 for (int i=0; i<rectCount; ++i) {
715 const QRect &r = rects[i];
716 // ### Is there a one off here?
717 qreal right = r.x() + r.width();
718 qreal bottom = r.y() + r.height();
719 qreal pts[] = { qreal(r.x()), qreal(r.y()),
722 qreal(r.x()), bottom,
723 qreal(r.x()), qreal(r.y()) };
724 QVectorPath vp(pts, 5, 0, QVectorPath::RectangleHint);
729 void QPaintEngineEx::drawRects(const QRectF *rects, int rectCount)
731 for (int i=0; i<rectCount; ++i) {
732 const QRectF &r = rects[i];
733 qreal right = r.x() + r.width();
734 qreal bottom = r.y() + r.height();
735 qreal pts[] = { r.x(), r.y(),
740 QVectorPath vp(pts, 5, 0, QVectorPath::RectangleHint);
746 void QPaintEngineEx::drawRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius,
749 qreal x1 = rect.left();
750 qreal x2 = rect.right();
751 qreal y1 = rect.top();
752 qreal y2 = rect.bottom();
754 if (mode == Qt::RelativeSize) {
755 xRadius = xRadius * rect.width() / 200.;
756 yRadius = yRadius * rect.height() / 200.;
759 xRadius = qMin(xRadius, rect.width() / 2);
760 yRadius = qMin(yRadius, rect.height() / 2);
763 x1 + xRadius, y1, // MoveTo
764 x2 - xRadius, y1, // LineTo
765 x2 - (1 - KAPPA) * xRadius, y1, // CurveTo
766 x2, y1 + (1 - KAPPA) * yRadius,
768 x2, y2 - yRadius, // LineTo
769 x2, y2 - (1 - KAPPA) * yRadius, // CurveTo
770 x2 - (1 - KAPPA) * xRadius, y2,
772 x1 + xRadius, y2, // LineTo
773 x1 + (1 - KAPPA) * xRadius, y2, // CurveTo
774 x1, y2 - (1 - KAPPA) * yRadius,
776 x1, y1 + yRadius, // LineTo
777 x1, y1 + (1 - KAPPA) * yRadius, // CurveTo
778 x1 + (1 - KAPPA) * xRadius, y1,
782 QVectorPath path(pts, 17, qpaintengineex_roundedrect_types, QVectorPath::RoundedRectHint);
788 void QPaintEngineEx::drawLines(const QLine *lines, int lineCount)
790 int elementCount = lineCount << 1;
791 while (elementCount > 0) {
792 int count = qMin(elementCount, 32);
795 int count2 = count<<1;
796 for (int i=0; i<count2; ++i)
797 pts[i] = ((int *) lines)[i];
799 QVectorPath path(pts, count, qpaintengineex_line_types_16, QVectorPath::LinesHint);
800 stroke(path, state()->pen);
807 void QPaintEngineEx::drawLines(const QLineF *lines, int lineCount)
809 int elementCount = lineCount << 1;
810 while (elementCount > 0) {
811 int count = qMin(elementCount, 32);
813 QVectorPath path((qreal *) lines, count, qpaintengineex_line_types_16,
814 QVectorPath::LinesHint);
815 stroke(path, state()->pen);
822 void QPaintEngineEx::drawEllipse(const QRectF &r)
824 qreal pts[26]; // QPointF[13] without constructors...
832 x.points[0] = qt_curves_for_arc(r, 0, -360, x.points + 1, &point_count);
833 QVectorPath vp((qreal *) pts, point_count + 1, qpaintengineex_ellipse_types, QVectorPath::EllipseHint);
837 void QPaintEngineEx::drawEllipse(const QRect &r)
839 drawEllipse(QRectF(r));
842 void QPaintEngineEx::drawPath(const QPainterPath &path)
845 draw(qtVectorPathForPath(path));
849 void QPaintEngineEx::drawPoints(const QPointF *points, int pointCount)
851 QPen pen = state()->pen;
852 if (pen.capStyle() == Qt::FlatCap)
853 pen.setCapStyle(Qt::SquareCap);
855 if (pen.brush().isOpaque()) {
856 while (pointCount > 0) {
857 int count = qMin(pointCount, 16);
860 for (int i=0; i<count; ++i) {
861 pts[++oset] = points[i].x();
862 pts[++oset] = points[i].y();
863 pts[++oset] = points[i].x() + 1/63.;
864 pts[++oset] = points[i].y();
866 QVectorPath path(pts, count * 2, qpaintengineex_line_types_16, QVectorPath::LinesHint);
872 for (int i=0; i<pointCount; ++i) {
873 qreal pts[] = { points[i].x(), points[i].y(), points[i].x() + qreal(1/63.), points[i].y() };
874 QVectorPath path(pts, 2, 0);
880 void QPaintEngineEx::drawPoints(const QPoint *points, int pointCount)
882 QPen pen = state()->pen;
883 if (pen.capStyle() == Qt::FlatCap)
884 pen.setCapStyle(Qt::SquareCap);
886 if (pen.brush().isOpaque()) {
887 while (pointCount > 0) {
888 int count = qMin(pointCount, 16);
891 for (int i=0; i<count; ++i) {
892 pts[++oset] = points[i].x();
893 pts[++oset] = points[i].y();
894 pts[++oset] = points[i].x() + 1/63.;
895 pts[++oset] = points[i].y();
897 QVectorPath path(pts, count * 2, qpaintengineex_line_types_16, QVectorPath::LinesHint);
903 for (int i=0; i<pointCount; ++i) {
904 qreal pts[] = { qreal(points[i].x()), qreal(points[i].y()),
905 qreal(points[i].x() +1/63.), qreal(points[i].y()) };
906 QVectorPath path(pts, 2, 0);
913 void QPaintEngineEx::drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode)
915 QVectorPath path((qreal *) points, pointCount, 0, QVectorPath::polygonFlags(mode));
917 if (mode == PolylineMode)
918 stroke(path, state()->pen);
923 void QPaintEngineEx::drawPolygon(const QPoint *points, int pointCount, PolygonDrawMode mode)
925 int count = pointCount<<1;
926 QVarLengthArray<qreal> pts(count);
928 for (int i=0; i<count; ++i)
929 pts[i] = ((int *) points)[i];
931 QVectorPath path(pts.data(), pointCount, 0, QVectorPath::polygonFlags(mode));
933 if (mode == PolylineMode)
934 stroke(path, state()->pen);
940 void QPaintEngineEx::drawPixmap(const QPointF &pos, const QPixmap &pm)
942 drawPixmap(QRectF(pos, pm.size()), pm, pm.rect());
945 void QPaintEngineEx::drawImage(const QPointF &pos, const QImage &image)
947 drawImage(QRectF(pos, image.size()), image, image.rect());
950 void QPaintEngineEx::drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPointF &s)
952 QBrush brush(state()->pen.color(), pixmap);
953 QTransform xform = QTransform::fromTranslate(r.x() - s.x(), r.y() - s.y());
954 brush.setTransform(xform);
956 qreal pts[] = { r.x(), r.y(),
957 r.x() + r.width(), r.y(),
958 r.x() + r.width(), r.y() + r.height(),
959 r.x(), r.y() + r.height() };
961 QVectorPath path(pts, 4, 0, QVectorPath::RectangleHint);
965 void QPaintEngineEx::drawPixmapFragments(const QPainter::PixmapFragment *fragments, int fragmentCount,
966 const QPixmap &pixmap, QPainter::PixmapFragmentHints /*hints*/)
971 qreal oldOpacity = state()->opacity;
972 QTransform oldTransform = state()->matrix;
974 for (int i = 0; i < fragmentCount; ++i) {
975 QTransform transform = oldTransform;
976 transform.translate(fragments[i].x, fragments[i].y);
977 transform.rotate(fragments[i].rotation);
978 state()->opacity = oldOpacity * fragments[i].opacity;
979 state()->matrix = transform;
983 qreal w = fragments[i].scaleX * fragments[i].width;
984 qreal h = fragments[i].scaleY * fragments[i].height;
985 QRectF sourceRect(fragments[i].sourceLeft, fragments[i].sourceTop,
986 fragments[i].width, fragments[i].height);
987 drawPixmap(QRectF(-0.5 * w, -0.5 * h, w, h), pixmap, sourceRect);
990 state()->opacity = oldOpacity;
991 state()->matrix = oldTransform;
996 void QPaintEngineEx::setState(QPainterState *s)
998 QPaintEngine::state = s;
1002 void QPaintEngineEx::updateState(const QPaintEngineState &)
1007 Q_GUI_EXPORT QPainterPath qt_painterPathFromVectorPath(const QVectorPath &path)
1009 const qreal *points = path.points();
1010 const QPainterPath::ElementType *types = path.elements();
1015 for (int i=0; i<path.elementCount(); ++i) {
1017 case QPainterPath::MoveToElement:
1018 p.moveTo(QPointF(points[id], points[id+1]));
1021 case QPainterPath::LineToElement:
1022 p.lineTo(QPointF(points[id], points[id+1]));
1025 case QPainterPath::CurveToElement: {
1026 QPointF p1(points[id], points[id+1]);
1027 QPointF p2(points[id+2], points[id+3]);
1028 QPointF p3(points[id+4], points[id+5]);
1029 p.cubicTo(p1, p2, p3);
1033 case QPainterPath::CurveToDataElement:
1039 p.moveTo(QPointF(points[0], points[1]));
1041 for (int i=1; i<path.elementCount(); ++i) {
1042 p.lineTo(QPointF(points[id], points[id+1]));
1046 if (path.hints() & QVectorPath::WindingFill)
1047 p.setFillRule(Qt::WindingFill);
1052 void QPaintEngineEx::drawStaticTextItem(QStaticTextItem *staticTextItem)
1055 path.setFillRule(Qt::WindingFill);
1057 if (staticTextItem->numGlyphs == 0)
1060 QFontEngine *fontEngine = staticTextItem->fontEngine();
1061 fontEngine->addGlyphsToPath(staticTextItem->glyphs, staticTextItem->glyphPositions,
1062 staticTextItem->numGlyphs, &path, 0);
1063 if (!path.isEmpty()) {
1064 QPainterState *s = state();
1065 QPainter::RenderHints oldHints = s->renderHints;
1066 bool changedHints = false;
1067 if (bool(oldHints & QPainter::TextAntialiasing)
1068 && !bool(fontEngine->fontDef.styleStrategy & QFont::NoAntialias)
1069 && !bool(oldHints & QPainter::Antialiasing)) {
1070 s->renderHints |= QPainter::Antialiasing;
1071 renderHintsChanged();
1072 changedHints = true;
1075 fill(qtVectorPathForPath(path), s->pen.brush());
1078 s->renderHints = oldHints;
1079 renderHintsChanged();
1084 bool QPaintEngineEx::supportsTransformations(QFontEngine *fontEngine, const QTransform &m) const
1086 Q_UNUSED(fontEngine);
1094 bool QPaintEngineEx::shouldDrawCachedGlyphs(QFontEngine *fontEngine, const QTransform &m) const
1096 qreal pixelSize = fontEngine->fontDef.pixelSize;
1097 return (pixelSize * pixelSize * qAbs(m.determinant())) <
1098 QT_MAX_CACHED_GLYPH_SIZE * QT_MAX_CACHED_GLYPH_SIZE;