diff --git a/ios/Elements/RNSVGPath.m b/ios/Elements/RNSVGPath.m index 97772e0..c4b56ee 100644 --- a/ios/Elements/RNSVGPath.m +++ b/ios/Elements/RNSVGPath.m @@ -21,105 +21,14 @@ _d = CGPathRetain(d); } -- (void)dealloc -{ - CGPathRelease(_d); -} - -- (void)renderLayerTo:(CGContextRef)context -{ - // todo: add detection if path has changed since last update. - CGPathRef path = [self getPath:context]; - if ((!self.fill && !self.stroke) || !path) { - return; - } - - if ([self getSvgView].responsible) { - // Add path to hitArea - CGMutablePathRef hitArea = CGPathCreateMutableCopy(path); - if (self.stroke && self.strokeWidth) { - // Add stroke to hitArea - CGPathRef strokePath = CGPathCreateCopyByStrokingPath(hitArea, nil, self.strokeWidth, self.strokeLinecap, self.strokeLinejoin, self.strokeMiterlimit); - CGPathAddPath(hitArea, nil, strokePath); - CGPathRelease(strokePath); - } - - self.hitArea = CFAutorelease(CGPathCreateCopy(hitArea)); - CGPathRelease(hitArea); - } - - if (self.opacity == 0) { - return; - } - - CGPathDrawingMode mode = kCGPathStroke; - BOOL fillColor = YES; - - if (self.fill) { - mode = self.fillRule == kRNSVGCGFCRuleEvenodd ? kCGPathEOFill : kCGPathFill; - fillColor = [self.fill applyFillColor:context opacity:self.fillOpacity]; - - if (!fillColor) { - [self clip:context]; - - CGContextSaveGState(context); - CGContextAddPath(context, path); - CGContextClip(context); - RNSVGBrushConverter *brushConverter = [[self getSvgView] getDefinedBrushConverter:[self.fill brushRef]]; - [self.fill paint:context opacity:self.fillOpacity brushConverter:brushConverter]; - CGContextRestoreGState(context); - if (!self.stroke) { - return; - } - } - } - - if (self.stroke && self.strokeWidth) { - CGContextSetLineWidth(context, self.strokeWidth); - CGContextSetLineCap(context, self.strokeLinecap); - CGContextSetLineJoin(context, self.strokeLinejoin); - RNSVGCGFloatArray dash = self.strokeDasharray; - - if (dash.count) { - CGContextSetLineDash(context, self.strokeDashoffset, dash.array, dash.count); - } - - if (!fillColor) { - CGContextAddPath(context, path); - CGContextReplacePathWithStrokedPath(context); - CGContextClip(context); - } - - if ([self.stroke applyStrokeColor:context opacity:self.strokeOpacity]) { - if (mode == kCGPathFill) { - mode = kCGPathFillStroke; - } else if (mode == kCGPathEOFill) { - mode = kCGPathEOFillStroke; - } - } else { - // draw fill - [self clip:context]; - CGContextAddPath(context, path); - CGContextDrawPath(context, mode); - - // draw stroke - CGContextAddPath(context, path); - CGContextReplacePathWithStrokedPath(context); - CGContextClip(context); - RNSVGBrushConverter *brushConverter = [[self getSvgView] getDefinedBrushConverter:[self.stroke brushRef]]; - [self.stroke paint:context opacity:self.strokeOpacity brushConverter:brushConverter]; - return; - } - } - - [self clip:context]; - CGContextAddPath(context, path); - CGContextDrawPath(context, mode); -} - - (CGPathRef)getPath:(CGContextRef)context { return self.d; } +- (void)dealloc +{ + CGPathRelease(_d); +} + @end diff --git a/ios/RNSVGRenderable.m b/ios/RNSVGRenderable.m index 126cef3..96b7dfc 100644 --- a/ios/RNSVGRenderable.m +++ b/ios/RNSVGRenderable.m @@ -25,7 +25,7 @@ if (self = [super init]) { _fillOpacity = 1; _strokeOpacity = 1; - _strokeWidth = 1; + _strokeWidth = 0; } return self; } @@ -175,6 +175,98 @@ CGContextRestoreGState(context); } +- (void)renderLayerTo:(CGContextRef)context +{ + // todo: add detection if path has changed since last update. + CGPathRef path = [self getPath:context]; + if ((!self.fill && !self.stroke) || !path) { + return; + } + + if ([self getSvgView].responsible) { + // Add path to hitArea + CGMutablePathRef hitArea = CGPathCreateMutableCopy(path); + if (self.stroke && self.strokeWidth) { + // Add stroke to hitArea + CGPathRef strokePath = CGPathCreateCopyByStrokingPath(hitArea, nil, self.strokeWidth, self.strokeLinecap, self.strokeLinejoin, self.strokeMiterlimit); + CGPathAddPath(hitArea, nil, strokePath); + CGPathRelease(strokePath); + } + + self.hitArea = CFAutorelease(CGPathCreateCopy(hitArea)); + CGPathRelease(hitArea); + } + + if (self.opacity == 0) { + return; + } + + CGPathDrawingMode mode = kCGPathStroke; + BOOL fillColor = YES; + + if (self.fill) { + mode = self.fillRule == kRNSVGCGFCRuleEvenodd ? kCGPathEOFill : kCGPathFill; + fillColor = [self.fill applyFillColor:context opacity:self.fillOpacity]; + + if (!fillColor) { + [self clip:context]; + + CGContextSaveGState(context); + CGContextAddPath(context, path); + CGContextClip(context); + RNSVGBrushConverter *brushConverter = [[self getSvgView] getDefinedBrushConverter:[self.fill brushRef]]; + [self.fill paint:context opacity:self.fillOpacity brushConverter:brushConverter]; + CGContextRestoreGState(context); + if (!self.stroke) { + return; + } + } + } + + if (self.stroke && self.strokeWidth) { + CGContextSetLineWidth(context, self.strokeWidth); + CGContextSetLineCap(context, self.strokeLinecap); + CGContextSetLineJoin(context, self.strokeLinejoin); + RNSVGCGFloatArray dash = self.strokeDasharray; + + if (dash.count) { + CGContextSetLineDash(context, self.strokeDashoffset, dash.array, dash.count); + } + + if (!fillColor) { + CGContextAddPath(context, path); + CGContextReplacePathWithStrokedPath(context); + CGContextClip(context); + } + + if ([self.stroke applyStrokeColor:context opacity:self.strokeOpacity]) { + if (mode == kCGPathFill) { + mode = kCGPathFillStroke; + } else if (mode == kCGPathEOFill) { + mode = kCGPathEOFillStroke; + } + } else { + // draw fill + [self clip:context]; + CGContextAddPath(context, path); + CGContextDrawPath(context, mode); + + // draw stroke + CGContextAddPath(context, path); + CGContextReplacePathWithStrokedPath(context); + CGContextClip(context); + RNSVGBrushConverter *brushConverter = [[self getSvgView] getDefinedBrushConverter:[self.stroke brushRef]]; + [self.stroke paint:context opacity:self.strokeOpacity brushConverter:brushConverter]; + return; + } + } + + [self clip:context]; + CGContextAddPath(context, path); + CGContextDrawPath(context, mode); +} + + // hitTest delagate - (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event { @@ -291,9 +383,4 @@ _attributeList = [_propList copy]; } -- (void)renderLayerTo:(CGContextRef)context -{ - // abstract -} - @end diff --git a/ios/Shapes/RNSVGCircle.h b/ios/Shapes/RNSVGCircle.h index b1f086b..8d19580 100644 --- a/ios/Shapes/RNSVGCircle.h +++ b/ios/Shapes/RNSVGCircle.h @@ -10,7 +10,7 @@ #import "RNSVGPath.h" -@interface RNSVGCircle : RNSVGPath +@interface RNSVGCircle : RNSVGRenderable @property (nonatomic, strong) NSString* cx; @property (nonatomic, strong) NSString* cy; diff --git a/ios/Shapes/RNSVGEllipse.h b/ios/Shapes/RNSVGEllipse.h index 17d279b..67c246a 100644 --- a/ios/Shapes/RNSVGEllipse.h +++ b/ios/Shapes/RNSVGEllipse.h @@ -10,7 +10,7 @@ #import "RNSVGPath.h" -@interface RNSVGEllipse : RNSVGPath +@interface RNSVGEllipse : RNSVGRenderable @property (nonatomic, strong) NSString* cx; @property (nonatomic, strong) NSString* cy; @property (nonatomic, strong) NSString* rx; diff --git a/ios/Shapes/RNSVGLine.h b/ios/Shapes/RNSVGLine.h index 0263e3a..e1df2cc 100644 --- a/ios/Shapes/RNSVGLine.h +++ b/ios/Shapes/RNSVGLine.h @@ -10,7 +10,7 @@ #import "RNSVGPath.h" -@interface RNSVGLine : RNSVGPath +@interface RNSVGLine : RNSVGRenderable @property (nonatomic, strong) NSString* x1; @property (nonatomic, strong) NSString* y1; @property (nonatomic, strong) NSString* x2; diff --git a/ios/Shapes/RNSVGRect.h b/ios/Shapes/RNSVGRect.h index c63c1b8..224aeef 100644 --- a/ios/Shapes/RNSVGRect.h +++ b/ios/Shapes/RNSVGRect.h @@ -10,7 +10,7 @@ #import "RNSVGPath.h" -@interface RNSVGRect : RNSVGPath +@interface RNSVGRect : RNSVGRenderable @property (nonatomic, strong) NSString* x; @property (nonatomic, strong) NSString* y; diff --git a/lib/extract/extractStroke.js b/lib/extract/extractStroke.js index 94b0856..ad97886 100644 --- a/lib/extract/extractStroke.js +++ b/lib/extract/extractStroke.js @@ -20,10 +20,6 @@ export default function(props) { let strokeWidth = +props.strokeWidth; - if (_.isNil(props.strokeWidth)) { - strokeWidth = null; - } - let strokeDasharray = props.strokeDasharray; if (typeof strokeDasharray === 'string') { @@ -41,7 +37,7 @@ export default function(props) { strokeLinecap: caps[props.strokeLinecap] || 0, strokeLinejoin: joins[props.strokeLinejoin] || 0, strokeDasharray: strokeDasharray || null, - strokeWidth: strokeWidth, + strokeWidth: strokeWidth || null, strokeDashoffset: strokeDasharray ? (+props.strokeDashoffset || 0) : null, strokeMiterlimit: props.strokeMiterlimit || 4 };