(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)
This commit is contained in:
18
src/gfx.cpp
18
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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user