Skip to content

Commit

Permalink
fix: crop rect on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
jakex7 committed Jun 28, 2024
1 parent 3178805 commit 1c2ca46
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
22 changes: 9 additions & 13 deletions apple/Filters/RNSVGFilter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -108,27 +108,23 @@ - (CIImage *)applyFilter:(CIImage *)img
CIFilter *crop = [CIFilter filterWithName:@"CICrop"];
[crop setDefaults];
[crop setValue:result forKey:@"inputImage"];
CIVector *cropRect;
CGAffineTransform inverseMatrix = CGAffineTransformMake(1, 0, 0, -1, 0, canvasBounds.size.height);

CGFloat scaleX = ctm.a, scaleY = fabs(ctm.d);
CGFloat x, y, width, height;
CGRect cropCGRect;
if (self.filterUnits == kRNSVGUnitsUserSpaceOnUse) {
x = [self relativeOn:self.x relative:canvasBounds.size.width];
y = [self relativeOn:self.y relative:canvasBounds.size.height];
width = [self relativeOn:self.width relative:canvasBounds.size.width];
height = [self relativeOn:self.height relative:canvasBounds.size.height];
cropCGRect = CGRectMake(x, y, width, height);
x = [self relativeOn:self.x relative:canvasBounds.size.width / scaleX];
y = [self relativeOn:self.y relative:canvasBounds.size.height / scaleY];
width = [self relativeOn:self.width relative:canvasBounds.size.width / scaleX];
height = [self relativeOn:self.height relative:canvasBounds.size.height / scaleY];
} else { // kRNSVGUnitsObjectBoundingBox
x = [self relativeOnFraction:self.x relative:renderableBounds.size.width];
y = [self relativeOnFraction:self.y relative:renderableBounds.size.height];
x = renderableBounds.origin.x + [self relativeOnFraction:self.x relative:renderableBounds.size.width];
y = renderableBounds.origin.y + [self relativeOnFraction:self.y relative:renderableBounds.size.height];
width = [self relativeOnFraction:self.width relative:renderableBounds.size.width];
height = [self relativeOnFraction:self.height relative:renderableBounds.size.height];
cropCGRect = CGRectApplyAffineTransform(cropCGRect, inverseMatrix);
cropCGRect = CGRectMake(x, y, width, height);
}
CGRect cropCGRect = CGRectMake(x, y, width, height);
cropCGRect = CGRectApplyAffineTransform(cropCGRect, ctm);
cropRect = [CIVector vectorWithCGRect:cropCGRect];
CIVector *cropRect = [CIVector vectorWithCGRect:cropCGRect];
[crop setValue:cropRect forKey:@"inputRectangle"];

return [crop valueForKey:@"outputImage"];
Expand Down
4 changes: 2 additions & 2 deletions apple/RNSVGRenderable.mm
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,8 @@ - (void)renderTo:(CGContextRef)context rect:(CGRect)rect

content = [filterNode applyFilter:content
backgroundImg:background
renderableBounds:self.bounds
canvasBounds:rect
renderableBounds:self.pathBounds
canvasBounds:scaledRect
ctm:currentCTM];

CGImageRelease(contentImage);
Expand Down

0 comments on commit 1c2ca46

Please sign in to comment.