]> git.saurik.com Git - apple/icu.git/blame - icuSources/layout/KhmerReordering.cpp
ICU-491.11.1.tar.gz
[apple/icu.git] / icuSources / layout / KhmerReordering.cpp
CommitLineData
73c04bcf
A
1/*
2 *
46f4442e 3 * (C) Copyright IBM Corp. 1998-2007 - All Rights Reserved
73c04bcf
A
4 *
5 * This file is a modification of the ICU file IndicReordering.cpp
6 * by Jens Herden and Javier Sola for Khmer language
7 *
8 */
9
10#include "LETypes.h"
11#include "OpenTypeTables.h"
12#include "KhmerReordering.h"
13#include "LEGlyphStorage.h"
14
15
16U_NAMESPACE_BEGIN
17
18// Characters that get refered to by name...
19enum
20{
21 C_SIGN_ZWNJ = 0x200C,
22 C_SIGN_ZWJ = 0x200D,
23 C_DOTTED_CIRCLE = 0x25CC,
24 C_RO = 0x179A,
25 C_VOWEL_AA = 0x17B6,
26 C_SIGN_NIKAHIT = 0x17C6,
27 C_VOWEL_E = 0x17C1,
28 C_COENG = 0x17D2
29};
30
31
32enum
33{
34 // simple classes, they are used in the statetable (in this file) to control the length of a syllable
35 // they are also used to know where a character should be placed (location in reference to the base character)
36 // and also to know if a character, when independtly displayed, should be displayed with a dotted-circle to
37 // indicate error in syllable construction
38 _xx = KhmerClassTable::CC_RESERVED,
39 _sa = KhmerClassTable::CC_SIGN_ABOVE | KhmerClassTable::CF_DOTTED_CIRCLE | KhmerClassTable::CF_POS_ABOVE,
40 _sp = KhmerClassTable::CC_SIGN_AFTER | KhmerClassTable::CF_DOTTED_CIRCLE| KhmerClassTable::CF_POS_AFTER,
41 _c1 = KhmerClassTable::CC_CONSONANT | KhmerClassTable::CF_CONSONANT,
42 _c2 = KhmerClassTable::CC_CONSONANT2 | KhmerClassTable::CF_CONSONANT,
43 _c3 = KhmerClassTable::CC_CONSONANT3 | KhmerClassTable::CF_CONSONANT,
44 _rb = KhmerClassTable::CC_ROBAT | KhmerClassTable::CF_POS_ABOVE | KhmerClassTable::CF_DOTTED_CIRCLE,
45 _cs = KhmerClassTable::CC_CONSONANT_SHIFTER | KhmerClassTable::CF_DOTTED_CIRCLE | KhmerClassTable::CF_SHIFTER,
46 _dl = KhmerClassTable::CC_DEPENDENT_VOWEL | KhmerClassTable::CF_POS_BEFORE | KhmerClassTable::CF_DOTTED_CIRCLE,
47 _db = KhmerClassTable::CC_DEPENDENT_VOWEL | KhmerClassTable::CF_POS_BELOW | KhmerClassTable::CF_DOTTED_CIRCLE,
48 _da = KhmerClassTable::CC_DEPENDENT_VOWEL | KhmerClassTable::CF_POS_ABOVE | KhmerClassTable::CF_DOTTED_CIRCLE | KhmerClassTable::CF_ABOVE_VOWEL,
49 _dr = KhmerClassTable::CC_DEPENDENT_VOWEL | KhmerClassTable::CF_POS_AFTER | KhmerClassTable::CF_DOTTED_CIRCLE,
50 _co = KhmerClassTable::CC_COENG | KhmerClassTable::CF_COENG | KhmerClassTable::CF_DOTTED_CIRCLE,
51
52 // split vowel
53 _va = _da | KhmerClassTable::CF_SPLIT_VOWEL,
54 _vr = _dr | KhmerClassTable::CF_SPLIT_VOWEL
55};
56
57
58// Character class tables
59// _xx character does not combine into syllable, such as numbers, puntuation marks, non-Khmer signs...
60// _sa Sign placed above the base
61// _sp Sign placed after the base
62// _c1 Consonant of type 1 or independent vowel (independent vowels behave as type 1 consonants)
63// _c2 Consonant of type 2 (only RO)
64// _c3 Consonant of type 3
65// _rb Khmer sign robat u17CC. combining mark for subscript consonants
66// _cd Consonant-shifter
67// _dl Dependent vowel placed before the base (left of the base)
68// _db Dependent vowel placed below the base
69// _da Dependent vowel placed above the base
70// _dr Dependent vowel placed behind the base (right of the base)
71// _co Khmer combining mark COENG u17D2, combines with the consonant or independent vowel following
72// it to create a subscript consonant or independent vowel
73// _va Khmer split vowel in wich the first part is before the base and the second one above the base
74// _vr Khmer split vowel in wich the first part is before the base and the second one behind (right of) the base
75
76static const KhmerClassTable::CharClass khmerCharClasses[] =
77{
78 _c1, _c1, _c1, _c3, _c1, _c1, _c1, _c1, _c3, _c1, _c1, _c1, _c1, _c3, _c1, _c1, // 1780 - 178F
79 _c1, _c1, _c1, _c1, _c3, _c1, _c1, _c1, _c1, _c3, _c2, _c1, _c1, _c1, _c3, _c3, // 1790 - 179F
80 _c1, _c3, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, // 17A0 - 17AF
81 _c1, _c1, _c1, _c1, _dr, _dr, _dr, _da, _da, _da, _da, _db, _db, _db, _va, _vr, // 17B0 - 17BF
82 _vr, _dl, _dl, _dl, _vr, _vr, _sa, _sp, _sp, _cs, _cs, _sa, _rb, _sa, _sa, _sa, // 17C0 - 17CF
83 _sa, _sa, _co, _sa, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _sa, _xx, _xx, // 17D0 - 17DF
84};
85
86
87//
88// Khmer Class Tables
89//
90
91//
92// The range of characters defined in the above table is defined here. FOr Khmer 1780 to 17DF
93// Even if the Khmer range is bigger, all other characters are not combinable, and therefore treated
94// as _xx
95static const KhmerClassTable khmerClassTable = {0x1780, 0x17df, khmerCharClasses};
96
97
98// Below we define how a character in the input string is either in the khmerCharClasses table
99// (in which case we get its type back), a ZWJ or ZWNJ (two characters that may appear
100// within the syllable, but are not in the table) we also get their type back, or an unknown object
101// in which case we get _xx (CC_RESERVED) back
102KhmerClassTable::CharClass KhmerClassTable::getCharClass(LEUnicode ch) const
103{
104
105 if (ch == C_SIGN_ZWJ) {
106 return CC_ZERO_WIDTH_J_MARK;
107 }
108
109 if (ch == C_SIGN_ZWNJ) {
110 return CC_ZERO_WIDTH_NJ_MARK;
111 }
112
113 if (ch < firstChar || ch > lastChar) {
114 return CC_RESERVED;
115 }
116
117 return classTable[ch - firstChar];
118}
119
120const KhmerClassTable *KhmerClassTable::getKhmerClassTable()
121{
122 return &khmerClassTable;
123}
124
125
126
127class KhmerReorderingOutput : public UMemory {
128private:
46f4442e 129 le_int32 fSyllableCount;
73c04bcf
A
130 le_int32 fOutIndex;
131 LEUnicode *fOutChars;
132
133 LEGlyphStorage &fGlyphStorage;
134
135
136public:
137 KhmerReorderingOutput(LEUnicode *outChars, LEGlyphStorage &glyphStorage)
46f4442e 138 : fSyllableCount(0), fOutIndex(0), fOutChars(outChars), fGlyphStorage(glyphStorage)
73c04bcf
A
139 {
140 // nothing else to do...
141 }
142
143 ~KhmerReorderingOutput()
144 {
145 // nothing to do here...
146 }
147
46f4442e
A
148 void reset()
149 {
150 fSyllableCount += 1;
151 }
152
73c04bcf
A
153 void writeChar(LEUnicode ch, le_uint32 charIndex, FeatureMask charFeatures)
154 {
155 LEErrorCode success = LE_NO_ERROR;
156
157 fOutChars[fOutIndex] = ch;
158
159 fGlyphStorage.setCharIndex(fOutIndex, charIndex, success);
46f4442e 160 fGlyphStorage.setAuxData(fOutIndex, charFeatures | (fSyllableCount & LE_GLYPH_GROUP_MASK), success);
73c04bcf
A
161
162 fOutIndex += 1;
163 }
164
165 le_int32 getOutputIndex()
166 {
167 return fOutIndex;
168 }
169};
170
171
172#define blwfFeatureTag LE_BLWF_FEATURE_TAG
173#define pstfFeatureTag LE_PSTF_FEATURE_TAG
174#define presFeatureTag LE_PRES_FEATURE_TAG
175#define blwsFeatureTag LE_BLWS_FEATURE_TAG
176#define abvsFeatureTag LE_ABVS_FEATURE_TAG
177#define pstsFeatureTag LE_PSTS_FEATURE_TAG
178
179#define blwmFeatureTag LE_BLWM_FEATURE_TAG
180#define abvmFeatureTag LE_ABVM_FEATURE_TAG
181#define distFeatureTag LE_DIST_FEATURE_TAG
182
183#define prefFeatureTag LE_PREF_FEATURE_TAG
184#define abvfFeatureTag LE_ABVF_FEATURE_TAG
185#define cligFeatureTag LE_CLIG_FEATURE_TAG
186#define mkmkFeatureTag LE_MKMK_FEATURE_TAG
187
188#define prefFeatureMask 0x80000000UL
189#define blwfFeatureMask 0x40000000UL
190#define abvfFeatureMask 0x20000000UL
191#define pstfFeatureMask 0x10000000UL
192#define presFeatureMask 0x08000000UL
193#define blwsFeatureMask 0x04000000UL
194#define abvsFeatureMask 0x02000000UL
195#define pstsFeatureMask 0x01000000UL
196#define cligFeatureMask 0x00800000UL
197#define distFeatureMask 0x00400000UL
198#define blwmFeatureMask 0x00200000UL
199#define abvmFeatureMask 0x00100000UL
200#define mkmkFeatureMask 0x00080000UL
201
202#define tagPref (prefFeatureMask | presFeatureMask | cligFeatureMask | distFeatureMask)
203#define tagAbvf (abvfFeatureMask | abvsFeatureMask | cligFeatureMask | distFeatureMask | abvmFeatureMask | mkmkFeatureMask)
204#define tagPstf (blwfFeatureMask | blwsFeatureMask | prefFeatureMask | presFeatureMask | pstfFeatureMask | pstsFeatureMask | cligFeatureMask | distFeatureMask | blwmFeatureMask)
205#define tagBlwf (blwfFeatureMask | blwsFeatureMask | cligFeatureMask | distFeatureMask | blwmFeatureMask | mkmkFeatureMask)
206#define tagDefault (prefFeatureMask | blwfFeatureMask | presFeatureMask | blwsFeatureMask | cligFeatureMask | distFeatureMask | abvmFeatureMask | blwmFeatureMask | mkmkFeatureMask)
207
208
209
210// These are in the order in which the features need to be applied
211// for correct processing
212static const FeatureMap featureMap[] =
213{
214 // Shaping features
215 {prefFeatureTag, prefFeatureMask},
216 {blwfFeatureTag, blwfFeatureMask},
217 {abvfFeatureTag, abvfFeatureMask},
218 {pstfFeatureTag, pstfFeatureMask},
219 {presFeatureTag, presFeatureMask},
220 {blwsFeatureTag, blwsFeatureMask},
221 {abvsFeatureTag, abvsFeatureMask},
222 {pstsFeatureTag, pstsFeatureMask},
223 {cligFeatureTag, cligFeatureMask},
224
225 // Positioning features
226 {distFeatureTag, distFeatureMask},
227 {blwmFeatureTag, blwmFeatureMask},
228 {abvmFeatureTag, abvmFeatureMask},
229 {mkmkFeatureTag, mkmkFeatureMask},
230};
231
232static const le_int32 featureMapCount = LE_ARRAY_SIZE(featureMap);
233
234// The stateTable is used to calculate the end (the length) of a well
235// formed Khmer Syllable.
236//
237// Each horizontal line is ordered exactly the same way as the values in KhmerClassTable
238// CharClassValues in KhmerReordering.h This coincidence of values allows the
239// follow up of the table.
240//
241// Each line corresponds to a state, which does not necessarily need to be a type
242// of component... for example, state 2 is a base, with is always a first character
243// in the syllable, but the state could be produced a consonant of any type when
244// it is the first character that is analysed (in ground state).
245//
246// Differentiating 3 types of consonants is necessary in order to
247// forbid the use of certain combinations, such as having a second
248// coeng after a coeng RO,
249// The inexistent possibility of having a type 3 after another type 3 is permitted,
250// eliminating it would very much complicate the table, and it does not create typing
251// problems, as the case above.
252//
253// The table is quite complex, in order to limit the number of coeng consonants
254// to 2 (by means of the table).
255//
256// There a peculiarity, as far as Unicode is concerned:
257// - The consonant-shifter is considered in two possible different
258// locations, the one considered in Unicode 3.0 and the one considered in
259// Unicode 4.0. (there is a backwards compatibility problem in this standard).
260
261
262// xx independent character, such as a number, punctuation sign or non-khmer char
263//
264// c1 Khmer consonant of type 1 or an independent vowel
265// that is, a letter in which the subscript for is only under the
266// base, not taking any space to the right or to the left
267//
268// c2 Khmer consonant of type 2, the coeng form takes space under
269// and to the left of the base (only RO is of this type)
270//
271// c3 Khmer consonant of type 3. Its subscript form takes space under
272// and to the right of the base.
273//
274// cs Khmer consonant shifter
275//
276// rb Khmer robat
277//
278// co coeng character (u17D2)
279//
280// dv dependent vowel (including split vowels, they are treated in the same way).
281// even if dv is not defined above, the component that is really tested for is
282// KhmerClassTable::CC_DEPENDENT_VOWEL, which is common to all dependent vowels
283//
284// zwj Zero Width joiner
285//
286// zwnj Zero width non joiner
287//
288// sa above sign
289//
290// sp post sign
291//
292// there are lines with equal content but for an easier understanding
293// (and maybe change in the future) we did not join them
294//
295static const le_int8 khmerStateTable[][KhmerClassTable::CC_COUNT] =
296{
297
298// xx c1 c2 c3 zwnj cs rb co dv sa sp zwj
299 { 1, 2, 2, 2, 1, 1, 1, 6, 1, 1, 1, 2}, // 0 - ground state
300 {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 1 - exit state (or sign to the right of the syllable)
301 {-1, -1, -1, -1, 3, 4, 5, 6, 16, 17, 1, -1}, // 2 - Base consonant
302 {-1, -1, -1, -1, -1, 4, -1, -1, 16, -1, -1, -1}, // 3 - First ZWNJ before a register shifter
303 // It can only be followed by a shifter or a vowel
304 {-1, -1, -1, -1, 15, -1, -1, 6, 16, 17, 1, 14}, // 4 - First register shifter
305 {-1, -1, -1, -1, -1, -1, -1, -1, 20, -1, 1, -1}, // 5 - Robat
306 {-1, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, -1}, // 6 - First Coeng
307 {-1, -1, -1, -1, 12, 13, -1, 10, 16, 17, 1, 14}, // 7 - First consonant of type 1 after coeng
308 {-1, -1, -1, -1, 12, 13, -1, -1, 16, 17, 1, 14}, // 8 - First consonant of type 2 after coeng
309 {-1, -1, -1, -1, 12, 13, -1, 10, 16, 17, 1, 14}, // 9 - First consonant or type 3 after ceong
310 {-1, 11, 11, 11, -1, -1, -1, -1, -1, -1, -1, -1}, // 10 - Second Coeng (no register shifter before)
311 {-1, -1, -1, -1, 15, -1, -1, -1, 16, 17, 1, 14}, // 11 - Second coeng consonant (or ind. vowel) no register shifter before
312 {-1, -1, -1, -1, -1, 13, -1, -1, 16, -1, -1, -1}, // 12 - Second ZWNJ before a register shifter
313 {-1, -1, -1, -1, 15, -1, -1, -1, 16, 17, 1, 14}, // 13 - Second register shifter
314 {-1, -1, -1, -1, -1, -1, -1, -1, 16, -1, -1, -1}, // 14 - ZWJ before vowel
315 {-1, -1, -1, -1, -1, -1, -1, -1, 16, -1, -1, -1}, // 15 - ZWNJ before vowel
316 {-1, -1, -1, -1, -1, -1, -1, -1, -1, 17, 1, 18}, // 16 - dependent vowel
317 {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 18}, // 17 - sign above
318 {-1, -1, -1, -1, -1, -1, -1, 19, -1, -1, -1, -1}, // 18 - ZWJ after vowel
319 {-1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1}, // 19 - Third coeng
320 {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1}, // 20 - dependent vowel after a Robat
321
322};
323
324
325const FeatureMap *KhmerReordering::getFeatureMap(le_int32 &count)
326{
327 count = featureMapCount;
328
329 return featureMap;
330}
331
332
333// Given an input string of characters and a location in which to start looking
334// calculate, using the state table, which one is the last character of the syllable
335// that starts in the starting position.
336le_int32 KhmerReordering::findSyllable(const KhmerClassTable *classTable, const LEUnicode *chars, le_int32 prev, le_int32 charCount)
337{
338 le_int32 cursor = prev;
339 le_int8 state = 0;
340
341 while (cursor < charCount) {
342 KhmerClassTable::CharClass charClass = (classTable->getCharClass(chars[cursor]) & KhmerClassTable::CF_CLASS_MASK);
343
344 state = khmerStateTable[state][charClass];
345
346 if (state < 0) {
347 break;
348 }
349
350 cursor += 1;
351 }
352
353 return cursor;
354}
355
356
357// This is the real reordering function as applied to the Khmer language
358
359le_int32 KhmerReordering::reorder(const LEUnicode *chars, le_int32 charCount, le_int32 /*scriptCode*/,
360 LEUnicode *outChars, LEGlyphStorage &glyphStorage)
361{
362 const KhmerClassTable *classTable = KhmerClassTable::getKhmerClassTable();
363
364 KhmerReorderingOutput output(outChars, glyphStorage);
365 KhmerClassTable::CharClass charClass;
366 le_int32 i, prev = 0, coengRo;
367
368
369 // This loop only exits when we reach the end of a run, which may contain
370 // several syllables.
371 while (prev < charCount) {
372 le_int32 syllable = findSyllable(classTable, chars, prev, charCount);
46f4442e
A
373
374 output.reset();
73c04bcf
A
375
376 // write a pre vowel or the pre part of a split vowel first
377 // and look out for coeng + ro. RO is the only vowel of type 2, and
378 // therefore the only one that requires saving space before the base.
379 coengRo = -1; // There is no Coeng Ro, if found this value will change
380 for (i = prev; i < syllable; i += 1) {
381 charClass = classTable->getCharClass(chars[i]);
382
383 // if a split vowel, write the pre part. In Khmer the pre part
384 // is the same for all split vowels, same glyph as pre vowel C_VOWEL_E
385 if (charClass & KhmerClassTable::CF_SPLIT_VOWEL) {
386 output.writeChar(C_VOWEL_E, i, tagPref);
387 break; // there can be only one vowel
388 }
389
390 // if a vowel with pos before write it out
391 if (charClass & KhmerClassTable::CF_POS_BEFORE) {
392 output.writeChar(chars[i], i, tagPref);
393 break; // there can be only one vowel
394 }
395
396 // look for coeng + ro and remember position
397 // works because coeng + ro is always in front of a vowel (if there is a vowel)
398 // and because CC_CONSONANT2 is enough to identify it, as it is the only consonant
399 // with this flag
400 if ( (charClass & KhmerClassTable::CF_COENG) && (i + 1 < syllable) &&
401 ( (classTable->getCharClass(chars[i + 1]) & KhmerClassTable::CF_CLASS_MASK) == KhmerClassTable::CC_CONSONANT2) )
402 {
403 coengRo = i;
404 }
405 }
406
407 // write coeng + ro if found
408 if (coengRo > -1) {
409 output.writeChar(C_COENG, coengRo, tagPref);
410 output.writeChar(C_RO, coengRo + 1, tagPref);
411 }
412
413 // shall we add a dotted circle?
414 // If in the position in which the base should be (first char in the string) there is
415 // a character that has the Dotted circle flag (a character that cannot be a base)
416 // then write a dotted circle
417 if (classTable->getCharClass(chars[prev]) & KhmerClassTable::CF_DOTTED_CIRCLE) {
418 output.writeChar(C_DOTTED_CIRCLE, prev, tagDefault);
419 }
420
421 // copy what is left to the output, skipping before vowels and coeng Ro if they are present
422 for (i = prev; i < syllable; i += 1) {
423 charClass = classTable->getCharClass(chars[i]);
424
425 // skip a before vowel, it was already processed
426 if (charClass & KhmerClassTable::CF_POS_BEFORE) {
427 continue;
428 }
429
430 // skip coeng + ro, it was already processed
431 if (i == coengRo) {
432 i += 1;
433 continue;
434 }
435
436 switch (charClass & KhmerClassTable::CF_POS_MASK) {
437 case KhmerClassTable::CF_POS_ABOVE :
438 output.writeChar(chars[i], i, tagAbvf);
439 break;
440
441 case KhmerClassTable::CF_POS_AFTER :
442 output.writeChar(chars[i], i, tagPstf);
443 break;
444
445 case KhmerClassTable::CF_POS_BELOW :
446 output.writeChar(chars[i], i, tagBlwf);
447 break;
448
449 default:
450 // assign the correct flags to a coeng consonant
451 // Consonants of type 3 are taged as Post forms and those type 1 as below forms
452 if ( (charClass & KhmerClassTable::CF_COENG) && i + 1 < syllable ) {
453 if ( (classTable->getCharClass(chars[i + 1]) & KhmerClassTable::CF_CLASS_MASK)
454 == KhmerClassTable::CC_CONSONANT3) {
455 output.writeChar(chars[i], i, tagPstf);
456 i += 1;
457 output.writeChar(chars[i], i, tagPstf);
458 }
459 else {
460 output.writeChar(chars[i], i, tagBlwf);
461 i += 1;
462 output.writeChar(chars[i], i, tagBlwf);
463 }
464 break;
465 }
466 // if a shifter is followed by an above vowel change the shifter to below form,
467 // an above vowel can have two possible positions i + 1 or i + 3
468 // (position i+1 corresponds to unicode 3, position i+3 to Unicode 4)
469 // and there is an extra rule for C_VOWEL_AA + C_SIGN_NIKAHIT also for two
470 // different positions, right after the shifter or after a vowel (Unicode 4)
471 if ( (charClass & KhmerClassTable::CF_SHIFTER) && (i + 1 < syllable) ) {
472 if ((classTable->getCharClass(chars[i + 1]) & KhmerClassTable::CF_ABOVE_VOWEL)
473 || (i + 2 < syllable
474 && ( (classTable->getCharClass(chars[i + 1]) & KhmerClassTable::CF_CLASS_MASK) == C_VOWEL_AA)
475 && ( (classTable->getCharClass(chars[i + 2]) & KhmerClassTable::CF_CLASS_MASK) == C_SIGN_NIKAHIT))
476 || (i + 3 < syllable && (classTable->getCharClass(chars[i + 3]) & KhmerClassTable::CF_ABOVE_VOWEL))
477 || (i + 4 < syllable
478 && ( (classTable->getCharClass(chars[i + 3]) & KhmerClassTable::CF_CLASS_MASK) == C_VOWEL_AA)
479 && ( (classTable->getCharClass(chars[i + 4]) & KhmerClassTable::CF_CLASS_MASK) == C_SIGN_NIKAHIT) ) )
480 {
481 output.writeChar(chars[i], i, tagBlwf);
482 break;
483 }
484
485 }
486 // default - any other characters
487 output.writeChar(chars[i], i, tagDefault);
488 break;
489 } // switch
490 } // for
491
492 prev = syllable; // move the pointer to the start of next syllable
493 }
494
495 return output.getOutputIndex();
496}
497
498
499U_NAMESPACE_END