| 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 <string.h> |
| 22 |
|
| 23 |
#include "kana.h" |
| 24 |
#include "factory.h" |
| 25 |
#include "imengine.h" |
| 26 |
#include "default_tables.h" |
| 27 |
#include "utils.h" |
| 28 |
|
| 29 |
static bool |
| 30 |
has_voiced_consonant (std::string str) |
| 31 |
{ |
| 32 |
VoicedConsonantRule *table = fcitx_anthy_voiced_consonant_table; |
| 33 |
|
| 34 |
for (unsigned int i = 0; table[i].string; i++) { |
| 35 |
if (!strcmp (str.c_str (), table[i].string) && |
| 36 |
table[i].voiced && *table[i].voiced) |
| 37 |
{ |
| 38 |
return true; |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
static bool |
| 46 |
has_half_voiced_consonant (std::string str) |
| 47 |
{ |
| 48 |
VoicedConsonantRule *table = fcitx_anthy_voiced_consonant_table; |
| 49 |
|
| 50 |
for (unsigned int i = 0; table[i].string; i++) { |
| 51 |
if (!strcmp (str.c_str (), table[i].string) && |
| 52 |
table[i].half_voiced && *table[i].half_voiced) |
| 53 |
{ |
| 54 |
return true; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
std::string |
| 62 |
to_voiced_consonant (std::string str) |
| 63 |
{ |
| 64 |
VoicedConsonantRule *table = fcitx_anthy_voiced_consonant_table; |
| 65 |
|
| 66 |
for (unsigned int i = 0; table[i].string; i++) { |
| 67 |
if (!strcmp (str.c_str (), table[i].string)) |
| 68 |
return std::string (table[i].voiced); |
| 69 |
} |
| 70 |
|
| 71 |
return str; |
| 72 |
} |
| 73 |
|
| 74 |
std::string |
| 75 |
to_half_voiced_consonant (std::string str) |
| 76 |
{ |
| 77 |
VoicedConsonantRule *table = fcitx_anthy_voiced_consonant_table; |
| 78 |
|
| 79 |
for (unsigned int i = 0; table[i].string; i++) { |
| 80 |
if (!strcmp (str.c_str (), table[i].string)) |
| 81 |
return std::string (table[i].half_voiced); |
| 82 |
} |
| 83 |
|
| 84 |
return str; |
| 85 |
} |
| 86 |
|
| 87 |
KanaConvertor::KanaConvertor (AnthyInstance &anthy) |
| 88 |
: m_anthy (anthy) |
| 89 |
{ |
| 90 |
} |
| 91 |
|
| 92 |
KanaConvertor::~KanaConvertor () |
| 93 |
{ |
| 94 |
} |
| 95 |
|
| 96 |
bool |
| 97 |
KanaConvertor::can_append (const KeyEvent & key, |
| 98 |
bool ignore_space) |
| 99 |
{ |
| 100 |
// ignore key release. |
| 101 |
if (key.is_release) |
| 102 |
return false; |
| 103 |
|
| 104 |
// ignore short cut keys of apllication. |
| 105 |
if ((key.state & FcitxKeyState_Ctrl) || |
| 106 |
(key.state & FcitxKeyState_Alt) || |
| 107 |
(key.state & FcitxKeyState_Super)) |
| 108 |
{ |
| 109 |
return false; |
| 110 |
} |
| 111 |
|
| 112 |
if (key.sym == FcitxKey_overline || |
| 113 |
(key.sym >= FcitxKey_kana_fullstop && |
| 114 |
key.sym <= FcitxKey_semivoicedsound)) |
| 115 |
{ |
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
#if 0 |
| 120 |
if (key.code == SCIM_KEY_KP_Equal || |
| 121 |
(key.code >= SCIM_KEY_KP_Multiply && |
| 122 |
key.code <= SCIM_KEY_KP_9)) |
| 123 |
{ |
| 124 |
return true; |
| 125 |
} |
| 126 |
#endif |
| 127 |
|
| 128 |
return false; |
| 129 |
} |
| 130 |
|
| 131 |
bool |
| 132 |
KanaConvertor::append (const KeyEvent & key, |
| 133 |
std::string & result, |
| 134 |
std::string & pending, |
| 135 |
std::string &raw) |
| 136 |
{ |
| 137 |
KeyCodeToCharRule *table = fcitx_anthy_keypad_table; |
| 138 |
|
| 139 |
// handle keypad code |
| 140 |
if (key.sym == FcitxKey_KP_Equal || |
| 141 |
(key.sym >= FcitxKey_KP_Multiply && |
| 142 |
key.sym <= FcitxKey_KP_9)) |
| 143 |
{ |
| 144 |
TenKeyType ten_key_type = m_anthy.get_config()->m_ten_key_type; |
| 145 |
|
| 146 |
for (unsigned int i = 0; table[i].code; i++) { |
| 147 |
if (table[i].code == key.sym) { |
| 148 |
if (ten_key_type == FCITX_ANTHY_TEN_KEY_TYPE_WIDE) |
| 149 |
util_convert_to_wide (result, table[i].kana); |
| 150 |
else |
| 151 |
result = table[i].kana; |
| 152 |
raw = table[i].kana; |
| 153 |
|
| 154 |
return false; |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
table = fcitx_anthy_kana_table; |
| 160 |
|
| 161 |
// handle voiced sound |
| 162 |
if (key.sym == FcitxKey_voicedsound && |
| 163 |
!m_pending.empty () && has_voiced_consonant (m_pending)) |
| 164 |
{ |
| 165 |
result = to_voiced_consonant (m_pending); |
| 166 |
raw = key.get_ascii_code (); |
| 167 |
m_pending = std::string (); |
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
// handle semi voiced sound |
| 172 |
if (key.sym == FcitxKey_semivoicedsound && |
| 173 |
!m_pending.empty () && has_half_voiced_consonant (m_pending)) |
| 174 |
{ |
| 175 |
result = to_half_voiced_consonant (m_pending); |
| 176 |
raw = key.get_ascii_code (); |
| 177 |
m_pending = std::string (); |
| 178 |
return false; |
| 179 |
} |
| 180 |
|
| 181 |
// kana key code |
| 182 |
for (unsigned int i = 0; table[i].code; i++) { |
| 183 |
if (table[i].code == key.sym) { |
| 184 |
bool retval = m_pending.empty () ? false : true; |
| 185 |
|
| 186 |
if (has_voiced_consonant (table[i].kana)) { |
| 187 |
result = std::string (); |
| 188 |
pending = table[i].kana; |
| 189 |
m_pending = table[i].kana; |
| 190 |
} else { |
| 191 |
result = table[i].kana; |
| 192 |
m_pending = std::string (); |
| 193 |
} |
| 194 |
raw = key.get_ascii_code (); |
| 195 |
|
| 196 |
return retval; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
std::string s; |
| 201 |
s += key.get_ascii_code (); |
| 202 |
raw = s; |
| 203 |
|
| 204 |
return append (raw, result, pending); |
| 205 |
} |
| 206 |
|
| 207 |
bool |
| 208 |
KanaConvertor::append (const std::string & str, |
| 209 |
std::string & result, |
| 210 |
std::string & pending) |
| 211 |
{ |
| 212 |
result = str; |
| 213 |
m_pending = std::string (); |
| 214 |
|
| 215 |
return false; |
| 216 |
} |
| 217 |
|
| 218 |
void |
| 219 |
KanaConvertor::clear (void) |
| 220 |
{ |
| 221 |
m_pending = std::string (); |
| 222 |
} |
| 223 |
|
| 224 |
bool |
| 225 |
KanaConvertor::is_pending (void) |
| 226 |
{ |
| 227 |
return !m_pending.empty (); |
| 228 |
} |
| 229 |
|
| 230 |
std::string |
| 231 |
KanaConvertor::get_pending (void) |
| 232 |
{ |
| 233 |
return std::string (m_pending); |
| 234 |
} |
| 235 |
|
| 236 |
std::string |
| 237 |
KanaConvertor::flush_pending (void) |
| 238 |
{ |
| 239 |
return std::string (); |
| 240 |
} |
| 241 |
|
| 242 |
void |
| 243 |
KanaConvertor::reset_pending (const std::string &result, const std::string &raw) |
| 244 |
{ |
| 245 |
m_pending = std::string (); |
| 246 |
if (has_voiced_consonant (result)) |
| 247 |
m_pending = result; |
| 248 |
} |
| 249 |
/* |
| 250 |
vi:ts=4:nowrap:ai:expandtab |
| 251 |
*/ |