Debian Codegraph @

unikey/ukengine.cpp

raw
1 // -*- mode:c++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 /* Unikey Vietnamese Input Method
3 * Copyright (C) 2000-2005 Pham Kim Long
4 * Contact:
5 * [email protected]
6 * UniKey project: http://unikey.org
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor
21 * Boston, MA 02110-1301, USA.
22 */
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <iostream>
27 #include "keycons.h"
28
29 /*
30 #if defined(_WIN32)
31 #include "keyhook.h"
32 #endif
33 */
34
35 #include "vnlexi.h"
36 #include "ukengine.h"
37
38 #include "charset.h"
39
40 using namespace std;
41
42 #define ENTER_CHAR 13
43 #define IS_ODD(x) (x & 1)
44 #define IS_EVEN(x) (!(x & 1))
45
46 #define IS_STD_VN_LOWER(x) ((x) >= VnStdCharOffset && (x) < (VnStdCharOffset + TOTAL_ALPHA_VNCHARS) && IS_ODD(x))
47 #define IS_STD_VN_UPPER(x) ((x) >= VnStdCharOffset && (x) < (VnStdCharOffset + TOTAL_ALPHA_VNCHARS) && IS_EVEN(x))
48
49 bool IsVnVowel[vnl_lastChar];
50
51 extern VnLexiName AZLexiUpper[]; //defined in inputproc.cpp
52 extern VnLexiName AZLexiLower[];
53
54 //see vnconv/data.cpp for explanation of these characters
55 unsigned char SpecialWesternChars[] = {
56 0x80, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88,
57 0x89, 0x8A, 0x8B, 0x8C, 0x8E, 0x91, 0x92, 0x93,
58 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B,
59 0x9C, 0x9E, 0x9F, 0x00};
60
61 StdVnChar IsoStdVnCharMap[256];
62
63 inline StdVnChar IsoToStdVnChar(int keyCode)
64 {
65 return (keyCode < 256)? IsoStdVnCharMap[keyCode] : keyCode;
66 }
67
68 struct VowelSeqInfo {
69 int len;
70 int complete;
71 int conSuffix; //allow consonnant suffix
72 VnLexiName v[3];
73 VowelSeq sub[3];
74
75 int roofPos;
76 VowelSeq withRoof;
77
78 int hookPos;
79 VowelSeq withHook; //hook & bowl
80 };
81
82 VowelSeqInfo VSeqList[] = {
83 {1, 1, 1, {vnl_a, vnl_nonVnChar, vnl_nonVnChar}, {vs_a, vs_nil, vs_nil}, -1, vs_ar, -1, vs_ab},
84 {1, 1, 1, {vnl_ar, vnl_nonVnChar, vnl_nonVnChar}, {vs_ar, vs_nil, vs_nil}, 0, vs_nil, -1, vs_ab},
85 {1, 1, 1, {vnl_ab, vnl_nonVnChar, vnl_nonVnChar}, {vs_ab, vs_nil, vs_nil}, -1, vs_ar, 0, vs_nil},
86 {1, 1, 1, {vnl_e, vnl_nonVnChar, vnl_nonVnChar}, {vs_e, vs_nil, vs_nil}, -1, vs_er, -1, vs_nil},
87 {1, 1, 1, {vnl_er, vnl_nonVnChar, vnl_nonVnChar}, {vs_er, vs_nil, vs_nil}, 0, vs_nil, -1, vs_nil},
88 {1, 1, 1, {vnl_i, vnl_nonVnChar, vnl_nonVnChar}, {vs_i, vs_nil, vs_nil}, -1, vs_nil, -1, vs_nil},
89 {1, 1, 1, {vnl_o, vnl_nonVnChar, vnl_nonVnChar}, {vs_o, vs_nil, vs_nil}, -1, vs_or, -1, vs_oh},
90 {1, 1, 1, {vnl_or, vnl_nonVnChar, vnl_nonVnChar}, {vs_or, vs_nil, vs_nil}, 0, vs_nil, -1, vs_oh},
91 {1, 1, 1, {vnl_oh, vnl_nonVnChar, vnl_nonVnChar}, {vs_oh, vs_nil, vs_nil}, -1, vs_or, 0, vs_nil},
92 {1, 1, 1, {vnl_u, vnl_nonVnChar, vnl_nonVnChar}, {vs_u, vs_nil, vs_nil}, -1, vs_nil, -1, vs_uh},
93 {1, 1, 1, {vnl_uh, vnl_nonVnChar, vnl_nonVnChar}, {vs_uh, vs_nil, vs_nil}, -1, vs_nil, 0, vs_nil},
94 {1, 1, 1, {vnl_y, vnl_nonVnChar, vnl_nonVnChar}, {vs_y, vs_nil, vs_nil}, -1, vs_nil, -1, vs_nil},
95 {2, 1, 0, {vnl_a, vnl_i, vnl_nonVnChar}, {vs_a, vs_ai, vs_nil}, -1, vs_nil, -1, vs_nil},
96 {2, 1, 0, {vnl_a, vnl_o, vnl_nonVnChar}, {vs_a, vs_ao, vs_nil}, -1, vs_nil, -1, vs_nil},
97 {2, 1, 0, {vnl_a, vnl_u, vnl_nonVnChar}, {vs_a, vs_au, vs_nil}, -1, vs_aru, -1, vs_nil},
98 {2, 1, 0, {vnl_a, vnl_y, vnl_nonVnChar}, {vs_a, vs_ay, vs_nil}, -1, vs_ary, -1, vs_nil},
99 {2, 1, 0, {vnl_ar, vnl_u, vnl_nonVnChar}, {vs_ar, vs_aru, vs_nil}, 0, vs_nil, -1, vs_nil},
100 {2, 1, 0, {vnl_ar, vnl_y, vnl_nonVnChar}, {vs_ar, vs_ary, vs_nil}, 0, vs_nil, -1, vs_nil},
101 {2, 1, 0, {vnl_e, vnl_o, vnl_nonVnChar}, {vs_e, vs_eo, vs_nil}, -1, vs_nil, -1, vs_nil},
102 {2, 0, 0, {vnl_e, vnl_u, vnl_nonVnChar}, {vs_e, vs_eu, vs_nil}, -1, vs_eru, -1, vs_nil},
103 {2, 1, 0, {vnl_er, vnl_u, vnl_nonVnChar}, {vs_er, vs_eru, vs_nil}, 0, vs_nil, -1, vs_nil},
104 {2, 1, 0, {vnl_i, vnl_a, vnl_nonVnChar}, {vs_i, vs_ia, vs_nil}, -1, vs_nil, -1, vs_nil},
105 {2, 0, 1, {vnl_i, vnl_e, vnl_nonVnChar}, {vs_i, vs_ie, vs_nil}, -1, vs_ier, -1, vs_nil},
106 {2, 1, 1, {vnl_i, vnl_er, vnl_nonVnChar}, {vs_i, vs_ier, vs_nil}, 1, vs_nil, -1, vs_nil},
107 {2, 1, 0, {vnl_i, vnl_u, vnl_nonVnChar}, {vs_i, vs_iu, vs_nil}, -1, vs_nil, -1, vs_nil},
108 {2, 1, 1, {vnl_o, vnl_a, vnl_nonVnChar}, {vs_o, vs_oa, vs_nil}, -1, vs_nil, -1, vs_oab},
109 {2, 1, 1, {vnl_o, vnl_ab, vnl_nonVnChar}, {vs_o, vs_oab, vs_nil}, -1, vs_nil, 1, vs_nil},
110 {2, 1, 1, {vnl_o, vnl_e, vnl_nonVnChar}, {vs_o, vs_oe, vs_nil}, -1, vs_nil, -1, vs_nil},
111 {2, 1, 0, {vnl_o, vnl_i, vnl_nonVnChar}, {vs_o, vs_oi, vs_nil}, -1, vs_ori, -1, vs_ohi},
112 {2, 1, 0, {vnl_or, vnl_i, vnl_nonVnChar}, {vs_or, vs_ori, vs_nil}, 0, vs_nil, -1, vs_ohi},
113 {2, 1, 0, {vnl_oh, vnl_i, vnl_nonVnChar}, {vs_oh, vs_ohi, vs_nil}, -1, vs_ori, 0, vs_nil},
114 {2, 1, 1, {vnl_u, vnl_a, vnl_nonVnChar}, {vs_u, vs_ua, vs_nil}, -1, vs_uar, -1, vs_uha},
115 {2, 1, 1, {vnl_u, vnl_ar, vnl_nonVnChar}, {vs_u, vs_uar, vs_nil}, 1, vs_nil, -1, vs_nil},
116 {2, 0, 1, {vnl_u, vnl_e, vnl_nonVnChar}, {vs_u, vs_ue, vs_nil}, -1, vs_uer, -1, vs_nil},
117 {2, 1, 1, {vnl_u, vnl_er, vnl_nonVnChar}, {vs_u, vs_uer, vs_nil}, 1, vs_nil, -1, vs_nil},
118 {2, 1, 0, {vnl_u, vnl_i, vnl_nonVnChar}, {vs_u, vs_ui, vs_nil}, -1, vs_nil, -1, vs_uhi},
119 {2, 0, 1, {vnl_u, vnl_o, vnl_nonVnChar}, {vs_u, vs_uo, vs_nil}, -1, vs_uor, -1, vs_uho},
120 {2, 1, 1, {vnl_u, vnl_or, vnl_nonVnChar}, {vs_u, vs_uor, vs_nil}, 1, vs_nil, -1, vs_uoh},
121 {2, 1, 1, {vnl_u, vnl_oh, vnl_nonVnChar}, {vs_u, vs_uoh, vs_nil}, -1, vs_uor, 1, vs_uhoh},
122 {2, 0, 0, {vnl_u, vnl_u, vnl_nonVnChar}, {vs_u, vs_uu, vs_nil}, -1, vs_nil, -1, vs_uhu},
123 {2, 1, 1, {vnl_u, vnl_y, vnl_nonVnChar}, {vs_u, vs_uy, vs_nil}, -1, vs_nil, -1, vs_nil},
124 {2, 1, 0, {vnl_uh, vnl_a, vnl_nonVnChar}, {vs_uh, vs_uha, vs_nil}, -1, vs_nil, 0, vs_nil},
125 {2, 1, 0, {vnl_uh, vnl_i, vnl_nonVnChar}, {vs_uh, vs_uhi, vs_nil}, -1, vs_nil, 0, vs_nil},
126 {2, 0, 1, {vnl_uh, vnl_o, vnl_nonVnChar}, {vs_uh, vs_uho, vs_nil}, -1, vs_nil, 0, vs_uhoh},
127 {2, 1, 1, {vnl_uh, vnl_oh, vnl_nonVnChar}, {vs_uh, vs_uhoh, vs_nil}, -1, vs_nil, 0, vs_nil},
128 {2, 1, 0, {vnl_uh, vnl_u, vnl_nonVnChar}, {vs_uh, vs_uhu, vs_nil}, -1, vs_nil, 0, vs_nil},
129 {2, 0, 1, {vnl_y, vnl_e, vnl_nonVnChar}, {vs_y, vs_ye, vs_nil}, -1, vs_yer, -1, vs_nil},
130 {2, 1, 1, {vnl_y, vnl_er, vnl_nonVnChar}, {vs_y, vs_yer, vs_nil}, 1, vs_nil, -1, vs_nil},
131 {3, 0, 0, {vnl_i, vnl_e, vnl_u}, {vs_i, vs_ie, vs_ieu}, -1, vs_ieru, -1, vs_nil},
132 {3, 1, 0, {vnl_i, vnl_er, vnl_u}, {vs_i, vs_ier, vs_ieru}, 1, vs_nil, -1, vs_nil},
133 {3, 1, 0, {vnl_o, vnl_a, vnl_i}, {vs_o, vs_oa, vs_oai}, -1, vs_nil, -1, vs_nil},
134 {3, 1, 0, {vnl_o, vnl_a, vnl_y}, {vs_o, vs_oa, vs_oay}, -1, vs_nil, -1, vs_nil},
135 {3, 1, 0, {vnl_o, vnl_e, vnl_o}, {vs_o, vs_oe, vs_oeo}, -1, vs_nil, -1, vs_nil},
136 {3, 0, 0, {vnl_u, vnl_a, vnl_y}, {vs_u, vs_ua, vs_uay}, -1, vs_uary, -1, vs_nil},
137 {3, 1, 0, {vnl_u, vnl_ar, vnl_y}, {vs_u, vs_uar, vs_uary}, 1, vs_nil, -1, vs_nil},
138 {3, 0, 0, {vnl_u, vnl_o, vnl_i}, {vs_u, vs_uo, vs_uoi}, -1, vs_uori, -1, vs_uhoi},
139 {3, 0, 0, {vnl_u, vnl_o, vnl_u}, {vs_u, vs_uo, vs_uou}, -1, vs_nil, -1, vs_uhou},
140 {3, 1, 0, {vnl_u, vnl_or, vnl_i}, {vs_u, vs_uor, vs_uori}, 1, vs_nil, -1, vs_uohi},
141 {3, 0, 0, {vnl_u, vnl_oh, vnl_i}, {vs_u, vs_uoh, vs_uohi}, -1, vs_uori, 1, vs_uhohi},
142 {3, 0, 0, {vnl_u, vnl_oh, vnl_u}, {vs_u, vs_uoh, vs_uohu}, -1, vs_nil, 1, vs_uhohu},
143 {3, 1, 0, {vnl_u, vnl_y, vnl_a}, {vs_u, vs_uy, vs_uya}, -1, vs_nil, -1, vs_nil},
144 {3, 0, 1, {vnl_u, vnl_y, vnl_e}, {vs_u, vs_uy, vs_uye}, -1, vs_uyer, -1, vs_nil},
145 {3, 1, 1, {vnl_u, vnl_y, vnl_er}, {vs_u, vs_uy, vs_uyer}, 2, vs_nil, -1, vs_nil},
146 {3, 1, 0, {vnl_u, vnl_y, vnl_u}, {vs_u, vs_uy, vs_uyu}, -1, vs_nil, -1, vs_nil},
147 {3, 0, 0, {vnl_uh, vnl_o, vnl_i}, {vs_uh, vs_uho, vs_uhoi}, -1, vs_nil, 0, vs_uhohi},
148 {3, 0, 0, {vnl_uh, vnl_o, vnl_u}, {vs_uh, vs_uho, vs_uhou}, -1, vs_nil, 0, vs_uhohu},
149 {3, 1, 0, {vnl_uh, vnl_oh, vnl_i}, {vs_uh, vs_uhoh, vs_uhohi}, -1, vs_nil, 0, vs_nil},
150 {3, 1, 0, {vnl_uh, vnl_oh, vnl_u}, {vs_uh, vs_uhoh, vs_uhohu}, -1, vs_nil, 0, vs_nil},
151 {3, 0, 0, {vnl_y, vnl_e, vnl_u}, {vs_y, vs_ye, vs_yeu}, -1, vs_yeru, -1, vs_nil},
152 {3, 1, 0, {vnl_y, vnl_er, vnl_u}, {vs_y, vs_yer, vs_yeru}, 1, vs_nil, -1, vs_nil}
153 };
154
155 struct ConSeqInfo {
156 int len;
157 VnLexiName c[3];
158 bool suffix;
159 };
160
161 ConSeqInfo CSeqList[] = {
162 {1, {vnl_b, vnl_nonVnChar, vnl_nonVnChar}, false},
163 {1, {vnl_c, vnl_nonVnChar, vnl_nonVnChar}, true},
164 {2, {vnl_c, vnl_h, vnl_nonVnChar}, true},
165 {1, {vnl_d, vnl_nonVnChar, vnl_nonVnChar}, false},
166 {1, {vnl_dd, vnl_nonVnChar, vnl_nonVnChar}, false},
167 {2, {vnl_d, vnl_z, vnl_nonVnChar}, false},
168 {1, {vnl_g, vnl_nonVnChar, vnl_nonVnChar}, false},
169 {2, {vnl_g, vnl_h, vnl_nonVnChar}, false},
170 {2, {vnl_g, vnl_i, vnl_nonVnChar}, false},
171 {3, {vnl_g, vnl_i, vnl_n}, false},
172 {1, {vnl_h, vnl_nonVnChar, vnl_nonVnChar}, false},
173 {1, {vnl_k, vnl_nonVnChar, vnl_nonVnChar}, false},
174 {2, {vnl_k, vnl_h, vnl_nonVnChar}, false},
175 {1, {vnl_l, vnl_nonVnChar, vnl_nonVnChar}, false},
176 {1, {vnl_m, vnl_nonVnChar, vnl_nonVnChar}, true},
177 {1, {vnl_n, vnl_nonVnChar, vnl_nonVnChar}, true},
178 {2, {vnl_n, vnl_g, vnl_nonVnChar}, true},
179 {3, {vnl_n, vnl_g, vnl_h}, false},
180 {2, {vnl_n, vnl_h, vnl_nonVnChar}, true},
181 {1, {vnl_p, vnl_nonVnChar, vnl_nonVnChar}, true},
182 {2, {vnl_p, vnl_h, vnl_nonVnChar}, false},
183 {1, {vnl_q, vnl_nonVnChar, vnl_nonVnChar}, false},
184 {2, {vnl_q, vnl_u, vnl_nonVnChar}, false},
185 {1, {vnl_r, vnl_nonVnChar, vnl_nonVnChar}, false},
186 {1, {vnl_s, vnl_nonVnChar, vnl_nonVnChar}, false},
187 {1, {vnl_t, vnl_nonVnChar, vnl_nonVnChar}, true},
188 {2, {vnl_t, vnl_h, vnl_nonVnChar}, false},
189 {2, {vnl_t, vnl_r, vnl_nonVnChar}, false},
190 {1, {vnl_v, vnl_nonVnChar, vnl_nonVnChar}, false},
191 {1, {vnl_x, vnl_nonVnChar, vnl_nonVnChar}, false}
192 };
193
194 const int VSeqCount = sizeof(VSeqList)/sizeof(VowelSeqInfo);
195 struct VSeqPair {
196 VnLexiName v[3];
197 VowelSeq vs;
198 };
199 VSeqPair SortedVSeqList[VSeqCount];
200
201 const int CSeqCount = sizeof(CSeqList)/sizeof(ConSeqInfo);
202 struct CSeqPair {
203 VnLexiName c[3];
204 ConSeq cs;
205 };
206 CSeqPair SortedCSeqList[CSeqCount];
207
208 struct VCPair {
209 VowelSeq v;
210 ConSeq c;
211 };
212
213 VCPair VCPairList [] = {
214 {vs_a, cs_c}, {vs_a, cs_ch}, {vs_a, cs_m}, {vs_a, cs_n}, {vs_a, cs_ng},
215 {vs_a, cs_nh}, {vs_a, cs_p}, {vs_a, cs_t},
216 {vs_ar, cs_c}, {vs_ar, cs_m}, {vs_ar, cs_n}, {vs_ar, cs_ng}, {vs_ar, cs_p}, {vs_ar, cs_t},
217 {vs_ab, cs_c}, {vs_ab, cs_m}, {vs_ab, cs_n}, {vs_ab, cs_ng}, {vs_ab, cs_p}, {vs_ab, cs_t},
218
219 {vs_e, cs_c}, {vs_e, cs_ch}, {vs_e, cs_m}, {vs_e, cs_n}, {vs_e, cs_ng},
220 {vs_e, cs_nh}, {vs_e, cs_p}, {vs_e, cs_t},
221 {vs_er, cs_c}, {vs_er, cs_ch}, {vs_er, cs_m}, {vs_er, cs_n}, {vs_er, cs_nh},
222 {vs_er, cs_p}, {vs_er, cs_t},
223
224 {vs_i, cs_c}, {vs_i, cs_ch}, {vs_i, cs_m}, {vs_i, cs_n}, {vs_i, cs_nh}, {vs_i, cs_p}, {vs_i, cs_t},
225
226 {vs_o, cs_c}, {vs_o, cs_m}, {vs_o, cs_n}, {vs_o, cs_ng}, {vs_o, cs_p}, {vs_o, cs_t},
227 {vs_or, cs_c}, {vs_or, cs_m}, {vs_or, cs_n}, {vs_or, cs_ng}, {vs_or, cs_p}, {vs_or, cs_t},
228 {vs_oh, cs_m}, {vs_oh, cs_n}, {vs_oh, cs_p}, {vs_oh, cs_t},
229
230 {vs_u, cs_c}, {vs_u, cs_m}, {vs_u, cs_n}, {vs_u, cs_ng}, {vs_u, cs_p}, {vs_u, cs_t},
231 {vs_uh, cs_c}, {vs_uh, cs_m}, {vs_uh, cs_n}, {vs_uh, cs_ng}, {vs_uh, cs_t},
232
233 {vs_y, cs_t},
234 {vs_ie, cs_c}, {vs_ie, cs_m}, {vs_ie, cs_n}, {vs_ie, cs_ng}, {vs_ie, cs_p}, {vs_ie, cs_t},
235 {vs_ier, cs_c}, {vs_ier, cs_m}, {vs_ier, cs_n}, {vs_ier, cs_ng}, {vs_ier, cs_p}, {vs_ier, cs_t},
236
237 {vs_oa, cs_c}, {vs_oa, cs_ch}, {vs_oa, cs_m}, {vs_oa, cs_n}, {vs_oa, cs_ng},
238 {vs_oa, cs_nh}, {vs_oa, cs_p}, {vs_oa, cs_t},
239 {vs_oab, cs_c}, {vs_oab, cs_m}, {vs_oab, cs_n}, {vs_oab, cs_ng}, {vs_oab, cs_t},
240
241 {vs_oe, cs_n}, {vs_oe, cs_t},
242
243 {vs_ua, cs_n}, {vs_ua, cs_ng}, {vs_ua, cs_t},
244 {vs_uar, cs_n}, {vs_uar, cs_ng}, {vs_uar, cs_t},
245
246 {vs_ue, cs_c}, {vs_ue, cs_ch}, {vs_ue, cs_n}, {vs_ue, cs_nh},
247 {vs_uer, cs_c}, {vs_uer, cs_ch}, {vs_uer, cs_n}, {vs_uer, cs_nh},
248
249 {vs_uo, cs_c}, {vs_uo, cs_m}, {vs_uo, cs_n}, {vs_uo, cs_ng}, {vs_uo, cs_p}, {vs_uo, cs_t},
250 {vs_uor, cs_c}, {vs_uor, cs_m}, {vs_uor, cs_n}, {vs_uor, cs_ng}, {vs_uor, cs_t},
251 {vs_uho, cs_c}, {vs_uho, cs_m}, {vs_uho, cs_n}, {vs_uho, cs_ng}, {vs_uho, cs_p}, {vs_uho, cs_t},
252 {vs_uhoh, cs_c}, {vs_uhoh, cs_m}, {vs_uhoh, cs_n}, {vs_uhoh, cs_ng}, {vs_uhoh, cs_p}, {vs_uhoh, cs_t},
253
254 {vs_uy, cs_c}, {vs_uy, cs_ch}, {vs_uy, cs_n}, {vs_uy, cs_nh}, {vs_uy, cs_p}, {vs_uy, cs_t},
255
256 {vs_ye, cs_m}, {vs_ye, cs_n}, {vs_ye, cs_ng}, {vs_ye, cs_p}, {vs_ye, cs_t},
257 {vs_yer, cs_m}, {vs_yer, cs_n}, {vs_yer, cs_ng}, {vs_yer, cs_t},
258
259 {vs_uye, cs_n}, {vs_uye, cs_t},
260 {vs_uyer, cs_n}, {vs_uyer, cs_t}
261
262 };
263
264 const int VCPairCount = sizeof(VCPairList)/sizeof(VCPair);
265
266 //TODO: auto-complete: e.g. luan -> lua^n
267
268 typedef int (UkEngine::* UkKeyProc)(UkKeyEvent & ev);
269
270 UkKeyProc UkKeyProcList[vneCount] = {
271 &UkEngine::processRoof, //vneRoofAll
272 &UkEngine::processRoof, //vneRoof_a
273 &UkEngine::processRoof, //vneRoof_e
274 &UkEngine::processRoof, //vneRoof_o
275 &UkEngine::processHook, //vneHookAll
276 &UkEngine::processHook, //vneHook_uo
277 &UkEngine::processHook, //vneHook_u
278 &UkEngine::processHook, //vneHook_o
279 &UkEngine::processHook, //vneBowl
280 &UkEngine::processDd, //vneDd
281 &UkEngine::processTone, //vneTone0
282 &UkEngine::processTone, //vneTone1
283 &UkEngine::processTone, //vneTone2
284 &UkEngine::processTone, //vneTone3
285 &UkEngine::processTone, //vneTone4
286 &UkEngine::processTone, //vneTone5
287 &UkEngine::processTelexW, //vne_telex_w
288 &UkEngine::processMapChar, //vneMapChar
289 &UkEngine::processEscChar, //vneEscChar
290 &UkEngine::processAppend //vneNormal
291 };
292
293
294 VowelSeq lookupVSeq(VnLexiName v1, VnLexiName v2 = vnl_nonVnChar, VnLexiName v3 = vnl_nonVnChar);
295 ConSeq lookupCSeq(VnLexiName c1, VnLexiName c2 = vnl_nonVnChar, VnLexiName c3 = vnl_nonVnChar);
296
297 bool UkEngine::m_classInit = false;
298
299 //------------------------------------------------
300 int tripleVowelCompare(const void *p1, const void *p2)
301 {
302 VSeqPair *t1 = (VSeqPair *)p1;
303 VSeqPair *t2 = (VSeqPair *)p2;
304
305 for (int i=0; i<3; i++) {
306 if (t1->v[i] < t2->v[i])
307 return -1;
308 if (t1->v[i] > t2->v[i])
309 return 1;
310 }
311 return 0;
312 }
313
314 //------------------------------------------------
315 int tripleConCompare(const void *p1, const void *p2)
316 {
317 CSeqPair *t1 = (CSeqPair *)p1;
318 CSeqPair *t2 = (CSeqPair *)p2;
319
320 for (int i=0; i<3; i++) {
321 if (t1->c[i] < t2->c[i])
322 return -1;
323 if (t1->c[i] > t2->c[i])
324 return 1;
325 }
326 return 0;
327 }
328
329 //------------------------------------------------
330 int VCPairCompare(const void *p1, const void *p2)
331 {
332 VCPair *t1 = (VCPair *)p1;
333 VCPair *t2 = (VCPair *)p2;
334
335 if (t1->v < t2->v)
336 return -1;
337 if (t1->v > t2->v)
338 return 1;
339
340 if (t1->c < t2->c)
341 return -1;
342 if (t1->c > t2->c)
343 return 1;
344 return 0;
345 }
346
347 //----------------------------------------------------------
348 bool isValidCV(ConSeq c, VowelSeq v)
349 {
350 if (c == cs_nil || v == vs_nil)
351 return true;
352
353 VowelSeqInfo & vInfo = VSeqList[v];
354
355 if ((c == cs_gi && vInfo.v[0] == vnl_i) ||
356 (c == cs_qu && vInfo.v[0] == vnl_u))
357 return false; // gi doesn't go with i, qu doesn't go with u
358
359 if (c == cs_k) {
360 // k can only go with the following vowel sequences
361 static VowelSeq kVseq[] = {vs_e, vs_i, vs_y, vs_er, vs_eo, vs_eu,
362 vs_eru, vs_ia, vs_ie, vs_ier, vs_ieu, vs_ieru, vs_nil};
363 int i;
364 for (i=0; kVseq[i] != vs_nil && kVseq[i] != v; i++);
365 return (kVseq[i] != vs_nil);
366 }
367
368 //More checks
369 return true;
370 }
371
372 //----------------------------------------------------------
373 bool isValidVC(VowelSeq v, ConSeq c)
374 {
375 if (v == vs_nil || c == cs_nil)
376 return true;
377
378 VowelSeqInfo & vInfo = VSeqList[v];
379 if (!vInfo.conSuffix)
380 return false;
381
382 ConSeqInfo & cInfo = CSeqList[c];
383 if (!cInfo.suffix)
384 return false;
385
386 VCPair p;
387 p.v = v;
388 p.c = c;
389 if (bsearch(&p, VCPairList, VCPairCount, sizeof(VCPair), VCPairCompare))
390 return true;
391
392 return false;
393 }
394
395 //----------------------------------------------------------
396 bool isValidCVC(ConSeq c1, VowelSeq v, ConSeq c2)
397 {
398 if (v == vs_nil)
399 return (c1 == cs_nil || c2 != cs_nil);
400
401 if (c1 == cs_nil)
402 return isValidVC(v, c2);
403
404 if (c2 == cs_nil)
405 return isValidCV(c1, v);
406
407 bool okCV = isValidCV(c1, v);
408 bool okVC = isValidVC(v, c2);
409
410 if (okCV && okVC)
411 return true;
412
413 if (!okVC) {
414 //check some exceptions: vc fails but cvc passes
415
416 // quyn, quynh
417 if (c1 == cs_qu && v == vs_y && (c2 == cs_n || c2 == cs_nh))
418 return true;
419
420 // gieng, gie^ng
421 if (c1 == cs_gi && (v == vs_e || v == vs_er) && (c2 == cs_n || c2 == cs_ng))
422 return true;
423 }
424 return false;
425 }
426
427 //------------------------------------------------
428 void engineClassInit()
429 {
430 int i, j;
431
432 for (i=0; i < VSeqCount; i++) {
433 for (j=0; j<3; j++)
434 SortedVSeqList[i].v[j] = VSeqList[i].v[j];
435 SortedVSeqList[i].vs = (VowelSeq)i;
436 }
437
438 for (i=0; i < CSeqCount; i++) {
439 for (j=0; j<3; j++)
440 SortedCSeqList[i].c[j] = CSeqList[i].c[j];
441 SortedCSeqList[i].cs = (ConSeq)i;
442 }
443
444 qsort(SortedVSeqList, VSeqCount, sizeof(VSeqPair), tripleVowelCompare);
445 qsort(SortedCSeqList, CSeqCount, sizeof(CSeqPair), tripleConCompare);
446 qsort(VCPairList, VCPairCount, sizeof(VCPair), VCPairCompare);
447
448 for (i=0; i<vnl_lastChar; i++)
449 IsVnVowel[i] = true;
450
451 unsigned char ch;
452 for (ch='a'; ch <= 'z'; ch++) {
453 if (ch != 'a' && ch != 'e' && ch != 'i' &&
454 ch != 'o' && ch != 'u' && ch != 'y') {
455 IsVnVowel[AZLexiLower[ch-'a']] = false;
456 IsVnVowel[AZLexiUpper[ch-'a']] = false;
457 }
458 }
459 IsVnVowel[vnl_dd] = false;
460 IsVnVowel[vnl_DD] = false;
461 }
462
463 //------------------------------------------------
464 VowelSeq lookupVSeq(VnLexiName v1, VnLexiName v2, VnLexiName v3)
465 {
466 VSeqPair key;
467 key.v[0] = v1;
468 key.v[1] = v2;
469 key.v[2] = v3;
470
471 VSeqPair *pInfo = (VSeqPair *)bsearch(&key, SortedVSeqList, VSeqCount, sizeof(VSeqPair), tripleVowelCompare);
472 if (pInfo == 0)
473 return vs_nil;
474 return pInfo->vs;
475 }
476
477 //------------------------------------------------
478 ConSeq lookupCSeq(VnLexiName c1, VnLexiName c2, VnLexiName c3)
479 {
480 CSeqPair key;
481 key.c[0] = c1;
482 key.c[1] = c2;
483 key.c[2] = c3;
484
485 CSeqPair *pInfo = (CSeqPair *)bsearch(&key, SortedCSeqList, CSeqCount, sizeof(CSeqPair), tripleConCompare);
486 if (pInfo == 0)
487 return cs_nil;
488 return pInfo->cs;
489 }
490
491 //------------------------------------------------------------------
492 int UkEngine::processRoof(UkKeyEvent & ev)
493 {
494 if (!m_pCtrl->vietKey || m_current < 0 || m_buffer[m_current].vOffset < 0)
495 return processAppend(ev);
496
497 VnLexiName target;
498 switch (ev.evType) {
499 case vneRoof_a:
500 target = vnl_ar;
501 break;
502 case vneRoof_e:
503 target = vnl_er;
504 break;
505 case vneRoof_o:
506 target = vnl_or;
507 break;
508 default:
509 target = vnl_nonVnChar;
510 }
511
512
513 VowelSeq vs, newVs;
514 int i, vStart, vEnd;
515 int curTonePos, newTonePos, tone;
516 int changePos;
517 bool roofRemoved = false;
518
519 vEnd = m_current - m_buffer[m_current].vOffset;
520 vs = m_buffer[vEnd].vseq;
521 vStart = vEnd - (VSeqList[vs].len - 1);
522 curTonePos = vStart + getTonePosition(vs, vEnd == m_current);
523 tone = m_buffer[curTonePos].tone;
524
525 bool doubleChangeUO = false;
526 if (vs == vs_uho || vs == vs_uhoh || vs == vs_uhoi || vs == vs_uhohi) {
527 //special cases: u+o+ -> uo^, u+o -> uo^, u+o+i -> uo^i, u+oi -> uo^i
528 newVs = lookupVSeq(vnl_u, vnl_or, VSeqList[vs].v[2]);
529 doubleChangeUO = true;
530 }
531 else {
532 newVs = VSeqList[vs].withRoof;
533 }
534
535 VowelSeqInfo *pInfo;
536
537 if (newVs == vs_nil) {
538 if (VSeqList[vs].roofPos == -1)
539 return processAppend(ev); //roof is not applicable
540
541 //a roof already exists -> undo roof
542 VnLexiName curCh = m_buffer[vStart + VSeqList[vs].roofPos].vnSym;
543 if (target != vnl_nonVnChar && curCh != target)
544 return processAppend(ev); //specific roof and the roof character don't match
545
546 VnLexiName newCh = (curCh == vnl_ar)? vnl_a : ((curCh == vnl_er)? vnl_e : vnl_o);
547 changePos = vStart + VSeqList[vs].roofPos;
548
549 if (!m_pCtrl->options.freeMarking && changePos != m_current)
550 return processAppend(ev);
551
552 markChange(changePos);
553 m_buffer[changePos].vnSym = newCh;
554
555 if (VSeqList[vs].len == 3)
556 newVs = lookupVSeq(m_buffer[vStart].vnSym, m_buffer[vStart+1].vnSym, m_buffer[vStart+2].vnSym);
557 else if (VSeqList[vs].len == 2)
558 newVs = lookupVSeq(m_buffer[vStart].vnSym, m_buffer[vStart+1].vnSym);
559 else
560 newVs = lookupVSeq(m_buffer[vStart].vnSym);
561
562 pInfo = &VSeqList[newVs];
563 roofRemoved = true;
564 }
565 else {
566 pInfo = &VSeqList[newVs];
567 if (target != vnl_nonVnChar && pInfo->v[pInfo->roofPos] != target)
568 return processAppend(ev);
569
570 //check validity of new VC and CV
571 bool valid = true;
572 ConSeq c1 = cs_nil;
573 ConSeq c2 = cs_nil;
574 if (m_buffer[m_current].c1Offset != -1)
575 c1 = m_buffer[m_current-m_buffer[m_current].c1Offset].cseq;
576
577 if (m_buffer[m_current].c2Offset != -1)
578 c2 = m_buffer[m_current-m_buffer[m_current].c2Offset].cseq;
579
580 valid = isValidCVC(c1, newVs, c2);
581 if (!valid)
582 return processAppend(ev);
583
584 if (doubleChangeUO) {
585 changePos = vStart;
586 }
587 else {
588 changePos = vStart + pInfo->roofPos;
589 }
590 if (!m_pCtrl->options.freeMarking && changePos != m_current)
591 return processAppend(ev);
592 markChange(changePos);
593 if (doubleChangeUO) {
594 m_buffer[vStart].vnSym = vnl_u;
595 m_buffer[vStart+1].vnSym = vnl_or;
596 }
597 else {
598 m_buffer[changePos].vnSym = pInfo->v[pInfo->roofPos];
599 }
600 }
601
602 for (i=0; i < pInfo->len; i++) { //update sub-sequences
603 m_buffer[vStart+i].vseq = pInfo->sub[i];
604 }
605
606 //check if tone re-position is needed
607 newTonePos = vStart + getTonePosition(newVs, vEnd == m_current);
608 /* //For now, users don't seem to like the following processing, thus commented out
609 if (roofRemoved && tone != 0 &&
610 (!pInfo->complete || changePos == curTonePos)) {
611 //remove tone if the vowel sequence becomes incomplete as a result of roof removal OR
612 //if removed roof is at the same position as the current tone
613 markChange(curTonePos);
614 m_buffer[curTonePos].tone = 0;
615 } else
616 */
617 if (curTonePos != newTonePos && tone != 0) {
618 markChange(newTonePos);
619 m_buffer[newTonePos].tone = tone;
620 markChange(curTonePos);
621 m_buffer[curTonePos].tone = 0;
622 }
623
624 if (roofRemoved) {
625 m_singleMode = false;
626 processAppend(ev);
627 m_reverted = true;
628 }
629
630 return 1;
631 }
632
633 //------------------------------------------------------------------
634 // can only be called from processHook
635 //------------------------------------------------------------------
636 int UkEngine::processHookWithUO(UkKeyEvent & ev)
637 {
638 VowelSeq vs, newVs;
639 int i, vStart, vEnd;
640 int curTonePos, newTonePos, tone;
641 bool hookRemoved = false;
642 bool removeWithUndo = true;
643 bool toneRemoved = false;
644
645 (void)toneRemoved; // fix warning
646
647 VnLexiName *v;
648
649 if (!m_pCtrl->options.freeMarking && m_buffer[m_current].vOffset != 0)
650 return processAppend(ev);
651
652 vEnd = m_current - m_buffer[m_current].vOffset;
653 vs = m_buffer[vEnd].vseq;
654 vStart = vEnd - (VSeqList[vs].len - 1);
655 v = VSeqList[vs].v;
656 curTonePos = vStart + getTonePosition(vs, vEnd == m_current);
657 tone = m_buffer[curTonePos].tone;
658
659 switch (ev.evType) {
660 case vneHook_u:
661 if (v[0] == vnl_u) {
662 newVs = VSeqList[vs].withHook;
663 markChange(vStart);
664 m_buffer[vStart].vnSym = vnl_uh;
665 }
666 else {// v[0] = vnl_uh, -> uo
667 newVs = lookupVSeq(vnl_u, vnl_o, v[2]);
668 markChange(vStart);
669 m_buffer[vStart].vnSym = vnl_u;
670 m_buffer[vStart+1].vnSym = vnl_o;
671 hookRemoved = true;
672 toneRemoved = (m_buffer[vStart].tone != 0);
673 }
674 break;
675 case vneHook_o:
676 if (v[1] == vnl_o || v[1] == vnl_or) {
677 if (vEnd == m_current && VSeqList[vs].len == 2 &&
678 m_buffer[m_current].form == vnw_cv && m_buffer[m_current-2].cseq == cs_th)
679 {
680 // o|o^ -> o+
681 newVs = VSeqList[vs].withHook;
682 markChange(vStart+1);
683 m_buffer[vStart+1].vnSym = vnl_oh;
684 }
685 else {
686 newVs = lookupVSeq(vnl_uh, vnl_oh, v[2]);
687 if (v[0] == vnl_u) {
688 markChange(vStart);
689 m_buffer[vStart].vnSym = vnl_uh;
690 m_buffer[vStart+1].vnSym = vnl_oh;
691 }
692 else {
693 markChange(vStart+1);
694 m_buffer[vStart+1].vnSym = vnl_oh;
695 }
696 }
697 }
698 else {// v[1] = vnl_oh, -> uo
699 newVs = lookupVSeq(vnl_u, vnl_o, v[2]);
700 if (v[0] == vnl_uh) {
701 markChange(vStart);
702 m_buffer[vStart].vnSym = vnl_u;
703 m_buffer[vStart+1].vnSym = vnl_o;
704 }
705 else {
706 markChange(vStart+1);
707 m_buffer[vStart+1].vnSym = vnl_o;
708 }
709 hookRemoved = true;
710 toneRemoved = (m_buffer[vStart+1].tone != 0);
711 }
712 break;
713 default: //vneHookAll, vneHookUO:
714 if (v[0] == vnl_u) {
715 if (v[1] == vnl_o || v[1] == vnl_or) {
716 //uo -> uo+ if prefixed by "th"
717 if ((vs == vs_uo || vs == vs_uor) && vEnd == m_current &&
718 m_buffer[m_current].form == vnw_cv && m_buffer[m_current-2].cseq == cs_th)
719 {
720 newVs = vs_uoh;
721 markChange(vStart+1);
722 m_buffer[vStart+1].vnSym = vnl_oh;
723 }
724 else {
725 //uo -> u+o+
726 newVs = VSeqList[vs].withHook;
727 markChange(vStart);
728 m_buffer[vStart].vnSym = vnl_uh;
729 newVs = VSeqList[newVs].withHook;
730 m_buffer[vStart+1].vnSym = vnl_oh;
731 }
732 }
733 else {//uo+ -> u+o+
734 newVs = VSeqList[vs].withHook;
735 markChange(vStart);
736 m_buffer[vStart].vnSym = vnl_uh;
737 }
738 }
739 else {//v[0] == vnl_uh
740 if (v[1] == vnl_o) { // u+o -> u+o+
741 newVs = VSeqList[vs].withHook;
742 markChange(vStart+1);
743 m_buffer[vStart+1].vnSym = vnl_oh;
744 }
745 else { //v[1] == vnl_oh, u+o+ -> uo
746 newVs = lookupVSeq(vnl_u, vnl_o, v[2]); //vs_uo;
747 markChange(vStart);
748 m_buffer[vStart].vnSym = vnl_u;
749 m_buffer[vStart+1].vnSym = vnl_o;
750 hookRemoved = true;
751 toneRemoved = (m_buffer[vStart].tone != 0 || m_buffer[vStart+1].tone != 0);
752 }
753 }
754 break;
755 }
756
757 VowelSeqInfo *p = &VSeqList[newVs];
758 for (i=0; i < p->len; i++) { //update sub-sequences
759 m_buffer[vStart+i].vseq = p->sub[i];
760 }
761
762 //check if tone re-position is needed
763 newTonePos = vStart + getTonePosition(newVs, vEnd == m_current);
764 /* //For now, users don't seem to like the following processing, thus commented out
765 if (hookRemoved && tone != 0 && (!p->complete || toneRemoved)) {
766 //remove tone if the vowel sequence becomes incomplete as a result of hook removal
767 //OR if a removed hook is at the same position as the current tone
768 markChange(curTonePos);
769 m_buffer[curTonePos].tone = 0;
770 }
771 else
772 */
773 if (curTonePos != newTonePos && tone != 0) {
774 markChange(newTonePos);
775 m_buffer[newTonePos].tone = tone;
776 markChange(curTonePos);
777 m_buffer[curTonePos].tone = 0;
778 }
779
780 if (hookRemoved && removeWithUndo) {
781 m_singleMode = false;
782 processAppend(ev);
783 m_reverted = true;
784 }
785
786 return 1;
787 }
788
789 //------------------------------------------------------------------
790 int UkEngine::processHook(UkKeyEvent & ev)
791 {
792 if (!m_pCtrl->vietKey || m_current < 0 || m_buffer[m_current].vOffset < 0)
793 return processAppend(ev);
794
795 VowelSeq vs, newVs;
796 int i, vStart, vEnd;
797 int curTonePos, newTonePos, tone;
798 int changePos;
799 bool hookRemoved = false;
800 VowelSeqInfo *pInfo;
801 VnLexiName *v;
802
803 vEnd = m_current - m_buffer[m_current].vOffset;
804 vs = m_buffer[vEnd].vseq;
805
806 v = VSeqList[vs].v;
807
808 if (VSeqList[vs].len > 1 &&
809 ev.evType != vneBowl &&
810 (v[0] == vnl_u || v[0] == vnl_uh) &&
811 (v[1] == vnl_o || v[1] == vnl_oh || v[1] == vnl_or))
812 return processHookWithUO(ev);
813
814 vStart = vEnd - (VSeqList[vs].len - 1);
815 curTonePos = vStart + getTonePosition(vs, vEnd == m_current);
816 tone = m_buffer[curTonePos].tone;
817
818 newVs = VSeqList[vs].withHook;
819 if (newVs == vs_nil) {
820 if (VSeqList[vs].hookPos == -1)
821 return processAppend(ev); //hook is not applicable
822
823 //a hook already exists -> undo hook
824 VnLexiName curCh = m_buffer[vStart + VSeqList[vs].hookPos].vnSym;
825 VnLexiName newCh = (curCh == vnl_ab)? vnl_a : ((curCh == vnl_uh)? vnl_u : vnl_o);
826 changePos = vStart + VSeqList[vs].hookPos;
827 if (!m_pCtrl->options.freeMarking && changePos != m_current)
828 return processAppend(ev);
829
830 switch (ev.evType) {
831 case vneHook_u:
832 if (curCh != vnl_uh)
833 return processAppend(ev);
834 break;
835 case vneHook_o:
836 if (curCh != vnl_oh)
837 return processAppend(ev);
838 break;
839 case vneBowl:
840 if (curCh != vnl_ab)
841 return processAppend(ev);
842 break;
843 default:
844 if (ev.evType == vneHook_uo && curCh == vnl_ab)
845 return processAppend(ev);
846 }
847
848 markChange(changePos);
849 m_buffer[changePos].vnSym = newCh;
850
851 if (VSeqList[vs].len == 3)
852 newVs = lookupVSeq(m_buffer[vStart].vnSym, m_buffer[vStart+1].vnSym, m_buffer[vStart+2].vnSym);
853 else if (VSeqList[vs].len == 2)
854 newVs = lookupVSeq(m_buffer[vStart].vnSym, m_buffer[vStart+1].vnSym);
855 else
856 newVs = lookupVSeq(m_buffer[vStart].vnSym);
857
858 pInfo = &VSeqList[newVs];
859 hookRemoved = true;
860 }
861 else {
862 pInfo = &VSeqList[newVs];
863
864 switch (ev.evType) {
865 case vneHook_u:
866 if (pInfo->v[pInfo->hookPos] != vnl_uh)
867 return processAppend(ev);
868 break;
869 case vneHook_o:
870 if (pInfo->v[pInfo->hookPos] != vnl_oh)
871 return processAppend(ev);
872 break;
873 case vneBowl:
874 if (pInfo->v[pInfo->hookPos] != vnl_ab)
875 return processAppend(ev);
876 break;
877 default: //vneHook_uo, vneHookAll
878 if (ev.evType == vneHook_uo && pInfo->v[pInfo->hookPos] == vnl_ab)
879 return processAppend(ev);
880 }
881
882 //check validity of new VC and CV
883 bool valid = true;
884 ConSeq c1 = cs_nil;
885 ConSeq c2 = cs_nil;
886 if (m_buffer[m_current].c1Offset != -1)
887 c1 = m_buffer[m_current-m_buffer[m_current].c1Offset].cseq;
888
889 if (m_buffer[m_current].c2Offset != -1)
890 c2 = m_buffer[m_current-m_buffer[m_current].c2Offset].cseq;
891
892 valid = isValidCVC(c1, newVs, c2);
893
894 if (!valid)
895 return processAppend(ev);
896
897 changePos = vStart + pInfo->hookPos;
898 if (!m_pCtrl->options.freeMarking && changePos != m_current)
899 return processAppend(ev);
900
901 markChange(changePos);
902 m_buffer[changePos].vnSym = pInfo->v[pInfo->hookPos];
903 }
904
905 for (i=0; i < pInfo->len; i++) { //update sub-sequences
906 m_buffer[vStart+i].vseq = pInfo->sub[i];
907 }
908
909 //check if tone re-position is needed
910 newTonePos = vStart + getTonePosition(newVs, vEnd == m_current);
911 /* //For now, users don't seem to like the following processing, thus commented out
912 if (hookRemoved && tone != 0 &&
913 (!pInfo->complete || (hookRemoved && curTonePos == changePos))) {
914 //remove tone if the vowel sequence becomes incomplete as a result of hook removal
915 //OR if a removed hook was at the same position as the current tone
916 markChange(curTonePos);
917 m_buffer[curTonePos].tone = 0;
918 }
919 else */
920 if (curTonePos != newTonePos && tone != 0) {
921 markChange(newTonePos);
922 m_buffer[newTonePos].tone = tone;
923 markChange(curTonePos);
924 m_buffer[curTonePos].tone = 0;
925 }
926
927 if (hookRemoved) {
928 m_singleMode = false;
929 processAppend(ev);
930 m_reverted = true;
931 }
932
933 return 1;
934 }
935
936 //----------------------------------------------------------
937 int UkEngine::getTonePosition(VowelSeq vs, bool terminated)
938 {
939 VowelSeqInfo & info = VSeqList[vs];
940 if (info.len == 1)
941 return 0;
942
943 if (info.roofPos != -1)
944 return info.roofPos;
945 if (info.hookPos != -1) {
946 if (vs == vs_uhoh || vs == vs_uhohi || vs == vs_uhohu) //u+o+, u+o+u, u+o+i
947 return 1;
948 return info.hookPos;
949 }
950
951 if (info.len == 3)
952 return 1;
953
954 if (m_pCtrl->options.modernStyle &&
955 (vs == vs_oa || vs == vs_oe ||vs == vs_uy))
956 return 1;
957
958 return terminated ? 0 : 1;
959 }
960
961 //----------------------------------------------------------
962 int UkEngine::processTone(UkKeyEvent & ev)
963 {
964 if (m_current < 0 || !m_pCtrl->vietKey)
965 return processAppend(ev);
966
967 if (m_buffer[m_current].form == vnw_c &&
968 (m_buffer[m_current].cseq == cs_gi || m_buffer[m_current].cseq == cs_gin)) {
969 int p = (m_buffer[m_current].cseq == cs_gi)? m_current : m_current - 1;
970 if (m_buffer[p].tone == 0 && ev.tone == 0)
971 return processAppend(ev);
972 markChange(p);
973 if (m_buffer[p].tone == ev.tone) {
974 m_buffer[p].tone = 0;
975 m_singleMode = false;
976 processAppend(ev);
977 m_reverted = true;
978 return 1;
979 }
980 m_buffer[p].tone = ev.tone;
981 return 1;
982 }
983
984 if (m_buffer[m_current].vOffset < 0)
985 return processAppend(ev);
986
987 int vEnd;
988 VowelSeq vs;
989
990 vEnd = m_current - m_buffer[m_current].vOffset;
991 vs = m_buffer[vEnd].vseq;
992 VowelSeqInfo & info = VSeqList[vs];
993 if (m_pCtrl->options.spellCheckEnabled && !m_pCtrl->options.freeMarking && !info.complete)
994 return processAppend(ev);
995
996 if (m_buffer[m_current].form == vnw_vc || m_buffer[m_current].form == vnw_cvc) {
997 ConSeq cs = m_buffer[m_current].cseq;
998 if ((cs == cs_c || cs == cs_ch || cs == cs_p || cs == cs_t) &&
999 (ev.tone == 2 || ev.tone == 3 || ev.tone == 4))
1000 return processAppend(ev); // c, ch, p, t suffixes don't allow ` ? ~
1001 }
1002
1003 int toneOffset = getTonePosition(vs, vEnd == m_current);
1004 int tonePos = vEnd - (info.len -1 ) + toneOffset;
1005 if (m_buffer[tonePos].tone == 0 && ev.tone == 0)
1006 return processAppend(ev);
1007
1008 if (m_buffer[tonePos].tone == ev.tone) {
1009 markChange(tonePos);
1010 m_buffer[tonePos].tone = 0;
1011 m_singleMode = false;
1012 processAppend(ev);
1013 m_reverted = true;
1014 return 1;
1015 }
1016
1017 markChange(tonePos);
1018 m_buffer[tonePos].tone = ev.tone;
1019 return 1;
1020 }
1021
1022 //----------------------------------------------------------
1023 int UkEngine::processDd(UkKeyEvent & ev)
1024 {
1025 if (!m_pCtrl->vietKey || m_current < 0)
1026 return processAppend(ev);
1027
1028 int pos;
1029
1030 // we want to allow dd even in non-vn sequence, because dd is used a lot in abbreviation
1031 // we allow dd only if preceding character is not a vowel
1032 if (m_buffer[m_current].form == vnw_nonVn &&
1033 m_buffer[m_current].vnSym == vnl_d &&
1034 (m_buffer[m_current-1].vnSym == vnl_nonVnChar ||!IsVnVowel[m_buffer[m_current-1].vnSym]))
1035 {
1036 m_singleMode = true;
1037 pos = m_current;
1038 markChange(pos);
1039 m_buffer[pos].cseq = cs_dd;
1040 m_buffer[pos].vnSym = vnl_dd;
1041 m_buffer[pos].form = vnw_c;
1042 m_buffer[pos].c1Offset = 0;
1043 m_buffer[pos].c2Offset = -1;
1044 m_buffer[pos].vOffset = -1;
1045 return 1;
1046 }
1047
1048 if (m_buffer[m_current].c1Offset < 0) {
1049 return processAppend(ev);
1050 }
1051
1052 pos = m_current - m_buffer[m_current].c1Offset;
1053 if (!m_pCtrl->options.freeMarking && pos != m_current)
1054 return processAppend(ev);
1055
1056 if (m_buffer[pos].cseq == cs_d) {
1057 markChange(pos);
1058 m_buffer[pos].cseq = cs_dd;
1059 m_buffer[pos].vnSym = vnl_dd;
1060 //never spellcheck a word which starts with dd, because it's used alot in abbreviation
1061 m_singleMode = true;
1062 return 1;
1063 }
1064
1065 if (m_buffer[pos].cseq == cs_dd) {
1066 //undo dd
1067 markChange(pos);
1068 m_buffer[pos].cseq = cs_d;
1069 m_buffer[pos].vnSym = vnl_d;
1070 m_singleMode = false;
1071 processAppend(ev);
1072 m_reverted = true;
1073 return 1;
1074 }
1075
1076 return processAppend(ev);
1077 }
1078
1079 //----------------------------------------------------------
1080 VnLexiName changeCase(VnLexiName x)
1081 {
1082 if (x == vnl_nonVnChar)
1083 return x;
1084 if (!(x & 0x01))
1085 return (VnLexiName)(x+1);
1086 return (VnLexiName)(x-1);
1087 }
1088
1089 //----------------------------------------------------------
1090 inline VnLexiName vnToLower(VnLexiName x)
1091 {
1092 if (x == vnl_nonVnChar)
1093 return x;
1094 if (!(x & 0x01)) //even
1095 return (VnLexiName)(x+1);
1096 return x;
1097 }
1098
1099 //----------------------------------------------------------
1100 int UkEngine::processMapChar(UkKeyEvent & ev)
1101 {
1102 int capsLockOn = 0;
1103 int shiftPressed = 0;
1104 if (m_keyCheckFunc)
1105 m_keyCheckFunc(&shiftPressed, &capsLockOn);
1106
1107 if (capsLockOn)
1108 ev.vnSym = changeCase(ev.vnSym);
1109
1110 int ret = processAppend(ev);
1111 if (!m_pCtrl->vietKey)
1112 return ret;
1113
1114 if (m_current >= 0 && m_buffer[m_current].form != vnw_empty &&
1115 m_buffer[m_current].form != vnw_nonVn) {
1116 return 1;
1117 }
1118
1119 if (m_current < 0)
1120 return 0;
1121
1122 // mapChar doesn't apply
1123 m_current--;
1124 WordInfo & entry = m_buffer[m_current];
1125
1126 bool undo = false;
1127 // test if undo is needed
1128 if (entry.form != vnw_empty && entry.form != vnw_nonVn) {
1129 VnLexiName prevSym = entry.vnSym;
1130 if (entry.caps) {
1131 prevSym = (VnLexiName)(prevSym - 1);
1132 }
1133 if (prevSym == ev.vnSym) {
1134 if (entry.form != vnw_c) {
1135 int vStart, vEnd, curTonePos, newTonePos, tone;
1136 VowelSeq vs, newVs;
1137
1138 vEnd = m_current - entry.vOffset;
1139 vs = m_buffer[vEnd].vseq;
1140 vStart = vEnd - VSeqList[vs].len +1;
1141 curTonePos = vStart + getTonePosition(vs, vEnd == m_current);
1142 tone = m_buffer[curTonePos].tone;
1143 markChange(m_current);
1144 m_current--;
1145
1146 //check if tone position is needed
1147 if (tone != 0 && m_current >= 0 &&
1148 (m_buffer[m_current].form == vnw_v || m_buffer[m_current].form == vnw_cv)) {
1149 newVs = m_buffer[m_current].vseq;
1150 newTonePos = vStart + getTonePosition(newVs, true);
1151 if (newTonePos != curTonePos) {
1152 markChange(newTonePos);
1153 m_buffer[newTonePos].tone = tone;
1154 markChange(curTonePos);
1155 m_buffer[curTonePos].tone = 0;
1156 }
1157 }
1158 }
1159 else {
1160 markChange(m_current);
1161 m_current--;
1162 }
1163 undo = true;
1164 }
1165 }
1166
1167 ev.evType = vneNormal;
1168 ev.chType = m_pCtrl->input.getCharType(ev.keyCode);
1169 ev.vnSym = IsoToVnLexi(ev.keyCode);
1170 ret = processAppend(ev);
1171 if (undo) {
1172 m_singleMode = false;
1173 m_reverted = true;
1174 return 1;
1175 }
1176 return ret;
1177 }
1178
1179 //----------------------------------------------------------
1180 int UkEngine::processTelexW(UkKeyEvent & ev)
1181 {
1182 if (!m_pCtrl->vietKey)
1183 return processAppend(ev);
1184
1185 int ret;
1186 static bool usedAsMapChar = false;
1187 int capsLockOn = 0;
1188 int shiftPressed = 0;
1189 if (m_keyCheckFunc)
1190 m_keyCheckFunc(&shiftPressed, &capsLockOn);
1191
1192 if (usedAsMapChar) {
1193 ev.evType = vneMapChar;
1194 ev.vnSym = isupper(ev.keyCode)? vnl_Uh : vnl_uh;
1195 if (capsLockOn)
1196 ev.vnSym = changeCase(ev.vnSym);
1197 ev.chType = ukcVn;
1198 ret = processMapChar(ev);
1199 if (ret == 0) {
1200 if (m_current >= 0)
1201 m_current--;
1202 usedAsMapChar = false;
1203 ev.evType = vneHookAll;
1204 return processHook(ev);
1205 }
1206 return ret;
1207 }
1208
1209 ev.evType = vneHookAll;
1210 usedAsMapChar = false;
1211 ret = processHook(ev);
1212 if (ret == 0) {
1213 if (m_current >= 0)
1214 m_current--;
1215 ev.evType = vneMapChar;
1216 ev.vnSym = isupper(ev.keyCode)? vnl_Uh : vnl_uh;
1217 if (capsLockOn)
1218 ev.vnSym = changeCase(ev.vnSym);
1219 ev.chType = ukcVn;
1220 usedAsMapChar = true;
1221 return processMapChar(ev);
1222 }
1223 return ret;
1224 }
1225
1226 //----------------------------------------------------------
1227 int UkEngine::checkEscapeVIQR(UkKeyEvent & ev)
1228 {
1229 if (m_current < 0)
1230 return 0;
1231 WordInfo & entry = m_buffer[m_current];
1232 int escape = 0;
1233 if (entry.form == vnw_v || entry.form == vnw_cv) {
1234 switch (ev.keyCode) {
1235 case '^':
1236 escape = (entry.vnSym == vnl_a || entry.vnSym == vnl_o || entry.vnSym == vnl_e);
1237 break;
1238 case '(':
1239 escape = (entry.vnSym == vnl_a);
1240 break;
1241 case '+':
1242 escape = (entry.vnSym == vnl_o || entry.vnSym == vnl_u);
1243 break;
1244 case '\'':
1245 case '`':
1246 case '?':
1247 case '~':
1248 case '.':
1249 escape = (entry.tone == 0);
1250 break;
1251 }
1252 }
1253 else if (entry.form == vnw_nonVn) {
1254 unsigned char ch = toupper(entry.keyCode);
1255 switch (ev.keyCode) {
1256 case '^':
1257 escape = (ch == 'A' || ch == 'O' || ch == 'E');
1258 break;
1259 case '(':
1260 escape = (ch == 'A');
1261 break;
1262 case '+':
1263 escape = (ch == 'O' || ch == 'U');
1264 break;
1265 case '\'':
1266 case '`':
1267 case '?':
1268 case '~':
1269 case '.':
1270 escape = (ch == 'A' || ch == 'E' || ch == 'I' ||
1271 ch == 'O' || ch == 'U' || ch == 'Y');
1272 break;
1273 }
1274 }
1275
1276 if (escape) {
1277 m_current++;
1278 WordInfo *p = &m_buffer[m_current];
1279 p->form = (ev.chType == ukcWordBreak) ? vnw_empty : vnw_nonVn;
1280 p->c1Offset = p->c2Offset = p->vOffset = -1;
1281 p->keyCode = '?';
1282 p->vnSym = vnl_nonVnChar;
1283
1284 m_current++;
1285 p++;
1286 p->form = (ev.chType == ukcWordBreak) ? vnw_empty : vnw_nonVn;
1287 p->c1Offset = p->c2Offset = p->vOffset = -1;
1288 p->keyCode = ev.keyCode;
1289 p->vnSym = vnl_nonVnChar;
1290
1291 //write output
1292 m_pOutBuf[0] = '\\';
1293 m_pOutBuf[1] = ev.keyCode;
1294 *m_pOutSize = 2;
1295 m_outputWritten = true;
1296 }
1297 return escape;
1298 }
1299
1300 //----------------------------------------------------------
1301 int UkEngine::processAppend(UkKeyEvent & ev)
1302 {
1303 int ret = 0;
1304 switch (ev.chType) {
1305 case ukcReset:
1306 #if defined(_WIN32)
1307 if (ev.keyCode == ENTER_CHAR) {
1308 if (m_pCtrl->options.macroEnabled && macroMatch(ev))
1309 return 1;
1310 }
1311 #endif
1312 reset();
1313 return 0;
1314 case ukcWordBreak:
1315 m_singleMode = false;
1316 return processWordEnd(ev);
1317 case ukcNonVn:
1318 {
1319 if (m_pCtrl->vietKey && m_pCtrl->charsetId == CONV_CHARSET_VIQR && checkEscapeVIQR(ev))
1320 return 1;
1321
1322 m_current++;
1323 WordInfo & entry = m_buffer[m_current];
1324 entry.form = (ev.chType == ukcWordBreak) ? vnw_empty : vnw_nonVn;
1325 entry.c1Offset = entry.c2Offset = entry.vOffset = -1;
1326 entry.keyCode = ev.keyCode;
1327 entry.vnSym = vnToLower(ev.vnSym);
1328 entry.tone = 0;
1329 entry.caps = (entry.vnSym != ev.vnSym);
1330 if (!m_pCtrl->vietKey || m_pCtrl->charsetId != CONV_CHARSET_UNI_CSTRING)
1331 return 0;
1332 markChange(m_current);
1333 return 1;
1334 }
1335 case ukcVn:
1336 {
1337 if (IsVnVowel[ev.vnSym]) {
1338 VnLexiName v = (VnLexiName)StdVnNoTone[vnToLower(ev.vnSym)];
1339 if (m_current >= 0 && m_buffer[m_current].form == vnw_c &&
1340 ((m_buffer[m_current].cseq == cs_q && v == vnl_u) ||
1341 (m_buffer[m_current].cseq == cs_g && v == vnl_i))) {
1342 return appendConsonnant(ev); //process u after q, i after g as consonnants
1343 }
1344 return appendVowel(ev);
1345 }
1346 return appendConsonnant(ev);
1347 }
1348 break;
1349 }
1350
1351 return ret;
1352 }
1353
1354 //----------------------------------------------------------
1355 int UkEngine::appendVowel(UkKeyEvent & ev)
1356 {
1357 bool autoCompleted = false;
1358
1359 m_current++;
1360 WordInfo & entry = m_buffer[m_current];
1361
1362 VnLexiName lowerSym = vnToLower(ev.vnSym);
1363 VnLexiName canSym = (VnLexiName)StdVnNoTone[lowerSym];
1364
1365 entry.vnSym = canSym;
1366 entry.caps = (lowerSym != ev.vnSym);
1367 entry.tone = (lowerSym - canSym)/2;
1368 entry.keyCode = ev.keyCode;
1369
1370 if (m_current == 0 || !m_pCtrl->vietKey) {
1371 entry.form = vnw_v;
1372 entry.c1Offset = entry.c2Offset = -1;
1373 entry.vOffset = 0;
1374 entry.vseq = lookupVSeq(canSym);
1375
1376 if (!m_pCtrl->vietKey ||
1377 ((m_pCtrl->charsetId != CONV_CHARSET_UNI_CSTRING) && isalpha(entry.keyCode)) ) {
1378 return 0;
1379 }
1380 markChange(m_current);
1381 return 1;
1382 }
1383
1384 WordInfo & prev = m_buffer[m_current-1];
1385 VowelSeq vs, newVs;
1386 ConSeq cs;
1387 int prevTonePos;
1388 int tone, newTone, tonePos, newTonePos;
1389
1390 switch (prev.form) {
1391
1392 case vnw_empty:
1393 entry.form = vnw_v;
1394 entry.c1Offset = entry.c2Offset = -1;
1395 entry.vOffset = 0;
1396 entry.vseq = newVs = lookupVSeq(canSym);
1397 break;
1398
1399 case vnw_nonVn:
1400 case vnw_cvc:
1401 case vnw_vc:
1402 entry.form = vnw_nonVn;
1403 entry.c1Offset = entry.c2Offset = entry.vOffset = -1;
1404 break;
1405
1406 case vnw_v:
1407 case vnw_cv:
1408 vs = prev.vseq;
1409 prevTonePos = (m_current - 1) - (VSeqList[vs].len - 1) + getTonePosition(vs, true);
1410 tone = m_buffer[prevTonePos].tone;
1411
1412 if (lowerSym != canSym && tone != 0) //new sym has a tone, but there's is already a preceeding tone
1413 newVs = vs_nil;
1414 else {
1415 if (VSeqList[vs].len == 3)
1416 newVs = vs_nil;
1417 else if (VSeqList[vs].len == 2)
1418 newVs = lookupVSeq(VSeqList[vs].v[0], VSeqList[vs].v[1], canSym);
1419 else
1420 newVs = lookupVSeq(VSeqList[vs].v[0], canSym);
1421 }
1422
1423 if (newVs != vs_nil && prev.form == vnw_cv) {
1424 cs = m_buffer[m_current - 1 - prev.c1Offset].cseq;
1425 if (!isValidCV(cs, newVs))
1426 newVs = vs_nil;
1427 }
1428
1429 if (newVs == vs_nil) {
1430 entry.form = vnw_nonVn;
1431 entry.c1Offset = entry.c2Offset = entry.vOffset = -1;
1432 break;
1433 }
1434
1435 entry.form = prev.form;
1436 if (prev.form == vnw_cv)
1437 entry.c1Offset = prev.c1Offset + 1;
1438 else
1439 entry.c1Offset = -1;
1440 entry.c2Offset = -1;
1441 entry.vOffset = 0;
1442 entry.vseq = newVs;
1443 entry.tone = 0;
1444
1445 newTone = (lowerSym - canSym)/2;
1446 if (tone == 0) {
1447 if (newTone != 0) {
1448 tone = newTone;
1449 tonePos = getTonePosition(newVs, true) + ((m_current - 1) - VSeqList[vs].len + 1);
1450 markChange(tonePos);
1451 m_buffer[tonePos].tone = tone;
1452 return 1;
1453 }
1454 }
1455 else {
1456 newTonePos = getTonePosition(newVs, true) + ((m_current - 1) - VSeqList[vs].len + 1);
1457 if (newTonePos != prevTonePos) {
1458 markChange(prevTonePos);
1459 m_buffer[prevTonePos].tone = 0;
1460 markChange(newTonePos);
1461 if (newTone != 0)
1462 tone = newTone;
1463 m_buffer[newTonePos].tone = tone;
1464 return 1;
1465 }
1466 if (newTone != 0 && newTone != tone) {
1467 tone = newTone;
1468 markChange(prevTonePos);
1469 m_buffer[prevTonePos].tone = tone;
1470 return 1;
1471 }
1472
1473 }
1474
1475 break;
1476 case vnw_c:
1477 newVs = lookupVSeq(canSym);
1478 cs = prev.cseq;
1479 if (!isValidCV(cs, newVs)) {
1480 entry.form = vnw_nonVn;
1481 entry.c1Offset = entry.c2Offset = entry.vOffset = -1;
1482 break;
1483 }
1484
1485 entry.form = vnw_cv;
1486 entry.c1Offset = 1;
1487 entry.c2Offset = -1;
1488 entry.vOffset = 0;
1489 entry.vseq = newVs;
1490
1491 if (cs == cs_gi && prev.tone != 0) {
1492 if (entry.tone == 0)
1493 entry.tone = prev.tone;
1494 markChange(m_current - 1);
1495 prev.tone = 0;
1496 return 1;
1497 }
1498
1499 break;
1500 }
1501
1502 if (!autoCompleted &&
1503 (m_pCtrl->charsetId != CONV_CHARSET_UNI_CSTRING) &&
1504 isalpha(entry.keyCode)) {
1505 return 0;
1506 }
1507
1508 markChange(m_current);
1509 return 1;
1510 }
1511
1512 //----------------------------------------------------------
1513 int UkEngine::appendConsonnant(UkKeyEvent & ev)
1514 {
1515 bool complexEvent = false;
1516 m_current++;
1517 WordInfo & entry = m_buffer[m_current];
1518
1519 VnLexiName lowerSym = vnToLower(ev.vnSym);
1520
1521 entry.vnSym = lowerSym;
1522 entry.caps = (lowerSym != ev.vnSym);
1523 entry.keyCode = ev.keyCode;
1524 entry.tone = 0;
1525
1526 if (m_current == 0 || !m_pCtrl->vietKey) {
1527 entry.form = vnw_c;
1528 entry.c1Offset = 0;
1529 entry.c2Offset = -1;
1530 entry.vOffset = -1;
1531 entry.cseq = lookupCSeq(lowerSym);
1532 if (!m_pCtrl->vietKey || m_pCtrl->charsetId != CONV_CHARSET_UNI_CSTRING)
1533 return 0;
1534 markChange(m_current);
1535 return 1;
1536 }
1537
1538 ConSeq cs, newCs, c1;
1539 VowelSeq vs, newVs;
1540 bool isValid;
1541
1542 WordInfo & prev = m_buffer[m_current-1];
1543
1544 switch (prev.form) {
1545 case vnw_nonVn:
1546 entry.form = vnw_nonVn;
1547 entry.c1Offset = entry.c2Offset = entry.vOffset = -1;
1548 if (m_pCtrl->charsetId != CONV_CHARSET_UNI_CSTRING)
1549 return 0;
1550 markChange(m_current);
1551 return 1;
1552 case vnw_empty:
1553 entry.form = vnw_c;
1554 entry.c1Offset = 0;
1555 entry.c2Offset = -1;
1556 entry.vOffset = -1;
1557 entry.cseq = lookupCSeq(lowerSym);
1558 if (m_pCtrl->charsetId != CONV_CHARSET_UNI_CSTRING)
1559 return 0;
1560 markChange(m_current);
1561 return 1;
1562 case vnw_v:
1563 case vnw_cv:
1564 vs = prev.vseq;
1565 newVs = vs;
1566 if (vs == vs_uoh || vs == vs_uho) {
1567 newVs = vs_uhoh;
1568 }
1569
1570 c1 = cs_nil;
1571 if (prev.c1Offset != -1)
1572 c1 = m_buffer[m_current-1-prev.c1Offset].cseq;
1573
1574 newCs = lookupCSeq(lowerSym);
1575 isValid = isValidCVC(c1, newVs, newCs);
1576
1577 if (isValid) {
1578 //check u+o -> u+o+
1579 if (vs == vs_uho) {
1580 markChange(m_current-1);
1581 prev.vnSym = vnl_oh;
1582 prev.vseq = vs_uhoh;
1583 complexEvent = true;
1584 }
1585 else if (vs == vs_uoh) {
1586 markChange(m_current-2);
1587 m_buffer[m_current-2].vnSym = vnl_uh;
1588 m_buffer[m_current-2].vseq = vs_uh;
1589 prev.vseq = vs_uhoh;
1590 complexEvent = true;
1591 }
1592
1593 if (prev.form == vnw_v) {
1594 entry.form = vnw_vc;
1595 entry.c1Offset = -1;
1596 entry.c2Offset = 0;
1597 entry.vOffset = 1;
1598 }
1599 else { //prev == vnw_cv
1600 entry.form = vnw_cvc;
1601 entry.c1Offset = prev.c1Offset + 1;
1602 entry.c2Offset = 0;
1603 entry.vOffset = 1;
1604 }
1605 entry.cseq = newCs;
1606
1607 //reposition tone if needed
1608 int oldIdx = (m_current-1) - (VSeqList[vs].len - 1) + getTonePosition(vs, true);
1609 if (m_buffer[oldIdx].tone != 0) {
1610 int newIdx = (m_current-1) - (VSeqList[newVs].len - 1) + getTonePosition(newVs, false);
1611 if (newIdx != oldIdx) {
1612 markChange(newIdx);
1613 m_buffer[newIdx].tone = m_buffer[oldIdx].tone;
1614 markChange(oldIdx);
1615 m_buffer[oldIdx].tone = 0;
1616 return 1;
1617 }
1618 }
1619 }
1620 else {
1621 entry.form = vnw_nonVn;
1622 entry.c1Offset = entry.c2Offset = entry.vOffset = -1;
1623 }
1624
1625 if (complexEvent) {
1626 return 1;
1627 }
1628
1629 if (m_pCtrl->charsetId != CONV_CHARSET_UNI_CSTRING)
1630 return 0;
1631 markChange(m_current);
1632 return 1;
1633 case vnw_c:
1634 case vnw_vc:
1635 case vnw_cvc:
1636 cs = prev.cseq;
1637 if (CSeqList[cs].len == 3)
1638 newCs = cs_nil;
1639 else if (CSeqList[cs].len == 2)
1640 newCs = lookupCSeq(CSeqList[cs].c[0], CSeqList[cs].c[1], lowerSym);
1641 else
1642 newCs = lookupCSeq(CSeqList[cs].c[0], lowerSym);
1643
1644 if (newCs != cs_nil && (prev.form == vnw_vc || prev.form == vnw_cvc)) {
1645 // Check CVC combination
1646 c1 = cs_nil;
1647 if (prev.c1Offset != -1)
1648 c1 = m_buffer[m_current-1-prev.c1Offset].cseq;
1649
1650 int vIdx = (m_current - 1) - prev.vOffset;
1651 vs = m_buffer[vIdx].vseq;
1652 isValid = isValidCVC(c1, vs, newCs);
1653
1654 if (!isValid)
1655 newCs = cs_nil;
1656 }
1657
1658 if (newCs == cs_nil) {
1659 entry.form = vnw_nonVn;
1660 entry.c1Offset = entry.c2Offset = entry.vOffset = -1;
1661 }
1662 else {
1663 if (prev.form == vnw_c) {
1664 entry.form = vnw_c;
1665 entry.c1Offset = 0;
1666 entry.c2Offset = -1;
1667 entry.vOffset = -1;
1668 }
1669 else if (prev.form == vnw_vc) {
1670 entry.form = vnw_vc;
1671 entry.c1Offset = -1;
1672 entry.c2Offset = 0;
1673 entry.vOffset = prev.vOffset + 1;
1674 }
1675 else { //vnw_cvc
1676 entry.form = vnw_cvc;
1677 entry.c1Offset = prev.c1Offset + 1;
1678 entry.c2Offset = 0;
1679 entry.vOffset = prev.vOffset + 1;
1680 }
1681 entry.cseq = newCs;
1682 }
1683 if (m_pCtrl->charsetId != CONV_CHARSET_UNI_CSTRING)
1684 return 0;
1685 markChange(m_current);
1686 return 1;
1687 }
1688
1689 if (m_pCtrl->charsetId != CONV_CHARSET_UNI_CSTRING)
1690 return 0;
1691 markChange(m_current);
1692 return 1;
1693 }
1694
1695 //----------------------------------------------------------
1696 int UkEngine::processEscChar(UkKeyEvent & ev)
1697 {
1698 if (m_pCtrl->vietKey &&
1699 m_current >=0 && m_buffer[m_current].form != vnw_empty && m_buffer[m_current].form != vnw_nonVn) {
1700 m_toEscape = true;
1701 }
1702 return processAppend(ev);
1703 }
1704
1705 //----------------------------------------------------------
1706 void UkEngine::pass(int keyCode)
1707 {
1708 UkKeyEvent ev;
1709 m_pCtrl->input.keyCodeToEvent(keyCode, ev);
1710 processAppend(ev);
1711 }
1712
1713 //---------------------------------------------
1714 // This can be called only after other processing have been done.
1715 // The new event is supposed to be put into m_buffer already
1716 //---------------------------------------------
1717 int UkEngine::processNoSpellCheck(UkKeyEvent & ev)
1718 {
1719 WordInfo & entry = m_buffer[m_current];
1720 if (IsVnVowel[entry.vnSym]) {
1721 entry.form = vnw_v;
1722 entry.vOffset = 0;
1723 entry.vseq = lookupVSeq(entry.vnSym);
1724 entry.c1Offset = entry.c2Offset = -1;
1725 }
1726 else {
1727 entry.form = vnw_c;
1728 entry.c1Offset = 0;
1729 entry.c2Offset = -1;
1730 entry.vOffset = -1;
1731 entry.cseq = lookupCSeq(entry.vnSym);
1732 }
1733
1734 if (ev.evType == vneNormal &&
1735 ((entry.keyCode >= 'a' && entry.keyCode <= 'z') ||
1736 (entry.keyCode >= 'A' && entry.keyCode <= 'Z') ) )
1737 return 0;
1738 markChange(m_current);
1739 return 1;
1740 }
1741 //----------------------------------------------------------
1742 int UkEngine::process(unsigned int keyCode, int & backs, unsigned char *outBuf, int & outSize, UkOutputType & outType)
1743 {
1744 UkKeyEvent ev;
1745 prepareBuffer();
1746 m_backs = 0;
1747 m_changePos = m_current+1;
1748 m_pOutBuf = outBuf;
1749 m_pOutSize = &outSize;
1750 m_outputWritten = false;
1751 m_reverted = false;
1752 m_keyRestored = false;
1753 m_keyRestoring = false;
1754 m_outType = UkCharOutput;
1755
1756 m_pCtrl->input.keyCodeToEvent(keyCode, ev);
1757
1758 int ret;
1759 if (!m_toEscape) {
1760 ret = (this->*UkKeyProcList[ev.evType])(ev);
1761 }
1762 else {
1763 m_toEscape = false;
1764 if (m_current < 0 || ev.evType == vneNormal || ev.evType == vneEscChar) {
1765 ret = processAppend(ev);
1766 }
1767 else {
1768 m_current--;
1769 processAppend(ev);
1770 markChange(m_current); //this will assign m_backs to 1 and mark the character for output
1771 ret = 1;
1772 }
1773 }
1774
1775 if ( m_pCtrl->vietKey &&
1776 m_current >= 0 && m_buffer[m_current].form == vnw_nonVn &&
1777 ev.chType == ukcVn &&
1778 (!m_pCtrl->options.spellCheckEnabled || m_singleMode) )
1779 {
1780
1781 //The spell check has failed, but because we are in non-spellcheck mode,
1782 //we consider the new character as the beginning of a new word
1783 ret = processNoSpellCheck(ev);
1784 /*
1785 if ((!m_pCtrl->options.spellCheckEnabled || m_singleMode) ||
1786 ( !m_reverted &&
1787 (m_current < 1 || m_buffer[m_current-1].form != vnw_nonVn)) ) {
1788
1789 ret = processNoSpellCheck(ev);
1790 }
1791 */
1792 }
1793
1794 //we add key to key buffer only if that key has not caused a reset
1795 if (m_current >= 0) {
1796 ev.chType = m_pCtrl->input.getCharType(ev.keyCode);
1797 m_keyCurrent++;
1798 m_keyStrokes[m_keyCurrent].ev = ev;
1799 m_keyStrokes[m_keyCurrent].converted = (ret && !m_keyRestored);
1800 }
1801
1802 if (ret == 0) {
1803 backs = 0;
1804 outSize = 0;
1805 outType = m_outType;
1806 return 0;
1807 }
1808
1809 backs = m_backs;
1810 if (!m_outputWritten) {
1811 writeOutput(outBuf, outSize);
1812 }
1813 outType = m_outType;
1814
1815 return ret;
1816 }
1817
1818
1819 //----------------------------------------------------------
1820 // Returns 0 on success
1821 // error code otherwise
1822 // outBuf: buffer to write
1823 // outSize: [in] size of buffer in bytes
1824 // [out] bytes written to buffer
1825 //----------------------------------------------------------
1826 int UkEngine::writeOutput(unsigned char *outBuf, int & outSize)
1827 {
1828 StdVnChar stdChar;
1829 int i, bytesWritten;
1830 int ret = 1;
1831 StringBOStream os(outBuf, outSize);
1832 VnCharset *pCharset = VnCharsetLibObj.getVnCharset(m_pCtrl->charsetId);
1833 pCharset->startOutput();
1834
1835 for (i = m_changePos; i <= m_current; i++) {
1836 if (m_buffer[i].vnSym != vnl_nonVnChar) {
1837 //process vn symbol
1838 stdChar = m_buffer[i].vnSym + VnStdCharOffset;
1839 if (m_buffer[i].caps)
1840 stdChar--;
1841 if (m_buffer[i].tone != 0)
1842 stdChar += m_buffer[i].tone * 2;
1843 }
1844 else {
1845 stdChar = IsoToStdVnChar(m_buffer[i].keyCode);
1846 }
1847
1848 if (stdChar != INVALID_STD_CHAR)
1849 ret = pCharset->putChar(os, stdChar, bytesWritten);
1850 }
1851
1852 outSize = os.getOutBytes();
1853 return (ret? 0 : VNCONV_OUT_OF_MEMORY);
1854 }
1855
1856 //---------------------------------------------
1857 // Returns the number of backspaces needed to
1858 // go back from last to first
1859 //---------------------------------------------
1860 int UkEngine::getSeqSteps(int first, int last)
1861 {
1862 StdVnChar stdChar;
1863
1864 if (last < first)
1865 return 0;
1866
1867 if (m_pCtrl->charsetId == CONV_CHARSET_XUTF8 ||
1868 m_pCtrl->charsetId == CONV_CHARSET_UNICODE)
1869 return (last - first + 1);
1870
1871 StringBOStream os(0, 0);
1872 int i, bytesWritten;
1873
1874 VnCharset *pCharset = VnCharsetLibObj.getVnCharset(m_pCtrl->charsetId);
1875 pCharset->startOutput();
1876
1877 for (i = first; i <= last; i++) {
1878 if (m_buffer[i].vnSym != vnl_nonVnChar) {
1879 //process vn symbol
1880 stdChar = m_buffer[i].vnSym + VnStdCharOffset;
1881 if (m_buffer[i].caps)
1882 stdChar--;
1883 if (m_buffer[i].tone != 0)
1884 stdChar += m_buffer[i].tone*2;
1885 }
1886 else {
1887 stdChar = m_buffer[i].keyCode;
1888 }
1889
1890 if (stdChar != INVALID_STD_CHAR)
1891 pCharset->putChar(os, stdChar, bytesWritten);
1892 }
1893
1894 int len = os.getOutBytes();
1895 if (m_pCtrl->charsetId == CONV_CHARSET_UNIDECOMPOSED)
1896 len = len / 2;
1897 return len;
1898 }
1899
1900 //---------------------------------------------
1901 void UkEngine::markChange(int pos)
1902 {
1903 if (pos < m_changePos) {
1904 m_backs += getSeqSteps(pos, m_changePos-1);
1905 m_changePos = pos;
1906 }
1907 }
1908
1909 //----------------------------------------------------------------
1910 // Called from processBackspace to keep
1911 // character buffer (m_buffer) and key stroke buffer in synch
1912 //----------------------------------------------------------------
1913 void UkEngine::synchKeyStrokeBuffer()
1914 {
1915 //synchronize with key-stroke buffer
1916 if (m_keyCurrent >= 0)
1917 m_keyCurrent--;
1918 if (m_current >= 0 && m_buffer[m_current].form == vnw_empty) {
1919 //in character buffer, we have reached a word break,
1920 //so we also need to move key stroke pointer backward to corresponding word break
1921 while (m_keyCurrent >= 0 && m_keyStrokes[m_keyCurrent].ev.chType != ukcWordBreak)
1922 {
1923 m_keyCurrent--;
1924 }
1925 }
1926 }
1927
1928 //---------------------------------------------
1929 int UkEngine::processBackspace(int & backs, unsigned char *outBuf, int & outSize, UkOutputType & outType)
1930 {
1931 outType = UkCharOutput;
1932 if (!m_pCtrl->vietKey || m_current < 0) {
1933 backs = 0;
1934 outSize = 0;
1935 return 0;
1936 }
1937
1938 m_backs = 0;
1939 m_changePos = m_current + 1;
1940 markChange(m_current);
1941
1942 if (m_current == 0 ||
1943 m_buffer[m_current].form == vnw_empty ||
1944 m_buffer[m_current].form == vnw_nonVn ||
1945 m_buffer[m_current].form == vnw_c ||
1946 m_buffer[m_current-1].form == vnw_c ||
1947 m_buffer[m_current-1].form == vnw_cvc ||
1948 m_buffer[m_current-1].form == vnw_vc) {
1949
1950 m_current--;
1951 backs = m_backs;
1952 outSize = 0;
1953 synchKeyStrokeBuffer();
1954 return (backs > 1);
1955 }
1956
1957 VowelSeq vs, newVs;
1958 int curTonePos, newTonePos, tone, vStart, vEnd;
1959
1960 vEnd = m_current - m_buffer[m_current].vOffset;
1961 vs = m_buffer[vEnd].vseq;
1962 vStart = vEnd - VSeqList[vs].len + 1;
1963 newVs = m_buffer[m_current-1].vseq;
1964 curTonePos = vStart + getTonePosition(vs, vEnd == m_current);
1965 newTonePos = vStart + getTonePosition(newVs, true);
1966 tone = m_buffer[curTonePos].tone;
1967
1968 if (tone == 0 || curTonePos == newTonePos ||
1969 (curTonePos == m_current && m_buffer[m_current].tone != 0)) {
1970 m_current--;
1971 backs = m_backs;
1972 outSize = 0;
1973 synchKeyStrokeBuffer();
1974 return (backs > 1);
1975 }
1976
1977 markChange(newTonePos);
1978 m_buffer[newTonePos].tone = tone;
1979 markChange(curTonePos);
1980 m_buffer[curTonePos].tone = 0;
1981 m_current--;
1982 synchKeyStrokeBuffer();
1983 backs = m_backs;
1984 writeOutput(outBuf, outSize);
1985 return 1;
1986 }
1987
1988 //------------------------------------------------
1989 void UkEngine::reset()
1990 {
1991 m_current = -1;
1992 m_keyCurrent = -1;
1993 m_singleMode = false;
1994 m_toEscape = false;
1995 }
1996
1997 //------------------------------------------------
1998 void UkEngine::resetKeyBuf()
1999 {
2000 m_keyCurrent = -1;
2001 }
2002
2003 //------------------------------------------------
2004 UkEngine::UkEngine()
2005 {
2006 if (!m_classInit) {
2007 engineClassInit();
2008 m_classInit = true;
2009 }
2010 m_pCtrl = 0;
2011 m_bufSize = MAX_UK_ENGINE;
2012 m_keyBufSize = MAX_UK_ENGINE;
2013 m_current = -1;
2014 m_keyCurrent = -1;
2015 m_singleMode = false;
2016 m_keyCheckFunc = 0;
2017 m_reverted = false;
2018 m_toEscape = false;
2019 m_keyRestored = false;
2020 }
2021
2022 //----------------------------------------------------
2023 // make sure there are at least 10 entries available
2024 //----------------------------------------------------
2025 void UkEngine::prepareBuffer()
2026 {
2027 int rid;
2028 //prepare symbol buffer
2029 if (m_current >= 0 && m_current + 10 >= m_bufSize) {
2030 // Get rid of at least half of the current entries
2031 // don't get rid from the middle of a word.
2032 for (rid = m_current/2; m_buffer[rid].form != vnw_empty && rid < m_current; rid++);
2033 if (rid == m_current) {
2034 m_current = -1;
2035 }
2036 else {
2037 rid++;
2038 memmove(m_buffer, m_buffer+rid, (m_current-rid+1)*sizeof(WordInfo));
2039 m_current -= rid;
2040 }
2041 }
2042
2043 //prepare key stroke buffer
2044 if (m_keyCurrent > 0 && m_keyCurrent + 1 >= m_keyBufSize) {
2045 // Get rid of at least half of the current entries
2046 rid = m_keyCurrent/2;
2047 memmove(m_keyStrokes, m_keyStrokes + rid, (m_keyCurrent-rid+1)*sizeof(m_keyStrokes[0]));
2048 m_keyCurrent -= rid;
2049 }
2050
2051 }
2052
2053 #define ENTER_CHAR 13
2054 enum VnCaseType {VnCaseNoChange, VnCaseAllCapital, VnCaseAllSmall};
2055
2056 //----------------------------------------------------
2057 int UkEngine::macroMatch(UkKeyEvent & ev)
2058 {
2059 int capsLockOn = 0;
2060 int shiftPressed = 0;
2061 if (m_keyCheckFunc)
2062 m_keyCheckFunc(&shiftPressed, &capsLockOn);
2063
2064 if (shiftPressed && (ev.keyCode ==' ' || ev.keyCode == ENTER_CHAR))
2065 return 0;
2066
2067 const StdVnChar *pMacText = NULL;
2068 StdVnChar key[MAX_MACRO_KEY_LEN+1];
2069 StdVnChar *pKeyStart;
2070
2071 // Use static macro text so we can gain a bit of performance
2072 // by avoiding memory allocation each time this function is called
2073 static StdVnChar macroText[MAX_MACRO_TEXT_LEN+1];
2074
2075 int i, j;
2076
2077 i = m_current;
2078 while (i >= 0 && (m_current-i + 1) < MAX_MACRO_KEY_LEN) {
2079 while (i>=0 && m_buffer[i].form != vnw_empty && (m_current-i + 1) < MAX_MACRO_KEY_LEN)
2080 i--;
2081 if (i>=0 && m_buffer[i].form != vnw_empty)
2082 return 0;
2083
2084 if (i>=0) {
2085 if (m_buffer[i].vnSym != vnl_nonVnChar) {
2086 key[0] = m_buffer[i].vnSym + VnStdCharOffset;
2087 if (m_buffer[i].caps)
2088 key[0]--;
2089 key[0] += m_buffer[i].tone*2;
2090 }
2091 else
2092 key[0] = m_buffer[i].keyCode;
2093 }
2094
2095 for (j=i+1; j<=m_current; j++) {
2096 if (m_buffer[j].vnSym != vnl_nonVnChar) {
2097 key[j-i] = m_buffer[j].vnSym + VnStdCharOffset;
2098 if (m_buffer[j].caps)
2099 key[j-i]--;
2100 key[j-i] += m_buffer[j].tone*2;
2101 }
2102 else
2103 key[j-i] = m_buffer[j].keyCode;
2104 }
2105 key[m_current-i+1] = 0;
2106 //search macro table
2107 pMacText = m_pCtrl->macStore.lookup(key+1);
2108 if (pMacText) {
2109 i++; //mark the position where change is needed
2110 pKeyStart = key + 1;
2111 break;
2112 }
2113 if (i>=0) {
2114 pMacText = m_pCtrl->macStore.lookup(key);
2115 if (pMacText) {
2116 pKeyStart = key;
2117 break;
2118 }
2119 }
2120 i--;
2121 }
2122
2123 if (!pMacText) {
2124 return 0;
2125 }
2126
2127 markChange(i);
2128
2129 // determine the form of macro replacements: ALL CAPITALS, First Character Capital, or no change
2130 VnCaseType macroCase;
2131 if (IS_STD_VN_LOWER(*pKeyStart)) {
2132 macroCase = VnCaseAllSmall;
2133 }
2134 else if (IS_STD_VN_UPPER(*pKeyStart)) {
2135 macroCase = VnCaseAllCapital;
2136 for (i=1; pKeyStart[i]; i++) {
2137 if (IS_STD_VN_LOWER(pKeyStart[i])) {
2138 macroCase = VnCaseNoChange;
2139 }
2140 }
2141 }
2142 else macroCase = VnCaseNoChange;
2143
2144 // Convert case of macro text according to macroCase
2145 int charCount = 0;
2146 while (pMacText[charCount] != 0)
2147 charCount++;
2148
2149 for (i = 0; i < charCount; i++)
2150 {
2151 if (macroCase == VnCaseAllCapital)
2152 macroText[i] = StdVnToUpper(pMacText[i]);
2153 else if (macroCase == VnCaseAllSmall)
2154 macroText[i] = StdVnToLower(pMacText[i]);
2155 else
2156 macroText[i] = pMacText[i];
2157 }
2158
2159 // Convert to target output charset
2160 int outSize;
2161 int maxOutSize = *m_pOutSize;
2162 int inLen = charCount * sizeof(StdVnChar);
2163 VnConvert(CONV_CHARSET_VNSTANDARD, m_pCtrl->charsetId,
2164 (UKBYTE *) macroText, (UKBYTE *)m_pOutBuf,
2165 &inLen, &maxOutSize);
2166 outSize = maxOutSize;
2167
2168 //write the last input character
2169 StdVnChar vnChar;
2170 if (outSize < *m_pOutSize) {
2171 maxOutSize = *m_pOutSize - outSize;
2172 if (ev.vnSym != vnl_nonVnChar)
2173 vnChar = ev.vnSym + VnStdCharOffset;
2174 else
2175 vnChar = ev.keyCode;
2176 inLen = sizeof(StdVnChar);
2177 VnConvert(CONV_CHARSET_VNSTANDARD, m_pCtrl->charsetId,
2178 (UKBYTE *) &vnChar, ((UKBYTE *)m_pOutBuf) + outSize,
2179 &inLen, &maxOutSize);
2180 outSize += maxOutSize;
2181 }
2182 int backs = m_backs; //store m_backs before calling reset
2183 reset();
2184 m_outputWritten = true;
2185 m_backs = backs;
2186 *m_pOutSize = outSize;
2187 return 1;
2188 }
2189
2190 //----------------------------------------------------
2191 int UkEngine::restoreKeyStrokes(int & backs, unsigned char *outBuf, int & outSize, UkOutputType & outType)
2192 {
2193 outType = UkKeyOutput;
2194 if (!lastWordHasVnMark()) {
2195 backs = 0;
2196 outSize = 0;
2197 return 0;
2198 }
2199
2200 m_backs = 0;
2201 m_changePos = m_current+1;
2202
2203 int keyStart;
2204 bool converted = false;
2205 for (keyStart = m_keyCurrent; keyStart >= 0 && m_keyStrokes[keyStart].ev.chType != ukcWordBreak; keyStart--) {
2206 if (m_keyStrokes[keyStart].converted) {
2207 converted = true;
2208 }
2209 }
2210 keyStart++;
2211
2212 if (!converted) {
2213 //no key stroke has been converted, so it doesn't make sense to restore key strokes
2214 backs = 0;
2215 outSize = 0;
2216 return 0;
2217 }
2218
2219 //int i = m_current;
2220 while (m_current >=0 && m_buffer[m_current].form != vnw_empty)
2221 m_current--;
2222 markChange(m_current+1);
2223 backs = m_backs;
2224
2225 int count;
2226 int i;
2227 UkKeyEvent ev;
2228 m_keyRestoring = true;
2229 for (i=keyStart, count = 0; i <= m_keyCurrent; i++) {
2230 if (count<outSize) {
2231 outBuf[count++] = (unsigned char)m_keyStrokes[i].ev.keyCode;
2232 }
2233 m_pCtrl->input.keyCodeToSymbol(m_keyStrokes[i].ev.keyCode, ev);
2234 m_keyStrokes[i].converted = false;
2235 processAppend(ev);
2236 }
2237 outSize = count;
2238 m_keyRestoring = false;
2239
2240 return 1;
2241 }
2242
2243 //--------------------------------------------------
2244 void UkEngine::setSingleMode()
2245 {
2246 m_singleMode = true;
2247 }
2248
2249 //--------------------------------------------------
2250 void SetupUnikeyEngine()
2251 {
2252 SetupInputClassifierTable();
2253 int i;
2254 VnLexiName lexi;
2255
2256 //Calculate IsoStdVnCharMap
2257 for (i=0; i < 256; i++) {
2258 IsoStdVnCharMap[i] = i;
2259 }
2260
2261 for (i=0; SpecialWesternChars[i]; i++) {
2262 IsoStdVnCharMap[SpecialWesternChars[i]] = (vnl_lastChar + i) + VnStdCharOffset;
2263 }
2264
2265 for (i=0; i < 256; i++) {
2266 if ((lexi = IsoToVnLexi(i)) != vnl_nonVnChar) {
2267 IsoStdVnCharMap[i] = lexi + VnStdCharOffset;
2268 }
2269 }
2270 }
2271
2272 //--------------------------------------------------
2273 bool UkEngine::atWordBeginning()
2274 {
2275 return (m_current < 0 || m_buffer[m_current].form == vnw_empty);
2276 }
2277
2278 //--------------------------------------------------
2279 // Check for macro first, if there's a match, expand macro. If not:
2280 // Spell-check, if is valid Vietnamese, return normally, if not:
2281 // restore key strokes if auto-restore is enabled
2282 //--------------------------------------------------
2283 int UkEngine::processWordEnd(UkKeyEvent & ev)
2284 {
2285 if (m_pCtrl->options.macroEnabled && macroMatch(ev))
2286 return 1;
2287
2288 if (!m_pCtrl->options.spellCheckEnabled || m_singleMode || m_current < 0 || m_keyRestoring) {
2289 m_current++;
2290 WordInfo & entry = m_buffer[m_current];
2291 entry.form = vnw_empty;
2292 entry.c1Offset = entry.c2Offset = entry.vOffset = -1;
2293 entry.keyCode = ev.keyCode;
2294 entry.vnSym = vnToLower(ev.vnSym);
2295 entry.caps = (entry.vnSym != ev.vnSym);
2296 return 0;
2297 }
2298
2299 int outSize = 0;
2300 if (m_pCtrl->options.autoNonVnRestore && lastWordIsNonVn()) {
2301 outSize = *m_pOutSize;
2302 if (restoreKeyStrokes(m_backs, m_pOutBuf, outSize, m_outType)) {
2303 m_keyRestored = true;
2304 m_outputWritten = true;
2305 }
2306 }
2307
2308 m_current++;
2309 WordInfo & entry = m_buffer[m_current];
2310 entry.form = vnw_empty;
2311 entry.c1Offset = entry.c2Offset = entry.vOffset = -1;
2312 entry.keyCode = ev.keyCode;
2313 entry.vnSym = vnToLower(ev.vnSym);
2314 entry.caps = (entry.vnSym != ev.vnSym);
2315
2316 if (m_keyRestored && outSize < *m_pOutSize) {
2317 m_pOutBuf[outSize] = ev.keyCode;
2318 outSize++;
2319 *m_pOutSize = outSize;
2320 return 1;
2321 }
2322
2323 return 0;
2324 }
2325
2326 //---------------------------------------------------------------------------
2327 // Test if last word is a non-Vietnamese word, so that
2328 // the engine can restore key strokes if it is indeed not a Vietnamese word
2329 //---------------------------------------------------------------------------
2330 bool UkEngine::lastWordIsNonVn()
2331 {
2332 if (m_current < 0)
2333 return false;
2334
2335 switch (m_buffer[m_current].form) {
2336 case vnw_nonVn:
2337 return true;
2338 case vnw_empty:
2339 case vnw_c:
2340 return false;
2341 case vnw_v:
2342 case vnw_cv:
2343 return !VSeqList[m_buffer[m_current].vseq].complete;
2344 case vnw_vc:
2345 case vnw_cvc: {
2346 int vIndex = m_current - m_buffer[m_current].vOffset;
2347 VowelSeq vs = m_buffer[vIndex].vseq;
2348 if (!VSeqList[vs].complete)
2349 return true;
2350 ConSeq cs = m_buffer[m_current].cseq;
2351 ConSeq c1 = cs_nil;
2352 if (m_buffer[m_current].c1Offset != -1)
2353 c1 = m_buffer[m_current-m_buffer[m_current].c1Offset].cseq;
2354
2355 if (!isValidCVC(c1, vs, cs)) {
2356 return true;
2357 }
2358
2359 int tonePos = (vIndex - VSeqList[vs].len + 1) + getTonePosition(vs, false);
2360 int tone = m_buffer[tonePos].tone;
2361 if ((cs == cs_c || cs == cs_ch || cs == cs_p || cs == cs_t) &&
2362 (tone == 2 || tone == 3 || tone == 4))
2363 {
2364 return true;
2365 }
2366 }
2367 }
2368 return false;
2369 }
2370
2371 //---------------------------------------------------------------------------
2372 // Test if last word has a Vietnamese mark, that is tones, decorators
2373 //---------------------------------------------------------------------------
2374 bool UkEngine::lastWordHasVnMark()
2375 {
2376 for (int i=m_current; i>=0 && m_buffer[i].form != vnw_empty; i--) {
2377 VnLexiName sym = m_buffer[i].vnSym;
2378 if (sym != vnl_nonVnChar) {
2379 if (IsVnVowel[sym]) {
2380 if (m_buffer[i].tone)
2381 return true;
2382 }
2383 if (sym != StdVnRootChar[sym] )
2384 return true;
2385 }
2386 }
2387 return false;
2388 }