aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common/ustr.cpp33
-rw-r--r--common/ustr.h5
2 files changed, 38 insertions, 0 deletions
diff --git a/common/ustr.cpp b/common/ustr.cpp
index 85946cda46..cddfda7201 100644
--- a/common/ustr.cpp
+++ b/common/ustr.cpp
@@ -327,4 +327,37 @@ void U32String::initWithCStr(const value_type *str, uint32 len) {
_str[len] = 0;
}
+// This is a quick and dirty converter.
+//
+// More comprehensive one lives in wintermute/utils/convert_utf.cpp
+U32String convertUtf8ToUtf32(const String &str) {
+ // The String class, and therefore the Font class as well, assume one
+ // character is one byte, but in this case it's actually an UTF-8
+ // string with up to 4 bytes per character. To work around this,
+ // convert it to an U32String before drawing it, because our Font class
+ // can handle that.
+ Common::U32String u32str;
+ uint i = 0;
+ while (i < str.size()) {
+ uint32 chr = 0;
+ if ((str[i] & 0xF8) == 0xF0) {
+ chr |= (str[i++] & 0x07) << 18;
+ chr |= (str[i++] & 0x3F) << 12;
+ chr |= (str[i++] & 0x3F) << 6;
+ chr |= (str[i++] & 0x3F);
+ } else if ((str[i] & 0xF0) == 0xE0) {
+ chr |= (str[i++] & 0x0F) << 12;
+ chr |= (str[i++] & 0x3F) << 6;
+ chr |= (str[i++] & 0x3F);
+ } else if ((str[i] & 0xE0) == 0xC0) {
+ chr |= (str[i++] & 0x1F) << 6;
+ chr |= (str[i++] & 0x3F);
+ } else {
+ chr = (str[i++] & 0x7F);
+ }
+ u32str += chr;
+ }
+ return u32str;
+}
+
} // End of namespace Common
diff --git a/common/ustr.h b/common/ustr.h
index 0059a1eb96..5be3c8b6e7 100644
--- a/common/ustr.h
+++ b/common/ustr.h
@@ -27,6 +27,8 @@
namespace Common {
+class String;
+
/**
* Very simple string class for UTF-32 strings in ScummVM. The main intention
* behind this class is to feature a simple way of displaying UTF-32 strings
@@ -182,6 +184,7 @@ public:
const_iterator end() const {
return begin() + size();
}
+
private:
void makeUnique();
void ensureCapacity(uint32 new_size, bool keep_old);
@@ -190,6 +193,8 @@ private:
void initWithCStr(const value_type *str, uint32 len);
};
+U32String convertUtf8ToUtf32(const String &str);
+
} // End of namespace Common
#endif