add some unicode support (trunk only)

This patch attempts to add some levels of unicode support
to NetHack.

The master on/off switch for any Unicode support is
defining UNICODE_SUPPORT in config.h. Currently
there is code support for two subsets of unicode support:

UNICODE_DRAWING

If UNICODE_DRAWING is defined, then the data
structures used to house drawing symbols are expanded
to the size of wchar_t, big enough to hold unicode characters.
A typdef called `nhsym' is involved and if UNICODE_DRAWING
is defined, it is wchar_t, otherwise it is uchar.

UNICODE_WIDEWINPORT

If UNICODE_WIDEWINPORT is defined, then the data
structures inside the window port are expanded to the size of
wchar_t, big enough to hold unicode characters.  Both map
symbols and text within the window port are expanded, in order
for potential support for displaying multinational characters some
day, but this patch only provides viewing of map symbols.
A typdef called `nhwchar' is involved and if UNICODE_WIDEWINPORT
is defined, it is wchar_t, otherwise it is char.

The only window port with code support for UNICODE_WIDEWINPORT
currently is the TTY port.  Don't enable UNICODE_WIDEWINPORT
unless:
- it is a TTY port
- the underlying platform specific routines can
handle the larger data structures.

Don't enable UNICODE_SUPPORT unless:
- your compiler can handle wchar_t.
- your compiler can accept L'a' characters.
- your compiler can accept L"wide" strings.

Note that if your compiler can handle the above, you could
enable the larger data structures (currently if TTY) even if your
platform can't actually display unicode or UTF-8, by messing
with u_putch() in win/tty/wintty.c to only deal regular chars.
That should be the only function that actually pushes wide characters
out to the display.

If you enable UNICODE_SUPPORT, and your platform is capable
you will need to turn on the unicode run-time option to be able to
load unicode character sets from the symbol file, to be able to
push unicode characters to the display. You'll also want to load
a unicode symbol set once the unicode option is toggled on. In
a config file you would do that via these two lines:
OPTIONS=unicode
OPTIONS=symset:Unicode_non_US

The repository was stamped with NETHACK_PRE_UNICODE
prior to applying this patch, and stamped with
NETHACK_POST_UNICODE afterwards. The code differences
between those two tagged versions are this patch.
This commit is contained in:
nethack.allison
2006-10-17 23:55:42 +00:00
parent 3054c1c846
commit 7f0f43e6f9
22 changed files with 1193 additions and 177 deletions

View File

@@ -261,7 +261,8 @@ NEARDATA winid WIN_MESSAGE = WIN_ERR;
NEARDATA winid WIN_STATUS = WIN_ERR;
#endif
NEARDATA winid WIN_MAP = WIN_ERR, WIN_INVEN = WIN_ERR;
char toplines[TBUFSZ];
nhwchar toplines[TBUFSZ];
/* Windowing stuff that's really tty oriented, but present for all ports */
struct tc_gbl_data tc_gbl_data = { 0,0, 0,0 }; /* AS,AE, LI,CO */

View File

