forked from twitter-archive/twui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TUITextRenderer.m
459 lines (372 loc) · 11.8 KB
/
TUITextRenderer.m
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
Copyright 2011 Twitter, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this work except in compliance with the License.
You may obtain a copy of the License in the LICENSE file, or at:
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#import "TUITextRenderer.h"
#import "ABActiveRange.h"
#import "TUIAttributedString.h"
#import "TUICGAdditions.h"
#import "TUIColor.h"
#import "TUIStringDrawing.h"
#import "TUITextRenderer+Event.h"
#import "TUIView.h"
@interface TUITextRenderer ()
@property (nonatomic, retain) NSMutableDictionary *lineRects;
@end
@implementation TUITextRenderer
@synthesize attributedString;
@synthesize frame;
@synthesize view;
@synthesize hitRange;
@synthesize shadowColor;
@synthesize shadowOffset;
@synthesize shadowBlur;
@synthesize verticalAlignment;
@synthesize lineRects;
- (void)_resetFrame
{
if(_ct_frame) {
CFRelease(_ct_frame);
_ct_frame = NULL;
}
if(_ct_path) {
CGPathRelease(_ct_path);
_ct_path = NULL;
}
lineRects = nil;
}
- (void)_resetFramesetter
{
if(_ct_framesetter) {
CFRelease(_ct_framesetter);
_ct_framesetter = NULL;
}
[self _resetFrame];
}
- (void)dealloc
{
[self _resetFramesetter];
}
- (void)_buildFrameWithEffectiveFrame:(CGRect)effectiveFrame
{
[self _resetFrame];
_ct_path = CGPathCreateMutable();
CGPathAddRect((CGMutablePathRef)_ct_path, NULL, effectiveFrame);
_ct_frame = CTFramesetterCreateFrame(_ct_framesetter, CFRangeMake(0, 0), _ct_path, NULL);
}
- (void)_buildFrame
{
if(!_ct_path) {
[self _buildFrameWithEffectiveFrame:frame];
// TUITextVerticalAlignmentTop is easy since that's how Core Text always draws. For Middle and Bottom we have to shift the CTFrame down.
if(verticalAlignment != TUITextVerticalAlignmentTop) {
CGRect effectiveFrame = frame;
CGSize size = AB_CTFrameGetSize(_ct_frame);
if(verticalAlignment == TUITextVerticalAlignmentMiddle) {
effectiveFrame.origin.y = size.height/2 - frame.size.height/2;
} else if(verticalAlignment == TUITextVerticalAlignmentBottom) {
effectiveFrame.origin.y = size.height;
}
effectiveFrame = CGRectIntegral(effectiveFrame);
[self _buildFrameWithEffectiveFrame:effectiveFrame];
}
}
}
- (void)_buildFramesetter
{
if(!_ct_framesetter) {
_ct_framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)attributedString);
}
[self _buildFrame];
}
- (CTFramesetterRef)ctFramesetter
{
[self _buildFramesetter];
return _ct_framesetter;
}
- (CTFrameRef)ctFrame
{
[self _buildFramesetter];
return _ct_frame;
}
- (CGPathRef)ctPath
{
[self _buildFramesetter];
return _ct_path;
}
- (CFIndex)_clampToValidRange:(CFIndex)index
{
if(index < 0) return 0;
CFIndex max = [attributedString length] - 1;
if(index > max) return max;
return index;
}
- (NSRange)_wordRangeAtIndex:(CFIndex)index
{
return [attributedString doubleClickAtIndex:[self _clampToValidRange:index]];
}
- (NSRange)_lineRangeAtIndex:(CFIndex)index
{
return [[attributedString string] lineRangeForRange:NSMakeRange(index, 0)];
}
- (NSRange)_paragraphRangeAtIndex:(CFIndex)index
{
return [[attributedString string] paragraphRangeForRange:NSMakeRange(index, 0)];
}
- (CFRange)_selectedRange
{
CFIndex first, last;
if(_selectionStart <= _selectionEnd) {
first = _selectionStart;
last = _selectionEnd;
} else {
first = _selectionEnd;
last = _selectionStart;
}
if(_selectionAffinity != TUITextSelectionAffinityCharacter) {
NSRange fr = {0,0};
NSRange lr = {0,0};
switch(_selectionAffinity) {
case TUITextSelectionAffinityCharacter:
// do nothing
break;
case TUITextSelectionAffinityWord:
fr = [self _wordRangeAtIndex:first];
lr = [self _wordRangeAtIndex:last];
break;
case TUITextSelectionAffinityLine:
fr = [self _lineRangeAtIndex:first];
lr = [self _lineRangeAtIndex:last];
break;
case TUITextSelectionAffinityParagraph:
fr = [self _paragraphRangeAtIndex:first];
lr = [self _paragraphRangeAtIndex:last];
break;
}
first = fr.location;
last = lr.location + lr.length;
}
return CFRangeMake(first, last - first);
}
- (NSRange)selectedRange
{
return ABNSRangeFromCFRange([self _selectedRange]);
}
- (void)setSelection:(NSRange)selection
{
_selectionAffinity = TUITextSelectionAffinityCharacter;
_selectionStart = selection.location;
_selectionEnd = selection.location + selection.length;
[view setNeedsDisplay];
}
- (NSString *)selectedString
{
return [[attributedString string] substringWithRange:[self selectedRange]];
}
- (void)draw
{
[self drawInContext:TUIGraphicsGetCurrentContext()];
}
- (void)drawInContext:(CGContextRef)context
{
if(attributedString) {
CGContextSaveGState(context);
CTFrameRef f = [self ctFrame];
if(_flags.preDrawBlocksEnabled && !_flags.drawMaskDragSelection) {
[self.attributedString enumerateAttribute:TUIAttributedStringPreDrawBlockName inRange:NSMakeRange(0, [self.attributedString length]) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
if(value == NULL) return;
CGContextSaveGState(context);
AB_CTLineRectAggregationType aggregationType = (AB_CTLineRectAggregationType) [[self.attributedString attribute:TUIAttributedStringBackgroundFillStyleName atIndex:range.location effectiveRange:NULL] integerValue];
NSArray *rectsArray = [self rectsForCharacterRange:CFRangeMake(range.location, range.length) aggregationType:aggregationType];
CFIndex rectCount = rectsArray.count;
CGRect rects[rectCount];
for(NSUInteger i = 0; i < rectCount; i++) {
rects[i] = [[rectsArray objectAtIndex:i] rectValue];
}
TUIAttributedStringPreDrawBlock block = value;
block(self.attributedString, range, rects, rectCount);
CGContextRestoreGState(context);
}];
}
if(_flags.backgroundDrawingEnabled && !_flags.drawMaskDragSelection) {
CGContextSaveGState(context);
[self.attributedString enumerateAttribute:TUIAttributedStringBackgroundColorAttributeName inRange:NSMakeRange(0, [self.attributedString length]) options:0 usingBlock:^(id value, NSRange range, BOOL *stop) {
if(value == NULL) return;
CGColorRef color = (__bridge CGColorRef) value;
CGContextSetFillColorWithColor(context, color);
AB_CTLineRectAggregationType aggregationType = (AB_CTLineRectAggregationType) [[self.attributedString attribute:TUIAttributedStringBackgroundFillStyleName atIndex:range.location effectiveRange:NULL] integerValue];
NSArray *rectsArray = [self rectsForCharacterRange:CFRangeMake(range.location, range.length) aggregationType:aggregationType];
CFIndex rectCount = rectsArray.count;
CGRect rects[rectCount];
for(NSUInteger i = 0; i < rectCount; i++) {
rects[i] = [[rectsArray objectAtIndex:i] rectValue];
}
for(CFIndex i = 0; i < rectCount; ++i) {
CGRect r = rects[i];
r = CGRectInset(r, -2, -1);
r = CGRectIntegral(r);
if(r.size.width > 1)
CGContextFillRect(context, r);
}
}];
CGContextRestoreGState(context);
}
if(hitRange && !_flags.drawMaskDragSelection) {
// draw highlight
CGContextSaveGState(context);
NSRange _r = [hitRange rangeValue];
CFRange r = {_r.location, _r.length};
CFIndex nRects = 10;
CGRect rects[nRects];
AB_CTFrameGetRectsForRange(f, r, rects, &nRects);
for(int i = 0; i < nRects; ++i) {
CGRect rect = rects[i];
rect = CGRectInset(rect, -2, -1);
rect.size.height -= 1;
rect = CGRectIntegral(rect);
TUIColor *color = [TUIColor colorWithWhite:1.0 alpha:1.0];
[color set];
CGContextSetShadowWithColor(context, CGSizeMake(0, 0), 8, color.CGColor);
CGContextFillRoundRect(context, rect, 10);
}
CGContextRestoreGState(context);
}
CFRange selectedRange = [self _selectedRange];
if(selectedRange.length > 0) {
[[NSColor selectedTextBackgroundColor] set];
// draw (or mask) selection
CFIndex rectCount = 100;
CGRect rects[rectCount];
AB_CTFrameGetRectsForRange(f, selectedRange, rects, &rectCount);
if(_flags.drawMaskDragSelection) {
CGContextClipToRects(context, rects, rectCount);
} else {
[self drawSelectionWithRects:rects count:rectCount];
}
}
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
if(shadowColor)
CGContextSetShadowWithColor(context, shadowOffset, shadowBlur, shadowColor.CGColor);
CTFrameDraw(f, context); // draw actual text
CGContextRestoreGState(context);
}
}
- (void)drawSelectionWithRects:(CGRect *)rects count:(CFIndex)count {
CGContextRef context = TUIGraphicsGetCurrentContext();
for(CFIndex i = 0; i < count; ++i) {
CGRect r = rects[i];
r = CGRectIntegral(r);
if(r.size.width > 1)
CGContextFillRect(context, r);
}
}
- (CGSize)size
{
if(attributedString) {
return AB_CTFrameGetSize([self ctFrame]);
}
return CGSizeZero;
}
- (CGSize)sizeConstrainedToWidth:(CGFloat)width
{
if(attributedString) {
CGRect oldFrame = frame;
self.frame = CGRectMake(0.0f, 0.0f, width, 1000000.0f);
CGSize size = [self size];
self.frame = oldFrame;
return size;
}
return CGSizeZero;
}
- (CGSize)sizeConstrainedToWidth:(CGFloat)width numberOfLines:(NSUInteger)numberOfLines
{
NSMutableAttributedString *fake = [self.attributedString mutableCopy];
[fake replaceCharactersInRange:NSMakeRange(0, [fake length]) withString:@"M"];
CGFloat singleLineHeight = [fake ab_sizeConstrainedToWidth:width].height;
CGFloat maxHeight = singleLineHeight * numberOfLines;
CGSize size = [self sizeConstrainedToWidth:width];
return CGSizeMake(size.width, MIN(maxHeight, size.height));
}
- (void)setAttributedString:(NSAttributedString *)a
{
attributedString = a;
[self _resetFramesetter];
}
- (void)setFrame:(CGRect)f
{
frame = f;
[self _resetFrame];
}
- (void)reset
{
[self _resetFramesetter];
}
- (CGRect)firstRectForCharacterRange:(CFRange)range
{
CFIndex rectCount = 1;
CGRect rects[rectCount];
AB_CTFrameGetRectsForRange([self ctFrame], range, rects, &rectCount);
if(rectCount > 0) {
return rects[0];
}
return CGRectZero;
}
- (NSArray *)rectsForCharacterRange:(CFRange)range
{
return [self rectsForCharacterRange:range aggregationType:AB_CTLineRectAggregationTypeInline];
}
- (NSArray *)rectsForCharacterRange:(CFRange)range aggregationType:(AB_CTLineRectAggregationType)aggregationType
{
if(self.lineRects == nil) {
self.lineRects = [NSMutableDictionary dictionary];
}
NSValue *cacheKey = [NSValue valueWithRange:NSMakeRange(range.location, range.length)];
NSArray *cachedRects = [self.lineRects objectForKey:cacheKey];
if(cachedRects == nil) {
CFIndex rectCount = 100;
CGRect rects[rectCount];
AB_CTFrameGetRectsForRangeWithAggregationType([self ctFrame], range, aggregationType, rects, &rectCount);
NSMutableArray *wrappedRects = [NSMutableArray arrayWithCapacity:rectCount];
for(CFIndex i = 0; i < rectCount; i++) {
[wrappedRects addObject:[NSValue valueWithRect:rects[i]]];
}
[self.lineRects setObject:wrappedRects forKey:cacheKey];
cachedRects = wrappedRects;
}
return cachedRects;
}
- (BOOL)backgroundDrawingEnabled
{
return _flags.backgroundDrawingEnabled;
}
- (void)setBackgroundDrawingEnabled:(BOOL)enabled
{
_flags.backgroundDrawingEnabled = enabled;
}
- (BOOL)preDrawBlocksEnabled
{
return _flags.preDrawBlocksEnabled;
}
- (void)setPreDrawBlocksEnabled:(BOOL)enabled
{
_flags.preDrawBlocksEnabled = enabled;
}
- (void)setVerticalAlignment:(TUITextVerticalAlignment)alignment
{
if(verticalAlignment == alignment) return;
verticalAlignment = alignment;
[self _resetFrame];
}
- (void)setNeedsDisplay {
[self _resetFramesetter];
[view setNeedsDisplay];
}
@end