added license comments
[framework/uifw/harfbuzz.git] / harfbuzz-0.1 / src / harfbuzz-khmer.c
1 /*
2  * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies)
3  *
4  * This is part of HarfBuzz, an OpenType Layout engine library.
5  *
6  * Permission is hereby granted, without written agreement and without
7  * license or royalty fees, to use, copy, modify, and distribute this
8  * software and its documentation for any purpose, provided that the
9  * above copyright notice and the following two paragraphs appear in
10  * all copies of this software.
11  *
12  * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15  * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16  * DAMAGE.
17  *
18  * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20  * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23  */
24
25 #include "harfbuzz-shaper.h"
26 #include "harfbuzz-shaper-private.h"
27
28 #include <assert.h>
29 #include <stdio.h>
30
31 /*
32 //  Vocabulary
33 //      Base ->         A consonant or an independent vowel in its full (not subscript) form. It is the
34 //                      center of the syllable, it can be surrounded by coeng (subscript) consonants, vowels,
35 //                      split vowels, signs... but there is only one base in a syllable, it has to be coded as
36 //                      the first character of the syllable.
37 //      split vowel --> vowel that has two parts placed separately (e.g. Before and after the consonant).
38 //                      Khmer language has five of them. Khmer split vowels either have one part before the
39 //                      base and one after the base or they have a part before the base and a part above the base.
40 //                      The first part of all Khmer split vowels is the same character, identical to
41 //                      the glyph of Khmer dependent vowel SRA EI
42 //      coeng -->  modifier used in Khmer to construct coeng (subscript) consonants
43 //                 Differently than indian languages, the coeng modifies the consonant that follows it,
44 //                 not the one preceding it  Each consonant has two forms, the base form and the subscript form
45 //                 the base form is the normal one (using the consonants code-point), the subscript form is
46 //                 displayed when the combination coeng + consonant is encountered.
47 //      Consonant of type 1 -> A consonant which has subscript for that only occupies space under a base consonant
48 //      Consonant of type 2.-> Its subscript form occupies space under and before the base (only one, RO)
49 //      Consonant of Type 3 -> Its subscript form occupies space under and after the base (KHO, CHHO, THHO, BA, YO, SA)
50 //      Consonant shifter -> Khmer has to series of consonants. The same dependent vowel has different sounds
51 //                           if it is attached to a consonant of the first series or a consonant of the second series
52 //                           Most consonants have an equivalent in the other series, but some of theme exist only in
53 //                           one series (for example SA). If we want to use the consonant SA with a vowel sound that
54 //                           can only be done with a vowel sound that corresponds to a vowel accompanying a consonant
55 //                           of the other series, then we need to use a consonant shifter: TRIISAP or MUSIKATOAN
56 //                           x17C9 y x17CA. TRIISAP changes a first series consonant to second series sound and
57 //                           MUSIKATOAN a second series consonant to have a first series vowel sound.
58 //                           Consonant shifter are both normally supercript marks, but, when they are followed by a
59 //                           superscript, they change shape and take the form of subscript dependent vowel SRA U.
60 //                           If they are in the same syllable as a coeng consonant, Unicode 3.0 says that they
61 //                           should be typed before the coeng. Unicode 4.0 breaks the standard and says that it should
62 //                           be placed after the coeng consonant.
63 //      Dependent vowel ->   In khmer dependent vowels can be placed above, below, before or after the base
64 //                           Each vowel has its own position. Only one vowel per syllable is allowed.
65 //      Signs            ->  Khmer has above signs and post signs. Only one above sign and/or one post sign are
66 //                           Allowed in a syllable.
67 //
68 //
69 //   order is important here! This order must be the same that is found in each horizontal
70 //   line in the statetable for Khmer (see khmerStateTable) .
71 */
72 enum KhmerCharClassValues {
73     CC_RESERVED             =  0,
74     CC_CONSONANT            =  1, /* Consonant of type 1 or independent vowel */
75     CC_CONSONANT2           =  2, /* Consonant of type 2 */
76     CC_CONSONANT3           =  3, /* Consonant of type 3 */
77     CC_ZERO_WIDTH_NJ_MARK   =  4, /* Zero Width non joiner character (0x200C) */
78     CC_CONSONANT_SHIFTER    =  5,
79     CC_ROBAT                =  6, /* Khmer special diacritic accent -treated differently in state table */
80     CC_COENG                =  7, /* Subscript consonant combining character */
81     CC_DEPENDENT_VOWEL      =  8,
82     CC_SIGN_ABOVE           =  9,
83     CC_SIGN_AFTER           = 10,
84     CC_ZERO_WIDTH_J_MARK    = 11, /* Zero width joiner character */
85     CC_COUNT                = 12  /* This is the number of character classes */
86 };
87
88
89 enum KhmerCharClassFlags {
90     CF_CLASS_MASK    = 0x0000FFFF,
91
92     CF_CONSONANT     = 0x01000000,  /* flag to speed up comparing */
93     CF_SPLIT_VOWEL   = 0x02000000,  /* flag for a split vowel -> the first part is added in front of the syllable */
94     CF_DOTTED_CIRCLE = 0x04000000,  /* add a dotted circle if a character with this flag is the first in a syllable */
95     CF_COENG         = 0x08000000,  /* flag to speed up comparing */
96     CF_SHIFTER       = 0x10000000,  /* flag to speed up comparing */
97     CF_ABOVE_VOWEL   = 0x20000000,  /* flag to speed up comparing */
98
99     /* position flags */
100     CF_POS_BEFORE    = 0x00080000,
101     CF_POS_BELOW     = 0x00040000,
102     CF_POS_ABOVE     = 0x00020000,
103     CF_POS_AFTER     = 0x00010000,
104     CF_POS_MASK      = 0x000f0000
105 };
106
107
108 /* Characters that get referred to by name */
109 enum KhmerChar {
110     C_SIGN_ZWNJ     = 0x200C,
111     C_SIGN_ZWJ      = 0x200D,
112     C_RO            = 0x179A,
113     C_VOWEL_AA      = 0x17B6,
114     C_SIGN_NIKAHIT  = 0x17C6,
115     C_VOWEL_E       = 0x17C1,
116     C_COENG         = 0x17D2
117 };
118
119
120 /*
121 //  simple classes, they are used in the statetable (in this file) to control the length of a syllable
122 //  they are also used to know where a character should be placed (location in reference to the base character)
123 //  and also to know if a character, when independently displayed, should be displayed with a dotted-circle to
124 //  indicate error in syllable construction
125 */
126 enum {
127     _xx = CC_RESERVED,
128     _sa = CC_SIGN_ABOVE | CF_DOTTED_CIRCLE | CF_POS_ABOVE,
129     _sp = CC_SIGN_AFTER | CF_DOTTED_CIRCLE| CF_POS_AFTER,
130     _c1 = CC_CONSONANT | CF_CONSONANT,
131     _c2 = CC_CONSONANT2 | CF_CONSONANT,
132     _c3 = CC_CONSONANT3 | CF_CONSONANT,
133     _rb = CC_ROBAT | CF_POS_ABOVE | CF_DOTTED_CIRCLE,
134     _cs = CC_CONSONANT_SHIFTER | CF_DOTTED_CIRCLE | CF_SHIFTER,
135     _dl = CC_DEPENDENT_VOWEL | CF_POS_BEFORE | CF_DOTTED_CIRCLE,
136     _db = CC_DEPENDENT_VOWEL | CF_POS_BELOW | CF_DOTTED_CIRCLE,
137     _da = CC_DEPENDENT_VOWEL | CF_POS_ABOVE | CF_DOTTED_CIRCLE | CF_ABOVE_VOWEL,
138     _dr = CC_DEPENDENT_VOWEL | CF_POS_AFTER | CF_DOTTED_CIRCLE,
139     _co = CC_COENG | CF_COENG | CF_DOTTED_CIRCLE,
140
141     /* split vowel */
142     _va = _da | CF_SPLIT_VOWEL,
143     _vr = _dr | CF_SPLIT_VOWEL
144 };
145
146
147 /*
148 //   Character class: a character class value
149 //   ORed with character class flags.
150 */
151 typedef unsigned long KhmerCharClass;
152
153
154 /*
155 //  Character class tables
156 //  _xx character does not combine into syllable, such as numbers, puntuation marks, non-Khmer signs...
157 //  _sa Sign placed above the base
158 //  _sp Sign placed after the base
159 //  _c1 Consonant of type 1 or independent vowel (independent vowels behave as type 1 consonants)
160 //  _c2 Consonant of type 2 (only RO)
161 //  _c3 Consonant of type 3
162 //  _rb Khmer sign robat u17CC. combining mark for subscript consonants
163 //  _cd Consonant-shifter
164 //  _dl Dependent vowel placed before the base (left of the base)
165 //  _db Dependent vowel placed below the base
166 //  _da Dependent vowel placed above the base
167 //  _dr Dependent vowel placed behind the base (right of the base)
168 //  _co Khmer combining mark COENG u17D2, combines with the consonant or independent vowel following
169 //      it to create a subscript consonant or independent vowel
170 //  _va Khmer split vowel in which the first part is before the base and the second one above the base
171 //  _vr Khmer split vowel in which the first part is before the base and the second one behind (right of) the base
172 */
173 static const KhmerCharClass khmerCharClasses[] = {
174     _c1, _c1, _c1, _c3, _c1, _c1, _c1, _c1, _c3, _c1, _c1, _c1, _c1, _c3, _c1, _c1, /* 1780 - 178F */
175     _c1, _c1, _c1, _c1, _c3, _c1, _c1, _c1, _c1, _c3, _c2, _c1, _c1, _c1, _c3, _c3, /* 1790 - 179F */
176     _c1, _c3, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, _c1, /* 17A0 - 17AF */
177     _c1, _c1, _c1, _c1, _dr, _dr, _dr, _da, _da, _da, _da, _db, _db, _db, _va, _vr, /* 17B0 - 17BF */
178     _vr, _dl, _dl, _dl, _vr, _vr, _sa, _sp, _sp, _cs, _cs, _sa, _rb, _sa, _sa, _sa, /* 17C0 - 17CF */
179     _sa, _sa, _co, _sa, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _xx, _sa, _xx, _xx  /* 17D0 - 17DF */
180 };
181
182 /* this enum must reflect the range of khmerCharClasses */
183 enum KhmerCharClassesRange {
184     KhmerFirstChar = 0x1780,
185     KhmerLastChar  = 0x17df
186 };
187
188 /*
189 //  Below we define how a character in the input string is either in the khmerCharClasses table
190 //  (in which case we get its type back), a ZWJ or ZWNJ (two characters that may appear
191 //  within the syllable, but are not in the table) we also get their type back, or an unknown object
192 //  in which case we get _xx (CC_RESERVED) back
193 */
194 static KhmerCharClass getKhmerCharClass(HB_UChar16 uc)
195 {
196     if (uc == C_SIGN_ZWJ) {
197         return CC_ZERO_WIDTH_J_MARK;
198     }
199
200     if (uc == C_SIGN_ZWNJ) {
201         return CC_ZERO_WIDTH_NJ_MARK;
202     }
203
204     if (uc < KhmerFirstChar || uc > KhmerLastChar) {
205         return CC_RESERVED;
206     }
207
208     return khmerCharClasses[uc - KhmerFirstChar];
209 }
210
211
212 /*
213 //  The stateTable is used to calculate the end (the length) of a well
214 //  formed Khmer Syllable.
215 //
216 //  Each horizontal line is ordered exactly the same way as the values in KhmerClassTable
217 //  CharClassValues. This coincidence of values allows the follow up of the table.
218 //
219 //  Each line corresponds to a state, which does not necessarily need to be a type
220 //  of component... for example, state 2 is a base, with is always a first character
221 //  in the syllable, but the state could be produced a consonant of any type when
222 //  it is the first character that is analysed (in ground state).
223 //
224 //  Differentiating 3 types of consonants is necessary in order to
225 //  forbid the use of certain combinations, such as having a second
226 //  coeng after a coeng RO,
227 //  The inexistent possibility of having a type 3 after another type 3 is permitted,
228 //  eliminating it would very much complicate the table, and it does not create typing
229 //  problems, as the case above.
230 //
231 //  The table is quite complex, in order to limit the number of coeng consonants
232 //  to 2 (by means of the table).
233 //
234 //  There a peculiarity, as far as Unicode is concerned:
235 //  - The consonant-shifter is considered in two possible different
236 //    locations, the one considered in Unicode 3.0 and the one considered in
237 //    Unicode 4.0. (there is a backwards compatibility problem in this standard).
238 //
239 //
240 //  xx    independent character, such as a number, punctuation sign or non-khmer char
241 //
242 //  c1    Khmer consonant of type 1 or an independent vowel
243 //        that is, a letter in which the subscript for is only under the
244 //        base, not taking any space to the right or to the left
245 //
246 //  c2    Khmer consonant of type 2, the coeng form takes space under
247 //        and to the left of the base (only RO is of this type)
248 //
249 //  c3    Khmer consonant of type 3. Its subscript form takes space under
250 //        and to the right of the base.
251 //
252 //  cs    Khmer consonant shifter
253 //
254 //  rb    Khmer robat
255 //
256 //  co    coeng character (u17D2)
257 //
258 //  dv    dependent vowel (including split vowels, they are treated in the same way).
259 //        even if dv is not defined above, the component that is really tested for is
260 //        KhmerClassTable::CC_DEPENDENT_VOWEL, which is common to all dependent vowels
261 //
262 //  zwj   Zero Width joiner
263 //
264 //  zwnj  Zero width non joiner
265 //
266 //  sa    above sign
267 //
268 //  sp    post sign
269 //
270 //  there are lines with equal content but for an easier understanding
271 //  (and maybe change in the future) we did not join them
272 */
273 static const signed char khmerStateTable[][CC_COUNT] =
274 {
275     /* xx  c1  c2  c3 zwnj cs  rb  co  dv  sa  sp zwj */
276     { 1,  2,  2,  2,  1,  1,  1,  6,  1,  1,  1,  2}, /*  0 - ground state */
277     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, /*  1 - exit state (or sign to the right of the syllable) */
278     {-1, -1, -1, -1,  3,  4,  5,  6, 16, 17,  1, -1}, /*  2 - Base consonant */
279     {-1, -1, -1, -1, -1,  4, -1, -1, 16, -1, -1, -1}, /*  3 - First ZWNJ before a register shifter It can only be followed by a shifter or a vowel */
280     {-1, -1, -1, -1, 15, -1, -1,  6, 16, 17,  1, 14}, /*  4 - First register shifter */
281     {-1, -1, -1, -1, -1, -1, -1, -1, 20, -1,  1, -1}, /*  5 - Robat */
282     {-1,  7,  8,  9, -1, -1, -1, -1, -1, -1, -1, -1}, /*  6 - First Coeng */
283     {-1, -1, -1, -1, 12, 13, -1, 10, 16, 17,  1, 14}, /*  7 - First consonant of type 1 after coeng */
284     {-1, -1, -1, -1, 12, 13, -1, -1, 16, 17,  1, 14}, /*  8 - First consonant of type 2 after coeng */
285     {-1, -1, -1, -1, 12, 13, -1, 10, 16, 17,  1, 14}, /*  9 - First consonant or type 3 after ceong */
286     {-1, 11, 11, 11, -1, -1, -1, -1, -1, -1, -1, -1}, /* 10 - Second Coeng (no register shifter before) */
287     {-1, -1, -1, -1, 15, -1, -1, -1, 16, 17,  1, 14}, /* 11 - Second coeng consonant (or ind. vowel) no register shifter before */
288     {-1, -1, -1, -1, -1, 13, -1, -1, 16, -1, -1, -1}, /* 12 - Second ZWNJ before a register shifter */
289     {-1, -1, -1, -1, 15, -1, -1, -1, 16, 17,  1, 14}, /* 13 - Second register shifter */
290     {-1, -1, -1, -1, -1, -1, -1, -1, 16, -1, -1, -1}, /* 14 - ZWJ before vowel */
291     {-1, -1, -1, -1, -1, -1, -1, -1, 16, -1, -1, -1}, /* 15 - ZWNJ before vowel */
292     {-1, -1, -1, -1, -1, -1, -1, -1, -1, 17,  1, 18}, /* 16 - dependent vowel */
293     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  1, 18}, /* 17 - sign above */
294     {-1, -1, -1, -1, -1, -1, -1, 19, -1, -1, -1, -1}, /* 18 - ZWJ after vowel */
295     {-1,  1, -1,  1, -1, -1, -1, -1, -1, -1, -1, -1}, /* 19 - Third coeng */
296     {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  1, -1}, /* 20 - dependent vowel after a Robat */
297 };
298
299
300 /*  #define KHMER_DEBUG */
301 #ifdef KHMER_DEBUG
302 #define KHDEBUG qDebug
303 #else
304 #define KHDEBUG if(0) printf
305 #endif
306
307 /*
308 //  Given an input string of characters and a location in which to start looking
309 //  calculate, using the state table, which one is the last character of the syllable
310 //  that starts in the starting position.
311 */
312 /* SAMSUNG - Kaja Changes -Start */
313 //static int khmer_nextSyllableBoundary(const HB_UChar16 *s, int start, int end, HB_Bool *invalid)
314 int khmer_nextSyllableBoundary(const HB_UChar16 *s, int start, int end, HB_Bool *invalid)
315 /* SAMSUNG - Kaja Changes -End */
316 {
317     const HB_UChar16 *uc = s + start;
318     int state = 0;
319     int pos = start;
320     *invalid = FALSE;
321
322     while (pos < end) {
323         KhmerCharClass charClass = getKhmerCharClass(*uc);
324         if (pos == start) {
325             *invalid = (charClass > 0) && ! (charClass & CF_CONSONANT);
326         }
327         state = khmerStateTable[state][charClass & CF_CLASS_MASK];
328
329         KHDEBUG("state[%d]=%d class=%8lx (uc=%4x)", pos - start, state,
330                 charClass, *uc );
331
332         if (state < 0) {
333             break;
334         }
335         ++uc;
336         ++pos;
337     }
338     return pos;
339 }
340
341 #ifndef NO_OPENTYPE
342 static const HB_OpenTypeFeature khmer_features[] = {
343     { HB_MAKE_TAG( 'p', 'r', 'e', 'f' ), PreFormProperty },
344     { HB_MAKE_TAG( 'b', 'l', 'w', 'f' ), BelowFormProperty },
345     { HB_MAKE_TAG( 'a', 'b', 'v', 'f' ), AboveFormProperty },
346     { HB_MAKE_TAG( 'p', 's', 't', 'f' ), PostFormProperty },
347     { HB_MAKE_TAG( 'p', 'r', 'e', 's' ), PreSubstProperty },
348     { HB_MAKE_TAG( 'b', 'l', 'w', 's' ), BelowSubstProperty },
349     { HB_MAKE_TAG( 'a', 'b', 'v', 's' ), AboveSubstProperty },
350     { HB_MAKE_TAG( 'p', 's', 't', 's' ), PostSubstProperty },
351     { HB_MAKE_TAG( 'c', 'l', 'i', 'g' ), CligProperty },
352     { 0, 0 }
353 };
354 #endif
355
356
357 static HB_Bool khmer_shape_syllable(HB_Bool openType, HB_ShaperItem *item)
358 {
359 /*    KHDEBUG("syllable from %d len %d, str='%s'", item->from, item->length,
360             item->string->mid(item->from, item->length).toUtf8().data()); */
361
362     int len = 0;
363     int syllableEnd = item->item.pos + item->item.length;
364     unsigned short reordered[16];
365     unsigned char properties[16];
366     enum {
367         AboveForm = 0x01,
368         PreForm = 0x02,
369         PostForm = 0x04,
370         BelowForm = 0x08
371     };
372 #ifndef NO_OPENTYPE
373     const int availableGlyphs = item->num_glyphs;
374 #endif
375     int coengRo;
376     int i;
377
378     /* according to the specs this is the max length one can get
379        ### the real value should be smaller */
380     assert(item->item.length < 13);
381
382     memset(properties, 0, 16*sizeof(unsigned char));
383
384 #ifdef KHMER_DEBUG
385     qDebug("original:");
386     for (int i = from; i < syllableEnd; i++) {
387         qDebug("    %d: %4x", i, string[i]);
388     }
389 #endif
390
391     /*
392     // write a pre vowel or the pre part of a split vowel first
393     // and look out for coeng + ro. RO is the only vowel of type 2, and
394     // therefore the only one that requires saving space before the base.
395     */
396     coengRo = -1;  /* There is no Coeng Ro, if found this value will change */
397     for (i = item->item.pos; i < syllableEnd; i += 1) {
398         KhmerCharClass charClass = getKhmerCharClass(item->string[i]);
399
400         /* if a split vowel, write the pre part. In Khmer the pre part
401            is the same for all split vowels, same glyph as pre vowel C_VOWEL_E */
402         if (charClass & CF_SPLIT_VOWEL) {
403             reordered[len] = C_VOWEL_E;
404             properties[len] = PreForm;
405             ++len;
406             break; /* there can be only one vowel */
407         }
408         /* if a vowel with pos before write it out */
409         if (charClass & CF_POS_BEFORE) {
410             reordered[len] = item->string[i];
411             properties[len] = PreForm;
412             ++len;
413             break; /* there can be only one vowel */
414         }
415         /* look for coeng + ro and remember position
416            works because coeng + ro is always in front of a vowel (if there is a vowel)
417            and because CC_CONSONANT2 is enough to identify it, as it is the only consonant
418            with this flag */
419         if ( (charClass & CF_COENG) && (i + 1 < syllableEnd) &&
420               ( (getKhmerCharClass(item->string[i+1]) & CF_CLASS_MASK) == CC_CONSONANT2) ) {
421             coengRo = i;
422         }
423     }
424
425     /* write coeng + ro if found */
426     if (coengRo > -1) {
427         reordered[len] = C_COENG;
428         properties[len] = PreForm;
429         ++len;
430         reordered[len] = C_RO;
431         properties[len] = PreForm;
432         ++len;
433     }
434
435     /*
436        shall we add a dotted circle?
437        If in the position in which the base should be (first char in the string) there is
438        a character that has the Dotted circle flag (a character that cannot be a base)
439        then write a dotted circle */
440     if (getKhmerCharClass(item->string[item->item.pos]) & CF_DOTTED_CIRCLE) {
441         reordered[len] = C_DOTTED_CIRCLE;
442         ++len;
443     }
444
445     /* copy what is left to the output, skipping before vowels and
446        coeng Ro if they are present */
447     for (i = item->item.pos; i < syllableEnd; i += 1) {
448         HB_UChar16 uc = item->string[i];
449         KhmerCharClass charClass = getKhmerCharClass(uc);
450
451         /* skip a before vowel, it was already processed */
452         if (charClass & CF_POS_BEFORE) {
453             continue;
454         }
455
456         /* skip coeng + ro, it was already processed */
457         if (i == coengRo) {
458             i += 1;
459             continue;
460         }
461
462         switch (charClass & CF_POS_MASK)
463         {
464             case CF_POS_ABOVE :
465                 reordered[len] = uc;
466                 properties[len] = AboveForm;
467                 ++len;
468                 break;
469
470             case CF_POS_AFTER :
471                 reordered[len] = uc;
472                 properties[len] = PostForm;
473                 ++len;
474                 break;
475
476             case CF_POS_BELOW :
477                 reordered[len] = uc;
478                 properties[len] = BelowForm;
479                 ++len;
480                 break;
481
482             default:
483                 /* assign the correct flags to a coeng consonant
484                    Consonants of type 3 are taged as Post forms and those type 1 as below forms */
485                 if ( (charClass & CF_COENG) && i + 1 < syllableEnd ) {
486                     unsigned char property = (getKhmerCharClass(item->string[i+1]) & CF_CLASS_MASK) == CC_CONSONANT3 ?
487                                               PostForm : BelowForm;
488                     reordered[len] = uc;
489                     properties[len] = property;
490                     ++len;
491                     i += 1;
492                     reordered[len] = item->string[i];
493                     properties[len] = property;
494                     ++len;
495                     break;
496                 }
497
498                 /* if a shifter is followed by an above vowel change the shifter to below form,
499                    an above vowel can have two possible positions i + 1 or i + 3
500                    (position i+1 corresponds to unicode 3, position i+3 to Unicode 4)
501                    and there is an extra rule for C_VOWEL_AA + C_SIGN_NIKAHIT also for two
502                    different positions, right after the shifter or after a vowel (Unicode 4) */
503                 if ( (charClass & CF_SHIFTER) && (i + 1 < syllableEnd) ) {
504                     if (getKhmerCharClass(item->string[i+1]) & CF_ABOVE_VOWEL ) {
505                         reordered[len] = uc;
506                         properties[len] = BelowForm;
507                         ++len;
508                         break;
509                     }
510                     if (i + 2 < syllableEnd &&
511                         (item->string[i+1] == C_VOWEL_AA) &&
512                         (item->string[i+2] == C_SIGN_NIKAHIT) )
513                     {
514                         reordered[len] = uc;
515                         properties[len] = BelowForm;
516                         ++len;
517                         break;
518                     }
519                     if (i + 3 < syllableEnd && (getKhmerCharClass(item->string[i+3]) & CF_ABOVE_VOWEL) ) {
520                         reordered[len] = uc;
521                         properties[len] = BelowForm;
522                         ++len;
523                         break;
524                     }
525                     if (i + 4 < syllableEnd &&
526                         (item->string[i+3] == C_VOWEL_AA) &&
527                         (item->string[i+4] == C_SIGN_NIKAHIT) )
528                     {
529                         reordered[len] = uc;
530                         properties[len] = BelowForm;
531                         ++len;
532                         break;
533                     }
534                 }
535
536                 /* default - any other characters */
537                 reordered[len] = uc;
538                 ++len;
539                 break;
540         } /* switch */
541     } /* for */
542
543     if (!item->font->klass->convertStringToGlyphIndices(item->font,
544                                                         reordered, len,
545                                                         item->glyphs, &item->num_glyphs,
546                                                         item->item.bidiLevel % 2))
547         return FALSE;
548
549
550     KHDEBUG("after shaping: len=%d", len);
551     for (i = 0; i < len; i++) {
552         item->attributes[i].mark = FALSE;
553         item->attributes[i].clusterStart = FALSE;
554         item->attributes[i].justification = 0;
555         item->attributes[i].zeroWidth = FALSE;
556         KHDEBUG("    %d: %4x property=%x", i, reordered[i], properties[i]);
557     }
558
559     /* now we have the syllable in the right order, and can start running it through open type. */
560
561 #ifndef NO_OPENTYPE
562     if (openType) {
563         hb_uint32 where[16];
564         for (i = 0; i < len; ++i) {
565             where[i] = ~(PreSubstProperty
566                          | BelowSubstProperty
567                          | AboveSubstProperty
568                          | PostSubstProperty
569                          | CligProperty
570                          | PositioningProperties);
571             if (properties[i] == PreForm)
572                 where[i] &= ~PreFormProperty;
573             else if (properties[i] == BelowForm)
574                 where[i] &= ~BelowFormProperty;
575             else if (properties[i] == AboveForm)
576                 where[i] &= ~AboveFormProperty;
577             else if (properties[i] == PostForm)
578                 where[i] &= ~PostFormProperty;
579         }
580
581         HB_OpenTypeShape(item, where);
582         if (!HB_OpenTypePosition(item, availableGlyphs, /*doLogClusters*/FALSE))
583             return FALSE;
584     } else
585 #endif
586     {
587         KHDEBUG("Not using openType");
588         HB_HeuristicPosition(item);
589     }
590
591     item->attributes[0].clusterStart = TRUE;
592     return TRUE;
593 }
594
595 HB_Bool HB_KhmerShape(HB_ShaperItem *item)
596 {
597     HB_Bool openType = FALSE;
598     unsigned short *logClusters = item->log_clusters;
599     int i;
600
601     HB_ShaperItem syllable = *item;
602     int first_glyph = 0;
603
604     int sstart = item->item.pos;
605     int end = sstart + item->item.length;
606
607     assert(item->item.script == HB_Script_Khmer);
608
609 #ifndef NO_OPENTYPE
610     openType = HB_SelectScript(item, khmer_features);
611 #endif
612
613     KHDEBUG("khmer_shape: from %d length %d", item->item.pos, item->item.length);
614     while (sstart < end) {
615         HB_Bool invalid;
616         int send = khmer_nextSyllableBoundary(item->string, sstart, end, &invalid);
617         KHDEBUG("syllable from %d, length %d, invalid=%s", sstart, send-sstart,
618                invalid ? "TRUE" : "FALSE");
619         syllable.item.pos = sstart;
620         syllable.item.length = send-sstart;
621         syllable.glyphs = item->glyphs + first_glyph;
622         syllable.attributes = item->attributes + first_glyph;
623         syllable.offsets = item->offsets + first_glyph;
624         syllable.advances = item->advances + first_glyph;
625         syllable.num_glyphs = item->num_glyphs - first_glyph;
626         if (!khmer_shape_syllable(openType, &syllable)) {
627             KHDEBUG("syllable shaping failed, syllable requests %d glyphs", syllable.num_glyphs);
628             item->num_glyphs += syllable.num_glyphs;
629             return FALSE;
630         }
631         /* fix logcluster array */
632         KHDEBUG("syllable:");
633         for (i = first_glyph; i < first_glyph + (int)syllable.num_glyphs; ++i)
634             KHDEBUG("        %d -> glyph %x", i, item->glyphs[i]);
635         KHDEBUG("    logclusters:");
636         for (i = sstart; i < send; ++i) {
637             KHDEBUG("        %d -> glyph %d", i, first_glyph);
638             logClusters[i-item->item.pos] = first_glyph;
639         }
640         sstart = send;
641         first_glyph += syllable.num_glyphs;
642     }
643     item->num_glyphs = first_glyph;
644     return TRUE;
645 }
646
647 void HB_KhmerAttributes(HB_Script script, const HB_UChar16 *text, hb_uint32 from, hb_uint32 len, HB_CharAttributes *attributes)
648 {
649     int end = from + len;
650     const HB_UChar16 *uc = text + from;
651     hb_uint32 i = 0;
652     HB_UNUSED(script);
653     attributes += from;
654     while ( i < len ) {
655         HB_Bool invalid;
656         hb_uint32 boundary = khmer_nextSyllableBoundary( text, from+i, end, &invalid ) - from;
657
658         attributes[i].charStop = TRUE;
659
660         if ( boundary > len-1 ) boundary = len;
661         i++;
662         while ( i < boundary ) {
663             attributes[i].charStop = FALSE;
664             ++uc;
665             ++i;
666         }
667         assert( i == boundary );
668     }
669 }
670