diff options
author | Darkvater <Darkvater@openttd.org> | 2007-03-05 00:45:56 +0000 |
---|---|---|
committer | Darkvater <Darkvater@openttd.org> | 2007-03-05 00:45:56 +0000 |
commit | 2ff94ab0004f6274a79ccf1f88090dd7d69073c6 (patch) | |
tree | 347173f94845f2ed93206df1d8a324c41c93e2bd /src/gfx.cpp | |
parent | ca4c8562474608ac15b79198230fd30b020de36c (diff) | |
download | openttd-2ff94ab0004f6274a79ccf1f88090dd7d69073c6.tar.xz |
(svn r9012) -Fix/Feature (UTF8): When cutting strings into multiple lines also take into consideration whitespace characters of more than 1 byte length (eg IDEOGRAPHIC SPACE, IsWhitespace() function). When trimming such strings, account for multiple-byte long sequences so use *Utf8PrevChar(v) = '\0'.
-Codechange: Add a function Utf8TrimString() that properly trims a string to an UTF8 encoding seperation instead of somewhere in the wild (and use it in the chat area)
Diffstat (limited to 'src/gfx.cpp')
-rw-r--r-- | src/gfx.cpp | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/src/gfx.cpp b/src/gfx.cpp index 4ed1ea424..2599344a5 100644 --- a/src/gfx.cpp +++ b/src/gfx.cpp @@ -296,7 +296,7 @@ static int TruncateString(char *str, int maxw) if (w >= maxw) { /* string got too big... insert dotdotdot */ ddd_pos[0] = ddd_pos[1] = ddd_pos[2] = '.'; - ddd_pos[3] = 0; + ddd_pos[3] = '\0'; return ddd_w; } } else { @@ -440,7 +440,7 @@ uint32 FormatStringLinebreaks(char *str, int maxw) for (;;) { WChar c = Utf8Consume((const char **)&str); /* whitespace is where we will insert the line-break */ - if (c == ' ') last_space = str; + if (IsWhitespace(c)) last_space = str; if (IsPrintable(c)) { w += GetCharacterWidth(size, c); @@ -451,7 +451,7 @@ uint32 FormatStringLinebreaks(char *str, int maxw) * 2. In all other cases force a linebreak at the last seen whitespace */ if (w > maxw) { if (last_space == NULL) { - str[-1] = '\0'; + *Utf8PrevChar(str) = '\0'; return num + (size << 16); } str = last_space; @@ -469,9 +469,17 @@ uint32 FormatStringLinebreaks(char *str, int maxw) } } end_of_inner_loop: - /* string didn't fit on line, so 'dummy' terminate and increase linecount */ + /* String didn't fit on line (or a '\n' was encountered), so 'dummy' terminate + * and increase linecount. We use Utf8PrevChar() as also non 1 char long + * whitespace seperators are supported */ num++; - str[-1] = '\0'; + char *s = Utf8PrevChar(str); + *s++ = '\0'; + + /* In which case (see above) we will shift remainder to left and close the gap */ + if (str - s >= 1) { + for (; str[-1] != '\0';) *s++ = *str++; + } } } |