Add CPBezierPath, clean up some of CGPath and CGContext

This commit is contained in:
Ross Boucher committed 2009-05-14 19:06:58 -07:00
1 parent 22f7985d20
commit 9c7f07979b
4 files changed
+305 -104

No files matched your search

+203
View File
@@ -0,0 +1,203 @@
/*
* CPBezierPath.j
*
* Created by Ross Boucher.
* Copyright 2009, 280 North, Inc.
*
* Adapted from Kevin Wojniak, portions Copyright 2009 Kevin Wojniak.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
* Copyright 2009 280 North, Inc.
*/
@import <AppKit/CPGraphicsContext.j>
@import <Foundation/CPObject.j>
// Class defaults
var DefaultLineWidth = 1.0;
@implementation CPBezierPath : CPObject
{
CGPath _path;
float _lineWidth;
}
+ (CPBezierPath)bezierPath
{
return [[[self class] alloc] init];
}
+ (CPBezierPath)bezierPathWithOvalInRect:(CGRect)rect
{
var path = [[self class] bezierPath];
[path appendBezierPathWithOvalInRect:rect];
return path;
}
+ (CPBezierPath)bezierPathWithRect:(CGRect)rect
{
var path = [[self class] bezierPath];
[path appendBezierPathWithRect:rect];
return path;
}
+ (float)defaultLineWidth
{
return DefaultLineWidth;
}
+ (void)setDefaultLineWidth:(float)width
{
DefaultLineWidth = width;
}
+ (void)fillRect:(CGRect)rect
{
[[[self class] bezierPathWithRect:rect] fill];
}
+ (void)strokeRect:(CGRect)rect
{
[[[self class] bezierPathWithRect:rect] stroke];
}
+ (void)strokeLineFromPoint:(CGPoint)point1 toPoint:(CGPoint)point2
{
var path = [[self class] bezierPath];
[path moveToPoint:point1];
[path lineToPoint:point2];
[path stroke];
}
- (id)init
{
if (self = [super init])
{
_path = CGPathCreateMutable();
_lineWidth = [[self class] defaultLineWidth];
}
return self;
}
- (void)moveToPoint:(CGPoint)point
{
CGPathMoveToPoint(_path, nil, point.x, point.y);
}
- (void)lineToPoint:(CGPoint)point
{
CGPathAddLineToPoint(_path, nil, point.x, point.y);
}
- (void)curveToPoint:(CGPoint)endPoint controlPoint1:(CGPoint)controlPoint1 controlPoint2:(CGPoint)controlPoint2
{
CGPathAddCurveToPoint(_path, nil, controlPoint1.x, controlPoint1.y, controlPoint2.x, controlPoint2.y, endPoint.x, endPoint.y);
}
- (void)closePath
{
CGPathCloseSubpath(_path);
}
- (void)stroke
{
var ctx = [[CPGraphicsContext currentContext] graphicsPort];
CGContextBeginPath(ctx);
CGContextAddPath(ctx, _path);
CGContextSetLineWidth(ctx, [self lineWidth]);
CGContextStrokePath(ctx);
CGContextClosePath(ctx);
}
- (void)fill
{
var ctx = [[CPGraphicsContext currentContext] graphicsPort];
CGContextBeginPath(ctx);
CGContextAddPath(ctx, _path);
CGContextSetLineWidth(ctx, [self lineWidth]);
CGContextFillPath(ctx);
CGContextClosePath(ctx);
}
- (float)lineWidth
{
return _lineWidth;
}
- (void)setLineWidth:(float)lineWidth
{
_lineWidth = lineWidth;
}
- (unsigned)elementCount
{
return _path.count;
}
- (BOOL)isEmpty
{
return CGPathIsEmpty(_path);
}
- (CGPoint)currentPoint
{
return CGPathGetCurrentPoint(_path);
}
- (void)appendBezierPathWithPoints:(CPArray)points count:(unsigned)count
{
CGPathAddLines(_path, nil, points, count);
}
- (void)appendBezierPathWithRect:(CGRect)rect
{
CGPathAddRect(_path, nil, rect);
}
- (void)appendBezierPathWithOvalInRect:(CGRect)rect
{
CGPathAddPath(_path, nil, CGPathWithEllipseInRect(rect));
}
- (void)appendBezierPathWithRoundedRect:(CGRect)rect xRadius:(float)xRadius yRadius:(float)yRadius
{
CGPathAddPath(_path, nil, CGPathWithRoundedRectangleInRect(rect, xRadius, yRadius, YES, YES, YES, YES));
}
- (void)appendBezierPath:(NSBezierPath *)other
{
CGPathAddPath(_path, nil, other._path);
}
- (void)removeAllPoints
{
_path = CGPathCreateMutable();
}
@end
+22
View File
@@ -590,6 +590,28 @@ url("data:image/png;base64,BASE64ENCODEDDATA") // if there is a pattern image
@end
@implementation CPColor (CoreGraphicsExtensions)
- (void)set
{
[self setFill];
[self setStroke];
}
- (void)setFill
{
var ctx = [[CPGraphicsContext currentContext] graphicsPort];
CGContextSetFillColor(ctx, self);
}
- (void)setStroke
{
var ctx = [[CPGraphicsContext currentContext] graphicsPort];
CGContextSetStrokeColor(ctx, self);
}
@end
var CPColorComponentsKey = @"CPColorComponentsKey",
CPColorPatternImageKey = @"CPColorPatternImageKey";
+3 -100
View File
@@ -459,24 +459,7 @@ var KAPPA = 4.0 * ((SQRT2 - 1.0) / 3.0);
*/
function CGContextAddEllipseInRect(aContext, aRect)
{
CGContextBeginPath(aContext);
if (_CGRectGetWidth(aRect) == _CGRectGetHeight(aRect))
CGContextAddArc(aContext, _CGRectGetMidX(aRect), _CGRectGetMidY(aRect), _CGRectGetWidth(aRect) / 2.0, 0.0, 2 * PI, YES);
else
{
var axis = _CGSizeMake(_CGRectGetWidth(aRect) / 2.0, _CGRectGetHeight(aRect) / 2.0),
center = _CGPointMake(_CGRectGetMinX(aRect) + axis.width, _CGRectGetMinY(aRect) + axis.height);
CGContextMoveToPoint(aContext, center.x, center.y - axis.height);
CGContextAddCurveToPoint(aContext, center.x + (KAPPA * axis.width), center.y - axis.height, center.x + axis.width, center.y - (KAPPA * axis.height), center.x + axis.width, center.y);
CGContextAddCurveToPoint(aContext, center.x + axis.width, center.y + (KAPPA * axis.height), center.x + (KAPPA * axis.width), center.y + axis.height, center.x, center.y + axis.height);
CGContextAddCurveToPoint(aContext, center.x - (KAPPA * axis.width), center.y + axis.height, center.x - axis.width, center.y + (KAPPA * axis.height), center.x - axis.width, center.y);
CGContextAddCurveToPoint(aContext, center.x - axis.width, center.y - (KAPPA * axis.height), center.x - (KAPPA * axis.width), center.y - axis.height, center.x, center.y - axis.height);
}
CGContextClosePath(aContext);
CGContextAddPath(aContext, CGPathWithEllipseInRect(aRect));
}
/*!
@@ -589,93 +572,13 @@ function CGContextSetStrokeColor(aContext, aColor)
*/
function CGContextFillRoundedRectangleInRect(aContext, aRect, aRadius, ne, se, sw, nw)
{
var xMin = _CGRectGetMinX(aRect),
xMax = _CGRectGetMaxX(aRect),
yMin = _CGRectGetMinY(aRect),
yMax = _CGRectGetMaxY(aRect);
CGContextBeginPath(aContext);
CGContextMoveToPoint(aContext, xMin + aRadius, yMin);
if (ne)
{
CGContextAddLineToPoint(aContext, xMax - aRadius, yMin);
CGContextAddCurveToPoint(aContext, xMax - aRadius, yMin, xMax, yMin, xMax, yMin + aRadius);
}
else
CGContextAddLineToPoint(aContext, xMax, yMin);
if (se)
{
CGContextAddLineToPoint(aContext, xMax, yMax - aRadius);
CGContextAddCurveToPoint(aContext, xMax, yMax - aRadius, xMax, yMax, xMax - aRadius, yMax);
}
else
CGContextAddLineToPoint(aContext, xMax, yMax);
if (sw)
{
CGContextAddLineToPoint(aContext, xMin + aRadius, yMax);
CGContextAddCurveToPoint(aContext, xMin + aRadius, yMax, xMin, yMax, xMin, yMax - aRadius);
}
else
CGContextAddLineToPoint(aContext, xMin, yMax);
if (nw)
{
CGContextAddLineToPoint(aContext, xMin, yMin + aRadius);
CGContextAddCurveToPoint(aContext, xMin, yMin + aRadius, xMin, yMin, xMin + aRadius, yMin);
} else
CGContextAddLineToPoint(aContext, xMin, yMin);
CGContextClosePath(aContext);
CGContextAddPath(aContext, CGPathWithRoundedRectangleInRect(aRect, aRadius, aRadius, ne, se, sw, nw));
CGContextFillPath(aContext);
}
function CGContextStrokeRoundedRectangleInRect(aContext, aRect, aRadius, ne, se, sw, nw)
{
var xMin = _CGRectGetMinX(aRect),
xMax = _CGRectGetMaxX(aRect),
yMin = _CGRectGetMinY(aRect),
yMax = _CGRectGetMaxY(aRect);
CGContextBeginPath(aContext);
CGContextMoveToPoint(aContext, xMin + aRadius, yMin);
if (ne)
{
CGContextAddLineToPoint(aContext, xMax - aRadius, yMin);
CGContextAddCurveToPoint(aContext, xMax - aRadius, yMin, xMax, yMin, xMax, yMin + aRadius);
}
else
CGContextAddLineToPoint(aContext, xMax, yMin);
if (se)
{
CGContextAddLineToPoint(aContext, xMax, yMax - aRadius);
CGContextAddCurveToPoint(aContext, xMax, yMax - aRadius, xMax, yMax, xMax - aRadius, yMax);
}
else
CGContextAddLineToPoint(aContext, xMax, yMax);
if (sw)
{
CGContextAddLineToPoint(aContext, xMin + aRadius, yMax);
CGContextAddCurveToPoint(aContext, xMin + aRadius, yMax, xMin, yMax, xMin, yMax - aRadius);
}
else
CGContextAddLineToPoint(aContext, xMin, yMax);
if (nw)
{
CGContextAddLineToPoint(aContext, xMin, yMin + aRadius);
CGContextAddCurveToPoint(aContext, xMin, yMin + aRadius, xMin, yMin, xMin + aRadius, yMin);
} else
CGContextAddLineToPoint(aContext, xMin, yMin);
CGContextClosePath(aContext);
CGContextAddPath(aContext, CGPathWithRoundedRectangleInRect(aRect, aRadius, aRadius, ne, se, sw, nw));
CGContextStrokePath(aContext);
}
+77 -4
View File
@@ -158,12 +158,15 @@ function CGPathAddPath(aPath, aTransform, anotherPath)
for (; i < count; ++i)
{
var element = anotherPath[i];
var element = anotherPath.elements[i];
aPath.elements[aPath.count++] = { type:element.type, points:element.point.slice() };
aPath.elements[aPath.count] = { type:element.type, x:element.x, y:element.y,
cpx:element.cpx, cpy:element.cpy,
radius:element.radius, startAngle:element.startAngle, endAngle:element.endAngle,
cp1x:element.cp1x, cp1y:element.cp1y, cp2x:element.cp2x, cp2y:element.cp2y,
points: element.points ? element.points.slice() : nil};
if (element.type == kCGPathElementAddArc || element.type == kCGPathElementAddArcToPoint)
aPath.elements[aPath.count - 1].radius = element.radius;
aPath.count++
}
aPath.current = anotherPath.current;
@@ -231,6 +234,76 @@ function CGPathMoveToPoint(aPath, aTransform, x, y)
aPath.elements[aPath.count++] = { type:kCGPathElementMoveToPoint, x:point.x, y:point.y };
}
function CGPathWithEllipseInRect(aRect)
{
var path = CGPathCreateMutable();
if (_CGRectGetWidth(aRect) == _CGRectGetHeight(aRect))
CGPathAddArc(path, nil, _CGRectGetMidX(aRect), _CGRectGetMidY(aRect), _CGRectGetWidth(aRect) / 2.0, 0.0, 2 * PI, YES);
else
{
var axis = _CGSizeMake(_CGRectGetWidth(aRect) / 2.0, _CGRectGetHeight(aRect) / 2.0),
center = _CGPointMake(_CGRectGetMinX(aRect) + axis.width, _CGRectGetMinY(aRect) + axis.height);
CGPathMoveToPoint(path, nil, center.x, center.y - axis.height);
CGPathAddCurveToPoint(path, nil, center.x + (KAPPA * axis.width), center.y - axis.height, center.x + axis.width, center.y - (KAPPA * axis.height), center.x + axis.width, center.y);
CGPathAddCurveToPoint(path, nil, center.x + axis.width, center.y + (KAPPA * axis.height), center.x + (KAPPA * axis.width), center.y + axis.height, center.x, center.y + axis.height);
CGPathAddCurveToPoint(path, nil, center.x - (KAPPA * axis.width), center.y + axis.height, center.x - axis.width, center.y + (KAPPA * axis.height), center.x - axis.width, center.y);
CGPathAddCurveToPoint(path, nil, center.x - axis.width, center.y - (KAPPA * axis.height), center.x - (KAPPA * axis.width), center.y - axis.height, center.x, center.y - axis.height);
}
CGPathCloseSubpath(path);
return path;
}
function CGPathWithRoundedRectangleInRect(aRect, xRadius, yRadius/*not currently supported*/, ne, se, sw, nw)
{
var path = CGPathCreateMutable(),
xMin = _CGRectGetMinX(aRect),
xMax = _CGRectGetMaxX(aRect),
yMin = _CGRectGetMinY(aRect),
yMax = _CGRectGetMaxY(aRect);
CGPathMoveToPoint(path, nil, xMin + xRadius, yMin);
if (ne)
{
CGPathAddLineToPoint(path, nil, xMax - xRadius, yMin);
CGPathAddCurveToPoint(path, nil, xMax - xRadius, yMin, xMax, yMin, xMax, yMin + xRadius);
}
else
CGPathAddLineToPoint(path, nil, xMax, yMin);
if (se)
{
CGPathAddLineToPoint(path, nil, xMax, yMax - xRadius);
CGPathAddCurveToPoint(path, nil, xMax, yMax - xRadius, xMax, yMax, xMax - xRadius, yMax);
}
else
CGPathAddLineToPoint(path, nil, xMax, yMax);
if (sw)
{
CGPathAddLineToPoint(path, nil, xMin + xRadius, yMax);
CGPathAddCurveToPoint(path, nil, xMin + xRadius, yMax, xMin, yMax, xMin, yMax - xRadius);
}
else
CGPathAddLineToPoint(path, nil, xMin, yMax);
if (nw)
{
CGPathAddLineToPoint(path, nil, xMin, yMin + xRadius);
CGPathAddCurveToPoint(path, nil, xMin, yMin + xRadius, xMin, yMin, xMin + xRadius, yMin);
} else
CGPathAddLineToPoint(path, nil, xMin, yMin);
CGPathCloseSubpath(path);
return path;
}
function CGPathCloseSubpath(aPath)
{
var count = aPath.count;