forked from pocketsvg/PocketSVG
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSVGBezierPath.mm
188 lines (179 loc) · 6.13 KB
/
SVGBezierPath.mm
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
#import "PocketSVG.h"
@implementation SVGBezierPath
#if !TARGET_OS_IPHONE
{
CGAffineTransform _cgTransformBuffer;
}
@synthesize CGPath=_CGPath;
- (instancetype)init
{
if ((self = [super init])) {
_cgTransformBuffer = CGAffineTransformIdentity;
}
return self;
}
#endif
+ (NSCache<NSString*, NSArray<SVGBezierPath*>*> *)_svg_pathCache
{
static NSCache<NSString*, NSArray<SVGBezierPath*>*> *pathCache;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
pathCache = [NSCache new];
});
return pathCache;
}
+ (void)resetCache
{
[self._svg_pathCache removeAllObjects];
}
+ (NSArray *)pathsFromSVGNamed:(NSString * const)aName
{
return [self pathsFromSVGNamed:aName inBundle:[NSBundle mainBundle]];
}
+ (NSArray<SVGBezierPath*> *)pathsFromSVGNamed:(NSString * const)aName inBundle:(NSBundle * const)aBundle
{
NSArray<SVGBezierPath*> *paths = [self._svg_pathCache objectForKey:aName];
if (!paths) {
NSString *path = [aBundle pathForResource:aName ofType:@"svg"];
paths = [self pathsFromContentsOfSVGFile:path];
if (paths) {
[self._svg_pathCache setObject:paths forKey:aName];
}
}
return [[NSArray alloc] initWithArray:paths copyItems:YES];
}
+ (NSArray *)pathsFromContentsOfSVGFile:(NSString * const)aPath
{
#ifndef NS_BLOCK_ASSERTIONS
BOOL isDir;
NSParameterAssert([[NSFileManager defaultManager] fileExistsAtPath:aPath isDirectory:&isDir] && !isDir);
#endif
return [self pathsFromSVGString:[NSString stringWithContentsOfFile:aPath usedEncoding:NULL error:nil]];
}
+ (NSArray *)pathsFromSVGString:(NSString * const)svgString
{
NSMapTable *cgAttrs;
NSArray * const pathRefs = CGPathsFromSVGString(svgString, &cgAttrs);
NSMutableArray * const paths = [NSMutableArray arrayWithCapacity:pathRefs.count];
for(id pathRef in pathRefs) {
SVGBezierPath * const uiPath = [self bezierPathWithCGPath:(__bridge CGPathRef)pathRef];
uiPath->_svgAttributes = [cgAttrs objectForKey:pathRef] ?: @{};
[paths addObject:uiPath];
}
return paths;
}
- (NSString *)SVGRepresentation
{
return SVGStringFromCGPaths(@[(__bridge id)self.CGPath], nil);
}
- (instancetype)copyWithZone:(NSZone *)zone
{
SVGBezierPath * const copy = [super copyWithZone:zone];
copy->_svgAttributes = [_svgAttributes copy];
#if !TARGET_OS_IPHONE
if(_CGPath)
copy->_CGPath = CGPathRetain(_CGPath);
#endif
return copy;
}
#if TARGET_OS_IPHONE
+ (instancetype)bezierPathWithCGPath:(CGPathRef)cgPath
{
if (NSProcessInfo.processInfo.operatingSystemVersion.majorVersion >= 9) {
return [super bezierPathWithCGPath:cgPath];
} else {
// iOS 8 and lower don't return instancetype...
SVGBezierPath *path = [self new];
path.CGPath = cgPath;
return path;
}
}
#else
+ (instancetype)bezierPathWithCGPath:(CGPathRef)cgPath
{
SVGBezierPath *path = [self new];
if (cgPath) {
[path _svg_setCGPath:cgPath];
CGPathApply(cgPath, (__bridge void *)path,
[](void * const info, const CGPathElement * const el)
{
SVGBezierPath * const path_ = (__bridge id)info;
switch(el->type) {
case kCGPathElementMoveToPoint:
[path_ moveToPoint:*(NSPoint *)el->points];
break;
case kCGPathElementAddLineToPoint:
[path_ lineToPoint:*(NSPoint *)el->points];
break;
case kCGPathElementAddQuadCurveToPoint:
[path_ curveToPoint:*(NSPoint *)(el->points+1)
controlPoint1:*(NSPoint *)(el->points)
controlPoint2:*(NSPoint *)(el->points)];
break;
case kCGPathElementAddCurveToPoint:
[path_ curveToPoint:*(NSPoint *)(el->points+2)
controlPoint1:*(NSPoint *)(el->points)
controlPoint2:*(NSPoint *)(el->points+1)];
break;
case kCGPathElementCloseSubpath:
[path_ closePath];
break;
}
});
}
return path;
}
- (void)dealloc
{
[self _svg_setCGPath:NULL];
}
- (void)_svg_setCGPath:(CGPathRef)CGPath
{
if (_CGPath)
CGPathRelease(_CGPath);
_CGPath = CGPath ? CGPathRetain(CGPath) : NULL;
}
- (CGPathRef)CGPath
{
if (!_CGPath) {
CGMutablePathRef path = CGPathCreateMutable();
for (NSInteger i = 0; i < self.elementCount; ++i) {
NSPoint pt[3];
switch ([self elementAtIndex:i associatedPoints:pt]) {
case NSMoveToBezierPathElement:
CGPathMoveToPoint(path, NULL, pt[0].x, pt[0].y);
break;
case NSLineToBezierPathElement:
CGPathAddLineToPoint(path, NULL, pt[0].x, pt[0].y);
break;
case NSCurveToBezierPathElement:
CGPathAddCurveToPoint(path, NULL, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x, pt[2].y);
break;
case NSClosePathBezierPathElement:
CGPathCloseSubpath(path);
}
}
[self _svg_setCGPath:path];
CGPathRelease(path);
_cgTransformBuffer = CGAffineTransformIdentity;
} else if (!CGAffineTransformIsIdentity(_cgTransformBuffer)) {
CGPathRef transformedPath = CGPathCreateCopyByTransformingPath(_CGPath, &_cgTransformBuffer);
[self _svg_setCGPath:transformedPath];
CGPathRelease(transformedPath);
_cgTransformBuffer = CGAffineTransformIdentity;
}
return _CGPath;
}
- (void)applyTransform:(CGAffineTransform)cgTransform
{
NSAffineTransform *transform = [NSAffineTransform new];
transform.transformStruct = (NSAffineTransformStruct) {
.m11 = cgTransform.a, .m12 = cgTransform.b,
.m21 = cgTransform.c, .m22 = cgTransform.d,
.tX = cgTransform.tx, .tY = cgTransform.ty,
};
_cgTransformBuffer = CGAffineTransformConcat(_cgTransformBuffer, cgTransform);
[self transformUsingAffineTransform:transform];
}
#endif
@end