-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathkey2kana_table.cpp
419 lines (365 loc) · 14.6 KB
/
key2kana_table.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
* SPDX-FileCopyrightText: 2005 Takuro Ashie
* SPDX-FileCopyrightText: 2012-2017 CSSlayer <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
*/
#include "key2kana_table.h"
#include "style_file.h"
// fundamental table
static Key2KanaTable romaji_table(("DefaultRomajiTable"),
fcitx_anthy_romaji_typing_rule);
static Key2KanaTable
romaji_double_consonant_table(("DefaultRomajiDoubleConsonantTable"),
fcitx_anthy_romaji_double_consonant_rule);
static Key2KanaTable kana_table(("DefaultKanaTable"),
fcitx_anthy_kana_typing_rule);
static Key2KanaTable
kana_voiced_consonant_table(("DefaultKanaVoicedConsonantTable"),
fcitx_anthy_kana_voiced_consonant_rule);
static Key2KanaTable nicola_table(("DefaultNICOLATable"),
fcitx_anthy_nicola_table);
// symbols
static Key2KanaTable half_symbol_table(("DefaultRomajiHalfSymbolTable"),
fcitx_anthy_half_symbol_rule);
static Key2KanaTable wide_symbol_table(("DefaultRomajiWideSymbolTable"),
fcitx_anthy_wide_symbol_rule);
// numbers
static Key2KanaTable half_number_table(("DefaultRomajiHalfNumberTable"),
fcitx_anthy_half_number_rule);
static Key2KanaTable wide_number_table(("DefaultRomajiWideNumberTable"),
fcitx_anthy_wide_number_rule);
// period
static Key2KanaTable romaji_ja_period_table(("DefaultRomajiJaPeriodTable"),
fcitx_anthy_romaji_ja_period_rule);
static Key2KanaTable
romaji_wide_period_table(("DefaultRomajiWidePeriodTable"),
fcitx_anthy_romaji_wide_period_rule);
static Key2KanaTable
romaji_half_period_table(("DefaultRomajiHalfPeriodTable"),
fcitx_anthy_romaji_half_period_rule);
static Key2KanaTable kana_ja_period_table(("DefaultKanaJaPeriodTable"),
fcitx_anthy_kana_ja_period_rule);
static Key2KanaTable kana_wide_period_table(("DefaultKanaWidePeriodTable"),
fcitx_anthy_kana_wide_period_rule);
static Key2KanaTable kana_half_period_table(("DefaultKanaHalfPeriodTable"),
fcitx_anthy_kana_half_period_rule);
// comma
static Key2KanaTable romaji_ja_comma_table(("DefaultRomajiJaCommaTable"),
fcitx_anthy_romaji_ja_comma_rule);
static Key2KanaTable
romaji_wide_comma_table(("DefaultRomajiWideCommaTable"),
fcitx_anthy_romaji_wide_comma_rule);
static Key2KanaTable
romaji_half_comma_table(("DefaultRomajiHalfCommaTable"),
fcitx_anthy_romaji_half_comma_rule);
static Key2KanaTable kana_ja_comma_table(("DefaultKanaJaCommaTable"),
fcitx_anthy_kana_ja_comma_rule);
static Key2KanaTable kana_wide_comma_table(("DefaultKanaWideCommaTable"),
fcitx_anthy_kana_wide_comma_rule);
static Key2KanaTable kana_half_comma_table(("DefaultKanaHalfCommaTable"),
fcitx_anthy_kana_half_comma_rule);
// bracket
static Key2KanaTable
romaji_ja_bracket_table(("DefaultRomajiJaBracketTable"),
fcitx_anthy_romaji_ja_bracket_rule);
static Key2KanaTable
romaji_wide_bracket_table(("DefaultRomajiWideBracketTable"),
fcitx_anthy_romaji_wide_bracket_rule);
static Key2KanaTable kana_ja_bracket_table(("DefaultKanaJaBracketTable"),
fcitx_anthy_kana_ja_bracket_rule);
static Key2KanaTable
kana_wide_bracket_table(("DefaultRomajiWideBracketTable"),
fcitx_anthy_kana_wide_bracket_rule);
// slash
static Key2KanaTable romaji_ja_slash_table(("DefaultRomajiJaSlashTable"),
fcitx_anthy_romaji_ja_slash_rule);
static Key2KanaTable
romaji_wide_slash_table(("DefaultRomajiWideSlashTable"),
fcitx_anthy_romaji_wide_slash_rule);
static Key2KanaTable kana_ja_slash_table(("DefaultKanaJaSlashTable"),
fcitx_anthy_kana_ja_slash_rule);
static Key2KanaTable kana_wide_slash_table(("DefaultRomajiWideSlashTable"),
fcitx_anthy_kana_wide_slash_rule);
Key2KanaRule::Key2KanaRule() {}
void Key2KanaRule::load(std::string sequence, std::vector<std::string> result) {
sequence_ = std::move(sequence);
result_ = std::move(result);
}
Key2KanaRule::~Key2KanaRule() {}
const std::string &Key2KanaRule::sequence() const { return sequence_; }
std::string Key2KanaRule::result(unsigned int idx) const {
if (idx < result_.size())
return result_[idx];
return std::string();
}
void Key2KanaRule::clear() {
sequence_ = std::string();
result_.clear();
}
bool Key2KanaRule::isEmpty() {
if (!sequence_.empty())
return false;
if (result_.empty())
return true;
for (unsigned int i = 0; i < result_.size(); i++) {
if (!result_[i].empty())
return false;
}
return true;
}
Key2KanaTable::Key2KanaTable(std::string name) : name_(std::move(name)) {}
Key2KanaTable::Key2KanaTable(std::string name, ConvRule *table)
: name_(std::move(name)) {
for (unsigned int i = 0; table[i].string; i++) {
appendRule(table[i].string ? table[i].string : "",
table[i].result ? table[i].result : "",
table[i].cont ? table[i].cont : "");
}
}
Key2KanaTable::Key2KanaTable(std::string name, NicolaRule *table)
: name_(std::move(name)) {
for (unsigned int i = 0; table[i].key; i++) {
appendRule(table[i].key ? table[i].key : "",
table[i].single ? table[i].single : "",
table[i].left_shift ? table[i].left_shift : "",
table[i].right_shift ? table[i].right_shift : "");
}
}
Key2KanaTable::Key2KanaTable(std::string section, const StyleFile &styleFile)
: Key2KanaTable(styleFile.title()) {
std::vector<std::string> keys;
bool success = styleFile.getKeyList(keys, section);
if (success) {
std::vector<std::string>::iterator it;
for (it = keys.begin(); it != keys.end(); it++) {
std::vector<std::string> array;
styleFile.getStringArray(array, section, *it);
appendRule(*it, array);
}
}
}
void Key2KanaTable::appendRule(std::string sequence,
std::vector<std::string> result) {
rules_.emplace_back();
rules_.back().load(std::move(sequence), std::move(result));
}
void Key2KanaTable::appendRule(std::string sequence, std::string result,
std::string cont) {
std::vector<std::string> list;
list.push_back(std::move(result));
list.push_back(std::move(cont));
appendRule(std::move(sequence), std::move(list));
}
void Key2KanaTable::appendRule(std::string sequence, std::string normal,
std::string left_shift,
std::string right_shift) {
std::vector<std::string> list;
list.push_back(normal);
list.push_back(left_shift);
list.push_back(right_shift);
appendRule(std::move(sequence), std::move(list));
}
void Key2KanaTable::clear() { rules_.clear(); }
Key2KanaTableSet::Key2KanaTableSet()
: name_(""), fundamentalTable_(nullptr),
voicedConsonantTable_(Key2KanaTable("voiced consonant table")),
additionalTable_(nullptr), typingMethod_(TypingMethod::ROMAJI),
periodStyle_(PeriodStyle::JAPANESE), commaStyle_(CommaStyle::JAPANESE),
bracketStyle_(BracketStyle::JAPANESE), slashStyle_(SlashStyle::JAPANESE),
useHalfSymbol_(false), useHalfNumber_(false) {
setTypingMethod(typingMethod_);
}
Key2KanaTableSet::~Key2KanaTableSet() {}
void Key2KanaTableSet::setTypingMethod(TypingMethod method,
Key2KanaTable *fundamental_table) {
typingMethod_ = method;
fundamentalTable_ = fundamental_table;
resetTables();
}
void Key2KanaTableSet::setSymbolHalf(bool half) {
useHalfSymbol_ = half;
resetTables();
}
void Key2KanaTableSet::setNumberHalf(bool half) {
useHalfNumber_ = half;
resetTables();
}
void Key2KanaTableSet::setPeriodStyle(PeriodStyle style) {
periodStyle_ = style;
resetTables();
}
void Key2KanaTableSet::setCommaStyle(CommaStyle style) {
commaStyle_ = style;
resetTables();
}
void Key2KanaTableSet::setBracketStyle(BracketStyle style) {
bracketStyle_ = style;
resetTables();
}
void Key2KanaTableSet::setSlashStyle(SlashStyle style) {
slashStyle_ = style;
resetTables();
}
static void create_voiced_consonant_table(Key2KanaTable &table,
const Key2KanaTable &fund_table) {
table.clear();
const std::string sonant_mark = std::string("\xE3\x82\x9B");
const std::string half_sonant_mark = std::string("\xE3\x82\x9C");
std::vector<std::string> sonant_mark_list;
std::vector<std::string> half_sonant_mark_list;
Key2KanaRules::const_iterator it;
const Key2KanaRules &rules = fund_table.table();
for (it = rules.begin(); it != rules.end(); it++) {
std::string result = it->result(0);
if (result == sonant_mark)
sonant_mark_list.push_back(it->sequence());
else if (result == half_sonant_mark)
half_sonant_mark_list.push_back(it->sequence());
}
VoicedConsonantRule *templ = fcitx_anthy_voiced_consonant_table;
for (unsigned int i = 0; templ[i].string; i++) {
if (templ[i].voiced && *templ[i].voiced) {
std::vector<std::string>::iterator it;
for (it = sonant_mark_list.begin(); it != sonant_mark_list.end();
it++) {
table.appendRule(std::string(templ[i].string) + *it,
std::string(templ[i].voiced), std::string());
}
}
if (templ[i].half_voiced && *templ[i].half_voiced) {
std::vector<std::string>::iterator it;
for (it = half_sonant_mark_list.begin();
it != half_sonant_mark_list.end(); it++) {
table.appendRule(std::string(templ[i].string) + *it,
std::string(templ[i].half_voiced),
std::string());
}
}
}
}
void Key2KanaTableSet::resetTables() {
allTables_.clear();
bool is_romaji = typingMethod_ == TypingMethod::ROMAJI;
bool is_kana = typingMethod_ == TypingMethod::KANA;
bool is_nicola = typingMethod_ == TypingMethod::NICOLA;
// symbols table
if (useHalfSymbol_)
allTables_.push_back(&half_symbol_table);
else
allTables_.push_back(&wide_symbol_table);
// numbers table
if (useHalfNumber_)
allTables_.push_back(&half_number_table);
else
allTables_.push_back(&wide_number_table);
if (is_romaji || is_kana) {
switch (periodStyle_) {
case PeriodStyle::JAPANESE:
if (is_romaji)
allTables_.push_back(&romaji_ja_period_table);
else
allTables_.push_back(&kana_ja_period_table);
break;
case PeriodStyle::WIDE:
if (is_romaji)
allTables_.push_back(&romaji_wide_period_table);
else
allTables_.push_back(&kana_wide_period_table);
break;
case PeriodStyle::HALF:
if (is_romaji)
allTables_.push_back(&romaji_half_period_table);
else
allTables_.push_back(&kana_half_period_table);
break;
default:
break;
}
}
if (is_romaji || is_kana) {
switch (commaStyle_) {
case CommaStyle::JAPANESE:
if (is_romaji)
allTables_.push_back(&romaji_ja_comma_table);
else
allTables_.push_back(&kana_ja_comma_table);
break;
case CommaStyle::WIDE:
if (is_romaji)
allTables_.push_back(&romaji_wide_comma_table);
else
allTables_.push_back(&kana_wide_comma_table);
break;
case CommaStyle::HALF:
if (is_romaji)
allTables_.push_back(&romaji_half_comma_table);
else
allTables_.push_back(&kana_half_comma_table);
break;
default:
break;
}
}
if (is_romaji || is_kana) {
switch (bracketStyle_) {
case BracketStyle::JAPANESE:
if (is_romaji)
allTables_.push_back(&romaji_ja_bracket_table);
else
allTables_.push_back(&kana_ja_bracket_table);
break;
case BracketStyle::WIDE:
if (is_romaji)
allTables_.push_back(&romaji_wide_bracket_table);
else
allTables_.push_back(&kana_wide_bracket_table);
break;
default:
break;
}
}
if (is_romaji || is_kana) {
switch (slashStyle_) {
case SlashStyle::JAPANESE:
if (is_romaji)
allTables_.push_back(&romaji_ja_slash_table);
else
allTables_.push_back(&kana_ja_slash_table);
break;
case SlashStyle::WIDE:
if (is_romaji)
allTables_.push_back(&romaji_wide_slash_table);
else
allTables_.push_back(&kana_wide_slash_table);
break;
default:
break;
}
}
if (!fundamentalTable_) {
if (is_romaji) {
allTables_.push_back(&romaji_double_consonant_table);
allTables_.push_back(&romaji_table);
} else if (is_kana) {
create_voiced_consonant_table(voicedConsonantTable_, kana_table);
allTables_.push_back(&voicedConsonantTable_);
allTables_.push_back(&kana_table);
} else if (is_nicola) {
allTables_.push_back(&nicola_table);
}
} else {
if (is_romaji) {
allTables_.push_back(&romaji_double_consonant_table);
allTables_.push_back(fundamentalTable_);
} else if (is_kana) {
create_voiced_consonant_table(voicedConsonantTable_,
*fundamentalTable_);
allTables_.push_back(&voicedConsonantTable_);
allTables_.push_back(fundamentalTable_);
} else if (is_nicola) {
allTables_.push_back(fundamentalTable_);
}
}
}