diff --git a/IJSVG.m b/IJSVG.m index b8b3039..fcdbead 100644 --- a/IJSVG.m +++ b/IJSVG.m @@ -81,13 +81,17 @@ static NSColor * _baseColor = nil; - (id)initWithFilePathURL:(NSURL *)aURL delegate:(id)delegate { -// if( [IJSVGCache enabled] ) -// { -// IJSVG * svg = nil; -// if( ( svg = [IJSVGCache cachedSVGForFileURL:aURL] ) != nil ) -// return [svg retain]; -// } -// + if( [IJSVGCache enabled] ) + { + IJSVG * svg = nil; + if( ( svg = [IJSVGCache cachedSVGForFileURL:aURL] ) != nil ) +#ifndef __clang_analyzer__ + return [svg retain]; +#else + {} +#endif + } + if( ( self = [super init] ) != nil ) { _delegate = delegate; @@ -127,9 +131,30 @@ static NSColor * _baseColor = nil; - (void)drawInRect:(NSRect)rect { + + // prep for draw... [self _beginDraw:rect]; + + // setup the transforms and scale on the main context + CGContextRef ref = [[NSGraphicsContext currentContext] graphicsPort]; + CGContextSaveGState(ref); + + // scale the whole drawing context, but first, we need + // to translate the context so its centered + CGFloat tX = round(rect.size.width/2-(_group.size.width/2)*_scale); + CGFloat tY = round(rect.size.height/2-(_group.size.height/2)*_scale); + CGContextTranslateCTM( ref, tX, tY ); + CGContextScaleCTM( ref, _scale, _scale ); + + // apply standard defaults + [self _applyDefaults:ref + node:_group]; + + // begin draw [self _drawGroup:_group rect:rect]; + + CGContextRestoreGState(ref); } - (void)_recursiveColors:(IJSVGGroup *)group @@ -194,6 +219,8 @@ static NSColor * _baseColor = nil; [self _drawGroup:child rect:rect]; } + + // restore the context CGContextRestoreGState(context); } @@ -225,14 +252,7 @@ static NSColor * _baseColor = nil; CGContextRef ref = [[NSGraphicsContext currentContext] graphicsPort]; CGContextSaveGState(ref); - // scale the whole drawing context, but first, we need - // to translate the context so its centered - CGFloat tX = round(rect.size.width/2-(_group.size.width/2)*_scale); - CGFloat tY = round(rect.size.height/2-(_group.size.height/2)*_scale); - CGContextTranslateCTM( ref, tX, tY ); - CGContextScaleCTM( ref, _scale, _scale ); - - // apply standard defaults + // there could be transforms per path [self _applyDefaults:ref node:path]; @@ -240,8 +260,6 @@ static NSColor * _baseColor = nil; // use the base if its not set if( path.fillColor == nil && _baseColor != nil ) [_baseColor set]; - else - [path.fillColor set]; // fill the path if( path.fillGradient != nil ) @@ -261,7 +279,11 @@ static NSColor * _baseColor = nil; } } else { // just use the color instead - [path.path fill]; + if( path.fillColor != nil ) + { + [path.fillColor set]; + [path.path fill]; + } } // any stroke? diff --git a/IJSVGCache.m b/IJSVGCache.m index a70608a..932e233 100644 --- a/IJSVGCache.m +++ b/IJSVGCache.m @@ -10,7 +10,7 @@ @implementation IJSVGCache -// 5mb +// 5MB #define MAX_CACHE_SIZE 5000000 static NSCache * _cache = nil; @@ -37,6 +37,14 @@ static BOOL _enabled = YES; NSFileManager * fm = [NSFileManager defaultManager]; NSDictionary * atts = [fm attributesOfItemAtPath:[aURL path] error:nil]; + + // cache is nil ... for some reason..? + if( _cache == nil ) + { + _cache = [[NSCache alloc] init]; + [_cache setTotalCostLimit:MAX_CACHE_SIZE]; + } + [_cache setObject:svg forKey:aURL cost:[atts fileSize]+sizeof(svg)]; diff --git a/IJSVGExample/IJSVGExample.xcodeproj/project.pbxproj b/IJSVGExample/IJSVGExample.xcodeproj/project.pbxproj index aef58a0..48e8885 100644 --- a/IJSVGExample/IJSVGExample.xcodeproj/project.pbxproj +++ b/IJSVGExample/IJSVGExample.xcodeproj/project.pbxproj @@ -42,6 +42,8 @@ 59A0E79D19B72EA7001BA157 /* IJSVGCommandArc.m in Sources */ = {isa = PBXBuildFile; fileRef = 59A0E79C19B72EA7001BA157 /* IJSVGCommandArc.m */; }; 59A0E7A219B7333C001BA157 /* IJSVGLinearGradient.m in Sources */ = {isa = PBXBuildFile; fileRef = 59A0E7A119B7333C001BA157 /* IJSVGLinearGradient.m */; }; 59A0E7A519B7334F001BA157 /* IJSVGDef.m in Sources */ = {isa = PBXBuildFile; fileRef = 59A0E7A419B7334F001BA157 /* IJSVGDef.m */; }; + 59B93C6D19B7D1840063E823 /* paperplane.svg in Resources */ = {isa = PBXBuildFile; fileRef = 59B93C6C19B7D1840063E823 /* paperplane.svg */; }; + 59B93C6F19B7D32C0063E823 /* products.svg in Resources */ = {isa = PBXBuildFile; fileRef = 59B93C6E19B7D32C0063E823 /* products.svg */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -124,6 +126,8 @@ 59A0E7A119B7333C001BA157 /* IJSVGLinearGradient.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = IJSVGLinearGradient.m; path = ../../IJSVGLinearGradient.m; sourceTree = ""; }; 59A0E7A319B7334F001BA157 /* IJSVGDef.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = IJSVGDef.h; path = ../../IJSVGDef.h; sourceTree = ""; }; 59A0E7A419B7334F001BA157 /* IJSVGDef.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = IJSVGDef.m; path = ../../IJSVGDef.m; sourceTree = ""; }; + 59B93C6C19B7D1840063E823 /* paperplane.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = paperplane.svg; sourceTree = ""; }; + 59B93C6E19B7D32C0063E823 /* products.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.xml; path = products.svg; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -180,6 +184,8 @@ 5956657A19B62F4600D805FF /* Supporting Files */ = { isa = PBXGroup; children = ( + 59B93C6E19B7D32C0063E823 /* products.svg */, + 59B93C6C19B7D1840063E823 /* paperplane.svg */, 595665DD19B6309C00D805FF /* test.svg */, 5956657B19B62F4600D805FF /* Info.plist */, 5956657C19B62F4600D805FF /* main.m */, @@ -347,8 +353,10 @@ isa = PBXResourcesBuildPhase; buildActionMask = 2147483647; files = ( + 59B93C6D19B7D1840063E823 /* paperplane.svg in Resources */, 5956658219B62F4600D805FF /* Images.xcassets in Resources */, 595665DE19B6309C00D805FF /* test.svg in Resources */, + 59B93C6F19B7D32C0063E823 /* products.svg in Resources */, 5956658519B62F4600D805FF /* MainMenu.xib in Resources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/IJSVGExample/IJSVGExample.xcodeproj/project.xcworkspace/xcuserdata/curtishard.xcuserdatad/UserInterfaceState.xcuserstate b/IJSVGExample/IJSVGExample.xcodeproj/project.xcworkspace/xcuserdata/curtishard.xcuserdatad/UserInterfaceState.xcuserstate index f16786d..fa7e554 100644 Binary files a/IJSVGExample/IJSVGExample.xcodeproj/project.xcworkspace/xcuserdata/curtishard.xcuserdatad/UserInterfaceState.xcuserstate and b/IJSVGExample/IJSVGExample.xcodeproj/project.xcworkspace/xcuserdata/curtishard.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/IJSVGExample/IJSVGExample/SVGView.m b/IJSVGExample/IJSVGExample/SVGView.m index 1b0fd83..ea54627 100644 --- a/IJSVGExample/IJSVGExample/SVGView.m +++ b/IJSVGExample/IJSVGExample/SVGView.m @@ -20,7 +20,7 @@ { if( ( self = [super initWithCoder:aDecoder] ) != nil ) { - svg = [[IJSVG svgNamed:@"test.svg"] retain]; + svg = [[IJSVG svgNamed:@"paperplane.svg"] retain]; } return self; } diff --git a/IJSVGExample/IJSVGExample/paperplane.svg b/IJSVGExample/IJSVGExample/paperplane.svg new file mode 100644 index 0000000..a4f73c7 --- /dev/null +++ b/IJSVGExample/IJSVGExample/paperplane.svg @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/IJSVGExample/IJSVGExample/products.svg b/IJSVGExample/IJSVGExample/products.svg new file mode 100644 index 0000000..ea7eec9 --- /dev/null +++ b/IJSVGExample/IJSVGExample/products.svg @@ -0,0 +1,33 @@ + + + + IPAD 2 + Created with Sketch. + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/IJSVGParser.m b/IJSVGParser.m index 6052306..08d71df 100644 --- a/IJSVGParser.m +++ b/IJSVGParser.m @@ -251,8 +251,16 @@ [self _parseElementForCommonAttributes:element node:group]; [parentGroup addChild:group]; + + // could be gradients within + // the group's aswell, so work those out + [self _parseDef:element + intoNode:group]; + + // recursively parse blocks [self _parseBlock:element intoGroup:group]; + continue; } else if( [subName isEqualToString:@"path"] ) {