Skip to content

Commit

Permalink
Move d parsing to native side(iOS)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicismight committed Jan 7, 2017
1 parent 0a6dc56 commit 293263d
Show file tree
Hide file tree
Showing 8 changed files with 423 additions and 60 deletions.
12 changes: 6 additions & 6 deletions android/src/main/java/com/horcrux/svg/PropHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public void setupPaint(Paint paint, RectF box, float scale, float opacity) {

static class PathParser {
static private Pattern PATH_REG_EXP = Pattern.compile("[a-df-z]|[\\-+]?(?:[\\d.]e[\\-+]?|[^\\s\\-+,a-z])+", Pattern.CASE_INSENSITIVE);
static private Pattern DECICAL_REG_EXP = Pattern.compile("(\\.\\d+)(?=\\-?\\.)");
static private Pattern DECIMAL_REG_EXP = Pattern.compile("(\\.\\d+)(?=\\-?\\.)");

private Matcher mMatcher;
private Path mPath;
Expand All @@ -218,7 +218,7 @@ public PathParser(String d, float scale) {
mScale = scale;
mString = d;
mPath = new Path();
mMatcher = PATH_REG_EXP.matcher(DECICAL_REG_EXP.matcher(mString).replaceAll("$1,"));
mMatcher = PATH_REG_EXP.matcher(DECIMAL_REG_EXP.matcher(mString).replaceAll("$1,"));

while (mMatcher.find() && mValid) {
executeCommand(mMatcher.group());
Expand Down Expand Up @@ -361,7 +361,7 @@ private void line(float x, float y) {
}

private void lineTo(float x, float y) {
setPendDown();
setPenDown();
mPivotX = mPenX = x;
mPivotY = mPenY = y;
mPath.lineTo(x * mScale, y * mScale);
Expand All @@ -378,7 +378,7 @@ private void curveTo(float c1x, float c1y, float c2x, float c2y, float ex, float
}

private void cubicTo(float c1x, float c1y, float c2x, float c2y, float ex, float ey) {
setPendDown();
setPenDown();
mPenX = ex;
mPenY = ey;
mPath.cubicTo(c1x * mScale, c1y * mScale, c2x * mScale, c2y * mScale, ex * mScale, ey * mScale);
Expand Down Expand Up @@ -489,7 +489,7 @@ private void arcTo(float rx, float ry, float rotation, boolean outer, boolean cl
x += tX;
y += tY;

setPendDown();
setPenDown();

mPenX = mPivotX = x;
mPenY = mPivotY = y;
Expand Down Expand Up @@ -570,7 +570,7 @@ private void arcToBezier(float cx, float cy, float rx, float ry, float sa, float
}
}

private void setPendDown() {
private void setPenDown() {
if (!mPendDownSet) {
mPenDownX = mPenX;
mPenDownY = mPenY;
Expand Down
10 changes: 9 additions & 1 deletion ios/RNSVG.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
10ED4A9E1CF0656A0078BC02 /* RNSVGClipPathManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 10ED4A9D1CF0656A0078BC02 /* RNSVGClipPathManager.m */; };
10ED4AA21CF078830078BC02 /* RNSVGNode.m in Sources */ = {isa = PBXBuildFile; fileRef = 10ED4AA11CF078830078BC02 /* RNSVGNode.m */; };
10FDEEB21D3FB60500A5C46C /* RNSVGBaseBrush.m in Sources */ = {isa = PBXBuildFile; fileRef = 10FDEEB11D3FB60500A5C46C /* RNSVGBaseBrush.m */; };
7F9CDAFA1E1F809C00E0C805 /* RNSVGPathParser.m in Sources */ = {isa = PBXBuildFile; fileRef = 7F9CDAF91E1F809C00E0C805 /* RNSVGPathParser.m */; };
/* End PBXBuildFile section */

/* Begin PBXCopyFilesBuildPhase section */
Expand Down Expand Up @@ -155,6 +156,8 @@
10FDEEB01D3FB60500A5C46C /* RNSVGBaseBrush.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNSVGBaseBrush.h; sourceTree = "<group>"; };
10FDEEB11D3FB60500A5C46C /* RNSVGBaseBrush.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNSVGBaseBrush.m; sourceTree = "<group>"; };
10FDEEB31D3FBED400A5C46C /* RNSVGBrushType.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNSVGBrushType.h; sourceTree = "<group>"; };
7F9CDAF81E1F809C00E0C805 /* RNSVGPathParser.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RNSVGPathParser.h; path = Utils/RNSVGPathParser.h; sourceTree = "<group>"; };
7F9CDAF91E1F809C00E0C805 /* RNSVGPathParser.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = RNSVGPathParser.m; path = Utils/RNSVGPathParser.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -310,13 +313,15 @@
1039D29A1CE7212C001E90A8 /* Utils */ = {
isa = PBXGroup;
children = (
1039D29E1CE72177001E90A8 /* RNSVGCGFloatArray.h */,
10ABC7381D43982B006CCF6E /* RNSVGVBMOS.h */,
10ABC7371D439779006CCF6E /* RNSVGCGFCRule.h */,
1039D2AE1CE72F27001E90A8 /* RNSVGPercentageConverter.h */,
1039D2AF1CE72F27001E90A8 /* RNSVGPercentageConverter.m */,
7F9CDAF81E1F809C00E0C805 /* RNSVGPathParser.h */,
7F9CDAF91E1F809C00E0C805 /* RNSVGPathParser.m */,
1039D29B1CE72177001E90A8 /* RCTConvert+RNSVG.h */,
1039D29C1CE72177001E90A8 /* RCTConvert+RNSVG.m */,
1039D29E1CE72177001E90A8 /* RNSVGCGFloatArray.h */,
);
name = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -412,6 +417,7 @@
10BA0D351CE74E3100887C2B /* RNSVGEllipseManager.m in Sources */,
1039D2A01CE72177001E90A8 /* RCTConvert+RNSVG.m in Sources */,
0CF68B0B1AF0549300FF9E5C /* RNSVGBrush.m in Sources */,
7F9CDAFA1E1F809C00E0C805 /* RNSVGPathParser.m in Sources */,
10BA0D361CE74E3100887C2B /* RNSVGGroupManager.m in Sources */,
10BA0D4A1CE74E3D00887C2B /* RNSVGLine.m in Sources */,
10FDEEB21D3FB60500A5C46C /* RNSVGBaseBrush.m in Sources */,
Expand Down Expand Up @@ -518,6 +524,7 @@
);
OTHER_LDFLAGS = "-ObjC";
PRODUCT_NAME = RNSVG;
PUBLIC_HEADERS_FOLDER_PATH = /usr/local/include/;
SKIP_INSTALL = YES;
};
name = Debug;
Expand All @@ -532,6 +539,7 @@
);
OTHER_LDFLAGS = "-ObjC";
PRODUCT_NAME = RNSVG;
PUBLIC_HEADERS_FOLDER_PATH = /usr/local/include/;
SKIP_INSTALL = YES;
};
name = Release;
Expand Down
3 changes: 2 additions & 1 deletion ios/Utils/RCTConvert+RNSVG.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@

@interface RCTConvert (RNSVG)

+ (CGPathRef)CGPath:(id)json;
+ (CGPathRef)CGPath:(NSString *)d;
+ (CTTextAlignment)CTTextAlignment:(id)json;
+ (RNSVGCGFCRule)RNSVGCGFCRule:(id)json;
+ (RNSVGTextFrame)RNSVGTextFrame:(id)json;
+ (RNSVGCGFloatArray)RNSVGCGFloatArray:(id)json;
+ (RNSVGBrush *)RNSVGBrush:(id)json;


+ (NSArray *)RNSVGBezier:(id)json;
+ (CGRect)CGRect:(id)json offset:(NSUInteger)offset;
+ (CGColorRef)CGColor:(id)json offset:(NSUInteger)offset;
Expand Down
47 changes: 3 additions & 44 deletions ios/Utils/RCTConvert+RNSVG.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,54 +15,13 @@
#import "RNSVGCGFCRule.h"
#import "RNSVGVBMOS.h"
#import <React/RCTFont.h>
#import "RNSVGPathParser.h"

@implementation RCTConvert (RNSVG)

+ (CGPathRef)CGPath:(id)json
+ (CGPathRef)CGPath:(NSString *)d
{
NSArray *arr = [self NSNumberArray:json];

NSUInteger count = [arr count];

#define NEXT_VALUE [self double:arr[i++]]

CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, 0, 0);

@try {
NSUInteger i = 0;
while (i < count) {
NSUInteger type = [arr[i++] unsignedIntegerValue];
switch (type) {
case 0:
CGPathMoveToPoint(path, nil, NEXT_VALUE, NEXT_VALUE);
break;
case 1:
CGPathCloseSubpath(path);
break;
case 2:
CGPathAddLineToPoint(path, nil, NEXT_VALUE, NEXT_VALUE);
break;
case 3:
CGPathAddCurveToPoint(path, nil, NEXT_VALUE, NEXT_VALUE, NEXT_VALUE, NEXT_VALUE, NEXT_VALUE, NEXT_VALUE);
break;
case 4:
CGPathAddArc(path, NULL, NEXT_VALUE, NEXT_VALUE, NEXT_VALUE, NEXT_VALUE, NEXT_VALUE, NEXT_VALUE == 0);
break;
default:
RCTLogError(@"Invalid CGPath type %zd at element %zd of %@", type, i, arr);
CGPathRelease(path);
return nil;
}
}
}
@catch (NSException *exception) {
RCTLogError(@"Invalid CGPath format: %@", arr);
CGPathRelease(path);
return nil;
}

return (CGPathRef)CFAutorelease(path);
return [[[RNSVGPathParser alloc] initWithPathString: d] getPath];
}

RCT_ENUM_CONVERTER(CTTextAlignment, (@{
Expand Down
17 changes: 17 additions & 0 deletions ios/Utils/RNSVGPathParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Copyright (c) 2015-present, Horcrux.
* All rights reserved.
*
* This source code is licensed under the MIT-style license found in the
* LICENSE file in the root directory of this source tree.
*/

#import <QuartzCore/QuartzCore.h>
#import <Foundation/Foundation.h>

@interface RNSVGPathParser : NSObject

- (instancetype) initWithPathString:(NSString *)d;
- (CGPathRef)getPath;

@end

0 comments on commit 293263d

Please sign in to comment.