hb-ot-shape-complex-indic.cc revision 617f4ac46f1084859d2034c08760e31e52d3bec3
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
1569f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod_hb_ot_shape_complex_collect_features_indic (hb_ot_map_builder_t *map,
1579f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod					     const hb_segment_properties_t *props HB_UNUSED)
158b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
159f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_bool_feature (HB_TAG('l','o','c','l'));
160a54a5505a35eef5315a8e2e7a79502901e3eff5fBehdad Esfahbod  /* The Indic specs do not require ccmp, but we apply it here since if
161a54a5505a35eef5315a8e2e7a79502901e3eff5fBehdad Esfahbod   * there is a use of it, it's typically at the beginning. */
162f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_bool_feature (HB_TAG('c','c','m','p'));
163f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
164743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  map->add_gsub_pause (initial_reordering, NULL);
165f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
166412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (indic_basic_features); i++) {
16776f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod    map->add_bool_feature (indic_basic_features[i].tag, indic_basic_features[i].is_global);
168412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod    map->add_gsub_pause (NULL, NULL);
169412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  }
170b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
171f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_gsub_pause (final_reordering, NULL);
172f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
173412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (indic_other_features); i++) {
174eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod    map->add_bool_feature (indic_other_features[i].tag, indic_other_features[i].is_global);
175412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod    map->add_gsub_pause (NULL, NULL);
176412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  }
177b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod}
178b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
179867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
18011138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbodhb_ot_shape_normalization_mode_t
18111138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbod_hb_ot_shape_complex_normalization_preference_indic (void)
18202cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod{
18302cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod  /* We want split matras decomposed by the common shaping logic. */
18411138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbod  return HB_OT_SHAPE_NORMALIZATION_MODE_DECOMPOSED;
18502cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod}
18602cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod
187867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
188b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbodvoid
1899f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod_hb_ot_shape_complex_setup_masks_indic (hb_ot_map_t *map HB_UNUSED,
1909f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod					hb_buffer_t *buffer,
1913f18236a03880c0960f5990dc90685f6146951a6Behdad Esfahbod					hb_font_t *font HB_UNUSED)
192b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
1939f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod  HB_BUFFER_ALLOCATE_VAR (buffer, indic_category);
1949f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod  HB_BUFFER_ALLOCATE_VAR (buffer, indic_position);
1959f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod
196743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* We cannot setup masks here.  We save information about characters
197743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * and setup masks later on in a pause-callback. */
198743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
199743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  unsigned int count = buffer->len;
200b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  for (unsigned int i = 0; i < count; i++)
201b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  {
20292332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    hb_glyph_info_t &info = buffer->info[i];
20392332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    unsigned int type = get_indic_categories (info.codepoint);
20492332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod
20592332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    info.indic_category() = type & 0x0F;
20692332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    info.indic_position() = type >> 4;
20792332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod
2083399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod    /* The spec says U+0952 is OT_A.  However, testing shows that Uniscribe
2093399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * treats U+0951..U+0952 all as OT_VD.
2103399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * TESTS:
2113399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * U+092E,U+0947,U+0952
2123399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * U+092E,U+0952,U+0947
2133399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * U+092E,U+0947,U+0951
2143399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * U+092E,U+0951,U+0947
2153399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * */
2163399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod    if (unlikely (hb_in_range<hb_codepoint_t> (info.codepoint, 0x0951, 0x0954)))
2173399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod      info.indic_category() = OT_VD;
2183399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod
21992332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    if (info.indic_category() == OT_C) {
22092332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_position() = consonant_position (info.codepoint);
22192332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      if (is_ra (info.codepoint))
22292332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod	info.indic_category() = OT_Ra;
22392332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    } else if (info.indic_category() == OT_SM ||
22492332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod	       info.indic_category() == OT_VD) {
22592332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_position() = POS_SMVD;
22692332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    } else if (unlikely (info.codepoint == 0x200C))
22792332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_category() = OT_ZWNJ;
22892332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    else if (unlikely (info.codepoint == 0x200D))
22992332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_category() = OT_ZWJ;
23018c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod    else if (unlikely (info.codepoint == 0x25CC))
23118c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod      info.indic_category() = OT_DOTTEDCIRCLE;
232b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  }
233743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
234b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
23545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodstatic int
23645d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodcompare_indic_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
23745d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod{
23845d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  int a = pa->indic_position();
23945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  int b = pb->indic_position();
24045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
24145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  return a < b ? -1 : a == b ? 0 : +1;
24245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod}
243867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
2447ea58db311bfb0d8f804d1e9f4a1f004bd45075aBehdad Esfahbod/* Rules from:
2457ea58db311bfb0d8f804d1e9f4a1f004bd45075aBehdad Esfahbod * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */
2467ea58db311bfb0d8f804d1e9f4a1f004bd45075aBehdad Esfahbod
247743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
248ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodinitial_reordering_consonant_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
249ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod				       unsigned int start, unsigned int end)
250743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
251ee58f3bc75d2d071a71b94063bf12205a5871acbBehdad Esfahbod  hb_glyph_info_t *info = buffer->info;
252743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
253617f4ac46f1084859d2034c08760e31e52d3bec3Behdad 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  {
272617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
273617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod     *    and has more than one consonant, Ra is excluded from candidates for
274617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod     *    base consonants. */
275617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    unsigned int limit = start;
276617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    if (mask_array[RPHF] &&
277617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod	start + 3 <= end &&
278617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod	info[start].indic_category() == OT_Ra &&
279617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod	info[start + 1].indic_category() == OT_H &&
280617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod	!is_joiner (info[start + 2]))
281617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    {
282617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod      limit += 2;
283617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod      base = start;
284617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod      has_reph = true;
285617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    };
28676b3409de6887c1cdd5c679939497b1b56f4554bBehdad 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... */
318743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
319617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod
320617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
321617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod     *    and has more than one consonant, Ra is excluded from candidates for
322617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod     *    base consonants. */
323617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    if (has_reph && base == start) {
324617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod      /* Have no other consonant, so Reph is not formed and Ra becomes base. */
325617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod      has_reph = false;
326617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    }
3275e4e21fce4b548b0b8a5951bc8f35a9f27428192Behdad Esfahbod  }
3283d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod
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 */
3695e4e21fce4b548b0b8a5951bc8f35a9f27428192Behdad Esfahbod  if (has_reph)
370dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod    info[start].indic_position() = POS_RA_TO_BECOME_REPH;
371fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod
372f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  /* For old-style Indic script tags, move the first post-base Halant after
373f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod   * last consonant. */
374f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  if ((map->get_chosen_script (0) & 0x000000FF) != '2') {
375f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod    /* We should only do this for Indic scripts which have a version two I guess. */
3763c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = base + 1; i < end; i++)
377f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod      if (info[i].indic_category() == OT_H) {
378f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        unsigned int j;
379f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        for (j = end - 1; j > i; j--)
380190eb31a16178269aecaf5d2ecc9012f956749f4Behdad Esfahbod	  if (is_consonant (info[j]))
381f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	    break;
382f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	if (j > i) {
383f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  /* Move Halant to after last consonant. */
384f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  hb_glyph_info_t t = info[i];
385f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  memmove (&info[i], &info[i + 1], (j - i) * sizeof (info[0]));
386f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  info[j] = t;
387f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	}
388f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        break;
389f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod      }
390f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  }
391f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod
39245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* Attach ZWJ, ZWNJ, nukta, and halant to previous char to move with them. */
393ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  if (!options.uniscribe_bug_compatible)
394ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  {
395ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    /* Please update the Uniscribe branch when touching this! */
396ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    for (unsigned int i = start + 1; i < end; i++)
397ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod      if ((FLAG (info[i].indic_category()) & (FLAG (OT_ZWNJ) | FLAG (OT_ZWJ) | FLAG (OT_N) | FLAG (OT_H))))
398ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	info[i].indic_position() = info[i - 1].indic_position();
399ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  } else {
40067ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod    /*
40167ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod     * Uniscribe doesn't move the Halant with Left Matra.
40267ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod     * TEST: U+092B,U+093F,U+094DE
40367ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod     */
404ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    /* Please update the non-Uniscribe branch when touching this! */
405ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    for (unsigned int i = start + 1; i < end; i++)
406ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod      if ((FLAG (info[i].indic_category()) & (FLAG (OT_ZWNJ) | FLAG (OT_ZWJ) | FLAG (OT_N) | FLAG (OT_H)))) {
407ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	info[i].indic_position() = info[i - 1].indic_position();
4086a091df9b403b147ef78f3974610dedf4ce1e08aBehdad Esfahbod	if (info[i].indic_category() == OT_H && info[i].indic_position() == POS_PRE_M)
409ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	  for (unsigned int j = i; j > start; j--)
4106a091df9b403b147ef78f3974610dedf4ce1e08aBehdad Esfahbod	    if (info[j - 1].indic_position() != POS_PRE_M) {
411ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	      info[i].indic_position() = info[j - 1].indic_position();
412ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	      break;
413ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	    }
414ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod      }
415ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  }
41645d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
41745d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* We do bubble-sort, skip malicious clusters attempts */
418b99d63ae114fb58f129562b293a8a66543d499adBehdad Esfahbod  if (end - start < 64)
419a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod  {
420a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    /* Sit tight, rock 'n roll! */
421d3637edb248162970e202e9d0671540274192844Behdad Esfahbod    hb_bubble_sort (info + start, end - start, compare_indic_order);
422a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    /* Find base again */
423a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    base = end;
4243c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = start; i < end; i++)
425a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod      if (info[i].indic_position() == POS_BASE_C) {
426a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod        base = i;
427a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod	break;
428a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod      }
429a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod  }
43045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
431743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Setup masks now */
432743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
433281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod  {
434281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    hb_mask_t mask;
435281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod
436dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod    /* Reph */
437668c6046c1b3af3bd316bda0cc8636f2a5e8df42Behdad Esfahbod    for (unsigned int i = start; i < end && info[i].indic_position() == POS_RA_TO_BECOME_REPH; i++)
438668c6046c1b3af3bd316bda0cc8636f2a5e8df42Behdad Esfahbod      info[i].mask |= mask_array[RPHF];
439dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
440281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Pre-base */
441281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    mask = mask_array[HALF] | mask_array[AKHN] | mask_array[CJCT];
4423c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = start; i < base; i++)
443281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod      info[i].mask  |= mask;
444281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Base */
445281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    mask = mask_array[AKHN] | mask_array[CJCT];
446281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    info[base].mask |= mask;
447281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Post-base */
448281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    mask = mask_array[BLWF] | mask_array[PSTF] | mask_array[CJCT];
4493c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = base + 1; i < end; i++)
450281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod      info[i].mask  |= mask;
451281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod  }
4529da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
4539da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  /* Apply ZWJ/ZWNJ effects */
4543c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod  for (unsigned int i = start + 1; i < end; i++)
4559da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    if (is_joiner (info[i])) {
4569da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      bool non_joiner = info[i].indic_category() == OT_ZWNJ;
4576b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod      unsigned int j = i;
4589da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
4599da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      do {
4609da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod	j--;
4616b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod
4623bf27a9f0e92aa31b464bd3b9fdea5933c9ae8b1Behdad Esfahbod	info[j].mask &= ~mask_array[CJCT];
4636b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod	if (non_joiner)
464c6d904d67db589dd6209928e56504f04f6a07756Behdad Esfahbod	  info[j].mask &= ~mask_array[HALF];
4656b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod
4669da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      } while (j > start && !is_consonant (info[j]));
4679da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    }
468743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
469743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
470743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
471743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
4729f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbodinitial_reordering_vowel_syllable (const hb_ot_map_t *map,
4739f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod				   hb_buffer_t *buffer,
4749f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod				   hb_mask_t *mask_array,
475ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod				   unsigned int start, unsigned int end)
476743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
477c5306b6861cfaa50af40e8ceb058791fa06d7981Behdad Esfahbod  /* We made the vowels look like consonants.  So let's call the consonant logic! */
478c5306b6861cfaa50af40e8ceb058791fa06d7981Behdad Esfahbod  initial_reordering_consonant_syllable (map, buffer, mask_array, start, end);
479743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
480743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
481743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
4829f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbodinitial_reordering_standalone_cluster (const hb_ot_map_t *map,
4839f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod				       hb_buffer_t *buffer,
4849f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod				       hb_mask_t *mask_array,
485ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod				       unsigned int start, unsigned int end)
486743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
48718c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod  /* We treat NBSP/dotted-circle as if they are consonants, so we should just chain.
48818c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod   * Only if not in compatibility mode that is... */
48918c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod
49018c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod  if (options.uniscribe_bug_compatible)
49118c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod  {
49218c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod    /* For dotted-circle, this is what Uniscribe does:
49318c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod     * If dotted-circle is the last glyph, it just does nothing.
49418c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod     * Ie. It doesn't form Reph. */
49518c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod    if (buffer->info[end - 1].indic_category() == OT_DOTTEDCIRCLE)
49618c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod      return;
49718c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod  }
49818c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod
4991a1fa8c655a082fc1439608457ba717306cc83caBehdad Esfahbod  initial_reordering_consonant_syllable (map, buffer, mask_array, start, end);
500743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
501743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
502743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
5039f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbodinitial_reordering_non_indic (const hb_ot_map_t *map HB_UNUSED,
5049f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod			      hb_buffer_t *buffer HB_UNUSED,
5059f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod			      hb_mask_t *mask_array HB_UNUSED,
5063f18236a03880c0960f5990dc90685f6146951a6Behdad Esfahbod			      unsigned int start HB_UNUSED, unsigned int end HB_UNUSED)
507743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
508743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Nothing to do right now.  If we ever switch to using the output
509743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * buffer in the reordering process, we'd need to next_glyph() here. */
510743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
511743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
512743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod#include "hb-ot-shape-complex-indic-machine.hh"
513743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
514743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
515743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodinitial_reordering (const hb_ot_map_t *map,
5163f18236a03880c0960f5990dc90685f6146951a6Behdad Esfahbod		    hb_face_t *face HB_UNUSED,
517743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_buffer_t *buffer,
518743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    void *user_data HB_UNUSED)
519743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
520b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  hb_mask_t mask_array[ARRAY_LENGTH (indic_basic_features)] = {0};
521b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  unsigned int num_masks = ARRAY_LENGTH (indic_basic_features);
522b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  for (unsigned int i = 0; i < num_masks; i++)
52376f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod    mask_array[i] = map->get_1_mask (indic_basic_features[i].tag);
524743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
525743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  find_syllables (map, buffer, mask_array);
526b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod}
527b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
528743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
529eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodfinal_reordering_syllable (hb_buffer_t *buffer, hb_mask_t *mask_array,
530ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod			   unsigned int start, unsigned int end)
531743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
5324ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  hb_glyph_info_t *info = buffer->info;
5334ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
534e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod  /* 4. Final reordering:
535e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
536e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * After the localized forms and basic shaping forms GSUB features have been
537e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * applied (see below), the shaping engine performs some final glyph
538e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * reordering before applying all the remaining font features to the entire
539e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * cluster.
5404ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod   */
5414ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5424ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  /* Find base again */
5434ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  unsigned int base = end;
5444ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  for (unsigned int i = start; i < end; i++)
5454ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    if (info[i].indic_position() == POS_BASE_C) {
5464ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod      base = i;
5474ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod      break;
5484ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    }
5494ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5504ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  if (base == start) {
5514ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    /* There's no Reph, and no left Matra to reposition.  Just merge the cluster
5524ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod     * and go home. */
5534ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    buffer->merge_clusters (start, end);
5544ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    return;
5554ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  }
5564ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5574705a7026900e51f6430f03a73c87f2df035df92Behdad Esfahbod  unsigned int start_of_last_cluster = base;
5584705a7026900e51f6430f03a73c87f2df035df92Behdad Esfahbod
5594ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  /*   o Reorder matras:
560e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
561e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     If a pre-base matra character had been reordered before applying basic
562e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     features, the glyph can be moved closer to the main consonant based on
563e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     whether half-forms had been formed. Actual position for the matra is
564e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     defined as “after last standalone halant glyph, after initial matra
565e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     position and before the main consonant”. If ZWJ or ZWNJ follow this
566e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     halant, position is moved after it.
5674ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod   */
5684ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5699d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod  {
5709d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    unsigned int new_matra_pos = base - 1;
5719d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    while (new_matra_pos > start &&
5729d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod	   !(FLAG (info[new_matra_pos].indic_category()) & (FLAG (OT_M) | FLAG (OT_H))))
5739d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      new_matra_pos--;
5749d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    /* If we found no Halant we are done.  Otherwise only proceed if the Halant does
5759d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod     * not belong to the Matra itself! */
5769d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    if (info[new_matra_pos].indic_category() == OT_H &&
5776a091df9b403b147ef78f3974610dedf4ce1e08aBehdad Esfahbod	info[new_matra_pos].indic_position() != POS_PRE_M) {
5789d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
5799d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      if (new_matra_pos + 1 < end && is_joiner (info[new_matra_pos + 1]))
5809d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod	new_matra_pos++;
5819d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod
5829d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      /* Now go see if there's actually any matras... */
5839d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      for (unsigned int i = new_matra_pos; i > start; i--)
5846a091df9b403b147ef78f3974610dedf4ce1e08aBehdad Esfahbod	if (info[i - 1].indic_position () == POS_PRE_M)
5859d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod	{
5869d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod	  unsigned int old_matra_pos = i - 1;
5879d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod	  hb_glyph_info_t matra = info[old_matra_pos];
5889d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod	  memmove (&info[old_matra_pos], &info[old_matra_pos + 1], (new_matra_pos - old_matra_pos) * sizeof (info[0]));
5899d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod	  info[new_matra_pos] = matra;
5909d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod	  start_of_last_cluster = MIN (new_matra_pos, start_of_last_cluster);
5919d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod	  new_matra_pos--;
5929d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod	}
5939d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    }
5944ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  }
5954ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5964ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5974ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  /*   o Reorder reph:
598e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
599e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     Reph’s original position is always at the beginning of the syllable,
600e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     (i.e. it is not reordered at the character reordering stage). However,
601e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     it will be reordered according to the basic-forms shaping results.
602e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     Possible positions for reph, depending on the script, are; after main,
603e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     before post-base consonant forms, and after post-base consonant forms.
604dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod   */
605dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
606dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  /* If there's anything after the Ra that has the REPH pos, it ought to be halant.
607dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod   * Which means that the font has failed to ligate the Reph.  In which case, we
608dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod   * shouldn't move. */
609dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  if (start + 1 < end &&
610dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod      info[start].indic_position() == POS_RA_TO_BECOME_REPH &&
611dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod      info[start + 1].indic_position() != POS_RA_TO_BECOME_REPH)
612dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  {
61302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      unsigned int new_reph_pos;
61402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
61502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     enum reph_position_t {
616f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_AFTER_MAIN,
617f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_BEFORE_SUBSCRIPT,
618f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_AFTER_SUBSCRIPT,
619f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_BEFORE_POSTSCRIPT,
620f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_AFTER_POSTSCRIPT,
621f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod     } reph_pos;
622f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
623f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod     /* XXX Figure out old behavior too */
6247f852b644b8143492a02edfc853114aaa23446bdBehdad Esfahbod     switch ((hb_tag_t) buffer->props.script)
625f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod     {
626f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_MALAYALAM:
627f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_ORIYA:
628f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_AFTER_MAIN;
629f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
630f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
631f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_GURMUKHI:
632f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_BEFORE_SUBSCRIPT;
633f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
634f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
635f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_BENGALI:
636f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_AFTER_SUBSCRIPT;
637f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
638f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
639f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       default:
640f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_DEVANAGARI:
641f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_GUJARATI:
642f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_BEFORE_POSTSCRIPT;
643f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
644f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
645f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_KANNADA:
646f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_TAMIL:
647f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_TELUGU:
648f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_AFTER_POSTSCRIPT;
649f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
650f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod     }
65102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
652dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod    /*       1. If reph should be positioned after post-base consonant forms,
653dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          proceed to step 5.
65402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
6559d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    if (reph_pos == REPH_AFTER_POSTSCRIPT)
65602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
6579d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      goto reph_step_5;
65802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
65902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
66002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       2. If the reph repositioning class is not after post-base: target
661dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          position is after the first explicit halant glyph between the
662dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first post-reph consonant and last main consonant. If ZWJ or ZWNJ
663dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          are following this halant, position is moved after it. If such
664dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          position is found, this is the target position. Otherwise,
665dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          proceed to the next step.
666dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *
667dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          Note: in old-implementation fonts, where classifications were
668dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          fixed in shaping engine, there was no case where reph position
669dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          will be found on this step.
67002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
67102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
67202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      new_reph_pos = start + 1;
67302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      while (new_reph_pos < base && info[new_reph_pos].indic_category() != OT_H)
67402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	new_reph_pos++;
67502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
67602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      if (new_reph_pos < base && info[new_reph_pos].indic_category() == OT_H) {
67702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	/* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */
67802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1]))
67902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	  new_reph_pos++;
68002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	goto reph_move;
68102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      }
68202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
68302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
68402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       3. If reph should be repositioned after the main consonant: find the
685dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first consonant not ligated with main, or find the first
686dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          consonant that is not a potential pre-base reordering Ra.
68702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
6889d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    if (reph_pos == REPH_AFTER_MAIN)
68902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
69002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      /* XXX */
69102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
69202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
69302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       4. If reph should be positioned before post-base consonant, find
694dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first post-base classified consonant not ligated with main. If no
695dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          consonant is found, the target position should be before the
696dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first matra, syllable modifier sign or vedic sign.
69702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
6989d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    /* This is our take on what step 4 is trying to say (and failing, BADLY). */
6999d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    if (reph_pos == REPH_AFTER_SUBSCRIPT)
70002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
7019d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      new_reph_pos = base;
7029d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      while (new_reph_pos < end &&
7036a091df9b403b147ef78f3974610dedf4ce1e08aBehdad Esfahbod	     !( FLAG (info[new_reph_pos + 1].indic_position()) & (FLAG (POS_POST_C) | FLAG (POS_POST_M) | FLAG (POS_SMVD))))
7049d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod	new_reph_pos++;
7059d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      if (new_reph_pos < end)
7069d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod        goto reph_move;
70702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
70802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
70902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       5. If no consonant is found in steps 3 or 4, move reph to a position
710dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          immediately before the first post-base matra, syllable modifier
711dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          sign or vedic sign that has a reordering class after the intended
712dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          reph position. For example, if the reordering position for reph
713dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          is post-main, it will skip above-base matras that also have a
714dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          post-main position.
715dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     */
71602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_step_5:
71702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
71802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      /* XXX */
71902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
720dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
72102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       6. Otherwise, reorder reph to the end of the syllable.
72202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
72302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
72402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      new_reph_pos = end - 1;
72502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      while (new_reph_pos > start && info[new_reph_pos].indic_position() == POS_SMVD)
72602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	new_reph_pos--;
72702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
728892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod      /*
729892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * If the Reph is to be ending up after a Matra,Halant sequence,
730892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * position it before that Halant so it can interact with the Matra.
731892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * However, if it's a plain Consonant,Halant we shouldn't do that.
732892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * Uniscribe doesn't do this.
733892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * TEST: U+0930,U+094D,U+0915,U+094B,U+094D
734892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       */
735892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod      if (!options.uniscribe_bug_compatible &&
736892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod	  unlikely (info[new_reph_pos].indic_category() == OT_H)) {
73702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	for (unsigned int i = base + 1; i < new_reph_pos; i++)
73802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	  if (info[i].indic_category() == OT_M) {
73902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	    /* Ok, got it. */
74002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	    new_reph_pos--;
74102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	  }
74202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      }
74302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      goto reph_move;
7448df5636968389ac7bf8620ccd091fd4872b0bbeeBehdad Esfahbod    }
7458df5636968389ac7bf8620ccd091fd4872b0bbeeBehdad Esfahbod
74602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_move:
74702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
74802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      /* Move */
74902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      hb_glyph_info_t reph = info[start];
75002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      memmove (&info[start], &info[start + 1], (new_reph_pos - start) * sizeof (info[0]));
75102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      info[new_reph_pos] = reph;
75202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      start_of_last_cluster = start; /* Yay, one big cluster! */
75302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
754dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  }
755dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
756dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
757dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  /*   o Reorder pre-base reordering consonants:
758e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
759e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     If a pre-base reordering consonant is found, reorder it according to
760e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     the following rules:
761e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
762e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       1. Only reorder a glyph produced by substitution during application
763e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          of the feature. (Note that a font may shape a Ra consonant with
764e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          the feature generally but block it in certain contexts.)
765e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
766e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       2. Try to find a target position the same way as for pre-base matra.
767e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          If it is found, reorder pre-base consonant glyph.
768e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
769e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       3. If position is not found, reorder immediately before main
770e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          consonant.
771e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   */
772e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
773eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  /* TODO */
774eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
775eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
776eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
777a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod  /* Apply 'init' to the Left Matra if it's a word start. */
7786a091df9b403b147ef78f3974610dedf4ce1e08aBehdad Esfahbod  if (info[start].indic_position () == POS_PRE_M &&
779a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod      (!start ||
780eace47b173807d94b29a6490d0bc3c9f8f6168d1Behdad Esfahbod       !(FLAG (_hb_glyph_info_get_general_category (&info[start - 1])) &
781a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	 (FLAG (HB_UNICODE_GENERAL_CATEGORY_LOWERCASE_LETTER) |
782a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_MODIFIER_LETTER) |
783a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER) |
784a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_TITLECASE_LETTER) |
785a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_UPPERCASE_LETTER) |
786a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK) |
787a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK) |
788a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)))))
789a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod    info[start].mask |= mask_array[INIT];
790a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod
791eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
79221d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod
79321d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod  /* Finish off the clusters and go home! */
79421d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod
795ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  if (!options.uniscribe_bug_compatible)
796ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  {
79721d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    /* This is what Uniscribe does.  Ie. add cluster boundaries after Halant,ZWNJ.
79821d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod     * This means, half forms are submerged into the main consonants cluster.
79921d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod     * This is unnecessary, and makes cursor positioning harder, but that's what
80021d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod     * Uniscribe does. */
80121d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    unsigned int cluster_start = start;
80221d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    for (unsigned int i = start + 1; i < start_of_last_cluster; i++)
80321d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod      if (info[i - 1].indic_category() == OT_H && info[i].indic_category() == OT_ZWNJ) {
80421d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod        i++;
80521d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod	buffer->merge_clusters (cluster_start, i);
80621d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod	cluster_start = i;
80721d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod      }
80821d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    start_of_last_cluster = cluster_start;
80921d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod  }
81021d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod
81121d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod  buffer->merge_clusters (start_of_last_cluster, end);
812ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod}
813e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
814e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
815ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodstatic void
816ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodfinal_reordering (const hb_ot_map_t *map,
8173f18236a03880c0960f5990dc90685f6146951a6Behdad Esfahbod		  hb_face_t *face HB_UNUSED,
818ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod		  hb_buffer_t *buffer,
819ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod		  void *user_data HB_UNUSED)
820ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod{
821ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  unsigned int count = buffer->len;
822ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  if (!count) return;
823ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod
824eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  hb_mask_t mask_array[ARRAY_LENGTH (indic_other_features)] = {0};
825eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  unsigned int num_masks = ARRAY_LENGTH (indic_other_features);
826eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  for (unsigned int i = 0; i < num_masks; i++)
827eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod    mask_array[i] = map->get_1_mask (indic_other_features[i].tag);
828eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
829ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  hb_glyph_info_t *info = buffer->info;
830ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  unsigned int last = 0;
831cee7187447b76b22e1bb6136d137b35ac49c3a5dBehdad Esfahbod  unsigned int last_syllable = info[0].syllable();
832ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  for (unsigned int i = 1; i < count; i++)
833cee7187447b76b22e1bb6136d137b35ac49c3a5dBehdad Esfahbod    if (last_syllable != info[i].syllable()) {
834eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod      final_reordering_syllable (buffer, mask_array, last, i);
835ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod      last = i;
836cee7187447b76b22e1bb6136d137b35ac49c3a5dBehdad Esfahbod      last_syllable = info[last].syllable();
837ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod    }
838eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  final_reordering_syllable (buffer, mask_array, last, count);
839e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
840743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category);
841743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position);
842743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
843743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
844743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
845b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
846