Skip to content

Commit

Permalink
Added support for clipPaths
Browse files Browse the repository at this point in the history
  • Loading branch information
curthard89 committed Sep 4, 2014
1 parent b7652e4 commit d70f10b
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 32 deletions.
97 changes: 82 additions & 15 deletions IJSVG.m
Original file line number Diff line number Diff line change
Expand Up @@ -207,21 +207,86 @@ - (void)_drawGroup:(IJSVGGroup *)group
[self _applyDefaults:context
node:group];

// it could be a group or a path
for( id child in [group children] )
dispatch_block_t drawBlock = ^{
// it could be a group or a path
for( id child in [group children] )
{
if( [child isKindOfClass:[IJSVGPath class]] )
{
IJSVGPath * p = (IJSVGPath *)child;
if( p.clipPath != nil )
{
for( id clip in p.clipPath.children )
{
if( [clip isKindOfClass:[IJSVGGroup class]] )
[self _drawGroup:clip
rect:rect];
else {

// there is a clip path, save the context
CGContextSaveGState( context );

// add the clip
IJSVGPath * p = (IJSVGPath *)clip;
[p.path addClip];

// draw the path
[self _drawPath:child
rect:rect];

// restore the context
CGContextRestoreGState( context );
}
}
} else {
// as its just a path, we can happily
// just draw it in the current context
[self _drawPath:child
rect:rect];
}
} else if( [child isKindOfClass:[IJSVGGroup class]] ) {

// if its a group, we recursively call this method
// to generate the paths required
[self _drawGroup:child
rect:rect];
}
}

};

// group clipping
if( group.clipPath != nil )
{
if( [child isKindOfClass:[IJSVGPath class]] )
// as its just a path, we can happily
// just draw it in the current context
[self _drawPath:child
rect:rect];
else if( [child isKindOfClass:[IJSVGGroup class]] )

// if its a group, we recursively call this method
// to generate the paths required
[self _drawGroup:child
rect:rect];
}

// find the clipped children
for( id child in group.clipPath.children )
{
// if its a group, run this again
if( [child isKindOfClass:[IJSVGGroup class]] )
[self _drawGroup:child
rect:rect];
else {

// save the context state
CGContextSaveGState( context );

// find the path
IJSVGPath * p = (IJSVGPath *)child;

// clip the context
[p.path addClip];

// draw the paths
drawBlock();

// restore again
CGContextRestoreGState( context );
}
}
} else
// just draw the block
drawBlock();

// restore the context
CGContextRestoreGState(context);
Expand Down Expand Up @@ -289,14 +354,16 @@ - (void)_drawPath:(IJSVGPath *)path
[path.fillColor set];
[path.path fill];
} else if( _baseColor != nil ) {

// is there a base color?
// this is basically used whenever no color
// is set, its also set via [IJSVG setBaseColor],
// this must be defined!

[_baseColor set];
[path.path fill];
} else {
[path.path fill];
}
}

