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

Call will / did display node for ASTextNode. Fixes #1680 #1893

Merged
merged 1 commit into from
Aug 14, 2020
Merged
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
24 changes: 22 additions & 2 deletions Source/ASTextNode.mm
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ @interface ASTextNodeDrawParameter : NSObject {
BOOL _opaque;
CGRect _bounds;
ASPrimitiveTraitCollection _traitCollection;
ASDisplayNodeContextModifier _willDisplayNodeContentWithRenderingContext;
ASDisplayNodeContextModifier _didDisplayNodeContentWithRenderingContext;
}
@end

Expand All @@ -152,7 +154,9 @@ - (instancetype)initWithRendererAttributes:(ASTextKitAttributes)rendererAttribut
contentScale:(CGFloat)contentScale
opaque:(BOOL)opaque
bounds:(CGRect)bounds
traitCollection: (ASPrimitiveTraitCollection)traitCollection
traitCollection:(ASPrimitiveTraitCollection)traitCollection
willDisplayNodeContentWithRenderingContext:(ASDisplayNodeContextModifier)willDisplayNodeContentWithRenderingContext
didDisplayNodeContentWithRenderingContext:(ASDisplayNodeContextModifier)didDisplayNodeContentWithRenderingContext
{
self = [super init];
if (self != nil) {
Expand All @@ -163,6 +167,8 @@ - (instancetype)initWithRendererAttributes:(ASTextKitAttributes)rendererAttribut
_opaque = opaque;
_bounds = bounds;
_traitCollection = traitCollection;
_willDisplayNodeContentWithRenderingContext = willDisplayNodeContentWithRenderingContext;
_didDisplayNodeContentWithRenderingContext = didDisplayNodeContentWithRenderingContext;
}
return self;
}
Expand Down Expand Up @@ -560,7 +566,9 @@ - (NSObject *)drawParametersForAsyncLayer:(_ASDisplayLayer *)layer
contentScale:_contentsScaleForDisplay
opaque:self.isOpaque
bounds:[self threadSafeBounds]
traitCollection:self.primitiveTraitCollection];
traitCollection:self.primitiveTraitCollection
willDisplayNodeContentWithRenderingContext:self.willDisplayNodeContentWithRenderingContext
didDisplayNodeContentWithRenderingContext:self.didDisplayNodeContentWithRenderingContext];
}

+ (UIImage *)displayWithParameters:(id<NSObject>)parameters isCancelled:(NS_NOESCAPE asdisplaynode_iscancelled_block_t)isCancelled
Expand All @@ -574,12 +582,19 @@ + (UIImage *)displayWithParameters:(id<NSObject>)parameters isCancelled:(NS_NOES
UIColor *backgroundColor = drawParameter->_backgroundColor;
UIEdgeInsets textContainerInsets = drawParameter ? drawParameter->_textContainerInsets : UIEdgeInsetsZero;
ASTextKitRenderer *renderer = [drawParameter rendererForBounds:drawParameter->_bounds];
ASDisplayNodeContextModifier willDisplayNodeContentWithRenderingContext = drawParameter->_willDisplayNodeContentWithRenderingContext;
ASDisplayNodeContextModifier didDisplayNodeContentWithRenderingContext = drawParameter->_didDisplayNodeContentWithRenderingContext;

UIImage *result = ASGraphicsCreateImage(drawParameter->_traitCollection, CGSizeMake(drawParameter->_bounds.size.width, drawParameter->_bounds.size.height), drawParameter->_opaque, drawParameter->_contentScale, nil, nil, ^{
CGContextRef context = UIGraphicsGetCurrentContext();
ASDisplayNodeAssert(context, @"This is no good without a context.");

CGContextSaveGState(context);

if (context && willDisplayNodeContentWithRenderingContext) {
willDisplayNodeContentWithRenderingContext(context, drawParameter);
}

CGContextTranslateCTM(context, textContainerInsets.left, textContainerInsets.top);

// Fill background
Expand All @@ -591,6 +606,11 @@ + (UIImage *)displayWithParameters:(id<NSObject>)parameters isCancelled:(NS_NOES

// Draw text
[renderer drawInContext:context bounds:drawParameter->_bounds];

if (context && didDisplayNodeContentWithRenderingContext) {
didDisplayNodeContentWithRenderingContext(context, drawParameter);
}

CGContextRestoreGState(context);
});

Expand Down