hb-ot-shape-complex-indic.cc revision f89367251109af235f4f0446c13c261a5a4a6f72
1b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod/*
220503ccd578c9983162857954e3236413469ed35Behdad Esfahbod * Copyright © 2011  Google, Inc.
3b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod *
4b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod *  This is part of HarfBuzz, a text shaping library.
5b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod *
6b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * Permission is hereby granted, without written agreement and without
7b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * license or royalty fees, to use, copy, modify, and distribute this
8b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * software and its documentation for any purpose, provided that the
9b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * above copyright notice and the following two paragraphs appear in
10b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * all copies of this software.
11b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod *
12b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * DAMAGE.
17b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod *
18b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod *
24b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * Google Author(s): Behdad Esfahbod
25b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod */
26b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
273ed4634ec349fa9e943ad23718c04be4dd4bba62Behdad Esfahbod#include "hb-ot-shape-complex-indic-private.hh"
28a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod#include "hb-ot-shape-private.hh"
29352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod
30ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbodstatic const struct indic_options_t
31ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod{
32ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  indic_options_t (void)
33ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  {
34ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    char *c = getenv ("HB_OT_INDIC_OPTIONS");
35ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    uniscribe_bug_compatible = c && strstr (c, "uniscribe-bug-compatible");
36ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  }
37ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod
38ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  bool uniscribe_bug_compatible;
39ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod} options;
40ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod
41743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic int
42743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodcompare_codepoint (const void *pa, const void *pb)
43743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
44743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  hb_codepoint_t a = * (hb_codepoint_t *) pa;
45743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  hb_codepoint_t b = * (hb_codepoint_t *) pb;
46743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
47743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  return a < b ? -1 : a == b ? 0 : +1;
48743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
49743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
50743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic indic_position_t
51743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodconsonant_position (hb_codepoint_t u)
52743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
53743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  consonant_position_t *record;
54743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
55743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  record = (consonant_position_t *) bsearch (&u, consonant_positions,
56743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod					     ARRAY_LENGTH (consonant_positions),
57743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod					     sizeof (consonant_positions[0]),
58743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod					     compare_codepoint);
59743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
60dbccf87eef0d26838fa4bb3ae26410f6c4818836Behdad Esfahbod  return record ? record->position : POS_BASE_C;
61743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
62743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
63352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbodstatic bool
64352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbodis_ra (hb_codepoint_t u)
65352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod{
66352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod  return !!bsearch (&u, ra_chars,
67352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod		    ARRAY_LENGTH (ra_chars),
68352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod		    sizeof (ra_chars[0]),
69352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod		    compare_codepoint);
70352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod}
71352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod
729ee27a928a989c71923cef82a9e9828f8e9ca051Behdad Esfahbodstatic bool
739da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbodis_joiner (const hb_glyph_info_t &info)
749ee27a928a989c71923cef82a9e9828f8e9ca051Behdad Esfahbod{
759da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  return !!(FLAG (info.indic_category()) & (FLAG (OT_ZWJ) | FLAG (OT_ZWNJ)));
769da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod}
779da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
789da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbodstatic bool
799da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbodis_consonant (const hb_glyph_info_t &info)
809da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod{
811a1fa8c655a082fc1439608457ba717306cc83caBehdad Esfahbod  /* Note:
821a1fa8c655a082fc1439608457ba717306cc83caBehdad Esfahbod   *
8318c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod   * We treat Vowels and placeholders as if they were consonants.  This is safe because Vowels
84c5306b6861cfaa50af40e8ceb058791fa06d7981Behdad Esfahbod   * cannot happen in a consonant syllable.  The plus side however is, we can call the
85c5306b6861cfaa50af40e8ceb058791fa06d7981Behdad Esfahbod   * consonant syllable logic from the vowel syllable function and get it all right! */
8618c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod  return !!(FLAG (info.indic_category()) & (FLAG (OT_C) | FLAG (OT_Ra) | FLAG (OT_V) | FLAG (OT_NBSP) | FLAG (OT_DOTTEDCIRCLE)));
879ee27a928a989c71923cef82a9e9828f8e9ca051Behdad Esfahbod}
88c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod
89eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodstruct feature_list_t {
90c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  hb_tag_t tag;
91c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  hb_bool_t is_global;
92eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod};
93eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
94eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodstatic const feature_list_t
95eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodindic_basic_features[] =
96b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
97c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('n','u','k','t'), true},
98c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('a','k','h','n'), false},
99c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('r','p','h','f'), false},
1001ac075b227090a9ad930dcc1670236c176b27067Behdad Esfahbod  {HB_TAG('r','k','r','f'), true},
101c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('p','r','e','f'), false},
102c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('b','l','w','f'), false},
103c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('h','a','l','f'), false},
104c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('v','a','t','u'), true},
105c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('p','s','t','f'), false},
1069da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  {HB_TAG('c','j','c','t'), false},
107c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod};
108c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod
109c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod/* Same order as the indic_basic_features array */
110c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbodenum {
111c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  _NUKT,
112c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  AKHN,
113c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  RPHF,
114df6d45c693c417bf311e6fa49f18a8558542e525Behdad Esfahbod  _RKRF,
115c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  PREF,
116c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  BLWF,
117c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  HALF,
118c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  _VATU,
119c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  PSTF,
120e8eedf2687f05372bf5476e84139d01ba67c9f73Behdad Esfahbod  CJCT
121b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod};
122b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
123eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodstatic const feature_list_t
124eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodindic_other_features[] =
125b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
126eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('i','n','i','t'), false},
127eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('p','r','e','s'), true},
128eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('a','b','v','s'), true},
129eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('b','l','w','s'), true},
130eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('p','s','t','s'), true},
131eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('h','a','l','n'), true},
132eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
133eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('d','i','s','t'), true},
134eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('a','b','v','m'), true},
135eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('b','l','w','m'), true},
136eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod};
137eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
138eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod/* Same order as the indic_other_features array */
139eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodenum {
140eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  INIT
141b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod};
142b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
143743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
144743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
145743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodinitial_reordering (const hb_ot_map_t *map,
146743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_face_t *face,
147743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_buffer_t *buffer,
148743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    void *user_data HB_UNUSED);
149f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbodstatic void
150f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbodfinal_reordering (const hb_ot_map_t *map,
151f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod		  hb_face_t *face,
152f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod		  hb_buffer_t *buffer,
153743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		  void *user_data HB_UNUSED);
154b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
155b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbodvoid
15676f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod_hb_ot_shape_complex_collect_features_indic (hb_ot_map_builder_t *map, const hb_segment_properties_t  *props)
157b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
158f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_bool_feature (HB_TAG('l','o','c','l'));
159a54a5505a35eef5315a8e2e7a79502901e3eff5fBehdad Esfahbod  /* The Indic specs do not require ccmp, but we apply it here since if
160a54a5505a35eef5315a8e2e7a79502901e3eff5fBehdad Esfahbod   * there is a use of it, it's typically at the beginning. */
161f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_bool_feature (HB_TAG('c','c','m','p'));
162f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
163743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  map->add_gsub_pause (initial_reordering, NULL);
164f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
165412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (indic_basic_features); i++) {
16676f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod    map->add_bool_feature (indic_basic_features[i].tag, indic_basic_features[i].is_global);
167412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod    map->add_gsub_pause (NULL, NULL);
168412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  }
169b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
170f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_gsub_pause (final_reordering, NULL);
171f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
172412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (indic_other_features); i++) {
173eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod    map->add_bool_feature (indic_other_features[i].tag, indic_other_features[i].is_global);
174412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod    map->add_gsub_pause (NULL, NULL);
175412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  }
176b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod}
177b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
178867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
17911138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbodhb_ot_shape_normalization_mode_t
18011138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbod_hb_ot_shape_complex_normalization_preference_indic (void)
18102cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod{
18202cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod  /* We want split matras decomposed by the common shaping logic. */
18311138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbod  return HB_OT_SHAPE_NORMALIZATION_MODE_DECOMPOSED;
18402cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod}
18502cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod
186867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
187b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbodvoid
188acd88e659fdb2fa1cbf7171f0a1c7fbc81b9f298Behdad Esfahbod_hb_ot_shape_complex_setup_masks_indic (hb_ot_map_t *map, hb_buffer_t *buffer, hb_font_t *font)
189b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
1909f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod  HB_BUFFER_ALLOCATE_VAR (buffer, indic_category);
1919f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod  HB_BUFFER_ALLOCATE_VAR (buffer, indic_position);
1929f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod
193743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* We cannot setup masks here.  We save information about characters
194743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * and setup masks later on in a pause-callback. */
195743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
196743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  unsigned int count = buffer->len;
197b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  for (unsigned int i = 0; i < count; i++)
198b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  {
19992332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    hb_glyph_info_t &info = buffer->info[i];
20092332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    unsigned int type = get_indic_categories (info.codepoint);
20192332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod
20292332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    info.indic_category() = type & 0x0F;
20392332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    info.indic_position() = type >> 4;
20492332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod
2053399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod    /* The spec says U+0952 is OT_A.  However, testing shows that Uniscribe
2063399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * treats U+0951..U+0952 all as OT_VD.
2073399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * TESTS:
2083399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * U+092E,U+0947,U+0952
2093399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * U+092E,U+0952,U+0947
2103399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * U+092E,U+0947,U+0951
2113399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * U+092E,U+0951,U+0947
2123399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * */
2133399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod    if (unlikely (hb_in_range<hb_codepoint_t> (info.codepoint, 0x0951, 0x0954)))
2143399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod      info.indic_category() = OT_VD;
2153399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod
21692332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    if (info.indic_category() == OT_C) {
21792332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_position() = consonant_position (info.codepoint);
21892332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      if (is_ra (info.codepoint))
21992332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod	info.indic_category() = OT_Ra;
22092332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    } else if (info.indic_category() == OT_SM ||
22192332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod	       info.indic_category() == OT_VD) {
22292332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_position() = POS_SMVD;
22392332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    } else if (unlikely (info.codepoint == 0x200C))
22492332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_category() = OT_ZWNJ;
22592332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    else if (unlikely (info.codepoint == 0x200D))
22692332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_category() = OT_ZWJ;
22718c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod    else if (unlikely (info.codepoint == 0x25CC))
22818c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod      info.indic_category() = OT_DOTTEDCIRCLE;
229b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  }
230743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
231b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
23245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodstatic int
23345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodcompare_indic_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
23445d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod{
23545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  int a = pa->indic_position();
23645d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  int b = pb->indic_position();
23745d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
23845d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  return a < b ? -1 : a == b ? 0 : +1;
23945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod}
240867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
2417ea58db311bfb0d8f804d1e9f4a1f004bd45075aBehdad Esfahbod/* Rules from:
2427ea58db311bfb0d8f804d1e9f4a1f004bd45075aBehdad Esfahbod * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */
2437ea58db311bfb0d8f804d1e9f4a1f004bd45075aBehdad Esfahbod
244743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
2450831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbodinitial_reordering_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
2460831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod			     unsigned int start, unsigned int end, unsigned int base);
2470831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod
2480831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbodstatic void
249ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodinitial_reordering_consonant_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
250ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod				       unsigned int start, unsigned int end)
251743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
252ee58f3bc75d2d071a71b94063bf12205a5871acbBehdad Esfahbod  hb_glyph_info_t *info = buffer->info;
253743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
254743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 1. Find base consonant:
255743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
256743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * The shaping engine finds the base consonant of the syllable, using the
257743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * following algorithm: starting from the end of the syllable, move backwards
258743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * until a consonant is found that does not have a below-base or post-base
259743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * form (post-base forms have to follow below-base forms), or that is not a
260743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * pre-base reordering Ra, or arrive at the first consonant. The consonant
261743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * stopped at will be the base.
262743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
263743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o If the syllable starts with Ra + Halant (in a script that has Reph)
264743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *     and has more than one consonant, Ra is excluded from candidates for
265743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *     base consonants.
266743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
267743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
2685e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod  unsigned int base = end;
26976b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  bool has_reph = false;
270743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
27176b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
27276b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod   *    and has more than one consonant, Ra is excluded from candidates for
27376b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod   *    base consonants. */
2745e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod  unsigned int limit = start;
27576b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  if (mask_array[RPHF] &&
2766d8e0cb74c02f6bc09cd4abe9e4bc82062e1b517Behdad Esfahbod      start + 3 <= end &&
27776b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod      info[start].indic_category() == OT_Ra &&
2786d8e0cb74c02f6bc09cd4abe9e4bc82062e1b517Behdad Esfahbod      info[start + 1].indic_category() == OT_H &&
2796d8e0cb74c02f6bc09cd4abe9e4bc82062e1b517Behdad Esfahbod      !is_joiner (info[start + 2]))
28076b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  {
2815e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod    limit += 2;
2825e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod    base = start;
28376b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod    has_reph = true;
2845e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod  };
28576b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod
2863c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod  {
2873c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    /* -> starting from the end of the syllable, move backwards */
2883c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    unsigned int i = end;
2893c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    do {
2903c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      i--;
2913c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      /* -> until a consonant is found */
2923c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      if (is_consonant (info[i]))
293743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod      {
2943c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	/* -> that does not have a below-base or post-base form
2953c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	 * (post-base forms have to follow below-base forms), */
2963c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	if (info[i].indic_position() != POS_BELOW_C &&
2973c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	    info[i].indic_position() != POS_POST_C)
2983c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	{
2993c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	  base = i;
3003c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	  break;
3013c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	}
302743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
3033c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	/* -> or that is not a pre-base reordering Ra,
3043c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	 *
3053c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	 * TODO
3063c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	 */
307e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
3083c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	/* -> or arrive at the first consonant. The consonant stopped at will
3093c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	 * be the base. */
3103c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	base = i;
3113c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      }
3123c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      else
3133c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	if (is_joiner (info[i]))
3143c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	  break;
3153c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    } while (i > limit);
3163c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    if (base < start)
3173c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      base = start; /* Just in case... */
3183c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod  }
319743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
3200831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod  /* Continue reading below ------v */
3210831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod  initial_reordering_syllable (map, buffer, mask_array, start, end, base);
3220831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod}
3233d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod
3240831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbodstatic void
3250831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbodinitial_reordering_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
3260831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod			     unsigned int start, unsigned int end, unsigned int base)
3270831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod{
3280831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod  hb_glyph_info_t *info = buffer->info;
3293d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod
330743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 2. Decompose and reorder Matras:
331743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
332743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * Each matra and any syllable modifier sign in the cluster are moved to the
333743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * appropriate position relative to the consonant(s) in the cluster. The
334743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * shaping engine decomposes two- or three-part matras into their constituent
335743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * parts before any repositioning. Matra characters are classified by which
336743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * consonant in a conjunct they have affinity for and are reordered to the
337743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * following positions:
338743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
339743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o Before first half form in the syllable
340743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After subjoined consonants
341743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After post-form consonant
342743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After main consonant (for above marks)
343743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
344743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * IMPLEMENTATION NOTES:
345743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
346743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * The normalize() routine has already decomposed matras for us, so we don't
347743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * need to worry about that.
348743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
349743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
350743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
351743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 3.  Reorder marks to canonical order:
352743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
353743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * Adjacent nukta and halant or nukta and vedic sign are always repositioned
354743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * if necessary, so that the nukta is first.
355743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
356743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * IMPLEMENTATION NOTES:
357743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
358743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * We don't need to do this: the normalize() routine already did this for us.
359743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
360743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
361743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
36245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* Reorder characters */
36345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
3643c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod  for (unsigned int i = start; i < base; i++)
365dbccf87eef0d26838fa4bb3ae26410f6c4818836Behdad Esfahbod    info[i].indic_position() = POS_PRE_C;
366dbccf87eef0d26838fa4bb3ae26410f6c4818836Behdad Esfahbod  info[base].indic_position() = POS_BASE_C;
36745d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
368fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod  /* Handle beginning Ra */
3690831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod  if (mask_array[RPHF] &&
3700831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod      start != base &&
3710831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod      start + 3 <= end &&
3720831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod      info[start].indic_category() == OT_Ra &&
3730831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod      info[start + 1].indic_category() == OT_H &&
3740831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod      !is_joiner (info[start + 2]))
375dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod    info[start].indic_position() = POS_RA_TO_BECOME_REPH;
376fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod
377f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  /* For old-style Indic script tags, move the first post-base Halant after
378f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod   * last consonant. */
379f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  if ((map->get_chosen_script (0) & 0x000000FF) != '2') {
380f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod    /* We should only do this for Indic scripts which have a version two I guess. */
3813c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = base + 1; i < end; i++)
382f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod      if (info[i].indic_category() == OT_H) {
383f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        unsigned int j;
384f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        for (j = end - 1; j > i; j--)
385190eb31a16178269aecaf5d2ecc9012f956749f4Behdad Esfahbod	  if (is_consonant (info[j]))
386f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	    break;
387f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	if (j > i) {
388f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  /* Move Halant to after last consonant. */
389f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  hb_glyph_info_t t = info[i];
390f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  memmove (&info[i], &info[i + 1], (j - i) * sizeof (info[0]));
391f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  info[j] = t;
392f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	}
393f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        break;
394f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod      }
395f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  }
396f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod
39745d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* Attach ZWJ, ZWNJ, nukta, and halant to previous char to move with them. */
398ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  if (!options.uniscribe_bug_compatible)
399ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  {
400ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    /* Please update the Uniscribe branch when touching this! */
401ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    for (unsigned int i = start + 1; i < end; i++)
402ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod      if ((FLAG (info[i].indic_category()) & (FLAG (OT_ZWNJ) | FLAG (OT_ZWJ) | FLAG (OT_N) | FLAG (OT_H))))
403ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	info[i].indic_position() = info[i - 1].indic_position();
404ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  } else {
40567ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod    /*
40667ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod     * Uniscribe doesn't move the Halant with Left Matra.
40767ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod     * TEST: U+092B,U+093F,U+094DE
40867ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod     */
409ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    /* Please update the non-Uniscribe branch when touching this! */
410ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    for (unsigned int i = start + 1; i < end; i++)
411ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod      if ((FLAG (info[i].indic_category()) & (FLAG (OT_ZWNJ) | FLAG (OT_ZWJ) | FLAG (OT_N) | FLAG (OT_H)))) {
412ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	info[i].indic_position() = info[i - 1].indic_position();
413ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	if (info[i].indic_category() == OT_H && info[i].indic_position() == POS_LEFT_MATRA)
414ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	  for (unsigned int j = i; j > start; j--)
415ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	    if (info[j - 1].indic_position() != POS_LEFT_MATRA) {
416ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	      info[i].indic_position() = info[j - 1].indic_position();
417ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	      break;
418ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	    }
419ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod      }
420ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  }
42145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
42245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* We do bubble-sort, skip malicious clusters attempts */
423b99d63ae114fb58f129562b293a8a66543d499adBehdad Esfahbod  if (end - start < 64)
424a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod  {
425a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    /* Sit tight, rock 'n roll! */
426d3637edb248162970e202e9d0671540274192844Behdad Esfahbod    hb_bubble_sort (info + start, end - start, compare_indic_order);
427a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    /* Find base again */
428a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    base = end;
4293c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = start; i < end; i++)
430a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod      if (info[i].indic_position() == POS_BASE_C) {
431a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod        base = i;
432a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod	break;
433a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod      }
434a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod  }
43545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
436743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Setup masks now */
437743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
438281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod  {
439281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    hb_mask_t mask;
440281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod
441dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod    /* Reph */
442668c6046c1b3af3bd316bda0cc8636f2a5e8df42Behdad Esfahbod    for (unsigned int i = start; i < end && info[i].indic_position() == POS_RA_TO_BECOME_REPH; i++)
443668c6046c1b3af3bd316bda0cc8636f2a5e8df42Behdad Esfahbod      info[i].mask |= mask_array[RPHF];
444dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
445281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Pre-base */
446281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    mask = mask_array[HALF] | mask_array[AKHN] | mask_array[CJCT];
4473c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = start; i < base; i++)
448281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod      info[i].mask  |= mask;
449281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Base */
450281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    mask = mask_array[AKHN] | mask_array[CJCT];
451281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    info[base].mask |= mask;
452281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Post-base */
453281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    mask = mask_array[BLWF] | mask_array[PSTF] | mask_array[CJCT];
4543c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = base + 1; i < end; i++)
455281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod      info[i].mask  |= mask;
456281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod  }
4579da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
4589da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  /* Apply ZWJ/ZWNJ effects */
4593c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod  for (unsigned int i = start + 1; i < end; i++)
4609da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    if (is_joiner (info[i])) {
4619da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      bool non_joiner = info[i].indic_category() == OT_ZWNJ;
4626b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod      unsigned int j = i;
4639da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
4649da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      do {
4659da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod	j--;
4666b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod
4673bf27a9f0e92aa31b464bd3b9fdea5933c9ae8b1Behdad Esfahbod	info[j].mask &= ~mask_array[CJCT];
4686b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod	if (non_joiner)
469c6d904d67db589dd6209928e56504f04f6a07756Behdad Esfahbod	  info[j].mask &= ~mask_array[HALF];
4706b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod
4719da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      } while (j > start && !is_consonant (info[j]));
4729da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    }
473743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
474743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
475743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
476743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
477ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodinitial_reordering_vowel_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
478ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod				   unsigned int start, unsigned int end)
479743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
480c5306b6861cfaa50af40e8ceb058791fa06d7981Behdad Esfahbod  /* We made the vowels look like consonants.  So let's call the consonant logic! */
481c5306b6861cfaa50af40e8ceb058791fa06d7981Behdad Esfahbod  initial_reordering_consonant_syllable (map, buffer, mask_array, start, end);
482743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
483743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
484743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
485ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodinitial_reordering_standalone_cluster (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
486ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod				       unsigned int start, unsigned int end)
487743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
48818c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod  /* We treat NBSP/dotted-circle as if they are consonants, so we should just chain.
48918c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod   * Only if not in compatibility mode that is... */
49018c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod
49118c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod  if (options.uniscribe_bug_compatible)
49218c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod  {
49318c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod    /* For dotted-circle, this is what Uniscribe does:
49418c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod     * If dotted-circle is the last glyph, it just does nothing.
49518c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod     * Ie. It doesn't form Reph. */
49618c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod    if (buffer->info[end - 1].indic_category() == OT_DOTTEDCIRCLE)
49718c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod      return;
49818c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod  }
49918c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod
5001a1fa8c655a082fc1439608457ba717306cc83caBehdad Esfahbod  initial_reordering_consonant_syllable (map, buffer, mask_array, start, end);
501743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
502743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
503743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
504ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodinitial_reordering_non_indic (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
505743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		 unsigned int start, unsigned int end)
506743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
507743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Nothing to do right now.  If we ever switch to using the output
508743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * buffer in the reordering process, we'd need to next_glyph() here. */
509743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
510743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
511743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod#include "hb-ot-shape-complex-indic-machine.hh"
512743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
513743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
514743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodinitial_reordering (const hb_ot_map_t *map,
515743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_face_t *face,
516743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_buffer_t *buffer,
517743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    void *user_data HB_UNUSED)
518743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
519b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  hb_mask_t mask_array[ARRAY_LENGTH (indic_basic_features)] = {0};
520b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  unsigned int num_masks = ARRAY_LENGTH (indic_basic_features);
521b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  for (unsigned int i = 0; i < num_masks; i++)
52276f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod    mask_array[i] = map->get_1_mask (indic_basic_features[i].tag);
523743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
524743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  find_syllables (map, buffer, mask_array);
525b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod}
526b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
527743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
528eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodfinal_reordering_syllable (hb_buffer_t *buffer, hb_mask_t *mask_array,
529ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod			   unsigned int start, unsigned int end)
530743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
5314ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  hb_glyph_info_t *info = buffer->info;
5324ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
533e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod  /* 4. Final reordering:
534e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
535e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * After the localized forms and basic shaping forms GSUB features have been
536e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * applied (see below), the shaping engine performs some final glyph
537e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * reordering before applying all the remaining font features to the entire
538e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * cluster.
5394ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod   */
5404ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5414ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  /* Find base again */
5424ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  unsigned int base = end;
5434ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  for (unsigned int i = start; i < end; i++)
5444ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    if (info[i].indic_position() == POS_BASE_C) {
5454ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod      base = i;
5464ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod      break;
5474ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    }
5484ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5494ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  if (base == start) {
5504ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    /* There's no Reph, and no left Matra to reposition.  Just merge the cluster
5514ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod     * and go home. */
5524ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    buffer->merge_clusters (start, end);
5534ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    return;
5544ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  }
5554ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5564705a7026900e51f6430f03a73c87f2df035df92Behdad Esfahbod  unsigned int start_of_last_cluster = base;
5574705a7026900e51f6430f03a73c87f2df035df92Behdad Esfahbod
5584ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  /*   o Reorder matras:
559e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
560e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     If a pre-base matra character had been reordered before applying basic
561e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     features, the glyph can be moved closer to the main consonant based on
562e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     whether half-forms had been formed. Actual position for the matra is
563e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     defined as “after last standalone halant glyph, after initial matra
564e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     position and before the main consonant”. If ZWJ or ZWNJ follow this
565e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     halant, position is moved after it.
5664ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod   */
5674ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5684ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  unsigned int new_matra_pos = base - 1;
5694ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  while (new_matra_pos > start &&
5704ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod	 !(FLAG (info[new_matra_pos].indic_category()) & (FLAG (OT_M) | FLAG (OT_H))))
5714ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    new_matra_pos--;
5726782bdae3be0357da1dadc7b806a43ceefa67a90Behdad Esfahbod  /* If we found no Halant we are done.  Otherwise only proceed if the Halant does
5736782bdae3be0357da1dadc7b806a43ceefa67a90Behdad Esfahbod   * not belong to the Matra itself! */
5746782bdae3be0357da1dadc7b806a43ceefa67a90Behdad Esfahbod  if (info[new_matra_pos].indic_category() == OT_H &&
5756782bdae3be0357da1dadc7b806a43ceefa67a90Behdad Esfahbod      info[new_matra_pos].indic_position() != POS_LEFT_MATRA) {
5764ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
5774ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    if (new_matra_pos + 1 < end && is_joiner (info[new_matra_pos + 1]))
5784ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod      new_matra_pos++;
5794ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5804ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    /* Now go see if there's actually any matras... */
5814ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    for (unsigned int i = new_matra_pos; i > start; i--)
5827708ee23cbcc8c8edce13e73b6e549b77bd8c2d0Behdad Esfahbod      if (info[i - 1].indic_position () == POS_LEFT_MATRA)
5834ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod      {
5844ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod	unsigned int old_matra_pos = i - 1;
5854ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod	hb_glyph_info_t matra = info[old_matra_pos];
5864ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod	memmove (&info[old_matra_pos], &info[old_matra_pos + 1], (new_matra_pos - old_matra_pos) * sizeof (info[0]));
5874ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod	info[new_matra_pos] = matra;
5884705a7026900e51f6430f03a73c87f2df035df92Behdad Esfahbod	start_of_last_cluster = MIN (new_matra_pos, start_of_last_cluster);
5894ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod	new_matra_pos--;
5904ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod      }
5914ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  }
5924ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5934ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5944ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  /*   o Reorder reph:
595e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
596e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     Reph’s original position is always at the beginning of the syllable,
597e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     (i.e. it is not reordered at the character reordering stage). However,
598e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     it will be reordered according to the basic-forms shaping results.
599e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     Possible positions for reph, depending on the script, are; after main,
600e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     before post-base consonant forms, and after post-base consonant forms.
601dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod   */
602dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
603dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  /* If there's anything after the Ra that has the REPH pos, it ought to be halant.
604dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod   * Which means that the font has failed to ligate the Reph.  In which case, we
605dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod   * shouldn't move. */
606dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  if (start + 1 < end &&
607dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod      info[start].indic_position() == POS_RA_TO_BECOME_REPH &&
608dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod      info[start + 1].indic_position() != POS_RA_TO_BECOME_REPH)
609dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  {
61002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      unsigned int new_reph_pos;
61102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
61202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     enum reph_position_t {
613f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_AFTER_MAIN,
614f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_BEFORE_SUBSCRIPT,
615f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_AFTER_SUBSCRIPT,
616f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_BEFORE_POSTSCRIPT,
617f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_AFTER_POSTSCRIPT,
618f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod     } reph_pos;
619f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
620f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod     /* XXX Figure out old behavior too */
621f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod     switch (buffer->props.script)
622f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod     {
623f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_MALAYALAM:
624f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_ORIYA:
625f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_AFTER_MAIN;
626f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
627f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
628f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_GURMUKHI:
629f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_BEFORE_SUBSCRIPT;
630f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
631f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
632f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_BENGALI:
633f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_AFTER_SUBSCRIPT;
634f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
635f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
636f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       default:
637f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_DEVANAGARI:
638f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_GUJARATI:
639f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_BEFORE_POSTSCRIPT;
640f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
641f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
642f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_KANNADA:
643f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_TAMIL:
644f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_TELUGU:
645f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_AFTER_POSTSCRIPT;
646f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
647f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod     }
64802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
649dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod    /*       1. If reph should be positioned after post-base consonant forms,
650dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          proceed to step 5.
65102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
65202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_step_1:
65302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
65402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      if (reph_pos == REPH_AFTER_POSTSCRIPT)
65502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	goto reph_step_5;
65602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
65702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
65802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       2. If the reph repositioning class is not after post-base: target
659dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          position is after the first explicit halant glyph between the
660dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first post-reph consonant and last main consonant. If ZWJ or ZWNJ
661dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          are following this halant, position is moved after it. If such
662dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          position is found, this is the target position. Otherwise,
663dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          proceed to the next step.
664dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *
665dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          Note: in old-implementation fonts, where classifications were
666dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          fixed in shaping engine, there was no case where reph position
667dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          will be found on this step.
66802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
66902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_step_2:
67002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
67102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      new_reph_pos = start + 1;
67202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      while (new_reph_pos < base && info[new_reph_pos].indic_category() != OT_H)
67302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	new_reph_pos++;
67402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
67502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      if (new_reph_pos < base && info[new_reph_pos].indic_category() == OT_H) {
67602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	/* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */
67702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1]))
67802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	  new_reph_pos++;
67902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	goto reph_move;
68002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      }
68102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
68202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
68302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       3. If reph should be repositioned after the main consonant: find the
684dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first consonant not ligated with main, or find the first
685dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          consonant that is not a potential pre-base reordering Ra.
68602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
68702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_step_3:
68802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
68902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      /* XXX */
69002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
69102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
69202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       4. If reph should be positioned before post-base consonant, find
693dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first post-base classified consonant not ligated with main. If no
694dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          consonant is found, the target position should be before the
695dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first matra, syllable modifier sign or vedic sign.
69602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
69702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_step_4:
69802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
69902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      /* XXX */
70002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
70102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
70202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       5. If no consonant is found in steps 3 or 4, move reph to a position
703dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          immediately before the first post-base matra, syllable modifier
704dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          sign or vedic sign that has a reordering class after the intended
705dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          reph position. For example, if the reordering position for reph
706dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          is post-main, it will skip above-base matras that also have a
707dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          post-main position.
708dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     */
70902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_step_5:
71002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
71102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      /* XXX */
71202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
713dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
71402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       6. Otherwise, reorder reph to the end of the syllable.
71502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
71602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_step_6:
71702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
71802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      new_reph_pos = end - 1;
71902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      while (new_reph_pos > start && info[new_reph_pos].indic_position() == POS_SMVD)
72002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	new_reph_pos--;
72102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
722892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod      /*
723892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * If the Reph is to be ending up after a Matra,Halant sequence,
724892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * position it before that Halant so it can interact with the Matra.
725892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * However, if it's a plain Consonant,Halant we shouldn't do that.
726892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * Uniscribe doesn't do this.
727892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * TEST: U+0930,U+094D,U+0915,U+094B,U+094D
728892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       */
729892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod      if (!options.uniscribe_bug_compatible &&
730892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod	  unlikely (info[new_reph_pos].indic_category() == OT_H)) {
73102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	for (unsigned int i = base + 1; i < new_reph_pos; i++)
73202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	  if (info[i].indic_category() == OT_M) {
73302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	    /* Ok, got it. */
73402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	    new_reph_pos--;
73502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	  }
73602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      }
73702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      goto reph_move;
7388df5636968389ac7bf8620ccd091fd4872b0bbeeBehdad Esfahbod    }
7398df5636968389ac7bf8620ccd091fd4872b0bbeeBehdad Esfahbod
74002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_move:
74102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
74202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      /* Move */
74302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      hb_glyph_info_t reph = info[start];
74402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      memmove (&info[start], &info[start + 1], (new_reph_pos - start) * sizeof (info[0]));
74502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      info[new_reph_pos] = reph;
74602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      start_of_last_cluster = start; /* Yay, one big cluster! */
74702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
748dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  }
749dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
750dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
751dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  /*   o Reorder pre-base reordering consonants:
752e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
753e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     If a pre-base reordering consonant is found, reorder it according to
754e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     the following rules:
755e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
756e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       1. Only reorder a glyph produced by substitution during application
757e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          of the feature. (Note that a font may shape a Ra consonant with
758e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          the feature generally but block it in certain contexts.)
759e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
760e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       2. Try to find a target position the same way as for pre-base matra.
761e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          If it is found, reorder pre-base consonant glyph.
762e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
763e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       3. If position is not found, reorder immediately before main
764e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          consonant.
765e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   */
766e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
767eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  /* TODO */
768eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
769eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
770eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
771a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod  /* Apply 'init' to the Left Matra if it's a word start. */
772a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod  if (info[start].indic_position () == POS_LEFT_MATRA &&
773a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod      (!start ||
774a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod       !(FLAG (_hb_glyph_info_get_general_category (&buffer->info[start - 1])) &
775a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	 (FLAG (HB_UNICODE_GENERAL_CATEGORY_LOWERCASE_LETTER) |
776a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_MODIFIER_LETTER) |
777a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER) |
778a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_TITLECASE_LETTER) |
779a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_UPPERCASE_LETTER) |
780a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK) |
781a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK) |
782a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)))))
783a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod    info[start].mask |= mask_array[INIT];
784a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod
785eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
78621d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod
78721d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod  /* Finish off the clusters and go home! */
78821d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod
789ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  if (!options.uniscribe_bug_compatible)
790ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  {
79121d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    /* This is what Uniscribe does.  Ie. add cluster boundaries after Halant,ZWNJ.
79221d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod     * This means, half forms are submerged into the main consonants cluster.
79321d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod     * This is unnecessary, and makes cursor positioning harder, but that's what
79421d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod     * Uniscribe does. */
79521d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    unsigned int cluster_start = start;
79621d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    for (unsigned int i = start + 1; i < start_of_last_cluster; i++)
79721d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod      if (info[i - 1].indic_category() == OT_H && info[i].indic_category() == OT_ZWNJ) {
79821d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod        i++;
79921d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod	buffer->merge_clusters (cluster_start, i);
80021d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod	cluster_start = i;
80121d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod      }
80221d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    start_of_last_cluster = cluster_start;
80321d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod  }
80421d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod
80521d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod  buffer->merge_clusters (start_of_last_cluster, end);
806ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod}
807e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
808e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
809ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodstatic void
810ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodfinal_reordering (const hb_ot_map_t *map,
811ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod		  hb_face_t *face,
812ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod		  hb_buffer_t *buffer,
813ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod		  void *user_data HB_UNUSED)
814ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod{
815ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  unsigned int count = buffer->len;
816ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  if (!count) return;
817ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod
818eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  hb_mask_t mask_array[ARRAY_LENGTH (indic_other_features)] = {0};
819eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  unsigned int num_masks = ARRAY_LENGTH (indic_other_features);
820eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  for (unsigned int i = 0; i < num_masks; i++)
821eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod    mask_array[i] = map->get_1_mask (indic_other_features[i].tag);
822eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
823ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  hb_glyph_info_t *info = buffer->info;
824ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  unsigned int last = 0;
825cee7187447b76b22e1bb6136d137b35ac49c3a5dBehdad Esfahbod  unsigned int last_syllable = info[0].syllable();
826ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  for (unsigned int i = 1; i < count; i++)
827cee7187447b76b22e1bb6136d137b35ac49c3a5dBehdad Esfahbod    if (last_syllable != info[i].syllable()) {
828eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod      final_reordering_syllable (buffer, mask_array, last, i);
829ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod      last = i;
830cee7187447b76b22e1bb6136d137b35ac49c3a5dBehdad Esfahbod      last_syllable = info[last].syllable();
831ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod    }
832eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  final_reordering_syllable (buffer, mask_array, last, count);
833e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
834743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category);
835743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position);
836743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
837743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
838743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
839b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
840