From 7647b4774c72a0803b36d89ece4edd95f5dbb97f Mon Sep 17 00:00:00 2001
From: patatahooligan <filwn.oikonomou@gmail.com>
Date: Thu, 20 Oct 2022 07:23:15 +0300
Subject: [PATCH] Fix unicode character printing (#112)

* Fix unicode character printing

Fix several issues that prevented it from working

  * link to the wide-character version of ncurses
  * define NCURSES_WIDECHAR
  * use a function that can print wide characters
  * fix the character range. I don't know what the original was supposed
    to be, but half-width kana (which is what the movie reportedly used)
    was not at that range.

* Fix half-width kana character range

I'm not very familiar with katakana, but I think the extra characters I
was using are punctuation or other marks. So let's remove them. Note
that this is the same range space-pagan is using.
---
 CMakeLists.txt |  1 +
 cmatrix.c      | 21 +++++++++++++++++----
 2 files changed, 18 insertions(+), 4 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6c50d7b..68ee176 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -50,6 +50,7 @@ if	(HAVE_GETOPT_H)
 endif	()
 
 Set(CURSES_NEED_NCURSES TRUE)
+Set(CURSES_NEED_WIDE TRUE)
 find_package(Curses)
 include_directories(${CURSES_INCLUDE_DIR})
 add_definitions(-DHAVE_NCURSES_H)
diff --git a/cmatrix.c b/cmatrix.c
index 7e3fbb9..7d8ca39 100644
--- a/cmatrix.c
+++ b/cmatrix.c
@@ -21,6 +21,8 @@
 
 */
 
+#define NCURSES_WIDECHAR 1
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdarg.h>
@@ -521,9 +523,12 @@ if (console) {
 
     /* Set up values for random number generation */
     if(classic) {
-        /* Japanese character unicode range [they are seen in the original cmatrix] */
-        randmin = 12288;
-        highnum = 12351;
+        /* Half-width kana characters. In the movie they are y-axis flipped, and
+         * they appear alongside latin characters and numerals, but this is the
+         * closest we can do with a standard unicode set and a single number
+         * range */
+        randmin = 0xff66;
+        highnum = 0xff9d;
     } else if (console || xwindow) {
         randmin = 166;
         highnum = 217;
@@ -834,7 +839,15 @@ if (console) {
                         } else if (lambda && matrix[i][j].val != ' ') {
                             addstr("λ");
                         } else {
-                            addch(matrix[i][j].val);
+                            /* addch doesn't seem to work with unicode
+                             * characters and there was no direct equivalent.
+                             * So, construct a c-style string with the character
+                             * and print that.
+                             */
+                            wchar_t char_array[2];
+                            char_array[0] = matrix[i][j].val;
+                            char_array[1] = 0;
+                            addwstr(char_array);
                         }
                         if (bold == 2 ||
                             (bold == 1 && matrix[i][j].val % 2 == 0)) {
