hb-ot-shape-complex-indic.cc revision 3604d64ced909ade91998d294a7b4b2ee14d47aa
1b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod/*
227aba594c90b4444c35273a38f5fedc8e09d9a88Behdad Esfahbod * Copyright © 2011,2012  Google, Inc.
3b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod *
4b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod *  This is part of HarfBuzz, a text shaping library.
5b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod *
6b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * Permission is hereby granted, without written agreement and without
7b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * license or royalty fees, to use, copy, modify, and distribute this
8b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * software and its documentation for any purpose, provided that the
9b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * above copyright notice and the following two paragraphs appear in
10b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * all copies of this software.
11b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod *
12b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * DAMAGE.
17b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod *
18b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod *
24b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod * Google Author(s): Behdad Esfahbod
25b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod */
26b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
273ed4634ec349fa9e943ad23718c04be4dd4bba62Behdad Esfahbod#include "hb-ot-shape-complex-indic-private.hh"
28a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod#include "hb-ot-shape-private.hh"
29352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod
30a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod#define OLD_INDIC_TAG(script) (((hb_tag_t) script) | 0x20000000)
31a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod#define IS_OLD_INDIC_TAG(tag) ( \
32a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_BENGALI) || \
33a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_DEVANAGARI) || \
34a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_GUJARATI) || \
35a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_GURMUKHI) || \
36a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_KANNADA) || \
37a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_MALAYALAM) || \
38a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_ORIYA) || \
39a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_TAMIL) || \
40a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod				(tag) == OLD_INDIC_TAG (HB_SCRIPT_TELUGU) \
41a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod			      )
42a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbodstruct indic_options_t
43ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod{
44a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  int initialized : 1;
45a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  int uniscribe_bug_compatible : 1;
46a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod};
47a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod
48a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbodunion indic_options_union_t {
49a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  int i;
50a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  indic_options_t opts;
51a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod};
52a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad EsfahbodASSERT_STATIC (sizeof (int) == sizeof (indic_options_union_t));
53a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod
54a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbodstatic indic_options_union_t
55a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbodindic_options_init (void)
56a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod{
57a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  indic_options_union_t u;
58a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  u.i = 0;
59a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  u.opts.initialized = 1;
60a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod
61a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  char *c = getenv ("HB_OT_INDIC_OPTIONS");
62a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  u.opts.uniscribe_bug_compatible = c && strstr (c, "uniscribe-bug-compatible");
63a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod
64a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  return u;
65a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod}
66a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod
67a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbodinline indic_options_t
68a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbodindic_options (void)
69a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod{
70a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  static indic_options_union_t options;
71a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod
72a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  if (unlikely (!options.i)) {
73a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod    /* This is idempotent and threadsafe. */
74a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod    options = indic_options_init ();
75ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  }
76ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod
77a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  return options.opts;
78a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod}
79a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod
80ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod
81743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic int
82743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodcompare_codepoint (const void *pa, const void *pb)
83743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
84743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  hb_codepoint_t a = * (hb_codepoint_t *) pa;
85743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  hb_codepoint_t b = * (hb_codepoint_t *) pb;
86743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
87743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  return a < b ? -1 : a == b ? 0 : +1;
88743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
89743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
90f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbodstatic bool
91f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbodwould_substitute (hb_codepoint_t *glyphs, unsigned int glyphs_count,
92f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod		  hb_tag_t feature_tag, hb_ot_map_t *map, hb_face_t *face)
93743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
94f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  unsigned int lookup_indices[32];
95f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  unsigned int offset, len;
96f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod
97f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  offset = 0;
98f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  do {
99f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod    len = ARRAY_LENGTH (lookup_indices);
100f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod    hb_ot_layout_feature_get_lookup_indexes (face, HB_OT_TAG_GSUB,
101f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod					     map->get_feature_index (0/*GSUB*/, feature_tag),
102f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod					     offset,
103f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod					     &len,
104f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod					     lookup_indices);
105f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod
106f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod    for (unsigned int i = 0; i < len; i++)
107f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod      if (hb_ot_layout_would_substitute_lookup (face, glyphs, glyphs_count, lookup_indices[i]))
108f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod	return true;
109f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod
110f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod    offset += len;
111f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  } while (len == ARRAY_LENGTH (lookup_indices));
112f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod
113f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  return false;
114f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod}
115743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
116f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbodstatic indic_position_t
117f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbodconsonant_position (hb_codepoint_t u, hb_ot_map_t *map, hb_font_t *font)
118f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod{
1195249f3aee108b0f41770d137e63a625f594418e7Behdad Esfahbod  if ((u & ~0x007F) == 0x1780)
1205249f3aee108b0f41770d137e63a625f594418e7Behdad Esfahbod    return POS_BELOW_C; /* In Khmer coeng model, all are subjoining. */
1215249f3aee108b0f41770d137e63a625f594418e7Behdad Esfahbod
122f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  hb_codepoint_t virama = (u & ~0x007F) | 0x004D;
123f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  if ((u & ~0x007F) == 0x0D80) virama = 0x0DCA; /* Sinahla */
124f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  hb_codepoint_t glyphs[2];
125f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod
126f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  hb_font_get_glyph (font, virama, 0, &glyphs[0]);
127f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  hb_font_get_glyph (font, u,      0, &glyphs[1]);
128f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod
129f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  hb_face_t *face = hb_font_get_face (font);
130f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  if (would_substitute (glyphs, ARRAY_LENGTH (glyphs), HB_TAG('p','r','e','f'), map, face)) return POS_BELOW_C;
131f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  if (would_substitute (glyphs, ARRAY_LENGTH (glyphs), HB_TAG('b','l','w','f'), map, face)) return POS_BELOW_C;
132f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  if (would_substitute (glyphs, ARRAY_LENGTH (glyphs), HB_TAG('p','s','t','f'), map, face)) return POS_POST_C;
133f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod  return POS_BASE_C;
134743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
135743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
1368bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod#define MATRA_POS_LEFT(u)	POS_PRE_M
1378c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod#define MATRA_POS_RIGHT(u)	( \
1388c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_DEVA(u) ? POS_AFTER_SUB   : \
1398c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_BENG(u) ? POS_AFTER_POST  : \
1408c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_GURM(u) ? POS_AFTER_POST  : \
1418c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_GUJA(u) ? POS_AFTER_POST  : \
1428c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_ORYA(u) ? POS_AFTER_POST  : \
1438c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_TAML(u) ? POS_AFTER_POST  : \
144fa247ebe524f92fa95d344ba912f704262879c13Behdad Esfahbod				  IS_TELU(u) ? (u <= 0x0C42 ? POS_BEFORE_SUB : POS_AFTER_SUB) : \
145fa247ebe524f92fa95d344ba912f704262879c13Behdad Esfahbod				  IS_KNDA(u) ? (u != 0x0CD5 ? POS_BEFORE_SUB : POS_AFTER_SUB) : \
1468c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_MLYM(u) ? POS_AFTER_POST  : \
1478c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_SINH(u) ? POS_AFTER_SUB   : \
1488c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  /*default*/  POS_AFTER_SUB     \
1498c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				)
1508c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod#define MATRA_POS_TOP(u)	( /* BENG and MLYM don't have top matras. */ \
1518c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_DEVA(u) ? POS_AFTER_SUB  : \
1528c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_GURM(u) ? POS_AFTER_SUB  : \
1538c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_GUJA(u) ? POS_AFTER_SUB  : \
1548c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_ORYA(u) ? POS_AFTER_MAIN : \
1558c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_TAML(u) ? POS_AFTER_SUB  : \
1568c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_TELU(u) ? POS_BEFORE_SUB : \
1578c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_KNDA(u) ? POS_BEFORE_SUB : \
1588c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_SINH(u) ? POS_AFTER_SUB  : \
1598c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  /*default*/  POS_AFTER_SUB    \
1608c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				)
1618c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod#define MATRA_POS_BOTTOM(u)	( \
1628c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_DEVA(u) ? POS_AFTER_SUB  : \
1638c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_BENG(u) ? POS_AFTER_SUB  : \
1648c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_GURM(u) ? POS_AFTER_POST : \
1658c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_GUJA(u) ? POS_AFTER_POST : \
1668c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_ORYA(u) ? POS_AFTER_SUB  : \
1678c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_TAML(u) ? POS_AFTER_POST : \
1688c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_TELU(u) ? POS_BEFORE_SUB : \
1698c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_KNDA(u) ? POS_BEFORE_SUB : \
1708c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_MLYM(u) ? POS_AFTER_POST : \
1718c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  IS_SINH(u) ? POS_AFTER_SUB  : \
1728c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				  /*default*/  POS_AFTER_SUB    \
1738c973ebf0f59abb5ee920edd5d64e23d8e47ad75Behdad Esfahbod				)
1748bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod
1758bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod
1768bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbodstatic indic_position_t
1778bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbodmatra_position (hb_codepoint_t u, indic_position_t side)
1788bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod{
1798bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  switch ((int) side)
1808bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  {
1818bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod    case POS_PRE_C:	return MATRA_POS_LEFT (u);
1828bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod    case POS_POST_C:	return MATRA_POS_RIGHT (u);
1838bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod    case POS_ABOVE_C:	return MATRA_POS_TOP (u);
1848bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod    case POS_BELOW_C:	return MATRA_POS_BOTTOM (u);
1858bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  };
1868bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  abort ();
1878bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod}
1888bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod
189352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbodstatic bool
190352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbodis_ra (hb_codepoint_t u)
191352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod{
192352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod  return !!bsearch (&u, ra_chars,
193352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod		    ARRAY_LENGTH (ra_chars),
194352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod		    sizeof (ra_chars[0]),
195352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod		    compare_codepoint);
196352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod}
197352372ae5ea0998e40cf9fe43c22b6b610a5764eBehdad Esfahbod
1989ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod#define JOINER_FLAGS (FLAG (OT_ZWJ) | FLAG (OT_ZWNJ))
1999ee27a928a989c71923cef82a9e9828f8e9ca051Behdad Esfahbodstatic bool
2009da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbodis_joiner (const hb_glyph_info_t &info)
2019ee27a928a989c71923cef82a9e9828f8e9ca051Behdad Esfahbod{
2029ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod  return !!(FLAG (info.indic_category()) & JOINER_FLAGS);
2039da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod}
2049da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
2059ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod#define CONSONANT_FLAGS (FLAG (OT_C) | FLAG (OT_Ra) | FLAG (OT_V) | FLAG (OT_NBSP) | FLAG (OT_DOTTEDCIRCLE))
2069da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbodstatic bool
2079da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbodis_consonant (const hb_glyph_info_t &info)
2089da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod{
2091a1fa8c655a082fc1439608457ba717306cc83caBehdad Esfahbod  /* Note:
2101a1fa8c655a082fc1439608457ba717306cc83caBehdad Esfahbod   *
21118c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod   * We treat Vowels and placeholders as if they were consonants.  This is safe because Vowels
212c5306b6861cfaa50af40e8ceb058791fa06d7981Behdad Esfahbod   * cannot happen in a consonant syllable.  The plus side however is, we can call the
213c5306b6861cfaa50af40e8ceb058791fa06d7981Behdad Esfahbod   * consonant syllable logic from the vowel syllable function and get it all right! */
2149ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod  return !!(FLAG (info.indic_category()) & CONSONANT_FLAGS);
2159ee27a928a989c71923cef82a9e9828f8e9ca051Behdad Esfahbod}
216c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod
2179ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod#define HALANT_OR_COENG_FLAGS (FLAG (OT_H) | FLAG (OT_Coeng))
218deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbodstatic bool
219deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbodis_halant_or_coeng (const hb_glyph_info_t &info)
220deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod{
2219ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod  return !!(FLAG (info.indic_category()) & HALANT_OR_COENG_FLAGS);
222deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod}
223deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod
2249ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbodstatic inline void
225f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbodset_indic_properties (hb_glyph_info_t &info, hb_ot_map_t *map, hb_font_t *font)
2269ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod{
2279ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod  hb_codepoint_t u = info.codepoint;
2289ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod  unsigned int type = get_indic_categories (u);
2298bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod
2308bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod
2318bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  /*
2328bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod   * Assign category
2338bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod   */
2348bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod
2358bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  indic_category_t cat = (indic_category_t) (type & 0x0F);
2369ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
2379ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod  /* The spec says U+0952 is OT_A.  However, testing shows that Uniscribe
2389ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod   * treats U+0951..U+0952 all as OT_VD.
2399ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod   * TESTS:
2409ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod   * U+092E,U+0947,U+0952
2419ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod   * U+092E,U+0952,U+0947
2429ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod   * U+092E,U+0947,U+0951
2439ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod   * U+092E,U+0951,U+0947
2449ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod   * */
2459ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod  if (unlikely (hb_in_range<hb_codepoint_t> (u, 0x0951, 0x0954)))
2469ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod    cat = OT_VD;
2479ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
2489ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod  if (cat == OT_X &&
2499ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod      unlikely (hb_in_range<hb_codepoint_t> (u, 0x17CB, 0x17D2))) /* Khmer Various signs */
2509ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod    cat = OT_N;
2519ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
2528bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  if (unlikely (u == 0x17D2)) cat = OT_Coeng; /* Khmer coeng */
2538bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  else if (unlikely (u == 0x200C)) cat = OT_ZWNJ;
2548bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  else if (unlikely (u == 0x200D)) cat = OT_ZWJ;
2558bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  else if (unlikely (u == 0x25CC)) cat = OT_DOTTEDCIRCLE;
2563604d64ced909ade91998d294a7b4b2ee14d47aaBehdad Esfahbod  else if (unlikely (u == 0x0A71)) cat = OT_SM; /* GURMUKHI ADDAK.  More like consonant medial. like 0A75. */
2579ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
2589ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod  if (cat == OT_Repha) {
2599ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod    /* There are two kinds of characters marked as Repha:
2609ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod     * - The ones that are GenCat=Mn are already positioned visually, ie. after base. (eg. Khmer)
2619ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod     * - The ones that are GenCat=Lo is encoded logically, ie. beginning of syllable. (eg. Malayalam)
2629ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod     *
2639ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod     * We recategorize the first kind to look like a Nukta and attached to the base directly.
2649ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod     */
2659ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod    if (_hb_glyph_info_get_general_category (&info) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
2669ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod      cat = OT_N;
2679ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod  }
2689ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
2699ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
2708bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod
2718bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod
2728bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  /*
2738bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod   * Assign position.
2748bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod   */
2758bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod
2768bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  indic_position_t pos = (indic_position_t) (type >> 4);
2778bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod
2788bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  if ((FLAG (cat) & CONSONANT_FLAGS))
2798bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  {
280f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod    pos = consonant_position (u, map, font);
2819ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod    if (is_ra (u))
2829ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod      cat = OT_Ra;
2838bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  }
2848bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  else if (cat == OT_M)
2858bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  {
2868bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod    pos = matra_position (u, pos);
2878bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  }
2888bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  else if (cat == OT_SM || cat == OT_VD)
2898bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  {
2909ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod    pos = POS_SMVD;
2918bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod  }
2928bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod
2938bb32458f95f13f66688e0811cc91f1bfffb867dBehdad Esfahbod
2949ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
2959ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod  info.indic_category() = cat;
2969ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod  info.indic_position() = pos;
2979ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod}
2989ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
2999ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
3009ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
3019ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
3029ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
3039ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
3049ccc6382ba43760167c134c18c1c4ada4b8c3f22Behdad Esfahbod
305eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodstruct feature_list_t {
306c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  hb_tag_t tag;
307c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  hb_bool_t is_global;
308eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod};
309eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
310d5c4edcdd6df32f2f23aca44f14838b4baab4d7aBehdad Esfahbod/* These features are applied one at a time, given the order in this table. */
311eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodstatic const feature_list_t
312eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodindic_basic_features[] =
313b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
314c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('n','u','k','t'), true},
315e0475345d5d7db8dbc8b554beedfa2435c5d7fd1Behdad Esfahbod  {HB_TAG('a','k','h','n'), true},
316c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('r','p','h','f'), false},
3171ac075b227090a9ad930dcc1670236c176b27067Behdad Esfahbod  {HB_TAG('r','k','r','f'), true},
318c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('p','r','e','f'), false},
319c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('b','l','w','f'), false},
320c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('h','a','l','f'), false},
32129f106d7fba25e1464debd3a4831a7380d75c4c9Behdad Esfahbod  {HB_TAG('a','b','v','f'), false},
322c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  {HB_TAG('p','s','t','f'), false},
3230201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod  {HB_TAG('c','f','a','r'), false},
3249da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  {HB_TAG('c','j','c','t'), false},
3251d6846db9ebf84561bb30a4e48c6c43184914099Behdad Esfahbod  {HB_TAG('v','a','t','u'), true},
326c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod};
327c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod
328c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod/* Same order as the indic_basic_features array */
329c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbodenum {
330c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  _NUKT,
331e0475345d5d7db8dbc8b554beedfa2435c5d7fd1Behdad Esfahbod  _AKHN,
332c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  RPHF,
333df6d45c693c417bf311e6fa49f18a8558542e525Behdad Esfahbod  _RKRF,
334c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  PREF,
335c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  BLWF,
336c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  HALF,
33729f106d7fba25e1464debd3a4831a7380d75c4c9Behdad Esfahbod  ABVF,
338c7fe56a1d5d3e969b6ec51cd9ecd471706a19568Behdad Esfahbod  PSTF,
3390201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod  CFAR,
3401d6846db9ebf84561bb30a4e48c6c43184914099Behdad Esfahbod  CJCT,
3411d6846db9ebf84561bb30a4e48c6c43184914099Behdad Esfahbod  VATU
342b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod};
343b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
344d5c4edcdd6df32f2f23aca44f14838b4baab4d7aBehdad Esfahbod/* These features are applied all at once. */
345eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodstatic const feature_list_t
346eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbodindic_other_features[] =
347b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
348eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('i','n','i','t'), false},
349eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('p','r','e','s'), true},
350eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('a','b','v','s'), true},
351eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('b','l','w','s'), true},
352eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('p','s','t','s'), true},
353eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('h','a','l','n'), true},
354eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
355eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('d','i','s','t'), true},
356eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('a','b','v','m'), true},
357eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod  {HB_TAG('b','l','w','m'), true},
358eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod};
359eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
360743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
361743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
362743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodinitial_reordering (const hb_ot_map_t *map,
363743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_face_t *face,
364743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_buffer_t *buffer,
365743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    void *user_data HB_UNUSED);
366f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbodstatic void
367f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbodfinal_reordering (const hb_ot_map_t *map,
368f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod		  hb_face_t *face,
369f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod		  hb_buffer_t *buffer,
370743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		  void *user_data HB_UNUSED);
371b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
372b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbodvoid
3739f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod_hb_ot_shape_complex_collect_features_indic (hb_ot_map_builder_t *map,
3749f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod					     const hb_segment_properties_t *props HB_UNUSED)
375b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
376f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_bool_feature (HB_TAG('l','o','c','l'));
377a54a5505a35eef5315a8e2e7a79502901e3eff5fBehdad Esfahbod  /* The Indic specs do not require ccmp, but we apply it here since if
378a54a5505a35eef5315a8e2e7a79502901e3eff5fBehdad Esfahbod   * there is a use of it, it's typically at the beginning. */
379f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_bool_feature (HB_TAG('c','c','m','p'));
380f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
381743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  map->add_gsub_pause (initial_reordering, NULL);
382f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
383412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (indic_basic_features); i++) {
38476f76812ac7cca8ac6935952a2360d5e151480faBehdad Esfahbod    map->add_bool_feature (indic_basic_features[i].tag, indic_basic_features[i].is_global);
385412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod    map->add_gsub_pause (NULL, NULL);
386412b91889d9a1ae477e8b6907d0b9a76e78a6c91Behdad Esfahbod  }
387b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
388f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod  map->add_gsub_pause (final_reordering, NULL);
389f6fd3780e12b23ff7ed3743497c8996e71dcb064Behdad Esfahbod
390d5c4edcdd6df32f2f23aca44f14838b4baab4d7aBehdad Esfahbod  for (unsigned int i = 0; i < ARRAY_LENGTH (indic_other_features); i++)
391eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod    map->add_bool_feature (indic_other_features[i].tag, indic_other_features[i].is_global);
392b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod}
393b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
394d96838ef951ce6170eb2dc576ebcba2262cf7008Behdad Esfahbodvoid
395d96838ef951ce6170eb2dc576ebcba2262cf7008Behdad Esfahbod_hb_ot_shape_complex_override_features_indic (hb_ot_map_builder_t *map,
396d96838ef951ce6170eb2dc576ebcba2262cf7008Behdad Esfahbod					      const hb_segment_properties_t *props HB_UNUSED)
397d96838ef951ce6170eb2dc576ebcba2262cf7008Behdad Esfahbod{
398af92b4cc90e4184d5bdd8037c551ed482700114fBehdad Esfahbod  /* Uniscribe does not apply 'kern'. */
399af92b4cc90e4184d5bdd8037c551ed482700114fBehdad Esfahbod  if (indic_options ().uniscribe_bug_compatible)
400af92b4cc90e4184d5bdd8037c551ed482700114fBehdad Esfahbod    map->add_feature (HB_TAG('k','e','r','n'), 0, true);
401d96838ef951ce6170eb2dc576ebcba2262cf7008Behdad Esfahbod}
402d96838ef951ce6170eb2dc576ebcba2262cf7008Behdad Esfahbod
403867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
40411138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbodhb_ot_shape_normalization_mode_t
40511138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbod_hb_ot_shape_complex_normalization_preference_indic (void)
40602cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod{
40702cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod  /* We want split matras decomposed by the common shaping logic. */
40811138ccff71f442da1fcf64faa0e1d22e083e775Behdad Esfahbod  return HB_OT_SHAPE_NORMALIZATION_MODE_DECOMPOSED;
40902cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod}
41002cdf743c2ec345a44d4fcf865594b6ac13fccd0Behdad Esfahbod
411867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
412b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbodvoid
413f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod_hb_ot_shape_complex_setup_masks_indic (hb_ot_map_t *map,
4149f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod					hb_buffer_t *buffer,
415f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod					hb_font_t *font)
416b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod{
4179f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod  HB_BUFFER_ALLOCATE_VAR (buffer, indic_category);
4189f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod  HB_BUFFER_ALLOCATE_VAR (buffer, indic_position);
4199f9bcceca6321d5a5812f878de1de39901349a78Behdad Esfahbod
420743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* We cannot setup masks here.  We save information about characters
421743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * and setup masks later on in a pause-callback. */
422743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
423743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  unsigned int count = buffer->len;
424b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  for (unsigned int i = 0; i < count; i++)
425f055442716ec7543ed156d4789955b19c11a5255Behdad Esfahbod    set_indic_properties (buffer->info[i], map, font);
426743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
427b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
42845d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodstatic int
42945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbodcompare_indic_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
43045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod{
43145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  int a = pa->indic_position();
43245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  int b = pb->indic_position();
43345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
43445d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  return a < b ? -1 : a == b ? 0 : +1;
43545d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod}
436867361c3ad39629a8d5b7dc48d558a1c19e37d43Behdad Esfahbod
4377ea58db311bfb0d8f804d1e9f4a1f004bd45075aBehdad Esfahbod/* Rules from:
4387ea58db311bfb0d8f804d1e9f4a1f004bd45075aBehdad Esfahbod * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */
4397ea58db311bfb0d8f804d1e9f4a1f004bd45075aBehdad Esfahbod
440743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
44170fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbodinitial_reordering_consonant_syllable (const hb_ot_map_t *map, hb_buffer_t *buffer, hb_mask_t *basic_mask_array,
442ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod				       unsigned int start, unsigned int end)
443743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
444ee58f3bc75d2d071a71b94063bf12205a5871acbBehdad Esfahbod  hb_glyph_info_t *info = buffer->info;
445743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
446617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod
447743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 1. Find base consonant:
448743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
449743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * The shaping engine finds the base consonant of the syllable, using the
450743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * following algorithm: starting from the end of the syllable, move backwards
451743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * until a consonant is found that does not have a below-base or post-base
452743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * form (post-base forms have to follow below-base forms), or that is not a
453743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * pre-base reordering Ra, or arrive at the first consonant. The consonant
454743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * stopped at will be the base.
455743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
456743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o If the syllable starts with Ra + Halant (in a script that has Reph)
457743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *     and has more than one consonant, Ra is excluded from candidates for
458743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *     base consonants.
459743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
460743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
4615e72071062c015237b79fbd0521341a63166a204Behdad Esfahbod  unsigned int base = end;
46276b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  bool has_reph = false;
463743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
46476b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod  {
465617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
466617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod     *    and has more than one consonant, Ra is excluded from candidates for
467617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod     *    base consonants. */
468617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    unsigned int limit = start;
46970fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod    if (basic_mask_array[RPHF] &&
470617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod	start + 3 <= end &&
471617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod	info[start].indic_category() == OT_Ra &&
472617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod	info[start + 1].indic_category() == OT_H &&
4733285e107c9a83aeb552e67f9460680ff6d167d88Behdad Esfahbod	(unlikely (buffer->props.script == HB_SCRIPT_SINHALA) ?
4743285e107c9a83aeb552e67f9460680ff6d167d88Behdad Esfahbod	 info[start + 2].indic_category() == OT_ZWJ /* In Sinhala, form Reph only if ZWJ is present */:
4753285e107c9a83aeb552e67f9460680ff6d167d88Behdad Esfahbod	 !is_joiner (info[start + 2] /* In other scripts, any joiner blocks Reph formation */ )
4763285e107c9a83aeb552e67f9460680ff6d167d88Behdad Esfahbod	))
477617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    {
478617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod      limit += 2;
4793285e107c9a83aeb552e67f9460680ff6d167d88Behdad Esfahbod      while (limit < end && is_joiner (info[limit]))
4803285e107c9a83aeb552e67f9460680ff6d167d88Behdad Esfahbod        limit++;
481617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod      base = start;
482617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod      has_reph = true;
483617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    };
48476b3409de6887c1cdd5c679939497b1b56f4554bBehdad Esfahbod
48514dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod     enum base_position_t {
48614dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod       BASE_FIRST,
48714dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod       BASE_LAST
48814dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod     } base_pos;
48914dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod
49014dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod    switch ((hb_tag_t) buffer->props.script)
49114dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod    {
49214dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod      case HB_SCRIPT_KHMER:
49314dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod	base_pos = BASE_FIRST;
49414dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod	break;
49514dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod
49614dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod      default:
49714dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod	base_pos = BASE_LAST;
49814dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod	break;
49914dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod    }
50014dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod
50114dbdd9e39d3a869fd1521000c889c347433d22bBehdad Esfahbod    if (base_pos == BASE_LAST)
5025d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod    {
5035d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod      /* -> starting from the end of the syllable, move backwards */
5045d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod      unsigned int i = end;
5055d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod      do {
5065d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	i--;
5075d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	/* -> until a consonant is found */
5085d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	if (is_consonant (info[i]))
5093c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	{
5105d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	  /* -> that does not have a below-base or post-base form
5115d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	   * (post-base forms have to follow below-base forms), */
5125d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	  if (info[i].indic_position() != POS_BELOW_C &&
5135d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	      info[i].indic_position() != POS_POST_C)
5145d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	  {
5155d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	    base = i;
5165d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	    break;
5175d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	  }
5185d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod
5195d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	  /* -> or that is not a pre-base reordering Ra,
5205d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	   *
52125e302da9a712e6f1d63b0d243a8df0d326ddba3Behdad Esfahbod	   * IMPLEMENTATION NOTES:
52225e302da9a712e6f1d63b0d243a8df0d326ddba3Behdad Esfahbod	   *
52325e302da9a712e6f1d63b0d243a8df0d326ddba3Behdad Esfahbod	   * Our pre-base reordering Ra's are marked POS_BELOW, so will be skipped
52425e302da9a712e6f1d63b0d243a8df0d326ddba3Behdad Esfahbod	   * by the logic above already.
5255d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	   */
5265d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod
5275d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	  /* -> or arrive at the first consonant. The consonant stopped at will
5285d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	   * be the base. */
5293c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	  base = i;
5303c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod	}
5315d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	else
5325d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	  if (is_joiner (info[i]))
5335d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	    break;
5345d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod      } while (i > limit);
5355d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod    }
5365d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod    else
5375d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod    {
5385d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod      /* In scripts without half forms (eg. Khmer), the first consonant is always the base. */
5395d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod
5405d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod      if (!has_reph)
5415d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod	base = limit;
5425d32690a3428fa86eb26fe5fcec943a10aa95881Behdad Esfahbod    }
543743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
5443c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    if (base < start)
5453c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod      base = start; /* Just in case... */
546743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
547617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod
548617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
549617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod     *    and has more than one consonant, Ra is excluded from candidates for
550617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod     *    base consonants. */
551617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    if (has_reph && base == start) {
552617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod      /* Have no other consonant, so Reph is not formed and Ra becomes base. */
553617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod      has_reph = false;
554617f4ac46f1084859d2034c08760e31e52d3bec3Behdad Esfahbod    }
5555e4e21fce4b548b0b8a5951bc8f35a9f27428192Behdad Esfahbod  }
5563d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod
5573d25079f8d6be81b9b4b91d3a97016b8a572f571Behdad Esfahbod
558743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 2. Decompose and reorder Matras:
559743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
560743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * Each matra and any syllable modifier sign in the cluster are moved to the
561743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * appropriate position relative to the consonant(s) in the cluster. The
562743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * shaping engine decomposes two- or three-part matras into their constituent
563743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * parts before any repositioning. Matra characters are classified by which
564743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * consonant in a conjunct they have affinity for and are reordered to the
565743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * following positions:
566743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
567743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o Before first half form in the syllable
568743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After subjoined consonants
569743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After post-form consonant
570743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *   o After main consonant (for above marks)
571743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
572743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * IMPLEMENTATION NOTES:
573743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
574743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * The normalize() routine has already decomposed matras for us, so we don't
575743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * need to worry about that.
576743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
577743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
578743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
579743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* 3.  Reorder marks to canonical order:
580743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
581743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * Adjacent nukta and halant or nukta and vedic sign are always repositioned
582743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * if necessary, so that the nukta is first.
583743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
584743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * IMPLEMENTATION NOTES:
585743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   *
586743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * We don't need to do this: the normalize() routine already did this for us.
587743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   */
588743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
589743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
59045d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* Reorder characters */
59145d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
5923c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod  for (unsigned int i = start; i < base; i++)
593e8cd81f76d159f3ecf808952dab24bc07782497aBehdad Esfahbod    info[i].indic_position() = MIN ((unsigned int) POS_PRE_C, info[i].indic_position());
59455f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod
595075d671f1093d2e3c58f7f45568696030f1b3efdBehdad Esfahbod  if (base < end)
596075d671f1093d2e3c58f7f45568696030f1b3efdBehdad Esfahbod    info[base].indic_position() = POS_BASE_C;
59745d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
59855f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod  /* Mark final consonants.  A final consonant is one appearing after a matra,
59955f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod   * like in Khmer. */
60055f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod  for (unsigned int i = base + 1; i < end; i++)
60155f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod    if (info[i].indic_category() == OT_M) {
60255f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod      for (unsigned int j = i + 1; j < end; j++)
60355f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod        if (is_consonant (info[j])) {
60455f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod	  info[j].indic_position() = POS_FINAL_C;
60555f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod	  break;
60655f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod	}
60755f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod      break;
60855f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod    }
60955f70ebfb95083f515d9b0044a2a65ab11484bb5Behdad Esfahbod
610fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod  /* Handle beginning Ra */
6115e4e21fce4b548b0b8a5951bc8f35a9f27428192Behdad Esfahbod  if (has_reph)
612dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod    info[start].indic_position() = POS_RA_TO_BECOME_REPH;
613fd06bf56110e73826b3d5c73ac964e2609450d46Behdad Esfahbod
614f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  /* For old-style Indic script tags, move the first post-base Halant after
615f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod   * last consonant. */
616a3e04bee2c2fa648759a87e460db6b4f1b685586Behdad Esfahbod  if (IS_OLD_INDIC_TAG (map->get_chosen_script (0))) {
6173c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = base + 1; i < end; i++)
618f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod      if (info[i].indic_category() == OT_H) {
619f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        unsigned int j;
620f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        for (j = end - 1; j > i; j--)
621190eb31a16178269aecaf5d2ecc9012f956749f4Behdad Esfahbod	  if (is_consonant (info[j]))
622f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	    break;
623f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	if (j > i) {
624f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  /* Move Halant to after last consonant. */
625f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  hb_glyph_info_t t = info[i];
626f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  memmove (&info[i], &info[i + 1], (j - i) * sizeof (info[0]));
627f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	  info[j] = t;
628f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod	}
629f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod        break;
630f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod      }
631f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod  }
632f5bc2725cb892264ba223e0a49f7fd2c622a0730Behdad Esfahbod
63345d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* Attach ZWJ, ZWNJ, nukta, and halant to previous char to move with them. */
634a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  if (!indic_options ().uniscribe_bug_compatible)
635ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  {
636ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    /* Please update the Uniscribe branch when touching this! */
637ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    for (unsigned int i = start + 1; i < end; i++)
63825bc489498ef7d0beb8fe9ab663e3f0b2f52c9c2Behdad Esfahbod      if ((FLAG (info[i].indic_category()) & (FLAG (OT_ZWNJ) | FLAG (OT_ZWJ) | FLAG (OT_N) | FLAG (OT_RS) | FLAG (OT_H))))
639ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	info[i].indic_position() = info[i - 1].indic_position();
640ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  } else {
64167ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod    /*
64267ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod     * Uniscribe doesn't move the Halant with Left Matra.
64367ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod     * TEST: U+092B,U+093F,U+094DE
64467ea29af49bb08ee679914076808327992cf6676Behdad Esfahbod     */
645ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    /* Please update the non-Uniscribe branch when touching this! */
646ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod    for (unsigned int i = start + 1; i < end; i++)
64725bc489498ef7d0beb8fe9ab663e3f0b2f52c9c2Behdad Esfahbod      if ((FLAG (info[i].indic_category()) & (FLAG (OT_ZWNJ) | FLAG (OT_ZWJ) | FLAG (OT_N) | FLAG (OT_RS) | FLAG (OT_H)))) {
648ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	info[i].indic_position() = info[i - 1].indic_position();
6496a091df9b403b147ef78f3974610dedf4ce1e08aBehdad Esfahbod	if (info[i].indic_category() == OT_H && info[i].indic_position() == POS_PRE_M)
650ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	  for (unsigned int j = i; j > start; j--)
6516a091df9b403b147ef78f3974610dedf4ce1e08aBehdad Esfahbod	    if (info[j - 1].indic_position() != POS_PRE_M) {
652ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	      info[i].indic_position() = info[j - 1].indic_position();
653ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	      break;
654ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod	    }
655ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod      }
656ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  }
65774ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod  /* Re-attach ZWJ, ZWNJ, and halant to next char, for after-base consonants. */
65874ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod  {
65974ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod    unsigned int last_halant = end;
66074ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod    for (unsigned int i = base + 1; i < end; i++)
661deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod      if (is_halant_or_coeng (info[i]))
66274ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod        last_halant = i;
66374ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod      else if (is_consonant (info[i])) {
66474ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod	for (unsigned int j = last_halant; j < i; j++)
66574ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod	  info[j].indic_position() = info[i].indic_position();
66674ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod      }
66774ccc6a1322f8c48c5f2a05f04821783c4b87a14Behdad Esfahbod  }
66845d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
66945d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod  /* We do bubble-sort, skip malicious clusters attempts */
670b99d63ae114fb58f129562b293a8a66543d499adBehdad Esfahbod  if (end - start < 64)
671a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod  {
672a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    /* Sit tight, rock 'n roll! */
673d3637edb248162970e202e9d0671540274192844Behdad Esfahbod    hb_bubble_sort (info + start, end - start, compare_indic_order);
674a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    /* Find base again */
675a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod    base = end;
6763c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = start; i < end; i++)
677a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod      if (info[i].indic_position() == POS_BASE_C) {
678a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod        base = i;
679a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod	break;
680a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod      }
681a391ff50b9a7b6ac3e58d199ea726b20ee6839bbBehdad Esfahbod  }
68245d6f29f15f1d2323bcaa2498aed23ff0c8a1567Behdad Esfahbod
683743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Setup masks now */
684743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
685281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod  {
686281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    hb_mask_t mask;
687281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod
688dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod    /* Reph */
689668c6046c1b3af3bd316bda0cc8636f2a5e8df42Behdad Esfahbod    for (unsigned int i = start; i < end && info[i].indic_position() == POS_RA_TO_BECOME_REPH; i++)
69070fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod      info[i].mask |= basic_mask_array[RPHF];
691dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
692281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Pre-base */
693e0475345d5d7db8dbc8b554beedfa2435c5d7fd1Behdad Esfahbod    mask = basic_mask_array[HALF] | basic_mask_array[CJCT];
6943c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = start; i < base; i++)
695281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod      info[i].mask  |= mask;
696281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Base */
697e0475345d5d7db8dbc8b554beedfa2435c5d7fd1Behdad Esfahbod    mask = basic_mask_array[CJCT];
698075d671f1093d2e3c58f7f45568696030f1b3efdBehdad Esfahbod    if (base < end)
699075d671f1093d2e3c58f7f45568696030f1b3efdBehdad Esfahbod      info[base].mask |= mask;
700281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod    /* Post-base */
70170fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod    mask = basic_mask_array[BLWF] | basic_mask_array[ABVF] | basic_mask_array[PSTF] | basic_mask_array[CJCT];
7023c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod    for (unsigned int i = base + 1; i < end; i++)
703281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod      info[i].mask  |= mask;
704281683995a46ed37aeeb84061249758c59822457Behdad Esfahbod  }
7059da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
70617d7de91d76406d3e92db37d9eef2fc615f06e68Behdad Esfahbod  /* XXX This will not match for old-Indic spec since the Halant-Ra order is reversed already. */
7078e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod  if (basic_mask_array[PREF] && base + 3 <= end)
70817d7de91d76406d3e92db37d9eef2fc615f06e68Behdad Esfahbod  {
7098e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod    /* Find a Halant,Ra sequence and mark it fore pre-base reordering processing. */
7108e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod    for (unsigned int i = base + 1; i + 1 < end; i++)
711deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod      if (is_halant_or_coeng (info[i]) &&
7128e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	  info[i + 1].indic_category() == OT_Ra)
7138e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod      {
7140201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	info[i++].mask |= basic_mask_array[PREF];
7150201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	info[i++].mask |= basic_mask_array[PREF];
7160201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod
7170201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	/* Mark the subsequent stuff with 'cfar'.  Used in Khmer.
7180201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	 * Read the feature spec.
7190201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	 * This allows distinguishing the following cases with MS Khmer fonts:
7200201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	 * U+1784,U+17D2,U+179A,U+17D2,U+1782
7210201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	 * U+1784,U+17D2,U+1782,U+17D2,U+179A
7220201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	 */
7230201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	for (; i < end; i++)
7240201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod	  info[i].mask |= basic_mask_array[CFAR];
7250201e0a4649ad5b607e50bcb9605e7a5b7143812Behdad Esfahbod
7268e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	break;
7278e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod      }
72817d7de91d76406d3e92db37d9eef2fc615f06e68Behdad Esfahbod  }
72917d7de91d76406d3e92db37d9eef2fc615f06e68Behdad Esfahbod
7309da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod  /* Apply ZWJ/ZWNJ effects */
7313c2ea9481b1028e927e615a5434ebf8edcb5f891Behdad Esfahbod  for (unsigned int i = start + 1; i < end; i++)
7329da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    if (is_joiner (info[i])) {
7339da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      bool non_joiner = info[i].indic_category() == OT_ZWNJ;
7346b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod      unsigned int j = i;
7359da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod
7369da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      do {
7379da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod	j--;
7386b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod
73970fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod	info[j].mask &= ~basic_mask_array[CJCT];
7406b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod	if (non_joiner)
74170fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod	  info[j].mask &= ~basic_mask_array[HALF];
7426b37bc80843e38ca7b62500f95fd70c08af68d62Behdad Esfahbod
7439da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod      } while (j > start && !is_consonant (info[j]));
7449da0487cd452d780673e24329ce03e174a4ef83bBehdad Esfahbod    }
745743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
746743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
747743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
748743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
7499f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbodinitial_reordering_vowel_syllable (const hb_ot_map_t *map,
7509f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod				   hb_buffer_t *buffer,
75170fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod				   hb_mask_t *basic_mask_array,
752ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod				   unsigned int start, unsigned int end)
753743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
754c5306b6861cfaa50af40e8ceb058791fa06d7981Behdad Esfahbod  /* We made the vowels look like consonants.  So let's call the consonant logic! */
75570fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod  initial_reordering_consonant_syllable (map, buffer, basic_mask_array, start, end);
756743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
757743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
758743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
7599f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbodinitial_reordering_standalone_cluster (const hb_ot_map_t *map,
7609f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod				       hb_buffer_t *buffer,
76170fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod				       hb_mask_t *basic_mask_array,
762ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod				       unsigned int start, unsigned int end)
763743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
76418c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod  /* We treat NBSP/dotted-circle as if they are consonants, so we should just chain.
76518c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod   * Only if not in compatibility mode that is... */
76618c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod
767a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  if (indic_options ().uniscribe_bug_compatible)
76818c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod  {
76918c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod    /* For dotted-circle, this is what Uniscribe does:
77018c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod     * If dotted-circle is the last glyph, it just does nothing.
77118c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod     * Ie. It doesn't form Reph. */
77218c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod    if (buffer->info[end - 1].indic_category() == OT_DOTTEDCIRCLE)
77318c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod      return;
77418c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod  }
77518c06e189bd078affbb84c3bb5bb80687a227c5eBehdad Esfahbod
77670fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod  initial_reordering_consonant_syllable (map, buffer, basic_mask_array, start, end);
777743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
778743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
779743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
7809f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbodinitial_reordering_non_indic (const hb_ot_map_t *map HB_UNUSED,
7819f377ed3210fe7d9f15e0c4f82020556f9a8f6f0Behdad Esfahbod			      hb_buffer_t *buffer HB_UNUSED,
78270fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod			      hb_mask_t *basic_mask_array HB_UNUSED,
7833f18236a03880c0960f5990dc90685f6146951a6Behdad Esfahbod			      unsigned int start HB_UNUSED, unsigned int end HB_UNUSED)
784743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
785743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  /* Nothing to do right now.  If we ever switch to using the output
786743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod   * buffer in the reordering process, we'd need to next_glyph() here. */
787743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
788743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
789743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod#include "hb-ot-shape-complex-indic-machine.hh"
790743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
791743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
792743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodinitial_reordering (const hb_ot_map_t *map,
7933f18236a03880c0960f5990dc90685f6146951a6Behdad Esfahbod		    hb_face_t *face HB_UNUSED,
794743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    hb_buffer_t *buffer,
795743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod		    void *user_data HB_UNUSED)
796743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
79770fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod  hb_mask_t basic_mask_array[ARRAY_LENGTH (indic_basic_features)] = {0};
798b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  unsigned int num_masks = ARRAY_LENGTH (indic_basic_features);
799b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod  for (unsigned int i = 0; i < num_masks; i++)
80070fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod    basic_mask_array[i] = map->get_1_mask (indic_basic_features[i].tag);
801743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
80270fe77bb9a25922bd34f206826d8731d901fb451Behdad Esfahbod  find_syllables (map, buffer, basic_mask_array);
803b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod}
804b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
805743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbodstatic void
80646e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbodfinal_reordering_syllable (hb_buffer_t *buffer,
80746e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod			   hb_mask_t init_mask, hb_mask_t pref_mask,
808ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod			   unsigned int start, unsigned int end)
809743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod{
8104ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  hb_glyph_info_t *info = buffer->info;
8114ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
812e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod  /* 4. Final reordering:
813e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
814e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * After the localized forms and basic shaping forms GSUB features have been
815e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * applied (see below), the shaping engine performs some final glyph
816e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * reordering before applying all the remaining font features to the entire
817e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   * cluster.
8184ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod   */
8194ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
8204ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  /* Find base again */
8214ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  unsigned int base = end;
82210cdc94eee2225f14c198c015256a5a0063eecadBehdad Esfahbod  while (start < base && info[base - 1].indic_position() >= POS_BASE_C)
82310cdc94eee2225f14c198c015256a5a0063eecadBehdad Esfahbod    base--;
8244ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
8254705a7026900e51f6430f03a73c87f2df035df92Behdad Esfahbod  unsigned int start_of_last_cluster = base;
8264705a7026900e51f6430f03a73c87f2df035df92Behdad Esfahbod
8274ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  /*   o Reorder matras:
828e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
829e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     If a pre-base matra character had been reordered before applying basic
830e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     features, the glyph can be moved closer to the main consonant based on
831e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     whether half-forms had been formed. Actual position for the matra is
832e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     defined as “after last standalone halant glyph, after initial matra
833e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     position and before the main consonant”. If ZWJ or ZWNJ follow this
834e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     halant, position is moved after it.
8354ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod   */
8364ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
837362d3db8d3527d0fef260a17d2466e92a4a25425Behdad Esfahbod  if (start < base) /* Otherwise there can't be any pre-base matra characters. */
8389d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod  {
839921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod    unsigned int new_pos = base - 1;
840921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod    while (new_pos > start &&
841deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod	   !(FLAG (info[new_pos].indic_category()) & (FLAG (OT_M) | FLAG (OT_H) | FLAG (OT_Coeng))))
842921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod      new_pos--;
8439d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    /* If we found no Halant we are done.  Otherwise only proceed if the Halant does
8449d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod     * not belong to the Matra itself! */
845deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod    if (is_halant_or_coeng (info[new_pos]) &&
846921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod	info[new_pos].indic_position() != POS_PRE_M) {
8479d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
848921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod      if (new_pos + 1 < end && is_joiner (info[new_pos + 1]))
849921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod	new_pos++;
8509d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod
8519d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      /* Now go see if there's actually any matras... */
852921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod      for (unsigned int i = new_pos; i > start; i--)
8536a091df9b403b147ef78f3974610dedf4ce1e08aBehdad Esfahbod	if (info[i - 1].indic_position () == POS_PRE_M)
8549d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod	{
8551a1dbe9a2787f226f3e43063da8eb6633438b0a0Behdad Esfahbod	  unsigned int old_pos = i - 1;
8561a1dbe9a2787f226f3e43063da8eb6633438b0a0Behdad Esfahbod	  hb_glyph_info_t tmp = info[old_pos];
8571a1dbe9a2787f226f3e43063da8eb6633438b0a0Behdad Esfahbod	  memmove (&info[old_pos], &info[old_pos + 1], (new_pos - old_pos) * sizeof (info[0]));
8581a1dbe9a2787f226f3e43063da8eb6633438b0a0Behdad Esfahbod	  info[new_pos] = tmp;
859921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod	  start_of_last_cluster = MIN (new_pos, start_of_last_cluster);
860921ce5b17daf06af8e17989a3e335b9f5df20483Behdad Esfahbod	  new_pos--;
8619d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod	}
8629d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    }
8634ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  }
8644ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
8654ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod
8664ac9e98d9d2ea973dd612dc4063cf78496c643a0Behdad Esfahbod  /*   o Reorder reph:
867e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
868e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     Reph’s original position is always at the beginning of the syllable,
869e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     (i.e. it is not reordered at the character reordering stage). However,
870e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     it will be reordered according to the basic-forms shaping results.
871e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     Possible positions for reph, depending on the script, are; after main,
872e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     before post-base consonant forms, and after post-base consonant forms.
873dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod   */
874dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
875dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  /* If there's anything after the Ra that has the REPH pos, it ought to be halant.
876dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod   * Which means that the font has failed to ligate the Reph.  In which case, we
877dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod   * shouldn't move. */
878dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  if (start + 1 < end &&
879dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod      info[start].indic_position() == POS_RA_TO_BECOME_REPH &&
880dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod      info[start + 1].indic_position() != POS_RA_TO_BECOME_REPH)
881dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  {
88202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      unsigned int new_reph_pos;
88302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
88402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     enum reph_position_t {
885f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_AFTER_MAIN,
886f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_BEFORE_SUBSCRIPT,
887f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_AFTER_SUBSCRIPT,
888f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       REPH_BEFORE_POSTSCRIPT,
8899fc7a11469113d31d8095757c4fc038c3427d44aBehdad Esfahbod       REPH_AFTER_POSTSCRIPT
890f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod     } reph_pos;
891f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
892f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod     /* XXX Figure out old behavior too */
8937f852b644b8143492a02edfc853114aaa23446bdBehdad Esfahbod     switch ((hb_tag_t) buffer->props.script)
894f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod     {
895f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_MALAYALAM:
896f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_ORIYA:
897f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_AFTER_MAIN;
898f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
899f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
900f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_GURMUKHI:
901f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_BEFORE_SUBSCRIPT;
902f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
903f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
904f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_BENGALI:
905f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_AFTER_SUBSCRIPT;
906f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
907f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
908f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       default:
909f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_DEVANAGARI:
910f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_GUJARATI:
9119c4d24a3a677a58ec59c7fb0f8b70b8aad30a032Behdad Esfahbod       case HB_SCRIPT_SINHALA:
912f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_BEFORE_POSTSCRIPT;
913f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
914f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod
915f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_KANNADA:
916f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_TAMIL:
917f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod       case HB_SCRIPT_TELUGU:
918f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 reph_pos = REPH_AFTER_POSTSCRIPT;
919f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod	 break;
920f89367251109af235f4f0446c13c261a5a4a6f72Behdad Esfahbod     }
92102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
922dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod    /*       1. If reph should be positioned after post-base consonant forms,
923dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          proceed to step 5.
92402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
9259d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    if (reph_pos == REPH_AFTER_POSTSCRIPT)
92602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
9279d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      goto reph_step_5;
92802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
92902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
93002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       2. If the reph repositioning class is not after post-base: target
931dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          position is after the first explicit halant glyph between the
932dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first post-reph consonant and last main consonant. If ZWJ or ZWNJ
933dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          are following this halant, position is moved after it. If such
934dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          position is found, this is the target position. Otherwise,
935dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          proceed to the next step.
936dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *
937dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          Note: in old-implementation fonts, where classifications were
938dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          fixed in shaping engine, there was no case where reph position
939dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          will be found on this step.
94002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
94102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
94202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      new_reph_pos = start + 1;
943deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod      while (new_reph_pos < base && !is_halant_or_coeng (info[new_reph_pos]))
94402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	new_reph_pos++;
94502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
946deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod      if (new_reph_pos < base && is_halant_or_coeng (info[new_reph_pos])) {
94702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	/* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */
94802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1]))
94902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	  new_reph_pos++;
95002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	goto reph_move;
95102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      }
95202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
95302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
95402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       3. If reph should be repositioned after the main consonant: find the
955dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first consonant not ligated with main, or find the first
956dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          consonant that is not a potential pre-base reordering Ra.
95702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
9589d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    if (reph_pos == REPH_AFTER_MAIN)
95902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
960b504e060f008e95b1ba36c06600c9fea4f5d4808Behdad Esfahbod      new_reph_pos = base;
961b504e060f008e95b1ba36c06600c9fea4f5d4808Behdad Esfahbod      /* XXX Skip potential pre-base reordering Ra. */
962b504e060f008e95b1ba36c06600c9fea4f5d4808Behdad Esfahbod      while (new_reph_pos < end &&
963be8b9f5f715f6fb36b98bd33c3303f79cc068f8aBehdad Esfahbod	     !( FLAG (info[new_reph_pos + 1].indic_position()) & (FLAG (POS_BELOW_C) | FLAG (POS_POST_C) | FLAG (POS_AFTER_POST) | FLAG (POS_SMVD))))
964b504e060f008e95b1ba36c06600c9fea4f5d4808Behdad Esfahbod	new_reph_pos++;
965b504e060f008e95b1ba36c06600c9fea4f5d4808Behdad Esfahbod      if (new_reph_pos < end)
966b504e060f008e95b1ba36c06600c9fea4f5d4808Behdad Esfahbod        goto reph_move;
96702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
96802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
96902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       4. If reph should be positioned before post-base consonant, find
970dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first post-base classified consonant not ligated with main. If no
971dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          consonant is found, the target position should be before the
972dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          first matra, syllable modifier sign or vedic sign.
97302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
9749d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    /* This is our take on what step 4 is trying to say (and failing, BADLY). */
9759d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod    if (reph_pos == REPH_AFTER_SUBSCRIPT)
97602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
9779d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      new_reph_pos = base;
9789d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      while (new_reph_pos < end &&
979be8b9f5f715f6fb36b98bd33c3303f79cc068f8aBehdad Esfahbod	     !( FLAG (info[new_reph_pos + 1].indic_position()) & (FLAG (POS_POST_C) | FLAG (POS_AFTER_POST) | FLAG (POS_SMVD))))
9809d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod	new_reph_pos++;
9819d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod      if (new_reph_pos < end)
9829d0d319a4a7e85d922e58fade0f40caae1c9f109Behdad Esfahbod        goto reph_move;
98302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
98402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
98502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       5. If no consonant is found in steps 3 or 4, move reph to a position
986dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          immediately before the first post-base matra, syllable modifier
987dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          sign or vedic sign that has a reordering class after the intended
988dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          reph position. For example, if the reordering position for reph
989dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          is post-main, it will skip above-base matras that also have a
990dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     *          post-main position.
991dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod     */
99202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_step_5:
99302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
99402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      /* XXX */
99502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
996dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
99702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    /*       6. Otherwise, reorder reph to the end of the syllable.
99802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod     */
99902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
100002b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      new_reph_pos = end - 1;
100102b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      while (new_reph_pos > start && info[new_reph_pos].indic_position() == POS_SMVD)
100202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	new_reph_pos--;
100302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod
1004892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod      /*
1005892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * If the Reph is to be ending up after a Matra,Halant sequence,
1006892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * position it before that Halant so it can interact with the Matra.
1007892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * However, if it's a plain Consonant,Halant we shouldn't do that.
1008892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * Uniscribe doesn't do this.
1009892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       * TEST: U+0930,U+094D,U+0915,U+094B,U+094D
1010892eb7878238d810a2a70f9dadbf958207bfeaa1Behdad Esfahbod       */
1011a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod      if (!indic_options ().uniscribe_bug_compatible &&
1012deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod	  unlikely (is_halant_or_coeng (info[new_reph_pos]))) {
101302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	for (unsigned int i = base + 1; i < new_reph_pos; i++)
101402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	  if (info[i].indic_category() == OT_M) {
101502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	    /* Ok, got it. */
101602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	    new_reph_pos--;
101702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod	  }
101802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      }
101902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      goto reph_move;
10208df5636968389ac7bf8620ccd091fd4872b0bbeeBehdad Esfahbod    }
10218df5636968389ac7bf8620ccd091fd4872b0bbeeBehdad Esfahbod
102202b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    reph_move:
102302b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    {
102402b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      /* Move */
102502b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      hb_glyph_info_t reph = info[start];
102602b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      memmove (&info[start], &info[start + 1], (new_reph_pos - start) * sizeof (info[0]));
102702b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      info[new_reph_pos] = reph;
102802b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod      start_of_last_cluster = start; /* Yay, one big cluster! */
102902b2922fbf098c8282eb23dc2c54d5829cf67024Behdad Esfahbod    }
1030dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  }
1031dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
1032dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod
1033dbb105883c6e9b83e78dc8b10766cd56b98cd7e1Behdad Esfahbod  /*   o Reorder pre-base reordering consonants:
1034e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *
1035e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     If a pre-base reordering consonant is found, reorder it according to
1036e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   *     the following rules:
1037e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod   */
1038e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
103946e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod  if (pref_mask && base + 1 < end) /* Otherwise there can't be any pre-base reordering Ra. */
104046e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod  {
10418e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod    for (unsigned int i = base + 1; i < end; i++)
10428e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod      if ((info[i].mask & pref_mask) != 0)
104378818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod      {
10448e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	/*       1. Only reorder a glyph produced by substitution during application
10458e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	 *          of the <pref> feature. (Note that a font may shape a Ra consonant with
10468e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	 *          the feature generally but block it in certain contexts.)
10478e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	 */
10488e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	if (i + 1 == end || (info[i + 1].mask & pref_mask) == 0)
10498e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	{
10508e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	  /*
10518e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	   *       2. Try to find a target position the same way as for pre-base matra.
10528e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	   *          If it is found, reorder pre-base consonant glyph.
10538e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	   *
10548e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	   *       3. If position is not found, reorder immediately before main
10558e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	   *          consonant.
10568e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	   */
10578e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod
10588e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	  unsigned int new_pos = base;
10598e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	  while (new_pos > start + 1 &&
1060deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod		 !(FLAG (info[new_pos - 1].indic_category()) & (FLAG (OT_M) | FLAG (OT_H) | FLAG (OT_Coeng))))
10618e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	    new_pos--;
10628e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod
1063deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod	  if (new_pos > start && is_halant_or_coeng (info[new_pos - 1]))
10648e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	    /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
10658e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	    if (new_pos < end && is_joiner (info[new_pos]))
10668e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	      new_pos++;
10678e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod
10688e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	  {
10698e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	    unsigned int old_pos = i;
10708e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	    hb_glyph_info_t tmp = info[old_pos];
10718e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	    memmove (&info[new_pos + 1], &info[new_pos], (old_pos - new_pos) * sizeof (info[0]));
10728e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	    info[new_pos] = tmp;
10738e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	    start_of_last_cluster = MIN (new_pos, start_of_last_cluster);
10748e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	  }
10758e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod	}
10768e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod
10778e7b5882fb4c1921c9d030d354a9b998115cdb8cBehdad Esfahbod        break;
107878818124b17691ec2c647142fdb9ae743aa03deeBehdad Esfahbod      }
107946e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod  }
1080eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
1081eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
1082a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod  /* Apply 'init' to the Left Matra if it's a word start. */
10836a091df9b403b147ef78f3974610dedf4ce1e08aBehdad Esfahbod  if (info[start].indic_position () == POS_PRE_M &&
1084a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod      (!start ||
1085eace47b173807d94b29a6490d0bc3c9f8f6168d1Behdad Esfahbod       !(FLAG (_hb_glyph_info_get_general_category (&info[start - 1])) &
1086a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	 (FLAG (HB_UNICODE_GENERAL_CATEGORY_LOWERCASE_LETTER) |
1087a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_MODIFIER_LETTER) |
1088a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER) |
1089a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_TITLECASE_LETTER) |
1090a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_UPPERCASE_LETTER) |
1091a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACING_MARK) |
1092a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_ENCLOSING_MARK) |
1093a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod	  FLAG (HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)))))
109446e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod    info[start].mask |= init_mask;
1095a913b024d84973556094fd64ce5f0b7106fcc3b5Behdad Esfahbod
1096eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
109721d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod
109821d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod  /* Finish off the clusters and go home! */
109921d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod
1100a2b471df821b32625d127f83b2f90e6d6a967e7eBehdad Esfahbod  if (!indic_options ().uniscribe_bug_compatible)
1101ebe29733d44fe0fa9fb30f946ab0dd7a40336a24Behdad Esfahbod  {
110221d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    /* This is what Uniscribe does.  Ie. add cluster boundaries after Halant,ZWNJ.
110321d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod     * This means, half forms are submerged into the main consonants cluster.
110421d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod     * This is unnecessary, and makes cursor positioning harder, but that's what
110521d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod     * Uniscribe does. */
110621d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    unsigned int cluster_start = start;
110721d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    for (unsigned int i = start + 1; i < start_of_last_cluster; i++)
1108deb521dee4fdca8c2124cfb39a205e6269d4a70dBehdad Esfahbod      if (is_halant_or_coeng (info[i - 1]) && info[i].indic_category() == OT_ZWNJ) {
110921d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod        i++;
111021d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod	buffer->merge_clusters (cluster_start, i);
111121d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod	cluster_start = i;
111221d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod      }
111321d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod    start_of_last_cluster = cluster_start;
111421d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod  }
111521d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod
111621d2803133c2c424ed37a9f3d17c7fc4963e5a60Behdad Esfahbod  buffer->merge_clusters (start_of_last_cluster, end);
1117ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod}
1118e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
1119e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
1120ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodstatic void
1121ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbodfinal_reordering (const hb_ot_map_t *map,
11223f18236a03880c0960f5990dc90685f6146951a6Behdad Esfahbod		  hb_face_t *face HB_UNUSED,
1123ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod		  hb_buffer_t *buffer,
1124ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod		  void *user_data HB_UNUSED)
1125ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod{
1126ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  unsigned int count = buffer->len;
1127ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  if (!count) return;
1128ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod
112946e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod  hb_mask_t init_mask = map->get_1_mask (HB_TAG('i','n','i','t'));
113046e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod  hb_mask_t pref_mask = map->get_1_mask (HB_TAG('p','r','e','f'));
1131eed903b1644e087178438959664a6a57bebc398bBehdad Esfahbod
1132ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  hb_glyph_info_t *info = buffer->info;
1133ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  unsigned int last = 0;
1134cee7187447b76b22e1bb6136d137b35ac49c3a5dBehdad Esfahbod  unsigned int last_syllable = info[0].syllable();
1135ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod  for (unsigned int i = 1; i < count; i++)
1136cee7187447b76b22e1bb6136d137b35ac49c3a5dBehdad Esfahbod    if (last_syllable != info[i].syllable()) {
113746e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod      final_reordering_syllable (buffer, init_mask, pref_mask, last, i);
1138ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod      last = i;
1139cee7187447b76b22e1bb6136d137b35ac49c3a5dBehdad Esfahbod      last_syllable = info[last].syllable();
1140ef24cc8c8e2478a6352c340f4611a617646de4ccBehdad Esfahbod    }
114146e645ec4b59f0a278347be11f40c7df700d5bb6Behdad Esfahbod  final_reordering_syllable (buffer, init_mask, pref_mask, last, count);
1142e7be05702447ae270d797398132c1930cd3a9b86Behdad Esfahbod
1143743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category);
1144743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position);
1145743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod}
1146743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
1147743807a3ce1b2229e5307a8aea074a7544623d8dBehdad Esfahbod
1148b9ddbd55930228422e82b34a141ad1b6093f5376Behdad Esfahbod
1149