@@ -23,13 +23,13 @@ struct symsetentry symset[NUM_GRAPHICS];
int currentgraphics = 0;
uchar showsyms[SYM_MAX] = DUMMY; /* symbols to be displayed */
uchar l_syms[SYM_MAX] = DUMMY; /* loaded symbols */
nhsym showsyms[SYM_MAX] = DUMMY; /* symbols to be displayed */
nhsym l_syms[SYM_MAX] = DUMMY; /* loaded symbols */
#ifdef REINCARNATION
uchar r_syms[SYM_MAX] = DUMMY; /* rogue symbols */
nhsym r_syms[SYM_MAX] = DUMMY; /* rogue symbols */
#endif
uchar warnsyms[WARNCOUNT] = DUMMY; /* the current warning display symbols */
nhsym warnsyms[WARNCOUNT] = DUMMY; /* the current warning display symbols */
const char invisexplain[] = "remembered, unseen, creature";
/* Default object class symbols. See objclass.h.
@@ -546,6 +546,7 @@ boolean name_too;
/* initialize restriction bits */
symset[which_set].primary = 0;
symset[which_set].rogue = 0;
symset[which_set].unicode = 0;
if (name_too) {
if (symset[which_set].name)
@@ -583,6 +584,7 @@ const char *known_handling[] = {
const char *known_restrictions[] = {
"primary",
"rogue",
"unicode",
(const char *)0,
};

View File

@@ -2531,6 +2531,7 @@ int which_set;
return 0;
if (!symset[which_set].name) {
int i;
/* A null symset name indicates that we're just
building a pick-list of possible symset
values from the file, so only do that */
@@ -2556,6 +2557,20 @@ int which_set;
/* initialize restriction bits */
tmpsp->primary = 0;
tmpsp->rogue = 0;
tmpsp->unicode = 0;
break;
case 2:
/* handler type identified */
tmpsp = symset_list; /* most recent symset */
tmpsp->handling = H_UNK;
i = 0;
while (known_handling[i]) {
if (!strcmpi(known_handling[i], bufp)) {
tmpsp->handling = i;
break; /* while loop */
}
i++;
}
break;
case 3: /* description:something */
tmpsp = symset_list; /* most recent symset */
@@ -2573,6 +2588,7 @@ int which_set;
switch(i) {
case 0: tmpsp->primary = 1; break;
case 1: tmpsp->rogue = 1; break;
case 2: tmpsp->unicode = 1; break;
}
break; /* while loop */
}
@@ -2594,6 +2610,7 @@ int which_set;
/* these init_*() functions clear symset fields too */
# ifdef REINCARNATION
if (which_set == ROGUESET) init_r_symbols();
else
# endif
if (which_set == PRIMARY) init_l_symbols();
}
@@ -2639,18 +2656,32 @@ int which_set;
}
i++;
}
}
/* Don't allow unicode set if code can't handle it */
if (symset[which_set].unicode &&
!iflags.unicodedisp) {
if (chosen_symset_start)
chosen_symset_end = FALSE;
chosen_symset_start = FALSE;
# ifdef REINCARNATION
if (which_set == ROGUESET) init_r_symbols();
else
# endif
if (which_set == PRIMARY) init_l_symbols();
}
}
break;
}
} else { /* !SYM_CONTROL */
val = sym_val(bufp);
if (chosen_symset_start) {
if (which_set == PRIMARY) {
update_l_symset(symp, val);
}
# ifdef REINCARNATION
if (which_set == ROGUESET)
update_r_symset(symp, val);
else
else if (which_set == ROGUESET) {
update_r_symset(symp, val);
}
# endif
update_l_symset(symp, val);
}
}
}
@@ -2673,6 +2704,72 @@ int which_set;
i++;
}
}
/*
* Produces a single integer value.
*/
int
sym_val(cp)
const char *cp;
{
unsigned int cval = 0;
int meta = 0, dcount = 0;
const char *dp, *hex = "00112233445566778899aAbBcCdDeEfF";
while (*cp)
{
if (*cp == '\\' && index("mM", cp[1])) {
meta = 1;
cp += 2;
}
if ((*cp == 'U' || *cp == 'u') && cp[1] == '+' && index(hex, cp[2]))
{
dcount = 0;
cp++;
for (++cp; *cp && (dp = index(hex, *cp)) && (dcount++ < 4); cp++)
cval = (unsigned int)((cval * 16) +
((unsigned int)(dp - hex) / 2));
}
else if (*cp == '\\' && index("0123456789xXoO", cp[1]))
{
dcount = 0;
cp++;
if (*cp == 'x' || *cp == 'X')
for (++cp; *cp && (dp = index(hex, *cp)) && (dcount++ < 4); cp++)
cval = (unsigned int)((cval * 16) +
((unsigned int)(dp - hex) / 2));
else if (*cp == 'o' || *cp == 'O')
for (++cp; *cp && (index("01234567",*cp)) && (dcount++ < 5); cp++)
cval = (cval * 8) + (unsigned int)(*cp - '0');
else
for (; *cp && (index("0123456789",*cp)) && (dcount++ < 5); cp++)
cval = (cval * 10) + (unsigned int)(*cp - '0');
}
else if (*cp == '\\') /* C-style character escapes */
{
switch (*++cp)
{
case '\\': cval = '\\'; break;
case 'n': cval = '\n'; break;
case 't': cval = '\t'; break;
case 'b': cval = '\b'; break;
case 'r': cval = '\r'; break;
default: cval = (unsigned int)*cp;
}
cp++;
}
else if (*cp == '^') /* expand control-character syntax */
{
cval = (unsigned int)(*++cp & 0x1f);
cp++;
}
else
cval = (unsigned int)*cp++;
if (meta)
cval |= 0x80;
}
return cval;
}
#endif /*LOADSYMSETS*/
/* ---------- END CONFIG FILE HANDLING ----------- */

