hb-ot-shape-complex-indic.cc revision 1e7d860613032e40a3f90e2caa2ee5ac44ab8c8c
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"
2949c5ec51444f27f33e1eb6aa1959c61b08fa89c0Behdad Esfahbod#include "hb-ot-layout-private.hh"
30352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod
31a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod#define OLD_INDIC_TAG(script) (((hb_tag_t) script) | 0x20000000)
32a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod#define IS_OLD_INDIC_TAG(tag) ( \
33a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_BENGALI) || \
34a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_DEVANAGARI) || \
35a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_GUJARATI) || \
36a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_GURMUKHI) || \
37a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_KANNADA) || \
38a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_MALAYALAM) || \
39a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_ORIYA) || \
40a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_TAMIL) || \
41a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_TELUGU) \
42a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod			      )
43a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbodstruct indic_options_t
44ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod{
45a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  int initialized : 1;
46a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  int uniscribe_bug_compatible : 1;
47a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod};
48a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod
49a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbodunion indic_options_union_t {
50a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  int i;
51a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  indic_options_t opts;
52a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod};
53a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad EsfahbodASSERT_STATIC (sizeof (int) == sizeof (indic_options_union_t));
54a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod
55a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbodstatic indic_options_union_t
56a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbodindic_options_init (void)
57a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod{
58a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  indic_options_union_t u;
59a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  u.i = 0;
60a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  u.opts.initialized = 1;
61a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod
62a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  char *c = getenv ("HB_OT_INDIC_OPTIONS");
63a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  u.opts.uniscribe_bug_compatible = c && strstr (c, "uniscribe-bug-compatible");
64a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod
65a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  return u;
66a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod}
67a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod
68a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbodinline indic_options_t
69a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbodindic_options (void)
70a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod{
71a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  static indic_options_union_t options;
72a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod
73a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  if (unlikely (!options.i)) {
74a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod    /* This is idempotent and threadsafe. */
75a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod    options = indic_options_init ();
76ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  }
77ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod
78a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  return options.opts;
79a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod}
80a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod
81ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod
82743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic int
83743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodcompare_codepoint (const void *pa, const void *pb)
84743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
85743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  hb_codepoint_t a = * (hb_codepoint_t *) pa;
86743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  hb_codepoint_t b = * (hb_codepoint_t *) pb;
87743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
88743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  return a < b ? -1 : a == b ? 0 : +1;
89743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
90743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
91f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbodstatic bool
92693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbodwould_substitute (hb_codepoint_t    *glyphs,
93693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod		  unsigned int       glyphs_count,
94693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod		  hb_tag_t           feature_tag,
95693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod		  const hb_ot_map_t *map,
96693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod		  hb_face_t         *face)
97743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
98f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  unsigned int lookup_indices[32];
99f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  unsigned int offset, len;
100f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod
101f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  offset = 0;
102f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  do {
103f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod    len = ARRAY_LENGTH (lookup_indices);
104f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod    hb_ot_layout_feature_get_lookup_indexes (face, HB_OT_TAG_GSUB,
105f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod					     map->get_feature_index (0/*GSUB*/, feature_tag),
106f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod					     offset,
107f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod					     &len,
108f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod					     lookup_indices);
109f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod
110f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod    for (unsigned int i = 0; i < len; i++)
111f860366456d9e59b139a940da6d89c3c4fb9e96eBehdad Esfahbod      if (hb_ot_layout_would_substitute_lookup_fast (face, glyphs, glyphs_count, lookup_indices[i]))
112f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod	return true;
113f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod
114f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod    offset += len;
115f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  } while (len == ARRAY_LENGTH (lookup_indices));
116f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod
117f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  return false;
118f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod}
119743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
120f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbodstatic indic_position_t
121693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbodconsonant_position (hb_codepoint_t     u,
122693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod		    const hb_ot_map_t *map,
123693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod		    hb_font_t         *font)
124f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod{
1255249f3aee108b0f41770d137e63a625f594418e7Behdad Esfahbod  if ((u & ~0x007F) == 0x1780)
1265249f3aee108b0f41770d137e63a625f594418e7Behdad Esfahbod    return POS_BELOW_C; /* In Khmer coeng model, all are subjoining. */
1275249f3aee108b0f41770d137e63a625f594418e7Behdad Esfahbod
128f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  hb_codepoint_t virama = (u & ~0x007F) | 0x004D;
129f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  if ((u & ~0x007F) == 0x0D80) virama = 0x0DCA; /* Sinahla */
130f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  hb_codepoint_t glyphs[2];
131f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod
13251e764de441072e7c9f67de23e8ed717b9b8957dBehdad Esfahbod  unsigned int virama_pos = IS_OLD_INDIC_TAG (map->get_chosen_script (0)) ? 1 : 0;
13351e764de441072e7c9f67de23e8ed717b9b8957dBehdad Esfahbod  hb_font_get_glyph (font, virama, 0, &glyphs[virama_pos]);
13451e764de441072e7c9f67de23e8ed717b9b8957dBehdad Esfahbod  hb_font_get_glyph (font, u,      0, &glyphs[1-virama_pos]);
135f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod
136f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  hb_face_t *face = hb_font_get_face (font);
137f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  if (would_substitute (glyphs, ARRAY_LENGTH (glyphs), HB_TAG('p','r','e','f'), map, face)) return POS_BELOW_C;
138f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  if (would_substitute (glyphs, ARRAY_LENGTH (glyphs), HB_TAG('b','l','w','f'), map, face)) return POS_BELOW_C;
139f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  if (would_substitute (glyphs, ARRAY_LENGTH (glyphs), HB_TAG('p','s','t','f'), map, face)) return POS_POST_C;
140f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  return POS_BASE_C;
141743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
142743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
1438bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod#define MATRA_POS_LEFT(u)	POS_PRE_M
1448c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod#define MATRA_POS_RIGHT(u)	( \
1458c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_DEVA(u) ? POS_AFTER_SUB   : \
1468c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_BENG(u) ? POS_AFTER_POST  : \
1478c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_GURM(u) ? POS_AFTER_POST  : \
1488c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_GUJA(u) ? POS_AFTER_POST  : \
1498c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_ORYA(u) ? POS_AFTER_POST  : \
1508c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_TAML(u) ? POS_AFTER_POST  : \
151fa247ebe524f92fa95d344ba912f704262879c13Behdad Esfahbod				  IS_TELU(u) ? (u <= 0x0C42 ? POS_BEFORE_SUB : POS_AFTER_SUB) : \
15287cd63266e73af316b250573ef57388a0bcc9133Behdad Esfahbod				  IS_KNDA(u) ? (u < 0x0CC3 || u > 0xCD6 ? POS_BEFORE_SUB : POS_AFTER_SUB) : \
1538c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_MLYM(u) ? POS_AFTER_POST  : \
1548c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_SINH(u) ? POS_AFTER_SUB   : \
1558c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  /*default*/  POS_AFTER_SUB     \
1568c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				)
1578c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod#define MATRA_POS_TOP(u)	( /* BENG and MLYM don't have top matras. */ \
1588c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_DEVA(u) ? POS_AFTER_SUB  : \
1596411e74caf23af7b0545f1fe54d19a1c8da895e8Behdad Esfahbod				  IS_GURM(u) ? POS_AFTER_POST  : /* Deviate from spec */ \
1608c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_GUJA(u) ? POS_AFTER_SUB  : \
1618c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_ORYA(u) ? POS_AFTER_MAIN : \
1628c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_TAML(u) ? POS_AFTER_SUB  : \
1638c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_TELU(u) ? POS_BEFORE_SUB : \
1648c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_KNDA(u) ? POS_BEFORE_SUB : \
1658c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_SINH(u) ? POS_AFTER_SUB  : \
1668c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  /*default*/  POS_AFTER_SUB    \
1678c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				)
1688c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod#define MATRA_POS_BOTTOM(u)	( \
1698c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_DEVA(u) ? POS_AFTER_SUB  : \
1708c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_BENG(u) ? POS_AFTER_SUB  : \
1718c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_GURM(u) ? POS_AFTER_POST : \
1728c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_GUJA(u) ? POS_AFTER_POST : \
1738c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_ORYA(u) ? POS_AFTER_SUB  : \
1748c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_TAML(u) ? POS_AFTER_POST : \
1758c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_TELU(u) ? POS_BEFORE_SUB : \
1768c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_KNDA(u) ? POS_BEFORE_SUB : \
1778c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_MLYM(u) ? POS_AFTER_POST : \
1788c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_SINH(u) ? POS_AFTER_SUB  : \
1798c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  /*default*/  POS_AFTER_SUB    \
1808c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				)
1818bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod
1828bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod
1838bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbodstatic indic_position_t
1848bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbodmatra_position (hb_codepoint_t u, indic_position_t side)
1858bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod{
1868bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  switch ((int) side)
1878bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  {
1888bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod    case POS_PRE_C:	return MATRA_POS_LEFT (u);
1898bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod    case POS_POST_C:	return MATRA_POS_RIGHT (u);
1908bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod    case POS_ABOVE_C:	return MATRA_POS_TOP (u);
1918bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod    case POS_BELOW_C:	return MATRA_POS_BOTTOM (u);
1928bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  };
1938bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  abort ();
1948bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod}
1958bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod
196771a8f50289e8fa458cfc3cd84f73a380ce98077Behdad Esfahbodstatic inline bool
197352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbodis_ra (hb_codepoint_t u)
198352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod{
199352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod  return !!bsearch (&u, ra_chars,
200352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod		    ARRAY_LENGTH (ra_chars),
201352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod		    sizeof (ra_chars[0]),
202352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod		    compare_codepoint);
203352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod}
204352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod
205771a8f50289e8fa458cfc3cd84f73a380ce98077Behdad Esfahbodstatic inline bool
206771a8f50289e8fa458cfc3cd84f73a380ce98077Behdad Esfahbodis_one_of (const hb_glyph_info_t &info, unsigned int flags)
207771a8f50289e8fa458cfc3cd84f73a380ce98077Behdad Esfahbod{
208771a8f50289e8fa458cfc3cd84f73a380ce98077Behdad Esfahbod  /* If it ligated, all bets are off. */
209c3e6fdc3791168cf2b4c9412e751f187d58faa42Behdad Esfahbod  if (is_a_ligature (info)) return false;
210771a8f50289e8fa458cfc3cd84f73a380ce98077Behdad Esfahbod  return !!(FLAG (info.indic_category()) & flags);
211771a8f50289e8fa458cfc3cd84f73a380ce98077Behdad Esfahbod}
212771a8f50289e8fa458cfc3cd84f73a380ce98077Behdad Esfahbod
2139ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod#define JOINER_FLAGS (FLAG (OT_ZWJ) | FLAG (OT_ZWNJ))
214771a8f50289e8fa458cfc3cd84f73a380ce98077Behdad Esfahbodstatic inline bool
2159da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbodis_joiner (const hb_glyph_info_t &info)
2169ee27a928a989c71923cef82a9e9828f8e9ca051Behdad Esfahbod{
217771a8f50289e8fa458cfc3cd84f73a380ce98077Behdad Esfahbod  return is_one_of (info, JOINER_FLAGS);
2189da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod}
2199da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
220771a8f50289e8fa458cfc3cd84f73a380ce98077Behdad Esfahbod/* Note:
221771a8f50289e8fa458cfc3cd84f73a380ce98077Behdad Esfahbod *
222771a8f50289e8fa458cfc3cd84f73a380ce98077Behdad Esfahbod * We treat Vowels and placeholders as if they were consonants.  This is safe because Vowels
223771a8f50289e8fa458cfc3cd84f73a380ce98077Behdad Esfahbod * cannot happen in a consonant syllable.  The plus side however is, we can call the
224771a8f50289e8fa458cfc3cd84f73a380ce98077Behdad Esfahbod * consonant syllable logic from the vowel syllable function and get it all right! */
2259ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod#define CONSONANT_FLAGS (FLAG (OT_C) | FLAG (OT_Ra) | FLAG (OT_V) | FLAG (OT_NBSP) | FLAG (OT_DOTTEDCIRCLE))
226771a8f50289e8fa458cfc3cd84f73a380ce98077Behdad Esfahbodstatic inline bool
2279da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbodis_consonant (const hb_glyph_info_t &info)
2289da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod{
229771a8f50289e8fa458cfc3cd84f73a380ce98077Behdad Esfahbod  return is_one_of (info, CONSONANT_FLAGS);
2309ee27a928a989c71923cef82a9e9828f8e9ca051Behdad Esfahbod}
231c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod
2329ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod#define HALANT_OR_COENG_FLAGS (FLAG (OT_H) | FLAG (OT_Coeng))
233771a8f50289e8fa458cfc3cd84f73a380ce98077Behdad Esfahbodstatic inline bool
234deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbodis_halant_or_coeng (const hb_glyph_info_t &info)
235deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod{
236771a8f50289e8fa458cfc3cd84f73a380ce98077Behdad Esfahbod  return is_one_of (info, HALANT_OR_COENG_FLAGS);
237baacd090df97610e3f6d1b2a110dc67b6c6f9f5cBehdad Esfahbod}
238baacd090df97610e3f6d1b2a110dc67b6c6f9f5cBehdad Esfahbod
2399ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbodstatic inline void
240693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbodset_indic_properties (hb_glyph_info_t   &info,
241693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod		      const hb_ot_map_t *map,
242693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod		      hb_font_t         *font)
2439ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod{
2449ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod  hb_codepoint_t u = info.codepoint;
2459ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod  unsigned int type = get_indic_categories (u);
2466824a7194e01b77eddb95bd95a9b32e219140912Behdad Esfahbod  indic_category_t cat = (indic_category_t) (type & 0x0F);
2476824a7194e01b77eddb95bd95a9b32e219140912Behdad Esfahbod  indic_position_t pos = (indic_position_t) (type >> 4);
2488bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod
2498bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod
2508bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  /*
2516824a7194e01b77eddb95bd95a9b32e219140912Behdad Esfahbod   * Re-assign category
2528bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod   */
2538bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod
2549ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
2559ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod  /* The spec says U+0952 is OT_A.  However, testing shows that Uniscribe
2569ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod   * treats U+0951..U+0952 all as OT_VD.
2579ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod   * TESTS:
2589ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod   * U+092E,U+0947,U+0952
2599ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod   * U+092E,U+0952,U+0947
2609ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod   * U+092E,U+0947,U+0951
2619ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod   * U+092E,U+0951,U+0947
2629ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod   * */
2639ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod  if (unlikely (hb_in_range<hb_codepoint_t> (u, 0x0951, 0x0954)))
2649ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod    cat = OT_VD;
2659ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
266330b329c8905a37ca88c556dea82c70d74c77458Behdad Esfahbod  if (unlikely (u == 0x17D1))
267330b329c8905a37ca88c556dea82c70d74c77458Behdad Esfahbod    cat = OT_X;
2689ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod  if (cat == OT_X &&
2696824a7194e01b77eddb95bd95a9b32e219140912Behdad Esfahbod      unlikely (hb_in_range<hb_codepoint_t> (u, 0x17CB, 0x17D3))) /* Khmer Various signs */
2706824a7194e01b77eddb95bd95a9b32e219140912Behdad Esfahbod  {
2716824a7194e01b77eddb95bd95a9b32e219140912Behdad Esfahbod    /* These are like Top Matras. */
2726824a7194e01b77eddb95bd95a9b32e219140912Behdad Esfahbod    cat = OT_M;
2736824a7194e01b77eddb95bd95a9b32e219140912Behdad Esfahbod    pos = POS_ABOVE_C;
2746824a7194e01b77eddb95bd95a9b32e219140912Behdad Esfahbod  }
2758d00e8d0e7d10f823e6975fecaffb9d557b1a99aBehdad Esfahbod  if (u == 0x17C6) /* Khmer Bindu doesn't like to be repositioned. */
2768d00e8d0e7d10f823e6975fecaffb9d557b1a99aBehdad Esfahbod    cat = OT_N;
2779ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
2788bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  if (unlikely (u == 0x17D2)) cat = OT_Coeng; /* Khmer coeng */
2798bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  else if (unlikely (u == 0x200C)) cat = OT_ZWNJ;
2808bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  else if (unlikely (u == 0x200D)) cat = OT_ZWJ;
2818bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  else if (unlikely (u == 0x25CC)) cat = OT_DOTTEDCIRCLE;
2823604d64ced909ade91998d294a7b4b2ee14d47aaBehdad Esfahbod  else if (unlikely (u == 0x0A71)) cat = OT_SM; /* GURMUKHI ADDAK.  More like consonant medial. like 0A75. */
2839ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
2849ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod  if (cat == OT_Repha) {
2859ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod    /* There are two kinds of characters marked as Repha:
2869ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod     * - The ones that are GenCat=Mn are already positioned visually, ie. after base. (eg. Khmer)
2879ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod     * - The ones that are GenCat=Lo is encoded logically, ie. beginning of syllable. (eg. Malayalam)
2889ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod     *
2899ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod     * We recategorize the first kind to look like a Nukta and attached to the base directly.
2909ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod     */
2919ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod    if (_hb_glyph_info_get_general_category (&info) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
2929ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod      cat = OT_N;
2939ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod  }
2949ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
2959ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
2968bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod
2978bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  /*
2986824a7194e01b77eddb95bd95a9b32e219140912Behdad Esfahbod   * Re-assign position.
2998bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod   */
3008bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod
3018bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  if ((FLAG (cat) & CONSONANT_FLAGS))
3028bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  {
303f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod    pos = consonant_position (u, map, font);
3049ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod    if (is_ra (u))
3059ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod      cat = OT_Ra;
3068bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  }
3078bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  else if (cat == OT_M)
3088bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  {
3098bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod    pos = matra_position (u, pos);
3108bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  }
3118bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  else if (cat == OT_SM || cat == OT_VD)
3128bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  {
3139ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod    pos = POS_SMVD;
3148bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  }
3158bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod
316bdd080431a40bc941ece3230f338b94a46bd12a2Behdad Esfahbod  if (unlikely (u == 0x0B01)) pos = POS_BEFORE_SUB; /* Oriya Bindu is BeforeSub in the spec. */
3178bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod
3187573799126e812a047daa5f64121ec959866b3c8Behdad Esfahbod
3199ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
3209ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod  info.indic_category() = cat;
3219ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod  info.indic_position() = pos;
3229ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod}
3239ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
3249ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
3259ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
3269ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
3279ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
3289ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
3299ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
330eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodstruct feature_list_t {
331c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  hb_tag_t tag;
332c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  hb_bool_t is_global;
333eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod};
334eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
335d5c4edcdd6df32f2f23aca44f14838b4baab4d7aBehdad Esfahbod/* These features are applied one at a time, given the order in this table. */
336eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodstatic const feature_list_t
337eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodindic_basic_features[] =
338b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
339c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('n','u','k','t'), true},
340e0475345d5d7db8dbc8b554beedfa2435c5d7fd1Behdad Esfahbod  {HB_TAG('a','k','h','n'), true},
341c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('r','p','h','f'), false},
3421ac075b227090a9ad930dcc1670236c176b27067Behdad Esfahbod  {HB_TAG('r','k','r','f'), true},
343c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('p','r','e','f'), false},
344c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('b','l','w','f'), false},
345c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('h','a','l','f'), false},
34629f106d7fba25e1464debd3a4831a7380d75c4c9Behdad Esfahbod  {HB_TAG('a','b','v','f'), false},
347c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('p','s','t','f'), false},
3480201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod  {HB_TAG('c','f','a','r'), false},
34920b68e699f73e6ce046c0ec143d40b3d6d48e06bBehdad Esfahbod  {HB_TAG('c','j','c','t'), true},
3501d6846db9ebf84561bb30a4e48c6c43184914099Behdad Esfahbod  {HB_TAG('v','a','t','u'), true},
351c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod};
352c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod
353c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod/* Same order as the indic_basic_features array */
354c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbodenum {
355c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  _NUKT,
356e0475345d5d7db8dbc8b554beedfa2435c5d7fd1Behdad Esfahbod  _AKHN,
357c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  RPHF,
358df6d45c693c417bf311e6fa49f18a8558542e525Behdad Esfahbod  _RKRF,
359c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  PREF,
360c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  BLWF,
361c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  HALF,
36229f106d7fba25e1464debd3a4831a7380d75c4c9Behdad Esfahbod  ABVF,
363c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  PSTF,
3640201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod  CFAR,
36520b68e699f73e6ce046c0ec143d40b3d6d48e06bBehdad Esfahbod  _CJCT,
3661d6846db9ebf84561bb30a4e48c6c43184914099Behdad Esfahbod  VATU
367b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod};
368b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
369d5c4edcdd6df32f2f23aca44f14838b4baab4d7aBehdad Esfahbod/* These features are applied all at once. */
370eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodstatic const feature_list_t
371eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodindic_other_features[] =
372b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
373eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('i','n','i','t'), false},
374eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('p','r','e','s'), true},
375eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('a','b','v','s'), true},
376eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('b','l','w','s'), true},
377eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('p','s','t','s'), true},
378eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('h','a','l','n'), true},
379eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
380eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('d','i','s','t'), true},
381eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('a','b','v','m'), true},
382eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('b','l','w','m'), true},
383eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod};
384eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
385743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
386743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
387743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodinitial_reordering (const hb_ot_map_t *map,
388743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_face_t *face,
389743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_buffer_t *buffer,
390743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    void *user_data HB_UNUSED);
391f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbodstatic void
392f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbodfinal_reordering (const hb_ot_map_t *map,
393f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod		  hb_face_t *face,
394f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod		  hb_buffer_t *buffer,
395743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		  void *user_data HB_UNUSED);
396b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
397693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbodstatic void
398693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbodcollect_features_indic (const hb_ot_complex_shaper_t  *shaper,
399693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod			hb_ot_map_builder_t           *map,
400693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod			const hb_segment_properties_t *props)
401b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
402f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_bool_feature (HB_TAG('l','o','c','l'));
403a54a5505a35eef5315a8e2e7a79502901e3eff5fBehdad Esfahbod  /* The Indic specs do not require ccmp, but we apply it here since if
404a54a5505a35eef5315a8e2e7a79502901e3eff5fBehdad Esfahbod   * there is a use of it, it's typically at the beginning. */
405f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_bool_feature (HB_TAG('c','c','m','p'));
406f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
407743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  map->add_gsub_pause (initial_reordering, NULL);
408f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
409412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (indic_basic_features); i++) {
41076f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod    map->add_bool_feature (indic_basic_features[i].tag, indic_basic_features[i].is_global);
411412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod    map->add_gsub_pause (NULL, NULL);
412412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  }
413b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
414f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_gsub_pause (final_reordering, NULL);
415f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
416d5c4edcdd6df32f2f23aca44f14838b4baab4d7aBehdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (indic_other_features); i++)
417eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod    map->add_bool_feature (indic_other_features[i].tag, indic_other_features[i].is_global);
418b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod}
419b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
420693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbodstatic void
421693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbodoverride_features_indic (const hb_ot_complex_shaper_t  *shaper,
422693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod			 hb_ot_map_builder_t           *map,
423693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod			 const hb_segment_properties_t *props)
424d96838ef951ce6170eb2dc576ebcba2262cf7008Behdad Esfahbod{
425af92b4cc90e4184d5bdd8037c551ed482700114fBehdad Esfahbod  /* Uniscribe does not apply 'kern'. */
426af92b4cc90e4184d5bdd8037c551ed482700114fBehdad Esfahbod  if (indic_options ().uniscribe_bug_compatible)
427af92b4cc90e4184d5bdd8037c551ed482700114fBehdad Esfahbod    map->add_feature (HB_TAG('k','e','r','n'), 0, true);
428d96838ef951ce6170eb2dc576ebcba2262cf7008Behdad Esfahbod}
429d96838ef951ce6170eb2dc576ebcba2262cf7008Behdad Esfahbod
430867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
431693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbodstatic void
432693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbodsetup_masks_indic (const hb_ot_complex_shaper_t *shaper,
433693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod		   const hb_ot_map_t            *map,
434693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod		   hb_buffer_t                  *buffer,
435693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod		   hb_font_t                    *font)
436b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
4379f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod  HB_BUFFER_ALLOCATE_VAR (buffer, indic_category);
4389f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod  HB_BUFFER_ALLOCATE_VAR (buffer, indic_position);
4399f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod
440743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* We cannot setup masks here.  We save information about characters
441743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * and setup masks later on in a pause-callback. */
442743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
443743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  unsigned int count = buffer->len;
444b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  for (unsigned int i = 0; i < count; i++)
445f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod    set_indic_properties (buffer->info[i], map, font);
446743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
447b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
44845d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodstatic int
44945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodcompare_indic_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
45045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod{
45145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  int a = pa->indic_position();
45245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  int b = pb->indic_position();
45345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
45445d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  return a < b ? -1 : a == b ? 0 : +1;
45545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod}
456867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
4577ea58db311bfb0d8f804d1e9f4a1f004bd45075aBehdad Esfahbod/* Rules from:
4587ea58db311bfb0d8f804d1e9f4a1f004bd45075aBehdad Esfahbod * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */
4597ea58db311bfb0d8f804d1e9f4a1f004bd45075aBehdad Esfahbod
460743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
46170fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbodinitial_reordering_consonant_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *basic_mask_array,
462ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod				       unsigned int start, unsigned int end)
463743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
464ee58f3bc75d2d071a71b94063bf12205a5871acbBehdad Esfahbod  hb_glyph_info_t *info = buffer->info;
465743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
466617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod
467743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 1. Find base consonant:
468743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
469743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * The shaping engine finds the base consonant of the syllable, using the
470743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * following algorithm: starting from the end of the syllable, move backwards
471743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * until a consonant is found that does not have a below-base or post-base
472743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * form (post-base forms have to follow below-base forms), or that is not a
473743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * pre-base reordering Ra, or arrive at the first consonant. The consonant
474743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * stopped at will be the base.
475743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
476743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o If the syllable starts with Ra + Halant (in a script that has Reph)
477743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *     and has more than one consonant, Ra is excluded from candidates for
478743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *     base consonants.
479743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
480743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
4815e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod  unsigned int base = end;
48276b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  bool has_reph = false;
483743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
48476b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  {
485617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
486617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod     *    and has more than one consonant, Ra is excluded from candidates for
487617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod     *    base consonants. */
488617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    unsigned int limit = start;
48970fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod    if (basic_mask_array[RPHF] &&
490617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod	start + 3 <= end &&
491617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod	info[start].indic_category() == OT_Ra &&
492617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod	info[start + 1].indic_category() == OT_H &&
493f31d97e44eeb6fb141f3de928e27e033fc7b1f47Behdad Esfahbod	(unlikely (buffer->props.script == HB_SCRIPT_SINHALA || buffer->props.script == HB_SCRIPT_TELUGU) ?
494f31d97e44eeb6fb141f3de928e27e033fc7b1f47Behdad Esfahbod	 info[start + 2].indic_category() == OT_ZWJ /* In Sinhala & Telugu, form Reph only if ZWJ is present */:
4953285e107c9a83aeb552e67f9460680ff6d167d88Behdad Esfahbod	 !is_joiner (info[start + 2] /* In other scripts, any joiner blocks Reph formation */ )
4963285e107c9a83aeb552e67f9460680ff6d167d88Behdad Esfahbod	))
497617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    {
498617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod      limit += 2;
4993285e107c9a83aeb552e67f9460680ff6d167d88Behdad Esfahbod      while (limit < end && is_joiner (info[limit]))
5003285e107c9a83aeb552e67f9460680ff6d167d88Behdad Esfahbod        limit++;
501617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod      base = start;
502617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod      has_reph = true;
503617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    };
50476b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod
50514dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod     enum base_position_t {
50614dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod       BASE_FIRST,
50714dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod       BASE_LAST
50814dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod     } base_pos;
50914dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod
51014dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod    switch ((hb_tag_t) buffer->props.script)
51114dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod    {
51234c215036f5fcdc7599b1ab0591b56dbb3811902Behdad Esfahbod      case HB_SCRIPT_SINHALA:
51314dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod      case HB_SCRIPT_KHMER:
51414dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod	base_pos = BASE_FIRST;
51514dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod	break;
51614dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod
51714dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod      default:
51814dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod	base_pos = BASE_LAST;
51914dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod	break;
52014dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod    }
52114dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod
52214dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod    if (base_pos == BASE_LAST)
5235d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod    {
5245d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod      /* -> starting from the end of the syllable, move backwards */
5255d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod      unsigned int i = end;
52692a1ad7bef9efb456ab87bd63818cfbed7da3f6fBehdad Esfahbod      bool seen_below = false;
5275d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod      do {
5285d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	i--;
5295d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	/* -> until a consonant is found */
5305d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	if (is_consonant (info[i]))
5313c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	{
5325d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	  /* -> that does not have a below-base or post-base form
5335d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	   * (post-base forms have to follow below-base forms), */
5345d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	  if (info[i].indic_position() != POS_BELOW_C &&
53592a1ad7bef9efb456ab87bd63818cfbed7da3f6fBehdad Esfahbod	      (info[i].indic_position() != POS_POST_C || seen_below))
5365d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	  {
5375d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	    base = i;
5385d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	    break;
5395d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	  }
54092a1ad7bef9efb456ab87bd63818cfbed7da3f6fBehdad Esfahbod	  if (info[i].indic_position() == POS_BELOW_C)
54192a1ad7bef9efb456ab87bd63818cfbed7da3f6fBehdad Esfahbod	    seen_below = true;
5425d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod
5435d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	  /* -> or that is not a pre-base reordering Ra,
5445d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	   *
54525e302da9a712e6f1d63b0d243a8df0d326ddba3Behdad Esfahbod	   * IMPLEMENTATION NOTES:
54625e302da9a712e6f1d63b0d243a8df0d326ddba3Behdad Esfahbod	   *
54725e302da9a712e6f1d63b0d243a8df0d326ddba3Behdad Esfahbod	   * Our pre-base reordering Ra's are marked POS_BELOW, so will be skipped
54825e302da9a712e6f1d63b0d243a8df0d326ddba3Behdad Esfahbod	   * by the logic above already.
5495d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	   */
5505d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod
5515d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	  /* -> or arrive at the first consonant. The consonant stopped at will
5525d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	   * be the base. */
5533c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	  base = i;
5543c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	}
5555d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	else
556a9e45c32e4a0d6da33c52f8427aa694e57f52eb9Behdad Esfahbod	{
55788f413b56f2858d149e2fc067685aeecaea779caBehdad Esfahbod	  /* A ZWJ after a Halant stops the base search, and requests an explicit
55888f413b56f2858d149e2fc067685aeecaea779caBehdad Esfahbod	   * half form.
55988f413b56f2858d149e2fc067685aeecaea779caBehdad Esfahbod	   * A ZWJ before a Halant, requests a subjoined form instead, and hence
56088f413b56f2858d149e2fc067685aeecaea779caBehdad Esfahbod	   * search continues.  This is particularly important for Bengali
56188f413b56f2858d149e2fc067685aeecaea779caBehdad Esfahbod	   * sequence Ra,H,Ya that shouls form Ya-Phalaa by subjoining Ya. */
56288f413b56f2858d149e2fc067685aeecaea779caBehdad Esfahbod	  if (start < i &&
56388f413b56f2858d149e2fc067685aeecaea779caBehdad Esfahbod	      info[i].indic_category() == OT_ZWJ &&
56488f413b56f2858d149e2fc067685aeecaea779caBehdad Esfahbod	      info[i - 1].indic_category() == OT_H)
5655d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	    break;
566a9e45c32e4a0d6da33c52f8427aa694e57f52eb9Behdad Esfahbod	}
5675d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod      } while (i > limit);
5685d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod    }
5695d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod    else
5705d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod    {
5715d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod      /* In scripts without half forms (eg. Khmer), the first consonant is always the base. */
5725d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod
5735d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod      if (!has_reph)
5745d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	base = limit;
57534c215036f5fcdc7599b1ab0591b56dbb3811902Behdad Esfahbod
57634c215036f5fcdc7599b1ab0591b56dbb3811902Behdad Esfahbod      /* Find the last base consonant that is not blocked by ZWJ.  If there is
57771fd5e80ad06c8e85a1112cc89e129d6cd03f82cBehdad Esfahbod       * a ZWJ right before a base consonant, that would request a subjoined form. */
57834c215036f5fcdc7599b1ab0591b56dbb3811902Behdad Esfahbod      for (unsigned int i = limit; i < end; i++)
57934c215036f5fcdc7599b1ab0591b56dbb3811902Behdad Esfahbod        if (is_consonant (info[i]) && info[i].indic_position() == POS_BASE_C)
58071fd5e80ad06c8e85a1112cc89e129d6cd03f82cBehdad Esfahbod	{
58171fd5e80ad06c8e85a1112cc89e129d6cd03f82cBehdad Esfahbod	  if (limit < i && info[i - 1].indic_category() == OT_ZWJ)
58271fd5e80ad06c8e85a1112cc89e129d6cd03f82cBehdad Esfahbod	    break;
58371fd5e80ad06c8e85a1112cc89e129d6cd03f82cBehdad Esfahbod          else
58471fd5e80ad06c8e85a1112cc89e129d6cd03f82cBehdad Esfahbod	    base = i;
58571fd5e80ad06c8e85a1112cc89e129d6cd03f82cBehdad Esfahbod	}
58634c215036f5fcdc7599b1ab0591b56dbb3811902Behdad Esfahbod
58734c215036f5fcdc7599b1ab0591b56dbb3811902Behdad Esfahbod      /* Mark all subsequent consonants as below. */
58834c215036f5fcdc7599b1ab0591b56dbb3811902Behdad Esfahbod      for (unsigned int i = base + 1; i < end; i++)
58934c215036f5fcdc7599b1ab0591b56dbb3811902Behdad Esfahbod        if (is_consonant (info[i]) && info[i].indic_position() == POS_BASE_C)
59034c215036f5fcdc7599b1ab0591b56dbb3811902Behdad Esfahbod	  info[i].indic_position() = POS_BELOW_C;
5915d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod    }
592743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
593617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
594617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod     *    and has more than one consonant, Ra is excluded from candidates for
5952278eefcdb3dd0d492b9d07176fbecc1f0516bb7Behdad Esfahbod     *    base consonants.
5962278eefcdb3dd0d492b9d07176fbecc1f0516bb7Behdad Esfahbod     *
5972278eefcdb3dd0d492b9d07176fbecc1f0516bb7Behdad Esfahbod     *  Only do this for unforced Reph. (ie. not for Ra,H,ZWJ. */
5982278eefcdb3dd0d492b9d07176fbecc1f0516bb7Behdad Esfahbod    if (has_reph && base == start && start + 2 == limit) {
599617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod      /* Have no other consonant, so Reph is not formed and Ra becomes base. */
600617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod      has_reph = false;
601617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    }
6025e4e21fce4b548b0b8a5951bc8f35a9f27428192Behdad Esfahbod  }
6032278eefcdb3dd0d492b9d07176fbecc1f0516bb7Behdad Esfahbod
60434c215036f5fcdc7599b1ab0591b56dbb3811902Behdad Esfahbod  if (base < end)
60534c215036f5fcdc7599b1ab0591b56dbb3811902Behdad Esfahbod    info[base].indic_position() = POS_BASE_C;
6063d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod
6073d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod
608743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 2. Decompose and reorder Matras:
609743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
610743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * Each matra and any syllable modifier sign in the cluster are moved to the
611743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * appropriate position relative to the consonant(s) in the cluster. The
612743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * shaping engine decomposes two- or three-part matras into their constituent
613743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * parts before any repositioning. Matra characters are classified by which
614743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * consonant in a conjunct they have affinity for and are reordered to the
615743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * following positions:
616743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
617743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o Before first half form in the syllable
618743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After subjoined consonants
619743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After post-form consonant
620743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After main consonant (for above marks)
621743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
622743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * IMPLEMENTATION NOTES:
623743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
624743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * The normalize() routine has already decomposed matras for us, so we don't
625743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * need to worry about that.
626743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
627743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
628743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
629743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 3.  Reorder marks to canonical order:
630743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
631743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * Adjacent nukta and halant or nukta and vedic sign are always repositioned
632743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * if necessary, so that the nukta is first.
633743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
634743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * IMPLEMENTATION NOTES:
635743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
636743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * We don't need to do this: the normalize() routine already did this for us.
637743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
638743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
639743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
64045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* Reorder characters */
64145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
6423c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod  for (unsigned int i = start; i < base; i++)
643900cf3d449bf36d4f8b1474590cae925fef48fc8Behdad Esfahbod    info[i].indic_position() = MIN (POS_PRE_C, (indic_position_t) info[i].indic_position());
64455f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod
645075d671f1093d2e3c58f7f45568696030f1b3efdBehdad Esfahbod  if (base < end)
646075d671f1093d2e3c58f7f45568696030f1b3efdBehdad Esfahbod    info[base].indic_position() = POS_BASE_C;
64745d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
64855f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod  /* Mark final consonants.  A final consonant is one appearing after a matra,
64955f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod   * like in Khmer. */
65055f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod  for (unsigned int i = base + 1; i < end; i++)
65155f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod    if (info[i].indic_category() == OT_M) {
65255f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod      for (unsigned int j = i + 1; j < end; j++)
65355f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod        if (is_consonant (info[j])) {
65455f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod	  info[j].indic_position() = POS_FINAL_C;
65555f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod	  break;
65655f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod	}
65755f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod      break;
65855f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod    }
65955f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod
660fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod  /* Handle beginning Ra */
6615e4e21fce4b548b0b8a5951bc8f35a9f27428192Behdad Esfahbod  if (has_reph)
662dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod    info[start].indic_position() = POS_RA_TO_BECOME_REPH;
663fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod
664f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  /* For old-style Indic script tags, move the first post-base Halant after
665f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod   * last consonant. */
666a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod  if (IS_OLD_INDIC_TAG (map->get_chosen_script (0))) {
6673c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = base + 1; i < end; i++)
668f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod      if (info[i].indic_category() == OT_H) {
669f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        unsigned int j;
670f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        for (j = end - 1; j > i; j--)
671190eb31a16178269aecaf5d2ecc9012f956749f4Behdad Esfahbod	  if (is_consonant (info[j]))
672f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	    break;
673f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	if (j > i) {
674f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  /* Move Halant to after last consonant. */
675f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  hb_glyph_info_t t = info[i];
676f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  memmove (&info[i], &info[i + 1], (j - i) * sizeof (info[0]));
677f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  info[j] = t;
678f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	}
679f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        break;
680f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod      }
681f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  }
682f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod
68381202bd860e4034c18d9f80c5a4f33d9f48463a3Behdad Esfahbod  /* Attach misc marks to previous char to move with them. */
684ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  {
68581202bd860e4034c18d9f80c5a4f33d9f48463a3Behdad Esfahbod    indic_position_t last_pos = POS_START;
68681202bd860e4034c18d9f80c5a4f33d9f48463a3Behdad Esfahbod    for (unsigned int i = start; i < end; i++)
68781202bd860e4034c18d9f80c5a4f33d9f48463a3Behdad Esfahbod    {
68881202bd860e4034c18d9f80c5a4f33d9f48463a3Behdad Esfahbod      if ((FLAG (info[i].indic_category()) & (JOINER_FLAGS | FLAG (OT_N) | FLAG (OT_RS) | HALANT_OR_COENG_FLAGS)))
68981202bd860e4034c18d9f80c5a4f33d9f48463a3Behdad Esfahbod      {
69081202bd860e4034c18d9f80c5a4f33d9f48463a3Behdad Esfahbod	info[i].indic_position() = last_pos;
69181202bd860e4034c18d9f80c5a4f33d9f48463a3Behdad Esfahbod	if (unlikely (indic_options ().uniscribe_bug_compatible &&
69281202bd860e4034c18d9f80c5a4f33d9f48463a3Behdad Esfahbod		      info[i].indic_category() == OT_H &&
69381202bd860e4034c18d9f80c5a4f33d9f48463a3Behdad Esfahbod		      info[i].indic_position() == POS_PRE_M))
69481202bd860e4034c18d9f80c5a4f33d9f48463a3Behdad Esfahbod	{
69581202bd860e4034c18d9f80c5a4f33d9f48463a3Behdad Esfahbod	  /*
69681202bd860e4034c18d9f80c5a4f33d9f48463a3Behdad Esfahbod	   * Uniscribe doesn't move the Halant with Left Matra.
69781202bd860e4034c18d9f80c5a4f33d9f48463a3Behdad Esfahbod	   * TEST: U+092B,U+093F,U+094DE
69881202bd860e4034c18d9f80c5a4f33d9f48463a3Behdad Esfahbod	   */
699ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	  for (unsigned int j = i; j > start; j--)
7006a091df9b403b147ef78f3974610dedf4ce1e08aBehdad Esfahbod	    if (info[j - 1].indic_position() != POS_PRE_M) {
701ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	      info[i].indic_position() = info[j - 1].indic_position();
702ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	      break;
703ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	    }
70481202bd860e4034c18d9f80c5a4f33d9f48463a3Behdad Esfahbod	}
70581202bd860e4034c18d9f80c5a4f33d9f48463a3Behdad Esfahbod      } else if (info[i].indic_position() != POS_SMVD) {
70681202bd860e4034c18d9f80c5a4f33d9f48463a3Behdad Esfahbod        last_pos = (indic_position_t) info[i].indic_position();
707ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod      }
70881202bd860e4034c18d9f80c5a4f33d9f48463a3Behdad Esfahbod    }
709ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  }
71074ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod  /* Re-attach ZWJ, ZWNJ, and halant to next char, for after-base consonants. */
71174ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod  {
71274ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod    unsigned int last_halant = end;
71374ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod    for (unsigned int i = base + 1; i < end; i++)
714deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod      if (is_halant_or_coeng (info[i]))
71574ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod        last_halant = i;
71674ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod      else if (is_consonant (info[i])) {
71774ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod	for (unsigned int j = last_halant; j < i; j++)
71881202bd860e4034c18d9f80c5a4f33d9f48463a3Behdad Esfahbod	  if (info[j].indic_position() != POS_SMVD)
71981202bd860e4034c18d9f80c5a4f33d9f48463a3Behdad Esfahbod	    info[j].indic_position() = info[i].indic_position();
72074ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod      }
72174ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod  }
72245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
723a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod  {
7247b2a7dadd6c616bbfe1d8358700cab9cee88e584Behdad Esfahbod    /* Things are out-of-control for post base positions, they may shuffle
7257b2a7dadd6c616bbfe1d8358700cab9cee88e584Behdad Esfahbod     * around like crazy, so merge clusters.  For pre-base stuff, we handle
7267b2a7dadd6c616bbfe1d8358700cab9cee88e584Behdad Esfahbod     * cluster issues in final reordering. */
7277b2a7dadd6c616bbfe1d8358700cab9cee88e584Behdad Esfahbod    buffer->merge_clusters (base, end);
728a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    /* Sit tight, rock 'n roll! */
729d3637edb248162970e202e9d0671540274192844Behdad Esfahbod    hb_bubble_sort (info + start, end - start, compare_indic_order);
730a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    /* Find base again */
731a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    base = end;
7323c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = start; i < end; i++)
733a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod      if (info[i].indic_position() == POS_BASE_C) {
734a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod        base = i;
735a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod	break;
736a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod      }
737a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod  }
73845d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
739743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Setup masks now */
740743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
741281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod  {
742281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    hb_mask_t mask;
743281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod
744dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod    /* Reph */
745668c6046c1b3af3bd316bda0cc8636f2a5e8df42Behdad Esfahbod    for (unsigned int i = start; i < end && info[i].indic_position() == POS_RA_TO_BECOME_REPH; i++)
74670fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod      info[i].mask |= basic_mask_array[RPHF];
747dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
748281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Pre-base */
74920b68e699f73e6ce046c0ec143d40b3d6d48e06bBehdad Esfahbod    mask = basic_mask_array[HALF];
7503c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = start; i < base; i++)
751281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod      info[i].mask  |= mask;
752281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Base */
75320b68e699f73e6ce046c0ec143d40b3d6d48e06bBehdad Esfahbod    mask = 0;
754075d671f1093d2e3c58f7f45568696030f1b3efdBehdad Esfahbod    if (base < end)
755075d671f1093d2e3c58f7f45568696030f1b3efdBehdad Esfahbod      info[base].mask |= mask;
756281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Post-base */
75720b68e699f73e6ce046c0ec143d40b3d6d48e06bBehdad Esfahbod    mask = basic_mask_array[BLWF] | basic_mask_array[ABVF] | basic_mask_array[PSTF];
7583c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = base + 1; i < end; i++)
759281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod      info[i].mask  |= mask;
760281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod  }
7619da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
76217d7de91d76406d3e92db37d9eef2fc615f06e68Behdad Esfahbod  /* XXX This will not match for old-Indic spec since the Halant-Ra order is reversed already. */
7635f0eaaad129ff04d56b8756bebf19fbc242718c9Behdad Esfahbod  if (basic_mask_array[PREF] && base + 2 < end)
76417d7de91d76406d3e92db37d9eef2fc615f06e68Behdad Esfahbod  {
765771a8f50289e8fa458cfc3cd84f73a380ce98077Behdad Esfahbod    /* Find a Halant,Ra sequence and mark it for pre-base reordering processing. */
7668e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod    for (unsigned int i = base + 1; i + 1 < end; i++)
767deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod      if (is_halant_or_coeng (info[i]) &&
7688e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	  info[i + 1].indic_category() == OT_Ra)
7698e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod      {
7700201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	info[i++].mask |= basic_mask_array[PREF];
7710201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	info[i++].mask |= basic_mask_array[PREF];
7720201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod
7730201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	/* Mark the subsequent stuff with 'cfar'.  Used in Khmer.
7740201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	 * Read the feature spec.
7750201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	 * This allows distinguishing the following cases with MS Khmer fonts:
7760201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	 * U+1784,U+17D2,U+179A,U+17D2,U+1782
7770201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	 * U+1784,U+17D2,U+1782,U+17D2,U+179A
7780201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	 */
7790201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	for (; i < end; i++)
7800201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	  info[i].mask |= basic_mask_array[CFAR];
7810201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod
7828e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	break;
7838e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod      }
78417d7de91d76406d3e92db37d9eef2fc615f06e68Behdad Esfahbod  }
78517d7de91d76406d3e92db37d9eef2fc615f06e68Behdad Esfahbod
7869da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  /* Apply ZWJ/ZWNJ effects */
7873c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod  for (unsigned int i = start + 1; i < end; i++)
7889da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    if (is_joiner (info[i])) {
7899da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      bool non_joiner = info[i].indic_category() == OT_ZWNJ;
7906b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod      unsigned int j = i;
7919da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
7929da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      do {
7939da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod	j--;
7946b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod
79520b68e699f73e6ce046c0ec143d40b3d6d48e06bBehdad Esfahbod	/* A ZWJ disables CJCT, however, it's mere presence is enough
79620b68e699f73e6ce046c0ec143d40b3d6d48e06bBehdad Esfahbod	 * to disable ligation.  No explicit action needed. */
79720b68e699f73e6ce046c0ec143d40b3d6d48e06bBehdad Esfahbod
79820b68e699f73e6ce046c0ec143d40b3d6d48e06bBehdad Esfahbod	/* A ZWNJ disables HALF. */
7996b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod	if (non_joiner)
80070fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod	  info[j].mask &= ~basic_mask_array[HALF];
8016b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod
8029da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      } while (j > start && !is_consonant (info[j]));
8039da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    }
804743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
805743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
806743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
807743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
8089f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbodinitial_reordering_vowel_syllable (const hb_ot_map_t *map,
8099f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod				   hb_buffer_t *buffer,
81070fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod				   hb_mask_t *basic_mask_array,
811ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod				   unsigned int start, unsigned int end)
812743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
813c5306b6861cfaa50af40e8ceb058791fa06d7981Behdad Esfahbod  /* We made the vowels look like consonants.  So let's call the consonant logic! */
81470fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod  initial_reordering_consonant_syllable (map, buffer, basic_mask_array, start, end);
815743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
816743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
817743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
8189f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbodinitial_reordering_standalone_cluster (const hb_ot_map_t *map,
8199f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod				       hb_buffer_t *buffer,
82070fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod				       hb_mask_t *basic_mask_array,
821ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod				       unsigned int start, unsigned int end)
822743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
82318c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod  /* We treat NBSP/dotted-circle as if they are consonants, so we should just chain.
82418c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod   * Only if not in compatibility mode that is... */
82518c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod
826a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  if (indic_options ().uniscribe_bug_compatible)
82718c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod  {
82818c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod    /* For dotted-circle, this is what Uniscribe does:
82918c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod     * If dotted-circle is the last glyph, it just does nothing.
83018c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod     * Ie. It doesn't form Reph. */
83118c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod    if (buffer->info[end - 1].indic_category() == OT_DOTTEDCIRCLE)
83218c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod      return;
83318c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod  }
83418c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod
83570fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod  initial_reordering_consonant_syllable (map, buffer, basic_mask_array, start, end);
836743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
837743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
838743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
8399f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbodinitial_reordering_non_indic (const hb_ot_map_t *map HB_UNUSED,
8409f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod			      hb_buffer_t *buffer HB_UNUSED,
84170fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod			      hb_mask_t *basic_mask_array HB_UNUSED,
8423f18236a03880c0960f5990dc90685f6146951a6Behdad Esfahbod			      unsigned int start HB_UNUSED, unsigned int end HB_UNUSED)
843743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
844743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Nothing to do right now.  If we ever switch to using the output
845743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * buffer in the reordering process, we'd need to next_glyph() here. */
846743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
847743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
848743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod#include "hb-ot-shape-complex-indic-machine.hh"
849743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
850743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
851743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodinitial_reordering (const hb_ot_map_t *map,
8523f18236a03880c0960f5990dc90685f6146951a6Behdad Esfahbod		    hb_face_t *face HB_UNUSED,
853743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_buffer_t *buffer,
854743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    void *user_data HB_UNUSED)
855743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
85670fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod  hb_mask_t basic_mask_array[ARRAY_LENGTH (indic_basic_features)] = {0};
857b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  unsigned int num_masks = ARRAY_LENGTH (indic_basic_features);
858b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  for (unsigned int i = 0; i < num_masks; i++)
85970fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod    basic_mask_array[i] = map->get_1_mask (indic_basic_features[i].tag);
860743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
86170fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod  find_syllables (map, buffer, basic_mask_array);
862b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod}
863b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
864743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
86546e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbodfinal_reordering_syllable (hb_buffer_t *buffer,
86646e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod			   hb_mask_t init_mask, hb_mask_t pref_mask,
867ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod			   unsigned int start, unsigned int end)
868743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
8694ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  hb_glyph_info_t *info = buffer->info;
8704ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
871e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod  /* 4. Final reordering:
872e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
873e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * After the localized forms and basic shaping forms GSUB features have been
874e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * applied (see below), the shaping engine performs some final glyph
875e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * reordering before applying all the remaining font features to the entire
876e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * cluster.
8774ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod   */
8784ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
8794ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  /* Find base again */
8805f0eaaad129ff04d56b8756bebf19fbc242718c9Behdad Esfahbod  unsigned int base;
8815f0eaaad129ff04d56b8756bebf19fbc242718c9Behdad Esfahbod  for (base = start; base < end; base++)
8825f0eaaad129ff04d56b8756bebf19fbc242718c9Behdad Esfahbod    if (info[base].indic_position() >= POS_BASE_C) {
8835f0eaaad129ff04d56b8756bebf19fbc242718c9Behdad Esfahbod      if (start < base && info[base].indic_position() > POS_BASE_C)
8845f0eaaad129ff04d56b8756bebf19fbc242718c9Behdad Esfahbod        base--;
8855f0eaaad129ff04d56b8756bebf19fbc242718c9Behdad Esfahbod      break;
8865f0eaaad129ff04d56b8756bebf19fbc242718c9Behdad Esfahbod    }
8874ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
8884705a7026900e51f6430f03a73c87f2df035df92Behdad Esfahbod
8894ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  /*   o Reorder matras:
890e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
891e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     If a pre-base matra character had been reordered before applying basic
892e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     features, the glyph can be moved closer to the main consonant based on
893e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     whether half-forms had been formed. Actual position for the matra is
894e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     defined as “after last standalone halant glyph, after initial matra
895e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     position and before the main consonant”. If ZWJ or ZWNJ follow this
896e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     halant, position is moved after it.
8974ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod   */
8984ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
89965c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod  if (start + 1 < end && start < base) /* Otherwise there can't be any pre-base matra characters. */
9009d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod  {
90165c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod    /* If we lost track of base, alas, position before last thingy. */
90265c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod    unsigned int new_pos = base == end ? base - 2 : base - 1;
90365c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod
90465c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod    /* Malayalam does not have "half" forms or explicit virama forms.
90565c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod     * The glyphs formed by 'half' are Chillus.  We want to position
90665c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod     * matra after them all.
90765c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod     */
90865c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod    if (buffer->props.script != HB_SCRIPT_MALAYALAM)
909e6b01a878cd2e63cb675e7e0c6ac4d83a8c10f37Behdad Esfahbod    {
91065c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod      while (new_pos > start &&
91165c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod	     !(is_one_of (info[new_pos], (FLAG (OT_M) | FLAG (OT_H) | FLAG (OT_Coeng)))))
91265c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod	new_pos--;
91365c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod
91465c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod      /* If we found no Halant we are done.
91565c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod       * Otherwise only proceed if the Halant does
91665c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod       * not belong to the Matra itself! */
91765c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod      if (is_halant_or_coeng (info[new_pos]) &&
91865c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod	  info[new_pos].indic_position() != POS_PRE_M)
91965c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod      {
92065c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod	/* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
92165c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod	if (new_pos + 1 < end && is_joiner (info[new_pos + 1]))
92265c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod	  new_pos++;
92365c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod      }
92465c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod      else
92565c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod        new_pos = start; /* No move. */
92665c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod    }
9279d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod
92865c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod    if (start < new_pos)
92965c43accdc4d2082282d5cedba8514b8df0c18a2Behdad Esfahbod    {
9309d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      /* Now go see if there's actually any matras... */
931921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod      for (unsigned int i = new_pos; i > start; i--)
9326a091df9b403b147ef78f3974610dedf4ce1e08aBehdad Esfahbod	if (info[i - 1].indic_position () == POS_PRE_M)
9339d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod	{
9341a1dbe9a2787f226f3e43063da8eb6633438b0a0Behdad Esfahbod	  unsigned int old_pos = i - 1;
9351a1dbe9a2787f226f3e43063da8eb6633438b0a0Behdad Esfahbod	  hb_glyph_info_t tmp = info[old_pos];
9361a1dbe9a2787f226f3e43063da8eb6633438b0a0Behdad Esfahbod	  memmove (&info[old_pos], &info[old_pos + 1], (new_pos - old_pos) * sizeof (info[0]));
9371a1dbe9a2787f226f3e43063da8eb6633438b0a0Behdad Esfahbod	  info[new_pos] = tmp;
938921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod	  new_pos--;
9399d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod	}
9402cc933aff97916e5d0fe42883f40f0879f848e25Behdad Esfahbod      buffer->merge_clusters (new_pos, MIN (end, base + 1));
941abb3239ef92cc5dccb4638806d7ae9868b9ac9b3Behdad Esfahbod    } else {
942e6b01a878cd2e63cb675e7e0c6ac4d83a8c10f37Behdad Esfahbod      for (unsigned int i = start; i < base; i++)
943abb3239ef92cc5dccb4638806d7ae9868b9ac9b3Behdad Esfahbod	if (info[i].indic_position () == POS_PRE_M) {
9442cc933aff97916e5d0fe42883f40f0879f848e25Behdad Esfahbod	  buffer->merge_clusters (i, MIN (end, base + 1));
945abb3239ef92cc5dccb4638806d7ae9868b9ac9b3Behdad Esfahbod	  break;
946abb3239ef92cc5dccb4638806d7ae9868b9ac9b3Behdad Esfahbod	}
9479d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    }
9484ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  }
9494ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
9504ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
9514ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  /*   o Reorder reph:
952e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
953e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     Reph’s original position is always at the beginning of the syllable,
954e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     (i.e. it is not reordered at the character reordering stage). However,
955e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     it will be reordered according to the basic-forms shaping results.
956e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     Possible positions for reph, depending on the script, are; after main,
957e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     before post-base consonant forms, and after post-base consonant forms.
958dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod   */
959dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
960dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  /* If there's anything after the Ra that has the REPH pos, it ought to be halant.
961dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod   * Which means that the font has failed to ligate the Reph.  In which case, we
962dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod   * shouldn't move. */
963dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  if (start + 1 < end &&
964dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod      info[start].indic_position() == POS_RA_TO_BECOME_REPH &&
965dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod      info[start + 1].indic_position() != POS_RA_TO_BECOME_REPH)
966dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  {
96702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      unsigned int new_reph_pos;
96802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
96902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     enum reph_position_t {
970f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_AFTER_MAIN,
971f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_BEFORE_SUBSCRIPT,
972f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_AFTER_SUBSCRIPT,
973f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_BEFORE_POSTSCRIPT,
9749fc7a11469113d31d8095757c4fc038c3427d44aBehdad Esfahbod       REPH_AFTER_POSTSCRIPT
975f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod     } reph_pos;
976f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
977f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod     /* XXX Figure out old behavior too */
9787f852b644b8143492a02edfc853114aaa23446bdBehdad Esfahbod     switch ((hb_tag_t) buffer->props.script)
979f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod     {
980f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_MALAYALAM:
981f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_ORIYA:
98234c215036f5fcdc7599b1ab0591b56dbb3811902Behdad Esfahbod       case HB_SCRIPT_SINHALA:
983f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_AFTER_MAIN;
984f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
985f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
986f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_GURMUKHI:
987f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_BEFORE_SUBSCRIPT;
988f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
989f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
990f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_BENGALI:
991f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_AFTER_SUBSCRIPT;
992f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
993f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
994f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       default:
995f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_DEVANAGARI:
996f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_GUJARATI:
997f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_BEFORE_POSTSCRIPT;
998f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
999f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
1000f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_KANNADA:
1001f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_TAMIL:
1002f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_TELUGU:
1003f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_AFTER_POSTSCRIPT;
1004f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
1005f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod     }
100602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
1007dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod    /*       1. If reph should be positioned after post-base consonant forms,
1008dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          proceed to step 5.
100902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
10109d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    if (reph_pos == REPH_AFTER_POSTSCRIPT)
101102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
10129d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      goto reph_step_5;
101302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
101402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
101502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       2. If the reph repositioning class is not after post-base: target
1016dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          position is after the first explicit halant glyph between the
1017dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first post-reph consonant and last main consonant. If ZWJ or ZWNJ
1018dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          are following this halant, position is moved after it. If such
1019dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          position is found, this is the target position. Otherwise,
1020dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          proceed to the next step.
1021dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *
1022dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          Note: in old-implementation fonts, where classifications were
1023dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          fixed in shaping engine, there was no case where reph position
1024dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          will be found on this step.
102502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
102602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
102702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      new_reph_pos = start + 1;
1028deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod      while (new_reph_pos < base && !is_halant_or_coeng (info[new_reph_pos]))
102902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	new_reph_pos++;
103002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
1031deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod      if (new_reph_pos < base && is_halant_or_coeng (info[new_reph_pos])) {
103202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	/* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */
103302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1]))
103402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	  new_reph_pos++;
103502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	goto reph_move;
103602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      }
103702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
103802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
103902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       3. If reph should be repositioned after the main consonant: find the
1040dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first consonant not ligated with main, or find the first
1041dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          consonant that is not a potential pre-base reordering Ra.
104202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
10439d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    if (reph_pos == REPH_AFTER_MAIN)
104402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
1045b504e060f008e95b1ba36c06600c9fea4f5d4808Behdad Esfahbod      new_reph_pos = base;
1046b504e060f008e95b1ba36c06600c9fea4f5d4808Behdad Esfahbod      /* XXX Skip potential pre-base reordering Ra. */
104734ae336f3fae93ef9372881d545c817bce383041Behdad Esfahbod      while (new_reph_pos + 1 < end && info[new_reph_pos + 1].indic_position() <= POS_AFTER_MAIN)
1048b504e060f008e95b1ba36c06600c9fea4f5d4808Behdad Esfahbod	new_reph_pos++;
1049b504e060f008e95b1ba36c06600c9fea4f5d4808Behdad Esfahbod      if (new_reph_pos < end)
1050b504e060f008e95b1ba36c06600c9fea4f5d4808Behdad Esfahbod        goto reph_move;
105102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
105202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
105302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       4. If reph should be positioned before post-base consonant, find
1054dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first post-base classified consonant not ligated with main. If no
1055dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          consonant is found, the target position should be before the
1056dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first matra, syllable modifier sign or vedic sign.
105702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
10589d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    /* This is our take on what step 4 is trying to say (and failing, BADLY). */
10599d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    if (reph_pos == REPH_AFTER_SUBSCRIPT)
106002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
10619d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      new_reph_pos = base;
10629d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      while (new_reph_pos < end &&
1063be8b9f5f715f6fb36b98bd33c3303f79cc068f8aBehdad Esfahbod	     !( FLAG (info[new_reph_pos + 1].indic_position()) & (FLAG (POS_POST_C) | FLAG (POS_AFTER_POST) | FLAG (POS_SMVD))))
10649d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod	new_reph_pos++;
10659d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      if (new_reph_pos < end)
10669d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod        goto reph_move;
106702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
106802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
106902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       5. If no consonant is found in steps 3 or 4, move reph to a position
1070dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          immediately before the first post-base matra, syllable modifier
1071dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          sign or vedic sign that has a reordering class after the intended
1072dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          reph position. For example, if the reordering position for reph
1073dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          is post-main, it will skip above-base matras that also have a
1074dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          post-main position.
1075dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     */
107602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_step_5:
107702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
1078d0e68dbd0b9fc9a42c4280d01c8ffd9c5015d550Behdad Esfahbod      /* Copied from step 2. */
1079d0e68dbd0b9fc9a42c4280d01c8ffd9c5015d550Behdad Esfahbod      new_reph_pos = start + 1;
1080d0e68dbd0b9fc9a42c4280d01c8ffd9c5015d550Behdad Esfahbod      while (new_reph_pos < base && !is_halant_or_coeng (info[new_reph_pos]))
1081d0e68dbd0b9fc9a42c4280d01c8ffd9c5015d550Behdad Esfahbod	new_reph_pos++;
1082d0e68dbd0b9fc9a42c4280d01c8ffd9c5015d550Behdad Esfahbod
1083d0e68dbd0b9fc9a42c4280d01c8ffd9c5015d550Behdad Esfahbod      if (new_reph_pos < base && is_halant_or_coeng (info[new_reph_pos])) {
1084d0e68dbd0b9fc9a42c4280d01c8ffd9c5015d550Behdad Esfahbod	/* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */
1085d0e68dbd0b9fc9a42c4280d01c8ffd9c5015d550Behdad Esfahbod	if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1]))
1086d0e68dbd0b9fc9a42c4280d01c8ffd9c5015d550Behdad Esfahbod	  new_reph_pos++;
1087d0e68dbd0b9fc9a42c4280d01c8ffd9c5015d550Behdad Esfahbod	goto reph_move;
1088d0e68dbd0b9fc9a42c4280d01c8ffd9c5015d550Behdad Esfahbod      }
108902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
1090dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
109102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       6. Otherwise, reorder reph to the end of the syllable.
109202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
109302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
109402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      new_reph_pos = end - 1;
109502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      while (new_reph_pos > start && info[new_reph_pos].indic_position() == POS_SMVD)
109602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	new_reph_pos--;
109702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
1098892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod      /*
1099892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * If the Reph is to be ending up after a Matra,Halant sequence,
1100892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * position it before that Halant so it can interact with the Matra.
1101892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * However, if it's a plain Consonant,Halant we shouldn't do that.
1102892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * Uniscribe doesn't do this.
1103892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * TEST: U+0930,U+094D,U+0915,U+094B,U+094D
1104892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       */
1105a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod      if (!indic_options ().uniscribe_bug_compatible &&
1106deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod	  unlikely (is_halant_or_coeng (info[new_reph_pos]))) {
110702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	for (unsigned int i = base + 1; i < new_reph_pos; i++)
110802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	  if (info[i].indic_category() == OT_M) {
110902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	    /* Ok, got it. */
111002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	    new_reph_pos--;
111102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	  }
111202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      }
111302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      goto reph_move;
11148df5636968389ac7bf8620ccd091fd4872b0bbeeBehdad Esfahbod    }
11158df5636968389ac7bf8620ccd091fd4872b0bbeeBehdad Esfahbod
111602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_move:
111702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
1118e6b01a878cd2e63cb675e7e0c6ac4d83a8c10f37Behdad Esfahbod      /* Yay, one big cluster! Merge before moving. */
1119e6b01a878cd2e63cb675e7e0c6ac4d83a8c10f37Behdad Esfahbod      buffer->merge_clusters (start, end);
1120e6b01a878cd2e63cb675e7e0c6ac4d83a8c10f37Behdad Esfahbod
112102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      /* Move */
112202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      hb_glyph_info_t reph = info[start];
112302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      memmove (&info[start], &info[start + 1], (new_reph_pos - start) * sizeof (info[0]));
112402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      info[new_reph_pos] = reph;
112502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
1126dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  }
1127dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
1128dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
1129dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  /*   o Reorder pre-base reordering consonants:
1130e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
1131e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     If a pre-base reordering consonant is found, reorder it according to
1132e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     the following rules:
1133e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   */
1134e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
113546e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod  if (pref_mask && base + 1 < end) /* Otherwise there can't be any pre-base reordering Ra. */
113646e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod  {
11378e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod    for (unsigned int i = base + 1; i < end; i++)
11388e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod      if ((info[i].mask & pref_mask) != 0)
113978818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod      {
11408e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	/*       1. Only reorder a glyph produced by substitution during application
11418e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	 *          of the <pref> feature. (Note that a font may shape a Ra consonant with
11428e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	 *          the feature generally but block it in certain contexts.)
11438e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	 */
11448e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	if (i + 1 == end || (info[i + 1].mask & pref_mask) == 0)
11458e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	{
11468e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	  /*
11478e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	   *       2. Try to find a target position the same way as for pre-base matra.
11488e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	   *          If it is found, reorder pre-base consonant glyph.
11498e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	   *
11508e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	   *       3. If position is not found, reorder immediately before main
11518e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	   *          consonant.
11528e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	   */
11538e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod
11548e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	  unsigned int new_pos = base;
11550afb84c12567ac35adac657bf8be29999b8c5a50Behdad Esfahbod	  while (new_pos > start &&
11560afb84c12567ac35adac657bf8be29999b8c5a50Behdad Esfahbod		 !(is_one_of (info[new_pos - 1], FLAG(OT_M) | HALANT_OR_COENG_FLAGS)))
11578e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	    new_pos--;
11588e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod
1159d90b8e841e0068a601c96ab184d18b0f48eec9d1Behdad Esfahbod	  /* In Khmer coeng model, a V,Ra can go *after* matras.  If it goes after a
1160d90b8e841e0068a601c96ab184d18b0f48eec9d1Behdad Esfahbod	   * split matra, it should be reordered to *before* the left part of such matra. */
1161d90b8e841e0068a601c96ab184d18b0f48eec9d1Behdad Esfahbod	  if (new_pos > start && info[new_pos - 1].indic_category() == OT_M)
1162d90b8e841e0068a601c96ab184d18b0f48eec9d1Behdad Esfahbod	  {
1163d90b8e841e0068a601c96ab184d18b0f48eec9d1Behdad Esfahbod	    unsigned int old_pos = i;
1164d90b8e841e0068a601c96ab184d18b0f48eec9d1Behdad Esfahbod	    for (unsigned int i = base + 1; i < old_pos; i++)
1165d90b8e841e0068a601c96ab184d18b0f48eec9d1Behdad Esfahbod	      if (info[i].indic_category() == OT_M)
1166d90b8e841e0068a601c96ab184d18b0f48eec9d1Behdad Esfahbod	      {
1167d90b8e841e0068a601c96ab184d18b0f48eec9d1Behdad Esfahbod		new_pos--;
1168d90b8e841e0068a601c96ab184d18b0f48eec9d1Behdad Esfahbod		break;
1169d90b8e841e0068a601c96ab184d18b0f48eec9d1Behdad Esfahbod	      }
1170d90b8e841e0068a601c96ab184d18b0f48eec9d1Behdad Esfahbod	  }
1171d90b8e841e0068a601c96ab184d18b0f48eec9d1Behdad Esfahbod
1172deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod	  if (new_pos > start && is_halant_or_coeng (info[new_pos - 1]))
11738e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	    /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
11748e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	    if (new_pos < end && is_joiner (info[new_pos]))
11758e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	      new_pos++;
11768e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod
11778e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	  {
11788e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	    unsigned int old_pos = i;
1179e6b01a878cd2e63cb675e7e0c6ac4d83a8c10f37Behdad Esfahbod	    buffer->merge_clusters (new_pos, old_pos + 1);
11808e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	    hb_glyph_info_t tmp = info[old_pos];
11818e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	    memmove (&info[new_pos + 1], &info[new_pos], (old_pos - new_pos) * sizeof (info[0]));
11828e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	    info[new_pos] = tmp;
11838e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	  }
11848e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	}
11858e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod
11868e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod        break;
118778818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod      }
118846e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod  }
1189eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
1190eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
1191a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod  /* Apply 'init' to the Left Matra if it's a word start. */
11926a091df9b403b147ef78f3974610dedf4ce1e08aBehdad Esfahbod  if (info[start].indic_position () == POS_PRE_M &&
1193a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod      (!start ||
1194eace47b173807d94b29a6490d0bc3c9f8f6168d1Behdad Esfahbod       !(FLAG (_hb_glyph_info_get_general_category (&info[start - 1])) &
11952c372b80f6befad69e216e3f218b38640b8cc044Behdad Esfahbod	 FLAG_RANGE (HB_UNICODE_GENERAL_CATEGORY_FORMAT, HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK))))
119646e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod    info[start].mask |= init_mask;
1197a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod
1198eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
11998ed248de77e5d2ed978e55c0ce1a11727bc9e34cBehdad Esfahbod  /*
12008ed248de77e5d2ed978e55c0ce1a11727bc9e34cBehdad Esfahbod   * Finish off the clusters and go home!
12018ed248de77e5d2ed978e55c0ce1a11727bc9e34cBehdad Esfahbod   */
1202decf6ffca475fe01ff3151b7641f629f031137d2Behdad Esfahbod  if (indic_options ().uniscribe_bug_compatible)
1203ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  {
120430c3d5e9fc61b49c2c6ad4e744300edd6f3e0261Behdad Esfahbod    /* Uniscribe merges the entire cluster.
120521d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod     * This means, half forms are submerged into the main consonants cluster.
120621d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod     * This is unnecessary, and makes cursor positioning harder, but that's what
120721d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod     * Uniscribe does. */
1208e6b01a878cd2e63cb675e7e0c6ac4d83a8c10f37Behdad Esfahbod    buffer->merge_clusters (start, end);
120921d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod  }
1210ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod}
1211e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
1212e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
1213ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodstatic void
1214ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodfinal_reordering (const hb_ot_map_t *map,
12153f18236a03880c0960f5990dc90685f6146951a6Behdad Esfahbod		  hb_face_t *face HB_UNUSED,
1216ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod		  hb_buffer_t *buffer,
1217ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod		  void *user_data HB_UNUSED)
1218ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod{
1219ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  unsigned int count = buffer->len;
1220ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  if (!count) return;
1221ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod
122246e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod  hb_mask_t init_mask = map->get_1_mask (HB_TAG('i','n','i','t'));
122346e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod  hb_mask_t pref_mask = map->get_1_mask (HB_TAG('p','r','e','f'));
1224eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
1225ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  hb_glyph_info_t *info = buffer->info;
1226ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  unsigned int last = 0;
1227cee7187447b76b22e1bb6136d137b35ac49c3a5dBehdad Esfahbod  unsigned int last_syllable = info[0].syllable();
1228ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  for (unsigned int i = 1; i < count; i++)
1229cee7187447b76b22e1bb6136d137b35ac49c3a5dBehdad Esfahbod    if (last_syllable != info[i].syllable()) {
123046e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod      final_reordering_syllable (buffer, init_mask, pref_mask, last, i);
1231ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod      last = i;
1232cee7187447b76b22e1bb6136d137b35ac49c3a5dBehdad Esfahbod      last_syllable = info[last].syllable();
1233ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod    }
123446e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod  final_reordering_syllable (buffer, init_mask, pref_mask, last, count);
1235e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
1236743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category);
1237743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position);
1238743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
1239743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
1240743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
1241693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbodconst hb_ot_complex_shaper_t _hb_ot_complex_shaper_indic =
1242693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod{
1243693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod  "indic",
1244693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod  collect_features_indic,
1245693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod  override_features_indic,
1246693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod  NULL, /* normalization_preference */
1247693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod  setup_masks_indic,
12481e7d860613032e40a3f90e2caa2ee5ac44ab8c8cBehdad Esfahbod  false, /* zero_width_attached_marks */
1249693918ef8541014a5ef7dfb91c6ea0ae36d9c368Behdad Esfahbod};
1250