diff --git a/ios/Elements/RNSVGGroup.m b/ios/Elements/RNSVGGroup.m index 14e64429..e8786836 100644 --- a/ios/Elements/RNSVGGroup.m +++ b/ios/Elements/RNSVGGroup.m @@ -72,12 +72,13 @@ - (void)pushGlyphContext { - [[[self getTextRoot] getGlyphContext] pushContext:self font:self.font]; + __weak typeof(self) weakSelf = self; + [[self.textRoot getGlyphContext] pushContext:weakSelf font:self.font]; } - (void)popGlyphContext { - [[[self getTextRoot] getGlyphContext] popContext]; + [[self.textRoot getGlyphContext] popContext]; } - (void)renderPathTo:(CGContextRef)context diff --git a/ios/RNSVGNode.h b/ios/RNSVGNode.h index 7f4dbd4d..7b5de41e 100644 --- a/ios/RNSVGNode.h +++ b/ios/RNSVGNode.h @@ -38,10 +38,10 @@ extern CGFloat const RNSVG_DEFAULT_FONT_SIZE; * RNSVGSvgView which ownes current RNSVGNode */ @property (nonatomic, readonly, weak) RNSVGSvgView *svgView; +@property (nonatomic, readonly, weak) RNSVGGroup *textRoot; - (void)invalidate; -- (RNSVGGroup *)getTextRoot; - (RNSVGGroup *)getParentTextRoot; - (void)renderTo:(CGContextRef)context; diff --git a/ios/RNSVGNode.m b/ios/RNSVGNode.m index afffc460..824c0b04 100644 --- a/ios/RNSVGNode.m +++ b/ios/RNSVGNode.m @@ -14,11 +14,11 @@ @interface RNSVGNode() @property (nonatomic, readwrite, weak) RNSVGSvgView *svgView; +@property (nonatomic, readwrite, weak) RNSVGGroup *textRoot; @end @implementation RNSVGNode { - RNSVGGroup *_textRoot; RNSVGGlyphContext *glyphContext; BOOL _transparent; CGPathRef _cachedClipPath; @@ -59,23 +59,25 @@ CGFloat const RNSVG_DEFAULT_FONT_SIZE = 12; [container invalidate]; } -- (RNSVGGroup *)getTextRoot +- (RNSVGGroup *)textRoot { + if (_textRoot) { + return _textRoot; + } + RNSVGNode* node = self; - if (_textRoot == nil) { - while (node != nil) { - if ([node isKindOfClass:[RNSVGGroup class]] && [((RNSVGGroup*) node) getGlyphContext] != nil) { - _textRoot = (RNSVGGroup*)node; - break; - } - - UIView* parent = [node superview]; - - if (![node isKindOfClass:[RNSVGNode class]]) { - node = nil; - } else { - node = (RNSVGNode*)parent; - } + while (node != nil) { + if ([node isKindOfClass:[RNSVGGroup class]] && [((RNSVGGroup*) node) getGlyphContext] != nil) { + _textRoot = (RNSVGGroup*)node; + break; + } + + UIView* parent = [node superview]; + + if (![node isKindOfClass:[RNSVGNode class]]) { + node = nil; + } else { + node = (RNSVGNode*)parent; } } @@ -88,13 +90,13 @@ CGFloat const RNSVG_DEFAULT_FONT_SIZE = 12; if (![parent isKindOfClass:[RNSVGGroup class]]) { return nil; } else { - return [parent getTextRoot]; + return parent.textRoot; } } - (CGFloat)getFontSizeFromContext { - RNSVGGroup* root = [self getTextRoot]; + RNSVGGroup* root = self.textRoot; if (root == nil) { return RNSVG_DEFAULT_FONT_SIZE; } @@ -274,7 +276,7 @@ CGFloat const RNSVG_DEFAULT_FONT_SIZE = 12; - (CGFloat)getContextWidth { - RNSVGGroup * root = [self getTextRoot]; + RNSVGGroup * root = self.textRoot; RNSVGGlyphContext * gc = [root getGlyphContext]; if (root == nil || gc == nil) { return CGRectGetWidth([self.svgView getContextBounds]); @@ -285,7 +287,7 @@ CGFloat const RNSVG_DEFAULT_FONT_SIZE = 12; - (CGFloat)getContextHeight { - RNSVGGroup * root = [self getTextRoot]; + RNSVGGroup * root = self.textRoot; RNSVGGlyphContext * gc = [root getGlyphContext]; if (root == nil || gc == nil) { return CGRectGetHeight([self.svgView getContextBounds]); diff --git a/ios/Text/RNSVGTSpan.m b/ios/Text/RNSVGTSpan.m index bd4deaee..5f6e6829 100644 --- a/ios/Text/RNSVGTSpan.m +++ b/ios/Text/RNSVGTSpan.m @@ -96,7 +96,7 @@ static double RNSVGTSpan_radToDeg = 180 / M_PI; // Create a dictionary for this font CTFontRef fontRef = [self getFontFromContext]; CGMutablePathRef path = CGPathCreateMutable(); - RNSVGGlyphContext* gc = [[self getTextRoot] getGlyphContext]; + RNSVGGlyphContext* gc = [self.textRoot getGlyphContext]; RNSVGFontData* font = [gc getFont]; NSUInteger n = str.length; /* diff --git a/ios/Text/RNSVGText.m b/ios/Text/RNSVGText.m index 28f665a9..21cec3c7 100644 --- a/ios/Text/RNSVGText.m +++ b/ios/Text/RNSVGText.m @@ -15,7 +15,6 @@ @implementation RNSVGText { - RNSVGText *_textRoot; RNSVGGlyphContext *_glyphContext; } @@ -76,20 +75,19 @@ [self popGlyphContext]; } -- (RNSVGText *)getTextRoot +// TODO: Optimisation required +- (RNSVGText *)textRoot { - if (!_textRoot) { - _textRoot = self; - while (_textRoot && [_textRoot class] != [RNSVGText class]) { - if (![_textRoot isKindOfClass:[RNSVGText class]]) { - //todo: throw exception here - break; - } - _textRoot = (RNSVGText*)[_textRoot superview]; + RNSVGText *root = self; + while (root && [root class] != [RNSVGText class]) { + if (![root isKindOfClass:[RNSVGText class]]) { + //todo: throw exception here + break; } + root = (RNSVGText*)[root superview]; } - return _textRoot; + return root; } - (NSString*) getAlignmentBaseline @@ -147,23 +145,23 @@ - (void)pushGlyphContext { - [[[self getTextRoot] getGlyphContext] pushContext:self - font:self.font - x:self.positionX - y:self.positionY - deltaX:self.deltaX - deltaY:self.deltaY - rotate:self.rotate]; + [[self.textRoot getGlyphContext] pushContext:self + font:self.font + x:self.positionX + y:self.positionY + deltaX:self.deltaX + deltaY:self.deltaY + rotate:self.rotate]; } - (void)popGlyphContext { - [[[self getTextRoot] getGlyphContext] popContext]; + [[self.textRoot getGlyphContext] popContext]; } - (CTFontRef)getFontFromContext { - return [[[self getTextRoot] getGlyphContext] getGlyphFont]; + return [[self.textRoot getGlyphContext] getGlyphFont]; } @end