hb-ot-shape-complex-indic.cc revision 1a1fa8c655a082fc1439608457ba717306cc83ca
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
27b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
283ed4634ec349fa9e943ad23718c04be4dd4bba62Behdad Esfahbod#include "hb-ot-shape-complex-indic-private.hh"
29352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod
30743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic int
31743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodcompare_codepoint (const void *pa, const void *pb)
32743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
33743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  hb_codepoint_t a = * (hb_codepoint_t *) pa;
34743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  hb_codepoint_t b = * (hb_codepoint_t *) pb;
35743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
36743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  return a < b ? -1 : a == b ? 0 : +1;
37743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
38743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
39743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic indic_position_t
40743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodconsonant_position (hb_codepoint_t u)
41743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
42743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  consonant_position_t *record;
43743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
44743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  record = (consonant_position_t *) bsearch (&u, consonant_positions,
45743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod					     ARRAY_LENGTH (consonant_positions),
46743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod					     sizeof (consonant_positions[0]),
47743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod					     compare_codepoint);
48743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
49dbccf87eef0d26838fa4bb3ae26410f6c4818836Behdad Esfahbod  return record ? record->position : POS_BASE_C;
50743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
51743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
52352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbodstatic bool
53352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbodis_ra (hb_codepoint_t u)
54352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod{
55352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod  return !!bsearch (&u, ra_chars,
56352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod		    ARRAY_LENGTH (ra_chars),
57352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod		    sizeof (ra_chars[0]),
58352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod		    compare_codepoint);
59352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod}
60352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod
619ee27a928a989c71923cef82a9e9828f8e9ca051Behdad Esfahbodstatic bool
629da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbodis_joiner (const hb_glyph_info_t &info)
639ee27a928a989c71923cef82a9e9828f8e9ca051Behdad Esfahbod{
649da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  return !!(FLAG (info.indic_category()) & (FLAG (OT_ZWJ) | FLAG (OT_ZWNJ)));
659da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod}
669da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
679da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbodstatic bool
689da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbodis_consonant (const hb_glyph_info_t &info)
699da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod{
701a1fa8c655a082fc1439608457ba717306cc83caBehdad Esfahbod  /* Note:
711a1fa8c655a082fc1439608457ba717306cc83caBehdad Esfahbod   *
721a1fa8c655a082fc1439608457ba717306cc83caBehdad Esfahbod   * We treat Vowels and NBSP as if they were consonants.  This is safe because Vowels
73c5306b6861cfaa50af40e8ceb058791fa06d7981Behdad Esfahbod   * cannot happen in a consonant syllable.  The plus side however is, we can call the
74c5306b6861cfaa50af40e8ceb058791fa06d7981Behdad Esfahbod   * consonant syllable logic from the vowel syllable function and get it all right! */
751a1fa8c655a082fc1439608457ba717306cc83caBehdad Esfahbod  return !!(FLAG (info.indic_category()) & (FLAG (OT_C) | FLAG (OT_Ra) | FLAG (OT_V) | FLAG (OT_NBSP)));
769ee27a928a989c71923cef82a9e9828f8e9ca051Behdad Esfahbod}
77c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod
78c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbodstatic const struct {
79c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  hb_tag_t tag;
80c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  hb_bool_t is_global;
81c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod} indic_basic_features[] =
82b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
83c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('n','u','k','t'), true},
84c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('a','k','h','n'), false},
85c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('r','p','h','f'), false},
861ac075b227090a9ad930dcc1670236c176b27067Behdad Esfahbod  {HB_TAG('r','k','r','f'), true},
87c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('p','r','e','f'), false},
88c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('b','l','w','f'), false},
89c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('h','a','l','f'), false},
90c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('v','a','t','u'), true},
91c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('p','s','t','f'), false},
929da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  {HB_TAG('c','j','c','t'), false},
93c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod};
94c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod
95c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod/* Same order as the indic_basic_features array */
96c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbodenum {
97c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  _NUKT,
98c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  AKHN,
99c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  RPHF,
100df6d45c693c417bf311e6fa49f18a8558542e525Behdad Esfahbod  _RKRF,
101c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  PREF,
102c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  BLWF,
103c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  HALF,
104c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  _VATU,
105c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  PSTF,
106e8eedf2687f05372bf5476e84139d01ba67c9f73Behdad Esfahbod  CJCT
107b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod};
108b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
109b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbodstatic const hb_tag_t indic_other_features[] =
110b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
111b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  HB_TAG('p','r','e','s'),
112b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  HB_TAG('a','b','v','s'),
113b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  HB_TAG('b','l','w','s'),
114b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  HB_TAG('p','s','t','s'),
115b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  HB_TAG('h','a','l','n'),
116b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
117b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  HB_TAG('d','i','s','t'),
118b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  HB_TAG('a','b','v','m'),
119b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  HB_TAG('b','l','w','m'),
120b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod};
121b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
122743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
123743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
124743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodinitial_reordering (const hb_ot_map_t *map,
125743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_face_t *face,
126743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_buffer_t *buffer,
127743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    void *user_data HB_UNUSED);
128f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbodstatic void
129f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbodfinal_reordering (const hb_ot_map_t *map,
130f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod		  hb_face_t *face,
131f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod		  hb_buffer_t *buffer,
132743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		  void *user_data HB_UNUSED);
133b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
134b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbodvoid
13576f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod_hb_ot_shape_complex_collect_features_indic (hb_ot_map_builder_t *map, const hb_segment_properties_t  *props)
136b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
137f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_bool_feature (HB_TAG('l','o','c','l'));
138a54a5505a35eef5315a8e2e7a79502901e3eff5fBehdad Esfahbod  /* The Indic specs do not require ccmp, but we apply it here since if
139a54a5505a35eef5315a8e2e7a79502901e3eff5fBehdad Esfahbod   * there is a use of it, it's typically at the beginning. */
140f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_bool_feature (HB_TAG('c','c','m','p'));
141f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
142743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  map->add_gsub_pause (initial_reordering, NULL);
143f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
144412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (indic_basic_features); i++) {
14576f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod    map->add_bool_feature (indic_basic_features[i].tag, indic_basic_features[i].is_global);
146412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod    map->add_gsub_pause (NULL, NULL);
147412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  }
148b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
149f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_gsub_pause (final_reordering, NULL);
150f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
151412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (indic_other_features); i++) {
15276f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod    map->add_bool_feature (indic_other_features[i], true);
153412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod    map->add_gsub_pause (NULL, NULL);
154412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  }
155b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod}
156b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
157867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
15811138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbodhb_ot_shape_normalization_mode_t
15911138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbod_hb_ot_shape_complex_normalization_preference_indic (void)
16002cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod{
16102cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod  /* We want split matras decomposed by the common shaping logic. */
16211138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbod  return HB_OT_SHAPE_NORMALIZATION_MODE_DECOMPOSED;
16302cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod}
16402cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod
165867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
166b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbodvoid
167acd88e659fdb2fa1cbf7171f0a1c7fbc81b9f298Behdad Esfahbod_hb_ot_shape_complex_setup_masks_indic (hb_ot_map_t *map, hb_buffer_t *buffer, hb_font_t *font)
168b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
1699f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod  HB_BUFFER_ALLOCATE_VAR (buffer, indic_category);
1709f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod  HB_BUFFER_ALLOCATE_VAR (buffer, indic_position);
171ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  HB_BUFFER_ALLOCATE_VAR (buffer, indic_syllable);
1729f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod
173743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* We cannot setup masks here.  We save information about characters
174743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * and setup masks later on in a pause-callback. */
175743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
176743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  unsigned int count = buffer->len;
177b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  for (unsigned int i = 0; i < count; i++)
178b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  {
17992332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    hb_glyph_info_t &info = buffer->info[i];
18092332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    unsigned int type = get_indic_categories (info.codepoint);
18192332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod
18292332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    info.indic_category() = type & 0x0F;
18392332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    info.indic_position() = type >> 4;
18492332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod
18592332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    if (info.indic_category() == OT_C) {
18692332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_position() = consonant_position (info.codepoint);
18792332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      if (is_ra (info.codepoint))
18892332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod	info.indic_category() = OT_Ra;
18992332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    } else if (info.indic_category() == OT_SM ||
19092332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod	       info.indic_category() == OT_VD) {
19192332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_position() = POS_SMVD;
19292332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    } else if (unlikely (info.codepoint == 0x200C))
19392332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_category() = OT_ZWNJ;
19492332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    else if (unlikely (info.codepoint == 0x200D))
19592332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_category() = OT_ZWJ;
19692332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod
19792332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    if (unlikely (info.codepoint == 0x0952)) {
19892332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_category() = OT_A;
19992332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_position() = POS_SMVD;
20033c92e769563ec2a6c1249b57d8cac742eea6f88Behdad Esfahbod    }
201b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  }
202743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
203b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
20445d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodstatic int
20545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodcompare_indic_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
20645d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod{
20745d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  int a = pa->indic_position();
20845d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  int b = pb->indic_position();
20945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
21045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  return a < b ? -1 : a == b ? 0 : +1;
21145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod}
212867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
213743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
214ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodinitial_reordering_consonant_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
215ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod				       unsigned int start, unsigned int end)
216743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
217743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  unsigned int i;
218ee58f3bc75d2d071a71b94063bf12205a5871acbBehdad Esfahbod  hb_glyph_info_t *info = buffer->info;
219743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
220743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Comments from:
221743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */
222743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
223743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 1. Find base consonant:
224743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
225743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * The shaping engine finds the base consonant of the syllable, using the
226743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * following algorithm: starting from the end of the syllable, move backwards
227743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * until a consonant is found that does not have a below-base or post-base
228743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * form (post-base forms have to follow below-base forms), or that is not a
229743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * pre-base reordering Ra, or arrive at the first consonant. The consonant
230743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * stopped at will be the base.
231743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
232743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o If the syllable starts with Ra + Halant (in a script that has Reph)
233743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *     and has more than one consonant, Ra is excluded from candidates for
234743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *     base consonants.
235743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
236743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
2375e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod  unsigned int base = end;
23876b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  bool has_reph = false;
239743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
24076b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
24176b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod   *    and has more than one consonant, Ra is excluded from candidates for
24276b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod   *    base consonants. */
2435e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod  unsigned int limit = start;
24476b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  if (mask_array[RPHF] &&
2456d8e0cb74c02f6bc09cd4abe9e4bc82062e1b517Behdad Esfahbod      start + 3 <= end &&
24676b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod      info[start].indic_category() == OT_Ra &&
2476d8e0cb74c02f6bc09cd4abe9e4bc82062e1b517Behdad Esfahbod      info[start + 1].indic_category() == OT_H &&
2486d8e0cb74c02f6bc09cd4abe9e4bc82062e1b517Behdad Esfahbod      !is_joiner (info[start + 2]))
24976b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  {
2505e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod    limit += 2;
2515e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod    base = start;
25276b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod    has_reph = true;
2535e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod  };
25476b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod
25576b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  /* -> starting from the end of the syllable, move backwards */
25676b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  i = end;
257743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  do {
258743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod    i--;
259743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod    /* -> until a consonant is found */
26097158392a5899ddb739afaac925128f33f699bd7Behdad Esfahbod    if (is_consonant (info[i]))
261743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod    {
262743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod      /* -> that does not have a below-base or post-base form
263743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod       * (post-base forms have to follow below-base forms), */
264dbccf87eef0d26838fa4bb3ae26410f6c4818836Behdad Esfahbod      if (info[i].indic_position() != POS_BELOW_C &&
265dbccf87eef0d26838fa4bb3ae26410f6c4818836Behdad Esfahbod	  info[i].indic_position() != POS_POST_C)
266743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod      {
267743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod        base = i;
268743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod	break;
269743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod      }
270743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
271cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod      /* -> or that is not a pre-base reordering Ra,
272cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod       *
273e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod       * TODO
274e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod       */
275e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
276cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod      /* -> or arrive at the first consonant. The consonant stopped at will
277cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod       * be the base. */
278743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod      base = i;
279743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod    }
2805e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod    else
2815e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod      if (is_joiner (info[i]))
2825e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod        break;
283cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod  } while (i > limit);
284743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  if (base < start)
285743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod    base = start; /* Just in case... */
286743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
2873d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod  /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
2883d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod   *    and has more than one consonant, Ra is excluded from candidates for
2893d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod   *    base consonants. */
2903d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod  if (has_reph && base == start) {
2913d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod    /* Have no other consonant, so Reph is not formed and Ra becomes base. */
2923d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod    has_reph = false;
2933d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod  }
2943d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod
2953d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod
296743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 2. Decompose and reorder Matras:
297743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
298743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * Each matra and any syllable modifier sign in the cluster are moved to the
299743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * appropriate position relative to the consonant(s) in the cluster. The
300743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * shaping engine decomposes two- or three-part matras into their constituent
301743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * parts before any repositioning. Matra characters are classified by which
302743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * consonant in a conjunct they have affinity for and are reordered to the
303743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * following positions:
304743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
305743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o Before first half form in the syllable
306743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After subjoined consonants
307743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After post-form consonant
308743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After main consonant (for above marks)
309743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
310743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * IMPLEMENTATION NOTES:
311743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
312743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * The normalize() routine has already decomposed matras for us, so we don't
313743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * need to worry about that.
314743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
315743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
316743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
317743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 3.  Reorder marks to canonical order:
318743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
319743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * Adjacent nukta and halant or nukta and vedic sign are always repositioned
320743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * if necessary, so that the nukta is first.
321743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
322743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * IMPLEMENTATION NOTES:
323743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
324743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * We don't need to do this: the normalize() routine already did this for us.
325743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
326743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
327743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
32845d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* Reorder characters */
32945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
33045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  for (i = start; i < base; i++)
331dbccf87eef0d26838fa4bb3ae26410f6c4818836Behdad Esfahbod    info[i].indic_position() = POS_PRE_C;
332dbccf87eef0d26838fa4bb3ae26410f6c4818836Behdad Esfahbod  info[base].indic_position() = POS_BASE_C;
33345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
334fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod  /* Handle beginning Ra */
3356d8e0cb74c02f6bc09cd4abe9e4bc82062e1b517Behdad Esfahbod  if (has_reph) {
336d4480ace7fdbe48aeaf77278c032b8b2ef2ebe8eBehdad Esfahbod    info[start].indic_position() = POS_REPH;
337fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod    info[start].mask = mask_array[RPHF];
3386d8e0cb74c02f6bc09cd4abe9e4bc82062e1b517Behdad Esfahbod  }
339fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod
340f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  /* For old-style Indic script tags, move the first post-base Halant after
341f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod   * last consonant. */
342f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  if ((map->get_chosen_script (0) & 0x000000FF) != '2') {
343f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod    /* We should only do this for Indic scripts which have a version two I guess. */
344f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod    for (i = base + 1; i < end; i++)
345f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod      if (info[i].indic_category() == OT_H) {
346f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        unsigned int j;
347f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        for (j = end - 1; j > i; j--)
348190eb31a16178269aecaf5d2ecc9012f956749f4Behdad Esfahbod	  if (is_consonant (info[j]))
349f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	    break;
350f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	if (j > i) {
351f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  /* Move Halant to after last consonant. */
352f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  hb_glyph_info_t t = info[i];
353f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  memmove (&info[i], &info[i + 1], (j - i) * sizeof (info[0]));
354f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  info[j] = t;
355f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	}
356f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        break;
357f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod      }
358f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  }
359f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod
36045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* Attach ZWJ, ZWNJ, nukta, and halant to previous char to move with them. */
36145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  for (i = start + 1; i < end; i++)
362ee58f3bc75d2d071a71b94063bf12205a5871acbBehdad Esfahbod    if ((FLAG (info[i].indic_category()) &
36345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod	 (FLAG (OT_ZWNJ) | FLAG (OT_ZWJ) | FLAG (OT_N) | FLAG (OT_H))))
364ee58f3bc75d2d071a71b94063bf12205a5871acbBehdad Esfahbod      info[i].indic_position() = info[i - 1].indic_position();
36545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
36645d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* We do bubble-sort, skip malicious clusters attempts */
367b99d63ae114fb58f129562b293a8a66543d499adBehdad Esfahbod  if (end - start < 64)
368a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod  {
369a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    /* Sit tight, rock 'n roll! */
370d3637edb248162970e202e9d0671540274192844Behdad Esfahbod    hb_bubble_sort (info + start, end - start, compare_indic_order);
371a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    /* Find base again */
372a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    base = end;
373a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    for (i = start; i < end; i++)
374a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod      if (info[i].indic_position() == POS_BASE_C) {
375a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod        base = i;
376a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod	break;
377a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod      }
378a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod  }
37945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
380743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Setup masks now */
381743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
382281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod  {
383281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    hb_mask_t mask;
384281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod
385281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Pre-base */
386281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    mask = mask_array[HALF] | mask_array[AKHN] | mask_array[CJCT];
387281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    for (i = start; i < base; i++)
388281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod      info[i].mask  |= mask;
389281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Base */
390281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    mask = mask_array[AKHN] | mask_array[CJCT];
391281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    info[base].mask |= mask;
392281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Post-base */
393281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    mask = mask_array[BLWF] | mask_array[PSTF] | mask_array[CJCT];
394281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    for (i = base + 1; i < end; i++)
395281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod      info[i].mask  |= mask;
396281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod  }
3979da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
3989da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  /* Apply ZWJ/ZWNJ effects */
3999da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  for (i = start + 1; i < end; i++)
4009da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    if (is_joiner (info[i])) {
4019da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      bool non_joiner = info[i].indic_category() == OT_ZWNJ;
4026b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod      unsigned int j = i;
4039da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
4049da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      do {
4059da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod	j--;
4066b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod
407a91c58bf98258a34e5f7c1ad39a38db58fadc4b9Behdad Esfahbod	/* Reading the Unicode and OpenType specs, I think the following line
408a91c58bf98258a34e5f7c1ad39a38db58fadc4b9Behdad Esfahbod	 * is correct, but this is not what the test suite expects currently.
409a91c58bf98258a34e5f7c1ad39a38db58fadc4b9Behdad Esfahbod	 * The test suite has been drinking, not me...  But disable while
410a91c58bf98258a34e5f7c1ad39a38db58fadc4b9Behdad Esfahbod	 * investigating.
411a91c58bf98258a34e5f7c1ad39a38db58fadc4b9Behdad Esfahbod	 */
412a91c58bf98258a34e5f7c1ad39a38db58fadc4b9Behdad Esfahbod	//info[j].mask &= !mask_array[CJCT];
4136b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod	if (non_joiner)
4146b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod	  info[j].mask &= !mask_array[HALF];
4156b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod
4169da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      } while (j > start && !is_consonant (info[j]));
4179da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    }
418743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
419743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
420743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
421743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
422ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodinitial_reordering_vowel_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
423ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod				   unsigned int start, unsigned int end)
424743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
425c5306b6861cfaa50af40e8ceb058791fa06d7981Behdad Esfahbod  /* We made the vowels look like consonants.  So let's call the consonant logic! */
426c5306b6861cfaa50af40e8ceb058791fa06d7981Behdad Esfahbod  initial_reordering_consonant_syllable (map, buffer, mask_array, start, end);
427743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
428743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
429743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
430ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodinitial_reordering_standalone_cluster (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
431ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod				       unsigned int start, unsigned int end)
432743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
4331a1fa8c655a082fc1439608457ba717306cc83caBehdad Esfahbod  /* We made the vowels look like consonants.  So let's call the consonant logic! */
4341a1fa8c655a082fc1439608457ba717306cc83caBehdad Esfahbod  initial_reordering_consonant_syllable (map, buffer, mask_array, start, end);
435743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
436743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
437743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
438ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodinitial_reordering_non_indic (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
439743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		 unsigned int start, unsigned int end)
440743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
441743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Nothing to do right now.  If we ever switch to using the output
442743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * buffer in the reordering process, we'd need to next_glyph() here. */
443743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
444743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
445743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod#include "hb-ot-shape-complex-indic-machine.hh"
446743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
447743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
448743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodinitial_reordering (const hb_ot_map_t *map,
449743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_face_t *face,
450743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_buffer_t *buffer,
451743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    void *user_data HB_UNUSED)
452743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
453b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  hb_mask_t mask_array[ARRAY_LENGTH (indic_basic_features)] = {0};
454b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  unsigned int num_masks = ARRAY_LENGTH (indic_basic_features);
455b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  for (unsigned int i = 0; i < num_masks; i++)
45676f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod    mask_array[i] = map->get_1_mask (indic_basic_features[i].tag);
457743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
458743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  find_syllables (map, buffer, mask_array);
459b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod}
460b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
461743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
462ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodfinal_reordering_syllable (hb_buffer_t *buffer,
463ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod			   unsigned int start, unsigned int end)
464743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
465e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod  /* 4. Final reordering:
466e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
467e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * After the localized forms and basic shaping forms GSUB features have been
468e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * applied (see below), the shaping engine performs some final glyph
469e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * reordering before applying all the remaining font features to the entire
470e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * cluster.
471e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
472e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *   o Reorder matras:
473e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
474e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     If a pre-base matra character had been reordered before applying basic
475e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     features, the glyph can be moved closer to the main consonant based on
476e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     whether half-forms had been formed. Actual position for the matra is
477e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     defined as “after last standalone halant glyph, after initial matra
478e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     position and before the main consonant”. If ZWJ or ZWNJ follow this
479e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     halant, position is moved after it.
480e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
481e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *   o Reorder reph:
482e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
483e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     Reph’s original position is always at the beginning of the syllable,
484e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     (i.e. it is not reordered at the character reordering stage). However,
485e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     it will be reordered according to the basic-forms shaping results.
486e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     Possible positions for reph, depending on the script, are; after main,
487e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     before post-base consonant forms, and after post-base consonant forms.
488e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
489e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       1. If reph should be positioned after post-base consonant forms,
490e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          proceed to step 5.
491e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
492e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       2. If the reph repositioning class is not after post-base: target
493e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          position is after the first explicit halant glyph between the
494e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          first post-reph consonant and last main consonant. If ZWJ or ZWNJ
495e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          are following this halant, position is moved after it. If such
496e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          position is found, this is the target position. Otherwise,
497e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          proceed to the next step.
498e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
499e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          Note: in old-implementation fonts, where classifications were
500e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          fixed in shaping engine, there was no case where reph position
501e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          will be found on this step.
502e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
503e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       3. If reph should be repositioned after the main consonant: from the
504e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          first consonant not ligated with main, or find the first
505e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          consonant that is not a potential pre-base reordering Ra.
506e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
507e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
508e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       4. If reph should be positioned before post-base consonant, find
509e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          first post-base classified consonant not ligated with main. If no
510e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          consonant is found, the target position should be before the
511e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          first matra, syllable modifier sign or vedic sign.
512e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
513e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       5. If no consonant is found in steps 3 or 4, move reph to a position
514e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          immediately before the first post-base matra, syllable modifier
515e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          sign or vedic sign that has a reordering class after the intended
516e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          reph position. For example, if the reordering position for reph
517e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          is post-main, it will skip above-base matras that also have a
518e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          post-main position.
519e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
520e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       6. Otherwise, reorder reph to the end of the syllable.
521e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
522e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *   o Reorder pre-base reordering consonants:
523e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
524e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     If a pre-base reordering consonant is found, reorder it according to
525e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     the following rules:
526e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
527e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       1. Only reorder a glyph produced by substitution during application
528e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          of the feature. (Note that a font may shape a Ra consonant with
529e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          the feature generally but block it in certain contexts.)
530e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
531e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       2. Try to find a target position the same way as for pre-base matra.
532e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          If it is found, reorder pre-base consonant glyph.
533e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
534e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       3. If position is not found, reorder immediately before main
535e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          consonant.
536e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   */
537e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
538e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod  /* TODO */
539ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  buffer->merge_clusters (start, end);
540ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod}
541e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
542e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
543ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodstatic void
544ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodfinal_reordering (const hb_ot_map_t *map,
545ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod		  hb_face_t *face,
546ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod		  hb_buffer_t *buffer,
547ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod		  void *user_data HB_UNUSED)
548ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod{
549ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  unsigned int count = buffer->len;
550ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  if (!count) return;
551ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod
552ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  hb_glyph_info_t *info = buffer->info;
553ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  unsigned int last = 0;
554ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  unsigned int last_syllable = info[0].indic_syllable();
555ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  for (unsigned int i = 1; i < count; i++)
556ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod    if (last_syllable != info[i].indic_syllable()) {
557ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod      final_reordering_syllable (buffer, last, i);
558ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod      last = i;
559ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod      last_syllable = info[last].indic_syllable();
560ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod    }
561ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  final_reordering_syllable (buffer, last, count);
562e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
563ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_syllable);
564743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category);
565743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position);
566743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
567743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
568743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
569b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
570