diff --git a/ios/Utils/RNSVGPathParser.m b/ios/Utils/RNSVGPathParser.m index 03057353..3866ddb8 100644 --- a/ios/Utils/RNSVGPathParser.m +++ b/ios/Utils/RNSVGPathParser.m @@ -10,7 +10,7 @@ #import #import "math.h" -@implementation RNSVGPathParser : NSObject +@implementation RNSVGPathParser : NSObject { NSString* _d; NSString* _originD; @@ -44,7 +44,7 @@ NSArray* results = [_pathRegularExpression matchesInString:_d options:0 range:NSMakeRange(0, [_d length])]; _bezierCurves = [[NSMutableArray alloc] init]; int count = [results count]; - + if (count) { NSUInteger i = 0; #define NEXT_VALUE [self getNextValue:results[i++]] @@ -52,7 +52,7 @@ #define NEXT_BOOL [self bool:NEXT_VALUE] NSString* lastCommand; NSString* command = NEXT_VALUE; - + @try { while (command) { if ([command isEqualToString:@"m"]) { // moveTo command @@ -100,14 +100,14 @@ i--; continue; } - + lastCommand = command; if ([lastCommand isEqualToString:@"m"]) { lastCommand = @"l"; } else if ([lastCommand isEqualToString:@"M"]) { lastCommand = @"L"; } - + command = i < count ? NEXT_VALUE : nil; } } @catch (NSException *exception) { @@ -117,7 +117,7 @@ } } - + return (CGPathRef)CFAutorelease(path); } @@ -126,7 +126,7 @@ if (!_bezierCurves) { CGPathRelease([self getPath]); } - + return [_bezierCurves copy]; } @@ -158,7 +158,7 @@ _pivotX = _penX = x; _pivotY = _penY = y; CGPathMoveToPoint(path, nil, x, y); - + _lastStartPoint = [NSValue valueWithCGPoint: CGPointMake(x, y)]; [_bezierCurves addObject: @[_lastStartPoint]]; } @@ -170,12 +170,12 @@ - (void)lineTo:(CGPathRef)path x:(float)x y:(float)y{ NSValue * source = [NSValue valueWithCGPoint:CGPointMake(_pivotX, _pivotY)]; - + [self setPenDown]; _pivotX = _penX = x; _pivotY = _penY = y; CGPathAddLineToPoint(path, nil, x, y); - + NSValue * destination = [NSValue valueWithCGPoint:CGPointMake(x, y)]; [_bezierCurves addObject: @[destination, destination, destination]]; } @@ -192,8 +192,8 @@ - (void)curveTo:(CGPathRef)path c1x:(float)c1x c1y:(float)c1y c2x:(float)c2x c2y:(float)c2y ex:(float)ex ey:(float)ey { - _pivotX = ex; - _pivotY = ey; + _pivotX = c2x; + _pivotY = c2y; [self curveToPoint:path c1x:(float)c1x c1y:(float)c1y c2x:(float)c2x c2y:(float)c2y ex:(float)ex ey:(float)ey]; } @@ -203,7 +203,7 @@ _penX = ex; _penY = ey; CGPathAddCurveToPoint(path, nil, c1x, c1y, c2x, c2y, ex, ey); - + [_bezierCurves addObject: @[ [NSValue valueWithCGPoint:CGPointMake(c1x, c1y)], [NSValue valueWithCGPoint:CGPointMake(c2x, c2y)], @@ -268,16 +268,16 @@ { float tX = _penX; float tY = _penY; - + ry = fabsf(ry == 0 ? (rx == 0 ? (y - tY) : rx) : ry); rx = fabsf(rx == 0 ? (x - tX) : rx); - + if (rx == 0 || ry == 0 || (x == tX && y == tY)) { [self lineTo:path x:x y:y]; return; } - - + + float rad = rotation * M_PI / 180; float cosed = cosf(rad); float sined = sinf(rad); @@ -290,7 +290,7 @@ float rycx = ry * ry * cx * cx; float rxcy = rx * rx * cy * cy; float a = rxry - rxcy - rycx; - + if (a < 0){ a = sqrtf(1 - a / rxry); rx *= a; @@ -299,7 +299,7 @@ cy = y / 2; } else { a = sqrtf(a / (rxcy + rycx)); - + if (outer == clockwise) { a = -a; } @@ -308,27 +308,27 @@ cx = cosed * cxd - sined * cyd + x / 2; cy = sined * cxd + cosed * cyd + y / 2; } - + // Rotation + Scale Transform float xx = cosed / rx; float yx = sined / rx; float xy = -sined / ry; float yy = cosed / ry; - + // Start and End Angle float sa = atan2f(xy * -cx + yy * -cy, xx * -cx + yx * -cy); float ea = atan2f(xy * (x - cx) + yy * (y - cy), xx * (x - cx) + yx * (y - cy)); - + cx += tX; cy += tY; x += tX; y += tY; - + [self setPenDown]; - + _penX = _pivotX = x; _penY = _pivotY = y; - + [self arcToBezier:path cx:cx cy:cy rx:rx ry:ry sa:sa ea:ea clockwise:clockwise rad:rad]; } @@ -341,7 +341,7 @@ float yx = -sined * ry; float xy = sined * rx; float yy = cosed * ry; - + // Bezier Curve Approximation float arc = ea - sa; if (arc < 0 && clockwise) { @@ -349,26 +349,26 @@ } else if (arc > 0 && !clockwise) { arc -= M_PI * 2; } - + int n = ceilf(fabsf(arc / (M_PI / 2))); - + float step = arc / n; float k = (4.0f / 3.0f) * tanf(step / 4); - + float x = cosf(sa); float y = sinf(sa); - + for (int i = 0; i < n; i++){ float cp1x = x - k * y; float cp1y = y + k * x; - + sa += step; x = cosf(sa); y = sinf(sa); - + float cp2x = x + k * y; float cp2y = y - k * x; - + CGPathAddCurveToPoint(path, nil, cx + xx * cp1x + yx * cp1y,