Mercurial > pidgin.yaz
annotate 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 |
rev | line source |
---|---|
14059
2d1b41e3cf0d
[gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff
changeset
|
1 #include "gntutils.h" |
2d1b41e3cf0d
[gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff
changeset
|
2 |
14311
fda9dc44807d
[gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14060
diff
changeset
|
3 #include <stdlib.h> |
fda9dc44807d
[gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14060
diff
changeset
|
4 #include <string.h> |
fda9dc44807d
[gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14060
diff
changeset
|
5 |
14417
64e28f46c695
[gaim-migrate @ 17125]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14366
diff
changeset
|
6 #include "config.h" |
64e28f46c695
[gaim-migrate @ 17125]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14366
diff
changeset
|
7 |
14059
2d1b41e3cf0d
[gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff
changeset
|
8 void gnt_util_get_text_bound(const char *text, int *width, int *height) |
2d1b41e3cf0d
[gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff
changeset
|
9 { |
2d1b41e3cf0d
[gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff
changeset
|
10 const char *s = text, *last; |
2d1b41e3cf0d
[gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff
changeset
|
11 int count = 1, max = 0; |
2d1b41e3cf0d
[gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff
changeset
|
12 int len; |
2d1b41e3cf0d
[gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff
changeset
|
13 |
2d1b41e3cf0d
[gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff
changeset
|
14 /* XXX: ew ... everyone look away */ |
2d1b41e3cf0d
[gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff
changeset
|
15 last = s; |
14060
81648c6aa918
[gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14059
diff
changeset
|
16 if (s) |
14059
2d1b41e3cf0d
[gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff
changeset
|
17 { |
14060
81648c6aa918
[gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14059
diff
changeset
|
18 while (*s) |
14059
2d1b41e3cf0d
[gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff
changeset
|
19 { |
14060
81648c6aa918
[gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14059
diff
changeset
|
20 if (*s == '\n' || *s == '\r') |
81648c6aa918
[gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14059
diff
changeset
|
21 { |
81648c6aa918
[gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14059
diff
changeset
|
22 count++; |
81648c6aa918
[gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14059
diff
changeset
|
23 len = g_utf8_pointer_to_offset(last, s); |
81648c6aa918
[gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14059
diff
changeset
|
24 if (max < len) |
81648c6aa918
[gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14059
diff
changeset
|
25 max = len; |
81648c6aa918
[gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14059
diff
changeset
|
26 last = s + 1; |
81648c6aa918
[gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14059
diff
changeset
|
27 } |
81648c6aa918
[gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14059
diff
changeset
|
28 s++; |
14059
2d1b41e3cf0d
[gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff
changeset
|
29 } |
14060
81648c6aa918
[gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14059
diff
changeset
|
30 |
81648c6aa918
[gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14059
diff
changeset
|
31 len = g_utf8_pointer_to_offset(last, s); |
81648c6aa918
[gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14059
diff
changeset
|
32 if (max < len) |
81648c6aa918
[gaim-migrate @ 16678]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14059
diff
changeset
|
33 max = len; |
14059
2d1b41e3cf0d
[gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff
changeset
|
34 } |
2d1b41e3cf0d
[gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff
changeset
|
35 |
2d1b41e3cf0d
[gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff
changeset
|
36 if (height) |
2d1b41e3cf0d
[gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff
changeset
|
37 *height = count; |
2d1b41e3cf0d
[gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff
changeset
|
38 if (width) |
2d1b41e3cf0d
[gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff
changeset
|
39 *width = max + (count > 1); |
2d1b41e3cf0d
[gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff
changeset
|
40 } |
2d1b41e3cf0d
[gaim-migrate @ 16674]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
diff
changeset
|
41 |
14311
fda9dc44807d
[gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14060
diff
changeset
|
42 int gnt_util_onscreen_width(const char *start, const char *end) |
fda9dc44807d
[gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14060
diff
changeset
|
43 { |
fda9dc44807d
[gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14060
diff
changeset
|
44 int width = 0; |
fda9dc44807d
[gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14060
diff
changeset
|
45 |
fda9dc44807d
[gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14060
diff
changeset
|
46 while (start < end) { |
14419 | 47 width += g_unichar_iswide(g_utf8_get_char(start)) ? 2 : 1; |
48 start = g_utf8_next_char(start); | |
14311
fda9dc44807d
[gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14060
diff
changeset
|
49 } |
fda9dc44807d
[gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14060
diff
changeset
|
50 return width; |
fda9dc44807d
[gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14060
diff
changeset
|
51 } |
fda9dc44807d
[gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14060
diff
changeset
|
52 |
fda9dc44807d
[gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14060
diff
changeset
|
53 char *gnt_util_onscreen_width_to_pointer(const char *string, int len, int *w) |
fda9dc44807d
[gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14060
diff
changeset
|
54 { |
fda9dc44807d
[gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14060
diff
changeset
|
55 int size; |
fda9dc44807d
[gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14060
diff
changeset
|
56 int width = 0; |
fda9dc44807d
[gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14060
diff
changeset
|
57 char *str = (char*)string; |
fda9dc44807d
[gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14060
diff
changeset
|
58 |
fda9dc44807d
[gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14060
diff
changeset
|
59 while (width < len && *str) { |
14419 | 60 size = g_unichar_iswide(g_utf8_get_char(str)) ? 2 : 1; |
61 if (width + size > len) | |
62 break; | |
63 str = g_utf8_next_char(str); | |
64 width += size; | |
14311
fda9dc44807d
[gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14060
diff
changeset
|
65 } |
fda9dc44807d
[gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14060
diff
changeset
|
66 if (w) |
fda9dc44807d
[gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14060
diff
changeset
|
67 *w = width; |
fda9dc44807d
[gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14060
diff
changeset
|
68 return str; |
fda9dc44807d
[gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14060
diff
changeset
|
69 } |
fda9dc44807d
[gaim-migrate @ 17001]
Sadrul Habib Chowdhury <imadil@gmail.com>
parents:
14060
diff
changeset
|
70 |