5#include <AppKit/AppKit.h>
6#include <CoreGraphics/CoreGraphics.h>
12#include <qpaintdevice.h>
13#include <qpainterpath.h>
14#include <qpixmapcache.h>
15#include <private/qpaintengine_raster_p.h>
19#include <qvarlengtharray.h>
21#include <qcoreapplication.h>
24#include <qpa/qplatformpixmap.h>
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>
40
41
45 free(
const_cast<
void *>(memoryToFree));
50 QImage image = pixmap.toImage();
51 if (image.format() != QImage::Format_ARGB32_Premultiplied)
52 image = std::move(image).convertToFormat(QImage::Format_ARGB32_Premultiplied);
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;
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;
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);
73 return CGAffineTransformMake(t.m11(), t.m12(), t.m21(), t.m22(), t.dx(), t.dy());
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())
84 QCFType<CGColorSpaceRef> colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
85 return CGColorCreate(colorSpace, components);
92#ifdef QT_MAC_USE_NATIVE_GRADIENTS
93static bool drawGradientNatively(
const QGradient *gradient)
95 return gradient->spread() == QGradient::PadSpread;
99static void qt_mac_color_gradient_function(
void *info,
const CGFloat *in, CGFloat *out)
101 QBrush *brush =
static_cast<QBrush *>(info);
102 Q_ASSERT(brush && brush->gradient());
104 const QGradientStops stops = brush->gradient()->stops();
105 const int n = stops.count();
107 const QGradientStop *begin = stops.constBegin();
108 const QGradientStop *end = begin + n;
111 const QGradientStop *i = begin;
112 while (i != end && i->first < p)
117 c = begin->second.rgba();
118 }
else if (i == end) {
119 c = (end - 1)->second.rgba();
121 const QGradientStop &s1 = *(i - 1);
122 const QGradientStop &s2 = *i;
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));
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));
145 static bool inReset =
false;
150 CGAffineTransform old_xform = CGContextGetCTM(
hd);
153 CGContextConcatCTM(
hd, CGAffineTransformInvert(old_xform));
160 CGContextConcatCTM(
hd, CGAffineTransformInvert(CGContextGetCTM(
hd)));
161 CGContextConcatCTM(
hd, old_xform);
166 return CGRectMake(r.x()+off, r.y()+off, r.width(), r.height());
171 CGMutablePathRef ret = CGPathCreateMutable();
173 for (
int i=0; i<p.elementCount(); ++i) {
174 const QPainterPath::Element &elm = p.elementAt(i);
176 case QPainterPath::MoveToElement:
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);
184 case QPainterPath::LineToElement:
185 CGPathAddLineToPoint(ret, 0, elm.x+off, elm.y+off);
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);
197 qFatal(
"QCoreGraphicsPaintEngine::drawPath(), unhandled type: %d", elm.type);
202 && p.elementAt(p.elementCount() - 1).x == startPt.x()
203 && p.elementAt(p.elementCount() - 1).y == startPt.y())
204 CGPathCloseSubpath(ret);
210# define QMACPATTERN_MASK_MULTIPLIER 32
212# define QMACPATTERN_MASK_MULTIPLIER 1
221 return CGImageGetWidth(image);
224 return data.pixmap.width();
228 return CGImageGetHeight(image);
231 return data.pixmap.height();
249 bool isBitmap = (pat->data.pixmap.depth() == 1);
252 Q_ASSERT(pat->data.bytes);
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);
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);
267 const QColor c0(0, 0, 0, 0), c1(255, 255, 255, 255);
270 QMacCGContext pm_ctx(&pm);
271 CGContextSetFillColorWithColor(c, cgColorForQColor(c1));
272 CGRect rect = CGRectMake(0, 0, w, h);
274 rect.origin.x = x * w;
276 rect.origin.y = y * h;
277 qt_mac_drawCGImage(pm_ctx, &rect, swatch);
280 pat->image = qt_mac_create_imagemask(pm, pm.rect());
281 CGImageRelease(swatch);
286 w = pat->data.pixmap.width();
287 h = pat->data.pixmap.height();
289 pat->image = qt_mac_create_imagemask(pat->data.pixmap, pat->data.pixmap.rect());
291 pat->image = qt_mac_toCGImage(pat->data.pixmap.toImage());
294 w = CGImageGetWidth(pat->image);
295 h = CGImageGetHeight(pat->image);
299 bool needRestore =
false;
300 if (CGImageIsMask(pat->image)) {
301 CGContextSaveGState(c);
302 CGContextSetFillColorWithColor(c, cgColorForQColor(pat->foreground));
304 CGRect rect = CGRectMake(0, 0, w, h);
305 qt_mac_drawCGImage(c, &rect, pat->image);
307 CGContextRestoreGState(c);
316
317
321 return QPaintEngine::PaintEngineFeatures(QPaintEngine::AllFeatures & ~QPaintEngine::PaintOutsidePaintEvent
322 & ~QPaintEngine::PerspectiveTransform
323 & ~QPaintEngine::ConicalGradientFill
324 & ~QPaintEngine::LinearGradientFill
325 & ~QPaintEngine::RadialGradientFill
326 & ~QPaintEngine::BrushStroke);
330: QPaintEngine(*(
new QCoreGraphicsPaintEnginePrivate), qt_mac_cg_features())
346 Q_D(QCoreGraphicsPaintEngine);
348 qWarning(
"QCoreGraphicsPaintEngine::begin: Painter already active");
354 d->complexXForm =
false;
355 d->cosmeticPen = QCoreGraphicsPaintEnginePrivate::CosmeticSetPenWidth;
356 d->cosmeticPenSize = 1;
357 d->current.clipEnabled =
false;
358 d->pixelSize = QPoint(1,1);
360 if (pdev->devType() != QInternal::Printer) {
361 QMacCGContext ctx(pdev);
362 d->hd = CGContextRetain(ctx);
364 d->saveGraphicsState();
365 d->orig_xform = CGContextGetCTM(d->hd);
367 CGShadingRelease(d->shading);
368 d->shading =
nullptr;
376 if (d->pdev->devType() == QInternal::Widget) {
377 QWidget *w = (QWidget*)d->pdev;
378 bool unclipped = w->testAttribute(Qt::WA_PaintUnclipped);
381 qWarning(
"QCoreGraphicsPaintEngine::begin: Does not support unclipped painting");
383 }
else if (d->pdev->devType() == QInternal::Pixmap) {
384 QPixmap *pm = (QPixmap*)d->pdev;
386 qWarning(
"QCoreGraphicsPaintEngine::begin: Cannot paint null pixmap");
392 setDirty(QPaintEngine::DirtyPen);
393 setDirty(QPaintEngine::DirtyBrush);
394 setDirty(QPaintEngine::DirtyBackground);
395 setDirty(QPaintEngine::DirtyHints);
402 Q_D(QCoreGraphicsPaintEngine);
405 CGShadingRelease(d->shading);
410 d->restoreGraphicsState();
411 CGContextSynchronize(d->hd);
412 CGContextRelease(d->hd);
421 Q_D(QCoreGraphicsPaintEngine);
422 QPaintEngine::DirtyFlags flags = state.state();
424 if (flags & DirtyTransform)
425 updateMatrix(state.transform());
427 if (flags & DirtyClipEnabled) {
428 if (state.isClipEnabled())
429 updateClipPath(painter()->clipPath(), Qt::ReplaceClip);
431 updateClipPath(QPainterPath(), Qt::NoClip);
434 if (flags & DirtyClipPath) {
435 updateClipPath(state.clipPath(), state.clipOperation());
436 }
else if (flags & DirtyClipRegion) {
437 updateClipRegion(state.clipRegion(), state.clipOperation());
443 if (flags & (DirtyClipPath | DirtyClipRegion | DirtyClipEnabled))
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());
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;
469 d->cosmeticPen = QCoreGraphicsPaintEnginePrivate::CosmeticSetPenWidth;
470 static const float sqrt2 =
std::sqrt(2.0f);
471 qreal width = d->current.pen.widthF();
474 d->cosmeticPenSize = std::sqrt(std::pow(d->pixelSize.y(), 2) + std::pow(d->pixelSize.x(), 2)) / sqrt2 * width;
482 Q_D(QCoreGraphicsPaintEngine);
483 Q_ASSERT(isActive());
484 d->current.pen = pen;
485 d->setStrokePen(pen);
491 Q_D(QCoreGraphicsPaintEngine);
492 Q_ASSERT(isActive());
493 d->current.brush = brush;
495#ifdef QT_MAC_USE_NATIVE_GRADIENTS
497 if (
const QGradient *gradient = brush.gradient()) {
498 if (drawGradientNatively(gradient)) {
499 gccaps |= QPaintEngine::LinearGradientFill | QPaintEngine::RadialGradientFill;
501 gccaps &= ~(QPaintEngine::LinearGradientFill | QPaintEngine::RadialGradientFill);
507 CGShadingRelease(d->shading);
508 d->shading =
nullptr;
510 d->setFillBrush(brushOrigin);
516 Q_D(QCoreGraphicsPaintEngine);
517 CGContextSetAlpha(d->hd, opacity);
523 Q_D(QCoreGraphicsPaintEngine);
524 Q_ASSERT(isActive());
525 updatePen(d->current.pen);
531 Q_D(QCoreGraphicsPaintEngine);
532 Q_ASSERT(isActive());
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()))
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);
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();
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;
566 CGRect rect = CGRectMake(0, 0, 0, 0);
567 CGContextClipToRect(d->hd, rect);
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);
575 CGContextEOClip(d->hd);
578 }
else if (op == Qt::IntersectClip) {
579 d->current.clip = d->current.clip.intersected(clipRegion);
580 d->setClip(&d->current.clip);
588 Q_D(QCoreGraphicsPaintEngine);
589 Q_ASSERT(isActive());
590 if (op == Qt::NoClip) {
591 d->current.clipEnabled =
false;
592 d->current.clip = QRegion();
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);
609 Q_D(QCoreGraphicsPaintEngine);
610 Q_ASSERT(isActive());
612 if (state->compositionMode() == QPainter::CompositionMode_Destination)
615 CGMutablePathRef path = qt_mac_compose_path(p);
617 if (p.fillRule() == Qt::WindingFill)
621 CGContextBeginPath(d->hd);
622 d->drawPath(ops, path);
629 Q_D(QCoreGraphicsPaintEngine);
630 Q_ASSERT(isActive());
632 if (state->compositionMode() == QPainter::CompositionMode_Destination)
635 for (
int i=0; i<rectCount; ++i) {
638 CGMutablePathRef path = CGPathCreateMutable();
639 CGPathAddRect(path,
nullptr, qt_mac_compose_rect(r));
640 d->drawPath(QCoreGraphicsPaintEnginePrivate::CGFill|QCoreGraphicsPaintEnginePrivate::CGStroke,
649 Q_D(QCoreGraphicsPaintEngine);
650 Q_ASSERT(isActive());
652 if (state->compositionMode() == QPainter::CompositionMode_Destination)
655 if (d->current.pen.capStyle() == Qt::FlatCap)
656 CGContextSetLineCap(d->hd, kCGLineCapSquare);
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);
665 bool doRestore =
false;
666 if (d->cosmeticPen == QCoreGraphicsPaintEnginePrivate::CosmeticNone && !(state->renderHints() & QPainter::Antialiasing)) {
669 d->saveGraphicsState();
670 CGContextSetLineWidth(d->hd, d->current.pen.widthF());
672 d->drawPath(QCoreGraphicsPaintEnginePrivate::CGStroke, path);
674 d->restoreGraphicsState();
676 if (d->current.pen.capStyle() == Qt::FlatCap)
677 CGContextSetLineCap(d->hd, kCGLineCapButt);
683 Q_D(QCoreGraphicsPaintEngine);
684 Q_ASSERT(isActive());
686 if (state->compositionMode() == QPainter::CompositionMode_Destination)
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,
701 Q_D(QCoreGraphicsPaintEngine);
702 Q_ASSERT(isActive());
704 if (state->compositionMode() == QPainter::CompositionMode_Destination)
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);
724 Q_D(QCoreGraphicsPaintEngine);
725 Q_ASSERT(isActive());
727 if (state->compositionMode() == QPainter::CompositionMode_Destination)
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());
736 d->drawPath(QCoreGraphicsPaintEnginePrivate::CGStroke, path);
742 Q_D(QCoreGraphicsPaintEngine);
743 Q_ASSERT(isActive());
745 if (state->compositionMode() == QPainter::CompositionMode_Destination)
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);
757 d->saveGraphicsState();
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());
765 image = CGImageCreateWithImageInRect(img, CGRectMake(qRound(sr.x()), qRound(sr.y()), qRound(sr.width()), qRound(sr.height())));
767 image = qt_mac_toCGImage(pm.toImage());
769 qt_mac_drawCGImage(d->hd, &rect, image);
771 d->restoreGraphicsState();
775 Qt::ImageConversionFlags flags)
777 Q_D(QCoreGraphicsPaintEngine);
779 Q_ASSERT(isActive());
781 if (img.isNull() || state->compositionMode() == QPainter::CompositionMode_Destination)
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);
810 Q_D(QCoreGraphicsPaintEngine);
811 Q_ASSERT(isActive());
813 if (state->compositionMode() == QPainter::CompositionMode_Destination)
817 d->saveGraphicsState();
821 qpattern->data.pixmap = pixmap;
822 qpattern->foreground = d->current.pen.color();
823 qpattern->pdev = d->pdev;
824 CGPatternCallbacks callbks;
829 CGAffineTransform trans = CGContextGetCTM(d->hd);
830 CGPatternRef pat = CGPatternCreate(qpattern, CGRectMake(0, 0, width, height),
831 trans, width, height,
832 kCGPatternTilingNoDistortion,
true, &callbks);
834 CGContextSetFillColorSpace(d->hd, cs);
835 CGFloat component = 1.0;
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);
841 CGRect mac_rect = CGRectMake(r.x(), r.y(), r.width(), r.height());
842 CGContextFillRect(d->hd, mac_rect);
845 d->restoreGraphicsState();
847 CGColorSpaceRelease(cs);
848 CGPatternRelease(pat);
853 Q_D(QCoreGraphicsPaintEngine);
854 if (d->current.transform.type() == QTransform::TxProject
855#ifndef QMAC_NATIVE_GRADIENTS
856 || painter()->pen().brush().gradient()
859 QPaintEngine::drawTextItem(pos, item);
863 if (state->compositionMode() == QPainter::CompositionMode_Destination)
866 const QTextItemInt &ti =
static_cast<
const QTextItemInt &>(item);
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));
874 Q_ASSERT(type() == QPaintEngine::CoreGraphics);
876 QFontEngine *fe = ti.fontEngine;
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);
884 const bool smoothing = textAA && !(fe->fontDef.styleStrategy & QFont::NoSubpixelAntialias);
885 if (d->disabledSmoothFonts == smoothing)
886 CGContextSetShouldSmoothFonts(d->hd, smoothing);
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());
893 case QFontEngine::Box:
894 d->drawBoxTextItem(pos, ti);
901 if (textAA != lineAA)
902 CGContextSetShouldAntialias(d->hd, !textAA);
904 if (smoothing == d->disabledSmoothFonts)
905 CGContextSetShouldSmoothFonts(d->hd, !d->disabledSmoothFonts);
908 updateBrush(oldBrush, oldBrushOrigin);
914 return QPainter::RenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform);
937 int cg_mode = kCGBlendModeNormal;
939 case QPainter::CompositionMode_Multiply:
940 cg_mode = kCGBlendModeMultiply;
942 case QPainter::CompositionMode_Screen:
943 cg_mode = kCGBlendModeScreen;
945 case QPainter::CompositionMode_Overlay:
946 cg_mode = kCGBlendModeOverlay;
948 case QPainter::CompositionMode_Darken:
949 cg_mode = kCGBlendModeDarken;
951 case QPainter::CompositionMode_Lighten:
952 cg_mode = kCGBlendModeLighten;
954 case QPainter::CompositionMode_ColorDodge:
955 cg_mode = kCGBlendModeColorDodge;
957 case QPainter::CompositionMode_ColorBurn:
958 cg_mode = kCGBlendModeColorBurn;
960 case QPainter::CompositionMode_HardLight:
961 cg_mode = kCGBlendModeHardLight;
963 case QPainter::CompositionMode_SoftLight:
964 cg_mode = kCGBlendModeSoftLight;
966 case QPainter::CompositionMode_Difference:
967 cg_mode = kCGBlendModeDifference;
969 case QPainter::CompositionMode_Exclusion:
970 cg_mode = kCGBlendModeExclusion;
972 case QPainter::CompositionMode_Plus:
973 cg_mode = kCGBlendModePlusLighter;
975 case QPainter::CompositionMode_SourceOver:
976 cg_mode = kCGBlendModeNormal;
978 case QPainter::CompositionMode_DestinationOver:
979 cg_mode = kCGBlendModeDestinationOver;
981 case QPainter::CompositionMode_Clear:
982 cg_mode = kCGBlendModeClear;
984 case QPainter::CompositionMode_Source:
985 cg_mode = kCGBlendModeCopy;
987 case QPainter::CompositionMode_Destination:
990 case QPainter::CompositionMode_SourceIn:
991 cg_mode = kCGBlendModeSourceIn;
993 case QPainter::CompositionMode_DestinationIn:
996 case QPainter::CompositionMode_SourceOut:
997 cg_mode = kCGBlendModeSourceOut;
999 case QPainter::CompositionMode_DestinationOut:
1000 cg_mode = kCGBlendModeDestinationOver;
1002 case QPainter::CompositionMode_SourceAtop:
1003 cg_mode = kCGBlendModeSourceAtop;
1005 case QPainter::CompositionMode_DestinationAtop:
1006 cg_mode = kCGBlendModeDestinationAtop;
1008 case QPainter::CompositionMode_Xor:
1009 cg_mode = kCGBlendModeXOR;
1015 CGContextSetBlendMode(d_func()->hd, CGBlendMode(cg_mode));
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);
1034
1035
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()));
1044
1045
1046
1049 Q_Q(QCoreGraphicsPaintEngine);
1050 float ret = penWidth;
1051 if (!complexXForm && !(q->state->renderHints() & QPainter::Antialiasing)) {
1054 else if (penWidth < 3)
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);
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);
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);
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)) {
1113 linedashes[i] += cglinewidth/2;
1115 linedashes[i] -= cglinewidth/2;
1118 CGContextSetLineDash(
hd, pen.dashOffset() * cglinewidth, linedashes.data(), linedashes.size());
1121 CGContextSetStrokeColorWithColor(hd, cgColorForQColor(pen.color()));
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];
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);
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 *>(¤t.brush),
1162 1, domain, 4,
nullptr, &callbacks);
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);
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);
1182 CGFunctionRelease(fill_func);
1186 if (bs != Qt::SolidPattern && bs != Qt::NoBrush
1187#ifndef QT_MAC_USE_NATIVE_GRADIENTS
1188 && (bs < Qt::LinearGradientPattern || bs > Qt::ConicalGradientPattern)
1193 qpattern->pdev = pdev;
1194 CGFloat components[4] = { 1.0, 1.0, 1.0, 1.0 };
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);
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);
1216 qpattern->foreground = current.brush.color();
1218 CGColorSpaceRef fill_colorspace = CGColorSpaceCreatePattern(base_colorspace);
1219 CGContextSetFillColorSpace(
hd, fill_colorspace);
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());
1225 CGPatternCallbacks callbks;
1226 callbks.version = 0;
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);
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()));
1246 Q_Q(QCoreGraphicsPaintEngine);
1249 QRegion sysClip = q->systemClip();
1250 if (!sysClip.isEmpty())
1251 qt_mac_clip_cg(hd, sysClip, &orig_xform);
1253 qt_mac_clip_cg(
hd, *rgn,
nullptr);
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);
1270 case kCGPathElementAddLineToPoint:
1271 CGPathAddLineToPoint(t->path, &t->transform, element->points[0].x, element->points[0].y);
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);
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);
1282 case kCGPathElementCloseSubpath:
1283 CGPathCloseSubpath(t->path);
1286 qDebug() <<
"Unhandled path transform type: " << element->type;
1292 Q_Q(QCoreGraphicsPaintEngine);
1297 CGContextBeginPath(
hd);
1298 CGContextAddPath(
hd, path);
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));
1311 CGContextDrawShading(hd, shading);
1315 }
else if (current.brush.style() == Qt::NoBrush) {
1320 if ((ops & CGStroke) && current.pen.style() == Qt::NoPen)
1324 CGContextBeginPath(
hd);
1325 CGContextAddPath(
hd, path);
1327 CGContextEOFillPath(
hd);
1329 CGContextFillPath(
hd);
1334 const bool needContextSave = (cosmeticPen != QCoreGraphicsPaintEnginePrivate::CosmeticNone ||
1335 !(q->state->renderHints() & QPainter::Antialiasing));
1337 if (needContextSave)
1339 CGContextBeginPath(
hd);
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);
1349 CGContextTranslateCTM(hd, 0,
double(pixelSize.y()) * 0.1);
1354 if (q->state->renderHints() & QPainter::Antialiasing)
1356 else if (current.pen.widthF() <= 1)
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);
1366 setTransform(
nullptr);
1368 CGContextAddPath(
hd, t.path);
1369 CGPathRelease(t.path);
1371 CGContextAddPath(
hd, path);
1374 CGContextStrokePath(
hd);
1375 if (needContextSave)
void setFillBrush(const QPointF &origin=QPoint())
QPointF devicePixelSize(CGContextRef context)
float adjustPenWidth(float penWidth)
void restoreGraphicsState()
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.
~QCoreGraphicsPaintEngine()
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.
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)
@ kCGCompositeModeDestinationAtop
@ kCGCompositeModeSourceOut
@ kCGCompositeModePlusLighter
@ kCGCompositeModeDestinationOver
@ kCGCompositeModeDestinationIn
@ kCGCompositeModeSourceAtop
@ kCGCompositeModePlusDarker
@ kCGCompositeModeSourceOver
@ kCGCompositeModeSourceIn
@ kCGCompositeModeDestinationOut
struct CGContext * CGContextRef
struct CGColorSpace * CGColorSpaceRef