static unsigned text_width(char *utf8) { unsigned short utf16[strlen(utf8) + 1]; utf8_to_utf16(utf8, utf16); unsigned i, w = 0; for (i = 0; utf16[i]; i++, w++) { if (is_double_width(utf16[i])) w++; } return w; }
static unsigned int text_width(char* str) { if (iconvW == NULL) iconvW = iconv_open("ucs-4be", "utf-8"); if (iconvW == (iconv_t) -1) { return utf8_strlen(str); } else { size_t len = strlen(str); size_t charlen = utf8_strlen(str); unsigned *wmessage; size_t wlen = (len + 1) * sizeof (unsigned); wmessage = (unsigned *) fcitx_malloc0 ((len + 1) * sizeof (unsigned)); char *inp = str; char *outp = (char*) wmessage; iconv(iconvW, &inp, &len, &outp, &wlen); int i = 0; int width = 0; for (i = 0; i < charlen; i ++) { if (is_double_width(wmessage[i])) width += 2; else width += 1; } free(wmessage); return width; } }