hb-ot-shape-complex-indic.cc revision dbccf87eef0d26838fa4bb3ae26410f6c4818836
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{
709da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  return !!(FLAG (info.indic_category()) & (FLAG (OT_C) | FLAG (OT_Ra)));
719ee27a928a989c71923cef82a9e9828f8e9ca051Behdad Esfahbod}
72c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod
73c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbodstatic const struct {
74c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  hb_tag_t tag;
75c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  hb_bool_t is_global;
76c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod} indic_basic_features[] =
77b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
78c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('n','u','k','t'), true},
79c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('a','k','h','n'), false},
80c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('r','p','h','f'), false},
811ac075b227090a9ad930dcc1670236c176b27067Behdad Esfahbod  {HB_TAG('r','k','r','f'), true},
82c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('p','r','e','f'), false},
83c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('b','l','w','f'), false},
84c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('h','a','l','f'), false},
85c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('v','a','t','u'), true},
86c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('p','s','t','f'), false},
879da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  {HB_TAG('c','j','c','t'), false},
88c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod};
89c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod
90c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod/* Same order as the indic_basic_features array */
91c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbodenum {
92c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  _NUKT,
93c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  AKHN,
94c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  RPHF,
95df6d45c693c417bf311e6fa49f18a8558542e525Behdad Esfahbod  _RKRF,
96c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  PREF,
97c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  BLWF,
98c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  HALF,
99c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  _VATU,
100c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  PSTF,
101e8eedf2687f05372bf5476e84139d01ba67c9f73Behdad Esfahbod  CJCT
102b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod};
103b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
104b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbodstatic const hb_tag_t indic_other_features[] =
105b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
106b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  HB_TAG('p','r','e','s'),
107b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  HB_TAG('a','b','v','s'),
108b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  HB_TAG('b','l','w','s'),
109b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  HB_TAG('p','s','t','s'),
110b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  HB_TAG('h','a','l','n'),
111b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
112b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  HB_TAG('d','i','s','t'),
113b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  HB_TAG('a','b','v','m'),
114b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  HB_TAG('b','l','w','m'),
115b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod};
116b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
117743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
118743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
119743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodinitial_reordering (const hb_ot_map_t *map,
120743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_face_t *face,
121743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_buffer_t *buffer,
122743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    void *user_data HB_UNUSED);
123f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbodstatic void
124f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbodfinal_reordering (const hb_ot_map_t *map,
125f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod		  hb_face_t *face,
126f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod		  hb_buffer_t *buffer,
127743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		  void *user_data HB_UNUSED);
128b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
129b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbodvoid
13076f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod_hb_ot_shape_complex_collect_features_indic (hb_ot_map_builder_t *map, const hb_segment_properties_t  *props)
131b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
132f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_bool_feature (HB_TAG('l','o','c','l'));
133a54a5505a35eef5315a8e2e7a79502901e3eff5fBehdad Esfahbod  /* The Indic specs do not require ccmp, but we apply it here since if
134a54a5505a35eef5315a8e2e7a79502901e3eff5fBehdad Esfahbod   * there is a use of it, it's typically at the beginning. */
135f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_bool_feature (HB_TAG('c','c','m','p'));
136f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
137743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  map->add_gsub_pause (initial_reordering, NULL);
138f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
139412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (indic_basic_features); i++) {
14076f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod    map->add_bool_feature (indic_basic_features[i].tag, indic_basic_features[i].is_global);
141412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod    map->add_gsub_pause (NULL, NULL);
142412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  }
143b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
144f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_gsub_pause (final_reordering, NULL);
145f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
146412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (indic_other_features); i++) {
14776f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod    map->add_bool_feature (indic_other_features[i], true);
148412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod    map->add_gsub_pause (NULL, NULL);
149412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  }
150b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod}
151b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
152867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
15311138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbodhb_ot_shape_normalization_mode_t
15411138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbod_hb_ot_shape_complex_normalization_preference_indic (void)
15502cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod{
15602cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod  /* We want split matras decomposed by the common shaping logic. */
15711138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbod  return HB_OT_SHAPE_NORMALIZATION_MODE_DECOMPOSED;
15802cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod}
15902cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod
160867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
161b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbodvoid
162acd88e659fdb2fa1cbf7171f0a1c7fbc81b9f298Behdad Esfahbod_hb_ot_shape_complex_setup_masks_indic (hb_ot_map_t *map, hb_buffer_t *buffer, hb_font_t *font)
163b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
1649f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod  HB_BUFFER_ALLOCATE_VAR (buffer, indic_category);
1659f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod  HB_BUFFER_ALLOCATE_VAR (buffer, indic_position);
1669f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod
167743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* We cannot setup masks here.  We save information about characters
168743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * and setup masks later on in a pause-callback. */
169743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
170743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  unsigned int count = buffer->len;
171b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  for (unsigned int i = 0; i < count; i++)
172b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  {
17376f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod    unsigned int type = get_indic_categories (buffer->info[i].codepoint);
174b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
17576f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod    buffer->info[i].indic_category() = type & 0x0F;
176d4480ace7fdbe48aeaf77278c032b8b2ef2ebe8eBehdad Esfahbod    buffer->info[i].indic_position() = type >> 4;
177743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
178352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod    if (buffer->info[i].indic_category() == OT_C) {
179743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod      buffer->info[i].indic_position() = consonant_position (buffer->info[i].codepoint);
180352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod      if (is_ra (buffer->info[i].codepoint))
181352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod	buffer->info[i].indic_category() = OT_Ra;
1829034641333d7bfb41a0784cce72e43591faea083Behdad Esfahbod    } else if (buffer->info[i].indic_category() == OT_SM ||
1839034641333d7bfb41a0784cce72e43591faea083Behdad Esfahbod	       buffer->info[i].indic_category() == OT_VD) {
184d4480ace7fdbe48aeaf77278c032b8b2ef2ebe8eBehdad Esfahbod      buffer->info[i].indic_position() = POS_SMVD;
18533c92e769563ec2a6c1249b57d8cac742eea6f88Behdad Esfahbod    } else if (unlikely (buffer->info[i].codepoint == 0x200C))
1869da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      buffer->info[i].indic_category() = OT_ZWNJ;
18733c92e769563ec2a6c1249b57d8cac742eea6f88Behdad Esfahbod    else if (unlikely (buffer->info[i].codepoint == 0x200D))
1889da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      buffer->info[i].indic_category() = OT_ZWJ;
18933c92e769563ec2a6c1249b57d8cac742eea6f88Behdad Esfahbod
19033c92e769563ec2a6c1249b57d8cac742eea6f88Behdad Esfahbod    if (unlikely (buffer->info[i].codepoint == 0x0952)) {
19133c92e769563ec2a6c1249b57d8cac742eea6f88Behdad Esfahbod      buffer->info[i].indic_category() = OT_A;
192dbccf87eef0d26838fa4bb3ae26410f6c4818836Behdad Esfahbod      buffer->info[i].indic_position() = POS_SMVD;
19333c92e769563ec2a6c1249b57d8cac742eea6f88Behdad Esfahbod    }
194b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  }
195743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
196b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
19745d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodstatic int
19845d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodcompare_indic_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
19945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod{
20045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  int a = pa->indic_position();
20145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  int b = pb->indic_position();
20245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
20345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  return a < b ? -1 : a == b ? 0 : +1;
20445d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod}
205867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
206743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
207743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodfound_consonant_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
208743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod			  unsigned int start, unsigned int end)
209743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
210743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  unsigned int i;
211ee58f3bc75d2d071a71b94063bf12205a5871acbBehdad Esfahbod  hb_glyph_info_t *info = buffer->info;
212743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
213743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Comments from:
214743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */
215743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
216743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 1. Find base consonant:
217743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
218743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * The shaping engine finds the base consonant of the syllable, using the
219743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * following algorithm: starting from the end of the syllable, move backwards
220743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * until a consonant is found that does not have a below-base or post-base
221743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * form (post-base forms have to follow below-base forms), or that is not a
222743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * pre-base reordering Ra, or arrive at the first consonant. The consonant
223743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * stopped at will be the base.
224743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
225743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o If the syllable starts with Ra + Halant (in a script that has Reph)
226743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *     and has more than one consonant, Ra is excluded from candidates for
227743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *     base consonants.
228743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
229743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
2305e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod  unsigned int base = end;
23176b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  bool has_reph = false;
232743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
23376b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
23476b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod   *    and has more than one consonant, Ra is excluded from candidates for
23576b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod   *    base consonants. */
2365e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod  unsigned int limit = start;
23776b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  if (mask_array[RPHF] &&
23876b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod      start + 2 < end &&
23976b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod      info[start].indic_category() == OT_Ra &&
24076b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod      info[start + 1].indic_category() == OT_H)
24176b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  {
2425e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod    limit += 2;
2435e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod    base = start;
24476b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod    has_reph = true;
2455e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod  };
24676b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod
24776b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  /* -> starting from the end of the syllable, move backwards */
24876b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  i = end;
249743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  do {
250743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod    i--;
251743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod    /* -> until a consonant is found */
25297158392a5899ddb739afaac925128f33f699bd7Behdad Esfahbod    if (is_consonant (info[i]))
253743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod    {
254743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod      /* -> that does not have a below-base or post-base form
255743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod       * (post-base forms have to follow below-base forms), */
256dbccf87eef0d26838fa4bb3ae26410f6c4818836Behdad Esfahbod      if (info[i].indic_position() != POS_BELOW_C &&
257dbccf87eef0d26838fa4bb3ae26410f6c4818836Behdad Esfahbod	  info[i].indic_position() != POS_POST_C)
258743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod      {
259743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod        base = i;
260743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod	break;
261743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod      }
262743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
263cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod      /* -> or that is not a pre-base reordering Ra,
264cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod       *
265e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod       * TODO
266e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod       */
267e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
268e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod      /* ->  o If the syllable starts with Ra + Halant (in a script that has Reph)
269e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod       *       and has more than one consonant, Ra is excluded from candidates for
270e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod       *       base consonants.
271cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod       *
272cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod       * IMPLEMENTATION NOTES:
273cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod       *
274cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod       * We do this by adjusting limit accordingly before entering the loop.
275cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod       */
276cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod
277cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod      /* -> or arrive at the first consonant. The consonant stopped at will
278cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod       * be the base. */
279743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod      base = i;
280743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod    }
2815e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod    else
2825e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod      if (is_joiner (info[i]))
2835e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod        break;
284cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod  } while (i > limit);
285743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  if (base < start)
286743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod    base = start; /* Just in case... */
287743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
288743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
289743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 2. Decompose and reorder Matras:
290743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
291743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * Each matra and any syllable modifier sign in the cluster are moved to the
292743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * appropriate position relative to the consonant(s) in the cluster. The
293743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * shaping engine decomposes two- or three-part matras into their constituent
294743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * parts before any repositioning. Matra characters are classified by which
295743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * consonant in a conjunct they have affinity for and are reordered to the
296743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * following positions:
297743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
298743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o Before first half form in the syllable
299743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After subjoined consonants
300743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After post-form consonant
301743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After main consonant (for above marks)
302743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
303743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * IMPLEMENTATION NOTES:
304743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
305743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * The normalize() routine has already decomposed matras for us, so we don't
306743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * need to worry about that.
307743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
308743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
309743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
310743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 3.  Reorder marks to canonical order:
311743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
312743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * Adjacent nukta and halant or nukta and vedic sign are always repositioned
313743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * if necessary, so that the nukta is first.
314743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
315743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * IMPLEMENTATION NOTES:
316743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
317743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * We don't need to do this: the normalize() routine already did this for us.
318743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
319743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
320743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
32145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* Reorder characters */
32245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
32345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  for (i = start; i < base; i++)
324dbccf87eef0d26838fa4bb3ae26410f6c4818836Behdad Esfahbod    info[i].indic_position() = POS_PRE_C;
325dbccf87eef0d26838fa4bb3ae26410f6c4818836Behdad Esfahbod  info[base].indic_position() = POS_BASE_C;
32645d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
327fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod
328fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod  /* Handle beginning Ra */
32976b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  if (has_reph &&
33076b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod      start + 3 <= end &&
3310d8f8a177c4bfd4dc642a353bab8d03674e839acBehdad Esfahbod      !is_joiner (info[start + 2]))
332fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod   {
333d4480ace7fdbe48aeaf77278c032b8b2ef2ebe8eBehdad Esfahbod    info[start].indic_position() = POS_REPH;
334fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod    info[start].mask = mask_array[RPHF];
335fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod   }
336fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod
337f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  /* For old-style Indic script tags, move the first post-base Halant after
338f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod   * last consonant. */
339f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  if ((map->get_chosen_script (0) & 0x000000FF) != '2') {
340f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod    /* We should only do this for Indic scripts which have a version two I guess. */
341f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod    for (i = base + 1; i < end; i++)
342f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod      if (info[i].indic_category() == OT_H) {
343f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        unsigned int j;
344f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        for (j = end - 1; j > i; j--)
345f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  if ((FLAG (info[j].indic_category()) & (FLAG (OT_C) | FLAG (OT_Ra))))
346f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	    break;
347f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	if (j > i) {
348f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  /* Move Halant to after last consonant. */
349f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  hb_glyph_info_t t = info[i];
350f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  memmove (&info[i], &info[i + 1], (j - i) * sizeof (info[0]));
351f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  info[j] = t;
352f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	}
353f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        break;
354f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod      }
355f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  }
356f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod
35745d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* Attach ZWJ, ZWNJ, nukta, and halant to previous char to move with them. */
35845d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  for (i = start + 1; i < end; i++)
359ee58f3bc75d2d071a71b94063bf12205a5871acbBehdad Esfahbod    if ((FLAG (info[i].indic_category()) &
36045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod	 (FLAG (OT_ZWNJ) | FLAG (OT_ZWJ) | FLAG (OT_N) | FLAG (OT_H))))
361ee58f3bc75d2d071a71b94063bf12205a5871acbBehdad Esfahbod      info[i].indic_position() = info[i - 1].indic_position();
36245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
36345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* We do bubble-sort, skip malicious clusters attempts */
36445d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  if (end - start > 20)
36545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod    return;
36645d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
36745d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* Sit tight, rock 'n roll! */
368ee58f3bc75d2d071a71b94063bf12205a5871acbBehdad Esfahbod  hb_bubble_sort (info + start, end - start, compare_indic_order);
36945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
370743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Setup masks now */
371743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
372281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod  {
373281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    hb_mask_t mask;
374281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod
375281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Pre-base */
376281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    mask = mask_array[HALF] | mask_array[AKHN] | mask_array[CJCT];
377281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    for (i = start; i < base; i++)
378281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod      info[i].mask  |= mask;
379281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Base */
380281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    mask = mask_array[AKHN] | mask_array[CJCT];
381281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    info[base].mask |= mask;
382281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Post-base */
383281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    mask = mask_array[BLWF] | mask_array[PSTF] | mask_array[CJCT];
384281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    for (i = base + 1; i < end; i++)
385281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod      info[i].mask  |= mask;
386281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod  }
3879da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
3889da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  /* Apply ZWJ/ZWNJ effects */
3899da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  for (i = start + 1; i < end; i++)
3909da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    if (is_joiner (info[i])) {
3919da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      bool non_joiner = info[i].indic_category() == OT_ZWNJ;
3926b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod      unsigned int j = i;
3939da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
3949da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      do {
3959da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod	j--;
3966b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod
397a91c58bf98258a34e5f7c1ad39a38db58fadc4b9Behdad Esfahbod	/* Reading the Unicode and OpenType specs, I think the following line
398a91c58bf98258a34e5f7c1ad39a38db58fadc4b9Behdad Esfahbod	 * is correct, but this is not what the test suite expects currently.
399a91c58bf98258a34e5f7c1ad39a38db58fadc4b9Behdad Esfahbod	 * The test suite has been drinking, not me...  But disable while
400a91c58bf98258a34e5f7c1ad39a38db58fadc4b9Behdad Esfahbod	 * investigating.
401a91c58bf98258a34e5f7c1ad39a38db58fadc4b9Behdad Esfahbod	 */
402a91c58bf98258a34e5f7c1ad39a38db58fadc4b9Behdad Esfahbod	//info[j].mask &= !mask_array[CJCT];
4036b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod	if (non_joiner)
4046b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod	  info[j].mask &= !mask_array[HALF];
4056b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod
4069da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      } while (j > start && !is_consonant (info[j]));
4079da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    }
408743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
409743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
410743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
411743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
412743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodfound_vowel_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
413743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		      unsigned int start, unsigned int end)
414743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
415743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* TODO
416743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * Not clear to me how this should work.  Do the matras move to before the
417743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * independent vowel?  No idea.
418743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
419743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
420743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
421743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
422743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodfound_standalone_cluster (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
423743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod			  unsigned int start, unsigned int end)
424743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
425743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* TODO
426743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * Easiest thing to do here is to convert the NBSP to consonant and
427743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * call found_consonant_syllable.
428743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
429743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
430743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
431743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
432743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodfound_non_indic (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
433743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		 unsigned int start, unsigned int end)
434743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
435743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Nothing to do right now.  If we ever switch to using the output
436743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * buffer in the reordering process, we'd need to next_glyph() here. */
437743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
438743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
439743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod#include "hb-ot-shape-complex-indic-machine.hh"
440743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
441743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
442743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodinitial_reordering (const hb_ot_map_t *map,
443743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_face_t *face,
444743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_buffer_t *buffer,
445743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    void *user_data HB_UNUSED)
446743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
447b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  hb_mask_t mask_array[ARRAY_LENGTH (indic_basic_features)] = {0};
448b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  unsigned int num_masks = ARRAY_LENGTH (indic_basic_features);
449b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  for (unsigned int i = 0; i < num_masks; i++)
45076f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod    mask_array[i] = map->get_1_mask (indic_basic_features[i].tag);
451743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
452743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  find_syllables (map, buffer, mask_array);
453b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod}
454b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
455743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
456743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodfinal_reordering (const hb_ot_map_t *map,
457743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		  hb_face_t *face,
458743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		  hb_buffer_t *buffer,
459743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		  void *user_data HB_UNUSED)
460743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
461e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod  /* 4. Final reordering:
462e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
463e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * After the localized forms and basic shaping forms GSUB features have been
464e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * applied (see below), the shaping engine performs some final glyph
465e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * reordering before applying all the remaining font features to the entire
466e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * cluster.
467e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
468e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *   o Reorder matras:
469e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
470e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     If a pre-base matra character had been reordered before applying basic
471e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     features, the glyph can be moved closer to the main consonant based on
472e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     whether half-forms had been formed. Actual position for the matra is
473e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     defined as “after last standalone halant glyph, after initial matra
474e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     position and before the main consonant”. If ZWJ or ZWNJ follow this
475e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     halant, position is moved after it.
476e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
477e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *   o Reorder reph:
478e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
479e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     Reph’s original position is always at the beginning of the syllable,
480e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     (i.e. it is not reordered at the character reordering stage). However,
481e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     it will be reordered according to the basic-forms shaping results.
482e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     Possible positions for reph, depending on the script, are; after main,
483e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     before post-base consonant forms, and after post-base consonant forms.
484e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
485e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       1. If reph should be positioned after post-base consonant forms,
486e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          proceed to step 5.
487e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
488e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       2. If the reph repositioning class is not after post-base: target
489e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          position is after the first explicit halant glyph between the
490e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          first post-reph consonant and last main consonant. If ZWJ or ZWNJ
491e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          are following this halant, position is moved after it. If such
492e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          position is found, this is the target position. Otherwise,
493e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          proceed to the next step.
494e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
495e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          Note: in old-implementation fonts, where classifications were
496e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          fixed in shaping engine, there was no case where reph position
497e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          will be found on this step.
498e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
499e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       3. If reph should be repositioned after the main consonant: from the
500e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          first consonant not ligated with main, or find the first
501e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          consonant that is not a potential pre-base reordering Ra.
502e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
503e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
504e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       4. If reph should be positioned before post-base consonant, find
505e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          first post-base classified consonant not ligated with main. If no
506e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          consonant is found, the target position should be before the
507e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          first matra, syllable modifier sign or vedic sign.
508e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
509e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       5. If no consonant is found in steps 3 or 4, move reph to a position
510e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          immediately before the first post-base matra, syllable modifier
511e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          sign or vedic sign that has a reordering class after the intended
512e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          reph position. For example, if the reordering position for reph
513e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          is post-main, it will skip above-base matras that also have a
514e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          post-main position.
515e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
516e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       6. Otherwise, reorder reph to the end of the syllable.
517e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
518e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *   o Reorder pre-base reordering consonants:
519e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
520e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     If a pre-base reordering consonant is found, reorder it according to
521e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     the following rules:
522e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
523e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       1. Only reorder a glyph produced by substitution during application
524e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          of the feature. (Note that a font may shape a Ra consonant with
525e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          the feature generally but block it in certain contexts.)
526e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
527e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       2. Try to find a target position the same way as for pre-base matra.
528e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          If it is found, reorder pre-base consonant glyph.
529e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
530e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       3. If position is not found, reorder immediately before main
531e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          consonant.
532e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   */
533e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
534e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod  /* TODO */
535e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
536e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
537e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
538743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category);
539743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position);
540743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
541743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
542743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
543b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
544