Codechange: simplify news string drawing by using StrMakeValid to replaces newlines with spaces

This commit is contained in:
Rubidium 2023-05-19 14:22:50 +02:00 committed by rubidium42
parent 9610705f46
commit 00695c29de
2 changed files with 7 additions and 52 deletions

View File

@ -1099,37 +1099,13 @@ void ShowLastNewsMessage()
*/
static void DrawNewsString(uint left, uint right, int y, TextColour colour, const NewsItem *ni)
{
char buffer[512], buffer2[512];
StringID str;
CopyInDParam(0, ni->params, lengthof(ni->params));
str = ni->string_id;
GetString(buffer, str, lastof(buffer));
/* Copy the just gotten string to another buffer to remove any formatting
* from it such as big fonts, etc. */
const char *ptr = buffer;
char *dest = buffer2;
WChar c_last = '\0';
for (;;) {
WChar c = Utf8Consume(&ptr);
if (c == 0) break;
/* Make a space from a newline, but ignore multiple newlines */
if (c == '\n' && c_last != '\n') {
dest[0] = ' ';
dest++;
} else if (c == '\r') {
dest[0] = dest[1] = dest[2] = dest[3] = ' ';
dest += 4;
} else if (IsPrintable(c)) {
dest += Utf8Encode(dest, c);
}
c_last = c;
}
/* Get the string, replaces newlines with spaces and remove control codes from the string. */
std::string message = StrMakeValid(GetString(ni->string_id), SVS_REPLACE_TAB_CR_NL_WITH_SPACE);
*dest = '\0';
/* Truncate and show string; postfixed by '...' if necessary */
DrawString(left, right, y, buffer2, colour);
DrawString(left, right, y, message, colour);
}
struct MessageHistoryWindow : Window {

View File

@ -40,39 +40,18 @@
static bool DrawScrollingStatusText(const NewsItem *ni, int scroll_pos, int left, int right, int top, int bottom)
{
CopyInDParam(0, ni->params, lengthof(ni->params));
StringID str = ni->string_id;
char buf[512];
GetString(buf, str, lastof(buf));
const char *s = buf;
char buffer[256];
char *d = buffer;
const char *last = lastof(buffer);
for (;;) {
WChar c = Utf8Consume(&s);
if (c == 0) {
break;
} else if (c == '\n') {
if (d + 4 >= last) break;
d[0] = d[1] = d[2] = d[3] = ' ';
d += 4;
} else if (IsPrintable(c)) {
if (d + Utf8CharLen(c) >= last) break;
d += Utf8Encode(d, c);
}
}
*d = '\0';
/* Replace newlines and the likes with spaces. */
std::string message = StrMakeValid(GetString(ni->string_id), SVS_REPLACE_TAB_CR_NL_WITH_SPACE);
DrawPixelInfo tmp_dpi;
if (!FillDrawPixelInfo(&tmp_dpi, left, top, right - left, bottom)) return true;
int width = GetStringBoundingBox(buffer).width;
int width = GetStringBoundingBox(message).width;
int pos = (_current_text_dir == TD_RTL) ? (scroll_pos - width) : (right - scroll_pos - left);
AutoRestoreBackup dpi_backup(_cur_dpi, &tmp_dpi);
DrawString(pos, INT16_MAX, 0, buffer, TC_LIGHT_BLUE, SA_LEFT | SA_FORCE);
DrawString(pos, INT16_MAX, 0, message, TC_LIGHT_BLUE, SA_LEFT | SA_FORCE);
return (_current_text_dir == TD_RTL) ? (pos < right - left) : (pos + width > 0);
}