From 8ba4dc56567f8d3bea53c7252a9b2d70a5ce9f1c Mon Sep 17 00:00:00 2001 From: borg323 <4010067-borg323@users.noreply.gitlab.com> Date: Mon, 10 Jun 2019 19:37:40 +0300 Subject: [PATCH] dumb: allow utf8 output --- src/dumb/dumb_frotz.h | 2 +- src/dumb/dumb_output.c | 53 +++++++++++++++++++++++++++++++----------- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/src/dumb/dumb_frotz.h b/src/dumb/dumb_frotz.h index fb46efd..4da1d80 100644 --- a/src/dumb/dumb_frotz.h +++ b/src/dumb/dumb_frotz.h @@ -38,7 +38,7 @@ void dumb_dump_screen(void); void dumb_display_user_input(char *); void dumb_discard_old_input(int num_chars); void dumb_elide_more_prompt(void); -void dumb_set_picture_cell(int row, int col, char c); +void dumb_set_picture_cell(int row, int col, zchar c); /* dumb-pic.c */ void dumb_init_pictures(char *graphics_filename); diff --git a/src/dumb/dumb_output.c b/src/dumb/dumb_output.c index 7fdb357..09bbdd2 100644 --- a/src/dumb/dumb_output.c +++ b/src/dumb/dumb_output.c @@ -42,13 +42,17 @@ static char latin1_to_ascii[] = static int screen_cells; /* The in-memory state of the screen. */ -/* Each cell contains a style in the upper byte and a char in the lower. */ +/* Each cell contains a style in the lower byte and a zchar above. */ +#ifdef USE_UTF8 +typedef unsigned int cell; +#else typedef unsigned short cell; +#endif static cell *screen_data; -static cell make_cell(int style, char c) {return (style << 8) | (0xff & c);} -static char cell_char(cell c) {return c & 0xff;} -static int cell_style(cell c) {return c >> 8;} +static cell make_cell(int style, zchar c) {return (c << 8) | (0xff & style);} +static zchar cell_char(cell c) {return c >> 8;} +static int cell_style(cell c) {return c & 0xff;} /* A cell's style is REVERSE_STYLE, normal (0), or PICTURE_STYLE. @@ -130,7 +134,7 @@ static void dumb_set_cell(int row, int col, cell c) dumb_row(row)[col] = c; } -void dumb_set_picture_cell(int row, int col, char c) +void dumb_set_picture_cell(int row, int col, zchar c) { dumb_set_cell(row, col, make_cell(PICTURE_STYLE, c)); } @@ -150,7 +154,7 @@ void os_set_text_style(int x) } /* put a character in the cell at the cursor and advance the cursor. */ -static void dumb_display_char(char c) +static void dumb_display_char(zchar c) { dumb_set_cell(cursor_row, cursor_col, make_cell(current_style, c)); if (++cursor_col == h_screen_cols) { @@ -262,26 +266,49 @@ int os_font_data(int font, int *height, int *width) void os_set_colour (int UNUSED (x), int UNUSED (y)) {} void os_set_font (int UNUSED (x)) {} +#ifdef USE_UTF8 +void zputchar(zchar c) +{ + if(c > 0x7ff) { + putchar(0xe0 | ((c >> 12) & 0xf)); + putchar(0x80 | ((c >> 6) & 0x3f)); + putchar(0x80 | (c & 0x3f)); + } else if(c > 0x7f) { + putchar(0xc0 | ((c >> 6) & 0x1f)); + putchar(0x80 | (c & 0x3f)); + } else { + putchar(c); + } +} +#else +#define zputchar(x) putchar(x) +#endif + /* Print a cell to stdout. */ static void show_cell(cell cel) { - char c = cell_char(cel); + zchar c = cell_char(cel); switch (cell_style(cel)) { case 0: - putchar(c); + zputchar(c); break; case PICTURE_STYLE: - putchar(show_pictures ? c : ' '); + zputchar(show_pictures ? c : ' '); break; case REVERSE_STYLE: if (c == ' ') putchar(rv_blank_char); else switch (rv_mode) { - case RV_NONE: putchar(c); break; - case RV_CAPS: putchar(toupper(c)); break; - case RV_UNDERLINE: putchar('_'); putchar('\b'); putchar(c); break; - case RV_DOUBLESTRIKE: putchar(c); putchar('\b'); putchar(c); break; + case RV_CAPS: + if (c <= 0x7f ) + { + zputchar(toupper(c)); + break; + } + case RV_NONE: zputchar(c); break; + case RV_UNDERLINE: putchar('_'); putchar('\b'); zputchar(c); break; + case RV_DOUBLESTRIKE: zputchar(c); putchar('\b'); zputchar(c); break; } break; } -- 2.34.1