demo/FTGLDemo.cpp, demo/FTGLMFontDemo.cpp: support Latin1 key strokes (fixes #15)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
diff --git a/demo/FTGLDemo.cpp b/demo/FTGLDemo.cpp
index 01f9049..49e550c 100644
--- a/demo/FTGLDemo.cpp
+++ b/demo/FTGLDemo.cpp
@@ -528,6 +528,31 @@ void myinit(const char* file)
}
+/*
+ * Simple conversion from Unicode character to UTF-8 string.
+ * (In real code, you may want want to use a library such as ICU
+ * or newer C++ features instead, but this avoids additional dependencies.
+ * s must contain enough space for up to 4 bytes.
+ * Returns number of bytes written.
+ */
+int ToUTF8 (unsigned int c, char *s)
+{
+ char *t = s;
+ int n = 0;
+ if (c < 0x80)
+ *s++ = c;
+ else if (c < 0x800)
+ *s++ = c >> (n = 6) | 0xc0;
+ else if (c < 0x10000)
+ *s++ = c >> (n = 12) | 0xe0;
+ else
+ *s++ = c >> (n = 18) | 0xf0;
+ while (n)
+ *s++ = (c >> (n -= 6) & 0x3f) | 0x80;
+ return s - t;
+}
+
+
void parsekey(unsigned char key, int, int)
{
switch (key)
@@ -573,14 +598,13 @@ void parsekey(unsigned char key, int, int)
default:
if(mode == INTERACTIVE)
{
- myString[0] = key;
- myString[1] = 0;
+ myString[ToUTF8 (key, myString)] = 0;
}
else
{
- myString[carat] = key;
- myString[carat + 1] = 0;
- carat = carat > 2000 ? 2000 : carat + 1;
+ int r = ToUTF8 (key, myString + carat) + carat;
+ myString[r] = 0;
+ carat = r > 2000 ? 2000 : r;
}
break;
}
diff --git a/demo/FTGLMFontDemo.cpp b/demo/FTGLMFontDemo.cpp
index 196b54c..f5bdb82 100644
--- a/demo/FTGLMFontDemo.cpp
+++ b/demo/FTGLMFontDemo.cpp
@@ -484,6 +484,31 @@ void myinit(int numFontFiles)
}
+/*
+ * Simple conversion from Unicode character to UTF-8 string.
+ * (In real code, you may want want to use a library such as ICU
+ * or newer C++ features instead, but this avoids additional dependencies.
+ * s must contain enough space for up to 4 bytes.
+ * Returns number of bytes written.
+ */
+int ToUTF8 (unsigned int c, char *s)
+{
+ char *t = s;
+ int n = 0;
+ if (c < 0x80)
+ *s++ = c;
+ else if (c < 0x800)
+ *s++ = c >> (n = 6) | 0xc0;
+ else if (c < 0x10000)
+ *s++ = c >> (n = 12) | 0xe0;
+ else
+ *s++ = c >> (n = 18) | 0xf0;
+ while (n)
+ *s++ = (c >> (n -= 6) & 0x3f) | 0x80;
+ return s - t;
+}
+
+
void parsekey(unsigned char key, int, int)
{
switch (key)
@@ -529,14 +554,13 @@ void parsekey(unsigned char key, int, int)
default:
if(mode == INTERACTIVE)
{
- myString[0] = key;
- myString[1] = 0;
+ myString[ToUTF8 (key, myString)] = 0;
}
else
{
- myString[carat] = key;
- myString[carat + 1] = 0;
- carat = carat > 2000 ? 2000 : carat + 1;
+ int r = ToUTF8 (key, myString + carat) + carat;
+ myString[r] = 0;
+ carat = r > 2000 ? 2000 : r;
}
break;
}