Debian Codegraph @

xbmc/input/InputCodingTableKorean.cpp

raw
1 /*
2 * Copyright (C) 2005-2024 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9 #include "InputCodingTableKorean.h"
10
11 #include "utils/CharsetConverter.h"
12
13 #include <stdlib.h>
14
15 CInputCodingTableKorean::CInputCodingTableKorean() = default;
16
17 std::vector<std::wstring> CInputCodingTableKorean::GetResponse(int)
18 {
19 return m_words;
20 }
21
22 bool CInputCodingTableKorean::GetWordListPage(const std::string& strCode, bool isFirstPage)
23 {
24 return false;
25 }
26
27 void CInputCodingTableKorean::SetTextPrev(const std::string& strTextPrev)
28 {
29 m_strTextPrev = strTextPrev;
30 }
31
32 int CInputCodingTableKorean::MergeCode(int choseong, int jungseong, int jongseong)
33 {
34 return (unsigned short)0xAC00 + choseong * 21 * 28 + jungseong * 28 + jongseong + 1;
35 }
36
37 // Reference
38 // https://en.wikipedia.org/wiki/Hangul
39 // http://www.theyt.net/wiki/%ED%95%9C%EC%98%81%ED%83%80%EB%B3%80%ED%99%98%EA%B8%B0
40
41 std::wstring CInputCodingTableKorean::InputToKorean(const std::wstring& input)
42 {
43 std::wstring dicEnglish = // L"rRseEfaqQtTdwWczxvgkoiOjpuPhynbml";
44 {0x72, 0x52, 0x73, 0x65, 0x45, 0x66, 0x61, 0x71, 0x51, 0x74, 0x54,
45 0x64, 0x77, 0x57, 0x63, 0x7A, 0x78, 0x76, 0x67, 0x6B, 0x6F, 0x69,
46 0x4F, 0x6A, 0x70, 0x75, 0x50, 0x68, 0x79, 0x6E, 0x62, 0x6D, 0x6C};
47 std::wstring dicKorean = // L"ㄱㄲㄴㄷㄸㄹㅁㅂㅃㅅㅆㅇㅈㅉㅊㅋㅌㅍㅎㅏㅐㅑㅒㅓㅔㅕㅖㅗㅛㅜㅠㅡㅣ";
48 {0x3131, 0x3132, 0x3134, 0x3137, 0x3138, 0x3139, 0x3141, 0x3142, 0x3143, 0x3145, 0x3146,
49 0x3147, 0x3148, 0x3149, 0x314a, 0x314b, 0x314c, 0x314d, 0x314e, 0x314f, 0x3150, 0x3151,
50 0x3152, 0x3153, 0x3154, 0x3155, 0x3156, 0x3157, 0x315b, 0x315c, 0x3160, 0x3161, 0x3163};
51 std::wstring dicChoseong = // L"ㄱㄲㄴㄷㄸㄹㅁㅂㅃㅅㅆㅇㅈㅉㅊㅋㅌㅍㅎ";
52 {0x3131, 0x3132, 0x3134, 0x3137, 0x3138, 0x3139, 0x3141, 0x3142, 0x3143, 0x3145,
53 0x3146, 0x3147, 0x3148, 0x3149, 0x314a, 0x314b, 0x314c, 0x314d, 0x314e};
54 std::wstring dicJungseong = // L"ㅏㅐㅑㅒㅓㅔㅕㅖㅗㅘㅙㅚㅛㅜㅝㅞㅟㅠㅡㅢㅣ";
55 {0x314f, 0x3150, 0x3151, 0x3152, 0x3153, 0x3154, 0x3155, 0x3156, 0x3157, 0x3158, 0x3159,
56 0x315a, 0x315b, 0x315c, 0x315d, 0x315e, 0x315f, 0x3160, 0x3161, 0x3162, 0x3163};
57 std::wstring dicJongseong = // L"ㄱㄲㄳㄴㄵㄶㄷㄹㄺㄻㄼㄽㄾㄿㅀㅁㅂㅄㅅㅆㅇㅈㅊㅋㅌㅍㅎ";
58 {0x3131, 0x3132, 0x3133, 0x3134, 0x3135, 0x3136, 0x3137, 0x3139, 0x313a,
59 0x313b, 0x313c, 0x313d, 0x313e, 0x313f, 0x3140, 0x3141, 0x3142, 0x3144,
60 0x3145, 0x3146, 0x3147, 0x3148, 0x314a, 0x314b, 0x314c, 0x314d, 0x314e};
61
62 std::wstring korean;
63
64 if (input.empty())
65 return korean;
66
67 int choseong = -1, jungseong = -1, jongseong = -1;
68
69 for (unsigned int i = 0; i < input.size(); i++)
70 {
71 wchar_t ch = input.at(i);
72 int key = dicKorean.find(ch);
73
74 // H/W Keyboard input with English will be changed to Korean
75 // because H/W input in Korean is not supported.
76 if (key == -1)
77 key = dicEnglish.find(ch);
78
79 if (key == -1) // If not Korean and English
80 {
81 // If there is remained Korean, merge code into character
82 if (choseong != -1) // There is choseong
83 {
84 if (jungseong != -1) // choseong+jungseong+(jongseong)
85 korean += MergeCode(choseong, jungseong, jongseong);
86 else // Only choseong
87 korean += dicChoseong.at(choseong);
88 }
89 else
90 {
91 if (jungseong != -1) // Jungseong
92 korean += dicJungseong.at(jungseong);
93
94 if (jongseong != -1) // Jongseong
95 korean += dicJongseong.at(jongseong);
96 }
97 choseong = -1;
98 jungseong = -1;
99 jongseong = -1;
100 korean += ch;
101 }
102 else if (key < 19) // If key is consonant, key could be choseong or jungseong
103 {
104 if (jungseong != -1)
105 {
106 // Jungseong without choseong cannot have jongseong.
107 // So inputted key is jungseong character, new character is begun.
108 if (choseong == -1)
109 {
110 korean += dicJungseong.at(jungseong);
111 jungseong = -1;
112 choseong = key;
113 }
114 else // Jungseong with choseong can have jongseong.
115 {
116 // Chongseong can have two consonant. So this is first consonant of chongseong.
117 if (jongseong == -1)
118 {
119 jongseong = dicJongseong.find(dicKorean.at(key));
120 if (jongseong == -1) // This consonant cannot be jongseong. ex) "ㄸ", "ㅃ", "ㅉ"
121 {
122 korean += MergeCode(choseong, jungseong, jongseong);
123 choseong = dicChoseong.find(dicKorean.at(key));
124 jungseong = -1;
125 }
126 }
127 else if (jongseong == 0 && key == 9) // "ㄳ"
128 jongseong = 2;
129 else if (jongseong == 3 && key == 12) // "ㄵ"
130 jongseong = 4;
131 else if (jongseong == 3 && key == 18) // "ㄶ"
132 jongseong = 5;
133 else if (jongseong == 7 && key == 0) // "ㄺ"
134 jongseong = 8;
135 else if (jongseong == 7 && key == 6) // "ㄻ"
136 jongseong = 9;
137 else if (jongseong == 7 && key == 7) // "ㄼ"
138 jongseong = 10;
139 else if (jongseong == 7 && key == 9) // "ㄽ"
140 jongseong = 11;
141 else if (jongseong == 7 && key == 16) // "ㄾ"
142 jongseong = 12;
143 else if (jongseong == 7 && key == 17) // "ㄿ"
144 jongseong = 13;
145 else if (jongseong == 7 && key == 18) // "ㅀ"
146 jongseong = 14;
147 else if (jongseong == 16 && key == 9) // "ㅄ"
148 jongseong = 17;
149 else // Jongseong is completed. So new consonant is choseong.
150 {
151 korean += MergeCode(choseong, jungseong, jongseong);
152 choseong = dicChoseong.find(dicKorean.at(key));
153 jungseong = -1;
154 jongseong = -1;
155 }
156 }
157 }
158 else // If there is no jungseong, new consonant can be choseong or second part of double
159 // consonant.
160 {
161 // New consonant is choseong. Also it could be first part of double consonant.
162 if (choseong == -1)
163 {
164 // If choseong is already completed, new consonant is another choseong.
165 // So previous character has only jongseong.
166 if (jongseong != -1)
167 {
168 korean += dicJongseong.at(jongseong);
169 jongseong = -1;
170 }
171 choseong = dicChoseong.find(dicKorean.at(key));
172 }
173 // Find double consonant of chongseong
174 else if (choseong == 0 && key == 9) // "ㄳ"
175 {
176 choseong = -1;
177 jongseong = 2;
178 }
179 else if (choseong == 2 && key == 12) // "ㄵ"
180 {
181 choseong = -1;
182 jongseong = 4;
183 }
184 else if (choseong == 2 && key == 18) // "ㄶ"
185 {
186 choseong = -1;
187 jongseong = 5;
188 }
189 else if (choseong == 5 && key == 0) // "ㄺ"
190 {
191 choseong = -1;
192 jongseong = 8;
193 }
194 else if (choseong == 5 && key == 6) // "ㄻ"
195 {
196 choseong = -1;
197 jongseong = 9;
198 }
199 else if (choseong == 5 && key == 7) // "ㄼ"
200 {
201 choseong = -1;
202 jongseong = 10;
203 }
204 else if (choseong == 5 && key == 9) // "ㄽ"
205 {
206 choseong = -1;
207 jongseong = 11;
208 }
209 else if (choseong == 5 && key == 16) // "ㄾ"
210 {
211 choseong = -1;
212 jongseong = 12;
213 }
214 else if (choseong == 5 && key == 17) // "ㄿ"
215 {
216 choseong = -1;
217 jongseong = 13;
218 }
219 else if (choseong == 5 && key == 18) // "ㅀ"
220 {
221 choseong = -1;
222 jongseong = 14;
223 }
224 else if (choseong == 7 && key == 9) // "ㅄ"
225 {
226 choseong = -1;
227 jongseong = 17;
228 }
229 else // In this case, previous character has only choseong. And new consonant is choseong.
230 {
231 korean += dicChoseong.at(choseong);
232 choseong = dicChoseong.find(dicKorean.at(key));
233 }
234 }
235 }
236 else // If key is vowel, key is jungseong.
237 {
238 // If previous character has jongseong and this key is jungseong,
239 // actually latest vowel is not jongseong. It's choseong of new character.
240 if (jongseong != -1)
241 {
242 // If jongseong of previous character is double consonant, we will separate it to two vowel
243 // again. First part of double consonant is jongseong of previous character. Second part of
244 // double consonant is choseong of current character.
245 int newCho;
246 if (jongseong == 2) // "ㄱ, ㅅ"
247 {
248 jongseong = 0;
249 newCho = 9;
250 }
251 else if (jongseong == 4) // "ㄴ, ㅈ"
252 {
253 jongseong = 3;
254 newCho = 12;
255 }
256 else if (jongseong == 5) // "ㄴ, ㅎ"
257 {
258 jongseong = 3;
259 newCho = 18;
260 }
261 else if (jongseong == 8) // "ㄹ, ㄱ"
262 {
263 jongseong = 7;
264 newCho = 0;
265 }
266 else if (jongseong == 9) // "ㄹ, ㅁ"
267 {
268 jongseong = 7;
269 newCho = 6;
270 }
271 else if (jongseong == 10) // "ㄹ, ㅂ"
272 {
273 jongseong = 7;
274 newCho = 7;
275 }
276 else if (jongseong == 11) // "ㄹ, ㅅ"
277 {
278 jongseong = 7;
279 newCho = 9;
280 }
281 else if (jongseong == 12) // "ㄹ, ㅌ"
282 {
283 jongseong = 7;
284 newCho = 16;
285 }
286 else if (jongseong == 13) // "ㄹ, ㅍ"
287 {
288 jongseong = 7;
289 newCho = 17;
290 }
291 else if (jongseong == 14) // "ㄹ, ㅎ"
292 {
293 jongseong = 7;
294 newCho = 18;
295 }
296 else if (jongseong == 17) // "ㅂ, ㅅ"
297 {
298 jongseong = 16;
299 newCho = 9;
300 }
301 else
302 {
303 // If jongseong is single consonant, previous character has no chongseong.
304 // It's choseong of current character.
305 newCho = dicChoseong.find(dicJongseong.at(jongseong));
306 jongseong = -1;
307 }
308 if (choseong != -1) // If previous character has choseong and jungseong.
309 korean += MergeCode(choseong, jungseong, jongseong);
310 else // If previous character has Jongseong only.
311 korean += dicJongseong.at(jongseong);
312
313 choseong = newCho;
314 jungseong = -1;
315 jongseong = -1;
316 }
317 if (jungseong == -1) // If this key is first vowel, it's first part of jungseong.
318 {
319 jungseong = dicJungseong.find(dicKorean.at(key));
320 }
321 // If there is jungseong already, jungseong is double vowel.
322 else if (jungseong == 8 && key == 19) // "ㅘ"
323 jungseong = 9;
324 else if (jungseong == 8 && key == 20) // "ㅙ"
325 jungseong = 10;
326 else if (jungseong == 8 && key == 32) // "ㅚ"
327 jungseong = 11;
328 else if (jungseong == 13 && key == 23) // "ㅝ"
329 jungseong = 14;
330 else if (jungseong == 13 && key == 24) // "ㅞ"
331 jungseong = 15;
332 else if (jungseong == 13 && key == 32) // "ㅟ"
333 jungseong = 16;
334 else if (jungseong == 18 && key == 32) // "ㅢ"
335 jungseong = 19;
336 else // If two vowel cannot be double vowel.
337 {
338 // Previous character is completed.
339 // Current character is begin with jungseong.
340 if (choseong != -1)
341 {
342 korean += MergeCode(choseong, jungseong, jongseong);
343 choseong = -1;
344 }
345 else // Previous character has jungseon only.
346 korean += dicJungseong.at(jungseong);
347 jungseong = -1;
348 korean += dicKorean.at(key);
349 }
350 }
351 }
352
353 // Process last character
354 if (choseong != -1)
355 {
356 if (jungseong != -1) // Current character has choseong and jungseong.
357 korean += MergeCode(choseong, jungseong, jongseong);
358 else // Current character has choseong only.
359 korean += dicChoseong.at(choseong);
360 }
361 else
362 {
363 if (jungseong != -1) // Current character has jungseong only
364 korean += dicJungseong.at(jungseong);
365 else if (jongseong != -1) // Current character has jongseong only
366 korean += dicJongseong.at(jongseong);
367 }
368
369 return korean;
370 }
371
372 std::string CInputCodingTableKorean::ConvertString(const std::string& strCode)
373 {
374 std::wstring input;
375 std::string result;
376 g_charsetConverter.utf8ToW(strCode, input);
377 InputToKorean(input);
378 g_charsetConverter.wToUTF8(InputToKorean(input), result);
379 return m_strTextPrev + result;
380 }