forked from djw/core-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPTLayer.m
More file actions
893 lines (739 loc) · 27 KB
/
Copy pathCPTLayer.m
File metadata and controls
893 lines (739 loc) · 27 KB
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
#import "CPTLayer.h"
#import "CPTAxisSet.h"
#import "CPTExceptions.h"
#import "CPTGraph.h"
#import "CPTLineStyle.h"
#import "CPTPathExtensions.h"
#import "CPTPlatformSpecificDefines.h"
#import "CPTPlatformSpecificFunctions.h"
#import "CPTShadow.h"
#import "CPTUtilities.h"
#import "CorePlotProbes.h"
#import "NSCoderExtensions.h"
#import "NSNumberExtensions.h"
#import <objc/runtime.h>
#import <tgmath.h>
/** @defgroup animation Animatable Properties
* @brief Custom layer properties that can be animated using Core Animation.
* @if MacOnly
* @since Custom layer property animation is supported on MacOS 10.6 and later.
* @endif
**/
/** @defgroup notification Notifications
* @brief Notifications used by Core Plot.
**/
/// @cond
@interface CPTLayer()
@property (nonatomic, readwrite, getter = isRenderingRecursively) BOOL renderingRecursively;
@property (nonatomic, readwrite, assign) BOOL useFastRendering;
-(void)applyTransform:(CATransform3D)transform toContext:(CGContextRef)context;
-(NSString *)subLayersAtIndex:(NSUInteger)index;
@end
/// @endcond
#pragma mark -
/** @brief Base class for all Core Animation layers in Core Plot.
*
* Unless <code>@link CPTLayer::useFastRendering useFastRendering @endlink == YES</code>,
* all drawing is done in a way that preserves the
* drawing vectors. Sublayers are arranged automatically to fill the layer's
* bounds, minus any padding. Default animations for changes in position, bounds,
* and sublayers are turned off. The default layer is not opaque and does not mask
* to bounds.
**/
@implementation CPTLayer
/** @property graph
* @brief The graph for the layer.
**/
@synthesize graph;
/** @property paddingLeft
* @brief Amount to inset the left side of each sublayer.
**/
@synthesize paddingLeft;
/** @property paddingTop
* @brief Amount to inset the top of each sublayer.
**/
@synthesize paddingTop;
/** @property paddingRight
* @brief Amount to inset the right side of each sublayer.
**/
@synthesize paddingRight;
/** @property paddingBottom
* @brief Amount to inset the bottom of each sublayer.
**/
@synthesize paddingBottom;
/** @property masksToBorder
* @brief If YES, a sublayer mask is applied to clip sublayer content to the inside of the border.
**/
@synthesize masksToBorder;
/** @property contentsScale
* @brief The scale factor applied to the layer.
**/
@dynamic contentsScale;
/** @property shadow
* @brief The shadow drawn under the layer content. If <code>nil</code> (the default), no shadow is drawn.
**/
@synthesize shadow;
/** @property outerBorderPath
* @brief A drawing path that encompasses the outer boundary of the layer border.
**/
@synthesize outerBorderPath;
/** @property innerBorderPath
* @brief A drawing path that encompasses the inner boundary of the layer border.
**/
@synthesize innerBorderPath;
/** @property maskingPath
* @brief A drawing path that encompasses the layer content including any borders. Set to NULL when no masking is desired.
*
* This path defines the outline of the layer and is used to mask all drawing. Set to NULL when no masking is desired.
* The caller must NOT release the path returned by this property.
**/
@dynamic maskingPath;
/** @property sublayerMaskingPath
* @brief A drawing path that encompasses the layer content excluding any borders. Set to NULL when no masking is desired.
*
* This path defines the outline of the part of the layer where sublayers should draw and is used to mask all sublayer drawing.
* Set to NULL when no masking is desired.
* The caller must NOT release the path returned by this property.
**/
@dynamic sublayerMaskingPath;
/** @property sublayersExcludedFromAutomaticLayout
* @brief A set of sublayers that should be excluded from the automatic sublayer layout.
**/
@dynamic sublayersExcludedFromAutomaticLayout;
/** @property useFastRendering
* @brief If YES, subclasses should optimize their drawing for speed over precision.
**/
@synthesize useFastRendering;
/** @property identifier
* @brief An object used to identify the layer in collections.
**/
@synthesize identifier;
// Private properties
@synthesize renderingRecursively;
#pragma mark -
#pragma mark Init/Dealloc
/** @brief Initializes a newly allocated CPTLayer object with the provided frame rectangle.
*
* This is the designated initializer. The initialized layer will have the following properties:
* - @link CPTLayer::paddingLeft paddingLeft @endlink = 0.0
* - @link CPTLayer::paddingTop paddingTop @endlink = 0.0
* - @link CPTLayer::paddingRight paddingRight @endlink = 0.0
* - @link CPTLayer::paddingBottom paddingBottom @endlink = 0.0
* - @link CPTLayer::masksToBorder masksToBorder @endlink = <code>NO</code>
* - @link CPTLayer::shadow shadow @endlink = <code>nil</code>
* - @link CPTLayer::useFastRendering useFastRendering @endlink = <code>NO</code>
* - @link CPTLayer::graph graph @endlink = <code>nil</code>
* - @link CPTLayer::outerBorderPath outerBorderPath @endlink = <code>NULL</code>
* - @link CPTLayer::innerBorderPath innerBorderPath @endlink = <code>NULL</code>
* - @link CPTLayer::identifier identifier @endlink = <code>nil</code>
* - <code>needsDisplayOnBoundsChange</code> = <code>NO</code>
* - <code>opaque</code> = <code>NO</code>
* - <code>masksToBounds</code> = <code>NO</code>
*
* @param newFrame The frame rectangle.
* @return The initialized CPTLayer object.
**/
-(id)initWithFrame:(CGRect)newFrame
{
if ( (self = [super init]) ) {
paddingLeft = 0.0;
paddingTop = 0.0;
paddingRight = 0.0;
paddingBottom = 0.0;
masksToBorder = NO;
shadow = nil;
renderingRecursively = NO;
useFastRendering = NO;
graph = nil;
outerBorderPath = NULL;
innerBorderPath = NULL;
identifier = nil;
self.frame = newFrame;
self.needsDisplayOnBoundsChange = NO;
self.opaque = NO;
self.masksToBounds = NO;
}
return self;
}
-(id)init
{
return [self initWithFrame:CGRectZero];
}
-(id)initWithLayer:(id)layer
{
if ( (self = [super initWithLayer:layer]) ) {
CPTLayer *theLayer = (CPTLayer *)layer;
paddingLeft = theLayer->paddingLeft;
paddingTop = theLayer->paddingTop;
paddingRight = theLayer->paddingRight;
paddingBottom = theLayer->paddingBottom;
masksToBorder = theLayer->masksToBorder;
shadow = [theLayer->shadow retain];
renderingRecursively = theLayer->renderingRecursively;
graph = theLayer->graph;
outerBorderPath = CGPathRetain(theLayer->outerBorderPath);
innerBorderPath = CGPathRetain(theLayer->innerBorderPath);
identifier = [theLayer->identifier retain];
}
return self;
}
-(void)dealloc
{
graph = nil;
[shadow release];
[identifier release];
CGPathRelease(outerBorderPath);
CGPathRelease(innerBorderPath);
[super dealloc];
}
-(void)finalize
{
CGPathRelease(outerBorderPath);
CGPathRelease(innerBorderPath);
[super finalize];
}
#pragma mark -
#pragma mark NSCoding methods
-(void)encodeWithCoder:(NSCoder *)coder
{
[super encodeWithCoder:coder];
[coder encodeCGFloat:self.paddingLeft forKey:@"CPTLayer.paddingLeft"];
[coder encodeCGFloat:self.paddingTop forKey:@"CPTLayer.paddingTop"];
[coder encodeCGFloat:self.paddingRight forKey:@"CPTLayer.paddingRight"];
[coder encodeCGFloat:self.paddingBottom forKey:@"CPTLayer.paddingBottom"];
[coder encodeBool:self.masksToBorder forKey:@"CPTLayer.masksToBorder"];
[coder encodeObject:self.shadow forKey:@"CPTLayer.shadow"];
[coder encodeConditionalObject:self.graph forKey:@"CPTLayer.graph"];
[coder encodeObject:self.identifier forKey:@"CPTLayer.identifier"];
// No need to archive these properties:
// renderingRecursively
// outerBorderPath
// innerBorderPath
}
-(id)initWithCoder:(NSCoder *)coder
{
if ( (self = [super initWithCoder:coder]) ) {
paddingLeft = [coder decodeCGFloatForKey:@"CPTLayer.paddingLeft"];
paddingTop = [coder decodeCGFloatForKey:@"CPTLayer.paddingTop"];
paddingRight = [coder decodeCGFloatForKey:@"CPTLayer.paddingRight"];
paddingBottom = [coder decodeCGFloatForKey:@"CPTLayer.paddingBottom"];
masksToBorder = [coder decodeBoolForKey:@"CPTLayer.masksToBorder"];
shadow = [[coder decodeObjectForKey:@"CPTLayer.shadow"] copy];
graph = [coder decodeObjectForKey:@"CPTLayer.graph"];
identifier = [[coder decodeObjectForKey:@"CPTLayer.identifier"] copy];
renderingRecursively = NO;
outerBorderPath = NULL;
innerBorderPath = NULL;
}
return self;
}
#pragma mark -
#pragma mark Animation
-(id<CAAction>)actionForKey:(NSString *)aKey
{
return nil;
}
#pragma mark -
#pragma mark Drawing
-(void)drawInContext:(CGContextRef)context
{
self.useFastRendering = YES;
[self renderAsVectorInContext:context];
self.useFastRendering = NO;
}
/** @brief Draws layer content into the provided graphics context.
*
* This method replaces the <code>-[CALayer drawInContext:]</code> method
* to ensure that layer content is always drawn as vectors
* and objects rather than as a cached bitmapped image representation.
* Subclasses should do all drawing here and must call super to set up the clipping path.
*
* @param context The graphics context to draw into.
**/
-(void)renderAsVectorInContext:(CGContextRef)context;
{
// This is where subclasses do their drawing
[self applyMaskToContext:context];
[self.shadow setShadowInContext:context];
}
/** @brief Draws layer content and the content of all sublayers into the provided graphics context.
* @param context The graphics context to draw into.
**/
-(void)recursivelyRenderInContext:(CGContextRef)context
{
// render self
CGContextSaveGState(context);
[self applyTransform:self.transform toContext:context];
self.renderingRecursively = YES;
if ( !self.masksToBounds ) {
CGContextSaveGState(context);
}
[self renderAsVectorInContext:context];
if ( !self.masksToBounds ) {
CGContextRestoreGState(context);
}
self.renderingRecursively = NO;
// render sublayers
NSArray *sublayersCopy = [self.sublayers copy];
for ( CALayer *currentSublayer in sublayersCopy ) {
CGContextSaveGState(context);
// Shift origin of context to match starting coordinate of sublayer
CGPoint currentSublayerFrameOrigin = currentSublayer.frame.origin;
CGRect currentSublayerBounds = currentSublayer.bounds;
CGContextTranslateCTM(context,
currentSublayerFrameOrigin.x - currentSublayerBounds.origin.x,
currentSublayerFrameOrigin.y - currentSublayerBounds.origin.y);
[self applyTransform:self.sublayerTransform toContext:context];
if ( [currentSublayer isKindOfClass:[CPTLayer class]] ) {
[(CPTLayer *) currentSublayer recursivelyRenderInContext:context];
}
else {
if ( self.masksToBounds ) {
CGContextClipToRect(context, currentSublayer.bounds);
}
[currentSublayer drawInContext:context];
}
CGContextRestoreGState(context);
}
[sublayersCopy release];
CGContextRestoreGState(context);
}
/// @cond
-(void)applyTransform:(CATransform3D)transform3D toContext:(CGContextRef)context
{
if ( !CATransform3DIsIdentity(transform3D) ) {
if ( CATransform3DIsAffine(transform3D) ) {
CGRect selfBounds = self.bounds;
CGPoint anchorPoint = self.anchorPoint;
CGPoint anchorOffset = CGPointMake(anchorOffset.x = selfBounds.origin.x + anchorPoint.x * selfBounds.size.width,
anchorOffset.y = selfBounds.origin.y + anchorPoint.y * selfBounds.size.height);
CGAffineTransform affineTransform = CGAffineTransformMakeTranslation(-anchorOffset.x, -anchorOffset.y);
affineTransform = CGAffineTransformConcat( affineTransform, CATransform3DGetAffineTransform(transform3D) );
affineTransform = CGAffineTransformTranslate(affineTransform, anchorOffset.x, anchorOffset.y);
CGRect transformedBounds = CGRectApplyAffineTransform(selfBounds, affineTransform);
CGContextTranslateCTM(context, -transformedBounds.origin.x, -transformedBounds.origin.y);
CGContextConcatCTM(context, affineTransform);
}
}
}
/// @endcond
/** @brief Updates the layer layout if needed and then draws layer content and the content of all sublayers into the provided graphics context.
* @param context The graphics context to draw into.
*/
-(void)layoutAndRenderInContext:(CGContextRef)context
{
[self layoutIfNeeded];
[self recursivelyRenderInContext:context];
}
/** @brief Draws layer content and the content of all sublayers into a PDF document.
* @return PDF representation of the layer content.
**/
-(NSData *)dataForPDFRepresentationOfLayer
{
NSMutableData *pdfData = [[NSMutableData alloc] init];
CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData( (CFMutableDataRef)pdfData );
const CGRect mediaBox = CGRectMake(0.0, 0.0, self.bounds.size.width, self.bounds.size.height);
CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, &mediaBox, NULL);
CPTPushCGContext(pdfContext);
CGContextBeginPage(pdfContext, &mediaBox);
[self layoutAndRenderInContext:pdfContext];
CGContextEndPage(pdfContext);
CGPDFContextClose(pdfContext);
CPTPopCGContext();
CGContextRelease(pdfContext);
CGDataConsumerRelease(dataConsumer);
return [pdfData autorelease];
}
#pragma mark -
#pragma mark Responder Chain and User interaction
/// @name User Interaction
/// @{
-(BOOL)pointingDeviceDownEvent:(CPTNativeEvent *)event atPoint:(CGPoint)interactionPoint
{
return NO;
}
-(BOOL)pointingDeviceUpEvent:(CPTNativeEvent *)event atPoint:(CGPoint)interactionPoint
{
return NO;
}
-(BOOL)pointingDeviceDraggedEvent:(CPTNativeEvent *)event atPoint:(CGPoint)interactionPoint
{
return NO;
}
-(BOOL)pointingDeviceCancelledEvent:(CPTNativeEvent *)event
{
return NO;
}
/// @}
#pragma mark -
#pragma mark Layout
/**
* @brief Align the receiver's position with pixel boundaries.
**/
-(void)pixelAlign
{
CGFloat scale = self.contentsScale;
CGPoint currentPosition = self.position;
CGPoint newPosition;
if ( scale == 1.0 ) {
newPosition.x = round(currentPosition.x);
newPosition.y = round(currentPosition.y);
}
else {
newPosition.x = round(currentPosition.x * scale) / scale;
newPosition.y = round(currentPosition.y * scale) / scale;
}
if ( CATransform3DIsIdentity(self.transform) ) {
CGSize currentSize = self.bounds.size;
CGPoint anchor = self.anchorPoint;
newPosition.x += (currentSize.width * anchor.x) - round(currentSize.width * anchor.x);
newPosition.y += (currentSize.height * anchor.y) - round(currentSize.height * anchor.y);
}
self.position = newPosition;
}
/// @cond
-(void)setPaddingLeft:(CGFloat)newPadding
{
if ( newPadding != paddingLeft ) {
paddingLeft = newPadding;
[self setNeedsLayout];
}
}
-(void)setPaddingRight:(CGFloat)newPadding
{
if ( newPadding != paddingRight ) {
paddingRight = newPadding;
[self setNeedsLayout];
}
}
-(void)setPaddingTop:(CGFloat)newPadding
{
if ( newPadding != paddingTop ) {
paddingTop = newPadding;
[self setNeedsLayout];
}
}
-(void)setPaddingBottom:(CGFloat)newPadding
{
if ( newPadding != paddingBottom ) {
paddingBottom = newPadding;
[self setNeedsLayout];
}
}
/// @endcond
-(void)layoutSublayers
{
// This is where we do our custom replacement for the Mac-only layout manager and autoresizing mask
// Subclasses should override to lay out their own sublayers
// Sublayers fill the super layer's bounds minus any padding by default
CGFloat leftPadding, topPadding, rightPadding, bottomPadding;
[self sublayerMarginLeft:&leftPadding top:&topPadding right:&rightPadding bottom:&bottomPadding];
CGRect selfBounds = self.bounds;
CGSize subLayerSize = selfBounds.size;
subLayerSize.width -= leftPadding + rightPadding;
subLayerSize.width = MAX(subLayerSize.width, (CGFloat)0.0);
subLayerSize.width = round(subLayerSize.width);
subLayerSize.height -= topPadding + bottomPadding;
subLayerSize.height = MAX(subLayerSize.height, (CGFloat)0.0);
subLayerSize.height = round(subLayerSize.height);
CGRect subLayerFrame;
subLayerFrame.origin = CGPointMake( round(leftPadding), round(bottomPadding) );
subLayerFrame.size = subLayerSize;
NSSet *excludedSublayers = [self sublayersExcludedFromAutomaticLayout];
Class layerClass = [CPTLayer class];
for ( CALayer *subLayer in self.sublayers ) {
if ( ![excludedSublayers containsObject:subLayer] && [subLayer isKindOfClass:layerClass] ) {
subLayer.frame = subLayerFrame;
[subLayer setNeedsLayout];
[subLayer setNeedsDisplay];
}
}
}
-(NSSet *)sublayersExcludedFromAutomaticLayout
{
return [NSSet set];
}
/** @brief Returns the margins that should be left between the bounds of the receiver and all sublayers.
* @param left The left margin.
* @param top The top margin.
* @param right The right margin.
* @param bottom The bottom margin.
**/
-(void)sublayerMarginLeft:(CGFloat *)left top:(CGFloat *)top right:(CGFloat *)right bottom:(CGFloat *)bottom
{
*left = self.paddingLeft;
*top = self.paddingTop;
*right = self.paddingRight;
*bottom = self.paddingBottom;
}
#pragma mark -
#pragma mark Sublayers
/// @cond
-(void)setSublayers:(NSArray *)sublayers
{
[super setSublayers:sublayers];
Class layerClass = [CPTLayer class];
CGFloat scale = self.contentsScale;
for ( CALayer *layer in sublayers ) {
if ( [layer isKindOfClass:layerClass] ) {
( (CPTLayer *)layer ).contentsScale = scale;
}
}
}
-(void)addSublayer:(CALayer *)layer
{
[super addSublayer:layer];
if ( [layer isKindOfClass:[CPTLayer class]] ) {
( (CPTLayer *)layer ).contentsScale = self.contentsScale;
}
}
-(void)insertSublayer:(CALayer *)layer atIndex:(unsigned)idx
{
[super insertSublayer:layer atIndex:idx];
if ( [layer isKindOfClass:[CPTLayer class]] ) {
( (CPTLayer *)layer ).contentsScale = self.contentsScale;
}
}
-(void)insertSublayer:(CALayer *)layer below:(CALayer *)sibling
{
[super insertSublayer:layer below:sibling];
if ( [layer isKindOfClass:[CPTLayer class]] ) {
( (CPTLayer *)layer ).contentsScale = self.contentsScale;
}
}
-(void)insertSublayer:(CALayer *)layer above:(CALayer *)sibling
{
[super insertSublayer:layer above:sibling];
if ( [layer isKindOfClass:[CPTLayer class]] ) {
( (CPTLayer *)layer ).contentsScale = self.contentsScale;
}
}
-(void)replaceSublayer:(CALayer *)layer with:(CALayer *)layer2
{
[super replaceSublayer:layer with:layer2];
if ( [layer2 isKindOfClass:[CPTLayer class]] ) {
( (CPTLayer *)layer2 ).contentsScale = self.contentsScale;
}
}
/// @endcond
#pragma mark -
#pragma mark Masking
// default path is the rounded rect layer bounds
-(CGPathRef)maskingPath
{
if ( self.masksToBounds ) {
CGPathRef path = self.outerBorderPath;
if ( path ) {
return path;
}
CGRect selfBounds = self.bounds;
if ( self.cornerRadius > 0.0 ) {
CGFloat radius = MIN(MIN(self.cornerRadius, selfBounds.size.width / (CGFloat)2.0), selfBounds.size.height / (CGFloat)2.0);
path = CreateRoundedRectPath(selfBounds, radius);
self.outerBorderPath = path;
CGPathRelease(path);
}
else {
CGMutablePathRef mutablePath = CGPathCreateMutable();
CGPathAddRect(mutablePath, NULL, selfBounds);
self.outerBorderPath = mutablePath;
CGPathRelease(mutablePath);
}
return self.outerBorderPath;
}
else {
return NULL;
}
}
-(CGPathRef)sublayerMaskingPath
{
return self.innerBorderPath;
}
/** @brief Recursively sets the clipping path of the given graphics context to the sublayer masking paths of its superlayers.
*
* The clipping path is built by recursively climbing the layer tree and combining the sublayer masks from
* each super layer. The tree traversal stops when a layer is encountered that is not a CPTLayer.
*
* @param context The graphics context to clip.
* @param sublayer The sublayer that called this method.
* @param offset The cumulative position offset between the receiver and the first layer in the recursive calling chain.
**/
-(void)applySublayerMaskToContext:(CGContextRef)context forSublayer:(CPTLayer *)sublayer withOffset:(CGPoint)offset
{
CGPoint sublayerBoundsOrigin = sublayer.bounds.origin;
CGPoint layerOffset = offset;
if ( !self.renderingRecursively ) {
CGPoint convertedOffset = [self convertPoint:sublayerBoundsOrigin fromLayer:sublayer];
layerOffset.x += convertedOffset.x;
layerOffset.y += convertedOffset.y;
}
CGAffineTransform sublayerTransform = CATransform3DGetAffineTransform(sublayer.transform);
CGContextConcatCTM( context, CGAffineTransformInvert(sublayerTransform) );
CALayer *superlayer = self.superlayer;
if ( [superlayer isKindOfClass:[CPTLayer class]] ) {
[(CPTLayer *) superlayer applySublayerMaskToContext:context forSublayer:self withOffset:layerOffset];
}
CGPathRef maskPath = self.sublayerMaskingPath;
if ( maskPath ) {
// CGAffineTransform transform = CATransform3DGetAffineTransform(self.transform);
// CGAffineTransform sublayerTransform = CATransform3DGetAffineTransform(self.sublayerTransform);
CGContextTranslateCTM(context, -layerOffset.x, -layerOffset.y);
// CGContextConcatCTM(context, CGAffineTransformInvert(transform));
// CGContextConcatCTM(context, CGAffineTransformInvert(sublayerTransform));
CGContextAddPath(context, maskPath);
CGContextClip(context);
// CGContextConcatCTM(context, sublayerTransform);
// CGContextConcatCTM(context, transform);
CGContextTranslateCTM(context, layerOffset.x, layerOffset.y);
}
CGContextConcatCTM(context, sublayerTransform);
}
/** @brief Sets the clipping path of the given graphics context to mask the content.
*
* The clipping path is built by recursively climbing the layer tree and combining the sublayer masks from
* each super layer. The tree traversal stops when a layer is encountered that is not a CPTLayer.
*
* @param context The graphics context to clip.
**/
-(void)applyMaskToContext:(CGContextRef)context
{
if ( [self.superlayer isKindOfClass:[CPTLayer class]] ) {
[(CPTLayer *)self.superlayer applySublayerMaskToContext:context forSublayer:self withOffset:CGPointZero];
}
CGPathRef maskPath = self.maskingPath;
if ( maskPath ) {
CGContextAddPath(context, maskPath);
CGContextClip(context);
}
}
-(void)setNeedsLayout
{
[super setNeedsLayout];
if ( self.graph ) {
[[NSNotificationCenter defaultCenter] postNotificationName:CPTGraphNeedsRedrawNotification object:self.graph];
}
}
-(void)setNeedsDisplay
{
[super setNeedsDisplay];
if ( self.graph ) {
[[NSNotificationCenter defaultCenter] postNotificationName:CPTGraphNeedsRedrawNotification object:self.graph];
}
}
#pragma mark -
#pragma mark Accessors
/// @cond
-(void)setPosition:(CGPoint)newPosition;
{
[super setPosition:newPosition];
if ( COREPLOT_LAYER_POSITION_CHANGE_ENABLED() ) {
CGRect currentFrame = self.frame;
if ( !CGRectEqualToRect( currentFrame, CGRectIntegral(self.frame) ) ) {
COREPLOT_LAYER_POSITION_CHANGE( (char *)class_getName([self class]),
(int)ceil(currentFrame.origin.x * 1000.0),
(int)ceil(currentFrame.origin.y * 1000.0),
(int)ceil(currentFrame.size.width * 1000.0),
(int)ceil(currentFrame.size.height * 1000.0) );
}
}
}
-(void)setHidden:(BOOL)newHidden
{
if ( newHidden != self.hidden ) {
[super setHidden:newHidden];
if ( !newHidden ) {
[self setNeedsDisplay];
}
}
}
-(void)setContentsScale:(CGFloat)newContentsScale
{
NSParameterAssert(newContentsScale > 0.0);
if ( self.contentsScale != newContentsScale ) {
if ( [CALayer instancesRespondToSelector:@selector(setContentsScale:)] ) {
super.contentsScale = newContentsScale;
[self setNeedsDisplay];
Class layerClass = [CPTLayer class];
for ( CALayer *subLayer in self.sublayers ) {
if ( [subLayer isKindOfClass:layerClass] ) {
( (CPTLayer *)subLayer ).contentsScale = newContentsScale;
}
}
}
}
}
-(CGFloat)contentsScale
{
CGFloat scale = 1.0;
if ( [CALayer instancesRespondToSelector:@selector(contentsScale)] ) {
scale = super.contentsScale;
}
return scale;
}
-(void)setShadow:(CPTShadow *)newShadow
{
if ( newShadow != shadow ) {
[shadow release];
shadow = [newShadow copy];
[self setNeedsDisplay];
}
}
-(void)setOuterBorderPath:(CGPathRef)newPath
{
if ( newPath != outerBorderPath ) {
CGPathRelease(outerBorderPath);
outerBorderPath = CGPathRetain(newPath);
}
}
-(void)setInnerBorderPath:(CGPathRef)newPath
{
if ( newPath != innerBorderPath ) {
CGPathRelease(innerBorderPath);
innerBorderPath = CGPathRetain(newPath);
}
}
-(void)setBounds:(CGRect)newBounds
{
[super setBounds:newBounds];
self.outerBorderPath = NULL;
self.innerBorderPath = NULL;
}
-(void)setCornerRadius:(CGFloat)newRadius
{
if ( newRadius != self.cornerRadius ) {
super.cornerRadius = newRadius;
[self setNeedsDisplay];
self.outerBorderPath = NULL;
self.innerBorderPath = NULL;
}
}
/// @endcond
#pragma mark -
#pragma mark Description
-(NSString *)description
{
return [NSString stringWithFormat:@"<%@ bounds: %@>", [super description], CPTStringFromRect(self.bounds)];
}
/**
* @brief Logs this layer and all of its sublayers.
**/
-(void)logLayers
{
NSLog(@"Layer tree:\n%@", [self subLayersAtIndex:0]);
}
/// @cond
-(NSString *)subLayersAtIndex:(NSUInteger)index
{
NSMutableString *result = [NSMutableString string];
for ( NSUInteger i = 0; i < index; i++ ) {
[result appendString:@" "];
}
[result appendString:[self description]];
for ( CPTLayer *sublayer in self.sublayers ) {
[result appendString:@"\n"];
[result appendString:[sublayer subLayersAtIndex:index + 1]];
}
return result;
}
/// @endcond
@end