+ // Add an extra line if the last character is a new paragraph character and the last line doesn't have zero characters.
+ if( layoutParameters.isLastNewParagraph )
+ {
+ Length numberOfLines = lines.Count();
+ if( 0u != numberOfLines )
+ {
+ const LineRun& lastLine = *( lines.End() - 1u );
+
+ if( 0u != lastLine.characterRun.numberOfCharacters )
+ {
+ // Need to add a new line with no characters but with height to increase the layoutSize.height
+ LineRun newLine;
+ Initialize( newLine );
+ lines.PushBack( newLine );
+
+ UpdateTextLayout( layoutParameters,
+ lastLine.characterRun.characterIndex + lastLine.characterRun.numberOfCharacters,
+ lastLine.glyphRun.glyphIndex + lastLine.glyphRun.numberOfGlyphs,
+ layoutSize,
+ lines.Begin(),
+ numberOfLines );
+ }
+ }
+ }
+
+ // Calculates the layout size.
+ UpdateLayoutSize( lines,
+ layoutSize );
+
+ // Nothing else do if there are no glyphs to layout.
+ return false;
+ }
+
+ const GlyphIndex lastGlyphPlusOne = layoutParameters.startGlyphIndex + layoutParameters.numberOfGlyphs;
+
+ // In a previous layout, an extra line with no characters may have been added if the text ended with a new paragraph character.
+ // This extra line needs to be removed.
+ if( 0u != lines.Count() )
+ {
+ Vector<LineRun>::Iterator lastLine = lines.End() - 1u;
+
+ if( ( 0u == lastLine->characterRun.numberOfCharacters ) &&
+ ( lastGlyphPlusOne == layoutParameters.totalNumberOfGlyphs ) )
+ {
+ lines.Remove( lastLine );
+ }
+ }
+
+ // Set the first paragraph's direction.
+ CharacterDirection paragraphDirection = ( NULL != layoutParameters.characterDirectionBuffer ) ? *layoutParameters.characterDirectionBuffer : !RTL;
+
+ // Whether the layout is being updated or set from scratch.
+ const bool updateCurrentBuffer = layoutParameters.numberOfGlyphs < layoutParameters.totalNumberOfGlyphs;
+
+ Vector2* glyphPositionsBuffer = NULL;
+ Vector<Vector2> newGlyphPositions;
+
+ LineRun* linesBuffer = NULL;
+ Vector<LineRun> newLines;
+
+ // Estimate the number of lines.
+ Length linesCapacity = std::max( 1u, layoutParameters.estimatedNumberOfLines );
+ Length numberOfLines = 0u;
+
+ if( updateCurrentBuffer )
+ {
+ newGlyphPositions.Resize( layoutParameters.numberOfGlyphs );
+ glyphPositionsBuffer = newGlyphPositions.Begin();
+
+ newLines.Resize( linesCapacity );
+ linesBuffer = newLines.Begin();
+ }
+ else
+ {
+ glyphPositionsBuffer = glyphPositions.Begin();
+
+ lines.Resize( linesCapacity );
+ linesBuffer = lines.Begin();
+ }
+
+ float penY = CalculateLineOffset( lines,
+ layoutParameters.startLineIndex );
+
+ for( GlyphIndex index = layoutParameters.startGlyphIndex; index < lastGlyphPlusOne; )
+ {
+ CharacterDirection currentParagraphDirection = paragraphDirection;