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