+ void Align( const LayoutParameters& layoutParameters,
+ const Vector<LineRun>& lines,
+ Vector<Vector2>& glyphPositions )
+ {
+ Vector2* glyphPositionsBuffer = glyphPositions.Begin();
+
+ // Traverse all lines and align the glyphs.
+ // LayoutParameters contains bidirectional info for those lines with
+ // right to left text, this info includes the paragraph's direction.
+
+ LineIndex bidiLineIndex = 0u;
+ for( Vector<LineRun>::ConstIterator it = lines.Begin(), endIt = lines.End();
+ it != endIt;
+ ++it )
+ {
+ const LineRun& line = *it;
+
+ // 1) Get the paragrap's direction.
+ bool paragraphDirection = false;
+
+ // Check if there is any right to left line.
+ if( ( NULL != layoutParameters.lineBidirectionalInfoRunsBuffer ) &&
+ ( bidiLineIndex < layoutParameters.numberOfBidirectionalInfoRuns ) )
+ {
+ const BidirectionalLineInfoRun* bidiLine = layoutParameters.lineBidirectionalInfoRunsBuffer + bidiLineIndex;
+
+ // Get the right to left line that match with current line.
+ while( ( line.characterRun.characterIndex > bidiLine->characterRun.characterIndex ) &&
+ ( bidiLineIndex < layoutParameters.numberOfBidirectionalInfoRuns ) )
+ {
+ ++bidiLineIndex;
+ bidiLine = layoutParameters.lineBidirectionalInfoRunsBuffer + bidiLineIndex;
+ }
+
+ if( line.characterRun.characterIndex == bidiLine->characterRun.characterIndex )
+ {
+ paragraphDirection = bidiLine->direction;
+ }
+ }
+
+ // 2) Calculate the alignment offset accordingly with the align option,
+ // the box width, line length, and the paragraphs direction.
+ float alignOffset = CalculateAlignment( layoutParameters.boundingBox.width,
+ line.lineSize.width,
+ line.extraLength,
+ paragraphDirection );
+
+ // 3) Traverse all glyphs and update the 'x' position.
+ for( GlyphIndex index = line.glyphIndex,
+ endIndex = line.glyphIndex + line.numberOfGlyphs;
+ index < endIndex;
+ ++index )
+ {
+ Vector2& position = *( glyphPositionsBuffer + index );
+
+ position.x += alignOffset;
+ }
+ }
+ }
+