comparison console/libgnt/gntutils.c @ 14419:b50aa149e09d

[gaim-migrate @ 17127] This fixes titles containing double-column wide characters for me. Editing of double-column characters in gntentry remains a bit bustinated (though it is my recollection that it worked at one time). This additionally does not require wcwidth() and its attendant funkiness. The version of gnt_util_onscreen_width_to_pointer in this commit *appears* to do what the extant version was supposed to do, though I'm not sure I grokked it fully; it does correct the rendering glitches for me. I'm not at all comfortable with the de-constification of the input string and attendant return of the de-constified pointer. Sadrul will have to let me know what is supposed to happen there. Output on non-UTF-8 terminals is not correct, but was not correct before, either. committer: Tailor Script <tailor@pidgin.im>
author Ethan Blanton <elb@pidgin.im>
date Sat, 02 Sep 2006 16:55:32 +0000
parents 010d8433db81
children c4a32405af68
comparison
equal deleted inserted replaced
14418:010d8433db81 14419:b50aa149e09d
2 2
3 #include <stdlib.h> 3 #include <stdlib.h>
4 #include <string.h> 4 #include <string.h>
5 5
6 #include "config.h" 6 #include "config.h"
7
8 #ifndef HAVE_WCWIDTH
9 #define wcwidth(X) 1
10 #else
11 #define __USE_XOPEN
12 #endif
13
14 #include <wchar.h>
15 7
16 void gnt_util_get_text_bound(const char *text, int *width, int *height) 8 void gnt_util_get_text_bound(const char *text, int *width, int *height)
17 { 9 {
18 const char *s = text, *last; 10 const char *s = text, *last;
19 int count = 1, max = 0; 11 int count = 1, max = 0;
47 *width = max + (count > 1); 39 *width = max + (count > 1);
48 } 40 }
49 41
50 int gnt_util_onscreen_width(const char *start, const char *end) 42 int gnt_util_onscreen_width(const char *start, const char *end)
51 { 43 {
52 wchar_t wch;
53 int size;
54 int width = 0; 44 int width = 0;
55 45
56 while (start < end) { 46 while (start < end) {
57 if ((size = mbtowc(&wch, start, end - start)) > 0) { 47 width += g_unichar_iswide(g_utf8_get_char(start)) ? 2 : 1;
58 start += size; 48 start = g_utf8_next_char(start);
59 width += wcwidth(wch);
60 } else {
61 ++width;
62 ++start;
63 }
64 } 49 }
65 return width; 50 return width;
66 } 51 }
67 52
68 char *gnt_util_onscreen_width_to_pointer(const char *string, int len, int *w) 53 char *gnt_util_onscreen_width_to_pointer(const char *string, int len, int *w)
69 { 54 {
70 wchar_t wch;
71 int size; 55 int size;
72 int width = 0; 56 int width = 0;
73 char *str = (char*)string; 57 char *str = (char*)string;
74 int slen = strlen(string); /* Yeah, no. of bytes */
75 58
76 while (width < len && *str) { 59 while (width < len && *str) {
77 if ((size = mbtowc(&wch, str, slen)) > 0) { 60 size = g_unichar_iswide(g_utf8_get_char(str)) ? 2 : 1;
78 if (width + wcwidth(wch) > len) 61 if (width + size > len)
79 break; 62 break;
80 str += size; 63 str = g_utf8_next_char(str);
81 width += wcwidth(wch); 64 width += size;
82 slen -= size;
83 } else {
84 ++str;
85 ++width;
86 --slen;
87 }
88 } 65 }
89
90 if (w) 66 if (w)
91 *w = width; 67 *w = width;
92
93 return str; 68 return str;
94 } 69 }
95 70