Back to index

im-sdk  12.3.91
hangul.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2003   Choe Hwanjin <krisna@kldp.org>
00003  *
00004  * This library is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU Library General Public
00006  * License as published by the Free Software Foundation; either
00007  * version 2 of the License, or (at your option) any later version.
00008  *
00009  * This library is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * Library General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Library General Public
00015  * License along with this library; if not, write to the
00016  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017  * Boston, MA 02111-1307, USA.
00018  */
00019 
00020 #include <stddef.h>
00021 
00022 #include "hangul.h"
00023 
00024 wchar_t
00025 hangul_choseong_to_cjamo(wchar_t ch)
00026 {
00027     static wchar_t table[] = {
00028        0x3131,           /* 0x1100 */
00029        0x3132,           /* 0x1101 */
00030        0x3134,           /* 0x1102 */
00031        0x3137,           /* 0x1103 */
00032        0x3138,           /* 0x1104 */
00033        0x3139,           /* 0x1105 */
00034        0x3141,           /* 0x1106 */
00035        0x3142,           /* 0x1107 */
00036        0x3143,           /* 0x1108 */
00037        0x3145,           /* 0x1109 */
00038        0x3146,           /* 0x110a */
00039        0x3147,           /* 0x110b */
00040        0x3148,           /* 0x110c */
00041        0x3149,           /* 0x110d */
00042        0x314a,           /* 0x110e */
00043        0x314b,           /* 0x110f */
00044        0x314c,           /* 0x1110 */
00045        0x314d,           /* 0x1111 */
00046        0x314e     /* 0x1112 */
00047     };
00048 
00049     if (ch < 0x1100 || ch > 0x1112)
00050        return 0;
00051     else
00052        return table[ch - 0x1100];
00053 }
00054 
00055 wchar_t
00056 hangul_jungseong_to_cjamo(wchar_t ch)
00057 {
00058     static wchar_t table[] = {
00059        0x314f,           /* 0x1161 */
00060        0x3150,           /* 0x1162 */
00061        0x3151,           /* 0x1163 */
00062        0x3152,           /* 0x1164 */
00063        0x3153,           /* 0x1165 */
00064        0x3154,           /* 0x1166 */
00065        0x3155,           /* 0x1167 */
00066        0x3156,           /* 0x1168 */
00067        0x3157,           /* 0x1169 */
00068        0x3158,           /* 0x116a */
00069        0x3159,           /* 0x116b */
00070        0x315a,           /* 0x116c */
00071        0x315b,           /* 0x116d */
00072        0x315c,           /* 0x116e */
00073        0x315d,           /* 0x116f */
00074        0x315e,           /* 0x1170 */
00075        0x315f,           /* 0x1171 */
00076        0x3160,           /* 0x1172 */
00077        0x3161,           /* 0x1173 */
00078        0x3162,           /* 0x1174 */
00079        0x3163     /* 0x1175 */
00080     };
00081 
00082     if (ch < 0x1161 || ch > 0x1175)
00083        return 0;
00084     else
00085        return table[ch - 0x1161];
00086 }
00087 
00088 wchar_t
00089 hangul_jongseong_to_cjamo(wchar_t ch)
00090 {
00091     static wchar_t table[] = {
00092        0x3131,           /* 0x11a8 */
00093        0x3132,           /* 0x11a9 */
00094        0x3133,           /* 0x11aa */
00095        0x3134,           /* 0x11ab */
00096        0x3135,           /* 0x11ac */
00097        0x3136,           /* 0x11ad */
00098        0x3137,           /* 0x11ae */
00099        0x3139,           /* 0x11af */
00100        0x313a,           /* 0x11b0 */
00101        0x313b,           /* 0x11b1 */
00102        0x313c,           /* 0x11b2 */
00103        0x313d,           /* 0x11b3 */
00104        0x313e,           /* 0x11b4 */
00105        0x313f,           /* 0x11b5 */
00106        0x3140,           /* 0x11b6 */
00107        0x3141,           /* 0x11b7 */
00108        0x3142,           /* 0x11b8 */
00109        0x3144,           /* 0x11b9 */
00110        0x3145,           /* 0x11ba */
00111        0x3146,           /* 0x11bb */
00112        0x3147,           /* 0x11bc */
00113        0x3148,           /* 0x11bd */
00114        0x314a,           /* 0x11be */
00115        0x314b,           /* 0x11bf */
00116        0x314c,           /* 0x11c0 */
00117        0x314d,           /* 0x11c1 */
00118        0x314e     /* 0x11c2 */
00119     };
00120 
00121     if (ch < 0x11a8 || ch > 0x11c2)
00122        return 0;
00123     else
00124        return table[ch - 0x11a8];
00125 }
00126 
00127 wchar_t
00128 hangul_choseong_to_jongseong(wchar_t ch)
00129 {
00130     static wchar_t table[] = {
00131         0x11a8,  /* choseong kiyeok      -> jongseong kiyeok      */
00132         0x11a9,  /* choseong ssangkiyeok -> jongseong ssangkiyeok */
00133         0x11ab,  /* choseong nieun       -> jongseong nieun       */
00134         0x11ae,  /* choseong tikeut      -> jongseong tikeut      */
00135         0x0,     /* choseong ssangtikeut -> jongseong tikeut      */
00136         0x11af,  /* choseong rieul       -> jongseong rieul       */
00137         0x11b7,  /* choseong mieum       -> jongseong mieum       */
00138         0x11b8,  /* choseong pieup       -> jongseong pieup       */
00139         0x0,     /* choseong ssangpieup  -> jongseong pieup       */
00140         0x11ba,  /* choseong sios        -> jongseong sios        */
00141         0x11bb,  /* choseong ssangsios   -> jongseong ssangsios   */
00142         0x11bc,  /* choseong ieung       -> jongseong ieung       */
00143         0x11bd,  /* choseong cieuc       -> jongseong cieuc       */
00144         0x0,     /* choseong ssangcieuc  -> jongseong cieuc       */
00145         0x11be,  /* choseong chieuch     -> jongseong chieuch     */
00146         0x11bf,  /* choseong khieukh     -> jongseong khieukh     */
00147         0x11c0,  /* choseong thieuth     -> jongseong thieuth     */
00148         0x11c1,  /* choseong phieuph     -> jongseong phieuph     */
00149         0x11c2   /* choseong hieuh       -> jongseong hieuh       */
00150     };
00151 
00152     if (ch < 0x1100 || ch > 0x1112)
00153        return 0;
00154     else
00155        return table[ch - 0x1100];
00156 }
00157 
00158 wchar_t
00159 hangul_jongseong_to_choseong(wchar_t ch)
00160 {
00161     static wchar_t table[] = {
00162        0x1100,  /* jongseong kiyeok        -> choseong kiyeok       */
00163        0x1101,  /* jongseong ssangkiyeok   -> choseong ssangkiyeok  */
00164        0x1109,  /* jongseong kiyeok-sios   -> choseong sios         */
00165        0x1102,  /* jongseong nieun         -> choseong nieun        */
00166        0x110c,  /* jongseong nieun-cieuc   -> choseong cieuc        */
00167        0x1112,  /* jongseong nieun-hieuh   -> choseong hieuh        */
00168        0x1103,  /* jongseong tikeut        -> choseong tikeut       */
00169        0x1105,  /* jongseong rieul         -> choseong rieul        */
00170        0x1100,  /* jongseong rieul-kiyeok  -> choseong kiyeok       */
00171        0x1106,  /* jongseong rieul-mieum   -> choseong mieum        */
00172        0x1107,  /* jongseong rieul-pieup   -> choseong pieup        */
00173        0x1109,  /* jongseong rieul-sios    -> choseong sios         */
00174        0x1110,  /* jongseong rieul-thieuth -> choseong thieuth      */
00175        0x1111,  /* jongseong rieul-phieuph -> choseong phieuph      */
00176        0x1112,  /* jongseong rieul-hieuh   -> choseong hieuh        */
00177        0x1106,  /* jongseong mieum         -> choseong mieum        */
00178        0x1107,  /* jongseong pieup         -> choseong pieup        */
00179        0x1109,  /* jongseong pieup-sios    -> choseong sios         */
00180        0x1109,  /* jongseong sios          -> choseong sios         */
00181        0x110a,  /* jongseong ssangsios     -> choseong ssangsios    */
00182        0x110b,  /* jongseong ieung         -> choseong ieung        */
00183        0x110c,  /* jongseong cieuc         -> choseong cieuc        */
00184        0x110e,  /* jongseong chieuch       -> choseong chieuch      */
00185        0x110f,  /* jongseong khieukh       -> choseong khieukh      */
00186        0x1110,  /* jongseong thieuth       -> choseong thieuth      */
00187        0x1111,  /* jongseong phieuph       -> choseong phieuph      */
00188        0x1112   /* jongseong hieuh         -> choseong hieuh        */
00189     };
00190 
00191     if (ch < 0x11a8 || ch > 0x11c2)
00192        return 0;
00193     else
00194        return table[ch - 0x11a8];
00195 }
00196 
00197 void
00198 hangul_jongseong_dicompose(wchar_t ch, wchar_t* jong, wchar_t* cho)
00199 {
00200     static wchar_t table[][2] = {
00201         { 0,      0x1100 }, /* jong kiyeok        = cho  kiyeok               */
00202         { 0x11a8, 0x1100 }, /* jong ssangkiyeok   = jong kiyeok + cho kiyeok  */
00203         { 0x11a8, 0x1109 }, /* jong kiyeok-sios   = jong kiyeok + cho sios    */
00204         { 0,      0x1102 }, /* jong nieun         = cho  nieun                */
00205         { 0x11ab, 0x110c }, /* jong nieun-cieuc   = jong nieun  + cho cieuc   */
00206         { 0x11ab, 0x1112 }, /* jong nieun-hieuh   = jong nieun  + cho hieuh   */
00207         { 0,      0x1103 }, /* jong tikeut        = cho  tikeut               */
00208         { 0,      0x1105 }, /* jong rieul         = cho  rieul                */
00209         { 0x11af, 0x1100 }, /* jong rieul-kiyeok  = jong rieul  + cho kiyeok  */
00210         { 0x11af, 0x1106 }, /* jong rieul-mieum   = jong rieul  + cho mieum   */
00211         { 0x11af, 0x1107 }, /* jong rieul-pieup   = jong rieul  + cho pieup   */
00212         { 0x11af, 0x1109 }, /* jong rieul-sios    = jong rieul  + cho sios    */
00213         { 0x11af, 0x1110 }, /* jong rieul-thieuth = jong rieul  + cho thieuth */
00214         { 0x11af, 0x1111 }, /* jong rieul-phieuph = jong rieul  + cho phieuph */
00215         { 0x11af, 0x1112 }, /* jong rieul-hieuh   = jong rieul  + cho hieuh   */
00216         { 0,      0x1106 }, /* jong mieum         = cho  mieum                */
00217         { 0,      0x1107 }, /* jong pieup         = cho  pieup                */
00218         { 0x11b8, 0x1109 }, /* jong pieup-sios    = jong pieup  + cho sios    */
00219         { 0,      0x1109 }, /* jong sios          = cho  sios                 */
00220         { 0x11ba, 0x1109 }, /* jong ssangsios     = jong sios   + cho sios    */
00221         { 0,      0x110b }, /* jong ieung         = cho  ieung                */
00222         { 0,      0x110c }, /* jong cieuc         = cho  cieuc                */
00223         { 0,      0x110e }, /* jong chieuch       = cho  chieuch              */
00224         { 0,      0x110f }, /* jong khieukh       = cho  khieukh              */
00225         { 0,      0x1110 }, /* jong thieuth       = cho  thieuth              */
00226         { 0,      0x1111 }, /* jong phieuph       = cho  phieuph              */
00227         { 0,      0x1112 }  /* jong hieuh         = cho  hieuh                */
00228     };
00229 
00230     if (ch >= 0x11a8 && ch <= 0x11c2) {
00231        *jong = table[ch - 0x11a8][0];
00232        *cho  = table[ch - 0x11a8][1];
00233     } else {
00234        *jong = 0;
00235        *cho = 0;
00236     }
00237 }
00238 
00239 wchar_t
00240 hangul_jamo_to_syllable(wchar_t choseong, wchar_t jungseong, wchar_t jongseong)
00241 {
00242     static wchar_t hangul_base       = 0xac00;
00243     static wchar_t choseong_base  = 0x1100;
00244     static wchar_t jungseong_base = 0x1161;
00245     static wchar_t jongseong_base = 0x11a7;
00246     static int njungseong = 21;
00247     static int njongseong = 28;
00248     wchar_t ch;
00249 
00250     /* we use 0x11a7 like a Jongseong filler */
00251     if (jongseong == 0)
00252        jongseong = 0x11a7;      /* Jongseong filler */
00253 
00254     if (choseong  < 0x1100 || choseong  > 0x1112)
00255        return 0;
00256     if (jungseong < 0x1161 || jungseong > 0x1175)
00257        return 0;
00258     if (jongseong < 0x11a7 || jongseong > 0x11c2)
00259        return 0;
00260 
00261     choseong  -= choseong_base;
00262     jungseong -= jungseong_base;
00263     jongseong -= jongseong_base;
00264 
00265     ch = ((choseong * njungseong) + jungseong) * njongseong + jongseong
00266        + hangul_base;
00267     return ch;
00268 }
00269 
00270 /* vim: set ts=8 sw=4 : */