Skip to content

Commit

Permalink
Allow nesting React-Native View components inside svg
Browse files Browse the repository at this point in the history
  • Loading branch information
msand committed Feb 21, 2018
1 parent ee1d1f7 commit b5eeb1e
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 43 deletions.
4 changes: 2 additions & 2 deletions ios/Elements/RNSVGGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@

@property (nonatomic, strong) NSDictionary *font;

- (void)renderPathTo:(CGContextRef)context;
- (void)renderGroupTo:(CGContextRef)context;
- (void)renderPathTo:(CGContextRef)context rect:(CGRect)rect;
- (void)renderGroupTo:(CGContextRef)context rect:(CGRect)rect;

- (RNSVGGlyphContext *)getGlyphContext;
- (void)pushGlyphContext;
Expand Down
14 changes: 7 additions & 7 deletions ios/Elements/RNSVGGroup.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ - (void)setFont:(NSDictionary*)font
_font = font;
}

- (void)renderLayerTo:(CGContextRef)context
- (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect
{
[self clip:context];
[self setupGlyphContext:context];
[self renderGroupTo:context];
[self renderGroupTo:context rect:rect];
}

- (void)renderGroupTo:(CGContextRef)context
- (void)renderGroupTo:(CGContextRef)context rect:(CGRect)rect
{
[self pushGlyphContext];
RNSVGSvgView* svg = [self getSvgView];
Expand All @@ -45,7 +45,7 @@ - (void)renderGroupTo:(CGContextRef)context
[(RNSVGRenderable*)node mergeProperties:self];
}

[svgNode renderTo:context];
[svgNode renderTo:context rect:rect];

if ([node isKindOfClass:[RNSVGRenderable class]]) {
[(RNSVGRenderable*)node resetProperties];
Expand All @@ -56,7 +56,7 @@ - (void)renderGroupTo:(CGContextRef)context
CGContextClipToRect(context, rect);
[svgView drawToContext:context withRect:(CGRect)rect];
} else {
RCTLogWarn(@"Not a RNSVGNode: %@", node.class);
[node drawRect:rect];
}

return YES;
Expand Down Expand Up @@ -90,9 +90,9 @@ - (void)popGlyphContext
[[[self getTextRoot] getGlyphContext] popContext];
}

- (void)renderPathTo:(CGContextRef)context
- (void)renderPathTo:(CGContextRef)context rect:(CGRect)rect
{
[super renderLayerTo:context];
[super renderLayerTo:context rect:rect];
}

- (CGPathRef)getPath:(CGContextRef)context
Expand Down
6 changes: 3 additions & 3 deletions ios/Elements/RNSVGSvgView.m
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ - (void)drawToContext:(CGContextRef)context withRect:(CGRect)rect {
for (UIView *node in self.subviews) {
if ([node isKindOfClass:[RNSVGNode class]]) {
RNSVGNode *svg = (RNSVGNode *)node;
[svg renderTo:context];
[svg renderTo:context rect:rect];
} else {
[node drawRect:rect];
}
}
}
Expand All @@ -156,8 +158,6 @@ - (void)drawRect:(CGRect)rect
}

[svg parseReference];
} else {
RCTLogWarn(@"Not a RNSVGNode: %@", node.class);
}
}

Expand Down
6 changes: 3 additions & 3 deletions ios/Elements/RNSVGSymbol.m
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ - (void)setMeetOrSlice:(RNSVGVBMOS)meetOrSlice
_meetOrSlice = meetOrSlice;
}

- (void)renderTo:(CGContextRef)context
- (void)renderTo:(CGContextRef)context rect:(CGRect)rect
{
// Do not render Symbol
}

