Skip to content

Commit

Permalink
StGLTextFormatter - implement a (broken) right-to-left text flipper
Browse files Browse the repository at this point in the history
  • Loading branch information
gkv311 committed Nov 6, 2018
1 parent 89a4337 commit d9c53eb
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
68 changes: 68 additions & 0 deletions StShared/StGLTextFormatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,27 @@ void StGLTextFormatter::newLine(const size_t theLastRect) {
}
}

void StGLTextFormatter::flipLeftRight(size_t theCharFrom, size_t theCharTo) {
if(theCharFrom == theCharTo
|| theCharTo == size_t(-1)) {
return;
}

float aRight = myRects[theCharTo].px.right();
for(size_t aCharIter = theCharFrom; aCharIter < theCharTo; ++aCharIter) {
StGLRect& aRect1 = myRects[aCharIter + 0].px;
const StGLRect& aRect2 = myRects[aCharIter + 1].px;
float aStepX = aRect2.left() - aRect1.left();
aRect1.moveRightTo(aRight);
aRight -= aStepX;
}

StGLRect& aRectLast = myRects[theCharTo].px;
aRectLast.moveRightTo(aRight);

std::reverse (myRects.begin() + theCharFrom, (theCharTo + 1) == myRects.size() ? myRects.end() : myRects.begin() + theCharTo + 1);
}

void StGLTextFormatter::format(const GLfloat theWidth,
const GLfloat theHeight) {
if(myRectsNb == 0 || myIsFormatted) {
Expand All @@ -412,6 +433,8 @@ void StGLTextFormatter::format(const GLfloat theWidth,
// split text into lines and apply horizontal alignment
myPenCurrLine = -myAscender;
size_t aRectIter = 0;
size_t aFlipLower = 0, aFlipInnerLower = 0;
bool isRight2Left = false, isLeft2RightInner = false;
for(StUtf8Iter anIter = myString.iterator(); *anIter != 0; ++anIter) {
const stUtf32_t aCharThis = *anIter;
if(aCharThis == '\x0D') {
Expand All @@ -423,13 +446,43 @@ void StGLTextFormatter::format(const GLfloat theWidth,
myAlignWidth = myRects[aLastRect].px.right();
///toCorrectXAlignment = true;
}
if(isRight2Left) {
if(isLeft2RightInner) {
flipLeftRight(aFlipInnerLower, aLastRect);
isLeft2RightInner = false;
}
flipLeftRight(aFlipLower, aLastRect);
aFlipLower = aLastRect + 1;
}
newLine(aLastRect);
continue;
} else if(aCharThis == ' ') {
myRectWordStart = aRectIter;
continue;
}

if(StFTFont::isRightToLeft(aCharThis)) {
if(!isRight2Left) {
isRight2Left = true;
aFlipLower = aRectIter;
} else if(isLeft2RightInner) {
flipLeftRight(aFlipInnerLower, aRectIter - 1);
isLeft2RightInner = false;
}
} else if(isRight2Left) {
if(aCharThis >= '0' && aCharThis <= '9'
|| (isLeft2RightInner && (aCharThis == '.' || aCharThis == ','))) {
if(!isLeft2RightInner) {
isLeft2RightInner = true;
aFlipInnerLower = aRectIter;
}
} else {
isRight2Left = false;
flipLeftRight(aFlipLower, aRectIter - 1);
aFlipLower = aRectIter;
}
}

GLfloat aWidth = myRects[aRectIter].px.right() - myLineLeft;
myTextWidth = stMax(myTextWidth, aWidth);
if(theWidth > 0.0f && aWidth >= theWidth) {
Expand All @@ -439,6 +492,14 @@ void StGLTextFormatter::format(const GLfloat theWidth,
aLastRect = aRectIter - 1;
}

if(isRight2Left) {
if(isLeft2RightInner) {
flipLeftRight(aFlipInnerLower, aLastRect);
isLeft2RightInner = false;
}
flipLeftRight(aFlipLower, aLastRect);
aFlipLower = aLastRect + 1;
}
newLine(aLastRect);
}

Expand All @@ -449,6 +510,13 @@ void StGLTextFormatter::format(const GLfloat theWidth,
if(myRectsNb != 0) {
myTextWidth = stMax(myTextWidth, myRects[myRectsNb - 1].px.right() - myLineLeft);
}
if(isRight2Left) {
if(isLeft2RightInner) {
flipLeftRight(aFlipInnerLower, myRectsNb - 1);
isLeft2RightInner = false;
}
flipLeftRight(aFlipLower, myRectsNb - 1);
}
newLine(myRectsNb - 1);
if(myRectsNb != 0
&& myAlignWidth <= 0.0f) {
Expand Down
5 changes: 5 additions & 0 deletions include/StGL/StGLTextFormatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ class StGLTextFormatter {
*/
ST_CPPEXPORT void newLine(const size_t theLastRect);

/**
* Flip left to right order.
*/
ST_CPPEXPORT void flipLeftRight(size_t theCharFrom, size_t theCharTo);

protected: //! @name configuration

StAlignX myAlignX; //!< horizontal alignment style
Expand Down

0 comments on commit d9c53eb

Please sign in to comment.