-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
StGLTextFormatter - implement a (broken) right-to-left text flipper
- Loading branch information
Showing
2 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/** | ||
* Copyright © 2012-2017 Kirill Gavrilov <[email protected]> | ||
* Copyright © 2012-2018 Kirill Gavrilov <[email protected]> | ||
* | ||
* Distributed under the Boost Software License, Version 1.0. | ||
* See accompanying file license-boost.txt or copy at | ||
|
@@ -10,6 +10,8 @@ | |
|
||
#include <StGL/StGLVertexBuffer.h> | ||
|
||
#include <algorithm> | ||
|
||
/** | ||
* Auxiliary function to translate rectangles by the vector. | ||
*/ | ||
|
@@ -395,6 +397,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) { | ||
|
@@ -412,6 +435,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') { | ||
|
@@ -423,13 +448,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) { | ||
|
@@ -439,6 +494,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); | ||
} | ||
|
||
|
@@ -449,6 +512,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) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters