Skip to content
This repository was archived by the owner on Feb 2, 2023. It is now read-only.

Commit bf9c142

Browse files
committed
Revert "[ASTextNode] Fix text node truncation (#1863)"
This reverts commit 6238e5e. We will re-apply this change, but there are some early signs of performance impacts that need to be investigated.
1 parent d83f319 commit bf9c142

5 files changed

Lines changed: 7 additions & 55 deletions

File tree

AsyncDisplayKit/TextKit/ASTextKitContext.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
#import <UIKit/UIKit.h>
1212

13-
typedef NSTextStorage *(^ASTextKitContextTextStorageCreationBlock)(NSAttributedString *attributedString);
14-
1513
/**
1614
A threadsafe container for the TextKit components that ASTextKit uses to lay out and truncate its text.
1715
@@ -32,19 +30,10 @@ typedef NSTextStorage *(^ASTextKitContextTextStorageCreationBlock)(NSAttributedS
3230
constrainedSize:(CGSize)constrainedSize
3331
layoutManagerCreationBlock:(NSLayoutManager * (^)(void))layoutCreationBlock
3432
layoutManagerDelegate:(id<NSLayoutManagerDelegate>)layoutManagerDelegate
35-
textStorageCreationBlock:(ASTextKitContextTextStorageCreationBlock)textStorageCreationBlock;
33+
textStorageCreationBlock:(NSTextStorage * (^)(NSAttributedString *attributedString))textStorageCreationBlock;
3634

37-
/**
38-
Set the constrained size for the text context.
39-
*/
4035
@property (nonatomic, assign, readwrite) CGSize constrainedSize;
4136

