+ void CalculateHorizontalAlignment( float boxWidth,
+ LineRun& line,
+ bool isLastLine )
+ {
+ line.alignmentOffset = 0.f;
+ const bool isRTL = RTL == line.direction;
+ float lineLength = line.width;
+
+ HorizontalAlignment alignment = mHorizontalAlignment;
+ if( isRTL &&
+ ( HORIZONTAL_ALIGN_CENTER != alignment ) )
+ {
+ if( HORIZONTAL_ALIGN_BEGIN == alignment )
+ {
+ alignment = HORIZONTAL_ALIGN_END;
+ }
+ else
+ {
+ alignment = HORIZONTAL_ALIGN_BEGIN;
+ }
+ }
+
+ switch( alignment )
+ {
+ case HORIZONTAL_ALIGN_BEGIN:
+ {
+ line.alignmentOffset = 0.f;
+
+ if( isRTL )
+ {
+ // 'Remove' the white spaces at the end of the line (which are at the beginning in visual order)
+ line.alignmentOffset -= line.extraLength;
+
+ if( isLastLine )
+ {
+ line.alignmentOffset += std::min( line.extraLength, boxWidth - lineLength );
+ }
+ }
+ break;
+ }
+ case HORIZONTAL_ALIGN_CENTER:
+ {
+ if( isLastLine && !isRTL )
+ {
+ lineLength += line.extraLength;
+ if( lineLength > boxWidth )
+ {
+ lineLength = boxWidth;
+ line.alignmentOffset = 0.f;
+ break;
+ }
+ }
+
+ line.alignmentOffset = 0.5f * ( boxWidth - lineLength );
+
+ if( isRTL )
+ {
+ line.alignmentOffset -= line.extraLength;
+
+ if( isLastLine )
+ {
+ line.alignmentOffset += 0.5f * std::min( line.extraLength, boxWidth - lineLength );
+ }
+ }
+
+ line.alignmentOffset = floorf( line.alignmentOffset ); // try to avoid pixel alignment.
+ break;
+ }
+ case HORIZONTAL_ALIGN_END:
+ {
+ if( isLastLine && !isRTL )
+ {
+ lineLength += line.extraLength;
+ if( lineLength > boxWidth )
+ {
+ line.alignmentOffset = 0.f;
+ break;
+ }
+ }
+
+ if( isRTL )
+ {
+ lineLength += line.extraLength;
+ }
+
+ line.alignmentOffset = boxWidth - lineLength;
+ break;
+ }
+ }