Debian Codegraph @

unikey/ukengine.cpp

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