forked from facebookarchive/three20
/
TTStyledLayout.m
695 lines (604 loc) · 21 KB
/
TTStyledLayout.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
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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
#import "Three20/TTStyledLayout.h"
#import "Three20/TTStyledNode.h"
#import "Three20/TTStyledFrame.h"
#import "Three20/TTDefaultStyleSheet.h"
//////////////////////////////////////////////////////////////////////////////////////////////////
@implementation TTStyledLayout
@synthesize width = _width, height = _height, rootFrame = _rootFrame, font = _font,
invalidImages = _invalidImages;
//////////////////////////////////////////////////////////////////////////////////////////////////
// private
- (UIFont*)boldVersionOfFont:(UIFont*)font {
// XXXjoe Clearly this doesn't work if your font is not the system font
return [UIFont boldSystemFontOfSize:font.pointSize];
}
- (UIFont*)italicVersionOfFont:(UIFont*)font {
// XXXjoe Clearly this doesn't work if your font is not the system font
return [UIFont italicSystemFontOfSize:font.pointSize];
}
- (TTStyledNode*)findLastNode:(TTStyledNode*)node {
TTStyledNode* lastNode = nil;
while (node) {
if ([node isKindOfClass:[TTStyledElement class]]) {
TTStyledElement* element = (TTStyledElement*)node;
lastNode = [self findLastNode:element.firstChild];
} else {
lastNode = node;
}
node = node.nextSibling;
}
return lastNode;
}
- (UIFont*)boldFont {
if (!_boldFont) {
_boldFont = [[self boldVersionOfFont:self.font] retain];
}
return _boldFont;
}
- (UIFont*)italicFont {
if (!_italicFont) {
_italicFont = [[self italicVersionOfFont:self.font] retain];
}
return _italicFont;
}
- (TTStyle*)linkStyle {
if (!_linkStyle) {
_linkStyle = [TTSTYLE(linkText:) retain];
}
return _linkStyle;
}
- (TTStyledNode*)lastNode {
if (!_lastNode) {
_lastNode = [self findLastNode:_rootNode];
}
return _lastNode;
}
- (CGFloat)fontHeight {
return (_font.ascender - _font.descender)+1;
}
- (void)offsetFrame:(TTStyledFrame*)frame by:(CGFloat)y {
frame.y += y;
if ([frame isKindOfClass:[TTStyledInlineFrame class]]) {
TTStyledInlineFrame* inlineFrame = (TTStyledInlineFrame*)frame;
TTStyledFrame* child = inlineFrame.firstChildFrame;
while (child) {
[self offsetFrame:child by:y];
child = child.nextFrame;
}
}
}
- (void)expandLineWidth:(CGFloat)width {
_lineWidth += width;
TTStyledInlineFrame* inlineFrame = _inlineFrame;
while (inlineFrame) {
inlineFrame.width += width;
inlineFrame = inlineFrame.inlineParentFrame;
}
}
- (void)inflateLineHeight:(CGFloat)height {
if (height > _lineHeight) {
_lineHeight = height;
}
if (_inlineFrame) {
TTStyledInlineFrame* inlineFrame = _inlineFrame;
while (inlineFrame) {
if (height > inlineFrame.height) {
inlineFrame.height = height;
}
inlineFrame = inlineFrame.inlineParentFrame;
}
}
}
- (void)addFrame:(TTStyledFrame*)frame {
if (!_rootFrame) {
_rootFrame = [frame retain];
} else if (_topFrame) {
if (!_topFrame.firstChildFrame) {
_topFrame.firstChildFrame = frame;
} else {
_lastFrame.nextFrame = frame;
}
} else {
_lastFrame.nextFrame = frame;
}
_lastFrame = frame;
}
- (void)pushFrame:(TTStyledBoxFrame*)frame {
[self addFrame:frame];
frame.parentFrame = _topFrame;
_topFrame = frame;
}
- (void)popFrame {
_lastFrame = _topFrame;
_topFrame = _topFrame.parentFrame;
}
- (TTStyledFrame*)addContentFrame:(TTStyledFrame*)frame width:(CGFloat)width {
[self addFrame:frame];
if (!_lineFirstFrame) {
_lineFirstFrame = frame;
}
_x += width;
return frame;
}
- (void)addContentFrame:(TTStyledFrame*)frame width:(CGFloat)width height:(CGFloat)height {
frame.bounds = CGRectMake(_x, _height, width, height);
[self addContentFrame:frame width:width];
}
- (void)addAbsoluteFrame:(TTStyledFrame*)frame width:(CGFloat)width height:(CGFloat)height {
frame.bounds = CGRectMake(_x, _height, width, height);
[self addFrame:frame];
}
- (TTStyledInlineFrame*)addInlineFrame:(TTStyle*)style element:(TTStyledElement*)element
width:(CGFloat)width height:(CGFloat)height {
TTStyledInlineFrame* frame = [[[TTStyledInlineFrame alloc] initWithElement:element] autorelease];
frame.style = style;
frame.bounds = CGRectMake(_x, _height, width, height);
[self pushFrame:frame];
if (!_lineFirstFrame) {
_lineFirstFrame = frame;
}
return frame;
}
- (TTStyledInlineFrame*)cloneInlineFrame:(TTStyledInlineFrame*)frame {
TTStyledInlineFrame* parent = frame.inlineParentFrame;
if (parent) {
[self cloneInlineFrame:parent];
}
TTStyledInlineFrame* clone = [self addInlineFrame:frame.style element:frame.element
width:0 height:0];
clone.inlinePreviousFrame = frame;
frame.inlineNextFrame = clone;
return clone;
}
- (TTStyledFrame*)addBlockFrame:(TTStyle*)style element:(TTStyledElement*)element
width:(CGFloat)width height:(CGFloat)height {
TTStyledBoxFrame* frame = [[[TTStyledBoxFrame alloc] initWithElement:element] autorelease];
frame.style = style;
frame.bounds = CGRectMake(_x, _height, width, height);
[self pushFrame:frame];
return frame;
}
- (void)checkFloats {
if (_floatHeight && _height > _floatHeight) {
_minX -= _floatLeftWidth;
_width += _floatLeftWidth+_floatRightWidth;
_floatRightWidth = 0;
_floatLeftWidth = 0;
_floatHeight = 0;
}
}
- (void)breakLine {
if (_inlineFrame) {
TTStyledInlineFrame* inlineFrame = _inlineFrame;
while (inlineFrame) {
if (inlineFrame.style) {
TTBoxStyle* padding = [inlineFrame.style firstStyleOfClass:[TTBoxStyle class]];
if (padding) {
TTStyledInlineFrame* inlineFrame2 = inlineFrame;
while (inlineFrame2) {
inlineFrame2.y -= padding.padding.top;
inlineFrame2.height += padding.padding.top+padding.padding.bottom;
inlineFrame2 = inlineFrame2.inlineParentFrame;
}
}
}
inlineFrame = inlineFrame.inlineParentFrame;
}
}
// Vertically align all frames on the current line
if (_lineFirstFrame.nextFrame) {
TTStyledFrame* frame = _lineFirstFrame;
while (frame) {
// Align to the text baseline
// XXXjoe Support top, bottom, and center alignment also
if (frame.height < _lineHeight) {
UIFont* font = frame.font ? frame.font : _font;
[self offsetFrame:frame by:(_lineHeight - (frame.height - font.descender))];
}
frame = frame.nextFrame;
}
}
_height += _lineHeight;
[self checkFloats];
_lineWidth = 0;
_lineHeight = 0;
_x = _minX;
_lineFirstFrame = nil;
if (_inlineFrame) {
while ([_topFrame isKindOfClass:[TTStyledInlineFrame class]]) {
[self popFrame];
}
_inlineFrame = [self cloneInlineFrame:_inlineFrame];
}
}
- (TTStyledFrame*)addFrameForText:(NSString*)text element:(TTStyledElement*)element
node:(TTStyledTextNode*)node width:(CGFloat)width height:(CGFloat)height {
TTStyledTextFrame* frame = [[[TTStyledTextFrame alloc] initWithText:text element:element
node:node] autorelease];
frame.font = _font;
[self addContentFrame:frame width:width height:height];
return frame;
}
- (void)layoutElement:(TTStyledElement*)elt {
TTStyle* style = nil;
if (elt.className) {
TTStyle* eltStyle = [[TTStyleSheet globalStyleSheet] styleWithSelector:elt.className];
if (eltStyle) {
style = eltStyle;
}
}
if (!style && [elt isKindOfClass:[TTStyledLinkNode class]]) {
style = self.linkStyle;
}
// Figure out which font to use for the node
UIFont* font = nil;
TTTextStyle* textStyle = nil;
if (style) {
textStyle = [style firstStyleOfClass:[TTTextStyle class]];
if (textStyle) {
font = textStyle.font;
}
}
if (!font) {
if ([elt isKindOfClass:[TTStyledLinkNode class]]
|| [elt isKindOfClass:[TTStyledBoldNode class]]) {
font = self.boldFont;
} else if ([elt isKindOfClass:[TTStyledItalicNode class]]) {
font = self.italicFont;
} else {
font = self.font;
}
}
UIFont* lastFont = _font;
self.font = font;
TTBoxStyle* padding = style ? [style firstStyleOfClass:[TTBoxStyle class]] : nil;
if (padding && padding.position) {
TTStyledFrame* blockFrame = [self addBlockFrame:style element:elt width:_width height:_height];
CGFloat contentWidth = padding.margin.left + padding.margin.right;
CGFloat contentHeight = padding.margin.top + padding.margin.bottom;
if (elt.firstChild) {
TTStyledNode* child = elt.firstChild;
TTStyledLayout* layout = [[[TTStyledLayout alloc] initWithX:_minX
width:0 height:_height] autorelease];
layout.font = _font;
layout.invalidImages = _invalidImages;
[layout layout:child];
if (!_invalidImages && layout.invalidImages) {
_invalidImages = [layout.invalidImages retain];
}
TTStyledFrame* frame = [self addContentFrame:layout.rootFrame width:layout.width];
CGFloat frameHeight = layout.height - _height;
contentWidth += layout.width;
contentHeight += frameHeight;
if (padding.position == TTPositionFloatLeft) {
frame.x += _floatLeftWidth;
_floatLeftWidth += contentWidth;
if (_height+contentHeight > _floatHeight) {
_floatHeight = contentHeight+_height;
}
_minX += contentWidth;
_width -= contentWidth;
} else if (padding.position == TTPositionFloatRight) {
frame.x += _width - (_floatRightWidth + contentWidth);
_floatRightWidth += contentWidth;
if (_height+contentHeight > _floatHeight) {
_floatHeight = contentHeight+_height;
}
_x -= contentWidth;
_width -= contentWidth;
}
blockFrame.width = layout.width + padding.padding.left + padding.padding.right;
blockFrame.height = frameHeight + padding.padding.top + padding.padding.bottom;
}
} else {
CGFloat minX = _minX, width = _width, floatLeftWidth = _floatLeftWidth,
floatRightWidth = _floatRightWidth, floatHeight = _floatHeight;
BOOL isBlock = [elt isKindOfClass:[TTStyledBlock class]];
TTStyledFrame* blockFrame = nil;
if (isBlock) {
if (padding) {
_x += padding.margin.left;
_minX += padding.margin.left;
_width -= padding.margin.left + padding.margin.right;
_height += padding.margin.top;
}
if (_lastFrame) {
if (!_lineHeight && [elt isKindOfClass:[TTStyledLineBreakNode class]]) {
_lineHeight = self.fontHeight;
}
[self breakLine];
}
if (style) {
blockFrame = [self addBlockFrame:style element:elt width:_width height:_height];
}
} else {
if (padding) {
_x += padding.margin.left;
}
if (style) {
_inlineFrame = [self addInlineFrame:style element:elt width:0 height:0];
}
}
if (padding) {
if (isBlock) {
_minX += padding.padding.left;
}
_width -= padding.padding.left+padding.padding.right;
_x += padding.padding.left;
[self expandLineWidth:padding.padding.left];
if (isBlock) {
_height += padding.padding.top;
}
}
if (elt.firstChild) {
[self layout:elt.firstChild container:elt];
}
if (isBlock) {
_minX = minX, _width = width, _floatLeftWidth = floatLeftWidth,
_floatRightWidth = floatRightWidth, _floatHeight = floatHeight;
[self breakLine];
if (padding) {
_height += padding.padding.bottom;
}
blockFrame.height = _height - blockFrame.height;
if (padding) {
if (blockFrame.height < padding.minSize.height) {
_height += padding.minSize.height - blockFrame.height;
blockFrame.height = padding.minSize.height;
}
_height += padding.margin.bottom;
}
} else if (!isBlock && style) {
if (padding) {
_x += padding.padding.right + padding.margin.right;
_lineWidth += padding.padding.right + padding.margin.right;
TTStyledInlineFrame* inlineFrame = _inlineFrame;
while (inlineFrame) {
if (inlineFrame != _inlineFrame) {
inlineFrame.width += padding.margin.right;
}
inlineFrame.width += padding.padding.right;
inlineFrame.y -= padding.padding.top;
inlineFrame.height += padding.padding.top+padding.padding.bottom;
inlineFrame = inlineFrame.inlineParentFrame;
}
}
_inlineFrame = _inlineFrame.inlineParentFrame;
}
}
self.font = lastFont;
if (style) {
[self popFrame];
}
}
- (void)layoutImage:(TTStyledImageNode*)imageNode container:(TTStyledElement*)element {
UIImage* image = imageNode.image;
if (!image && imageNode.url) {
if (!_invalidImages) {
_invalidImages = TTCreateNonRetainingArray();
}
[_invalidImages addObject:imageNode];
}
TTStyle* style = imageNode.className
? [[TTStyleSheet globalStyleSheet] styleWithSelector:imageNode.className] : nil;
TTBoxStyle* padding = style ? [style firstStyleOfClass:[TTBoxStyle class]] : nil;
CGFloat imageWidth = imageNode.width ? imageNode.width : image.size.width;
CGFloat imageHeight = imageNode.height ? imageNode.height : image.size.height;
CGFloat contentWidth = imageWidth;
CGFloat contentHeight = imageHeight;
if (padding && padding.position != TTPositionAbsolute) {
_x += padding.margin.left;
contentWidth += padding.margin.left + padding.margin.right;
contentHeight += padding.margin.top + padding.margin.bottom;
}
if ((!padding || !padding.position) && (_lineWidth + contentWidth > _width)) {
if (_lineWidth) {
// The image will be placed on the next line, so create a new frame for
// the current line and mark it with a line break
[self breakLine];
} else {
_width = contentWidth;
}
}
TTStyledImageFrame* frame = [[[TTStyledImageFrame alloc] initWithElement:element
node:imageNode] autorelease];
frame.style = style;
if (!padding || !padding.position) {
[self addContentFrame:frame width:imageWidth height:imageHeight];
[self expandLineWidth:contentWidth];
[self inflateLineHeight:contentHeight];
} else if (padding.position == TTPositionAbsolute) {
[self addAbsoluteFrame:frame width:imageWidth height:imageHeight];
frame.x += padding.margin.left;
frame.y += padding.margin.top;
} else if (padding.position == TTPositionFloatLeft) {
[self addContentFrame:frame width:imageWidth height:imageHeight];
frame.x += _floatLeftWidth;
_floatLeftWidth += contentWidth;
if (_height+contentHeight > _floatHeight) {
_floatHeight = contentHeight+_height;
}
_minX += contentWidth;
_width -= contentWidth;
} else if (padding.position == TTPositionFloatRight) {
[self addContentFrame:frame width:imageWidth height:imageHeight];
frame.x += _width - (_floatRightWidth + contentWidth);
_floatRightWidth += contentWidth;
if (_height+contentHeight > _floatHeight) {
_floatHeight = contentHeight+_height;
}
_x -= contentWidth;
_width -= contentWidth;
}
if (padding && padding.position != TTPositionAbsolute) {
frame.y += padding.margin.top;
_x += padding.margin.right;
}
}
- (void)layoutText:(TTStyledTextNode*)textNode container:(TTStyledElement*)element {
NSString* text = textNode.text;
NSUInteger length = text.length;
if (!textNode.nextSibling && textNode == _rootNode) {
// This is the only node, so measure it all at once and move on
CGSize textSize = [text sizeWithFont:_font
constrainedToSize:CGSizeMake(_width, CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
[self addFrameForText:text element:element node:textNode width:textSize.width
height:textSize.height];
_height += textSize.height;
return;
}
NSCharacterSet* whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSInteger index = 0;
NSInteger lineStartIndex = 0;
CGFloat frameWidth = 0;
while (index < length) {
// Search for the next whitespace character
NSRange searchRange = NSMakeRange(index, length - index);
NSRange spaceRange = [text rangeOfCharacterFromSet:whitespace options:0 range:searchRange];
// Get the word prior to the whitespace
NSRange wordRange = spaceRange.location != NSNotFound
? NSMakeRange(searchRange.location, (spaceRange.location+1) - searchRange.location)
: NSMakeRange(searchRange.location, length - searchRange.location);
NSString* word = [text substringWithRange:wordRange];
// Measure the word and check to see if it fits on the current line
CGSize wordSize = [word sizeWithFont:_font
constrainedToSize:CGSizeMake(_width, CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
if (_lineWidth + wordSize.width > _width) {
// The word will be placed on the next line, so create a new frame for
// the current line and mark it with a line break
NSRange lineRange = NSMakeRange(lineStartIndex, index - lineStartIndex);
if (lineRange.length) {
NSString* line = [text substringWithRange:lineRange];
[self addFrameForText:line element:element node:textNode width:frameWidth
height:_lineHeight ? _lineHeight : self.fontHeight];
}
if (_lineWidth) {
[self breakLine];
} else {
_width = wordSize.width;
}
lineStartIndex = lineRange.location + lineRange.length;
frameWidth = 0;
}
if (!_lineWidth && textNode == _lastNode) {
// We are at the start of a new line, and this is the last node, so we don't need to
// keep measuring every word. We can just measure all remaining text and create a new
// frame for all of it.
NSString* lines = [text substringWithRange:searchRange];
CGSize linesSize = [lines sizeWithFont:_font
constrainedToSize:CGSizeMake(_width, CGFLOAT_MAX)
lineBreakMode:UILineBreakModeWordWrap];
[self addFrameForText:lines element:element node:textNode width:linesSize.width
height:linesSize.height];
_height += linesSize.height;
break;
}
frameWidth += wordSize.width;
[self expandLineWidth:wordSize.width];
[self inflateLineHeight:wordSize.height];
index = wordRange.location + wordRange.length;
if (index >= length) {
// The current word was at the very end of the string
NSRange lineRange = NSMakeRange(lineStartIndex, (wordRange.location + wordRange.length)
- lineStartIndex);
NSString* line = !_lineWidth ? word : [text substringWithRange:lineRange];
[self addFrameForText:line element:element node:textNode width:frameWidth
height:_lineHeight ? _lineHeight : self.fontHeight];
frameWidth = 0;
}
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// NSObject
- (id)initWithRootNode:(TTStyledNode*)rootNode {
if (self = [self init]) {
_rootNode = rootNode;
}
return self;
}
- (id)initWithX:(CGFloat)x width:(CGFloat)width height:(CGFloat)height {
if (self = [self init]) {
_x = x;
_minX = x;
_width = width;
_height = height;
}
return self;
}
- (id)init {
if (self = [super init]) {
_x = 0;
_width = 0;
_height = 0;
_lineWidth = 0;
_lineHeight = 0;
_minX = 0;
_floatLeftWidth = 0;
_floatRightWidth = 0;
_floatHeight = 0;
_rootFrame = nil;
_lineFirstFrame = nil;
_inlineFrame = nil;
_topFrame = nil;
_lastFrame = nil;
_font = nil;
_boldFont = nil;
_italicFont = nil;
_linkStyle = nil;
_rootNode = nil;
_lastNode = nil;
_invalidImages = nil;
}
return self;
}
- (void)dealloc {
[_rootFrame release];
[_font release];
[_boldFont release];
[_italicFont release];
[_linkStyle release];
[_invalidImages release];
[super dealloc];
}
//////////////////////////////////////////////////////////////////////////////////////////////////
// public
- (UIFont*)font {
if (!_font) {
self.font = TTSTYLEVAR(font);
}
return _font;
}
- (void)setFont:(UIFont*)font {
if (font != _font) {
[_font release];
_font = [font retain];
[_boldFont release];
_boldFont = nil;
[_italicFont release];
_italicFont = nil;
}
}
- (void)layout:(TTStyledNode*)node container:(TTStyledElement*)element {
while (node) {
if ([node isKindOfClass:[TTStyledImageNode class]]) {
TTStyledImageNode* imageNode = (TTStyledImageNode*)node;
[self layoutImage:imageNode container:element];
} else if ([node isKindOfClass:[TTStyledElement class]]) {
TTStyledElement* elt = (TTStyledElement*)node;
[self layoutElement:elt];
} else if ([node isKindOfClass:[TTStyledTextNode class]]) {
TTStyledTextNode* textNode = (TTStyledTextNode*)node;
[self layoutText:textNode container:element];
}
node = node.nextSibling;
}
}
- (void)layout:(TTStyledNode*)node {
[self layout:node container:nil];
if (_lineWidth) {
[self breakLine];
}
}
@end