summaryrefslogtreecommitdiff
path: root/src/gfx.cpp
diff options
context:
space:
mode:
authorDarkvater <darkvater@openttd.org>2007-03-05 00:45:56 +0000
committerDarkvater <darkvater@openttd.org>2007-03-05 00:45:56 +0000
commit915ae8ffc28aee2e0bb9d9c623a711b4fc1f7faa (patch)
tree347173f94845f2ed93206df1d8a324c41c93e2bd /src/gfx.cpp
parentaea64adbb9ff755be303515bebb428d92e7bcc92 (diff)
downloadopenttd-915ae8ffc28aee2e0bb9d9c623a711b4fc1f7faa.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.cpp18
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++;
+ }
}
}