Attributes for UTF-8 characters

540 views Asked by At

Ncurses can display characters with attached attributes via chtypes, which are constructed by or'ing a single character with attributes bitmasks thusly :

addch('a' | A_REVERSE);

However, after enabling UTF-8 support, pushing a multibyte character to the screen must be done via addstr(char const*), and there is no room for attributes.

Is there a possibility of keeping attributes with multibyte characters, or should I just keep track of them myself and use attron()/attroff() when needed ?

2

There are 2 answers

0
Thomas Dickey On BEST ANSWER

There are actually four character types which can be used with ncurses:

The char and chtype data came first, for 8-bit encodings. wchar_t and cchar_t came later for wide-characters. The latter of each pair is essentially the former combined with video attributes and color.

ncurses differs from X/Open curses by allowing multibyte characters to be added via the waddstr (and waddch) interfaces. Actually this would be the "ncursesw" library (the "ncurses" library does 8-bit encodings).

wchar_t holds more bits than char.

On Linux, wchar_t is (almost) synonymous with Unicode. This is not necessarily portable, so ncurses uses the wide-character functions to convert from wchar_t as needed into UTF-8 — or whatever the terminal is using for its encoding. Likewise, the input to waddstr may be UTF-8, but ncurses uses the corresponding multibyte-character functions for converting from your application's locale-encoding into wchar_t values.

0
rici On

If your ncurses supports wide characters, then you can use routines like add_wch to add a single wide character, optionally with attributes. However, a wide character is not the same as a multibyte character; you would need to convert the multibyte sequences into wide characters in order to use them with add_wch.

See the standard library function mbtowc and mbstowcs, as well as their reentrant variants.