]> git.saurik.com Git - apple/icu.git/blame - icuSources/layout/KhmerReordering.cpp
ICU-8.11.tar.gz
[apple/icu.git] / icuSources / layout / KhmerReordering.cpp
CommitLineData
73c04bcf
A
1/*
2 *
3 * (C) Copyright IBM Corp. 1998-2006 - All Rights Reserved
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:
129 le_int32 fOutIndex;
130 LEUnicode *fOutChars;
131
132 LEGlyphStorage &fGlyphStorage;
133
134
135public:
136 KhmerReorderingOutput(LEUnicode *outChars, LEGlyphStorage &glyphStorage)
137 : fOutIndex(0), fOutChars(outChars), fGlyphStorage(glyphStorage)
138 {
139 // nothing else to do...
140 }
141
142 ~KhmerReorderingOutput()
143 {
144 // nothing to do here...
145 }
146
147 void writeChar(LEUnicode ch, le_uint32 charIndex, FeatureMask charFeatures)
148 {
149 LEErrorCode success = LE_NO_ERROR;
150
151 fOutChars[fOutIndex] = ch;
152
153 fGlyphStorage.setCharIndex(fOutIndex, charIndex, success);
154 fGlyphStorage.setAuxData(fOutIndex, charFeatures, success);
155
156 fOutIndex += 1;
157 }
158
159 le_int32 getOutputIndex()
160 {
161 return fOutIndex;
162 }
163};
164
165
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
172
173#define blwmFeatureTag LE_BLWM_FEATURE_TAG
174#define abvmFeatureTag LE_ABVM_FEATURE_TAG
175#define distFeatureTag LE_DIST_FEATURE_TAG
176
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
181
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
195
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)
201
202
203
204// These are in the order in which the features need to be applied
205// for correct processing
206static const FeatureMap featureMap[] =
207{
208 // Shaping features
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},
218
219 // Positioning features
220 {distFeatureTag, distFeatureMask},
221 {blwmFeatureTag, blwmFeatureMask},
222 {abvmFeatureTag, abvmFeatureMask},
223 {mkmkFeatureTag, mkmkFeatureMask},
224};
225
226static const le_int32 featureMapCount = LE_ARRAY_SIZE(featureMap);
227
228// The stateTable is used to calculate the end (the length) of a well
229// formed Khmer Syllable.
230//
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.
234//
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).
239//
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.
246//
247// The table is quite complex, in order to limit the number of coeng consonants
248// to 2 (by means of the table).
249//
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).
254
255
256// xx independent character, such as a number, punctuation sign or non-khmer char
257//
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
261//
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)
264//
265// c3 Khmer consonant of type 3. Its subscript form takes space under
266// and to the right of the base.
267//
268// cs Khmer consonant shifter
269//
270// rb Khmer robat
271//
272// co coeng character (u17D2)
273//
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
277//
278// zwj Zero Width joiner
279//
280// zwnj Zero width non joiner
281//
282// sa above sign
283//
284// sp post sign
285//
286// there are lines with equal content but for an easier understanding
287// (and maybe change in the future) we did not join them
288//
289static const le_int8 khmerStateTable[][KhmerClassTable::CC_COUNT] =
290{
291
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
315
316};
317
318
319const FeatureMap *KhmerReordering::getFeatureMap(le_int32 &count)
320{
321 count = featureMapCount;
322
323 return featureMap;
324}
325
326
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.
330le_int32 KhmerReordering::findSyllable(const KhmerClassTable *classTable, const LEUnicode *chars, le_int32 prev, le_int32 charCount)
331{
332 le_int32 cursor = prev;
333 le_int8 state = 0;
334
335 while (cursor < charCount) {
336 KhmerClassTable::CharClass charClass = (classTable->getCharClass(chars[cursor]) & KhmerClassTable::CF_CLASS_MASK);
337
338 state = khmerStateTable[state][charClass];
339
340 if (state < 0) {
341 break;
342 }
343
344 cursor += 1;
345 }
346
347 return cursor;
348}
349
350
351// This is the real reordering function as applied to the Khmer language
352
353le_int32 KhmerReordering::reorder(const LEUnicode *chars, le_int32 charCount, le_int32 /*scriptCode*/,
354 LEUnicode *outChars, LEGlyphStorage &glyphStorage)
355{
356 const KhmerClassTable *classTable = KhmerClassTable::getKhmerClassTable();
357
358 KhmerReorderingOutput output(outChars, glyphStorage);
359 KhmerClassTable::CharClass charClass;
360 le_int32 i, prev = 0, coengRo;
361
362
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);
367
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]);
374
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
380 }
381
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
386 }
387
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
391 // with this flag
392 if ( (charClass & KhmerClassTable::CF_COENG) && (i + 1 < syllable) &&
393 ( (classTable->getCharClass(chars[i + 1]) & KhmerClassTable::CF_CLASS_MASK) == KhmerClassTable::CC_CONSONANT2) )
394 {
395 coengRo = i;
396 }
397 }
398
399 // write coeng + ro if found
400 if (coengRo > -1) {
401 output.writeChar(C_COENG, coengRo, tagPref);
402 output.writeChar(C_RO, coengRo + 1, tagPref);
403 }
404
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);
411 }
412
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]);
416
417 // skip a before vowel, it was already processed
418 if (charClass & KhmerClassTable::CF_POS_BEFORE) {
419 continue;
420 }
421
422 // skip coeng + ro, it was already processed
423 if (i == coengRo) {
424 i += 1;
425 continue;
426 }
427
428 switch (charClass & KhmerClassTable::CF_POS_MASK) {
429 case KhmerClassTable::CF_POS_ABOVE :
430 output.writeChar(chars[i], i, tagAbvf);
431 break;
432
433 case KhmerClassTable::CF_POS_AFTER :
434 output.writeChar(chars[i], i, tagPstf);
435 break;
436
437 case KhmerClassTable::CF_POS_BELOW :
438 output.writeChar(chars[i], i, tagBlwf);
439 break;
440
441 default:
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);
448 i += 1;
449 output.writeChar(chars[i], i, tagPstf);
450 }
451 else {
452 output.writeChar(chars[i], i, tagBlwf);
453 i += 1;
454 output.writeChar(chars[i], i, tagBlwf);
455 }
456 break;
457 }
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)
465 || (i + 2 < syllable
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))
469 || (i + 4 < syllable
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) ) )
472 {
473 output.writeChar(chars[i], i, tagBlwf);
474 break;
475 }
476
477 }
478 // default - any other characters
479 output.writeChar(chars[i], i, tagDefault);
480 break;
481 } // switch
482 } // for
483
484 prev = syllable; // move the pointer to the start of next syllable
485 }
486
487 return output.getOutputIndex();
488}
489
490
491U_NAMESPACE_END