hb-ot-shape-complex-indic.cc revision 3285e107c9a83aeb552e67f9460680ff6d167d88
1b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod/*
227aba594c90b4444c35273a38f5fedc8e09d9a88Behdad Esfahbod * Copyright © 2011,2012  Google, Inc.
3b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod *
4b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod *  This is part of HarfBuzz, a text shaping library.
5b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod *
6b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * Permission is hereby granted, without written agreement and without
7b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * license or royalty fees, to use, copy, modify, and distribute this
8b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * software and its documentation for any purpose, provided that the
9b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * above copyright notice and the following two paragraphs appear in
10b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * all copies of this software.
11b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod *
12b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * DAMAGE.
17b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod *
18b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod *
24b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * Google Author(s): Behdad Esfahbod
25b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod */
26b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
273ed4634ec349fa9e943ad23718c04be4dd4bba62Behdad Esfahbod#include "hb-ot-shape-complex-indic-private.hh"
28a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod#include "hb-ot-shape-private.hh"
29352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod
30a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod#define OLD_INDIC_TAG(script) (((hb_tag_t) script) | 0x20000000)
31a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod#define IS_OLD_INDIC_TAG(tag) ( \
32a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_BENGALI) || \
33a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_DEVANAGARI) || \
34a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_GUJARATI) || \
35a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_GURMUKHI) || \
36a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_KANNADA) || \
37a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_MALAYALAM) || \
38a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_ORIYA) || \
39a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_TAMIL) || \
40a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_TELUGU) \
41a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod			      )
42a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbodstruct indic_options_t
43ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod{
44a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  int initialized : 1;
45a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  int uniscribe_bug_compatible : 1;
46a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod};
47a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod
48a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbodunion indic_options_union_t {
49a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  int i;
50a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  indic_options_t opts;
51a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod};
52a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad EsfahbodASSERT_STATIC (sizeof (int) == sizeof (indic_options_union_t));
53a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod
54a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbodstatic indic_options_union_t
55a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbodindic_options_init (void)
56a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod{
57a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  indic_options_union_t u;
58a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  u.i = 0;
59a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  u.opts.initialized = 1;
60a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod
61a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  char *c = getenv ("HB_OT_INDIC_OPTIONS");
62a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  u.opts.uniscribe_bug_compatible = c && strstr (c, "uniscribe-bug-compatible");
63a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod
64a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  return u;
65a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod}
66a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod
67a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbodinline indic_options_t
68a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbodindic_options (void)
69a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod{
70a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  static indic_options_union_t options;
71a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod
72a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  if (unlikely (!options.i)) {
73a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod    /* This is idempotent and threadsafe. */
74a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod    options = indic_options_init ();
75ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  }
76ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod
77a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  return options.opts;
78a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod}
79a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod
80ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod
81743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic int
82743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodcompare_codepoint (const void *pa, const void *pb)
83743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
84743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  hb_codepoint_t a = * (hb_codepoint_t *) pa;
85743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  hb_codepoint_t b = * (hb_codepoint_t *) pb;
86743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
87743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  return a < b ? -1 : a == b ? 0 : +1;
88743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
89743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
90743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic indic_position_t
91743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodconsonant_position (hb_codepoint_t u)
92743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
93743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  consonant_position_t *record;
94743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
950de771b72da6b342b015e3556190821547a4011dBehdad Esfahbod  /* Khmer does not have pre-base half forms. */
960de771b72da6b342b015e3556190821547a4011dBehdad Esfahbod  if (0x1780 <= u && u <= 0x17FF)
970de771b72da6b342b015e3556190821547a4011dBehdad Esfahbod    return POS_BELOW_C;
980de771b72da6b342b015e3556190821547a4011dBehdad Esfahbod
99743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  record = (consonant_position_t *) bsearch (&u, consonant_positions,
100743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod					     ARRAY_LENGTH (consonant_positions),
101743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod					     sizeof (consonant_positions[0]),
102743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod					     compare_codepoint);
103743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
104dbccf87eef0d26838fa4bb3ae26410f6c4818836Behdad Esfahbod  return record ? record->position : POS_BASE_C;
105743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
106743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
107352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbodstatic bool
108352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbodis_ra (hb_codepoint_t u)
109352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod{
110352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod  return !!bsearch (&u, ra_chars,
111352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod		    ARRAY_LENGTH (ra_chars),
112352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod		    sizeof (ra_chars[0]),
113352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod		    compare_codepoint);
114352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod}
115352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod
1169ee27a928a989c71923cef82a9e9828f8e9ca051Behdad Esfahbodstatic bool
1179da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbodis_joiner (const hb_glyph_info_t &info)
1189ee27a928a989c71923cef82a9e9828f8e9ca051Behdad Esfahbod{
1199da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  return !!(FLAG (info.indic_category()) & (FLAG (OT_ZWJ) | FLAG (OT_ZWNJ)));
1209da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod}
1219da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
1229da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbodstatic bool
1239da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbodis_consonant (const hb_glyph_info_t &info)
1249da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod{
1251a1fa8c655a082fc1439608457ba717306cc83caBehdad Esfahbod  /* Note:
1261a1fa8c655a082fc1439608457ba717306cc83caBehdad Esfahbod   *
12718c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod   * We treat Vowels and placeholders as if they were consonants.  This is safe because Vowels
128c5306b6861cfaa50af40e8ceb058791fa06d7981Behdad Esfahbod   * cannot happen in a consonant syllable.  The plus side however is, we can call the
129c5306b6861cfaa50af40e8ceb058791fa06d7981Behdad Esfahbod   * consonant syllable logic from the vowel syllable function and get it all right! */
13018c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod  return !!(FLAG (info.indic_category()) & (FLAG (OT_C) | FLAG (OT_Ra) | FLAG (OT_V) | FLAG (OT_NBSP) | FLAG (OT_DOTTEDCIRCLE)));
1319ee27a928a989c71923cef82a9e9828f8e9ca051Behdad Esfahbod}
132c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod
133deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbodstatic bool
134deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbodis_halant_or_coeng (const hb_glyph_info_t &info)
135deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod{
136deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod  return !!(FLAG (info.indic_category()) & (FLAG (OT_H) | FLAG (OT_Coeng)));
137deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod}
138deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod
139eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodstruct feature_list_t {
140c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  hb_tag_t tag;
141c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  hb_bool_t is_global;
142eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod};
143eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
144d5c4edcdd6df32f2f23aca44f14838b4baab4d7aBehdad Esfahbod/* These features are applied one at a time, given the order in this table. */
145eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodstatic const feature_list_t
146eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodindic_basic_features[] =
147b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
148c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('n','u','k','t'), true},
149c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('a','k','h','n'), false},
150c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('r','p','h','f'), false},
1511ac075b227090a9ad930dcc1670236c176b27067Behdad Esfahbod  {HB_TAG('r','k','r','f'), true},
152c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('p','r','e','f'), false},
153c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('b','l','w','f'), false},
154c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('h','a','l','f'), false},
15529f106d7fba25e1464debd3a4831a7380d75c4c9Behdad Esfahbod  {HB_TAG('a','b','v','f'), false},
156c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('p','s','t','f'), false},
1570201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod  {HB_TAG('c','f','a','r'), false},
1589da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  {HB_TAG('c','j','c','t'), false},
1591d6846db9ebf84561bb30a4e48c6c43184914099Behdad Esfahbod  {HB_TAG('v','a','t','u'), true},
160c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod};
161c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod
162c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod/* Same order as the indic_basic_features array */
163c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbodenum {
164c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  _NUKT,
165c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  AKHN,
166c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  RPHF,
167df6d45c693c417bf311e6fa49f18a8558542e525Behdad Esfahbod  _RKRF,
168c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  PREF,
169c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  BLWF,
170c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  HALF,
17129f106d7fba25e1464debd3a4831a7380d75c4c9Behdad Esfahbod  ABVF,
172c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  PSTF,
1730201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod  CFAR,
1741d6846db9ebf84561bb30a4e48c6c43184914099Behdad Esfahbod  CJCT,
1751d6846db9ebf84561bb30a4e48c6c43184914099Behdad Esfahbod  VATU
176b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod};
177b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
178d5c4edcdd6df32f2f23aca44f14838b4baab4d7aBehdad Esfahbod/* These features are applied all at once. */
179eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodstatic const feature_list_t
180eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodindic_other_features[] =
181b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
182eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('i','n','i','t'), false},
183eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('p','r','e','s'), true},
184eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('a','b','v','s'), true},
185eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('b','l','w','s'), true},
186eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('p','s','t','s'), true},
187eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('h','a','l','n'), true},
188eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
189eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('d','i','s','t'), true},
190eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('a','b','v','m'), true},
191eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('b','l','w','m'), true},
192eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod};
193eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
194743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
195743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
196743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodinitial_reordering (const hb_ot_map_t *map,
197743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_face_t *face,
198743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_buffer_t *buffer,
199743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    void *user_data HB_UNUSED);
200f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbodstatic void
201f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbodfinal_reordering (const hb_ot_map_t *map,
202f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod		  hb_face_t *face,
203f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod		  hb_buffer_t *buffer,
204743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		  void *user_data HB_UNUSED);
205b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
206b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbodvoid
2079f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod_hb_ot_shape_complex_collect_features_indic (hb_ot_map_builder_t *map,
2089f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod					     const hb_segment_properties_t *props HB_UNUSED)
209b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
210f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_bool_feature (HB_TAG('l','o','c','l'));
211a54a5505a35eef5315a8e2e7a79502901e3eff5fBehdad Esfahbod  /* The Indic specs do not require ccmp, but we apply it here since if
212a54a5505a35eef5315a8e2e7a79502901e3eff5fBehdad Esfahbod   * there is a use of it, it's typically at the beginning. */
213f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_bool_feature (HB_TAG('c','c','m','p'));
214f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
215743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  map->add_gsub_pause (initial_reordering, NULL);
216f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
217412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (indic_basic_features); i++) {
21876f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod    map->add_bool_feature (indic_basic_features[i].tag, indic_basic_features[i].is_global);
219412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod    map->add_gsub_pause (NULL, NULL);
220412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  }
221b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
222f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_gsub_pause (final_reordering, NULL);
223f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
224d5c4edcdd6df32f2f23aca44f14838b4baab4d7aBehdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (indic_other_features); i++)
225eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod    map->add_bool_feature (indic_other_features[i].tag, indic_other_features[i].is_global);
226b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod}
227b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
228d96838ef951ce6170eb2dc576ebcba2262cf7008Behdad Esfahbodvoid
229d96838ef951ce6170eb2dc576ebcba2262cf7008Behdad Esfahbod_hb_ot_shape_complex_override_features_indic (hb_ot_map_builder_t *map,
230d96838ef951ce6170eb2dc576ebcba2262cf7008Behdad Esfahbod					      const hb_segment_properties_t *props HB_UNUSED)
231d96838ef951ce6170eb2dc576ebcba2262cf7008Behdad Esfahbod{
232af92b4cc90e4184d5bdd8037c551ed482700114fBehdad Esfahbod  /* Uniscribe does not apply 'kern'. */
233af92b4cc90e4184d5bdd8037c551ed482700114fBehdad Esfahbod  if (indic_options ().uniscribe_bug_compatible)
234af92b4cc90e4184d5bdd8037c551ed482700114fBehdad Esfahbod    map->add_feature (HB_TAG('k','e','r','n'), 0, true);
235d96838ef951ce6170eb2dc576ebcba2262cf7008Behdad Esfahbod}
236d96838ef951ce6170eb2dc576ebcba2262cf7008Behdad Esfahbod
237867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
23811138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbodhb_ot_shape_normalization_mode_t
23911138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbod_hb_ot_shape_complex_normalization_preference_indic (void)
24002cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod{
24102cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod  /* We want split matras decomposed by the common shaping logic. */
24211138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbod  return HB_OT_SHAPE_NORMALIZATION_MODE_DECOMPOSED;
24302cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod}
24402cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod
245867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
246b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbodvoid
2479f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod_hb_ot_shape_complex_setup_masks_indic (hb_ot_map_t *map HB_UNUSED,
2489f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod					hb_buffer_t *buffer,
2493f18236a03880c0960f5990dc90685f6146951a6Behdad Esfahbod					hb_font_t *font HB_UNUSED)
250b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
2519f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod  HB_BUFFER_ALLOCATE_VAR (buffer, indic_category);
2529f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod  HB_BUFFER_ALLOCATE_VAR (buffer, indic_position);
2539f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod
254743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* We cannot setup masks here.  We save information about characters
255743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * and setup masks later on in a pause-callback. */
256743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
257743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  unsigned int count = buffer->len;
258b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  for (unsigned int i = 0; i < count; i++)
259b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  {
26092332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    hb_glyph_info_t &info = buffer->info[i];
26192332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    unsigned int type = get_indic_categories (info.codepoint);
26292332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod
26392332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    info.indic_category() = type & 0x0F;
26492332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    info.indic_position() = type >> 4;
26592332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod
2663399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod    /* The spec says U+0952 is OT_A.  However, testing shows that Uniscribe
2673399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * treats U+0951..U+0952 all as OT_VD.
2683399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * TESTS:
2693399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * U+092E,U+0947,U+0952
2703399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * U+092E,U+0952,U+0947
2713399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * U+092E,U+0947,U+0951
2723399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * U+092E,U+0951,U+0947
2733399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * */
2743399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod    if (unlikely (hb_in_range<hb_codepoint_t> (info.codepoint, 0x0951, 0x0954)))
2753399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod      info.indic_category() = OT_VD;
2763399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod
277df50b8474094f0563ccfdae12c4425a51b72add6Behdad Esfahbod    if (info.indic_category() == OT_X &&
27825bc489498ef7d0beb8fe9ab663e3f0b2f52c9c2Behdad Esfahbod	unlikely (hb_in_range<hb_codepoint_t> (info.codepoint, 0x17CB, 0x17D2))) /* Khmer Various signs */
27925bc489498ef7d0beb8fe9ab663e3f0b2f52c9c2Behdad Esfahbod      info.indic_category() = OT_N;
280df50b8474094f0563ccfdae12c4425a51b72add6Behdad Esfahbod
281c50ed71e9a3df1844f564de66d54b46a696c1356Behdad Esfahbod    /* Khmer Virama is different since it can be used to form a final consonant. */
282c50ed71e9a3df1844f564de66d54b46a696c1356Behdad Esfahbod    if (unlikely (info.codepoint == 0x17D2))
283c50ed71e9a3df1844f564de66d54b46a696c1356Behdad Esfahbod      info.indic_category() = OT_Coeng;
284c50ed71e9a3df1844f564de66d54b46a696c1356Behdad Esfahbod
285db8981f1e0e8625714568c6d0f11f0b317b11d0aBehdad Esfahbod    if (info.indic_category() == OT_Repha) {
286db8981f1e0e8625714568c6d0f11f0b317b11d0aBehdad Esfahbod      /* There are two kinds of characters marked as Repha:
287db8981f1e0e8625714568c6d0f11f0b317b11d0aBehdad Esfahbod       * - The ones that are GenCat=Mn are already positioned visually, ie. after base. (eg. Khmer)
288db8981f1e0e8625714568c6d0f11f0b317b11d0aBehdad Esfahbod       * - The ones that are GenCat=Lo is encoded logically, ie. beginning of syllable. (eg. Malayalam)
289db8981f1e0e8625714568c6d0f11f0b317b11d0aBehdad Esfahbod       *
290db8981f1e0e8625714568c6d0f11f0b317b11d0aBehdad Esfahbod       * We recategorize the first kind to look like a Nukta and attached to the base directly.
291db8981f1e0e8625714568c6d0f11f0b317b11d0aBehdad Esfahbod       */
292db8981f1e0e8625714568c6d0f11f0b317b11d0aBehdad Esfahbod      if (_hb_glyph_info_get_general_category (&info) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
293db8981f1e0e8625714568c6d0f11f0b317b11d0aBehdad Esfahbod        info.indic_category() = OT_N;
294db8981f1e0e8625714568c6d0f11f0b317b11d0aBehdad Esfahbod    }
295db8981f1e0e8625714568c6d0f11f0b317b11d0aBehdad Esfahbod
296db8981f1e0e8625714568c6d0f11f0b317b11d0aBehdad Esfahbod
297db8981f1e0e8625714568c6d0f11f0b317b11d0aBehdad Esfahbod    /* Assign positions... */
298c50ed71e9a3df1844f564de66d54b46a696c1356Behdad Esfahbod    if (is_consonant (info)) {
29992332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_position() = consonant_position (info.codepoint);
30092332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      if (is_ra (info.codepoint))
30192332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod	info.indic_category() = OT_Ra;
30292332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    } else if (info.indic_category() == OT_SM ||
30392332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod	       info.indic_category() == OT_VD) {
30492332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_position() = POS_SMVD;
30592332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    } else if (unlikely (info.codepoint == 0x200C))
30692332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_category() = OT_ZWNJ;
30792332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    else if (unlikely (info.codepoint == 0x200D))
30892332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_category() = OT_ZWJ;
30918c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod    else if (unlikely (info.codepoint == 0x25CC))
31018c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod      info.indic_category() = OT_DOTTEDCIRCLE;
311b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  }
312743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
313b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
31445d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodstatic int
31545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodcompare_indic_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
31645d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod{
31745d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  int a = pa->indic_position();
31845d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  int b = pb->indic_position();
31945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
32045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  return a < b ? -1 : a == b ? 0 : +1;
32145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod}
322867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
3237ea58db311bfb0d8f804d1e9f4a1f004bd45075aBehdad Esfahbod/* Rules from:
3247ea58db311bfb0d8f804d1e9f4a1f004bd45075aBehdad Esfahbod * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */
3257ea58db311bfb0d8f804d1e9f4a1f004bd45075aBehdad Esfahbod
326743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
32770fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbodinitial_reordering_consonant_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *basic_mask_array,
328ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod				       unsigned int start, unsigned int end)
329743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
330ee58f3bc75d2d071a71b94063bf12205a5871acbBehdad Esfahbod  hb_glyph_info_t *info = buffer->info;
331743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
332617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod
333743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 1. Find base consonant:
334743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
335743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * The shaping engine finds the base consonant of the syllable, using the
336743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * following algorithm: starting from the end of the syllable, move backwards
337743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * until a consonant is found that does not have a below-base or post-base
338743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * form (post-base forms have to follow below-base forms), or that is not a
339743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * pre-base reordering Ra, or arrive at the first consonant. The consonant
340743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * stopped at will be the base.
341743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
342743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o If the syllable starts with Ra + Halant (in a script that has Reph)
343743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *     and has more than one consonant, Ra is excluded from candidates for
344743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *     base consonants.
345743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
346743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
3475e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod  unsigned int base = end;
34876b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  bool has_reph = false;
349743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
35076b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  {
351617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
352617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod     *    and has more than one consonant, Ra is excluded from candidates for
353617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod     *    base consonants. */
354617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    unsigned int limit = start;
35570fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod    if (basic_mask_array[RPHF] &&
356617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod	start + 3 <= end &&
357617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod	info[start].indic_category() == OT_Ra &&
358617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod	info[start + 1].indic_category() == OT_H &&
3593285e107c9a83aeb552e67f9460680ff6d167d88Behdad Esfahbod	(unlikely (buffer->props.script == HB_SCRIPT_SINHALA) ?
3603285e107c9a83aeb552e67f9460680ff6d167d88Behdad Esfahbod	 info[start + 2].indic_category() == OT_ZWJ /* In Sinhala, form Reph only if ZWJ is present */:
3613285e107c9a83aeb552e67f9460680ff6d167d88Behdad Esfahbod	 !is_joiner (info[start + 2] /* In other scripts, any joiner blocks Reph formation */ )
3623285e107c9a83aeb552e67f9460680ff6d167d88Behdad Esfahbod	))
363617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    {
364617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod      limit += 2;
3653285e107c9a83aeb552e67f9460680ff6d167d88Behdad Esfahbod      while (limit < end && is_joiner (info[limit]))
3663285e107c9a83aeb552e67f9460680ff6d167d88Behdad Esfahbod        limit++;
367617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod      base = start;
368617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod      has_reph = true;
369617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    };
37076b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod
37114dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod     enum base_position_t {
37214dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod       BASE_FIRST,
37314dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod       BASE_LAST
37414dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod     } base_pos;
37514dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod
37614dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod    switch ((hb_tag_t) buffer->props.script)
37714dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod    {
37814dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod      case HB_SCRIPT_KHMER:
37914dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod	base_pos = BASE_FIRST;
38014dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod	break;
38114dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod
38214dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod      default:
38314dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod	base_pos = BASE_LAST;
38414dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod	break;
38514dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod    }
38614dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod
38714dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod    if (base_pos == BASE_LAST)
3885d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod    {
3895d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod      /* -> starting from the end of the syllable, move backwards */
3905d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod      unsigned int i = end;
3915d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod      do {
3925d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	i--;
3935d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	/* -> until a consonant is found */
3945d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	if (is_consonant (info[i]))
3953c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	{
3965d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	  /* -> that does not have a below-base or post-base form
3975d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	   * (post-base forms have to follow below-base forms), */
3985d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	  if (info[i].indic_position() != POS_BELOW_C &&
3995d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	      info[i].indic_position() != POS_POST_C)
4005d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	  {
4015d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	    base = i;
4025d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	    break;
4035d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	  }
4045d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod
4055d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	  /* -> or that is not a pre-base reordering Ra,
4065d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	   *
40725e302da9a712e6f1d63b0d243a8df0d326ddba3Behdad Esfahbod	   * IMPLEMENTATION NOTES:
40825e302da9a712e6f1d63b0d243a8df0d326ddba3Behdad Esfahbod	   *
40925e302da9a712e6f1d63b0d243a8df0d326ddba3Behdad Esfahbod	   * Our pre-base reordering Ra's are marked POS_BELOW, so will be skipped
41025e302da9a712e6f1d63b0d243a8df0d326ddba3Behdad Esfahbod	   * by the logic above already.
4115d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	   */
4125d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod
4135d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	  /* -> or arrive at the first consonant. The consonant stopped at will
4145d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	   * be the base. */
4153c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	  base = i;
4163c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	}
4175d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	else
4185d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	  if (is_joiner (info[i]))
4195d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	    break;
4205d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod      } while (i > limit);
4215d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod    }
4225d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod    else
4235d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod    {
4245d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod      /* In scripts without half forms (eg. Khmer), the first consonant is always the base. */
4255d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod
4265d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod      if (!has_reph)
4275d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	base = limit;
4285d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod    }
429743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
4303c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    if (base < start)
4313c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      base = start; /* Just in case... */
432743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
433617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod
434617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
435617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod     *    and has more than one consonant, Ra is excluded from candidates for
436617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod     *    base consonants. */
437617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    if (has_reph && base == start) {
438617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod      /* Have no other consonant, so Reph is not formed and Ra becomes base. */
439617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod      has_reph = false;
440617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    }
4415e4e21fce4b548b0b8a5951bc8f35a9f27428192Behdad Esfahbod  }
4423d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod
4433d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod
444743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 2. Decompose and reorder Matras:
445743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
446743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * Each matra and any syllable modifier sign in the cluster are moved to the
447743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * appropriate position relative to the consonant(s) in the cluster. The
448743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * shaping engine decomposes two- or three-part matras into their constituent
449743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * parts before any repositioning. Matra characters are classified by which
450743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * consonant in a conjunct they have affinity for and are reordered to the
451743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * following positions:
452743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
453743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o Before first half form in the syllable
454743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After subjoined consonants
455743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After post-form consonant
456743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After main consonant (for above marks)
457743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
458743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * IMPLEMENTATION NOTES:
459743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
460743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * The normalize() routine has already decomposed matras for us, so we don't
461743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * need to worry about that.
462743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
463743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
464743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
465743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 3.  Reorder marks to canonical order:
466743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
467743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * Adjacent nukta and halant or nukta and vedic sign are always repositioned
468743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * if necessary, so that the nukta is first.
469743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
470743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * IMPLEMENTATION NOTES:
471743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
472743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * We don't need to do this: the normalize() routine already did this for us.
473743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
474743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
475743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
47645d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* Reorder characters */
47745d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
4783c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod  for (unsigned int i = start; i < base; i++)
479e8cd81f76d159f3ecf808952dab24bc07782497aBehdad Esfahbod    info[i].indic_position() = MIN ((unsigned int) POS_PRE_C, info[i].indic_position());
48055f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod
481075d671f1093d2e3c58f7f45568696030f1b3efdBehdad Esfahbod  if (base < end)
482075d671f1093d2e3c58f7f45568696030f1b3efdBehdad Esfahbod    info[base].indic_position() = POS_BASE_C;
48345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
48455f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod  /* Mark final consonants.  A final consonant is one appearing after a matra,
48555f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod   * like in Khmer. */
48655f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod  for (unsigned int i = base + 1; i < end; i++)
48755f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod    if (info[i].indic_category() == OT_M) {
48855f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod      for (unsigned int j = i + 1; j < end; j++)
48955f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod        if (is_consonant (info[j])) {
49055f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod	  info[j].indic_position() = POS_FINAL_C;
49155f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod	  break;
49255f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod	}
49355f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod      break;
49455f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod    }
49555f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod
496fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod  /* Handle beginning Ra */
4975e4e21fce4b548b0b8a5951bc8f35a9f27428192Behdad Esfahbod  if (has_reph)
498dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod    info[start].indic_position() = POS_RA_TO_BECOME_REPH;
499fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod
500f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  /* For old-style Indic script tags, move the first post-base Halant after
501f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod   * last consonant. */
502a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod  if (IS_OLD_INDIC_TAG (map->get_chosen_script (0))) {
5033c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = base + 1; i < end; i++)
504f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod      if (info[i].indic_category() == OT_H) {
505f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        unsigned int j;
506f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        for (j = end - 1; j > i; j--)
507190eb31a16178269aecaf5d2ecc9012f956749f4Behdad Esfahbod	  if (is_consonant (info[j]))
508f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	    break;
509f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	if (j > i) {
510f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  /* Move Halant to after last consonant. */
511f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  hb_glyph_info_t t = info[i];
512f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  memmove (&info[i], &info[i + 1], (j - i) * sizeof (info[0]));
513f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  info[j] = t;
514f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	}
515f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        break;
516f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod      }
517f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  }
518f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod
51945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* Attach ZWJ, ZWNJ, nukta, and halant to previous char to move with them. */
520a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  if (!indic_options ().uniscribe_bug_compatible)
521ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  {
522ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    /* Please update the Uniscribe branch when touching this! */
523ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    for (unsigned int i = start + 1; i < end; i++)
52425bc489498ef7d0beb8fe9ab663e3f0b2f52c9c2Behdad Esfahbod      if ((FLAG (info[i].indic_category()) & (FLAG (OT_ZWNJ) | FLAG (OT_ZWJ) | FLAG (OT_N) | FLAG (OT_RS) | FLAG (OT_H))))
525ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	info[i].indic_position() = info[i - 1].indic_position();
526ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  } else {
52767ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod    /*
52867ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod     * Uniscribe doesn't move the Halant with Left Matra.
52967ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod     * TEST: U+092B,U+093F,U+094DE
53067ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod     */
531ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    /* Please update the non-Uniscribe branch when touching this! */
532ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    for (unsigned int i = start + 1; i < end; i++)
53325bc489498ef7d0beb8fe9ab663e3f0b2f52c9c2Behdad Esfahbod      if ((FLAG (info[i].indic_category()) & (FLAG (OT_ZWNJ) | FLAG (OT_ZWJ) | FLAG (OT_N) | FLAG (OT_RS) | FLAG (OT_H)))) {
534ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	info[i].indic_position() = info[i - 1].indic_position();
5356a091df9b403b147ef78f3974610dedf4ce1e08aBehdad Esfahbod	if (info[i].indic_category() == OT_H && info[i].indic_position() == POS_PRE_M)
536ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	  for (unsigned int j = i; j > start; j--)
5376a091df9b403b147ef78f3974610dedf4ce1e08aBehdad Esfahbod	    if (info[j - 1].indic_position() != POS_PRE_M) {
538ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	      info[i].indic_position() = info[j - 1].indic_position();
539ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	      break;
540ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	    }
541ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod      }
542ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  }
54374ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod  /* Re-attach ZWJ, ZWNJ, and halant to next char, for after-base consonants. */
54474ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod  {
54574ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod    unsigned int last_halant = end;
54674ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod    for (unsigned int i = base + 1; i < end; i++)
547deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod      if (is_halant_or_coeng (info[i]))
54874ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod        last_halant = i;
54974ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod      else if (is_consonant (info[i])) {
55074ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod	for (unsigned int j = last_halant; j < i; j++)
55174ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod	  info[j].indic_position() = info[i].indic_position();
55274ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod      }
55374ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod  }
55445d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
55545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* We do bubble-sort, skip malicious clusters attempts */
556b99d63ae114fb58f129562b293a8a66543d499adBehdad Esfahbod  if (end - start < 64)
557a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod  {
558a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    /* Sit tight, rock 'n roll! */
559d3637edb248162970e202e9d0671540274192844Behdad Esfahbod    hb_bubble_sort (info + start, end - start, compare_indic_order);
560a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    /* Find base again */
561a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    base = end;
5623c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = start; i < end; i++)
563a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod      if (info[i].indic_position() == POS_BASE_C) {
564a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod        base = i;
565a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod	break;
566a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod      }
567a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod  }
56845d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
569743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Setup masks now */
570743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
571281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod  {
572281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    hb_mask_t mask;
573281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod
574dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod    /* Reph */
575668c6046c1b3af3bd316bda0cc8636f2a5e8df42Behdad Esfahbod    for (unsigned int i = start; i < end && info[i].indic_position() == POS_RA_TO_BECOME_REPH; i++)
57670fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod      info[i].mask |= basic_mask_array[RPHF];
577dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
578281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Pre-base */
57970fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod    mask = basic_mask_array[HALF] | basic_mask_array[AKHN] | basic_mask_array[CJCT];
5803c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = start; i < base; i++)
581281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod      info[i].mask  |= mask;
582281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Base */
58370fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod    mask = basic_mask_array[AKHN] | basic_mask_array[CJCT];
584075d671f1093d2e3c58f7f45568696030f1b3efdBehdad Esfahbod    if (base < end)
585075d671f1093d2e3c58f7f45568696030f1b3efdBehdad Esfahbod      info[base].mask |= mask;
586281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Post-base */
58770fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod    mask = basic_mask_array[BLWF] | basic_mask_array[ABVF] | basic_mask_array[PSTF] | basic_mask_array[CJCT];
5883c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = base + 1; i < end; i++)
589281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod      info[i].mask  |= mask;
590281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod  }
5919da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
59217d7de91d76406d3e92db37d9eef2fc615f06e68Behdad Esfahbod  /* XXX This will not match for old-Indic spec since the Halant-Ra order is reversed already. */
5938e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod  if (basic_mask_array[PREF] && base + 3 <= end)
59417d7de91d76406d3e92db37d9eef2fc615f06e68Behdad Esfahbod  {
5958e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod    /* Find a Halant,Ra sequence and mark it fore pre-base reordering processing. */
5968e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod    for (unsigned int i = base + 1; i + 1 < end; i++)
597deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod      if (is_halant_or_coeng (info[i]) &&
5988e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	  info[i + 1].indic_category() == OT_Ra)
5998e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod      {
6000201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	info[i++].mask |= basic_mask_array[PREF];
6010201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	info[i++].mask |= basic_mask_array[PREF];
6020201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod
6030201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	/* Mark the subsequent stuff with 'cfar'.  Used in Khmer.
6040201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	 * Read the feature spec.
6050201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	 * This allows distinguishing the following cases with MS Khmer fonts:
6060201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	 * U+1784,U+17D2,U+179A,U+17D2,U+1782
6070201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	 * U+1784,U+17D2,U+1782,U+17D2,U+179A
6080201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	 */
6090201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	for (; i < end; i++)
6100201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	  info[i].mask |= basic_mask_array[CFAR];
6110201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod
6128e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	break;
6138e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod      }
61417d7de91d76406d3e92db37d9eef2fc615f06e68Behdad Esfahbod  }
61517d7de91d76406d3e92db37d9eef2fc615f06e68Behdad Esfahbod
6169da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  /* Apply ZWJ/ZWNJ effects */
6173c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod  for (unsigned int i = start + 1; i < end; i++)
6189da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    if (is_joiner (info[i])) {
6199da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      bool non_joiner = info[i].indic_category() == OT_ZWNJ;
6206b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod      unsigned int j = i;
6219da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
6229da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      do {
6239da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod	j--;
6246b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod
62570fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod	info[j].mask &= ~basic_mask_array[CJCT];
6266b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod	if (non_joiner)
62770fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod	  info[j].mask &= ~basic_mask_array[HALF];
6286b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod
6299da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      } while (j > start && !is_consonant (info[j]));
6309da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    }
631743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
632743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
633743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
634743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
6359f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbodinitial_reordering_vowel_syllable (const hb_ot_map_t *map,
6369f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod				   hb_buffer_t *buffer,
63770fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod				   hb_mask_t *basic_mask_array,
638ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod				   unsigned int start, unsigned int end)
639743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
640c5306b6861cfaa50af40e8ceb058791fa06d7981Behdad Esfahbod  /* We made the vowels look like consonants.  So let's call the consonant logic! */
64170fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod  initial_reordering_consonant_syllable (map, buffer, basic_mask_array, start, end);
642743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
643743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
644743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
6459f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbodinitial_reordering_standalone_cluster (const hb_ot_map_t *map,
6469f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod				       hb_buffer_t *buffer,
64770fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod				       hb_mask_t *basic_mask_array,
648ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod				       unsigned int start, unsigned int end)
649743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
65018c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod  /* We treat NBSP/dotted-circle as if they are consonants, so we should just chain.
65118c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod   * Only if not in compatibility mode that is... */
65218c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod
653a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  if (indic_options ().uniscribe_bug_compatible)
65418c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod  {
65518c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod    /* For dotted-circle, this is what Uniscribe does:
65618c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod     * If dotted-circle is the last glyph, it just does nothing.
65718c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod     * Ie. It doesn't form Reph. */
65818c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod    if (buffer->info[end - 1].indic_category() == OT_DOTTEDCIRCLE)
65918c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod      return;
66018c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod  }
66118c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod
66270fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod  initial_reordering_consonant_syllable (map, buffer, basic_mask_array, start, end);
663743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
664743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
665743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
6669f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbodinitial_reordering_non_indic (const hb_ot_map_t *map HB_UNUSED,
6679f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod			      hb_buffer_t *buffer HB_UNUSED,
66870fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod			      hb_mask_t *basic_mask_array HB_UNUSED,
6693f18236a03880c0960f5990dc90685f6146951a6Behdad Esfahbod			      unsigned int start HB_UNUSED, unsigned int end HB_UNUSED)
670743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
671743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Nothing to do right now.  If we ever switch to using the output
672743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * buffer in the reordering process, we'd need to next_glyph() here. */
673743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
674743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
675743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod#include "hb-ot-shape-complex-indic-machine.hh"
676743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
677743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
678743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodinitial_reordering (const hb_ot_map_t *map,
6793f18236a03880c0960f5990dc90685f6146951a6Behdad Esfahbod		    hb_face_t *face HB_UNUSED,
680743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_buffer_t *buffer,
681743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    void *user_data HB_UNUSED)
682743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
68370fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod  hb_mask_t basic_mask_array[ARRAY_LENGTH (indic_basic_features)] = {0};
684b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  unsigned int num_masks = ARRAY_LENGTH (indic_basic_features);
685b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  for (unsigned int i = 0; i < num_masks; i++)
68670fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod    basic_mask_array[i] = map->get_1_mask (indic_basic_features[i].tag);
687743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
68870fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod  find_syllables (map, buffer, basic_mask_array);
689b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod}
690b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
691743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
69246e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbodfinal_reordering_syllable (hb_buffer_t *buffer,
69346e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod			   hb_mask_t init_mask, hb_mask_t pref_mask,
694ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod			   unsigned int start, unsigned int end)
695743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
6964ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  hb_glyph_info_t *info = buffer->info;
6974ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
698e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod  /* 4. Final reordering:
699e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
700e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * After the localized forms and basic shaping forms GSUB features have been
701e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * applied (see below), the shaping engine performs some final glyph
702e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * reordering before applying all the remaining font features to the entire
703e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * cluster.
7044ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod   */
7054ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
7064ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  /* Find base again */
7074ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  unsigned int base = end;
7084ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  for (unsigned int i = start; i < end; i++)
7094ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    if (info[i].indic_position() == POS_BASE_C) {
7104ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod      base = i;
7114ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod      break;
7124ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    }
7134ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
7144705a7026900e51f6430f03a73c87f2df035df92Behdad Esfahbod  unsigned int start_of_last_cluster = base;
7154705a7026900e51f6430f03a73c87f2df035df92Behdad Esfahbod
7164ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  /*   o Reorder matras:
717e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
718e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     If a pre-base matra character had been reordered before applying basic
719e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     features, the glyph can be moved closer to the main consonant based on
720e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     whether half-forms had been formed. Actual position for the matra is
721e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     defined as “after last standalone halant glyph, after initial matra
722e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     position and before the main consonant”. If ZWJ or ZWNJ follow this
723e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     halant, position is moved after it.
7244ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod   */
7254ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
726362d3db8d3527d0fef260a17d2466e92a4a25425Behdad Esfahbod  if (start < base) /* Otherwise there can't be any pre-base matra characters. */
7279d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod  {
728921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod    unsigned int new_pos = base - 1;
729921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod    while (new_pos > start &&
730deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod	   !(FLAG (info[new_pos].indic_category()) & (FLAG (OT_M) | FLAG (OT_H) | FLAG (OT_Coeng))))
731921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod      new_pos--;
7329d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    /* If we found no Halant we are done.  Otherwise only proceed if the Halant does
7339d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod     * not belong to the Matra itself! */
734deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod    if (is_halant_or_coeng (info[new_pos]) &&
735921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod	info[new_pos].indic_position() != POS_PRE_M) {
7369d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
737921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod      if (new_pos + 1 < end && is_joiner (info[new_pos + 1]))
738921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod	new_pos++;
7399d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod
7409d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      /* Now go see if there's actually any matras... */
741921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod      for (unsigned int i = new_pos; i > start; i--)
7426a091df9b403b147ef78f3974610dedf4ce1e08aBehdad Esfahbod	if (info[i - 1].indic_position () == POS_PRE_M)
7439d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod	{
7441a1dbe9a2787f226f3e43063da8eb6633438b0a0Behdad Esfahbod	  unsigned int old_pos = i - 1;
7451a1dbe9a2787f226f3e43063da8eb6633438b0a0Behdad Esfahbod	  hb_glyph_info_t tmp = info[old_pos];
7461a1dbe9a2787f226f3e43063da8eb6633438b0a0Behdad Esfahbod	  memmove (&info[old_pos], &info[old_pos + 1], (new_pos - old_pos) * sizeof (info[0]));
7471a1dbe9a2787f226f3e43063da8eb6633438b0a0Behdad Esfahbod	  info[new_pos] = tmp;
748921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod	  start_of_last_cluster = MIN (new_pos, start_of_last_cluster);
749921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod	  new_pos--;
7509d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod	}
7519d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    }
7524ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  }
7534ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
7544ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
7554ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  /*   o Reorder reph:
756e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
757e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     Reph’s original position is always at the beginning of the syllable,
758e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     (i.e. it is not reordered at the character reordering stage). However,
759e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     it will be reordered according to the basic-forms shaping results.
760e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     Possible positions for reph, depending on the script, are; after main,
761e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     before post-base consonant forms, and after post-base consonant forms.
762dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod   */
763dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
764dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  /* If there's anything after the Ra that has the REPH pos, it ought to be halant.
765dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod   * Which means that the font has failed to ligate the Reph.  In which case, we
766dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod   * shouldn't move. */
767dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  if (start + 1 < end &&
768dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod      info[start].indic_position() == POS_RA_TO_BECOME_REPH &&
769dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod      info[start + 1].indic_position() != POS_RA_TO_BECOME_REPH)
770dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  {
77102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      unsigned int new_reph_pos;
77202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
77302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     enum reph_position_t {
774f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_AFTER_MAIN,
775f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_BEFORE_SUBSCRIPT,
776f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_AFTER_SUBSCRIPT,
777f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_BEFORE_POSTSCRIPT,
7789fc7a11469113d31d8095757c4fc038c3427d44aBehdad Esfahbod       REPH_AFTER_POSTSCRIPT
779f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod     } reph_pos;
780f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
781f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod     /* XXX Figure out old behavior too */
7827f852b644b8143492a02edfc853114aaa23446bdBehdad Esfahbod     switch ((hb_tag_t) buffer->props.script)
783f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod     {
784f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_MALAYALAM:
785f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_ORIYA:
786f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_AFTER_MAIN;
787f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
788f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
789f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_GURMUKHI:
790f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_BEFORE_SUBSCRIPT;
791f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
792f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
793f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_BENGALI:
794f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_AFTER_SUBSCRIPT;
795f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
796f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
797f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       default:
798f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_DEVANAGARI:
799f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_GUJARATI:
800f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_BEFORE_POSTSCRIPT;
801f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
802f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
803f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_KANNADA:
804f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_TAMIL:
805f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_TELUGU:
806f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_AFTER_POSTSCRIPT;
807f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
808f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod     }
80902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
810dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod    /*       1. If reph should be positioned after post-base consonant forms,
811dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          proceed to step 5.
81202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
8139d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    if (reph_pos == REPH_AFTER_POSTSCRIPT)
81402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
8159d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      goto reph_step_5;
81602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
81702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
81802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       2. If the reph repositioning class is not after post-base: target
819dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          position is after the first explicit halant glyph between the
820dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first post-reph consonant and last main consonant. If ZWJ or ZWNJ
821dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          are following this halant, position is moved after it. If such
822dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          position is found, this is the target position. Otherwise,
823dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          proceed to the next step.
824dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *
825dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          Note: in old-implementation fonts, where classifications were
826dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          fixed in shaping engine, there was no case where reph position
827dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          will be found on this step.
82802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
82902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
83002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      new_reph_pos = start + 1;
831deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod      while (new_reph_pos < base && !is_halant_or_coeng (info[new_reph_pos]))
83202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	new_reph_pos++;
83302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
834deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod      if (new_reph_pos < base && is_halant_or_coeng (info[new_reph_pos])) {
83502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	/* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */
83602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1]))
83702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	  new_reph_pos++;
83802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	goto reph_move;
83902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      }
84002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
84102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
84202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       3. If reph should be repositioned after the main consonant: find the
843dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first consonant not ligated with main, or find the first
844dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          consonant that is not a potential pre-base reordering Ra.
84502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
8469d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    if (reph_pos == REPH_AFTER_MAIN)
84702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
848b504e060f008e95b1ba36c06600c9fea4f5d4808Behdad Esfahbod      new_reph_pos = base;
849b504e060f008e95b1ba36c06600c9fea4f5d4808Behdad Esfahbod      /* XXX Skip potential pre-base reordering Ra. */
850b504e060f008e95b1ba36c06600c9fea4f5d4808Behdad Esfahbod      while (new_reph_pos < end &&
851b504e060f008e95b1ba36c06600c9fea4f5d4808Behdad Esfahbod	     !( FLAG (info[new_reph_pos + 1].indic_position()) & (FLAG (POS_BELOW_C) | FLAG (POS_POST_C) | FLAG (POS_POST_M) | FLAG (POS_SMVD))))
852b504e060f008e95b1ba36c06600c9fea4f5d4808Behdad Esfahbod	new_reph_pos++;
853b504e060f008e95b1ba36c06600c9fea4f5d4808Behdad Esfahbod      if (new_reph_pos < end)
854b504e060f008e95b1ba36c06600c9fea4f5d4808Behdad Esfahbod        goto reph_move;
85502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
85602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
85702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       4. If reph should be positioned before post-base consonant, find
858dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first post-base classified consonant not ligated with main. If no
859dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          consonant is found, the target position should be before the
860dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first matra, syllable modifier sign or vedic sign.
86102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
8629d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    /* This is our take on what step 4 is trying to say (and failing, BADLY). */
8639d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    if (reph_pos == REPH_AFTER_SUBSCRIPT)
86402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
8659d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      new_reph_pos = base;
8669d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      while (new_reph_pos < end &&
8676a091df9b403b147ef78f3974610dedf4ce1e08aBehdad Esfahbod	     !( FLAG (info[new_reph_pos + 1].indic_position()) & (FLAG (POS_POST_C) | FLAG (POS_POST_M) | FLAG (POS_SMVD))))
8689d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod	new_reph_pos++;
8699d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      if (new_reph_pos < end)
8709d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod        goto reph_move;
87102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
87202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
87302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       5. If no consonant is found in steps 3 or 4, move reph to a position
874dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          immediately before the first post-base matra, syllable modifier
875dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          sign or vedic sign that has a reordering class after the intended
876dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          reph position. For example, if the reordering position for reph
877dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          is post-main, it will skip above-base matras that also have a
878dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          post-main position.
879dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     */
88002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_step_5:
88102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
88202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      /* XXX */
88302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
884dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
88502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       6. Otherwise, reorder reph to the end of the syllable.
88602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
88702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
88802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      new_reph_pos = end - 1;
88902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      while (new_reph_pos > start && info[new_reph_pos].indic_position() == POS_SMVD)
89002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	new_reph_pos--;
89102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
892892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod      /*
893892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * If the Reph is to be ending up after a Matra,Halant sequence,
894892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * position it before that Halant so it can interact with the Matra.
895892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * However, if it's a plain Consonant,Halant we shouldn't do that.
896892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * Uniscribe doesn't do this.
897892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * TEST: U+0930,U+094D,U+0915,U+094B,U+094D
898892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       */
899a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod      if (!indic_options ().uniscribe_bug_compatible &&
900deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod	  unlikely (is_halant_or_coeng (info[new_reph_pos]))) {
90102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	for (unsigned int i = base + 1; i < new_reph_pos; i++)
90202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	  if (info[i].indic_category() == OT_M) {
90302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	    /* Ok, got it. */
90402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	    new_reph_pos--;
90502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	  }
90602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      }
90702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      goto reph_move;
9088df5636968389ac7bf8620ccd091fd4872b0bbeeBehdad Esfahbod    }
9098df5636968389ac7bf8620ccd091fd4872b0bbeeBehdad Esfahbod
91002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_move:
91102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
91202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      /* Move */
91302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      hb_glyph_info_t reph = info[start];
91402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      memmove (&info[start], &info[start + 1], (new_reph_pos - start) * sizeof (info[0]));
91502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      info[new_reph_pos] = reph;
91602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      start_of_last_cluster = start; /* Yay, one big cluster! */
91702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
918dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  }
919dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
920dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
921dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  /*   o Reorder pre-base reordering consonants:
922e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
923e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     If a pre-base reordering consonant is found, reorder it according to
924e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     the following rules:
925e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   */
926e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
92746e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod  if (pref_mask && base + 1 < end) /* Otherwise there can't be any pre-base reordering Ra. */
92846e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod  {
9298e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod    for (unsigned int i = base + 1; i < end; i++)
9308e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod      if ((info[i].mask & pref_mask) != 0)
93178818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod      {
9328e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	/*       1. Only reorder a glyph produced by substitution during application
9338e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	 *          of the <pref> feature. (Note that a font may shape a Ra consonant with
9348e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	 *          the feature generally but block it in certain contexts.)
9358e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	 */
9368e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	if (i + 1 == end || (info[i + 1].mask & pref_mask) == 0)
9378e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	{
9388e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	  /*
9398e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	   *       2. Try to find a target position the same way as for pre-base matra.
9408e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	   *          If it is found, reorder pre-base consonant glyph.
9418e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	   *
9428e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	   *       3. If position is not found, reorder immediately before main
9438e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	   *          consonant.
9448e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	   */
9458e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod
9468e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	  unsigned int new_pos = base;
9478e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	  while (new_pos > start + 1 &&
948deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod		 !(FLAG (info[new_pos - 1].indic_category()) & (FLAG (OT_M) | FLAG (OT_H) | FLAG (OT_Coeng))))
9498e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	    new_pos--;
9508e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod
951deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod	  if (new_pos > start && is_halant_or_coeng (info[new_pos - 1]))
9528e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	    /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
9538e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	    if (new_pos < end && is_joiner (info[new_pos]))
9548e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	      new_pos++;
9558e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod
9568e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	  {
9578e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	    unsigned int old_pos = i;
9588e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	    hb_glyph_info_t tmp = info[old_pos];
9598e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	    memmove (&info[new_pos + 1], &info[new_pos], (old_pos - new_pos) * sizeof (info[0]));
9608e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	    info[new_pos] = tmp;
9618e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	    start_of_last_cluster = MIN (new_pos, start_of_last_cluster);
9628e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	  }
9638e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	}
9648e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod
9658e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod        break;
96678818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod      }
96746e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod  }
968eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
969eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
970a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod  /* Apply 'init' to the Left Matra if it's a word start. */
9716a091df9b403b147ef78f3974610dedf4ce1e08aBehdad Esfahbod  if (info[start].indic_position () == POS_PRE_M &&
972a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod      (!start ||
973eace47b173807d94b29a6490d0bc3c9f8f6168d1Behdad Esfahbod       !(FLAG (_hb_glyph_info_get_general_category (&info[start - 1])) &
974a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	 (FLAG (HB_UNICODE_GENERAL_CATEGORY_LOWERCASE_LETTER) |
975a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_MODIFIER_LETTER) |
976a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER) |
977a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_TITLECASE_LETTER) |
978a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_UPPERCASE_LETTER) |
979a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK) |
980a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK) |
981a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)))))
98246e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod    info[start].mask |= init_mask;
983a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod
984eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
98521d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod
98621d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod  /* Finish off the clusters and go home! */
98721d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod
988a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  if (!indic_options ().uniscribe_bug_compatible)
989ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  {
99021d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    /* This is what Uniscribe does.  Ie. add cluster boundaries after Halant,ZWNJ.
99121d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod     * This means, half forms are submerged into the main consonants cluster.
99221d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod     * This is unnecessary, and makes cursor positioning harder, but that's what
99321d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod     * Uniscribe does. */
99421d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    unsigned int cluster_start = start;
99521d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    for (unsigned int i = start + 1; i < start_of_last_cluster; i++)
996deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod      if (is_halant_or_coeng (info[i - 1]) && info[i].indic_category() == OT_ZWNJ) {
99721d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod        i++;
99821d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod	buffer->merge_clusters (cluster_start, i);
99921d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod	cluster_start = i;
100021d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod      }
100121d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    start_of_last_cluster = cluster_start;
100221d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod  }
100321d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod
100421d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod  buffer->merge_clusters (start_of_last_cluster, end);
1005ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod}
1006e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
1007e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
1008ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodstatic void
1009ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodfinal_reordering (const hb_ot_map_t *map,
10103f18236a03880c0960f5990dc90685f6146951a6Behdad Esfahbod		  hb_face_t *face HB_UNUSED,
1011ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod		  hb_buffer_t *buffer,
1012ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod		  void *user_data HB_UNUSED)
1013ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod{
1014ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  unsigned int count = buffer->len;
1015ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  if (!count) return;
1016ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod
101746e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod  hb_mask_t init_mask = map->get_1_mask (HB_TAG('i','n','i','t'));
101846e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod  hb_mask_t pref_mask = map->get_1_mask (HB_TAG('p','r','e','f'));
1019eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
1020ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  hb_glyph_info_t *info = buffer->info;
1021ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  unsigned int last = 0;
1022cee7187447b76b22e1bb6136d137b35ac49c3a5dBehdad Esfahbod  unsigned int last_syllable = info[0].syllable();
1023ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  for (unsigned int i = 1; i < count; i++)
1024cee7187447b76b22e1bb6136d137b35ac49c3a5dBehdad Esfahbod    if (last_syllable != info[i].syllable()) {
102546e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod      final_reordering_syllable (buffer, init_mask, pref_mask, last, i);
1026ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod      last = i;
1027cee7187447b76b22e1bb6136d137b35ac49c3a5dBehdad Esfahbod      last_syllable = info[last].syllable();
1028ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod    }
102946e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod  final_reordering_syllable (buffer, init_mask, pref_mask, last, count);
1030e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
1031743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category);
1032743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position);
1033743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
1034743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
1035743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
1036b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
1037