From 2aaec5adc7455ae8c679d39be891adcf0ccee9a6 Mon Sep 17 00:00:00 2001 From: Timo Sirainen Date: Mon, 14 Oct 2002 18:33:29 +0000 Subject: [PATCH] Some changes to make doublewidth chars possibly work (or possibly horribly break whole utf8 support :) git-svn-id: http://svn.irssi.org/repos/irssi/trunk@2956 dbcabf3a-b0e7-0310-adc4-f8d773084564 --- src/fe-text/gui-entry.c | 6 ++- src/fe-text/textbuffer-view.c | 54 +++++++++++++++++-------- src/fe-text/utf8.c | 74 +++++++++++++++++++++++++++++++++++ src/fe-text/utf8.h | 3 ++ 4 files changed, 120 insertions(+), 17 deletions(-) diff --git a/src/fe-text/gui-entry.c b/src/fe-text/gui-entry.c index e7e5fcc5..f6bfb27d 100644 --- a/src/fe-text/gui-entry.c +++ b/src/fe-text/gui-entry.c @@ -104,7 +104,11 @@ static void gui_entry_draw_from(GUI_ENTRY_REC *entry, int pos) p = entry->scrstart + pos < entry->text_len ? entry->text + entry->scrstart + pos : empty_str; - for (; *p != '\0' && xpos < end_xpos; p++, xpos++) { + for (; *p != '\0'; p++) { + xpos += utf8_width(*p); + if (xpos > end_xpos) + break; + if (entry->hidden) term_addch(root_window, ' '); else if (*p >= 32 && (entry->utf8 || (*p & 127) >= 32)) diff --git a/src/fe-text/textbuffer-view.c b/src/fe-text/textbuffer-view.c index a3cbe460..77b21a2e 100644 --- a/src/fe-text/textbuffer-view.c +++ b/src/fe-text/textbuffer-view.c @@ -152,8 +152,10 @@ view_update_line_cache(TEXT_BUFFER_VIEW_REC *view, LINE_REC *line) LINE_CACHE_SUB_REC *sub; GSList *lines; unsigned char cmd; - const unsigned char *ptr, *last_space_ptr; + const unsigned char *ptr, *next_ptr, *last_space_ptr; int xpos, pos, indent_pos, last_space, last_color, color, linecount; + int char_len; + unichar chr; g_return_val_if_fail(line->text != NULL, NULL); @@ -196,7 +198,24 @@ view_update_line_cache(TEXT_BUFFER_VIEW_REC *view, LINE_REC *line) continue; } - if (xpos == view->width && sub != NULL && + if (!view->utf8) { + next_ptr = ptr+1; + char_len = 1; + } else { + char_len = 1; + while (ptr[char_len] != '\0' && char_len < 6) + char_len++; + + next_ptr = ptr; + chr = get_utf8_char(&next_ptr, char_len); + if (chr < 0) + char_len = 1; + else + char_len = utf8_width(chr); + next_ptr++; + } + + if (xpos + char_len > view->width && sub != NULL && (last_space <= indent_pos || last_space <= 10) && view->longword_noindent) { /* long word, remove the indentation from this line */ @@ -204,7 +223,7 @@ view_update_line_cache(TEXT_BUFFER_VIEW_REC *view, LINE_REC *line) sub->indent = 0; } - if (xpos == view->width) { + if (xpos + char_len > view->width) { xpos = indent_func == NULL ? indent_pos : indent_func(view, line, -1); @@ -232,15 +251,14 @@ view_update_line_cache(TEXT_BUFFER_VIEW_REC *view, LINE_REC *line) continue; } - if (view->utf8) - get_utf8_char(&ptr, 6); - - xpos++; - if (*ptr++ == ' ') { - last_space = xpos-1; + if (*ptr == ' ') { + last_space = xpos; last_space_ptr = ptr; last_color = color; } + + xpos += char_len; + ptr = next_ptr; } rec = g_malloc(sizeof(LINE_CACHE_REC)-sizeof(LINE_CACHE_SUB_REC) + @@ -309,9 +327,9 @@ static int view_line_draw(TEXT_BUFFER_VIEW_REC *view, LINE_REC *line, { INDENT_FUNC indent_func; LINE_CACHE_REC *cache; - const unsigned char *text, *text_newline; + const unsigned char *text, *end, *text_newline; unsigned char *tmp; - int xpos, color, drawcount, first, need_move, need_clrtoeol; + int xpos, color, drawcount, first, need_move, need_clrtoeol, char_width; if (view->dirty) /* don't bother drawing anything - redraw is coming */ return 0; @@ -401,11 +419,16 @@ static int view_line_draw(TEXT_BUFFER_VIEW_REC *view, LINE_REC *line, continue; } - if (xpos < term_width) { - const unsigned char *end = text; - if (view->utf8) - get_utf8_char(&end, 6); + end = text; + if (view->utf8) { + unichar chr = get_utf8_char(&end, 6); + char_width = utf8_width(chr); + } else { + char_width = 1; + } + xpos += char_width; + if (xpos <= term_width) { if (*text >= 32 && (end != text || (*text & 127) >= 32)) { for (; text < end; text++) @@ -419,7 +442,6 @@ static int view_line_draw(TEXT_BUFFER_VIEW_REC *view, LINE_REC *line, } } text++; - xpos++; } if (need_clrtoeol && xpos < term_width) { diff --git a/src/fe-text/utf8.c b/src/fe-text/utf8.c index 2d72fe2b..4049991d 100644 --- a/src/fe-text/utf8.c +++ b/src/fe-text/utf8.c @@ -7,6 +7,10 @@ * Copyright (C) 1999 Tom Tromey * Copyright (C) 2000 Red Hat, Inc. * + * UTF-8 width tables based on locale data from GNU libc by + * + * Copyright (C) 1991-2002 Free Software Foundation, Inc. + * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either @@ -175,3 +179,73 @@ void utf16_to_utf8(const unichar *str, char *out) } *out = '\0'; } + +static const unichar wcc[] = { + 0x0, 0x300, 0x34F, 0x360, 0x363, 0x483, 0x487, 0x488, 0x48A, 0x591, + 0x5A2, 0x5A3, 0x5BA, 0x5BB, 0x5BE, 0x5BF, 0x5C0, 0x5C1, 0x5C3, 0x5C4, + 0x5C5, 0x64B, 0x656, 0x670, 0x671, 0x6D6, 0x6E5, 0x6E7, 0x6E9, 0x6EA, + 0x6EE, 0x70F, 0x710, 0x711, 0x712, 0x730, 0x74B, 0x7A6, 0x7B1, 0x901, + 0x903, 0x93C, 0x93D, 0x941, 0x949, 0x94D, 0x94E, 0x951, 0x955, 0x962, + 0x964, 0x981, 0x982, 0x9BC, 0x9BD, 0x9C1, 0x9C5, 0x9CD, 0x9CE, 0x9E2, + 0x9E4, 0xA02, 0xA03, 0xA3C, 0xA3D, 0xA41, 0xA43, 0xA47, 0xA49, 0xA4B, + 0xA4E, 0xA70, 0xA72, 0xA81, 0xA83, 0xABC, 0xABD, 0xAC1, 0xAC6, 0xAC7, + 0xAC9, 0xACD, 0xACE, 0xB01, 0xB02, 0xB3C, 0xB3D, 0xB3F, 0xB40, 0xB41, + 0xB44, 0xB4D, 0xB4E, 0xB56, 0xB57, 0xB82, 0xB83, 0xBC0, 0xBC1, 0xBCD, + 0xBCE, 0xC3E, 0xC41, 0xC46, 0xC49, 0xC4A, 0xC4E, 0xC55, 0xC57, 0xCBF, + 0xCC0, 0xCC6, 0xCC7, 0xCCC, 0xCCE, 0xD41, 0xD44, 0xD4D, 0xD4E, 0xDCA, + 0xDCB, 0xDD2, 0xDD5, 0xDD6, 0xDD7, 0xE31, 0xE32, 0xE34, 0xE3B, 0xE47, + 0xE4F, 0xEB1, 0xEB2, 0xEB4, 0xEBA, 0xEBB, 0xEBD, 0xEC8, 0xECE, 0xF18, + 0xF1A, 0xF35, 0xF36, 0xF37, 0xF38, 0xF39, 0xF3A, 0xF71, 0xF7F, 0xF80, + 0xF85, 0xF86, 0xF88, 0xF90, 0xF98, 0xF99, 0xFBD, 0xFC6, 0xFC7, 0x102D, + 0x1031, 0x1032, 0x1033, 0x1036, 0x1038, 0x1039, 0x103A, 0x1058, 0x105A, + 0x1100, 0x1160, 0x17B7, 0x17BE, 0x17C6, 0x17C7, 0x17C9, 0x17D4, 0x180B, + 0x180F, 0x18A9, 0x18AA, 0x200B, 0x2010, 0x202A, 0x202F, 0x206A, 0x2070, + 0x20D0, 0x20E4, 0x2E80, 0x3008, 0x300C, 0x3014, 0x3016, 0x3018, 0x301C, + 0x302A, 0x3030, 0x303F, 0x3041, 0x3095, 0x3099, 0x309B, 0xA4C7, 0xAC00, + 0xD7A4, 0xF8F0, 0xF900, 0xFA2E, 0xFB1E, 0xFB1F, 0xFE20, 0xFE24, 0xFE30, + 0xFE6C, 0xFEFF, 0xFF00, 0xFF01, 0xFF5F, 0xFFE0, 0xFFE7, 0xFFF9, 0xFFFC, +#if 1 + 0x1D167, 0x1D16A, 0x1D173, 0x1D183, 0x1D185, 0x1D18C, 0x1D1AA, 0x1D1AE, + 0x20000, 0x2A6D7, 0x2F800, 0x2FA1E, 0xE0001, 0xE0002, 0xE0020, 0xE0080 +#endif +}; + +static const int wccnum = sizeof(wcc) / sizeof(wcc[0]) - 1; + +static const char wws[] = { + 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, + 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, + 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, + 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, + 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, + 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, + 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, + 1, 2, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 1, 2, + 1, 2, 1, 2, 0, 2, 1, 2, 1, 0, 2, 1, 2, 1, 0, 2, 1, 0, 1, 0, 1, 2, 1, 0, + 1, 2, 1, 2, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 2, 1, 2, 1, 0, 1, 0, 1, -1 +}; + +int utf8_width(unichar c) +{ + int p, q, r; + unichar d; + + if (c < wcc[1]) + return 1; + + p = 0; + q = wccnum; + + while (p < q - 1) { + r = (p + q)/2; + d = wcc[r]; + if (d < c) + p = r; + else if (d > c) + q = r; + else + return wws[r]; + } + + return wws[p]; +} diff --git a/src/fe-text/utf8.h b/src/fe-text/utf8.h index efc6af08..64c0e347 100644 --- a/src/fe-text/utf8.h +++ b/src/fe-text/utf8.h @@ -18,4 +18,7 @@ int utf16_char_to_utf8(unichar c, char *outbuf); Make sure out is at least 6 x length of str. */ void utf16_to_utf8(const unichar *str, char *out); +/* Returns width for character (0-2). */ +int utf8_width(unichar c); + #endif