/
pxSVGRenderPath.m
427 lines (413 loc) · 16.7 KB
/
pxSVGRenderPath.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//
// pxSVGRenderPath.m
// pxSVG
//
// Created by Yury Popov on 12 мая.
// Copyright (c) 2015 PhoeniX. All rights reserved.
//
#import "pxSVGRenderPath.h"
#import "pxSVGGroup.h"
#import "pxSVGPath.h"
@interface pxSVGGradient : NSObject
@property CGGradientRef gradient;
@property NSArray *colors;
@property NSArray *locations;
@property CGPoint startPoint, endPoint;
@end
@implementation pxSVGGradient
@end
@interface pxSVGPattern : pxSVGGroup
@end
@implementation pxSVGPattern
@end
@interface pxSVGRenderPath ()
@property NSMutableDictionary *defCache;
@property pxXMLNode *xml;
@property pxSVGObject *root;
@property CGRect bounds;
@end
@interface pxSVGPatternLayer : CALayer
@property CALayer *patternLayer;
@end
@implementation pxSVGPatternLayer
+ (instancetype)layer
{
return [self new];
}
- (instancetype)init
{
self = [super init];
self.delegate = self;
[self setNeedsDisplayOnBoundsChange:YES];
return self;
}
- (BOOL)drawsAsynchronously
{
return NO;
}
- (void)drawInContext:(CGContextRef)ctx
{
CGRect r = CGContextGetClipBoundingBox(ctx);
CGContextTranslateCTM(ctx, -r.size.width/2-37.73, -r.size.height/2);
[self.patternLayer drawInContext:ctx];
{
CALayer *l = self.patternLayer;
UIGraphicsBeginImageContext([l frame].size);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(ctx, -l.frame.origin.x, -l.frame.origin.y);
[l renderInContext:ctx];
NSLog(@"%@",UIImagePNGRepresentation(UIGraphicsGetImageFromCurrentImageContext()));
UIGraphicsEndImageContext();
}
}
@end
@implementation pxSVGRenderPath
+ (instancetype)pathWithXML:(pxXMLNode *)xmlNode
{
return [[self alloc] initWithXML:xmlNode];
}
- (instancetype)initWithXML:(pxXMLNode *)xmlNode
{
self = [super init];
self.xml = xmlNode;
self.defCache = [NSMutableDictionary new];
self.root = [self parseObject:self.xml inheritAttributes:nil];
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 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 {
self.bounds = self.root.bounds;
}
return self;
}
- (pxSVGObject*)findDef:(NSString*)name inNode:(pxXMLNode*)xml
{
id def;
for (pxXMLNode *n in xml.childNodes) {
if ([[n.attributes objectForKey:@"id"] isEqualToString:name]) {
def = [self parseObject:n inheritAttributes:nil];
if (def) return def;
}
def = [self findDef:name inNode:n];
if (def) return def;
}
return nil;
}
- (pxSVGObject*)findDef:(NSString*)name
{
if (!name) return nil;
id def = [self.defCache objectForKey:name];
if (def) return def;
return [self findDef:name inNode:self.xml];
}
- (pxSVGObject*)reuseObjectWithAttributes:(NSDictionary*)attributes
{
NSString *href = [attributes objectForKey:@"xlink:href"];
if (!href) href = [attributes objectForKey:@"href"];
if (!href) return nil;
href = [href substringFromIndex:1];
pxSVGObject *oobj = [self findDef:href], *obj;
if (!oobj) return nil;
obj = [oobj.class new];
obj.fillColor = oobj.fillColor;
obj.fillDef = oobj.fillDef;
obj.strokeColor = oobj.strokeColor;
obj.strokeWidth = oobj.strokeWidth;
obj.opacity = oobj.opacity;
obj.fillOpacity = oobj.fillOpacity;
if ([oobj respondsToSelector:@selector(d)])
[(id)obj setD:[(id)oobj d]];
if ([oobj respondsToSelector:@selector(subnodes)])
[(id)obj setSubnodes:[(id)oobj subnodes]];
CATransform3D tr = oobj.transform;
if ([attributes objectForKey:@"x"]) tr = CATransform3DTranslate(tr, [[attributes objectForKey:@"x"] doubleValue], 0, 0);
if ([attributes objectForKey:@"y"]) tr = CATransform3DTranslate(tr, 0, [[attributes objectForKey:@"y"] doubleValue], 0);
if ([attributes objectForKey:@"width"]) {
CGFloat ow = oobj.bounds.size.width, w = [[attributes objectForKey:@"width"] doubleValue];
tr = CATransform3DScale(tr, ow/w, 1, 1);
}
if ([attributes objectForKey:@"height"]) {
CGFloat oh = oobj.bounds.size.height, h = [[attributes objectForKey:@"height"] doubleValue];
tr = CATransform3DScale(tr, 1, oh/h, 1);
}
if ([attributes objectForKey:@"transform"]) tr = CATransform3DConcat(tr, [pxSVGObject transformFromString:[attributes objectForKey:@"transform"]]);
obj.transform = tr;
obj.animations = oobj.animations;
return obj;
}
- (pxSVGGradient*)parseLinearGradient:(pxXMLNode*)node
{
NSString *gid = [node.attributes objectForKey:@"id"];
if (!gid) return nil;
NSString *href = [node.attributes objectForKey:@"xlink:href"];
if (!href) href = [node.attributes objectForKey:@"href"];
CGGradientRef gr;
CGPoint sp = CGPointZero, ep = (CGPoint){INFINITY,INFINITY};
NSMutableArray *cls, *locs;
if (href) {
href = [href substringFromIndex:1];
pxSVGGradient *g = (id)[self findDef:href];
if (![g isKindOfClass:[pxSVGGradient class]]) return nil;
gr = g.gradient;
sp = g.startPoint;
ep = g.endPoint;
cls = (id)g.colors;
locs = (id)g.locations;
} else {
cls = [NSMutableArray new], locs = [NSMutableArray new];
CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
for (pxXMLNode *s in node.childNodes) {
if (![s.tagName isEqualToString:@"stop"]) {
NSLog(@"Unknown gradient node: %@",s);
}
NSMutableDictionary *ma = [s.attributes mutableCopy];
if ([s.attributes objectForKey:@"style"]) {
for (NSString *ss in [[s.attributes objectForKey:@"style"] componentsSeparatedByString:@";"]) {
NSUInteger sep = [ss rangeOfString:@":"].location;
if (sep == NSNotFound) continue;
NSString *k = [[ss substringToIndex:sep] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
NSString *v = [[ss substringFromIndex:sep+1] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[ma setObject:v forKey:k];
}
}
if ([ma objectForKey:@"stop-color"] && [ma objectForKey:@"offset"]) {
UIColor *cl = [pxSVGObject colorWithSVGColor:[ma objectForKey:@"stop-color"]];
if ([ma objectForKey:@"stop-opacity"])
cl = [cl colorWithAlphaComponent:[[ma objectForKey:@"stop-opacity"] doubleValue]];
[cls addObject:(__bridge id)cl.CGColor];
[locs addObject:@([[ma objectForKey:@"offset"] doubleValue])];
}
}
CGFloat *locs_a = CFAllocatorAllocate(CFAllocatorGetDefault(), (sizeof(CGFloat)*locs.count), 0);
for (NSUInteger i=0; i<locs.count; i++) {
locs_a[i] = [locs[i] doubleValue];
}
gr = CGGradientCreateWithColors(cs, (__bridge CFArrayRef)cls, locs_a);
CFAllocatorDeallocate(CFAllocatorGetDefault(), locs_a);
CGColorSpaceRelease(cs);
}
if ([node.attributes objectForKey:@"x1"]) sp.x = [[node.attributes objectForKey:@"x1"] doubleValue];
if ([node.attributes objectForKey:@"y1"]) sp.y = [[node.attributes objectForKey:@"y1"] doubleValue];
if ([node.attributes objectForKey:@"x2"]) ep.x = [[node.attributes objectForKey:@"x2"] doubleValue];
if ([node.attributes objectForKey:@"y2"]) ep.y = [[node.attributes objectForKey:@"y2"] doubleValue];
if (gr) {
pxSVGGradient *g = [pxSVGGradient new];
g.gradient = gr;
g.colors = cls;
g.locations = locs;
g.startPoint = sp;
g.endPoint = ep;
[self.defCache setObject:g forKey:gid];
return g;
}
return nil;
}
- (pxSVGPattern*)parsePattern:(pxXMLNode*)node
{
NSString *pid = [node.attributes objectForKey:@"id"];
if (!pid) return nil;
NSString *href = [node.attributes objectForKey:@"xlink:href"];
if (!href) href = [node.attributes objectForKey:@"href"];
pxSVGPattern *p = nil;
if (href) {
href = [href substringFromIndex:1];
pxSVGPattern *op = (id)[self findDef:href];
if (![op isKindOfClass:[pxSVGPattern class]]) return nil;
p = [pxSVGPattern new];
p.id = pid;
p.subnodes = op.subnodes;
p.transform = op.transform;
} else {
p = [pxSVGPattern new];
p.id = pid;
NSMutableArray *sub = [NSMutableArray new];
for (pxXMLNode *n in node.childNodes) {
pxSVGObject *o = [self parseObject:n inheritAttributes:nil];
if (o) [sub addObject:o];
}
p.subnodes = [NSArray arrayWithArray:sub];
p.transform = CATransform3DIdentity;
}
CGRect pr = p.bounds, tr = CGRectZero;
if ([node.attributes objectForKey:@"viewBox"]) {
NSArray *vb = [[node.attributes objectForKey:@"viewBox"] componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
tr = (CGRect){
{
[vb[0] doubleValue],
[vb[1] doubleValue]
},{
[vb[2] doubleValue],
[vb[3] doubleValue]
}
};
} else if ([node.attributes objectForKey:@"width"] &&
[node.attributes objectForKey:@"height"]) {
CGPoint o = CGPointZero;
if ([node.attributes objectForKey:@"x"] &&
[node.attributes objectForKey:@"y"]) {
o = (CGPoint){
[[node.attributes objectForKey:@"x"] doubleValue],
[[node.attributes objectForKey:@"y"] doubleValue]
};
}
tr = (CGRect){
o,{
[[node.attributes objectForKey:@"width"] doubleValue],
[[node.attributes objectForKey:@"height"] doubleValue]
}
};
} else {
tr = pr;
}
if (!CGRectEqualToRect(pr, tr)) {
p.transform = CATransform3DTranslate(p.transform, -pr.origin.x-pr.size.width/2, -pr.origin.y-pr.size.height/2, 0);
p.transform = CATransform3DScale(p.transform, tr.size.width/pr.size.width,tr.size.height/pr.size.height,1);
p.transform = CATransform3DTranslate(p.transform, tr.origin.x+tr.size.width/2, tr.origin.y+tr.size.height/2, 0);
}
if ([node.attributes objectForKey:@"patternTransform"]) {
p.transform = CATransform3DConcat(p.transform, [pxSVGObject transformFromString:[node.attributes objectForKey:@"patternTransform"]]);
}
[self.defCache setObject:p forKey:pid];
return p;
}
- (pxSVGObject*)parseObject:(pxXMLNode*)node inheritAttributes:(pxSVGObject*)inherit
{
if ([node.tagName rangeOfString:@":"].location != NSNotFound) return nil;
if ([node.tagName isEqualToString:@"metadata"]) return nil;
if ([node.tagName isEqualToString:@"marker"]) return nil;
if ([node.tagName isEqualToString:@"filter"]) return nil;
if ([node.tagName isEqualToString:@"a"]) return nil;
if ([node.tagName isEqualToString:@"use"]) return [self reuseObjectWithAttributes:node.attributes];
if ([node.tagName isEqualToString:@"linearGradient"]) {
[self parseLinearGradient:node];
return nil;
}
if ([node.tagName isEqualToString:@"pattern"]) return [self parsePattern:node];;
Class objClass = pxSVGObject.class;
if ([node.tagName isEqualToString:@"g"])
objClass = pxSVGGroup.class;
else if ([node.tagName isEqualToString:@"svg"])
objClass = pxSVGGroup.class;
else if ([node.tagName isEqualToString:@"defs"])
objClass = pxSVGGroup.class;
else if ([node.tagName isEqualToString:@"path"])
objClass = pxSVGPath.class;
else if ([node.tagName isEqualToString:@"polygon"])
objClass = pxSVGPath.class;
else if ([node.tagName isEqualToString:@"ellipse"])
objClass = pxSVGPath.class;
else if ([node.tagName isEqualToString:@"circle"])
objClass = pxSVGPath.class;
else if ([node.tagName isEqualToString:@"rect"])
objClass = pxSVGPath.class;
else NSLog(@"Unknown tag: %@",node.tagName);
pxSVGObject *obj = [objClass new];
[obj loadAttributes:node.attributes];
if (!obj.fillColor)
obj.fillColor = inherit?inherit.fillColor:[UIColor blackColor];
if (isnan(obj.strokeWidth))
obj.strokeWidth = inherit?inherit.strokeWidth:0;
if (isnan(obj.fillOpacity))
obj.fillOpacity = inherit?inherit.fillOpacity:1;
if (!obj.strokeColor)
obj.strokeColor = inherit?inherit.strokeColor:nil;
if (obj.id) [self.defCache setObject:obj forKey:obj.id];
if (node.childNodes.count) {
NSMutableArray *subnodes = [NSMutableArray new];
for (pxXMLNode *n in node.childNodes) {
pxSVGObject *o = [self parseObject:n inheritAttributes:obj];
if (o && ![n.tagName isEqualToString:@"defs"]) {
[subnodes addObject:o];
}
}
[obj setSubnodes:[NSArray arrayWithArray:subnodes]];
}
return obj;
}
- (CALayer *)makeLayerWithNode:(pxSVGObject*)node rootBounds:(CGRect)bounds;
{
CALayer *l;
if ([node respondsToSelector:@selector(d)]) {
CAShapeLayer *sl = [CAShapeLayer new];
UIBezierPath *p = [(id)node d];
sl.path = p.CGPath;
if (node.fillDef) {
sl.fillColor = [UIColor clearColor].CGColor;
id def = [self findDef:node.fillDef];
if ([def isKindOfClass:[pxSVGGradient class]]) {
CAGradientLayer *gl = [CAGradientLayer new];
gl.frame = p.bounds;
gl.startPoint = (CGPoint){([def startPoint].x-gl.frame.origin.x)/gl.frame.size.width,([def startPoint].y-gl.frame.origin.y)/gl.frame.size.height};
gl.endPoint = (CGPoint){([def endPoint].x-gl.frame.origin.x)/gl.frame.size.width,([def endPoint].y-gl.frame.origin.y)/gl.frame.size.height};
gl.locations = [def locations];
gl.colors = [def colors];
gl.opacity = isnan(node.fillOpacity)?1:node.fillOpacity;
CAShapeLayer *ml = [CAShapeLayer new];
ml.frame = (CGRect){{-p.bounds.origin.x,-p.bounds.origin.y},{p.bounds.size.width+p.bounds.origin.x,p.bounds.size.height+p.bounds.origin.y}};
ml.path = sl.path;
gl.mask = ml;
[sl addSublayer:gl];
}
if ([def isKindOfClass:[pxSVGPattern class]]) {
pxSVGPatternLayer *tl = [pxSVGPatternLayer new];
CALayer *pl = [self makeLayerWithNode:def rootBounds:[def bounds]];
tl.patternLayer = pl;
tl.frame = p.bounds;
tl.opacity = isnan(node.fillOpacity)?1:node.fillOpacity;
CAShapeLayer *ml = [CAShapeLayer new];
ml.frame = (CGRect){{-p.bounds.origin.x,-p.bounds.origin.y},{p.bounds.size.width+p.bounds.origin.x,p.bounds.size.height+p.bounds.origin.y}};
ml.path = sl.path;
tl.mask = ml;
[sl addSublayer:tl];
}
} else
sl.fillColor = [(node.fillColor?:[UIColor blackColor]) colorWithAlphaComponent:isnan(node.fillOpacity)?1:node.fillOpacity].CGColor;
sl.strokeColor = node.strokeColor.CGColor;
sl.lineWidth = isnan(node.strokeWidth)?0:node.strokeWidth;
l = sl;
} else {
l = [CALayer new];
}
l.frame = bounds;
CATransform3D tr = node.transform;
tr = CATransform3DConcat(CATransform3DMakeTranslation( self.bounds.size.width/2, self.bounds.size.height/2, 0), tr);
tr = CATransform3DConcat(tr, CATransform3DMakeTranslation(-self.bounds.size.width/2, -self.bounds.size.height/2, 0));
l.transform = tr;
l.opacity = node.opacity;
if ([node respondsToSelector:@selector(subnodes)]) {
for (pxSVGObject *n in [(id)node subnodes]) {
CALayer *sl = [self makeLayerWithNode:n rootBounds:bounds];
if (sl) [l addSublayer:sl];
}
}
return l;
}
- (CALayer *)makeLayer
{
return [self makeLayerWithNode:self.root rootBounds:self.bounds];
}
@end