From 027b8c16aa99267467b5aef0fcfd1aa8c2c8582a Mon Sep 17 00:00:00 2001 From: Mikael Sand Date: Wed, 1 Jan 2020 20:28:30 +0200 Subject: [PATCH] fix: Text color doesn't work with inlineSize #1225 --- .../java/com/horcrux/svg/RenderableView.java | 4 +-- .../main/java/com/horcrux/svg/TSpanView.java | 7 ++++- ios/Brushes/RNSVGBrush.h | 2 ++ ios/Brushes/RNSVGSolidColorBrush.m | 5 +++ ios/Text/RNSVGTSpan.m | 31 +++++++++++++++++-- 5 files changed, 44 insertions(+), 5 deletions(-) diff --git a/android/src/main/java/com/horcrux/svg/RenderableView.java b/android/src/main/java/com/horcrux/svg/RenderableView.java index a269d8347..a6470ff6f 100644 --- a/android/src/main/java/com/horcrux/svg/RenderableView.java +++ b/android/src/main/java/com/horcrux/svg/RenderableView.java @@ -421,7 +421,7 @@ void renderMarkers(Canvas canvas, Paint paint, float opacity) { * Sets up paint according to the props set on a view. Returns {@code true} * if the fill should be drawn, {@code false} if not. */ - private boolean setupFillPaint(Paint paint, float opacity) { + boolean setupFillPaint(Paint paint, float opacity) { if (fill != null && fill.size() > 0) { paint.reset(); paint.setFlags(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG | Paint.SUBPIXEL_TEXT_FLAG); @@ -436,7 +436,7 @@ private boolean setupFillPaint(Paint paint, float opacity) { * Sets up paint according to the props set on a view. Returns {@code true} * if the stroke should be drawn, {@code false} if not. */ - private boolean setupStrokePaint(Paint paint, float opacity) { + boolean setupStrokePaint(Paint paint, float opacity) { paint.reset(); double strokeWidth = relativeOnOther(this.strokeWidth); if (strokeWidth == 0 || stroke == null || stroke.size() == 0) { diff --git a/android/src/main/java/com/horcrux/svg/TSpanView.java b/android/src/main/java/com/horcrux/svg/TSpanView.java index 573138ecc..da9f4699e 100644 --- a/android/src/main/java/com/horcrux/svg/TSpanView.java +++ b/android/src/main/java/com/horcrux/svg/TSpanView.java @@ -93,7 +93,12 @@ void clearCache() { void draw(Canvas canvas, Paint paint, float opacity) { if (mContent != null) { if (mInlineSize != null && mInlineSize.value != 0) { - drawWrappedText(canvas, paint); + if (setupFillPaint(paint, opacity * fillOpacity)) { + drawWrappedText(canvas, paint); + } + if (setupStrokePaint(paint, opacity * strokeOpacity)) { + drawWrappedText(canvas, paint); + } } else { int numEmoji = emoji.size(); if (numEmoji > 0) { diff --git a/ios/Brushes/RNSVGBrush.h b/ios/Brushes/RNSVGBrush.h index ee72401fe..0bc84d2cf 100644 --- a/ios/Brushes/RNSVGBrush.h +++ b/ios/Brushes/RNSVGBrush.h @@ -29,6 +29,8 @@ - (BOOL)applyStrokeColor:(CGContextRef)context opacity:(CGFloat)opacity; +- (CGColorRef)getColorWithOpacity:(CGFloat)opacity; + /** * @abstract * paint fills the context with a brush. The context is assumed to diff --git a/ios/Brushes/RNSVGSolidColorBrush.m b/ios/Brushes/RNSVGSolidColorBrush.m index 45a5de7a6..2e1178d6c 100644 --- a/ios/Brushes/RNSVGSolidColorBrush.m +++ b/ios/Brushes/RNSVGSolidColorBrush.m @@ -29,6 +29,11 @@ - (void)dealloc CGColorRelease(_color); } +- (CGColorRef)getColorWithOpacity:(CGFloat)opacity +{ + return CGColorCreateCopyWithAlpha(_color, opacity * CGColorGetAlpha(_color)); +} + - (BOOL)applyFillColor:(CGContextRef)context opacity:(CGFloat)opacity { CGColorRef color = CGColorCreateCopyWithAlpha(_color, opacity * CGColorGetAlpha(_color)); diff --git a/ios/Text/RNSVGTSpan.m b/ios/Text/RNSVGTSpan.m index 0888b3f9b..3567d17bd 100644 --- a/ios/Text/RNSVGTSpan.m +++ b/ios/Text/RNSVGTSpan.m @@ -84,7 +84,33 @@ - (void)renderLayerTo:(CGContextRef)context rect:(CGRect)rect if (self.content) { RNSVGGlyphContext* gc = [self.textRoot getGlyphContext]; if (self.inlineSize != nil && self.inlineSize.value != 0) { - [self drawWrappedText:context gc:gc rect:rect]; + CGColorRef color; + if (self.fill) { + if (self.fill.class == RNSVGBrush.class) { + color = [self.tintColor CGColor]; + [self drawWrappedText:context gc:gc rect:rect color:color]; + } else { + 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]; + [self drawWrappedText:context gc:gc rect:rect color:color]; + } else { + color = [self.stroke getColorWithOpacity:self.strokeOpacity]; + [self drawWrappedText:context gc:gc rect:rect color:color]; + } + if (color) { + CGColorRelease(color); + color = nil; + } + } } else { if (self.path) { NSUInteger count = [emoji count]; @@ -138,7 +164,7 @@ - (NSMutableDictionary *)getAttributes:(RNSVGFontData *)fontdata { } TopAlignedLabel *label; -- (void)drawWrappedText:(CGContextRef)context gc:(RNSVGGlyphContext *)gc rect:(CGRect)rect { +- (void)drawWrappedText:(CGContextRef)context gc:(RNSVGGlyphContext *)gc rect:(CGRect)rect color:(CGColorRef)color { [self pushGlyphContext]; if (fontRef != nil) { CFRelease(fontRef); @@ -182,6 +208,7 @@ - (void)drawWrappedText:(CGContextRef)context gc:(RNSVGGlyphContext *)gc rect:(C label.numberOfLines = 0; label.opaque = NO; label.font = font; + label.textColor = [UIColor colorWithCGColor:color]; CGFloat fontSize = [gc getFontSize]; CGFloat height = CGRectGetHeight(rect);