Add Unicode rendering to the teken demo application.

Some time ago I tried adding Unicode rendering to the teken demo
application, but I didn't get it working. It seems I forgot to call
setlocale(). Polish this code and make sure it doesn't get lost.

Also a small fix for my previous commit: all Unicode characters in
teken_boxdrawing are below 0x10000, so store them as 16-bit values.
This commit is contained in:
Ed Schouten 2009-01-21 19:43:10 +00:00
parent b63865c6bf
commit 2dfc3551a6
2 changed files with 32 additions and 11 deletions

View file

@ -31,6 +31,7 @@
#include <assert.h>
#include <errno.h>
#include <inttypes.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@ -83,7 +84,8 @@ static void
printchar(const teken_pos_t *p)
{
int y, x, attr = 0;
struct pixel *px, pt;
struct pixel *px;
char str[5] = { 0 };
assert(p->tp_row < NROWS);
assert(p->tp_col < NCOLS);
@ -92,14 +94,26 @@ printchar(const teken_pos_t *p)
px = &buffer[p->tp_col][p->tp_row];
if (px->c >= 0x80) {
/* Mark UTF-8 chars (we don't support it). */
px = &pt;
px->c = '?';
px->a.ta_format = TF_BOLD;
px->a.ta_fgcolor = TC_BROWN;
px->a.ta_bgcolor = TC_RED;
/* Convert Unicode to UTF-8. */
#ifdef TEKEN_UTF8
if (px->c < 0x80) {
str[0] = px->c;
} else if (px->c < 0x800) {
str[0] = 0xc0 | (px->c >> 6);
str[1] = 0x80 | (px->c & 0x3f);
} else if (px->c < 0x10000) {
str[0] = 0xe0 | (px->c >> 12);
str[1] = 0x80 | ((px->c >> 6) & 0x3f);
str[2] = 0x80 | (px->c & 0x3f);
} else {
str[0] = 0xf0 | (px->c >> 18);
str[1] = 0x80 | ((px->c >> 12) & 0x3f);
str[2] = 0x80 | ((px->c >> 6) & 0x3f);
str[3] = 0x80 | (px->c & 0x3f);
}
#else /* !TEKEN_UTF8 */
str[0] = px->c;
#endif /* TEKEN_UTF8 */
if (px->a.ta_format & TF_BOLD)
attr |= A_BOLD;
@ -109,7 +123,7 @@ printchar(const teken_pos_t *p)
attr |= A_BLINK;
bkgdset(attr | COLOR_PAIR(px->a.ta_fgcolor + 8 * px->a.ta_bgcolor));
mvaddch(p->tp_row, p->tp_col, px->c);
mvaddstr(p->tp_row, p->tp_col, str);
move(y, x);
}
@ -272,9 +286,16 @@ main(int argc __unused, char *argv[] __unused)
fd_set rfds;
char b[256];
ssize_t bl;
const int ccolors[8] = { COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW, COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE };
const int ccolors[8] = {
COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW,
COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE
};
int i, j;
#ifdef TEKEN_UTF8
setlocale(LC_CTYPE, "UTF-8");
#endif /* TEKEN_UTF8 */
tp.tp_row = ws.ws_row = NROWS;
tp.tp_col = ws.ws_col = NCOLS;

View file

@ -62,7 +62,7 @@ teken_scs_process(teken_t *t, teken_char_t c)
}
/* Unicode points for VT100 box drawing. */
static const teken_char_t teken_boxdrawing[31] = {
static const uint16_t teken_boxdrawing[31] = {
0x25c6, 0x2592, 0x2409, 0x240c, 0x240d, 0x240a, 0x00b0, 0x00b1,
0x2424, 0x240b, 0x2518, 0x2510, 0x250c, 0x2514, 0x253c, 0x23ba,
0x23bb, 0x2500, 0x23bc, 0x23bd, 0x251c, 0x2524, 0x2534, 0x252c,