Repository: software-mansion/react-native-svg (language: java)

Issue:
Android crashes if r="0" for radialGradient
# Bug

Android is crashing when using `<radialGradient r="0">` with message:

```
IllegalArgumentException
radius must be > 0
```

#### Demo
https://snack.expo.dev/@kocaagasefa/radial-gradient-demo

Library version: 13.4.0

## Steps To Reproduce
- run https://snack.expo.dev/@kocaagasefa/radial-gradient-demo on android

## Example svg: (from [MDN](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/r#example))

```
<svg viewBox="0 0 300 200" xmlns="http://www.w3.org/2000/svg">
  <radialGradient r="0" id="myGradient000">
    <stop offset="0" stop-color="white" />
    <stop offset="100%" stop-color="black" />
  </radialGradient>
  <radialGradient r="50%" id="myGradient050">
    <stop offset="0" stop-color="white" />
    <stop offset="100%" stop-color="black" />
  </radialGradient>
  <radialGradient r="100%" id="myGradient100">
    <stop offset="0" stop-color="white" />
    <stop offset="100%" stop-color="black" />
  </radialGradient>

  <circle cx="50" cy="50" r="0" />
  <circle cx="150" cy="50" r="25" />
  <circle cx="250" cy="50" r="50" />

  <rect x="20" y="120" width="60" height="60" fill="url(#myGradient000)" />
  <rect x="120" y="120" width="60" height="60" fill="url(#myGradient050)" />
  <rect x="220" y="120" width="60" height="60" fill="url(#myGradient100)" />
</svg>
```


#### Describe what you expected to happen:
1. Render example svg given on https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/r#example 



Discussion / hints:

Fix patch:
diff --git a/TestsExample/src/Test2170.tsx b/TestsExample/src/Test2170.tsx
new file mode 100644
index 000000000..aaf1a1d58
--- /dev/null
+++ b/TestsExample/src/Test2170.tsx
@@ -0,0 +1,65 @@
+import * as React from 'react';
+import {View} from 'react-native';
+import {
+  Circle,
+  RadialGradient,
+  Rect,
+  Stop,
+  Svg,
+  SvgXml,
+} from 'react-native-svg';
+
+// https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/r#example
+const svgXml = `
+<svg viewBox="0 0 300 200" xmlns="http://www.w3.org/2000/svg">
+  <radialGradient r="0" id="myGradient000">
+    <stop offset="0" stop-color="white" />
+    <stop offset="100%" stop-color="black" />
+  </radialGradient>
+  <radialGradient r="50%" id="myGradient050">
+    <stop offset="0" stop-color="white" />
+    <stop offset="100%" stop-color="black" />
+  </radialGradient>
+  <radialGradient r="100%" id="myGradient100">
+    <stop offset="0" stop-color="white" />
+    <stop offset="100%" stop-color="black" />
+  </radialGradient>
+
+  <circle cx="50" cy="50" r="0" />
+  <circle cx="150" cy="50" r="25" />
+  <circle cx="250" cy="50" r="50" />
+
+  <rect x="20" y="120" width="60" height="60" fill="url(#myGradient000)" />
+  <rect x="120" y="120" width="60" height="60" fill="url(#myGradient050)" />
+  <rect x="220" y="120" width="60" height="60" fill="url(#myGradient100)" />
+</svg>
+`;
+
+export default function App() {
+  return (
+    <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
+      <SvgXml width={400} height={300} xml={svgXml} />
+      <Svg width={400} height={300}>
+        <RadialGradient id="a" r={0}>
+          <Stop offset={0} stopColor="#f00" />
+          <Stop offset="50%" stopColor="#0f0" />
+          <Stop offset="100%" stopColor="#00f" />
+        </RadialGradient>
+        <RadialGradient id="b" r="50%">
+          <Stop offset={0} stopColor="#f00" />
+          <Stop offset="100%" stopColor="#00f" />
+        </RadialGradient>
+        <RadialGradient id="c" r="100%">
+          <Stop offset={0} stopColor="#f00" />
+          <Stop offset="100%" stopColor="#00f" />
+        </RadialGradient>
+        <Rect x={0} y={100} width={100} height={100} fill="url(#a)" />
+        <Rect x={150} y={100} width={100} height={100} fill="url(#b)" />
+        <Rect x={300} y={100} width={100} height={100} fill="url(#c)" />
+        <Circle cx={50} cy={250} r={50} fill="url(#a)" />
+        <Circle cx={200} cy={250} r={50} fill="url(#b)" />
+        <Circle cx={350} cy={250} r={50} fill="url(#c)" />
+      </Svg>
+    </View>
+  );
+}
diff --git a/android/src/main/java/com/horcrux/svg/Brush.java b/android/src/main/java/com/horcrux/svg/Brush.java
index 0492cda35..dc3dc04ee 100644
--- a/android/src/main/java/com/horcrux/svg/Brush.java
+++ b/android/src/main/java/com/horcrux/svg/Brush.java
@@ -204,6 +204,14 @@ void setupPaint(Paint paint, RectF pathBoundingBox, float scale, float opacity)
       double rx = getVal(mPoints[2], width, scale, textSize);
       double ry = getVal(mPoints[3], height, scale, textSize);
 
+      if (rx <= 0 || ry <= 0) {
+        // Gradient with radius = 0 should be rendered as solid color of the last stop
+        rx = width;
+        ry = height;
+        stops = new float[] {stops[0], stops[stops.length - 1]};
+        stopsColors =
+            new int[] {stopsColors[stopsColors.length - 1], stopsColors[stopsColors.length - 1]};
+      }
       double ratio = ry / rx;
 
       double cx = getVal(mPoints[4], width, scale, textSize) + offsetX;
diff --git a/apple/Brushes/RNSVGPainter.mm b/apple/Brushes/RNSVGPainter.mm
index 276fa6e33..6a30528df 100644
--- a/apple/Brushes/RNSVGPainter.mm
+++ b/apple/Brushes/RNSVGPainter.mm
@@ -232,6 +232,15 @@ - (void)paintRadialGradient:(CGContextRef)context bounds:(CGRect)bounds
   CGFloat rx = [self getVal:[_points objectAtIndex:2] relative:width];
   CGFloat ry = [self getVal:[_points objectAtIndex:3] relative:height];
 
+  if (rx <= 0 || ry <= 0) {
+    // Gradient with radius = 0 should be rendered as solid color of the last stop
+    rx = width;
+    ry = height;
+    CGGradientRelease(gradient);
+    NSArray<NSNumber *> *gradientArray = @[_colors.firstObject, _colors.lastObject, _colors[_colors.count-2], _colors.lastObject];
+    gradient = CGGradientRetain([RCTConvert RNSVGCGGradient:gradientArray]);
+  }
+    
   double ratio = ry / rx;
 
   CGFloat fx = [self getVal:[_points objectAtIndex:0] relative:width] + offsetX;


Test patch: