hb-ot-shape-complex-indic.cc revision 3399a06e7033651ee926448737bdb18e553c1796
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   *
821a1fa8c655a082fc1439608457ba717306cc83caBehdad Esfahbod   * We treat Vowels and NBSP 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! */
851a1fa8c655a082fc1439608457ba717306cc83caBehdad Esfahbod  return !!(FLAG (info.indic_category()) & (FLAG (OT_C) | FLAG (OT_Ra) | FLAG (OT_V) | FLAG (OT_NBSP)));
869ee27a928a989c71923cef82a9e9828f8e9ca051Behdad Esfahbod}
87c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod
88c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbodstatic const struct {
89c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  hb_tag_t tag;
90c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  hb_bool_t is_global;
91c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod} indic_basic_features[] =
92b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
93c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('n','u','k','t'), true},
94c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('a','k','h','n'), false},
95c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('r','p','h','f'), false},
961ac075b227090a9ad930dcc1670236c176b27067Behdad Esfahbod  {HB_TAG('r','k','r','f'), true},
97c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('p','r','e','f'), false},
98c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('b','l','w','f'), false},
99c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('h','a','l','f'), false},
100c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('v','a','t','u'), true},
101c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('p','s','t','f'), false},
1029da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  {HB_TAG('c','j','c','t'), false},
103c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod};
104c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod
105c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod/* Same order as the indic_basic_features array */
106c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbodenum {
107c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  _NUKT,
108c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  AKHN,
109c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  RPHF,
110df6d45c693c417bf311e6fa49f18a8558542e525Behdad Esfahbod  _RKRF,
111c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  PREF,
112c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  BLWF,
113c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  HALF,
114c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  _VATU,
115c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  PSTF,
116e8eedf2687f05372bf5476e84139d01ba67c9f73Behdad Esfahbod  CJCT
117b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod};
118b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
119b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbodstatic const hb_tag_t indic_other_features[] =
120b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
121b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  HB_TAG('p','r','e','s'),
122b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  HB_TAG('a','b','v','s'),
123b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  HB_TAG('b','l','w','s'),
124b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  HB_TAG('p','s','t','s'),
125b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  HB_TAG('h','a','l','n'),
126b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
127b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  HB_TAG('d','i','s','t'),
128b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  HB_TAG('a','b','v','m'),
129b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  HB_TAG('b','l','w','m'),
130b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod};
131b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
132743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
133743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
134743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodinitial_reordering (const hb_ot_map_t *map,
135743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_face_t *face,
136743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_buffer_t *buffer,
137743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    void *user_data HB_UNUSED);
138f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbodstatic void
139f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbodfinal_reordering (const hb_ot_map_t *map,
140f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod		  hb_face_t *face,
141f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod		  hb_buffer_t *buffer,
142743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		  void *user_data HB_UNUSED);
143b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
144b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbodvoid
14576f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod_hb_ot_shape_complex_collect_features_indic (hb_ot_map_builder_t *map, const hb_segment_properties_t  *props)
146b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
147f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_bool_feature (HB_TAG('l','o','c','l'));
148a54a5505a35eef5315a8e2e7a79502901e3eff5fBehdad Esfahbod  /* The Indic specs do not require ccmp, but we apply it here since if
149a54a5505a35eef5315a8e2e7a79502901e3eff5fBehdad Esfahbod   * there is a use of it, it's typically at the beginning. */
150f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_bool_feature (HB_TAG('c','c','m','p'));
151f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
152743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  map->add_gsub_pause (initial_reordering, NULL);
153f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
154412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (indic_basic_features); i++) {
15576f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod    map->add_bool_feature (indic_basic_features[i].tag, indic_basic_features[i].is_global);
156412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod    map->add_gsub_pause (NULL, NULL);
157412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  }
158b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
159f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_gsub_pause (final_reordering, NULL);
160f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
161412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (indic_other_features); i++) {
16276f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod    map->add_bool_feature (indic_other_features[i], true);
163412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod    map->add_gsub_pause (NULL, NULL);
164412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  }
165b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod}
166b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
167867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
16811138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbodhb_ot_shape_normalization_mode_t
16911138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbod_hb_ot_shape_complex_normalization_preference_indic (void)
17002cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod{
17102cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod  /* We want split matras decomposed by the common shaping logic. */
17211138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbod  return HB_OT_SHAPE_NORMALIZATION_MODE_DECOMPOSED;
17302cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod}
17402cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod
175867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
176b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbodvoid
177acd88e659fdb2fa1cbf7171f0a1c7fbc81b9f298Behdad Esfahbod_hb_ot_shape_complex_setup_masks_indic (hb_ot_map_t *map, hb_buffer_t *buffer, hb_font_t *font)
178b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
1799f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod  HB_BUFFER_ALLOCATE_VAR (buffer, indic_category);
1809f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod  HB_BUFFER_ALLOCATE_VAR (buffer, indic_position);
1819f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod
182743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* We cannot setup masks here.  We save information about characters
183743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * and setup masks later on in a pause-callback. */
184743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
185743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  unsigned int count = buffer->len;
186b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  for (unsigned int i = 0; i < count; i++)
187b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  {
18892332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    hb_glyph_info_t &info = buffer->info[i];
18992332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    unsigned int type = get_indic_categories (info.codepoint);
19092332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod
19192332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    info.indic_category() = type & 0x0F;
19292332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    info.indic_position() = type >> 4;
19392332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod
1943399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod    /* The spec says U+0952 is OT_A.  However, testing shows that Uniscribe
1953399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * treats U+0951..U+0952 all as OT_VD.
1963399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * TESTS:
1973399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * U+092E,U+0947,U+0952
1983399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * U+092E,U+0952,U+0947
1993399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * U+092E,U+0947,U+0951
2003399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * U+092E,U+0951,U+0947
2013399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * */
2023399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod    if (unlikely (hb_in_range<hb_codepoint_t> (info.codepoint, 0x0951, 0x0954)))
2033399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod      info.indic_category() = OT_VD;
2043399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod
20592332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    if (info.indic_category() == OT_C) {
20692332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_position() = consonant_position (info.codepoint);
20792332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      if (is_ra (info.codepoint))
20892332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod	info.indic_category() = OT_Ra;
20992332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    } else if (info.indic_category() == OT_SM ||
21092332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod	       info.indic_category() == OT_VD) {
21192332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_position() = POS_SMVD;
21292332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    } else if (unlikely (info.codepoint == 0x200C))
21392332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_category() = OT_ZWNJ;
21492332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    else if (unlikely (info.codepoint == 0x200D))
21592332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_category() = OT_ZWJ;
21692332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod
217b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  }
218743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
219b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
22045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodstatic int
22145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodcompare_indic_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
22245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod{
22345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  int a = pa->indic_position();
22445d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  int b = pb->indic_position();
22545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
22645d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  return a < b ? -1 : a == b ? 0 : +1;
22745d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod}
228867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
229743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
230ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodinitial_reordering_consonant_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
231ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod				       unsigned int start, unsigned int end)
232743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
233ee58f3bc75d2d071a71b94063bf12205a5871acbBehdad Esfahbod  hb_glyph_info_t *info = buffer->info;
234743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
235743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Comments from:
236743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */
237743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
238743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 1. Find base consonant:
239743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
240743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * The shaping engine finds the base consonant of the syllable, using the
241743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * following algorithm: starting from the end of the syllable, move backwards
242743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * until a consonant is found that does not have a below-base or post-base
243743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * form (post-base forms have to follow below-base forms), or that is not a
244743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * pre-base reordering Ra, or arrive at the first consonant. The consonant
245743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * stopped at will be the base.
246743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
247743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o If the syllable starts with Ra + Halant (in a script that has Reph)
248743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *     and has more than one consonant, Ra is excluded from candidates for
249743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *     base consonants.
250743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
251743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
2525e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod  unsigned int base = end;
25376b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  bool has_reph = false;
254743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
25576b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
25676b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod   *    and has more than one consonant, Ra is excluded from candidates for
25776b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod   *    base consonants. */
2585e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod  unsigned int limit = start;
25976b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  if (mask_array[RPHF] &&
2606d8e0cb74c02f6bc09cd4abe9e4bc82062e1b517Behdad Esfahbod      start + 3 <= end &&
26176b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod      info[start].indic_category() == OT_Ra &&
2626d8e0cb74c02f6bc09cd4abe9e4bc82062e1b517Behdad Esfahbod      info[start + 1].indic_category() == OT_H &&
2636d8e0cb74c02f6bc09cd4abe9e4bc82062e1b517Behdad Esfahbod      !is_joiner (info[start + 2]))
26476b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  {
2655e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod    limit += 2;
2665e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod    base = start;
26776b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod    has_reph = true;
2685e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod  };
26976b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod
2703c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod  {
2713c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    /* -> starting from the end of the syllable, move backwards */
2723c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    unsigned int i = end;
2733c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    do {
2743c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      i--;
2753c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      /* -> until a consonant is found */
2763c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      if (is_consonant (info[i]))
277743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod      {
2783c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	/* -> that does not have a below-base or post-base form
2793c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	 * (post-base forms have to follow below-base forms), */
2803c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	if (info[i].indic_position() != POS_BELOW_C &&
2813c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	    info[i].indic_position() != POS_POST_C)
2823c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	{
2833c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	  base = i;
2843c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	  break;
2853c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	}
286743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
2873c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	/* -> or that is not a pre-base reordering Ra,
2883c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	 *
2893c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	 * TODO
2903c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	 */
291e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
2923c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	/* -> or arrive at the first consonant. The consonant stopped at will
2933c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	 * be the base. */
2943c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	base = i;
2953c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      }
2963c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      else
2973c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	if (is_joiner (info[i]))
2983c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	  break;
2993c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    } while (i > limit);
3003c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    if (base < start)
3013c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      base = start; /* Just in case... */
3023c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod  }
303743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
3043d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod  /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
3053d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod   *    and has more than one consonant, Ra is excluded from candidates for
3063d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod   *    base consonants. */
3073d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod  if (has_reph && base == start) {
3083d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod    /* Have no other consonant, so Reph is not formed and Ra becomes base. */
3093d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod    has_reph = false;
3103d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod  }
3113d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod
3123d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod
313743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 2. Decompose and reorder Matras:
314743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
315743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * Each matra and any syllable modifier sign in the cluster are moved to the
316743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * appropriate position relative to the consonant(s) in the cluster. The
317743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * shaping engine decomposes two- or three-part matras into their constituent
318743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * parts before any repositioning. Matra characters are classified by which
319743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * consonant in a conjunct they have affinity for and are reordered to the
320743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * following positions:
321743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
322743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o Before first half form in the syllable
323743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After subjoined consonants
324743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After post-form consonant
325743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After main consonant (for above marks)
326743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
327743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * IMPLEMENTATION NOTES:
328743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
329743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * The normalize() routine has already decomposed matras for us, so we don't
330743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * need to worry about that.
331743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
332743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
333743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
334743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 3.  Reorder marks to canonical order:
335743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
336743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * Adjacent nukta and halant or nukta and vedic sign are always repositioned
337743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * if necessary, so that the nukta is first.
338743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
339743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * IMPLEMENTATION NOTES:
340743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
341743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * We don't need to do this: the normalize() routine already did this for us.
342743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
343743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
344743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
34545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* Reorder characters */
34645d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
3473c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod  for (unsigned int i = start; i < base; i++)
348dbccf87eef0d26838fa4bb3ae26410f6c4818836Behdad Esfahbod    info[i].indic_position() = POS_PRE_C;
349dbccf87eef0d26838fa4bb3ae26410f6c4818836Behdad Esfahbod  info[base].indic_position() = POS_BASE_C;
35045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
351fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod  /* Handle beginning Ra */
352dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  if (has_reph)
353dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod    info[start].indic_position() = POS_RA_TO_BECOME_REPH;
354fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod
355f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  /* For old-style Indic script tags, move the first post-base Halant after
356f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod   * last consonant. */
357f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  if ((map->get_chosen_script (0) & 0x000000FF) != '2') {
358f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod    /* We should only do this for Indic scripts which have a version two I guess. */
3593c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = base + 1; i < end; i++)
360f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod      if (info[i].indic_category() == OT_H) {
361f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        unsigned int j;
362f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        for (j = end - 1; j > i; j--)
363190eb31a16178269aecaf5d2ecc9012f956749f4Behdad Esfahbod	  if (is_consonant (info[j]))
364f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	    break;
365f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	if (j > i) {
366f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  /* Move Halant to after last consonant. */
367f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  hb_glyph_info_t t = info[i];
368f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  memmove (&info[i], &info[i + 1], (j - i) * sizeof (info[0]));
369f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  info[j] = t;
370f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	}
371f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        break;
372f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod      }
373f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  }
374f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod
37545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* Attach ZWJ, ZWNJ, nukta, and halant to previous char to move with them. */
376ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  if (!options.uniscribe_bug_compatible)
377ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  {
378ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    /* Please update the Uniscribe branch when touching this! */
379ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    for (unsigned int i = start + 1; i < end; i++)
380ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod      if ((FLAG (info[i].indic_category()) & (FLAG (OT_ZWNJ) | FLAG (OT_ZWJ) | FLAG (OT_N) | FLAG (OT_H))))
381ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	info[i].indic_position() = info[i - 1].indic_position();
382ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  } else {
38367ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod    /*
38467ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod     * Uniscribe doesn't move the Halant with Left Matra.
38567ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod     * TEST: U+092B,U+093F,U+094DE
38667ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod     */
387ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    /* Please update the non-Uniscribe branch when touching this! */
388ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    for (unsigned int i = start + 1; i < end; i++)
389ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod      if ((FLAG (info[i].indic_category()) & (FLAG (OT_ZWNJ) | FLAG (OT_ZWJ) | FLAG (OT_N) | FLAG (OT_H)))) {
390ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	info[i].indic_position() = info[i - 1].indic_position();
391ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	if (info[i].indic_category() == OT_H && info[i].indic_position() == POS_LEFT_MATRA)
392ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	  for (unsigned int j = i; j > start; j--)
393ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	    if (info[j - 1].indic_position() != POS_LEFT_MATRA) {
394ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	      info[i].indic_position() = info[j - 1].indic_position();
395ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	      break;
396ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	    }
397ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod      }
398ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  }
39945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
40045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* We do bubble-sort, skip malicious clusters attempts */
401b99d63ae114fb58f129562b293a8a66543d499adBehdad Esfahbod  if (end - start < 64)
402a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod  {
403a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    /* Sit tight, rock 'n roll! */
404d3637edb248162970e202e9d0671540274192844Behdad Esfahbod    hb_bubble_sort (info + start, end - start, compare_indic_order);
405a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    /* Find base again */
406a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    base = end;
4073c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = start; i < end; i++)
408a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod      if (info[i].indic_position() == POS_BASE_C) {
409a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod        base = i;
410a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod	break;
411a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod      }
412a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod  }
41345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
414743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Setup masks now */
415743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
416281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod  {
417281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    hb_mask_t mask;
418281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod
419dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod    /* Reph */
420668c6046c1b3af3bd316bda0cc8636f2a5e8df42Behdad Esfahbod    for (unsigned int i = start; i < end && info[i].indic_position() == POS_RA_TO_BECOME_REPH; i++)
421668c6046c1b3af3bd316bda0cc8636f2a5e8df42Behdad Esfahbod      info[i].mask |= mask_array[RPHF];
422dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
423281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Pre-base */
424281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    mask = mask_array[HALF] | mask_array[AKHN] | mask_array[CJCT];
4253c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = start; i < base; i++)
426281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod      info[i].mask  |= mask;
427281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Base */
428281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    mask = mask_array[AKHN] | mask_array[CJCT];
429281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    info[base].mask |= mask;
430281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Post-base */
431281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    mask = mask_array[BLWF] | mask_array[PSTF] | mask_array[CJCT];
4323c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = base + 1; i < end; i++)
433281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod      info[i].mask  |= mask;
434281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod  }
4359da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
4369da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  /* Apply ZWJ/ZWNJ effects */
4373c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod  for (unsigned int i = start + 1; i < end; i++)
4389da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    if (is_joiner (info[i])) {
4399da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      bool non_joiner = info[i].indic_category() == OT_ZWNJ;
4406b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod      unsigned int j = i;
4419da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
4429da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      do {
4439da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod	j--;
4446b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod
4453bf27a9f0e92aa31b464bd3b9fdea5933c9ae8b1Behdad Esfahbod	info[j].mask &= ~mask_array[CJCT];
4466b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod	if (non_joiner)
447c6d904d67db589dd6209928e56504f04f6a07756Behdad Esfahbod	  info[j].mask &= ~mask_array[HALF];
4486b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod
4499da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      } while (j > start && !is_consonant (info[j]));
4509da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    }
451743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
452743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
453743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
454743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
455ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodinitial_reordering_vowel_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
456ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod				   unsigned int start, unsigned int end)
457743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
458c5306b6861cfaa50af40e8ceb058791fa06d7981Behdad Esfahbod  /* We made the vowels look like consonants.  So let's call the consonant logic! */
459c5306b6861cfaa50af40e8ceb058791fa06d7981Behdad Esfahbod  initial_reordering_consonant_syllable (map, buffer, mask_array, start, end);
460743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
461743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
462743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
463ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodinitial_reordering_standalone_cluster (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
464ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod				       unsigned int start, unsigned int end)
465743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
4661a1fa8c655a082fc1439608457ba717306cc83caBehdad Esfahbod  /* We made the vowels look like consonants.  So let's call the consonant logic! */
4671a1fa8c655a082fc1439608457ba717306cc83caBehdad Esfahbod  initial_reordering_consonant_syllable (map, buffer, mask_array, start, end);
468743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
469743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
470743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
471ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodinitial_reordering_non_indic (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
472743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		 unsigned int start, unsigned int end)
473743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
474743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Nothing to do right now.  If we ever switch to using the output
475743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * buffer in the reordering process, we'd need to next_glyph() here. */
476743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
477743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
478743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod#include "hb-ot-shape-complex-indic-machine.hh"
479743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
480743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
481743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodinitial_reordering (const hb_ot_map_t *map,
482743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_face_t *face,
483743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_buffer_t *buffer,
484743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    void *user_data HB_UNUSED)
485743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
486b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  hb_mask_t mask_array[ARRAY_LENGTH (indic_basic_features)] = {0};
487b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  unsigned int num_masks = ARRAY_LENGTH (indic_basic_features);
488b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  for (unsigned int i = 0; i < num_masks; i++)
48976f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod    mask_array[i] = map->get_1_mask (indic_basic_features[i].tag);
490743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
491743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  find_syllables (map, buffer, mask_array);
492b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod}
493b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
494743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
495ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodfinal_reordering_syllable (hb_buffer_t *buffer,
496ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod			   unsigned int start, unsigned int end)
497743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
4984ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  hb_glyph_info_t *info = buffer->info;
4994ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
500e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod  /* 4. Final reordering:
501e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
502e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * After the localized forms and basic shaping forms GSUB features have been
503e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * applied (see below), the shaping engine performs some final glyph
504e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * reordering before applying all the remaining font features to the entire
505e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * cluster.
5064ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod   */
5074ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5084ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  /* Find base again */
5094ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  unsigned int base = end;
5104ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  for (unsigned int i = start; i < end; i++)
5114ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    if (info[i].indic_position() == POS_BASE_C) {
5124ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod      base = i;
5134ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod      break;
5144ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    }
5154ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5164ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  if (base == start) {
5174ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    /* There's no Reph, and no left Matra to reposition.  Just merge the cluster
5184ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod     * and go home. */
5194ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    buffer->merge_clusters (start, end);
5204ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    return;
5214ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  }
5224ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5234705a7026900e51f6430f03a73c87f2df035df92Behdad Esfahbod  unsigned int start_of_last_cluster = base;
5244705a7026900e51f6430f03a73c87f2df035df92Behdad Esfahbod
5254ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  /*   o Reorder matras:
526e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
527e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     If a pre-base matra character had been reordered before applying basic
528e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     features, the glyph can be moved closer to the main consonant based on
529e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     whether half-forms had been formed. Actual position for the matra is
530e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     defined as “after last standalone halant glyph, after initial matra
531e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     position and before the main consonant”. If ZWJ or ZWNJ follow this
532e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     halant, position is moved after it.
5334ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod   */
5344ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5354ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  unsigned int new_matra_pos = base - 1;
5364ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  while (new_matra_pos > start &&
5374ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod	 !(FLAG (info[new_matra_pos].indic_category()) & (FLAG (OT_M) | FLAG (OT_H))))
5384ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    new_matra_pos--;
5396782bdae3be0357da1dadc7b806a43ceefa67a90Behdad Esfahbod  /* If we found no Halant we are done.  Otherwise only proceed if the Halant does
5406782bdae3be0357da1dadc7b806a43ceefa67a90Behdad Esfahbod   * not belong to the Matra itself! */
5416782bdae3be0357da1dadc7b806a43ceefa67a90Behdad Esfahbod  if (info[new_matra_pos].indic_category() == OT_H &&
5426782bdae3be0357da1dadc7b806a43ceefa67a90Behdad Esfahbod      info[new_matra_pos].indic_position() != POS_LEFT_MATRA) {
5434ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
5444ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    if (new_matra_pos + 1 < end && is_joiner (info[new_matra_pos + 1]))
5454ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod      new_matra_pos++;
5464ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5474ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    /* Now go see if there's actually any matras... */
5484ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    for (unsigned int i = new_matra_pos; i > start; i--)
5497708ee23cbcc8c8edce13e73b6e549b77bd8c2d0Behdad Esfahbod      if (info[i - 1].indic_position () == POS_LEFT_MATRA)
5504ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod      {
5514ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod	unsigned int old_matra_pos = i - 1;
5524ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod	hb_glyph_info_t matra = info[old_matra_pos];
5534ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod	memmove (&info[old_matra_pos], &info[old_matra_pos + 1], (new_matra_pos - old_matra_pos) * sizeof (info[0]));
5544ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod	info[new_matra_pos] = matra;
5554705a7026900e51f6430f03a73c87f2df035df92Behdad Esfahbod	start_of_last_cluster = MIN (new_matra_pos, start_of_last_cluster);
5564ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod	new_matra_pos--;
5574ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod      }
5584ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  }
5594ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5604ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5614ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  /*   o Reorder reph:
562e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
563e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     Reph’s original position is always at the beginning of the syllable,
564e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     (i.e. it is not reordered at the character reordering stage). However,
565e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     it will be reordered according to the basic-forms shaping results.
566e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     Possible positions for reph, depending on the script, are; after main,
567e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     before post-base consonant forms, and after post-base consonant forms.
568dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod   */
569dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
570dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  /* If there's anything after the Ra that has the REPH pos, it ought to be halant.
571dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod   * Which means that the font has failed to ligate the Reph.  In which case, we
572dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod   * shouldn't move. */
573dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  if (start + 1 < end &&
574dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod      info[start].indic_position() == POS_RA_TO_BECOME_REPH &&
575dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod      info[start + 1].indic_position() != POS_RA_TO_BECOME_REPH)
576dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  {
57702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      unsigned int new_reph_pos;
57802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
57902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     enum reph_position_t {
58002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod       REPH_AFTER_MAIN, /* Malayalam, Oriya */
58102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod       REPH_BEFORE_SUBSCRIPT, /* Gurmukhi  */
58202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod       REPH_AFTER_SUBSCRIPT, /* Bengali */
58302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod       REPH_BEFORE_POSTSCRIPT, /* Devanagari, Gujarati  */
58402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod       REPH_AFTER_POSTSCRIPT, /* Kannada, Tamil, Telugu  */
58502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     } reph_pos = REPH_BEFORE_POSTSCRIPT; /* XXX */ /* XXX Figure out old behavior too */
58602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
587dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod    /*       1. If reph should be positioned after post-base consonant forms,
588dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          proceed to step 5.
58902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
59002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_step_1:
59102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
59202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      if (reph_pos == REPH_AFTER_POSTSCRIPT)
59302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	goto reph_step_5;
59402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
59502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
59602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       2. If the reph repositioning class is not after post-base: target
597dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          position is after the first explicit halant glyph between the
598dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first post-reph consonant and last main consonant. If ZWJ or ZWNJ
599dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          are following this halant, position is moved after it. If such
600dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          position is found, this is the target position. Otherwise,
601dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          proceed to the next step.
602dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *
603dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          Note: in old-implementation fonts, where classifications were
604dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          fixed in shaping engine, there was no case where reph position
605dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          will be found on this step.
60602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
60702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_step_2:
60802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
60902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      new_reph_pos = start + 1;
61002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      while (new_reph_pos < base && info[new_reph_pos].indic_category() != OT_H)
61102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	new_reph_pos++;
61202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
61302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      if (new_reph_pos < base && info[new_reph_pos].indic_category() == OT_H) {
61402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	/* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */
61502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1]))
61602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	  new_reph_pos++;
61702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	goto reph_move;
61802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      }
61902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
62002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
62102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       3. If reph should be repositioned after the main consonant: find the
622dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first consonant not ligated with main, or find the first
623dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          consonant that is not a potential pre-base reordering Ra.
62402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
62502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_step_3:
62602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
62702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      /* XXX */
62802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
62902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
63002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       4. If reph should be positioned before post-base consonant, find
631dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first post-base classified consonant not ligated with main. If no
632dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          consonant is found, the target position should be before the
633dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first matra, syllable modifier sign or vedic sign.
63402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
63502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_step_4:
63602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
63702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      /* XXX */
63802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
63902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
64002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       5. If no consonant is found in steps 3 or 4, move reph to a position
641dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          immediately before the first post-base matra, syllable modifier
642dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          sign or vedic sign that has a reordering class after the intended
643dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          reph position. For example, if the reordering position for reph
644dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          is post-main, it will skip above-base matras that also have a
645dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          post-main position.
646dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     */
64702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_step_5:
64802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
64902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      /* XXX */
65002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
651dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
65202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       6. Otherwise, reorder reph to the end of the syllable.
65302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
65402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_step_6:
65502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
65602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      new_reph_pos = end - 1;
65702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      while (new_reph_pos > start && info[new_reph_pos].indic_position() == POS_SMVD)
65802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	new_reph_pos--;
65902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
660892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod      /*
661892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * If the Reph is to be ending up after a Matra,Halant sequence,
662892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * position it before that Halant so it can interact with the Matra.
663892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * However, if it's a plain Consonant,Halant we shouldn't do that.
664892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * Uniscribe doesn't do this.
665892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * TEST: U+0930,U+094D,U+0915,U+094B,U+094D
666892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       */
667892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod      if (!options.uniscribe_bug_compatible &&
668892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod	  unlikely (info[new_reph_pos].indic_category() == OT_H)) {
66902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	for (unsigned int i = base + 1; i < new_reph_pos; i++)
67002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	  if (info[i].indic_category() == OT_M) {
67102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	    /* Ok, got it. */
67202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	    new_reph_pos--;
67302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	  }
67402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      }
67502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      goto reph_move;
6768df5636968389ac7bf8620ccd091fd4872b0bbeeBehdad Esfahbod    }
6778df5636968389ac7bf8620ccd091fd4872b0bbeeBehdad Esfahbod
67802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_move:
67902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
68002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      /* Move */
68102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      hb_glyph_info_t reph = info[start];
68202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      memmove (&info[start], &info[start + 1], (new_reph_pos - start) * sizeof (info[0]));
68302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      info[new_reph_pos] = reph;
68402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      start_of_last_cluster = start; /* Yay, one big cluster! */
68502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
686dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  }
687dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
688dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
689dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  /*   o Reorder pre-base reordering consonants:
690e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
691e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     If a pre-base reordering consonant is found, reorder it according to
692e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     the following rules:
693e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
694e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       1. Only reorder a glyph produced by substitution during application
695e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          of the feature. (Note that a font may shape a Ra consonant with
696e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          the feature generally but block it in certain contexts.)
697e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
698e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       2. Try to find a target position the same way as for pre-base matra.
699e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          If it is found, reorder pre-base consonant glyph.
700e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
701e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       3. If position is not found, reorder immediately before main
702e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          consonant.
703e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   */
704e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
70521d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod
70621d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod  /* Finish off the clusters and go home! */
70721d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod
708ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  if (!options.uniscribe_bug_compatible)
709ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  {
71021d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    /* This is what Uniscribe does.  Ie. add cluster boundaries after Halant,ZWNJ.
71121d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod     * This means, half forms are submerged into the main consonants cluster.
71221d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod     * This is unnecessary, and makes cursor positioning harder, but that's what
71321d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod     * Uniscribe does. */
71421d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    unsigned int cluster_start = start;
71521d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    for (unsigned int i = start + 1; i < start_of_last_cluster; i++)
71621d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod      if (info[i - 1].indic_category() == OT_H && info[i].indic_category() == OT_ZWNJ) {
71721d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod        i++;
71821d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod	buffer->merge_clusters (cluster_start, i);
71921d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod	cluster_start = i;
72021d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod      }
72121d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    start_of_last_cluster = cluster_start;
72221d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod  }
72321d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod
72421d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod  buffer->merge_clusters (start_of_last_cluster, end);
725ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod}
726e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
727e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
728ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodstatic void
729ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodfinal_reordering (const hb_ot_map_t *map,
730ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod		  hb_face_t *face,
731ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod		  hb_buffer_t *buffer,
732ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod		  void *user_data HB_UNUSED)
733ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod{
734ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  unsigned int count = buffer->len;
735ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  if (!count) return;
736ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod
737ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  hb_glyph_info_t *info = buffer->info;
738ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  unsigned int last = 0;
739cee7187447b76b22e1bb6136d137b35ac49c3a5dBehdad Esfahbod  unsigned int last_syllable = info[0].syllable();
740ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  for (unsigned int i = 1; i < count; i++)
741cee7187447b76b22e1bb6136d137b35ac49c3a5dBehdad Esfahbod    if (last_syllable != info[i].syllable()) {
742ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod      final_reordering_syllable (buffer, last, i);
743ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod      last = i;
744cee7187447b76b22e1bb6136d137b35ac49c3a5dBehdad Esfahbod      last_syllable = info[last].syllable();
745ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod    }
746ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  final_reordering_syllable (buffer, last, count);
747e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
748743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category);
749743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position);
750743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
751743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
752743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
753b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
754