1 /* |
|
2 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.inputmethods.internal.thaiim; |
|
27 |
|
28 import java.awt.im.InputMethodRequests; |
|
29 |
|
30 public class ThaiRules { |
|
31 |
|
32 public static final char BASE = 0x0e00; |
|
33 |
|
34 public static final byte NON = 0; |
|
35 public static final byte CONS = 1; |
|
36 public static final byte LV = 2; |
|
37 public static final byte FV1 = 3; |
|
38 public static final byte FV2 = 4; |
|
39 public static final byte FV3 = 5; |
|
40 public static final byte FV4 = 6; |
|
41 /* Note that FV4 is added. It is not in WTT. |
|
42 * We need it for SARA AM since it has a |
|
43 * weired characteristic to share the same |
|
44 * cell with whatever consonant preceeds it. |
|
45 */ |
|
46 public static final byte BV1 = 7; |
|
47 public static final byte BV2 = 8; |
|
48 public static final byte BD = 9; |
|
49 public static final byte TONE = 10; |
|
50 public static final byte AD1 = 11; |
|
51 public static final byte AD2 = 12; |
|
52 public static final byte AD3 = 13; |
|
53 public static final byte AV1 = 14; |
|
54 public static final byte AV2 = 15; |
|
55 public static final byte AV3 = 16; |
|
56 |
|
57 /** |
|
58 * Constants for validity checking and auto correction |
|
59 */ |
|
60 public static final byte STRICT = 0; |
|
61 public static final byte LOOSE = 1; |
|
62 public static final byte NOREPLACE = 2; |
|
63 |
|
64 public static final byte[] CHARTYPE = { |
|
65 /* 0e00 UNUSED */ NON, |
|
66 /* THAI CHARACTER KO KAI */ CONS, |
|
67 /* THAI CHARACTER KHO KHAI */ CONS, |
|
68 /* THAI CHARACTER KHO KHUAT */ CONS, |
|
69 /* THAI CHARACTER KHO KHWAI */ CONS, |
|
70 /* THAI CHARACTER KHO KHON */ CONS, |
|
71 /* THAI CHARACTER KHO RAKHANG */ CONS, |
|
72 /* THAI CHARACTER NGO NGU */ CONS, |
|
73 /* THAI CHARACTER CHO CHAN */ CONS, |
|
74 /* THAI CHARACTER CHO CHING */ CONS, |
|
75 /* THAI CHARACTER CHO CHANG */ CONS, |
|
76 /* THAI CHARACTER SO SO */ CONS, |
|
77 /* THAI CHARACTER CHO CHOE */ CONS, |
|
78 /* THAI CHARACTER YO YING */ CONS, |
|
79 /* THAI CHARACTER DO CHADA */ CONS, |
|
80 /* THAI CHARACTER TO PATAK */ CONS, |
|
81 /* THAI CHARACTER THO THAN */ CONS, |
|
82 /* THAI CHARACTER THO NANGMONTHO */ CONS, |
|
83 /* THAI CHARACTER THO PHUTHAO */ CONS, |
|
84 /* THAI CHARACTER NO NEN */ CONS, |
|
85 /* THAI CHARACTER DO DEK */ CONS, |
|
86 /* THAI CHARACTER TO TAO */ CONS, |
|
87 /* THAI CHARACTER THO THUNG */ CONS, |
|
88 /* THAI CHARACTER THO THAHAN */ CONS, |
|
89 /* THAI CHARACTER THO THONG */ CONS, |
|
90 /* THAI CHARACTER NO NU */ CONS, |
|
91 /* THAI CHARACTER BO BAIMAI */ CONS, |
|
92 /* THAI CHARACTER PO PLA */ CONS, |
|
93 /* THAI CHARACTER PHO PHUNG */ CONS, |
|
94 /* THAI CHARACTER FO FA */ CONS, |
|
95 /* THAI CHARACTER PHO PHAN */ CONS, |
|
96 /* THAI CHARACTER FO FAN */ CONS, |
|
97 /* THAI CHARACTER PHO SAMPHAO */ CONS, |
|
98 /* THAI CHARACTER MO MA */ CONS, |
|
99 /* THAI CHARACTER YO YAK */ CONS, |
|
100 /* THAI CHARACTER RO RUA */ CONS, |
|
101 /* THAI CHARACTER RU */ FV3, |
|
102 /* THAI CHARACTER LO LING */ CONS, |
|
103 /* THAI CHARACTER LU */ FV3, |
|
104 /* THAI CHARACTER WO WAEN */ CONS, |
|
105 /* THAI CHARACTER SO SALA */ CONS, |
|
106 /* THAI CHARACTER SO RUSI */ CONS, |
|
107 /* THAI CHARACTER SO SUA */ CONS, |
|
108 /* THAI CHARACTER HO HIP */ CONS, |
|
109 /* THAI CHARACTER LO CHULA */ CONS, |
|
110 /* THAI CHARACTER O ANG */ CONS, |
|
111 /* THAI CHARACTER HO NOKHUK */ CONS, |
|
112 /* THAI CHARACTER PAIYANNOI */ NON, |
|
113 /* THAI CHARACTER SARA A */ FV1, |
|
114 /* THAI CHARACTER MAI HAN-AKAT */ AV2, |
|
115 /* THAI CHARACTER SARA AA */ FV1, |
|
116 /* THAI CHARACTER SARA AM */ FV4, |
|
117 /* THAI CHARACTER SARA I */ AV1, |
|
118 /* THAI CHARACTER SARA II */ AV3, |
|
119 /* THAI CHARACTER SARA UE */ AV2, |
|
120 /* THAI CHARACTER SARA UEE */ AV3, |
|
121 /* THAI CHARACTER SARA U */ BV1, |
|
122 /* THAI CHARACTER SARA UU */ BV2, |
|
123 /* THAI CHARACTER PHINTHU */ BD, |
|
124 /* 0e3b UNUSED */ NON, |
|
125 /* 0e3c UNUSED */ NON, |
|
126 /* 0e3d UNUSED */ NON, |
|
127 /* 0e3e UNUSED */ NON, |
|
128 /* THAI CURRENCY SYMBOL BAHT */ NON, |
|
129 /* THAI CHARACTER SARA E */ LV, |
|
130 /* THAI CHARACTER SARA AE */ LV, |
|
131 /* THAI CHARACTER SARA O */ LV, |
|
132 /* THAI CHARACTER SARA AI MAIMUAN */ LV, |
|
133 /* THAI CHARACTER SARA AI MAIMALAI */ LV, |
|
134 /* THAI CHARACTER LAKKHANGYAO */ FV2, |
|
135 /* THAI CHARACTER MAIYAMOK */ NON, |
|
136 /* THAI CHARACTER MAITAIKHU */ AD2, |
|
137 /* THAI CHARACTER MAI EK */ TONE, |
|
138 /* THAI CHARACTER MAI THO */ TONE, |
|
139 /* THAI CHARACTER MAI TRI */ TONE, |
|
140 /* THAI CHARACTER MAI CHATTAWA */ TONE, |
|
141 /* THAI CHARACTER THANTHAKHAT */ AD1, |
|
142 /* THAI CHARACTER NIKHAHIT */ AD3, |
|
143 /* THAI CHARACTER YAMAKKAN */ AD3, |
|
144 /* THAI CHARACTER FONGMAN */ NON, |
|
145 /* THAI DIGIT ZERO */ NON, |
|
146 /* THAI DIGIT ONE */ NON, |
|
147 /* THAI DIGIT TWO */ NON, |
|
148 /* THAI DIGIT THREE */ NON, |
|
149 /* THAI DIGIT FOUR */ NON, |
|
150 /* THAI DIGIT FIVE */ NON, |
|
151 /* THAI DIGIT SIX */ NON, |
|
152 /* THAI DIGIT SEVEN */ NON, |
|
153 /* THAI DIGIT EIGHT */ NON, |
|
154 /* THAI DIGIT NINE */ NON, |
|
155 /* THAI CHARACTER ANGKHANKHU */ NON, |
|
156 /* THAI CHARACTER KHOMUT */ NON |
|
157 }; |
|
158 |
|
159 private InputMethodRequests requests; |
|
160 |
|
161 ThaiRules(InputMethodRequests requests) { |
|
162 this.requests = requests; |
|
163 } |
|
164 |
|
165 public static byte getCharType(char c) { |
|
166 byte cType; |
|
167 int ci = ((int) c) - (int) BASE; |
|
168 if (ci < 0 || ci >= CHARTYPE.length) |
|
169 cType = NON; |
|
170 else |
|
171 cType = CHARTYPE[ci]; |
|
172 return cType; |
|
173 } |
|
174 |
|
175 private static boolean isValid(char c1, char c2, int[] validityArray) { |
|
176 return ((validityArray[getCharType(c1)] |
|
177 & (1 << getCharType(c2))) != 0); |
|
178 } |
|
179 |
|
180 /** |
|
181 * VALIDITY is a bit matrix defining whether one |
|
182 * character is allowed to be typed in after the |
|
183 * previous one (array index). Determining the |
|
184 * validity is done by bit-anding the 2nd char |
|
185 * type's mask (obtained by 1 << chartype) with |
|
186 * the array element indexed by the first char |
|
187 * type. If the result is non-zero, the 2nd |
|
188 * character is allowed to follow the first. |
|
189 */ |
|
190 |
|
191 /* Please note that the bits in the comment below |
|
192 * are displayed least significant bit first. |
|
193 * The actual value reflexs this representation |
|
194 * when the bits are swapped. |
|
195 */ |
|
196 |
|
197 private static final int[] INPUTVALIDITY = { |
|
198 /* NON 1110 010 0 0000 0000 0 */ 0x00027, |
|
199 /* CONS 1111 111 1 1111 1111 1 */ 0x1ffff, |
|
200 /* LV 0100 000 0 0000 0000 0 */ 0x00002, |
|
201 /* FV1 1110 010 0 0000 0000 0 */ 0x00027, |
|
202 /* FV2 1110 010 0 0000 0000 0 */ 0x00027, |
|
203 /* FV3 1110 110 0 0000 0000 0 */ 0x00037, |
|
204 /* FV4 1110 010 0 0000 0000 0 */ 0x00027, |
|
205 /* BV1 1110 010 0 0011 0000 0 */ 0x00c27, |
|
206 /* BV2 1110 010 0 0010 0000 0 */ 0x00427, |
|
207 /* BD 1110 010 0 0000 0000 0 */ 0x00027, |
|
208 /* TONE 1111 011 0 0000 0000 0 */ 0x0006f, |
|
209 /* AD1 1110 010 0 0000 0000 0 */ 0x00027, |
|
210 /* AD2 1110 010 0 0000 0000 0 */ 0x00027, |
|
211 /* AD3 1110 010 0 0000 0000 0 */ 0x00027, |
|
212 /* AV1 1110 010 0 0011 0000 0 */ 0x00c27, |
|
213 /* AV2 1110 010 0 0010 0000 0 */ 0x00427, |
|
214 /* AV3 1110 010 0 0010 0100 0 */ 0x02427 |
|
215 }; |
|
216 |
|
217 private static final int[] COMPOSABLE = { |
|
218 /* NON 0000 000 0 0000 0000 0 */ 0x00000, |
|
219 /* CONS 0000 001 1 1111 1111 1 */ 0x1ffc0, |
|
220 /* LV 0000 000 0 0000 0000 0 */ 0x00000, |
|
221 /* FV1 0000 000 0 0000 0000 0 */ 0x00000, |
|
222 /* FV2 0000 000 0 0000 0000 0 */ 0x00000, |
|
223 /* FV3 0000 000 0 0000 0000 0 */ 0x00000, |
|
224 /* FV4 0000 000 0 0000 0000 0 */ 0x00000, |
|
225 /* BV1 0000 000 0 0011 0000 0 */ 0x00c00, |
|
226 /* BV2 0000 000 0 0010 0000 0 */ 0x00400, |
|
227 /* BD 0000 000 0 0000 0000 0 */ 0x00000, |
|
228 /* TONE 0000 001 0 0000 0000 0 */ 0x00040, |
|
229 /* AD1 0000 000 0 0000 0000 0 */ 0x00000, |
|
230 /* AD2 0000 000 0 0000 0000 0 */ 0x00000, |
|
231 /* AD3 0000 000 0 0000 0000 0 */ 0x00000, |
|
232 /* AV1 0000 000 0 0011 0000 0 */ 0x00c00, |
|
233 /* AV2 0000 000 0 0010 0000 0 */ 0x00400, |
|
234 /* AV3 0000 000 0 0010 0100 0 */ 0x02400 |
|
235 }; |
|
236 |
|
237 private static final int[] REPLACABLE = { |
|
238 /* NON 0000 000 0 0000 0000 0 */ 0x00000, |
|
239 /* CONS 0000 000 0 0000 0000 0 */ 0x00000, |
|
240 /* LV 0000 000 0 0000 0000 0 */ 0x00000, |
|
241 /* FV1 0000 000 0 0000 0000 0 */ 0x00000, |
|
242 /* FV2 0000 000 0 0000 0000 0 */ 0x00000, |
|
243 /* FV3 0000 000 0 0000 0000 0 */ 0x00000, |
|
244 /* FV4 0000 001 1 1001 1111 1 */ 0x1f9c0, |
|
245 /* BV1 0000 001 1 1100 1111 1 */ 0x1f3c0, |
|
246 /* BV2 0000 001 1 1101 1111 1 */ 0x1fbc0, |
|
247 /* BD 0000 001 1 1111 1111 1 */ 0x1ffc0, |
|
248 /* TONE 0000 000 0 0111 1100 0 */ 0x03e00, |
|
249 /* AD1 0000 001 0 1111 1101 1 */ 0x1bf40, |
|
250 /* AD2 0000 001 1 1111 1111 1 */ 0x1ffc0, |
|
251 /* AD3 0000 001 1 1111 1111 0 */ 0x0ffc0, |
|
252 /* AV1 0000 001 1 1100 1111 1 */ 0x1f3c0, |
|
253 /* AV2 0000 001 1 1101 1111 1 */ 0x1fbc0, |
|
254 /* AV3 0000 001 1 1101 1011 1 */ 0x1dbc0 |
|
255 }; |
|
256 |
|
257 private static final int[] SWAPPABLE = { |
|
258 /* NON 0000 000 0 0000 0000 0 */ 0x00000, |
|
259 /* CONS 0000 000 0 0000 0000 0 */ 0x00000, |
|
260 /* LV 0000 000 0 0000 0000 0 */ 0x00000, |
|
261 /* FV1 0000 000 0 0000 0000 0 */ 0x00000, |
|
262 /* FV2 0000 000 0 0000 0000 0 */ 0x00000, |
|
263 /* FV3 0000 000 0 0000 0000 0 */ 0x00000, |
|
264 /* FV4 0000 000 0 0010 0000 0 */ 0x00400, |
|
265 /* BV1 0000 000 0 0000 0000 0 */ 0x00000, |
|
266 /* BV2 0000 000 0 0000 0000 0 */ 0x00000, |
|
267 /* BD 0000 000 0 0000 0000 0 */ 0x00000, |
|
268 /* TONE 0000 000 1 1000 0011 1 */ 0x1c180, |
|
269 /* AD1 0000 000 1 0000 0010 0 */ 0x04080, |
|
270 /* AD2 0000 000 0 0000 0000 0 */ 0x00000, |
|
271 /* AD3 0000 000 0 0000 0000 1 */ 0x10000, |
|
272 /* AV1 0000 000 0 0000 0000 0 */ 0x00000, |
|
273 /* AV2 0000 000 0 0000 0000 0 */ 0x00000, |
|
274 /* AV3 0000 000 0 0000 0000 0 */ 0x00000 |
|
275 }; |
|
276 |
|
277 public static boolean isInputValid(char c1, char c2) { |
|
278 return isValid(c1, c2, INPUTVALIDITY); |
|
279 } |
|
280 |
|
281 public static boolean isComposable(char c1, char c2) { |
|
282 return isValid(c1, c2, COMPOSABLE); |
|
283 } |
|
284 |
|
285 public static boolean isSwappable(char c1, char c2) { |
|
286 return isValid(c1, c2, SWAPPABLE); |
|
287 } |
|
288 |
|
289 public static boolean isReplacable(char c1, char c2) { |
|
290 return isValid(c1, c2, REPLACABLE); |
|
291 } |
|
292 |
|
293 public static boolean isForward(char c) { |
|
294 return (getCharType(c) < FV4); |
|
295 } |
|
296 |
|
297 public static boolean isDead(char c) { |
|
298 return (getCharType(c) > FV3); |
|
299 } |
|
300 |
|
301 public boolean isInputValid(char current) { |
|
302 int offset = requests.getInsertPositionOffset(); |
|
303 if (offset == 0) { |
|
304 byte charType = getCharType(current); |
|
305 return ((charType < FV1) || (charType == FV3)); |
|
306 } |
|
307 else { |
|
308 char prev = requests.getCommittedText(offset-1, offset, null).first(); |
|
309 |
|
310 if(isForward(current)) { |
|
311 if (isInputValid(prev, current)) { |
|
312 if (getCharType(prev) == TONE && |
|
313 getCharType(current) == FV1) { |
|
314 if (offset == 1) { |
|
315 return true; |
|
316 } else { |
|
317 char pprev = |
|
318 requests.getCommittedText(offset-2, offset-1, null).first(); |
|
319 return isInputValid(pprev, current); |
|
320 } |
|
321 } else { |
|
322 return true; |
|
323 } |
|
324 } else if (prev == '\u0e32' && // SARA AA |
|
325 current == '\u0e30') { // SARA A |
|
326 return true; |
|
327 } else if (prev == '\u0e4d' && // NIKAHIT |
|
328 current == '\u0e32') { // SARA AA |
|
329 // Special compose to SARA AM |
|
330 return true; |
|
331 } else { |
|
332 return false; |
|
333 } |
|
334 } else { |
|
335 if(isInputValid(prev, current)) { |
|
336 if (getCharType(prev) == TONE && |
|
337 getCharType(current) == FV4) { |
|
338 return (offset != 1); |
|
339 } else { |
|
340 return true; |
|
341 } |
|
342 } else { |
|
343 return false; |
|
344 } |
|
345 } |
|
346 } |
|
347 } |
|
348 } |
|