From 85e7943448621f0d96adea89f89127ad07eadba6 Mon Sep 17 00:00:00 2001 From: Mikael Sand Date: Sat, 11 Jan 2020 21:35:44 +0200 Subject: [PATCH] fix: foreignObject clipping and transform --- .../com/horcrux/svg/ForeignObjectView.java | 22 +++++++++++++++++++ ios/Elements/RNSVGForeignObject.m | 15 ++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/com/horcrux/svg/ForeignObjectView.java b/android/src/main/java/com/horcrux/svg/ForeignObjectView.java index a1b1e695..59cbfb6c 100644 --- a/android/src/main/java/com/horcrux/svg/ForeignObjectView.java +++ b/android/src/main/java/com/horcrux/svg/ForeignObjectView.java @@ -10,6 +10,11 @@ package com.horcrux.svg; import android.annotation.SuppressLint; +import android.graphics.Canvas; +import android.graphics.Paint; +import android.view.View; + +import androidx.annotation.NonNull; import com.facebook.react.bridge.Dynamic; import com.facebook.react.bridge.ReactContext; @@ -27,6 +32,23 @@ class ForeignObjectView extends GroupView { super(reactContext); } + @Override + void draw(Canvas canvas, Paint paint, float opacity) { + float x = (float)relativeOnWidth(mX); + float y = (float)relativeOnHeight(mY); + float w = (float)relativeOnWidth(mW); + float h = (float)relativeOnHeight(mH); + canvas.translate(x, y); + canvas.clipRect(0, 0, w, h); + super.draw(canvas, paint, opacity); + } + + @Override + public void onDescendantInvalidated(@NonNull View child, @NonNull View target) { + super.onDescendantInvalidated(child, target); + invalidate(); + } + @ReactProp(name = "x") public void setX(Dynamic x) { mX = SVGLength.from(x); diff --git a/ios/Elements/RNSVGForeignObject.m b/ios/Elements/RNSVGForeignObject.m index 479f77cd..e2a7b80d 100644 --- a/ios/Elements/RNSVGForeignObject.m +++ b/ios/Elements/RNSVGForeignObject.m @@ -18,7 +18,20 @@ - (void)parseReference { self.dirty = false; - [self.svgView defineForeignObject:self foreignObjectName:self.name]; +} + +- (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect +{ + [self clip:context]; + CGContextTranslateCTM(context, [self relativeOnWidth:self.x], [self relativeOnHeight:self.y]); + CGRect clip = CGRectMake( + 0, + 0, + [self relativeOnWidth:self.foreignObjectwidth], + [self relativeOnHeight:self.foreignObjectheight] + ); + CGContextClipToRect(context, clip); + [super renderLayerTo:context rect:rect]; } - (void)setX:(RNSVGLength *)x