Commit 1dc3293f authored by PhoeniX's avatar PhoeniX

SVG images bounds detection

parent 8b728e1c
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 57 57"><rect x="26.607" width="2.01" height="33.804" fill="#231F20"/><polygon fill="#231F20" points="35.795,33.629 35.006,31.781 42.393,28.627 41.541,24.036 50.348,20.513 51.096,22.379 43.818,25.29 44.662,29.842"/><polygon fill="#231F20" points="36.586,39.282 36.246,37.302 44.48,35.889 45.586,31.212 54.902,29.932 55.178,31.922 47.225,33.016 46.129,37.646"/><polygon fill="#231F20" points="46.445,44.97 36.924,44.97 36.924,42.959 45.18,42.959 47.391,38.367 56.68,38.644 56.619,40.651 48.637,40.415"/><polygon fill="#231F20" points="42.646,52.7 33.879,50.438 34.381,48.493 42.037,50.466 45.951,46.553 54.398,49.096 53.818,51.021 46.521,48.823"/><polygon fill="#231F20" points="18.968,33.612 11.005,29.816 11.764,25.262 5.277,22.362 6.097,20.528 14.002,24.062 13.237,28.652 19.833,31.799"/><polygon fill="#231F20" points="18.281,39.279 9.66,37.628 8.734,33.001 1.642,31.92 1.944,29.935 10.428,31.226 11.365,35.908 18.659,37.307"/><polygon fill="#231F20" points="18.131,44.97 9.437,44.97 7.414,40.419 0.387,40.651 0.321,38.644 8.701,38.365 10.742,42.959 18.131,42.959"/><polygon fill="#231F20" points="12.898,52.721 9.366,48.849 2.96,51.012 2.318,49.106 9.966,46.524 13.542,50.445 20.308,48.499 20.864,50.432"/><ellipse cx="27.527" cy="41.169" rx="10.074" ry="15.831"/><ellipse fill="#FFFFFF" cx="24.142" cy="48.027" rx="2.709" ry="3.809"/><ellipse cx="24.734" cy="48.958" rx="1.016" ry="1.44"/><ellipse fill="#FFFFFF" cx="31.168" cy="48.027" rx="2.709" ry="3.809"/><ellipse cx="32.184" cy="48.958" rx="1.018" ry="1.44"/></svg>
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -6,9 +6,10 @@
// Copyright (c) 2015 PhoeniX. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface pxSVGImage : NSObject
+ (instancetype) svgImageWithXML:(NSString*)data;
- (instancetype) initWithXML:(NSString*)data;
@property (nonatomic,readonly) CGRect bounds;
@end
......@@ -8,9 +8,10 @@
#import "pxSVGImage.h"
#import "pxXMLNode.h"
#import "pxSVGRenderPath.h"
@interface pxSVGImage ()
@property pxXMLNode *xmlTree;
@property pxSVGRenderPath *renderPath;
@end
@implementation pxSVGImage
......@@ -20,11 +21,20 @@
}
- (instancetype)initWithXML:(NSString *)xml
{
pxXMLNode *xmlTree =
[[pxXMLNode parseTree:[[NSScanner alloc] initWithString:xml]]
filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"tagName=%@",@"svg"]]
.firstObject;
if (!xmlTree) return nil;
pxSVGRenderPath *renderPath = [pxSVGRenderPath pathWithXML:xmlTree];
if (!renderPath) return nil;
self = [self init];
NSScanner *scan = [[NSScanner alloc] initWithString:xml];
self.xmlTree = [[pxXMLNode parseTree:scan] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"tagName=%@",@"svg"]].firstObject;
if (!self.xmlTree) return nil;
self.renderPath = renderPath;
return self;
}
- (CGRect)bounds
{
return self.renderPath.bounds;
}
@end
......@@ -88,7 +88,7 @@
NSBlockOperation *sync = [NSBlockOperation blockOperationWithBlock:^{
if ([op isCancelled]) return;
if (!img) return [weakself loadError:[NSError errorWithDomain:@"pxSVGParser.parseError" code:0 userInfo:nil]];
NSLog(@"%@",img);
NSLog(@"%@ %@",img,[NSValue valueWithCGRect:img.bounds]);
}];
[[NSOperationQueue mainQueue] addOperations:@[sync] waitUntilFinished:YES];
}];
......
//
// pxSVGRenderPath.h
// pxSVG
//
// Created by Yury Popov on 12 мая.
// Copyright (c) 2015 PhoeniX. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "pxXMLNode.h"
@interface pxSVGRenderPath : NSObject
+ (instancetype) pathWithXML:(pxXMLNode*)xmlNode;
- (instancetype) initWithXML:(pxXMLNode*)xmlNode;
@property (readonly) CGRect bounds;
@end
//
// pxSVGRenderPath.m
// pxSVG
//
// Created by Yury Popov on 12 мая.
// Copyright (c) 2015 PhoeniX. All rights reserved.
//
#import "pxSVGRenderPath.h"
@interface pxSVGRenderPath ()
@property NSDictionary *defs;
//@property pxSVGGroup *root;
@property CGRect bounds;
@end
@implementation pxSVGRenderPath
+ (instancetype)pathWithXML:(pxXMLNode *)xmlNode
{
return [[self alloc] initWithXML:xmlNode];
}
- (instancetype)initWithXML:(pxXMLNode *)xmlNode
{
self = [super init];
if ([xmlNode.attributes objectForKey:@"width"] &&
[xmlNode.attributes objectForKey:@"height"]) {
CGPoint o = CGPointZero;
if ([xmlNode.attributes objectForKey:@"x"] &&
[xmlNode.attributes objectForKey:@"y"]) {
o = (CGPoint){
[[xmlNode.attributes objectForKey:@"x"] doubleValue],
[[xmlNode.attributes objectForKey:@"y"] doubleValue]
};
}
self.bounds = (CGRect){
o,{
[[xmlNode.attributes objectForKey:@"width"] doubleValue],
[[xmlNode.attributes objectForKey:@"height"] doubleValue]
}
};
} else if ([xmlNode.attributes objectForKey:@"viewBox"]) {
NSArray *vb = [[xmlNode.attributes objectForKey:@"viewBox"] componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
self.bounds = (CGRect){
{
[vb[0] doubleValue],
[vb[1] doubleValue]
},{
[vb[2] doubleValue],
[vb[3] doubleValue]
}
};
} else {
self.bounds = CGRectNull;
}
return self;
}
@end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment