Skip to content

Commit

Permalink
[ios] Fix clipped bounds calculation for gradients
Browse files Browse the repository at this point in the history
  • Loading branch information
JackWillie committed Sep 1, 2018
1 parent 42ab9ff commit a0e3164
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion ios/Brushes/RNSVGBrush.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
* be clipped.
* @abstract
*/
- (void)paint:(CGContextRef)context opacity:(CGFloat)opacity painter:(RNSVGPainter *)painter;
- (void)paint:(CGContextRef)context opacity:(CGFloat)opacity painter:(RNSVGPainter *)painter bounds:(CGRect)bounds;

@end
2 changes: 1 addition & 1 deletion ios/Brushes/RNSVGPainter.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

- (instancetype)initWithPointsArray:(NSArray<NSString *> *)pointsArray NS_DESIGNATED_INITIALIZER;

- (void)paint:(CGContextRef)context;
- (void)paint:(CGContextRef)context bounds:(CGRect)bounds;

- (void)setUnits:(RNSVGUnits)unit;

Expand Down
18 changes: 9 additions & 9 deletions ios/Brushes/RNSVGPainter.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,20 @@ - (void)setRadialGradientColors:(NSArray<NSNumber *> *)colors
_colors = colors;
}

- (void)paint:(CGContextRef)context
- (void)paint:(CGContextRef)context bounds:(CGRect)bounds
{
if (_type == kRNSVGLinearGradient) {
[self paintLinearGradient:context];
[self paintLinearGradient:context bounds:(CGRect)bounds];
} else if (_type == kRNSVGRadialGradient) {
[self paintRidialGradient:context];
[self paintRidialGradient:context bounds:(CGRect)bounds];
} else if (_type == kRNSVGPattern) {
// todo:
}
}

- (CGRect)getPaintRect:(CGContextRef)context
- (CGRect)getPaintRect:(CGContextRef)context bounds:(CGRect)bounds
{
CGRect rect = _useObjectBoundingBox ? CGContextGetClipBoundingBox(context) : _userSpaceBoundingBox;
CGRect rect = _useObjectBoundingBox ? bounds : _userSpaceBoundingBox;
float height = CGRectGetHeight(rect);
float width = CGRectGetWidth(rect);
float x = 0.0;
Expand All @@ -93,13 +93,13 @@ - (CGRect)getPaintRect:(CGContextRef)context
return CGRectMake(x, y, width, height);
}

- (void)paintLinearGradient:(CGContextRef)context
- (void)paintLinearGradient:(CGContextRef)context bounds:(CGRect)bounds
{

CGGradientRef gradient = CGGradientRetain([RCTConvert RNSVGCGGradient:_colors offset:0]);
CGGradientDrawingOptions extendOptions = kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation;

CGRect rect = [self getPaintRect:context];
CGRect rect = [self getPaintRect:context bounds:bounds];
float height = CGRectGetHeight(rect);
float width = CGRectGetWidth(rect);
float offsetX = CGRectGetMinX(rect);
Expand All @@ -124,12 +124,12 @@ - (void)paintLinearGradient:(CGContextRef)context
CGGradientRelease(gradient);
}

- (void)paintRidialGradient:(CGContextRef)context
- (void)paintRidialGradient:(CGContextRef)context bounds:(CGRect)bounds
{
CGGradientRef gradient = CGGradientRetain([RCTConvert RNSVGCGGradient:_colors offset:0]);
CGGradientDrawingOptions extendOptions = kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation;

CGRect rect = [self getPaintRect:context];
CGRect rect = [self getPaintRect:context bounds:bounds];
float height = CGRectGetHeight(rect);
float width = CGRectGetWidth(rect);
float offsetX = CGRectGetMinX(rect);
Expand Down
10 changes: 5 additions & 5 deletions ios/Brushes/RNSVGPainterBrush.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,22 @@ - (instancetype)initWithArray:(NSArray *)array
self.class, NSStringFromSelector(_cmd), array);
return nil;
}

self.brushRef = [array objectAtIndex:1];
}
return self;
}

- (void)paint:(CGContextRef)context opacity:(CGFloat)opacity painter:(RNSVGPainter *)painter
- (void)paint:(CGContextRef)context opacity:(CGFloat)opacity painter:(RNSVGPainter *)painter bounds:(CGRect)bounds
{
BOOL transparency = opacity < 1;
if (transparency) {
CGContextSetAlpha(context, opacity);
CGContextBeginTransparencyLayer(context, NULL);
}
[painter paint:context];

[painter paint:context bounds:(CGRect)bounds];

if (transparency) {
CGContextEndTransparencyLayer(context);
}
Expand Down
2 changes: 2 additions & 0 deletions ios/RNSVGRenderable.m
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ - (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect
[self.fill paint:context
opacity:self.fillOpacity
painter:[self.svgView getDefinedPainter:self.fill.brushRef]
bounds:pathBounding
];
CGContextRestoreGState(context);

Expand Down Expand Up @@ -256,6 +257,7 @@ - (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect
[self.stroke paint:context
opacity:self.strokeOpacity
painter:[self.svgView getDefinedPainter:self.stroke.brushRef]
bounds:pathBounding
];
return;
}
Expand Down

0 comments on commit a0e3164

Please sign in to comment.