Skip to content

Commit

Permalink
fix: Text color doesn't work with inlineSize #1225
Browse files Browse the repository at this point in the history
  • Loading branch information
msand committed Jan 1, 2020
1 parent 690c3aa commit 027b8c1
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
4 changes: 2 additions & 2 deletions android/src/main/java/com/horcrux/svg/RenderableView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
7 changes: 6 additions & 1 deletion android/src/main/java/com/horcrux/svg/TSpanView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions ios/Brushes/RNSVGBrush.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions ios/Brushes/RNSVGSolidColorBrush.m
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
31 changes: 29 additions & 2 deletions ios/Text/RNSVGTSpan.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 027b8c1

Please sign in to comment.