Fix text rendering over maximum line width with newline

When a word is rendered over the maximum line width it should be moved to the next line.

However, this was not working correctly if the word ends with a newline instead of a space, because then the text x-advance including this word did not exceed the maximum width, as the newline caused the x-advance to be reset before it can be used.

This is fixed by replacing the unused flag `TEXTFLAG_ALLOW_NEWLINE` with the flag `TEXTFLAG_DISALLOW_NEWLINE`, which causes newline characters to be replaced with space characters. This flag is then used when calculating whether the next word fits in the current line.

Closes #4541.
This commit is contained in:
Robert Müller 2023-02-18 14:00:18 +01:00
parent 6813c807e0
commit 3cd294372a
2 changed files with 20 additions and 12 deletions

View file

@ -1182,6 +1182,7 @@ public:
Compare.m_X = DrawX;
Compare.m_Y = DrawY;
Compare.m_Flags &= ~TEXTFLAG_RENDER;
Compare.m_Flags |= TEXTFLAG_DISALLOW_NEWLINE;
Compare.m_LineWidth = -1;
TextEx(&Compare, pCurrent, Wlen);
@ -1196,7 +1197,7 @@ public:
Cutter.m_X = DrawX;
Cutter.m_Y = DrawY;
Cutter.m_Flags &= ~TEXTFLAG_RENDER;
Cutter.m_Flags |= TEXTFLAG_STOP_AT_END;
Cutter.m_Flags |= TEXTFLAG_STOP_AT_END | TEXTFLAG_DISALLOW_NEWLINE;
TextEx(&Cutter, pCurrent, Wlen);
Wlen = Cutter.m_CharCount;
@ -1221,17 +1222,24 @@ public:
{
pCursor->m_CharCount += pTmp - pCurrent;
pCurrent = pTmp;
const int Character = NextCharacter;
int Character = NextCharacter;
NextCharacter = str_utf8_decode(&pTmp);
if(Character == '\n')
{
LastCharGlyphIndex = 0;
++CharacterCounter;
StartNewLine();
if(pCursor->m_MaxLines > 0 && LineCount > pCursor->m_MaxLines)
break;
continue;
if((pCursor->m_Flags & TEXTFLAG_DISALLOW_NEWLINE) == 0)
{
LastCharGlyphIndex = 0;
++CharacterCounter;
StartNewLine();
if(pCursor->m_MaxLines > 0 && LineCount > pCursor->m_MaxLines)
break;
continue;
}
else
{
Character = ' ';
}
}
const SFontSizeChar *pChr = GetChar(TextContainer.m_pFont, pSizeData, Character);

View file

@ -12,10 +12,10 @@
enum
{
TEXTFLAG_RENDER = 1,
TEXTFLAG_ALLOW_NEWLINE = 2,
TEXTFLAG_STOP_AT_END = 4,
TEXTFLAG_ELLIPSIS_AT_END = 8,
TEXTFLAG_RENDER = 1 << 0,
TEXTFLAG_DISALLOW_NEWLINE = 1 << 1,
TEXTFLAG_STOP_AT_END = 1 << 2,
TEXTFLAG_ELLIPSIS_AT_END = 1 << 3,
};
enum ETextAlignment