hb-ot-shape-complex-indic.cc revision eed903b1644e087178438959664a6a57bebc398b
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"
28352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod
29ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbodstatic const struct indic_options_t
30ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod{
31ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  indic_options_t (void)
32ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  {
33ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    char *c = getenv ("HB_OT_INDIC_OPTIONS");
34ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    uniscribe_bug_compatible = c && strstr (c, "uniscribe-bug-compatible");
35ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  }
36ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod
37ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  bool uniscribe_bug_compatible;
38ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod} options;
39ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod
40743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic int
41743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodcompare_codepoint (const void *pa, const void *pb)
42743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
43743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  hb_codepoint_t a = * (hb_codepoint_t *) pa;
44743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  hb_codepoint_t b = * (hb_codepoint_t *) pb;
45743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
46743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  return a < b ? -1 : a == b ? 0 : +1;
47743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
48743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
49743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic indic_position_t
50743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodconsonant_position (hb_codepoint_t u)
51743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
52743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  consonant_position_t *record;
53743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
54743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  record = (consonant_position_t *) bsearch (&u, consonant_positions,
55743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod					     ARRAY_LENGTH (consonant_positions),
56743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod					     sizeof (consonant_positions[0]),
57743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod					     compare_codepoint);
58743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
59dbccf87eef0d26838fa4bb3ae26410f6c4818836Behdad Esfahbod  return record ? record->position : POS_BASE_C;
60743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
61743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
62352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbodstatic bool
63352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbodis_ra (hb_codepoint_t u)
64352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod{
65352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod  return !!bsearch (&u, ra_chars,
66352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod		    ARRAY_LENGTH (ra_chars),
67352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod		    sizeof (ra_chars[0]),
68352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod		    compare_codepoint);
69352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod}
70352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod
719ee27a928a989c71923cef82a9e9828f8e9ca051Behdad Esfahbodstatic bool
729da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbodis_joiner (const hb_glyph_info_t &info)
739ee27a928a989c71923cef82a9e9828f8e9ca051Behdad Esfahbod{
749da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  return !!(FLAG (info.indic_category()) & (FLAG (OT_ZWJ) | FLAG (OT_ZWNJ)));
759da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod}
769da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
779da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbodstatic bool
789da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbodis_consonant (const hb_glyph_info_t &info)
799da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod{
801a1fa8c655a082fc1439608457ba717306cc83caBehdad Esfahbod  /* Note:
811a1fa8c655a082fc1439608457ba717306cc83caBehdad Esfahbod   *
8218c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod   * We treat Vowels and placeholders as if they were consonants.  This is safe because Vowels
83c5306b6861cfaa50af40e8ceb058791fa06d7981Behdad Esfahbod   * cannot happen in a consonant syllable.  The plus side however is, we can call the
84c5306b6861cfaa50af40e8ceb058791fa06d7981Behdad Esfahbod   * consonant syllable logic from the vowel syllable function and get it all right! */
8518c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod  return !!(FLAG (info.indic_category()) & (FLAG (OT_C) | FLAG (OT_Ra) | FLAG (OT_V) | FLAG (OT_NBSP) | FLAG (OT_DOTTEDCIRCLE)));
869ee27a928a989c71923cef82a9e9828f8e9ca051Behdad Esfahbod}
87c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod
88eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodstruct feature_list_t {
89c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  hb_tag_t tag;
90c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  hb_bool_t is_global;
91eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod};
92eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
93eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodstatic const feature_list_t
94eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodindic_basic_features[] =
95b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
96c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('n','u','k','t'), true},
97c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('a','k','h','n'), false},
98c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('r','p','h','f'), false},
991ac075b227090a9ad930dcc1670236c176b27067Behdad Esfahbod  {HB_TAG('r','k','r','f'), true},
100c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('p','r','e','f'), false},
101c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('b','l','w','f'), false},
102c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('h','a','l','f'), false},
103c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('v','a','t','u'), true},
104c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('p','s','t','f'), false},
1059da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  {HB_TAG('c','j','c','t'), false},
106c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod};
107c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod
108c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod/* Same order as the indic_basic_features array */
109c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbodenum {
110c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  _NUKT,
111c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  AKHN,
112c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  RPHF,
113df6d45c693c417bf311e6fa49f18a8558542e525Behdad Esfahbod  _RKRF,
114c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  PREF,
115c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  BLWF,
116c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  HALF,
117c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  _VATU,
118c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  PSTF,
119e8eedf2687f05372bf5476e84139d01ba67c9f73Behdad Esfahbod  CJCT
120b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod};
121b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
122eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodstatic const feature_list_t
123eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodindic_other_features[] =
124b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
125eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('i','n','i','t'), false},
126eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('p','r','e','s'), true},
127eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('a','b','v','s'), true},
128eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('b','l','w','s'), true},
129eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('p','s','t','s'), true},
130eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('h','a','l','n'), true},
131eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
132eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('d','i','s','t'), true},
133eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('a','b','v','m'), true},
134eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('b','l','w','m'), true},
135eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod};
136eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
137eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod/* Same order as the indic_other_features array */
138eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodenum {
139eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  INIT
140b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod};
141b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
142743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
143743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
144743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodinitial_reordering (const hb_ot_map_t *map,
145743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_face_t *face,
146743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_buffer_t *buffer,
147743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    void *user_data HB_UNUSED);
148f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbodstatic void
149f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbodfinal_reordering (const hb_ot_map_t *map,
150f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod		  hb_face_t *face,
151f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod		  hb_buffer_t *buffer,
152743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		  void *user_data HB_UNUSED);
153b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
154b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbodvoid
15576f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod_hb_ot_shape_complex_collect_features_indic (hb_ot_map_builder_t *map, const hb_segment_properties_t  *props)
156b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
157f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_bool_feature (HB_TAG('l','o','c','l'));
158a54a5505a35eef5315a8e2e7a79502901e3eff5fBehdad Esfahbod  /* The Indic specs do not require ccmp, but we apply it here since if
159a54a5505a35eef5315a8e2e7a79502901e3eff5fBehdad Esfahbod   * there is a use of it, it's typically at the beginning. */
160f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_bool_feature (HB_TAG('c','c','m','p'));
161f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
162743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  map->add_gsub_pause (initial_reordering, NULL);
163f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
164412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (indic_basic_features); i++) {
16576f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod    map->add_bool_feature (indic_basic_features[i].tag, indic_basic_features[i].is_global);
166412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod    map->add_gsub_pause (NULL, NULL);
167412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  }
168b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
169f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_gsub_pause (final_reordering, NULL);
170f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
171412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (indic_other_features); i++) {
172eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod    map->add_bool_feature (indic_other_features[i].tag, indic_other_features[i].is_global);
173412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod    map->add_gsub_pause (NULL, NULL);
174412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  }
175b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod}
176b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
177867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
17811138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbodhb_ot_shape_normalization_mode_t
17911138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbod_hb_ot_shape_complex_normalization_preference_indic (void)
18002cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod{
18102cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod  /* We want split matras decomposed by the common shaping logic. */
18211138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbod  return HB_OT_SHAPE_NORMALIZATION_MODE_DECOMPOSED;
18302cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod}
18402cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod
185867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
186b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbodvoid
187acd88e659fdb2fa1cbf7171f0a1c7fbc81b9f298Behdad Esfahbod_hb_ot_shape_complex_setup_masks_indic (hb_ot_map_t *map, hb_buffer_t *buffer, hb_font_t *font)
188b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
1899f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod  HB_BUFFER_ALLOCATE_VAR (buffer, indic_category);
1909f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod  HB_BUFFER_ALLOCATE_VAR (buffer, indic_position);
1919f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod
192743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* We cannot setup masks here.  We save information about characters
193743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * and setup masks later on in a pause-callback. */
194743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
195743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  unsigned int count = buffer->len;
196b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  for (unsigned int i = 0; i < count; i++)
197b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  {
19892332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    hb_glyph_info_t &info = buffer->info[i];
19992332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    unsigned int type = get_indic_categories (info.codepoint);
20092332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod
20192332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    info.indic_category() = type & 0x0F;
20292332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    info.indic_position() = type >> 4;
20392332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod
2043399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod    /* The spec says U+0952 is OT_A.  However, testing shows that Uniscribe
2053399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * treats U+0951..U+0952 all as OT_VD.
2063399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * TESTS:
2073399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * U+092E,U+0947,U+0952
2083399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * U+092E,U+0952,U+0947
2093399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * U+092E,U+0947,U+0951
2103399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * U+092E,U+0951,U+0947
2113399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * */
2123399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod    if (unlikely (hb_in_range<hb_codepoint_t> (info.codepoint, 0x0951, 0x0954)))
2133399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod      info.indic_category() = OT_VD;
2143399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod
21592332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    if (info.indic_category() == OT_C) {
21692332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_position() = consonant_position (info.codepoint);
21792332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      if (is_ra (info.codepoint))
21892332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod	info.indic_category() = OT_Ra;
21992332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    } else if (info.indic_category() == OT_SM ||
22092332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod	       info.indic_category() == OT_VD) {
22192332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_position() = POS_SMVD;
22292332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    } else if (unlikely (info.codepoint == 0x200C))
22392332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_category() = OT_ZWNJ;
22492332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    else if (unlikely (info.codepoint == 0x200D))
22592332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_category() = OT_ZWJ;
22618c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod    else if (unlikely (info.codepoint == 0x25CC))
22718c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod      info.indic_category() = OT_DOTTEDCIRCLE;
228b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  }
229743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
230b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
23145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodstatic int
23245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodcompare_indic_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
23345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod{
23445d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  int a = pa->indic_position();
23545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  int b = pb->indic_position();
23645d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
23745d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  return a < b ? -1 : a == b ? 0 : +1;
23845d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod}
239867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
2407ea58db311bfb0d8f804d1e9f4a1f004bd45075aBehdad Esfahbod/* Rules from:
2417ea58db311bfb0d8f804d1e9f4a1f004bd45075aBehdad Esfahbod * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */
2427ea58db311bfb0d8f804d1e9f4a1f004bd45075aBehdad Esfahbod
243743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
2440831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbodinitial_reordering_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
2450831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod			     unsigned int start, unsigned int end, unsigned int base);
2460831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod
2470831061efb78983b9c6e1e72574c977e56383c08Behdad 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
253743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 1. Find base consonant:
254743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
255743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * The shaping engine finds the base consonant of the syllable, using the
256743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * following algorithm: starting from the end of the syllable, move backwards
257743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * until a consonant is found that does not have a below-base or post-base
258743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * form (post-base forms have to follow below-base forms), or that is not a
259743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * pre-base reordering Ra, or arrive at the first consonant. The consonant
260743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * stopped at will be the base.
261743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
262743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o If the syllable starts with Ra + Halant (in a script that has Reph)
263743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *     and has more than one consonant, Ra is excluded from candidates for
264743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *     base consonants.
265743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
266743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
2675e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod  unsigned int base = end;
26876b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  bool has_reph = false;
269743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
27076b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
27176b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod   *    and has more than one consonant, Ra is excluded from candidates for
27276b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod   *    base consonants. */
2735e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod  unsigned int limit = start;
27476b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  if (mask_array[RPHF] &&
2756d8e0cb74c02f6bc09cd4abe9e4bc82062e1b517Behdad Esfahbod      start + 3 <= end &&
27676b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod      info[start].indic_category() == OT_Ra &&
2776d8e0cb74c02f6bc09cd4abe9e4bc82062e1b517Behdad Esfahbod      info[start + 1].indic_category() == OT_H &&
2786d8e0cb74c02f6bc09cd4abe9e4bc82062e1b517Behdad Esfahbod      !is_joiner (info[start + 2]))
27976b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  {
2805e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod    limit += 2;
2815e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod    base = start;
28276b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod    has_reph = true;
2835e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod  };
28476b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod
2853c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod  {
2863c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    /* -> starting from the end of the syllable, move backwards */
2873c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    unsigned int i = end;
2883c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    do {
2893c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      i--;
2903c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      /* -> until a consonant is found */
2913c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      if (is_consonant (info[i]))
292743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod      {
2933c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	/* -> that does not have a below-base or post-base form
2943c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	 * (post-base forms have to follow below-base forms), */
2953c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	if (info[i].indic_position() != POS_BELOW_C &&
2963c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	    info[i].indic_position() != POS_POST_C)
2973c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	{
2983c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	  base = i;
2993c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	  break;
3003c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	}
301743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
3023c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	/* -> or that is not a pre-base reordering Ra,
3033c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	 *
3043c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	 * TODO
3053c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	 */
306e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
3073c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	/* -> or arrive at the first consonant. The consonant stopped at will
3083c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	 * be the base. */
3093c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	base = i;
3103c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      }
3113c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      else
3123c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	if (is_joiner (info[i]))
3133c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	  break;
3143c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    } while (i > limit);
3153c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    if (base < start)
3163c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      base = start; /* Just in case... */
3173c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod  }
318743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
3190831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod  /* Continue reading below ------v */
3200831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod  initial_reordering_syllable (map, buffer, mask_array, start, end, base);
3210831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod}
3223d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod
3230831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbodstatic void
3240831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbodinitial_reordering_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
3250831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod			     unsigned int start, unsigned int end, unsigned int base)
3260831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod{
3270831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod  hb_glyph_info_t *info = buffer->info;
3283d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod
329743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 2. Decompose and reorder Matras:
330743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
331743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * Each matra and any syllable modifier sign in the cluster are moved to the
332743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * appropriate position relative to the consonant(s) in the cluster. The
333743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * shaping engine decomposes two- or three-part matras into their constituent
334743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * parts before any repositioning. Matra characters are classified by which
335743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * consonant in a conjunct they have affinity for and are reordered to the
336743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * following positions:
337743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
338743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o Before first half form in the syllable
339743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After subjoined consonants
340743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After post-form consonant
341743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After main consonant (for above marks)
342743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
343743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * IMPLEMENTATION NOTES:
344743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
345743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * The normalize() routine has already decomposed matras for us, so we don't
346743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * need to worry about that.
347743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
348743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
349743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
350743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 3.  Reorder marks to canonical order:
351743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
352743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * Adjacent nukta and halant or nukta and vedic sign are always repositioned
353743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * if necessary, so that the nukta is first.
354743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
355743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * IMPLEMENTATION NOTES:
356743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
357743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * We don't need to do this: the normalize() routine already did this for us.
358743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
359743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
360743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
36145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* Reorder characters */
36245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
3633c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod  for (unsigned int i = start; i < base; i++)
364dbccf87eef0d26838fa4bb3ae26410f6c4818836Behdad Esfahbod    info[i].indic_position() = POS_PRE_C;
365dbccf87eef0d26838fa4bb3ae26410f6c4818836Behdad Esfahbod  info[base].indic_position() = POS_BASE_C;
36645d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
367fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod  /* Handle beginning Ra */
3680831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod  if (mask_array[RPHF] &&
3690831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod      start != base &&
3700831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod      start + 3 <= end &&
3710831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod      info[start].indic_category() == OT_Ra &&
3720831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod      info[start + 1].indic_category() == OT_H &&
3730831061efb78983b9c6e1e72574c977e56383c08Behdad Esfahbod      !is_joiner (info[start + 2]))
374dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod    info[start].indic_position() = POS_RA_TO_BECOME_REPH;
375fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod
376f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  /* For old-style Indic script tags, move the first post-base Halant after
377f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod   * last consonant. */
378f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  if ((map->get_chosen_script (0) & 0x000000FF) != '2') {
379f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod    /* We should only do this for Indic scripts which have a version two I guess. */
3803c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = base + 1; i < end; i++)
381f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod      if (info[i].indic_category() == OT_H) {
382f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        unsigned int j;
383f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        for (j = end - 1; j > i; j--)
384190eb31a16178269aecaf5d2ecc9012f956749f4Behdad Esfahbod	  if (is_consonant (info[j]))
385f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	    break;
386f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	if (j > i) {
387f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  /* Move Halant to after last consonant. */
388f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  hb_glyph_info_t t = info[i];
389f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  memmove (&info[i], &info[i + 1], (j - i) * sizeof (info[0]));
390f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  info[j] = t;
391f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	}
392f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        break;
393f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod      }
394f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  }
395f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod
39645d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* Attach ZWJ, ZWNJ, nukta, and halant to previous char to move with them. */
397ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  if (!options.uniscribe_bug_compatible)
398ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  {
399ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    /* Please update the Uniscribe branch when touching this! */
400ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    for (unsigned int i = start + 1; i < end; i++)
401ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod      if ((FLAG (info[i].indic_category()) & (FLAG (OT_ZWNJ) | FLAG (OT_ZWJ) | FLAG (OT_N) | FLAG (OT_H))))
402ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	info[i].indic_position() = info[i - 1].indic_position();
403ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  } else {
40467ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod    /*
40567ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod     * Uniscribe doesn't move the Halant with Left Matra.
40667ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod     * TEST: U+092B,U+093F,U+094DE
40767ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod     */
408ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    /* Please update the non-Uniscribe branch when touching this! */
409ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    for (unsigned int i = start + 1; i < end; i++)
410ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod      if ((FLAG (info[i].indic_category()) & (FLAG (OT_ZWNJ) | FLAG (OT_ZWJ) | FLAG (OT_N) | FLAG (OT_H)))) {
411ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	info[i].indic_position() = info[i - 1].indic_position();
412ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	if (info[i].indic_category() == OT_H && info[i].indic_position() == POS_LEFT_MATRA)
413ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	  for (unsigned int j = i; j > start; j--)
414ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	    if (info[j - 1].indic_position() != POS_LEFT_MATRA) {
415ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	      info[i].indic_position() = info[j - 1].indic_position();
416ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	      break;
417ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	    }
418ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod      }
419ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  }
42045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
42145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* We do bubble-sort, skip malicious clusters attempts */
422b99d63ae114fb58f129562b293a8a66543d499adBehdad Esfahbod  if (end - start < 64)
423a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod  {
424a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    /* Sit tight, rock 'n roll! */
425d3637edb248162970e202e9d0671540274192844Behdad Esfahbod    hb_bubble_sort (info + start, end - start, compare_indic_order);
426a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    /* Find base again */
427a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    base = end;
4283c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = start; i < end; i++)
429a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod      if (info[i].indic_position() == POS_BASE_C) {
430a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod        base = i;
431a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod	break;
432a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod      }
433a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod  }
43445d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
435743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Setup masks now */
436743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
437281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod  {
438281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    hb_mask_t mask;
439281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod
440dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod    /* Reph */
441668c6046c1b3af3bd316bda0cc8636f2a5e8df42Behdad Esfahbod    for (unsigned int i = start; i < end && info[i].indic_position() == POS_RA_TO_BECOME_REPH; i++)
442668c6046c1b3af3bd316bda0cc8636f2a5e8df42Behdad Esfahbod      info[i].mask |= mask_array[RPHF];
443dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
444281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Pre-base */
445281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    mask = mask_array[HALF] | mask_array[AKHN] | mask_array[CJCT];
4463c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = start; i < base; i++)
447281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod      info[i].mask  |= mask;
448281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Base */
449281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    mask = mask_array[AKHN] | mask_array[CJCT];
450281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    info[base].mask |= mask;
451281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Post-base */
452281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    mask = mask_array[BLWF] | mask_array[PSTF] | mask_array[CJCT];
4533c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = base + 1; i < end; i++)
454281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod      info[i].mask  |= mask;
455281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod  }
4569da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
4579da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  /* Apply ZWJ/ZWNJ effects */
4583c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod  for (unsigned int i = start + 1; i < end; i++)
4599da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    if (is_joiner (info[i])) {
4609da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      bool non_joiner = info[i].indic_category() == OT_ZWNJ;
4616b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod      unsigned int j = i;
4629da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
4639da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      do {
4649da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod	j--;
4656b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod
4663bf27a9f0e92aa31b464bd3b9fdea5933c9ae8b1Behdad Esfahbod	info[j].mask &= ~mask_array[CJCT];
4676b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod	if (non_joiner)
468c6d904d67db589dd6209928e56504f04f6a07756Behdad Esfahbod	  info[j].mask &= ~mask_array[HALF];
4696b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod
4709da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      } while (j > start && !is_consonant (info[j]));
4719da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    }
472743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
473743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
474743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
475743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
476ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodinitial_reordering_vowel_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
477ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod				   unsigned int start, unsigned int end)
478743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
479c5306b6861cfaa50af40e8ceb058791fa06d7981Behdad Esfahbod  /* We made the vowels look like consonants.  So let's call the consonant logic! */
480c5306b6861cfaa50af40e8ceb058791fa06d7981Behdad Esfahbod  initial_reordering_consonant_syllable (map, buffer, mask_array, start, end);
481743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
482743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
483743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
484ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodinitial_reordering_standalone_cluster (const hb_ot_map_t *map, hb_buffer_t *buffer, 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
503ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodinitial_reordering_non_indic (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
504743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		 unsigned int start, unsigned int end)
505743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
506743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Nothing to do right now.  If we ever switch to using the output
507743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * buffer in the reordering process, we'd need to next_glyph() here. */
508743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
509743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
510743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod#include "hb-ot-shape-complex-indic-machine.hh"
511743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
512743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
513743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodinitial_reordering (const hb_ot_map_t *map,
514743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_face_t *face,
515743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_buffer_t *buffer,
516743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    void *user_data HB_UNUSED)
517743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
518b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  hb_mask_t mask_array[ARRAY_LENGTH (indic_basic_features)] = {0};
519b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  unsigned int num_masks = ARRAY_LENGTH (indic_basic_features);
520b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  for (unsigned int i = 0; i < num_masks; i++)
52176f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod    mask_array[i] = map->get_1_mask (indic_basic_features[i].tag);
522743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
523743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  find_syllables (map, buffer, mask_array);
524b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod}
525b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
526743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
527eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodfinal_reordering_syllable (hb_buffer_t *buffer, hb_mask_t *mask_array,
528ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod			   unsigned int start, unsigned int end)
529743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
5304ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  hb_glyph_info_t *info = buffer->info;
5314ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
532e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod  /* 4. Final reordering:
533e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
534e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * After the localized forms and basic shaping forms GSUB features have been
535e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * applied (see below), the shaping engine performs some final glyph
536e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * reordering before applying all the remaining font features to the entire
537e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * cluster.
5384ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod   */
5394ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5404ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  /* Find base again */
5414ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  unsigned int base = end;
5424ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  for (unsigned int i = start; i < end; i++)
5434ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    if (info[i].indic_position() == POS_BASE_C) {
5444ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod      base = i;
5454ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod      break;
5464ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    }
5474ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5484ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  if (base == start) {
5494ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    /* There's no Reph, and no left Matra to reposition.  Just merge the cluster
5504ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod     * and go home. */
5514ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    buffer->merge_clusters (start, end);
5524ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    return;
5534ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  }
5544ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5554705a7026900e51f6430f03a73c87f2df035df92Behdad Esfahbod  unsigned int start_of_last_cluster = base;
5564705a7026900e51f6430f03a73c87f2df035df92Behdad Esfahbod
5574ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  /*   o Reorder matras:
558e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
559e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     If a pre-base matra character had been reordered before applying basic
560e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     features, the glyph can be moved closer to the main consonant based on
561e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     whether half-forms had been formed. Actual position for the matra is
562e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     defined as “after last standalone halant glyph, after initial matra
563e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     position and before the main consonant”. If ZWJ or ZWNJ follow this
564e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     halant, position is moved after it.
5654ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod   */
5664ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5674ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  unsigned int new_matra_pos = base - 1;
5684ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  while (new_matra_pos > start &&
5694ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod	 !(FLAG (info[new_matra_pos].indic_category()) & (FLAG (OT_M) | FLAG (OT_H))))
5704ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    new_matra_pos--;
5716782bdae3be0357da1dadc7b806a43ceefa67a90Behdad Esfahbod  /* If we found no Halant we are done.  Otherwise only proceed if the Halant does
5726782bdae3be0357da1dadc7b806a43ceefa67a90Behdad Esfahbod   * not belong to the Matra itself! */
5736782bdae3be0357da1dadc7b806a43ceefa67a90Behdad Esfahbod  if (info[new_matra_pos].indic_category() == OT_H &&
5746782bdae3be0357da1dadc7b806a43ceefa67a90Behdad Esfahbod      info[new_matra_pos].indic_position() != POS_LEFT_MATRA) {
5754ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
5764ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    if (new_matra_pos + 1 < end && is_joiner (info[new_matra_pos + 1]))
5774ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod      new_matra_pos++;
5784ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5794ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    /* Now go see if there's actually any matras... */
5804ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    for (unsigned int i = new_matra_pos; i > start; i--)
5817708ee23cbcc8c8edce13e73b6e549b77bd8c2d0Behdad Esfahbod      if (info[i - 1].indic_position () == POS_LEFT_MATRA)
5824ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod      {
5834ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod	unsigned int old_matra_pos = i - 1;
5844ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod	hb_glyph_info_t matra = info[old_matra_pos];
5854ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod	memmove (&info[old_matra_pos], &info[old_matra_pos + 1], (new_matra_pos - old_matra_pos) * sizeof (info[0]));
5864ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod	info[new_matra_pos] = matra;
5874705a7026900e51f6430f03a73c87f2df035df92Behdad Esfahbod	start_of_last_cluster = MIN (new_matra_pos, start_of_last_cluster);
5884ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod	new_matra_pos--;
5894ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod      }
5904ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  }
5914ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5924ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5934ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  /*   o Reorder reph:
594e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
595e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     Reph’s original position is always at the beginning of the syllable,
596e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     (i.e. it is not reordered at the character reordering stage). However,
597e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     it will be reordered according to the basic-forms shaping results.
598e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     Possible positions for reph, depending on the script, are; after main,
599e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     before post-base consonant forms, and after post-base consonant forms.
600dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod   */
601dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
602dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  /* If there's anything after the Ra that has the REPH pos, it ought to be halant.
603dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod   * Which means that the font has failed to ligate the Reph.  In which case, we
604dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod   * shouldn't move. */
605dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  if (start + 1 < end &&
606dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod      info[start].indic_position() == POS_RA_TO_BECOME_REPH &&
607dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod      info[start + 1].indic_position() != POS_RA_TO_BECOME_REPH)
608dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  {
60902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      unsigned int new_reph_pos;
61002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
61102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     enum reph_position_t {
61202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod       REPH_AFTER_MAIN, /* Malayalam, Oriya */
61302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod       REPH_BEFORE_SUBSCRIPT, /* Gurmukhi  */
61402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod       REPH_AFTER_SUBSCRIPT, /* Bengali */
61502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod       REPH_BEFORE_POSTSCRIPT, /* Devanagari, Gujarati  */
61602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod       REPH_AFTER_POSTSCRIPT, /* Kannada, Tamil, Telugu  */
61702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     } reph_pos = REPH_BEFORE_POSTSCRIPT; /* XXX */ /* XXX Figure out old behavior too */
61802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
619dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod    /*       1. If reph should be positioned after post-base consonant forms,
620dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          proceed to step 5.
62102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
62202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_step_1:
62302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
62402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      if (reph_pos == REPH_AFTER_POSTSCRIPT)
62502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	goto reph_step_5;
62602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
62702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
62802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       2. If the reph repositioning class is not after post-base: target
629dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          position is after the first explicit halant glyph between the
630dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first post-reph consonant and last main consonant. If ZWJ or ZWNJ
631dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          are following this halant, position is moved after it. If such
632dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          position is found, this is the target position. Otherwise,
633dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          proceed to the next step.
634dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *
635dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          Note: in old-implementation fonts, where classifications were
636dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          fixed in shaping engine, there was no case where reph position
637dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          will be found on this step.
63802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
63902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_step_2:
64002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
64102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      new_reph_pos = start + 1;
64202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      while (new_reph_pos < base && info[new_reph_pos].indic_category() != OT_H)
64302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	new_reph_pos++;
64402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
64502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      if (new_reph_pos < base && info[new_reph_pos].indic_category() == OT_H) {
64602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	/* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */
64702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1]))
64802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	  new_reph_pos++;
64902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	goto reph_move;
65002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      }
65102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
65202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
65302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       3. If reph should be repositioned after the main consonant: find the
654dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first consonant not ligated with main, or find the first
655dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          consonant that is not a potential pre-base reordering Ra.
65602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
65702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_step_3:
65802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
65902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      /* XXX */
66002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
66102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
66202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       4. If reph should be positioned before post-base consonant, find
663dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first post-base classified consonant not ligated with main. If no
664dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          consonant is found, the target position should be before the
665dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first matra, syllable modifier sign or vedic sign.
66602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
66702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_step_4:
66802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
66902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      /* XXX */
67002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
67102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
67202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       5. If no consonant is found in steps 3 or 4, move reph to a position
673dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          immediately before the first post-base matra, syllable modifier
674dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          sign or vedic sign that has a reordering class after the intended
675dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          reph position. For example, if the reordering position for reph
676dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          is post-main, it will skip above-base matras that also have a
677dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          post-main position.
678dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     */
67902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_step_5:
68002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
68102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      /* XXX */
68202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
683dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
68402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       6. Otherwise, reorder reph to the end of the syllable.
68502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
68602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_step_6:
68702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
68802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      new_reph_pos = end - 1;
68902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      while (new_reph_pos > start && info[new_reph_pos].indic_position() == POS_SMVD)
69002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	new_reph_pos--;
69102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
692892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod      /*
693892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * If the Reph is to be ending up after a Matra,Halant sequence,
694892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * position it before that Halant so it can interact with the Matra.
695892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * However, if it's a plain Consonant,Halant we shouldn't do that.
696892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * Uniscribe doesn't do this.
697892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * TEST: U+0930,U+094D,U+0915,U+094B,U+094D
698892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       */
699892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod      if (!options.uniscribe_bug_compatible &&
700892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod	  unlikely (info[new_reph_pos].indic_category() == OT_H)) {
70102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	for (unsigned int i = base + 1; i < new_reph_pos; i++)
70202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	  if (info[i].indic_category() == OT_M) {
70302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	    /* Ok, got it. */
70402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	    new_reph_pos--;
70502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	  }
70602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      }
70702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      goto reph_move;
7088df5636968389ac7bf8620ccd091fd4872b0bbeeBehdad Esfahbod    }
7098df5636968389ac7bf8620ccd091fd4872b0bbeeBehdad Esfahbod
71002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_move:
71102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
71202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      /* Move */
71302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      hb_glyph_info_t reph = info[start];
71402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      memmove (&info[start], &info[start + 1], (new_reph_pos - start) * sizeof (info[0]));
71502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      info[new_reph_pos] = reph;
71602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      start_of_last_cluster = start; /* Yay, one big cluster! */
71702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
718dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  }
719dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
720dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
721dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  /*   o Reorder pre-base reordering consonants:
722e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
723e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     If a pre-base reordering consonant is found, reorder it according to
724e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     the following rules:
725e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
726e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       1. Only reorder a glyph produced by substitution during application
727e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          of the feature. (Note that a font may shape a Ra consonant with
728e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          the feature generally but block it in certain contexts.)
729e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
730e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       2. Try to find a target position the same way as for pre-base matra.
731e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          If it is found, reorder pre-base consonant glyph.
732e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
733e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       3. If position is not found, reorder immediately before main
734e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          consonant.
735e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   */
736e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
737eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  /* TODO */
738eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
739eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
740eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
741eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
74221d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod
74321d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod  /* Finish off the clusters and go home! */
74421d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod
745ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  if (!options.uniscribe_bug_compatible)
746ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  {
74721d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    /* This is what Uniscribe does.  Ie. add cluster boundaries after Halant,ZWNJ.
74821d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod     * This means, half forms are submerged into the main consonants cluster.
74921d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod     * This is unnecessary, and makes cursor positioning harder, but that's what
75021d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod     * Uniscribe does. */
75121d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    unsigned int cluster_start = start;
75221d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    for (unsigned int i = start + 1; i < start_of_last_cluster; i++)
75321d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod      if (info[i - 1].indic_category() == OT_H && info[i].indic_category() == OT_ZWNJ) {
75421d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod        i++;
75521d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod	buffer->merge_clusters (cluster_start, i);
75621d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod	cluster_start = i;
75721d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod      }
75821d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    start_of_last_cluster = cluster_start;
75921d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod  }
76021d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod
76121d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod  buffer->merge_clusters (start_of_last_cluster, end);
762ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod}
763e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
764e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
765ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodstatic void
766ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodfinal_reordering (const hb_ot_map_t *map,
767ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod		  hb_face_t *face,
768ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod		  hb_buffer_t *buffer,
769ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod		  void *user_data HB_UNUSED)
770ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod{
771ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  unsigned int count = buffer->len;
772ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  if (!count) return;
773ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod
774eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  hb_mask_t mask_array[ARRAY_LENGTH (indic_other_features)] = {0};
775eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  unsigned int num_masks = ARRAY_LENGTH (indic_other_features);
776eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  for (unsigned int i = 0; i < num_masks; i++)
777eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod    mask_array[i] = map->get_1_mask (indic_other_features[i].tag);
778eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
779ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  hb_glyph_info_t *info = buffer->info;
780ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  unsigned int last = 0;
781cee7187447b76b22e1bb6136d137b35ac49c3a5dBehdad Esfahbod  unsigned int last_syllable = info[0].syllable();
782ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  for (unsigned int i = 1; i < count; i++)
783cee7187447b76b22e1bb6136d137b35ac49c3a5dBehdad Esfahbod    if (last_syllable != info[i].syllable()) {
784eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod      final_reordering_syllable (buffer, mask_array, last, i);
785ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod      last = i;
786cee7187447b76b22e1bb6136d137b35ac49c3a5dBehdad Esfahbod      last_syllable = info[last].syllable();
787ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod    }
788eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  final_reordering_syllable (buffer, mask_array, last, count);
789e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
790743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category);
791743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position);
792743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
793743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
794743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
795b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
796