forked from Hanjp-IM/libhanjp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhanjpinputcontext.c
305 lines (240 loc) · 7.81 KB
/
hanjpinputcontext.c
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
#include "hanjpinputcontext.h"
#include "hanjpautomata.h"
#include "hanjpkeyboard.h"
static gboolean is_hiragana(gunichar ch) {
return (ch >= 0x3041 && ch <= 0x3096) || (ch >= 0x309D && ch <= 0x309E);
}
static gboolean is_katakana(gunichar ch) {
return (ch >= 0x30A1 && ch <= 0x30F6) || (ch >= 0x30FD && ch <= 0x30FE);
}
#define KANA_GAP 0x60
typedef struct {
HanjpAutomata *ams[2];
HanjpAutomata *cur_am;
HanjpKeyboard *keyboard;
GArray *preedit;
gint kana_len;
GArray *committed;
GArray *hangul;
gint output_mode;
} HanjpInputContextPrivate;
G_DEFINE_TYPE_WITH_PRIVATE(HanjpInputContext, hanjp_ic, G_TYPE_OBJECT)
static void
hanjp_ic_dispose(GObject *self)
{
HanjpInputContextPrivate *priv;
priv = hanjp_ic_get_instance_private(HANJP_INPUTCONTEXT(self));
g_clear_object(&priv->ams[0]);
g_clear_object(&priv->ams[1]);
g_clear_object(&priv->cur_am);
g_clear_object(&priv->keyboard);
g_array_unref(priv->preedit);
priv->preedit = NULL;
g_array_unref(priv->committed);
priv->committed = NULL;
g_array_unref(priv->hangul);
priv->hangul = NULL;
//chain up
G_OBJECT_CLASS(hanjp_ic_parent_class)->dispose(self);
}
static void
hanjp_ic_finalize(GObject *self)
{
G_OBJECT_CLASS(hanjp_ic_parent_class)->finalize(self);
}
static void
hanjp_ic_class_init(HanjpInputContextClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS(klass);
object_class->dispose = hanjp_ic_dispose;
object_class->finalize = hanjp_ic_finalize;
}
static void
hanjp_ic_init(HanjpInputContext *self)
{
gint i;
HanjpInputContextPrivate *priv;
priv = hanjp_ic_get_instance_private(self);
priv->ams[0] = HANJP_AM(hanjp_am_builtin_new());
priv->ams[1] = NULL;
priv->cur_am = g_object_ref(priv->ams[0]);
priv->keyboard = HANJP_KB(hanjp_kb_builtin_new());
priv->preedit = g_array_sized_new(TRUE, TRUE, sizeof(gunichar), 64);
priv->committed = g_array_sized_new(TRUE, TRUE, sizeof(gunichar), 64);
priv->hangul = g_array_sized_new(TRUE, TRUE, sizeof(gunichar), 64);
priv->output_mode = HANJP_OUTPUT_HIRAGANA;
}
HanjpInputContext* hanjp_ic_new()
{
return g_object_new(HANJP_TYPE_INPUTCONTEXT, NULL);
}
void hanjp_ic_reset(HanjpInputContext *self)
{
HanjpInputContextPrivate *priv;
g_return_if_fail(HANJP_IS_INPUTCONTEXT(self));
priv = hanjp_ic_get_instance_private(self);
hanjp_am_flush(priv->cur_am);
g_array_set_size(priv->preedit, 0);
g_array_set_size(priv->committed, 0);
g_array_set_size(priv->hangul, 0);
priv->kana_len = 0;
priv->output_mode = HANJP_OUTPUT_HIRAGANA;
}
void hanjp_ic_flush(HanjpInputContext *self)
{
gint i;
HanjpInputContextPrivate *priv;
g_return_if_fail(HANJP_IS_INPUTCONTEXT(self));
priv = hanjp_ic_get_instance_private(self);
hanjp_am_flush(priv->cur_am);
for(i = 0; i < priv->preedit->len; i++) {
g_array_append_val(priv->committed, g_array_index(priv->preedit, gunichar, i));
}
g_array_set_size(priv->preedit, 0);
priv->kana_len = 0;
}
gint hanjp_ic_process(HanjpInputContext *self, gint ascii)
{
int i;
gunichar ch;
gint res;
HanjpInputContextPrivate *priv;
g_return_if_fail(HANJP_IS_INPUTCONTEXT(self));
priv = hanjp_ic_get_instance_private(self);
//map jaso from ascii
ch = hanjp_kb_get_mapping(priv->keyboard, 0, ascii);
if(ch == 0) {
ch = (gunichar)ascii;
}
//shrink preedit before push
g_array_set_size(priv->preedit, priv->kana_len);
g_array_set_size(priv->committed, 0);
g_array_set_size(priv->hangul, 0);
//push jaso into automata
res = hanjp_am_push(priv->cur_am, priv->preedit, priv->hangul, ch);
if(res < 0) {
priv->kana_len += -res;
hanjp_ic_flush(self);
}
else if(res > 0) {
// if mode is katakana, change result to katakana
if(priv->output_mode == HANJP_OUTPUT_KATAKANA) {
for(i = priv->kana_len; i < priv->preedit->len; i++) {
if(!is_hiragana(g_array_index(priv->preedit, gunichar, i))){
continue;
}
g_array_index(priv->preedit, gunichar, i) += KANA_GAP;
}
}
priv->kana_len += res;
}
return res;
}
void hanjp_ic_toggle_preedit(HanjpInputContext *self)
{
int i;
HanjpInputContextPrivate *priv;
g_return_if_fail(HANJP_IS_INPUTCONTEXT(self));
priv = hanjp_ic_get_instance_private(self);
for (i = 0; i < priv->preedit->len; i++) {
if (is_katakana(g_array_index(priv->preedit, gunichar, i))) {
g_array_index(priv->preedit, gunichar, i) += -KANA_GAP;
} else if (is_hiragana(g_array_index(priv->preedit, gunichar, i))) {
g_array_index(priv->preedit, gunichar, i) += KANA_GAP;
}
}
}
void hanjp_ic_to_haragana_preedit(HanjpInputContext *self)
{
int i;
HanjpInputContextPrivate *priv;
g_return_if_fail(HANJP_IS_INPUTCONTEXT(self));
priv = hanjp_ic_get_instance_private(self);
for (i = 0; i < priv->preedit->len; i++) {
if (is_katakana(g_array_index(priv->preedit, gunichar, i))) {
g_array_index(priv->preedit, gunichar, i) += -KANA_GAP;
}
}
}
void hanjp_ic_to_katakana_preedit(HanjpInputContext *self) {
int i;
HanjpInputContextPrivate *priv;
g_return_if_fail(HANJP_IS_INPUTCONTEXT(self));
priv = hanjp_ic_get_instance_private(self);
for (i = 0; i < priv->preedit->len; i++) {
if (is_hiragana(g_array_index(priv->preedit, gunichar, i))) {
g_array_index(priv->preedit, gunichar, i) += KANA_GAP;
}
}
}
void hanjp_ic_replace(HanjpInputContext *self, int start, int end, const gunichar* str_insert)
{
int i;
HanjpInputContextPrivate *priv;
g_return_if_fail(HANJP_IS_INPUTCONTEXT(self));
g_return_if_fail(str_insert != NULL);
g_return_if_fail(start <= end);
priv = hanjp_ic_get_instance_private(self);
g_array_remove_range(priv->preedit, start, end - start);
while (*str_insert)
{
g_array_insert_val(priv->preedit, start, *str_insert);
start++;
str_insert++;
}
}
gboolean hanjp_ic_backspace(HanjpInputContext *self)
{
g_return_if_fail(HANJP_IS_INPUTCONTEXT(self));
HanjpInputContextPrivate *priv;
priv = hanjp_ic_get_instance_private(self);
if(!hanjp_am_backspace(priv->cur_am)){
if((priv->preedit->len)==0)
{
return FALSE;
}
g_array_remove_index(priv->preedit,(priv->preedit->len)-1);
}
return TRUE;
}
GArray* hanjp_ic_ref_preedit_string(HanjpInputContext *self)
{
HanjpInputContextPrivate *priv;
g_return_if_fail(HANJP_IS_INPUTCONTEXT(self));
priv = hanjp_ic_get_instance_private(self);
return g_array_ref(priv->preedit);
}
GArray* hanjp_ic_ref_commit_string(HanjpInputContext *self)
{
HanjpInputContextPrivate *priv;
g_return_if_fail(HANJP_IS_INPUTCONTEXT(self));
priv = hanjp_ic_get_instance_private(self);
return g_array_ref(priv->committed);
}
GArray* hanjp_ic_ref_hangul_string(HanjpInputContext *self)
{
HanjpInputContextPrivate *priv;
g_return_if_fail(HANJP_IS_INPUTCONTEXT(self));
priv = hanjp_ic_get_instance_private(self);
return g_array_ref(priv->hangul);
}
void hanjp_ic_set_am(HanjpInputContext *self, gint i)
{
HanjpInputContextPrivate *priv;
g_return_if_fail(HANJP_IS_INPUTCONTEXT(self));
priv = hanjp_ic_get_instance_private(self);
g_object_unref(priv->cur_am);
priv->cur_am = g_object_ref(priv->ams[i]);
}
void hanjp_ic_set_output_mode(HanjpInputContext *self, gint i)
{
HanjpInputContextPrivate *priv;
g_return_if_fail(HANJP_IS_INPUTCONTEXT(self));
priv->output_mode = i;
}
gint hanjp_ic_get_output_mode(HanjpInputContext *self)
{
HanjpInputContextPrivate *priv;
g_return_if_fail(HANJP_IS_INPUTCONTEXT(self));
return priv->output_mode;
}