Expand Down
10 changes: 10 additions & 0 deletions IJSVGExample/IJSVGExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
5910114B19B739B7009F8A35 /* IJSVGStyle.m in Sources */ = {isa = PBXBuildFile; fileRef = 5910114A19B739B7009F8A35 /* IJSVGStyle.m */; };
5910114E19B75C9C009F8A35 /* IJSVGGradient.m in Sources */ = {isa = PBXBuildFile; fileRef = 5910114D19B75C9C009F8A35 /* IJSVGGradient.m */; };
5910115119B75E44009F8A35 /* IJSVGRadialGradient.m in Sources */ = {isa = PBXBuildFile; fileRef = 5910115019B75E44009F8A35 /* IJSVGRadialGradient.m */; };
59459CEC19B906FE00CE493B /* clipped.svg in Resources */ = {isa = PBXBuildFile; fileRef = 59459CEB19B906FE00CE493B /* clipped.svg */; };
59459CEF19B9074F00CE493B /* SVGExampleView4.m in Sources */ = {isa = PBXBuildFile; fileRef = 59459CEE19B9074F00CE493B /* SVGExampleView4.m */; };
5956657D19B62F4600D805FF /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 5956657C19B62F4600D805FF /* main.m */; };
5956658019B62F4600D805FF /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 5956657F19B62F4600D805FF /* AppDelegate.m */; };
5956658219B62F4600D805FF /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5956658119B62F4600D805FF /* Images.xcassets */; };
Expand Down Expand Up @@ -67,6 +69,9 @@
5910114D19B75C9C009F8A35 /* IJSVGGradient.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = IJSVGGradient.m; path = ../../IJSVGGradient.m; sourceTree = "<group>"; };
5910114F19B75E44009F8A35 /* IJSVGRadialGradient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = IJSVGRadialGradient.h; path = ../../IJSVGRadialGradient.h; sourceTree = "<group>"; };
5910115019B75E44009F8A35 /* IJSVGRadialGradient.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = IJSVGRadialGradient.m; path = ../../IJSVGRadialGradient.m; sourceTree = "<group>"; };
59459CEB19B906FE00CE493B /* clipped.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = clipped.svg; sourceTree = "<group>"; };
59459CED19B9074F00CE493B /* SVGExampleView4.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SVGExampleView4.h; sourceTree = "<group>"; };
59459CEE19B9074F00CE493B /* SVGExampleView4.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SVGExampleView4.m; sourceTree = "<group>"; };
5956657719B62F4600D805FF /* IJSVGExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = IJSVGExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
5956657B19B62F4600D805FF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
5956657C19B62F4600D805FF /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -194,13 +199,16 @@
59A11E6E19B89D2000E44498 /* SVGExampleView2.m */,
59A11E7019B89D6600E44498 /* SVGExampleView3.h */,
59A11E7119B89D6600E44498 /* SVGExampleView3.m */,
59459CED19B9074F00CE493B /* SVGExampleView4.h */,
59459CEE19B9074F00CE493B /* SVGExampleView4.m */,
);
path = IJSVGExample;
sourceTree = "<group>";
};
5956657A19B62F4600D805FF /* Supporting Files */ = {
isa = PBXGroup;
children = (
59459CEB19B906FE00CE493B /* clipped.svg */,
59F799E119B880CE00096CB7 /* htc_one.svg */,
59B93C6E19B7D32C0063E823 /* products.svg */,
59B93C6C19B7D1840063E823 /* paperplane.svg */,
Expand Down Expand Up @@ -377,6 +385,7 @@
59F799E219B880CE00096CB7 /* htc_one.svg in Resources */,
59B93C6F19B7D32C0063E823 /* products.svg in Resources */,
5956658519B62F4600D805FF /* MainMenu.xib in Resources */,
59459CEC19B906FE00CE493B /* clipped.svg in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -427,6 +436,7 @@
5910114E19B75C9C009F8A35 /* IJSVGGradient.m in Sources */,
59A11E6F19B89D2000E44498 /* SVGExampleView2.m in Sources */,
595665CA19B6302600D805FF /* IJSVGColor.m in Sources */,
59459CEF19B9074F00CE493B /* SVGExampleView4.m in Sources */,
595665DB19B6302600D805FF /* IJSVGUtils.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Binary file not shown.
8 changes: 6 additions & 2 deletions IJSVGExample/IJSVGExample/Base.lproj/MainMenu.xib
Original file line number Diff line number Diff line change
Expand Up @@ -689,8 +689,12 @@
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" widthSizable="YES" flexibleMaxX="YES" flexibleMinY="YES"/>
</customView>
<customView id="bHV-ou-brV" customClass="SVGExampleView3">
<rect key="frame" x="0.0" y="0.0" width="631" height="238"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<rect key="frame" x="0.0" y="0.0" width="310" height="238"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxX="YES" heightSizable="YES"/>
</customView>
<customView id="osq-NY-OyC" customClass="SVGExampleView4">
<rect key="frame" x="309" y="0.0" width="322" height="238"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" widthSizable="YES" heightSizable="YES"/>
</customView>
</subviews>
</view>
Expand Down
14 changes: 14 additions & 0 deletions IJSVGExample/IJSVGExample/SVGExampleView4.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// SVGExampleView4.h
// IJSVGExample
//
// Created by Curtis Hard on 04/09/2014.
// Copyright (c) 2014 Curtis Hard. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "SVGView.h"

@interface SVGExampleView4 : SVGView

@end
18 changes: 18 additions & 0 deletions IJSVGExample/IJSVGExample/SVGExampleView4.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// SVGExampleView4.m
// IJSVGExample
//
// Created by Curtis Hard on 04/09/2014.
// Copyright (c) 2014 Curtis Hard. All rights reserved.
//

#import "SVGExampleView4.h"

@implementation SVGExampleView4

- (IJSVG *)svg
{
return [[IJSVG svgNamed:@"clipped"] retain];
}

@end
15 changes: 15 additions & 0 deletions IJSVGExample/IJSVGExample/clipped.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions IJSVGNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

@class IJSVGDef;
@class IJSVGGradient;
@class IJSVGGroup;

typedef NS_OPTIONS( NSInteger, IJSVGNodeType ) {
IJSVGNodeTypeGroup,
Expand All @@ -25,6 +26,7 @@ typedef NS_OPTIONS( NSInteger, IJSVGNodeType ) {
IJSVGNodeTypeUse,
IJSVGNodeTypeLinearGradient,
IJSVGNodeTypeRadialGradient,
IJSVGNodeTypeClipPath,
IJSVGNodeTypeNotFound
};

Expand Down Expand Up @@ -59,6 +61,7 @@ static CGFloat IJSVGInheritedFloatValue = -99.9999991;
NSString * identifier;

IJSVGNode * parentNode;
IJSVGGroup * clipPath;
NSArray * transforms;

IJSVGWindingRule windingRule;
Expand All @@ -81,6 +84,7 @@ static CGFloat IJSVGInheritedFloatValue = -99.9999991;
@property ( nonatomic, retain ) NSColor * strokeColor;
@property ( nonatomic, copy ) NSString * identifier;
@property ( nonatomic, assign ) IJSVGNode * parentNode;
@property ( nonatomic, assign ) IJSVGGroup * clipPath;
@property ( nonatomic, assign ) IJSVGWindingRule windingRule;
@property ( nonatomic, retain ) NSArray * transforms;
@property ( nonatomic, retain ) IJSVGDef * def;
Expand Down
4 changes: 4 additions & 0 deletions IJSVGNode.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ @implementation IJSVGNode
@synthesize windingRule;
@synthesize def;
@synthesize fillGradient;
@synthesize clipPath;

- (void)dealloc
{
Expand Down Expand Up @@ -69,6 +70,8 @@ + (IJSVGNodeType)typeForString:(NSString *)string
return IJSVGNodeTypeLinearGradient;
if( [string isEqualToString:@"radialgradient"] )
return IJSVGNodeTypeRadialGradient;
if( [string isEqualToString:@"clippath"] )
return IJSVGNodeTypeClipPath;
return IJSVGNodeTypeNotFound;
}

Expand All @@ -95,6 +98,7 @@ - (id)copyWithZone:(NSZone *)zone

node.fillColor = self.fillColor;
node.strokeColor = self.strokeColor;
node.clipPath = self.clipPath;

node.opacity = self.opacity;
node.strokeWidth = self.strokeWidth;
Expand Down
Loading

0 comments on commit d70f10b

Please sign in to comment.