X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/blobdiff_plain/61507e3ceb97b0b99e8283ef2573ca738001a930..6ebc4527c6a103d0532c08744fb916f951018413:/lib/charset.c diff --git a/lib/charset.c b/lib/charset.c index 3f0e8bc..fdc6d61 100644 --- a/lib/charset.c +++ b/lib/charset.c @@ -1,29 +1,25 @@ /* * This file is part of DisOrder. - * Copyright (C) 2004, 2005 Richard Kettlewell + * Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell * - * This program is free software; you can redistribute it and/or modify + * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or + * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 - * USA + * along with this program. If not, see . */ /** @file lib/charset.c @brief Character set conversion */ -#include -#include "types.h" +#include "common.h" #include -#include #include #include @@ -31,9 +27,8 @@ #include "log.h" #include "charset.h" #include "configuration.h" -#include "utf8.h" #include "vector.h" -#include "unidata.h" +#include "unicode.h" /** @brief Low-level converstion routine * @param from Source encoding @@ -50,7 +45,7 @@ static void *convert(const char *from, const char *to, size_t bufsize = 0, sl, dl; if((i = iconv_open(to, from)) == (iconv_t)-1) - fatal(errno, "error calling iconv_open"); + disorder_fatal(errno, "error calling iconv_open"); do { bufsize = bufsize ? 2 * bufsize : 32; buf = xrealloc_noptr(buf, bufsize); @@ -64,102 +59,53 @@ static void *convert(const char *from, const char *to, } while(len == (size_t)-1 && errno == E2BIG); iconv_close(i); if(len == (size_t)-1) { - error(errno, "error converting from %s to %s", from, to); + disorder_error(errno, "error converting from %s to %s", from, to); return 0; } return buf; } -/** @brief Convert UTF-8 to UCS-4 - * @param mb Pointer to 0-terminated UTF-8 string - * @return Pointer to 0-terminated UCS-4 string - * - * Not everybody's iconv supports UCS-4, and it's inconvenient to have to know - * our endianness, and it's easy to convert it ourselves, so we do. See also - * @ref ucs42utf8(). - */ -uint32_t *utf82ucs4(const char *mb) { - struct dynstr_ucs4 d; - uint32_t c; - - dynstr_ucs4_init(&d); - while(*mb) { - PARSE_UTF8(mb, c, - error(0, "invalid UTF-8 sequence"); return 0;); - dynstr_ucs4_append(&d, c); - } - dynstr_ucs4_terminate(&d); - return d.vec; -} - -/** @brief Convert one UCS-4 character to UTF-8 - * @param c Character to convert - * @param d Dynamic string to append UTF-8 sequence to - * @return 0 on success, -1 on error - */ -int one_ucs42utf8(uint32_t c, struct dynstr *d) { - if(c < 0x80) - dynstr_append(d, c); - else if(c < 0x800) { - dynstr_append(d, 0xC0 | (c >> 6)); - dynstr_append(d, 0x80 | (c & 0x3F)); - } else if(c < 0x10000) { - dynstr_append(d, 0xE0 | (c >> 12)); - dynstr_append(d, 0x80 | ((c >> 6) & 0x3F)); - dynstr_append(d, 0x80 | (c & 0x3F)); - } else if(c < 0x110000) { - dynstr_append(d, 0xF0 | (c >> 18)); - dynstr_append(d, 0x80 | ((c >> 12) & 0x3F)); - dynstr_append(d, 0x80 | ((c >> 6) & 0x3F)); - dynstr_append(d, 0x80 | (c & 0x3F)); - } else { - error(0, "invalid UCS-4 character %#"PRIx32, c); - return -1; - } - return 0; -} - -/** @brief Convert UCS-4 to UTF-8 - * @param u Pointer to 0-terminated UCS-4 string - * @return Pointer to 0-terminated UTF-8 string - * - * See @ref utf82ucs4(). +/** @brief Convert from the local multibyte encoding to UTF-8 + * @param mb String in current locale's multibyte encoding + * @return Same string in UTF-8 */ -char *ucs42utf8(const uint32_t *u) { - struct dynstr d; - uint32_t c; - - dynstr_init(&d); - while((c = *u++)) { - if(one_ucs42utf8(c, &d)) - return 0; - } - dynstr_terminate(&d); - return d.vec; -} - -/** @brief Convert from the local multibyte encoding to UTF-8 */ char *mb2utf8(const char *mb) { return convert(nl_langinfo(CODESET), "UTF-8", mb, strlen(mb) + 1); } -/** @brief Convert from UTF-8 to the local multibyte encoding */ +/** @brief Convert from UTF-8 to the local multibyte encoding + * @param utf8 String in UTF-8 + * @return Same string in current locale's multibyte encoding + */ char *utf82mb(const char *utf8) { return convert("UTF-8", nl_langinfo(CODESET), utf8, strlen(utf8) + 1); } -/** @brief Convert from encoding @p from to UTF-8 */ +/** @brief Convert from encoding @p from to UTF-8 + * @param from Source encoding + * @param any String in encoding @p from + * @return @p any converted to UTF-8 + */ char *any2utf8(const char *from, const char *any) { return convert(from, "UTF-8", any, strlen(any) + 1); } -/** @brief Convert from encoding @p from to the local multibyte encoding */ +/** @brief Convert from encoding @p from to the local multibyte encoding + * @param from Source encoding + * @param any String in encoding @p from + * @return @p any converted to current locale's multibyte encoding + */ char *any2mb(const char *from, const char *any) { if(from) return convert(from, nl_langinfo(CODESET), any, strlen(any) + 1); else return xstrdup(any); } -/** @brief Convert from encoding @p from to encoding @p to */ +/** @brief Convert from encoding @p from to encoding @p to + * @param from Source encoding + * @param to Destination encoding + * @param any String in encoding @p from + * @return @p any converted to encoding @p to + */ char *any2any(const char *from, const char *to, const char *any) { @@ -167,80 +113,52 @@ char *any2any(const char *from, else return xstrdup(any); } -/** @brief strlen workalike for UCS-4 strings - * - * We don't rely on the local @c wchar_t being UCS-4. - */ -int ucs4cmp(const uint32_t *a, const uint32_t *b) { - while(*a && *b && *a == *b) ++a, ++b; - if(*a > *b) return 1; - else if(*a < *b) return -1; - else return 0; -} - -/** @brief Return nonzero if @p c is a combining character */ -static int combining(int c) { - if(c < UNICODE_NCHARS) { - const struct unidata *const ud = &unidata[c / 256][c % 256]; - - return ud->gc == unicode_gc_Mn || ud->ccc != 0; - } - /* Assume unknown characters are noncombining */ - return 0; -} - /** @brief Truncate a string for display purposes * @param s Pointer to UTF-8 string * @param max Maximum number of columns * @return @p or truncated string (never NULL) * - * We don't correctly support bidi or double-width characters yet, nor - * locate default grapheme cluster boundaries for saner truncation. + * Returns a string that is no longer than @p max graphemes long and is either + * (canonically) equal to @p s or is a truncated form of it with an ellipsis + * appended. + * + * We don't take display width into account (tricky for HTML!) and we don't + * attempt to implement the Bidi algorithm. If you have track names for which + * either of these matter in practice then get in touch. */ const char *truncate_for_display(const char *s, long max) { - const char *t = s, *r, *cut = 0; - char *truncated; - uint32_t c; - long n = 0; + uint32_t *s32; + size_t l32, cut; + utf32_iterator it; + long graphemes; - /* We need to discover two things: firstly whether the string is - * longer than @p max glyphs and secondly if it is not, where to cut - * the string. - * - * Combining characters follow their base character (unicode - * standard 5.0 s2.11), so after each base character we must - */ - while(*t) { - PARSE_UTF8(t, c, return s); - if(combining(c)) - /* This must be an initial combining character. We just skip it. */ - continue; - /* So c must be a base character. It may be followed by any - * number of combining characters. We advance past them. */ - do { - r = t; - PARSE_UTF8(t, c, return s); - } while(combining(c)); - /* Last character wasn't a combining character so back up */ - t = r; - ++n; - /* So now there are N glyphs before position T. We might - * therefore have reached the cut position. */ - if(n == max - 3) - cut = t; + /* Convert to UTF-32 for processing */ + if(!(s32 = utf8_to_utf32(s, strlen(s), &l32))) + return 0; + it = utf32_iterator_new(s32, l32); + cut = l32; + graphemes = 0; /* # of graphemes left of it */ + while(graphemes <= max && utf32_iterator_where(it) < l32) { + if(graphemes == max - 1) + cut = utf32_iterator_where(it); + utf32_iterator_advance(it, 1); + if(utf32_iterator_grapheme_boundary(it)) + ++graphemes; + } + if(graphemes > max) { /* we need to cut */ + s32[cut] = 0x2026; /* HORIZONTAL ELLIPSIS */ + l32 = cut + 1; + s = utf32_to_utf8(s32, l32, 0); } - /* If the string is short enough we return it unmodified */ - if(n < max) - return s; - truncated = xmalloc_noptr(cut - s + 4); - memcpy(truncated, s, cut - s); - strcpy(truncated + (cut - s), "..."); - return truncated; + xfree(s32); + return s; } /* Local Variables: c-basic-offset:2 comment-column:40 +fill-column:79 +indent-tabs-mode:nil End: */