-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcairo-quartz-surface.c
2570 lines (2139 loc) · 77.3 KB
/
cairo-quartz-surface.c
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
/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
/* cairo - a vector graphics library with display and print output
*
* Copyright � 2006, 2007 Mozilla Corporation
*
* This library is free software; you can redistribute it and/or
* modify it either under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation
* (the "LGPL") or, at your option, under the terms of the Mozilla
* Public License Version 1.1 (the "MPL"). If you do not alter this
* notice, a recipient may use your version of this file under either
* the MPL or the LGPL.
*
* You should have received a copy of the LGPL along with this library
* in the file COPYING-LGPL-2.1; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
* You should have received a copy of the MPL along with this library
* in the file COPYING-MPL-1.1
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
* OF ANY KIND, either express or implied. See the LGPL or the MPL for
* the specific language governing rights and limitations.
*
* The Original Code is the cairo graphics library.
*
* The Initial Developer of the Original Code is Mozilla Foundation.
*
* Contributor(s):
* Vladimir Vukicevic <[email protected]>
*/
#define _GNU_SOURCE /* required for RTLD_DEFAULT */
#include "cairoint.h"
#include "cairo-quartz-private.h"
#include "cairo-composite-rectangles-private.h"
#include "cairo-compositor-private.h"
#include "cairo-default-context-private.h"
#include "cairo-error-private.h"
#include "cairo-image-surface-inline.h"
#include "cairo-pattern-private.h"
#include "cairo-surface-backend-private.h"
#include "cairo-surface-clipper-private.h"
#include "cairo-recording-surface-private.h"
#include <dlfcn.h>
#ifndef RTLD_DEFAULT
#define RTLD_DEFAULT ((void *) 0)
#endif
#include <limits.h>
#undef QUARTZ_DEBUG
#ifdef QUARTZ_DEBUG
#define ND(_x) fprintf _x
#else
#define ND(_x) do {} while(0)
#endif
#define IS_EMPTY(s) ((s)->extents.width == 0 || (s)->extents.height == 0)
/**
* SECTION:cairo-quartz
* @Title: Quartz Surfaces
* @Short_Description: Rendering to Quartz surfaces
* @See_Also: #cairo_surface_t
*
* The Quartz surface is used to render cairo graphics targeting the
* Apple OS X Quartz rendering system.
**/
/**
* CAIRO_HAS_QUARTZ_SURFACE:
*
* Defined if the Quartz surface backend is available.
* This macro can be used to conditionally compile backend-specific code.
*
* Since: 1.6
**/
#if __MAC_OS_X_VERSION_MIN_REQUIRED < 1050
/* This method is private, but it exists. Its params are are exposed
* as args to the NS* method, but not as CG.
*/
enum PrivateCGCompositeMode {
kPrivateCGCompositeClear = 0,
kPrivateCGCompositeCopy = 1,
kPrivateCGCompositeSourceOver = 2,
kPrivateCGCompositeSourceIn = 3,
kPrivateCGCompositeSourceOut = 4,
kPrivateCGCompositeSourceAtop = 5,
kPrivateCGCompositeDestinationOver = 6,
kPrivateCGCompositeDestinationIn = 7,
kPrivateCGCompositeDestinationOut = 8,
kPrivateCGCompositeDestinationAtop = 9,
kPrivateCGCompositeXOR = 10,
kPrivateCGCompositePlusDarker = 11, // (max (0, (1-d) + (1-s)))
kPrivateCGCompositePlusLighter = 12, // (min (1, s + d))
};
typedef enum PrivateCGCompositeMode PrivateCGCompositeMode;
CG_EXTERN void CGContextSetCompositeOperation (CGContextRef, PrivateCGCompositeMode);
#endif
/* Some of these are present in earlier versions of the OS than where
* they are public; other are not public at all
*/
/* public since 10.5 */
static void (*CGContextDrawTiledImagePtr) (CGContextRef, CGRect, CGImageRef) = NULL;
/* public since 10.6 */
static CGPathRef (*CGContextCopyPathPtr) (CGContextRef) = NULL;
static void (*CGContextSetAllowsFontSmoothingPtr) (CGContextRef, bool) = NULL;
/* not yet public */
static unsigned int (*CGContextGetTypePtr) (CGContextRef) = NULL;
static bool (*CGContextGetAllowsFontSmoothingPtr) (CGContextRef) = NULL;
static cairo_bool_t _cairo_quartz_symbol_lookup_done = FALSE;
/*
* Utility functions
*/
#ifdef QUARTZ_DEBUG
static void quartz_surface_to_png (cairo_quartz_surface_t *nq, char *dest);
static void quartz_image_to_png (CGImageRef, char *dest);
#endif
static cairo_quartz_surface_t *
_cairo_quartz_surface_create_internal (CGContextRef cgContext,
cairo_content_t content,
unsigned int width,
unsigned int height);
static cairo_bool_t
_cairo_surface_is_quartz (const cairo_surface_t *surface);
/* Load all extra symbols */
static void quartz_ensure_symbols (void)
{
if (likely (_cairo_quartz_symbol_lookup_done))
return;
CGContextDrawTiledImagePtr = dlsym (RTLD_DEFAULT, "CGContextDrawTiledImage");
CGContextGetTypePtr = dlsym (RTLD_DEFAULT, "CGContextGetType");
CGContextCopyPathPtr = dlsym (RTLD_DEFAULT, "CGContextCopyPath");
CGContextGetAllowsFontSmoothingPtr = dlsym (RTLD_DEFAULT, "CGContextGetAllowsFontSmoothing");
CGContextSetAllowsFontSmoothingPtr = dlsym (RTLD_DEFAULT, "CGContextSetAllowsFontSmoothing");
_cairo_quartz_symbol_lookup_done = TRUE;
}
CGImageRef
CairoQuartzCreateCGImage (cairo_format_t format,
unsigned int width,
unsigned int height,
unsigned int stride,
void *data,
cairo_bool_t interpolate,
CGColorSpaceRef colorSpaceOverride,
CGDataProviderReleaseDataCallback releaseCallback,
void *releaseInfo)
{
CGImageRef image = NULL;
CGDataProviderRef dataProvider = NULL;
CGColorSpaceRef colorSpace = colorSpaceOverride;
CGBitmapInfo bitinfo = kCGBitmapByteOrder32Host;
int bitsPerComponent, bitsPerPixel;
switch (format) {
case CAIRO_FORMAT_ARGB32:
if (colorSpace == NULL)
colorSpace = CGColorSpaceCreateDeviceRGB ();
bitinfo |= kCGImageAlphaPremultipliedFirst;
bitsPerComponent = 8;
bitsPerPixel = 32;
break;
case CAIRO_FORMAT_RGB24:
if (colorSpace == NULL)
colorSpace = CGColorSpaceCreateDeviceRGB ();
bitinfo |= kCGImageAlphaNoneSkipFirst;
bitsPerComponent = 8;
bitsPerPixel = 32;
break;
case CAIRO_FORMAT_A8:
bitsPerComponent = 8;
bitsPerPixel = 8;
break;
case CAIRO_FORMAT_A1:
#ifdef WORDS_BIGENDIAN
bitsPerComponent = 1;
bitsPerPixel = 1;
break;
#endif
case CAIRO_FORMAT_RGB30:
case CAIRO_FORMAT_RGB16_565:
case CAIRO_FORMAT_INVALID:
default:
return NULL;
}
dataProvider = CGDataProviderCreateWithData (releaseInfo,
data,
height * stride,
releaseCallback);
if (unlikely (!dataProvider)) {
// manually release
if (releaseCallback)
releaseCallback (releaseInfo, data, height * stride);
goto FINISH;
}
if (format == CAIRO_FORMAT_A8 || format == CAIRO_FORMAT_A1) {
cairo_quartz_float_t decode[] = {1.0, 0.0};
image = CGImageMaskCreate (width, height,
bitsPerComponent,
bitsPerPixel,
stride,
dataProvider,
decode,
interpolate);
} else
image = CGImageCreate (width, height,
bitsPerComponent,
bitsPerPixel,
stride,
colorSpace,
bitinfo,
dataProvider,
NULL,
interpolate,
kCGRenderingIntentDefault);
FINISH:
CGDataProviderRelease (dataProvider);
if (colorSpace != colorSpaceOverride)
CGColorSpaceRelease (colorSpace);
return image;
}
static inline cairo_bool_t
_cairo_quartz_is_cgcontext_bitmap_context (CGContextRef cgc)
{
if (unlikely (cgc == NULL))
return FALSE;
if (likely (CGContextGetTypePtr)) {
/* 4 is the type value of a bitmap context */
return CGContextGetTypePtr (cgc) == 4;
}
/* This will cause a (harmless) warning to be printed if called on a non-bitmap context */
return CGBitmapContextGetBitsPerPixel (cgc) != 0;
}
/* CoreGraphics limitation with flipped CTM surfaces: height must be less than signed 16-bit max */
#define CG_MAX_HEIGHT SHRT_MAX
#define CG_MAX_WIDTH USHRT_MAX
/* is the desired size of the surface within bounds? */
cairo_bool_t
_cairo_quartz_verify_surface_size (int width, int height)
{
/* hmmm, allow width, height == 0 ? */
if (width < 0 || height < 0)
return FALSE;
if (width > CG_MAX_WIDTH || height > CG_MAX_HEIGHT)
return FALSE;
return TRUE;
}
/*
* Cairo path -> Quartz path conversion helpers
*/
/* cairo path -> execute in context */
static cairo_status_t
_cairo_path_to_quartz_context_move_to (void *closure,
const cairo_point_t *point)
{
//ND ((stderr, "moveto: %f %f\n", _cairo_fixed_to_double (point->x), _cairo_fixed_to_double (point->y)));
double x = _cairo_fixed_to_double (point->x);
double y = _cairo_fixed_to_double (point->y);
CGContextMoveToPoint (closure, x, y);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_path_to_quartz_context_line_to (void *closure,
const cairo_point_t *point)
{
//ND ((stderr, "lineto: %f %f\n", _cairo_fixed_to_double (point->x), _cairo_fixed_to_double (point->y)));
double x = _cairo_fixed_to_double (point->x);
double y = _cairo_fixed_to_double (point->y);
CGContextAddLineToPoint (closure, x, y);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_path_to_quartz_context_curve_to (void *closure,
const cairo_point_t *p0,
const cairo_point_t *p1,
const cairo_point_t *p2)
{
//ND ((stderr, "curveto: %f,%f %f,%f %f,%f\n",
// _cairo_fixed_to_double (p0->x), _cairo_fixed_to_double (p0->y),
// _cairo_fixed_to_double (p1->x), _cairo_fixed_to_double (p1->y),
// _cairo_fixed_to_double (p2->x), _cairo_fixed_to_double (p2->y)));
double x0 = _cairo_fixed_to_double (p0->x);
double y0 = _cairo_fixed_to_double (p0->y);
double x1 = _cairo_fixed_to_double (p1->x);
double y1 = _cairo_fixed_to_double (p1->y);
double x2 = _cairo_fixed_to_double (p2->x);
double y2 = _cairo_fixed_to_double (p2->y);
CGContextAddCurveToPoint (closure, x0, y0, x1, y1, x2, y2);
return CAIRO_STATUS_SUCCESS;
}
static cairo_status_t
_cairo_path_to_quartz_context_close_path (void *closure)
{
//ND ((stderr, "closepath\n"));
CGContextClosePath (closure);
return CAIRO_STATUS_SUCCESS;
}
static void
_cairo_quartz_cairo_path_to_quartz_context (const cairo_path_fixed_t *path,
CGContextRef closure)
{
cairo_status_t status;
CGContextBeginPath (closure);
status = _cairo_path_fixed_interpret (path,
_cairo_path_to_quartz_context_move_to,
_cairo_path_to_quartz_context_line_to,
_cairo_path_to_quartz_context_curve_to,
_cairo_path_to_quartz_context_close_path,
closure);
assert (status == CAIRO_STATUS_SUCCESS);
}
/*
* Misc helpers/callbacks
*/
#if __MAC_OS_X_VERSION_MIN_REQUIRED < 1050
static PrivateCGCompositeMode
_cairo_quartz_cairo_operator_to_quartz_composite (cairo_operator_t op)
{
switch (op) {
case CAIRO_OPERATOR_CLEAR:
return kPrivateCGCompositeClear;
case CAIRO_OPERATOR_SOURCE:
return kPrivateCGCompositeCopy;
case CAIRO_OPERATOR_OVER:
return kPrivateCGCompositeSourceOver;
case CAIRO_OPERATOR_IN:
return kPrivateCGCompositeSourceIn;
case CAIRO_OPERATOR_OUT:
return kPrivateCGCompositeSourceOut;
case CAIRO_OPERATOR_ATOP:
return kPrivateCGCompositeSourceAtop;
case CAIRO_OPERATOR_DEST_OVER:
return kPrivateCGCompositeDestinationOver;
case CAIRO_OPERATOR_DEST_IN:
return kPrivateCGCompositeDestinationIn;
case CAIRO_OPERATOR_DEST_OUT:
return kPrivateCGCompositeDestinationOut;
case CAIRO_OPERATOR_DEST_ATOP:
return kPrivateCGCompositeDestinationAtop;
case CAIRO_OPERATOR_XOR:
return kPrivateCGCompositeXOR;
case CAIRO_OPERATOR_ADD:
return kPrivateCGCompositePlusLighter;
case CAIRO_OPERATOR_DEST:
case CAIRO_OPERATOR_SATURATE:
case CAIRO_OPERATOR_MULTIPLY:
case CAIRO_OPERATOR_SCREEN:
case CAIRO_OPERATOR_OVERLAY:
case CAIRO_OPERATOR_DARKEN:
case CAIRO_OPERATOR_LIGHTEN:
case CAIRO_OPERATOR_COLOR_DODGE:
case CAIRO_OPERATOR_COLOR_BURN:
case CAIRO_OPERATOR_HARD_LIGHT:
case CAIRO_OPERATOR_SOFT_LIGHT:
case CAIRO_OPERATOR_DIFFERENCE:
case CAIRO_OPERATOR_EXCLUSION:
case CAIRO_OPERATOR_HSL_HUE:
case CAIRO_OPERATOR_HSL_SATURATION:
case CAIRO_OPERATOR_HSL_COLOR:
case CAIRO_OPERATOR_HSL_LUMINOSITY:
default:
ASSERT_NOT_REACHED;
}
}
#endif
static CGBlendMode
_cairo_quartz_cairo_operator_to_quartz_blend (cairo_operator_t op)
{
switch (op) {
case CAIRO_OPERATOR_MULTIPLY:
return kCGBlendModeMultiply;
case CAIRO_OPERATOR_SCREEN:
return kCGBlendModeScreen;
case CAIRO_OPERATOR_OVERLAY:
return kCGBlendModeOverlay;
case CAIRO_OPERATOR_DARKEN:
return kCGBlendModeDarken;
case CAIRO_OPERATOR_LIGHTEN:
return kCGBlendModeLighten;
case CAIRO_OPERATOR_COLOR_DODGE:
return kCGBlendModeColorDodge;
case CAIRO_OPERATOR_COLOR_BURN:
return kCGBlendModeColorBurn;
case CAIRO_OPERATOR_HARD_LIGHT:
return kCGBlendModeHardLight;
case CAIRO_OPERATOR_SOFT_LIGHT:
return kCGBlendModeSoftLight;
case CAIRO_OPERATOR_DIFFERENCE:
return kCGBlendModeDifference;
case CAIRO_OPERATOR_EXCLUSION:
return kCGBlendModeExclusion;
case CAIRO_OPERATOR_HSL_HUE:
return kCGBlendModeHue;
case CAIRO_OPERATOR_HSL_SATURATION:
return kCGBlendModeSaturation;
case CAIRO_OPERATOR_HSL_COLOR:
return kCGBlendModeColor;
case CAIRO_OPERATOR_HSL_LUMINOSITY:
return kCGBlendModeLuminosity;
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1050
case CAIRO_OPERATOR_CLEAR:
return kCGBlendModeClear;
case CAIRO_OPERATOR_SOURCE:
return kCGBlendModeCopy;
case CAIRO_OPERATOR_OVER:
return kCGBlendModeNormal;
case CAIRO_OPERATOR_IN:
return kCGBlendModeSourceIn;
case CAIRO_OPERATOR_OUT:
return kCGBlendModeSourceOut;
case CAIRO_OPERATOR_ATOP:
return kCGBlendModeSourceAtop;
case CAIRO_OPERATOR_DEST_OVER:
return kCGBlendModeDestinationOver;
case CAIRO_OPERATOR_DEST_IN:
return kCGBlendModeDestinationIn;
case CAIRO_OPERATOR_DEST_OUT:
return kCGBlendModeDestinationOut;
case CAIRO_OPERATOR_DEST_ATOP:
return kCGBlendModeDestinationAtop;
case CAIRO_OPERATOR_XOR:
return kCGBlendModeXOR;
case CAIRO_OPERATOR_ADD:
return kCGBlendModePlusLighter;
#else
case CAIRO_OPERATOR_CLEAR:
case CAIRO_OPERATOR_SOURCE:
case CAIRO_OPERATOR_OVER:
case CAIRO_OPERATOR_IN:
case CAIRO_OPERATOR_OUT:
case CAIRO_OPERATOR_ATOP:
case CAIRO_OPERATOR_DEST_OVER:
case CAIRO_OPERATOR_DEST_IN:
case CAIRO_OPERATOR_DEST_OUT:
case CAIRO_OPERATOR_DEST_ATOP:
case CAIRO_OPERATOR_XOR:
case CAIRO_OPERATOR_ADD:
#endif
case CAIRO_OPERATOR_DEST:
case CAIRO_OPERATOR_SATURATE:
default:
ASSERT_NOT_REACHED;
}
}
static cairo_int_status_t
_cairo_cgcontext_set_cairo_operator (CGContextRef context, cairo_operator_t op)
{
CGBlendMode blendmode;
assert (op != CAIRO_OPERATOR_DEST);
/* Quartz doesn't support SATURATE at all. COLOR_DODGE and
* COLOR_BURN in Quartz follow the ISO32000 definition, but cairo
* uses the definition from the Adobe Supplement.
*/
if (op == CAIRO_OPERATOR_SATURATE ||
op == CAIRO_OPERATOR_COLOR_DODGE ||
op == CAIRO_OPERATOR_COLOR_BURN)
{
return CAIRO_INT_STATUS_UNSUPPORTED;
}
#if __MAC_OS_X_VERSION_MIN_REQUIRED < 1050
if (op <= CAIRO_OPERATOR_ADD) {
PrivateCGCompositeMode compmode;
compmode = _cairo_quartz_cairo_operator_to_quartz_composite (op);
CGContextSetCompositeOperation (context, compmode);
return CAIRO_STATUS_SUCCESS;
}
#endif
blendmode = _cairo_quartz_cairo_operator_to_quartz_blend (op);
CGContextSetBlendMode (context, blendmode);
return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
_cairo_quartz_surface_set_cairo_operator (cairo_quartz_surface_t *surface, cairo_operator_t op)
{
ND((stderr, "%p _cairo_quartz_surface_set_cairo_operator %d\n", surface, op));
/* When the destination has no color components, we can avoid some
* fallbacks, but we have to workaround operators which behave
* differently in Quartz. */
if (surface->base.content == CAIRO_CONTENT_ALPHA) {
assert (op != CAIRO_OPERATOR_ATOP); /* filtered by surface layer */
if (op == CAIRO_OPERATOR_SOURCE ||
op == CAIRO_OPERATOR_IN ||
op == CAIRO_OPERATOR_OUT ||
op == CAIRO_OPERATOR_DEST_IN ||
op == CAIRO_OPERATOR_DEST_ATOP ||
op == CAIRO_OPERATOR_XOR)
{
return CAIRO_INT_STATUS_UNSUPPORTED;
}
if (op == CAIRO_OPERATOR_DEST_OVER)
op = CAIRO_OPERATOR_OVER;
else if (op == CAIRO_OPERATOR_SATURATE)
op = CAIRO_OPERATOR_ADD;
else if (op == CAIRO_OPERATOR_COLOR_DODGE)
op = CAIRO_OPERATOR_OVER;
else if (op == CAIRO_OPERATOR_COLOR_BURN)
op = CAIRO_OPERATOR_OVER;
}
return _cairo_cgcontext_set_cairo_operator (surface->cgContext, op);
}
static inline CGLineCap
_cairo_quartz_cairo_line_cap_to_quartz (cairo_line_cap_t ccap)
{
switch (ccap) {
default:
ASSERT_NOT_REACHED;
case CAIRO_LINE_CAP_BUTT:
return kCGLineCapButt;
case CAIRO_LINE_CAP_ROUND:
return kCGLineCapRound;
case CAIRO_LINE_CAP_SQUARE:
return kCGLineCapSquare;
}
}
static inline CGLineJoin
_cairo_quartz_cairo_line_join_to_quartz (cairo_line_join_t cjoin)
{
switch (cjoin) {
default:
ASSERT_NOT_REACHED;
case CAIRO_LINE_JOIN_MITER:
return kCGLineJoinMiter;
case CAIRO_LINE_JOIN_ROUND:
return kCGLineJoinRound;
case CAIRO_LINE_JOIN_BEVEL:
return kCGLineJoinBevel;
}
}
static inline CGInterpolationQuality
_cairo_quartz_filter_to_quartz (cairo_filter_t filter)
{
switch (filter) {
case CAIRO_FILTER_NEAREST:
case CAIRO_FILTER_FAST:
return kCGInterpolationNone;
case CAIRO_FILTER_BEST:
case CAIRO_FILTER_GOOD:
case CAIRO_FILTER_BILINEAR:
case CAIRO_FILTER_GAUSSIAN:
return kCGInterpolationDefault;
default:
ASSERT_NOT_REACHED;
return kCGInterpolationDefault;
}
}
static inline void
_cairo_quartz_cairo_matrix_to_quartz (const cairo_matrix_t *src,
CGAffineTransform *dst)
{
dst->a = src->xx;
dst->b = src->yx;
dst->c = src->xy;
dst->d = src->yy;
dst->tx = src->x0;
dst->ty = src->y0;
}
/*
* Source -> Quartz setup and finish functions
*/
static void
ComputeGradientValue (void *info,
const cairo_quartz_float_t *in,
cairo_quartz_float_t *out)
{
double fdist = *in;
const cairo_gradient_pattern_t *grad = (cairo_gradient_pattern_t*) info;
unsigned int i;
/* Put fdist back in the 0.0..1.0 range if we're doing
* REPEAT/REFLECT
*/
if (grad->base.extend == CAIRO_EXTEND_REPEAT) {
fdist = fdist - floor (fdist);
} else if (grad->base.extend == CAIRO_EXTEND_REFLECT) {
fdist = fmod (fabs (fdist), 2.0);
if (fdist > 1.0)
fdist = 2.0 - fdist;
}
for (i = 0; i < grad->n_stops; i++)
if (grad->stops[i].offset > fdist)
break;
if (i == 0 || i == grad->n_stops) {
if (i == grad->n_stops)
--i;
out[0] = grad->stops[i].color.red;
out[1] = grad->stops[i].color.green;
out[2] = grad->stops[i].color.blue;
out[3] = grad->stops[i].color.alpha;
} else {
cairo_quartz_float_t ax = grad->stops[i-1].offset;
cairo_quartz_float_t bx = grad->stops[i].offset - ax;
cairo_quartz_float_t bp = (fdist - ax)/bx;
cairo_quartz_float_t ap = 1.0 - bp;
out[0] =
grad->stops[i-1].color.red * ap +
grad->stops[i].color.red * bp;
out[1] =
grad->stops[i-1].color.green * ap +
grad->stops[i].color.green * bp;
out[2] =
grad->stops[i-1].color.blue * ap +
grad->stops[i].color.blue * bp;
out[3] =
grad->stops[i-1].color.alpha * ap +
grad->stops[i].color.alpha * bp;
}
}
static const cairo_quartz_float_t gradient_output_value_ranges[8] = {
0.f, 1.f, 0.f, 1.f, 0.f, 1.f, 0.f, 1.f
};
static const CGFunctionCallbacks gradient_callbacks = {
0, ComputeGradientValue, (CGFunctionReleaseInfoCallback) cairo_pattern_destroy
};
/* Quartz computes a small number of samples of the gradient color
* function. On MacOS X 10.5 it apparently computes only 1024
* samples. */
#define MAX_GRADIENT_RANGE 1024
static CGFunctionRef
CairoQuartzCreateGradientFunction (const cairo_gradient_pattern_t *gradient,
const cairo_rectangle_int_t *extents,
cairo_circle_double_t *start,
cairo_circle_double_t *end)
{
cairo_pattern_t *pat;
cairo_quartz_float_t input_value_range[2];
if (gradient->base.extend != CAIRO_EXTEND_NONE) {
double bounds_x1, bounds_x2, bounds_y1, bounds_y2;
double t[2], tolerance;
tolerance = fabs (_cairo_matrix_compute_determinant (&gradient->base.matrix));
tolerance /= _cairo_matrix_transformed_circle_major_axis (&gradient->base.matrix, 1);
bounds_x1 = extents->x;
bounds_y1 = extents->y;
bounds_x2 = extents->x + extents->width;
bounds_y2 = extents->y + extents->height;
_cairo_matrix_transform_bounding_box (&gradient->base.matrix,
&bounds_x1, &bounds_y1,
&bounds_x2, &bounds_y2,
NULL);
_cairo_gradient_pattern_box_to_parameter (gradient,
bounds_x1, bounds_y1,
bounds_x2, bounds_y2,
tolerance,
t);
if (gradient->base.extend == CAIRO_EXTEND_PAD) {
t[0] = MAX (t[0], -0.5);
t[1] = MIN (t[1], 1.5);
} else if (t[1] - t[0] > MAX_GRADIENT_RANGE)
return NULL;
/* set the input range for the function -- the function knows how
to map values outside of 0.0 .. 1.0 to the correct color */
input_value_range[0] = t[0];
input_value_range[1] = t[1];
} else {
input_value_range[0] = 0;
input_value_range[1] = 1;
}
_cairo_gradient_pattern_interpolate (gradient, input_value_range[0], start);
_cairo_gradient_pattern_interpolate (gradient, input_value_range[1], end);
if (_cairo_pattern_create_copy (&pat, &gradient->base))
return NULL;
return CGFunctionCreate (pat,
1,
input_value_range,
4,
gradient_output_value_ranges,
&gradient_callbacks);
}
/* Obtain a CGImageRef from a #cairo_surface_t * */
typedef struct {
cairo_surface_t *surface;
cairo_image_surface_t *image_out;
void *image_extra;
} quartz_source_image_t;
static void
DataProviderReleaseCallback (void *info, const void *data, size_t size)
{
quartz_source_image_t *source_img = info;
_cairo_surface_release_source_image (source_img->surface, source_img->image_out, source_img->image_extra);
free (source_img);
}
static cairo_status_t
_cairo_surface_to_cgimage (cairo_surface_t *source,
cairo_rectangle_int_t *extents,
cairo_format_t format,
cairo_matrix_t *matrix,
const cairo_clip_t *clip,
CGImageRef *image_out)
{
cairo_status_t status;
quartz_source_image_t *source_img;
cairo_image_surface_t *image_surface;
if (source->backend && source->backend->type == CAIRO_SURFACE_TYPE_QUARTZ_IMAGE) {
cairo_quartz_image_surface_t *surface = (cairo_quartz_image_surface_t *) source;
*image_out = CGImageRetain (surface->image);
return CAIRO_STATUS_SUCCESS;
}
if (_cairo_surface_is_quartz (source)) {
cairo_quartz_surface_t *surface = (cairo_quartz_surface_t *) source;
if (IS_EMPTY (surface)) {
*image_out = NULL;
return CAIRO_INT_STATUS_NOTHING_TO_DO;
}
if (_cairo_quartz_is_cgcontext_bitmap_context (surface->cgContext)) {
*image_out = CGBitmapContextCreateImage (surface->cgContext);
if (*image_out)
return CAIRO_STATUS_SUCCESS;
}
}
source_img = malloc (sizeof (quartz_source_image_t));
if (unlikely (source_img == NULL))
return _cairo_error (CAIRO_STATUS_NO_MEMORY);
source_img->surface = source;
if (source->type == CAIRO_SURFACE_TYPE_RECORDING) {
image_surface = (cairo_image_surface_t *)
cairo_image_surface_create (format, extents->width, extents->height);
if (unlikely (image_surface->base.status)) {
status = image_surface->base.status;
cairo_surface_destroy (&image_surface->base);
free (source_img);
return status;
}
status = _cairo_recording_surface_replay_with_clip (source,
matrix,
&image_surface->base,
NULL);
if (unlikely (status)) {
cairo_surface_destroy (&image_surface->base);
free (source_img);
return status;
}
source_img->image_out = image_surface;
source_img->image_extra = NULL;
cairo_matrix_init_identity (matrix);
}
else {
status = _cairo_surface_acquire_source_image (source_img->surface,
&source_img->image_out,
&source_img->image_extra);
if (unlikely (status)) {
free (source_img);
return status;
}
}
if (source_img->image_out->width == 0 || source_img->image_out->height == 0) {
*image_out = NULL;
DataProviderReleaseCallback (source_img,
source_img->image_out->data,
source_img->image_out->height * source_img->image_out->stride);
} else {
*image_out = CairoQuartzCreateCGImage (source_img->image_out->format,
source_img->image_out->width,
source_img->image_out->height,
source_img->image_out->stride,
source_img->image_out->data,
TRUE,
NULL,
DataProviderReleaseCallback,
source_img);
/* TODO: differentiate memory error and unsupported surface type */
if (unlikely (*image_out == NULL))
status = CAIRO_INT_STATUS_UNSUPPORTED;
}
return status;
}
/* Generic #cairo_pattern_t -> CGPattern function */
typedef struct {
CGImageRef image;
CGRect imageBounds;
cairo_bool_t do_reflect;
} SurfacePatternDrawInfo;
static void
SurfacePatternDrawFunc (void *ainfo, CGContextRef context)
{
SurfacePatternDrawInfo *info = (SurfacePatternDrawInfo*) ainfo;
CGContextTranslateCTM (context, 0, info->imageBounds.size.height);
CGContextScaleCTM (context, 1, -1);
CGContextDrawImage (context, info->imageBounds, info->image);
if (info->do_reflect) {
/* draw 3 more copies of the image, flipped.
* DrawImage draws the image according to the current Y-direction into the rectangle given
* (imageBounds); at the time of the first DrawImage above, the origin is at the bottom left
* of the base image position, and the Y axis is extending upwards.
*/
/* Make the y axis extend downwards, and draw a flipped image below */
CGContextScaleCTM (context, 1, -1);
CGContextDrawImage (context, info->imageBounds, info->image);
/* Shift over to the right, and flip vertically (translation is 2x,
* since we'll be flipping and thus rendering the rectangle "backwards"
*/
CGContextTranslateCTM (context, 2 * info->imageBounds.size.width, 0);
CGContextScaleCTM (context, -1, 1);
CGContextDrawImage (context, info->imageBounds, info->image);
/* Then unflip the Y-axis again, and draw the image above the point. */
CGContextScaleCTM (context, 1, -1);
CGContextDrawImage (context, info->imageBounds, info->image);
}
}
static void
SurfacePatternReleaseInfoFunc (void *ainfo)
{
SurfacePatternDrawInfo *info = (SurfacePatternDrawInfo*) ainfo;
CGImageRelease (info->image);
free (info);
}
static cairo_int_status_t
_cairo_quartz_cairo_repeating_surface_pattern_to_quartz (cairo_quartz_surface_t *dest,
const cairo_pattern_t *apattern,
const cairo_clip_t *clip,
CGPatternRef *cgpat)
{
cairo_surface_pattern_t *spattern;
cairo_surface_t *pat_surf;
cairo_rectangle_int_t extents;
cairo_format_t format = _cairo_format_from_content (dest->base.content);
CGImageRef image;
CGRect pbounds;
CGAffineTransform ptransform, stransform;
CGPatternCallbacks cb = { 0,
SurfacePatternDrawFunc,
SurfacePatternReleaseInfoFunc };
SurfacePatternDrawInfo *info;
cairo_quartz_float_t rw, rh;
cairo_status_t status;
cairo_bool_t is_bounded;
cairo_matrix_t m;
/* SURFACE is the only type we'll handle here */
assert (apattern->type == CAIRO_PATTERN_TYPE_SURFACE);
spattern = (cairo_surface_pattern_t *) apattern;
pat_surf = spattern->surface;
if (pat_surf->type != CAIRO_SURFACE_TYPE_RECORDING) {
is_bounded = _cairo_surface_get_extents (pat_surf, &extents);
assert (is_bounded);
}
else
_cairo_surface_get_extents (&dest->base, &extents);
m = spattern->base.matrix;
status = _cairo_surface_to_cgimage (pat_surf, &extents, format,
&m, clip, &image);
if (unlikely (status))
return status;
info = malloc (sizeof (SurfacePatternDrawInfo));
if (unlikely (!info))
return CAIRO_STATUS_NO_MEMORY;
/* XXX -- if we're printing, we may need to call CGImageCreateCopy to make sure
* that the data will stick around for this image when the printer gets to it.
* Otherwise, the underlying data store may disappear from under us!
*
* _cairo_surface_to_cgimage will copy when it converts non-Quartz surfaces,
* since the Quartz surfaces have a higher chance of sticking around. If the
* source is a quartz image surface, then it's set up to retain a ref to the
* image surface that it's backed by.
*/
info->image = image;
info->imageBounds = CGRectMake (0, 0, extents.width, extents.height);
info->do_reflect = FALSE;
pbounds.origin.x = 0;
pbounds.origin.y = 0;
if (spattern->base.extend == CAIRO_EXTEND_REFLECT) {
pbounds.size.width = 2.0 * extents.width;
pbounds.size.height = 2.0 * extents.height;
info->do_reflect = TRUE;
} else {
pbounds.size.width = extents.width;