-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathkana.cpp
187 lines (149 loc) · 5.09 KB
/
kana.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
/*
* SPDX-FileCopyrightText: 2005 Takuro Ashie
* SPDX-FileCopyrightText: 2017-2017 CSSlayer <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
*/
#include "kana.h"
#include "default_tables.h"
#include "engine.h"
#include "state.h"
#include "utils.h"
static bool has_voiced_consonant(std::string str) {
VoicedConsonantRule *table = fcitx_anthy_voiced_consonant_table;
for (unsigned int i = 0; table[i].string; i++) {
if (!strcmp(str.c_str(), table[i].string) && table[i].voiced &&
*table[i].voiced) {
return true;
}
}
return false;
}
static bool has_half_voiced_consonant(std::string str) {
VoicedConsonantRule *table = fcitx_anthy_voiced_consonant_table;
for (unsigned int i = 0; table[i].string; i++) {
if (!strcmp(str.c_str(), table[i].string) && table[i].half_voiced &&
*table[i].half_voiced) {
return true;
}
}
return false;
}
std::string to_voiced_consonant(std::string str) {
VoicedConsonantRule *table = fcitx_anthy_voiced_consonant_table;
for (unsigned int i = 0; table[i].string; i++) {
if (!strcmp(str.c_str(), table[i].string))
return std::string(table[i].voiced);
}
return str;
}
std::string to_half_voiced_consonant(std::string str) {
VoicedConsonantRule *table = fcitx_anthy_voiced_consonant_table;
for (unsigned int i = 0; table[i].string; i++) {
if (!strcmp(str.c_str(), table[i].string))
return std::string(table[i].half_voiced);
}
return str;
}
KanaConvertor::KanaConvertor(AnthyState &anthy)
: Key2KanaConvertorBase(anthy) {}
KanaConvertor::~KanaConvertor() {}
bool KanaConvertor::canAppend(const fcitx::KeyEvent &key, bool) {
// ignore key release.
if (key.isRelease())
return false;
auto states = key.rawKey().states();
// ignore short cut keys of application.
if ((states & fcitx::KeyState::Ctrl) || (states & fcitx::KeyState::Alt) ||
(states & fcitx::KeyState::Super)) {
return false;
}
auto sym = key.rawKey().sym();
if (sym == FcitxKey_overline ||
(sym >= FcitxKey_kana_fullstop && sym <= FcitxKey_semivoicedsound)) {
return true;
}
#if 0
if (key.code == SCIM_KEY_KP_Equal ||
(key.code >= SCIM_KEY_KP_Multiply &&
key.code <= SCIM_KEY_KP_9))
{
return true;
}
#endif
return false;
}
bool KanaConvertor::append(const fcitx::KeyEvent &key, std::string &result,
std::string &pending, std::string &raw) {
KeyCodeToCharRule *table = fcitx_anthy_keypad_table;
auto sym = key.rawKey().sym();
// handle keypad code
if (sym == FcitxKey_KP_Equal ||
(sym >= FcitxKey_KP_Multiply && sym <= FcitxKey_KP_9)) {
TenKeyType ten_key_type = *config().general->tenKeyType;
for (unsigned int i = 0; table[i].code; i++) {
if (table[i].code == sym) {
if (ten_key_type == TenKeyType::WIDE)
result = util::convert_to_wide(table[i].kana);
else
result = table[i].kana;
raw = table[i].kana;
return false;
}
}
}
table = fcitx_anthy_kana_table;
// handle voiced sound
if (sym == FcitxKey_voicedsound && !pending_.empty() &&
has_voiced_consonant(pending_)) {
result = to_voiced_consonant(pending_);
raw = util::get_ascii_code(key);
pending_ = std::string();
return false;
}
// handle semi voiced sound
if (sym == FcitxKey_semivoicedsound && !pending_.empty() &&
has_half_voiced_consonant(pending_)) {
result = to_half_voiced_consonant(pending_);
raw = util::get_ascii_code(key);
pending_ = std::string();
return false;
}
// kana key code
for (unsigned int i = 0; table[i].code; i++) {
if (table[i].code == sym) {
bool retval = pending_.empty() ? false : true;
if (has_voiced_consonant(table[i].kana)) {
result = std::string();
pending = table[i].kana;
pending_ = table[i].kana;
} else {
result = table[i].kana;
pending_ = std::string();
}
raw = util::get_ascii_code(key);
return retval;
}
}
std::string s;
s += util::get_ascii_code(key);
raw = s;
return append(raw, result, pending);
}
bool KanaConvertor::append(const std::string &str, std::string &result,
std::string &) {
result = str;
pending_ = std::string();
return false;
}
void KanaConvertor::clear() { pending_ = std::string(); }
bool KanaConvertor::isPending() const { return !pending_.empty(); }
std::string KanaConvertor::pending() const { return pending_; }
std::string KanaConvertor::flushPending() { return std::string(); }
void KanaConvertor::resetPending(const std::string &result,
const std::string &) {
pending_ = std::string();
if (has_voiced_consonant(result))
pending_ = result;
}