42-
/**
43-
Resets the text storage to the original value in case it was truncated before. This method is called within
44-
a locked context.
45-
*/
46-
- (void)resetTextStorage;
47-
4837
/**
4938
All operations on TextKit values MUST occur within this locked context. Simultaneous access (even non-mutative) to
5039
TextKit components may cause crashes.

AsyncDisplayKit/TextKit/ASTextKitContext.mm

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,8 @@ @implementation ASTextKitContext
2121
NSLayoutManager *_layoutManager;
2222
NSTextStorage *_textStorage;
2323
NSTextContainer *_textContainer;
24-
25-
NSAttributedString *_attributedString;
2624
}
2725

28-
#pragma mark - Lifecycle
29-
3026
- (instancetype)initWithAttributedString:(NSAttributedString *)attributedString
3127
lineBreakMode:(NSLineBreakMode)lineBreakMode
3228
maximumNumberOfLines:(NSUInteger)maximumNumberOfLines
@@ -41,23 +37,16 @@ - (instancetype)initWithAttributedString:(NSAttributedString *)attributedString
4137
// Concurrently initialising TextKit components crashes (rdar://18448377) so we use a global lock.
4238
static std::mutex __static_mutex;
4339
std::lock_guard<std::mutex> l(__static_mutex);
44-
45-
_attributedString = [attributedString copy];
46-
4740
// Create the TextKit component stack with our default configuration.
4841
if (textStorageCreationBlock) {
4942
_textStorage = textStorageCreationBlock(attributedString);
5043
} else {
51-
_textStorage = [[NSTextStorage alloc] init];
52-
[self _resetTextStorage];
44+
_textStorage = (attributedString ? [[NSTextStorage alloc] initWithAttributedString:attributedString] : [[NSTextStorage alloc] init]);
5345
}
54-
55-
5646
_layoutManager = layoutCreationBlock ? layoutCreationBlock() : [[ASLayoutManager alloc] init];
5747
_layoutManager.usesFontLeading = NO;
5848
_layoutManager.delegate = layoutManagerDelegate;
5949
[_textStorage addLayoutManager:_layoutManager];
60-
6150
_textContainer = [[NSTextContainer alloc] initWithSize:constrainedSize];
6251
// We want the text laid out up to the very edges of the container.
6352
_textContainer.lineFragmentPadding = 0;
@@ -69,21 +58,6 @@ - (instancetype)initWithAttributedString:(NSAttributedString *)attributedString
6958
return self;
7059
}
7160

72-
#pragma mark - Text Storage
73-
74-
- (void)resetTextStorage
75-
{
76-
std::lock_guard<std::mutex> l(_textKitMutex);
77-
[self _resetTextStorage];
78-
}
79-
80-
- (void)_resetTextStorage
81-
{
82-
[_textStorage setAttributedString:_attributedString];
83-
}
84-
85-
#pragma mark - Setter / Getter
86-
8761
- (CGSize)constrainedSize
8862
{
8963
std::lock_guard<std::mutex> l(_textKitMutex);
@@ -96,8 +70,6 @@ - (void)setConstrainedSize:(CGSize)constrainedSize
9670
_textContainer.size = constrainedSize;
9771
}
9872

99-
#pragma mark - Locking
100-
10173
- (void)performBlockWithLockedTextKitComponents:(void (^)(NSLayoutManager *,
10274
NSTextStorage *,
10375
NSTextContainer *))block

AsyncDisplayKit/TextKit/ASTextKitRenderer.mm

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,8 @@ - (void)setConstrainedSize:(CGSize)constrainedSize
128128
// If we're updating an existing context, make sure to use the same inset logic used during initialization.
129129
// This codepath allows us to reuse the
130130
CGSize shadowConstrainedSize = [[self shadower] insetSizeWithConstrainedSize:constrainedSize];
131-
if (_context) {
132-
_context.constrainedSize = shadowConstrainedSize;
133-
}
134-
if (_fontSizeAdjuster) {
135-
_fontSizeAdjuster.constrainedSize = shadowConstrainedSize;
136-
}
131+
if (_context) _context.constrainedSize = shadowConstrainedSize;
132+
if (_fontSizeAdjuster) _fontSizeAdjuster.constrainedSize = shadowConstrainedSize;
137133
}
138134
}
139135
}

AsyncDisplayKit/TextKit/ASTextKitTailTruncater.mm

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,9 @@ - (NSUInteger)_findTruncationInsertionPointAtOrBeforeCharacterIndex:(NSUInteger)
150150

151151
- (void)truncate
152152
{
153-
// Reset the text storage to start always with the full string
154-
[_context resetTextStorage];
155-
156-
// Start truncation
157153
[_context performBlockWithLockedTextKitComponents:^(NSLayoutManager *layoutManager, NSTextStorage *textStorage, NSTextContainer *textContainer) {
158-
159154
NSUInteger originalStringLength = textStorage.length;
160-
155+
161156
[layoutManager ensureLayoutForTextContainer:textContainer];
162157

163158
NSRange visibleGlyphRange = [layoutManager glyphRangeForBoundingRect:{ .size = textContainer.size }

AsyncDisplayKitTests/ASTextNodeTests.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ - (void)testRecalculationOfSizeIsSameAsOriginallyCalculatedSize
125125
for (NSInteger i = 10; i < 500; i += 50) {
126126
CGSize constrainedSize = CGSizeMake(i, i);
127127
CGSize calculatedSize = [_textNode measure:constrainedSize];
128-
CGSize recalculatedSize = [_textNode measure:constrainedSize];
128+
CGSize recalculatedSize = [_textNode measure:calculatedSize];
129129

130130
XCTAssertTrue(CGSizeEqualToSizeWithIn(calculatedSize, recalculatedSize, 4.0), @"Recalculated size %@ should be same as original size %@", NSStringFromCGSize(recalculatedSize), NSStringFromCGSize(calculatedSize));
131131
}
@@ -136,7 +136,7 @@ - (void)testRecalculationOfSizeIsSameAsOriginallyCalculatedFloatingPointSize
136136
for (CGFloat i = 10; i < 500; i *= 1.3) {
137137
CGSize constrainedSize = CGSizeMake(i, i);
138138
CGSize calculatedSize = [_textNode measure:constrainedSize];
139-
CGSize recalculatedSize = [_textNode measure:constrainedSize];
139+
CGSize recalculatedSize = [_textNode measure:calculatedSize];
140140

141141
XCTAssertTrue(CGSizeEqualToSizeWithIn(calculatedSize, recalculatedSize, 11.0), @"Recalculated size %@ should be same as original size %@", NSStringFromCGSize(recalculatedSize), NSStringFromCGSize(calculatedSize));
142142
}

0 commit comments

Comments
 (0)