Skip to content

Commit

Permalink
Text: Fix line length calculation
Browse files Browse the repository at this point in the history
The numChars return from ARX_UNICODE_FormattingInRect() did not include
the last character when the whole text fit into rect but no more lines
would fit.

The credits code had a workaround for this which did not take into
account that characters can be longer than one byte.

This fixes the bug in ARX_UNICODE_FormattingInRect() and removes the
credits workaround.

Fixes: issue #1545
  • Loading branch information
dscharrer committed Jun 1, 2021
1 parent c98cd80 commit 36933f2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/gui/Credits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,9 @@ void Credits::addLine(std::string & phrase, float & drawpos, int sourceLineNumbe
if(!phrase.empty() && phrase[0] == '~') {
// Heading
drawpos += m_lineHeight * 0.6f;
phrase[0] = ' ';
phrase = phrase.substr(1);
infomations.fColors = Color::white;
} else if(phrase[0] == '&') {
} else if(!phrase.empty() && phrase[0] == '&') {
// Heading continued
infomations.fColors = Color::white;
} else {
Expand All @@ -330,13 +330,13 @@ void Credits::addLine(std::string & phrase, float & drawpos, int sourceLineNumbe

// Split long lines
long n = ARX_UNICODE_ForceFormattingInRect(hFontCredits, phrase.begin(), phrase.end(), linerect);
arx_assert(n >= 0 && size_t(n) < phrase.length());
arx_assert(n >= 0 && size_t(n) <= phrase.length());

// Long lines are not simple
isSimpleLine = isSimpleLine && size_t(n + 1) == phrase.length();
isSimpleLine = isSimpleLine && size_t(n) == phrase.length();

infomations.sText = phrase.substr(0, size_t(n + 1) == phrase.length() ? n + 1 : n);
phrase = phrase.substr(n + 1);
infomations.sText = phrase.substr(0, n);
phrase = phrase.substr(n);

// Center the text on the screen
int linesize = hFontCredits->getTextSize(infomations.sText).width();
Expand Down
6 changes: 5 additions & 1 deletion src/gui/Text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,14 @@ static void ARX_UNICODE_FormattingInRect(Font * font, std::string::const_iterato
// This is the last character of the string
if(isLineBreak || next == txtend) {

if(!isLineBreak) {
it = next;
}

// Draw the line
if(!computeOnly) {
std::string::const_iterator itTextStart = itLastLineBreak;
std::string::const_iterator itTextEnd = isLineBreak ? it : next;
std::string::const_iterator itTextEnd = it;
font->draw(rect.left, penY, itTextStart, itTextEnd, col);
}

Expand Down

0 comments on commit 36933f2

Please sign in to comment.