Add some support for UTF-8
authorBill Lash <william.lash@gmail.com>
Fri, 1 Feb 2019 05:34:31 +0000 (23:34 -0600)
committerBill Lash <william.lash@gmail.com>
Fri, 1 Feb 2019 05:34:31 +0000 (23:34 -0600)
This adds support for printing UTF-8 characters using ncursesw in
place of ncurses.

Makefile
src/curses/ux_init.c
src/curses/ux_text.c

index ea7edab68ae271fb165e8263d57d61e01b0686bc..b908f657027fd3e38e2326483b7968a45f724318 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -66,7 +66,8 @@ DEFAULT_CONVERTER ?= SRC_SINC_MEDIUM_QUALITY
 COLOR ?= yes
 
 # If this matters, you can choose -lcurses or -lncurses
-CURSES ?= -lncurses
+# For UTF-8 support -lncursesw needs to be used
+CURSES ?= -lncursesw
 
 # Uncomment this if you're compiling Unix Frotz on a machine that lacks
 # the strrchr() libc library call.  If you don't know what this means,
index 19c72fce29757ca0d17e5230aca5f7dbe90d849a..b529f2a73506470ee3c58685e5e9f72b0f73018e 100644 (file)
@@ -45,6 +45,8 @@
 #include "ux_frotz.h"
 #include "ux_blorb.h"
 
+#include <locale.h>
+
 volatile sig_atomic_t terminal_resized = 0;
 
 static void sigwinch_handler(int);
@@ -406,6 +408,8 @@ void os_init_screen (void)
 {
     /*trace(TRACE_CALLS);*/
 
+    setlocale(LC_ALL, "");
+
     if (initscr() == NULL) {    /* Set up curses */
        os_fatal("Unable to initialize curses. Maybe your $TERM setting is bad.");
        exit(1);
index 95228b320cbe74760a47181ecc957a4697ee6180..694ca8359c7d6dabb4bb96112438461164e6678d 100644 (file)
@@ -218,8 +218,20 @@ void os_display_char (zchar c)
          if (c3 != ' ')
            addch(c3);
 
-       } else
-         addch(c);
+       } else {
+         // Looking at the UTF-8 table at
+         // https://www.utf8-chartable.de/unicode-utf8-table.pl
+         // Shows that characters from 0xa0-0xbf are encoded as
+         // 0xc2 0xa0-0xbf, and characters from 0xc0-0xff are
+         // encoded as 0xc3 0x80-0xbf
+         if ( c < 0xc0) {
+           addch(0xc2);
+           addch(c);
+         } else {
+           addch(0xc3);
+           addch(c - 0x40);
+         }
+       }
        return;
     }
     if (c >= ZC_ASCII_MIN && c <= ZC_ASCII_MAX) {