diff options
author | Kent Overstreet <koverstreet@google.com> | 2013-03-23 16:48:17 -0700 |
---|---|---|
committer | Kent Overstreet <koverstreet@google.com> | 2013-03-23 16:48:17 -0700 |
commit | b91dc51882b74d0fe572798bff1c7c549d55d2c9 (patch) | |
tree | 6f4cda2a3626a6a86cc0f84abd4f9f2c1c9efd97 | |
parent | deb79ca7df3e6ffe01e8d8b8ea264ce9ec0aeac4 (diff) |
Use fontconfig utf8 functions
-rw-r--r-- | st.c | 154 |
1 files changed, 20 insertions, 134 deletions
@@ -140,11 +140,6 @@ enum selection_type { SEL_RECTANGULAR = 2 }; -/* bit macro */ -#undef B0 -enum { B0 = 1, B1 = 2, B2 = 4, B3 = 8, B4 = 16, B5 = 32, B6 = 64, B7 = 128 -}; - typedef unsigned char uchar; typedef unsigned int uint; typedef unsigned long ulong; @@ -440,126 +435,10 @@ static void *xcalloc(size_t nmemb, size_t size) return p; } -static int utf8decode(char *s, long *u) -{ - uchar c; - int i, n, rtn; - - rtn = 1; - c = *s; - if (~c & B7) { /* 0xxxxxxx */ - *u = c; - return rtn; - } else if ((c & (B7 | B6 | B5)) == (B7 | B6)) { /* 110xxxxx */ - *u = c & (B4 | B3 | B2 | B1 | B0); - n = 1; - } else if ((c & (B7 | B6 | B5 | B4)) == (B7 | B6 | B5)) { /* 1110xxxx */ - *u = c & (B3 | B2 | B1 | B0); - n = 2; - } else if ((c & (B7 | B6 | B5 | B4 | B3)) == (B7 | B6 | B5 | B4)) { /* 11110xxx */ - *u = c & (B2 | B1 | B0); - n = 3; - } else { - goto invalid; - } - - for (i = n, ++s; i > 0; --i, ++rtn, ++s) { - c = *s; - if ((c & (B7 | B6)) != B7) /* 10xxxxxx */ - goto invalid; - *u <<= 6; - *u |= c & (B5 | B4 | B3 | B2 | B1 | B0); - } - - if ((n == 1 && *u < 0x80) || - (n == 2 && *u < 0x800) || - (n == 3 && *u < 0x10000) || (*u >= 0xD800 && *u <= 0xDFFF)) { - goto invalid; - } - - return rtn; - invalid: - *u = 0xFFFD; - - return rtn; -} - -static int utf8encode(long *u, char *s) -{ - uchar *sp; - ulong uc; - int i, n; - - sp = (uchar *) s; - uc = *u; - if (uc < 0x80) { - *sp = uc; /* 0xxxxxxx */ - return 1; - } else if (*u < 0x800) { - *sp = (uc >> 6) | (B7 | B6); /* 110xxxxx */ - n = 1; - } else if (uc < 0x10000) { - *sp = (uc >> 12) | (B7 | B6 | B5); /* 1110xxxx */ - n = 2; - } else if (uc <= 0x10FFFF) { - *sp = (uc >> 18) | (B7 | B6 | B5 | B4); /* 11110xxx */ - n = 3; - } else { - goto invalid; - } - - for (i = n, ++sp; i > 0; --i, ++sp) - *sp = ((uc >> 6 * (i - 1)) & (B5 | B4 | B3 | B2 | B1 | B0)) | B7; /* 10xxxxxx */ - - return n + 1; - invalid: - /* U+FFFD */ - *s++ = '\xEF'; - *s++ = '\xBF'; - *s = '\xBD'; - - return 3; -} - -/* use this if your buffer is less than UTF_SIZ, it returns 1 if you can decode - UTF-8 otherwise return 0 */ -static int isfullutf8(char *s, int b) -{ - uchar *c1, *c2, *c3; - - c1 = (uchar *) s; - c2 = (uchar *)++ s; - c3 = (uchar *)++ s; - if (b < 1) { - return 0; - } else if ((*c1 & (B7 | B6 | B5)) == (B7 | B6) && b == 1) { - return 0; - } else if ((*c1 & (B7 | B6 | B5 | B4)) == (B7 | B6 | B5) && - ((b == 1) || ((b == 2) && (*c2 & (B7 | B6)) == B7))) { - return 0; - } else if ((*c1 & (B7 | B6 | B5 | B4 | B3)) == (B7 | B6 | B5 | B4) - && ((b == 1) || ((b == 2) && (*c2 & (B7 | B6)) == B7) - || ((b == 3) && (*c2 & (B7 | B6)) == B7 - && (*c3 & (B7 | B6)) == B7))) { - return 0; - } else { - return 1; - } -} - static int utf8size(char *s) { - uchar c = *s; - - if (~c & B7) { - return 1; - } else if ((c & (B7 | B6 | B5)) == (B7 | B6)) { - return 2; - } else if ((c & (B7 | B6 | B5 | B4)) == (B7 | B6 | B5)) { - return 3; - } else { - return 4; - } + unsigned ucs; + return FcUtf8ToUcs4((unsigned char *) s, &ucs, UTF_SIZ); } static ushort sixd_to_16bit(int x) @@ -896,7 +775,7 @@ void xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) int frp, frcflags; int u8fl, u8fblen, u8cblen, doesexist; char *u8c, *u8fs; - long u8char; + unsigned u8char; Font *font = &dc.font; FcResult fcres; FcPattern *fcpattern, *fontpattern; @@ -1000,7 +879,8 @@ void xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) u8fl = 0; for (;;) { u8c = s; - u8cblen = utf8decode(s, &u8char); + u8cblen = FcUtf8ToUcs4((unsigned char *) s, + &u8char, UTF_SIZ); s += u8cblen; bytelen -= u8cblen; @@ -2395,12 +2275,9 @@ static void kpress(XEvent *ev) static void ttyread(void) { - static char buf[BUFSIZ]; + static unsigned char buf[BUFSIZ]; static int buflen = 0; - char *ptr; - char s[UTF_SIZ]; - int charsize; /* size of utf8 char in bytes */ - long utf8c; + unsigned char *ptr; int ret; /* append read bytes to unprocessed bytes */ @@ -2410,10 +2287,19 @@ static void ttyread(void) /* process every complete utf8 char */ buflen += ret; ptr = buf; - while (buflen >= UTF_SIZ || isfullutf8(ptr, buflen)) { - charsize = utf8decode(ptr, &utf8c); - utf8encode(&utf8c, s); - tputc(s, charsize); + + while (buflen) { + unsigned ucs; + int charsize = FcUtf8ToUcs4(ptr, &ucs, buflen); + if (charsize < 0) { + charsize = 1; + ucs = *ptr; + } + + if (charsize > buflen) + break; + + tputc((char *) ptr, charsize); ptr += charsize; buflen -= charsize; } |