From 859a5ab42d2c9922f5ddc0b73961bb4231e379b7 Mon Sep 17 00:00:00 2001 From: dshaligram Date: Fri, 8 Jun 2007 18:48:41 +0000 Subject: When showing glyphs in messages (as in the tutorial), use stringize_glyph() to convert Unicode glyphs to multibyte sequences. Added multibyte_strlen to calculate string lengths correctly when dealing with multibyte strings. git-svn-id: https://crawl-ref.svn.sourceforge.net/svnroot/crawl-ref/trunk@1560 c06c8d41-db1a-0410-9941-cceddc491573 --- crawl-ref/source/libunix.cc | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) (limited to 'crawl-ref/source/libunix.cc') diff --git a/crawl-ref/source/libunix.cc b/crawl-ref/source/libunix.cc index b92e33241b..b26397113a 100644 --- a/crawl-ref/source/libunix.cc +++ b/crawl-ref/source/libunix.cc @@ -48,6 +48,7 @@ #include "files.h" #ifdef UNICODE_GLYPHS +#include #include #endif @@ -167,6 +168,26 @@ static void setup_colour_pairs( void ) init_pair(63, COLOR_BLACK, Options.background); } // end setup_colour_pairs() +#ifdef UNICODE_GLYPHS +static std::string unix_glyph2string(unsigned gly) +{ + char buf[50]; // Overkill, I know. + wchar_t wcbuf[2]; + wcbuf[0] = gly; + wcbuf[1] = 0; + if (wcstombs(buf, wcbuf, sizeof buf) != (size_t) -1) + return (buf); + + return std::string(1, gly); +} + +static int unix_multibyte_strlen(const std::string &s) +{ + const char *cs = s.c_str(); + size_t len = mbsrtowcs(NULL, &cs, 0, NULL); + return (len == (size_t) -1? s.length() : len); +} +#endif static void termio_init() { @@ -187,7 +208,11 @@ static void termio_init() tcsetattr(0, TCSAFLUSH, &game_term); #ifdef UNICODE_GLYPHS - crawl_state.unicode_ok = !!setlocale(LC_ALL, ""); + if ((crawl_state.unicode_ok = !!setlocale(LC_ALL, ""))) + { + crawl_state.glyph2strfn = unix_glyph2string; + crawl_state.multibyte_strlen = unix_multibyte_strlen; + } #endif } @@ -271,7 +296,7 @@ void message_out(int which_line, int color, const char *s, int firstcol, firstcol = Options.delay_message_clear? 1 : 0; else firstcol--; - + mvwaddstr(Message_Window, which_line, firstcol, s); if (newline && which_line == crawl_view.msgsz.y - 1) @@ -394,7 +419,6 @@ int itoa(int value, char *strptr, int radix) return (OK); /* Me? Fail? Nah. */ } - int cprintf(const char *format,...) { int i; -- cgit v1.2.3-54-g00ecf