Debian Codegraph @

src/key2kana.cpp

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * Copyright (C) 2004 Hiroyuki Ikezoe
4 * Copyright (C) 2004 Takuro Ashie
5 * Copyright (C) 2012 CSSlayer
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20 */
21
22 #include <fcitx/module/xkb/fcitx-xkb.h>
23 #include "key2kana.h"
24 #include "factory.h"
25 #include "imengine.h"
26 #include "utils.h"
27
28 Key2KanaConvertor::Key2KanaConvertor (AnthyInstance & anthy,
29 Key2KanaTableSet & tables)
30 : m_anthy (anthy),
31 m_tables (tables),
32 m_is_in_pseudo_ascii_mode (false)
33 {
34 set_case_sensitive (false);
35 set_pseudo_ascii_mode (0);
36 }
37
38 Key2KanaConvertor::~Key2KanaConvertor ()
39 {
40 }
41
42 bool
43 Key2KanaConvertor::can_append (const KeyEvent & key,
44 bool ignore_space)
45 {
46 // ignore key release.
47 if (key.is_release)
48 return false;
49
50 // ignore short cut keys of apllication.
51 if ((key.state & FcitxKeyState_Ctrl) ||
52 (key.state & FcitxKeyState_Alt) ||
53 (key.state & FcitxKeyState_Super))
54 {
55 return false;
56 }
57
58 if (isprint(key.get_ascii_code ()) &&
59 (ignore_space || !isspace(key.get_ascii_code ())))
60 return true;
61
62 if (util_key_is_keypad (key))
63 return true;
64
65 return false;
66 }
67
68 bool
69 Key2KanaConvertor::append (const KeyEvent & key,
70 std::string & result,
71 std::string & pending,
72 std::string &raw)
73 {
74 if (!can_append (key))
75 return false;
76
77 m_last_key = key;
78
79 util_keypad_to_string (raw, key);
80
81 if (util_key_is_keypad (key)) {
82 bool retval = false;
83 std::string wide;
84 TenKeyType ten_key_type = m_anthy.get_config()->m_ten_key_type;
85
86 // convert key pad string to wide
87 if ((ten_key_type == FCITX_ANTHY_TEN_KEY_TYPE_FOLLOWMODE &&
88 (m_anthy.get_input_mode () == FCITX_ANTHY_MODE_LATIN ||
89 m_anthy.get_input_mode () == FCITX_ANTHY_MODE_HALF_KATAKANA)) ||
90 ten_key_type == FCITX_ANTHY_TEN_KEY_TYPE_HALF)
91 {
92 wide = raw;
93 } else {
94 util_convert_to_wide (wide, raw);
95 }
96
97 // join to previous string
98 if (!m_exact_match.is_empty()) {
99 if (!m_exact_match.get_result(0).empty() &&
100 m_exact_match.get_result(1).empty())
101 {
102 result = m_exact_match.get_result(0);
103 } else {
104 retval = true; /* commit prev pending */
105 }
106 result += wide;
107 } else {
108 if (m_pending.length () > 0)
109 retval = true; /* commit prev pending */
110 result = wide;
111 }
112
113 m_pending.clear ();
114 m_exact_match.clear ();
115
116 return retval;
117
118 } else {
119 // the key isn't keypad
120 return append (raw, result, pending);
121 }
122 }
123
124 static
125 int split_string_list(std::vector<std::string>& vec, const std::string& str)
126 {
127 int count = 0;
128
129 std::string temp;
130 std::string::const_iterator bg, ed;
131
132 vec.clear ();
133
134 bg = str.begin ();
135 ed = str.begin ();
136
137 while (bg != str.end () && ed != str.end ()) {
138 for (; ed != str.end (); ++ed) {
139 if (*ed == ',')
140 break;
141 }
142 temp.assign (bg, ed);
143 vec.push_back (temp);
144 ++count;
145
146 if (ed != str.end ())
147 bg = ++ ed;
148 }
149 return count;
150 }
151
152 static inline bool CheckLayout(FcitxInstance* instance)
153 {
154 char *layout = NULL, *variant = NULL;
155 bool layout_is_jp = false;
156 FcitxXkbGetCurrentLayout(instance, &layout, &variant);
157 if (layout && strcmp(layout, "jp") == 0)
158 layout_is_jp = true;
159
160 fcitx_utils_free(layout);
161 fcitx_utils_free(variant);
162
163 return layout_is_jp;
164 }
165
166 bool
167 Key2KanaConvertor::append (const std::string & str,
168 std::string & result, std::string & pending)
169 {
170 std::string widestr = str;
171 std::string matching_str = m_pending + widestr;
172 Key2KanaRule exact_match;
173 bool has_partial_match = false;
174 bool retval = false;
175
176 if (m_pseudo_ascii_mode != 0 && process_pseudo_ascii_mode (widestr)) {
177 m_pending += widestr;
178 pending = m_pending;
179 return false;
180 }
181 if (!m_case_sensitive) {
182 std::string half = matching_str;
183 for (unsigned int i = 0; i < half.length (); i++)
184 half[i] = tolower (half[i]);
185 matching_str = half;
186 }
187
188 /* find matched table */
189 if ((m_anthy.get_typing_method () == FCITX_ANTHY_TYPING_METHOD_KANA) &&
190 (CheckLayout(m_anthy.get_owner())) &&
191 (m_last_key.sym == FcitxKey_backslash) &&
192 (!(m_last_key.keycode == 132 || m_last_key.keycode == 133)) &&
193 (m_anthy.get_config()->m_kana_layout_ro_key[0]))
194 {
195 // Special treatment for Kana "Ro" key.
196 // This code is a temporary solution. It doesn't care some minor cases.
197 std::vector<std::string> kana_ro_result;
198 split_string_list (kana_ro_result,
199 m_anthy.get_config()->m_kana_layout_ro_key);
200 Key2KanaRule kana_ro_rule("\\", kana_ro_result);
201 result = kana_ro_rule.get_result (0);
202 m_pending.clear ();
203 m_exact_match.clear ();
204 if (matching_str == "\\") {
205 return false;
206 } else {
207 return true;
208 }
209
210 } else {
211 std::vector<Key2KanaTable*> &tables = m_tables.get_tables();
212 for (unsigned int j = 0; j < tables.size(); j++) {
213 if (!tables[j])
214 continue;
215
216 Key2KanaRules &rules = tables[j]->get_table ();
217
218 for (unsigned int i = 0; i < rules.size(); i++) {
219 /* matching */
220 std::string seq = rules[i].get_sequence ();
221 if (!m_case_sensitive) {
222 for (unsigned int j = 0; j < seq.length (); j++)
223 seq[j] = tolower (seq[j]);
224 }
225 std::string romaji = seq;
226 if (romaji.find (matching_str) == 0) {
227 if (romaji.length () == matching_str.length ()) {
228 /* exact match */
229 exact_match = rules[i];
230 } else {
231 /* partial match */
232 has_partial_match = true;
233 }
234 }
235 }
236 }
237 }
238
239 /* return result */
240 if (has_partial_match) {
241 m_exact_match = exact_match;
242 result.clear ();
243 m_pending += widestr;
244 pending = m_pending;
245
246 } else if (!exact_match.is_empty()) {
247 if (!exact_match.get_result(1).empty())
248 m_exact_match = exact_match;
249 else
250 m_exact_match.clear ();
251 m_pending = exact_match.get_result (1);
252 result = exact_match.get_result (0);
253 pending = m_pending;
254
255 } else {
256 if (!m_exact_match.is_empty()) {
257 if (!m_exact_match.get_result(0).empty() &&
258 m_exact_match.get_result(1).empty())
259 {
260 result = m_exact_match.get_result(0);
261 } else {
262 retval = true; /* commit prev pending */
263 }
264 m_pending.clear ();
265 m_exact_match.clear ();
266
267 std::string tmp_result;
268 append(str, tmp_result, pending);
269 result += tmp_result;
270
271 } else {
272 if (m_pending.length () > 0) {
273 retval = true; /* commit prev pending */
274 m_pending = widestr;
275 pending = m_pending;
276
277 } else {
278 result = widestr;
279 pending.clear();
280 m_pending.clear ();
281 }
282 }
283 }
284
285 return retval;
286 }
287
288 void
289 Key2KanaConvertor::clear (void)
290 {
291 m_pending.clear ();
292 m_exact_match.clear ();
293 m_last_key = KeyEvent ();
294 reset_pseudo_ascii_mode();
295 }
296
297 bool
298 Key2KanaConvertor::is_pending (void)
299 {
300 if (m_pending.length () > 0)
301 return true;
302 else
303 return false;
304 }
305
306 std::string
307 Key2KanaConvertor::get_pending (void)
308 {
309 return m_pending;
310 }
311
312 std::string
313 Key2KanaConvertor::flush_pending (void)
314 {
315 std::string result;
316 if (!m_exact_match.is_empty ()) {
317 if (!m_exact_match.get_result(0).empty() &&
318 m_exact_match.get_result(1).empty())
319 {
320 result = m_exact_match.get_result(0);
321 } else if (!m_exact_match.get_result(1).empty()) {
322 result += m_exact_match.get_result(1);
323 } else if (m_pending.length () > 0) {
324 result += m_pending;
325 }
326 }
327 clear ();
328 return result;
329 }
330
331 void
332 Key2KanaConvertor::reset_pending (const std::string &result, const std::string &raw)
333 {
334 m_last_key = KeyEvent ();
335
336 for (unsigned int i = 0; i < util_utf8_string_length(raw); i++) {
337 std::string res, pend;
338 append (util_utf8_string_substr(raw, i, 1), res, pend);
339 }
340 }
341
342 bool
343 Key2KanaConvertor::process_pseudo_ascii_mode (const std::string & wstr)
344 {
345 for (unsigned int i = 0; i < wstr.length (); i++) {
346 if ((wstr[i] >= 'A' && wstr[i] <= 'Z') ||
347 isspace(wstr[i]))
348 {
349 m_is_in_pseudo_ascii_mode = true;
350 } else if (wstr[i] & 0x80) {
351 m_is_in_pseudo_ascii_mode = false;
352 }
353 }
354
355 return m_is_in_pseudo_ascii_mode;
356 }
357
358 void
359 Key2KanaConvertor::reset_pseudo_ascii_mode (void)
360 {
361 if (m_is_in_pseudo_ascii_mode)
362 m_pending.clear();
363 m_is_in_pseudo_ascii_mode = false;
364 }
365
366 /*
367 vi:ts=4:nowrap:ai:expandtab
368 */