Skip to content

Instantly share code, notes, and snippets.

@turbobabr
Created July 30, 2014 01:53
Show Gist options
  • Save turbobabr/dab1ffa945ca29d68aa5 to your computer and use it in GitHub Desktop.
Save turbobabr/dab1ffa945ca29d68aa5 to your computer and use it in GitHub Desktop.
+(NSString*)convertNSBezierPathToJSON:(NSBezierPath*)path {
NSMutableArray* newPoints=[NSMutableArray array];
NSPoint points[3];
NSInteger numElements = [path elementCount];
for (int i = 0; i < numElements; i++)
{
NSBezierPathElement element=[path elementAtIndex:i associatedPoints:points];
switch (element)
{
case NSMoveToBezierPathElement:
{
NSDictionary* pt= @{
@"t" : [NSNumber numberWithInteger:element],
@"p" : @[[NSNumber numberWithFloat:points[0].x],[NSNumber numberWithFloat:points[0].y]]
};
[newPoints addObject:pt];
}
break;
case NSLineToBezierPathElement:
{
NSDictionary* pt= @{
@"t" : [NSNumber numberWithInteger:element],
@"p" : @[[NSNumber numberWithFloat:points[0].x],[NSNumber numberWithFloat:points[0].y]]
};
[newPoints addObject:pt];
}
break;
case NSCurveToBezierPathElement:
{
NSDictionary* pt= @{
@"t" : [NSNumber numberWithInteger:element],
@"p" : @[[NSNumber numberWithFloat:points[0].x],[NSNumber numberWithFloat:points[0].y],[NSNumber numberWithFloat:points[1].x],[NSNumber numberWithFloat:points[1].y],[NSNumber numberWithFloat:points[2].x],[NSNumber numberWithFloat:points[2].y]]
};
[newPoints addObject:pt];
}
break;
case NSClosePathBezierPathElement:
{
NSDictionary* pt= @{ @"t" : [NSNumber numberWithInteger:element] };
[newPoints addObject:pt];
}
break;
}
// if(element==NSClosePathBezierPathElement) break;
}
NSDictionary* pathObject = @{
@"bounds" : @{
@"x" : [NSNumber numberWithFloat:path.bounds.origin.x],
@"y" : [NSNumber numberWithFloat:path.bounds.origin.y],
@"w" : [NSNumber numberWithFloat:path.bounds.size.width],
@"h" : [NSNumber numberWithFloat:path.bounds.size.height]
},
@"cpbounds" : @{
@"x" : [NSNumber numberWithFloat:path.controlPointBounds.origin.x],
@"y" : [NSNumber numberWithFloat:path.controlPointBounds.origin.y],
@"w" : [NSNumber numberWithFloat:path.controlPointBounds.size.width],
@"h" : [NSNumber numberWithFloat:path.controlPointBounds.size.height]
},
@"points" : newPoints
};
NSString* json = nil;
NSError* error = nil;
NSData *data = [NSJSONSerialization dataWithJSONObject:pathObject options:0 error:&error];
json = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return json;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment