Skip to content

Commit

Permalink
[ASTextNode2] Fix background color drawing (#831)
Browse files Browse the repository at this point in the history
* Fix TextNode2 not respecting background color

* ASTextNode2: Use locks and copies right

* Increment changelog

* Make the Dangerfile accept any license header with Pinterest in it
  • Loading branch information
Adlai-Holler committed Mar 12, 2018
1 parent ed6da29 commit 5cafdb9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
- Fix UIResponder handling with view backing ASDisplayNode. [Michael Schneider](https://github.com/maicki) [#789] (https://github.com/TextureGroup/Texture/pull/789/)
- Optimized thread-local storage by replacing pthread_specific with C11 thread-local variables. [Adlai Holler](https://github.com/Adlai-Holler) [#811] (https://github.com/TextureGroup/Texture/pull/811/)
- Fixed a thread-sanitizer warning in ASTextNode. [Adlai Holler](https://github.com/Adlai-Holler) [#830] (https://github.com/TextureGroup/Texture/pull/830/)
- Fix ASTextNode2 handling background color incorrectly. [Adlai Holler](https://github.com/Adlai-Holler) [#831] (https://github.com/TextureGroup/Texture/pull/831/)

## 2.6
- [Xcode 9] Updated to require Xcode 9 (to fix warnings) [Garrett Moon](https://github.com/garrettmoon)
Expand Down
6 changes: 1 addition & 5 deletions Dangerfile
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ def check_file_header(files_to_check, licenses)
correct_license = false
licenses.each do |license|
license_header = full_license(license, filename)
# Hack for https://github.com/TextureGroup/Texture/issues/745
# If it's already a "modified-post-Texture" file, leave it with it original copyright year.
if data.include? "Modifications to this file made after 4/13/2017"
correct_license = true
elsif data.start_with?(license_header)
if data.include? "Pinterest, Inc."
correct_license = true
end
end
Expand Down
41 changes: 29 additions & 12 deletions Source/ASTextNode2.mm
Original file line number Diff line number Diff line change
Expand Up @@ -359,20 +359,17 @@ - (NSObject *)drawParametersForAsyncLayer:(_ASDisplayLayer *)layer

[self prepareAttributedString:mutableText];

// Apply background color if needed before drawing. To access the backgroundColor we need to be on the main thread
UIColor *backgroundColor = self.backgroundColor;
if (CGColorGetAlpha(backgroundColor.CGColor) > 0) {
[mutableText addAttribute:NSBackgroundColorAttributeName value:backgroundColor range:NSMakeRange(0, mutableText.length)];
}

return @{
@"container": copiedContainer,
@"text": mutableText
};
@"container": copiedContainer,
@"text": mutableText,
@"bgColor": self.backgroundColor
};
}

/**
* If it can't find a compatible layout, this method creates one.
*
* NOTE: Be careful to copy `text` if needed.
*/
+ (ASTextLayout *)compatibleLayoutWithContainer:(ASTextContainer *)container
text:(NSAttributedString *)text
Expand All @@ -391,7 +388,7 @@ + (ASTextLayout *)compatibleLayoutWithContainer:(ASTextContainer *)container
cacheValue = [textLayoutCache objectForKey:text];
if (cacheValue == nil) {
cacheValue = [[ASTextCacheValue alloc] init];
[textLayoutCache setObject:cacheValue forKey:text];
[textLayoutCache setObject:cacheValue forKey:[text copy]];
}
cacheValue;
});
Expand Down Expand Up @@ -458,15 +455,25 @@ + (ASTextLayout *)compatibleLayoutWithContainer:(ASTextContainer *)container
return layout;
}

+ (void)drawRect:(CGRect)bounds withParameters:(NSDictionary *)layoutDict isCancelled:(asdisplaynode_iscancelled_block_t)isCancelledBlock isRasterizing:(BOOL)isRasterizing;
+ (void)drawRect:(CGRect)bounds withParameters:(NSDictionary *)layoutDict isCancelled:(asdisplaynode_iscancelled_block_t)isCancelledBlock isRasterizing:(BOOL)isRasterizing
{
ASTextContainer *container = layoutDict[@"container"];
NSAttributedString *text = layoutDict[@"text"];
UIColor *bgColor = layoutDict[@"bgColor"];
ASTextLayout *layout = [self compatibleLayoutWithContainer:container text:text];

if (isCancelledBlock()) {
return;
}

// Fill background color.
// They may have already drawn into this context in the pre-context block
// so unfortunately we have to use the normal blend mode, not copy.
if (CGColorGetAlpha(bgColor.CGColor) > 0) {
[bgColor setFill];
UIRectFillUsingBlendMode(bounds, kCGBlendModeNormal);
}

CGContextRef context = UIGraphicsGetCurrentContext();
ASDisplayNodeAssert(context, @"This is no good without a context.");

Expand Down Expand Up @@ -941,11 +948,21 @@ - (UIEdgeInsets)shadowPadding
- (void)setPointSizeScaleFactors:(NSArray<NSNumber *> *)scaleFactors
{
AS_TEXT_ALERT_UNIMPLEMENTED_FEATURE();
_pointSizeScaleFactors = [scaleFactors copy];
{
ASDN::MutexLocker l(__instanceLock__);
if (ASObjectIsEqual(scaleFactors, _pointSizeScaleFactors)) {
return;
}

_pointSizeScaleFactors = [scaleFactors copy];
}

[self setNeedsLayout];
}

- (NSArray *)pointSizeScaleFactors
{
ASDN::MutexLocker l(__instanceLock__);
return _pointSizeScaleFactors;
}

Expand Down

0 comments on commit 5cafdb9

Please sign in to comment.