From 4bf0c740be3ae43c44bd3452c93da7162aba5bfa Mon Sep 17 00:00:00 2001 From: "Marc H. Thoben" Date: Sat, 20 Feb 2010 17:47:29 +0100 Subject: [PATCH] Add Stone Soup specific character stripping. * Adjusted glyphs tables * Removed DEC and added UNICODE (cherry picked from commit 3927855ca3d803b89b40debd2fd89266482b34e5) --- stripgfx.c | 402 +++++++++++------------------------------------------ stripgfx.h | 8 +- ttyplay.c | 12 +- 3 files changed, 92 insertions(+), 330 deletions(-) diff --git a/stripgfx.c b/stripgfx.c index c33fbfc..ad4b0a4 100644 --- a/stripgfx.c +++ b/stripgfx.c @@ -10,333 +10,47 @@ unsigned char gfx_map[256]; unsigned int state = 0; +extern int stripped; + static unsigned char no_graphics[MAXPCHARS] = { - ' ', /* 0 */ - /* stone */ - '|', /* vwall */ - '-', /* hwall */ - '-', /* tlcorn */ - '-', /* trcorn */ - '-', /* blcorn */ - '-', /* brcorn */ - '-', /* crwall */ - '-', /* tuwall */ - '-', /* tdwall */ - '|', /* 10 */ - /* tlwall */ - '|', /* trwall */ - '.', /* ndoor */ - '-', /* vodoor */ - '|', /* hodoor */ - '+', /* vcdoor */ - '+', /* hcdoor */ - '#', /* bars */ - '#', /* tree */ - '.', /* room */ - '#', /* 20 */ - /* dark corr */ - '#', /* lit corr */ - '<', /* upstair */ - '>', /* dnstair */ - '<', /* upladder */ - '>', /* dnladder */ - '_', /* altar */ - '|', /* grave */ - '\\', /* throne */ - '#', /* sink */ - '{', /* 30 */ - /* fountain */ - '}', /* pool */ - '.', /* ice */ - '}', /* lava */ - '.', /* vodbridge */ - '.', /* hodbridge */ - '#', /* vcdbridge */ - '#', /* hcdbridge */ - ' ', /* open air */ - '#', /* [part of] a cloud */ - '}', /* 40 */ - /* under water */ - '^', /* trap */ - '^', /* trap */ - '^', /* trap */ - '^', /* trap */ - '^', /* trap */ - '^', /* trap */ - '^', /* trap */ - '^', /* trap */ - '^', /* trap */ - '^', /* 50 */ - /* trap */ - '^', /* trap */ - '^', /* trap */ - '^', /* trap */ - '^', /* trap */ - '^', /* trap */ - '^', /* trap */ - '^', /* trap */ - '"', /* web */ - '^', /* trap */ - '^', /* 60 */ - /* trap */ - '^', /* trap */ - '^', /* trap */ - '|', /* vbeam */ - '-', /* hbeam */ - '\\', /* lslant */ - '/', /* rslant */ - '*', /* dig beam */ - '!', /* camera flash beam */ - ')', /* boomerang open left */ - '(', /* 70 */ - /* boomerang open right */ - '0', /* 4 magic shield symbols */ - '#', - '@', - '*', - '/', /* swallow top left */ - '-', /* swallow top center */ - '\\', /* swallow top right */ - '|', /* swallow middle left */ - '|', /* swallow middle right */ - '\\', /* 80 */ - /* swallow bottom left */ - '-', /* swallow bottom center */ - '/', /* swallow bottom right */ - '/', /* explosion top left */ - '-', /* explosion top center */ - '\\', /* explosion top right */ - '|', /* explosion middle left */ - ' ', /* explosion middle center */ - '|', /* explosion middle right */ - '\\', /* explosion bottom left */ - '-', /* 90 */ - /* explosion bottom center */ - '/' /* explosion bottom right */ + '#', '*', '.', ',', '\'', '+', '^', '>', '<', // wall, stairs up + '_', '\\', '}', '{', '8', '~', '~', // altar, item detect + '0', ')', '[', '/', '%', '?', '=', '!', '(', // orb, missile + ':', '|', '}', '%', '$', '"', '#', // book, cloud }; static unsigned char ibm_graphics[MAXPCHARS] = { -/* 0*/ 0x00, - 0xb3, /* : meta-3, vertical rule */ - 0xc4, /* : meta-D, horizontal rule */ - 0xda, /* : meta-Z, top left corner */ - 0xbf, /* : meta-?, top right corner */ - 0xc0, /* : meta-@, bottom left */ - 0xd9, /* : meta-Y, bottom right */ - 0xc5, /* : meta-E, cross */ - 0xc1, /* : meta-A, T up */ - 0xc2, /* : meta-B, T down */ - /*10 */ 0xb4, - /* : meta-4, T left */ - 0xc3, /* : meta-C, T right */ - 0xfa, /* : meta-z, centered dot */ - 0xfe, /* : meta-~, small centered square */ - 0xfe, /* : meta-~, small centered square */ - 0x00, - 0x00, - 240, /* : equivalence symbol */ - 241, /* : plus or minus symbol */ - 0xfa, /* : meta-z, centered dot */ - /*20 */ 0xb0, - /* : meta-0, light shading */ - 0xb1, /* : meta-1, medium shading */ - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - /*30 */ 0xf4, - /* : meta-t, integral top half */ - 0xf7, /* : meta-w, approx. equals */ - 0xfa, /* : meta-z, centered dot */ - 0xf7, /* : meta-w, approx. equals */ - 0xfa, /* : meta-z, centered dot */ - 0xfa, /* : meta-z, centered dot */ - 0x00, - 0x00, - 0x00, - 0x00, - /*40 */ 0xf7, - /* : meta-w, approx. equals */ - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, -/*50*/ 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, -/*60*/ 0x00, - 0x00, - 0x00, - 0xb3, /* : meta-3, vertical rule */ - 0xc4, /* : meta-D, horizontal rule */ - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, -/*70*/ 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0xb3, /* : meta-3, vertical rule */ - 0xb3, /* : meta-3, vertical rule */ -/*80*/ 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0xb3, /* : meta-3, vertical rule */ - 0x00, - 0xb3, /* : meta-3, vertical rule */ - 0x00, -/*90*/ 0x00, - 0x00 + 177, 176, 249, 250, 0, 254, 0, 0, 0, + 220, 239, 244, 247, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, }; static unsigned char dec_graphics[MAXPCHARS] = { -/* 0*/ 0x00, - 0xf8, /* : meta-x, vertical rule */ - 0xf1, /* : meta-q, horizontal rule */ - 0xec, /* : meta-l, top left corner */ - 0xeb, /* : meta-k, top right corner */ - 0xed, /* : meta-m, bottom left */ - 0xea, /* : meta-j, bottom right */ - 0xee, /* : meta-n, cross */ - 0xf6, /* : meta-v, T up */ - 0xf7, /* : meta-w, T down */ - /*10 */ 0xf5, - /* : meta-u, T left */ - 0xf4, /* : meta-t, T right */ - 0xfe, /* : meta-~, centered dot */ - 0xe1, /* : meta-a, solid block */ - 0xe1, /* : meta-a, solid block */ - 0x00, - 0x00, - 0xfb, /* : meta-{, small pi */ - 0xe7, /* : meta-g, plus-or-minus */ - 0xfe, /* : meta-~, centered dot */ -/*20*/ 0x00, - 0x00, - 0x00, - 0x00, - 0xf9, /* : meta-y, greater-than-or-equals */ - 0xfa, /* : meta-z, less-than-or-equals */ - 0x00, /* 0xc3, \E)3: meta-C, dagger */ - 0x00, - 0x00, - 0x00, - /*30 */ 0x00, - /* 0xdb, \E)3: meta-[, integral top half */ - 0xe0, /* : meta-\, diamond */ - 0xfe, /* : meta-~, centered dot */ - 0xe0, /* : meta-\, diamond */ - 0xfe, /* : meta-~, centered dot */ - 0xfe, /* : meta-~, centered dot */ - 0x00, - 0x00, - 0x00, - 0x00, - /*40 */ 0xe0, - /* : meta-\, diamond */ - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, -/*50*/ 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, /* 0xbd, \E)3: meta-=, int'l currency */ - 0x00, -/*60*/ 0x00, - 0x00, - 0x00, - 0xf8, /* : meta-x, vertical rule */ - 0xf1, /* : meta-q, horizontal rule */ - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, -/*70*/ 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0x00, - 0xef, /* : meta-o, high horizontal line */ - 0x00, - 0xf8, /* : meta-x, vertical rule */ - 0xf8, /* : meta-x, vertical rule */ -/*80*/ 0x00, - 0xf3, /* : meta-s, low horizontal line */ - 0x00, - 0x00, - 0xef, /* : meta-o, high horizontal line */ - 0x00, - 0xf8, /* : meta-x, vertical rule */ - 0x00, - 0xf8, /* : meta-x, vertical rule */ - 0x00, - /*90 */ 0xf3, - /* : meta-s, low horizontal line */ - 0x00 + 225, 224, 254, 0, 0, 238, 0, 0, 0, + 251, 182, 167, 187, 0, 171, 168, + 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, }; -static unsigned char IBM_r_oc_syms[18] = { /* a la EPYX Rogue */ -/* 0*/ '\0', - 0x00, - 0x18, /* weapon: up arrow */ - /* 0x0a, */ 0x00, - /* armor: Vert rect with o */ - /* 0x09, */ 0x00, - /* ring: circle with arrow */ - /* 5 */ 0x0c, - /* amulet: "female" symbol */ - 0x00, - 0x05, /* food: club (as in cards) */ - 0xad, /* potion: upside down '!' */ - 0x0e, /* scroll: musical note */ -/*10*/ 0x00, - 0xe7, /* wand: greek tau */ - 0x0f, /* gold: yes it's the same as gems */ - 0x0f, /* gems: fancy '*' */ - 0x00, -/*15*/ 0x00, - 0x00, - 0x00 +static +struct { + const char *sequence; + int len; + char ascii; +} utf8_mappings[] = { + { "\xe2\x96\x92", 0, '#' }, + { "\xe2\x96\x91", 0, '*' }, + { "\xc2\xb7", 0, '.' }, + { "\xe2\x97\xa6", 0, ',' }, + { "\xe2\x97\xbc", 0, '+' }, + { "\xe2\x88\xa9", 0, '\\' }, + { "\xe2\x8c\xa0", 0, '}' }, + { "\xe2\x89\x88", 0, '{' }, }; +#define ASIZE(x) (sizeof(x) / sizeof(*x)) + void populate_gfx_array (int gfxset) { @@ -349,11 +63,22 @@ populate_gfx_array (int gfxset) if (gfxset == NO_GRAPHICS) return; + if (gfxset == UNICODE_GRAPHICS) + { + for (i = 0; i < ASIZE(utf8_mappings); ++i) + utf8_mappings[i].len = strlen(utf8_mappings[i].sequence); + return; + } + for (i = 0; i < MAXPCHARS; i++) { - if ((gfxset == DEC_GRAPHICS) && (dec_graphics[i]) - && !(gfx_map[dec_graphics[i]])) - gfx_map[dec_graphics[i] - 128] = no_graphics[i]; + if ((gfxset == DEC_GRAPHICS) && (dec_graphics[i])) + { + const int dec_ch = dec_graphics[i]; + const int shifted = dec_ch >= 0xE0? dec_ch & 0x7F : dec_ch; + if (!gfx_map[shifted]) + gfx_map[shifted] = no_graphics[i]; + } if ((gfxset == IBM_GRAPHICS) && (ibm_graphics[i])) gfx_map[ibm_graphics[i]] = no_graphics[i]; } @@ -397,3 +122,42 @@ strip_gfx (unsigned char inchar) return inchar; } + +inline static void +buffer_strip_mapping(char *buffer, int *len, + const char *seq, int slen, char ascii) +{ + char *s = buffer; + char *find; + while ((find = strstr(s, seq)) && (find < buffer + *len)) + { + memmove(find + 1, find + slen, *len - (find - buffer) - slen); + *len -= slen - 1; + *find = ascii; + s = find + 1; + } +} + +static void +buffer_strip_unicode(char *buffer, int *len) +{ + unsigned i; + for (i = 0; i < ASIZE(utf8_mappings); ++i) + buffer_strip_mapping(buffer, len, + utf8_mappings[i].sequence, + utf8_mappings[i].len, + utf8_mappings[i].ascii); +} + +void +buffer_strip_gfx (char *buffer, int *len) +{ + int i; + if (stripped == UNICODE_GRAPHICS) + buffer_strip_unicode(buffer, len); + else + { + for (i = 0; i < *len; ++i) + buffer[i] = strip_gfx(buffer[i]); + } +} diff --git a/stripgfx.h b/stripgfx.h index 617d4ee..b4b4791 100644 --- a/stripgfx.h +++ b/stripgfx.h @@ -1,11 +1,13 @@ #ifndef INCLUDED_stripgfx_h #define INCLUDED_stripgfx_h -#define NO_GRAPHICS 1 -#define DEC_GRAPHICS 2 -#define IBM_GRAPHICS 3 +#define NO_GRAPHICS 1 +#define DEC_GRAPHICS 2 +#define IBM_GRAPHICS 3 +#define UNICODE_GRAPHICS 4 void populate_gfx_array (int gfxset); unsigned char strip_gfx (unsigned char inchar); +void buffer_strip_gfx (char *buffer, int *len); #endif /* !INCLUDED_stripgfx_h */ diff --git a/ttyplay.c b/ttyplay.c index fbdfdfc..48bcb3d 100644 --- a/ttyplay.c +++ b/ttyplay.c @@ -157,7 +157,8 @@ ttyplay_keyboard_action(int c) switch (stripped) { case NO_GRAPHICS: populate_gfx_array ((stripped = DEC_GRAPHICS)); break; - case DEC_GRAPHICS: populate_gfx_array ((stripped = IBM_GRAPHICS)); break; + /* case DEC_GRAPHICS: populate_gfx_array ((stripped = IBM_GRAPHICS)); break; */ + case UNICODE_GRAPHICS: populate_gfx_array ((stripped = IBM_GRAPHICS)); break; case IBM_GRAPHICS: populate_gfx_array ((stripped = NO_GRAPHICS)); break; } return READ_RESTART; @@ -334,13 +335,8 @@ ttypread (FILE * fp, Header * h, char **buf, int pread) void ttywrite (char *buf, int len) { - int i; - - for (i = 0; i < len; i++) - { - if (stripped != NO_GRAPHICS) - buf[i] = strip_gfx (buf[i]); - } + if (stripped != NO_GRAPHICS) + buffer_strip_gfx (buf, &len); fwrite (buf, 1, len, stdout); } -- 2.47.3