forked from Cocoanetics/DTCoreText
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDTCoreTextLayoutLine.m
More file actions
544 lines (417 loc) · 10.7 KB
/
Copy pathDTCoreTextLayoutLine.m
File metadata and controls
544 lines (417 loc) · 10.7 KB
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
//
// DTCoreTextLayoutLine.m
// DTCoreText
//
// Created by Oliver Drobnik on 1/24/11.
// Copyright 2011 Drobnik.com. All rights reserved.
//
#import "DTCoreTextLayoutLine.h"
#import "DTCoreTextGlyphRun.h"
#import "DTCoreTextLayoutFrame.h"
#import "DTCoreTextLayouter.h"
#import "DTTextAttachment.h"
#import "NSDictionary+DTCoreText.h"
#import "DTTextBlock.h"
#import "DTCoreTextConstants.h"
#import "DTCoreTextFunctions.h"
@interface DTCoreTextLayoutLine ()
@property (nonatomic, strong) NSArray *glyphRuns;
@end
@implementation DTCoreTextLayoutLine
{
CGRect _frame;
CTLineRef _line;
CGPoint _baselineOrigin;
CGFloat _ascent;
CGFloat _descent;
CGFloat _leading;
CGFloat _width;
CGFloat _trailingWhitespaceWidth;
CGFloat _underlineOffset;
CGFloat _lineHeight;
NSArray *_glyphRuns;
BOOL _didCalculateMetrics;
BOOL _writingDirectionIsRightToLeft;
BOOL _needsToDetectWritingDirection;
BOOL _hasScannedGlyphRunsForValues;
}
- (id)initWithLine:(CTLineRef)line
{
return [self initWithLine:line stringLocationOffset:0];
}
- (id)initWithLine:(CTLineRef)line stringLocationOffset:(NSInteger)stringLocationOffset
{
if ((self = [super init]))
{
_line = line;
CFRetain(_line);
// writing direction
_needsToDetectWritingDirection = YES;
_stringLocationOffset = stringLocationOffset;
}
return self;
}
- (void)dealloc
{
CFRelease(_line);
}
#ifndef COVERAGE
// exclude method from coverage testing
- (NSString *)description
{
return [NSString stringWithFormat:@"<%@ origin=%@ frame=%@ range=%@", [self class], NSStringFromCGPoint(_baselineOrigin), NSStringFromCGRect(self.frame), NSStringFromRange([self stringRange])];
}
#endif
- (NSRange)stringRange
{
CFRange range = CTLineGetStringRange(_line);
// add offset if there is one, i.e. from merged lines
range.location += _stringLocationOffset;
return NSMakeRange(range.location, range.length);
}
- (NSInteger)numberOfGlyphs
{
NSInteger ret = 0;
for (DTCoreTextGlyphRun *oneRun in self.glyphRuns)
{
ret += [oneRun numberOfGlyphs];
}
return ret;
}
#pragma mark - Drawing
- (void)drawInContext:(CGContextRef)context
{
CTLineDraw(_line, context);
}
- (CGPathRef)newPathWithGlyphs
{
// mutable path for the line
CGMutablePathRef mutablePath = CGPathCreateMutable();
for (DTCoreTextGlyphRun *oneRun in self.glyphRuns)
{
CGPathRef glyphPath = [oneRun newPathWithGlyphs];
CGAffineTransform posTransform = CGAffineTransformMakeTranslation(_baselineOrigin.x, _baselineOrigin.y);
CGPathAddPath(mutablePath, &posTransform, glyphPath);
CGPathRelease(glyphPath);
}
return mutablePath;
}
#pragma mark - Creating Variants
- (DTCoreTextLayoutLine *)justifiedLineWithFactor:(CGFloat)justificationFactor justificationWidth:(CGFloat)justificationWidth
{
// make this line justified
CTLineRef justifiedLine = CTLineCreateJustifiedLine(_line, justificationFactor, justificationWidth);
DTCoreTextLayoutLine *newLine = [[DTCoreTextLayoutLine alloc] initWithLine:justifiedLine];
CFRelease(justifiedLine);
return newLine;
}
#pragma mark - Calculations
- (NSArray *)stringIndices
{
NSMutableArray *array = [NSMutableArray array];
for (DTCoreTextGlyphRun *oneRun in self.glyphRuns)
{
[array addObjectsFromArray:[oneRun stringIndices]];
}
return array;
}
- (CGRect)frameOfGlyphAtIndex:(NSInteger)index
{
for (DTCoreTextGlyphRun *oneRun in self.glyphRuns)
{
NSInteger count = [oneRun numberOfGlyphs];
if (index >= count)
{
index -= count;
}
else
{
return [oneRun frameOfGlyphAtIndex:index];
}
}
return CGRectZero;
}
- (NSArray *)glyphRunsWithRange:(NSRange)range
{
NSMutableArray *tmpArray = [NSMutableArray arrayWithCapacity:[self numberOfGlyphs]];
for (DTCoreTextGlyphRun *oneRun in self.glyphRuns)
{
NSRange runRange = [oneRun stringRange];
// intersect these ranges
NSRange intersectionRange = NSIntersectionRange(range, runRange);
// if intersection is longer than zero length they intersect
if (intersectionRange.length)
{
[tmpArray addObject:oneRun];
}
}
return tmpArray;
}
- (CGRect)frameOfGlyphsWithRange:(NSRange)range
{
NSArray *glyphRuns = [self glyphRunsWithRange:range];
CGRect tmpRect = CGRectMake(CGFLOAT_MAX, CGFLOAT_MAX, 0, 0);
for (DTCoreTextGlyphRun *oneRun in glyphRuns)
{
CGRect glyphFrame = oneRun.frame;
if (glyphFrame.origin.x < tmpRect.origin.x)
{
tmpRect.origin.x = glyphFrame.origin.x;
}
if (glyphFrame.origin.y < tmpRect.origin.y)
{
tmpRect.origin.y = glyphFrame.origin.y;
}
if (glyphFrame.size.height > tmpRect.size.height)
{
tmpRect.size.height = glyphFrame.size.height;
}
tmpRect.size.width = glyphFrame.origin.x + glyphFrame.size.width - tmpRect.origin.x;
}
CGFloat maxX = CGRectGetMaxX(self.frame) - _trailingWhitespaceWidth;
if (CGRectGetMaxX(tmpRect) > maxX)
{
tmpRect.size.width = maxX - tmpRect.origin.x;
}
return tmpRect;
}
// bounds of an image encompassing the entire run
- (CGRect)imageBoundsInContext:(CGContextRef)context
{
return CTLineGetImageBounds(_line, context);
}
- (CGFloat)offsetForStringIndex:(NSInteger)index
{
// subtract offset if there is one, i.e. from merged lines
index -= _stringLocationOffset;
return CTLineGetOffsetForStringIndex(_line, index, NULL);
}
- (NSInteger)stringIndexForPosition:(CGPoint)position
{
// position is in same coordinate system as frame
CGPoint adjustedPosition = position;
CGRect frame = self.frame;
adjustedPosition.x -= frame.origin.x;
adjustedPosition.y -= frame.origin.y;
NSInteger index = CTLineGetStringIndexForPosition(_line, adjustedPosition);
// add offset if there is one, i.e. from merged lines
index += _stringLocationOffset;
return index;
}
- (void)_calculateMetrics
{
@synchronized(self)
{
if (!_didCalculateMetrics)
{
_width = (CGFloat)CTLineGetTypographicBounds(_line, &_ascent, &_descent, &_leading);
_trailingWhitespaceWidth = (CGFloat)CTLineGetTrailingWhitespaceWidth(_line);
_didCalculateMetrics = YES;
}
}
}
- (BOOL)isHorizontalRule
{
// HR is only a single \n
if (self.stringRange.length>1)
{
return NO;
}
NSArray *runs = self.glyphRuns;
// thus only a single glyphRun
if ([runs count]>1)
{
return NO;
}
DTCoreTextGlyphRun *singleRun = [runs lastObject];
if ([singleRun.attributes objectForKey:DTHorizontalRuleStyleAttribute])
{
return YES;
}
return NO;
}
#pragma mark Determining Values from the glyph runs
- (void)_scanGlyphRunsForValues
{
@synchronized(self)
{
CGFloat maxOffset = 0;
CGFloat maxFontSize = 0;
for (DTCoreTextGlyphRun *oneRun in self.glyphRuns)
{
CTFontRef usedFont = (__bridge CTFontRef)([oneRun.attributes objectForKey:(id)kCTFontAttributeName]);
if (usedFont)
{
maxOffset = MAX(maxOffset, fabs(CTFontGetUnderlinePosition(usedFont)));
maxFontSize = MAX(maxFontSize, CTFontGetSize(usedFont));
}
}
_underlineOffset = maxOffset;
_lineHeight = maxFontSize;
_hasScannedGlyphRunsForValues= YES;
}
}
#pragma mark - Properties
- (NSArray *)glyphRuns
{
@synchronized(self)
{
if (!_glyphRuns)
{
// run array is owned by line
CFArrayRef runs = CTLineGetGlyphRuns(_line);
CFIndex runCount = CFArrayGetCount(runs);
if (runCount)
{
NSMutableArray *tmpArray = [[NSMutableArray alloc] initWithCapacity:runCount];
for (CFIndex i=0; i<runCount; i++)
{
CTRunRef oneRun = CFArrayGetValueAtIndex(runs, i);
// assumption: position of first glyph is also the correct offset of the entire run
CGPoint position = *CTRunGetPositionsPtr(oneRun);
DTCoreTextGlyphRun *glyphRun = [[DTCoreTextGlyphRun alloc] initWithRun:oneRun layoutLine:self offset:position.x];
[tmpArray addObject:glyphRun];
}
_glyphRuns = tmpArray;
}
}
return _glyphRuns;
}
}
- (CGRect)frame
{
if (!_didCalculateMetrics)
{
[self _calculateMetrics];
}
CGRect frame = CGRectMake(_baselineOrigin.x, _baselineOrigin.y - _ascent, _width, _ascent + _descent);
// make sure that HR are extremely wide to be be picked up
if ([self isHorizontalRule])
{
frame.size.width = CGFLOAT_MAX;
}
return frame;
}
- (CGFloat)width
{
if (!_didCalculateMetrics)
{
[self _calculateMetrics];
}
return _width;
}
- (NSArray *)attachments
{
NSMutableArray *tmpArray = [NSMutableArray array];
for (DTCoreTextGlyphRun *oneRun in self.glyphRuns)
{
DTTextAttachment *attachment = oneRun.attachment;
if (attachment)
{
[tmpArray addObject:attachment];
}
}
if ([tmpArray count])
{
return tmpArray;
}
return nil;
}
- (CGFloat)ascent
{
if (!_didCalculateMetrics)
{
[self _calculateMetrics];
}
return _ascent;
}
- (void)setAscent:(CGFloat)ascent
{
// need to get metrics because otherwise ascent gets overwritten
if (!_didCalculateMetrics)
{
[self _calculateMetrics];
}
_ascent = ascent;
}
- (CGFloat)descent
{
if (!_didCalculateMetrics)
{
[self _calculateMetrics];
}
return _descent;
}
- (CGFloat)leading
{
if (!_didCalculateMetrics)
{
[self _calculateMetrics];
}
return _leading;
}
- (CGFloat)underlineOffset
{
if (!_hasScannedGlyphRunsForValues)
{
[self _scanGlyphRunsForValues];
}
return _underlineOffset;
}
- (CGFloat)lineHeight
{
if (!_hasScannedGlyphRunsForValues)
{
[self _scanGlyphRunsForValues];
}
return _lineHeight;
}
- (DTCoreTextParagraphStyle *)paragraphStyle
{
// get paragraph style from any glyph
DTCoreTextGlyphRun *lastRun = [self.glyphRuns lastObject];
NSDictionary *attributes = lastRun.attributes;
return [attributes paragraphStyle];
}
- (NSArray *)textBlocks
{
// get text blocks from any glyph
DTCoreTextGlyphRun *lastRun = [self.glyphRuns lastObject];
NSDictionary *attributes = lastRun.attributes;
return [attributes objectForKey:DTTextBlocksAttribute];
}
- (CGFloat)trailingWhitespaceWidth
{
if (!_didCalculateMetrics)
{
[self _calculateMetrics];
}
return _trailingWhitespaceWidth;
}
- (BOOL)writingDirectionIsRightToLeft
{
if (_needsToDetectWritingDirection)
{
if ([self.glyphRuns count])
{
DTCoreTextGlyphRun *firstRun = [self.glyphRuns objectAtIndex:0];
_writingDirectionIsRightToLeft = [firstRun writingDirectionIsRightToLeft];
}
}
return _writingDirectionIsRightToLeft;
}
- (void)setWritingDirectionIsRightToLeft:(BOOL)writingDirectionIsRightToLeft
{
_writingDirectionIsRightToLeft = writingDirectionIsRightToLeft;
_needsToDetectWritingDirection = NO;
}
@synthesize frame =_frame;
@synthesize glyphRuns = _glyphRuns;
@synthesize ascent = _ascent;
@synthesize descent = _descent;
@synthesize leading = _leading;
@synthesize trailingWhitespaceWidth = _trailingWhitespaceWidth;
@synthesize baselineOrigin = _baselineOrigin;
@synthesize writingDirectionIsRightToLeft = _writingDirectionIsRightToLeft;
@synthesize stringLocationOffset = _stringLocationOffset;
@end