- (void)renderSymbolTo:(CGContextRef)context width:(CGFloat)width height:(CGFloat)height
{
CGRect eRect = CGRectMake(0, 0, width, height);
if (self.align) {
CGRect eRect = CGRectMake(0, 0, width, height);

CGAffineTransform viewBoxTransform = [RNSVGViewBox getTransform:CGRectMake(self.minX, self.minY, self.vbWidth, self.vbHeight)
eRect:eRect
Expand All @@ -89,7 +89,7 @@ - (void)renderSymbolTo:(CGContextRef)context width:(CGFloat)width height:(CGFloa

CGContextConcatCTM(context, viewBoxTransform);
}
[self renderGroupTo:context];
[self renderGroupTo:context rect:eRect];
}

@end
Expand Down
4 changes: 2 additions & 2 deletions ios/Elements/RNSVGUse.m
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ - (void)setHref:(NSString *)href
}


- (void)renderLayerTo:(CGContextRef)context
- (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect
{
RNSVGNode* template = [[self getSvgView] getDefinedTemplate:self.href];
if (template) {
Expand All @@ -37,7 +37,7 @@ - (void)renderLayerTo:(CGContextRef)context
RNSVGSymbol *symbol = (RNSVGSymbol*)template;
[symbol renderSymbolTo:context width:[self relativeOnWidth:self.width] height:[self relativeOnWidth:self.height]];
} else {
[template renderTo:context];
[template renderTo:context rect:rect];
}

if ([template isKindOfClass:[RNSVGRenderable class]]) {
Expand Down
4 changes: 2 additions & 2 deletions ios/RNSVGNode.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ extern CGFloat const RNSVG_DEFAULT_FONT_SIZE;
- (RNSVGGroup *)getTextRoot;
- (RNSVGGroup *)getParentTextRoot;

- (void)renderTo:(CGContextRef)context;
- (void)renderTo:(CGContextRef)context rect:(CGRect)rect;

/**
* renderTo will take opacity into account and draw renderLayerTo off-screen if there is opacity
* specified, then composite that onto the context. renderLayerTo always draws at opacity=1.
* @abstract
*/
- (void)renderLayerTo:(CGContextRef)context;
- (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect;

/**
* get clipPath from cache
Expand Down
16 changes: 4 additions & 12 deletions ios/RNSVGNode.m
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ - (void)endTransparencyLayer:(CGContextRef)context
}
}

- (void)renderTo:(CGContextRef)context
- (void)renderTo:(CGContextRef)context rect:(CGRect)rect
{
// abstract
}
Expand Down Expand Up @@ -199,7 +199,7 @@ - (CGPathRef)getPath: (CGContextRef)context
return nil;
}

- (void)renderLayerTo:(CGContextRef)context
- (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect
{
// abstract
}
Expand Down Expand Up @@ -313,16 +313,8 @@ - (void)parseReference
- (void)traverseSubviews:(BOOL (^)(__kindof UIView *node))block
{
for (UIView *node in self.subviews) {
if ([node isKindOfClass:[RNSVGNode class]]) {
if (!block(node)) {
break;
}
} else if ([node isKindOfClass:[RNSVGSvgView class]]) {
if (!block(node)) {
break;
}
} else {
RCTLogWarn(@"Not a RNSVGNode: %@", node.class);
if (!block(node)) {
break;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions ios/RNSVGRenderable.m
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,22 @@ - (void)dealloc
}
}

- (void)renderTo:(CGContextRef)context
- (void)renderTo:(CGContextRef)context rect:(CGRect)rect
{
// This needs to be painted on a layer before being composited.
CGContextSaveGState(context);
CGContextConcatCTM(context, self.matrix);
CGContextSetAlpha(context, self.opacity);

[self beginTransparencyLayer:context];
[self renderLayerTo:context];
[self renderLayerTo:context rect:rect];
[self endTransparencyLayer:context];

CGContextRestoreGState(context);
}


- (void)renderLayerTo:(CGContextRef)context
- (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect
{
if (!self.fill && !self.stroke) {
return;
Expand Down
6 changes: 3 additions & 3 deletions ios/Text/RNSVGTSpan.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ - (void)setContent:(NSString *)content
_content = content;
}

- (void)renderLayerTo:(CGContextRef)context
- (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect
{
if (self.content) {
[self renderPathTo:context];
[self renderPathTo:context rect:rect];
} else {
[self clip:context];
[self renderGroupTo:context];
[self renderGroupTo:context rect:rect];
}
}

Expand Down
8 changes: 4 additions & 4 deletions ios/Text/RNSVGText.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ @implementation RNSVGText
RNSVGGlyphContext *_glyphContext;
}

- (void)renderLayerTo:(CGContextRef)context
- (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect
{
[self clip:context];
CGContextSaveGState(context);
[self setupGlyphContext:context];

CGPathRef path = [self getGroupPath:context];
[self renderGroupTo:context];
[self renderGroupTo:context rect:rect];
[self releaseCachedPath];
CGContextRestoreGState(context);

Expand Down Expand Up @@ -69,10 +69,10 @@ - (CGPathRef)getPath:(CGContextRef)context
return (CGPathRef)CFAutorelease(CGPathCreateCopyByTransformingPath(groupPath, &CGAffineTransformIdentity));
}

- (void)renderGroupTo:(CGContextRef)context
- (void)renderGroupTo:(CGContextRef)context rect:(CGRect)rect
{
[self pushGlyphContext];
[super renderGroupTo:context];
[super renderGroupTo:context rect:rect];
[self popGlyphContext];
}

Expand Down
4 changes: 2 additions & 2 deletions ios/Text/RNSVGTextPath.m
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ - (void)getPathLength:(CGFloat*)lengthP lineCount:(NSUInteger*)lineCountP length
*linesP = lines;
}

- (void)renderLayerTo:(CGContextRef)context
- (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect
{
[self renderGroupTo:context];
[self renderGroupTo:context rect:rect];
}

- (CGPathRef)getPath:(CGContextRef)context
Expand Down

0 comments on commit b5eeb1e

Please sign in to comment.