LAB: Improve whitespace handling in flowText

flowText was handling presence/absence of whitespace after lines
inconsistently. This caused end-of-string to be missed, which broke
last-page detection in the journal. It also introduced extra spaces at the
beginning of pages.
This commit is contained in:
Willem Jan Palenstijn 2015-12-24 15:23:20 +01:00
parent 6ad6632070
commit a08dd694e5

View file

@ -120,26 +120,29 @@ Common::String DisplayMan::getWord(const char *mainBuffer) {
Common::String DisplayMan::getLine(TextFont *tf, const char **mainBuffer, uint16 lineWidth) { Common::String DisplayMan::getLine(TextFont *tf, const char **mainBuffer, uint16 lineWidth) {
uint16 curWidth = 0; uint16 curWidth = 0;
Common::String result; Common::String result;
bool doit = true;
lineWidth += textLength(tf, " "); while ((*mainBuffer)[0]) {
Common::String wordBuffer = getWord(*mainBuffer);
while ((*mainBuffer)[0] && doit) {
Common::String wordBuffer = getWord(*mainBuffer) + " ";
if ((curWidth + textLength(tf, wordBuffer)) <= lineWidth) { if ((curWidth + textLength(tf, wordBuffer)) <= lineWidth) {
result += wordBuffer; result += wordBuffer;
(*mainBuffer) += wordBuffer.size() - 1; (*mainBuffer) += wordBuffer.size();
if ((*mainBuffer)[0] == '\n') // end of line
doit = false; if ((*mainBuffer)[0] == '\n') {
if ((*mainBuffer)[0])
(*mainBuffer)++; (*mainBuffer)++;
break;
}
// append any space after the word
if ((*mainBuffer)[0]) {
result += (*mainBuffer)[0];
(*mainBuffer)++;
}
curWidth = textLength(tf, result); curWidth = textLength(tf, result);
} else } else
doit = false; break;
} }
return result; return result;
@ -161,19 +164,20 @@ int DisplayMan::flowText(TextFont *font, int16 spacing, byte penColor, byte back
if (!str) if (!str)
return 0; return 0;
const char *orig = str;
TextFont *msgFont = font; TextFont *msgFont = font;
uint16 fontHeight = textHeight(msgFont) + spacing; uint16 fontHeight = textHeight(msgFont) + spacing;
uint16 numLines = (textRect.height() + 1) / fontHeight; uint16 numLines = (textRect.height() + 1) / fontHeight;
uint16 width = textRect.width() + 1; uint16 width = textRect.width() + 1;
uint16 y = textRect.top; uint16 y = textRect.top;
Common::String lineBuffer;
if (centerv && output) { if (centerv && output) {
const char *temp = str; const char *temp = str;
uint16 actlines = 0; uint16 actlines = 0;
while (temp[0]) { while (temp[0]) {
lineBuffer = getLine(msgFont, &temp, width); getLine(msgFont, &temp, width);
actlines++; actlines++;
} }
@ -183,6 +187,7 @@ int DisplayMan::flowText(TextFont *font, int16 spacing, byte penColor, byte back
int len = 0; int len = 0;
while (numLines && str[0]) { while (numLines && str[0]) {
Common::String lineBuffer;
lineBuffer = getLine(msgFont, &str, width); lineBuffer = getLine(msgFont, &str, width);
uint16 x = textRect.left; uint16 x = textRect.left;
@ -198,11 +203,9 @@ int DisplayMan::flowText(TextFont *font, int16 spacing, byte penColor, byte back
y += fontHeight; y += fontHeight;
} }
len--;
_currentDisplayBuffer = saveDisplayBuffer; _currentDisplayBuffer = saveDisplayBuffer;
return len; return (str - orig);
} }
void DisplayMan::createBox(uint16 y2) { void DisplayMan::createBox(uint16 y2) {