]> git.saurik.com Git - apple/icu.git/blob - icuSources/layout/ThaiShaping.cpp
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / layout / ThaiShaping.cpp
1 /*
2 * @(#)ThaiShaping.cpp 1.13 00/03/15
3 *
4 * (C) Copyright IBM Corp. 1998-2003 - All Rights Reserved
5 *
6 */
7
8 #include "LETypes.h"
9 #include "LEGlyphFilter.h"
10 #include "OpenTypeTables.h"
11 #include "ThaiShaping.h"
12
13 U_NAMESPACE_BEGIN
14
15 enum {
16 CH_SPACE = 0x0020,
17 CH_YAMAKKAN = 0x0E4E,
18 CH_MAI_HANAKAT = 0x0E31,
19 CH_SARA_AA = 0x0E32,
20 CH_SARA_AM = 0x0E33,
21 CH_SARA_UEE = 0x0E37,
22 CH_MAITAIKHU = 0x0E47,
23 CH_NIKHAHIT = 0x0E4D,
24 CH_SARA_U = 0x0E38,
25 CH_PHINTHU = 0x0E3A,
26 CH_YO_YING = 0x0E0D,
27 CH_THO_THAN = 0x0E10,
28 CH_DOTTED_CIRCLE = 0x25CC
29 };
30
31 le_uint8 ThaiShaping::getCharClass(LEUnicode ch)
32 {
33 le_uint8 charClass = NON;
34
35 if (ch >= 0x0E00 && ch <= 0x0E5B) {
36 charClass = classTable[ch - 0x0E00];
37 }
38
39 return charClass;
40 }
41
42
43 LEUnicode ThaiShaping::leftAboveVowel(LEUnicode vowel, le_uint8 glyphSet)
44 {
45 static LEUnicode leftAboveVowels[][7] = {
46 {0x0E61, 0x0E32, 0x0E33, 0x0E64, 0x0E65, 0x0E66, 0x0E67},
47 {0xF710, 0x0E32, 0x0E33, 0xF701, 0xF702, 0xF703, 0xF704},
48 {0xF884, 0x0E32, 0x0E33, 0xF885, 0xF886, 0xF887, 0xF788},
49 {0x0E31, 0x0E32, 0x0E33, 0x0E34, 0x0E35, 0x0E36, 0x0E37}
50 };
51
52 if (vowel >= CH_MAI_HANAKAT && vowel <= CH_SARA_UEE) {
53 return leftAboveVowels[glyphSet][vowel - CH_MAI_HANAKAT];
54 }
55
56 if (vowel == CH_YAMAKKAN && glyphSet == 0) {
57 return 0x0E7E;
58 }
59
60 return vowel;
61 }
62
63 LEUnicode ThaiShaping::lowerRightTone(LEUnicode tone, le_uint8 glyphSet)
64 {
65 static LEUnicode lowerRightTones[][7] = {
66 {0x0E68, 0x0E69, 0x0E6A, 0x0E6B, 0x0E6C, 0x0E6D, 0x0E6E},
67 {0x0E47, 0xF70A, 0xF70B, 0xF70C, 0xF70D, 0xF70E, 0x0E4D},
68 {0x0E47, 0xF88B, 0xF88E, 0xF891, 0xF894, 0xF897, 0x0E4D},
69 {0x0E47, 0x0E48, 0x0E49, 0x0E4A, 0x0E4B, 0x0E4C, 0x0E4D}
70 };
71
72 if (tone >= CH_MAITAIKHU && tone <= CH_NIKHAHIT) {
73 return lowerRightTones[glyphSet][tone - CH_MAITAIKHU];
74 }
75
76 return tone;
77 }
78
79 LEUnicode ThaiShaping::lowerLeftTone(LEUnicode tone, le_uint8 glyphSet)
80 {
81 static LEUnicode lowerLeftTones[][7] = {
82 {0x0E76, 0x0E77, 0x0E78, 0x0E79, 0x0E7A, 0x0E7B, 0x0E7C},
83 {0xF712, 0xF705, 0xF706, 0xF707, 0xF708, 0xF709, 0xF711},
84 {0xF889, 0xF88C, 0xF88F, 0xF892, 0xF895, 0xF898, 0xF899},
85 {0x0E47, 0x0E48, 0x0E49, 0x0E4A, 0x0E4B, 0x0E4C, 0x0E4D}
86 };
87
88 if (tone >= CH_MAITAIKHU && tone <= CH_NIKHAHIT) {
89 return lowerLeftTones[glyphSet][tone - CH_MAITAIKHU];
90 }
91
92 return tone;
93 }
94
95 LEUnicode ThaiShaping::upperLeftTone(LEUnicode tone, le_uint8 glyphSet)
96 {
97 static LEUnicode upperLeftTones[][7] = {
98 {0x0E6F, 0x0E70, 0x0E71, 0x0E72, 0x0E73, 0x0E74, 0x0E75},
99 {0xF712, 0xF713, 0xF714, 0xF715, 0xF716, 0xF717, 0xF711},
100 {0xF889, 0xF88A, 0xF88D, 0xF890, 0xF893, 0xF896, 0xF899},
101 {0x0E47, 0x0E48, 0x0E49, 0x0E4A, 0x0E4B, 0x0E4C, 0x0E4D}
102 };
103
104 if (tone >= CH_MAITAIKHU && tone <= CH_NIKHAHIT) {
105 return upperLeftTones[glyphSet][tone - CH_MAITAIKHU];
106 }
107
108 return tone;
109 }
110
111 LEUnicode ThaiShaping::lowerBelowVowel(LEUnicode vowel, le_uint8 glyphSet)
112 {
113 static LEUnicode lowerBelowVowels[][3] = {
114 {0x0E3C, 0x0E3D, 0x0E3E},
115 {0xF718, 0xF719, 0xF71A},
116 {0x0E38, 0x0E39, 0x0E3A},
117 {0x0E38, 0x0E39, 0x0E3A}
118
119 };
120
121 if (vowel >= CH_SARA_U && vowel <= CH_PHINTHU) {
122 return lowerBelowVowels[glyphSet][vowel - CH_SARA_U];
123 }
124
125 return vowel;
126 }
127
128 LEUnicode ThaiShaping::noDescenderCOD(LEUnicode cod, le_uint8 glyphSet)
129 {
130 static LEUnicode noDescenderCODs[][4] = {
131 {0x0E60, 0x0E0E, 0x0E0F, 0x0E63},
132 {0xF70F, 0x0E0E, 0x0E0F, 0xF700},
133 {0x0E0D, 0x0E0E, 0x0E0F, 0x0E10},
134 {0x0E0D, 0x0E0E, 0x0E0F, 0x0E10}
135
136 };
137
138 if (cod >= CH_YO_YING && cod <= CH_THO_THAN) {
139 return noDescenderCODs[glyphSet][cod - CH_YO_YING];
140 }
141
142 return cod;
143 }
144
145 le_uint8 ThaiShaping::doTransition (StateTransition transition, LEUnicode currChar, le_int32 inputIndex, le_uint8 glyphSet,
146 LEUnicode errorChar, LEUnicode *outputBuffer, le_int32 *charIndicies, le_int32 &outputIndex)
147 {
148 switch (transition.action) {
149 case _A:
150 charIndicies[outputIndex] = inputIndex;
151 outputBuffer[outputIndex++] = currChar;
152 break;
153
154 case _C:
155 charIndicies[outputIndex] = inputIndex;
156 outputBuffer[outputIndex++] = currChar;
157 break;
158
159 case _D:
160 charIndicies[outputIndex] = inputIndex;
161 outputBuffer[outputIndex++] = leftAboveVowel(currChar, glyphSet);
162 break;
163
164 case _E:
165 charIndicies[outputIndex] = inputIndex;
166 outputBuffer[outputIndex++] = lowerRightTone(currChar, glyphSet);
167 break;
168
169 case _F:
170 charIndicies[outputIndex] = inputIndex;
171 outputBuffer[outputIndex++] = lowerLeftTone(currChar, glyphSet);
172 break;
173
174 case _G:
175 charIndicies[outputIndex] = inputIndex;
176 outputBuffer[outputIndex++] = upperLeftTone(currChar, glyphSet);
177 break;
178
179 case _H:
180 {
181 LEUnicode cod = outputBuffer[outputIndex - 1];
182 LEUnicode coa = noDescenderCOD(cod, glyphSet);
183
184 if (cod != coa) {
185 outputBuffer[outputIndex - 1] = coa;
186
187 charIndicies[outputIndex] = inputIndex;
188 outputBuffer[outputIndex++] = currChar;
189 break;
190 }
191
192 charIndicies[outputIndex] = inputIndex;
193 outputBuffer[outputIndex++] = lowerBelowVowel(currChar, glyphSet);
194 break;
195 }
196
197 case _R:
198 charIndicies[outputIndex] = inputIndex;
199 outputBuffer[outputIndex++] = errorChar;
200
201 charIndicies[outputIndex] = inputIndex;
202 outputBuffer[outputIndex++] = currChar;
203 break;
204
205 case _S:
206 if (currChar == CH_SARA_AM) {
207 charIndicies[outputIndex] = inputIndex;
208 outputBuffer[outputIndex++] = errorChar;
209 }
210
211 charIndicies[outputIndex] = inputIndex;
212 outputBuffer[outputIndex++] = currChar;
213 break;
214
215 default:
216 // FIXME: if we get here, there's an error
217 // in the state table!
218 charIndicies[outputIndex] = inputIndex;
219 outputBuffer[outputIndex++] = currChar;
220 break;
221 }
222
223 return transition.nextState;
224 }
225
226 le_uint8 ThaiShaping::getNextState(LEUnicode ch, le_uint8 prevState, le_int32 inputIndex, le_uint8 glyphSet, LEUnicode errorChar,
227 le_uint8 &charClass, LEUnicode *output, le_int32 *charIndicies, le_int32 &outputIndex)
228 {
229 StateTransition transition;
230
231 charClass = getCharClass(ch);
232 transition = getTransition(prevState, charClass);
233
234 return doTransition(transition, ch, inputIndex, glyphSet, errorChar, output, charIndicies, outputIndex);
235 }
236
237 le_bool ThaiShaping::isLegalHere(LEUnicode ch, le_uint8 prevState)
238 {
239 le_uint8 charClass = getCharClass(ch);
240 StateTransition transition = getTransition(prevState, charClass);
241
242 switch (transition.action) {
243 case _A:
244 case _C:
245 case _D:
246 case _E:
247 case _F:
248 case _G:
249 case _H:
250 return true;
251
252 case _R:
253 case _S:
254 return false;
255
256 default:
257 // FIXME: if we get here, there's an error
258 // in the state table!
259 return false;
260 }
261 }
262
263 le_int32 ThaiShaping::compose(const LEUnicode *input, le_int32 offset, le_int32 charCount, le_uint8 glyphSet,
264 LEUnicode errorChar, LEUnicode *output, le_int32 *charIndicies)
265 {
266 le_uint8 state = 0;
267 le_int32 inputIndex;
268 le_int32 outputIndex = 0;
269 le_uint8 conState = 0xFF;
270 le_int32 conInput = -1;
271 le_int32 conOutput = -1;
272
273 for (inputIndex = 0; inputIndex < charCount; inputIndex += 1) {
274 LEUnicode ch = input[inputIndex + offset];
275 le_uint8 charClass;
276
277 // Decompose SARA AM into NIKHAHIT + SARA AA
278 if (ch == CH_SARA_AM && isLegalHere(ch, state)) {
279 outputIndex = conOutput;
280 state = getNextState(CH_NIKHAHIT, conState, inputIndex, glyphSet, errorChar, charClass,
281 output, charIndicies, outputIndex);
282
283 for (int j = conInput + 1; j < inputIndex; j += 1) {
284 ch = input[j + offset];
285 state = getNextState(ch, state, j, glyphSet, errorChar, charClass,
286 output, charIndicies, outputIndex);
287 }
288
289 ch = CH_SARA_AA;
290 }
291
292 state = getNextState(ch, state, inputIndex, glyphSet, errorChar, charClass,
293 output, charIndicies, outputIndex);
294
295 if (charClass >= CON && charClass <= COD) {
296 conState = state;
297 conInput = inputIndex;
298 conOutput = outputIndex;
299 }
300 }
301
302 return outputIndex;
303 }
304
305 U_NAMESPACE_END