forked from core-plot/core-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPTLineStyle.m
More file actions
411 lines (336 loc) · 12.6 KB
/
CPTLineStyle.m
File metadata and controls
411 lines (336 loc) · 12.6 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
#import "CPTLineStyle.h"
#import "CPTColor.h"
#import "CPTFill.h"
#import "CPTGradient.h"
#import "CPTMutableLineStyle.h"
#import "CPTPlatformSpecificFunctions.h"
#import "CPTUtilities.h"
#import "NSCoderExtensions.h"
#import "NSNumberExtensions.h"
/// @cond
@interface CPTLineStyle()
@property (nonatomic, readwrite, assign) CGLineCap lineCap;
@property (nonatomic, readwrite, assign) CGLineJoin lineJoin;
@property (nonatomic, readwrite, assign) CGFloat miterLimit;
@property (nonatomic, readwrite, assign) CGFloat lineWidth;
@property (nonatomic, readwrite, strong, nullable) CPTNumberArray dashPattern;
@property (nonatomic, readwrite, assign) CGFloat patternPhase;
@property (nonatomic, readwrite, strong, nullable) CPTColor *lineColor;
@property (nonatomic, readwrite, strong, nullable) CPTFill *lineFill;
@property (nonatomic, readwrite, strong, nullable) CPTGradient *lineGradient;
-(void)strokePathWithGradient:(CPTGradient *)gradient inContext:(CGContextRef)context;
@end
/// @endcond
#pragma mark -
/** @brief Immutable wrapper for various line drawing properties. Create a CPTMutableLineStyle if you want to customize properties.
*
* The line stroke can be drawn three different ways, prioritized in the following order:
*
* -# A gradient that follows the stroked path (@ref lineGradient)
* -# As a cut out mask over an area filled with a CPTFill (@ref lineFill)
* -# Filled with a solid color (@ref lineColor)
*
* @see See Apple’s <a href="http://developer.apple.com/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_paths/dq_paths.html#//apple_ref/doc/uid/TP30001066-CH211-TPXREF105">Quartz 2D</a>
* and <a href="http://developer.apple.com/documentation/GraphicsImaging/Reference/CGContext/Reference/reference.html">CGContext</a>
* documentation for more information about each of these properties.
**/
@implementation CPTLineStyle
/** @property CGLineCap lineCap;
* @brief The style for the endpoints of lines drawn in a graphics context. Default is @ref kCGLineCapButt.
**/
@synthesize lineCap;
/** @property CGLineJoin lineJoin
* @brief The style for the joins of connected lines in a graphics context. Default is @ref kCGLineJoinMiter.
**/
@synthesize lineJoin;
/** @property CGFloat miterLimit
* @brief The miter limit for the joins of connected lines in a graphics context. Default is @num{10.0}.
**/
@synthesize miterLimit;
/** @property CGFloat lineWidth
* @brief The line width for a graphics context. Default is @num{1.0}.
**/
@synthesize lineWidth;
/** @property CPTNumberArray dashPattern
* @brief The dash-and-space pattern for the line. Default is @nil.
**/
@synthesize dashPattern;
/** @property CGFloat patternPhase
* @brief The starting phase of the line dash pattern. Default is @num{0.0}.
**/
@synthesize patternPhase;
/** @property CPTColor *lineColor
* @brief The current stroke color in a context. Default is solid black.
**/
@synthesize lineColor;
/** @property CPTFill *lineFill
* @brief The current line fill. Default is @nil.
*
* If @nil, the line is drawn using the @ref lineGradient or @ref lineColor.
**/
@synthesize lineFill;
/** @property CPTGradient *lineGradient
* @brief The current line gradient fill. Default is @nil.
*
* If @nil, the line is drawn using the @ref lineFill or @ref lineColor.
**/
@synthesize lineGradient;
/** @property BOOL opaque
* @brief If @YES, a line drawn using the line style is completely opaque.
*/
@dynamic opaque;
#pragma mark -
#pragma mark Init/Dealloc
/** @brief Creates and returns a new CPTLineStyle instance.
* @return A new CPTLineStyle instance.
**/
+(instancetype)lineStyle
{
return [[self alloc] init];
}
/** @brief Creates and returns a new line style instance initialized from an existing line style.
*
* The line style will be initialized with values from the given @par{lineStyle}.
*
* @param lineStyle An existing CPTLineStyle.
* @return A new line style instance.
**/
+(instancetype)lineStyleWithStyle:(CPTLineStyle *)lineStyle
{
CPTLineStyle *newLineStyle = [[self alloc] init];
newLineStyle.lineCap = lineStyle.lineCap;
newLineStyle.lineJoin = lineStyle.lineJoin;
newLineStyle.miterLimit = lineStyle.miterLimit;
newLineStyle.lineWidth = lineStyle.lineWidth;
newLineStyle.dashPattern = [lineStyle.dashPattern copy];
newLineStyle.patternPhase = lineStyle.patternPhase;
newLineStyle.lineColor = lineStyle.lineColor;
newLineStyle.lineFill = lineStyle.lineFill;
newLineStyle.lineGradient = lineStyle.lineGradient;
return newLineStyle;
}
/// @name Initialization
/// @{
/** @brief Initializes a newly allocated CPTLineStyle object.
*
* The initialized object will have the following properties:
* - @ref lineCap = @ref kCGLineCapButt
* - @ref lineJoin = @ref kCGLineJoinMiter
* - @ref miterLimit = @num{10.0}
* - @ref lineWidth = @num{1.0}
* - @ref dashPattern = @nil
* - @ref patternPhase = @num{0.0}
* - @ref lineColor = opaque black
* - @ref lineFill = @nil
* - @ref lineGradient = @nil
*
* @return The initialized object.
**/
-(instancetype)init
{
if ( (self = [super init]) ) {
lineCap = kCGLineCapButt;
lineJoin = kCGLineJoinMiter;
miterLimit = CPTFloat(10.0);
lineWidth = CPTFloat(1.0);
dashPattern = nil;
patternPhase = CPTFloat(0.0);
lineColor = [CPTColor blackColor];
lineFill = nil;
lineGradient = nil;
}
return self;
}
/// @}
#pragma mark -
#pragma mark NSCoding Methods
/// @cond
-(void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeInt:self.lineCap forKey:@"CPTLineStyle.lineCap"];
[coder encodeInt:self.lineJoin forKey:@"CPTLineStyle.lineJoin"];
[coder encodeCGFloat:self.miterLimit forKey:@"CPTLineStyle.miterLimit"];
[coder encodeCGFloat:self.lineWidth forKey:@"CPTLineStyle.lineWidth"];
[coder encodeObject:self.dashPattern forKey:@"CPTLineStyle.dashPattern"];
[coder encodeCGFloat:self.patternPhase forKey:@"CPTLineStyle.patternPhase"];
[coder encodeObject:self.lineColor forKey:@"CPTLineStyle.lineColor"];
[coder encodeObject:self.lineFill forKey:@"CPTLineStyle.lineFill"];
[coder encodeObject:self.lineGradient forKey:@"CPTLineStyle.lineGradient"];
}
-(instancetype)initWithCoder:(NSCoder *)coder
{
if ( (self = [super init]) ) {
lineCap = (CGLineCap)[coder decodeIntForKey : @"CPTLineStyle.lineCap"];
lineJoin = (CGLineJoin)[coder decodeIntForKey : @"CPTLineStyle.lineJoin"];
miterLimit = [coder decodeCGFloatForKey:@"CPTLineStyle.miterLimit"];
lineWidth = [coder decodeCGFloatForKey:@"CPTLineStyle.lineWidth"];
dashPattern = [coder decodeObjectForKey:@"CPTLineStyle.dashPattern"];
patternPhase = [coder decodeCGFloatForKey:@"CPTLineStyle.patternPhase"];
lineColor = [coder decodeObjectForKey:@"CPTLineStyle.lineColor"];
lineFill = [coder decodeObjectForKey:@"CPTLineStyle.lineFill"];
lineGradient = [coder decodeObjectForKey:@"CPTLineStyle.lineGradient"];
}
return self;
}
/// @endcond
#pragma mark -
#pragma mark Drawing
/** @brief Sets all of the line drawing properties in the given graphics context.
* @param context The graphics context.
**/
-(void)setLineStyleInContext:(CGContextRef)context
{
CGContextSetLineCap(context, self.lineCap);
CGContextSetLineJoin(context, self.lineJoin);
CGContextSetMiterLimit(context, self.miterLimit);
CGContextSetLineWidth(context, self.lineWidth);
CPTNumberArray myDashPattern = self.dashPattern;
NSUInteger dashCount = myDashPattern.count;
if ( dashCount > 0 ) {
CGFloat *dashLengths = (CGFloat *)calloc( dashCount, sizeof(CGFloat) );
NSUInteger dashCounter = 0;
for ( NSNumber *currentDashLength in myDashPattern ) {
dashLengths[dashCounter++] = [currentDashLength cgFloatValue];
}
CGContextSetLineDash(context, self.patternPhase, dashLengths, dashCount);
free(dashLengths);
}
else {
CGContextSetLineDash(context, CPTFloat(0.0), NULL, 0);
}
CGContextSetStrokeColorWithColor(context, self.lineColor.cgColor);
}
/** @brief Stroke the current path in the given graphics context.
* Call @link CPTLineStyle::setLineStyleInContext: -setLineStyleInContext: @endlink first to set up the drawing properties.
*
* @param context The graphics context.
**/
-(void)strokePathInContext:(CGContextRef)context
{
CPTGradient *gradient = self.lineGradient;
CPTFill *fill = self.lineFill;
if ( gradient ) {
[self strokePathWithGradient:gradient inContext:context];
}
else if ( fill ) {
CGContextReplacePathWithStrokedPath(context);
[fill fillPathInContext:context];
}
else {
CGContextStrokePath(context);
}
}
/** @brief Stroke a rectangular path in the given graphics context.
* Call @link CPTLineStyle::setLineStyleInContext: -setLineStyleInContext: @endlink first to set up the drawing properties.
*
* @param rect The rectangle to draw.
* @param context The graphics context.
**/
-(void)strokeRect:(CGRect)rect inContext:(CGContextRef)context
{
CPTGradient *gradient = self.lineGradient;
CPTFill *fill = self.lineFill;
if ( gradient ) {
CGContextBeginPath(context);
CGContextAddRect(context, rect);
[self strokePathWithGradient:gradient inContext:context];
}
else if ( fill ) {
CGContextBeginPath(context);
CGContextAddRect(context, rect);
CGContextReplacePathWithStrokedPath(context);
[fill fillPathInContext:context];
}
else {
CGContextStrokeRect(context, rect);
}
}
/// @cond
-(void)strokePathWithGradient:(CPTGradient *)gradient inContext:(CGContextRef)context
{
if ( gradient ) {
CGRect deviceRect = CGContextConvertRectToDeviceSpace( context, CPTRectMake(0.0, 0.0, 1.0, 1.0) );
CGFloat step = CPTFloat(2.0) / deviceRect.size.height;
CGFloat startWidth = self.lineWidth;
CGPathRef path = CGContextCopyPath(context);
CGContextBeginPath(context);
for ( CGFloat width = startWidth; width > CPTFloat(0.0); width -= step ) {
CGContextSetLineWidth(context, width);
CGColorRef gradientColor = [gradient newColorAtPosition:CPTFloat(1.0) - width / startWidth];
CGContextSetStrokeColorWithColor(context, gradientColor);
CGColorRelease(gradientColor);
CGContextAddPath(context, path);
CGContextStrokePath(context);
}
CGPathRelease(path);
}
}
/// @endcond
#pragma mark -
#pragma mark Opacity
-(BOOL)isOpaque
{
BOOL opaqueLine = NO;
if ( self.dashPattern.count <= 1 ) {
if ( self.lineGradient ) {
opaqueLine = self.lineGradient.opaque;
}
else if ( self.lineFill ) {
opaqueLine = self.lineFill.opaque;
}
else if ( self.lineColor ) {
opaqueLine = self.lineColor.opaque;
}
}
return opaqueLine;
}
#pragma mark -
#pragma mark NSCopying Methods
/// @cond
-(id)copyWithZone:(NSZone *)zone
{
CPTLineStyle *styleCopy = [[CPTLineStyle allocWithZone:zone] init];
styleCopy.lineCap = self.lineCap;
styleCopy.lineJoin = self.lineJoin;
styleCopy.miterLimit = self.miterLimit;
styleCopy.lineWidth = self.lineWidth;
styleCopy.dashPattern = [self.dashPattern copy];
styleCopy.patternPhase = self.patternPhase;
styleCopy.lineColor = self.lineColor;
styleCopy.lineFill = self.lineFill;
styleCopy.lineGradient = self.lineGradient;
return styleCopy;
}
/// @endcond
#pragma mark -
#pragma mark NSMutableCopying Methods
/// @cond
-(id)mutableCopyWithZone:(NSZone *)zone
{
CPTLineStyle *styleCopy = [[CPTMutableLineStyle allocWithZone:zone] init];
styleCopy.lineCap = self.lineCap;
styleCopy.lineJoin = self.lineJoin;
styleCopy.miterLimit = self.miterLimit;
styleCopy.lineWidth = self.lineWidth;
styleCopy.dashPattern = [self.dashPattern copy];
styleCopy.patternPhase = self.patternPhase;
styleCopy.lineColor = self.lineColor;
styleCopy.lineFill = self.lineFill;
styleCopy.lineGradient = self.lineGradient;
return styleCopy;
}
/// @endcond
#pragma mark -
#pragma mark Debugging
/// @cond
-(id)debugQuickLookObject
{
const CGRect rect = CGRectMake(0.0, 0.0, 100.0, 100.0);
return CPTQuickLookImage(rect, ^(CGContextRef context, CGFloat scale, CGRect bounds) {
const CGRect alignedRect = CPTAlignBorderedRectToUserSpace(context, bounds, self);
[self setLineStyleInContext:context];
[self strokeRect:alignedRect inContext:context];
});
}
/// @endcond
@end