API  0.9.6
 All Classes Files Functions Variables Macros Groups Pages
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 
45 @implementation CPBezierPath : CPObject
46 {
47  CGPath _path;
48  float _lineWidth;
49 }
50 
54 + (CPBezierPath)bezierPath
55 {
56  return [[self alloc] init];
57 }
58 
62 + (CPBezierPath)bezierPathWithOvalInRect:(CGRect)rect
63 {
64  var path = [self bezierPath];
65 
66  [path appendBezierPathWithOvalInRect:rect];
67 
68  return path;
69 }
70 
74 + (CPBezierPath)bezierPathWithRect:(CGRect)rect
75 {
76  var path = [self bezierPath];
77 
78  [path appendBezierPathWithRect:rect];
79 
80  return path;
81 }
82 
86 + (float)defaultLineWidth
87 {
88  return DefaultLineWidth;
89 }
90 
94 + (void)setDefaultLineWidth:(float)width
95 {
97 }
98 
102 + (void)fillRect:(CGRect)aRect
103 {
104  [[self bezierPathWithRect:aRect] fill];
105 }
106 
110 + (void)strokeRect:(CGRect)aRect
111 {
112  [[self bezierPathWithRect:aRect] stroke];
113 }
114 
118 + (void)strokeLineFromPoint:(CGPoint)point1 toPoint:(CGPoint)point2
119 {
120  var path = [self bezierPath];
121 
122  [path moveToPoint:point1];
123  [path lineToPoint:point2];
124 
125  [path stroke];
126 }
127 
131 - (id)init
132 {
133  if (self = [super init])
134  {
135  _path = CGPathCreateMutable();
136  _lineWidth = [[self class] defaultLineWidth];
137  }
138 
139  return self;
140 }
141 
145 - (void)moveToPoint:(CGPoint)point
146 {
147  CGPathMoveToPoint(_path, nil, point.x, point.y);
148 }
149 
153 - (void)lineToPoint:(CGPoint)point
154 {
155  CGPathAddLineToPoint(_path, nil, point.x, point.y);
156 }
157 
161 - (void)curveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2
162 {
163  CGPathAddCurveToPoint(_path, nil, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, endPoint.x, endPoint.y);
164 }
165 
166 - (CGRect)bounds
167 {
168  // TODO: this should return this. The controlPointBounds is not a tight fit.
169  // return CGPathGetPathBoundingBox(_path);
170 
171  return [self controlPointBounds];
172 }
173 
174 - (CGRect)controlPointBounds
175 {
176  return CGPathGetBoundingBox(_path);
177 }
178 
182 - (void)closePath
183 {
184  CGPathCloseSubpath(_path);
185 }
186 
190 - (void)stroke
191 {
193 
194  CGContextBeginPath(ctx);
195  CGContextAddPath(ctx, _path);
196  CGContextSetLineWidth(ctx, [self lineWidth]);
197  CGContextStrokePath(ctx);
198 }
199 
203 - (void)fill
204 {
206 
207  CGContextBeginPath(ctx);
208  CGContextAddPath(ctx, _path);
209  CGContextSetLineWidth(ctx, [self lineWidth]);
210  CGContextClosePath(ctx);
211  CGContextFillPath(ctx);
212 }
213 
217 - (float)lineWidth
218 {
219  return _lineWidth;
220 }
221 
225 - (void)setLineWidth:(float)lineWidth
226 {
227  _lineWidth = lineWidth;
228 }
229 
233 - (unsigned)elementCount
234 {
235  return _path.count;
236 }
237 
241 - (BOOL)isEmpty
242 {
243  return CGPathIsEmpty(_path);
244 }
245 
249 - (CGPoint)currentPoint
250 {
251  return CGPathGetCurrentPoint(_path);
252 }
253 
257 - (void)appendBezierPathWithPoints:(CPArray)points count:(unsigned)count
258 {
259  CGPathAddLines(_path, nil, points, count);
260 }
261 
265 - (void)appendBezierPathWithRect:(CGRect)rect
266 {
267  CGPathAddRect(_path, nil, rect);
268 }
269 
273 - (void)appendBezierPathWithOvalInRect:(CGRect)rect
274 {
275  CGPathAddPath(_path, nil, CGPathWithEllipseInRect(rect));
276 }
277 
281 - (void)appendBezierPathWithRoundedRect:(CGRect)rect xRadius:(float)xRadius yRadius:(float)yRadius
282 {
283  CGPathAddPath(_path, nil, CGPathWithRoundedRectangleInRect(rect, xRadius, yRadius, YES, YES, YES, YES));
284 }
285 
286 - (void)appendBezierPathWithArcFromPoint:(CGPoint)fromPoint toPoint:(CGPoint)toPoint radius:(float)radius
287 {
288  CGPathAddArcToPoint(_path, null, fromPoint.x, fromPoint.y, toPoint.x, toPoint.y, radius);
289 }
290 
294 - (void)appendBezierPath:(CPBezierPath)other
295 {
296  CGPathAddPath(_path, nil, other._path);
297 }
298 
302 - (void)removeAllPoints
303 {
304  _path = CGPathCreateMutable();
305 }
306 
307 - (void)addClip
308 {
310 
311  CGContextAddPath(ctx, _path);
312  CGContextClip(ctx);
313 }
314 
315 - (void)setClip
316 {
318 
319  CGContextBeginPath(ctx);
320  CGContextAddPath(ctx, _path);
321  CGContextClip(ctx);
322 }
323 
324 @end