Skip to content

Commit

Permalink
fix: PlatformColor crashes on iOS and Android (#1703)
Browse files Browse the repository at this point in the history
Follow-up PR to #1561 fixing the problems mentioned there. Also fixing the wrong releasing introduced in 027b8c1
  • Loading branch information
WoLewicki authored Feb 24, 2022
1 parent d35878b commit b007efe
Show file tree
Hide file tree
Showing 10 changed files with 527 additions and 708 deletions.
57 changes: 38 additions & 19 deletions TestsExample/src/SampleTest.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,49 @@
import React from 'react';
import { PlatformColor, Platform, Button } from 'react-native';
import {
Svg,
Circle,
Rect,
Text,
TSpan
} from 'react-native-svg';

const color = PlatformColor(Platform.select({
ios: 'label',
android: '@android:color/primary_text_light',
default: 'black',
}))

export default () => {
const [test, setTest] = React.useState(50);

return (
<Svg height="100" width="100">
<Circle
cx="50"
cy="50"
r="45"
stroke="blue"
strokeWidth="2.5"
fill="green"
/>
<Rect
x="15"
y="15"
width="70"
height="70"
stroke="red"
strokeWidth="2"
fill="yellow"
/>
</Svg>
<>
<Svg height="100" width="100" color={color}>
<Circle
cx="50"
cy="50"
r={test}
strokeWidth="2.5"
fill={color}
/>
<Rect
x="15"
y="15"
width="70"
height="70"
stroke="currentColor"
strokeWidth="5"
/>
</Svg>
<Svg height="300" width="300" fill="red">
<Text x={0} y={0} fontSize={20}>
<TSpan dx={test} inlineSize={"100%"} fill="currentColor">
Testing word-wrap... Testing word-wrap... Testing word-wrap... Testing word-wrap...
</TSpan>
</Text>
</Svg>
<Button title="Click me" onPress={()=> setTest(test + 1)}/>
</>
);
}
18 changes: 12 additions & 6 deletions android/src/main/java/com/horcrux/svg/SvgView.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import androidx.annotation.NonNull;

import com.facebook.react.bridge.ColorPropConverter;
import com.facebook.react.bridge.Dynamic;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.uimanager.DisplayMetricsHolder;
Expand Down Expand Up @@ -173,12 +174,17 @@ private void clearChildCache() {
}

@ReactProp(name = "tintColor")
public void setTintColor(@Nullable Integer tintColor) {
if (tintColor == null) {
mTintColor = 0;
} else {
mTintColor = tintColor;
}
public void setTintColor(@Nullable Dynamic tintColor) {
switch (tintColor.getType()) {
case Map:
mTintColor = ColorPropConverter.getColor(tintColor.asMap(), getContext());
break;
case Number:
mTintColor = tintColor.asInt();
break;
default:
mTintColor = 0;
}
invalidate();
clearChildCache();
}
Expand Down
4 changes: 2 additions & 2 deletions android/src/main/java/com/horcrux/svg/SvgViewManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ public boolean needsCustomLayoutForChildren() {
}

@ReactProp(name = "tintColor")
public void setTintColor(SvgView node, @Nullable Integer tintColor) {
public void setTintColor(SvgView node, @Nullable Dynamic tintColor) {
node.setTintColor(tintColor);
}

@ReactProp(name = "color")
public void setColor(SvgView node, @Nullable Integer color) {
public void setColor(SvgView node, @Nullable Dynamic color) {
node.setTintColor(color);
}

Expand Down
10 changes: 6 additions & 4 deletions apple/Brushes/RNSVGSolidColorBrush.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,28 @@
*/

#import "RNSVGSolidColorBrush.h"
#import "RNSVGUIKit.h"

#import "RCTConvert+RNSVG.h"
#import <React/RCTLog.h>

@implementation RNSVGSolidColorBrush
{
UIColor *_color;
RNSVGColor *_color;
}

- (instancetype)initWithArray:(NSArray<RNSVGLength *> *)array
{
if ((self = [super initWithArray:array])) {
_color = [RCTConvert RNSVGUIColor:array offset:1];
_color = [RCTConvert RNSVGColor:array offset:1];
}
return self;
}

- (instancetype)initWithNumber:(NSNumber *)number
{
if ((self = [super init])) {
_color = [RCTConvert UIColor:number];
_color = [RCTConvert RNSVGColor:number];
}
return self;
}
Expand All @@ -41,21 +42,22 @@ - (CGColorRef)getColorWithOpacity:(CGFloat)opacity
{
CGColorRef baseColor = _color.CGColor;
CGColorRef color = CGColorCreateCopyWithAlpha(baseColor, opacity * CGColorGetAlpha(baseColor));
CGColorRelease(baseColor);
return color;
}

- (BOOL)applyFillColor:(CGContextRef)context opacity:(CGFloat)opacity
{
CGColorRef color = [self getColorWithOpacity:opacity];
CGContextSetFillColorWithColor(context, color);
CGColorRelease(color);
return YES;
}

- (BOOL)applyStrokeColor:(CGContextRef)context opacity:(CGFloat)opacity
{
CGColorRef color = [self getColorWithOpacity:opacity];
CGContextSetStrokeColorWithColor(context, color);
CGColorRelease(color);
return YES;
}

Expand Down
15 changes: 4 additions & 11 deletions apple/Text/RNSVGTSpan.m
Original file line number Diff line number Diff line change
Expand Up @@ -65,31 +65,24 @@ - (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect
if (self.content) {
RNSVGGlyphContext* gc = [self.textRoot getGlyphContext];
if (self.inlineSize != nil && self.inlineSize.value != 0) {
CGColorRef color;
if (self.fill) {
if (self.fill.class == RNSVGBrush.class) {
color = [self.tintColor CGColor];
CGColorRef color = [self.tintColor CGColor];
[self drawWrappedText:context gc:gc rect:rect color:color];
} else {
color = [self.fill getColorWithOpacity:self.fillOpacity];
CGColorRef color = [self.fill getColorWithOpacity:self.fillOpacity];
[self drawWrappedText:context gc:gc rect:rect color:color];
}
if (color) {
CGColorRelease(color);
color = nil;
}
}
if (self.stroke) {
if (self.stroke.class == RNSVGBrush.class) {
color = [self.tintColor CGColor];
CGColorRef color = [self.tintColor CGColor];
[self drawWrappedText:context gc:gc rect:rect color:color];
} else {
color = [self.stroke getColorWithOpacity:self.strokeOpacity];
CGColorRef color = [self.stroke getColorWithOpacity:self.strokeOpacity];
[self drawWrappedText:context gc:gc rect:rect color:color];
}
if (color) {
CGColorRelease(color);
color = nil;
}
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion apple/Utils/RCTConvert+RNSVG.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
+ (RNSVGBrush *)RNSVGBrush:(id)json;
+ (RNSVGPathParser *)RNSVGCGPath:(NSString *)d;
+ (CGRect)RNSVGCGRect:(id)json offset:(NSUInteger)offset;
+ (UIColor *)RNSVGUIColor:(id)json offset:(NSUInteger)offset;
+ (RNSVGColor *)RNSVGColor:(id)json offset:(NSUInteger)offset;
+ (CGGradientRef)RNSVGCGGradient:(id)json;

@end
6 changes: 3 additions & 3 deletions apple/Utils/RCTConvert+RNSVG.m
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,17 @@ + (CGRect)RNSVGCGRect:(id)json offset:(NSUInteger)offset
};
}

+ (UIColor *)RNSVGUIColor:(id)json offset:(NSUInteger)offset
+ (RNSVGColor *)RNSVGColor:(id)json offset:(NSUInteger)offset
{
NSArray *arr = [self NSArray:json];
if (arr.count == offset + 1) {
return [self UIColor:[arr objectAtIndex:offset]];
return [self RNSVGColor:[arr objectAtIndex:offset]];
}
if (arr.count < offset + 4) {
RCTLogError(@"Too few elements in array (expected at least %zd): %@", (ssize_t)(4 + offset), arr);
return nil;
}
return [self UIColor:[arr subarrayWithRange:(NSRange){offset, 4}]];
return [self RNSVGColor:[arr subarrayWithRange:(NSRange){offset, 4}]];
}

+ (CGGradientRef)RNSVGCGGradient:(id)json
Expand Down
Loading

0 comments on commit b007efe

Please sign in to comment.