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/direct.cc | 4 +++- crawl-ref/source/externs.h | 5 ++++- crawl-ref/source/libunix.cc | 30 +++++++++++++++++++++++++++--- crawl-ref/source/message.cc | 7 +++---- crawl-ref/source/message.h | 2 +- crawl-ref/source/tutorial.cc | 4 ++-- crawl-ref/source/view.cc | 25 ++++++++++++++++++++----- crawl-ref/source/view.h | 5 ++++- 8 files changed, 64 insertions(+), 18 deletions(-) diff --git a/crawl-ref/source/direct.cc b/crawl-ref/source/direct.cc index c7177ead30..bf044736fc 100644 --- a/crawl-ref/source/direct.cc +++ b/crawl-ref/source/direct.cc @@ -1640,7 +1640,9 @@ static void describe_cell(int mx, int my) std::string feature_desc = feature_description(mx, my); #ifdef DEBUG_DIAGNOSTICS - mprf("(%d,%d): %s", mx, my, feature_desc.c_str()); + mprf("(%d,%d): %s - %s", mx, my, + stringize_glyph(get_screen_glyph(mx, my)).c_str(), + feature_desc.c_str()); #else mpr(feature_desc.c_str()); #endif diff --git a/crawl-ref/source/externs.h b/crawl-ref/source/externs.h index 9eef79ac7f..99f95df13e 100644 --- a/crawl-ref/source/externs.h +++ b/crawl-ref/source/externs.h @@ -1194,9 +1194,12 @@ struct game_state bool unicode_ok; // Is unicode support available? + std::string (*glyph2strfn)(unsigned glyph); + int (*multibyte_strlen)(const std::string &s); + game_state() : need_save(false), saving_game(false), updating_scores(false), shopping(false), seen_hups(0), - unicode_ok(false) + unicode_ok(false), glyph2strfn(NULL), multibyte_strlen(NULL) { } }; 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; diff --git a/crawl-ref/source/message.cc b/crawl-ref/source/message.cc index be11e734db..3e92777930 100644 --- a/crawl-ref/source/message.cc +++ b/crawl-ref/source/message.cc @@ -556,7 +556,6 @@ static void mpr_formatted_output(formatted_string fs, int colour) need_prefix = false; } - for ( unsigned i = 0; i < fs.ops.size(); ++i ) { switch ( fs.ops[i].type ) @@ -567,7 +566,7 @@ static void mpr_formatted_output(formatted_string fs, int colour) case FSOP_TEXT: message_out(Message_Line, colour, fs.ops[i].text.c_str(), curcol, false); - curcol += fs.ops[i].text.length(); + curcol += multibyte_strlen(fs.ops[i].text); break; case FSOP_CURSOR: break; @@ -595,7 +594,7 @@ void formatted_mpr(const formatted_string& fs, int channel, int param) // output given string as formatted message, but check patterns // for string stripped of tags and store original tagged string // for message history -void formatted_message_history(const std::string st, int channel, int param) +void formatted_message_history(const std::string &st, int channel, int param) { if (suppress_messages) return; @@ -787,7 +786,7 @@ void replay_messages(void) textcolor( colour ); gotoxy(curcol, wherey()); cprintf(fs.ops[j].text.c_str()); - curcol += fs.ops[j].text.length(); + curcol += multibyte_strlen(fs.ops[j].text); break; case FSOP_CURSOR: break; diff --git a/crawl-ref/source/message.h b/crawl-ref/source/message.h index 5be11ce82f..d2cafbeeab 100644 --- a/crawl-ref/source/message.h +++ b/crawl-ref/source/message.h @@ -62,7 +62,7 @@ class formatted_string; void formatted_mpr(const formatted_string& fs, int channel = MSGCH_PLAIN, int param = 0); -void formatted_message_history(const std::string st, +void formatted_message_history(const std::string &st, int channel = MSGCH_PLAIN, int param = 0); // 4.1-style mpr, currently named mprf for minimal disruption. diff --git a/crawl-ref/source/tutorial.cc b/crawl-ref/source/tutorial.cc index 798c3c10c1..eba41f060b 100644 --- a/crawl-ref/source/tutorial.cc +++ b/crawl-ref/source/tutorial.cc @@ -1051,7 +1051,7 @@ void learned_something_new(tutorial_event_type seen_what, int x, int y) "the responding faith you'll be asked for confirmation."; break; case TUT_SEEN_SHOP: - text << "The " << static_cast(get_screen_glyph(x,y)) + text << "The " << stringize_glyph(get_screen_glyph(x,y)) << " is a shop. You can enter it by typing " "<<."; break; @@ -1059,7 +1059,7 @@ void learned_something_new(tutorial_event_type seen_what, int x, int y) if (you.num_turns < 1) return; - text << "The " << static_cast(get_screen_glyph(x,y)) + text << "The " << stringize_glyph(get_screen_glyph(x,y)) << " is a closed door. You can open it by walking into it. " "Sometimes it is useful to close a door. Do so by pressing " "c, followed by the direction."; diff --git a/crawl-ref/source/view.cc b/crawl-ref/source/view.cc index 0be4619683..922b2837d3 100644 --- a/crawl-ref/source/view.cc +++ b/crawl-ref/source/view.cc @@ -3544,13 +3544,12 @@ void init_feature_table( void ) } } -int get_screen_glyph( int x, int y ) +unsigned get_screen_glyph( int x, int y ) { - const int ex = x - you.x_pos + 9; - const int ey = y - you.y_pos + 9; + const coord_def ep = view2show(grid2view(coord_def(x,y))); - int object = env.show[ex][ey]; - unsigned short colour = env.show_col[ex][ey]; + int object = env.show(ep); + unsigned short colour = env.show_col(ep); unsigned ch; if (!object) @@ -3563,6 +3562,22 @@ int get_screen_glyph( int x, int y ) return (ch); } +std::string stringize_glyph(unsigned glyph) +{ + if (crawl_state.glyph2strfn) + return (*crawl_state.glyph2strfn)(glyph); + + return std::string(1, glyph); +} + +int multibyte_strlen(const std::string &s) +{ + if (crawl_state.multibyte_strlen) + return (*crawl_state.multibyte_strlen)(s); + + return (s.length()); +} + // Returns a string containing an ASCII representation of the map. If fullscreen // is set to false, only the viewable area is returned. Leading and trailing // spaces are trimmed from each line. Leading and trailing empty lines are also diff --git a/crawl-ref/source/view.h b/crawl-ref/source/view.h index 38bdc47b61..8bf9e79237 100644 --- a/crawl-ref/source/view.h +++ b/crawl-ref/source/view.h @@ -114,7 +114,10 @@ void get_item_glyph(const item_def *item, unsigned *glych, unsigned short *glycol); void get_mons_glyph(const monsters *mons, unsigned *glych, unsigned short *glycol); -int get_screen_glyph( int x, int y ); +unsigned get_screen_glyph( int x, int y ); +std::string stringize_glyph(unsigned glyph); +int multibyte_strlen(const std::string &s); + void get_item_symbol(unsigned int object, unsigned *ch, unsigned short *colour); -- cgit v1.2.3-54-g00ecf