Debian Codegraph @

src/key2kana_table.cpp

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * Copyright (C) 2005 Takuro Ashie
4 * Copyright (C) 2012 CSSlayer
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20
21 #include "key2kana_table.h"
22
23 // fundamental table
24 static Key2KanaTable romaji_table (
25 ("DefaultRomajiTable"),
26 fcitx_anthy_romaji_typing_rule);
27 static Key2KanaTable romaji_double_consonant_table (
28 ("DefaultRomajiDoubleConsonantTable"),
29 fcitx_anthy_romaji_double_consonant_rule);
30 static Key2KanaTable kana_table (
31 ("DefaultKanaTable"),
32 fcitx_anthy_kana_typing_rule);
33 static Key2KanaTable kana_voiced_consonant_table (
34 ("DefaultKanaVoicedConsonantTable"),
35 fcitx_anthy_kana_voiced_consonant_rule);
36 static Key2KanaTable nicola_table (
37 ("DefaultNICOLATable"),
38 fcitx_anthy_nicola_table);
39
40 // symbols
41 static Key2KanaTable half_symbol_table (
42 ("DefaultRomajiHalfSymbolTable"),
43 fcitx_anthy_half_symbol_rule);
44 static Key2KanaTable wide_symbol_table (
45 ("DefaultRomajiWideSymbolTable"),
46 fcitx_anthy_wide_symbol_rule);
47
48 // numbers
49 static Key2KanaTable half_number_table (
50 ("DefaultRomajiHalfNumberTable"),
51 fcitx_anthy_half_number_rule);
52 static Key2KanaTable wide_number_table (
53 ("DefaultRomajiWideNumberTable"),
54 fcitx_anthy_wide_number_rule);
55
56 // period
57 static Key2KanaTable romaji_ja_period_table (
58 ("DefaultRomajiJaPeriodTable"),
59 fcitx_anthy_romaji_ja_period_rule);
60 static Key2KanaTable romaji_wide_period_table (
61 ("DefaultRomajiWidePeriodTable"),
62 fcitx_anthy_romaji_wide_period_rule);
63 static Key2KanaTable romaji_half_period_table (
64 ("DefaultRomajiHalfPeriodTable"),
65 fcitx_anthy_romaji_half_period_rule);
66
67 static Key2KanaTable kana_ja_period_table (
68 ("DefaultKanaJaPeriodTable"),
69 fcitx_anthy_kana_ja_period_rule);
70 static Key2KanaTable kana_wide_period_table (
71 ("DefaultKanaWidePeriodTable"),
72 fcitx_anthy_kana_wide_period_rule);
73 static Key2KanaTable kana_half_period_table (
74 ("DefaultKanaHalfPeriodTable"),
75 fcitx_anthy_kana_half_period_rule);
76
77 // comma
78 static Key2KanaTable romaji_ja_comma_table (
79 ("DefaultRomajiJaCommaTable"),
80 fcitx_anthy_romaji_ja_comma_rule);
81 static Key2KanaTable romaji_wide_comma_table (
82 ("DefaultRomajiWideCommaTable"),
83 fcitx_anthy_romaji_wide_comma_rule);
84 static Key2KanaTable romaji_half_comma_table (
85 ("DefaultRomajiHalfCommaTable"),
86 fcitx_anthy_romaji_half_comma_rule);
87
88 static Key2KanaTable kana_ja_comma_table (
89 ("DefaultKanaJaCommaTable"),
90 fcitx_anthy_kana_ja_comma_rule);
91 static Key2KanaTable kana_wide_comma_table (
92 ("DefaultKanaWideCommaTable"),
93 fcitx_anthy_kana_wide_comma_rule);
94 static Key2KanaTable kana_half_comma_table (
95 ("DefaultKanaHalfCommaTable"),
96 fcitx_anthy_kana_half_comma_rule);
97
98 // bracket
99 static Key2KanaTable romaji_ja_bracket_table (
100 ("DefaultRomajiJaBracketTable"),
101 fcitx_anthy_romaji_ja_bracket_rule);
102 static Key2KanaTable romaji_wide_bracket_table (
103 ("DefaultRomajiWideBracketTable"),
104 fcitx_anthy_romaji_wide_bracket_rule);
105
106 static Key2KanaTable kana_ja_bracket_table (
107 ("DefaultKanaJaBracketTable"),
108 fcitx_anthy_kana_ja_bracket_rule);
109 static Key2KanaTable kana_wide_bracket_table (
110 ("DefaultRomajiWideBracketTable"),
111 fcitx_anthy_kana_wide_bracket_rule);
112
113 // slash
114 static Key2KanaTable romaji_ja_slash_table (
115 ("DefaultRomajiJaSlashTable"),
116 fcitx_anthy_romaji_ja_slash_rule);
117 static Key2KanaTable romaji_wide_slash_table (
118 ("DefaultRomajiWideSlashTable"),
119 fcitx_anthy_romaji_wide_slash_rule);
120
121 static Key2KanaTable kana_ja_slash_table (
122 ("DefaultKanaJaSlashTable"),
123 fcitx_anthy_kana_ja_slash_rule);
124 static Key2KanaTable kana_wide_slash_table (
125 ("DefaultRomajiWideSlashTable"),
126 fcitx_anthy_kana_wide_slash_rule);
127
128
129 Key2KanaRule::Key2KanaRule ()
130 {
131 }
132
133 Key2KanaRule::Key2KanaRule (std::string sequence,
134 const std::vector<std::string> &result)
135 : m_sequence (sequence),
136 m_result (result)
137 {
138 }
139
140
141 Key2KanaRule::~Key2KanaRule ()
142 {
143 }
144
145 std::string
146 Key2KanaRule::get_sequence (void)
147 {
148 return m_sequence;
149 }
150
151 std::string
152 Key2KanaRule::get_result (unsigned int idx)
153 {
154 if (idx < m_result.size ())
155 return m_result[idx];
156
157 return std::string ();
158 }
159
160 void
161 Key2KanaRule::clear (void)
162 {
163 m_sequence = std::string ();
164 m_result.clear ();
165 }
166
167 bool
168 Key2KanaRule::is_empty (void)
169 {
170 if (!m_sequence.empty ())
171 return false;
172
173 if (m_result.empty ())
174 return true;
175
176 for (unsigned int i = 0; i < m_result.size (); i++) {
177 if (!m_result[i].empty ())
178 return false;
179 }
180
181 return true;
182 }
183
184
185 Key2KanaTable::Key2KanaTable (std::string name)
186 : m_name (name)
187 {
188 }
189
190 Key2KanaTable::Key2KanaTable (std::string name, ConvRule *table)
191 : m_name (name)
192 {
193 for (unsigned int i = 0; table[i].string; i++) {
194 append_rule (table[i].string ? table[i].string : "",
195 table[i].result ? table[i].result : "",
196 table[i].cont ? table[i].cont : "");
197 }
198 }
199
200 Key2KanaTable::Key2KanaTable (std::string name, NicolaRule *table)
201 : m_name (name)
202 {
203 for (unsigned int i = 0; table[i].key; i++) {
204 append_rule (table[i].key ? table[i].key : "",
205 table[i].single ? table[i].single : "",
206 table[i].left_shift ? table[i].left_shift : "",
207 table[i].right_shift ? table[i].right_shift : "");
208 }
209 }
210
211 Key2KanaTable::~Key2KanaTable ()
212 {
213 }
214
215 void
216 Key2KanaTable::append_rule (std::string sequence,
217 const std::vector<std::string> &result)
218 {
219 m_rules.push_back (Key2KanaRule (sequence, result));
220 }
221
222 void
223 Key2KanaTable::append_rule (std::string sequence,
224 std::string result,
225 std::string cont)
226 {
227 std::vector<std::string> list;
228 list.push_back (result);
229 list.push_back (cont);
230 m_rules.push_back (Key2KanaRule (sequence, list));
231 }
232
233 void
234 Key2KanaTable::append_rule (std::string sequence,
235 std::string normal,
236 std::string left_shift,
237 std::string right_shift)
238 {
239 std::vector<std::string> list;
240 list.push_back (normal);
241 list.push_back (left_shift);
242 list.push_back (right_shift);
243 m_rules.push_back (Key2KanaRule (sequence, list));
244 }
245
246 void
247 Key2KanaTable::clear (void)
248 {
249 m_rules.clear ();
250
251 }
252
253 Key2KanaTableSet::Key2KanaTableSet ()
254 : m_name (""),
255 m_fundamental_table (NULL),
256 m_voiced_consonant_table (Key2KanaTable ("voiced consonant table")),
257 m_additional_table (NULL),
258 m_typing_method (FCITX_ANTHY_TYPING_METHOD_ROMAJI),
259 m_period_style (FCITX_ANTHY_PERIOD_JAPANESE),
260 m_comma_style (FCITX_ANTHY_COMMA_JAPANESE),
261 m_bracket_style (FCITX_ANTHY_BRACKET_JAPANESE),
262 m_slash_style (FCITX_ANTHY_SLASH_JAPANESE),
263 m_use_half_symbol (false),
264 m_use_half_number (false)
265 {
266 set_typing_method (m_typing_method);
267 }
268
269 Key2KanaTableSet::~Key2KanaTableSet ()
270 {
271 }
272
273 void
274 Key2KanaTableSet::set_typing_method (TypingMethod method,
275 Key2KanaTable *fundamental_table)
276 {
277 m_typing_method = method;
278 m_fundamental_table = fundamental_table;
279 reset_tables ();
280 }
281
282 void
283 Key2KanaTableSet::set_symbol_width (bool half)
284 {
285 m_use_half_symbol = half;
286 reset_tables ();
287 }
288
289 void
290 Key2KanaTableSet::set_number_width (bool half)
291 {
292 m_use_half_number = half;
293 reset_tables ();
294 }
295
296 void
297 Key2KanaTableSet::set_period_style (PeriodStyle style)
298 {
299 m_period_style = style;
300 reset_tables ();
301 }
302
303 void
304 Key2KanaTableSet::set_comma_style (CommaStyle style)
305 {
306 m_comma_style = style;
307 reset_tables ();
308 }
309
310 void
311 Key2KanaTableSet::set_bracket_style (BracketStyle style)
312 {
313 m_bracket_style = style;
314 reset_tables ();
315 }
316
317 void
318 Key2KanaTableSet::set_slash_style (SlashStyle style)
319 {
320 m_slash_style = style;
321 reset_tables ();
322 }
323
324 static void
325 create_voiced_consonant_table (Key2KanaTable &table, Key2KanaTable &fund_table)
326 {
327 table.clear ();
328
329 const std::string sonant_mark = std::string ("\xE3\x82\x9B");
330 const std::string half_sonant_mark = std::string ("\xE3\x82\x9C");
331 std::vector<std::string> sonant_mark_list;
332 std::vector<std::string> half_sonant_mark_list;
333
334 Key2KanaRules::iterator it;
335 Key2KanaRules &rules = fund_table.get_table ();
336 for (it = rules.begin (); it != rules.end (); it++) {
337 std::string result = it->get_result (0);
338 if (result == sonant_mark)
339 sonant_mark_list.push_back (it->get_sequence ());
340 else if (result == half_sonant_mark)
341 half_sonant_mark_list.push_back (it->get_sequence ());
342 }
343
344 VoicedConsonantRule *templ = fcitx_anthy_voiced_consonant_table;
345
346 for (unsigned int i = 0; templ[i].string; i++) {
347 if (templ[i].voiced && *templ[i].voiced) {
348 std::vector<std::string>::iterator it;
349 for (it = sonant_mark_list.begin ();
350 it != sonant_mark_list.end ();
351 it++)
352 {
353 table.append_rule (std::string (templ[i].string) + *it,
354 std::string (templ[i].voiced),
355 std::string ());
356 }
357 }
358 if (templ[i].half_voiced && *templ[i].half_voiced) {
359 std::vector<std::string>::iterator it;
360 for (it = half_sonant_mark_list.begin ();
361 it != half_sonant_mark_list.end ();
362 it++)
363 {
364 table.append_rule (std::string (templ[i].string) + *it,
365 std::string (templ[i].half_voiced),
366 std::string ());
367 }
368 }
369 }
370 }
371
372 void
373 Key2KanaTableSet::reset_tables (void)
374 {
375 m_all_tables.clear ();
376
377 bool is_romaji = m_typing_method == FCITX_ANTHY_TYPING_METHOD_ROMAJI;
378 bool is_kana = m_typing_method == FCITX_ANTHY_TYPING_METHOD_KANA;
379 bool is_nicola = m_typing_method == FCITX_ANTHY_TYPING_METHOD_NICOLA;
380
381 // symbols table
382 if (m_use_half_symbol)
383 m_all_tables.push_back (&half_symbol_table);
384 else
385 m_all_tables.push_back (&wide_symbol_table);
386
387 // numbers table
388 if (m_use_half_number)
389 m_all_tables.push_back (&half_number_table);
390 else
391 m_all_tables.push_back (&wide_number_table);
392
393 if (is_romaji || is_kana)
394 {
395 switch (m_period_style) {
396 case FCITX_ANTHY_PERIOD_JAPANESE:
397 if (is_romaji)
398 m_all_tables.push_back (&romaji_ja_period_table);
399 else
400 m_all_tables.push_back (&kana_ja_period_table);
401 break;
402 case FCITX_ANTHY_PERIOD_WIDE:
403 if (is_romaji)
404 m_all_tables.push_back (&romaji_wide_period_table);
405 else
406 m_all_tables.push_back (&kana_wide_period_table);
407 break;
408 case FCITX_ANTHY_PERIOD_HALF:
409 if (is_romaji)
410 m_all_tables.push_back (&romaji_half_period_table);
411 else
412 m_all_tables.push_back (&kana_half_period_table);
413 break;
414 default:
415 break;
416 }
417 }
418
419 if (is_romaji || is_kana)
420 {
421 switch (m_comma_style) {
422 case FCITX_ANTHY_COMMA_JAPANESE:
423 if (is_romaji)
424 m_all_tables.push_back (&romaji_ja_comma_table);
425 else
426 m_all_tables.push_back (&kana_ja_comma_table);
427 break;
428 case FCITX_ANTHY_COMMA_WIDE:
429 if (is_romaji)
430 m_all_tables.push_back (&romaji_wide_comma_table);
431 else
432 m_all_tables.push_back (&kana_wide_comma_table);
433 break;
434 case FCITX_ANTHY_COMMA_HALF:
435 if (is_romaji)
436 m_all_tables.push_back (&romaji_half_comma_table);
437 else
438 m_all_tables.push_back (&kana_half_comma_table);
439 break;
440 default:
441 break;
442 }
443 }
444
445 if (is_romaji || is_kana)
446 {
447 switch (m_bracket_style) {
448 case FCITX_ANTHY_BRACKET_JAPANESE:
449 if (is_romaji)
450 m_all_tables.push_back (&romaji_ja_bracket_table);
451 else
452 m_all_tables.push_back (&kana_ja_bracket_table);
453 break;
454 case FCITX_ANTHY_BRACKET_WIDE:
455 if (is_romaji)
456 m_all_tables.push_back (&romaji_wide_bracket_table);
457 else
458 m_all_tables.push_back (&kana_wide_bracket_table);
459 break;
460 default:
461 break;
462 }
463 }
464
465 if (is_romaji || is_kana)
466 {
467 switch (m_slash_style) {
468 case FCITX_ANTHY_SLASH_JAPANESE:
469 if (is_romaji)
470 m_all_tables.push_back (&romaji_ja_slash_table);
471 else
472 m_all_tables.push_back (&kana_ja_slash_table);
473 break;
474 case FCITX_ANTHY_SLASH_WIDE:
475 if (is_romaji)
476 m_all_tables.push_back (&romaji_wide_slash_table);
477 else
478 m_all_tables.push_back (&kana_wide_slash_table);
479 break;
480 default:
481 break;
482 }
483 }
484
485 if (!m_fundamental_table) {
486 if (is_romaji) {
487 m_all_tables.push_back (&romaji_double_consonant_table);
488 m_all_tables.push_back (&romaji_table);
489 } else if (is_kana) {
490 create_voiced_consonant_table (m_voiced_consonant_table,
491 kana_table);
492 m_all_tables.push_back (&m_voiced_consonant_table);
493 m_all_tables.push_back (&kana_table);
494 } else if (is_nicola) {
495 m_all_tables.push_back (&nicola_table);
496 }
497 } else {
498 if (is_romaji) {
499 m_all_tables.push_back (&romaji_double_consonant_table);
500 m_all_tables.push_back (m_fundamental_table);
501 } else if (is_kana) {
502 create_voiced_consonant_table (m_voiced_consonant_table,
503 *m_fundamental_table);
504 m_all_tables.push_back (&m_voiced_consonant_table);
505 m_all_tables.push_back (m_fundamental_table);
506 } else if (is_nicola) {
507 m_all_tables.push_back (m_fundamental_table);
508 }
509 }
510 }