forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASTextKitRenderer.mm
295 lines (239 loc) · 12.3 KB
/
ASTextKitRenderer.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//
// ASTextKitRenderer.mm
// Texture
//
// Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
// Changes after 4/13/2017 are: Copyright (c) Pinterest, Inc. All rights reserved.
// Licensed under Apache 2.0: http://www.apache.org/licenses/LICENSE-2.0
//
#import <AsyncDisplayKit/ASTextKitRenderer.h>
#if AS_ENABLE_TEXTNODE
#import <AsyncDisplayKit/ASAssert.h>
#import <AsyncDisplayKit/ASTextKitContext.h>
#import <AsyncDisplayKit/ASTextKitShadower.h>
#import <AsyncDisplayKit/ASTextKitTailTruncater.h>
#import <AsyncDisplayKit/ASTextKitFontSizeAdjuster.h>
#import <AsyncDisplayKit/ASInternalHelpers.h>
#import <AsyncDisplayKit/ASRunLoopQueue.h>
//#define LOG(...) NSLog(__VA_ARGS__)
#define LOG(...)
static NSCharacterSet *_defaultAvoidTruncationCharacterSet()
{
static NSCharacterSet *truncationCharacterSet;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSMutableCharacterSet *mutableCharacterSet = [[NSMutableCharacterSet alloc] init];
[mutableCharacterSet formUnionWithCharacterSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
[mutableCharacterSet addCharactersInString:@".,!?:;"];
truncationCharacterSet = mutableCharacterSet;
});
return truncationCharacterSet;
}
@implementation ASTextKitRenderer {
CGSize _calculatedSize;
}
@synthesize attributes = _attributes, context = _context, shadower = _shadower, truncater = _truncater, fontSizeAdjuster = _fontSizeAdjuster;
#pragma mark - Initialization
- (instancetype)initWithTextKitAttributes:(const ASTextKitAttributes &)attributes
constrainedSize:(const CGSize)constrainedSize
{
if (self = [super init]) {
_constrainedSize = constrainedSize;
_attributes = attributes;
_currentScaleFactor = 1;
// As the renderer should be thread safe, create all subcomponents in the initialization method
_shadower = [ASTextKitShadower shadowerWithShadowOffset:attributes.shadowOffset
shadowColor:attributes.shadowColor
shadowOpacity:attributes.shadowOpacity
shadowRadius:attributes.shadowRadius];
// We must inset the constrained size by the size of the shadower.
CGSize shadowConstrainedSize = [[self shadower] insetSizeWithConstrainedSize:_constrainedSize];
_context = [[ASTextKitContext alloc] initWithAttributedString:attributes.attributedString
lineBreakMode:attributes.lineBreakMode
maximumNumberOfLines:attributes.maximumNumberOfLines
exclusionPaths:attributes.exclusionPaths
constrainedSize:shadowConstrainedSize];
NSCharacterSet *avoidTailTruncationSet = attributes.avoidTailTruncationSet ?: _defaultAvoidTruncationCharacterSet();
_truncater = [[ASTextKitTailTruncater alloc] initWithContext:[self context]
truncationAttributedString:attributes.truncationAttributedString
avoidTailTruncationSet:avoidTailTruncationSet];
ASTextKitAttributes attributes = _attributes;
// We must inset the constrained size by the size of the shadower.
_fontSizeAdjuster = [[ASTextKitFontSizeAdjuster alloc] initWithContext:[self context]
constrainedSize:shadowConstrainedSize
textKitAttributes:attributes];
// Calcualate size immediately
[self _calculateSize];
}
return self;
}
- (NSStringDrawingContext *)stringDrawingContext
{
// String drawing contexts are not safe to use from more than one thread.
// i.e. if they are created on one thread, it is unsafe to use them on another.
// Therefore we always need to create a new one.
//
// http://web.archive.org/web/20140703122636/https://developer.apple.com/library/ios/documentation/uikit/reference/NSAttributedString_UIKit_Additions/Reference/Reference.html
NSStringDrawingContext *stringDrawingContext = [[NSStringDrawingContext alloc] init];
if (isinf(_constrainedSize.width) == NO && _attributes.maximumNumberOfLines > 0) {
ASDisplayNodeAssert(_attributes.maximumNumberOfLines != 1, @"Max line count 1 is not supported in fast-path.");
[stringDrawingContext setValue:@(_attributes.maximumNumberOfLines) forKey:@"maximumNumberOfLines"];
}
return stringDrawingContext;
}
#pragma mark - Sizing
- (CGSize)size
{
return _calculatedSize;
}
- (void)_calculateSize
{
// if we have no scale factors or an unconstrained width, there is no reason to try to adjust the font size
if (isinf(_constrainedSize.width) == NO && [_attributes.pointSizeScaleFactors count] > 0) {
_currentScaleFactor = [[self fontSizeAdjuster] scaleFactor];
}
const CGRect constrainedRect = {CGPointZero, _constrainedSize};
// If we do not scale, do exclusion, or do custom truncation, we should just use NSAttributedString drawing for a fast-path.
if (self.canUseFastPath) {
CGRect rect = [_attributes.attributedString boundingRectWithSize:_constrainedSize options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine context:self.stringDrawingContext];
// Intersect with constrained rect, in case text kit goes out-of-bounds.
rect = CGRectIntersection(rect, constrainedRect);
_calculatedSize = [self.shadower outsetSizeWithInsetSize:rect.size];
return;
}
BOOL isScaled = [self isScaled];
__block NSTextStorage *scaledTextStorage = nil;
if (isScaled) {
// apply the string scale before truncating or else we may truncate the string after we've done the work to shrink it.
[[self context] performBlockWithLockedTextKitComponents:^(NSLayoutManager *layoutManager, NSTextStorage *textStorage, NSTextContainer *textContainer) {
NSMutableAttributedString *scaledString = [[NSMutableAttributedString alloc] initWithAttributedString:textStorage];
[ASTextKitFontSizeAdjuster adjustFontSizeForAttributeString:scaledString withScaleFactor:self->_currentScaleFactor];
scaledTextStorage = [[NSTextStorage alloc] initWithAttributedString:scaledString];
[textStorage removeLayoutManager:layoutManager];
[scaledTextStorage addLayoutManager:layoutManager];
}];
}
[[self truncater] truncate];
__block CGRect boundingRect;
// Force glyph generation and layout, which may not have happened yet (and isn't triggered by
// -usedRectForTextContainer:).
[[self context] performBlockWithLockedTextKitComponents:^(NSLayoutManager *layoutManager, NSTextStorage *textStorage, NSTextContainer *textContainer) {
[layoutManager ensureLayoutForTextContainer:textContainer];
boundingRect = [layoutManager usedRectForTextContainer:textContainer];
if (isScaled) {
// put the non-scaled version back
[scaledTextStorage removeLayoutManager:layoutManager];
[textStorage addLayoutManager:layoutManager];
}
}];
// TextKit often returns incorrect glyph bounding rects in the horizontal direction, so we clip to our bounding rect
// to make sure our width calculations aren't being offset by glyphs going beyond the constrained rect.
boundingRect = CGRectIntersection(boundingRect, constrainedRect);
_calculatedSize = [_shadower outsetSizeWithInsetSize:boundingRect.size];
}
- (BOOL)isScaled
{
return (_currentScaleFactor > 0 && _currentScaleFactor < 1.0);
}
- (BOOL)usesCustomTruncation
{
// NOTE: This code does not correctly handle if they set `…` with different attributes.
return _attributes.avoidTailTruncationSet != nil || [_attributes.truncationAttributedString.string isEqualToString:@"\u2026"] == NO;
}
- (BOOL)usesExclusionPaths
{
return _attributes.exclusionPaths.count > 0;
}
- (BOOL)canUseFastPath
{
return NO;
// Fast path is temporarily disabled, because it's crashing in production.
// NOTE: Remember to re-enable testFastPathTruncation when we re-enable this.
// return self.isScaled == NO
// && self.usesCustomTruncation == NO
// && self.usesExclusionPaths == NO
// // NSAttributedString drawing methods ignore usesLineFragmentOrigin if max line count 1,
// // rendering them useless:
// && (_attributes.maximumNumberOfLines != 1 || isinf(_constrainedSize.width));
}
#pragma mark - Drawing
- (void)drawInContext:(CGContextRef)context bounds:(CGRect)bounds;
{
// We add an assertion so we can track the rare conditions where a graphics context is not present
ASDisplayNodeAssertNotNil(context, @"This is no good without a context.");
bounds = CGRectIntersection(bounds, { .size = _constrainedSize });
CGRect shadowInsetBounds = [[self shadower] insetRectWithConstrainedRect:bounds];
CGContextSaveGState(context);
[[self shadower] setShadowInContext:context];
UIGraphicsPushContext(context);
LOG(@"%@, shadowInsetBounds = %@",self, NSStringFromCGRect(shadowInsetBounds));
// If we use default options, we can use NSAttributedString for a
// fast path.
if (self.canUseFastPath) {
CGRect drawingBounds = shadowInsetBounds;
[_attributes.attributedString drawWithRect:drawingBounds options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine context:self.stringDrawingContext];
} else {
BOOL isScaled = [self isScaled];
[[self context] performBlockWithLockedTextKitComponents:^(NSLayoutManager *layoutManager, NSTextStorage *textStorage, NSTextContainer *textContainer) {
NSTextStorage *scaledTextStorage = nil;
if (isScaled) {
// if we are going to scale the text, swap out the non-scaled text for the scaled version.
NSMutableAttributedString *scaledString = [[NSMutableAttributedString alloc] initWithAttributedString:textStorage];
[ASTextKitFontSizeAdjuster adjustFontSizeForAttributeString:scaledString withScaleFactor:self->_currentScaleFactor];
scaledTextStorage = [[NSTextStorage alloc] initWithAttributedString:scaledString];
[textStorage removeLayoutManager:layoutManager];
[scaledTextStorage addLayoutManager:layoutManager];
}
LOG(@"usedRect: %@", NSStringFromCGRect([layoutManager usedRectForTextContainer:textContainer]));
NSRange glyphRange = [layoutManager glyphRangeForBoundingRect:(CGRect){ .size = textContainer.size } inTextContainer:textContainer];
LOG(@"boundingRect: %@", NSStringFromCGRect([layoutManager boundingRectForGlyphRange:glyphRange inTextContainer:textContainer]));
[layoutManager drawBackgroundForGlyphRange:glyphRange atPoint:shadowInsetBounds.origin];
[layoutManager drawGlyphsForGlyphRange:glyphRange atPoint:shadowInsetBounds.origin];
if (isScaled) {
// put the non-scaled version back
[scaledTextStorage removeLayoutManager:layoutManager];
[textStorage addLayoutManager:layoutManager];
}
}];
}
UIGraphicsPopContext();
CGContextRestoreGState(context);
}
#pragma mark - String Ranges
- (NSUInteger)lineCount
{
__block NSUInteger lineCount = 0;
[[self context] performBlockWithLockedTextKitComponents:^(NSLayoutManager *layoutManager, NSTextStorage *textStorage, NSTextContainer *textContainer) {
for (NSRange lineRange = { 0, 0 }; NSMaxRange(lineRange) < [layoutManager numberOfGlyphs]; lineCount++) {
[layoutManager lineFragmentRectForGlyphAtIndex:NSMaxRange(lineRange) effectiveRange:&lineRange];
}
}];
return lineCount;
}
- (BOOL)isTruncated
{
if (self.canUseFastPath) {
CGRect boundedRect = [_attributes.attributedString boundingRectWithSize:CGSizeMake(_constrainedSize.width, CGFLOAT_MAX)
options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine
context:nil];
return boundedRect.size.height > _constrainedSize.height;
} else {
return self.firstVisibleRange.length < _attributes.attributedString.length;
}
}
- (std::vector<NSRange>)visibleRanges
{
return _truncater.visibleRanges;
}
@end
@implementation ASTextKitRenderer (ASTextKitRendererConvenience)
- (NSRange)firstVisibleRange
{
std::vector<NSRange> visibleRanges = self.visibleRanges;
if (visibleRanges.size() > 0) {
return visibleRanges[0];
}
return NSMakeRange(0, 0);
}
@end
#endif