Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qpaintengine_mac.mm
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include <AppKit/AppKit.h>
6#include <CoreGraphics/CoreGraphics.h>
7
10
11#include <qbitmap.h>
12#include <qpaintdevice.h>
13#include <qpainterpath.h>
14#include <qpixmapcache.h>
15#include <private/qpaintengine_raster_p.h>
16#include <qprinter.h>
17#include <qstack.h>
18#include <qwidget.h>
19#include <qvarlengtharray.h>
20#include <qdebug.h>
21#include <qcoreapplication.h>
22#include <qmath.h>
23
24#include <qpa/qplatformpixmap.h>
25
26#include <private/qfont_p.h>
27#include <private/qfontengine_p.h>
28#include <private/qfontengine_coretext_p.h>
29#include <private/qnumeric_p.h>
30#include <private/qpainter_p.h>
31#include <private/qpainterpath_p.h>
32#include <private/qtextengine_p.h>
33#include <private/qcoregraphics_p.h>
34
35#include <string.h>
36
38
39/*****************************************************************************
40 QCoreGraphicsPaintEngine utility functions
41 *****************************************************************************/
42
43void qt_mac_cgimage_data_free(void *, const void *memoryToFree, size_t)
44{
45 free(const_cast<void *>(memoryToFree));
46}
47
48CGImageRef qt_mac_create_imagemask(const QPixmap &pixmap, const QRectF &sr)
49{
50 QImage image = pixmap.toImage();
51 if (image.format() != QImage::Format_ARGB32_Premultiplied)
52 image = std::move(image).convertToFormat(QImage::Format_ARGB32_Premultiplied);
53
54 const int sx = qRound(sr.x()), sy = qRound(sr.y()), sw = qRound(sr.width()), sh = qRound(sr.height());
55 const qsizetype sbpr = image.bytesPerLine();
56 const uint nbytes = sw * sh;
57 // alpha is always 255 for bitmaps, ignore it in this case.
58 const quint32 mask = pixmap.depth() == 1 ? 0x00ffffff : 0xffffffff;
59 quint8 *dptr = static_cast<quint8 *>(malloc(nbytes));
60 quint32 *sptr = reinterpret_cast<quint32 *>(image.scanLine(0)), *srow;
61 for (int y = sy, offset=0; y < sh; ++y) {
62 srow = sptr + (y * (sbpr / 4));
63 for (int x = sx; x < sw; ++x)
64 *(dptr+(offset++)) = (*(srow+x) & mask) ? 255 : 0;
65 }
66 QCFType<CGDataProviderRef> provider = CGDataProviderCreateWithData(nullptr, dptr, nbytes, qt_mac_cgimage_data_free);
67 return CGImageMaskCreate(sw, sh, 8, 8, nbytes / sh, provider, nullptr, false);
68}
69
70//conversion
71inline static float qt_mac_convert_color_to_cg(int c) { return ((float)c * 1000 / 255) / 1000; }
73 return CGAffineTransformMake(t.m11(), t.m12(), t.m21(), t.m22(), t.dx(), t.dy());
74}
75
76inline static QCFType<CGColorRef> cgColorForQColor(const QColor &col)
77{
78 CGFloat components[] = {
79 qt_mac_convert_color_to_cg(col.red()),
80 qt_mac_convert_color_to_cg(col.green()),
81 qt_mac_convert_color_to_cg(col.blue()),
82 qt_mac_convert_color_to_cg(col.alpha())
83 };
84 QCFType<CGColorSpaceRef> colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
85 return CGColorCreate(colorSpace, components);
86}
87
88// There's architectural problems with using native gradients
89// on the Mac at the moment, so disable them.
90// #define QT_MAC_USE_NATIVE_GRADIENTS
91
92#ifdef QT_MAC_USE_NATIVE_GRADIENTS
93static bool drawGradientNatively(const QGradient *gradient)
94{
95 return gradient->spread() == QGradient::PadSpread;
96}
97
98// gradiant callback
99static void qt_mac_color_gradient_function(void *info, const CGFloat *in, CGFloat *out)
100{
101 QBrush *brush = static_cast<QBrush *>(info);
102 Q_ASSERT(brush && brush->gradient());
103
104 const QGradientStops stops = brush->gradient()->stops();
105 const int n = stops.count();
106 Q_ASSERT(n >= 1);
107 const QGradientStop *begin = stops.constBegin();
108 const QGradientStop *end = begin + n;
109
110 qreal p = in[0];
111 const QGradientStop *i = begin;
112 while (i != end && i->first < p)
113 ++i;
114
115 QRgb c;
116 if (i == begin) {
117 c = begin->second.rgba();
118 } else if (i == end) {
119 c = (end - 1)->second.rgba();
120 } else {
121 const QGradientStop &s1 = *(i - 1);
122 const QGradientStop &s2 = *i;
123 qreal p1 = s1.first;
124 qreal p2 = s2.first;
125 QRgb c1 = s1.second.rgba();
126 QRgb c2 = s2.second.rgba();
127 int idist = 256 * (p - p1) / (p2 - p1);
128 int dist = 256 - idist;
129 c = qRgba(INTERPOLATE_PIXEL_256(qRed(c1), dist, qRed(c2), idist),
130 INTERPOLATE_PIXEL_256(qGreen(c1), dist, qGreen(c2), idist),
131 INTERPOLATE_PIXEL_256(qBlue(c1), dist, qBlue(c2), idist),
132 INTERPOLATE_PIXEL_256(qAlpha(c1), dist, qAlpha(c2), idist));
133 }
134
135 out[0] = qt_mac_convert_color_to_cg(qRed(c));
136 out[1] = qt_mac_convert_color_to_cg(qGreen(c));
137 out[2] = qt_mac_convert_color_to_cg(qBlue(c));
138 out[3] = qt_mac_convert_color_to_cg(qAlpha(c));
139}
140#endif
141
142//clipping handling
144{
145 static bool inReset = false;
146 if (inReset)
147 return;
148 inReset = true;
149
150 CGAffineTransform old_xform = CGContextGetCTM(hd);
151
152 //setup xforms
153 CGContextConcatCTM(hd, CGAffineTransformInvert(old_xform));
154 while (stackCount > 0) {
156 }
158 inReset = false;
159 //reset xforms
160 CGContextConcatCTM(hd, CGAffineTransformInvert(CGContextGetCTM(hd)));
161 CGContextConcatCTM(hd, old_xform);
162}
163
164static CGRect qt_mac_compose_rect(const QRectF &r, float off=0)
165{
166 return CGRectMake(r.x()+off, r.y()+off, r.width(), r.height());
167}
168
169static CGMutablePathRef qt_mac_compose_path(const QPainterPath &p, float off=0)
170{
171 CGMutablePathRef ret = CGPathCreateMutable();
172 QPointF startPt;
173 for (int i=0; i<p.elementCount(); ++i) {
174 const QPainterPath::Element &elm = p.elementAt(i);
175 switch (elm.type) {
176 case QPainterPath::MoveToElement:
177 if (i > 0
178 && p.elementAt(i - 1).x == startPt.x()
179 && p.elementAt(i - 1).y == startPt.y())
180 CGPathCloseSubpath(ret);
181 startPt = QPointF(elm.x, elm.y);
182 CGPathMoveToPoint(ret, 0, elm.x+off, elm.y+off);
183 break;
184 case QPainterPath::LineToElement:
185 CGPathAddLineToPoint(ret, 0, elm.x+off, elm.y+off);
186 break;
187 case QPainterPath::CurveToElement:
188 Q_ASSERT(p.elementAt(i+1).type == QPainterPath::CurveToDataElement);
189 Q_ASSERT(p.elementAt(i+2).type == QPainterPath::CurveToDataElement);
190 CGPathAddCurveToPoint(ret, 0,
191 elm.x+off, elm.y+off,
192 p.elementAt(i+1).x+off, p.elementAt(i+1).y+off,
193 p.elementAt(i+2).x+off, p.elementAt(i+2).y+off);
194 i+=2;
195 break;
196 default:
197 qFatal("QCoreGraphicsPaintEngine::drawPath(), unhandled type: %d", elm.type);
198 break;
199 }
200 }
201 if (!p.isEmpty()
202 && p.elementAt(p.elementCount() - 1).x == startPt.x()
203 && p.elementAt(p.elementCount() - 1).y == startPt.y())
204 CGPathCloseSubpath(ret);
205 return ret;
206}
207
208//pattern handling (tiling)
209#if 1
210# define QMACPATTERN_MASK_MULTIPLIER 32
211#else
212# define QMACPATTERN_MASK_MULTIPLIER 1
213#endif
215{
216public:
217 QMacPattern() : as_mask(false), pdev(0), image(0) { data.bytes = 0; }
218 ~QMacPattern() { CGImageRelease(image); }
219 int width() {
220 if (image)
221 return CGImageGetWidth(image);
222 if (data.bytes)
224 return data.pixmap.width();
225 }
226 int height() {
227 if (image)
228 return CGImageGetHeight(image);
229 if (data.bytes)
231 return data.pixmap.height();
232 }
233
234 //input
237 struct {
238 QPixmap pixmap;
239 const uchar *bytes;
240 } data;
242 //output
244};
245static void qt_mac_draw_pattern(void *info, CGContextRef c)
246{
247 QMacPattern *pat = (QMacPattern*)info;
248 int w = 0, h = 0;
249 bool isBitmap = (pat->data.pixmap.depth() == 1);
250 if (!pat->image) { //lazy cache
251 if (pat->as_mask) {
252 Q_ASSERT(pat->data.bytes);
253 w = h = 8;
255 CGDataProviderRef provider = CGDataProviderCreateWithData(nullptr, pat->data.bytes, w*h, nullptr);
256 pat->image = CGImageMaskCreate(w, h, 1, 1, 1, provider, nullptr, false);
257 CGDataProviderRelease(provider);
258#else
259 const int numBytes = (w*h)/sizeof(uchar);
260 QVarLengthArray<uchar> xor_bytes(numBytes);
261 for (int i = 0; i < numBytes; ++i)
262 xor_bytes[i] = pat->data.bytes[i] ^ 0xFF;
263 CGDataProviderRef provider = CGDataProviderCreateWithData(nullptr, xor_bytes.constData(), w*h, nullptr);
264 CGImageRef swatch = CGImageMaskCreate(w, h, 1, 1, 1, provider, nullptr, false);
265 CGDataProviderRelease(provider);
266
267 const QColor c0(0, 0, 0, 0), c1(255, 255, 255, 255);
269 pm.fill(c0);
270 QMacCGContext pm_ctx(&pm);
271 CGContextSetFillColorWithColor(c, cgColorForQColor(c1));
272 CGRect rect = CGRectMake(0, 0, w, h);
273 for (int x = 0; x < QMACPATTERN_MASK_MULTIPLIER; ++x) {
274 rect.origin.x = x * w;
275 for (int y = 0; y < QMACPATTERN_MASK_MULTIPLIER; ++y) {
276 rect.origin.y = y * h;
277 qt_mac_drawCGImage(pm_ctx, &rect, swatch);
278 }
279 }
280 pat->image = qt_mac_create_imagemask(pm, pm.rect());
281 CGImageRelease(swatch);
284#endif
285 } else {
286 w = pat->data.pixmap.width();
287 h = pat->data.pixmap.height();
288 if (isBitmap)
289 pat->image = qt_mac_create_imagemask(pat->data.pixmap, pat->data.pixmap.rect());
290 else
291 pat->image = qt_mac_toCGImage(pat->data.pixmap.toImage());
292 }
293 } else {
294 w = CGImageGetWidth(pat->image);
295 h = CGImageGetHeight(pat->image);
296 }
297
298 //draw
299 bool needRestore = false;
300 if (CGImageIsMask(pat->image)) {
301 CGContextSaveGState(c);
302 CGContextSetFillColorWithColor(c, cgColorForQColor(pat->foreground));
303 }
304 CGRect rect = CGRectMake(0, 0, w, h);
305 qt_mac_drawCGImage(c, &rect, pat->image);
306 if (needRestore)
307 CGContextRestoreGState(c);
308}
309static void qt_mac_dispose_pattern(void *info)
310{
311 QMacPattern *pat = (QMacPattern*)info;
312 delete pat;
313}
314
315/*****************************************************************************
316 QCoreGraphicsPaintEngine member functions
317 *****************************************************************************/
318
320{
321 return QPaintEngine::PaintEngineFeatures(QPaintEngine::AllFeatures & ~QPaintEngine::PaintOutsidePaintEvent
322 & ~QPaintEngine::PerspectiveTransform
323 & ~QPaintEngine::ConicalGradientFill
324 & ~QPaintEngine::LinearGradientFill
325 & ~QPaintEngine::RadialGradientFill
326 & ~QPaintEngine::BrushStroke);
327}
328
329QCoreGraphicsPaintEngine::QCoreGraphicsPaintEngine()
330: QPaintEngine(*(new QCoreGraphicsPaintEnginePrivate), qt_mac_cg_features())
331{
332}
333
338
342
343bool
344QCoreGraphicsPaintEngine::begin(QPaintDevice *pdev)
345{
346 Q_D(QCoreGraphicsPaintEngine);
347 if (isActive()) { // already active painting
348 qWarning("QCoreGraphicsPaintEngine::begin: Painter already active");
349 return false;
350 }
351
352 //initialization
353 d->pdev = pdev;
354 d->complexXForm = false;
355 d->cosmeticPen = QCoreGraphicsPaintEnginePrivate::CosmeticSetPenWidth;
356 d->cosmeticPenSize = 1;
357 d->current.clipEnabled = false;
358 d->pixelSize = QPoint(1,1);
359
360 if (pdev->devType() != QInternal::Printer) {
361 QMacCGContext ctx(pdev);
362 d->hd = CGContextRetain(ctx);
363 if (d->hd) {
364 d->saveGraphicsState();
365 d->orig_xform = CGContextGetCTM(d->hd);
366 if (d->shading) {
367 CGShadingRelease(d->shading);
368 d->shading = nullptr;
369 }
370 d->setClip(nullptr); //clear the context's clipping
371 }
372 }
373
374 setActive(true);
375
376 if (d->pdev->devType() == QInternal::Widget) { // device is a widget
377 QWidget *w = (QWidget*)d->pdev;
378 bool unclipped = w->testAttribute(Qt::WA_PaintUnclipped);
379
380 if (unclipped) {
381 qWarning("QCoreGraphicsPaintEngine::begin: Does not support unclipped painting");
382 }
383 } else if (d->pdev->devType() == QInternal::Pixmap) { // device is a pixmap
384 QPixmap *pm = (QPixmap*)d->pdev;
385 if (pm->isNull()) {
386 qWarning("QCoreGraphicsPaintEngine::begin: Cannot paint null pixmap");
387 end();
388 return false;
389 }
390 }
391
392 setDirty(QPaintEngine::DirtyPen);
393 setDirty(QPaintEngine::DirtyBrush);
394 setDirty(QPaintEngine::DirtyBackground);
395 setDirty(QPaintEngine::DirtyHints);
396 return true;
397}
398
399bool
401{
402 Q_D(QCoreGraphicsPaintEngine);
403 setActive(false);
404 if (d->shading) {
405 CGShadingRelease(d->shading);
406 d->shading = 0;
407 }
408 d->pdev = nullptr;
409 if (d->hd) {
410 d->restoreGraphicsState();
411 CGContextSynchronize(d->hd);
412 CGContextRelease(d->hd);
413 d->hd = nullptr;
414 }
415 return true;
416}
417
418void
419QCoreGraphicsPaintEngine::updateState(const QPaintEngineState &state)
420{
421 Q_D(QCoreGraphicsPaintEngine);
422 QPaintEngine::DirtyFlags flags = state.state();
423
424 if (flags & DirtyTransform)
425 updateMatrix(state.transform());
426
427 if (flags & DirtyClipEnabled) {
428 if (state.isClipEnabled())
429 updateClipPath(painter()->clipPath(), Qt::ReplaceClip);
430 else
431 updateClipPath(QPainterPath(), Qt::NoClip);
432 }
433
434 if (flags & DirtyClipPath) {
435 updateClipPath(state.clipPath(), state.clipOperation());
436 } else if (flags & DirtyClipRegion) {
437 updateClipRegion(state.clipRegion(), state.clipOperation());
438 }
439
440 // If the clip has changed we need to update all other states
441 // too, since they are included in the system context on OSX,
442 // and changing the clip resets that context back to scratch.
443 if (flags & (DirtyClipPath | DirtyClipRegion | DirtyClipEnabled))
444 flags |= AllDirty;
445
446 if (flags & DirtyPen)
447 updatePen(state.pen());
448 if (flags & (DirtyBrush|DirtyBrushOrigin))
449 updateBrush(state.brush(), state.brushOrigin());
450 if (flags & DirtyFont)
451 updateFont(state.font());
452 if (flags & DirtyOpacity)
453 updateOpacity(state.opacity());
454 if (flags & DirtyHints)
455 updateRenderHints(state.renderHints());
456 if (flags & DirtyCompositionMode)
457 updateCompositionMode(state.compositionMode());
458
459 if (flags & (DirtyPen | DirtyTransform | DirtyHints)) {
460 if (!d->current.pen.isCosmetic()) {
461 d->cosmeticPen = QCoreGraphicsPaintEnginePrivate::CosmeticNone;
462 } else if (d->current.transform.m11() < d->current.transform.m22()-1.0 ||
463 d->current.transform.m11() > d->current.transform.m22()+1.0) {
464 d->cosmeticPen = QCoreGraphicsPaintEnginePrivate::CosmeticTransformPath;
465 d->cosmeticPenSize = d->adjustPenWidth(d->current.pen.widthF());
466 if (!d->cosmeticPenSize)
467 d->cosmeticPenSize = 1.0;
468 } else {
469 d->cosmeticPen = QCoreGraphicsPaintEnginePrivate::CosmeticSetPenWidth;
470 static const float sqrt2 = std::sqrt(2.0f);
471 qreal width = d->current.pen.widthF();
472 if (!width)
473 width = 1;
474 d->cosmeticPenSize = std::sqrt(std::pow(d->pixelSize.y(), 2) + std::pow(d->pixelSize.x(), 2)) / sqrt2 * width;
475 }
476 }
477}
478
479void
480QCoreGraphicsPaintEngine::updatePen(const QPen &pen)
481{
482 Q_D(QCoreGraphicsPaintEngine);
483 Q_ASSERT(isActive());
484 d->current.pen = pen;
485 d->setStrokePen(pen);
486}
487
488void
489QCoreGraphicsPaintEngine::updateBrush(const QBrush &brush, const QPointF &brushOrigin)
490{
491 Q_D(QCoreGraphicsPaintEngine);
492 Q_ASSERT(isActive());
493 d->current.brush = brush;
494
495#ifdef QT_MAC_USE_NATIVE_GRADIENTS
496 // Quartz supports only pad spread
497 if (const QGradient *gradient = brush.gradient()) {
498 if (drawGradientNatively(gradient)) {
499 gccaps |= QPaintEngine::LinearGradientFill | QPaintEngine::RadialGradientFill;
500 } else {
501 gccaps &= ~(QPaintEngine::LinearGradientFill | QPaintEngine::RadialGradientFill);
502 }
503 }
504#endif
505
506 if (d->shading) {
507 CGShadingRelease(d->shading);
508 d->shading = nullptr;
509 }
510 d->setFillBrush(brushOrigin);
511}
512
513void
515{
516 Q_D(QCoreGraphicsPaintEngine);
517 CGContextSetAlpha(d->hd, opacity);
518}
519
520void
521QCoreGraphicsPaintEngine::updateFont(const QFont &)
522{
523 Q_D(QCoreGraphicsPaintEngine);
524 Q_ASSERT(isActive());
525 updatePen(d->current.pen);
526}
527
528void
529QCoreGraphicsPaintEngine::updateMatrix(const QTransform &transform)
530{
531 Q_D(QCoreGraphicsPaintEngine);
532 Q_ASSERT(isActive());
533
534 if (qt_is_nan(transform.m11()) || qt_is_nan(transform.m12()) || qt_is_nan(transform.m13())
535 || qt_is_nan(transform.m21()) || qt_is_nan(transform.m22()) || qt_is_nan(transform.m23())
536 || qt_is_nan(transform.m31()) || qt_is_nan(transform.m32()) || qt_is_nan(transform.m33()))
537 return;
538
539 d->current.transform = transform;
540 d->setTransform(transform.isIdentity() ? 0 : &transform);
541 d->complexXForm = (transform.m11() != 1 || transform.m22() != 1
542 || transform.m12() != 0 || transform.m21() != 0);
543 d->pixelSize = d->devicePixelSize(d->hd);
544}
545
546void
547QCoreGraphicsPaintEngine::updateClipPath(const QPainterPath &p, Qt::ClipOperation op)
548{
549 Q_D(QCoreGraphicsPaintEngine);
550 Q_ASSERT(isActive());
551 if (op == Qt::NoClip) {
552 if (d->current.clipEnabled) {
553 d->current.clipEnabled = false;
554 d->current.clip = QRegion();
555 d->setClip(nullptr);
556 }
557 } else {
558 if (!d->current.clipEnabled)
559 op = Qt::ReplaceClip;
560 d->current.clipEnabled = true;
561 QRegion clipRegion(p.toFillPolygon().toPolygon(), p.fillRule());
562 if (op == Qt::ReplaceClip) {
563 d->current.clip = clipRegion;
564 d->setClip(nullptr);
565 if (p.isEmpty()) {
566 CGRect rect = CGRectMake(0, 0, 0, 0);
567 CGContextClipToRect(d->hd, rect);
568 } else {
569 CGMutablePathRef path = qt_mac_compose_path(p);
570 CGContextBeginPath(d->hd);
571 CGContextAddPath(d->hd, path);
572 if (p.fillRule() == Qt::WindingFill)
573 CGContextClip(d->hd);
574 else
575 CGContextEOClip(d->hd);
576 CGPathRelease(path);
577 }
578 } else if (op == Qt::IntersectClip) {
579 d->current.clip = d->current.clip.intersected(clipRegion);
580 d->setClip(&d->current.clip);
581 }
582 }
583}
584
585void
586QCoreGraphicsPaintEngine::updateClipRegion(const QRegion &clipRegion, Qt::ClipOperation op)
587{
588 Q_D(QCoreGraphicsPaintEngine);
589 Q_ASSERT(isActive());
590 if (op == Qt::NoClip) {
591 d->current.clipEnabled = false;
592 d->current.clip = QRegion();
593 d->setClip(nullptr);
594 } else {
595 if (!d->current.clipEnabled)
596 op = Qt::ReplaceClip;
597 d->current.clipEnabled = true;
598 if (op == Qt::IntersectClip)
599 d->current.clip = d->current.clip.intersected(clipRegion);
600 else if (op == Qt::ReplaceClip)
601 d->current.clip = clipRegion;
602 d->setClip(&d->current.clip);
603 }
604}
605
606void
607QCoreGraphicsPaintEngine::drawPath(const QPainterPath &p)
608{
609 Q_D(QCoreGraphicsPaintEngine);
610 Q_ASSERT(isActive());
611
612 if (state->compositionMode() == QPainter::CompositionMode_Destination)
613 return;
614
615 CGMutablePathRef path = qt_mac_compose_path(p);
617 if (p.fillRule() == Qt::WindingFill)
619 else
621 CGContextBeginPath(d->hd);
622 d->drawPath(ops, path);
623 CGPathRelease(path);
624}
625
626void
627QCoreGraphicsPaintEngine::drawRects(const QRectF *rects, int rectCount)
628{
629 Q_D(QCoreGraphicsPaintEngine);
630 Q_ASSERT(isActive());
631
632 if (state->compositionMode() == QPainter::CompositionMode_Destination)
633 return;
634
635 for (int i=0; i<rectCount; ++i) {
636 QRectF r = rects[i];
637
638 CGMutablePathRef path = CGPathCreateMutable();
639 CGPathAddRect(path, nullptr, qt_mac_compose_rect(r));
640 d->drawPath(QCoreGraphicsPaintEnginePrivate::CGFill|QCoreGraphicsPaintEnginePrivate::CGStroke,
641 path);
642 CGPathRelease(path);
643 }
644}
645
646void
647QCoreGraphicsPaintEngine::drawPoints(const QPointF *points, int pointCount)
648{
649 Q_D(QCoreGraphicsPaintEngine);
650 Q_ASSERT(isActive());
651
652 if (state->compositionMode() == QPainter::CompositionMode_Destination)
653 return;
654
655 if (d->current.pen.capStyle() == Qt::FlatCap)
656 CGContextSetLineCap(d->hd, kCGLineCapSquare);
657
658 CGMutablePathRef path = CGPathCreateMutable();
659 for (int i=0; i < pointCount; i++) {
660 float x = points[i].x(), y = points[i].y();
661 CGPathMoveToPoint(path, nullptr, x, y);
662 CGPathAddLineToPoint(path, nullptr, x+0.001, y);
663 }
664
665 bool doRestore = false;
666 if (d->cosmeticPen == QCoreGraphicsPaintEnginePrivate::CosmeticNone && !(state->renderHints() & QPainter::Antialiasing)) {
667 //we don't want adjusted pens for point rendering
668 doRestore = true;
669 d->saveGraphicsState();
670 CGContextSetLineWidth(d->hd, d->current.pen.widthF());
671 }
672 d->drawPath(QCoreGraphicsPaintEnginePrivate::CGStroke, path);
673 if (doRestore)
674 d->restoreGraphicsState();
675 CGPathRelease(path);
676 if (d->current.pen.capStyle() == Qt::FlatCap)
677 CGContextSetLineCap(d->hd, kCGLineCapButt);
678}
679
680void
682{
683 Q_D(QCoreGraphicsPaintEngine);
684 Q_ASSERT(isActive());
685
686 if (state->compositionMode() == QPainter::CompositionMode_Destination)
687 return;
688
689 CGMutablePathRef path = CGPathCreateMutable();
690 CGAffineTransform transform = CGAffineTransformMakeScale(r.width() / r.height(), 1);
691 CGPathAddArc(path, &transform,(r.x() + (r.width() / 2)) / (r.width() / r.height()),
692 r.y() + (r.height() / 2), r.height() / 2, 0, (2 * M_PI), false);
693 d->drawPath(QCoreGraphicsPaintEnginePrivate::CGFill | QCoreGraphicsPaintEnginePrivate::CGStroke,
694 path);
695 CGPathRelease(path);
696}
697
698void
699QCoreGraphicsPaintEngine::drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode)
700{
701 Q_D(QCoreGraphicsPaintEngine);
702 Q_ASSERT(isActive());
703
704 if (state->compositionMode() == QPainter::CompositionMode_Destination)
705 return;
706
707 CGMutablePathRef path = CGPathCreateMutable();
708 CGPathMoveToPoint(path, nullptr, points[0].x(), points[0].y());
709 for (int x = 1; x < pointCount; ++x)
710 CGPathAddLineToPoint(path, nullptr, points[x].x(), points[x].y());
711 if (mode != PolylineMode && points[0] != points[pointCount-1])
712 CGPathAddLineToPoint(path, nullptr, points[0].x(), points[0].y());
714 if (mode != PolylineMode)
715 op |= mode == OddEvenMode ? QCoreGraphicsPaintEnginePrivate::CGEOFill
716 : QCoreGraphicsPaintEnginePrivate::CGFill;
717 d->drawPath(op, path);
718 CGPathRelease(path);
719}
720
721void
722QCoreGraphicsPaintEngine::drawLines(const QLineF *lines, int lineCount)
723{
724 Q_D(QCoreGraphicsPaintEngine);
725 Q_ASSERT(isActive());
726
727 if (state->compositionMode() == QPainter::CompositionMode_Destination)
728 return;
729
730 CGMutablePathRef path = CGPathCreateMutable();
731 for (int i = 0; i < lineCount; i++) {
732 const QPointF start = lines[i].p1(), end = lines[i].p2();
733 CGPathMoveToPoint(path, nullptr, start.x(), start.y());
734 CGPathAddLineToPoint(path, nullptr, end.x(), end.y());
735 }
736 d->drawPath(QCoreGraphicsPaintEnginePrivate::CGStroke, path);
737 CGPathRelease(path);
738}
739
740void QCoreGraphicsPaintEngine::drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr)
741{
742 Q_D(QCoreGraphicsPaintEngine);
743 Q_ASSERT(isActive());
744
745 if (state->compositionMode() == QPainter::CompositionMode_Destination)
746 return;
747
748 if (pm.isNull())
749 return;
750
751 bool differentSize = (QRectF(0, 0, pm.width(), pm.height()) != sr), doRestore = false;
752 CGRect rect = CGRectMake(r.x(), r.y(), r.width(), r.height());
753 QCFType<CGImageRef> image;
754 bool isBitmap = (pm.depth() == 1);
755 if (isBitmap) {
756 doRestore = true;
757 d->saveGraphicsState();
758
759 const QColor &col = d->current.pen.color();
760 CGContextSetFillColorWithColor(d->hd, cgColorForQColor(col));
761 image = qt_mac_create_imagemask(pm, sr);
762 } else if (differentSize) {
763 QCFType<CGImageRef> img = qt_mac_toCGImage(pm.toImage());
764 if (img)
765 image = CGImageCreateWithImageInRect(img, CGRectMake(qRound(sr.x()), qRound(sr.y()), qRound(sr.width()), qRound(sr.height())));
766 } else {
767 image = qt_mac_toCGImage(pm.toImage());
768 }
769 qt_mac_drawCGImage(d->hd, &rect, image);
770 if (doRestore)
771 d->restoreGraphicsState();
772}
773
774void QCoreGraphicsPaintEngine::drawImage(const QRectF &r, const QImage &img, const QRectF &sr,
775 Qt::ImageConversionFlags flags)
776{
777 Q_D(QCoreGraphicsPaintEngine);
778 Q_UNUSED(flags);
779 Q_ASSERT(isActive());
780
781 if (img.isNull() || state->compositionMode() == QPainter::CompositionMode_Destination)
782 return;
783
784 QCFType<CGImageRef> cgimage = qt_mac_toCGImage(img);
785 CGRect rect = CGRectMake(r.x(), r.y(), r.width(), r.height());
786 if (QRectF(0, 0, img.width(), img.height()) != sr)
787 cgimage = CGImageCreateWithImageInRect(cgimage, CGRectMake(sr.x(), sr.y(),
788 sr.width(), sr.height()));
789 qt_mac_drawCGImage(d->hd, &rect, cgimage);
790}
791
795
799
802{
803 return d_func()->hd;
804}
805
806void
807QCoreGraphicsPaintEngine::drawTiledPixmap(const QRectF &r, const QPixmap &pixmap,
808 const QPointF &p)
809{
810 Q_D(QCoreGraphicsPaintEngine);
811 Q_ASSERT(isActive());
812
813 if (state->compositionMode() == QPainter::CompositionMode_Destination)
814 return;
815
816 //save the old state
817 d->saveGraphicsState();
818
819 //setup the pattern
820 QMacPattern *qpattern = new QMacPattern;
821 qpattern->data.pixmap = pixmap;
822 qpattern->foreground = d->current.pen.color();
823 qpattern->pdev = d->pdev;
824 CGPatternCallbacks callbks;
825 callbks.version = 0;
826 callbks.drawPattern = qt_mac_draw_pattern;
827 callbks.releaseInfo = qt_mac_dispose_pattern;
828 const int width = qpattern->width(), height = qpattern->height();
829 CGAffineTransform trans = CGContextGetCTM(d->hd);
830 CGPatternRef pat = CGPatternCreate(qpattern, CGRectMake(0, 0, width, height),
831 trans, width, height,
832 kCGPatternTilingNoDistortion, true, &callbks);
833 CGColorSpaceRef cs = CGColorSpaceCreatePattern(nullptr);
834 CGContextSetFillColorSpace(d->hd, cs);
835 CGFloat component = 1.0; //just one
836 CGContextSetFillPattern(d->hd, pat, &component);
837 CGSize phase = CGSizeApplyAffineTransform(CGSizeMake(-(p.x()-r.x()), -(p.y()-r.y())), trans);
838 CGContextSetPatternPhase(d->hd, phase);
839
840 //fill the rectangle
841 CGRect mac_rect = CGRectMake(r.x(), r.y(), r.width(), r.height());
842 CGContextFillRect(d->hd, mac_rect);
843
844 //restore the state
845 d->restoreGraphicsState();
846 //cleanup
847 CGColorSpaceRelease(cs);
848 CGPatternRelease(pat);
849}
850
851void QCoreGraphicsPaintEngine::drawTextItem(const QPointF &pos, const QTextItem &item)
852{
853 Q_D(QCoreGraphicsPaintEngine);
854 if (d->current.transform.type() == QTransform::TxProject
855#ifndef QMAC_NATIVE_GRADIENTS
856 || painter()->pen().brush().gradient() //Just let the base engine "emulate" the gradient
857#endif
858 ) {
859 QPaintEngine::drawTextItem(pos, item);
860 return;
861 }
862
863 if (state->compositionMode() == QPainter::CompositionMode_Destination)
864 return;
865
866 const QTextItemInt &ti = static_cast<const QTextItemInt &>(item);
867
868 QPen oldPen = painter()->pen();
869 QBrush oldBrush = painter()->brush();
870 QPointF oldBrushOrigin = painter()->brushOrigin();
871 updatePen(Qt::NoPen);
872 updateBrush(oldPen.brush(), QPointF(0, 0));
873
874 Q_ASSERT(type() == QPaintEngine::CoreGraphics);
875
876 QFontEngine *fe = ti.fontEngine;
877
878 const bool textAA = ((state->renderHints() & QPainter::TextAntialiasing)
879 && !(fe->fontDef.styleStrategy & QFont::NoAntialias));
880 const bool lineAA = state->renderHints() & QPainter::Antialiasing;
881 if (textAA != lineAA)
882 CGContextSetShouldAntialias(d->hd, textAA);
883
884 const bool smoothing = textAA && !(fe->fontDef.styleStrategy & QFont::NoSubpixelAntialias);
885 if (d->disabledSmoothFonts == smoothing)
886 CGContextSetShouldSmoothFonts(d->hd, smoothing);
887
888 if (ti.glyphs.numGlyphs) {
889 switch (fe->type()) {
890 case QFontEngine::Mac:
891 static_cast<QCoreTextFontEngine *>(fe)->draw(d->hd, pos.x(), pos.y(), ti, paintDevice()->height());
892 break;
893 case QFontEngine::Box:
894 d->drawBoxTextItem(pos, ti);
895 break;
896 default:
897 break;
898 }
899 }
900
901 if (textAA != lineAA)
902 CGContextSetShouldAntialias(d->hd, !textAA);
903
904 if (smoothing == d->disabledSmoothFonts)
905 CGContextSetShouldSmoothFonts(d->hd, !d->disabledSmoothFonts);
906
907 updatePen(oldPen);
908 updateBrush(oldBrush, oldBrushOrigin);
909}
910
913{
914 return QPainter::RenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
915}
931extern "C" {
933} // private function, but is in all versions of OS X.
934void
935QCoreGraphicsPaintEngine::updateCompositionMode(QPainter::CompositionMode mode)
936{
937 int cg_mode = kCGBlendModeNormal;
938 switch (mode) {
939 case QPainter::CompositionMode_Multiply:
940 cg_mode = kCGBlendModeMultiply;
941 break;
942 case QPainter::CompositionMode_Screen:
943 cg_mode = kCGBlendModeScreen;
944 break;
945 case QPainter::CompositionMode_Overlay:
946 cg_mode = kCGBlendModeOverlay;
947 break;
948 case QPainter::CompositionMode_Darken:
949 cg_mode = kCGBlendModeDarken;
950 break;
951 case QPainter::CompositionMode_Lighten:
952 cg_mode = kCGBlendModeLighten;
953 break;
954 case QPainter::CompositionMode_ColorDodge:
955 cg_mode = kCGBlendModeColorDodge;
956 break;
957 case QPainter::CompositionMode_ColorBurn:
958 cg_mode = kCGBlendModeColorBurn;
959 break;
960 case QPainter::CompositionMode_HardLight:
961 cg_mode = kCGBlendModeHardLight;
962 break;
963 case QPainter::CompositionMode_SoftLight:
964 cg_mode = kCGBlendModeSoftLight;
965 break;
966 case QPainter::CompositionMode_Difference:
967 cg_mode = kCGBlendModeDifference;
968 break;
969 case QPainter::CompositionMode_Exclusion:
970 cg_mode = kCGBlendModeExclusion;
971 break;
972 case QPainter::CompositionMode_Plus:
973 cg_mode = kCGBlendModePlusLighter;
974 break;
975 case QPainter::CompositionMode_SourceOver:
976 cg_mode = kCGBlendModeNormal;
977 break;
978 case QPainter::CompositionMode_DestinationOver:
979 cg_mode = kCGBlendModeDestinationOver;
980 break;
981 case QPainter::CompositionMode_Clear:
982 cg_mode = kCGBlendModeClear;
983 break;
984 case QPainter::CompositionMode_Source:
985 cg_mode = kCGBlendModeCopy;
986 break;
987 case QPainter::CompositionMode_Destination:
988 cg_mode = -1;
989 break;
990 case QPainter::CompositionMode_SourceIn:
991 cg_mode = kCGBlendModeSourceIn;
992 break;
993 case QPainter::CompositionMode_DestinationIn:
995 break;
996 case QPainter::CompositionMode_SourceOut:
997 cg_mode = kCGBlendModeSourceOut;
998 break;
999 case QPainter::CompositionMode_DestinationOut:
1000 cg_mode = kCGBlendModeDestinationOver;
1001 break;
1002 case QPainter::CompositionMode_SourceAtop:
1003 cg_mode = kCGBlendModeSourceAtop;
1004 break;
1005 case QPainter::CompositionMode_DestinationAtop:
1006 cg_mode = kCGBlendModeDestinationAtop;
1007 break;
1008 case QPainter::CompositionMode_Xor:
1009 cg_mode = kCGBlendModeXOR;
1010 break;
1011 default:
1012 break;
1013 }
1014 if (cg_mode > -1) {
1015 CGContextSetBlendMode(d_func()->hd, CGBlendMode(cg_mode));
1016 }
1017}
1018
1019void
1020QCoreGraphicsPaintEngine::updateRenderHints(QPainter::RenderHints hints)
1021{
1022 Q_D(QCoreGraphicsPaintEngine);
1023 CGContextSetShouldAntialias(d->hd, hints & QPainter::Antialiasing);
1024 CGContextSetInterpolationQuality(d->hd, (hints & QPainter::SmoothPixmapTransform) ?
1025 kCGInterpolationHigh : kCGInterpolationNone);
1026 bool textAntialiasing = (hints & QPainter::TextAntialiasing) == QPainter::TextAntialiasing;
1027 if (!textAntialiasing || d->disabledSmoothFonts) {
1028 d->disabledSmoothFonts = !textAntialiasing;
1029 CGContextSetShouldSmoothFonts(d->hd, textAntialiasing);
1030 }
1031}
1032
1033/*
1034 Returns the size of one device pixel in user-space coordinates.
1035*/
1037{
1038 QPointF p1 = current.transform.inverted().map(QPointF(0, 0));
1039 QPointF p2 = current.transform.inverted().map(QPointF(1, 1));
1040 return QPointF(qAbs(p2.x() - p1.x()), qAbs(p2.y() - p1.y()));
1041}
1042
1043/*
1044 Adjusts the pen width so we get correct line widths in the
1045 non-transformed, aliased case.
1046*/
1048{
1049 Q_Q(QCoreGraphicsPaintEngine);
1050 float ret = penWidth;
1051 if (!complexXForm && !(q->state->renderHints() & QPainter::Antialiasing)) {
1052 if (penWidth < 2)
1053 ret = 1;
1054 else if (penWidth < 3)
1055 ret = 1.5;
1056 else
1057 ret = penWidth -1;
1058 }
1059 return ret;
1060}
1061
1062void
1063QCoreGraphicsPaintEnginePrivate::setStrokePen(const QPen &pen)
1064{
1065 //pencap
1066 CGLineCap cglinecap = kCGLineCapButt;
1067 if (pen.capStyle() == Qt::SquareCap)
1068 cglinecap = kCGLineCapSquare;
1069 else if (pen.capStyle() == Qt::RoundCap)
1070 cglinecap = kCGLineCapRound;
1071 CGContextSetLineCap(hd, cglinecap);
1072 CGContextSetLineWidth(hd, adjustPenWidth(pen.widthF()));
1073
1074 //join
1075 CGLineJoin cglinejoin = kCGLineJoinMiter;
1076 if (pen.joinStyle() == Qt::BevelJoin)
1077 cglinejoin = kCGLineJoinBevel;
1078 else if (pen.joinStyle() == Qt::RoundJoin)
1079 cglinejoin = kCGLineJoinRound;
1080 CGContextSetLineJoin(hd, cglinejoin);
1081// CGContextSetMiterLimit(hd, pen.miterLimit());
1082
1083 //pen style
1084 QVector<CGFloat> linedashes;
1085 if (pen.style() == Qt::CustomDashLine) {
1086 QVector<qreal> customs = pen.dashPattern();
1087 for (int i = 0; i < customs.size(); ++i)
1088 linedashes.append(customs.at(i));
1089 } else if (pen.style() == Qt::DashLine) {
1090 linedashes.append(4);
1091 linedashes.append(2);
1092 } else if (pen.style() == Qt::DotLine) {
1093 linedashes.append(1);
1094 linedashes.append(2);
1095 } else if (pen.style() == Qt::DashDotLine) {
1096 linedashes.append(4);
1097 linedashes.append(2);
1098 linedashes.append(1);
1099 linedashes.append(2);
1100 } else if (pen.style() == Qt::DashDotDotLine) {
1101 linedashes.append(4);
1102 linedashes.append(2);
1103 linedashes.append(1);
1104 linedashes.append(2);
1105 linedashes.append(1);
1106 linedashes.append(2);
1107 }
1108 const CGFloat cglinewidth = pen.widthF() <= 0.0f ? 1.0f : float(pen.widthF());
1109 for (int i = 0; i < linedashes.size(); ++i) {
1110 linedashes[i] *= cglinewidth;
1111 if (cglinewidth < 3 && (cglinecap == kCGLineCapSquare || cglinecap == kCGLineCapRound)) {
1112 if ((i%2))
1113 linedashes[i] += cglinewidth/2;
1114 else
1115 linedashes[i] -= cglinewidth/2;
1116 }
1117 }
1118 CGContextSetLineDash(hd, pen.dashOffset() * cglinewidth, linedashes.data(), linedashes.size());
1119
1120 // color
1121 CGContextSetStrokeColorWithColor(hd, cgColorForQColor(pen.color()));
1122}
1123
1124// Add our own patterns here to deal with the fact that the coordinate system
1125// is flipped vertically with Quartz2D.
1126static const uchar *qt_mac_patternForBrush(int brushStyle)
1127{
1128 Q_ASSERT(brushStyle > Qt::SolidPattern && brushStyle < Qt::LinearGradientPattern);
1129 static const uchar dense1_pat[] = { 0x00, 0x00, 0x44, 0x00, 0x00, 0x00, 0x44, 0x00 };
1130 static const uchar dense2_pat[] = { 0x00, 0x22, 0x00, 0x88, 0x00, 0x22, 0x00, 0x88 };
1131 static const uchar dense3_pat[] = { 0x11, 0xaa, 0x44, 0xaa, 0x11, 0xaa, 0x44, 0xaa };
1132 static const uchar dense4_pat[] = { 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55, 0xaa, 0x55 };
1133 static const uchar dense5_pat[] = { 0xee, 0x55, 0xbb, 0x55, 0xee, 0x55, 0xbb, 0x55 };
1134 static const uchar dense6_pat[] = { 0xff, 0xdd, 0xff, 0x77, 0xff, 0xdd, 0xff, 0x77 };
1135 static const uchar dense7_pat[] = { 0xff, 0xff, 0xbb, 0xff, 0xff, 0xff, 0xbb, 0xff };
1136 static const uchar hor_pat[] = { 0xff, 0xff, 0xff, 0xff, 0x00, 0xff, 0xff, 0xff };
1137 static const uchar ver_pat[] = { 0xef, 0xef, 0xef, 0xef, 0xef, 0xef, 0xef, 0xef };
1138 static const uchar cross_pat[] = { 0xef, 0xef, 0xef, 0xef, 0x00, 0xef, 0xef, 0xef };
1139 static const uchar fdiag_pat[] = { 0x7f, 0xbf, 0xdf, 0xef, 0xf7, 0xfb, 0xfd, 0xfe };
1140 static const uchar bdiag_pat[] = { 0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f };
1141 static const uchar dcross_pat[] = { 0x7e, 0xbd, 0xdb, 0xe7, 0xe7, 0xdb, 0xbd, 0x7e };
1142 static const uchar *const pat_tbl[] = {
1143 dense1_pat, dense2_pat, dense3_pat, dense4_pat, dense5_pat,
1144 dense6_pat, dense7_pat,
1145 hor_pat, ver_pat, cross_pat, bdiag_pat, fdiag_pat, dcross_pat };
1146 return pat_tbl[brushStyle - Qt::Dense1Pattern];
1147}
1148
1150{
1151 // pattern
1152 Qt::BrushStyle bs = current.brush.style();
1153#ifdef QT_MAC_USE_NATIVE_GRADIENTS
1154 if (bs == Qt::LinearGradientPattern || bs == Qt::RadialGradientPattern) {
1155 const QGradient *grad = static_cast<const QGradient*>(current.brush.gradient());
1156 if (drawGradientNatively(grad)) {
1157 Q_ASSERT(grad->spread() == QGradient::PadSpread);
1158
1159 static const CGFloat domain[] = { 0.0f, +1.0f };
1160 static const CGFunctionCallbacks callbacks = { 0, qt_mac_color_gradient_function, nullptr };
1161 CGFunctionRef fill_func = CGFunctionCreate(reinterpret_cast<void *>(&current.brush),
1162 1, domain, 4, nullptr, &callbacks);
1163
1164 CGColorSpaceRef colorspace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB)
1165 if (bs == Qt::LinearGradientPattern) {
1166 const QLinearGradient *linearGrad = static_cast<const QLinearGradient *>(grad);
1167 const QPointF start(linearGrad->start());
1168 const QPointF stop(linearGrad->finalStop());
1169 shading = CGShadingCreateAxial(colorspace, CGPointMake(start.x(), start.y()),
1170 CGPointMake(stop.x(), stop.y()), fill_func, true, true);
1171 } else {
1172 Q_ASSERT(bs == Qt::RadialGradientPattern);
1173 const QRadialGradient *radialGrad = static_cast<const QRadialGradient *>(grad);
1174 QPointF center(radialGrad->center());
1175 QPointF focal(radialGrad->focalPoint());
1176 qreal radius = radialGrad->radius();
1177 qreal focalRadius = radialGrad->focalRadius();
1178 shading = CGShadingCreateRadial(colorspace, CGPointMake(focal.x(), focal.y()),
1179 focalRadius, CGPointMake(center.x(), center.y()), radius, fill_func, false, true);
1180 }
1181
1182 CGFunctionRelease(fill_func);
1183 }
1184 } else
1185#endif
1186 if (bs != Qt::SolidPattern && bs != Qt::NoBrush
1187#ifndef QT_MAC_USE_NATIVE_GRADIENTS
1188 && (bs < Qt::LinearGradientPattern || bs > Qt::ConicalGradientPattern)
1189#endif
1190 )
1191 {
1192 QMacPattern *qpattern = new QMacPattern;
1193 qpattern->pdev = pdev;
1194 CGFloat components[4] = { 1.0, 1.0, 1.0, 1.0 };
1195 CGColorSpaceRef base_colorspace = nullptr;
1196 if (bs == Qt::TexturePattern) {
1197 qpattern->data.pixmap = current.brush.texture();
1198 if (qpattern->data.pixmap.isQBitmap()) {
1199 const QColor &col = current.brush.color();
1200 components[0] = qt_mac_convert_color_to_cg(col.red());
1201 components[1] = qt_mac_convert_color_to_cg(col.green());
1202 components[2] = qt_mac_convert_color_to_cg(col.blue());
1203 base_colorspace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
1204 }
1205 } else {
1206 qpattern->as_mask = true;
1207
1208 qpattern->data.bytes = qt_mac_patternForBrush(bs);
1209 const QColor &col = current.brush.color();
1210 components[0] = qt_mac_convert_color_to_cg(col.red());
1211 components[1] = qt_mac_convert_color_to_cg(col.green());
1212 components[2] = qt_mac_convert_color_to_cg(col.blue());
1213 base_colorspace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
1214 }
1215 int width = qpattern->width(), height = qpattern->height();
1216 qpattern->foreground = current.brush.color();
1217
1218 CGColorSpaceRef fill_colorspace = CGColorSpaceCreatePattern(base_colorspace);
1219 CGContextSetFillColorSpace(hd, fill_colorspace);
1220
1221 CGAffineTransform xform = CGContextGetCTM(hd);
1222 xform = CGAffineTransformConcat(qt_mac_convert_transform_to_cg(current.brush.transform()), xform);
1223 xform = CGAffineTransformTranslate(xform, offset.x(), offset.y());
1224
1225 CGPatternCallbacks callbks;
1226 callbks.version = 0;
1227 callbks.drawPattern = qt_mac_draw_pattern;
1228 callbks.releaseInfo = qt_mac_dispose_pattern;
1229 CGPatternRef fill_pattern = CGPatternCreate(qpattern, CGRectMake(0, 0, width, height),
1230 xform, width, height, kCGPatternTilingNoDistortion,
1231 !base_colorspace, &callbks);
1232 CGContextSetFillPattern(hd, fill_pattern, components);
1233
1234
1235 CGPatternRelease(fill_pattern);
1236 CGColorSpaceRelease(base_colorspace);
1237 CGColorSpaceRelease(fill_colorspace);
1238 } else if (bs != Qt::NoBrush) {
1239 CGContextSetFillColorWithColor(hd, cgColorForQColor(current.brush.color()));
1240 }
1241}
1242
1243void
1244QCoreGraphicsPaintEnginePrivate::setClip(const QRegion *rgn)
1245{
1246 Q_Q(QCoreGraphicsPaintEngine);
1247 if (hd) {
1249 QRegion sysClip = q->systemClip();
1250 if (!sysClip.isEmpty())
1251 qt_mac_clip_cg(hd, sysClip, &orig_xform);
1252 if (rgn)
1253 qt_mac_clip_cg(hd, *rgn, nullptr);
1254 }
1255}
1256
1261
1262void qt_mac_cg_transform_path_apply(void *info, const CGPathElement *element)
1263{
1264 Q_ASSERT(info && element);
1266 switch (element->type) {
1267 case kCGPathElementMoveToPoint:
1268 CGPathMoveToPoint(t->path, &t->transform, element->points[0].x, element->points[0].y);
1269 break;
1270 case kCGPathElementAddLineToPoint:
1271 CGPathAddLineToPoint(t->path, &t->transform, element->points[0].x, element->points[0].y);
1272 break;
1273 case kCGPathElementAddQuadCurveToPoint:
1274 CGPathAddQuadCurveToPoint(t->path, &t->transform, element->points[0].x, element->points[0].y,
1275 element->points[1].x, element->points[1].y);
1276 break;
1277 case kCGPathElementAddCurveToPoint:
1278 CGPathAddCurveToPoint(t->path, &t->transform, element->points[0].x, element->points[0].y,
1279 element->points[1].x, element->points[1].y,
1280 element->points[2].x, element->points[2].y);
1281 break;
1282 case kCGPathElementCloseSubpath:
1283 CGPathCloseSubpath(t->path);
1284 break;
1285 default:
1286 qDebug() << "Unhandled path transform type: " << element->type;
1287 }
1288}
1289
1290void QCoreGraphicsPaintEnginePrivate::drawPath(uchar ops, CGMutablePathRef path)
1291{
1292 Q_Q(QCoreGraphicsPaintEngine);
1293 Q_ASSERT((ops & (CGFill | CGEOFill)) != (CGFill | CGEOFill)); //can't really happen
1294 if ((ops & (CGFill | CGEOFill))) {
1295 if (shading) {
1296 Q_ASSERT(path);
1297 CGContextBeginPath(hd);
1298 CGContextAddPath(hd, path);
1300 if (ops & CGFill)
1301 CGContextClip(hd);
1302 else if (ops & CGEOFill)
1303 CGContextEOClip(hd);
1304 if (current.brush.gradient()->coordinateMode() == QGradient::ObjectBoundingMode) {
1305 CGRect boundingBox = CGPathGetBoundingBox(path);
1306 CGContextConcatCTM(hd,
1307 CGAffineTransformMake(boundingBox.size.width, 0,
1308 0, boundingBox.size.height,
1309 boundingBox.origin.x, boundingBox.origin.y));
1310 }
1311 CGContextDrawShading(hd, shading);
1313 ops &= ~CGFill;
1314 ops &= ~CGEOFill;
1315 } else if (current.brush.style() == Qt::NoBrush) {
1316 ops &= ~CGFill;
1317 ops &= ~CGEOFill;
1318 }
1319 }
1320 if ((ops & CGStroke) && current.pen.style() == Qt::NoPen)
1321 ops &= ~CGStroke;
1322
1323 if (ops & (CGEOFill | CGFill)) {
1324 CGContextBeginPath(hd);
1325 CGContextAddPath(hd, path);
1326 if (ops & CGEOFill) {
1327 CGContextEOFillPath(hd);
1328 } else {
1329 CGContextFillPath(hd);
1330 }
1331 }
1332
1333 // Avoid saving and restoring the context if we can.
1334 const bool needContextSave = (cosmeticPen != QCoreGraphicsPaintEnginePrivate::CosmeticNone ||
1335 !(q->state->renderHints() & QPainter::Antialiasing));
1336 if (ops & CGStroke) {
1337 if (needContextSave)
1339 CGContextBeginPath(hd);
1340
1341 // Translate a fraction of a pixel size in the y direction
1342 // to make sure that primitives painted at pixel borders
1343 // fills the right pixel. This is needed since the y xais
1344 // in the Quartz coordinate system is inverted compared to Qt.
1345 if (!(q->state->renderHints() & QPainter::Antialiasing)) {
1346 if (current.pen.style() == Qt::SolidLine || current.pen.width() >= 3)
1347 CGContextTranslateCTM(hd, double(pixelSize.x()) * 0.25, double(pixelSize.y()) * 0.25);
1348 else
1349 CGContextTranslateCTM(hd, 0, double(pixelSize.y()) * 0.1);
1350 }
1351
1353 // If antialiazing is enabled, use the cosmetic pen size directly.
1354 if (q->state->renderHints() & QPainter::Antialiasing)
1355 CGContextSetLineWidth(hd, cosmeticPenSize);
1356 else if (current.pen.widthF() <= 1)
1357 CGContextSetLineWidth(hd, cosmeticPenSize * 0.9f);
1358 else
1359 CGContextSetLineWidth(hd, cosmeticPenSize);
1360 }
1363 t.transform = qt_mac_convert_transform_to_cg(current.transform);
1364 t.path = CGPathCreateMutable();
1365 CGPathApply(path, &t, qt_mac_cg_transform_path_apply); //transform the path
1366 setTransform(nullptr); //unset the context transform
1367 CGContextSetLineWidth(hd, cosmeticPenSize);
1368 CGContextAddPath(hd, t.path);
1369 CGPathRelease(t.path);
1370 } else {
1371 CGContextAddPath(hd, path);
1372 }
1373
1374 CGContextStrokePath(hd);
1375 if (needContextSave)
1377 }
1378}
1379
1380QT_END_NAMESPACE
void setFillBrush(const QPointF &origin=QPoint())
QPointF devicePixelSize(CGContextRef context)
void drawPath(uchar ops, CGMutablePathRef path=nullptr)
void updateState(const QPaintEngineState &state)
Reimplement this function to update the state of a paint engine.
void updateRenderHints(QPainter::RenderHints hints)
void drawImage(const QRectF &r, const QImage &pm, const QRectF &sr, Qt::ImageConversionFlags flags=Qt::AutoColor)
Reimplement this function to draw the part of the image specified by the sr rectangle in the given re...
void drawPoints(const QPointF *p, int pointCount)
Draws the first pointCount points in the buffer points.
void drawRects(const QRectF *rects, int rectCount)
Draws the first rectCount rectangles in the buffer rects.
void drawTextItem(const QPointF &pos, const QTextItem &item)
This function draws the text item textItem at position p.
void updateBrush(const QBrush &brush, const QPointF &pt)
QPainter::RenderHints supportedRenderHints() const
void updateClipPath(const QPainterPath &path, Qt::ClipOperation op)
bool end()
Reimplement this function to finish painting on the current paint device.
void updateCompositionMode(QPainter::CompositionMode mode)
CGContextRef handle() const
QCoreGraphicsPaintEngine(QPaintEnginePrivate &dptr)
void drawLines(const QLineF *lines, int lineCount)
The default implementation splits the list of lines in lines into lineCount separate calls to drawPat...
void drawEllipse(const QRectF &r)
Reimplement this function to draw the largest ellipse that can be contained within rectangle rect.
void drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode)
Reimplement this virtual function to draw the polygon defined by the pointCount first points in point...
void updateOpacity(qreal opacity)
void drawPath(const QPainterPath &path)
The default implementation ignores the path and does nothing.
QPaintDevice * pdev
const uchar * bytes
#define M_PI
Definition qmath.h:200
QT_BEGIN_NAMESPACE void qt_mac_cgimage_data_free(void *, const void *memoryToFree, size_t)
static QCFType< CGColorRef > cgColorForQColor(const QColor &col)
static void qt_mac_draw_pattern(void *info, CGContextRef c)
static CGMutablePathRef qt_mac_compose_path(const QPainterPath &p, float off=0)
CGAffineTransform qt_mac_convert_transform_to_cg(const QTransform &t)
static float qt_mac_convert_color_to_cg(int c)
static CGRect qt_mac_compose_rect(const QRectF &r, float off=0)
void qt_mac_cg_transform_path_apply(void *info, const CGPathElement *element)
void CGContextSetCompositeOperation(CGContextRef, int)
CGImageRef qt_mac_create_imagemask(const QPixmap &pixmap, const QRectF &sr)
static const uchar * qt_mac_patternForBrush(int brushStyle)
#define QMACPATTERN_MASK_MULTIPLIER
static QPaintEngine::PaintEngineFeatures qt_mac_cg_features()
static void qt_mac_dispose_pattern(void *info)
CGCompositeMode
@ kCGCompositeModeCopy
@ kCGCompositeModeDestinationAtop
@ kCGCompositeModeSourceOut
@ kCGCompositeModePlusLighter
@ kCGCompositeModeDestinationOver
@ kCGCompositeModeDestinationIn
@ kCGCompositeModeClear
@ kCGCompositeModeSourceAtop
@ kCGCompositeModeXOR
@ kCGCompositeModePlusDarker
@ kCGCompositeModeSourceOver
@ kCGCompositeModeSourceIn
@ kCGCompositeModeDestinationOut
struct CGContext * CGContextRef
struct CGColorSpace * CGColorSpaceRef