API  1.0.0
CPBezierPath.j
Go to the documentation of this file.
1 /*
2  * CPBezierPath.j
3  *
4  * Created by Ross Boucher.
5  * Copyright 2009, 280 North, Inc.
6  *
7  * Adapted from Kevin Wojniak, portions Copyright 2009 Kevin Wojniak.
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  * Copyright 2009 280 North, Inc.
23  */
24 
25 
26 
27 
28 // Class defaults
29 
30 var DefaultLineWidth = 1.0;
31 
32 
44 @implementation CPBezierPath : CPObject
45 {
46  CGPath _path;
47  float _lineWidth;
48  CPArray _lineDashes;
49  float _lineDashesPhase;
50 }
51 
55 + (CPBezierPath)bezierPath
56 {
57  return [[self alloc] init];
58 }
59 
63 + (CPBezierPath)bezierPathWithOvalInRect:(CGRect)aRect
64 {
65  var path = [self bezierPath];
66 
67  [path appendBezierPathWithOvalInRect:aRect];
68 
69  return path;
70 }
71 
75 + (CPBezierPath)bezierPathWithRect:(CGRect)aRect
76 {
77  var path = [self bezierPath];
78 
79  [path appendBezierPathWithRect:aRect];
80 
81  return path;
82 }
83 
84 + (CPBezierPath)bezierPathWithRoundedRect:(CGRect)aRect xRadius:(float)xRadius yRadius:(float)yRadius
85 {
86  var path = [self bezierPath];
87 
88  [path appendBezierPathWithRoundedRect:aRect xRadius:xRadius yRadius:yRadius];
89 
90  return path;
91 }
92 
96 + (float)defaultLineWidth
97 {
98  return DefaultLineWidth;
99 }
100 
104 + (void)setDefaultLineWidth:(float)width
105 {
107 }
108 
112 + (void)fillRect:(CGRect)aRect
113 {
114  [[self bezierPathWithRect:aRect] fill];
115 }
116 
120 + (void)strokeRect:(CGRect)aRect
121 {
122  [[self bezierPathWithRect:aRect] stroke];
123 }
124 
128 + (void)strokeLineFromPoint:(CGPoint)point1 toPoint:(CGPoint)point2
129 {
130  var path = [self bezierPath];
131 
132  [path moveToPoint:point1];
133  [path lineToPoint:point2];
134 
135  [path stroke];
136 }
137 
141 - (id)init
142 {
143  if (self = [super init])
144  {
145  _path = CGPathCreateMutable();
146  _lineWidth = [[self class] defaultLineWidth];
147  _lineDashesPhase = 0;
148  _lineDashes = [];
149  }
150 
151  return self;
152 }
153 
157 - (void)moveToPoint:(CGPoint)point
158 {
159  CGPathMoveToPoint(_path, nil, point.x, point.y);
160 }
161 
165 - (void)lineToPoint:(CGPoint)point
166 {
167  CGPathAddLineToPoint(_path, nil, point.x, point.y);
168 }
169 
173 - (void)curveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2
174 {
175  CGPathAddCurveToPoint(_path, nil, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, endPoint.x, endPoint.y);
176 }
177 
178 - (CGRect)bounds
179 {
180  // TODO: this should return this. The controlPointBounds is not a tight fit.
181  // return CGPathGetBoundingBox(_path);
182 
183  return [self controlPointBounds];
184 }
185 
186 - (CGRect)controlPointBounds
187 {
188  return CGPathGetBoundingBox(_path);
189 }
190 
194 - (void)closePath
195 {
196  CGPathCloseSubpath(_path);
197 }
198 
202 - (void)stroke
203 {
205 
206  CGContextBeginPath(ctx);
207  CGContextAddPath(ctx, _path);
208  CGContextSetLineWidth(ctx, [self lineWidth]);
209  CGContextSetLineDash(ctx, _lineDashesPhase, _lineDashes);
210  CGContextStrokePath(ctx);
211 }
212 
216 - (void)fill
217 {
219 
220  CGContextBeginPath(ctx);
221  CGContextAddPath(ctx, _path);
222  CGContextSetLineWidth(ctx, [self lineWidth]);
223  CGContextSetLineDash(ctx, _lineDashesPhase, _lineDashes);
224  CGContextClosePath(ctx);
225  CGContextFillPath(ctx);
226 }
227 
231 - (void)getLineDash:(CPArrayRef)patternRef count:(NSInteger)count phase:(CGFloatRef)phaseRef
232 {
233  return [self getLineDash:patternRef phase:phaseRef];
234 }
235 
239 - (void)getLineDash:(CPArrayRef)patternRef phase:(CGFloatRef)phaseRef
240 {
241  if (patternRef)
242  @deref(patternRef) = [_lineDashes copy];
243  if (phaseRef)
244  @deref(phaseRef) = _lineDashesPhase;
245 }
246 
250 - (void)setLineDash:(CPArray)aPattern count:(NSInteger)count phase:(CGFloat)aPhase
251 {
252  [self setLineDash:aPattern phase:aPhase];
253 }
254 
261 - (void)setLineDash:(CPArray)aPattern phase:(CGFloat)aPhase
262 {
263  _lineDashes = aPattern;
264  _lineDashesPhase = aPhase;
265 }
266 
270 - (float)lineWidth
271 {
272  return _lineWidth;
273 }
274 
278 - (void)setLineWidth:(float)lineWidth
279 {
280  _lineWidth = lineWidth;
281 }
282 
286 - (unsigned)elementCount
287 {
288  return _path.count;
289 }
290 
294 - (BOOL)isEmpty
295 {
296  return CGPathIsEmpty(_path);
297 }
298 
302 - (CGPoint)currentPoint
303 {
304  return CGPathGetCurrentPoint(_path);
305 }
306 
310 - (void)appendBezierPathWithPoints:(CPArray)points count:(unsigned)count
311 {
312  CGPathAddLines(_path, nil, points, count);
313 }
314 
318 - (void)appendBezierPathWithRect:(CGRect)rect
319 {
320  CGPathAddRect(_path, nil, rect);
321 }
322 
326 - (void)appendBezierPathWithOvalInRect:(CGRect)rect
327 {
328  CGPathAddPath(_path, nil, CGPathWithEllipseInRect(rect));
329 }
330 
334 - (void)appendBezierPathWithRoundedRect:(CGRect)rect xRadius:(float)xRadius yRadius:(float)yRadius
335 {
336  CGPathAddPath(_path, nil, CGPathWithRoundedRectangleInRect(rect, xRadius, yRadius, YES, YES, YES, YES));
337 }
338 
339 - (void)appendBezierPathWithArcFromPoint:(CGPoint)fromPoint toPoint:(CGPoint)toPoint radius:(float)radius
340 {
341  CGPathAddArcToPoint(_path, null, fromPoint.x, fromPoint.y, toPoint.x, toPoint.y, radius);
342 }
343 
347 - (void)appendBezierPath:(CPBezierPath)other
348 {
349  CGPathAddPath(_path, nil, other._path);
350 }
351 
355 - (void)removeAllPoints
356 {
357  _path = CGPathCreateMutable();
358 }
359 
360 - (void)addClip
361 {
363 
364  CGContextAddPath(ctx, _path);
365  CGContextClip(ctx);
366 }
367 
368 - (void)setClip
369 {
371 
372  CGContextBeginPath(ctx);
373  CGContextAddPath(ctx, _path);
374  CGContextClip(ctx);
375 }
376 
377 @end
function CGPathWithRoundedRectangleInRect(aRect, xRadius, yRadius, ne, se, sw, nw)
Definition: CGPath.j:352
id init()
Definition: CALayer.j:126
CPGraphicsContext currentContext()
function CGPathGetBoundingBox(aPath)
Definition: CGPath.j:510
function CGPathCloseSubpath(aPath)
Definition: CGPath.j:399
CGRect controlPointBounds()
Definition: CPBezierPath.j:186
int width
void setLineDash:phase:(CPArray aPattern, [phase] CGFloat aPhase)
Definition: CPBezierPath.j:261
CPBezierPath bezierPathWithRect:(CGRect aRect)
Definition: CPBezierPath.j:75
function CGContextStrokePath(aContext)
Definition: CGContext.j:619
function CGContextSetLineWidth(aContext, aLineWidth)
Definition: CGContext.j:177
function CGContextClosePath(aContext)
Definition: CGContext.j:322
function CGPathMoveToPoint(aPath, aTransform, x, y)
Definition: CGPath.j:301
CGRect bounds()
Definition: CALayer.j:203
function CGPathAddLines(aPath, aTransform, points, count)
Definition: CGPath.j:187
var DefaultLineWidth
Definition: CPBezierPath.j:30
function CGContextSetLineDash(aContext, aPhase, someDashes)
Definition: CGContext.j:166
function CGPathAddCurveToPoint(aPath, aTransform, cp1x, cp1y, cp2x, cp2y, x, y)
Definition: CGPath.j:170
function CGContextAddPath(aContext, aPath)
Definition: CGContext.j:258
function CGPathWithEllipseInRect(aRect)
Definition: CGPath.j:328
function CGPathIsEmpty(aPath)
Definition: CGPath.j:502
void getLineDash:phase:(CPArrayRef patternRef, [phase] CGFloatRef phaseRef)
Definition: CPBezierPath.j:239
function CGContextBeginPath(aContext)
Definition: CGContext.j:311
function CGPathCreateMutable()
Definition: CGPath.j:43
function CGPathAddRect(aPath, aTransform, aRect)
Definition: CGPath.j:276
function CGPathGetCurrentPoint(aPath)
Definition: CGPath.j:497
CPBezierPath bezierPath()
Definition: CPBezierPath.j:55
function CGPathAddLineToPoint(aPath, aTransform, x, y)
Definition: CGPath.j:201
Class class()
Definition: CPObject.j:179
function CGContextFillPath(aContext)
Definition: CGContext.j:548
function CGPathAddPath(aPath, aTransform, anotherPath)
Definition: CGPath.j:212
function CGPathAddArcToPoint(aPath, aTransform, x1, y1, x2, y2, aRadius)
Definition: CGPath.j:140
id alloc()
Definition: CPObject.j:130