Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ASTextNode2] Fix background color drawing #831

Merged
merged 5 commits into from
Mar 12, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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