-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSVGDocument.m
282 lines (209 loc) · 6.79 KB
/
SVGDocument.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//
// SVGDocument.m
// SVGKit
//
// Copyright Matt Rajca 2010-2011. All rights reserved.
//
#import "SVGDocument.h"
#import "SVGDefsElement.h"
#import "SVGDescriptionElement.h"
#import "SVGElement+Private.h"
#import "SVGParser.h"
#import "SVGTitleElement.h"
#import "SVGPathElement.h"
#import "SVGParserSVG.h"
@interface SVGDocument ()
@property (nonatomic, copy) NSString *version;
/*! Only preserved for temporary backwards compatibility */
- (BOOL)parseFileAtPath:(NSString *)aPath;
/*! Only preserved for temporary backwards compatibility */
-(BOOL)parseFileAtURL:(NSURL *)url;
- (BOOL)parseFileAtPath:(NSString *)aPath error:(NSError**) error;
- (BOOL)parseFileAtURL:(NSURL *)url error:(NSError**) error;
- (SVGElement *)findFirstElementOfClass:(Class)class;
@end
@implementation SVGDocument
@synthesize width = _width;
@synthesize height = _height;
@synthesize version = _version;
@synthesize viewBoxFrame = _viewBoxFrame;
@synthesize graphicsGroups, anonymousGraphicsGroups;
@dynamic title, desc, defs;
static NSMutableArray* _parserExtensions;
+ (void) addSVGParserExtension:(NSObject<SVGParserExtension>*) extension
{
if( _parserExtensions == nil )
{
_parserExtensions = [NSMutableArray new];
}
[_parserExtensions addObject:extension];
}
/* TODO: parse 'viewBox' */
+ (id)documentNamed:(NSString *)name {
NSParameterAssert(name != nil);
NSBundle *bundle = [NSBundle mainBundle];
if (!bundle)
return nil;
NSString *newName = [name stringByDeletingPathExtension];
NSString *extension = [name pathExtension];
if ([@"" isEqualToString:extension]) {
extension = @"svg";
}
NSString *path = [bundle pathForResource:newName ofType:extension];
if (!path)
{
NSLog(@"[%@] MISSING FILE, COULD NOT CREATE DOCUMENT: filename = %@, extension = %@", [self class], newName, extension);
return nil;
}
return [self documentWithContentsOfFile:path];
}
+ (id)documentFromURL:(NSURL *)url {
NSParameterAssert(url != nil);
return [[[[self class] alloc] initWithContentsOfURL:url] autorelease];
}
+ (id)documentWithContentsOfFile:(NSString *)aPath {
return [[[[self class] alloc] initWithContentsOfFile:aPath] autorelease];
}
- (id)initWithContentsOfFile:(NSString *)aPath {
NSParameterAssert(aPath != nil);
self = [super initWithDocument:self name:@"svg"];
if (self) {
_width = _height = 100;
NSError* parseError = nil;
if (![self parseFileAtPath:aPath error:&parseError]) {
NSLog(@"[%@] MISSING OR CORRUPT FILE, OR FILE USES FEATURES THAT SVGKit DOES NOT YET SUPPORT, COULD NOT CREATE DOCUMENT: path = %@, error = %@", [self class], aPath, parseError);
[self release];
return nil;
}
}
return self;
}
- (id)initWithContentsOfURL:(NSURL *)url {
NSParameterAssert(url != nil);
self = [super initWithDocument:self name:@"svg"];
if (self) {
_width = _height = 100;
if (![self parseFileAtURL:url]) {
NSLog(@"[%@] ERROR: COULD NOT FIND SVG AT URL = %@", [self class], url);
[self release];
return nil;
}
}
return self;
}
- (id) initWithFrame:(CGRect)frame
{
self = [super initWithDocument:self name:@"svg"];
if (self) {
_width = CGRectGetWidth(frame);
_height = CGRectGetHeight(frame);
}
return self;
}
- (void)dealloc {
[_version release];
self.graphicsGroups = nil;
self.anonymousGraphicsGroups = nil;
[super dealloc];
}
- (BOOL)parseFileAtPath:(NSString *)aPath error:(NSError**) error {
SVGParser *parser = [[SVGParser alloc] initWithPath:aPath document:self];
SVGParserSVG *subParserSVG = [[[SVGParserSVG alloc] init] autorelease];
[parser.parserExtensions addObject:subParserSVG];
for( NSObject<SVGParserExtension>* extension in _parserExtensions )
{
[parser.parserExtensions addObject:extension];
}
if (![parser parse:error]) {
NSLog(@"[%@] SVGKit Parse error: %@", [self class], *error);
[parser release];
return NO;
}
[parser release];
return YES;
}
- (BOOL)parseFileAtPath:(NSString *)aPath {
return [self parseFileAtPath:aPath error:nil];
}
-(BOOL)parseFileAtURL:(NSURL *)url error:(NSError**) error {
SVGParser *parser = [[SVGParser alloc] initWithURL:url document:self];
SVGParserSVG *subParserSVG = [[[SVGParserSVG alloc] init] autorelease];
[parser.parserExtensions addObject:subParserSVG];
for( NSObject<SVGParserExtension>* extension in _parserExtensions )
{
[parser.parserExtensions addObject:extension];
}
if (![parser parse:error]) {
NSLog(@"[%@] SVGKit Parse error: %@", [self class], *error);
[parser release];
return NO;
}
[parser release];
return YES;
}
-(BOOL)parseFileAtURL:(NSURL *)url {
return [self parseFileAtURL:url error:nil];
}
- (CALayer *)newLayer {
CALayer* _layer = [CALayer layer];
_layer.frame = CGRectMake(0.0f, 0.0f, _width, _height);
return _layer;
}
- (void)layoutLayer:(CALayer *)layer { }
- (SVGElement *)findFirstElementOfClass:(Class)class {
for (SVGElement *element in self.children) {
if ([element isKindOfClass:class])
return element;
}
return nil;
}
- (NSString *)title {
return [self findFirstElementOfClass:[SVGTitleElement class]].stringValue;
}
- (NSString *)desc {
return [self findFirstElementOfClass:[SVGDescriptionElement class]].stringValue;
}
- (SVGDefsElement *)defs {
return (SVGDefsElement *) [self findFirstElementOfClass:[SVGDefsElement class]];
}
- (void)parseAttributes:(NSDictionary *)attributes {
[super parseAttributes:attributes];
id value = nil;
if ((value = [attributes objectForKey:@"width"])) {
_width = [value floatValue];
}
if ((value = [attributes objectForKey:@"height"])) {
_height = [value floatValue];
}
if ((value = [attributes objectForKey:@"version"])) {
self.version = value;
}
if( (value = [attributes objectForKey:@"viewBox"])) {
NSArray* boxElements = [(NSString*) value componentsSeparatedByString:@" "];
_viewBoxFrame = CGRectMake([[boxElements objectAtIndex:0] floatValue], [[boxElements objectAtIndex:1] floatValue], [[boxElements objectAtIndex:2] floatValue], [[boxElements objectAtIndex:3] floatValue]);
NSLog(@"[%@] DEBUG INFO: set document viewBox = %@", [self class], NSStringFromCGRect(self.viewBoxFrame));
}
}
#if NS_BLOCKS_AVAILABLE
- (void) applyAggregator:(SVGElementAggregationBlock)aggregator toElement:(SVGElement < SVGLayeredElement > *)element
{
if (![element.children count]) {
return;
}
for (SVGElement *child in element.children) {
if ([child conformsToProtocol:@protocol(SVGLayeredElement)]) {
SVGElement<SVGLayeredElement>* layeredElement = (SVGElement<SVGLayeredElement>*)child;
if (layeredElement) {
aggregator(layeredElement);
[self applyAggregator:aggregator
toElement:layeredElement];
}
}
}
}
- (void) applyAggregator:(SVGElementAggregationBlock)aggregator
{
[self applyAggregator:aggregator toElement:self];
}
#endif
@end