diff --git a/stepmania/src/BitmapText.cpp b/stepmania/src/BitmapText.cpp index 8eae919d82..1f1ceb6c42 100644 --- a/stepmania/src/BitmapText.cpp +++ b/stepmania/src/BitmapText.cpp @@ -22,93 +22,11 @@ #include "GameConstantsAndTypes.h" #include "Font.h" -char *g_utf8_find_next_char (const char *p, - const char *end) -{ - if (*p) { - if (end) - for (++p; p < end && (*p & 0xc0) == 0x80; ++p) - ; - else - for (++p; (*p & 0xc0) == 0x80; ++p) - ; - } - return (p == end) ? NULL : (char *)p; -} - -#define UTF8_GET \ - -wchar_t -g_utf8_get_char (const char *p) -{ - int i, mask = 0, len; - wchar_t result; - unsigned char c = (unsigned char) *p; - - if (c < 128) { - len = 1; - mask = 0x7f; - } else if ((c & 0xe0) == 0xc0) { - len = 2; - mask = 0x1f; - } else if ((c & 0xf0) == 0xe0) { - len = 3; - mask = 0x0f; - } else if ((c & 0xf8) == 0xf0) { - len = 4; - mask = 0x07; - } else if ((c & 0xfc) == 0xf8) { - len = 5; - mask = 0x03; - } else if ((c & 0xfe) == 0xfc) { - len = 6; - mask = 0x01; - } else - return (wchar_t)-1; - - result = wchar_t(p[0] & mask); - for (i = 1; i < len; ++i) { - if ((p[i] & 0xc0) != 0x80) - return (wchar_t) -1; - - result <<= 6; - result |= p[i] & 0x3f; - } - - return result; -} - -wstring CStringToWstring(const CString &str) -{ - const char *ptr = str.c_str(), *end = str.c_str()+str.size(); - - wstring ret; - - while(ptr) - { - wchar_t c = g_utf8_get_char (ptr); - if(c != wchar_t(-1)) - ret.push_back(c); - - ptr = g_utf8_find_next_char (ptr, end); - - } - - return ret; -} - - - - - - - #define RAINBOW_COLOR(n) THEME->GetMetricC("BitmapText",ssprintf("RainbowColor%i", n+1)) const int NUM_RAINBOW_COLORS = 7; RageColor RAINBOW_COLORS[NUM_RAINBOW_COLORS]; - BitmapText::BitmapText() { // Loading these theme metrics is slow, so only do it ever 20th time. diff --git a/stepmania/src/RageUtil.cpp b/stepmania/src/RageUtil.cpp index a0517f79df..3b48122cf3 100644 --- a/stepmania/src/RageUtil.cpp +++ b/stepmania/src/RageUtil.cpp @@ -854,3 +854,77 @@ bool regex(CString pattern, CString str) vector matches; return regex(pattern, str, matches); } + +/* UTF-8 decoding code from glib. */ +char *utf8_find_next_char (const char *p, const char *end) +{ + if (*p) { + if (end) + for (++p; p < end && (*p & 0xc0) == 0x80; ++p) + ; + else + for (++p; (*p & 0xc0) == 0x80; ++p) + ; + } + return (p == end) ? NULL : (char *)p; +} + +#define UTF8_GET \ + +wchar_t utf8_get_char (const char *p) +{ + int i, mask = 0, len; + wchar_t result; + unsigned char c = (unsigned char) *p; + + if (c < 128) { + len = 1; + mask = 0x7f; + } else if ((c & 0xe0) == 0xc0) { + len = 2; + mask = 0x1f; + } else if ((c & 0xf0) == 0xe0) { + len = 3; + mask = 0x0f; + } else if ((c & 0xf8) == 0xf0) { + len = 4; + mask = 0x07; + } else if ((c & 0xfc) == 0xf8) { + len = 5; + mask = 0x03; + } else if ((c & 0xfe) == 0xfc) { + len = 6; + mask = 0x01; + } else + return (wchar_t)-1; + + result = wchar_t(p[0] & mask); + for (i = 1; i < len; ++i) { + if ((p[i] & 0xc0) != 0x80) + return (wchar_t) -1; + + result <<= 6; + result |= p[i] & 0x3f; + } + + return result; +} + +wstring CStringToWstring(const CString &str) +{ + const char *ptr = str.c_str(), *end = str.c_str()+str.size(); + + wstring ret; + + while(ptr) + { + wchar_t c = utf8_get_char (ptr); + if(c != wchar_t(-1)) + ret.push_back(c); + + ptr = utf8_find_next_char (ptr, end); + + } + + return ret; +} diff --git a/stepmania/src/RageUtil.h b/stepmania/src/RageUtil.h index 47d786b43c..c9715faa2c 100644 --- a/stepmania/src/RageUtil.h +++ b/stepmania/src/RageUtil.h @@ -197,6 +197,8 @@ bool regex(CString str, CString pattern, vector &matches); bool regex(CString str, CString pattern); void regex_flags(int flags); +wstring CStringToWstring(const CString &str); + #ifndef WIN32 #include /* correct place with correct definitions */ #endif