View File

@@ -624,4 +624,192 @@ midnight()
return(getlt()->tm_hour == 0);
}
#ifdef UNICODE_WIDEWINPORT
nhwchar *
nhwstrncpy(dest, strSource, cnt)
nhwchar *dest;
const char *strSource;
size_t cnt;
{
nhwchar *d = dest;
const char *s = strSource;
size_t dcnt = 0;
while(*s && dcnt < cnt) {
*d++ = (nhwchar)*s++;
dcnt++;
}
if (dcnt < cnt) *d = 0;
return dest;
}
nhwchar *
nhwncpy(dest, src, cnt)
nhwchar *dest;
const nhwchar *src;
size_t cnt;
{
nhwchar *d = dest;
const nhwchar *s = src;
size_t dcnt = 0;
while(*s && dcnt < cnt) {
*d++ = *s++;
dcnt++;
}
if (dcnt < cnt) *d = 0;
return dest;
}
nhwchar *
nhwcpy(dest, src)
nhwchar *dest;
const nhwchar *src;
{
nhwchar *d = dest;
const nhwchar *s = src;
while(*s) {
*d++ = *s++;
}
*d = 0;
return dest;
}
nhwchar *
nhwstrcpy(dest, strSource)
nhwchar *dest;
const char *strSource;
{
nhwchar *d = dest;
const char *s = strSource;
while(*s) {
*d++ = *s++;
}
*d = 0;
return dest;
}
char *
strnhwcpy(strDest, src)
char *strDest;
const nhwchar *src;
{
char *d = strDest;
const nhwchar *s = src;
while(*s) {
*d++ = (char)*s++;
}
*d = 0;
return strDest;
}
nhwchar *
nhwstrcat(dest, strSource)
nhwchar *dest;
const char *strSource;
{
nhwchar *d = dest;
const char *s = strSource;
while(*d) d++;
while(*s) {
*d++ = *s++;
}
*d = 0;
return dest;
}
nhwchar *
nhwcat(dest, src)
nhwchar *dest;
const nhwchar *src;
{
nhwchar *d = dest;
const nhwchar *s = src;
while(*d) d++;
while(*s) {
*d++ = *s++;
}
*d = 0;
return dest;
}
nhwchar *
nhwindex(ss, c)
const nhwchar *ss;
int c;
{
const nhwchar *s = ss;
while (*s) {
if (*s == c) return (nhwchar *)s;
s++;
}
if (*s == c) return (nhwchar *)s;
return (nhwchar *)0;
}
size_t nhwlen(src)
const nhwchar *src;
{
register size_t dl = 0;
while(*src++) dl++;
return dl;
}
int
nhwcmp(s1, s2) /* case insensitive counted string comparison */
register const nhwchar *s1, *s2;
{ /*{ aka strncasecmp }*/
register nhwchar t1, t2;
for (;;) {
if (!*s2) return (*s1 != 0); /* s1 >= s2 */
else if (!*s1) return -1; /* s1 < s2 */
t1 = *s1++;
t2 = *s2++;
if (t1 != t2) return (t1 > t2) ? 1 : -1;
}
return 0; /* s1 == s2 */
}
int
nhwncmp(s1, s2, n) /* case sensitive counted nhwchar (wide string) comparison */
register const nhwchar *s1, *s2;
register int n; /*(should probably be size_t, which is usually unsigned)*/
{
register nhwchar t1, t2;
while (n--) {
if (!*s2) return (*s1 != 0); /* s1 >= s2 */
else if (!*s1) return -1; /* s1 < s2 */
t1 = *s1++;
t2 = *s2++;
if (t1 != t2) return (t1 > t2) ? 1 : -1;
}
return 0; /* s1 == s2 */
}
int
nhwstrcmp(s1, s2)
register const nhwchar *s1;
const char *s2;
{ /*{ aka strncasecmp }*/
register nhwchar t1, t2;
for (;;) {
if (!*s2) return (*s1 != 0); /* s1 >= s2 */
else if (!*s1) return -1; /* s1 < s2 */
t1 = *s1++;
t2 = (nhwchar)*s2++;
if (t1 != t2) return (t1 > t2) ? 1 : -1;
}
return 0; /* s1 == s2 */
}
#endif
/*hacklib.c*/

View File

@@ -67,7 +67,7 @@ unsigned *ospecial;
#if defined(TEXTCOLOR) || defined(ROGUE_COLOR)
int color = NO_COLOR;
#endif
uchar ch;
nhsym ch;
unsigned special = 0;
/* condense multiple tests in macro version down to single */
boolean has_rogue_ibm_graphics = HAS_ROGUE_IBM_GRAPHICS;
@@ -263,6 +263,7 @@ int glyph;
return encbuf;
}
#ifndef UNICODE_WIDEWINPORT
/*
* This differs from putstr() because the str parameter can
* contain a sequence of characters representing:
@@ -304,25 +305,12 @@ genl_putmixed(window, attr, str)
gv = (int)((gv * 16) + ((int)(dp - hex) / 2));
so = mapglyph(gv, &ch, &oc, &os, 0, 0);
*put++ = showsyms[so];
continue;
} else {
/* possible forgery - leave it the way it is */
cp = save_cp;
}
break;
# if 0
case 'S': /* symbol offset */
dcount = 0;
for (++cp; *cp && (dp = index(hex, *cp)) && (dcount++ < 4); cp++)
rndchk = (int)((rndchk * 16) + ((int)(dp - hex) / 2));
if (rndchk == context.rndencode) {
dcount = 0;
for (; *cp && (dp = index(hex, *cp)) && (dcount++ < 2); cp++)
so = (int)((so * 16) + ((int)(dp - hex) / 2));
}
*put++ = showsyms[so];
break;
# endif
case '\\':
break;
}
@@ -333,4 +321,5 @@ genl_putmixed(window, attr, str)
/* now send it to the normal putstr */
putstr(window, attr, buf);
}
#endif /*!UNICODE_WIDEWINPORT*/
/*mapglyph.c*/

View File

