[Indic] Support ZWJ/ZWNJ
[apps/core/preloaded/video-player.git] / src / hb-ot-shape-complex-indic.cc
1 /*
2  * Copyright © 2011  Google, Inc.
3  *
4  *  This is part of HarfBuzz, a text shaping 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  * Google Author(s): Behdad Esfahbod
25  */
26
27 #include "hb-ot-shape-complex-private.hh"
28
29 HB_BEGIN_DECLS
30
31
32 /* buffer var allocations */
33 #define indic_category() complex_var_persistent_u8_0() /* indic_category_t */
34 #define indic_position() complex_var_persistent_u8_1() /* indic_matra_category_t */
35
36 #define INDIC_TABLE_ELEMENT_TYPE uint8_t
37
38 /* Cateories used in the OpenType spec:
39  * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx
40  */
41 /* Note: This enum is duplicated in the -machine.rl source file.
42  * Not sure how to avoid duplication. */
43 enum indic_category_t {
44   OT_X = 0,
45   OT_C,
46   OT_Ra, /* Not explicitly listed in the OT spec, but used in the grammar. */
47   OT_V,
48   OT_N,
49   OT_H,
50   OT_ZWNJ,
51   OT_ZWJ,
52   OT_M,
53   OT_SM,
54   OT_VD,
55   OT_A,
56   OT_NBSP
57 };
58
59 /* Visual positions in a syllable from left to right. */
60 enum indic_position_t {
61   POS_PRE,
62   POS_BASE,
63   POS_ABOVE,
64   POS_BELOW,
65   POS_POST,
66 };
67
68 /* Categories used in IndicSyllabicCategory.txt from UCD */
69 /* The assignments are guesswork */
70 enum indic_syllabic_category_t {
71   INDIC_SYLLABIC_CATEGORY_OTHER                 = OT_X,
72
73   INDIC_SYLLABIC_CATEGORY_AVAGRAHA              = OT_X,
74   INDIC_SYLLABIC_CATEGORY_BINDU                 = OT_SM,
75   INDIC_SYLLABIC_CATEGORY_CONSONANT             = OT_C,
76   INDIC_SYLLABIC_CATEGORY_CONSONANT_DEAD        = OT_C,
77   INDIC_SYLLABIC_CATEGORY_CONSONANT_FINAL       = OT_C,
78   INDIC_SYLLABIC_CATEGORY_CONSONANT_HEAD_LETTER = OT_C,
79   INDIC_SYLLABIC_CATEGORY_CONSONANT_MEDIAL      = OT_C,
80   INDIC_SYLLABIC_CATEGORY_CONSONANT_PLACEHOLDER = OT_NBSP,
81   INDIC_SYLLABIC_CATEGORY_CONSONANT_SUBJOINED   = OT_C,
82   INDIC_SYLLABIC_CATEGORY_CONSONANT_REPHA       = OT_C,
83   INDIC_SYLLABIC_CATEGORY_MODIFYING_LETTER      = OT_X,
84   INDIC_SYLLABIC_CATEGORY_NUKTA                 = OT_N,
85   INDIC_SYLLABIC_CATEGORY_REGISTER_SHIFTER      = OT_X,
86   INDIC_SYLLABIC_CATEGORY_TONE_LETTER           = OT_X,
87   INDIC_SYLLABIC_CATEGORY_TONE_MARK             = OT_X,
88   INDIC_SYLLABIC_CATEGORY_VIRAMA                = OT_H,
89   INDIC_SYLLABIC_CATEGORY_VISARGA               = OT_SM,
90   INDIC_SYLLABIC_CATEGORY_VOWEL                 = OT_V,
91   INDIC_SYLLABIC_CATEGORY_VOWEL_DEPENDENT       = OT_M,
92   INDIC_SYLLABIC_CATEGORY_VOWEL_INDEPENDENT     = OT_V
93 };
94
95 /* Categories used in IndicSMatraCategory.txt from UCD */
96 enum indic_matra_category_t {
97   INDIC_MATRA_CATEGORY_NOT_APPLICABLE           = POS_BASE,
98
99   INDIC_MATRA_CATEGORY_LEFT                     = POS_PRE,
100   INDIC_MATRA_CATEGORY_TOP                      = POS_ABOVE,
101   INDIC_MATRA_CATEGORY_BOTTOM                   = POS_BELOW,
102   INDIC_MATRA_CATEGORY_RIGHT                    = POS_POST,
103
104   /* We don't really care much about these since we decompose them
105    * in the generic pre-shaping layer.  They will only be used if
106    * the font does not cover the decomposition.  In which case, we
107    * define these as aliases to the place we want the split-matra
108    * glyph to show up.  Quite arbitrary. */
109   INDIC_MATRA_CATEGORY_BOTTOM_AND_RIGHT         = INDIC_MATRA_CATEGORY_BOTTOM,
110   INDIC_MATRA_CATEGORY_LEFT_AND_RIGHT           = INDIC_MATRA_CATEGORY_LEFT,
111   INDIC_MATRA_CATEGORY_TOP_AND_BOTTOM           = INDIC_MATRA_CATEGORY_BOTTOM,
112   INDIC_MATRA_CATEGORY_TOP_AND_BOTTOM_AND_RIGHT = INDIC_MATRA_CATEGORY_BOTTOM,
113   INDIC_MATRA_CATEGORY_TOP_AND_LEFT             = INDIC_MATRA_CATEGORY_LEFT,
114   INDIC_MATRA_CATEGORY_TOP_AND_LEFT_AND_RIGHT   = INDIC_MATRA_CATEGORY_LEFT,
115   INDIC_MATRA_CATEGORY_TOP_AND_RIGHT            = INDIC_MATRA_CATEGORY_RIGHT,
116
117   INDIC_MATRA_CATEGORY_INVISIBLE                = INDIC_MATRA_CATEGORY_NOT_APPLICABLE,
118   INDIC_MATRA_CATEGORY_OVERSTRUCK               = INDIC_MATRA_CATEGORY_NOT_APPLICABLE,
119   INDIC_MATRA_CATEGORY_VISUAL_ORDER_LEFT        = INDIC_MATRA_CATEGORY_NOT_APPLICABLE
120 };
121
122 /* Note: We use ASSERT_STATIC_EXPR_ZERO() instead of ASSERT_STATIC_EXPR() and the comma operation
123  * because gcc fails to optimize the latter and fills the table in at runtime. */
124 #define INDIC_COMBINE_CATEGORIES(S,M) \
125   (ASSERT_STATIC_EXPR_ZERO (M == INDIC_MATRA_CATEGORY_NOT_APPLICABLE || (S == INDIC_SYLLABIC_CATEGORY_VIRAMA || S == INDIC_SYLLABIC_CATEGORY_VOWEL_DEPENDENT)) + \
126    ASSERT_STATIC_EXPR_ZERO (S < 16 && M < 16) + \
127    ((M << 4) | S))
128
129 #include "hb-ot-shape-complex-indic-table.hh"
130
131 /* XXX
132  * This is a hack for now.  We should:
133  * 1. Move this data into the main Indic table,
134  * and/or
135  * 2. Probe font lookups to determine consonant positions.
136  */
137 static const struct consonant_position_t {
138   hb_codepoint_t u;
139   indic_position_t position;
140 } consonant_positions[] = {
141   {0x0930, POS_BELOW},
142   {0x09AC, POS_BELOW},
143   {0x09AF, POS_POST},
144   {0x09B0, POS_BELOW},
145   {0x09F0, POS_BELOW},
146   {0x0A2F, POS_POST},
147   {0x0A30, POS_BELOW},
148   {0x0A35, POS_BELOW},
149   {0x0A39, POS_BELOW},
150   {0x0AB0, POS_BELOW},
151   {0x0B24, POS_BELOW},
152   {0x0B28, POS_BELOW},
153   {0x0B2C, POS_BELOW},
154   {0x0B2D, POS_BELOW},
155   {0x0B2E, POS_BELOW},
156   {0x0B2F, POS_POST},
157   {0x0B30, POS_BELOW},
158   {0x0B32, POS_BELOW},
159   {0x0B33, POS_BELOW},
160   {0x0B5F, POS_POST},
161   {0x0B71, POS_BELOW},
162   {0x0C15, POS_BELOW},
163   {0x0C16, POS_BELOW},
164   {0x0C17, POS_BELOW},
165   {0x0C18, POS_BELOW},
166   {0x0C19, POS_BELOW},
167   {0x0C1A, POS_BELOW},
168   {0x0C1B, POS_BELOW},
169   {0x0C1C, POS_BELOW},
170   {0x0C1D, POS_BELOW},
171   {0x0C1E, POS_BELOW},
172   {0x0C1F, POS_BELOW},
173   {0x0C20, POS_BELOW},
174   {0x0C21, POS_BELOW},
175   {0x0C22, POS_BELOW},
176   {0x0C23, POS_BELOW},
177   {0x0C24, POS_BELOW},
178   {0x0C25, POS_BELOW},
179   {0x0C26, POS_BELOW},
180   {0x0C27, POS_BELOW},
181   {0x0C28, POS_BELOW},
182   {0x0C2A, POS_BELOW},
183   {0x0C2B, POS_BELOW},
184   {0x0C2C, POS_BELOW},
185   {0x0C2D, POS_BELOW},
186   {0x0C2E, POS_BELOW},
187   {0x0C2F, POS_BELOW},
188   {0x0C30, POS_BELOW},
189   {0x0C32, POS_BELOW},
190   {0x0C33, POS_BELOW},
191   {0x0C35, POS_BELOW},
192   {0x0C36, POS_BELOW},
193   {0x0C37, POS_BELOW},
194   {0x0C38, POS_BELOW},
195   {0x0C39, POS_BELOW},
196   {0x0C95, POS_BELOW},
197   {0x0C96, POS_BELOW},
198   {0x0C97, POS_BELOW},
199   {0x0C98, POS_BELOW},
200   {0x0C99, POS_BELOW},
201   {0x0C9A, POS_BELOW},
202   {0x0C9B, POS_BELOW},
203   {0x0C9C, POS_BELOW},
204   {0x0C9D, POS_BELOW},
205   {0x0C9E, POS_BELOW},
206   {0x0C9F, POS_BELOW},
207   {0x0CA0, POS_BELOW},
208   {0x0CA1, POS_BELOW},
209   {0x0CA2, POS_BELOW},
210   {0x0CA3, POS_BELOW},
211   {0x0CA4, POS_BELOW},
212   {0x0CA5, POS_BELOW},
213   {0x0CA6, POS_BELOW},
214   {0x0CA7, POS_BELOW},
215   {0x0CA8, POS_BELOW},
216   {0x0CAA, POS_BELOW},
217   {0x0CAB, POS_BELOW},
218   {0x0CAC, POS_BELOW},
219   {0x0CAD, POS_BELOW},
220   {0x0CAE, POS_BELOW},
221   {0x0CAF, POS_BELOW},
222   {0x0CB0, POS_BELOW},
223   {0x0CB2, POS_BELOW},
224   {0x0CB3, POS_BELOW},
225   {0x0CB5, POS_BELOW},
226   {0x0CB6, POS_BELOW},
227   {0x0CB7, POS_BELOW},
228   {0x0CB8, POS_BELOW},
229   {0x0CB9, POS_BELOW},
230   {0x0CDE, POS_BELOW},
231   {0x0D2F, POS_POST},
232   {0x0D30, POS_POST},
233   {0x0D32, POS_BELOW},
234   {0x0D35, POS_POST},
235 };
236
237 /* XXX
238  * This is a hack for now.  We should move this data into the main Indic table.
239  */
240 static const hb_codepoint_t ra_chars[] = {
241   0x0930, /* Devanagari */
242   0x09B0, /* Bengali */
243   0x09F0, /* Bengali */
244 //0x09F1, /* Bengali */
245 //0x0A30, /* Gurmukhi */
246   0x0AB0, /* Gujarati */
247   0x0B30, /* Oriya */
248 //0x0BB0, /* Tamil */
249 //0x0C30, /* Telugu */
250   0x0CB0, /* Kannada */
251 //0x0D30, /* Malayalam */
252 };
253
254 static int
255 compare_codepoint (const void *pa, const void *pb)
256 {
257   hb_codepoint_t a = * (hb_codepoint_t *) pa;
258   hb_codepoint_t b = * (hb_codepoint_t *) pb;
259
260   return a < b ? -1 : a == b ? 0 : +1;
261 }
262
263 static indic_position_t
264 consonant_position (hb_codepoint_t u)
265 {
266   consonant_position_t *record;
267
268   record = (consonant_position_t *) bsearch (&u, consonant_positions,
269                                              ARRAY_LENGTH (consonant_positions),
270                                              sizeof (consonant_positions[0]),
271                                              compare_codepoint);
272
273   return record ? record->position : POS_BASE;
274 }
275
276 static bool
277 is_ra (hb_codepoint_t u)
278 {
279   return !!bsearch (&u, ra_chars,
280                     ARRAY_LENGTH (ra_chars),
281                     sizeof (ra_chars[0]),
282                     compare_codepoint);
283 }
284
285 static bool
286 is_joiner (const hb_glyph_info_t &info)
287 {
288   return !!(FLAG (info.indic_category()) & (FLAG (OT_ZWJ) | FLAG (OT_ZWNJ)));
289 }
290
291 static bool
292 is_consonant (const hb_glyph_info_t &info)
293 {
294   return !!(FLAG (info.indic_category()) & (FLAG (OT_C) | FLAG (OT_Ra)));
295 }
296
297 static const struct {
298   hb_tag_t tag;
299   hb_bool_t is_global;
300 } indic_basic_features[] =
301 {
302   {HB_TAG('n','u','k','t'), true},
303   {HB_TAG('a','k','h','n'), false},
304   {HB_TAG('r','p','h','f'), false},
305   {HB_TAG('r','k','r','f'), false},
306   {HB_TAG('p','r','e','f'), false},
307   {HB_TAG('b','l','w','f'), false},
308   {HB_TAG('h','a','l','f'), false},
309   {HB_TAG('v','a','t','u'), true},
310   {HB_TAG('p','s','t','f'), false},
311   {HB_TAG('c','j','c','t'), false},
312 };
313
314 /* Same order as the indic_basic_features array */
315 enum {
316   _NUKT,
317   AKHN,
318   RPHF,
319   RKRF,
320   PREF,
321   BLWF,
322   HALF,
323   _VATU,
324   PSTF,
325   CJCT,
326 };
327
328 static const hb_tag_t indic_other_features[] =
329 {
330   HB_TAG('p','r','e','s'),
331   HB_TAG('a','b','v','s'),
332   HB_TAG('b','l','w','s'),
333   HB_TAG('p','s','t','s'),
334   HB_TAG('h','a','l','n'),
335
336   HB_TAG('d','i','s','t'),
337   HB_TAG('a','b','v','m'),
338   HB_TAG('b','l','w','m'),
339 };
340
341
342 static void
343 initial_reordering (const hb_ot_map_t *map,
344                     hb_face_t *face,
345                     hb_buffer_t *buffer,
346                     void *user_data HB_UNUSED);
347 static void
348 final_reordering (const hb_ot_map_t *map,
349                   hb_face_t *face,
350                   hb_buffer_t *buffer,
351                   void *user_data HB_UNUSED);
352
353 void
354 _hb_ot_shape_complex_collect_features_indic (hb_ot_map_builder_t *map, const hb_segment_properties_t  *props)
355 {
356   map->add_bool_feature (HB_TAG('l','o','c','l'));
357   /* The Indic specs do not require ccmp, but we apply it here since if
358    * there is a use of it, it's typically at the beginning. */
359   map->add_bool_feature (HB_TAG('c','c','m','p'));
360
361   map->add_gsub_pause (initial_reordering, NULL);
362
363   for (unsigned int i = 0; i < ARRAY_LENGTH (indic_basic_features); i++)
364     map->add_bool_feature (indic_basic_features[i].tag, indic_basic_features[i].is_global);
365
366   map->add_gsub_pause (final_reordering, NULL);
367
368   for (unsigned int i = 0; i < ARRAY_LENGTH (indic_other_features); i++)
369     map->add_bool_feature (indic_other_features[i], true);
370 }
371
372
373 bool
374 _hb_ot_shape_complex_prefer_decomposed_indic (void)
375 {
376   /* We want split matras decomposed by the common shaping logic. */
377   return TRUE;
378 }
379
380
381 void
382 _hb_ot_shape_complex_setup_masks_indic (hb_ot_map_t *map, hb_buffer_t *buffer)
383 {
384   HB_BUFFER_ALLOCATE_VAR (buffer, indic_category);
385   HB_BUFFER_ALLOCATE_VAR (buffer, indic_position);
386
387   /* We cannot setup masks here.  We save information about characters
388    * and setup masks later on in a pause-callback. */
389
390   unsigned int count = buffer->len;
391   for (unsigned int i = 0; i < count; i++)
392   {
393     unsigned int type = get_indic_categories (buffer->info[i].codepoint);
394
395     buffer->info[i].indic_category() = type & 0x0F;
396     buffer->info[i].indic_position() = type >> 4;
397
398     if (buffer->info[i].indic_category() == OT_C) {
399       buffer->info[i].indic_position() = consonant_position (buffer->info[i].codepoint);
400       if (is_ra (buffer->info[i].codepoint))
401         buffer->info[i].indic_category() = OT_Ra;
402     } else if (buffer->info[i].codepoint == 0x200C)
403       buffer->info[i].indic_category() = OT_ZWNJ;
404     else if (buffer->info[i].codepoint == 0x200D)
405       buffer->info[i].indic_category() = OT_ZWJ;
406   }
407 }
408
409 static int
410 compare_indic_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
411 {
412   int a = pa->indic_position();
413   int b = pb->indic_position();
414
415   return a < b ? -1 : a == b ? 0 : +1;
416 }
417
418 static void
419 found_consonant_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
420                           unsigned int start, unsigned int end)
421 {
422   unsigned int i;
423   hb_glyph_info_t *info = buffer->info;
424
425   /* Comments from:
426    * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */
427
428   /* 1. Find base consonant:
429    *
430    * The shaping engine finds the base consonant of the syllable, using the
431    * following algorithm: starting from the end of the syllable, move backwards
432    * until a consonant is found that does not have a below-base or post-base
433    * form (post-base forms have to follow below-base forms), or that is not a
434    * pre-base reordering Ra, or arrive at the first consonant. The consonant
435    * stopped at will be the base.
436    *
437    *   o If the syllable starts with Ra + Halant (in a script that has Reph)
438    *     and has more than one consonant, Ra is excluded from candidates for
439    *     base consonants.
440    */
441
442   unsigned int base = 0;
443
444   /* -> starting from the end of the syllable, move backwards */
445   i = end;
446   do {
447     i--;
448     /* -> until a consonant is found */
449     if (info[i].indic_category() == OT_C)
450     //if ((FLAG (info[i].indic_category()) & (FLAG (OT_C) | FLAG (OT_Ra))))
451     {
452       /* -> that does not have a below-base or post-base form
453        * (post-base forms have to follow below-base forms), */
454       if (info[i].indic_position() != POS_BELOW &&
455           info[i].indic_position() != POS_POST)
456       {
457         base = i;
458         break;
459       }
460
461       /* TODO: or that is not a pre-base reordering Ra, */
462
463       /* -> or arrive at the first consonant. The consonant stopped at will be the base. */
464       base = i;
465     }
466   } while (i > start);
467   if (base < start)
468     base = start; /* Just in case... */
469
470   /* TODO
471    * If the syllable starts with Ra + Halant (in a script that has Reph)
472    * and has more than one consonant, Ra is excluded from candidates for
473    * base consonants. */
474
475
476   /* 2. Decompose and reorder Matras:
477    *
478    * Each matra and any syllable modifier sign in the cluster are moved to the
479    * appropriate position relative to the consonant(s) in the cluster. The
480    * shaping engine decomposes two- or three-part matras into their constituent
481    * parts before any repositioning. Matra characters are classified by which
482    * consonant in a conjunct they have affinity for and are reordered to the
483    * following positions:
484    *
485    *   o Before first half form in the syllable
486    *   o After subjoined consonants
487    *   o After post-form consonant
488    *   o After main consonant (for above marks)
489    *
490    * IMPLEMENTATION NOTES:
491    *
492    * The normalize() routine has already decomposed matras for us, so we don't
493    * need to worry about that.
494    */
495
496
497   /* 3.  Reorder marks to canonical order:
498    *
499    * Adjacent nukta and halant or nukta and vedic sign are always repositioned
500    * if necessary, so that the nukta is first.
501    *
502    * IMPLEMENTATION NOTES:
503    *
504    * We don't need to do this: the normalize() routine already did this for us.
505    */
506
507
508   /* Reorder characters */
509
510   for (i = start; i < base; i++)
511     info[i].indic_position() = POS_PRE;
512   info[base].indic_position() = POS_BASE;
513
514
515   /* Handle beginning Ra */
516   if (start + 2 <= end &&
517       info[start].indic_category() == OT_Ra &&
518       info[start + 1].indic_category() == OT_H &&
519       (start + 2 == end || !is_joiner (info[start])))
520    {
521     info[start].indic_position() = POS_POST;
522     info[start].mask = mask_array[RPHF];
523    }
524
525   /* For old-style Indic script tags, move the first post-base Halant after
526    * last consonant. */
527   if ((map->get_chosen_script (0) & 0x000000FF) != '2') {
528     /* We should only do this for Indic scripts which have a version two I guess. */
529     for (i = base + 1; i < end; i++)
530       if (info[i].indic_category() == OT_H) {
531         unsigned int j;
532         for (j = end - 1; j > i; j--)
533           if ((FLAG (info[j].indic_category()) & (FLAG (OT_C) | FLAG (OT_Ra))))
534             break;
535         if (j > i) {
536           /* Move Halant to after last consonant. */
537           hb_glyph_info_t t = info[i];
538           memmove (&info[i], &info[i + 1], (j - i) * sizeof (info[0]));
539           info[j] = t;
540         }
541         break;
542       }
543   }
544
545   /* Attach ZWJ, ZWNJ, nukta, and halant to previous char to move with them. */
546   for (i = start + 1; i < end; i++)
547     if ((FLAG (info[i].indic_category()) &
548          (FLAG (OT_ZWNJ) | FLAG (OT_ZWJ) | FLAG (OT_N) | FLAG (OT_H))))
549       info[i].indic_position() = info[i - 1].indic_position();
550
551   /* We do bubble-sort, skip malicious clusters attempts */
552   if (end - start > 20)
553     return;
554
555   /* Sit tight, rock 'n roll! */
556   hb_bubble_sort (info + start, end - start, compare_indic_order);
557
558   /* Setup masks now */
559
560   /* Pre-base */
561   for (i = start; i < base; i++)
562     info[i].mask  |= mask_array[HALF] | mask_array[AKHN] | mask_array[CJCT];
563   /* Base */
564   info[base].mask |= mask_array[AKHN] | mask_array[CJCT];
565   /* Post-base */
566   for (i = base + 1; i < end; i++)
567     info[i].mask  |= mask_array[BLWF] | mask_array[PSTF] | mask_array[CJCT];
568
569   /* Apply ZWJ/ZWNJ effects */
570   for (i = start + 1; i < end; i++)
571     if (is_joiner (info[i])) {
572       bool non_joiner = info[i].indic_category() == OT_ZWNJ;
573       unsigned int j = i - 1;
574
575       do {
576         info[j].mask &= !mask_array[HALF];
577         if (non_joiner)
578           info[j].mask &= !mask_array[CJCT];
579         j--;
580       } while (j > start && !is_consonant (info[j]));
581     }
582 }
583
584
585 static void
586 found_vowel_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
587                       unsigned int start, unsigned int end)
588 {
589   /* TODO
590    * Not clear to me how this should work.  Do the matras move to before the
591    * independent vowel?  No idea.
592    */
593 }
594
595 static void
596 found_standalone_cluster (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
597                           unsigned int start, unsigned int end)
598 {
599   /* TODO
600    * Easiest thing to do here is to convert the NBSP to consonant and
601    * call found_consonant_syllable.
602    */
603 }
604
605 static void
606 found_non_indic (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
607                  unsigned int start, unsigned int end)
608 {
609   /* Nothing to do right now.  If we ever switch to using the output
610    * buffer in the reordering process, we'd need to next_glyph() here. */
611 }
612
613 #include "hb-ot-shape-complex-indic-machine.hh"
614
615 static void
616 remove_joiners (hb_buffer_t *buffer)
617 {
618   buffer->clear_output ();
619   unsigned int count = buffer->len;
620   for (buffer->idx = 0; buffer->idx < count;)
621     if (unlikely (is_joiner (buffer->info[buffer->idx])))
622       buffer->skip_glyph ();
623     else
624       buffer->next_glyph ();
625
626   buffer->swap_buffers ();
627 }
628
629 static void
630 initial_reordering (const hb_ot_map_t *map,
631                     hb_face_t *face,
632                     hb_buffer_t *buffer,
633                     void *user_data HB_UNUSED)
634 {
635   hb_mask_t mask_array[ARRAY_LENGTH (indic_basic_features)] = {0};
636   unsigned int num_masks = ARRAY_LENGTH (indic_basic_features);
637   for (unsigned int i = 0; i < num_masks; i++)
638     mask_array[i] = map->get_1_mask (indic_basic_features[i].tag);
639
640   find_syllables (map, buffer, mask_array);
641
642   remove_joiners (buffer);
643 }
644
645 static void
646 final_reordering (const hb_ot_map_t *map,
647                   hb_face_t *face,
648                   hb_buffer_t *buffer,
649                   void *user_data HB_UNUSED)
650 {
651   HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category);
652   HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position);
653 }
654
655
656
657 HB_END_DECLS