61d7dac70dbcbb3ef4774b34c2c0189a8a914c29
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "charset.h"
22 #include <assert.h>
23 #include <string.h>
24 #include <glib.h>
26 #ifdef ENABLE_LOCALE
27 static bool noconvert = true;
28 static const char *charset;
30 const char *
31 charset_init(void)
32 {
33 noconvert = g_get_charset(&charset);
34 return charset;
35 }
36 #endif
38 #ifdef HAVE_CURSES_ENHANCED
39 static inline unsigned
40 unicode_char_width(gunichar ch)
41 {
42 if (g_unichar_iszerowidth(ch))
43 return 0;
45 if (g_unichar_iswide(ch))
46 return 2;
48 return 1;
49 }
50 #endif /* HAVE_CURSES_ENHANCED */
52 unsigned
53 utf8_width(const char *str)
54 {
55 assert(str != NULL);
57 #if defined(ENABLE_MULTIBYTE) && !defined(HAVE_CURSES_ENHANCED)
58 return g_utf8_strlen(str, -1);
59 #else
60 #ifdef HAVE_CURSES_ENHANCED
61 if (g_utf8_validate(str, -1, NULL)) {
62 size_t len = g_utf8_strlen(str, -1);
63 unsigned width = 0;
64 gunichar c;
66 while (len--) {
67 c = g_utf8_get_char(str);
68 width += unicode_char_width(c);
69 str += g_unichar_to_utf8(c, NULL);
70 }
72 return width;
73 } else
74 #endif
75 return strlen(str);
76 #endif
77 }
79 unsigned
80 locale_width(const char *p)
81 {
82 #if defined(ENABLE_LOCALE) && defined(ENABLE_MULTIBYTE)
83 char *utf8;
84 unsigned width;
86 if (noconvert)
87 return utf8_width(p);
89 utf8 = locale_to_utf8(p);
90 width = utf8_width(utf8);
91 g_free(utf8);
93 return width;
94 #else
95 return strlen(p);
96 #endif
97 }
99 static inline unsigned
100 ascii_cut_width(char *p, unsigned max_width)
101 {
102 size_t length = strlen(p);
103 if (length <= (size_t)max_width)
104 return (unsigned)length;
106 p[max_width] = 0;
107 return max_width;
108 }
110 static inline unsigned
111 narrow_cut_width(char *p, unsigned max_width)
112 {
113 size_t length = g_utf8_strlen(p, -1);
114 if (length <= (size_t)max_width)
115 return (unsigned)length;
117 *g_utf8_offset_to_pointer(p, max_width) = 0;
118 return max_width;
119 }
121 static inline unsigned
122 wide_cut_width(char *p, unsigned max_width)
123 {
124 size_t length = g_utf8_strlen(p, -1);
125 unsigned width = 0, prev_width;
127 while (length-- > 0) {
128 gunichar c = g_utf8_get_char(p);
129 prev_width = width;
130 width += g_unichar_iswide(c) ? 2 : 1;
131 if (width > max_width) {
132 /* too wide - cut the rest off */
133 *p = 0;
134 return prev_width;
135 }
137 p += g_unichar_to_utf8(c, NULL);
138 }
140 return width;
141 }
143 unsigned
144 utf8_cut_width(char *p, unsigned max_width)
145 {
146 assert(p != NULL);
148 #ifdef HAVE_CURSES_ENHANCED
149 if (!g_utf8_validate(p, -1, NULL))
150 return ascii_cut_width(p, max_width);
152 return wide_cut_width(p, max_width);
153 #elif defined(ENABLE_MULTIBYTE) && !defined(HAVE_CURSES_ENHANCED)
154 return narrow_cut_width(p, max_width);
155 #else
156 return ascii_cut_width(p, max_width);
157 #endif
158 }
160 char *
161 utf8_to_locale(const char *utf8str)
162 {
163 #ifdef ENABLE_LOCALE
164 assert(utf8str != NULL);
166 if (noconvert)
167 return g_strdup(utf8str);
169 gchar *str = g_convert_with_fallback(utf8str, -1,
170 charset, "utf-8",
171 NULL, NULL, NULL, NULL);
172 if (str == NULL)
173 return g_strdup(utf8str);
175 return str;
176 #else
177 return g_strdup(utf8str);
178 #endif
179 }
181 char *
182 locale_to_utf8(const char *localestr)
183 {
184 #ifdef ENABLE_LOCALE
185 assert(localestr != NULL);
187 if (noconvert)
188 return g_strdup(localestr);
190 gchar *str = g_convert_with_fallback(localestr, -1,
191 "utf-8", charset,
192 NULL, NULL, NULL, NULL);
193 if (str == NULL)
194 return g_strdup(localestr);
196 return str;
197 #else
198 return g_strdup(localestr);
199 #endif
200 }
202 char *
203 replace_utf8_to_locale(char *src)
204 {
205 #ifdef ENABLE_LOCALE
206 assert(src != NULL);
208 if (noconvert)
209 return src;
211 return utf8_to_locale(src);
212 #else
213 return src;
214 #endif
215 }
218 char *
219 replace_locale_to_utf8(char *src)
220 {
221 #ifdef ENABLE_LOCALE
222 assert(src != NULL);
224 if (noconvert)
225 return src;
227 return locale_to_utf8(src);
228 #else
229 return src;
230 #endif
231 }