Skip to content

Commit

Permalink
Clip-paths support
Browse files Browse the repository at this point in the history
  • Loading branch information
djphoenix committed May 14, 2015
1 parent 6747fb2 commit 4c15691
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions pxSVG/pxSVGObject.h
Expand Up @@ -17,6 +17,7 @@
@property NSArray *animations;
@property UIColor *fillColor;
@property NSString *fillDef;
@property NSString *clipDef;
@property UIColor *strokeColor;
@property CGFloat strokeWidth;
@property CGFloat opacity;
Expand Down
5 changes: 5 additions & 0 deletions pxSVG/pxSVGObject.m
Expand Up @@ -108,6 +108,11 @@ - (void)loadAttributes:(NSDictionary *)attributes
u = [[u substringWithRange:NSMakeRange(3, u.length-4)] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\n\r\t ()#"]];
self.fillDef = u;
} else self.fillColor = [self.class colorWithSVGColor:[ma objectForKey:@"fill"]];
if ([ma objectForKey:@"clip-path"]) {
NSString *u = [[ma objectForKey:@"clip-path"] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
u = [[u substringWithRange:NSMakeRange(3, u.length-4)] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"\n\r\t ()#"]];
self.clipDef = u;
}
self.strokeColor = [self.class colorWithSVGColor:[ma objectForKey:@"stroke"]];
self.strokeWidth = [ma objectForKey:@"stroke-width"]?[[ma objectForKey:@"stroke-width"] doubleValue]:NAN;
self.transform = [self.class transformFromString:[ma objectForKey:@"transform"]];
Expand Down
25 changes: 25 additions & 0 deletions pxSVG/pxSVGRenderPath.m
Expand Up @@ -183,6 +183,7 @@ - (pxSVGObject*)reuseObjectWithAttributes:(NSDictionary*)attributes
obj.fillOpacity = oobj.fillOpacity;
obj.fillColor = oobj.fillColor;
obj.fillDef = oobj.fillDef;
obj.clipDef = oobj.clipDef;
if ([attributes objectForKey:@"fill"]) {
CGFloat a = obj.fillOpacity;
if ([[attributes objectForKey:@"fill"] hasPrefix:@"url("]) {
Expand Down Expand Up @@ -286,6 +287,7 @@ - (pxSVGPattern*)parsePattern:(pxXMLNode*)node
p.fillOpacity = op.fillOpacity;
p.fillColor = op.fillColor;
p.fillDef = op.fillDef;
p.clipDef = op.clipDef;
p.strokeColor = op.strokeColor;
p.strokeWidth = op.strokeWidth;
p.subnodes = op.subnodes;
Expand Down Expand Up @@ -389,6 +391,7 @@ - (pxSVGObject*)parseObject:(pxXMLNode*)node inheritAttributes:(pxSVGObject*)inh
obj.strokeColor = inherit?inherit.strokeColor:nil;
if (obj.id) [self.defCache setObject:obj forKey:obj.id];
if (obj.fillDef) [self findDef:obj.fillDef];
if (obj.clipDef) [self findDef:obj.clipDef];
if (node.childNodes.count) {
NSMutableArray *subnodes = [NSMutableArray new];
for (pxXMLNode *n in node.childNodes) {
Expand All @@ -401,6 +404,19 @@ - (pxSVGObject*)parseObject:(pxXMLNode*)node inheritAttributes:(pxSVGObject*)inh
}
return obj;
}
- (UIBezierPath*)mergePath:(pxSVGObject*)node
{
UIBezierPath *bp = [UIBezierPath new];
if ([node respondsToSelector:@selector(d)]) {
[bp appendPath:[(id)node d]];
}
if ([node respondsToSelector:@selector(subnodes)]) {
for (pxSVGObject *o in [(id)node subnodes]) {
[bp appendPath:[self mergePath:o]];
}
}
return bp;
}
- (CALayer *)makeLayerWithNode:(pxSVGObject*)node rootBounds:(CGRect)bounds;
{
CALayer *l;
Expand Down Expand Up @@ -446,6 +462,15 @@ - (CALayer *)makeLayerWithNode:(pxSVGObject*)node rootBounds:(CGRect)bounds;
} else {
l = [CALayer new];
}
if (node.clipDef) {
id def = [self findDef:node.clipDef];
if ([def isKindOfClass:[pxSVGObject class]]) {
CAShapeLayer *ml = [CAShapeLayer new];
ml.frame = (CGRect){CGPointZero,bounds.size};
ml.path = [self mergePath:def].CGPath;
l.mask = ml;
}
}
l.frame = (CGRect){{-bounds.origin.x,-bounds.origin.y},bounds.size};
CATransform3D tr = node.transform;
tr = CATransform3DConcat(CATransform3DMakeTranslation( bounds.size.width/2, bounds.size.height/2, 0), tr);
Expand Down

0 comments on commit 4c15691

Please sign in to comment.