Skip to content

Commit

Permalink
PocketSVG init method now only takes in name of SVG file
Browse files Browse the repository at this point in the history
  • Loading branch information
arielelkin committed Sep 1, 2012
1 parent a2ea15f commit b5df597
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 46 deletions.
2 changes: 1 addition & 1 deletion PocketSVG.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
B0A9DBA615CA9B340095DFAF /* en */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = en; path = en.lproj/MainStoryboard_iPad.storyboard; sourceTree = "<group>"; };
B0A9DBA815CA9B340095DFAF /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = "<group>"; };
B0A9DBA915CA9B340095DFAF /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = "<group>"; };
B0A9DBBF15CA9F370095DFAF /* PocketSVG.h */ = {isa = PBXFileReference; fileEncoding = 4; path = PocketSVG.h; sourceTree = "<group>"; };
B0A9DBBF15CA9F370095DFAF /* PocketSVG.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PocketSVG.h; sourceTree = "<group>"; };
B0A9DBC015CA9F370095DFAF /* PocketSVG.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PocketSVG.m; sourceTree = "<group>"; };
B0A9DBC315CAA2380095DFAF /* QuartzCore.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = QuartzCore.framework; path = System/Library/Frameworks/QuartzCore.framework; sourceTree = SDKROOT; };
B0ABB61F15CC70B7001C9177 /* BezierCurve2-iPad.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = "BezierCurve2-iPad.svg"; sourceTree = "<group>"; };
Expand Down
4 changes: 1 addition & 3 deletions PocketSVG/PocketSVG.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

@interface PocketSVG : NSObject {
@private
CGRect viewBox;
float pathScale;
#ifdef TARGET_OS_IPHONE
UIBezierPath *bezier;
Expand All @@ -37,7 +36,6 @@
@property(nonatomic, readonly) NSBezierPath *bezier;
#endif

//- (id)initFromSVGPathNodeDAttr:(NSString *)attr rect:(CGRect)rect;
- (id)initFromSVGFileNamed:(NSString *)nameOfSVG rect:(CGRect)rect;
- (id)initFromSVGFileNamed:(NSString *)nameOfSVG;

@end
68 changes: 33 additions & 35 deletions PocketSVG/PocketSVG.m
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ - (void)appendSVGMCommand:(Token *)token;
- (void)appendSVGLCommand:(Token *)token;
- (void)appendSVGCCommand:(Token *)token;
- (void)appendSVGSCommand:(Token *)token;
- (CGPoint)bezierPoint:(CGPoint)svgPoint;

@end

Expand All @@ -85,8 +84,8 @@ @implementation PocketSVG
@synthesize bezier;


- (id)initFromSVGFileNamed:(NSString *)nameOfSVG rect:(CGRect)rect{
return [self initFromSVGPathNodeDAttr:[self parseSVGNamed:nameOfSVG] rect:rect];
- (id)initFromSVGFileNamed:(NSString *)nameOfSVG{
return [self initFromSVGPathNodeDAttr:[self parseSVGNamed:nameOfSVG]];
}

/********
Expand Down Expand Up @@ -138,12 +137,11 @@ -(NSString *)parseSVGNamed:(NSString *)nameOfSVG{
}


- (id)initFromSVGPathNodeDAttr:(NSString *)attr rect:(CGRect)rect
- (id)initFromSVGPathNodeDAttr:(NSString *)attr
{
self = [super init];
if (self) {
pathScale = 0;
viewBox = rect;
[self reset];
separatorSet = [NSCharacterSet characterSetWithCharactersInString:separatorCharString];
commandSet = [NSCharacterSet characterSetWithCharactersInString:commandCharString];
Expand Down Expand Up @@ -282,19 +280,6 @@ - (void)reset
validLastControlPoint = NO;
}

- (CGPoint)bezierPoint:(CGPoint)svgPoint
{
/*CGPoint newPoint;
CGFloat scaleX = viewBox.size.width / pathScale;
CGFloat scaleY = viewBox.size.height / pathScale;
newPoint.x = (svgPoint.x * scaleX) + viewBox.origin.x;
newPoint.y = (svgPoint.y * scaleY) + viewBox.origin.y;*/
return svgPoint;

}

- (void)appendSVGMCommand:(Token *)token
{
validLastControlPoint = NO;
Expand All @@ -309,14 +294,14 @@ - (void)appendSVGMCommand:(Token *)token
CGFloat y = [token parameter:index] + ([token command] == 'm' ? lastPoint.y : 0);
lastPoint = CGPointMake(x, y);
if (first) {
[bezier moveToPoint:[self bezierPoint:lastPoint]];
[bezier moveToPoint:lastPoint];
first = NO;
}
else {
#ifdef TARGET_OS_IPHONE
[bezier addLineToPoint:[self bezierPoint:lastPoint]];
[bezier addLineToPoint:lastPoint];
#else
[bezier lineToPoint:NSPointFromCGPoint([self bezierPoint:lastPoint])];
[bezier lineToPoint:NSPointFromCGPoint(lastPoint)];
#endif
}
index++;
Expand Down Expand Up @@ -360,9 +345,9 @@ - (void)appendSVGLCommand:(Token *)token
}
lastPoint = CGPointMake(x, y);
#ifdef TARGET_OS_IPHONE
[bezier addLineToPoint: [self bezierPoint: lastPoint]];
[bezier addLineToPoint:lastPoint];
#else
[bezier lineToPoint:NSPointFromCGPoint([self bezierPoint: lastPoint])];
[bezier lineToPoint:NSPointFromCGPoint(lastPoint)];
#endif
index++;
}
Expand All @@ -380,13 +365,13 @@ - (void)appendSVGCCommand:(Token *)token
CGFloat y = [token parameter:index++] + ([token command] == 'c' ? lastPoint.y : 0);
lastPoint = CGPointMake(x, y);
#ifdef TARGET_OS_IPHONE
[bezier addCurveToPoint:[self bezierPoint:lastPoint]
controlPoint1:[self bezierPoint:CGPointMake(x1,y1)]
controlPoint2:[self bezierPoint:CGPointMake(x2, y2)]];
[bezier addCurveToPoint:lastPoint
controlPoint1:CGPointMake(x1,y1)
controlPoint2:CGPointMake(x2, y2)];
#else
[bezier curveToPoint:NSPointFromCGPoint([self bezierPoint:lastPoint])
controlPoint1:NSPointFromCGPoint([self bezierPoint:CGPointMake(x1,y1)])
controlPoint2:NSPointFromCGPoint([self bezierPoint:CGPointMake(x2, y2)])];
[bezier curveToPoint:NSPointFromCGPoint(lastPoint)
controlPoint1:NSPointFromCGPoint(CGPointMake(x1,y1))
controlPoint2:NSPointFromCGPoint(CGPointMake(x2, y2)];
#endif
lastControlPoint = CGPointMake(x2, y2);
validLastControlPoint = YES;
Expand All @@ -411,13 +396,13 @@ - (void)appendSVGSCommand:(Token *)token
CGFloat y = [token parameter:index++] + ([token command] == 's' ? lastPoint.y : 0);
lastPoint = CGPointMake(x, y);
#ifdef TARGET_OS_IPHONE
[bezier addCurveToPoint:[self bezierPoint:lastPoint]
controlPoint1:[self bezierPoint:CGPointMake(x1,y1)]
controlPoint2:[self bezierPoint:CGPointMake(x2, y2)]];
[bezier addCurveToPoint:lastPoint
controlPoint1:CGPointMake(x1,y1)
controlPoint2:CGPointMake(x2, y2)];
#else
[bezier curveToPoint:NSPointFromCGPoint([self bezierPoint:lastPoint])
controlPoint1:NSPointFromCGPoint([self bezierPoint:CGPointMake(x1,y1)])
controlPoint2:NSPointFromCGPoint([self bezierPoint:CGPointMake(x2, y2)])];
[bezier curveToPoint:NSPointFromCGPoint(lastPoint)
controlPoint1:NSPointFromCGPoint(CGPointMake(x1,y1))
controlPoint2:NSPointFromCGPoint(CGPointMake(x2, y2)];
#endif
lastControlPoint = CGPointMake(x2, y2);
validLastControlPoint = YES;
Expand All @@ -427,4 +412,17 @@ - (void)appendSVGSCommand:(Token *)token
}
}

//- (CGPoint)bezierPoint:(CGPoint)svgPoint
//{
// CGPoint newPoint;
//
// CGFloat scaleX = viewBox.size.width / pathScale;
// CGFloat scaleY = viewBox.size.height / pathScale;
//
// newPoint.x = (svgPoint.x * scaleX) + viewBox.origin.x;
// newPoint.y = (svgPoint.y * scaleY) + viewBox.origin.y;
// return svgPoint;
//}


@end
21 changes: 14 additions & 7 deletions PocketSVG/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,29 @@ @implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];

//1: Create a PocketSVG object from your SVG file:
PocketSVG *myBezier = [[PocketSVG alloc] initFromSVGFileNamed:@"BezierCurve1-iPad"];

//Set the frame in which to draw our SVG:
CGRect frameRect = CGRectMake(0, 0, 1024, 768);

//Create an SvgToBezier object:
PocketSVG *myBezier = [[PocketSVG alloc] initFromSVGFileNamed:@"BezierCurve3-iPad" rect:frameRect];

//2: Its bezier property is the corresponding UIBezierPath:
UIBezierPath *myPath = myBezier.bezier;



//3: To display it on screen, create a CAShapeLayer and set
//the CGPath property of the above UIBezierPath as its
//path.
CAShapeLayer *myShapeLayer = [CAShapeLayer layer];
myShapeLayer.path = myPath.CGPath;


//4: Fiddle with it using CAShapeLayer's properties:
myShapeLayer.strokeColor = [[UIColor redColor] CGColor];
myShapeLayer.lineWidth = 4;

myShapeLayer.fillColor = [[UIColor clearColor] CGColor];


//5: Display it!
[self.view.layer addSublayer:myShapeLayer];


Expand Down

0 comments on commit b5df597

Please sign in to comment.