source: trunk/src/gui/painting/qpaintengineex.cpp@ 574

Last change on this file since 574 was 561, checked in by Dmitry A. Kuminov, 16 years ago

trunk: Merged in qt 4.6.1 sources.

File size: 34.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
6**
7** This file is part of the QtGui module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
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.
23**
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.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
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
48#include <qvarlengtharray.h>
49#include <qdebug.h>
50
51
52QT_BEGIN_NAMESPACE
53
54/*******************************************************************************
55 *
56 * class QVectorPath
57 *
58 */
59QVectorPath::~QVectorPath()
60{
61 if (m_hints & ShouldUseCacheHint) {
62 CacheEntry *e = m_cache;
63 while (e) {
64 if (e->data)
65 e->cleanup(e->engine, e->data);
66 CacheEntry *n = e->next;
67 delete e;
68 e = n;
69 }
70 }
71}
72
73
74QRectF QVectorPath::controlPointRect() const
75{
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));
78
79 if (m_count == 0) {
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));
83 }
84 Q_ASSERT(m_points && m_count > 0);
85
86 const qreal *pts = m_points;
87 m_cp_rect.x1 = m_cp_rect.x2 = *pts;
88 ++pts;
89 m_cp_rect.y1 = m_cp_rect.y2 = *pts;
90 ++pts;
91
92 const qreal *epts = m_points + (m_count << 1);
93 while (pts < epts) {
94 qreal x = *pts;
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;
97 ++pts;
98
99 qreal y = *pts;
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;
102 ++pts;
103 }
104
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));
107}
108
109
110QVectorPath::CacheEntry *QVectorPath::addCacheData(QPaintEngineEx *engine, void *data,
111 qvectorpath_cache_cleanup cleanup) const{
112 Q_ASSERT(!lookupCacheData(engine));
113 if ((m_hints & IsCachedHint) == 0) {
114 m_cache = 0;
115 m_hints |= IsCachedHint;
116 }
117 CacheEntry *e = new CacheEntry;
118 e->engine = engine;
119 e->data = data;
120 e->cleanup = cleanup;
121 e->next = m_cache;
122 m_cache = e;
123 return m_cache;
124}
125
126
127const QVectorPath &qtVectorPathForPath(const QPainterPath &path)
128{
129 Q_ASSERT(path.d_func());
130 return path.d_func()->vectorPath();
131}
132
133#ifndef QT_NO_DEBUG_STREAM
134QDebug Q_GUI_EXPORT &operator<<(QDebug &s, const QVectorPath &path)
135{
136 QRectF rf = path.controlPointRect();
137 s << "QVectorPath(size:" << path.elementCount()
138 << " hints:" << hex << path.hints()
139 << rf << ')';
140 return s;
141}
142#endif
143
144/*******************************************************************************
145 *
146 * class QPaintEngineExPrivate:
147 *
148 */
149
150
151struct StrokeHandler {
152 QDataBuffer<qreal> pts;
153 QDataBuffer<QPainterPath::ElementType> types;
154};
155
156
157QPaintEngineExPrivate::QPaintEngineExPrivate()
158 : dasher(&stroker),
159 strokeHandler(0),
160 activeStroker(0),
161 strokerPen(Qt::NoPen)
162{
163}
164
165
166QPaintEngineExPrivate::~QPaintEngineExPrivate()
167{
168 delete strokeHandler;
169}
170
171
172void QPaintEngineExPrivate::replayClipOperations()
173{
174 Q_Q(QPaintEngineEx);
175
176 QPainter *p = q->painter();
177 if (!p || !p->d_ptr)
178 return;
179
180 QList<QPainterClipInfo> clipInfo = p->d_ptr->state->clipInfo;
181
182 QTransform transform = q->state()->matrix;
183
184 for (int i = 0; i < clipInfo.size(); ++i) {
185 const QPainterClipInfo &info = clipInfo.at(i);
186
187 if (info.matrix != q->state()->matrix) {
188 q->state()->matrix = info.matrix;
189 q->transformChanged();
190 }
191
192 switch (info.clipType) {
193 case QPainterClipInfo::RegionClip:
194 q->clip(info.region, info.operation);
195 break;
196 case QPainterClipInfo::PathClip:
197 q->clip(info.path, info.operation);
198 break;
199 case QPainterClipInfo::RectClip:
200 q->clip(info.rect, info.operation);
201 break;
202 case QPainterClipInfo::RectFClip: {
203 qreal right = info.rectf.x() + info.rectf.width();
204 qreal bottom = info.rectf.y() + info.rectf.height();
205 qreal pts[] = { info.rectf.x(), info.rectf.y(),
206 right, info.rectf.y(),
207 right, bottom,
208 info.rectf.x(), bottom };
209 QVectorPath vp(pts, 4, 0, QVectorPath::RectangleHint);
210 q->clip(vp, info.operation);
211 break;
212 }
213 }
214 }
215
216 if (transform != q->state()->matrix) {
217 q->state()->matrix = transform;
218 q->transformChanged();
219 }
220}
221
222
223bool QPaintEngineExPrivate::hasClipOperations() const
224{
225 Q_Q(const QPaintEngineEx);
226
227 QPainter *p = q->painter();
228 if (!p || !p->d_ptr)
229 return false;
230
231 QList<QPainterClipInfo> clipInfo = p->d_ptr->state->clipInfo;
232
233 return !clipInfo.isEmpty();
234}
235
236/*******************************************************************************
237 *
238 * class QPaintEngineEx:
239 *
240 */
241
242static QPainterPath::ElementType qpaintengineex_ellipse_types[] = {
243 QPainterPath::MoveToElement,
244 QPainterPath::CurveToElement,
245 QPainterPath::CurveToDataElement,
246 QPainterPath::CurveToDataElement,
247
248 QPainterPath::CurveToElement,
249 QPainterPath::CurveToDataElement,
250 QPainterPath::CurveToDataElement,
251
252 QPainterPath::CurveToElement,
253 QPainterPath::CurveToDataElement,
254 QPainterPath::CurveToDataElement,
255
256 QPainterPath::CurveToElement,
257 QPainterPath::CurveToDataElement,
258 QPainterPath::CurveToDataElement
259};
260
261static QPainterPath::ElementType qpaintengineex_line_types_16[] = {
262 QPainterPath::MoveToElement, QPainterPath::LineToElement,
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};
279
280static QPainterPath::ElementType qpaintengineex_rect4_types_32[] = {
281 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 1
282 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 2
283 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 3
284 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 4
285 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 5
286 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 6
287 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 7
288 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 8
289 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 9
290 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 10
291 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 11
292 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 12
293 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 13
294 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 14
295 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 15
296 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 16
297 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 17
298 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 18
299 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 19
300 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 20
301 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 21
302 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 22
303 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 23
304 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 24
305 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 25
306 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 26
307 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 27
308 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 28
309 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 29
310 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 30
311 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 31
312 QPainterPath::MoveToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, QPainterPath::LineToElement, // 32
313};
314
315
316static QPainterPath::ElementType qpaintengineex_roundedrect_types[] = {
317 QPainterPath::MoveToElement,
318 QPainterPath::LineToElement,
319 QPainterPath::CurveToElement,
320 QPainterPath::CurveToDataElement,
321 QPainterPath::CurveToDataElement,
322 QPainterPath::LineToElement,
323 QPainterPath::CurveToElement,
324 QPainterPath::CurveToDataElement,
325 QPainterPath::CurveToDataElement,
326 QPainterPath::LineToElement,
327 QPainterPath::CurveToElement,
328 QPainterPath::CurveToDataElement,
329 QPainterPath::CurveToDataElement,
330 QPainterPath::LineToElement,
331 QPainterPath::CurveToElement,
332 QPainterPath::CurveToDataElement,
333 QPainterPath::CurveToDataElement
334};
335
336
337
338static void qpaintengineex_moveTo(qreal x, qreal y, void *data) {
339 ((StrokeHandler *) data)->pts.add(x);
340 ((StrokeHandler *) data)->pts.add(y);
341 ((StrokeHandler *) data)->types.add(QPainterPath::MoveToElement);
342}
343
344static void qpaintengineex_lineTo(qreal x, qreal y, void *data) {
345 ((StrokeHandler *) data)->pts.add(x);
346 ((StrokeHandler *) data)->pts.add(y);
347 ((StrokeHandler *) data)->types.add(QPainterPath::LineToElement);
348}
349
350static void qpaintengineex_cubicTo(qreal c1x, qreal c1y, qreal c2x, qreal c2y, qreal ex, qreal ey, void *data) {
351 ((StrokeHandler *) data)->pts.add(c1x);
352 ((StrokeHandler *) data)->pts.add(c1y);
353 ((StrokeHandler *) data)->types.add(QPainterPath::CurveToElement);
354
355 ((StrokeHandler *) data)->pts.add(c2x);
356 ((StrokeHandler *) data)->pts.add(c2y);
357 ((StrokeHandler *) data)->types.add(QPainterPath::CurveToDataElement);
358
359 ((StrokeHandler *) data)->pts.add(ex);
360 ((StrokeHandler *) data)->pts.add(ey);
361 ((StrokeHandler *) data)->types.add(QPainterPath::CurveToDataElement);
362}
363
364QPaintEngineEx::QPaintEngineEx()
365 : QPaintEngine(*new QPaintEngineExPrivate, AllFeatures)
366{
367 extended = true;
368}
369
370QPaintEngineEx::QPaintEngineEx(QPaintEngineExPrivate &data)
371 : QPaintEngine(data, AllFeatures)
372{
373 extended = true;
374}
375
376QPainterState *QPaintEngineEx::createState(QPainterState *orig) const
377{
378 if (!orig)
379 return new QPainterState;
380 return new QPainterState(orig);
381}
382
383extern bool qt_scaleForTransform(const QTransform &transform, qreal *scale); // qtransform.cpp
384
385void QPaintEngineEx::stroke(const QVectorPath &path, const QPen &pen)
386{
387#ifdef QT_DEBUG_DRAW
388 qDebug() << "QPaintEngineEx::stroke()" << pen;
389#endif
390
391 Q_D(QPaintEngineEx);
392
393 if (path.isEmpty())
394 return;
395
396 if (!d->strokeHandler) {
397 d->strokeHandler = new StrokeHandler;
398 d->stroker.setMoveToHook(qpaintengineex_moveTo);
399 d->stroker.setLineToHook(qpaintengineex_lineTo);
400 d->stroker.setCubicToHook(qpaintengineex_cubicTo);
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 = 0;
419 } else {
420 // ### re-enable...
421 if (pen.isCosmetic()) {
422 d->dasher.setClipRect(d->exDeviceRect);
423 } else {
424 QRectF clipRect = state()->matrix.inverted().mapRect(QRectF(d->exDeviceRect));
425 d->dasher.setClipRect(clipRect);
426 }
427 d->dasher.setDashPattern(pen.dashPattern());
428 d->dasher.setDashOffset(pen.dashOffset());
429 d->activeStroker = &d->dasher;
430 }
431 }
432
433 if (!d->activeStroker) {
434 return;
435 }
436
437 const QPainterPath::ElementType *types = path.elements();
438 const qreal *points = path.points();
439 int pointCount = path.elementCount();
440
441 const qreal *lastPoint = points + (pointCount<<1);
442
443 d->strokeHandler->types.reset();
444 d->strokeHandler->pts.reset();
445
446 // Some engines might decide to optimize for the non-shape hint later on...
447 uint flags = QVectorPath::WindingFill;
448
449 if (path.elementCount() > 2)
450 flags |= QVectorPath::NonConvexShapeMask;
451
452 if (d->stroker.capStyle() == Qt::RoundCap || d->stroker.joinStyle() == Qt::RoundJoin)
453 flags |= QVectorPath::CurvedShapeMask;
454
455 // ### Perspective Xforms are currently not supported...
456 if (!pen.isCosmetic()) {
457 // We include cosmetic pens in this case to avoid having to
458 // change the current transform. Normal transformed,
459 // non-cosmetic pens will be transformed as part of fill
460 // later, so they are also covered here..
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 ++types;
494 while (points < lastPoint) {
495 d->activeStroker->lineTo(points[0], points[1]);
496 points += 2;
497 ++types;
498 }
499 if (path.hasImplicitClose())
500 d->activeStroker->lineTo(path.points()[0], path.points()[1]);
501 }
502 d->activeStroker->end();
503
504 if (!d->strokeHandler->types.size()) // an empty path...
505 return;
506
507 QVectorPath strokePath(d->strokeHandler->pts.data(),
508 d->strokeHandler->types.size(),
509 d->strokeHandler->types.data(),
510 flags);
511 fill(strokePath, pen.brush());
512 } else {
513 // For cosmetic pens we need a bit of trickery... We to process xform the input points
514 if (state()->matrix.type() >= QTransform::TxProject) {
515 QPainterPath painterPath = state()->matrix.map(path.convertToPainterPath());
516 d->activeStroker->strokePath(painterPath, d->strokeHandler, QTransform());
517 } else {
518 d->activeStroker->begin(d->strokeHandler);
519 if (types) {
520 while (points < lastPoint) {
521 switch (*types) {
522 case QPainterPath::MoveToElement: {
523 QPointF pt = (*(QPointF *) points) * state()->matrix;
524 d->activeStroker->moveTo(pt.x(), pt.y());
525 points += 2;
526 ++types;
527 break;
528 }
529 case QPainterPath::LineToElement: {
530 QPointF pt = (*(QPointF *) points) * state()->matrix;
531 d->activeStroker->lineTo(pt.x(), pt.y());
532 points += 2;
533 ++types;
534 break;
535 }
536 case QPainterPath::CurveToElement: {
537 QPointF c1 = ((QPointF *) points)[0] * state()->matrix;
538 QPointF c2 = ((QPointF *) points)[1] * state()->matrix;
539 QPointF e = ((QPointF *) points)[2] * state()->matrix;
540 d->activeStroker->cubicTo(c1.x(), c1.y(), c2.x(), c2.y(), e.x(), e.y());
541 points += 6;
542 types += 3;
543 flags |= QVectorPath::CurvedShapeMask;
544 break;
545 }
546 default:
547 break;
548 }
549 }
550 if (path.hasImplicitClose()) {
551 QPointF pt = * ((QPointF *) path.points()) * state()->matrix;
552 d->activeStroker->lineTo(pt.x(), pt.y());
553 }
554
555 } else {
556 QPointF p = ((QPointF *)points)[0] * state()->matrix;
557 d->activeStroker->moveTo(p.x(), p.y());
558 points += 2;
559 ++types;
560 while (points < lastPoint) {
561 QPointF p = ((QPointF *)points)[0] * state()->matrix;
562 d->activeStroker->lineTo(p.x(), p.y());
563 points += 2;
564 ++types;
565 }
566 if (path.hasImplicitClose())
567 d->activeStroker->lineTo(p.x(), p.y());
568 }
569 d->activeStroker->end();
570 }
571
572 QVectorPath strokePath(d->strokeHandler->pts.data(),
573 d->strokeHandler->types.size(),
574 d->strokeHandler->types.data(),
575 flags);
576
577 QTransform xform = state()->matrix;
578 state()->matrix = QTransform();
579 transformChanged();
580
581 QBrush brush = pen.brush();
582 if (qbrush_style(brush) != Qt::SolidPattern)
583 brush.setTransform(brush.transform() * xform);
584
585 fill(strokePath, brush);
586
587 state()->matrix = xform;
588 transformChanged();
589 }
590}
591
592void QPaintEngineEx::draw(const QVectorPath &path)
593{
594 const QBrush &brush = state()->brush;
595 if (qbrush_style(brush) != Qt::NoBrush)
596 fill(path, brush);
597
598 const QPen &pen = state()->pen;
599 if (qpen_style(pen) != Qt::NoPen && qbrush_style(qpen_brush(pen)) != Qt::NoBrush)
600 stroke(path, pen);
601}
602
603
604void QPaintEngineEx::clip(const QRect &r, Qt::ClipOperation op)
605{
606 qreal right = r.x() + r.width();
607 qreal bottom = r.y() + r.height();
608 qreal pts[] = { r.x(), r.y(),
609 right, r.y(),
610 right, bottom,
611 r.x(), bottom,
612 r.x(), r.y() };
613 QVectorPath vp(pts, 5, 0, QVectorPath::RectangleHint);
614 clip(vp, op);
615}
616
617void QPaintEngineEx::clip(const QRegion &region, Qt::ClipOperation op)
618{
619 if (region.rectCount() == 1)
620 clip(region.boundingRect(), op);
621
622 QVector<QRect> rects = region.rects();
623 if (rects.size() <= 32) {
624 qreal pts[2*32*4];
625 int pos = 0;
626 for (QVector<QRect>::const_iterator i = rects.constBegin(); i != rects.constEnd(); ++i) {
627 qreal x1 = i->x();
628 qreal y1 = i->y();
629 qreal x2 = i->x() + i->width();
630 qreal y2 = i->y() + i->height();
631
632 pts[pos++] = x1;
633 pts[pos++] = y1;
634
635 pts[pos++] = x2;
636 pts[pos++] = y1;
637
638 pts[pos++] = x2;
639 pts[pos++] = y2;
640
641 pts[pos++] = x1;
642 pts[pos++] = y2;
643 }
644 QVectorPath vp(pts, rects.size() * 4, qpaintengineex_rect4_types_32);
645 clip(vp, op);
646 } else {
647 QVarLengthArray<qreal> pts(rects.size() * 2 * 4);
648 QVarLengthArray<QPainterPath::ElementType> types(rects.size() * 4);
649 int ppos = 0;
650 int tpos = 0;
651
652 for (QVector<QRect>::const_iterator i = rects.constBegin(); i != rects.constEnd(); ++i) {
653 qreal x1 = i->x();
654 qreal y1 = i->y();
655 qreal x2 = i->x() + i->width();
656 qreal y2 = i->y() + i->height();
657
658 pts[ppos++] = x1;
659 pts[ppos++] = y1;
660
661 pts[ppos++] = x2;
662 pts[ppos++] = y1;
663
664 pts[ppos++] = x2;
665 pts[ppos++] = y2;
666
667 pts[ppos++] = x1;
668 pts[ppos++] = y2;
669
670 types[tpos++] = QPainterPath::MoveToElement;
671 types[tpos++] = QPainterPath::LineToElement;
672 types[tpos++] = QPainterPath::LineToElement;
673 types[tpos++] = QPainterPath::LineToElement;
674 }
675
676 QVectorPath vp(pts.data(), rects.size() * 4, types.data());
677 clip(vp, op);
678 }
679
680}
681
682void QPaintEngineEx::clip(const QPainterPath &path, Qt::ClipOperation op)
683{
684 if (path.isEmpty()) {
685 QVectorPath vp(0, 0);
686 clip(vp, op);
687 } else {
688 clip(qtVectorPathForPath(path), op);
689 }
690}
691
692void QPaintEngineEx::fillRect(const QRectF &r, const QBrush &brush)
693{
694 qreal pts[] = { r.x(), r.y(), r.x() + r.width(), r.y(),
695 r.x() + r.width(), r.y() + r.height(), r.x(), r.y() + r.height() };
696 QVectorPath vp(pts, 4, 0, QVectorPath::RectangleHint);
697 fill(vp, brush);
698}
699
700void QPaintEngineEx::fillRect(const QRectF &r, const QColor &color)
701{
702 fillRect(r, QBrush(color));
703}
704
705void QPaintEngineEx::drawRects(const QRect *rects, int rectCount)
706{
707 for (int i=0; i<rectCount; ++i) {
708 const QRect &r = rects[i];
709 // ### Is there a one off here?
710 qreal right = r.x() + r.width();
711 qreal bottom = r.y() + r.height();
712 qreal pts[] = { r.x(), r.y(),
713 right, r.y(),
714 right, bottom,
715 r.x(), bottom,
716 r.x(), r.y() };
717 QVectorPath vp(pts, 5, 0, QVectorPath::RectangleHint);
718 draw(vp);
719 }
720}
721
722void QPaintEngineEx::drawRects(const QRectF *rects, int rectCount)
723{
724 for (int i=0; i<rectCount; ++i) {
725 const QRectF &r = rects[i];
726 qreal right = r.x() + r.width();
727 qreal bottom = r.y() + r.height();
728 qreal pts[] = { r.x(), r.y(),
729 right, r.y(),
730 right, bottom,
731 r.x(), bottom,
732 r.x(), r.y() };
733 QVectorPath vp(pts, 5, 0, QVectorPath::RectangleHint);
734 draw(vp);
735 }
736}
737
738
739void QPaintEngineEx::drawRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius,
740 Qt::SizeMode mode)
741{
742 qreal x1 = rect.left();
743 qreal x2 = rect.right();
744 qreal y1 = rect.top();
745 qreal y2 = rect.bottom();
746
747 if (mode == Qt::RelativeSize) {
748 xRadius = xRadius * rect.width() / 200.;
749 yRadius = yRadius * rect.height() / 200.;
750 }
751
752 xRadius = qMin(xRadius, rect.width() / 2);
753 yRadius = qMin(yRadius, rect.height() / 2);
754
755 qreal pts[] = {
756 x1 + xRadius, y1, // MoveTo
757 x2 - xRadius, y1, // LineTo
758 x2 - (1 - KAPPA) * xRadius, y1, // CurveTo
759 x2, y1 + (1 - KAPPA) * yRadius,
760 x2, y1 + yRadius,
761 x2, y2 - yRadius, // LineTo
762 x2, y2 - (1 - KAPPA) * yRadius, // CurveTo
763 x2 - (1 - KAPPA) * xRadius, y2,
764 x2 - xRadius, y2,
765 x1 + xRadius, y2, // LineTo
766 x1 + (1 - KAPPA) * xRadius, y2, // CurveTo
767 x1, y2 - (1 - KAPPA) * yRadius,
768 x1, y2 - yRadius,
769 x1, y1 + yRadius, // LineTo
770 x1, y1 + KAPPA * yRadius, // CurveTo
771 x1 + (1 - KAPPA) * xRadius, y1,
772 x1 + xRadius, y1
773 };
774
775 QVectorPath path(pts, 17, qpaintengineex_roundedrect_types, QVectorPath::RoundedRectHint);
776 draw(path);
777}
778
779
780
781void QPaintEngineEx::drawLines(const QLine *lines, int lineCount)
782{
783 int elementCount = lineCount << 1;
784 while (elementCount > 0) {
785 int count = qMin(elementCount, 32);
786
787 qreal pts[64];
788 int count2 = count<<1;
789#ifdef Q_WS_MAC
790 for (int i=0; i<count2; i+=2) {
791 pts[i] = ((int *) lines)[i+1];
792 pts[i+1] = ((int *) lines)[i];
793 }
794#else
795 for (int i=0; i<count2; ++i)
796 pts[i] = ((int *) lines)[i];
797#endif
798
799 QVectorPath path(pts, count, qpaintengineex_line_types_16, QVectorPath::LinesHint);
800 stroke(path, state()->pen);
801
802 elementCount -= 32;
803 lines += 16;
804 }
805}
806
807void QPaintEngineEx::drawLines(const QLineF *lines, int lineCount)
808{
809 int elementCount = lineCount << 1;
810 while (elementCount > 0) {
811 int count = qMin(elementCount, 32);
812
813 QVectorPath path((qreal *) lines, count, qpaintengineex_line_types_16,
814 QVectorPath::LinesHint);
815 stroke(path, state()->pen);
816
817 elementCount -= 32;
818 lines += 16;
819 }
820}
821
822void QPaintEngineEx::drawEllipse(const QRectF &r)
823{
824 qreal pts[26]; // QPointF[13] without constructors...
825 union {
826 qreal *ptr;
827 QPointF *points;
828 } x;
829 x.ptr = pts;
830
831 int point_count = 0;
832 x.points[0] = qt_curves_for_arc(r, 0, -360, x.points + 1, &point_count);
833 QVectorPath vp((qreal *) pts, 13, qpaintengineex_ellipse_types, QVectorPath::EllipseHint);
834 draw(vp);
835}
836
837void QPaintEngineEx::drawEllipse(const QRect &r)
838{
839 drawEllipse(QRectF(r));
840}
841
842void QPaintEngineEx::drawPath(const QPainterPath &path)
843{
844 if (!path.isEmpty())
845 draw(qtVectorPathForPath(path));
846}
847
848
849void QPaintEngineEx::drawPoints(const QPointF *points, int pointCount)
850{
851 QPen pen = state()->pen;
852 if (pen.capStyle() == Qt::FlatCap)
853 pen.setCapStyle(Qt::SquareCap);
854
855 if (pen.brush().isOpaque()) {
856 while (pointCount > 0) {
857 int count = qMin(pointCount, 16);
858 qreal pts[64];
859 int oset = -1;
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() + 0.001;
864 pts[++oset] = points[i].y();
865 }
866 QVectorPath path(pts, count * 2, qpaintengineex_line_types_16, QVectorPath::LinesHint);
867 stroke(path, pen);
868 pointCount -= 16;
869 points += 16;
870 }
871 } else {
872 for (int i=0; i<pointCount; ++i) {
873 qreal pts[] = { points[i].x(), points[i].y(), points[i].x() + 0.001, points[i].y() };
874 QVectorPath path(pts, 2, 0);
875 stroke(path, pen);
876 }
877 }
878}
879
880void QPaintEngineEx::drawPoints(const QPoint *points, int pointCount)
881{
882 QPen pen = state()->pen;
883 if (pen.capStyle() == Qt::FlatCap)
884 pen.setCapStyle(Qt::SquareCap);
885
886 if (pen.brush().isOpaque()) {
887 while (pointCount > 0) {
888 int count = qMin(pointCount, 16);
889 qreal pts[64];
890 int oset = -1;
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() + 0.001;
895 pts[++oset] = points[i].y();
896 }
897 QVectorPath path(pts, count * 2, qpaintengineex_line_types_16, QVectorPath::LinesHint);
898 stroke(path, pen);
899 pointCount -= 16;
900 points += 16;
901 }
902 } else {
903 for (int i=0; i<pointCount; ++i) {
904 qreal pts[] = { points[i].x(), points[i].y(), points[i].x() + 0.001, points[i].y() };
905 QVectorPath path(pts, 2, 0);
906 stroke(path, pen);
907 }
908 }
909}
910
911
912void QPaintEngineEx::drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode)
913{
914 QVectorPath path((qreal *) points, pointCount, 0, QVectorPath::polygonFlags(mode));
915
916 if (mode == PolylineMode)
917 stroke(path, state()->pen);
918 else
919 draw(path);
920}
921
922void QPaintEngineEx::drawPolygon(const QPoint *points, int pointCount, PolygonDrawMode mode)
923{
924 int count = pointCount<<1;
925 QVarLengthArray<qreal> pts(count);
926
927#ifdef Q_WS_MAC
928 for (int i=0; i<count; i+=2) {
929 pts[i] = ((int *) points)[i+1];
930 pts[i+1] = ((int *) points)[i];
931 }
932#else
933 for (int i=0; i<count; ++i)
934 pts[i] = ((int *) points)[i];
935#endif
936
937 QVectorPath path(pts.data(), pointCount, 0, QVectorPath::polygonFlags(mode));
938
939 if (mode == PolylineMode)
940 stroke(path, state()->pen);
941 else
942 draw(path);
943
944}
945
946void QPaintEngineEx::drawPixmap(const QPointF &pos, const QPixmap &pm)
947{
948 drawPixmap(QRectF(pos, pm.size()), pm, pm.rect());
949}
950
951void QPaintEngineEx::drawImage(const QPointF &pos, const QImage &image)
952{
953 drawImage(QRectF(pos, image.size()), image, image.rect());
954}
955
956void QPaintEngineEx::drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPointF &s)
957{
958 QBrush brush(state()->pen.color(), pixmap);
959 QTransform xform = QTransform::fromTranslate(r.x() - s.x(), r.y() - s.y());
960 brush.setTransform(xform);
961
962 qreal pts[] = { r.x(), r.y(),
963 r.x() + r.width(), r.y(),
964 r.x() + r.width(), r.y() + r.height(),
965 r.x(), r.y() + r.height() };
966
967 QVectorPath path(pts, 4, 0, QVectorPath::RectangleHint);
968 fill(path, brush);
969}
970
971void QPaintEngineEx::drawPixmaps(const QDrawPixmaps::Data *drawingData, int dataCount, const QPixmap &pixmap, QDrawPixmaps::DrawingHints /*hints*/)
972{
973 qreal oldOpacity = state()->opacity;
974 QTransform oldTransform = state()->matrix;
975
976 for (int i = 0; i < dataCount; ++i) {
977 QTransform transform = oldTransform;
978 transform.translate(drawingData[i].point.x(), drawingData[i].point.y());
979 transform.rotate(drawingData[i].rotation);
980 state()->opacity = oldOpacity * drawingData[i].opacity;
981 state()->matrix = transform;
982 opacityChanged();
983 transformChanged();
984
985 qreal w = drawingData[i].scaleX * drawingData[i].source.width();
986 qreal h = drawingData[i].scaleY * drawingData[i].source.height();
987 drawPixmap(QRectF(-0.5 * w, -0.5 * h, w, h), pixmap, drawingData[i].source);
988 }
989
990 state()->opacity = oldOpacity;
991 state()->matrix = oldTransform;
992 opacityChanged();
993 transformChanged();
994}
995
996void QPaintEngineEx::setState(QPainterState *s)
997{
998 QPaintEngine::state = s;
999}
1000
1001
1002void QPaintEngineEx::updateState(const QPaintEngineState &)
1003{
1004 // do nothing...
1005}
1006
1007QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.