source: trunk/src/gui/painting/qpaintengine_mac.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 63.0 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 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 <qbitmap.h>
43#include <qpaintdevice.h>
44#include <private/qpaintengine_mac_p.h>
45#include <qpainterpath.h>
46#include <qpixmapcache.h>
47#include <private/qpaintengine_raster_p.h>
48#include <private/qprintengine_mac_p.h>
49#include <qprinter.h>
50#include <qstack.h>
51#include <qtextcodec.h>
52#include <qwidget.h>
53#include <qvarlengtharray.h>
54#include <qdebug.h>
55#include <qcoreapplication.h>
56#include <qmath.h>
57
58#include <private/qfont_p.h>
59#include <private/qfontengine_p.h>
60#include <private/qnumeric_p.h>
61#include <private/qpainter_p.h>
62#include <private/qpainterpath_p.h>
63#include <private/qpixmap_mac_p.h>
64#include <private/qt_mac_p.h>
65#include <private/qtextengine_p.h>
66#include <private/qwidget_p.h>
67#include <private/qt_cocoa_helpers_mac_p.h>
68
69#include <string.h>
70
71QT_BEGIN_NAMESPACE
72
73extern int qt_antialiasing_threshold; // QApplication.cpp
74
75/*****************************************************************************
76 External functions
77 *****************************************************************************/
78extern CGImageRef qt_mac_create_imagemask(const QPixmap &px, const QRectF &sr); //qpixmap_mac.cpp
79extern QPoint qt_mac_posInWindow(const QWidget *w); //qwidget_mac.cpp
80extern OSWindowRef qt_mac_window_for(const QWidget *); //qwidget_mac.cpp
81extern CGContextRef qt_mac_cg_context(const QPaintDevice *); //qpaintdevice_mac.cpp
82extern void qt_mac_dispose_rgn(RgnHandle r); //qregion_mac.cpp
83extern QPixmap qt_pixmapForBrush(int, bool); //qbrush.cpp
84
85void qt_mac_clip_cg(CGContextRef hd, const QRegion &rgn, CGAffineTransform *orig_xform);
86
87
88//Implemented for qt_mac_p.h
89QMacCGContext::QMacCGContext(QPainter *p)
90{
91 QPaintEngine *pe = p->paintEngine();
92 if (pe->type() == QPaintEngine::MacPrinter)
93 pe = static_cast<QMacPrintEngine*>(pe)->paintEngine();
94 pe->syncState();
95 context = 0;
96 if(pe->type() == QPaintEngine::CoreGraphics)
97 context = static_cast<QCoreGraphicsPaintEngine*>(pe)->handle();
98
99 int devType = p->device()->devType();
100 if (pe->type() == QPaintEngine::Raster
101 && (devType == QInternal::Widget || devType == QInternal::Pixmap)) {
102
103 extern CGColorSpaceRef qt_mac_colorSpaceForDeviceType(const QPaintDevice *paintDevice);
104 CGColorSpaceRef colorspace = qt_mac_colorSpaceForDeviceType(pe->paintDevice());
105 uint flags = kCGImageAlphaPremultipliedFirst;
106#ifdef kCGBitmapByteOrder32Host //only needed because CGImage.h added symbols in the minor version
107 flags |= kCGBitmapByteOrder32Host;
108#endif
109 const QImage *image = (const QImage *) pe->paintDevice();
110
111 context = CGBitmapContextCreate((void *) image->bits(), image->width(), image->height(),
112 8, image->bytesPerLine(), colorspace, flags);
113
114 CGContextTranslateCTM(context, 0, image->height());
115 CGContextScaleCTM(context, 1, -1);
116
117 if (devType == QInternal::Widget) {
118 QRegion clip = p->paintEngine()->systemClip();
119 QTransform native = p->deviceTransform();
120 QTransform logical = p->combinedTransform();
121
122 if (p->hasClipping()) {
123 QRegion r = p->clipRegion();
124 r.translate(native.dx(), native.dy());
125 if (clip.isEmpty())
126 clip = r;
127 else
128 clip &= r;
129 }
130 qt_mac_clip_cg(context, clip, 0);
131
132 CGContextTranslateCTM(context, native.dx(), native.dy());
133 }
134 }
135 CGContextRetain(context);
136}
137
138
139/*****************************************************************************
140 QCoreGraphicsPaintEngine utility functions
141 *****************************************************************************/
142
143//conversion
144inline static float qt_mac_convert_color_to_cg(int c) { return ((float)c * 1000 / 255) / 1000; }
145inline static int qt_mac_convert_color_from_cg(float c) { return qRound(c * 255); }
146CGAffineTransform qt_mac_convert_transform_to_cg(const QTransform &t) {
147 return CGAffineTransformMake(t.m11(), t.m12(), t.m21(), t.m22(), t.dx(), t.dy());
148}
149
150CGColorSpaceRef qt_mac_colorSpaceForDeviceType(const QPaintDevice *paintDevice)
151{
152 bool isWidget = (paintDevice->devType() == QInternal::Widget);
153 return QCoreGraphicsPaintEngine::macDisplayColorSpace(isWidget ? static_cast<const QWidget *>(paintDevice)
154 : 0);
155}
156
157inline static QCFType<CGColorRef> cgColorForQColor(const QColor &col, QPaintDevice *pdev)
158{
159 CGFloat components[] = {
160 qt_mac_convert_color_to_cg(col.red()),
161 qt_mac_convert_color_to_cg(col.green()),
162 qt_mac_convert_color_to_cg(col.blue()),
163 qt_mac_convert_color_to_cg(col.alpha())
164 };
165 return CGColorCreate(qt_mac_colorSpaceForDeviceType(pdev), components);
166}
167
168// There's architectural problems with using native gradients
169// on the Mac at the moment, so disable them.
170// #define QT_MAC_USE_NATIVE_GRADIENTS
171
172#ifdef QT_MAC_USE_NATIVE_GRADIENTS
173static bool drawGradientNatively(const QGradient *gradient)
174{
175 return gradient->spread() == QGradient::PadSpread;
176}
177
178// gradiant callback
179static void qt_mac_color_gradient_function(void *info, const CGFloat *in, CGFloat *out)
180{
181 QBrush *brush = static_cast<QBrush *>(info);
182 Q_ASSERT(brush && brush->gradient());
183
184 const QGradientStops stops = brush->gradient()->stops();
185 const int n = stops.count();
186 Q_ASSERT(n >= 1);
187 const QGradientStop *begin = stops.constBegin();
188 const QGradientStop *end = begin + n;
189
190 qreal p = in[0];
191 const QGradientStop *i = begin;
192 while (i != end && i->first < p)
193 ++i;
194
195 QRgb c;
196 if (i == begin) {
197 c = begin->second.rgba();
198 } else if (i == end) {
199 c = (end - 1)->second.rgba();
200 } else {
201 const QGradientStop &s1 = *(i - 1);
202 const QGradientStop &s2 = *i;
203 qreal p1 = s1.first;
204 qreal p2 = s2.first;
205 QRgb c1 = s1.second.rgba();
206 QRgb c2 = s2.second.rgba();
207 int idist = 256 * (p - p1) / (p2 - p1);
208 int dist = 256 - idist;
209 c = qRgba(INTERPOLATE_PIXEL_256(qRed(c1), dist, qRed(c2), idist),
210 INTERPOLATE_PIXEL_256(qGreen(c1), dist, qGreen(c2), idist),
211 INTERPOLATE_PIXEL_256(qBlue(c1), dist, qBlue(c2), idist),
212 INTERPOLATE_PIXEL_256(qAlpha(c1), dist, qAlpha(c2), idist));
213 }
214
215 out[0] = qt_mac_convert_color_to_cg(qRed(c));
216 out[1] = qt_mac_convert_color_to_cg(qGreen(c));
217 out[2] = qt_mac_convert_color_to_cg(qBlue(c));
218 out[3] = qt_mac_convert_color_to_cg(qAlpha(c));
219}
220#endif
221
222//clipping handling
223void QCoreGraphicsPaintEnginePrivate::resetClip()
224{
225 static bool inReset = false;
226 if (inReset)
227 return;
228 inReset = true;
229
230 CGAffineTransform old_xform = CGContextGetCTM(hd);
231
232 //setup xforms
233 CGContextConcatCTM(hd, CGAffineTransformInvert(old_xform));
234 while (stackCount > 0) {
235 restoreGraphicsState();
236 }
237 saveGraphicsState();
238 inReset = false;
239 //reset xforms
240 CGContextConcatCTM(hd, CGAffineTransformInvert(CGContextGetCTM(hd)));
241 CGContextConcatCTM(hd, old_xform);
242}
243
244static CGRect qt_mac_compose_rect(const QRectF &r, float off=0)
245{
246 return CGRectMake(r.x()+off, r.y()+off, r.width(), r.height());
247}
248
249static CGMutablePathRef qt_mac_compose_path(const QPainterPath &p, float off=0)
250{
251 CGMutablePathRef ret = CGPathCreateMutable();
252 QPointF startPt;
253 for (int i=0; i<p.elementCount(); ++i) {
254 const QPainterPath::Element &elm = p.elementAt(i);
255 switch (elm.type) {
256 case QPainterPath::MoveToElement:
257 if(i > 0
258 && p.elementAt(i - 1).x == startPt.x()
259 && p.elementAt(i - 1).y == startPt.y())
260 CGPathCloseSubpath(ret);
261 startPt = QPointF(elm.x, elm.y);
262 CGPathMoveToPoint(ret, 0, elm.x+off, elm.y+off);
263 break;
264 case QPainterPath::LineToElement:
265 CGPathAddLineToPoint(ret, 0, elm.x+off, elm.y+off);
266 break;
267 case QPainterPath::CurveToElement:
268 Q_ASSERT(p.elementAt(i+1).type == QPainterPath::CurveToDataElement);
269 Q_ASSERT(p.elementAt(i+2).type == QPainterPath::CurveToDataElement);
270 CGPathAddCurveToPoint(ret, 0,
271 elm.x+off, elm.y+off,
272 p.elementAt(i+1).x+off, p.elementAt(i+1).y+off,
273 p.elementAt(i+2).x+off, p.elementAt(i+2).y+off);
274 i+=2;
275 break;
276 default:
277 qFatal("QCoreGraphicsPaintEngine::drawPath(), unhandled type: %d", elm.type);
278 break;
279 }
280 }
281 if(!p.isEmpty()
282 && p.elementAt(p.elementCount() - 1).x == startPt.x()
283 && p.elementAt(p.elementCount() - 1).y == startPt.y())
284 CGPathCloseSubpath(ret);
285 return ret;
286}
287
288CGColorSpaceRef QCoreGraphicsPaintEngine::m_genericColorSpace = 0;
289QHash<CGDirectDisplayID, CGColorSpaceRef> QCoreGraphicsPaintEngine::m_displayColorSpaceHash;
290bool QCoreGraphicsPaintEngine::m_postRoutineRegistered = false;
291
292CGColorSpaceRef QCoreGraphicsPaintEngine::macGenericColorSpace()
293{
294#if 0
295 if (!m_genericColorSpace) {
296#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
297 if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_4) {
298 m_genericColorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
299 } else
300#endif
301 {
302 m_genericColorSpace = CGColorSpaceCreateDeviceRGB();
303 }
304 if (!m_postRoutineRegistered) {
305 m_postRoutineRegistered = true;
306 qAddPostRoutine(QCoreGraphicsPaintEngine::cleanUpMacColorSpaces);
307 }
308 }
309 return m_genericColorSpace;
310#else
311 // Just return the main display colorspace for the moment.
312 return macDisplayColorSpace();
313#endif
314}
315
316/*
317 Ideally, we should pass the widget in here, and use CGGetDisplaysWithRect() etc.
318 to support multiple displays correctly.
319*/
320CGColorSpaceRef QCoreGraphicsPaintEngine::macDisplayColorSpace(const QWidget *widget)
321{
322 CGColorSpaceRef colorSpace;
323
324 CGDirectDisplayID displayID;
325 CMProfileRef displayProfile = 0;
326 if (widget == 0) {
327 displayID = CGMainDisplayID();
328 } else {
329 const QRect &qrect = widget->window()->geometry();
330 CGRect rect = CGRectMake(qrect.x(), qrect.y(), qrect.width(), qrect.height());
331 CGDisplayCount throwAway;
332 CGDisplayErr dErr = CGGetDisplaysWithRect(rect, 1, &displayID, &throwAway);
333 if (dErr != kCGErrorSuccess)
334 return macDisplayColorSpace(0); // fall back on main display
335 }
336 if ((colorSpace = m_displayColorSpaceHash.value(displayID)))
337 return colorSpace;
338
339 CMError err = CMGetProfileByAVID((CMDisplayIDType)displayID, &displayProfile);
340 if (err == noErr) {
341 colorSpace = CGColorSpaceCreateWithPlatformColorSpace(displayProfile);
342 } else if (widget) {
343 return macDisplayColorSpace(0); // fall back on main display
344 }
345
346 if (colorSpace == 0)
347 colorSpace = CGColorSpaceCreateDeviceRGB();
348
349 m_displayColorSpaceHash.insert(displayID, colorSpace);
350 CMCloseProfile(displayProfile);
351 if (!m_postRoutineRegistered) {
352 m_postRoutineRegistered = true;
353 qAddPostRoutine(QCoreGraphicsPaintEngine::cleanUpMacColorSpaces);
354 }
355 return colorSpace;
356}
357
358void QCoreGraphicsPaintEngine::cleanUpMacColorSpaces()
359{
360 if (m_genericColorSpace) {
361 CFRelease(m_genericColorSpace);
362 m_genericColorSpace = 0;
363 }
364 QHash<CGDirectDisplayID, CGColorSpaceRef>::const_iterator it = m_displayColorSpaceHash.constBegin();
365 while (it != m_displayColorSpaceHash.constEnd()) {
366 if (it.value())
367 CFRelease(it.value());
368 ++it;
369 }
370 m_displayColorSpaceHash.clear();
371}
372
373void qt_mac_clip_cg(CGContextRef hd, const QRegion &rgn, CGAffineTransform *orig_xform)
374{
375 CGAffineTransform old_xform = CGAffineTransformIdentity;
376 if(orig_xform) { //setup xforms
377 old_xform = CGContextGetCTM(hd);
378 CGContextConcatCTM(hd, CGAffineTransformInvert(old_xform));
379 CGContextConcatCTM(hd, *orig_xform);
380 }
381
382 //do the clipping
383 CGContextBeginPath(hd);
384 if(rgn.isEmpty()) {
385 CGContextAddRect(hd, CGRectMake(0, 0, 0, 0));
386 } else {
387#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
388 if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_5) {
389 QCFType<HIMutableShapeRef> shape = rgn.toHIMutableShape();
390 Q_ASSERT(!HIShapeIsEmpty(shape));
391 HIShapeReplacePathInCGContext(shape, hd);
392 } else
393#endif
394 {
395 QVector<QRect> rects = rgn.rects();
396 const int count = rects.size();
397 for(int i = 0; i < count; i++) {
398 const QRect &r = rects[i];
399 CGRect mac_r = CGRectMake(r.x(), r.y(), r.width(), r.height());
400 CGContextAddRect(hd, mac_r);
401 }
402 }
403
404 }
405 CGContextClip(hd);
406
407 if(orig_xform) {//reset xforms
408 CGContextConcatCTM(hd, CGAffineTransformInvert(CGContextGetCTM(hd)));
409 CGContextConcatCTM(hd, old_xform);
410 }
411}
412
413
414//pattern handling (tiling)
415#if 1
416# define QMACPATTERN_MASK_MULTIPLIER 32
417#else
418# define QMACPATTERN_MASK_MULTIPLIER 1
419#endif
420class QMacPattern
421{
422public:
423 QMacPattern() : as_mask(false), pdev(0), image(0) { data.bytes = 0; }
424 ~QMacPattern() { CGImageRelease(image); }
425 int width() {
426 if(image)
427 return CGImageGetWidth(image);
428 if(data.bytes)
429 return 8*QMACPATTERN_MASK_MULTIPLIER;
430 return data.pixmap.width();
431 }
432 int height() {
433 if(image)
434 return CGImageGetHeight(image);
435 if(data.bytes)
436 return 8*QMACPATTERN_MASK_MULTIPLIER;
437 return data.pixmap.height();
438 }
439
440 //input
441 QColor foreground;
442 bool as_mask;
443 struct {
444 QPixmap pixmap;
445 const uchar *bytes;
446 } data;
447 QPaintDevice *pdev;
448 //output
449 CGImageRef image;
450};
451static void qt_mac_draw_pattern(void *info, CGContextRef c)
452{
453 QMacPattern *pat = (QMacPattern*)info;
454 int w = 0, h = 0;
455 bool isBitmap = (pat->data.pixmap.depth() == 1);
456 if(!pat->image) { //lazy cache
457 if(pat->as_mask) {
458 Q_ASSERT(pat->data.bytes);
459 w = h = 8;
460#if (QMACPATTERN_MASK_MULTIPLIER == 1)
461 CGDataProviderRef provider = CGDataProviderCreateWithData(0, pat->data.bytes, w*h, 0);
462 pat->image = CGImageMaskCreate(w, h, 1, 1, 1, provider, 0, false);
463 CGDataProviderRelease(provider);
464#else
465 const int numBytes = (w*h)/sizeof(uchar);
466 uchar xor_bytes[numBytes];
467 for(int i = 0; i < numBytes; ++i)
468 xor_bytes[i] = pat->data.bytes[i] ^ 0xFF;
469 CGDataProviderRef provider = CGDataProviderCreateWithData(0, xor_bytes, w*h, 0);
470 CGImageRef swatch = CGImageMaskCreate(w, h, 1, 1, 1, provider, 0, false);
471 CGDataProviderRelease(provider);
472
473 const QColor c0(0, 0, 0, 0), c1(255, 255, 255, 255);
474 QPixmap pm(w*QMACPATTERN_MASK_MULTIPLIER, h*QMACPATTERN_MASK_MULTIPLIER);
475 pm.fill(c0);
476 CGContextRef pm_ctx = qt_mac_cg_context(&pm);
477 CGContextSetFillColorWithColor(c, cgColorForQColor(c1, pat->pdev));
478 CGRect rect = CGRectMake(0, 0, w, h);
479 for(int x = 0; x < QMACPATTERN_MASK_MULTIPLIER; ++x) {
480 rect.origin.x = x * w;
481 for(int y = 0; y < QMACPATTERN_MASK_MULTIPLIER; ++y) {
482 rect.origin.y = y * h;
483 qt_mac_drawCGImage(pm_ctx, &rect, swatch);
484 }
485 }
486 pat->image = qt_mac_create_imagemask(pm, pm.rect());
487 CGImageRelease(swatch);
488 CGContextRelease(pm_ctx);
489 w *= QMACPATTERN_MASK_MULTIPLIER;
490 h *= QMACPATTERN_MASK_MULTIPLIER;
491#endif
492 } else {
493 w = pat->data.pixmap.width();
494 h = pat->data.pixmap.height();
495 if (isBitmap)
496 pat->image = qt_mac_create_imagemask(pat->data.pixmap, pat->data.pixmap.rect());
497 else
498 pat->image = (CGImageRef)pat->data.pixmap.macCGHandle();
499 }
500 } else {
501 w = CGImageGetWidth(pat->image);
502 h = CGImageGetHeight(pat->image);
503 }
504
505 //draw
506 bool needRestore = false;
507 if (CGImageIsMask(pat->image)) {
508 CGContextSaveGState(c);
509 CGContextSetFillColorWithColor(c, cgColorForQColor(pat->foreground, pat->pdev));
510 }
511 CGRect rect = CGRectMake(0, 0, w, h);
512 qt_mac_drawCGImage(c, &rect, pat->image);
513 if(needRestore)
514 CGContextRestoreGState(c);
515}
516static void qt_mac_dispose_pattern(void *info)
517{
518 QMacPattern *pat = (QMacPattern*)info;
519 delete pat;
520}
521
522/*****************************************************************************
523 QCoreGraphicsPaintEngine member functions
524 *****************************************************************************/
525
526inline static QPaintEngine::PaintEngineFeatures qt_mac_cg_features()
527{
528 return QPaintEngine::PaintEngineFeatures(QPaintEngine::AllFeatures & ~QPaintEngine::PaintOutsidePaintEvent
529 & ~QPaintEngine::PerspectiveTransform
530 & ~QPaintEngine::ConicalGradientFill
531 & ~QPaintEngine::LinearGradientFill
532 & ~QPaintEngine::RadialGradientFill
533 & ~QPaintEngine::BrushStroke);
534}
535
536QCoreGraphicsPaintEngine::QCoreGraphicsPaintEngine()
537: QPaintEngine(*(new QCoreGraphicsPaintEnginePrivate), qt_mac_cg_features())
538{
539}
540
541QCoreGraphicsPaintEngine::QCoreGraphicsPaintEngine(QPaintEnginePrivate &dptr)
542: QPaintEngine(dptr, qt_mac_cg_features())
543{
544}
545
546QCoreGraphicsPaintEngine::~QCoreGraphicsPaintEngine()
547{
548}
549
550bool
551QCoreGraphicsPaintEngine::begin(QPaintDevice *pdev)
552{
553 Q_D(QCoreGraphicsPaintEngine);
554 if(isActive()) { // already active painting
555 qWarning("QCoreGraphicsPaintEngine::begin: Painter already active");
556 return false;
557 }
558
559 //initialization
560 d->pdev = pdev;
561 d->complexXForm = false;
562 d->cosmeticPen = QCoreGraphicsPaintEnginePrivate::CosmeticSetPenWidth;
563 d->cosmeticPenSize = 1;
564 d->current.clipEnabled = false;
565 d->pixelSize = QPoint(1,1);
566 d->hd = qt_mac_cg_context(pdev);
567 if(d->hd) {
568 d->saveGraphicsState();
569 d->orig_xform = CGContextGetCTM(d->hd);
570 if (d->shading) {
571 CGShadingRelease(d->shading);
572 d->shading = 0;
573 }
574 d->setClip(0); //clear the context's clipping
575 }
576
577 setActive(true);
578
579 if(d->pdev->devType() == QInternal::Widget) { // device is a widget
580 QWidget *w = (QWidget*)d->pdev;
581 bool unclipped = w->testAttribute(Qt::WA_PaintUnclipped);
582
583 if((w->windowType() == Qt::Desktop)) {
584 if(!unclipped)
585 qWarning("QCoreGraphicsPaintEngine::begin: Does not support clipped desktop on Mac OS X");
586 // ## need to do [qt_mac_window_for(w) makeKeyAndOrderFront]; (need to rename the file)
587 } else if(unclipped) {
588 qWarning("QCoreGraphicsPaintEngine::begin: Does not support unclipped painting");
589 }
590 } else if(d->pdev->devType() == QInternal::Pixmap) { // device is a pixmap
591 QPixmap *pm = (QPixmap*)d->pdev;
592 if(pm->isNull()) {
593 qWarning("QCoreGraphicsPaintEngine::begin: Cannot paint null pixmap");
594 end();
595 return false;
596 }
597 }
598
599 setDirty(QPaintEngine::DirtyPen);
600 setDirty(QPaintEngine::DirtyBrush);
601 setDirty(QPaintEngine::DirtyBackground);
602 setDirty(QPaintEngine::DirtyHints);
603 return true;
604}
605
606bool
607QCoreGraphicsPaintEngine::end()
608{
609 Q_D(QCoreGraphicsPaintEngine);
610 setActive(false);
611 if(d->pdev->devType() == QInternal::Widget && static_cast<QWidget*>(d->pdev)->windowType() == Qt::Desktop) {
612#ifndef QT_MAC_USE_COCOA
613 HideWindow(qt_mac_window_for(static_cast<QWidget*>(d->pdev)));
614#else
615// // ### need to do [qt_mac_window_for(static_cast<QWidget *>(d->pdev)) orderOut]; (need to rename)
616#endif
617
618 }
619 if(d->shading) {
620 CGShadingRelease(d->shading);
621 d->shading = 0;
622 }
623 d->pdev = 0;
624 if(d->hd) {
625 d->restoreGraphicsState();
626 CGContextSynchronize(d->hd);
627 CGContextRelease(d->hd);
628 d->hd = 0;
629 }
630 return true;
631}
632
633void
634QCoreGraphicsPaintEngine::updateState(const QPaintEngineState &state)
635{
636 Q_D(QCoreGraphicsPaintEngine);
637 QPaintEngine::DirtyFlags flags = state.state();
638
639 if (flags & DirtyTransform)
640 updateMatrix(state.transform());
641
642 if (flags & DirtyClipEnabled) {
643 if (state.isClipEnabled())
644 updateClipPath(painter()->clipPath(), Qt::ReplaceClip);
645 else
646 updateClipPath(QPainterPath(), Qt::NoClip);
647 }
648
649 if (flags & DirtyClipPath) {
650 updateClipPath(state.clipPath(), state.clipOperation());
651 } else if (flags & DirtyClipRegion) {
652 updateClipRegion(state.clipRegion(), state.clipOperation());
653 }
654
655 // If the clip has changed we need to update all other states
656 // too, since they are included in the system context on OSX,
657 // and changing the clip resets that context back to scratch.
658 if (flags & (DirtyClipPath | DirtyClipRegion | DirtyClipEnabled))
659 flags |= AllDirty;
660
661 if (flags & DirtyPen)
662 updatePen(state.pen());
663 if (flags & (DirtyBrush|DirtyBrushOrigin))
664 updateBrush(state.brush(), state.brushOrigin());
665 if (flags & DirtyFont)
666 updateFont(state.font());
667 if (flags & DirtyOpacity)
668 updateOpacity(state.opacity());
669 if (flags & DirtyHints)
670 updateRenderHints(state.renderHints());
671 if (flags & DirtyCompositionMode)
672 updateCompositionMode(state.compositionMode());
673
674 if (flags & (DirtyPen | DirtyTransform)) {
675 if (!d->current.pen.isCosmetic()) {
676 d->cosmeticPen = QCoreGraphicsPaintEnginePrivate::CosmeticNone;
677 } else if (d->current.transform.m11() < d->current.transform.m22()-1.0 ||
678 d->current.transform.m11() > d->current.transform.m22()+1.0) {
679 d->cosmeticPen = QCoreGraphicsPaintEnginePrivate::CosmeticTransformPath;
680 d->cosmeticPenSize = d->adjustPenWidth(d->current.pen.widthF());
681 if (!d->cosmeticPenSize)
682 d->cosmeticPenSize = 1.0;
683 } else {
684 d->cosmeticPen = QCoreGraphicsPaintEnginePrivate::CosmeticSetPenWidth;
685 static const float sqrt2 = sqrt(2);
686 qreal width = d->current.pen.widthF();
687 if (!width)
688 width = 1;
689 d->cosmeticPenSize = sqrt(pow(d->pixelSize.y(), 2) + pow(d->pixelSize.x(), 2)) / sqrt2 * width;
690 }
691 }
692}
693
694void
695QCoreGraphicsPaintEngine::updatePen(const QPen &pen)
696{
697 Q_D(QCoreGraphicsPaintEngine);
698 Q_ASSERT(isActive());
699 d->current.pen = pen;
700 d->setStrokePen(pen);
701}
702
703void
704QCoreGraphicsPaintEngine::updateBrush(const QBrush &brush, const QPointF &brushOrigin)
705{
706 Q_D(QCoreGraphicsPaintEngine);
707 Q_ASSERT(isActive());
708 d->current.brush = brush;
709
710#ifdef QT_MAC_USE_NATIVE_GRADIENTS
711 // Quartz supports only pad spread
712 if (const QGradient *gradient = brush.gradient()) {
713 if (drawGradientNatively(gradient)) {
714 gccaps |= QPaintEngine::LinearGradientFill | QPaintEngine::RadialGradientFill;
715 } else {
716 gccaps &= ~(QPaintEngine::LinearGradientFill | QPaintEngine::RadialGradientFill);
717 }
718 }
719#endif
720
721 if (d->shading) {
722 CGShadingRelease(d->shading);
723 d->shading = 0;
724 }
725 d->setFillBrush(brushOrigin);
726}
727
728void
729QCoreGraphicsPaintEngine::updateOpacity(qreal opacity)
730{
731 Q_D(QCoreGraphicsPaintEngine);
732 CGContextSetAlpha(d->hd, opacity);
733}
734
735void
736QCoreGraphicsPaintEngine::updateFont(const QFont &)
737{
738 Q_D(QCoreGraphicsPaintEngine);
739 Q_ASSERT(isActive());
740 updatePen(d->current.pen);
741}
742
743void
744QCoreGraphicsPaintEngine::updateMatrix(const QTransform &transform)
745{
746 Q_D(QCoreGraphicsPaintEngine);
747 Q_ASSERT(isActive());
748
749 if (qt_is_nan(transform.m11()) || qt_is_nan(transform.m12()) || qt_is_nan(transform.m13())
750 || qt_is_nan(transform.m21()) || qt_is_nan(transform.m22()) || qt_is_nan(transform.m23())
751 || qt_is_nan(transform.m31()) || qt_is_nan(transform.m32()) || qt_is_nan(transform.m33()))
752 return;
753
754 d->current.transform = transform;
755 d->setTransform(transform.isIdentity() ? 0 : &transform);
756 d->complexXForm = (transform.m11() != 1 || transform.m22() != 1
757 || transform.m12() != 0 || transform.m21() != 0);
758 d->pixelSize = d->devicePixelSize(d->hd);
759}
760
761void
762QCoreGraphicsPaintEngine::updateClipPath(const QPainterPath &p, Qt::ClipOperation op)
763{
764 Q_D(QCoreGraphicsPaintEngine);
765 Q_ASSERT(isActive());
766 if(op == Qt::NoClip) {
767 if(d->current.clipEnabled) {
768 d->current.clipEnabled = false;
769 d->current.clip = QRegion();
770 d->setClip(0);
771 }
772 } else {
773 if(!d->current.clipEnabled)
774 op = Qt::ReplaceClip;
775 d->current.clipEnabled = true;
776 QRegion clipRegion(p.toFillPolygon().toPolygon(), p.fillRule());
777 if(op == Qt::ReplaceClip) {
778 d->current.clip = clipRegion;
779 d->setClip(0);
780 if(p.isEmpty()) {
781 CGRect rect = CGRectMake(0, 0, 0, 0);
782 CGContextClipToRect(d->hd, rect);
783 } else {
784 CGMutablePathRef path = qt_mac_compose_path(p);
785 CGContextBeginPath(d->hd);
786 CGContextAddPath(d->hd, path);
787 if(p.fillRule() == Qt::WindingFill)
788 CGContextClip(d->hd);
789 else
790 CGContextEOClip(d->hd);
791 CGPathRelease(path);
792 }
793 } else if(op == Qt::IntersectClip) {
794 d->current.clip = d->current.clip.intersected(clipRegion);
795 d->setClip(&d->current.clip);
796 } else if(op == Qt::UniteClip) {
797 d->current.clip = d->current.clip.united(clipRegion);
798 d->setClip(&d->current.clip);
799 }
800 }
801}
802
803void
804QCoreGraphicsPaintEngine::updateClipRegion(const QRegion &clipRegion, Qt::ClipOperation op)
805{
806 Q_D(QCoreGraphicsPaintEngine);
807 Q_ASSERT(isActive());
808 if(op == Qt::NoClip) {
809 d->current.clipEnabled = false;
810 d->current.clip = QRegion();
811 d->setClip(0);
812 } else {
813 if(!d->current.clipEnabled)
814 op = Qt::ReplaceClip;
815 d->current.clipEnabled = true;
816 if(op == Qt::IntersectClip)
817 d->current.clip = d->current.clip.intersected(clipRegion);
818 else if(op == Qt::ReplaceClip)
819 d->current.clip = clipRegion;
820 else if(op == Qt::UniteClip)
821 d->current.clip = d->current.clip.united(clipRegion);
822 d->setClip(&d->current.clip);
823 }
824}
825
826void
827QCoreGraphicsPaintEngine::drawPath(const QPainterPath &p)
828{
829 Q_D(QCoreGraphicsPaintEngine);
830 Q_ASSERT(isActive());
831
832 if (state->compositionMode() == QPainter::CompositionMode_Destination)
833 return;
834
835 CGMutablePathRef path = qt_mac_compose_path(p);
836 uchar ops = QCoreGraphicsPaintEnginePrivate::CGStroke;
837 if(p.fillRule() == Qt::WindingFill)
838 ops |= QCoreGraphicsPaintEnginePrivate::CGFill;
839 else
840 ops |= QCoreGraphicsPaintEnginePrivate::CGEOFill;
841 CGContextBeginPath(d->hd);
842 d->drawPath(ops, path);
843 CGPathRelease(path);
844}
845
846void
847QCoreGraphicsPaintEngine::drawRects(const QRectF *rects, int rectCount)
848{
849 Q_D(QCoreGraphicsPaintEngine);
850 Q_ASSERT(isActive());
851
852 if (state->compositionMode() == QPainter::CompositionMode_Destination)
853 return;
854
855 for (int i=0; i<rectCount; ++i) {
856 QRectF r = rects[i];
857
858 CGMutablePathRef path = CGPathCreateMutable();
859 CGPathAddRect(path, 0, qt_mac_compose_rect(r));
860 d->drawPath(QCoreGraphicsPaintEnginePrivate::CGFill|QCoreGraphicsPaintEnginePrivate::CGStroke,
861 path);
862 CGPathRelease(path);
863 }
864}
865
866void
867QCoreGraphicsPaintEngine::drawPoints(const QPointF *points, int pointCount)
868{
869 Q_D(QCoreGraphicsPaintEngine);
870 Q_ASSERT(isActive());
871
872 if (state->compositionMode() == QPainter::CompositionMode_Destination)
873 return;
874
875 if (d->current.pen.capStyle() == Qt::FlatCap)
876 CGContextSetLineCap(d->hd, kCGLineCapSquare);
877
878 CGMutablePathRef path = CGPathCreateMutable();
879 for(int i=0; i < pointCount; i++) {
880 float x = points[i].x(), y = points[i].y();
881 CGPathMoveToPoint(path, 0, x, y);
882 CGPathAddLineToPoint(path, 0, x+0.001, y);
883 }
884
885 bool doRestore = false;
886 if(d->cosmeticPen == QCoreGraphicsPaintEnginePrivate::CosmeticNone && !(state->renderHints() & QPainter::Antialiasing)) {
887 //we don't want adjusted pens for point rendering
888 doRestore = true;
889 d->saveGraphicsState();
890 CGContextSetLineWidth(d->hd, d->current.pen.widthF());
891 }
892 d->drawPath(QCoreGraphicsPaintEnginePrivate::CGStroke, path);
893 if (doRestore)
894 d->restoreGraphicsState();
895 CGPathRelease(path);
896 if (d->current.pen.capStyle() == Qt::FlatCap)
897 CGContextSetLineCap(d->hd, kCGLineCapButt);
898}
899
900void
901QCoreGraphicsPaintEngine::drawEllipse(const QRectF &r)
902{
903 Q_D(QCoreGraphicsPaintEngine);
904 Q_ASSERT(isActive());
905
906 if (state->compositionMode() == QPainter::CompositionMode_Destination)
907 return;
908
909 CGMutablePathRef path = CGPathCreateMutable();
910 CGAffineTransform transform = CGAffineTransformMakeScale(r.width() / r.height(), 1);
911 CGPathAddArc(path, &transform,(r.x() + (r.width() / 2)) / (r.width() / r.height()),
912 r.y() + (r.height() / 2), r.height() / 2, 0, (2 * M_PI), false);
913 d->drawPath(QCoreGraphicsPaintEnginePrivate::CGFill | QCoreGraphicsPaintEnginePrivate::CGStroke,
914 path);
915 CGPathRelease(path);
916}
917
918void
919QCoreGraphicsPaintEngine::drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode)
920{
921 Q_D(QCoreGraphicsPaintEngine);
922 Q_ASSERT(isActive());
923
924 if (state->compositionMode() == QPainter::CompositionMode_Destination)
925 return;
926
927 CGMutablePathRef path = CGPathCreateMutable();
928 CGPathMoveToPoint(path, 0, points[0].x(), points[0].y());
929 for(int x = 1; x < pointCount; ++x)
930 CGPathAddLineToPoint(path, 0, points[x].x(), points[x].y());
931 if(mode != PolylineMode && points[0] != points[pointCount-1])
932 CGPathAddLineToPoint(path, 0, points[0].x(), points[0].y());
933 uint op = QCoreGraphicsPaintEnginePrivate::CGStroke;
934 if (mode != PolylineMode)
935 op |= mode == OddEvenMode ? QCoreGraphicsPaintEnginePrivate::CGEOFill
936 : QCoreGraphicsPaintEnginePrivate::CGFill;
937 d->drawPath(op, path);
938 CGPathRelease(path);
939}
940
941void
942QCoreGraphicsPaintEngine::drawLines(const QLineF *lines, int lineCount)
943{
944 Q_D(QCoreGraphicsPaintEngine);
945 Q_ASSERT(isActive());
946
947 if (state->compositionMode() == QPainter::CompositionMode_Destination)
948 return;
949
950 CGMutablePathRef path = CGPathCreateMutable();
951 for(int i = 0; i < lineCount; i++) {
952 const QPointF start = lines[i].p1(), end = lines[i].p2();
953 CGPathMoveToPoint(path, 0, start.x(), start.y());
954 CGPathAddLineToPoint(path, 0, end.x(), end.y());
955 }
956 d->drawPath(QCoreGraphicsPaintEnginePrivate::CGStroke, path);
957 CGPathRelease(path);
958}
959
960void QCoreGraphicsPaintEngine::drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr)
961{
962 Q_D(QCoreGraphicsPaintEngine);
963 Q_ASSERT(isActive());
964
965 if (state->compositionMode() == QPainter::CompositionMode_Destination)
966 return;
967
968 if(pm.isNull())
969 return;
970
971 bool differentSize = (QRectF(0, 0, pm.width(), pm.height()) != sr), doRestore = false;
972 CGRect rect = CGRectMake(qRound(r.x()), qRound(r.y()), qRound(r.width()), qRound(r.height()));
973 QCFType<CGImageRef> image;
974 bool isBitmap = (pm.depth() == 1);
975 if (isBitmap) {
976 doRestore = true;
977 d->saveGraphicsState();
978
979 const QColor &col = d->current.pen.color();
980 CGContextSetFillColorWithColor(d->hd, cgColorForQColor(col, d->pdev));
981 image = qt_mac_create_imagemask(pm, sr);
982 } else if (differentSize) {
983 QCFType<CGImageRef> img = pm.toMacCGImageRef();
984 image = CGImageCreateWithImageInRect(img, CGRectMake(qRound(sr.x()), qRound(sr.y()), qRound(sr.width()), qRound(sr.height())));
985 } else {
986 image = (CGImageRef)pm.macCGHandle();
987 }
988 qt_mac_drawCGImage(d->hd, &rect, image);
989 if (doRestore)
990 d->restoreGraphicsState();
991}
992
993static void drawImageReleaseData (void *info, const void *, size_t)
994{
995 delete static_cast<QImage *>(info);
996}
997
998CGImageRef qt_mac_createCGImageFromQImage(const QImage &img, const QImage **imagePtr = 0)
999{
1000 QImage *image;
1001 if (img.depth() != 32)
1002 image = new QImage(img.convertToFormat(QImage::Format_ARGB32_Premultiplied));
1003 else
1004 image = new QImage(img);
1005
1006 uint cgflags = kCGImageAlphaNone;
1007 switch (image->format()) {
1008 case QImage::Format_ARGB32_Premultiplied:
1009 cgflags = kCGImageAlphaPremultipliedFirst;
1010 break;
1011 case QImage::Format_ARGB32:
1012 cgflags = kCGImageAlphaFirst;
1013 break;
1014 case QImage::Format_RGB32:
1015 cgflags = kCGImageAlphaNoneSkipFirst;
1016 default:
1017 break;
1018 }
1019#if defined(kCGBitmapByteOrder32Host) //only needed because CGImage.h added symbols in the minor version
1020 cgflags |= kCGBitmapByteOrder32Host;
1021#endif
1022 QCFType<CGDataProviderRef> dataProvider = CGDataProviderCreateWithData(image,
1023 static_cast<const QImage *>(image)->bits(),
1024 image->byteCount(),
1025 drawImageReleaseData);
1026 if (imagePtr)
1027 *imagePtr = image;
1028 return CGImageCreate(image->width(), image->height(), 8, 32,
1029 image->bytesPerLine(),
1030 QCoreGraphicsPaintEngine::macGenericColorSpace(),
1031 cgflags, dataProvider, 0, false, kCGRenderingIntentDefault);
1032
1033}
1034
1035void QCoreGraphicsPaintEngine::drawImage(const QRectF &r, const QImage &img, const QRectF &sr,
1036 Qt::ImageConversionFlags flags)
1037{
1038 Q_D(QCoreGraphicsPaintEngine);
1039 Q_UNUSED(flags);
1040 Q_ASSERT(isActive());
1041
1042 if (img.isNull() || state->compositionMode() == QPainter::CompositionMode_Destination)
1043 return;
1044
1045 const QImage *image;
1046 QCFType<CGImageRef> cgimage = qt_mac_createCGImageFromQImage(img, &image);
1047 CGRect rect = CGRectMake(r.x(), r.y(), r.width(), r.height());
1048 if (QRectF(0, 0, img.width(), img.height()) != sr)
1049 cgimage = CGImageCreateWithImageInRect(cgimage, CGRectMake(sr.x(), sr.y(),
1050 sr.width(), sr.height()));
1051 qt_mac_drawCGImage(d->hd, &rect, cgimage);
1052}
1053
1054void QCoreGraphicsPaintEngine::initialize()
1055{
1056}
1057
1058void QCoreGraphicsPaintEngine::cleanup()
1059{
1060}
1061
1062CGContextRef
1063QCoreGraphicsPaintEngine::handle() const
1064{
1065 return d_func()->hd;
1066}
1067
1068void
1069QCoreGraphicsPaintEngine::drawTiledPixmap(const QRectF &r, const QPixmap &pixmap,
1070 const QPointF &p)
1071{
1072 Q_D(QCoreGraphicsPaintEngine);
1073 Q_ASSERT(isActive());
1074
1075 if (state->compositionMode() == QPainter::CompositionMode_Destination)
1076 return;
1077
1078 //save the old state
1079 d->saveGraphicsState();
1080
1081 //setup the pattern
1082 QMacPattern *qpattern = new QMacPattern;
1083 qpattern->data.pixmap = pixmap;
1084 qpattern->foreground = d->current.pen.color();
1085 qpattern->pdev = d->pdev;
1086 CGPatternCallbacks callbks;
1087 callbks.version = 0;
1088 callbks.drawPattern = qt_mac_draw_pattern;
1089 callbks.releaseInfo = qt_mac_dispose_pattern;
1090 const int width = qpattern->width(), height = qpattern->height();
1091 CGAffineTransform trans = CGContextGetCTM(d->hd);
1092 CGPatternRef pat = CGPatternCreate(qpattern, CGRectMake(0, 0, width, height),
1093 trans, width, height,
1094 kCGPatternTilingNoDistortion, true, &callbks);
1095 CGColorSpaceRef cs = CGColorSpaceCreatePattern(0);
1096 CGContextSetFillColorSpace(d->hd, cs);
1097 CGFloat component = 1.0; //just one
1098 CGContextSetFillPattern(d->hd, pat, &component);
1099 CGSize phase = CGSizeApplyAffineTransform(CGSizeMake(-(p.x()-r.x()), -(p.y()-r.y())), trans);
1100 CGContextSetPatternPhase(d->hd, phase);
1101
1102 //fill the rectangle
1103 CGRect mac_rect = CGRectMake(r.x(), r.y(), r.width(), r.height());
1104 CGContextFillRect(d->hd, mac_rect);
1105
1106 //restore the state
1107 d->restoreGraphicsState();
1108 //cleanup
1109 CGColorSpaceRelease(cs);
1110 CGPatternRelease(pat);
1111}
1112
1113void QCoreGraphicsPaintEngine::drawTextItem(const QPointF &pos, const QTextItem &item)
1114{
1115 Q_D(QCoreGraphicsPaintEngine);
1116 if (d->current.transform.type() == QTransform::TxProject
1117#ifndef QMAC_NATIVE_GRADIENTS
1118 || painter()->pen().brush().gradient() //Just let the base engine "emulate" the gradient
1119#endif
1120 ) {
1121 QPaintEngine::drawTextItem(pos, item);
1122 return;
1123 }
1124
1125 if (state->compositionMode() == QPainter::CompositionMode_Destination)
1126 return;
1127
1128 const QTextItemInt &ti = static_cast<const QTextItemInt &>(item);
1129
1130 QPen oldPen = painter()->pen();
1131 QBrush oldBrush = painter()->brush();
1132 QPointF oldBrushOrigin = painter()->brushOrigin();
1133 updatePen(Qt::NoPen);
1134 updateBrush(oldPen.brush(), QPointF(0, 0));
1135
1136 Q_ASSERT(type() == QPaintEngine::CoreGraphics);
1137
1138 QFontEngine *fe = ti.fontEngine;
1139
1140 const bool textAA = state->renderHints() & QPainter::TextAntialiasing && fe->fontDef.pointSize > qt_antialiasing_threshold && !(fe->fontDef.styleStrategy & QFont::NoAntialias);
1141 const bool lineAA = state->renderHints() & QPainter::Antialiasing;
1142 if(textAA != lineAA)
1143 CGContextSetShouldAntialias(d->hd, textAA);
1144
1145 if (ti.glyphs.numGlyphs) {
1146 switch (fe->type()) {
1147 case QFontEngine::Mac:
1148#ifdef QT_MAC_USE_COCOA
1149 static_cast<QCoreTextFontEngine *>(fe)->draw(d->hd, pos.x(), pos.y(), ti, paintDevice()->height());
1150#else
1151 static_cast<QFontEngineMac *>(fe)->draw(d->hd, pos.x(), pos.y(), ti, paintDevice()->height());
1152#endif
1153 break;
1154 case QFontEngine::Box:
1155 d->drawBoxTextItem(pos, ti);
1156 break;
1157 default:
1158 break;
1159 }
1160 }
1161
1162 if(textAA != lineAA)
1163 CGContextSetShouldAntialias(d->hd, !textAA);
1164
1165 updatePen(oldPen);
1166 updateBrush(oldBrush, oldBrushOrigin);
1167}
1168
1169QPainter::RenderHints
1170QCoreGraphicsPaintEngine::supportedRenderHints() const
1171{
1172 return QPainter::RenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
1173}
1174enum CGCompositeMode {
1175 kCGCompositeModeClear = 0,
1176 kCGCompositeModeCopy = 1,
1177 kCGCompositeModeSourceOver = 2,
1178 kCGCompositeModeSourceIn = 3,
1179 kCGCompositeModeSourceOut = 4,
1180 kCGCompositeModeSourceAtop = 5,
1181 kCGCompositeModeDestinationOver = 6,
1182 kCGCompositeModeDestinationIn = 7,
1183 kCGCompositeModeDestinationOut = 8,
1184 kCGCompositeModeDestinationAtop = 9,
1185 kCGCompositeModeXOR = 10,
1186 kCGCompositeModePlusDarker = 11, // (max (0, (1-d) + (1-s)))
1187 kCGCompositeModePlusLighter = 12, // (min (1, s + d))
1188 };
1189extern "C" {
1190 extern void CGContextSetCompositeOperation(CGContextRef, int);
1191} // private function, but is in all versions of OS X.
1192void
1193QCoreGraphicsPaintEngine::updateCompositionMode(QPainter::CompositionMode mode)
1194{
1195#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5)
1196 if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_5) {
1197 int cg_mode = kCGBlendModeNormal;
1198 switch(mode) {
1199 case QPainter::CompositionMode_Multiply:
1200 cg_mode = kCGBlendModeMultiply;
1201 break;
1202 case QPainter::CompositionMode_Screen:
1203 cg_mode = kCGBlendModeScreen;
1204 break;
1205 case QPainter::CompositionMode_Overlay:
1206 cg_mode = kCGBlendModeOverlay;
1207 break;
1208 case QPainter::CompositionMode_Darken:
1209 cg_mode = kCGBlendModeDarken;
1210 break;
1211 case QPainter::CompositionMode_Lighten:
1212 cg_mode = kCGBlendModeLighten;
1213 break;
1214 case QPainter::CompositionMode_ColorDodge:
1215 cg_mode = kCGBlendModeColorDodge;
1216 break;
1217 case QPainter::CompositionMode_ColorBurn:
1218 cg_mode = kCGBlendModeColorBurn;
1219 break;
1220 case QPainter::CompositionMode_HardLight:
1221 cg_mode = kCGBlendModeHardLight;
1222 break;
1223 case QPainter::CompositionMode_SoftLight:
1224 cg_mode = kCGBlendModeSoftLight;
1225 break;
1226 case QPainter::CompositionMode_Difference:
1227 cg_mode = kCGBlendModeDifference;
1228 break;
1229 case QPainter::CompositionMode_Exclusion:
1230 cg_mode = kCGBlendModeExclusion;
1231 break;
1232 case QPainter::CompositionMode_Plus:
1233 cg_mode = kCGBlendModePlusLighter;
1234 break;
1235 case QPainter::CompositionMode_SourceOver:
1236 cg_mode = kCGBlendModeNormal;
1237 break;
1238 case QPainter::CompositionMode_DestinationOver:
1239 cg_mode = kCGBlendModeDestinationOver;
1240 break;
1241 case QPainter::CompositionMode_Clear:
1242 cg_mode = kCGBlendModeClear;
1243 break;
1244 case QPainter::CompositionMode_Source:
1245 cg_mode = kCGBlendModeCopy;
1246 break;
1247 case QPainter::CompositionMode_Destination:
1248 cg_mode = -1;
1249 break;
1250 case QPainter::CompositionMode_SourceIn:
1251 cg_mode = kCGBlendModeSourceIn;
1252 break;
1253 case QPainter::CompositionMode_DestinationIn:
1254 cg_mode = kCGCompositeModeDestinationIn;
1255 break;
1256 case QPainter::CompositionMode_SourceOut:
1257 cg_mode = kCGBlendModeSourceOut;
1258 break;
1259 case QPainter::CompositionMode_DestinationOut:
1260 cg_mode = kCGBlendModeDestinationOver;
1261 break;
1262 case QPainter::CompositionMode_SourceAtop:
1263 cg_mode = kCGBlendModeSourceAtop;
1264 break;
1265 case QPainter::CompositionMode_DestinationAtop:
1266 cg_mode = kCGBlendModeDestinationAtop;
1267 break;
1268 case QPainter::CompositionMode_Xor:
1269 cg_mode = kCGBlendModeXOR;
1270 break;
1271 default:
1272 break;
1273 }
1274 if (cg_mode > -1) {
1275 CGContextSetBlendMode(d_func()->hd, CGBlendMode(cg_mode));
1276 }
1277 } else
1278#endif
1279 // The standard porter duff ops.
1280 if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_3
1281 && mode <= QPainter::CompositionMode_Xor) {
1282 int cg_mode = kCGCompositeModeCopy;
1283 switch (mode) {
1284 case QPainter::CompositionMode_SourceOver:
1285 cg_mode = kCGCompositeModeSourceOver;
1286 break;
1287 case QPainter::CompositionMode_DestinationOver:
1288 cg_mode = kCGCompositeModeDestinationOver;
1289 break;
1290 case QPainter::CompositionMode_Clear:
1291 cg_mode = kCGCompositeModeClear;
1292 break;
1293 default:
1294 qWarning("QCoreGraphicsPaintEngine: Unhandled composition mode %d", (int)mode);
1295 break;
1296 case QPainter::CompositionMode_Source:
1297 cg_mode = kCGCompositeModeCopy;
1298 break;
1299 case QPainter::CompositionMode_Destination:
1300 cg_mode = CGCompositeMode(-1);
1301 break;
1302 case QPainter::CompositionMode_SourceIn:
1303 cg_mode = kCGCompositeModeSourceIn;
1304 break;
1305 case QPainter::CompositionMode_DestinationIn:
1306 cg_mode = kCGCompositeModeDestinationIn;
1307 break;
1308 case QPainter::CompositionMode_SourceOut:
1309 cg_mode = kCGCompositeModeSourceOut;
1310 break;
1311 case QPainter::CompositionMode_DestinationOut:
1312 cg_mode = kCGCompositeModeDestinationOut;
1313 break;
1314 case QPainter::CompositionMode_SourceAtop:
1315 cg_mode = kCGCompositeModeSourceAtop;
1316 break;
1317 case QPainter::CompositionMode_DestinationAtop:
1318 cg_mode = kCGCompositeModeDestinationAtop;
1319 break;
1320 case QPainter::CompositionMode_Xor:
1321 cg_mode = kCGCompositeModeXOR;
1322 break;
1323 }
1324 if (cg_mode > -1)
1325 CGContextSetCompositeOperation(d_func()->hd, CGCompositeMode(cg_mode));
1326 } else {
1327#if (MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4)
1328 bool needPrivateAPI = false;
1329 if (QSysInfo::MacintoshVersion >= QSysInfo::MV_10_4) {
1330 int cg_mode = kCGBlendModeNormal;
1331 switch (mode) {
1332 case QPainter::CompositionMode_Multiply:
1333 cg_mode = kCGBlendModeMultiply;
1334 break;
1335 case QPainter::CompositionMode_Screen:
1336 cg_mode = kCGBlendModeScreen;
1337 break;
1338 case QPainter::CompositionMode_Overlay:
1339 cg_mode = kCGBlendModeOverlay;
1340 break;
1341 case QPainter::CompositionMode_Darken:
1342 cg_mode = kCGBlendModeDarken;
1343 break;
1344 case QPainter::CompositionMode_Lighten:
1345 cg_mode = kCGBlendModeLighten;
1346 break;
1347 case QPainter::CompositionMode_ColorDodge:
1348 cg_mode = kCGBlendModeColorDodge;
1349 break;
1350 case QPainter::CompositionMode_ColorBurn:
1351 cg_mode = kCGBlendModeColorBurn;
1352 break;
1353 case QPainter::CompositionMode_HardLight:
1354 cg_mode = kCGBlendModeHardLight;
1355 break;
1356 case QPainter::CompositionMode_SoftLight:
1357 cg_mode = kCGBlendModeSoftLight;
1358 break;
1359 case QPainter::CompositionMode_Difference:
1360 cg_mode = kCGBlendModeDifference;
1361 break;
1362 case QPainter::CompositionMode_Exclusion:
1363 cg_mode = kCGBlendModeExclusion;
1364 break;
1365 case QPainter::CompositionMode_Plus:
1366 needPrivateAPI = true;
1367 cg_mode = kCGCompositeModePlusLighter;
1368 break;
1369 default:
1370 break;
1371 }
1372 if (!needPrivateAPI)
1373 CGContextSetBlendMode(d_func()->hd, CGBlendMode(cg_mode));
1374 else
1375 CGContextSetCompositeOperation(d_func()->hd, CGCompositeMode(cg_mode));
1376 }
1377#endif
1378 }
1379}
1380
1381void
1382QCoreGraphicsPaintEngine::updateRenderHints(QPainter::RenderHints hints)
1383{
1384 Q_D(QCoreGraphicsPaintEngine);
1385 CGContextSetShouldAntialias(d->hd, hints & QPainter::Antialiasing);
1386 static const CGFloat ScaleFactor = qt_mac_get_scalefactor();
1387 if (ScaleFactor > 1.) {
1388 CGContextSetInterpolationQuality(d->hd, kCGInterpolationHigh);
1389 } else {
1390 CGContextSetInterpolationQuality(d->hd, (hints & QPainter::SmoothPixmapTransform) ?
1391 kCGInterpolationHigh : kCGInterpolationNone);
1392 }
1393 bool textAntialiasing = (hints & QPainter::TextAntialiasing) == QPainter::TextAntialiasing;
1394 if (!textAntialiasing || d->disabledSmoothFonts) {
1395 d->disabledSmoothFonts = !textAntialiasing;
1396 CGContextSetShouldSmoothFonts(d->hd, textAntialiasing);
1397 }
1398}
1399
1400/*
1401 Returns the size of one device pixel in user-space coordinates.
1402*/
1403QPointF QCoreGraphicsPaintEnginePrivate::devicePixelSize(CGContextRef)
1404{
1405 QPointF p1 = current.transform.inverted().map(QPointF(0, 0));
1406 QPointF p2 = current.transform.inverted().map(QPointF(1, 1));
1407 return QPointF(qAbs(p2.x() - p1.x()), qAbs(p2.y() - p1.y()));
1408}
1409
1410/*
1411 Adjusts the pen width so we get correct line widths in the
1412 non-transformed, aliased case.
1413*/
1414float QCoreGraphicsPaintEnginePrivate::adjustPenWidth(float penWidth)
1415{
1416 Q_Q(QCoreGraphicsPaintEngine);
1417 float ret = penWidth;
1418 if (!complexXForm && !(q->state->renderHints() & QPainter::Antialiasing)) {
1419 if (penWidth < 2)
1420 ret = 1;
1421 else if (penWidth < 3)
1422 ret = 1.5;
1423 else
1424 ret = penWidth -1;
1425 }
1426 return ret;
1427}
1428
1429void
1430QCoreGraphicsPaintEnginePrivate::setStrokePen(const QPen &pen)
1431{
1432 //pencap
1433 CGLineCap cglinecap = kCGLineCapButt;
1434 if(pen.capStyle() == Qt::SquareCap)
1435 cglinecap = kCGLineCapSquare;
1436 else if(pen.capStyle() == Qt::RoundCap)
1437 cglinecap = kCGLineCapRound;
1438 CGContextSetLineCap(hd, cglinecap);
1439 CGContextSetLineWidth(hd, adjustPenWidth(pen.widthF()));
1440
1441 //join
1442 CGLineJoin cglinejoin = kCGLineJoinMiter;
1443 if(pen.joinStyle() == Qt::BevelJoin)
1444 cglinejoin = kCGLineJoinBevel;
1445 else if(pen.joinStyle() == Qt::RoundJoin)
1446 cglinejoin = kCGLineJoinRound;
1447 CGContextSetLineJoin(hd, cglinejoin);
1448// CGContextSetMiterLimit(hd, pen.miterLimit());
1449
1450 //pen style
1451 QVector<CGFloat> linedashes;
1452 if(pen.style() == Qt::CustomDashLine) {
1453 QVector<qreal> customs = pen.dashPattern();
1454 for(int i = 0; i < customs.size(); ++i)
1455 linedashes.append(customs.at(i));
1456 } else if(pen.style() == Qt::DashLine) {
1457 linedashes.append(4);
1458 linedashes.append(2);
1459 } else if(pen.style() == Qt::DotLine) {
1460 linedashes.append(1);
1461 linedashes.append(2);
1462 } else if(pen.style() == Qt::DashDotLine) {
1463 linedashes.append(4);
1464 linedashes.append(2);
1465 linedashes.append(1);
1466 linedashes.append(2);
1467 } else if(pen.style() == Qt::DashDotDotLine) {
1468 linedashes.append(4);
1469 linedashes.append(2);
1470 linedashes.append(1);
1471 linedashes.append(2);
1472 linedashes.append(1);
1473 linedashes.append(2);
1474 }
1475 const CGFloat cglinewidth = pen.widthF() <= 0.0f ? 1.0f : float(pen.widthF());
1476 for(int i = 0; i < linedashes.size(); ++i) {
1477 linedashes[i] *= cglinewidth;
1478 if(cglinewidth < 3 && (cglinecap == kCGLineCapSquare || cglinecap == kCGLineCapRound)) {
1479 if((i%2))
1480 linedashes[i] += cglinewidth/2;
1481 else
1482 linedashes[i] -= cglinewidth/2;
1483 }
1484 }
1485 CGContextSetLineDash(hd, pen.dashOffset() * cglinewidth, linedashes.data(), linedashes.size());
1486
1487 // color
1488 CGContextSetStrokeColorWithColor(hd, cgColorForQColor(pen.color(), pdev));
1489}
1490
1491// Add our own patterns here to deal with the fact that the coordinate system
1492// is flipped vertically with Quartz2D.
1493static const uchar *qt_mac_patternForBrush(int brushStyle)
1494{
1495 Q_ASSERT(brushStyle > Qt::SolidPattern && brushStyle < Qt::LinearGradientPattern);
1496 static const uchar dense1_pat[] = { 0x00, 0x00, 0x44, 0x00, 0x00, 0x00, 0x44, 0x00 };
1497 static const uchar dense2_pat[] = { 0x00, 0x22, 0x00, 0x88, 0x00, 0x22, 0x00, 0x88 };
1498 static const uchar dense3_pat[] = { 0x11, 0xaa, 0x44, 0xaa, 0x11, 0xaa, 0x44, 0xaa };
1499 static const uchar dense4_pat[] = { 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55 };
1500 static const uchar dense5_pat[] = { 0xee, 0x55, 0xbb, 0x55, 0xee, 0x55, 0xbb, 0x55 };
1501 static const uchar dense6_pat[] = { 0xff, 0xdd, 0xff, 0x77, 0xff, 0xdd, 0xff, 0x77 };
1502 static const uchar dense7_pat[] = { 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb, 0xff };
1503 static const uchar hor_pat[] = { 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff };
1504 static const uchar ver_pat[] = { 0xef, 0xef, 0xef, 0xef, 0xef, 0xef, 0xef, 0xef };
1505 static const uchar cross_pat[] = { 0xef, 0xef, 0xef, 0xef, 0x00, 0xef, 0xef, 0xef };
1506 static const uchar fdiag_pat[] = { 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0xfe };
1507 static const uchar bdiag_pat[] = { 0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f };
1508 static const uchar dcross_pat[] = { 0x7e, 0xbd, 0xdb, 0xe7, 0xe7, 0xdb, 0xbd, 0x7e };
1509 static const uchar *const pat_tbl[] = {
1510 dense1_pat, dense2_pat, dense3_pat, dense4_pat, dense5_pat,
1511 dense6_pat, dense7_pat,
1512 hor_pat, ver_pat, cross_pat, bdiag_pat, fdiag_pat, dcross_pat };
1513 return pat_tbl[brushStyle - Qt::Dense1Pattern];
1514}
1515
1516void QCoreGraphicsPaintEnginePrivate::setFillBrush(const QPointF &offset)
1517{
1518 // pattern
1519 Qt::BrushStyle bs = current.brush.style();
1520#ifdef QT_MAC_USE_NATIVE_GRADIENTS
1521 if (bs == Qt::LinearGradientPattern || bs == Qt::RadialGradientPattern) {
1522 const QGradient *grad = static_cast<const QGradient*>(current.brush.gradient());
1523 if (drawGradientNatively(grad)) {
1524 Q_ASSERT(grad->spread() == QGradient::PadSpread);
1525
1526 static const CGFloat domain[] = { 0.0f, +1.0f };
1527 static const CGFunctionCallbacks callbacks = { 0, qt_mac_color_gradient_function, 0 };
1528 CGFunctionRef fill_func = CGFunctionCreate(reinterpret_cast<void *>(&current.brush),
1529 1, domain, 4, 0, &callbacks);
1530
1531 CGColorSpaceRef colorspace = qt_mac_colorSpaceForDeviceType(pdev);
1532 if (bs == Qt::LinearGradientPattern) {
1533 const QLinearGradient *linearGrad = static_cast<const QLinearGradient *>(grad);
1534 const QPointF start(linearGrad->start());
1535 const QPointF stop(linearGrad->finalStop());
1536 shading = CGShadingCreateAxial(colorspace, CGPointMake(start.x(), start.y()),
1537 CGPointMake(stop.x(), stop.y()), fill_func, true, true);
1538 } else {
1539 Q_ASSERT(bs == Qt::RadialGradientPattern);
1540 const QRadialGradient *radialGrad = static_cast<const QRadialGradient *>(grad);
1541 QPointF center(radialGrad->center());
1542 QPointF focal(radialGrad->focalPoint());
1543 qreal radius = radialGrad->radius();
1544 shading = CGShadingCreateRadial(colorspace, CGPointMake(focal.x(), focal.y()),
1545 0.0, CGPointMake(center.x(), center.y()), radius, fill_func, false, true);
1546 }
1547
1548 CGFunctionRelease(fill_func);
1549 }
1550 } else
1551#endif
1552 if(bs != Qt::SolidPattern && bs != Qt::NoBrush
1553#ifndef QT_MAC_USE_NATIVE_GRADIENTS
1554 && (bs < Qt::LinearGradientPattern || bs > Qt::ConicalGradientPattern)
1555#endif
1556 )
1557 {
1558 QMacPattern *qpattern = new QMacPattern;
1559 qpattern->pdev = pdev;
1560 CGFloat components[4] = { 1.0, 1.0, 1.0, 1.0 };
1561 CGColorSpaceRef base_colorspace = 0;
1562 if(bs == Qt::TexturePattern) {
1563 qpattern->data.pixmap = current.brush.texture();
1564 if(qpattern->data.pixmap.isQBitmap()) {
1565 const QColor &col = current.brush.color();
1566 components[0] = qt_mac_convert_color_to_cg(col.red());
1567 components[1] = qt_mac_convert_color_to_cg(col.green());
1568 components[2] = qt_mac_convert_color_to_cg(col.blue());
1569 base_colorspace = QCoreGraphicsPaintEngine::macGenericColorSpace();
1570 }
1571 } else {
1572 qpattern->as_mask = true;
1573
1574 qpattern->data.bytes = qt_mac_patternForBrush(bs);
1575 const QColor &col = current.brush.color();
1576 components[0] = qt_mac_convert_color_to_cg(col.red());
1577 components[1] = qt_mac_convert_color_to_cg(col.green());
1578 components[2] = qt_mac_convert_color_to_cg(col.blue());
1579 base_colorspace = QCoreGraphicsPaintEngine::macGenericColorSpace();
1580 }
1581 int width = qpattern->width(), height = qpattern->height();
1582 qpattern->foreground = current.brush.color();
1583
1584 CGColorSpaceRef fill_colorspace = CGColorSpaceCreatePattern(base_colorspace);
1585 CGContextSetFillColorSpace(hd, fill_colorspace);
1586
1587 CGAffineTransform xform = CGContextGetCTM(hd);
1588 xform = CGAffineTransformConcat(qt_mac_convert_transform_to_cg(current.brush.transform()), xform);
1589 xform = CGAffineTransformTranslate(xform, offset.x(), offset.y());
1590
1591 CGPatternCallbacks callbks;
1592 callbks.version = 0;
1593 callbks.drawPattern = qt_mac_draw_pattern;
1594 callbks.releaseInfo = qt_mac_dispose_pattern;
1595 CGPatternRef fill_pattern = CGPatternCreate(qpattern, CGRectMake(0, 0, width, height),
1596 xform, width, height, kCGPatternTilingNoDistortion,
1597 !base_colorspace, &callbks);
1598 CGContextSetFillPattern(hd, fill_pattern, components);
1599
1600 CGPatternRelease(fill_pattern);
1601 CGColorSpaceRelease(fill_colorspace);
1602 } else if(bs != Qt::NoBrush) {
1603 CGContextSetFillColorWithColor(hd, cgColorForQColor(current.brush.color(), pdev));
1604 }
1605}
1606
1607void
1608QCoreGraphicsPaintEnginePrivate::setClip(const QRegion *rgn)
1609{
1610 Q_Q(QCoreGraphicsPaintEngine);
1611 if(hd) {
1612 resetClip();
1613 QRegion sysClip = q->systemClip();
1614 if(!sysClip.isEmpty())
1615 qt_mac_clip_cg(hd, sysClip, &orig_xform);
1616 if(rgn)
1617 qt_mac_clip_cg(hd, *rgn, 0);
1618 }
1619}
1620
1621struct qt_mac_cg_transform_path {
1622 CGMutablePathRef path;
1623 CGAffineTransform transform;
1624};
1625
1626void qt_mac_cg_transform_path_apply(void *info, const CGPathElement *element)
1627{
1628 Q_ASSERT(info && element);
1629 qt_mac_cg_transform_path *t = (qt_mac_cg_transform_path*)info;
1630 switch(element->type) {
1631 case kCGPathElementMoveToPoint:
1632 CGPathMoveToPoint(t->path, &t->transform, element->points[0].x, element->points[0].y);
1633 break;
1634 case kCGPathElementAddLineToPoint:
1635 CGPathAddLineToPoint(t->path, &t->transform, element->points[0].x, element->points[0].y);
1636 break;
1637 case kCGPathElementAddQuadCurveToPoint:
1638 CGPathAddQuadCurveToPoint(t->path, &t->transform, element->points[0].x, element->points[0].y,
1639 element->points[1].x, element->points[1].y);
1640 break;
1641 case kCGPathElementAddCurveToPoint:
1642 CGPathAddCurveToPoint(t->path, &t->transform, element->points[0].x, element->points[0].y,
1643 element->points[1].x, element->points[1].y,
1644 element->points[2].x, element->points[2].y);
1645 break;
1646 case kCGPathElementCloseSubpath:
1647 CGPathCloseSubpath(t->path);
1648 break;
1649 default:
1650 qDebug() << "Unhandled path transform type: " << element->type;
1651 }
1652}
1653
1654void QCoreGraphicsPaintEnginePrivate::drawPath(uchar ops, CGMutablePathRef path)
1655{
1656 Q_Q(QCoreGraphicsPaintEngine);
1657 Q_ASSERT((ops & (CGFill | CGEOFill)) != (CGFill | CGEOFill)); //can't really happen
1658 if((ops & (CGFill | CGEOFill))) {
1659 if (shading) {
1660 Q_ASSERT(path);
1661 CGContextBeginPath(hd);
1662 CGContextAddPath(hd, path);
1663 saveGraphicsState();
1664 if (ops & CGFill)
1665 CGContextClip(hd);
1666 else if (ops & CGEOFill)
1667 CGContextEOClip(hd);
1668 if (current.brush.gradient()->coordinateMode() == QGradient::ObjectBoundingMode) {
1669 CGRect boundingBox = CGPathGetBoundingBox(path);
1670 CGContextConcatCTM(hd,
1671 CGAffineTransformMake(boundingBox.size.width, 0,
1672 0, boundingBox.size.height,
1673 boundingBox.origin.x, boundingBox.origin.y));
1674 }
1675 CGContextDrawShading(hd, shading);
1676 restoreGraphicsState();
1677 ops &= ~CGFill;
1678 ops &= ~CGEOFill;
1679 } else if (current.brush.style() == Qt::NoBrush) {
1680 ops &= ~CGFill;
1681 ops &= ~CGEOFill;
1682 }
1683 }
1684 if((ops & CGStroke) && current.pen.style() == Qt::NoPen)
1685 ops &= ~CGStroke;
1686
1687 if(ops & (CGEOFill | CGFill)) {
1688 CGContextBeginPath(hd);
1689 CGContextAddPath(hd, path);
1690 if (ops & CGEOFill) {
1691 CGContextEOFillPath(hd);
1692 } else {
1693 CGContextFillPath(hd);
1694 }
1695 }
1696
1697 // Avoid saving and restoring the context if we can.
1698 const bool needContextSave = (cosmeticPen != QCoreGraphicsPaintEnginePrivate::CosmeticNone ||
1699 !(q->state->renderHints() & QPainter::Antialiasing));
1700 if(ops & CGStroke) {
1701 if (needContextSave)
1702 saveGraphicsState();
1703 CGContextBeginPath(hd);
1704
1705 // Translate a fraction of a pixel size in the y direction
1706 // to make sure that primitives painted at pixel borders
1707 // fills the right pixel. This is needed since the y xais
1708 // in the Quartz coordinate system is inverted compared to Qt.
1709 if (!(q->state->renderHints() & QPainter::Antialiasing)) {
1710 if (current.pen.style() == Qt::SolidLine || current.pen.width() >= 3)
1711 CGContextTranslateCTM(hd, double(pixelSize.x()) * 0.25, double(pixelSize.y()) * 0.25);
1712 else if (current.pen.style() == Qt::DotLine && QSysInfo::MacintoshVersion == QSysInfo::MV_10_3)
1713 ; // Do nothing.
1714 else
1715 CGContextTranslateCTM(hd, 0, double(pixelSize.y()) * 0.1);
1716 }
1717
1718 if (cosmeticPen != QCoreGraphicsPaintEnginePrivate::CosmeticNone) {
1719 // If antialiazing is enabled, use the cosmetic pen size directly.
1720 if (q->state->renderHints() & QPainter::Antialiasing)
1721 CGContextSetLineWidth(hd, cosmeticPenSize);
1722 else if (current.pen.widthF() <= 1)
1723 CGContextSetLineWidth(hd, cosmeticPenSize * 0.9f);
1724 else
1725 CGContextSetLineWidth(hd, cosmeticPenSize);
1726 }
1727 if(cosmeticPen == QCoreGraphicsPaintEnginePrivate::CosmeticTransformPath) {
1728 qt_mac_cg_transform_path t;
1729 t.transform = qt_mac_convert_transform_to_cg(current.transform);
1730 t.path = CGPathCreateMutable();
1731 CGPathApply(path, &t, qt_mac_cg_transform_path_apply); //transform the path
1732 setTransform(0); //unset the context transform
1733 CGContextSetLineWidth(hd, cosmeticPenSize);
1734 CGContextAddPath(hd, t.path);
1735 CGPathRelease(t.path);
1736 } else {
1737 CGContextAddPath(hd, path);
1738 }
1739
1740 CGContextStrokePath(hd);
1741 if (needContextSave)
1742 restoreGraphicsState();
1743 }
1744}
1745
1746QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.