Ticket #6772: utf32.txt

File utf32.txt, 3.3 KB (added by eriktorbjorn, 6 years ago)
Line 
1diff --git a/engines/zvision/text/truetype_font.cpp b/engines/zvision/text/truetype_font.cpp
2index 9f4fbe7..f7580fb 100644
3--- a/engines/zvision/text/truetype_font.cpp
4+++ b/engines/zvision/text/truetype_font.cpp
5@@ -26,6 +26,7 @@
6 #include "common/file.h"
7 #include "common/system.h"
8 #include "common/unzip.h"
9+#include "common/ustr.h"
10 #include "graphics/font.h"
11 #include "graphics/fonts/ttf.h"
12 #include "graphics/surface.h"
13@@ -168,6 +169,36 @@ int StyledTTFont::getKerningOffset(byte left, byte right) {
14 return 0;
15 }
16
17+Common::U32String StyledTTFont::convertUtf8ToUtf32(const Common::String &str) {
18+ // The String class, and therefore the Font class as well, assume one
19+ // character is one byte, but in this case it's actually an UTF-8
20+ // string with up to 4 bytes per character. To work around this,
21+ // convert it to an U32String before drawing it, because our Font class
22+ // can handle that.
23+ Common::U32String u32str;
24+ uint i = 0;
25+ while (i < str.size()) {
26+ uint32 chr = 0;
27+ if ((str[i] & 0xF8) == 0xF0) {
28+ chr |= (str[i++] & 0x07) << 18;
29+ chr |= (str[i++] & 0x3F) << 12;
30+ chr |= (str[i++] & 0x3F) << 6;
31+ chr |= (str[i++] & 0x3F);
32+ } else if ((str[i] & 0xF0) == 0xE0) {
33+ chr |= (str[i++] & 0x0F) << 12;
34+ chr |= (str[i++] & 0x3F) << 6;
35+ chr |= (str[i++] & 0x3F);
36+ } else if ((str[i] & 0xE0) == 0xC0) {
37+ chr |= (str[i++] & 0x1F) << 6;
38+ chr |= (str[i++] & 0x3F);
39+ } else {
40+ chr = (str[i++] & 0x7F);
41+ }
42+ u32str += chr;
43+ }
44+ return u32str;
45+}
46+
47 void StyledTTFont::drawChar(Graphics::Surface *dst, byte chr, int x, int y, uint32 color) {
48 if (_font) {
49 _font->drawChar(dst, chr, x, y, color);
50@@ -186,10 +217,11 @@ void StyledTTFont::drawChar(Graphics::Surface *dst, byte chr, int x, int y, uint
51
52 void StyledTTFont::drawString(Graphics::Surface *dst, const Common::String &str, int x, int y, int w, uint32 color, Graphics::TextAlign align) {
53 if (_font) {
54- _font->drawString(dst, str, x, y, w, color, align);
55+ Common::U32String u32str = convertUtf8ToUtf32(str);
56+ _font->drawString(dst, u32str, x, y, w, color, align);
57 if (_style & STTF_UNDERLINE) {
58 int16 pos = floor(_font->getFontHeight() * 0.87);
59- int16 wd = MIN(_font->getStringWidth(str), w);
60+ int16 wd = MIN(_font->getStringWidth(u32str), w);
61 int16 stX = x;
62 if (align == Graphics::kTextAlignCenter)
63 stX += (w - wd) / 2;
64@@ -202,7 +234,7 @@ void StyledTTFont::drawString(Graphics::Surface *dst, const Common::String &str,
65 }
66 if (_style & STTF_STRIKEOUT) {
67 int16 pos = floor(_font->getFontHeight() * 0.60);
68- int16 wd = MIN(_font->getStringWidth(str), w);
69+ int16 wd = MIN(_font->getStringWidth(u32str), w);
70 int16 stX = x;
71 if (align == Graphics::kTextAlignCenter)
72 stX += (w - wd) / 2;
73diff --git a/engines/zvision/text/truetype_font.h b/engines/zvision/text/truetype_font.h
74index 6fbb1f0..caa9c09 100644
75--- a/engines/zvision/text/truetype_font.h
76+++ b/engines/zvision/text/truetype_font.h
77@@ -77,6 +77,8 @@ public:
78 int getCharWidth(byte chr);
79 int getKerningOffset(byte left, byte right);
80
81+ Common::U32String convertUtf8ToUtf32(const Common::String &str);
82+
83 void drawChar(Graphics::Surface *dst, byte chr, int x, int y, uint32 color);
84
85 void drawString(Graphics::Surface *dst, const Common::String &str, int x, int y, int w, uint32 color, Graphics::TextAlign align = Graphics::kTextAlignLeft);