comparison console/libgnt/gntutils.c @ 14311:fda9dc44807d

[gaim-migrate @ 17001] Wide-characters should no longer eat characters at the end of the line in textview. committer: Tailor Script <tailor@pidgin.im>
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Wed, 23 Aug 2006 13:46:51 +0000
parents 81648c6aa918
children 71666c137a07
comparison
equal deleted inserted replaced
14310:a766441af5ea 14311:fda9dc44807d
1 #include "gntutils.h" 1 #include "gntutils.h"
2
3 #include <stdlib.h>
4 #include <string.h>
5 #include <wchar.h>
2 6
3 void gnt_util_get_text_bound(const char *text, int *width, int *height) 7 void gnt_util_get_text_bound(const char *text, int *width, int *height)
4 { 8 {
5 const char *s = text, *last; 9 const char *s = text, *last;
6 int count = 1, max = 0; 10 int count = 1, max = 0;
32 *height = count; 36 *height = count;
33 if (width) 37 if (width)
34 *width = max + (count > 1); 38 *width = max + (count > 1);
35 } 39 }
36 40
41 int gnt_util_onscreen_width(const char *start, const char *end)
42 {
43 wchar_t wch;
44 int size;
45 int width = 0;
46
47 while (start < end) {
48 if ((size = mbtowc(&wch, start, end - start)) > 0) {
49 start += size;
50 width += wcwidth(wch);
51 } else {
52 ++width;
53 ++start;
54 }
55 }
56 return width;
57 }
58
59 char *gnt_util_onscreen_width_to_pointer(const char *string, int len, int *w)
60 {
61 wchar_t wch;
62 int size;
63 int width = 0;
64 char *str = (char*)string;
65 int slen = strlen(string); /* Yeah, no. of bytes */
66
67 while (width < len && *str) {
68 if ((size = mbtowc(&wch, str, slen)) > 0) {
69 if (width + wcwidth(wch) > len)
70 break;
71 str += size;
72 width += wcwidth(wch);
73 slen -= size;
74 } else {
75 ++str;
76 ++width;
77 --slen;
78 }
79 }
80
81 if (w)
82 *w = width;
83
84 return str;
85 }
86