forked from facebookarchive/AsyncDisplayKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASTextLine.mm
169 lines (146 loc) · 5.18 KB
/
ASTextLine.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
//
// ASTextLine.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/ASTextLine.h>
#import <AsyncDisplayKit/ASTextUtilities.h>
@implementation ASTextLine {
CGFloat _firstGlyphPos; // first glyph position for baseline, typically 0.
}
+ (instancetype)lineWithCTLine:(CTLineRef)CTLine position:(CGPoint)position vertical:(BOOL)isVertical NS_RETURNS_RETAINED {
if (!CTLine) return nil;
ASTextLine *line = [self new];
line->_position = position;
line->_vertical = isVertical;
[line setCTLine:CTLine];
return line;
}
- (void)dealloc {
if (_CTLine) CFRelease(_CTLine);
}
- (void)setCTLine:(_Nonnull CTLineRef)CTLine {
if (_CTLine != CTLine) {
if (CTLine) CFRetain(CTLine);
if (_CTLine) CFRelease(_CTLine);
_CTLine = CTLine;
if (_CTLine) {
_lineWidth = CTLineGetTypographicBounds(_CTLine, &_ascent, &_descent, &_leading);
CFRange range = CTLineGetStringRange(_CTLine);
_range = NSMakeRange(range.location, range.length);
if (CTLineGetGlyphCount(_CTLine) > 0) {
CFArrayRef runs = CTLineGetGlyphRuns(_CTLine);
CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runs, 0);
CGPoint pos;
CTRunGetPositions(run, CFRangeMake(0, 1), &pos);
_firstGlyphPos = pos.x;
} else {
_firstGlyphPos = 0;
}
_trailingWhitespaceWidth = CTLineGetTrailingWhitespaceWidth(_CTLine);
} else {
_lineWidth = _ascent = _descent = _leading = _firstGlyphPos = _trailingWhitespaceWidth = 0;
_range = NSMakeRange(0, 0);
}
[self reloadBounds];
}
}
- (void)setPosition:(CGPoint)position {
_position = position;
[self reloadBounds];
}
- (void)reloadBounds {
if (_vertical) {
_bounds = CGRectMake(_position.x - _descent, _position.y, _ascent + _descent, _lineWidth);
_bounds.origin.y += _firstGlyphPos;
} else {
_bounds = CGRectMake(_position.x, _position.y - _ascent, _lineWidth, _ascent + _descent);
_bounds.origin.x += _firstGlyphPos;
}
_attachments = nil;
_attachmentRanges = nil;
_attachmentRects = nil;
if (!_CTLine) return;
CFArrayRef runs = CTLineGetGlyphRuns(_CTLine);
NSUInteger runCount = CFArrayGetCount(runs);
if (runCount == 0) return;
NSMutableArray<ASTextAttachment *> *attachments = nil;
NSMutableArray<NSValue *> *attachmentRanges = nil;
NSMutableArray<NSValue *> *attachmentRects = nil;
for (NSUInteger r = 0; r < runCount; r++) {
CTRunRef run = (CTRunRef)CFArrayGetValueAtIndex(runs, r);
CFIndex glyphCount = CTRunGetGlyphCount(run);
if (glyphCount == 0) continue;
NSDictionary *attrs = (id)CTRunGetAttributes(run);
ASTextAttachment *attachment = attrs[ASTextAttachmentAttributeName];
if (attachment) {
CGPoint runPosition = CGPointZero;
CTRunGetPositions(run, CFRangeMake(0, 1), &runPosition);
CGFloat ascent, descent, leading, runWidth;
CGRect runTypoBounds;
runWidth = CTRunGetTypographicBounds(run, CFRangeMake(0, 0), &ascent, &descent, &leading);
if (_vertical) {
ASTEXT_SWAP(runPosition.x, runPosition.y);
runPosition.y = _position.y + runPosition.y;
runTypoBounds = CGRectMake(_position.x + runPosition.x - descent, runPosition.y , ascent + descent, runWidth);
} else {
runPosition.x += _position.x;
runPosition.y = _position.y - runPosition.y;
runTypoBounds = CGRectMake(runPosition.x, runPosition.y - ascent, runWidth, ascent + descent);
}
NSRange runRange = ASTextNSRangeFromCFRange(CTRunGetStringRange(run));
if (!attachments) {
attachments = [[NSMutableArray alloc] init];
attachmentRanges = [[NSMutableArray alloc] init];
attachmentRects = [[NSMutableArray alloc] init];
}
[attachments addObject:attachment];
[attachmentRanges addObject:[NSValue valueWithRange:runRange]];
[attachmentRects addObject:[NSValue valueWithCGRect:runTypoBounds]];
}
}
_attachments = attachments;
_attachmentRanges = attachmentRanges;
_attachmentRects = attachmentRects;
}
- (CGSize)size {
return _bounds.size;
}
- (CGFloat)width {
return CGRectGetWidth(_bounds);
}
- (CGFloat)height {
return CGRectGetHeight(_bounds);
}
- (CGFloat)top {
return CGRectGetMinY(_bounds);
}
- (CGFloat)bottom {
return CGRectGetMaxY(_bounds);
}
- (CGFloat)left {
return CGRectGetMinX(_bounds);
}
- (CGFloat)right {
return CGRectGetMaxX(_bounds);
}
- (NSString *)description {
NSMutableString *desc = @"".mutableCopy;
NSRange range = self.range;
[desc appendFormat:@"<ASTextLine: %p> row:%ld range:%tu,%tu", self, (long)self.row, range.location, range.length];
[desc appendFormat:@" position:%@",NSStringFromCGPoint(self.position)];
[desc appendFormat:@" bounds:%@",NSStringFromCGRect(self.bounds)];
return desc;
}
@end
@implementation ASTextRunGlyphRange
+ (instancetype)rangeWithRange:(NSRange)range drawMode:(ASTextRunGlyphDrawMode)mode NS_RETURNS_RETAINED {
ASTextRunGlyphRange *one = [self new];
one.glyphRangeInRun = range;
one.drawMode = mode;
return one;
}
@end