@@ -195,6 +195,9 @@ static struct Bool_Opt
{"tombstone",&flags.tombstone, TRUE, SET_IN_GAME},
{"toptenwin",&flags.toptenwin, FALSE, SET_IN_GAME},
{"travel", &flags.travelcmd, TRUE, SET_IN_GAME},
#ifdef UNICODE_SUPPORT
{"unicode", &iflags.unicodedisp, FALSE, SET_IN_GAME},
#endif
#ifdef WIN32CON
{"use_inverse", &iflags.wc_inverse, TRUE, SET_IN_GAME}, /*WC*/
#else
@@ -347,7 +350,7 @@ static struct Comp_Opt
DISP_IN_GAME},
# endif
#else
SET_IN_FILE },
SET_IN_FILE},
#endif
{ "suppress_alert", "suppress alerts about version-specific features",
8, SET_IN_GAME },
@@ -582,6 +585,9 @@ initoptions()
iflags.msg_history = 20;
#ifdef TTY_GRAPHICS
iflags.prevmsg_window = 's';
# if defined(UNIX) && defined(UNICODE_WIDEWINPORT)
iflags.unicodecapable = TRUE;
# endif
#endif
iflags.menu_headings = ATR_INVERSE;
@@ -2326,7 +2332,7 @@ goodfruit:
# ifdef LOADSYMSETS
if (duplicate) complain_about_duplicate(opts,1);
if (!negated) {
for (i = 0; i < NUM_GRAPHICS; ++i) {
for (i = PRIMARY; i <= ROGUESET; ++i) {
if (symset[i].name)
badflag = TRUE;
else {
@@ -3154,25 +3160,31 @@ boolean setinitial,setfromfile;
if (pick_cnt >= 0) goto ape_again;
}
#endif /* AUTOPICKUP_EXCEPTIONS */
} else if (!strcmp("symset", optname) ||
!strcmp("roguesymset", optname)) {
} else if (!strcmp("symset", optname)
|| !strcmp("roguesymset", optname)) {
menu_item *symset_pick = (menu_item *)0;
boolean primaryflag = (*optname == 's'),
rogueflag = (*optname == 'r');
rogueflag = (*optname == 'r'),
ready_to_switch = FALSE,
nothing_to_do = FALSE;
#ifdef LOADSYMSETS
int res;
char *symset_name, fmtstr[20];
struct symsetentry *sl;
int setcount = 0;
#endif
int chosen = -2, which_set =
int chosen = -2, which_set;
#ifdef REINCARNATION
rogueflag ? ROGUESET :
if (rogueflag) which_set = ROGUESET;
else
#endif
PRIMARY;
which_set = PRIMARY;
#ifndef REINCARNATION
if (rogueflag) return TRUE;
#endif
#ifdef LOADSYMSETS
/* clear symset[].name as a flag to read_sym_file() to build list */
symset_name = symset[which_set].name;
@@ -3186,7 +3198,8 @@ boolean setinitial,setfromfile;
sl = symset_list;
while (sl) {
/* check restrictions */
if ((!rogueflag && sl->rogue) ||
if ((!rogueflag && sl->rogue) ||
(!iflags.unicodedisp && sl->unicode) ||
(!primaryflag && sl->primary)) {
sl = sl->next;
continue;
@@ -3199,8 +3212,8 @@ boolean setinitial,setfromfile;
}
if (!setcount) {
pline("There are no appropriate %ssymbol sets available.",
(rogueflag) ? "rogue level " :
(primaryflag) ? "primary " :
(rogueflag) ? "rogue level " :
(primaryflag) ? "primary " :
"");
return TRUE;
}
@@ -3216,6 +3229,7 @@ boolean setinitial,setfromfile;
while (sl) {
/* check restrictions */
if ((!rogueflag && sl->rogue) ||
(!iflags.unicodedisp && sl->unicode) ||
(!primaryflag && sl->primary)) {
sl = sl->next;
continue;
@@ -3226,10 +3240,10 @@ boolean setinitial,setfromfile;
sl->desc ? sl->desc : "");
add_menu(tmpwin, NO_GLYPH, &any, let, 0,
ATR_NONE, symsetchoice, MENU_UNSELECTED);
sl = sl->next;
if (let == 'z') let = 'A';
else let++;
}
sl = sl->next;
}
end_menu(tmpwin, "Select symbol set:");
if (select_menu(tmpwin, PICK_ONE, &symset_pick) > 0) {
@@ -3254,7 +3268,7 @@ boolean setinitial,setfromfile;
symset[which_set].name =
(char *)alloc(strlen(sl->name)+1);
Strcpy(symset[which_set].name, sl->name);
ready_to_switch = TRUE;
break;
}
sl = sl->next;
@@ -3268,6 +3282,7 @@ boolean setinitial,setfromfile;
symset_name = (char *)0;
clear_symsetentry(which_set, TRUE);
}
else nothing_to_do = TRUE;
} else if (!res) {
/* The symbols file could not be accessed */
pline("Unable to access \"%s\" file.", SYMBOLS);
@@ -3292,34 +3307,41 @@ boolean setinitial,setfromfile;
free((genericptr_t)sl);
}
/* Set default symbols and clear the handling value */
# ifdef REINCARNATION
if(rogueflag) init_r_symbols();
else
# endif
init_l_symbols();
if (nothing_to_do) return TRUE;
if (!symset[which_set].name && symset_name)
symset[which_set].name = symset_name;
symset[which_set].name = symset_name;
/* Set default symbols and clear the handling value */
# ifdef REINCARNATION
if(rogueflag)
init_r_symbols();
else
# endif
init_l_symbols();
if (symset[which_set].name) {
if (read_sym_file(which_set))
switch_symbols(TRUE);
ready_to_switch = TRUE;
else {
clear_symsetentry(which_set, TRUE);
return TRUE;
}
}
switch_symbols(TRUE);
if (ready_to_switch) switch_symbols(TRUE);
# ifdef REINCARNATION
if (Is_rogue_level(&u.uz))
assign_graphics(ROGUESET);
else
if (Is_rogue_level(&u.uz)) {
if (rogueflag)
assign_graphics(ROGUESET);
} else
# endif
assign_graphics(PRIMARY);
if (!rogueflag) assign_graphics(PRIMARY);
need_redraw = TRUE;
#endif /*LOADSYMSETS*/
return TRUE;
} else {
/* didn't match any of the special options */
return FALSE;
@@ -3366,7 +3388,11 @@ char *buf;
#endif
#ifdef BACKWARD_COMPAT
else if (!strcmp(optname, "boulder"))
# ifdef UNICODE_DRAWING
Sprintf(buf, "\\x%04X", iflags.bouldersym ?
# else
Sprintf(buf, "%c", iflags.bouldersym ?
# endif
iflags.bouldersym :
showsyms[(int)objects[BOULDER].oc_class + SYM_OFF_O]);
#endif
@@ -3519,14 +3545,17 @@ char *buf;
else if (!strcmp(optname, "race"))
Sprintf(buf, "%s", rolestring(flags.initrace, races, noun));
#ifdef REINCARNATION
else if (!strcmp(optname, "roguesymset"))
else if (!strcmp(optname, "roguesymset")) {
Sprintf(buf, "%s",
# ifdef LOADSYMSETS
symset[ROGUESET].name ?
symset[ROGUESET].name :
# endif
"default");
if (currentgraphics == ROGUESET && symset[ROGUESET].name)
Strcat(buf, ", active");
#endif
}
else if (!strcmp(optname, "role"))
Sprintf(buf, "%s", rolestring(flags.initrole, roles, name.m));
else if (!strcmp(optname, "runmode"))
@@ -3558,13 +3587,16 @@ char *buf;
FEATURE_NOTICE_VER_MIN,
FEATURE_NOTICE_VER_PATCH);
}
else if (!strcmp(optname, "symset"))
else if (!strcmp(optname, "symset")) {
Sprintf(buf, "%s",
#ifdef LOADSYMSETS
symset[PRIMARY].name ?
symset[PRIMARY].name :
#endif
"default");
if (currentgraphics == PRIMARY && symset[PRIMARY].name)
Strcat(buf, ", active");
}
else if (!strcmp(optname, "tile_file"))
Sprintf(buf, "%s", iflags.wc_tile_file ? iflags.wc_tile_file : defopt);
else if (!strcmp(optname, "tile_height")) {
@@ -3818,15 +3850,6 @@ char *buf;
}
return (struct symparse *)0;
}
int sym_val(strval)
char *strval;
{
char buf[QBUFSZ];
buf[0] = '\0';
escapes(strval, buf);
return (int)*buf;
}
#endif /*LOADSYMSETS*/
/* data for option_help() */

View File

@@ -581,7 +581,7 @@ do_look(mode, click_cc)
for (i = 0; i < MAXMCLASSES; i++) {
if (sym == ((from_screen || clicklook) ?
showsyms[i + SYM_OFF_M] : def_monsyms[i].sym) &&
def_monsyms[i].explain) {
def_monsyms[i].explain) {
need_to_look = TRUE;
if (!found) {
Sprintf(out_str, "%s %s",

View File

@@ -58,7 +58,11 @@ pline VA_DECL(const char *, line)
return;
}
#ifndef MAC
if (no_repeat && !strcmp(line, toplines))
# ifdef UNICODE_WIDEWINPORT
if (no_repeat && !nhwstrcmp(toplines, line))
# else
if (no_repeat && !strcmp(toplines, line))
# endif
return;
#endif /* MAC */
if (vision_full_recalc) vision_recalc(0);