Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 885 lines (724 sloc) 29.517 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 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 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884
//
// Copyright 2009-2011 Facebook
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License 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 "Three20Style/TTStyledLayout.h"

// Style
#import "Three20Style/TTGlobalStyle.h"
#import "Three20Style/TTStyledFrame.h"
#import "Three20Style/TTStyleSheet.h"
#import "Three20Style/TTBoxStyle.h"
#import "Three20Style/TTTextStyle.h"
#import "Three20Style/TTStyledElement.h"
#import "Three20Style/TTStyledInlineFrame.h"
#import "Three20Style/TTStyledTextFrame.h"
#import "Three20Style/TTStyledImageFrame.h"
#import "Three20Style/UIFontAdditions.h"

// Styled nodes
#import "Three20Style/TTStyledImageNode.h"
#import "Three20Style/TTStyledBoldNode.h"
#import "Three20Style/TTStyledItalicNode.h"
#import "Three20Style/TTStyledLinkNode.h"
#import "Three20Style/TTStyledBlock.h"
#import "Three20Style/TTStyledLineBreakNode.h"
#import "Three20Style/TTStyledTextNode.h"

// Core
#import "Three20Core/TTGlobalCore.h"
#import "Three20Core/TTCorePreprocessorMacros.h"


///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation TTStyledLayout

@synthesize width = _width;
@synthesize height = _height;
@synthesize rootFrame = _rootFrame;
@synthesize font = _font;
@synthesize textAlignment = _textAlignment;
@synthesize invalidImages = _invalidImages;


