utf-8 support is working

This commit is contained in:
Glenn Maynard
2003-01-05 05:13:45 +00:00
parent 28a4abd876
commit 16052ecd3d
6 changed files with 126 additions and 37 deletions
+84 -2
View File
@@ -22,6 +22,86 @@
#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))
@@ -118,7 +198,7 @@ void BitmapText::BuildChars()
for( i=0; i<m_szTextLines.size(); i++ ) // foreach line
{
const CString &szLine = m_szTextLines[i];
const wstring &szLine = m_szTextLines[i];
const int iLineWidth = m_iLineWidths[i];
int iX;
@@ -172,6 +252,8 @@ void BitmapText::DrawChars()
}
}
/* sText is UTF-8. */
void BitmapText::SetText( CString sText )
{
ASSERT( m_pFont );
@@ -185,7 +267,7 @@ void BitmapText::SetText( CString sText )
m_iLineWidths.clear();
m_iLineHeights.clear();
split(m_szText, "\n", m_szTextLines, false);
split(CStringToWstring(sText), L"\n", m_szTextLines, false);
/* calculate line lengths and widths */
m_iWidestLineWidth = 0;