home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / anwor032.zip / antiword.0.32 / utf8.c < prev    next >
C/C++ Source or Header  |  2001-09-12  |  8KB  |  274 lines

  1. /*
  2.  * utf-8.c
  3.  * Copyright (C) 2001 A.J. van Os; Released under GPL
  4.  *
  5.  *====================================================================
  6.  * This part of the software is based on:
  7.  * An implementation of wcwidth() as defined in
  8.  * "The Single UNIX Specification, Version 2, The Open Group, 1997"
  9.  * <http://www.UNIX-systems.org/online.html>
  10.  * Markus Kuhn -- 2001-01-12 -- public domain
  11.  *====================================================================
  12.  * The credit should go to him, but all the bugs are mine. 
  13.  */
  14.  
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include "antiword.h"
  18.  
  19. struct interval {
  20.     unsigned short    first;
  21.     unsigned short    last;
  22. };
  23. /* Sorted list of non-overlapping intervals of non-spacing characters */
  24. static const struct interval combining[] = {
  25.     { 0x0300, 0x034E }, { 0x0360, 0x0362 }, { 0x0483, 0x0486 },
  26.     { 0x0488, 0x0489 }, { 0x0591, 0x05A1 }, { 0x05A3, 0x05B9 },
  27.     { 0x05BB, 0x05BD }, { 0x05BF, 0x05BF }, { 0x05C1, 0x05C2 },
  28.     { 0x05C4, 0x05C4 }, { 0x064B, 0x0655 }, { 0x0670, 0x0670 },
  29.     { 0x06D6, 0x06E4 }, { 0x06E7, 0x06E8 }, { 0x06EA, 0x06ED },
  30.     { 0x070F, 0x070F }, { 0x0711, 0x0711 }, { 0x0730, 0x074A },
  31.     { 0x07A6, 0x07B0 }, { 0x0901, 0x0902 }, { 0x093C, 0x093C },
  32.     { 0x0941, 0x0948 }, { 0x094D, 0x094D }, { 0x0951, 0x0954 },
  33.     { 0x0962, 0x0963 }, { 0x0981, 0x0981 }, { 0x09BC, 0x09BC },
  34.     { 0x09C1, 0x09C4 }, { 0x09CD, 0x09CD }, { 0x09E2, 0x09E3 },
  35.     { 0x0A02, 0x0A02 }, { 0x0A3C, 0x0A3C }, { 0x0A41, 0x0A42 },
  36.     { 0x0A47, 0x0A48 }, { 0x0A4B, 0x0A4D }, { 0x0A70, 0x0A71 },
  37.     { 0x0A81, 0x0A82 }, { 0x0ABC, 0x0ABC }, { 0x0AC1, 0x0AC5 },
  38.     { 0x0AC7, 0x0AC8 }, { 0x0ACD, 0x0ACD }, { 0x0B01, 0x0B01 },
  39.     { 0x0B3C, 0x0B3C }, { 0x0B3F, 0x0B3F }, { 0x0B41, 0x0B43 },
  40.     { 0x0B4D, 0x0B4D }, { 0x0B56, 0x0B56 }, { 0x0B82, 0x0B82 },
  41.     { 0x0BC0, 0x0BC0 }, { 0x0BCD, 0x0BCD }, { 0x0C3E, 0x0C40 },
  42.     { 0x0C46, 0x0C48 }, { 0x0C4A, 0x0C4D }, { 0x0C55, 0x0C56 },
  43.     { 0x0CBF, 0x0CBF }, { 0x0CC6, 0x0CC6 }, { 0x0CCC, 0x0CCD },
  44.     { 0x0D41, 0x0D43 }, { 0x0D4D, 0x0D4D }, { 0x0DCA, 0x0DCA },
  45.     { 0x0DD2, 0x0DD4 }, { 0x0DD6, 0x0DD6 }, { 0x0E31, 0x0E31 },
  46.     { 0x0E34, 0x0E3A }, { 0x0E47, 0x0E4E }, { 0x0EB1, 0x0EB1 },
  47.     { 0x0EB4, 0x0EB9 }, { 0x0EBB, 0x0EBC }, { 0x0EC8, 0x0ECD },
  48.     { 0x0F18, 0x0F19 }, { 0x0F35, 0x0F35 }, { 0x0F37, 0x0F37 },
  49.     { 0x0F39, 0x0F39 }, { 0x0F71, 0x0F7E }, { 0x0F80, 0x0F84 },
  50.     { 0x0F86, 0x0F87 }, { 0x0F90, 0x0F97 }, { 0x0F99, 0x0FBC },
  51.     { 0x0FC6, 0x0FC6 }, { 0x102D, 0x1030 }, { 0x1032, 0x1032 },
  52.     { 0x1036, 0x1037 }, { 0x1039, 0x1039 }, { 0x1058, 0x1059 },
  53.     { 0x1160, 0x11FF }, { 0x17B7, 0x17BD }, { 0x17C6, 0x17C6 },
  54.     { 0x17C9, 0x17D3 }, { 0x180B, 0x180E }, { 0x18A9, 0x18A9 },
  55.     { 0x200B, 0x200F }, { 0x202A, 0x202E }, { 0x206A, 0x206F },
  56.     { 0x20D0, 0x20E3 }, { 0x302A, 0x302F }, { 0x3099, 0x309A },
  57.     { 0xFB1E, 0xFB1E }, { 0xFE20, 0xFE23 }, { 0xFEFF, 0xFEFF },
  58.     { 0xFFF9, 0xFFFB }
  59. };
  60.  
  61. /* Auxiliary function for binary search in interval table */
  62. static BOOL
  63. bIsZeroWidthChar(unsigned long ucs)
  64. {
  65.     int low = 0;
  66.     int high = elementsof(combining) - 1;
  67.     int mid;
  68.  
  69.     if (ucs < combining[low].first || ucs > combining[high].last) {
  70.         return FALSE;
  71.     }
  72.  
  73.     while (high >= low) {
  74.         mid = (low + high) / 2;
  75.         if (ucs > combining[mid].last) {
  76.             low = mid + 1;
  77.         } else if (ucs < combining[mid].first) {
  78.             high = mid - 1;
  79.         } else {
  80.             return TRUE;
  81.         }
  82.     }
  83.     return FALSE;
  84. } /* end of bIsZeroWidthChar */
  85.  
  86. /* The following functions define the column width of an ISO 10646
  87.  * character as follows:
  88.  *
  89.  *    - The null character (U+0000) has a column width of 0.
  90.  *
  91.  *    - Other C0/C1 control characters and DEL will lead to a return
  92.  *      value of -1.
  93.  *
  94.  *    - Non-spacing and enclosing combining characters (general
  95.  *      category code Mn or Me in the Unicode database) have a
  96.  *      column width of 0.
  97.  *
  98.  *    - Other format characters (general category code Cf in the Unicode
  99.  *      database) and ZERO WIDTH SPACE (U+200B) have a column width of 0.
  100.  *
  101.  *    - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF)
  102.  *      have a column width of 0.
  103.  *
  104.  *    - Spacing characters in the East Asian Wide (W) or East Asian
  105.  *      FullWidth (F) category as defined in Unicode Technical
  106.  *      Report #11 have a column width of 2.
  107.  *
  108.  *    - All remaining characters (including all printable
  109.  *      ISO 8859-1 and WGL4 characters, Unicode control characters,
  110.  *      etc.) have a column width of 1.
  111.  *
  112.  * This implementation assumes that all characters are encoded
  113.  * in ISO 10646.
  114.  */
  115. int
  116. wcwidth(unsigned long ucs)
  117. {
  118.     /* Test for 8-bit control characters */
  119.     if (ucs == 0) {
  120.         return 0;
  121.     }
  122.     if (ucs < 0x20 || (ucs >= 0x7f && ucs < 0xa0)) {
  123.         return -1;
  124.     }
  125.  
  126.     /* Binary search in table of non-spacing characters */
  127.     if (bIsZeroWidthChar(ucs)) {
  128.         return 0;
  129.     }
  130.  
  131.     /* Ucs is not a combining or C0/C1 control character */
  132.  
  133.     return 1 + 
  134.     (ucs >= 0x1100 &&
  135.      (ucs <= 0x115f ||                    /* Hangul Jamo init. consonants */
  136.       (ucs >= 0x2e80 && ucs <= 0xa4cf && (ucs & ~0x0011) != 0x300a &&
  137.        ucs != 0x303f) ||                  /* CJK ... Yi */
  138.       (ucs >= 0xac00 && ucs <= 0xd7a3) || /* Hangul Syllables */
  139.       (ucs >= 0xf900 && ucs <= 0xfaff) || /* CJK Compatibility Ideographs */
  140.       (ucs >= 0xfe30 && ucs <= 0xfe6f) || /* CJK Compatibility Forms */
  141.       (ucs >= 0xff00 && ucs <= 0xff5f) || /* Fullwidth Forms */
  142.       (ucs >= 0xffe0 && ucs <= 0xffe6) ||
  143.       (ucs >= 0x20000 && ucs <= 0x2ffff)));
  144. } /* end of wcwidth */
  145.  
  146. #if 0
  147. int
  148. wcswidth(const unsigned long *pwcs, size_t n)
  149. {
  150.     int w, width = 0;
  151.  
  152.     for (;*pwcs && n-- > 0; pwcs++) {
  153.     w = wcwidth(*pwcs);
  154.     if (w < 0) {
  155.     return -1;
  156.     }
  157.     width += w;
  158.     }
  159.     return width;
  160. } /* end of wcswidth */
  161. #endif
  162.  
  163. static unsigned long
  164. utf8_to_ucs(const char *p, int *utflen)
  165. {
  166.     unsigned long wc = (unsigned long)(unsigned char)p[0];
  167.     int charlen;
  168.     int j;
  169.  
  170.     if (wc < 0x80) {
  171.         *utflen = 1;
  172.         return wc;
  173.     }
  174.  
  175.     if (wc < 0xe0){
  176.         charlen = 2;
  177.         wc &= 0x1f;
  178.     } else if (wc < 0xf0){
  179.         charlen = 3;
  180.         wc &= 0x0f;
  181.     } else if (wc < 0xf8){
  182.         charlen = 4;
  183.         wc &= 0x07;
  184.     } else if (wc < 0xfc){
  185.         charlen = 5;
  186.         wc &= 0x03;
  187.     } else {
  188.         charlen = 6;
  189.         wc &= 0x01;
  190.     }
  191.     for (j = 1; j < charlen; j++) {
  192.         wc <<= 6;
  193.         wc |= (unsigned char)p[j] & 0x3f;
  194.     }
  195.     *utflen = charlen;
  196.     return wc;
  197. } /* end of utf8_to_ucs */
  198.  
  199. long
  200. utf8_strwidth(const char *p, int numchars)
  201. {
  202.     const char    *maxp;
  203.     unsigned long    ucs;
  204.     long    totlen;
  205.     int    utflen;
  206.  
  207.     totlen = 0;
  208.     maxp = p + numchars;
  209.  
  210.     while (*p != '\0' && p < maxp) {
  211.         ucs = utf8_to_ucs(p, &utflen);
  212.         totlen += wcwidth(ucs);
  213.         p += utflen;
  214.     }
  215.     return totlen;
  216. } /* end of utf8_strwidth */
  217.  
  218. /*
  219.  * Original version:
  220.  * Copyright (C) 1999  Bruno Haible
  221.  */
  222. BOOL
  223. is_locale_utf8(void)
  224. {
  225.     const char    *locale, *cp, *encoding;
  226.  
  227.     /*
  228.      * Determine the current locale the same way as setlocale() does,
  229.      * according to POSIX.
  230.      */
  231.     locale = getenv("LC_ALL");
  232.     if (locale == NULL || locale[0] == '\0') {
  233.         locale = getenv("LC_CTYPE");
  234.         if (locale == NULL || locale[0] == '\0') {
  235.             locale = getenv("LANG");
  236.         }
  237.     }
  238.  
  239.     if (locale == NULL || locale[0] == '\0') {
  240.         return FALSE;
  241.     }
  242.  
  243.     /* The most general syntax of a locale (not all optional parts
  244.      * recognized by all systems) is
  245.      * language[_territory][.codeset][@modifier][+special][,[sponsor][_revision]]
  246.      * To retrieve the codeset, search the first dot. Stop searching when
  247.      * a '@' or '+' or ',' is encountered.
  248.      */
  249.     for (cp = locale;
  250.          *cp != '\0' && *cp != '@' && *cp != '+' && *cp != ',';
  251.          cp++) {
  252.         if (*cp != '.') {
  253.             continue;
  254.         }
  255.         encoding = cp + 1;
  256.         for (cp = encoding;
  257.              *cp != '\0' && *cp != '@' && *cp != '+' && *cp != ',';
  258.              cp++)
  259.             ;    /* EMPTY */
  260.         /*
  261.          * The encoding is now contained in the part from encoding to
  262.          * cp. Check it for "UTF-8", which is the only official IANA
  263.          * name of UTF-8. Also check for the lowercase-no-dashes
  264.          * version, which is what some SystemV systems use.
  265.          */
  266.         if ((cp - encoding == 5 && STRNEQ(encoding, "UTF-8", 5)) ||
  267.             (cp - encoding == 4 && STRNEQ(encoding, "utf8", 4))) {
  268.             return TRUE;
  269.         }
  270.         return FALSE;
  271.     }
  272.     return FALSE;
  273. } /* end of is_locale_utf8 */
  274.