foxygit / doom Log in
commit 16c56ea0c4476f4d908a4f8073cab48cdfe43005
Author:     Simon Howard <fraggle@gmail.com>
AuthorDate: Sun Oct 19 16:52:36 2014 -0400
Commit:     Simon Howard <fraggle@gmail.com>
CommitDate: Sun Oct 19 16:52:36 2014 -0400

    textscreen: Don't allow input of unprintable characters.

    If a Unicode character is not part of the Extended ASCII set that
    we can display on screen, don't allow it to be typed (which would
    display a backwards question mark). Thanks Alexandre-Xavier for this
    suggestion on #229.
---
 textscreen/txt_gui.c      | 23 +++++++++++++++++++++++
 textscreen/txt_gui.h      |  1 +
 textscreen/txt_inputbox.c |  7 ++++---
 3 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/textscreen/txt_gui.c b/textscreen/txt_gui.c
index 1a5275b6..74e3ec75 100644
--- a/textscreen/txt_gui.c
+++ b/textscreen/txt_gui.c
@@ -322,6 +322,29 @@ static void PutUnicodeChar(unsigned int c)
     TXT_PutChar('\xa8');
 }

+int TXT_CanDrawCharacter(unsigned int c)
+{
+    unsigned int i;
+
+    // Standard ASCII range?
+    if (c < 128)
+    {
+        return 1;
+    }
+
+    // Extended ASCII range?
+    for (i = 0; i < 128; ++i)
+    {
+        if (cp437_unicode[i] == c)
+        {
+            return 1;
+        }
+    }
+
+    // Nope.
+    return 0;
+}
+
 void TXT_DrawUTF8String(const char *s)
 {
     int x, y;
diff --git a/textscreen/txt_gui.h b/textscreen/txt_gui.h
index bb41b23d..f745ddd2 100644
--- a/textscreen/txt_gui.h
+++ b/textscreen/txt_gui.h
@@ -27,6 +27,7 @@ void TXT_DrawWindowFrame(const char *title, int x, int y, int w, int h);
 void TXT_DrawSeparator(int x, int y, int w);
 void TXT_DrawString(const char *s);
 void TXT_DrawUTF8String(const char *s);
+int TXT_CanDrawCharacter(unsigned int c);

 void TXT_DrawHorizScrollbar(int x, int y, int w, int cursor, int range);
 void TXT_DrawVertScrollbar(int x, int y, int h, int cursor, int range);
diff --git a/textscreen/txt_inputbox.c b/textscreen/txt_inputbox.c
index b2acaa8b..60512845 100644
--- a/textscreen/txt_inputbox.c
+++ b/textscreen/txt_inputbox.c
@@ -237,10 +237,11 @@ static int TXT_InputBoxKeyPress(TXT_UNCAST_ARG(inputbox), int key)

     c = TXT_KEY_TO_UNICODE(key);

-    if (c >= 128 || isprint(c))
+    // Add character to the buffer, but only if it's a printable character
+    // that we can represent on the screen.
+    if (isprint(c)
+     || (c >= 128 && TXT_CanDrawCharacter(c)))
     {
-        // Add character to the buffer
-
         AddCharacter(inputbox, c);
     }