From e3026c0bb5725d4762d5fc3488a9df02018c747d Mon Sep 17 00:00:00 2001 From: Horcrux Date: Thu, 12 Jan 2017 20:09:57 +0800 Subject: [PATCH] Fix TextPath bug when using lines --- ios/Text/RNSVGGlyphContext.m | 5 +++++ ios/Text/RNSVGTSpan.h | 2 -- ios/Text/RNSVGTSpan.m | 29 ++++++++++++++--------------- ios/Text/RNSVGText.h | 4 ---- ios/Text/RNSVGText.m | 12 +++--------- ios/Utils/RNSVGPathParser.m | 11 ++++------- 6 files changed, 26 insertions(+), 37 deletions(-) diff --git a/ios/Text/RNSVGGlyphContext.m b/ios/Text/RNSVGGlyphContext.m index 8766d385..cb698b90 100644 --- a/ios/Text/RNSVGGlyphContext.m +++ b/ios/Text/RNSVGGlyphContext.m @@ -64,6 +64,11 @@ [_locationContext removeLastObject]; [_deltaXContext removeLastObject]; [_deltaYContext removeLastObject]; + [_xContext removeLastObject]; + + if (_xContext.count) { + [_xContext replaceObjectAtIndex:_xContext.count - 1 withObject:x]; + } if (_locationContext.count) { _currentLocation = [[_locationContext lastObject] CGPointValue]; diff --git a/ios/Text/RNSVGTSpan.h b/ios/Text/RNSVGTSpan.h index 062c231e..3e04fc0b 100644 --- a/ios/Text/RNSVGTSpan.h +++ b/ios/Text/RNSVGTSpan.h @@ -14,6 +14,4 @@ @property (nonatomic, strong) NSString *content; -- (CGAffineTransform)getTextPathTransform:(CGFloat)distance; - @end diff --git a/ios/Text/RNSVGTSpan.m b/ios/Text/RNSVGTSpan.m index ee3f2b34..303881cc 100644 --- a/ios/Text/RNSVGTSpan.m +++ b/ios/Text/RNSVGTSpan.m @@ -107,17 +107,16 @@ glyphPoint = [self getGlyphPointFromContext:positions[i] glyphWidth:CGRectGetWidth(CGPathGetBoundingBox(letter))]; - CGAffineTransform textPathTransform = [self getTextPathTransform:glyphPoint.x]; - - if ([RNSVGBezierTransformer hasReachedEnd:textPathTransform]) { - break; - } else if ([RNSVGBezierTransformer hasReachedStart:textPathTransform]) { - continue; - } - + CGAffineTransform textPathTransform = CGAffineTransformIdentity; CGAffineTransform transform; - if (_bezierTransformer) { + textPathTransform = [_bezierTransformer getTransformAtDistance:glyphPoint.x]; + if ([self textPathHasReachedEnd]) { + break; + } else if (![self textPathHasReachedStart]) { + continue; + } + textPathTransform = CGAffineTransformConcat(CGAffineTransformMakeTranslation(0, glyphPoint.y), textPathTransform); transform = CGAffineTransformScale(textPathTransform, 1.0, -1.0); } else { @@ -162,14 +161,14 @@ } } -- (CGAffineTransform)getTextPathTransform:(CGFloat)distance +- (BOOL)textPathHasReachedEnd { - if (_bezierTransformer) { - return [_bezierTransformer getTransformAtDistance:distance]; - } - - return CGAffineTransformIdentity; + return [_bezierTransformer hasReachedEnd]; +} +- (BOOL)textPathHasReachedStart +{ + return [_bezierTransformer hasReachedStart]; } @end diff --git a/ios/Text/RNSVGText.h b/ios/Text/RNSVGText.h index 45d3e0c9..f6d843b1 100644 --- a/ios/Text/RNSVGText.h +++ b/ios/Text/RNSVGText.h @@ -20,10 +20,6 @@ @property (nonatomic, strong) NSString *positionY; @property (nonatomic, strong) NSDictionary *font; -@property (nonatomic, assign) CGFloat lastX; -@property (nonatomic, assign) CGFloat lastY; -@property (nonatomic, assign) NSUInteger lastIndex; - - (RNSVGText *)getTextRoot; - (void)releaseCachedPath; - (CGPathRef)getGroupPath:(CGContextRef)context; diff --git a/ios/Text/RNSVGText.m b/ios/Text/RNSVGText.m index de9e7b0b..2392017e 100644 --- a/ios/Text/RNSVGText.m +++ b/ios/Text/RNSVGText.m @@ -31,7 +31,7 @@ [self setupGlyphContext:context]; CGPathRef path = [self getGroupPath:context]; - CGAffineTransform transform = [self getAlignTransform:context path:path]; + CGAffineTransform transform = [self getAlignTransform:path]; CGContextConcatCTM(context, transform); [self renderGroupTo:context]; [self releaseCachedPath]; @@ -73,7 +73,7 @@ { [self setupGlyphContext:context]; CGPathRef groupPath = [self getGroupPath:context]; - CGAffineTransform transform = [self getAlignTransform:context path:groupPath]; + CGAffineTransform transform = [self getAlignTransform:groupPath]; [self releaseCachedPath]; return (CGPathRef)CFAutorelease(CGPathCreateCopyByTransformingPath(groupPath, &transform)); @@ -86,7 +86,7 @@ [self popGlyphContext]; } -- (CGAffineTransform)getAlignTransform:(CGContextRef)context path:(CGPathRef)path +- (CGAffineTransform)getAlignTransform:(CGPathRef)path { CGFloat width = CGRectGetWidth(CGPathGetBoundingBox(path)); CGFloat x = 0; @@ -152,12 +152,6 @@ [[[self getTextRoot] getGlyphContext] popContext]; } -- (CGPoint)getGlyphLineOffset -{ - RNSVGText *text = [self getTextRoot]; - return CGPointMake(text.lastX, text.lastY); -} - - (CTFontRef)getFontFromContext { return [[[self getTextRoot] getGlyphContext] getGlyphFont]; diff --git a/ios/Utils/RNSVGPathParser.m b/ios/Utils/RNSVGPathParser.m index e34e4420..01027c61 100644 --- a/ios/Utils/RNSVGPathParser.m +++ b/ios/Utils/RNSVGPathParser.m @@ -168,17 +168,15 @@ } - (void)lineTo:(CGPathRef)path x:(double)x y:(double)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, - _lastStartPoint, - destination - ]]; + [_bezierCurves addObject: @[destination, destination, destination]]; } - (void)curve:(CGPathRef)path c1x:(double)c1x c1y:(double)c1y c2x:(double)c2x c2y:(double)c2y ex:(double)ex ey:(double)ey @@ -205,7 +203,6 @@ _penY = ey; CGPathAddCurveToPoint(path, nil, c1x, c1y, c2x, c2y, ex, ey); - [_bezierCurves addObject: @[ [NSValue valueWithCGPoint:CGPointMake(c1x, c1y)], [NSValue valueWithCGPoint:CGPointMake(c2x, c2y)], @@ -389,7 +386,7 @@ _penY = _penDownY; _penDownSet = NO; CGPathCloseSubpath(path); - [_bezierCurves addObject: @[]]; + [_bezierCurves addObject: @[_lastStartPoint, _lastStartPoint, _lastStartPoint]]; } }