Skip to content

Commit

Permalink
Most SVG tags parse support
Browse files Browse the repository at this point in the history
  • Loading branch information
Yury Popov authored and Yury Popov committed May 12, 2015
1 parent 1dc3293 commit 71cec95
Show file tree
Hide file tree
Showing 8 changed files with 432 additions and 1 deletion.
28 changes: 28 additions & 0 deletions pxSVG.xcodeproj/project.pbxproj
Expand Up @@ -90,6 +90,9 @@
D07DB4FE1B026FC1004733DE /* no_sympathies.svg in Resources */ = {isa = PBXBuildFile; fileRef = D07DB4F31B026FC1004733DE /* no_sympathies.svg */; };
D07DB4FF1B026FC1004733DE /* share.svg in Resources */ = {isa = PBXBuildFile; fileRef = D07DB4F41B026FC1004733DE /* share.svg */; };
D07DB5001B026FC1004733DE /* support.svg in Resources */ = {isa = PBXBuildFile; fileRef = D07DB4F51B026FC1004733DE /* support.svg */; };
D0DCC4D11B0281F700586BF3 /* pxSVGObject.m in Sources */ = {isa = PBXBuildFile; fileRef = D0DCC4D01B0281F700586BF3 /* pxSVGObject.m */; };
D0DCC4D41B02827E00586BF3 /* pxSVGGroup.m in Sources */ = {isa = PBXBuildFile; fileRef = D0DCC4D31B02827E00586BF3 /* pxSVGGroup.m */; };
D0DCC4D71B0284B200586BF3 /* pxSVGPath.m in Sources */ = {isa = PBXBuildFile; fileRef = D0DCC4D61B0284B200586BF3 /* pxSVGPath.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -208,6 +211,12 @@
D07DB4F31B026FC1004733DE /* no_sympathies.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = no_sympathies.svg; sourceTree = "<group>"; };
D07DB4F41B026FC1004733DE /* share.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = share.svg; sourceTree = "<group>"; };
D07DB4F51B026FC1004733DE /* support.svg */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = support.svg; sourceTree = "<group>"; };
D0DCC4CF1B0281F700586BF3 /* pxSVGObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = pxSVGObject.h; sourceTree = "<group>"; };
D0DCC4D01B0281F700586BF3 /* pxSVGObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = pxSVGObject.m; sourceTree = "<group>"; };
D0DCC4D21B02827E00586BF3 /* pxSVGGroup.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = pxSVGGroup.h; sourceTree = "<group>"; };
D0DCC4D31B02827E00586BF3 /* pxSVGGroup.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = pxSVGGroup.m; sourceTree = "<group>"; };
D0DCC4D51B0284B200586BF3 /* pxSVGPath.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = pxSVGPath.h; sourceTree = "<group>"; };
D0DCC4D61B0284B200586BF3 /* pxSVGPath.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = pxSVGPath.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -342,6 +351,7 @@
D07DB4BA1B024A45004733DE /* pxSVG */ = {
isa = PBXGroup;
children = (
D0DCC4CE1B0281DA00586BF3 /* SVG */,
D07DB4DC1B025350004733DE /* Parsers */,
D07DB4D31B024A91004733DE /* Views */,
D07DB4BB1B024A45004733DE /* pxSVG.h */,
Expand Down Expand Up @@ -373,6 +383,19 @@
name = Parsers;
sourceTree = "<group>";
};
D0DCC4CE1B0281DA00586BF3 /* SVG */ = {
isa = PBXGroup;
children = (
D0DCC4CF1B0281F700586BF3 /* pxSVGObject.h */,
D0DCC4D01B0281F700586BF3 /* pxSVGObject.m */,
D0DCC4D21B02827E00586BF3 /* pxSVGGroup.h */,
D0DCC4D31B02827E00586BF3 /* pxSVGGroup.m */,
D0DCC4D51B0284B200586BF3 /* pxSVGPath.h */,
D0DCC4D61B0284B200586BF3 /* pxSVGPath.m */,
);
name = SVG;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -546,6 +569,9 @@
D07DB4D61B024AAA004733DE /* pxSVGLayer.m in Sources */,
D07DB4EA1B026BF8004733DE /* pxSVGRenderPath.m in Sources */,
D07DB4E41B026959004733DE /* pxXMLNode.m in Sources */,
D0DCC4D71B0284B200586BF3 /* pxSVGPath.m in Sources */,
D0DCC4D11B0281F700586BF3 /* pxSVGObject.m in Sources */,
D0DCC4D41B02827E00586BF3 /* pxSVGGroup.m in Sources */,
D07DB4D91B024AC2004733DE /* pxSVGView.m in Sources */,
D07DB4DF1B025366004733DE /* pxSVGImage.m in Sources */,
);
Expand Down Expand Up @@ -707,6 +733,7 @@
D07DB4381B024475004733DE /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
D07DB4CA1B024A45004733DE /* Build configuration list for PBXNativeTarget "pxSVG" */ = {
isa = XCConfigurationList;
Expand All @@ -715,6 +742,7 @@
D07DB4CC1B024A45004733DE /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
Expand Down
13 changes: 13 additions & 0 deletions pxSVG/pxSVGGroup.h
@@ -0,0 +1,13 @@
//
// pxSVGGroup.h
// pxSVG
//
// Created by Yury Popov on 12.05.15.
// Copyright (c) 2015 PhoeniX. All rights reserved.
//

#import "pxSVGObject.h"

@interface pxSVGGroup : pxSVGObject
@property NSArray *subnodes;
@end
13 changes: 13 additions & 0 deletions pxSVG/pxSVGGroup.m
@@ -0,0 +1,13 @@
//
// pxSVGGroup.m
// pxSVG
//
// Created by Yury Popov on 12.05.15.
// Copyright (c) 2015 PhoeniX. All rights reserved.
//

#import "pxSVGGroup.h"

@implementation pxSVGGroup

@end
21 changes: 21 additions & 0 deletions pxSVG/pxSVGObject.h
@@ -0,0 +1,21 @@
//
// pxSVGObject.h
// pxSVG
//
// Created by Yury Popov on 12.05.15.
// Copyright (c) 2015 PhoeniX. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface pxSVGObject : NSObject
- (void) loadAttributes:(NSDictionary*)attributes;
- (void) setSubnodes:(NSArray*)subnodes;
@property NSString *id;
@property NSArray *animations;
@property UIColor *fillColor;
@property UIColor *strokeColor;
@property CGFloat strokeWidth;
@property CGFloat opacity;
@property CATransform3D transform;
@end
106 changes: 106 additions & 0 deletions pxSVG/pxSVGObject.m
@@ -0,0 +1,106 @@
//
// pxSVGObject.m
// pxSVG
//
// Created by Yury Popov on 12.05.15.
// Copyright (c) 2015 PhoeniX. All rights reserved.
//

#import "pxSVGObject.h"

@implementation pxSVGObject
+ (CATransform3D) transformFromString:(NSString*)string
{
CATransform3D tr = CATransform3DIdentity;
if (!string.length) return tr;
NSScanner *sc = [NSScanner scannerWithString:string];
sc.charactersToBeSkipped = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSString *op; double p[6]; int i;
while (!sc.atEnd) {
[sc scanUpToString:@"(" intoString:&op];
[sc scanString:@"(" intoString:nil];
i=0;
while ([sc scanDouble:&p[i++]]) [sc scanCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:@" ,"] intoString:nil];
i--;
[sc scanString:@")" intoString:nil];

if ([op isEqualToString:@"scale"]) {
switch (i) {
case 1:
tr = CATransform3DScale(tr, p[0], p[0], 1);
break;
case 2:
tr = CATransform3DScale(tr, p[0], p[1], 1);
break;
default: NSLog(@"Invalid number of operands for %@: %@",op,@(i)); break;
}
} else if ([op isEqualToString:@"translate"]) {
switch (i) {
case 1:
tr = CATransform3DTranslate(tr, p[0], 0, 0);
break;
case 2:
tr = CATransform3DTranslate(tr, p[0], p[1], 0);
break;
default: NSLog(@"Invalid number of operands for %@: %@",op,@(i)); break;
}
} else if ([op isEqualToString:@"matrix"]) {
switch (i) {
case 6:
tr = CATransform3DConcat(tr, CATransform3DMakeAffineTransform(CGAffineTransformMake(p[0], p[1], p[2], p[3], p[4], p[5])));
break;
default: NSLog(@"Invalid number of operands for %@: %@",op,@(i)); break;
}
} else {
NSLog(@"Unknown transform: %@",op);
}
}
return tr;
}
- (UIColor*) colorWithSVGColor:(NSString*)string
{
if (!string) return nil;
string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([string isEqualToString:@"none"]) return nil;
if ([string isEqualToString:@"black"]) return [UIColor blackColor];
if ([string isEqualToString:@"white"]) return [UIColor whiteColor];
NSScanner *sc = [NSScanner scannerWithString:[string lowercaseString]];
if ([sc scanString:@"#" intoString:nil]) {
unsigned int cl;
[sc scanHexInt:&cl];
float r,g,b;
switch (string.length) {
case 4:
b = (cl & 0xF)<<4; cl >>= 4;
g = (cl & 0xF)<<4; cl >>= 4;
r = (cl & 0xF)<<4; cl >>= 4;
break;
case 7:
b = cl & 0xFF; cl >>= 8;
g = cl & 0xFF; cl >>= 8;
r = cl & 0xFF; cl >>= 8;
break;

default:
NSLog(@"Invalid hex color: %@",string);
return nil;
}
r /= 255.f;
g /= 255.f;
b /= 255.f;
return [UIColor colorWithRed:r green:g blue:b alpha:1];
}
NSLog(@"Unknown color: %@",string);
return nil;
}
- (void)loadAttributes:(NSDictionary *)attributes
{
self.id = [attributes objectForKey:@"id"];
self.fillColor = [self colorWithSVGColor:[attributes objectForKey:@"fill"]];
self.strokeColor = [self colorWithSVGColor:[attributes objectForKey:@"stroke"]];
self.strokeWidth = [[attributes objectForKey:@"stroke-width"] doubleValue];
self.transform = [self.class transformFromString:[attributes objectForKey:@"transform"]];
self.opacity = [[attributes objectForKey:@"opacity"] doubleValue];
}
- (void)setSubnodes:(NSArray *)subnodes { }
@end
13 changes: 13 additions & 0 deletions pxSVG/pxSVGPath.h
@@ -0,0 +1,13 @@
//
// pxSVGPath.h
// pxSVG
//
// Created by Yury Popov on 12.05.15.
// Copyright (c) 2015 PhoeniX. All rights reserved.
//

#import "pxSVGObject.h"

@interface pxSVGPath : pxSVGObject
@property UIBezierPath *d;
@end

0 comments on commit 71cec95

Please sign in to comment.