shithub: choc

Download patch

ref: 16c56ea0c4476f4d908a4f8073cab48cdfe43005
parent: a8a5d2899a73f9830f69fe2c329d2a81e299def8
author: Simon Howard <fraggle@gmail.com>
date: Sun Oct 19 12:52:36 EDT 2014

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.

--- a/textscreen/txt_gui.c
+++ b/textscreen/txt_gui.c
@@ -322,6 +322,29 @@
     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;
--- a/textscreen/txt_gui.h
+++ b/textscreen/txt_gui.h
@@ -27,6 +27,7 @@
 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);
--- a/textscreen/txt_inputbox.c
+++ b/textscreen/txt_inputbox.c
@@ -237,10 +237,11 @@
 
     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);
     }