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
|
/*
* Copyright (C) 2003, 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
* Copyright (C) 2008 Eric Seidel <eric@webkit.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#define _USE_MATH_DEFINES 1
#include "config.h"
#include "GraphicsContextCG.h"
#include "AffineTransform.h"
#include "FloatConversion.h"
#include "GraphicsContextPlatformPrivateCG.h"
#include "ImageBuffer.h"
#include "KURL.h"
#include "Path.h"
#include "Pattern.h"
#include "ShadowBlur.h"
#include <CoreGraphics/CoreGraphics.h>
#include <wtf/MathExtras.h>
#include <wtf/OwnArrayPtr.h>
#include <wtf/RetainPtr.h>
#include <wtf/UnusedParam.h>
#if PLATFORM(MAC) || PLATFORM(CHROMIUM)
#include "WebCoreSystemInterface.h"
#endif
#if PLATFORM(WIN)
#include <WebKitSystemInterface/WebKitSystemInterface.h>
#endif
#if PLATFORM(MAC) || (PLATFORM(CHROMIUM) && OS(DARWIN))
#if !defined(BUILDING_ON_TIGER) && !defined(BUILDING_ON_LEOPARD)
// Building on 10.6 or later: kCGInterpolationMedium is defined in the CGInterpolationQuality enum.
#define HAVE_CG_INTERPOLATION_MEDIUM 1
#endif
#if !defined(TARGETING_TIGER) && !defined(TARGETING_LEOPARD)
// Targeting 10.6 or later: use kCGInterpolationMedium.
#define WTF_USE_CG_INTERPOLATION_MEDIUM 1
#endif
#endif
// Undocumented CGContextSetCTM function, available at least since 10.4.
extern "C" {
CG_EXTERN void CGContextSetCTM(CGContextRef, CGAffineTransform);
};
using namespace std;
namespace WebCore {
static void setCGFillColor(CGContextRef context, const Color& color, ColorSpace colorSpace)
{
CGContextSetFillColorWithColor(context, cachedCGColor(color, colorSpace));
}
static void setCGStrokeColor(CGContextRef context, const Color& color, ColorSpace colorSpace)
{
CGContextSetStrokeColorWithColor(context, cachedCGColor(color, colorSpace));
}
CGColorSpaceRef deviceRGBColorSpaceRef()
{
static CGColorSpaceRef deviceSpace = CGColorSpaceCreateDeviceRGB();
return deviceSpace;
}
CGColorSpaceRef sRGBColorSpaceRef()
{
// FIXME: Windows should be able to use kCGColorSpaceSRGB, this is tracked by http://webkit.org/b/31363.
#if PLATFORM(WIN) || defined(BUILDING_ON_TIGER)
return deviceRGBColorSpaceRef();
#else
static CGColorSpaceRef sRGBSpace = CGColorSpaceCreateWithName(kCGColorSpaceSRGB);
return sRGBSpace;
#endif
}
CGColorSpaceRef linearRGBColorSpaceRef()
{
// FIXME: Windows should be able to use kCGColorSpaceGenericRGBLinear, this is tracked by http://webkit.org/b/31363.
#if PLATFORM(WIN) || defined(BUILDING_ON_TIGER)
return deviceRGBColorSpaceRef();
#else
static CGColorSpaceRef linearRGBSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGBLinear);
return linearRGBSpace;
#endif
}
void GraphicsContext::platformInit(CGContextRef cgContext)
{
m_data = new GraphicsContextPlatformPrivate(cgContext);
setPaintingDisabled(!cgContext);
if (cgContext) {
// Make sure the context starts in sync with our state.
setPlatformFillColor(fillColor(), fillColorSpace());
setPlatformStrokeColor(strokeColor(), strokeColorSpace());
}
}
void GraphicsContext::platformDestroy()
{
delete m_data;
}
CGContextRef GraphicsContext::platformContext() const
{
ASSERT(!paintingDisabled());
ASSERT(m_data->m_cgContext);
return m_data->m_cgContext.get();
}
void GraphicsContext::savePlatformState()
{
// Note: Do not use this function within this class implementation, since we want to avoid the extra
// save of the secondary context (in GraphicsContextPlatformPrivateCG.h).
CGContextSaveGState(platformContext());
m_data->save();
}
void GraphicsContext::restorePlatformState()
{
// Note: Do not use this function within this class implementation, since we want to avoid the extra
// restore of the secondary context (in GraphicsContextPlatformPrivateCG.h).
CGContextRestoreGState(platformContext());
m_data->restore();
m_data->m_userToDeviceTransformKnownToBeIdentity = false;
}
// Draws a filled rectangle with a stroked border.
void GraphicsContext::drawRect(const IntRect& rect)
{
// FIXME: this function does not handle patterns and gradients
// like drawPath does, it probably should.
if (paintingDisabled())
return;
CGContextRef context = platformContext();
CGContextFillRect(context, rect);
if (strokeStyle() != NoStroke) {
// We do a fill of four rects to simulate the stroke of a border.
Color oldFillColor = fillColor();
if (oldFillColor != strokeColor())
setCGFillColor(context, strokeColor(), strokeColorSpace());
CGRect rects[4] = {
FloatRect(rect.x(), rect.y(), rect.width(), 1),
FloatRect(rect.x(), rect.maxY() - 1, rect.width(), 1),
FloatRect(rect.x(), rect.y() + 1, 1, rect.height() - 2),
FloatRect(rect.maxX() - 1, rect.y() + 1, 1, rect.height() - 2)
};
CGContextFillRects(context, rects, 4);
if (oldFillColor != strokeColor())
setCGFillColor(context, oldFillColor, fillColorSpace());
}
}
// This is only used to draw borders.
void GraphicsContext::drawLine(const IntPoint& point1, const IntPoint& point2)
{
if (paintingDisabled())
return;
if (strokeStyle() == NoStroke)
return;
float width = strokeThickness();
FloatPoint p1 = point1;
FloatPoint p2 = point2;
bool isVerticalLine = (p1.x() == p2.x());
// For odd widths, we add in 0.5 to the appropriate x/y so that the float arithmetic
// works out. For example, with a border width of 3, KHTML will pass us (y1+y2)/2, e.g.,
// (50+53)/2 = 103/2 = 51 when we want 51.5. It is always true that an even width gave
// us a perfect position, but an odd width gave us a position that is off by exactly 0.5.
if (strokeStyle() == DottedStroke || strokeStyle() == DashedStroke) {
if (isVerticalLine) {
p1.move(0, width);
p2.move(0, -width);
} else {
p1.move(width, 0);
p2.move(-width, 0);
}
}
if (((int)width) % 2) {
if (isVerticalLine) {
// We're a vertical line. Adjust our x.
p1.move(0.5f, 0.0f);
p2.move(0.5f, 0.0f);
} else {
// We're a horizontal line. Adjust our y.
p1.move(0.0f, 0.5f);
p2.move(0.0f, 0.5f);
}
}
int patWidth = 0;
switch (strokeStyle()) {
case NoStroke:
case SolidStroke:
break;
case DottedStroke:
patWidth = (int)width;
break;
case DashedStroke:
patWidth = 3 * (int)width;
break;
}
CGContextRef context = platformContext();
if (shouldAntialias())
CGContextSetShouldAntialias(context, false);
if (patWidth) {
CGContextSaveGState(context);
// Do a rect fill of our endpoints. This ensures we always have the
// appearance of being a border. We then draw the actual dotted/dashed line.
setCGFillColor(context, strokeColor(), strokeColorSpace()); // The save/restore make it safe to mutate the fill color here without setting it back to the old color.
if (isVerticalLine) {
CGContextFillRect(context, FloatRect(p1.x() - width / 2, p1.y() - width, width, width));
CGContextFillRect(context, FloatRect(p2.x() - width / 2, p2.y(), width, width));
} else {
CGContextFillRect(context, FloatRect(p1.x() - width, p1.y() - width / 2, width, width));
CGContextFillRect(context, FloatRect(p2.x(), p2.y() - width / 2, width, width));
}
// Example: 80 pixels with a width of 30 pixels.
// Remainder is 20. The maximum pixels of line we could paint
// will be 50 pixels.
int distance = (isVerticalLine ? (point2.y() - point1.y()) : (point2.x() - point1.x())) - 2*(int)width;
int remainder = distance % patWidth;
int coverage = distance - remainder;
int numSegments = coverage / patWidth;
float patternOffset = 0.0f;
// Special case 1px dotted borders for speed.
if (patWidth == 1)
patternOffset = 1.0f;
else {
bool evenNumberOfSegments = !(numSegments % 2);
if (remainder)
evenNumberOfSegments = !evenNumberOfSegments;
if (evenNumberOfSegments) {
if (remainder) {
patternOffset += patWidth - remainder;
patternOffset += remainder / 2;
} else
patternOffset = patWidth / 2;
} else {
if (remainder)
patternOffset = (patWidth - remainder)/2;
}
}
const CGFloat dottedLine[2] = { patWidth, patWidth };
CGContextSetLineDash(context, patternOffset, dottedLine, 2);
}
CGContextBeginPath(context);
CGContextMoveToPoint(context, p1.x(), p1.y());
CGContextAddLineToPoint(context, p2.x(), p2.y());
CGContextStrokePath(context);
if (patWidth)
CGContextRestoreGState(context);
if (shouldAntialias())
CGContextSetShouldAntialias(context, true);
}
// This method is only used to draw the little circles used in lists.
void GraphicsContext::drawEllipse(const IntRect& rect)
{
if (paintingDisabled())
return;
Path path;
path.addEllipse(rect);
drawPath(path);
}
void GraphicsContext::strokeArc(const IntRect& rect, int startAngle, int angleSpan)
{
if (paintingDisabled() || strokeStyle() == NoStroke || strokeThickness() <= 0.0f)
return;
CGContextRef context = platformContext();
CGContextSaveGState(context);
CGContextBeginPath(context);
CGContextSetShouldAntialias(context, false);
int x = rect.x();
int y = rect.y();
float w = (float)rect.width();
float h = (float)rect.height();
float scaleFactor = h / w;
float reverseScaleFactor = w / h;
if (w != h)
scale(FloatSize(1, scaleFactor));
float hRadius = w / 2;
float vRadius = h / 2;
float fa = startAngle;
float falen = fa + angleSpan;
float start = -fa * piFloat / 180.0f;
float end = -falen * piFloat / 180.0f;
CGContextAddArc(context, x + hRadius, (y + vRadius) * reverseScaleFactor, hRadius, start, end, true);
if (w != h)
scale(FloatSize(1, reverseScaleFactor));
float width = strokeThickness();
int patWidth = 0;
switch (strokeStyle()) {
case DottedStroke:
patWidth = (int)(width / 2);
break;
case DashedStroke:
patWidth = 3 * (int)(width / 2);
break;
default:
break;
}
if (patWidth) {
// Example: 80 pixels with a width of 30 pixels.
// Remainder is 20. The maximum pixels of line we could paint
// will be 50 pixels.
int distance;
if (hRadius == vRadius)
distance = static_cast<int>((piFloat * hRadius) / 2.0f);
else // We are elliptical and will have to estimate the distance
distance = static_cast<int>((piFloat * sqrtf((hRadius * hRadius + vRadius * vRadius) / 2.0f)) / 2.0f);
int remainder = distance % patWidth;
int coverage = distance - remainder;
int numSegments = coverage / patWidth;
float patternOffset = 0.0f;
// Special case 1px dotted borders for speed.
if (patWidth == 1)
patternOffset = 1.0f;
else {
bool evenNumberOfSegments = !(numSegments % 2);
if (remainder)
evenNumberOfSegments = !evenNumberOfSegments;
if (evenNumberOfSegments) {
if (remainder) {
patternOffset += patWidth - remainder;
patternOffset += remainder / 2.0f;
} else
patternOffset = patWidth / 2.0f;
} else {
if (remainder)
patternOffset = (patWidth - remainder) / 2.0f;
}
}
const CGFloat dottedLine[2] = { patWidth, patWidth };
CGContextSetLineDash(context, patternOffset, dottedLine, 2);
}
CGContextStrokePath(context);
CGContextRestoreGState(context);
}
static void addConvexPolygonToPath(Path& path, size_t numberOfPoints, const FloatPoint* points)
{
ASSERT(numberOfPoints > 0);
path.moveTo(points[0]);
for (size_t i = 1; i < numberOfPoints; ++i)
path.addLineTo(points[i]);
path.closeSubpath();
}
void GraphicsContext::drawConvexPolygon(size_t numberOfPoints, const FloatPoint* points, bool antialiased)
{
if (paintingDisabled())
return;
if (numberOfPoints <= 1)
return;
CGContextRef context = platformContext();
if (antialiased != shouldAntialias())
CGContextSetShouldAntialias(context, antialiased);
Path path;
addConvexPolygonToPath(path, numberOfPoints, points);
drawPath(path);
if (antialiased != shouldAntialias())
CGContextSetShouldAntialias(context, shouldAntialias());
}
void GraphicsContext::clipConvexPolygon(size_t numberOfPoints, const FloatPoint* points, bool antialias)
{
if (paintingDisabled())
return;
if (numberOfPoints <= 1)
return;
CGContextRef context = platformContext();
if (antialias != shouldAntialias())
CGContextSetShouldAntialias(context, antialias);
Path path;
addConvexPolygonToPath(path, numberOfPoints, points);
clipPath(path, RULE_NONZERO);
if (antialias != shouldAntialias())
CGContextSetShouldAntialias(context, shouldAntialias());
}
void GraphicsContext::applyStrokePattern()
{
CGContextRef cgContext = platformContext();
RetainPtr<CGPatternRef> platformPattern(AdoptCF, m_state.strokePattern->createPlatformPattern(getCTM()));
if (!platformPattern)
return;
RetainPtr<CGColorSpaceRef> patternSpace(AdoptCF, CGColorSpaceCreatePattern(0));
CGContextSetStrokeColorSpace(cgContext, patternSpace.get());
const CGFloat patternAlpha = 1;
CGContextSetStrokePattern(cgContext, platformPattern.get(), &patternAlpha);
}
void GraphicsContext::applyFillPattern()
{
CGContextRef cgContext = platformContext();
RetainPtr<CGPatternRef> platformPattern(AdoptCF, m_state.fillPattern->createPlatformPattern(getCTM()));
if (!platformPattern)
return;
RetainPtr<CGColorSpaceRef> patternSpace(AdoptCF, CGColorSpaceCreatePattern(0));
CGContextSetFillColorSpace(cgContext, patternSpace.get());
const CGFloat patternAlpha = 1;
CGContextSetFillPattern(cgContext, platformPattern.get(), &patternAlpha);
}
static inline bool calculateDrawingMode(const GraphicsContextState& state, CGPathDrawingMode& mode)
{
bool shouldFill = state.fillPattern || state.fillColor.alpha();
bool shouldStroke = state.strokePattern || (state.strokeStyle != NoStroke && state.strokeColor.alpha());
bool useEOFill = state.fillRule == RULE_EVENODD;
if (shouldFill) {
if (shouldStroke) {
if (useEOFill)
mode = kCGPathEOFillStroke;
else
mode = kCGPathFillStroke;
} else { // fill, no stroke
if (useEOFill)
mode = kCGPathEOFill;
else
mode = kCGPathFill;
}
} else {
// Setting mode to kCGPathStroke even if shouldStroke is false. In that case, we return false and mode will not be used,
// but the compiler will not complain about an uninitialized variable.
mode = kCGPathStroke;
}
return shouldFill || shouldStroke;
}
void GraphicsContext::drawPath(const Path& path)
{
if (paintingDisabled())
return;
CGContextRef context = platformContext();
const GraphicsContextState& state = m_state;
if (state.fillGradient || state.strokeGradient) {
// We don't have any optimized way to fill & stroke a path using gradients
// FIXME: Be smarter about this.
fillPath(path);
strokePath(path);
return;
}
CGContextBeginPath(context);
CGContextAddPath(context, path.platformPath());
if (state.fillPattern)
applyFillPattern();
if (state.strokePattern)
applyStrokePattern();
CGPathDrawingMode drawingMode;
if (calculateDrawingMode(state, drawingMode))
CGContextDrawPath(context, drawingMode);
}
static inline void fillPathWithFillRule(CGContextRef context, WindRule fillRule)
{
if (fillRule == RULE_EVENODD)
CGContextEOFillPath(context);
else
CGContextFillPath(context);
}
void GraphicsContext::fillPath(const Path& path)
{
if (paintingDisabled())
return;
CGContextRef context = platformContext();
if (m_state.fillGradient) {
if (hasShadow()) {
FloatRect rect = path.boundingRect();
CGLayerRef layer = CGLayerCreateWithContext(context, CGSizeMake(rect.width(), rect.height()), 0);
CGContextRef layerContext = CGLayerGetContext(layer);
CGContextTranslateCTM(layerContext, -rect.x(), -rect.y());
CGContextBeginPath(layerContext);
CGContextAddPath(layerContext, path.platformPath());
CGContextConcatCTM(layerContext, m_state.fillGradient->gradientSpaceTransform());
if (fillRule() == RULE_EVENODD)
CGContextEOClip(layerContext);
else
CGContextClip(layerContext);
m_state.fillGradient->paint(layerContext);
CGContextDrawLayerAtPoint(context, CGPointMake(rect.x(), rect.y()), layer);
CGLayerRelease(layer);
} else {
CGContextBeginPath(context);
CGContextAddPath(context, path.platformPath());
CGContextSaveGState(context);
CGContextConcatCTM(context, m_state.fillGradient->gradientSpaceTransform());
if (fillRule() == RULE_EVENODD)
CGContextEOClip(context);
else
CGContextClip(context);
m_state.fillGradient->paint(this);
CGContextRestoreGState(context);
}
return;
}
CGContextBeginPath(context);
CGContextAddPath(context, path.platformPath());
if (m_state.fillPattern)
applyFillPattern();
fillPathWithFillRule(context, fillRule());
}
void GraphicsContext::strokePath(const Path& path)
{
if (paintingDisabled())
return;
CGContextRef context = platformContext();
CGContextBeginPath(context);
CGContextAddPath(context, path.platformPath());
if (m_state.strokeGradient) {
if (hasShadow()) {
FloatRect rect = path.boundingRect();
float lineWidth = strokeThickness();
float doubleLineWidth = lineWidth * 2;
float layerWidth = ceilf(rect.width() + doubleLineWidth);
float layerHeight = ceilf(rect.height() + doubleLineWidth);
CGLayerRef layer = CGLayerCreateWithContext(context, CGSizeMake(layerWidth, layerHeight), 0);
CGContextRef layerContext = CGLayerGetContext(layer);
CGContextSetLineWidth(layerContext, lineWidth);
// Compensate for the line width, otherwise the layer's top-left corner would be
// aligned with the rect's top-left corner. This would result in leaving pixels out of
// the layer on the left and top sides.
float translationX = lineWidth - rect.x();
float translationY = lineWidth - rect.y();
CGContextTranslateCTM(layerContext, translationX, translationY);
CGContextAddPath(layerContext, path.platformPath());
CGContextReplacePathWithStrokedPath(layerContext);
CGContextClip(layerContext);
CGContextConcatCTM(layerContext, m_state.strokeGradient->gradientSpaceTransform());
m_state.strokeGradient->paint(layerContext);
float destinationX = roundf(rect.x() - lineWidth);
float destinationY = roundf(rect.y() - lineWidth);
CGContextDrawLayerAtPoint(context, CGPointMake(destinationX, destinationY), layer);
CGLayerRelease(layer);
} else {
CGContextSaveGState(context);
CGContextReplacePathWithStrokedPath(context);
CGContextClip(context);
CGContextConcatCTM(context, m_state.strokeGradient->gradientSpaceTransform());
m_state.strokeGradient->paint(this);
CGContextRestoreGState(context);
}
return;
}
if (m_state.strokePattern)
applyStrokePattern();
CGContextStrokePath(context);
}
static float radiusToLegacyRadius(float radius)
{
return radius > 8 ? 8 + 4 * sqrt((radius - 8) / 2) : radius;
}
static bool hasBlurredShadow(const GraphicsContextState& state)
{
return state.shadowColor.isValid() && state.shadowColor.alpha() && state.shadowBlur;
}
void GraphicsContext::fillRect(const FloatRect& rect)
{
if (paintingDisabled())
return;
CGContextRef context = platformContext();
if (m_state.fillGradient) {
CGContextSaveGState(context);
if (hasShadow()) {
CGLayerRef layer = CGLayerCreateWithContext(context, CGSizeMake(rect.width(), rect.height()), 0);
CGContextRef layerContext = CGLayerGetContext(layer);
CGContextTranslateCTM(layerContext, -rect.x(), -rect.y());
CGContextAddRect(layerContext, rect);
CGContextClip(layerContext);
CGContextConcatCTM(layerContext, m_state.fillGradient->gradientSpaceTransform());
m_state.fillGradient->paint(layerContext);
CGContextDrawLayerAtPoint(context, CGPointMake(rect.x(), rect.y()), layer);
CGLayerRelease(layer);
} else {
CGContextClipToRect(context, rect);
CGContextConcatCTM(context, m_state.fillGradient->gradientSpaceTransform());
m_state.fillGradient->paint(this);
}
CGContextRestoreGState(context);
return;
}
if (m_state.fillPattern)
applyFillPattern();
bool drawOwnShadow = hasBlurredShadow(m_state) && !m_state.shadowsIgnoreTransforms; // Don't use ShadowBlur for canvas yet.
if (drawOwnShadow) {
float shadowBlur = m_state.shadowsUseLegacyRadius ? radiusToLegacyRadius(m_state.shadowBlur) : m_state.shadowBlur;
// Turn off CG shadows.
CGContextSaveGState(context);
CGContextSetShadowWithColor(platformContext(), CGSizeZero, 0, 0);
ShadowBlur contextShadow(shadowBlur, m_state.shadowOffset, m_state.shadowColor, m_state.shadowColorSpace);
contextShadow.drawRectShadow(this, rect, RoundedIntRect::Radii());
}
CGContextFillRect(context, rect);
if (drawOwnShadow)
CGContextRestoreGState(context);
}
void GraphicsContext::fillRect(const FloatRect& rect, const Color& color, ColorSpace colorSpace)
{
if (paintingDisabled())
return;
CGContextRef context = platformContext();
Color oldFillColor = fillColor();
ColorSpace oldColorSpace = fillColorSpace();
if (oldFillColor != color || oldColorSpace != colorSpace)
setCGFillColor(context, color, colorSpace);
bool drawOwnShadow = hasBlurredShadow(m_state) && !m_state.shadowsIgnoreTransforms; // Don't use ShadowBlur for canvas yet.
if (drawOwnShadow) {
float shadowBlur = m_state.shadowsUseLegacyRadius ? radiusToLegacyRadius(m_state.shadowBlur) : m_state.shadowBlur;
// Turn off CG shadows.
CGContextSaveGState(context);
CGContextSetShadowWithColor(platformContext(), CGSizeZero, 0, 0);
ShadowBlur contextShadow(shadowBlur, m_state.shadowOffset, m_state.shadowColor, m_state.shadowColorSpace);
contextShadow.drawRectShadow(this, rect, RoundedIntRect::Radii());
}
CGContextFillRect(context, rect);
if (drawOwnShadow)
CGContextRestoreGState(context);
if (oldFillColor != color || oldColorSpace != colorSpace)
setCGFillColor(context, oldFillColor, oldColorSpace);
}
void GraphicsContext::fillRoundedRect(const IntRect& rect, const IntSize& topLeft, const IntSize& topRight, const IntSize& bottomLeft, const IntSize& bottomRight, const Color& color, ColorSpace colorSpace)
{
if (paintingDisabled())
return;
CGContextRef context = platformContext();
Color oldFillColor = fillColor();
ColorSpace oldColorSpace = fillColorSpace();
if (oldFillColor != color || oldColorSpace != colorSpace)
setCGFillColor(context, color, colorSpace);
Path path;
path.addRoundedRect(rect, topLeft, topRight, bottomLeft, bottomRight);
bool drawOwnShadow = hasBlurredShadow(m_state) && !m_state.shadowsIgnoreTransforms; // Don't use ShadowBlur for canvas yet.
if (drawOwnShadow) {
float shadowBlur = m_state.shadowsUseLegacyRadius ? radiusToLegacyRadius(m_state.shadowBlur) : m_state.shadowBlur;
// Turn off CG shadows.
CGContextSaveGState(context);
CGContextSetShadowWithColor(platformContext(), CGSizeZero, 0, 0);
ShadowBlur contextShadow(shadowBlur, m_state.shadowOffset, m_state.shadowColor, m_state.shadowColorSpace);
contextShadow.drawRectShadow(this, rect, RoundedIntRect::Radii(topLeft, topRight, bottomLeft, bottomRight));
}
fillPath(path);
if (drawOwnShadow)
CGContextRestoreGState(context);
if (oldFillColor != color || oldColorSpace != colorSpace)
setCGFillColor(context, oldFillColor, oldColorSpace);
}
void GraphicsContext::fillRectWithRoundedHole(const IntRect& rect, const RoundedIntRect& roundedHoleRect, const Color& color, ColorSpace colorSpace)
{
if (paintingDisabled())
return;
CGContextRef context = platformContext();
Path path;
path.addRect(rect);
if (!roundedHoleRect.radii().isZero())
path.addRoundedRect(roundedHoleRect.rect(), roundedHoleRect.radii().topLeft(), roundedHoleRect.radii().topRight(), roundedHoleRect.radii().bottomLeft(), roundedHoleRect.radii().bottomRight());
else
path.addRect(roundedHoleRect.rect());
WindRule oldFillRule = fillRule();
Color oldFillColor = fillColor();
ColorSpace oldFillColorSpace = fillColorSpace();
setFillRule(RULE_EVENODD);
setFillColor(color, colorSpace);
// fillRectWithRoundedHole() assumes that the edges of rect are clipped out, so we only care about shadows cast around inside the hole.
bool drawOwnShadow = hasBlurredShadow(m_state) && !m_state.shadowsIgnoreTransforms;
if (drawOwnShadow) {
float shadowBlur = m_state.shadowsUseLegacyRadius ? radiusToLegacyRadius(m_state.shadowBlur) : m_state.shadowBlur;
// Turn off CG shadows.
CGContextSaveGState(context);
CGContextSetShadowWithColor(platformContext(), CGSizeZero, 0, 0);
ShadowBlur contextShadow(shadowBlur, m_state.shadowOffset, m_state.shadowColor, m_state.shadowColorSpace);
contextShadow.drawInsetShadow(this, rect, roundedHoleRect.rect(), roundedHoleRect.radii());
}
fillPath(path);
if (drawOwnShadow)
CGContextRestoreGState(context);
setFillRule(oldFillRule);
setFillColor(oldFillColor, oldFillColorSpace);
}
void GraphicsContext::clip(const FloatRect& rect)
{
if (paintingDisabled())
return;
CGContextClipToRect(platformContext(), rect);
m_data->clip(rect);
}
void GraphicsContext::clipOut(const IntRect& rect)
{
if (paintingDisabled())
return;
CGRect rects[2] = { CGContextGetClipBoundingBox(platformContext()), rect };
CGContextBeginPath(platformContext());
CGContextAddRects(platformContext(), rects, 2);
CGContextEOClip(platformContext());
}
void GraphicsContext::clipPath(const Path& path, WindRule clipRule)
{
if (paintingDisabled())
return;
if (path.isEmpty())
return;
CGContextRef context = platformContext();
CGContextBeginPath(platformContext());
CGContextAddPath(platformContext(), path.platformPath());
if (clipRule == RULE_EVENODD)
CGContextEOClip(context);
else
CGContextClip(context);
}
IntRect GraphicsContext::clipBounds() const
{
return enclosingIntRect(CGContextGetClipBoundingBox(platformContext()));
}
void GraphicsContext::addInnerRoundedRectClip(const IntRect& rect, int thickness)
{
if (paintingDisabled())
return;
clip(rect);
CGContextRef context = platformContext();
// Add outer ellipse
CGContextAddEllipseInRect(context, CGRectMake(rect.x(), rect.y(), rect.width(), rect.height()));
// Add inner ellipse.
CGContextAddEllipseInRect(context, CGRectMake(rect.x() + thickness, rect.y() + thickness,
rect.width() - (thickness * 2), rect.height() - (thickness * 2)));
CGContextEOClip(context);
}
void GraphicsContext::beginTransparencyLayer(float opacity)
{
if (paintingDisabled())
return;
CGContextRef context = platformContext();
CGContextSaveGState(context);
CGContextSetAlpha(context, opacity);
CGContextBeginTransparencyLayer(context, 0);
m_data->beginTransparencyLayer();
m_data->m_userToDeviceTransformKnownToBeIdentity = false;
}
void GraphicsContext::endTransparencyLayer()
{
if (paintingDisabled())
return;
CGContextRef context = platformContext();
CGContextEndTransparencyLayer(context);
CGContextRestoreGState(context);
m_data->endTransparencyLayer();
m_data->m_userToDeviceTransformKnownToBeIdentity = false;
}
void GraphicsContext::setPlatformShadow(const FloatSize& offset, float blur, const Color& color, ColorSpace colorSpace)
{
if (paintingDisabled())
return;
// FIXME: we could avoid the shadow setup cost when we know we'll render the shadow ourselves.
CGFloat xOffset = offset.width();
CGFloat yOffset = offset.height();
CGFloat blurRadius = blur;
CGContextRef context = platformContext();
if (!m_state.shadowsIgnoreTransforms) {
CGAffineTransform userToBaseCTM = wkGetUserToBaseCTM(context);
CGFloat A = userToBaseCTM.a * userToBaseCTM.a + userToBaseCTM.b * userToBaseCTM.b;
CGFloat B = userToBaseCTM.a * userToBaseCTM.c + userToBaseCTM.b * userToBaseCTM.d;
CGFloat C = B;
CGFloat D = userToBaseCTM.c * userToBaseCTM.c + userToBaseCTM.d * userToBaseCTM.d;
CGFloat smallEigenvalue = narrowPrecisionToCGFloat(sqrt(0.5 * ((A + D) - sqrt(4 * B * C + (A - D) * (A - D)))));
// Extreme "blur" values can make text drawing crash or take crazy long times, so clamp
blurRadius = min(blur * smallEigenvalue, narrowPrecisionToCGFloat(1000.0));
CGSize offsetInBaseSpace = CGSizeApplyAffineTransform(offset, userToBaseCTM);
xOffset = offsetInBaseSpace.width;
yOffset = offsetInBaseSpace.height;
}
// Work around <rdar://problem/5539388> by ensuring that the offsets will get truncated
// to the desired integer.
static const CGFloat extraShadowOffset = narrowPrecisionToCGFloat(1.0 / 128);
if (xOffset > 0)
xOffset += extraShadowOffset;
else if (xOffset < 0)
xOffset -= extraShadowOffset;
if (yOffset > 0)
yOffset += extraShadowOffset;
else if (yOffset < 0)
yOffset -= extraShadowOffset;
// Check for an invalid color, as this means that the color was not set for the shadow
// and we should therefore just use the default shadow color.
if (!color.isValid())
CGContextSetShadow(context, CGSizeMake(xOffset, yOffset), blurRadius);
else
CGContextSetShadowWithColor(context, CGSizeMake(xOffset, yOffset), blurRadius, cachedCGColor(color, colorSpace));
}
void GraphicsContext::clearPlatformShadow()
{
if (paintingDisabled())
return;
CGContextSetShadowWithColor(platformContext(), CGSizeZero, 0, 0);
}
void GraphicsContext::setMiterLimit(float limit)
{
if (paintingDisabled())
return;
CGContextSetMiterLimit(platformContext(), limit);
}
void GraphicsContext::setAlpha(float alpha)
{
if (paintingDisabled())
return;
CGContextSetAlpha(platformContext(), alpha);
}
void GraphicsContext::clearRect(const FloatRect& r)
{
if (paintingDisabled())
return;
CGContextClearRect(platformContext(), r);
}
void GraphicsContext::strokeRect(const FloatRect& rect, float lineWidth)
{
if (paintingDisabled())
return;
CGContextRef context = platformContext();
if (m_state.strokeGradient) {
if (hasShadow()) {
const float doubleLineWidth = lineWidth * 2;
const float layerWidth = ceilf(rect.width() + doubleLineWidth);
const float layerHeight = ceilf(rect.height() + doubleLineWidth);
CGLayerRef layer = CGLayerCreateWithContext(context, CGSizeMake(layerWidth, layerHeight), 0);
CGContextRef layerContext = CGLayerGetContext(layer);
m_state.strokeThickness = lineWidth;
CGContextSetLineWidth(layerContext, lineWidth);
// Compensate for the line width, otherwise the layer's top-left corner would be
// aligned with the rect's top-left corner. This would result in leaving pixels out of
// the layer on the left and top sides.
const float translationX = lineWidth - rect.x();
const float translationY = lineWidth - rect.y();
CGContextTranslateCTM(layerContext, translationX, translationY);
CGContextAddRect(layerContext, rect);
CGContextReplacePathWithStrokedPath(layerContext);
CGContextClip(layerContext);
CGContextConcatCTM(layerContext, m_state.strokeGradient->gradientSpaceTransform());
m_state.strokeGradient->paint(layerContext);
const float destinationX = roundf(rect.x() - lineWidth);
const float destinationY = roundf(rect.y() - lineWidth);
CGContextDrawLayerAtPoint(context, CGPointMake(destinationX, destinationY), layer);
CGLayerRelease(layer);
} else {
CGContextSaveGState(context);
setStrokeThickness(lineWidth);
CGContextAddRect(context, rect);
CGContextReplacePathWithStrokedPath(context);
CGContextClip(context);
CGContextConcatCTM(context, m_state.strokeGradient->gradientSpaceTransform());
m_state.strokeGradient->paint(this);
CGContextRestoreGState(context);
}
return;
}
if (m_state.strokePattern)
applyStrokePattern();
CGContextStrokeRectWithWidth(context, rect, lineWidth);
}
void GraphicsContext::setLineCap(LineCap cap)
{
if (paintingDisabled())
return;
switch (cap) {
case ButtCap:
CGContextSetLineCap(platformContext(), kCGLineCapButt);
break;
case RoundCap:
CGContextSetLineCap(platformContext(), kCGLineCapRound);
break;
case SquareCap:
CGContextSetLineCap(platformContext(), kCGLineCapSquare);
break;
}
}
void GraphicsContext::setLineDash(const DashArray& dashes, float dashOffset)
{
CGContextSetLineDash(platformContext(), dashOffset, dashes.data(), dashes.size());
}
void GraphicsContext::setLineJoin(LineJoin join)
{
if (paintingDisabled())
return;
switch (join) {
case MiterJoin:
CGContextSetLineJoin(platformContext(), kCGLineJoinMiter);
break;
case RoundJoin:
CGContextSetLineJoin(platformContext(), kCGLineJoinRound);
break;
case BevelJoin:
CGContextSetLineJoin(platformContext(), kCGLineJoinBevel);
break;
}
}
void GraphicsContext::clip(const Path& path)
{
if (paintingDisabled())
return;
CGContextRef context = platformContext();
// CGContextClip does nothing if the path is empty, so in this case, we
// instead clip against a zero rect to reduce the clipping region to
// nothing - which is the intended behavior of clip() if the path is empty.
if (path.isEmpty())
CGContextClipToRect(context, CGRectZero);
else {
CGContextBeginPath(context);
CGContextAddPath(context, path.platformPath());
CGContextClip(context);
}
m_data->clip(path);
}
void GraphicsContext::canvasClip(const Path& path)
{
clip(path);
}
void GraphicsContext::clipOut(const Path& path)
{
if (paintingDisabled())
return;
CGContextBeginPath(platformContext());
CGContextAddRect(platformContext(), CGContextGetClipBoundingBox(platformContext()));
CGContextAddPath(platformContext(), path.platformPath());
CGContextEOClip(platformContext());
}
void GraphicsContext::scale(const FloatSize& size)
{
if (paintingDisabled())
return;
CGContextScaleCTM(platformContext(), size.width(), size.height());
m_data->scale(size);
m_data->m_userToDeviceTransformKnownToBeIdentity = false;
}
void GraphicsContext::rotate(float angle)
{
if (paintingDisabled())
return;
CGContextRotateCTM(platformContext(), angle);
m_data->rotate(angle);
m_data->m_userToDeviceTransformKnownToBeIdentity = false;
}
void GraphicsContext::translate(float x, float y)
{
if (paintingDisabled())
return;
CGContextTranslateCTM(platformContext(), x, y);
m_data->translate(x, y);
m_data->m_userToDeviceTransformKnownToBeIdentity = false;
}
void GraphicsContext::concatCTM(const AffineTransform& transform)
{
if (paintingDisabled())
return;
CGContextConcatCTM(platformContext(), transform);
m_data->concatCTM(transform);
m_data->m_userToDeviceTransformKnownToBeIdentity = false;
}
void GraphicsContext::setCTM(const AffineTransform& transform)
{
if (paintingDisabled())
return;
CGContextSetCTM(platformContext(), transform);
m_data->setCTM(transform);
m_data->m_userToDeviceTransformKnownToBeIdentity = false;
}
AffineTransform GraphicsContext::getCTM() const
{
CGAffineTransform t = CGContextGetCTM(platformContext());
return AffineTransform(t.a, t.b, t.c, t.d, t.tx, t.ty);
}
FloatRect GraphicsContext::roundToDevicePixels(const FloatRect& rect)
{
// It is not enough just to round to pixels in device space. The rotation part of the
// affine transform matrix to device space can mess with this conversion if we have a
// rotating image like the hands of the world clock widget. We just need the scale, so
// we get the affine transform matrix and extract the scale.
if (m_data->m_userToDeviceTransformKnownToBeIdentity)
return rect;
CGAffineTransform deviceMatrix = CGContextGetUserSpaceToDeviceSpaceTransform(platformContext());
if (CGAffineTransformIsIdentity(deviceMatrix)) {
m_data->m_userToDeviceTransformKnownToBeIdentity = true;
return rect;
}
float deviceScaleX = sqrtf(deviceMatrix.a * deviceMatrix.a + deviceMatrix.b * deviceMatrix.b);
float deviceScaleY = sqrtf(deviceMatrix.c * deviceMatrix.c + deviceMatrix.d * deviceMatrix.d);
CGPoint deviceOrigin = CGPointMake(rect.x() * deviceScaleX, rect.y() * deviceScaleY);
CGPoint deviceLowerRight = CGPointMake((rect.x() + rect.width()) * deviceScaleX,
(rect.y() + rect.height()) * deviceScaleY);
deviceOrigin.x = roundf(deviceOrigin.x);
deviceOrigin.y = roundf(deviceOrigin.y);
deviceLowerRight.x = roundf(deviceLowerRight.x);
deviceLowerRight.y = roundf(deviceLowerRight.y);
// Don't let the height or width round to 0 unless either was originally 0
if (deviceOrigin.y == deviceLowerRight.y && rect.height())
deviceLowerRight.y += 1;
if (deviceOrigin.x == deviceLowerRight.x && rect.width())
deviceLowerRight.x += 1;
FloatPoint roundedOrigin = FloatPoint(deviceOrigin.x / deviceScaleX, deviceOrigin.y / deviceScaleY);
FloatPoint roundedLowerRight = FloatPoint(deviceLowerRight.x / deviceScaleX, deviceLowerRight.y / deviceScaleY);
return FloatRect(roundedOrigin, roundedLowerRight - roundedOrigin);
}
void GraphicsContext::drawLineForText(const FloatPoint& point, float width, bool printing)
{
if (paintingDisabled())
return;
if (width <= 0)
return;
float x = point.x();
float y = point.y();
float lineLength = width;
// Use a minimum thickness of 0.5 in user space.
// See http://bugs.webkit.org/show_bug.cgi?id=4255 for details of why 0.5 is the right minimum thickness to use.
float thickness = max(strokeThickness(), 0.5f);
bool restoreAntialiasMode = false;
if (!printing) {
// On screen, use a minimum thickness of 1.0 in user space (later rounded to an integral number in device space).
float adjustedThickness = max(thickness, 1.0f);
// FIXME: This should be done a better way.
// We try to round all parameters to integer boundaries in device space. If rounding pixels in device space
// makes our thickness more than double, then there must be a shrinking-scale factor and rounding to pixels
// in device space will make the underlines too thick.
CGRect lineRect = roundToDevicePixels(FloatRect(x, y, lineLength, adjustedThickness));
if (lineRect.size.height < thickness * 2.0) {
x = lineRect.origin.x;
y = lineRect.origin.y;
lineLength = lineRect.size.width;
thickness = lineRect.size.height;
if (shouldAntialias()) {
CGContextSetShouldAntialias(platformContext(), false);
restoreAntialiasMode = true;
}
}
}
if (fillColor() != strokeColor())
setCGFillColor(platformContext(), strokeColor(), strokeColorSpace());
CGContextFillRect(platformContext(), CGRectMake(x, y, lineLength, thickness));
if (fillColor() != strokeColor())
setCGFillColor(platformContext(), fillColor(), fillColorSpace());
if (restoreAntialiasMode)
CGContextSetShouldAntialias(platformContext(), true);
}
void GraphicsContext::setURLForRect(const KURL& link, const IntRect& destRect)
{
if (paintingDisabled())
return;
RetainPtr<CFURLRef> urlRef(AdoptCF, link.createCFURL());
if (!urlRef)
return;
CGContextRef context = platformContext();
// Get the bounding box to handle clipping.
CGRect box = CGContextGetClipBoundingBox(context);
IntRect intBox((int)box.origin.x, (int)box.origin.y, (int)box.size.width, (int)box.size.height);
IntRect rect = destRect;
rect.intersect(intBox);
CGPDFContextSetURLForRect(context, urlRef.get(),
CGRectApplyAffineTransform(rect, CGContextGetCTM(context)));
}
void GraphicsContext::setImageInterpolationQuality(InterpolationQuality mode)
{
if (paintingDisabled())
return;
CGInterpolationQuality quality = kCGInterpolationDefault;
switch (mode) {
case InterpolationDefault:
quality = kCGInterpolationDefault;
break;
case InterpolationNone:
quality = kCGInterpolationNone;
break;
case InterpolationLow:
quality = kCGInterpolationLow;
break;
// Fall through to InterpolationHigh if kCGInterpolationMedium is not usable.
case InterpolationMedium:
#if USE(CG_INTERPOLATION_MEDIUM)
quality = kCGInterpolationMedium;
break;
#endif
case InterpolationHigh:
quality = kCGInterpolationHigh;
break;
}
CGContextSetInterpolationQuality(platformContext(), quality);
}
InterpolationQuality GraphicsContext::imageInterpolationQuality() const
{
if (paintingDisabled())
return InterpolationDefault;
CGInterpolationQuality quality = CGContextGetInterpolationQuality(platformContext());
switch (quality) {
case kCGInterpolationDefault:
return InterpolationDefault;
case kCGInterpolationNone:
return InterpolationNone;
case kCGInterpolationLow:
return InterpolationLow;
#if HAVE(CG_INTERPOLATION_MEDIUM)
// kCGInterpolationMedium is known to be present in the CGInterpolationQuality enum.
case kCGInterpolationMedium:
#if USE(CG_INTERPOLATION_MEDIUM)
// Only map to InterpolationMedium if targeting a system that understands it.
return InterpolationMedium;
#else
return InterpolationDefault;
#endif // USE(CG_INTERPOLATION_MEDIUM)
#endif // HAVE(CG_INTERPOLATION_MEDIUM)
case kCGInterpolationHigh:
return InterpolationHigh;
}
return InterpolationDefault;
}
void GraphicsContext::setAllowsFontSmoothing(bool allowsFontSmoothing)
{
UNUSED_PARAM(allowsFontSmoothing);
#if !defined(BUILDING_ON_TIGER) && !defined(BUILDING_ON_LEOPARD)
CGContextRef context = platformContext();
CGContextSetAllowsFontSmoothing(context, allowsFontSmoothing);
#endif
}
void GraphicsContext::setIsCALayerContext(bool)
{
m_data->m_isCALayerContext = true;
}
bool GraphicsContext::isCALayerContext() const
{
return m_data->m_isCALayerContext;
}
void GraphicsContext::setPlatformTextDrawingMode(TextDrawingModeFlags mode)
{
if (paintingDisabled())
return;
// Wow, wish CG had used bits here.
CGContextRef context = platformContext();
switch (mode) {
case TextModeInvisible:
CGContextSetTextDrawingMode(context, kCGTextInvisible);
break;
case TextModeFill:
CGContextSetTextDrawingMode(context, kCGTextFill);
break;
case TextModeStroke:
CGContextSetTextDrawingMode(context, kCGTextStroke);
break;
case TextModeFill | TextModeStroke:
CGContextSetTextDrawingMode(context, kCGTextFillStroke);
break;
case TextModeClip:
CGContextSetTextDrawingMode(context, kCGTextClip);
break;
case TextModeFill | TextModeClip:
CGContextSetTextDrawingMode(context, kCGTextFillClip);
break;
case TextModeStroke | TextModeClip:
CGContextSetTextDrawingMode(context, kCGTextStrokeClip);
break;
case TextModeFill | TextModeStroke | TextModeClip:
CGContextSetTextDrawingMode(context, kCGTextFillStrokeClip);
break;
default:
break;
}
}
void GraphicsContext::setPlatformStrokeColor(const Color& color, ColorSpace colorSpace)
{
if (paintingDisabled())
return;
setCGStrokeColor(platformContext(), color, colorSpace);
}
void GraphicsContext::setPlatformStrokeThickness(float thickness)
{
if (paintingDisabled())
return;
CGContextSetLineWidth(platformContext(), thickness);
}
void GraphicsContext::setPlatformFillColor(const Color& color, ColorSpace colorSpace)
{
if (paintingDisabled())
return;
setCGFillColor(platformContext(), color, colorSpace);
}
void GraphicsContext::setPlatformShouldAntialias(bool enable)
{
if (paintingDisabled())
return;
CGContextSetShouldAntialias(platformContext(), enable);
}
void GraphicsContext::setPlatformShouldSmoothFonts(bool enable)
{
if (paintingDisabled())
return;
CGContextSetShouldSmoothFonts(platformContext(), enable);
}
#ifndef BUILDING_ON_TIGER // Tiger's setPlatformCompositeOperation() is defined in GraphicsContextMac.mm.
void GraphicsContext::setPlatformCompositeOperation(CompositeOperator mode)
{
if (paintingDisabled())
return;
CGBlendMode target = kCGBlendModeNormal;
switch (mode) {
case CompositeClear:
target = kCGBlendModeClear;
break;
case CompositeCopy:
target = kCGBlendModeCopy;
break;
case CompositeSourceOver:
//kCGBlendModeNormal
break;
case CompositeSourceIn:
target = kCGBlendModeSourceIn;
break;
case CompositeSourceOut:
target = kCGBlendModeSourceOut;
break;
case CompositeSourceAtop:
target = kCGBlendModeSourceAtop;
break;
case CompositeDestinationOver:
target = kCGBlendModeDestinationOver;
break;
case CompositeDestinationIn:
target = kCGBlendModeDestinationIn;
break;
case CompositeDestinationOut:
target = kCGBlendModeDestinationOut;
break;
case CompositeDestinationAtop:
target = kCGBlendModeDestinationAtop;
break;
case CompositeXOR:
target = kCGBlendModeXOR;
break;
case CompositePlusDarker:
target = kCGBlendModePlusDarker;
break;
case CompositeHighlight:
// currently unsupported
break;
case CompositePlusLighter:
target = kCGBlendModePlusLighter;
break;
}
CGContextSetBlendMode(platformContext(), target);
}
#endif
}
|