hb-ot-shape-complex-indic.cc revision df6d45c693c417bf311e6fa49f18a8558542e525
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
49743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  return record ? record->position : POS_BASE;
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;
176a06411ecf93c7e5256e363eef3ef69554896dd55Behdad Esfahbod    buffer->info[i].indic_position() = (type >> 4) & 7;
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;
1829da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    } else if (buffer->info[i].codepoint == 0x200C)
1839da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      buffer->info[i].indic_category() = OT_ZWNJ;
1849da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    else if (buffer->info[i].codepoint == 0x200D)
1859da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      buffer->info[i].indic_category() = OT_ZWJ;
186b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  }
187743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
188b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
18945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodstatic int
19045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodcompare_indic_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
19145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod{
19245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  int a = pa->indic_position();
19345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  int b = pb->indic_position();
19445d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
19545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  return a < b ? -1 : a == b ? 0 : +1;
19645d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod}
197867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
198743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
199743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodfound_consonant_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
200743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod			  unsigned int start, unsigned int end)
201743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
202743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  unsigned int i;
203ee58f3bc75d2d071a71b94063bf12205a5871acbBehdad Esfahbod  hb_glyph_info_t *info = buffer->info;
204743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
205743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Comments from:
206743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */
207743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
208743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 1. Find base consonant:
209743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
210743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * The shaping engine finds the base consonant of the syllable, using the
211743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * following algorithm: starting from the end of the syllable, move backwards
212743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * until a consonant is found that does not have a below-base or post-base
213743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * form (post-base forms have to follow below-base forms), or that is not a
214743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * pre-base reordering Ra, or arrive at the first consonant. The consonant
215743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * stopped at will be the base.
216743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
217743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o If the syllable starts with Ra + Halant (in a script that has Reph)
218743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *     and has more than one consonant, Ra is excluded from candidates for
219743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *     base consonants.
220743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
221743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
2225e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod  unsigned int base = end;
223743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
224743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* -> starting from the end of the syllable, move backwards */
225743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  i = end;
2265e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod  unsigned int limit = start;
2275e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod  if (info[start].indic_category() == OT_Ra && start + 2 <= end) {
2285e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod    limit += 2;
2295e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod    base = start;
2305e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod  };
231743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  do {
232743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod    i--;
233743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod    /* -> until a consonant is found */
23497158392a5899ddb739afaac925128f33f699bd7Behdad Esfahbod    if (is_consonant (info[i]))
235743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod    {
236743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod      /* -> that does not have a below-base or post-base form
237743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod       * (post-base forms have to follow below-base forms), */
238ee58f3bc75d2d071a71b94063bf12205a5871acbBehdad Esfahbod      if (info[i].indic_position() != POS_BELOW &&
239ee58f3bc75d2d071a71b94063bf12205a5871acbBehdad Esfahbod	  info[i].indic_position() != POS_POST)
240743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod      {
241743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod        base = i;
242743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod	break;
243743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod      }
244743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
245cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod      /* -> or that is not a pre-base reordering Ra,
246cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod       *
247e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod       * TODO
248e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod       */
249e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
250e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod      /* ->  o If the syllable starts with Ra + Halant (in a script that has Reph)
251e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod       *       and has more than one consonant, Ra is excluded from candidates for
252e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod       *       base consonants.
253cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod       *
254cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod       * IMPLEMENTATION NOTES:
255cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod       *
256cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod       * We do this by adjusting limit accordingly before entering the loop.
257cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod       */
258cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod
259cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod      /* -> or arrive at the first consonant. The consonant stopped at will
260cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod       * be the base. */
261743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod      base = i;
262743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod    }
2635e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod    else
2645e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod      if (is_joiner (info[i]))
2655e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod        break;
266cfd4382ec1af91640129551697de36fd42c0849aBehdad Esfahbod  } while (i > limit);
267743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  if (base < start)
268743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod    base = start; /* Just in case... */
269743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
270743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
271743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 2. Decompose and reorder Matras:
272743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
273743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * Each matra and any syllable modifier sign in the cluster are moved to the
274743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * appropriate position relative to the consonant(s) in the cluster. The
275743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * shaping engine decomposes two- or three-part matras into their constituent
276743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * parts before any repositioning. Matra characters are classified by which
277743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * consonant in a conjunct they have affinity for and are reordered to the
278743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * following positions:
279743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
280743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o Before first half form in the syllable
281743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After subjoined consonants
282743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After post-form consonant
283743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After main consonant (for above marks)
284743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
285743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * IMPLEMENTATION NOTES:
286743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
287743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * The normalize() routine has already decomposed matras for us, so we don't
288743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * need to worry about that.
289743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
290743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
291743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
292743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 3.  Reorder marks to canonical order:
293743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
294743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * Adjacent nukta and halant or nukta and vedic sign are always repositioned
295743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * if necessary, so that the nukta is first.
296743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
297743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * IMPLEMENTATION NOTES:
298743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
299743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * We don't need to do this: the normalize() routine already did this for us.
300743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
301743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
302743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
30345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* Reorder characters */
30445d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
30545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  for (i = start; i < base; i++)
306ee58f3bc75d2d071a71b94063bf12205a5871acbBehdad Esfahbod    info[i].indic_position() = POS_PRE;
307ee58f3bc75d2d071a71b94063bf12205a5871acbBehdad Esfahbod  info[base].indic_position() = POS_BASE;
30845d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
309fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod
310fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod  /* Handle beginning Ra */
3110d8f8a177c4bfd4dc642a353bab8d03674e839acBehdad Esfahbod  if (start + 3 <= end &&
312fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod      info[start].indic_category() == OT_Ra &&
3139ee27a928a989c71923cef82a9e9828f8e9ca051Behdad Esfahbod      info[start + 1].indic_category() == OT_H &&
3140d8f8a177c4bfd4dc642a353bab8d03674e839acBehdad Esfahbod      !is_joiner (info[start + 2]))
315fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod   {
316fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod    info[start].indic_position() = POS_POST;
317fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod    info[start].mask = mask_array[RPHF];
318fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod   }
319fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod
320f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  /* For old-style Indic script tags, move the first post-base Halant after
321f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod   * last consonant. */
322f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  if ((map->get_chosen_script (0) & 0x000000FF) != '2') {
323f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod    /* We should only do this for Indic scripts which have a version two I guess. */
324f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod    for (i = base + 1; i < end; i++)
325f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod      if (info[i].indic_category() == OT_H) {
326f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        unsigned int j;
327f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        for (j = end - 1; j > i; j--)
328f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  if ((FLAG (info[j].indic_category()) & (FLAG (OT_C) | FLAG (OT_Ra))))
329f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	    break;
330f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	if (j > i) {
331f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  /* Move Halant to after last consonant. */
332f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  hb_glyph_info_t t = info[i];
333f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  memmove (&info[i], &info[i + 1], (j - i) * sizeof (info[0]));
334f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  info[j] = t;
335f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	}
336f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        break;
337f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod      }
338f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  }
339f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod
34045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* Attach ZWJ, ZWNJ, nukta, and halant to previous char to move with them. */
34145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  for (i = start + 1; i < end; i++)
342ee58f3bc75d2d071a71b94063bf12205a5871acbBehdad Esfahbod    if ((FLAG (info[i].indic_category()) &
34345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod	 (FLAG (OT_ZWNJ) | FLAG (OT_ZWJ) | FLAG (OT_N) | FLAG (OT_H))))
344ee58f3bc75d2d071a71b94063bf12205a5871acbBehdad Esfahbod      info[i].indic_position() = info[i - 1].indic_position();
34545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
34645d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* We do bubble-sort, skip malicious clusters attempts */
34745d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  if (end - start > 20)
34845d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod    return;
34945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
35045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* Sit tight, rock 'n roll! */
351ee58f3bc75d2d071a71b94063bf12205a5871acbBehdad Esfahbod  hb_bubble_sort (info + start, end - start, compare_indic_order);
35245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
353743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Setup masks now */
354743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
355281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod  {
356281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    hb_mask_t mask;
357281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod
358281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Pre-base */
359281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    mask = mask_array[HALF] | mask_array[AKHN] | mask_array[CJCT];
360281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    for (i = start; i < base; i++)
361281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod      info[i].mask  |= mask;
362281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Base */
363281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    mask = mask_array[AKHN] | mask_array[CJCT];
364281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    info[base].mask |= mask;
365281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Post-base */
366281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    mask = mask_array[BLWF] | mask_array[PSTF] | mask_array[CJCT];
367281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    for (i = base + 1; i < end; i++)
368281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod      info[i].mask  |= mask;
369281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod  }
3709da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
3719da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  /* Apply ZWJ/ZWNJ effects */
3729da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  for (i = start + 1; i < end; i++)
3739da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    if (is_joiner (info[i])) {
3749da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      bool non_joiner = info[i].indic_category() == OT_ZWNJ;
3756b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod      unsigned int j = i;
3769da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
3779da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      do {
3789da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod	j--;
3796b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod
380a91c58bf98258a34e5f7c1ad39a38db58fadc4b9Behdad Esfahbod	/* Reading the Unicode and OpenType specs, I think the following line
381a91c58bf98258a34e5f7c1ad39a38db58fadc4b9Behdad Esfahbod	 * is correct, but this is not what the test suite expects currently.
382a91c58bf98258a34e5f7c1ad39a38db58fadc4b9Behdad Esfahbod	 * The test suite has been drinking, not me...  But disable while
383a91c58bf98258a34e5f7c1ad39a38db58fadc4b9Behdad Esfahbod	 * investigating.
384a91c58bf98258a34e5f7c1ad39a38db58fadc4b9Behdad Esfahbod	 */
385a91c58bf98258a34e5f7c1ad39a38db58fadc4b9Behdad Esfahbod	//info[j].mask &= !mask_array[CJCT];
3866b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod	if (non_joiner)
3876b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod	  info[j].mask &= !mask_array[HALF];
3886b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod
3899da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      } while (j > start && !is_consonant (info[j]));
3909da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    }
391743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
392743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
393743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
394743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
395743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodfound_vowel_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
396743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		      unsigned int start, unsigned int end)
397743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
398743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* TODO
399743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * Not clear to me how this should work.  Do the matras move to before the
400743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * independent vowel?  No idea.
401743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
402743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
403743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
404743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
405743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodfound_standalone_cluster (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
406743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod			  unsigned int start, unsigned int end)
407743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
408743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* TODO
409743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * Easiest thing to do here is to convert the NBSP to consonant and
410743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * call found_consonant_syllable.
411743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
412743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
413743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
414743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
415743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodfound_non_indic (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *mask_array,
416743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		 unsigned int start, unsigned int end)
417743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
418743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Nothing to do right now.  If we ever switch to using the output
419743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * buffer in the reordering process, we'd need to next_glyph() here. */
420743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
421743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
422743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod#include "hb-ot-shape-complex-indic-machine.hh"
423743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
424743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
4259da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbodremove_joiners (hb_buffer_t *buffer)
4269da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod{
4270a965eee880428a43ad7f9d1317c344666247dd7Behdad Esfahbod  /* For now we remove joiners.  However, Uniscbire seems to keep them
4280a965eee880428a43ad7f9d1317c344666247dd7Behdad Esfahbod   * and output a zero-width space glyph for them.  It is not clear to
4290a965eee880428a43ad7f9d1317c344666247dd7Behdad Esfahbod   * me how that is supposed to interact with GSUB. */
4300a965eee880428a43ad7f9d1317c344666247dd7Behdad Esfahbod
4319da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  buffer->clear_output ();
4329da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  unsigned int count = buffer->len;
4339da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  for (buffer->idx = 0; buffer->idx < count;)
4349da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    if (unlikely (is_joiner (buffer->info[buffer->idx])))
4359da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      buffer->skip_glyph ();
4369da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    else
4379da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      buffer->next_glyph ();
4389da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
4399da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  buffer->swap_buffers ();
4409da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod}
4419da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
4429da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbodstatic void
443743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodinitial_reordering (const hb_ot_map_t *map,
444743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_face_t *face,
445743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_buffer_t *buffer,
446743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    void *user_data HB_UNUSED)
447743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
448b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  hb_mask_t mask_array[ARRAY_LENGTH (indic_basic_features)] = {0};
449b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  unsigned int num_masks = ARRAY_LENGTH (indic_basic_features);
450b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  for (unsigned int i = 0; i < num_masks; i++)
45176f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod    mask_array[i] = map->get_1_mask (indic_basic_features[i].tag);
452743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
453743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  find_syllables (map, buffer, mask_array);
4549da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
4559da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  remove_joiners (buffer);
456b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod}
457b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
458743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
459743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodfinal_reordering (const hb_ot_map_t *map,
460743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		  hb_face_t *face,
461743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		  hb_buffer_t *buffer,
462743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		  void *user_data HB_UNUSED)
463743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
464e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod  /* 4. Final reordering:
465e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
466e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * After the localized forms and basic shaping forms GSUB features have been
467e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * applied (see below), the shaping engine performs some final glyph
468e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * reordering before applying all the remaining font features to the entire
469e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * cluster.
470e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
471e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *   o Reorder matras:
472e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
473e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     If a pre-base matra character had been reordered before applying basic
474e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     features, the glyph can be moved closer to the main consonant based on
475e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     whether half-forms had been formed. Actual position for the matra is
476e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     defined as “after last standalone halant glyph, after initial matra
477e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     position and before the main consonant”. If ZWJ or ZWNJ follow this
478e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     halant, position is moved after it.
479e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
480e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *   o Reorder reph:
481e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
482e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     Reph’s original position is always at the beginning of the syllable,
483e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     (i.e. it is not reordered at the character reordering stage). However,
484e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     it will be reordered according to the basic-forms shaping results.
485e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     Possible positions for reph, depending on the script, are; after main,
486e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     before post-base consonant forms, and after post-base consonant forms.
487e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
488e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       1. If reph should be positioned after post-base consonant forms,
489e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          proceed to step 5.
490e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
491e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       2. If the reph repositioning class is not after post-base: target
492e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          position is after the first explicit halant glyph between the
493e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          first post-reph consonant and last main consonant. If ZWJ or ZWNJ
494e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          are following this halant, position is moved after it. If such
495e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          position is found, this is the target position. Otherwise,
496e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          proceed to the next step.
497e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
498e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          Note: in old-implementation fonts, where classifications were
499e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          fixed in shaping engine, there was no case where reph position
500e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          will be found on this step.
501e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
502e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       3. If reph should be repositioned after the main consonant: from the
503e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          first consonant not ligated with main, or find the first
504e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          consonant that is not a potential pre-base reordering Ra.
505e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
506e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
507e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       4. If reph should be positioned before post-base consonant, find
508e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          first post-base classified consonant not ligated with main. If no
509e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          consonant is found, the target position should be before the
510e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          first matra, syllable modifier sign or vedic sign.
511e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
512e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       5. If no consonant is found in steps 3 or 4, move reph to a position
513e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          immediately before the first post-base matra, syllable modifier
514e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          sign or vedic sign that has a reordering class after the intended
515e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          reph position. For example, if the reordering position for reph
516e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          is post-main, it will skip above-base matras that also have a
517e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          post-main position.
518e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
519e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       6. Otherwise, reorder reph to the end of the syllable.
520e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
521e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *   o Reorder pre-base reordering consonants:
522e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
523e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     If a pre-base reordering consonant is found, reorder it according to
524e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     the following rules:
525e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
526e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       1. Only reorder a glyph produced by substitution during application
527e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          of the feature. (Note that a font may shape a Ra consonant with
528e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          the feature generally but block it in certain contexts.)
529e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
530e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       2. Try to find a target position the same way as for pre-base matra.
531e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          If it is found, reorder pre-base consonant glyph.
532e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
533e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *       3. If position is not found, reorder immediately before main
534e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *          consonant.
535e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   */
536e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
537e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod  /* TODO */
538e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
539e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
540e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
541743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category);
542743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position);
543743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
544743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
545743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
546b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
547