hb-ot-shape-complex-indic.cc revision 7d09c98a1fff97127e48eae48d380dc9fcff288e
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
133eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodstruct feature_list_t {
134c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  hb_tag_t tag;
135c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  hb_bool_t is_global;
136eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod};
137eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
138eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodstatic const feature_list_t
139eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodindic_basic_features[] =
140b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
141c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('n','u','k','t'), true},
142c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('a','k','h','n'), false},
143c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('r','p','h','f'), false},
1441ac075b227090a9ad930dcc1670236c176b27067Behdad Esfahbod  {HB_TAG('r','k','r','f'), true},
145c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('p','r','e','f'), false},
146c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('b','l','w','f'), false},
147c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('h','a','l','f'), false},
14829f106d7fba25e1464debd3a4831a7380d75c4c9Behdad Esfahbod  {HB_TAG('a','b','v','f'), false},
149c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('p','s','t','f'), false},
1509da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  {HB_TAG('c','j','c','t'), false},
1511d6846db9ebf84561bb30a4e48c6c43184914099Behdad Esfahbod  {HB_TAG('v','a','t','u'), true},
152c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod};
153c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod
154c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod/* Same order as the indic_basic_features array */
155c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbodenum {
156c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  _NUKT,
157c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  AKHN,
158c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  RPHF,
159df6d45c693c417bf311e6fa49f18a8558542e525Behdad Esfahbod  _RKRF,
160c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  PREF,
161c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  BLWF,
162c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  HALF,
16329f106d7fba25e1464debd3a4831a7380d75c4c9Behdad Esfahbod  ABVF,
164c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  PSTF,
1651d6846db9ebf84561bb30a4e48c6c43184914099Behdad Esfahbod  CJCT,
1661d6846db9ebf84561bb30a4e48c6c43184914099Behdad Esfahbod  VATU
167b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod};
168b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
169eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodstatic const feature_list_t
170eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodindic_other_features[] =
171b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
172eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('i','n','i','t'), false},
173eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('p','r','e','s'), true},
174eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('a','b','v','s'), true},
175eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('b','l','w','s'), true},
176eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('p','s','t','s'), true},
177eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('h','a','l','n'), true},
178eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
179eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('d','i','s','t'), true},
180eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('a','b','v','m'), true},
181eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('b','l','w','m'), true},
182eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod};
183eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
184743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
185743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
186743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodinitial_reordering (const hb_ot_map_t *map,
187743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_face_t *face,
188743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_buffer_t *buffer,
189743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    void *user_data HB_UNUSED);
190f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbodstatic void
191f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbodfinal_reordering (const hb_ot_map_t *map,
192f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod		  hb_face_t *face,
193f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod		  hb_buffer_t *buffer,
194743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		  void *user_data HB_UNUSED);
195b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
196b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbodvoid
1979f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod_hb_ot_shape_complex_collect_features_indic (hb_ot_map_builder_t *map,
1989f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod					     const hb_segment_properties_t *props HB_UNUSED)
199b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
200f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_bool_feature (HB_TAG('l','o','c','l'));
201a54a5505a35eef5315a8e2e7a79502901e3eff5fBehdad Esfahbod  /* The Indic specs do not require ccmp, but we apply it here since if
202a54a5505a35eef5315a8e2e7a79502901e3eff5fBehdad Esfahbod   * there is a use of it, it's typically at the beginning. */
203f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_bool_feature (HB_TAG('c','c','m','p'));
204f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
205743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  map->add_gsub_pause (initial_reordering, NULL);
206f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
207412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (indic_basic_features); i++) {
20876f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod    map->add_bool_feature (indic_basic_features[i].tag, indic_basic_features[i].is_global);
209412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod    map->add_gsub_pause (NULL, NULL);
210412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  }
211b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
212f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_gsub_pause (final_reordering, NULL);
213f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
214412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (indic_other_features); i++) {
215eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod    map->add_bool_feature (indic_other_features[i].tag, indic_other_features[i].is_global);
216412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod    map->add_gsub_pause (NULL, NULL);
217412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  }
218b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod}
219b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
220867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
22111138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbodhb_ot_shape_normalization_mode_t
22211138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbod_hb_ot_shape_complex_normalization_preference_indic (void)
22302cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod{
22402cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod  /* We want split matras decomposed by the common shaping logic. */
22511138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbod  return HB_OT_SHAPE_NORMALIZATION_MODE_DECOMPOSED;
22602cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod}
22702cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod
228867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
229b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbodvoid
2309f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod_hb_ot_shape_complex_setup_masks_indic (hb_ot_map_t *map HB_UNUSED,
2319f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod					hb_buffer_t *buffer,
2323f18236a03880c0960f5990dc90685f6146951a6Behdad Esfahbod					hb_font_t *font HB_UNUSED)
233b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
2349f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod  HB_BUFFER_ALLOCATE_VAR (buffer, indic_category);
2359f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod  HB_BUFFER_ALLOCATE_VAR (buffer, indic_position);
2369f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod
237743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* We cannot setup masks here.  We save information about characters
238743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * and setup masks later on in a pause-callback. */
239743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
240743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  unsigned int count = buffer->len;
241b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  for (unsigned int i = 0; i < count; i++)
242b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  {
24392332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    hb_glyph_info_t &info = buffer->info[i];
24492332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    unsigned int type = get_indic_categories (info.codepoint);
24592332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod
24692332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    info.indic_category() = type & 0x0F;
24792332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    info.indic_position() = type >> 4;
24892332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod
2493399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod    /* The spec says U+0952 is OT_A.  However, testing shows that Uniscribe
2503399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * treats U+0951..U+0952 all as OT_VD.
2513399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * TESTS:
2523399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * U+092E,U+0947,U+0952
2533399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * U+092E,U+0952,U+0947
2543399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * U+092E,U+0947,U+0951
2553399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * U+092E,U+0951,U+0947
2563399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod     * */
2573399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod    if (unlikely (hb_in_range<hb_codepoint_t> (info.codepoint, 0x0951, 0x0954)))
2583399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod      info.indic_category() = OT_VD;
2593399a06e7033651ee926448737bdb18e553c1796Behdad Esfahbod
26092332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    if (info.indic_category() == OT_C) {
26192332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_position() = consonant_position (info.codepoint);
26292332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      if (is_ra (info.codepoint))
26392332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod	info.indic_category() = OT_Ra;
2647d09c98a1fff97127e48eae48d380dc9fcff288eBehdad Esfahbod    } else if (info.indic_category() == OT_RS) {
2657d09c98a1fff97127e48eae48d380dc9fcff288eBehdad Esfahbod      info.indic_position() = POS_ABOVE_M;
26692332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    } else if (info.indic_category() == OT_SM ||
26792332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod	       info.indic_category() == OT_VD) {
26892332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_position() = POS_SMVD;
26992332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    } else if (unlikely (info.codepoint == 0x200C))
27092332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_category() = OT_ZWNJ;
27192332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod    else if (unlikely (info.codepoint == 0x200D))
27292332e5116271a5d96e532005fe750e7552a6cbbBehdad Esfahbod      info.indic_category() = OT_ZWJ;
27318c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod    else if (unlikely (info.codepoint == 0x25CC))
27418c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod      info.indic_category() = OT_DOTTEDCIRCLE;
275b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  }
276743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
277b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
27845d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodstatic int
27945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodcompare_indic_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
28045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod{
28145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  int a = pa->indic_position();
28245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  int b = pb->indic_position();
28345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
28445d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  return a < b ? -1 : a == b ? 0 : +1;
28545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod}
286867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
2877ea58db311bfb0d8f804d1e9f4a1f004bd45075aBehdad Esfahbod/* Rules from:
2887ea58db311bfb0d8f804d1e9f4a1f004bd45075aBehdad Esfahbod * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */
2897ea58db311bfb0d8f804d1e9f4a1f004bd45075aBehdad Esfahbod
290743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
29170fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbodinitial_reordering_consonant_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *basic_mask_array,
292ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod				       unsigned int start, unsigned int end)
293743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
294ee58f3bc75d2d071a71b94063bf12205a5871acbBehdad Esfahbod  hb_glyph_info_t *info = buffer->info;
295743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
296617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod
297743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 1. Find base consonant:
298743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
299743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * The shaping engine finds the base consonant of the syllable, using the
300743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * following algorithm: starting from the end of the syllable, move backwards
301743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * until a consonant is found that does not have a below-base or post-base
302743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * form (post-base forms have to follow below-base forms), or that is not a
303743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * pre-base reordering Ra, or arrive at the first consonant. The consonant
304743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * stopped at will be the base.
305743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
306743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o If the syllable starts with Ra + Halant (in a script that has Reph)
307743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *     and has more than one consonant, Ra is excluded from candidates for
308743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *     base consonants.
309743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
310743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
3115e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod  unsigned int base = end;
31276b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  bool has_reph = false;
313743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
31476b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  {
315617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
316617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod     *    and has more than one consonant, Ra is excluded from candidates for
317617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod     *    base consonants. */
318617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    unsigned int limit = start;
31970fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod    if (basic_mask_array[RPHF] &&
320617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod	start + 3 <= end &&
321617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod	info[start].indic_category() == OT_Ra &&
322617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod	info[start + 1].indic_category() == OT_H &&
323617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod	!is_joiner (info[start + 2]))
324617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    {
325617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod      limit += 2;
326617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod      base = start;
327617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod      has_reph = true;
328617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    };
32976b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod
3303c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    /* -> starting from the end of the syllable, move backwards */
3313c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    unsigned int i = end;
3323c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    do {
3333c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      i--;
3343c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      /* -> until a consonant is found */
3353c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      if (is_consonant (info[i]))
336743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod      {
3373c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	/* -> that does not have a below-base or post-base form
3383c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	 * (post-base forms have to follow below-base forms), */
3393c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	if (info[i].indic_position() != POS_BELOW_C &&
3403c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	    info[i].indic_position() != POS_POST_C)
3413c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	{
3423c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	  base = i;
3433c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	  break;
3443c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	}
345743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
3463c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	/* -> or that is not a pre-base reordering Ra,
3473c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	 *
3483c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	 * TODO
3493c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	 */
350e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
3513c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	/* -> or arrive at the first consonant. The consonant stopped at will
3523c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	 * be the base. */
3533c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	base = i;
3543c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      }
3553c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      else
3563c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	if (is_joiner (info[i]))
3573c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	  break;
3583c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    } while (i > limit);
3593c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    if (base < start)
3603c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      base = start; /* Just in case... */
361743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
362617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod
363617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
364617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod     *    and has more than one consonant, Ra is excluded from candidates for
365617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod     *    base consonants. */
366617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    if (has_reph && base == start) {
367617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod      /* Have no other consonant, so Reph is not formed and Ra becomes base. */
368617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod      has_reph = false;
369617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    }
3705e4e21fce4b548b0b8a5951bc8f35a9f27428192Behdad Esfahbod  }
3713d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod
3723d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod
373743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 2. Decompose and reorder Matras:
374743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
375743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * Each matra and any syllable modifier sign in the cluster are moved to the
376743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * appropriate position relative to the consonant(s) in the cluster. The
377743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * shaping engine decomposes two- or three-part matras into their constituent
378743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * parts before any repositioning. Matra characters are classified by which
379743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * consonant in a conjunct they have affinity for and are reordered to the
380743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * following positions:
381743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
382743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o Before first half form in the syllable
383743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After subjoined consonants
384743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After post-form consonant
385743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After main consonant (for above marks)
386743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
387743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * IMPLEMENTATION NOTES:
388743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
389743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * The normalize() routine has already decomposed matras for us, so we don't
390743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * need to worry about that.
391743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
392743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
393743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
394743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 3.  Reorder marks to canonical order:
395743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
396743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * Adjacent nukta and halant or nukta and vedic sign are always repositioned
397743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * if necessary, so that the nukta is first.
398743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
399743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * IMPLEMENTATION NOTES:
400743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
401743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * We don't need to do this: the normalize() routine already did this for us.
402743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
403743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
404743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
40545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* Reorder characters */
40645d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
4073c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod  for (unsigned int i = start; i < base; i++)
408dbccf87eef0d26838fa4bb3ae26410f6c4818836Behdad Esfahbod    info[i].indic_position() = POS_PRE_C;
409dbccf87eef0d26838fa4bb3ae26410f6c4818836Behdad Esfahbod  info[base].indic_position() = POS_BASE_C;
41045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
411fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod  /* Handle beginning Ra */
4125e4e21fce4b548b0b8a5951bc8f35a9f27428192Behdad Esfahbod  if (has_reph)
413dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod    info[start].indic_position() = POS_RA_TO_BECOME_REPH;
414fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod
415f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  /* For old-style Indic script tags, move the first post-base Halant after
416f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod   * last consonant. */
417a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod  if (IS_OLD_INDIC_TAG (map->get_chosen_script (0))) {
4183c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = base + 1; i < end; i++)
419f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod      if (info[i].indic_category() == OT_H) {
420f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        unsigned int j;
421f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        for (j = end - 1; j > i; j--)
422190eb31a16178269aecaf5d2ecc9012f956749f4Behdad Esfahbod	  if (is_consonant (info[j]))
423f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	    break;
424f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	if (j > i) {
425f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  /* Move Halant to after last consonant. */
426f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  hb_glyph_info_t t = info[i];
427f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  memmove (&info[i], &info[i + 1], (j - i) * sizeof (info[0]));
428f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  info[j] = t;
429f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	}
430f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        break;
431f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod      }
432f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  }
433f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod
43445d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* Attach ZWJ, ZWNJ, nukta, and halant to previous char to move with them. */
435a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  if (!indic_options ().uniscribe_bug_compatible)
436ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  {
437ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    /* Please update the Uniscribe branch when touching this! */
438ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    for (unsigned int i = start + 1; i < end; i++)
439ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod      if ((FLAG (info[i].indic_category()) & (FLAG (OT_ZWNJ) | FLAG (OT_ZWJ) | FLAG (OT_N) | FLAG (OT_H))))
440ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	info[i].indic_position() = info[i - 1].indic_position();
441ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  } else {
44267ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod    /*
44367ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod     * Uniscribe doesn't move the Halant with Left Matra.
44467ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod     * TEST: U+092B,U+093F,U+094DE
44567ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod     */
446ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    /* Please update the non-Uniscribe branch when touching this! */
447ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    for (unsigned int i = start + 1; i < end; i++)
448ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod      if ((FLAG (info[i].indic_category()) & (FLAG (OT_ZWNJ) | FLAG (OT_ZWJ) | FLAG (OT_N) | FLAG (OT_H)))) {
449ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	info[i].indic_position() = info[i - 1].indic_position();
4506a091df9b403b147ef78f3974610dedf4ce1e08aBehdad Esfahbod	if (info[i].indic_category() == OT_H && info[i].indic_position() == POS_PRE_M)
451ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	  for (unsigned int j = i; j > start; j--)
4526a091df9b403b147ef78f3974610dedf4ce1e08aBehdad Esfahbod	    if (info[j - 1].indic_position() != POS_PRE_M) {
453ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	      info[i].indic_position() = info[j - 1].indic_position();
454ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	      break;
455ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	    }
456ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod      }
457ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  }
45845d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
45945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* We do bubble-sort, skip malicious clusters attempts */
460b99d63ae114fb58f129562b293a8a66543d499adBehdad Esfahbod  if (end - start < 64)
461a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod  {
462a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    /* Sit tight, rock 'n roll! */
463d3637edb248162970e202e9d0671540274192844Behdad Esfahbod    hb_bubble_sort (info + start, end - start, compare_indic_order);
464a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    /* Find base again */
465a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    base = end;
4663c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = start; i < end; i++)
467a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod      if (info[i].indic_position() == POS_BASE_C) {
468a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod        base = i;
469a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod	break;
470a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod      }
471a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod  }
47245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
473743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Setup masks now */
474743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
475281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod  {
476281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    hb_mask_t mask;
477281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod
478dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod    /* Reph */
479668c6046c1b3af3bd316bda0cc8636f2a5e8df42Behdad Esfahbod    for (unsigned int i = start; i < end && info[i].indic_position() == POS_RA_TO_BECOME_REPH; i++)
48070fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod      info[i].mask |= basic_mask_array[RPHF];
481dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
482281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Pre-base */
48370fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod    mask = basic_mask_array[HALF] | basic_mask_array[AKHN] | basic_mask_array[CJCT];
4843c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = start; i < base; i++)
485281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod      info[i].mask  |= mask;
486281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Base */
48770fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod    mask = basic_mask_array[AKHN] | basic_mask_array[CJCT];
488281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    info[base].mask |= mask;
489281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Post-base */
49070fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod    mask = basic_mask_array[BLWF] | basic_mask_array[ABVF] | basic_mask_array[PSTF] | basic_mask_array[CJCT];
4913c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = base + 1; i < end; i++)
492281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod      info[i].mask  |= mask;
493281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod  }
4949da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
49517d7de91d76406d3e92db37d9eef2fc615f06e68Behdad Esfahbod  /* XXX This will not match for old-Indic spec since the Halant-Ra order is reversed already. */
49617d7de91d76406d3e92db37d9eef2fc615f06e68Behdad Esfahbod  if (basic_mask_array[PREF] &&
49717d7de91d76406d3e92db37d9eef2fc615f06e68Behdad Esfahbod      base + 3 <= end &&
49817d7de91d76406d3e92db37d9eef2fc615f06e68Behdad Esfahbod      info[base + 1].indic_category() == OT_H &&
49917d7de91d76406d3e92db37d9eef2fc615f06e68Behdad Esfahbod      info[base + 2].indic_category() == OT_Ra)
50017d7de91d76406d3e92db37d9eef2fc615f06e68Behdad Esfahbod  {
50117d7de91d76406d3e92db37d9eef2fc615f06e68Behdad Esfahbod    info[base + 1].mask |= basic_mask_array[PREF];
50217d7de91d76406d3e92db37d9eef2fc615f06e68Behdad Esfahbod    info[base + 2].mask |= basic_mask_array[PREF];
50317d7de91d76406d3e92db37d9eef2fc615f06e68Behdad Esfahbod  }
50417d7de91d76406d3e92db37d9eef2fc615f06e68Behdad Esfahbod
5059da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  /* Apply ZWJ/ZWNJ effects */
5063c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod  for (unsigned int i = start + 1; i < end; i++)
5079da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    if (is_joiner (info[i])) {
5089da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      bool non_joiner = info[i].indic_category() == OT_ZWNJ;
5096b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod      unsigned int j = i;
5109da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
5119da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      do {
5129da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod	j--;
5136b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod
51470fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod	info[j].mask &= ~basic_mask_array[CJCT];
5156b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod	if (non_joiner)
51670fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod	  info[j].mask &= ~basic_mask_array[HALF];
5176b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod
5189da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      } while (j > start && !is_consonant (info[j]));
5199da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    }
520743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
521743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
522743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
523743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
5249f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbodinitial_reordering_vowel_syllable (const hb_ot_map_t *map,
5259f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod				   hb_buffer_t *buffer,
52670fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod				   hb_mask_t *basic_mask_array,
527ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod				   unsigned int start, unsigned int end)
528743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
529c5306b6861cfaa50af40e8ceb058791fa06d7981Behdad Esfahbod  /* We made the vowels look like consonants.  So let's call the consonant logic! */
53070fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod  initial_reordering_consonant_syllable (map, buffer, basic_mask_array, start, end);
531743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
532743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
533743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
5349f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbodinitial_reordering_standalone_cluster (const hb_ot_map_t *map,
5359f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod				       hb_buffer_t *buffer,
53670fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod				       hb_mask_t *basic_mask_array,
537ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod				       unsigned int start, unsigned int end)
538743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
53918c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod  /* We treat NBSP/dotted-circle as if they are consonants, so we should just chain.
54018c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod   * Only if not in compatibility mode that is... */
54118c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod
542a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  if (indic_options ().uniscribe_bug_compatible)
54318c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod  {
54418c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod    /* For dotted-circle, this is what Uniscribe does:
54518c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod     * If dotted-circle is the last glyph, it just does nothing.
54618c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod     * Ie. It doesn't form Reph. */
54718c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod    if (buffer->info[end - 1].indic_category() == OT_DOTTEDCIRCLE)
54818c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod      return;
54918c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod  }
55018c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod
55170fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod  initial_reordering_consonant_syllable (map, buffer, basic_mask_array, start, end);
552743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
553743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
554743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
5559f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbodinitial_reordering_non_indic (const hb_ot_map_t *map HB_UNUSED,
5569f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod			      hb_buffer_t *buffer HB_UNUSED,
55770fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod			      hb_mask_t *basic_mask_array HB_UNUSED,
5583f18236a03880c0960f5990dc90685f6146951a6Behdad Esfahbod			      unsigned int start HB_UNUSED, unsigned int end HB_UNUSED)
559743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
560743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Nothing to do right now.  If we ever switch to using the output
561743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * buffer in the reordering process, we'd need to next_glyph() here. */
562743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
563743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
564743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod#include "hb-ot-shape-complex-indic-machine.hh"
565743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
566743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
567743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodinitial_reordering (const hb_ot_map_t *map,
5683f18236a03880c0960f5990dc90685f6146951a6Behdad Esfahbod		    hb_face_t *face HB_UNUSED,
569743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_buffer_t *buffer,
570743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    void *user_data HB_UNUSED)
571743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
57270fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod  hb_mask_t basic_mask_array[ARRAY_LENGTH (indic_basic_features)] = {0};
573b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  unsigned int num_masks = ARRAY_LENGTH (indic_basic_features);
574b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  for (unsigned int i = 0; i < num_masks; i++)
57570fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod    basic_mask_array[i] = map->get_1_mask (indic_basic_features[i].tag);
576743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
57770fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod  find_syllables (map, buffer, basic_mask_array);
578b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod}
579b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
580743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
58146e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbodfinal_reordering_syllable (hb_buffer_t *buffer,
58246e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod			   hb_mask_t init_mask, hb_mask_t pref_mask,
583ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod			   unsigned int start, unsigned int end)
584743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
5854ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  hb_glyph_info_t *info = buffer->info;
5864ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
587e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod  /* 4. Final reordering:
588e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
589e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * After the localized forms and basic shaping forms GSUB features have been
590e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * applied (see below), the shaping engine performs some final glyph
591e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * reordering before applying all the remaining font features to the entire
592e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * cluster.
5934ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod   */
5944ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
5954ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  /* Find base again */
5964ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  unsigned int base = end;
5974ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  for (unsigned int i = start; i < end; i++)
5984ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    if (info[i].indic_position() == POS_BASE_C) {
5994ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod      base = i;
6004ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod      break;
6014ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod    }
6024ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
6034705a7026900e51f6430f03a73c87f2df035df92Behdad Esfahbod  unsigned int start_of_last_cluster = base;
6044705a7026900e51f6430f03a73c87f2df035df92Behdad Esfahbod
6054ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  /*   o Reorder matras:
606e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
607e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     If a pre-base matra character had been reordered before applying basic
608e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     features, the glyph can be moved closer to the main consonant based on
609e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     whether half-forms had been formed. Actual position for the matra is
610e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     defined as “after last standalone halant glyph, after initial matra
611e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     position and before the main consonant”. If ZWJ or ZWNJ follow this
612e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     halant, position is moved after it.
6134ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod   */
6144ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
615362d3db8d3527d0fef260a17d2466e92a4a25425Behdad Esfahbod  if (start < base) /* Otherwise there can't be any pre-base matra characters. */
6169d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod  {
617921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod    unsigned int new_pos = base - 1;
618921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod    while (new_pos > start &&
619921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod	   !(FLAG (info[new_pos].indic_category()) & (FLAG (OT_M) | FLAG (OT_H))))
620921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod      new_pos--;
6219d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    /* If we found no Halant we are done.  Otherwise only proceed if the Halant does
6229d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod     * not belong to the Matra itself! */
623921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod    if (info[new_pos].indic_category() == OT_H &&
624921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod	info[new_pos].indic_position() != POS_PRE_M) {
6259d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
626921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod      if (new_pos + 1 < end && is_joiner (info[new_pos + 1]))
627921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod	new_pos++;
6289d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod
6299d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      /* Now go see if there's actually any matras... */
630921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod      for (unsigned int i = new_pos; i > start; i--)
6316a091df9b403b147ef78f3974610dedf4ce1e08aBehdad Esfahbod	if (info[i - 1].indic_position () == POS_PRE_M)
6329d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod	{
6331a1dbe9a2787f226f3e43063da8eb6633438b0a0Behdad Esfahbod	  unsigned int old_pos = i - 1;
6341a1dbe9a2787f226f3e43063da8eb6633438b0a0Behdad Esfahbod	  hb_glyph_info_t tmp = info[old_pos];
6351a1dbe9a2787f226f3e43063da8eb6633438b0a0Behdad Esfahbod	  memmove (&info[old_pos], &info[old_pos + 1], (new_pos - old_pos) * sizeof (info[0]));
6361a1dbe9a2787f226f3e43063da8eb6633438b0a0Behdad Esfahbod	  info[new_pos] = tmp;
637921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod	  start_of_last_cluster = MIN (new_pos, start_of_last_cluster);
638921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod	  new_pos--;
6399d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod	}
6409d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    }
6414ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  }
6424ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
6434ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
6444ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  /*   o Reorder reph:
645e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
646e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     Reph’s original position is always at the beginning of the syllable,
647e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     (i.e. it is not reordered at the character reordering stage). However,
648e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     it will be reordered according to the basic-forms shaping results.
649e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     Possible positions for reph, depending on the script, are; after main,
650e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     before post-base consonant forms, and after post-base consonant forms.
651dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod   */
652dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
653dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  /* If there's anything after the Ra that has the REPH pos, it ought to be halant.
654dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod   * Which means that the font has failed to ligate the Reph.  In which case, we
655dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod   * shouldn't move. */
656dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  if (start + 1 < end &&
657dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod      info[start].indic_position() == POS_RA_TO_BECOME_REPH &&
658dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod      info[start + 1].indic_position() != POS_RA_TO_BECOME_REPH)
659dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  {
66002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      unsigned int new_reph_pos;
66102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
66202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     enum reph_position_t {
663f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_AFTER_MAIN,
664f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_BEFORE_SUBSCRIPT,
665f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_AFTER_SUBSCRIPT,
666f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_BEFORE_POSTSCRIPT,
6679fc7a11469113d31d8095757c4fc038c3427d44aBehdad Esfahbod       REPH_AFTER_POSTSCRIPT
668f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod     } reph_pos;
669f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
670f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod     /* XXX Figure out old behavior too */
6717f852b644b8143492a02edfc853114aaa23446bdBehdad Esfahbod     switch ((hb_tag_t) buffer->props.script)
672f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod     {
673f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_MALAYALAM:
674f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_ORIYA:
675f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_AFTER_MAIN;
676f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
677f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
678f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_GURMUKHI:
679f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_BEFORE_SUBSCRIPT;
680f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
681f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
682f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_BENGALI:
683f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_AFTER_SUBSCRIPT;
684f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
685f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
686f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       default:
687f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_DEVANAGARI:
688f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_GUJARATI:
689f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_BEFORE_POSTSCRIPT;
690f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
691f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
692f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_KANNADA:
693f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_TAMIL:
694f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_TELUGU:
695f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_AFTER_POSTSCRIPT;
696f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
697f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod     }
69802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
699dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod    /*       1. If reph should be positioned after post-base consonant forms,
700dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          proceed to step 5.
70102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
7029d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    if (reph_pos == REPH_AFTER_POSTSCRIPT)
70302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
7049d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      goto reph_step_5;
70502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
70602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
70702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       2. If the reph repositioning class is not after post-base: target
708dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          position is after the first explicit halant glyph between the
709dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first post-reph consonant and last main consonant. If ZWJ or ZWNJ
710dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          are following this halant, position is moved after it. If such
711dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          position is found, this is the target position. Otherwise,
712dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          proceed to the next step.
713dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *
714dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          Note: in old-implementation fonts, where classifications were
715dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          fixed in shaping engine, there was no case where reph position
716dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          will be found on this step.
71702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
71802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
71902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      new_reph_pos = start + 1;
72002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      while (new_reph_pos < base && info[new_reph_pos].indic_category() != OT_H)
72102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	new_reph_pos++;
72202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
72302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      if (new_reph_pos < base && info[new_reph_pos].indic_category() == OT_H) {
72402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	/* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */
72502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1]))
72602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	  new_reph_pos++;
72702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	goto reph_move;
72802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      }
72902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
73002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
73102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       3. If reph should be repositioned after the main consonant: find the
732dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first consonant not ligated with main, or find the first
733dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          consonant that is not a potential pre-base reordering Ra.
73402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
7359d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    if (reph_pos == REPH_AFTER_MAIN)
73602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
737b504e060f008e95b1ba36c06600c9fea4f5d4808Behdad Esfahbod      new_reph_pos = base;
738b504e060f008e95b1ba36c06600c9fea4f5d4808Behdad Esfahbod      /* XXX Skip potential pre-base reordering Ra. */
739b504e060f008e95b1ba36c06600c9fea4f5d4808Behdad Esfahbod      while (new_reph_pos < end &&
740b504e060f008e95b1ba36c06600c9fea4f5d4808Behdad Esfahbod	     !( FLAG (info[new_reph_pos + 1].indic_position()) & (FLAG (POS_BELOW_C) | FLAG (POS_POST_C) | FLAG (POS_POST_M) | FLAG (POS_SMVD))))
741b504e060f008e95b1ba36c06600c9fea4f5d4808Behdad Esfahbod	new_reph_pos++;
742b504e060f008e95b1ba36c06600c9fea4f5d4808Behdad Esfahbod      if (new_reph_pos < end)
743b504e060f008e95b1ba36c06600c9fea4f5d4808Behdad Esfahbod        goto reph_move;
74402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
74502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
74602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       4. If reph should be positioned before post-base consonant, find
747dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first post-base classified consonant not ligated with main. If no
748dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          consonant is found, the target position should be before the
749dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first matra, syllable modifier sign or vedic sign.
75002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
7519d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    /* This is our take on what step 4 is trying to say (and failing, BADLY). */
7529d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    if (reph_pos == REPH_AFTER_SUBSCRIPT)
75302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
7549d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      new_reph_pos = base;
7559d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      while (new_reph_pos < end &&
7566a091df9b403b147ef78f3974610dedf4ce1e08aBehdad Esfahbod	     !( FLAG (info[new_reph_pos + 1].indic_position()) & (FLAG (POS_POST_C) | FLAG (POS_POST_M) | FLAG (POS_SMVD))))
7579d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod	new_reph_pos++;
7589d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      if (new_reph_pos < end)
7599d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod        goto reph_move;
76002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
76102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
76202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       5. If no consonant is found in steps 3 or 4, move reph to a position
763dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          immediately before the first post-base matra, syllable modifier
764dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          sign or vedic sign that has a reordering class after the intended
765dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          reph position. For example, if the reordering position for reph
766dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          is post-main, it will skip above-base matras that also have a
767dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          post-main position.
768dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     */
76902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_step_5:
77002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
77102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      /* XXX */
77202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
773dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
77402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       6. Otherwise, reorder reph to the end of the syllable.
77502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
77602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
77702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      new_reph_pos = end - 1;
77802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      while (new_reph_pos > start && info[new_reph_pos].indic_position() == POS_SMVD)
77902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	new_reph_pos--;
78002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
781892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod      /*
782892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * If the Reph is to be ending up after a Matra,Halant sequence,
783892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * position it before that Halant so it can interact with the Matra.
784892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * However, if it's a plain Consonant,Halant we shouldn't do that.
785892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * Uniscribe doesn't do this.
786892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * TEST: U+0930,U+094D,U+0915,U+094B,U+094D
787892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       */
788a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod      if (!indic_options ().uniscribe_bug_compatible &&
789892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod	  unlikely (info[new_reph_pos].indic_category() == OT_H)) {
79002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	for (unsigned int i = base + 1; i < new_reph_pos; i++)
79102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	  if (info[i].indic_category() == OT_M) {
79202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	    /* Ok, got it. */
79302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	    new_reph_pos--;
79402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	  }
79502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      }
79602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      goto reph_move;
7978df5636968389ac7bf8620ccd091fd4872b0bbeeBehdad Esfahbod    }
7988df5636968389ac7bf8620ccd091fd4872b0bbeeBehdad Esfahbod
79902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_move:
80002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
80102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      /* Move */
80202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      hb_glyph_info_t reph = info[start];
80302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      memmove (&info[start], &info[start + 1], (new_reph_pos - start) * sizeof (info[0]));
80402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      info[new_reph_pos] = reph;
80502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      start_of_last_cluster = start; /* Yay, one big cluster! */
80602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
807dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  }
808dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
809dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
810dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  /*   o Reorder pre-base reordering consonants:
811e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
812e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     If a pre-base reordering consonant is found, reorder it according to
813e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     the following rules:
814e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   */
815e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
81646e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod  if (pref_mask && base + 1 < end) /* Otherwise there can't be any pre-base reordering Ra. */
81746e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod  {
81878818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod    /*       1. Only reorder a glyph produced by substitution during application
81978818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod     *          of the <pref> feature. (Note that a font may shape a Ra consonant with
82078818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod     *          the feature generally but block it in certain contexts.)
82178818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod     */
82246e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod    if ((info[base + 1].mask & pref_mask) != 0 &&
82346e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod	(base + 2 == end ||
82446e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod	 (info[base + 2].mask & pref_mask) == 0))
82546e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod    {
82678818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod      /*
82778818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod       *       2. Try to find a target position the same way as for pre-base matra.
82878818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod       *          If it is found, reorder pre-base consonant glyph.
82978818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod       *
83078818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod       *       3. If position is not found, reorder immediately before main
83178818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod       *          consonant.
83278818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod       */
83378818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod
83478818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod      unsigned int new_pos = base;
83578818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod      while (new_pos > start + 1 &&
83678818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod	     !(FLAG (info[new_pos - 1].indic_category()) & (FLAG (OT_M) | FLAG (OT_H))))
83778818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod	new_pos--;
83878818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod
83978818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod      if (new_pos > start && info[new_pos - 1].indic_category() == OT_H)
84078818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod	/* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
84178818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod	if (new_pos < end && is_joiner (info[new_pos]))
84278818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod	  new_pos++;
84378818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod
84478818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod      {
84578818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod	unsigned int old_pos = base + 1;
84678818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod	hb_glyph_info_t tmp = info[old_pos];
84778818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod	memmove (&info[new_pos + 1], &info[new_pos], (old_pos - new_pos) * sizeof (info[0]));
84878818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod	info[new_pos] = tmp;
84978818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod	start_of_last_cluster = MIN (new_pos, start_of_last_cluster);
85078818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod      }
85146e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod    }
85246e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod  }
853eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
854eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
855a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod  /* Apply 'init' to the Left Matra if it's a word start. */
8566a091df9b403b147ef78f3974610dedf4ce1e08aBehdad Esfahbod  if (info[start].indic_position () == POS_PRE_M &&
857a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod      (!start ||
858eace47b173807d94b29a6490d0bc3c9f8f6168d1Behdad Esfahbod       !(FLAG (_hb_glyph_info_get_general_category (&info[start - 1])) &
859a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	 (FLAG (HB_UNICODE_GENERAL_CATEGORY_LOWERCASE_LETTER) |
860a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_MODIFIER_LETTER) |
861a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER) |
862a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_TITLECASE_LETTER) |
863a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_UPPERCASE_LETTER) |
864a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK) |
865a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK) |
866a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)))))
86746e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod    info[start].mask |= init_mask;
868a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod
869eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
87021d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod
87121d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod  /* Finish off the clusters and go home! */
87221d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod
873a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  if (!indic_options ().uniscribe_bug_compatible)
874ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  {
87521d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    /* This is what Uniscribe does.  Ie. add cluster boundaries after Halant,ZWNJ.
87621d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod     * This means, half forms are submerged into the main consonants cluster.
87721d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod     * This is unnecessary, and makes cursor positioning harder, but that's what
87821d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod     * Uniscribe does. */
87921d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    unsigned int cluster_start = start;
88021d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    for (unsigned int i = start + 1; i < start_of_last_cluster; i++)
88121d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod      if (info[i - 1].indic_category() == OT_H && info[i].indic_category() == OT_ZWNJ) {
88221d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod        i++;
88321d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod	buffer->merge_clusters (cluster_start, i);
88421d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod	cluster_start = i;
88521d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod      }
88621d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    start_of_last_cluster = cluster_start;
88721d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod  }
88821d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod
88921d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod  buffer->merge_clusters (start_of_last_cluster, end);
890ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod}
891e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
892e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
893ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodstatic void
894ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodfinal_reordering (const hb_ot_map_t *map,
8953f18236a03880c0960f5990dc90685f6146951a6Behdad Esfahbod		  hb_face_t *face HB_UNUSED,
896ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod		  hb_buffer_t *buffer,
897ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod		  void *user_data HB_UNUSED)
898ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod{
899ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  unsigned int count = buffer->len;
900ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  if (!count) return;
901ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod
90246e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod  hb_mask_t init_mask = map->get_1_mask (HB_TAG('i','n','i','t'));
90346e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod  hb_mask_t pref_mask = map->get_1_mask (HB_TAG('p','r','e','f'));
904eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
905ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  hb_glyph_info_t *info = buffer->info;
906ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  unsigned int last = 0;
907cee7187447b76b22e1bb6136d137b35ac49c3a5dBehdad Esfahbod  unsigned int last_syllable = info[0].syllable();
908ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  for (unsigned int i = 1; i < count; i++)
909cee7187447b76b22e1bb6136d137b35ac49c3a5dBehdad Esfahbod    if (last_syllable != info[i].syllable()) {
91046e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod      final_reordering_syllable (buffer, init_mask, pref_mask, last, i);
911ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod      last = i;
912cee7187447b76b22e1bb6136d137b35ac49c3a5dBehdad Esfahbod      last_syllable = info[last].syllable();
913ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod    }
91446e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod  final_reordering_syllable (buffer, init_mask, pref_mask, last, count);
915e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
916743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category);
917743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position);
918743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
919743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
920743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
921b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
922