Skip to content
This repository has been archived by the owner on May 3, 2023. It is now read-only.

Commit

Permalink
Call will / did display node for ASTextNode. Fixes TextureGroup#1680 (T…
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettmoon authored and piotrdebosz committed Mar 1, 2021
1 parent 5182c6f commit 763d344
Showing 1 changed file with 22 additions and 2 deletions.
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

0 comments on commit 763d344

Please sign in to comment.