///////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithRootNode:(TTStyledNode*)rootNode {
self = [super init];
  if (self) {
    _rootNode = rootNode;
  }

  return self;
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithX:(CGFloat)x width:(CGFloat)width height:(CGFloat)height {
self = [super init];
  if (self) {
    _x = x;
    _minX = x;
    _width = width;
    _height = height;
  }

  return self;
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc {
  TT_RELEASE_SAFELY(_rootFrame);
  TT_RELEASE_SAFELY(_font);
  TT_RELEASE_SAFELY(_boldFont);
  TT_RELEASE_SAFELY(_italicFont);
  TT_RELEASE_SAFELY(_linkStyle);
  TT_RELEASE_SAFELY(_invalidImages);

  [super dealloc];
}


///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark 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;
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (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)offsetFrame:(TTStyledFrame*)frame byX:(CGFloat)x {
  frame.x += x;

  if ([frame isKindOfClass:[TTStyledInlineFrame class]]) {
    TTStyledInlineFrame* inlineFrame = (TTStyledInlineFrame*)frame;
    TTStyledFrame* child = inlineFrame.firstChildFrame;
    while (child) {
      [self offsetFrame:child byX:x];
      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;

    // Find the descender that descends the farthest below the baseline.
    // font.descender is a negative number if the descender descends below
    // the baseline (as most descenders do), but can also be a positive
    // number for a descender above the baseline.
    CGFloat lowestDescender = MAXFLOAT;
    frame = _lineFirstFrame;
    while (frame) {
      UIFont* font = frame.font ? frame.font : _font;
      lowestDescender = MIN(lowestDescender, font.descender);
      frame = frame.nextFrame;
    }

    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 -
                                                   (lowestDescender - font.descender)))];
      }
      frame = frame.nextFrame;
    }
  }

  // Horizontally align all frames on current line if required
  if (_textAlignment != UITextAlignmentLeft) {
    CGFloat remainingSpace = _width - _lineWidth;
    CGFloat offset = _textAlignment == UITextAlignmentCenter ? remainingSpace/2 : remainingSpace;

    TTStyledFrame* frame = _lineFirstFrame;
    while (frame) {
      [self offsetFrame:frame byX:offset];
      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 = [_font ttLineHeight];
        }
        [self breakLine];
      }
      if (style) {
        blockFrame = [self addBlockFrame:style element:elt width:_width height:_height];
      }

    } else {
      if (padding) {
        _x += padding.margin.left;
        _height += padding.margin.top;
      }
      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 stringIndex = 0;
  NSInteger lineStartIndex = 0;
  CGFloat frameWidth = 0.0f;
  NSInteger frameStart = 0;

  while (stringIndex < length) {
    // Search for the next whitespace character
    NSRange searchRange = NSMakeRange(stringIndex, length - stringIndex);
    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];

    // If there is no width to constrain to, then just use an infinite width,
    // which will prevent any word wrapping
    CGFloat availWidth = _width ? _width : CGFLOAT_MAX;

    // Measure the word and check to see if it fits on the current line
    CGSize wordSize = [word sizeWithFont:_font];
    if (wordSize.width > _width) {
      for (NSInteger i = 0; i < word.length; ++i) {
        NSString* c = [word substringWithRange:NSMakeRange(i, 1)];
        CGSize letterSize = [c sizeWithFont:_font];

        if (_lineWidth + letterSize.width > _width) {
          NSRange lineRange = NSMakeRange(lineStartIndex, stringIndex - lineStartIndex);
          if (lineRange.length) {
            NSString* line = [text substringWithRange:lineRange];
            frameWidth = [[text substringWithRange:NSMakeRange(frameStart,
                                                               stringIndex - frameStart)]
                          sizeWithFont:_font].width;
            [self addFrameForText:line element:element node:textNode width:frameWidth
                  height:[_font ttLineHeight]];
          }

          if (_lineWidth) {
            [self breakLine];
          }

          lineStartIndex = lineRange.location + lineRange.length;
          frameStart = stringIndex;
        }

        [self expandLineWidth:letterSize.width];
        [self inflateLineHeight:wordSize.height];
        ++stringIndex;
      }

      NSRange lineRange = NSMakeRange(lineStartIndex, stringIndex - lineStartIndex);
      if (lineRange.length) {
        NSString* line = [text substringWithRange:lineRange];
        frameWidth = [[text substringWithRange:NSMakeRange(frameStart, stringIndex - frameStart)]
                      sizeWithFont:_font].width;
        [self addFrameForText:line element:element node:textNode width:frameWidth
              height:[_font ttLineHeight]];

        lineStartIndex = lineRange.location + lineRange.length;
        frameStart = stringIndex;
      }

    } else {
      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, stringIndex - lineStartIndex);
        if (lineRange.length) {
          NSString* line = [text substringWithRange:lineRange];
          frameWidth = [[text substringWithRange:NSMakeRange(frameStart, stringIndex - frameStart)]
                        sizeWithFont:_font].width;
          [self addFrameForText:line element:element node:textNode width:frameWidth
                height:[_font ttLineHeight]];
        }

        if (_lineWidth) {
          [self breakLine];
        }
        lineStartIndex = lineRange.location + lineRange.length;
        frameStart = stringIndex;
      }

      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(availWidth, CGFLOAT_MAX)
                                  lineBreakMode:UILineBreakModeWordWrap];

        [self addFrameForText:lines element:element node:textNode width:linesSize.width
             height:linesSize.height];
        _height += linesSize.height;
        break;
      }

      [self expandLineWidth:wordSize.width];
      [self inflateLineHeight:wordSize.height];

      stringIndex = wordRange.location + wordRange.length;
      if (stringIndex >= 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];
        frameWidth = [[text substringWithRange:NSMakeRange(frameStart, stringIndex - frameStart)]
                      sizeWithFont:_font].width;
        [self addFrameForText:line element:element node:textNode width:frameWidth
              height:[_font ttLineHeight]];
        frameWidth = 0;
      }
    }
  }
}


///////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Public


///////////////////////////////////////////////////////////////////////////////////////////////////
- (UIFont*)font {
  if (!_font) {
    self.font = TTSTYLEVAR(font);
  }
  return _font;
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setFont:(UIFont*)font {
  if (font != _font) {
    [_font release];
    _font = [font retain];
    TT_RELEASE_SAFELY(_boldFont);
    TT_RELEASE_SAFELY(_italicFont);
  }
}


///////////////////////////////////////////////////////////////////////////////////////////////////
- (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
Something went wrong with that request. Please try again.