Debian Codegraph @

plugins/ko/qml6/keys/parser.js

raw
1 /*
2 * Copyright (C) 2014 Henry Carlson
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19 /**
20 * Library for combining Hangul Jamo into syllables.
21 *
22 * References for hangul implementation in unicode:
23 * http://gernot-katzers-spice-pages.com/var/korean_hangul_unicode.html
24 * http://www.decodeunicode.org/en/hangul_jamo
25 * http://www.unicode.org/faq/korean.html
26 **/
27
28 var _BASE_OFFS = 0xAC00;
29 var _COMP_OFFS = 0x3130;
30
31 var _JAMO_LEAD = 0x1100;
32 var _JAMO_VOWEL = 0x1161;
33 var _JAMO_TRAIL = 0x11A8;
34
35 var _VOWEL_DIV = 28;
36 var _LEAD_DIV = 588;
37
38
39 // map compatibility jamo (virtual keyboard rendering) to component jamo
40 var LEAD = {
41 'ㄱ': 0x1100, 'ㄲ': 0x1101, 'ㄴ': 0x1102, 'ㄷ': 0x1103, 'ㄸ': 0x1104,
42 'ㄹ': 0x1105, 'ㅁ': 0x1106, 'ㅂ': 0x1107, 'ㅃ': 0x1108, 'ㅅ': 0x1109,
43 'ㅆ': 0x110A, 'ㅇ': 0x110B, 'ㅈ': 0x110C, 'ㅉ': 0x110D, 'ㅊ': 0x110E,
44 'ㅋ': 0x110F, 'ㅌ': 0x1110, 'ㅍ': 0x1111, 'ㅎ': 0x1112
45 };
46 var VOWEL = {
47 'ㅏ': 0x1161, 'ㅐ': 0x1162, 'ㅑ': 0x1163, 'ㅒ': 0x1164, 'ㅓ': 0x1165,
48 'ㅔ': 0x1166, 'ㅕ': 0x1167, 'ㅖ': 0x1168, 'ㅗ': 0x1169, 'ㅛ': 0x116D,
49 'ㅜ': 0x116E, 'ㅠ': 0x1172, 'ㅡ': 0x1173, 'ㅣ': 0x1175
50 };
51 var TRAIL = {
52 'ㄱ': 0x11A8, 'ㄲ': 0x11A9, 'ㄴ': 0x11AB, 'ㄷ': 0x11AE, 'ㄹ': 0x11AF,
53 'ㅁ': 0x11B7, 'ㅂ': 0x11B8, 'ㅅ': 0x11BA, 'ㅆ': 0x11BB, 'ㅇ': 0x11BC,
54 'ㅈ': 0x11BD, 'ㅊ': 0x11BE, 'ㅋ': 0x11BF, 'ㅌ': 0x11C0, 'ㅍ': 0x11C1,
55 'ㅎ': 0x11C2
56 };
57
58 // mappings for legal vowel diphthongs and compound consonant padchims
59 var VOWEL_COMPOUND = {
60 'ㅗㅏ': 0x116A, 'ㅗㅐ': 0x116B, 'ㅗㅣ': 0x116C, 'ㅜㅓ': 0x116F,
61 'ㅜㅔ': 0x1170, 'ㅜㅣ': 0x1171, 'ㅡㅣ': 0x1174
62 }
63 var TRAIL_COMPOUND = {
64 'ㄱㅅ': 0x11AA, 'ㄴㅈ': 0x11AC, 'ㄴㅎ': 0x11AD,
65 'ㄹㄱ': 0x11B0, 'ㄹㅁ': 0x11B1, 'ㄹㅂ': 0x11B2,
66 'ㄹㅅ': 0x11B3, 'ㄹㅌ': 0x11B4, 'ㄹㅍ': 0x11B5,
67 'ㄹㅎ': 0x11B6, 'ㅂㅅ': 0x11B9
68 };
69
70
71 function is_jamo(str) {
72 if (typeof str !== "string")
73 return false;
74
75 if ((str.charCodeAt(0) >= _COMP_OFFS) && (str.charCodeAt(0) <= 0x318F))
76 return true;
77 return false;
78 }
79
80 function is_syllable(str) {
81 if (typeof str !== "string")
82 return false;
83
84 if ((str.charCodeAt(0) >= _BASE_OFFS) && (str.charCodeAt(0) <= 0xD7A3))
85 return true;
86 return false;
87 }
88
89 function is_hangul(str) {
90 if (is_jamo(str) || is_syllable(str))
91 return true;
92 return false;
93 }
94
95 // convert compatiblity jamo into component jamo
96 function get_component(str, table) {
97 if (table[str] != null)
98 return String.fromCharCode(table[str]);
99 return str;
100 }
101
102 function normalise(str, table) {
103 for (var jamo in table) {
104 if (table[jamo] === str.charCodeAt(0))
105 return jamo;
106 }
107 return str;
108 }
109
110 /**
111 / Get base offset for jamo type
112 / Valid values: _JAMO_LEAD, _JAMO_VOWEL, _JAMO_TRAIL
113 */
114 function get_base(str) {
115 var code = str.charCodeAt(0);
116 if (code >= _JAMO_TRAIL)
117 return _JAMO_TRAIL;
118 else if (code >= _JAMO_VOWEL)
119 return _JAMO_VOWEL;
120 else if (code >= _JAMO_LEAD)
121 return _JAMO_LEAD;
122 return -1;
123 }
124
125
126
127 // merge jamo into a syllable block
128 function join(lead, vowel, trail) {
129 if (is_jamo(lead))
130 lead = get_component(lead, LEAD);
131 if (is_jamo(vowel))
132 vowel = get_component(vowel, VOWEL);
133 if (is_jamo(trail))
134 trail = get_component(trail, TRAIL);
135
136 var lead_offs = lead.charCodeAt(0) - _JAMO_LEAD;
137 var vowel_offs = vowel.charCodeAt(0) - _JAMO_VOWEL;
138 var trail_offs = -1;
139
140 if (trail !== "")
141 trail_offs = trail.charCodeAt(0) - _JAMO_TRAIL;
142
143 return String.fromCharCode(
144 trail_offs + vowel_offs*_VOWEL_DIV +
145 lead_offs*_LEAD_DIV +
146 _BASE_OFFS + 1
147 );
148 }
149
150 // split a syllable block into component jamo
151 function split(str) {
152 var char_offs = str.charCodeAt(0) - _BASE_OFFS;
153 var trail_offs = Math.floor(char_offs%_VOWEL_DIV - 1);
154 var vowel_offs = Math.floor((char_offs - trail_offs)%_LEAD_DIV / _VOWEL_DIV);
155 var lead_offs = Math.floor(char_offs/_LEAD_DIV);
156
157 var trail = "";
158
159 if (trail_offs !== -1)
160 trail = normalise(String.fromCharCode(_JAMO_TRAIL + trail_offs), TRAIL);
161
162 return [
163 normalise(String.fromCharCode(_JAMO_LEAD + lead_offs), LEAD),
164 normalise(String.fromCharCode(_JAMO_VOWEL + vowel_offs), VOWEL),
165 trail
166 ];
167 }
168
169 // merge new jamo with the existing string
170 function add_jamo(str, jamo) {
171 // make sure merging is actually a valid option
172 if (is_jamo(jamo) && is_hangul(str)) {
173
174
175 // merge jamo with jamo
176 if (is_jamo(str)) {
177 var lead = get_component(str, LEAD);
178 var vowel = get_component(jamo, VOWEL);
179
180 if ((get_base(lead) === _JAMO_LEAD) && (get_base(vowel) === _JAMO_VOWEL))
181 return join(lead, vowel, '');
182
183 // merge syllable with jamo
184 } else {
185 var buffer = split(str);
186
187 // merging vowel to existing syllable
188 if (get_base(get_component(jamo, VOWEL)) === _JAMO_VOWEL) {
189 // if syllable has padchim, split into two complete syllables:
190 if (buffer[2] !== "") {
191 // verify that the padchim is a legal initial consonant:
192 if (get_component(buffer[2], LEAD) !== buffer[2]) {
193 return join(buffer[0], buffer[1], '') + join(normalise(buffer[2], TRAIL), jamo, '');
194 } else {
195 // if the padchim is a compound consonant, we want to split it:
196 var split_trail = normalise(buffer[2], TRAIL_COMPOUND);
197 if (split_trail !== buffer[2])
198 return join(buffer[0], buffer[1], split_trail[0]) + join(split_trail[1], jamo, '');
199 }
200 return str + jamo;
201 }
202
203 // attempt vowel mergers:
204 var vowel = VOWEL_COMPOUND[buffer[1] + jamo];
205 if (vowel != null)
206 return join(buffer[0], String.fromCharCode(vowel), '');
207
208 // merging consonant to existing syllable
209 } else if (get_base(get_component(jamo, TRAIL)) === _JAMO_TRAIL) {
210 if (buffer[2] === "") {
211 // verify that the consonant is a legal trailing consonant:
212 if (get_component(jamo, TRAIL) !== jamo)
213 return join(buffer[0], buffer[1], jamo);
214 return str + jamo;
215 }
216
217 // attempt consonant mergers:
218 var trail = TRAIL_COMPOUND[buffer[2] + jamo];
219 if (trail != null)
220 return join(buffer[0], buffer[1], String.fromCharCode(trail));
221 }
222 }
223 }
224
225 return str + jamo;
226 }
227
228 // erase jamo from the syllable under creation
229 function erase_jamo(str) {
230 if (is_jamo(str) || !is_hangul(str))
231 return "";
232
233 var buffer = split(str);
234 if (buffer[2] !== "") {
235 var split_trail = normalise(buffer[2], TRAIL_COMPOUND);
236 if (split_trail !== buffer[2])
237 return join(buffer[0], buffer[1], split_trail[0]);
238 return join(buffer[0], buffer[1], "");
239 } else {
240 var split_vowel = normalise(buffer[1], VOWEL_COMPOUND);
241 if (split_vowel !== buffer[1])
242 return join(buffer[0], split_vowel[0], '');
243 return buffer[0];
244 }
245 }