johab_hangul.h
Upload User: yingmei828
Upload Date: 2007-01-01
Package Size: 1646k
Code Size: 9k
Development Platform:

Unix_Linux

  1. /*
  2.  * JOHAB Hangul
  3.  *
  4.  * Ken Lunde writes in his "CJKV Information Processing" book, p. 114:
  5.  * "Hangul can be composed of two or three jamo (some jamo are considered
  6.  *  compound). Johab uses 19 initial jamo (consonants), 21 medial jamo (vowels)
  7.  *  and 27 final jamo (consonants; 28 when you include the "fill" character
  8.  *  for Hangul containing only two jamo). Multiplying these numbers results in
  9.  *  11172."
  10.  *
  11.  * Structure of the Johab encoding (see p. 181-184):
  12.  *   bit 15 = 1
  13.  *   bit 14..10 = initial jamo, only 19+1 out of 32 possible values are used
  14.  *   bit 9..5 = medial jamo, only 21+1 out of 32 possible values are used
  15.  *   bit 4..0 = final jamo, only 27+1 out of 32 possible values are used
  16.  * 
  17.  * Structure of the Unicode encoding:
  18.  * grep '^0x([8-C]...|D[0-7]..)' unicode.org-mappings/EASTASIA/KSC/JOHAB.TXT
  19.  * You see that all characters there are marked "HANGUL LETTER" or "HANGUL
  20.  * SYLLABLE". If you eliminate the "HANGUL LETTER"s, the table is sorted
  21.  * in ascending order according to Johab encoding and according to the Unicode
  22.  * encoding. Now look a little more carefully, and you see that the following
  23.  * formula holds:
  24.  *     unicode == 0xAC00
  25.  *                + 21 * 28 * (jamo_initial_index[(johab >> 10) & 31] - 1)
  26.  *                + 28 * (jamo_medial_index[(johab >> 5) & 31] - 1)
  27.  *                + jamo_final_index[johab & 31]
  28.  * where the index tables are defined as below.
  29.  */
  30. /* Tables mapping 5-bit groups to jamo letters. */
  31. /* Note that Jamo XX = UHC 0xA4A0+XX = Unicode 0x3130+XX */
  32. #define NONE 0xfd
  33. #define FILL 0xff
  34. static const unsigned char jamo_initial[32] = {
  35.   NONE, FILL, 0x01, 0x02, 0x04, 0x07, 0x08, 0x09,
  36.   0x11, 0x12, 0x13, 0x15, 0x16, 0x17, 0x18, 0x19,
  37.   0x1a, 0x1b, 0x1c, 0x1d, 0x1e, NONE, NONE, NONE,
  38.   NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
  39. };
  40. static const unsigned char jamo_medial[32] = {
  41.   NONE, NONE, FILL, 0x1f, 0x20, 0x21, 0x22, 0x23,
  42.   NONE, NONE, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29,
  43.   NONE, NONE, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  44.   NONE, NONE, 0x30, 0x31, 0x32, 0x33, NONE, NONE,
  45. };
  46. static const unsigned char jamo_final[32] = {
  47.   NONE, FILL, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
  48.   0x07, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  49.   0x10, 0x11, NONE, 0x12, 0x14, 0x15, 0x16, 0x17,
  50.   0x18, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, NONE, NONE,
  51. };
  52. /* Same as jamo_final, except that it excludes characters already
  53.    contained in jamo_initial. 11 characters instead of 27. */
  54. static const unsigned char jamo_final_notinitial[32] = {
  55.   NONE, NONE, NONE, NONE, 0x03, NONE, 0x05, 0x06,
  56.   NONE, NONE, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  57.   0x10, NONE, NONE, NONE, 0x14, NONE, NONE, NONE,
  58.   NONE, NONE, NONE, NONE, NONE, NONE, NONE, NONE,
  59. };
  60. /* Tables mapping 5-bit groups to packed indices. */
  61. #define none -1
  62. #define fill 0
  63. static const signed char jamo_initial_index[32] = {
  64.   none, fill, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
  65.   0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
  66.   0x0f, 0x10, 0x11, 0x12, 0x13, none, none, none,
  67.   none, none, none, none, none, none, none, none,
  68. };
  69. static const signed char jamo_medial_index[32] = {
  70.   none, none, fill, 0x01, 0x02, 0x03, 0x04, 0x05,
  71.   none, none, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  72.   none, none, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
  73.   none, none, 0x12, 0x13, 0x14, 0x15, none, none,
  74. };
  75. static const signed char jamo_final_index[32] = {
  76.   none, fill, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
  77.   0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e,
  78.   0x0f, 0x10, none, 0x11, 0x12, 0x13, 0x14, 0x15,
  79.   0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, none, none,
  80. };
  81. static int
  82. johab_hangul_mbtowc (conv_t conv, wchar_t *pwc, const unsigned char *s, int n)
  83. {
  84.   unsigned char c1 = s[0];
  85.   if ((c1 >= 0x84 && c1 <= 0xd3)) {
  86.     if (n >= 2) {
  87.       unsigned char c2 = s[1];
  88.       if ((c2 >= 0x41 && c2 < 0x7f) || (c2 >= 0x81 && c2 < 0xff)) {
  89.         unsigned int johab = (c1 << 8) | c2;
  90.         unsigned int bitspart1 = (johab >> 10) & 31;
  91.         unsigned int bitspart2 = (johab >> 5) & 31;
  92.         unsigned int bitspart3 = johab & 31;
  93.         int index1 = jamo_initial_index[bitspart1];
  94.         int index2 = jamo_medial_index[bitspart2];
  95.         int index3 = jamo_final_index[bitspart3];
  96.         /* Exclude "none" values. */
  97.         if (index1 >= 0 && index2 >= 0 && index3 >= 0) {
  98.           /* Deal with "fill" values in initial or medial position. */
  99.           if (index1 == fill) {
  100.             if (index2 == fill) {
  101.               unsigned char jamo3 = jamo_final_notinitial[bitspart3];
  102.               if (jamo3 != NONE) {
  103.                 *pwc = (wchar_t) 0x3130 + jamo3;
  104.                 return 2;
  105.               }
  106.             } else if (index3 == fill) {
  107.               unsigned char jamo2 = jamo_medial[bitspart2];
  108.               if (jamo2 != NONE && jamo2 != FILL) {
  109.                 *pwc = (wchar_t) 0x3130 + jamo2;
  110.                 return 2;
  111.               }
  112.             }
  113.             /* Syllables composed only of medial and final don't exist. */
  114.           } else if (index2 == fill) {
  115.             if (index3 == fill) {
  116.               unsigned char jamo1 = jamo_initial[bitspart1];
  117.               if (jamo1 != NONE && jamo1 != FILL) {
  118.                 *pwc = (wchar_t) 0x3130 + jamo1;
  119.                 return 2;
  120.               }
  121.             }
  122.             /* Syllables composed only of initial and final don't exist. */
  123.           } else {
  124.              /* index1 and index2 are not fill, but index3 may be fill. */
  125.              /* Nothing more to exclude. All 11172 code points are valid. */
  126.              *pwc = 0xac00 + ((index1 - 1) * 21 + (index2 - 1)) * 28 + index3;
  127.              return 2;
  128.           }
  129.         }
  130.       }
  131.       return RET_ILSEQ;
  132.     }
  133.     return RET_TOOFEW(0);
  134.   }
  135.   return RET_ILSEQ;
  136. }
  137. /* 51 Jamo: 19 initial, 21 medial, 11 final not initial. */
  138. static const unsigned short johab_hangul_page31[51] = {
  139.           0x8841, 0x8c41, 0x8444, 0x9041, 0x8446, 0x8447, 0x9441, /*0x30-0x37*/
  140.   0x9841, 0x9c41, 0x844a, 0x844b, 0x844c, 0x844d, 0x844e, 0x844f, /*0x38-0x3f*/
  141.   0x8450, 0xa041, 0xa441, 0xa841, 0x8454, 0xac41, 0xb041, 0xb441, /*0x40-0x47*/
  142.   0xb841, 0xbc41, 0xc041, 0xc441, 0xc841, 0xcc41, 0xd041, 0x8461, /*0x48-0x4f*/
  143.   0x8481, 0x84a1, 0x84c1, 0x84e1, 0x8541, 0x8561, 0x8581, 0x85a1, /*0x50-0x57*/
  144.   0x85c1, 0x85e1, 0x8641, 0x8661, 0x8681, 0x86a1, 0x86c1, 0x86e1, /*0x58-0x5f*/
  145.   0x8741, 0x8761, 0x8781, 0x87a1,                                 /*0x60-0x67*/
  146. };
  147. /* Tables mapping packed indices to 5-bit groups. */
  148. /* index1+1 = jamo_initial_index[bitspart1]  <==>
  149.    bitspart1 = jamo_initial_index_inverse[index1] */
  150. static const char jamo_initial_index_inverse[19] = {
  151.               0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  152.   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  153.   0x10, 0x11, 0x12, 0x13, 0x14,
  154. };
  155. /* index2+1 = jamo_medial_index[bitspart2]  <==>
  156.    bitspart2 = jamo_medial_index_inverse[index2] */
  157. static const char jamo_medial_index_inverse[21] = {
  158.                     0x03, 0x04, 0x05, 0x06, 0x07,
  159.               0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  160.               0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  161.               0x1a, 0x1b, 0x1c, 0x1d,
  162. };
  163. /* index3 = jamo_final_index[bitspart3]  <==>
  164.    bitspart3 = jamo_final_index_inverse[index3] */
  165. static const char jamo_final_index_inverse[28] = {
  166.         0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  167.   0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  168.   0x10, 0x11,       0x13, 0x14, 0x15, 0x16, 0x17,
  169.   0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
  170. };
  171. static int
  172. johab_hangul_wctomb (conv_t conv, unsigned char *r, wchar_t wc, int n)
  173. {
  174.   if (n >= 2) {
  175.     if (wc >= 0x3131 && wc < 0x3164) {
  176.       unsigned short c = johab_hangul_page31[wc-0x3131];
  177.       r[0] = (c >> 8); r[1] = (c & 0xff);
  178.       return 2;
  179.     } else if (wc >= 0xac00 && wc < 0xd7a4) {
  180.       unsigned int index1;
  181.       unsigned int index2;
  182.       unsigned int index3;
  183.       unsigned short c;
  184.       unsigned int tmp = wc - 0xac00;
  185.       index3 = tmp % 28; tmp = tmp / 28;
  186.       index2 = tmp % 21; tmp = tmp / 21;
  187.       index1 = tmp;
  188.       c = (((((1 << 5)
  189.               | jamo_initial_index_inverse[index1]) << 5)
  190.             | jamo_medial_index_inverse[index2]) << 5)
  191.           | jamo_final_index_inverse[index3];
  192.       r[0] = (c >> 8); r[1] = (c & 0xff);
  193.       return 2;
  194.     }
  195.     return RET_ILSEQ;
  196.   }
  197.   return RET_TOOSMALL;
  198. }
  199. /*
  200.  * Decomposition of JOHAB Hangul in one to three Johab Jamo elements.
  201.  */
  202. /* Decompose wc into r[0..2], and return the number of resulting Jamo elements.
  203.    Return RET_ILSEQ if decomposition is not possible. */
  204. static int johab_hangul_decompose (conv_t conv, wchar_t* r, wchar_t wc)
  205. {
  206.   unsigned char buf[2];
  207.   int ret = johab_hangul_wctomb(conv,buf,wc,2);
  208.   if (ret != RET_ILSEQ) {
  209.     unsigned int hangul = (buf[0] << 8) | buf[1];
  210.     unsigned char jamo1 = jamo_initial[(hangul >> 10) & 31];
  211.     unsigned char jamo2 = jamo_medial[(hangul >> 5) & 31];
  212.     unsigned char jamo3 = jamo_final[hangul & 31];
  213.     if ((hangul >> 15) != 1) abort();
  214.     if (jamo1 != NONE && jamo2 != NONE && jamo3 != NONE) {
  215.       /* They are not all three == FILL because that would correspond to
  216.          johab = 0x8441, which doesn't exist. */
  217.       wchar_t* p = r;
  218.       if (jamo1 != FILL)
  219.         *p++ = 0x3130 + jamo1;
  220.       if (jamo2 != FILL)
  221.         *p++ = 0x3130 + jamo2;
  222.       if (jamo3 != FILL)
  223.         *p++ = 0x3130 + jamo3;
  224.       return p-r;
  225.     }
  226.   }
  227.   return RET_ILSEQ;
  228. }
  229. #undef fill
  230. #undef none
  231. #undef FILL
  232. #undef NONE