3 * (C) Copyright IBM Corp. 1998-2006 - All Rights Reserved
5 * This file is a modification of the ICU file IndicReordering.cpp
6 * by Jens Herden and Javier Sola for Khmer language
11 #include "OpenTypeTables.h"
12 #include "KhmerReordering.h"
13 #include "LEGlyphStorage.h"
18 // Characters that get refered to by name...
23 C_DOTTED_CIRCLE
= 0x25CC,
26 C_SIGN_NIKAHIT
= 0x17C6,
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
,
53 _va
= _da
| KhmerClassTable::CF_SPLIT_VOWEL
,
54 _vr
= _dr
| KhmerClassTable::CF_SPLIT_VOWEL
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
76 static const KhmerClassTable::CharClass khmerCharClasses
[] =
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
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
95 static const KhmerClassTable khmerClassTable
= {0x1780, 0x17df, khmerCharClasses
};
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
102 KhmerClassTable::CharClass
KhmerClassTable::getCharClass(LEUnicode ch
) const
105 if (ch
== C_SIGN_ZWJ
) {
106 return CC_ZERO_WIDTH_J_MARK
;
109 if (ch
== C_SIGN_ZWNJ
) {
110 return CC_ZERO_WIDTH_NJ_MARK
;
113 if (ch
< firstChar
|| ch
> lastChar
) {
117 return classTable
[ch
- firstChar
];
120 const KhmerClassTable
*KhmerClassTable::getKhmerClassTable()
122 return &khmerClassTable
;
127 class KhmerReorderingOutput
: public UMemory
{
130 LEUnicode
*fOutChars
;
132 LEGlyphStorage
&fGlyphStorage
;
136 KhmerReorderingOutput(LEUnicode
*outChars
, LEGlyphStorage
&glyphStorage
)
137 : fOutIndex(0), fOutChars(outChars
), fGlyphStorage(glyphStorage
)
139 // nothing else to do...
142 ~KhmerReorderingOutput()
144 // nothing to do here...
147 void writeChar(LEUnicode ch
, le_uint32 charIndex
, FeatureMask charFeatures
)
149 LEErrorCode success
= LE_NO_ERROR
;
151 fOutChars
[fOutIndex
] = ch
;
153 fGlyphStorage
.setCharIndex(fOutIndex
, charIndex
, success
);
154 fGlyphStorage
.setAuxData(fOutIndex
, charFeatures
, success
);
159 le_int32
getOutputIndex()
166 #define blwfFeatureTag LE_BLWF_FEATURE_TAG
167 #define pstfFeatureTag LE_PSTF_FEATURE_TAG
168 #define presFeatureTag LE_PRES_FEATURE_TAG
169 #define blwsFeatureTag LE_BLWS_FEATURE_TAG
170 #define abvsFeatureTag LE_ABVS_FEATURE_TAG
171 #define pstsFeatureTag LE_PSTS_FEATURE_TAG
173 #define blwmFeatureTag LE_BLWM_FEATURE_TAG
174 #define abvmFeatureTag LE_ABVM_FEATURE_TAG
175 #define distFeatureTag LE_DIST_FEATURE_TAG
177 #define prefFeatureTag LE_PREF_FEATURE_TAG
178 #define abvfFeatureTag LE_ABVF_FEATURE_TAG
179 #define cligFeatureTag LE_CLIG_FEATURE_TAG
180 #define mkmkFeatureTag LE_MKMK_FEATURE_TAG
182 #define prefFeatureMask 0x80000000UL
183 #define blwfFeatureMask 0x40000000UL
184 #define abvfFeatureMask 0x20000000UL
185 #define pstfFeatureMask 0x10000000UL
186 #define presFeatureMask 0x08000000UL
187 #define blwsFeatureMask 0x04000000UL
188 #define abvsFeatureMask 0x02000000UL
189 #define pstsFeatureMask 0x01000000UL
190 #define cligFeatureMask 0x00800000UL
191 #define distFeatureMask 0x00400000UL
192 #define blwmFeatureMask 0x00200000UL
193 #define abvmFeatureMask 0x00100000UL
194 #define mkmkFeatureMask 0x00080000UL
196 #define tagPref (prefFeatureMask | presFeatureMask | cligFeatureMask | distFeatureMask)
197 #define tagAbvf (abvfFeatureMask | abvsFeatureMask | cligFeatureMask | distFeatureMask | abvmFeatureMask | mkmkFeatureMask)
198 #define tagPstf (blwfFeatureMask | blwsFeatureMask | prefFeatureMask | presFeatureMask | pstfFeatureMask | pstsFeatureMask | cligFeatureMask | distFeatureMask | blwmFeatureMask)
199 #define tagBlwf (blwfFeatureMask | blwsFeatureMask | cligFeatureMask | distFeatureMask | blwmFeatureMask | mkmkFeatureMask)
200 #define tagDefault (prefFeatureMask | blwfFeatureMask | presFeatureMask | blwsFeatureMask | cligFeatureMask | distFeatureMask | abvmFeatureMask | blwmFeatureMask | mkmkFeatureMask)
204 // These are in the order in which the features need to be applied
205 // for correct processing
206 static const FeatureMap featureMap
[] =
209 {prefFeatureTag
, prefFeatureMask
},
210 {blwfFeatureTag
, blwfFeatureMask
},
211 {abvfFeatureTag
, abvfFeatureMask
},
212 {pstfFeatureTag
, pstfFeatureMask
},
213 {presFeatureTag
, presFeatureMask
},
214 {blwsFeatureTag
, blwsFeatureMask
},
215 {abvsFeatureTag
, abvsFeatureMask
},
216 {pstsFeatureTag
, pstsFeatureMask
},
217 {cligFeatureTag
, cligFeatureMask
},
219 // Positioning features
220 {distFeatureTag
, distFeatureMask
},
221 {blwmFeatureTag
, blwmFeatureMask
},
222 {abvmFeatureTag
, abvmFeatureMask
},
223 {mkmkFeatureTag
, mkmkFeatureMask
},
226 static const le_int32 featureMapCount
= LE_ARRAY_SIZE(featureMap
);
228 // The stateTable is used to calculate the end (the length) of a well
229 // formed Khmer Syllable.
231 // Each horizontal line is ordered exactly the same way as the values in KhmerClassTable
232 // CharClassValues in KhmerReordering.h This coincidence of values allows the
233 // follow up of the table.
235 // Each line corresponds to a state, which does not necessarily need to be a type
236 // of component... for example, state 2 is a base, with is always a first character
237 // in the syllable, but the state could be produced a consonant of any type when
238 // it is the first character that is analysed (in ground state).
240 // Differentiating 3 types of consonants is necessary in order to
241 // forbid the use of certain combinations, such as having a second
242 // coeng after a coeng RO,
243 // The inexistent possibility of having a type 3 after another type 3 is permitted,
244 // eliminating it would very much complicate the table, and it does not create typing
245 // problems, as the case above.
247 // The table is quite complex, in order to limit the number of coeng consonants
248 // to 2 (by means of the table).
250 // There a peculiarity, as far as Unicode is concerned:
251 // - The consonant-shifter is considered in two possible different
252 // locations, the one considered in Unicode 3.0 and the one considered in
253 // Unicode 4.0. (there is a backwards compatibility problem in this standard).
256 // xx independent character, such as a number, punctuation sign or non-khmer char
258 // c1 Khmer consonant of type 1 or an independent vowel
259 // that is, a letter in which the subscript for is only under the
260 // base, not taking any space to the right or to the left
262 // c2 Khmer consonant of type 2, the coeng form takes space under
263 // and to the left of the base (only RO is of this type)
265 // c3 Khmer consonant of type 3. Its subscript form takes space under
266 // and to the right of the base.
268 // cs Khmer consonant shifter
272 // co coeng character (u17D2)
274 // dv dependent vowel (including split vowels, they are treated in the same way).
275 // even if dv is not defined above, the component that is really tested for is
276 // KhmerClassTable::CC_DEPENDENT_VOWEL, which is common to all dependent vowels
278 // zwj Zero Width joiner
280 // zwnj Zero width non joiner
286 // there are lines with equal content but for an easier understanding
287 // (and maybe change in the future) we did not join them
289 static const le_int8 khmerStateTable
[][KhmerClassTable::CC_COUNT
] =
292 // xx c1 c2 c3 zwnj cs rb co dv sa sp zwj
293 { 1, 2, 2, 2, 1, 1, 1, 6, 1, 1, 1, 2}, // 0 - ground state
294 {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, // 1 - exit state (or sign to the right of the syllable)
295 {-1, -1, -1, -1, 3, 4, 5, 6, 16, 17, 1, -1}, // 2 - Base consonant
296 {-1, -1, -1, -1, -1, 4, -1, -1, 16, -1, -1, -1}, // 3 - First ZWNJ before a register shifter
297 // It can only be followed by a shifter or a vowel
298 {-1, -1, -1, -1, 15, -1, -1, 6, 16, 17, 1, 14}, // 4 - First register shifter
299 {-1, -1, -1, -1, -1, -1, -1, -1, 20, -1, 1, -1}, // 5 - Robat
300 {-1, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, -1}, // 6 - First Coeng
301 {-1, -1, -1, -1, 12, 13, -1, 10, 16, 17, 1, 14}, // 7 - First consonant of type 1 after coeng
302 {-1, -1, -1, -1, 12, 13, -1, -1, 16, 17, 1, 14}, // 8 - First consonant of type 2 after coeng
303 {-1, -1, -1, -1, 12, 13, -1, 10, 16, 17, 1, 14}, // 9 - First consonant or type 3 after ceong
304 {-1, 11, 11, 11, -1, -1, -1, -1, -1, -1, -1, -1}, // 10 - Second Coeng (no register shifter before)
305 {-1, -1, -1, -1, 15, -1, -1, -1, 16, 17, 1, 14}, // 11 - Second coeng consonant (or ind. vowel) no register shifter before
306 {-1, -1, -1, -1, -1, 13, -1, -1, 16, -1, -1, -1}, // 12 - Second ZWNJ before a register shifter
307 {-1, -1, -1, -1, 15, -1, -1, -1, 16, 17, 1, 14}, // 13 - Second register shifter
308 {-1, -1, -1, -1, -1, -1, -1, -1, 16, -1, -1, -1}, // 14 - ZWJ before vowel
309 {-1, -1, -1, -1, -1, -1, -1, -1, 16, -1, -1, -1}, // 15 - ZWNJ before vowel
310 {-1, -1, -1, -1, -1, -1, -1, -1, -1, 17, 1, 18}, // 16 - dependent vowel
311 {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, 18}, // 17 - sign above
312 {-1, -1, -1, -1, -1, -1, -1, 19, -1, -1, -1, -1}, // 18 - ZWJ after vowel
313 {-1, 1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1}, // 19 - Third coeng
314 {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1}, // 20 - dependent vowel after a Robat
319 const FeatureMap
*KhmerReordering::getFeatureMap(le_int32
&count
)
321 count
= featureMapCount
;
327 // Given an input string of characters and a location in which to start looking
328 // calculate, using the state table, which one is the last character of the syllable
329 // that starts in the starting position.
330 le_int32
KhmerReordering::findSyllable(const KhmerClassTable
*classTable
, const LEUnicode
*chars
, le_int32 prev
, le_int32 charCount
)
332 le_int32 cursor
= prev
;
335 while (cursor
< charCount
) {
336 KhmerClassTable::CharClass charClass
= (classTable
->getCharClass(chars
[cursor
]) & KhmerClassTable::CF_CLASS_MASK
);
338 state
= khmerStateTable
[state
][charClass
];
351 // This is the real reordering function as applied to the Khmer language
353 le_int32
KhmerReordering::reorder(const LEUnicode
*chars
, le_int32 charCount
, le_int32
/*scriptCode*/,
354 LEUnicode
*outChars
, LEGlyphStorage
&glyphStorage
)
356 const KhmerClassTable
*classTable
= KhmerClassTable::getKhmerClassTable();
358 KhmerReorderingOutput
output(outChars
, glyphStorage
);
359 KhmerClassTable::CharClass charClass
;
360 le_int32 i
, prev
= 0, coengRo
;
363 // This loop only exits when we reach the end of a run, which may contain
364 // several syllables.
365 while (prev
< charCount
) {
366 le_int32 syllable
= findSyllable(classTable
, chars
, prev
, charCount
);
368 // write a pre vowel or the pre part of a split vowel first
369 // and look out for coeng + ro. RO is the only vowel of type 2, and
370 // therefore the only one that requires saving space before the base.
371 coengRo
= -1; // There is no Coeng Ro, if found this value will change
372 for (i
= prev
; i
< syllable
; i
+= 1) {
373 charClass
= classTable
->getCharClass(chars
[i
]);
375 // if a split vowel, write the pre part. In Khmer the pre part
376 // is the same for all split vowels, same glyph as pre vowel C_VOWEL_E
377 if (charClass
& KhmerClassTable::CF_SPLIT_VOWEL
) {
378 output
.writeChar(C_VOWEL_E
, i
, tagPref
);
379 break; // there can be only one vowel
382 // if a vowel with pos before write it out
383 if (charClass
& KhmerClassTable::CF_POS_BEFORE
) {
384 output
.writeChar(chars
[i
], i
, tagPref
);
385 break; // there can be only one vowel
388 // look for coeng + ro and remember position
389 // works because coeng + ro is always in front of a vowel (if there is a vowel)
390 // and because CC_CONSONANT2 is enough to identify it, as it is the only consonant
392 if ( (charClass
& KhmerClassTable::CF_COENG
) && (i
+ 1 < syllable
) &&
393 ( (classTable
->getCharClass(chars
[i
+ 1]) & KhmerClassTable::CF_CLASS_MASK
) == KhmerClassTable::CC_CONSONANT2
) )
399 // write coeng + ro if found
401 output
.writeChar(C_COENG
, coengRo
, tagPref
);
402 output
.writeChar(C_RO
, coengRo
+ 1, tagPref
);
405 // shall we add a dotted circle?
406 // If in the position in which the base should be (first char in the string) there is
407 // a character that has the Dotted circle flag (a character that cannot be a base)
408 // then write a dotted circle
409 if (classTable
->getCharClass(chars
[prev
]) & KhmerClassTable::CF_DOTTED_CIRCLE
) {
410 output
.writeChar(C_DOTTED_CIRCLE
, prev
, tagDefault
);
413 // copy what is left to the output, skipping before vowels and coeng Ro if they are present
414 for (i
= prev
; i
< syllable
; i
+= 1) {
415 charClass
= classTable
->getCharClass(chars
[i
]);
417 // skip a before vowel, it was already processed
418 if (charClass
& KhmerClassTable::CF_POS_BEFORE
) {
422 // skip coeng + ro, it was already processed
428 switch (charClass
& KhmerClassTable::CF_POS_MASK
) {
429 case KhmerClassTable::CF_POS_ABOVE
:
430 output
.writeChar(chars
[i
], i
, tagAbvf
);
433 case KhmerClassTable::CF_POS_AFTER
:
434 output
.writeChar(chars
[i
], i
, tagPstf
);
437 case KhmerClassTable::CF_POS_BELOW
:
438 output
.writeChar(chars
[i
], i
, tagBlwf
);
442 // assign the correct flags to a coeng consonant
443 // Consonants of type 3 are taged as Post forms and those type 1 as below forms
444 if ( (charClass
& KhmerClassTable::CF_COENG
) && i
+ 1 < syllable
) {
445 if ( (classTable
->getCharClass(chars
[i
+ 1]) & KhmerClassTable::CF_CLASS_MASK
)
446 == KhmerClassTable::CC_CONSONANT3
) {
447 output
.writeChar(chars
[i
], i
, tagPstf
);
449 output
.writeChar(chars
[i
], i
, tagPstf
);
452 output
.writeChar(chars
[i
], i
, tagBlwf
);
454 output
.writeChar(chars
[i
], i
, tagBlwf
);
458 // if a shifter is followed by an above vowel change the shifter to below form,
459 // an above vowel can have two possible positions i + 1 or i + 3
460 // (position i+1 corresponds to unicode 3, position i+3 to Unicode 4)
461 // and there is an extra rule for C_VOWEL_AA + C_SIGN_NIKAHIT also for two
462 // different positions, right after the shifter or after a vowel (Unicode 4)
463 if ( (charClass
& KhmerClassTable::CF_SHIFTER
) && (i
+ 1 < syllable
) ) {
464 if ((classTable
->getCharClass(chars
[i
+ 1]) & KhmerClassTable::CF_ABOVE_VOWEL
)
466 && ( (classTable
->getCharClass(chars
[i
+ 1]) & KhmerClassTable::CF_CLASS_MASK
) == C_VOWEL_AA
)
467 && ( (classTable
->getCharClass(chars
[i
+ 2]) & KhmerClassTable::CF_CLASS_MASK
) == C_SIGN_NIKAHIT
))
468 || (i
+ 3 < syllable
&& (classTable
->getCharClass(chars
[i
+ 3]) & KhmerClassTable::CF_ABOVE_VOWEL
))
470 && ( (classTable
->getCharClass(chars
[i
+ 3]) & KhmerClassTable::CF_CLASS_MASK
) == C_VOWEL_AA
)
471 && ( (classTable
->getCharClass(chars
[i
+ 4]) & KhmerClassTable::CF_CLASS_MASK
) == C_SIGN_NIKAHIT
) ) )
473 output
.writeChar(chars
[i
], i
, tagBlwf
);
478 // default - any other characters
479 output
.writeChar(chars
[i
], i
, tagDefault
);
484 prev
= syllable
; // move the pointer to the start of next syllable
487 return output
.getOutputIndex();