1/*
2 * Copyright © 2011,2012  Google, Inc.
3 *
4 *  This is part of HarfBuzz, a text shaping library.
5 *
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
11 *
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16 * DAMAGE.
17 *
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23 *
24 * Google Author(s): Behdad Esfahbod
25 */
26
27#include "hb-ot-shape-complex-indic-private.hh"
28#include "hb-ot-layout-private.hh"
29
30/* buffer var allocations */
31#define indic_category() complex_var_u8_0() /* indic_category_t */
32#define indic_position() complex_var_u8_1() /* indic_position_t */
33
34
35/*
36 * Indic shaper.
37 */
38
39
40#define IN_HALF_BLOCK(u, Base) (((u) & ~0x7F) == (Base))
41
42#define IS_DEVA(u) (IN_HALF_BLOCK (u, 0x0900))
43#define IS_BENG(u) (IN_HALF_BLOCK (u, 0x0980))
44#define IS_GURU(u) (IN_HALF_BLOCK (u, 0x0A00))
45#define IS_GUJR(u) (IN_HALF_BLOCK (u, 0x0A80))
46#define IS_ORYA(u) (IN_HALF_BLOCK (u, 0x0B00))
47#define IS_TAML(u) (IN_HALF_BLOCK (u, 0x0B80))
48#define IS_TELU(u) (IN_HALF_BLOCK (u, 0x0C00))
49#define IS_KNDA(u) (IN_HALF_BLOCK (u, 0x0C80))
50#define IS_MLYM(u) (IN_HALF_BLOCK (u, 0x0D00))
51#define IS_SINH(u) (IN_HALF_BLOCK (u, 0x0D80))
52#define IS_KHMR(u) (IN_HALF_BLOCK (u, 0x1780))
53
54
55#define MATRA_POS_LEFT(u)	POS_PRE_M
56#define MATRA_POS_RIGHT(u)	( \
57				  IS_DEVA(u) ? POS_AFTER_SUB  : \
58				  IS_BENG(u) ? POS_AFTER_POST : \
59				  IS_GURU(u) ? POS_AFTER_POST : \
60				  IS_GUJR(u) ? POS_AFTER_POST : \
61				  IS_ORYA(u) ? POS_AFTER_POST : \
62				  IS_TAML(u) ? POS_AFTER_POST : \
63				  IS_TELU(u) ? (u <= 0x0C42 ? POS_BEFORE_SUB : POS_AFTER_SUB) : \
64				  IS_KNDA(u) ? (u < 0x0CC3 || u > 0xCD6 ? POS_BEFORE_SUB : POS_AFTER_SUB) : \
65				  IS_MLYM(u) ? POS_AFTER_POST : \
66				  IS_SINH(u) ? POS_AFTER_SUB  : \
67				  IS_KHMR(u) ? POS_AFTER_POST : \
68				  /*default*/  POS_AFTER_SUB    \
69				)
70#define MATRA_POS_TOP(u)	( /* BENG and MLYM don't have top matras. */ \
71				  IS_DEVA(u) ? POS_AFTER_SUB  : \
72				  IS_GURU(u) ? POS_AFTER_POST : /* Deviate from spec */ \
73				  IS_GUJR(u) ? POS_AFTER_SUB  : \
74				  IS_ORYA(u) ? POS_AFTER_MAIN : \
75				  IS_TAML(u) ? POS_AFTER_SUB  : \
76				  IS_TELU(u) ? POS_BEFORE_SUB : \
77				  IS_KNDA(u) ? POS_BEFORE_SUB : \
78				  IS_SINH(u) ? POS_AFTER_SUB  : \
79				  IS_KHMR(u) ? POS_AFTER_POST : \
80				  /*default*/  POS_AFTER_SUB    \
81				)
82#define MATRA_POS_BOTTOM(u)	( \
83				  IS_DEVA(u) ? POS_AFTER_SUB  : \
84				  IS_BENG(u) ? POS_AFTER_SUB  : \
85				  IS_GURU(u) ? POS_AFTER_POST : \
86				  IS_GUJR(u) ? POS_AFTER_POST : \
87				  IS_ORYA(u) ? POS_AFTER_SUB  : \
88				  IS_TAML(u) ? POS_AFTER_POST : \
89				  IS_TELU(u) ? POS_BEFORE_SUB : \
90				  IS_KNDA(u) ? POS_BEFORE_SUB : \
91				  IS_MLYM(u) ? POS_AFTER_POST : \
92				  IS_SINH(u) ? POS_AFTER_SUB  : \
93				  IS_KHMR(u) ? POS_AFTER_POST : \
94				  /*default*/  POS_AFTER_SUB    \
95				)
96
97static inline indic_position_t
98matra_position (hb_codepoint_t u, indic_position_t side)
99{
100  switch ((int) side)
101  {
102    case POS_PRE_C:	return MATRA_POS_LEFT (u);
103    case POS_POST_C:	return MATRA_POS_RIGHT (u);
104    case POS_ABOVE_C:	return MATRA_POS_TOP (u);
105    case POS_BELOW_C:	return MATRA_POS_BOTTOM (u);
106  };
107  return side;
108}
109
110/* XXX
111 * This is a hack for now.  We should move this data into the main Indic table.
112 * Or completely remove it and just check in the tables.
113 */
114static const hb_codepoint_t ra_chars[] = {
115  0x0930, /* Devanagari */
116  0x09B0, /* Bengali */
117  0x09F0, /* Bengali */
118  0x0A30, /* Gurmukhi */	/* No Reph */
119  0x0AB0, /* Gujarati */
120  0x0B30, /* Oriya */
121  0x0BB0, /* Tamil */		/* No Reph */
122  0x0C30, /* Telugu */		/* Reph formed only with ZWJ */
123  0x0CB0, /* Kannada */
124  0x0D30, /* Malayalam */	/* No Reph, Logical Repha */
125
126  0x0DBB, /* Sinhala */		/* Reph formed only with ZWJ */
127
128  0x179A, /* Khmer */		/* No Reph, Visual Repha */
129};
130
131static inline indic_position_t
132consonant_position (hb_codepoint_t  u)
133{
134  if ((u & ~0x007F) == 0x1780)
135    return POS_BELOW_C; /* In Khmer coeng model, post and below forms should not be reordered. */
136  return POS_BASE_C; /* Will recategorize later based on font lookups. */
137}
138
139static inline bool
140is_ra (hb_codepoint_t u)
141{
142  for (unsigned int i = 0; i < ARRAY_LENGTH (ra_chars); i++)
143    if (u == ra_chars[i])
144      return true;
145  return false;
146}
147
148static inline bool
149is_one_of (const hb_glyph_info_t &info, unsigned int flags)
150{
151  /* If it ligated, all bets are off. */
152  if (is_a_ligature (info)) return false;
153  return !!(FLAG (info.indic_category()) & flags);
154}
155
156#define JOINER_FLAGS (FLAG (OT_ZWJ) | FLAG (OT_ZWNJ))
157static inline bool
158is_joiner (const hb_glyph_info_t &info)
159{
160  return is_one_of (info, JOINER_FLAGS);
161}
162
163/* Note:
164 *
165 * We treat Vowels and placeholders as if they were consonants.  This is safe because Vowels
166 * cannot happen in a consonant syllable.  The plus side however is, we can call the
167 * consonant syllable logic from the vowel syllable function and get it all right! */
168#define CONSONANT_FLAGS (FLAG (OT_C) | FLAG (OT_CM) | FLAG (OT_Ra) | FLAG (OT_V) | FLAG (OT_NBSP) | FLAG (OT_DOTTEDCIRCLE))
169static inline bool
170is_consonant (const hb_glyph_info_t &info)
171{
172  return is_one_of (info, CONSONANT_FLAGS);
173}
174
175#define HALANT_OR_COENG_FLAGS (FLAG (OT_H) | FLAG (OT_Coeng))
176static inline bool
177is_halant_or_coeng (const hb_glyph_info_t &info)
178{
179  return is_one_of (info, HALANT_OR_COENG_FLAGS);
180}
181
182static inline void
183set_indic_properties (hb_glyph_info_t &info)
184{
185  hb_codepoint_t u = info.codepoint;
186  unsigned int type = hb_indic_get_categories (u);
187  indic_category_t cat = (indic_category_t) (type & 0x7F);
188  indic_position_t pos = (indic_position_t) (type >> 8);
189
190
191  /*
192   * Re-assign category
193   */
194
195
196  /* The spec says U+0952 is OT_A.  However, testing shows that Uniscribe
197   * treats U+0951..U+0952 all as OT_VD.
198   * TESTS:
199   * U+092E,U+0947,U+0952
200   * U+092E,U+0952,U+0947
201   * U+092E,U+0947,U+0951
202   * U+092E,U+0951,U+0947
203   * */
204  if (unlikely (hb_in_range<hb_codepoint_t> (u, 0x0951, 0x0954)))
205    cat = OT_VD;
206
207  if (unlikely (u == 0x17D1))
208    cat = OT_X;
209  if (cat == OT_X &&
210      unlikely (hb_in_range<hb_codepoint_t> (u, 0x17CB, 0x17D3))) /* Khmer Various signs */
211  {
212    /* These are like Top Matras. */
213    cat = OT_M;
214    pos = POS_ABOVE_C;
215  }
216  if (u == 0x17C6) /* Khmer Bindu doesn't like to be repositioned. */
217    cat = OT_N;
218
219  if (unlikely (u == 0x17D2)) cat = OT_Coeng; /* Khmer coeng */
220  else if (unlikely (u == 0x200C)) cat = OT_ZWNJ;
221  else if (unlikely (u == 0x200D)) cat = OT_ZWJ;
222  else if (unlikely (u == 0x25CC)) cat = OT_DOTTEDCIRCLE;
223  else if (unlikely (u == 0x0A71)) cat = OT_SM; /* GURMUKHI ADDAK.  More like consonant medial. like 0A75. */
224
225  if (cat == OT_Repha) {
226    /* There are two kinds of characters marked as Repha:
227     * - The ones that are GenCat=Mn are already positioned visually, ie. after base. (eg. Khmer)
228     * - The ones that are GenCat=Lo is encoded logically, ie. beginning of syllable. (eg. Malayalam)
229     *
230     * We recategorize the first kind to look like a Nukta and attached to the base directly.
231     */
232    if (_hb_glyph_info_get_general_category (&info) == HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)
233      cat = OT_N;
234  }
235
236
237
238  /*
239   * Re-assign position.
240   */
241
242  if ((FLAG (cat) & CONSONANT_FLAGS))
243  {
244    pos = consonant_position (u);
245    if (is_ra (u))
246      cat = OT_Ra;
247  }
248  else if (cat == OT_M)
249  {
250    pos = matra_position (u, pos);
251  }
252  else if (cat == OT_SM || cat == OT_VD)
253  {
254    pos = POS_SMVD;
255  }
256
257  if (unlikely (u == 0x0B01)) pos = POS_BEFORE_SUB; /* Oriya Bindu is BeforeSub in the spec. */
258
259
260
261  info.indic_category() = cat;
262  info.indic_position() = pos;
263}
264
265/*
266 * Things above this line should ideally be moved to the Indic table itself.
267 */
268
269
270/*
271 * Indic configurations.  Note that we do not want to keep every single script-specific
272 * behavior in these tables necessarily.  This should mainly be used for per-script
273 * properties that are cheaper keeping here, than in the code.  Ie. if, say, one and
274 * only one script has an exception, that one script can be if'ed directly in the code,
275 * instead of adding a new flag in these structs.
276 */
277
278enum base_position_t {
279  BASE_POS_FIRST,
280  BASE_POS_LAST
281};
282enum reph_position_t {
283  REPH_POS_DEFAULT     = POS_BEFORE_POST,
284
285  REPH_POS_AFTER_MAIN  = POS_AFTER_MAIN,
286  REPH_POS_BEFORE_SUB  = POS_BEFORE_SUB,
287  REPH_POS_AFTER_SUB   = POS_AFTER_SUB,
288  REPH_POS_BEFORE_POST = POS_BEFORE_POST,
289  REPH_POS_AFTER_POST  = POS_AFTER_POST
290};
291enum reph_mode_t {
292  REPH_MODE_IMPLICIT,  /* Reph formed out of initial Ra,H sequence. */
293  REPH_MODE_EXPLICIT,  /* Reph formed out of initial Ra,H,ZWJ sequence. */
294  REPH_MODE_VIS_REPHA, /* Encoded Repha character, no reordering needed. */
295  REPH_MODE_LOG_REPHA  /* Encoded Repha character, needs reordering. */
296};
297struct indic_config_t
298{
299  hb_script_t     script;
300  bool            has_old_spec;
301  hb_codepoint_t  virama;
302  base_position_t base_pos;
303  reph_position_t reph_pos;
304  reph_mode_t     reph_mode;
305};
306
307static const indic_config_t indic_configs[] =
308{
309  /* Default.  Should be first. */
310  {HB_SCRIPT_INVALID,	false,     0,BASE_POS_LAST, REPH_POS_DEFAULT,    REPH_MODE_IMPLICIT},
311  {HB_SCRIPT_DEVANAGARI,true, 0x094D,BASE_POS_LAST, REPH_POS_BEFORE_POST,REPH_MODE_IMPLICIT},
312  {HB_SCRIPT_BENGALI,	true, 0x09CD,BASE_POS_LAST, REPH_POS_AFTER_SUB,  REPH_MODE_IMPLICIT},
313  {HB_SCRIPT_GURMUKHI,	true, 0x0A4D,BASE_POS_LAST, REPH_POS_BEFORE_SUB, REPH_MODE_IMPLICIT},
314  {HB_SCRIPT_GUJARATI,	true, 0x0ACD,BASE_POS_LAST, REPH_POS_BEFORE_POST,REPH_MODE_IMPLICIT},
315  {HB_SCRIPT_ORIYA,	true, 0x0B4D,BASE_POS_LAST, REPH_POS_AFTER_MAIN, REPH_MODE_IMPLICIT},
316  {HB_SCRIPT_TAMIL,	true, 0x0BCD,BASE_POS_LAST, REPH_POS_AFTER_POST, REPH_MODE_IMPLICIT},
317  {HB_SCRIPT_TELUGU,	true, 0x0C4D,BASE_POS_LAST, REPH_POS_AFTER_POST, REPH_MODE_EXPLICIT},
318  {HB_SCRIPT_KANNADA,	true, 0x0CCD,BASE_POS_LAST, REPH_POS_AFTER_POST, REPH_MODE_IMPLICIT},
319  {HB_SCRIPT_MALAYALAM,	true, 0x0D4D,BASE_POS_LAST, REPH_POS_AFTER_MAIN, REPH_MODE_LOG_REPHA},
320  {HB_SCRIPT_SINHALA,	false,0x0DCA,BASE_POS_FIRST,REPH_POS_AFTER_MAIN, REPH_MODE_EXPLICIT},
321  {HB_SCRIPT_KHMER,	false,0x17D2,BASE_POS_FIRST,REPH_POS_DEFAULT,    REPH_MODE_VIS_REPHA},
322};
323
324
325
326/*
327 * Indic shaper.
328 */
329
330struct feature_list_t {
331  hb_tag_t tag;
332  hb_ot_map_feature_flags_t flags;
333};
334
335static const feature_list_t
336indic_features[] =
337{
338  /*
339   * Basic features.
340   * These features are applied in order, one at a time, after initial_reordering.
341   */
342  {HB_TAG('n','u','k','t'), F_GLOBAL},
343  {HB_TAG('a','k','h','n'), F_GLOBAL},
344  {HB_TAG('r','p','h','f'), F_NONE},
345  {HB_TAG('r','k','r','f'), F_GLOBAL},
346  {HB_TAG('p','r','e','f'), F_NONE},
347  {HB_TAG('b','l','w','f'), F_NONE},
348  {HB_TAG('h','a','l','f'), F_NONE},
349  {HB_TAG('a','b','v','f'), F_NONE},
350  {HB_TAG('p','s','t','f'), F_NONE},
351  {HB_TAG('c','f','a','r'), F_NONE},
352  {HB_TAG('v','a','t','u'), F_GLOBAL},
353  {HB_TAG('c','j','c','t'), F_GLOBAL},
354  /*
355   * Other features.
356   * These features are applied all at once, after final_reordering.
357   */
358  {HB_TAG('i','n','i','t'), F_NONE},
359  {HB_TAG('p','r','e','s'), F_GLOBAL},
360  {HB_TAG('a','b','v','s'), F_GLOBAL},
361  {HB_TAG('b','l','w','s'), F_GLOBAL},
362  {HB_TAG('p','s','t','s'), F_GLOBAL},
363  {HB_TAG('h','a','l','n'), F_GLOBAL},
364  /* Positioning features, though we don't care about the types. */
365  {HB_TAG('d','i','s','t'), F_GLOBAL},
366  {HB_TAG('a','b','v','m'), F_GLOBAL},
367  {HB_TAG('b','l','w','m'), F_GLOBAL},
368};
369
370/*
371 * Must be in the same order as the indic_features array.
372 */
373enum {
374  _NUKT,
375  _AKHN,
376  RPHF,
377  _RKRF,
378  PREF,
379  BLWF,
380  HALF,
381  ABVF,
382  PSTF,
383  CFAR,
384  _VATU,
385  _CJCT,
386
387  INIT,
388  _PRES,
389  _ABVS,
390  _BLWS,
391  _PSTS,
392  _HALN,
393  _DIST,
394  _ABVM,
395  _BLWM,
396
397  INDIC_NUM_FEATURES,
398  INDIC_BASIC_FEATURES = INIT /* Don't forget to update this! */
399};
400
401static void
402setup_syllables (const hb_ot_shape_plan_t *plan,
403		 hb_font_t *font,
404		 hb_buffer_t *buffer);
405static void
406initial_reordering (const hb_ot_shape_plan_t *plan,
407		    hb_font_t *font,
408		    hb_buffer_t *buffer);
409static void
410final_reordering (const hb_ot_shape_plan_t *plan,
411		  hb_font_t *font,
412		  hb_buffer_t *buffer);
413
414static void
415collect_features_indic (hb_ot_shape_planner_t *plan)
416{
417  hb_ot_map_builder_t *map = &plan->map;
418
419  /* Do this before any lookups have been applied. */
420  map->add_gsub_pause (setup_syllables);
421
422  map->add_global_bool_feature (HB_TAG('l','o','c','l'));
423  /* The Indic specs do not require ccmp, but we apply it here since if
424   * there is a use of it, it's typically at the beginning. */
425  map->add_global_bool_feature (HB_TAG('c','c','m','p'));
426
427
428  unsigned int i = 0;
429  map->add_gsub_pause (initial_reordering);
430  for (; i < INDIC_BASIC_FEATURES; i++) {
431    map->add_feature (indic_features[i].tag, 1, indic_features[i].flags | F_MANUAL_ZWJ);
432    map->add_gsub_pause (NULL);
433  }
434  map->add_gsub_pause (final_reordering);
435  for (; i < INDIC_NUM_FEATURES; i++) {
436    map->add_feature (indic_features[i].tag, 1, indic_features[i].flags | F_MANUAL_ZWJ);
437  }
438}
439
440static void
441override_features_indic (hb_ot_shape_planner_t *plan)
442{
443  /* Uniscribe does not apply 'kern'. */
444  if (hb_options ().uniscribe_bug_compatible)
445    plan->map.add_feature (HB_TAG('k','e','r','n'), 0, F_GLOBAL);
446
447  plan->map.add_feature (HB_TAG('l','i','g','a'), 0, F_GLOBAL);
448}
449
450
451struct would_substitute_feature_t
452{
453  inline void init (const hb_ot_map_t *map, hb_tag_t feature_tag)
454  {
455    map->get_stage_lookups (0/*GSUB*/,
456			    map->get_feature_stage (0/*GSUB*/, feature_tag),
457			    &lookups, &count);
458  }
459
460  inline bool would_substitute (const hb_codepoint_t *glyphs,
461				unsigned int          glyphs_count,
462				bool                  zero_context,
463				hb_face_t            *face) const
464  {
465    for (unsigned int i = 0; i < count; i++)
466      if (hb_ot_layout_lookup_would_substitute_fast (face, lookups[i].index, glyphs, glyphs_count, zero_context))
467	return true;
468    return false;
469  }
470
471  private:
472  const hb_ot_map_t::lookup_map_t *lookups;
473  unsigned int count;
474};
475
476struct indic_shape_plan_t
477{
478  ASSERT_POD ();
479
480  inline bool get_virama_glyph (hb_font_t *font, hb_codepoint_t *pglyph) const
481  {
482    hb_codepoint_t glyph = virama_glyph;
483    if (unlikely (virama_glyph == (hb_codepoint_t) -1))
484    {
485      if (!config->virama || !font->get_glyph (config->virama, 0, &glyph))
486	glyph = 0;
487      /* Technically speaking, the spec says we should apply 'locl' to virama too.
488       * Maybe one day... */
489
490      /* Our get_glyph() function needs a font, so we can't get the virama glyph
491       * during shape planning...  Instead, overwrite it here.  It's safe.  Don't worry! */
492      (const_cast<indic_shape_plan_t *> (this))->virama_glyph = glyph;
493    }
494
495    *pglyph = glyph;
496    return glyph != 0;
497  }
498
499  const indic_config_t *config;
500
501  bool is_old_spec;
502  hb_codepoint_t virama_glyph;
503
504  would_substitute_feature_t rphf;
505  would_substitute_feature_t pref;
506  would_substitute_feature_t blwf;
507  would_substitute_feature_t pstf;
508
509  hb_mask_t mask_array[INDIC_NUM_FEATURES];
510};
511
512static void *
513data_create_indic (const hb_ot_shape_plan_t *plan)
514{
515  indic_shape_plan_t *indic_plan = (indic_shape_plan_t *) calloc (1, sizeof (indic_shape_plan_t));
516  if (unlikely (!indic_plan))
517    return NULL;
518
519  indic_plan->config = &indic_configs[0];
520  for (unsigned int i = 1; i < ARRAY_LENGTH (indic_configs); i++)
521    if (plan->props.script == indic_configs[i].script) {
522      indic_plan->config = &indic_configs[i];
523      break;
524    }
525
526  indic_plan->is_old_spec = indic_plan->config->has_old_spec && ((plan->map.chosen_script[0] & 0x000000FF) != '2');
527  indic_plan->virama_glyph = (hb_codepoint_t) -1;
528
529  indic_plan->rphf.init (&plan->map, HB_TAG('r','p','h','f'));
530  indic_plan->pref.init (&plan->map, HB_TAG('p','r','e','f'));
531  indic_plan->blwf.init (&plan->map, HB_TAG('b','l','w','f'));
532  indic_plan->pstf.init (&plan->map, HB_TAG('p','s','t','f'));
533
534  for (unsigned int i = 0; i < ARRAY_LENGTH (indic_plan->mask_array); i++)
535    indic_plan->mask_array[i] = (indic_features[i].flags & F_GLOBAL) ?
536				 0 : plan->map.get_1_mask (indic_features[i].tag);
537
538  return indic_plan;
539}
540
541static void
542data_destroy_indic (void *data)
543{
544  free (data);
545}
546
547static indic_position_t
548consonant_position_from_face (const indic_shape_plan_t *indic_plan,
549			      const hb_codepoint_t glyphs[2],
550			      hb_face_t *face)
551{
552  /* For old-spec, the order of glyphs is Consonant,Virama,
553   * whereas for new-spec, it's Virama,Consonant.  However,
554   * some broken fonts (like Free Sans) simply copied lookups
555   * from old-spec to new-spec without modification.
556   * And oddly enough, Uniscribe seems to respect those lookups.
557   * Eg. in the sequence U+0924,U+094D,U+0930, Uniscribe finds
558   * base at 0.  The font however, only has lookups matching
559   * 930,94D in 'blwf', not the expected 94D,930 (with new-spec
560   * table).  As such, we simply match both sequences.  Seems
561   * to work. */
562  bool zero_context = indic_plan->is_old_spec ? false : true;
563  hb_codepoint_t glyphs_r[2] = {glyphs[1], glyphs[0]};
564  if (indic_plan->pref.would_substitute (glyphs  , 2, zero_context, face) ||
565      indic_plan->pref.would_substitute (glyphs_r, 2, zero_context, face))
566    return POS_POST_C;
567  if (indic_plan->blwf.would_substitute (glyphs  , 2, zero_context, face) ||
568      indic_plan->blwf.would_substitute (glyphs_r, 2, zero_context, face))
569    return POS_BELOW_C;
570  if (indic_plan->pstf.would_substitute (glyphs  , 2, zero_context, face) ||
571      indic_plan->pstf.would_substitute (glyphs_r, 2, zero_context, face))
572    return POS_POST_C;
573  return POS_BASE_C;
574}
575
576
577enum syllable_type_t {
578  consonant_syllable,
579  vowel_syllable,
580  standalone_cluster,
581  broken_cluster,
582  non_indic_cluster,
583};
584
585#include "hb-ot-shape-complex-indic-machine.hh"
586
587
588static void
589setup_masks_indic (const hb_ot_shape_plan_t *plan HB_UNUSED,
590		   hb_buffer_t              *buffer,
591		   hb_font_t                *font HB_UNUSED)
592{
593  HB_BUFFER_ALLOCATE_VAR (buffer, indic_category);
594  HB_BUFFER_ALLOCATE_VAR (buffer, indic_position);
595
596  /* We cannot setup masks here.  We save information about characters
597   * and setup masks later on in a pause-callback. */
598
599  unsigned int count = buffer->len;
600  for (unsigned int i = 0; i < count; i++)
601    set_indic_properties (buffer->info[i]);
602}
603
604static void
605setup_syllables (const hb_ot_shape_plan_t *plan HB_UNUSED,
606		 hb_font_t *font HB_UNUSED,
607		 hb_buffer_t *buffer)
608{
609  find_syllables (buffer);
610}
611
612static int
613compare_indic_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
614{
615  int a = pa->indic_position();
616  int b = pb->indic_position();
617
618  return a < b ? -1 : a == b ? 0 : +1;
619}
620
621
622
623static void
624update_consonant_positions (const hb_ot_shape_plan_t *plan,
625			    hb_font_t         *font,
626			    hb_buffer_t       *buffer)
627{
628  const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data;
629
630  hb_codepoint_t glyphs[2];
631  if (indic_plan->get_virama_glyph (font, &glyphs[0]))
632  {
633    hb_face_t *face = font->face;
634    unsigned int count = buffer->len;
635    for (unsigned int i = 0; i < count; i++)
636      if (buffer->info[i].indic_position() == POS_BASE_C) {
637	glyphs[1] = buffer->info[i].codepoint;
638	buffer->info[i].indic_position() = consonant_position_from_face (indic_plan, glyphs, face);
639      }
640  }
641}
642
643
644/* Rules from:
645 * https://www.microsoft.com/typography/otfntdev/devanot/shaping.aspx */
646
647static void
648initial_reordering_consonant_syllable (const hb_ot_shape_plan_t *plan,
649				       hb_face_t *face,
650				       hb_buffer_t *buffer,
651				       unsigned int start, unsigned int end)
652{
653  const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data;
654  hb_glyph_info_t *info = buffer->info;
655
656
657  /* 1. Find base consonant:
658   *
659   * The shaping engine finds the base consonant of the syllable, using the
660   * following algorithm: starting from the end of the syllable, move backwards
661   * until a consonant is found that does not have a below-base or post-base
662   * form (post-base forms have to follow below-base forms), or that is not a
663   * pre-base reordering Ra, or arrive at the first consonant. The consonant
664   * stopped at will be the base.
665   *
666   *   o If the syllable starts with Ra + Halant (in a script that has Reph)
667   *     and has more than one consonant, Ra is excluded from candidates for
668   *     base consonants.
669   */
670
671  unsigned int base = end;
672  bool has_reph = false;
673
674  {
675    /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
676     *    and has more than one consonant, Ra is excluded from candidates for
677     *    base consonants. */
678    unsigned int limit = start;
679    if (indic_plan->mask_array[RPHF] &&
680	start + 3 <= end &&
681	(
682	 (indic_plan->config->reph_mode == REPH_MODE_IMPLICIT && !is_joiner (info[start + 2])) ||
683	 (indic_plan->config->reph_mode == REPH_MODE_EXPLICIT && info[start + 2].indic_category() == OT_ZWJ)
684	))
685    {
686      /* See if it matches the 'rphf' feature. */
687      hb_codepoint_t glyphs[2] = {info[start].codepoint, info[start + 1].codepoint};
688      if (indic_plan->rphf.would_substitute (glyphs, ARRAY_LENGTH (glyphs), true, face))
689      {
690	limit += 2;
691	while (limit < end && is_joiner (info[limit]))
692	  limit++;
693	base = start;
694	has_reph = true;
695      }
696    } else if (indic_plan->config->reph_mode == REPH_MODE_LOG_REPHA && info[start].indic_category() == OT_Repha)
697    {
698	limit += 1;
699	while (limit < end && is_joiner (info[limit]))
700	  limit++;
701	base = start;
702	has_reph = true;
703    }
704
705    switch (indic_plan->config->base_pos)
706    {
707      default:
708        assert (false);
709	/* fallthrough */
710
711      case BASE_POS_LAST:
712      {
713	/* -> starting from the end of the syllable, move backwards */
714	unsigned int i = end;
715	bool seen_below = false;
716	do {
717	  i--;
718	  /* -> until a consonant is found */
719	  if (is_consonant (info[i]))
720	  {
721	    /* -> that does not have a below-base or post-base form
722	     * (post-base forms have to follow below-base forms), */
723	    if (info[i].indic_position() != POS_BELOW_C &&
724		(info[i].indic_position() != POS_POST_C || seen_below))
725	    {
726	      base = i;
727	      break;
728	    }
729	    if (info[i].indic_position() == POS_BELOW_C)
730	      seen_below = true;
731
732	    /* -> or that is not a pre-base reordering Ra,
733	     *
734	     * IMPLEMENTATION NOTES:
735	     *
736	     * Our pre-base reordering Ra's are marked POS_POST_C, so will be skipped
737	     * by the logic above already.
738	     */
739
740	    /* -> or arrive at the first consonant. The consonant stopped at will
741	     * be the base. */
742	    base = i;
743	  }
744	  else
745	  {
746	    /* A ZWJ after a Halant stops the base search, and requests an explicit
747	     * half form.
748	     * A ZWJ before a Halant, requests a subjoined form instead, and hence
749	     * search continues.  This is particularly important for Bengali
750	     * sequence Ra,H,Ya that should form Ya-Phalaa by subjoining Ya. */
751	    if (start < i &&
752		info[i].indic_category() == OT_ZWJ &&
753		info[i - 1].indic_category() == OT_H)
754	      break;
755	  }
756	} while (i > limit);
757      }
758      break;
759
760      case BASE_POS_FIRST:
761      {
762	/* In scripts without half forms (eg. Khmer), the first consonant is always the base. */
763
764	if (!has_reph)
765	  base = limit;
766
767	/* Find the last base consonant that is not blocked by ZWJ.  If there is
768	 * a ZWJ right before a base consonant, that would request a subjoined form. */
769	for (unsigned int i = limit; i < end; i++)
770	  if (is_consonant (info[i]) && info[i].indic_position() == POS_BASE_C)
771	  {
772	    if (limit < i && info[i - 1].indic_category() == OT_ZWJ)
773	      break;
774	    else
775	      base = i;
776	  }
777
778	/* Mark all subsequent consonants as below. */
779	for (unsigned int i = base + 1; i < end; i++)
780	  if (is_consonant (info[i]) && info[i].indic_position() == POS_BASE_C)
781	    info[i].indic_position() = POS_BELOW_C;
782      }
783      break;
784    }
785
786    /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
787     *    and has more than one consonant, Ra is excluded from candidates for
788     *    base consonants.
789     *
790     *  Only do this for unforced Reph. (ie. not for Ra,H,ZWJ. */
791    if (has_reph && base == start && limit - base <= 2) {
792      /* Have no other consonant, so Reph is not formed and Ra becomes base. */
793      has_reph = false;
794    }
795  }
796
797
798  /* 2. Decompose and reorder Matras:
799   *
800   * Each matra and any syllable modifier sign in the cluster are moved to the
801   * appropriate position relative to the consonant(s) in the cluster. The
802   * shaping engine decomposes two- or three-part matras into their constituent
803   * parts before any repositioning. Matra characters are classified by which
804   * consonant in a conjunct they have affinity for and are reordered to the
805   * following positions:
806   *
807   *   o Before first half form in the syllable
808   *   o After subjoined consonants
809   *   o After post-form consonant
810   *   o After main consonant (for above marks)
811   *
812   * IMPLEMENTATION NOTES:
813   *
814   * The normalize() routine has already decomposed matras for us, so we don't
815   * need to worry about that.
816   */
817
818
819  /* 3.  Reorder marks to canonical order:
820   *
821   * Adjacent nukta and halant or nukta and vedic sign are always repositioned
822   * if necessary, so that the nukta is first.
823   *
824   * IMPLEMENTATION NOTES:
825   *
826   * We don't need to do this: the normalize() routine already did this for us.
827   */
828
829
830  /* Reorder characters */
831
832  for (unsigned int i = start; i < base; i++)
833    info[i].indic_position() = MIN (POS_PRE_C, (indic_position_t) info[i].indic_position());
834
835  if (base < end)
836    info[base].indic_position() = POS_BASE_C;
837
838  /* Mark final consonants.  A final consonant is one appearing after a matra,
839   * like in Khmer. */
840  for (unsigned int i = base + 1; i < end; i++)
841    if (info[i].indic_category() == OT_M) {
842      for (unsigned int j = i + 1; j < end; j++)
843        if (is_consonant (info[j])) {
844	  info[j].indic_position() = POS_FINAL_C;
845	  break;
846	}
847      break;
848    }
849
850  /* Handle beginning Ra */
851  if (has_reph)
852    info[start].indic_position() = POS_RA_TO_BECOME_REPH;
853
854  /* For old-style Indic script tags, move the first post-base Halant after
855   * last consonant.  Only do this if there is *not* a Halant after last
856   * consonant.  Otherwise it becomes messy. */
857  if (indic_plan->is_old_spec) {
858    for (unsigned int i = base + 1; i < end; i++)
859      if (info[i].indic_category() == OT_H) {
860        unsigned int j;
861        for (j = end - 1; j > i; j--)
862	  if (is_consonant (info[j]) || info[j].indic_category() == OT_H)
863	    break;
864	if (info[j].indic_category() != OT_H && j > i) {
865	  /* Move Halant to after last consonant. */
866	  hb_glyph_info_t t = info[i];
867	  memmove (&info[i], &info[i + 1], (j - i) * sizeof (info[0]));
868	  info[j] = t;
869	}
870        break;
871      }
872  }
873
874  /* Attach misc marks to previous char to move with them. */
875  {
876    indic_position_t last_pos = POS_START;
877    for (unsigned int i = start; i < end; i++)
878    {
879      if ((FLAG (info[i].indic_category()) & (JOINER_FLAGS | FLAG (OT_N) | FLAG (OT_RS) | HALANT_OR_COENG_FLAGS)))
880      {
881	info[i].indic_position() = last_pos;
882	if (unlikely (info[i].indic_category() == OT_H &&
883		      info[i].indic_position() == POS_PRE_M))
884	{
885	  /*
886	   * Uniscribe doesn't move the Halant with Left Matra.
887	   * TEST: U+092B,U+093F,U+094DE
888	   * We follow.  This is important for the Sinhala
889	   * U+0DDA split matra since it decomposes to U+0DD9,U+0DCA
890	   * where U+0DD9 is a left matra and U+0DCA is the virama.
891	   * We don't want to move the virama with the left matra.
892	   * TEST: U+0D9A,U+0DDA
893	   */
894	  for (unsigned int j = i; j > start; j--)
895	    if (info[j - 1].indic_position() != POS_PRE_M) {
896	      info[i].indic_position() = info[j - 1].indic_position();
897	      break;
898	    }
899	}
900      } else if (info[i].indic_position() != POS_SMVD) {
901        last_pos = (indic_position_t) info[i].indic_position();
902      }
903    }
904  }
905  /* Re-attach ZWJ, ZWNJ, and halant to next char, for after-base consonants. */
906  {
907    unsigned int last_halant = end;
908    for (unsigned int i = base + 1; i < end; i++)
909      if (is_halant_or_coeng (info[i]))
910        last_halant = i;
911      else if (is_consonant (info[i])) {
912	for (unsigned int j = last_halant; j < i; j++)
913	  if (info[j].indic_position() != POS_SMVD)
914	    info[j].indic_position() = info[i].indic_position();
915      }
916  }
917
918  {
919    /* Things are out-of-control for post base positions, they may shuffle
920     * around like crazy, so merge clusters.  For pre-base stuff, we handle
921     * cluster issues in final reordering. */
922    buffer->merge_clusters (base, end);
923    /* Sit tight, rock 'n roll! */
924    hb_bubble_sort (info + start, end - start, compare_indic_order);
925    /* Find base again */
926    base = end;
927    for (unsigned int i = start; i < end; i++)
928      if (info[i].indic_position() == POS_BASE_C) {
929        base = i;
930	break;
931      }
932  }
933
934  /* Setup masks now */
935
936  {
937    hb_mask_t mask;
938
939    /* Reph */
940    for (unsigned int i = start; i < end && info[i].indic_position() == POS_RA_TO_BECOME_REPH; i++)
941      info[i].mask |= indic_plan->mask_array[RPHF];
942
943    /* Pre-base */
944    mask = indic_plan->mask_array[HALF];
945    for (unsigned int i = start; i < base; i++)
946      info[i].mask  |= mask;
947    /* Base */
948    mask = 0;
949    if (base < end)
950      info[base].mask |= mask;
951    /* Post-base */
952    mask = indic_plan->mask_array[BLWF] | indic_plan->mask_array[ABVF] | indic_plan->mask_array[PSTF];
953    for (unsigned int i = base + 1; i < end; i++)
954      info[i].mask  |= mask;
955  }
956
957  if (indic_plan->is_old_spec &&
958      buffer->props.script == HB_SCRIPT_DEVANAGARI)
959  {
960    /* Old-spec eye-lash Ra needs special handling.  From the
961     * spec:
962     *
963     * "The feature 'below-base form' is applied to consonants
964     * having below-base forms and following the base consonant.
965     * The exception is vattu, which may appear below half forms
966     * as well as below the base glyph. The feature 'below-base
967     * form' will be applied to all such occurrences of Ra as well."
968     *
969     * Test case: U+0924,U+094D,U+0930,U+094d,U+0915
970     * with Sanskrit 2003 font.
971     *
972     * However, note that Ra,Halant,ZWJ is the correct way to
973     * request eyelash form of Ra, so we wouldbn't inhibit it
974     * in that sequence.
975     *
976     * Test case: U+0924,U+094D,U+0930,U+094d,U+200D,U+0915
977     */
978    for (unsigned int i = start; i + 1 < base; i++)
979      if (info[i  ].indic_category() == OT_Ra &&
980	  info[i+1].indic_category() == OT_H  &&
981	  (i + 2 == base ||
982	   info[i+2].indic_category() != OT_ZWJ))
983      {
984	info[i  ].mask |= indic_plan->mask_array[BLWF];
985	info[i+1].mask |= indic_plan->mask_array[BLWF];
986      }
987  }
988
989  if (indic_plan->mask_array[PREF] && base + 2 < end)
990  {
991    /* Find a Halant,Ra sequence and mark it for pre-base reordering processing. */
992    for (unsigned int i = base + 1; i + 1 < end; i++) {
993      hb_codepoint_t glyphs[2] = {info[i].codepoint, info[i + 1].codepoint};
994      if (indic_plan->pref.would_substitute (glyphs, ARRAY_LENGTH (glyphs), true, face))
995      {
996	info[i++].mask |= indic_plan->mask_array[PREF];
997	info[i++].mask |= indic_plan->mask_array[PREF];
998
999	/* Mark the subsequent stuff with 'cfar'.  Used in Khmer.
1000	 * Read the feature spec.
1001	 * This allows distinguishing the following cases with MS Khmer fonts:
1002	 * U+1784,U+17D2,U+179A,U+17D2,U+1782
1003	 * U+1784,U+17D2,U+1782,U+17D2,U+179A
1004	 */
1005	for (; i < end; i++)
1006	  info[i].mask |= indic_plan->mask_array[CFAR];
1007
1008	break;
1009      }
1010    }
1011  }
1012
1013  /* Apply ZWJ/ZWNJ effects */
1014  for (unsigned int i = start + 1; i < end; i++)
1015    if (is_joiner (info[i])) {
1016      bool non_joiner = info[i].indic_category() == OT_ZWNJ;
1017      unsigned int j = i;
1018
1019      do {
1020	j--;
1021
1022	/* ZWJ/ZWNJ should disable CJCT.  They do that by simply
1023	 * being there, since we don't skip them for the CJCT
1024	 * feature (ie. F_MANUAL_ZWJ) */
1025
1026	/* A ZWNJ disables HALF. */
1027	if (non_joiner)
1028	  info[j].mask &= ~indic_plan->mask_array[HALF];
1029
1030      } while (j > start && !is_consonant (info[j]));
1031    }
1032}
1033
1034
1035static void
1036initial_reordering_vowel_syllable (const hb_ot_shape_plan_t *plan,
1037				   hb_face_t *face,
1038				   hb_buffer_t *buffer,
1039				   unsigned int start, unsigned int end)
1040{
1041  /* We made the vowels look like consonants.  So let's call the consonant logic! */
1042  initial_reordering_consonant_syllable (plan, face, buffer, start, end);
1043}
1044
1045static void
1046initial_reordering_standalone_cluster (const hb_ot_shape_plan_t *plan,
1047				       hb_face_t *face,
1048				       hb_buffer_t *buffer,
1049				       unsigned int start, unsigned int end)
1050{
1051  /* We treat NBSP/dotted-circle as if they are consonants, so we should just chain.
1052   * Only if not in compatibility mode that is... */
1053
1054  if (hb_options ().uniscribe_bug_compatible)
1055  {
1056    /* For dotted-circle, this is what Uniscribe does:
1057     * If dotted-circle is the last glyph, it just does nothing.
1058     * Ie. It doesn't form Reph. */
1059    if (buffer->info[end - 1].indic_category() == OT_DOTTEDCIRCLE)
1060      return;
1061  }
1062
1063  initial_reordering_consonant_syllable (plan, face, buffer, start, end);
1064}
1065
1066static void
1067initial_reordering_broken_cluster (const hb_ot_shape_plan_t *plan,
1068				   hb_face_t *face,
1069				   hb_buffer_t *buffer,
1070				   unsigned int start, unsigned int end)
1071{
1072  /* We already inserted dotted-circles, so just call the standalone_cluster. */
1073  initial_reordering_standalone_cluster (plan, face, buffer, start, end);
1074}
1075
1076static void
1077initial_reordering_non_indic_cluster (const hb_ot_shape_plan_t *plan HB_UNUSED,
1078				      hb_face_t *face HB_UNUSED,
1079				      hb_buffer_t *buffer HB_UNUSED,
1080				      unsigned int start HB_UNUSED, unsigned int end HB_UNUSED)
1081{
1082  /* Nothing to do right now.  If we ever switch to using the output
1083   * buffer in the reordering process, we'd need to next_glyph() here. */
1084}
1085
1086
1087static void
1088initial_reordering_syllable (const hb_ot_shape_plan_t *plan,
1089			     hb_face_t *face,
1090			     hb_buffer_t *buffer,
1091			     unsigned int start, unsigned int end)
1092{
1093  syllable_type_t syllable_type = (syllable_type_t) (buffer->info[start].syllable() & 0x0F);
1094  switch (syllable_type) {
1095  case consonant_syllable:	initial_reordering_consonant_syllable (plan, face, buffer, start, end); return;
1096  case vowel_syllable:		initial_reordering_vowel_syllable     (plan, face, buffer, start, end); return;
1097  case standalone_cluster:	initial_reordering_standalone_cluster (plan, face, buffer, start, end); return;
1098  case broken_cluster:		initial_reordering_broken_cluster     (plan, face, buffer, start, end); return;
1099  case non_indic_cluster:	initial_reordering_non_indic_cluster  (plan, face, buffer, start, end); return;
1100  }
1101}
1102
1103static inline void
1104insert_dotted_circles (const hb_ot_shape_plan_t *plan HB_UNUSED,
1105		       hb_font_t *font,
1106		       hb_buffer_t *buffer)
1107{
1108  /* Note: This loop is extra overhead, but should not be measurable. */
1109  bool has_broken_syllables = false;
1110  unsigned int count = buffer->len;
1111  for (unsigned int i = 0; i < count; i++)
1112    if ((buffer->info[i].syllable() & 0x0F) == broken_cluster) {
1113      has_broken_syllables = true;
1114      break;
1115    }
1116  if (likely (!has_broken_syllables))
1117    return;
1118
1119
1120  hb_codepoint_t dottedcircle_glyph;
1121  if (!font->get_glyph (0x25CC, 0, &dottedcircle_glyph))
1122    return;
1123
1124  hb_glyph_info_t dottedcircle = {0};
1125  dottedcircle.codepoint = 0x25CC;
1126  set_indic_properties (dottedcircle);
1127  dottedcircle.codepoint = dottedcircle_glyph;
1128
1129  buffer->clear_output ();
1130
1131  buffer->idx = 0;
1132  unsigned int last_syllable = 0;
1133  while (buffer->idx < buffer->len)
1134  {
1135    unsigned int syllable = buffer->cur().syllable();
1136    syllable_type_t syllable_type = (syllable_type_t) (syllable & 0x0F);
1137    if (unlikely (last_syllable != syllable && syllable_type == broken_cluster))
1138    {
1139      last_syllable = syllable;
1140
1141      hb_glyph_info_t info = dottedcircle;
1142      info.cluster = buffer->cur().cluster;
1143      info.mask = buffer->cur().mask;
1144      info.syllable() = buffer->cur().syllable();
1145
1146      /* Insert dottedcircle after possible Repha. */
1147      while (buffer->idx < buffer->len &&
1148	     last_syllable == buffer->cur().syllable() &&
1149	     buffer->cur().indic_category() == OT_Repha)
1150        buffer->next_glyph ();
1151
1152      buffer->output_info (info);
1153    }
1154    else
1155      buffer->next_glyph ();
1156  }
1157
1158  buffer->swap_buffers ();
1159}
1160
1161static void
1162initial_reordering (const hb_ot_shape_plan_t *plan,
1163		    hb_font_t *font,
1164		    hb_buffer_t *buffer)
1165{
1166  update_consonant_positions (plan, font, buffer);
1167  insert_dotted_circles (plan, font, buffer);
1168
1169  hb_glyph_info_t *info = buffer->info;
1170  unsigned int count = buffer->len;
1171  if (unlikely (!count)) return;
1172  unsigned int last = 0;
1173  unsigned int last_syllable = info[0].syllable();
1174  for (unsigned int i = 1; i < count; i++)
1175    if (last_syllable != info[i].syllable()) {
1176      initial_reordering_syllable (plan, font->face, buffer, last, i);
1177      last = i;
1178      last_syllable = info[last].syllable();
1179    }
1180  initial_reordering_syllable (plan, font->face, buffer, last, count);
1181}
1182
1183static void
1184final_reordering_syllable (const hb_ot_shape_plan_t *plan,
1185			   hb_buffer_t *buffer,
1186			   unsigned int start, unsigned int end)
1187{
1188  const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data;
1189  hb_glyph_info_t *info = buffer->info;
1190
1191  /* 4. Final reordering:
1192   *
1193   * After the localized forms and basic shaping forms GSUB features have been
1194   * applied (see below), the shaping engine performs some final glyph
1195   * reordering before applying all the remaining font features to the entire
1196   * cluster.
1197   */
1198
1199  /* Find base again */
1200  unsigned int base;
1201  for (base = start; base < end; base++)
1202    if (info[base].indic_position() >= POS_BASE_C) {
1203      if (start < base && info[base].indic_position() > POS_BASE_C)
1204        base--;
1205      break;
1206    }
1207  if (base == end && start < base &&
1208      info[base - 1].indic_category() != OT_ZWJ)
1209    base--;
1210  while (start < base &&
1211	 (info[base].indic_category() == OT_H ||
1212	  info[base].indic_category() == OT_N))
1213    base--;
1214
1215
1216  /*   o Reorder matras:
1217   *
1218   *     If a pre-base matra character had been reordered before applying basic
1219   *     features, the glyph can be moved closer to the main consonant based on
1220   *     whether half-forms had been formed. Actual position for the matra is
1221   *     defined as “after last standalone halant glyph, after initial matra
1222   *     position and before the main consonant”. If ZWJ or ZWNJ follow this
1223   *     halant, position is moved after it.
1224   */
1225
1226  if (start + 1 < end && start < base) /* Otherwise there can't be any pre-base matra characters. */
1227  {
1228    /* If we lost track of base, alas, position before last thingy. */
1229    unsigned int new_pos = base == end ? base - 2 : base - 1;
1230
1231    /* Malayalam / Tamil do not have "half" forms or explicit virama forms.
1232     * The glyphs formed by 'half' are Chillus or ligated explicit viramas.
1233     * We want to position matra after them.
1234     */
1235    if (buffer->props.script != HB_SCRIPT_MALAYALAM && buffer->props.script != HB_SCRIPT_TAMIL)
1236    {
1237      while (new_pos > start &&
1238	     !(is_one_of (info[new_pos], (FLAG (OT_M) | FLAG (OT_H) | FLAG (OT_Coeng)))))
1239	new_pos--;
1240
1241      /* If we found no Halant we are done.
1242       * Otherwise only proceed if the Halant does
1243       * not belong to the Matra itself! */
1244      if (is_halant_or_coeng (info[new_pos]) &&
1245	  info[new_pos].indic_position() != POS_PRE_M)
1246      {
1247	/* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
1248	if (new_pos + 1 < end && is_joiner (info[new_pos + 1]))
1249	  new_pos++;
1250      }
1251      else
1252        new_pos = start; /* No move. */
1253    }
1254
1255    if (start < new_pos && info[new_pos].indic_position () != POS_PRE_M)
1256    {
1257      /* Now go see if there's actually any matras... */
1258      for (unsigned int i = new_pos; i > start; i--)
1259	if (info[i - 1].indic_position () == POS_PRE_M)
1260	{
1261	  unsigned int old_pos = i - 1;
1262	  hb_glyph_info_t tmp = info[old_pos];
1263	  memmove (&info[old_pos], &info[old_pos + 1], (new_pos - old_pos) * sizeof (info[0]));
1264	  info[new_pos] = tmp;
1265	  if (old_pos < base && base <= new_pos) /* Shouldn't actually happen. */
1266	    base--;
1267	  new_pos--;
1268	}
1269      buffer->merge_clusters (new_pos, MIN (end, base + 1));
1270    } else {
1271      for (unsigned int i = start; i < base; i++)
1272	if (info[i].indic_position () == POS_PRE_M) {
1273	  buffer->merge_clusters (i, MIN (end, base + 1));
1274	  break;
1275	}
1276    }
1277  }
1278
1279
1280  /*   o Reorder reph:
1281   *
1282   *     Reph’s original position is always at the beginning of the syllable,
1283   *     (i.e. it is not reordered at the character reordering stage). However,
1284   *     it will be reordered according to the basic-forms shaping results.
1285   *     Possible positions for reph, depending on the script, are; after main,
1286   *     before post-base consonant forms, and after post-base consonant forms.
1287   */
1288
1289  /* If there's anything after the Ra that has the REPH pos, it ought to be halant.
1290   * Which means that the font has failed to ligate the Reph.  In which case, we
1291   * shouldn't move. */
1292  if (start + 1 < end &&
1293      info[start].indic_position() == POS_RA_TO_BECOME_REPH &&
1294      info[start + 1].indic_position() != POS_RA_TO_BECOME_REPH)
1295  {
1296    unsigned int new_reph_pos;
1297    reph_position_t reph_pos = indic_plan->config->reph_pos;
1298
1299    /* XXX Figure out old behavior too */
1300
1301    /*       1. If reph should be positioned after post-base consonant forms,
1302     *          proceed to step 5.
1303     */
1304    if (reph_pos == REPH_POS_AFTER_POST)
1305    {
1306      goto reph_step_5;
1307    }
1308
1309    /*       2. If the reph repositioning class is not after post-base: target
1310     *          position is after the first explicit halant glyph between the
1311     *          first post-reph consonant and last main consonant. If ZWJ or ZWNJ
1312     *          are following this halant, position is moved after it. If such
1313     *          position is found, this is the target position. Otherwise,
1314     *          proceed to the next step.
1315     *
1316     *          Note: in old-implementation fonts, where classifications were
1317     *          fixed in shaping engine, there was no case where reph position
1318     *          will be found on this step.
1319     */
1320    {
1321      new_reph_pos = start + 1;
1322      while (new_reph_pos < base && !is_halant_or_coeng (info[new_reph_pos]))
1323	new_reph_pos++;
1324
1325      if (new_reph_pos < base && is_halant_or_coeng (info[new_reph_pos]))
1326      {
1327	/* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */
1328	if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1]))
1329	  new_reph_pos++;
1330	goto reph_move;
1331      }
1332    }
1333
1334    /*       3. If reph should be repositioned after the main consonant: find the
1335     *          first consonant not ligated with main, or find the first
1336     *          consonant that is not a potential pre-base reordering Ra.
1337     */
1338    if (reph_pos == REPH_POS_AFTER_MAIN)
1339    {
1340      new_reph_pos = base;
1341      /* XXX Skip potential pre-base reordering Ra. */
1342      while (new_reph_pos + 1 < end && info[new_reph_pos + 1].indic_position() <= POS_AFTER_MAIN)
1343	new_reph_pos++;
1344      if (new_reph_pos < end)
1345        goto reph_move;
1346    }
1347
1348    /*       4. If reph should be positioned before post-base consonant, find
1349     *          first post-base classified consonant not ligated with main. If no
1350     *          consonant is found, the target position should be before the
1351     *          first matra, syllable modifier sign or vedic sign.
1352     */
1353    /* This is our take on what step 4 is trying to say (and failing, BADLY). */
1354    if (reph_pos == REPH_POS_AFTER_SUB)
1355    {
1356      new_reph_pos = base;
1357      while (new_reph_pos < end &&
1358	     !( FLAG (info[new_reph_pos + 1].indic_position()) & (FLAG (POS_POST_C) | FLAG (POS_AFTER_POST) | FLAG (POS_SMVD))))
1359	new_reph_pos++;
1360      if (new_reph_pos < end)
1361        goto reph_move;
1362    }
1363
1364    /*       5. If no consonant is found in steps 3 or 4, move reph to a position
1365     *          immediately before the first post-base matra, syllable modifier
1366     *          sign or vedic sign that has a reordering class after the intended
1367     *          reph position. For example, if the reordering position for reph
1368     *          is post-main, it will skip above-base matras that also have a
1369     *          post-main position.
1370     */
1371    reph_step_5:
1372    {
1373      /* Copied from step 2. */
1374      new_reph_pos = start + 1;
1375      while (new_reph_pos < base && !is_halant_or_coeng (info[new_reph_pos]))
1376	new_reph_pos++;
1377
1378      if (new_reph_pos < base && is_halant_or_coeng (info[new_reph_pos]))
1379      {
1380	/* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */
1381	if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1]))
1382	  new_reph_pos++;
1383	goto reph_move;
1384      }
1385    }
1386
1387    /*       6. Otherwise, reorder reph to the end of the syllable.
1388     */
1389    {
1390      new_reph_pos = end - 1;
1391      while (new_reph_pos > start && info[new_reph_pos].indic_position() == POS_SMVD)
1392	new_reph_pos--;
1393
1394      /*
1395       * If the Reph is to be ending up after a Matra,Halant sequence,
1396       * position it before that Halant so it can interact with the Matra.
1397       * However, if it's a plain Consonant,Halant we shouldn't do that.
1398       * Uniscribe doesn't do this.
1399       * TEST: U+0930,U+094D,U+0915,U+094B,U+094D
1400       */
1401      if (!hb_options ().uniscribe_bug_compatible &&
1402	  unlikely (is_halant_or_coeng (info[new_reph_pos]))) {
1403	for (unsigned int i = base + 1; i < new_reph_pos; i++)
1404	  if (info[i].indic_category() == OT_M) {
1405	    /* Ok, got it. */
1406	    new_reph_pos--;
1407	  }
1408      }
1409      goto reph_move;
1410    }
1411
1412    reph_move:
1413    {
1414      /* Yay, one big cluster! Merge before moving. */
1415      buffer->merge_clusters (start, end);
1416
1417      /* Move */
1418      hb_glyph_info_t reph = info[start];
1419      memmove (&info[start], &info[start + 1], (new_reph_pos - start) * sizeof (info[0]));
1420      info[new_reph_pos] = reph;
1421      if (start < base && base <= new_reph_pos)
1422	base--;
1423    }
1424  }
1425
1426
1427  /*   o Reorder pre-base reordering consonants:
1428   *
1429   *     If a pre-base reordering consonant is found, reorder it according to
1430   *     the following rules:
1431   */
1432
1433  if (indic_plan->mask_array[PREF] && base + 1 < end) /* Otherwise there can't be any pre-base reordering Ra. */
1434  {
1435    for (unsigned int i = base + 1; i < end; i++)
1436      if ((info[i].mask & indic_plan->mask_array[PREF]) != 0)
1437      {
1438	/*       1. Only reorder a glyph produced by substitution during application
1439	 *          of the <pref> feature. (Note that a font may shape a Ra consonant with
1440	 *          the feature generally but block it in certain contexts.)
1441	 */
1442	if (i + 1 == end || (info[i + 1].mask & indic_plan->mask_array[PREF]) == 0)
1443	{
1444	  /*
1445	   *       2. Try to find a target position the same way as for pre-base matra.
1446	   *          If it is found, reorder pre-base consonant glyph.
1447	   *
1448	   *       3. If position is not found, reorder immediately before main
1449	   *          consonant.
1450	   */
1451
1452	  unsigned int new_pos = base;
1453	  /* Malayalam / Tamil do not have "half" forms or explicit virama forms.
1454	   * The glyphs formed by 'half' are Chillus or ligated explicit viramas.
1455	   * We want to position matra after them.
1456	   */
1457	  if (buffer->props.script != HB_SCRIPT_MALAYALAM && buffer->props.script != HB_SCRIPT_TAMIL)
1458	  {
1459	    while (new_pos > start &&
1460		   !(is_one_of (info[new_pos - 1], FLAG(OT_M) | HALANT_OR_COENG_FLAGS)))
1461	      new_pos--;
1462
1463	    /* In Khmer coeng model, a V,Ra can go *after* matras.  If it goes after a
1464	     * split matra, it should be reordered to *before* the left part of such matra. */
1465	    if (new_pos > start && info[new_pos - 1].indic_category() == OT_M)
1466	    {
1467	      unsigned int old_pos = i;
1468	      for (unsigned int i = base + 1; i < old_pos; i++)
1469		if (info[i].indic_category() == OT_M)
1470		{
1471		  new_pos--;
1472		  break;
1473		}
1474	    }
1475	  }
1476
1477	  if (new_pos > start && is_halant_or_coeng (info[new_pos - 1]))
1478	  {
1479	    /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
1480	    if (new_pos < end && is_joiner (info[new_pos]))
1481	      new_pos++;
1482	  }
1483
1484	  {
1485	    unsigned int old_pos = i;
1486	    buffer->merge_clusters (new_pos, old_pos + 1);
1487	    hb_glyph_info_t tmp = info[old_pos];
1488	    memmove (&info[new_pos + 1], &info[new_pos], (old_pos - new_pos) * sizeof (info[0]));
1489	    info[new_pos] = tmp;
1490	    if (new_pos <= base && base < old_pos)
1491	      base++;
1492	  }
1493	}
1494
1495        break;
1496      }
1497  }
1498
1499
1500  /* Apply 'init' to the Left Matra if it's a word start. */
1501  if (info[start].indic_position () == POS_PRE_M &&
1502      (!start ||
1503       !(FLAG (_hb_glyph_info_get_general_category (&info[start - 1])) &
1504	 FLAG_RANGE (HB_UNICODE_GENERAL_CATEGORY_FORMAT, HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK))))
1505    info[start].mask |= indic_plan->mask_array[INIT];
1506
1507
1508  /*
1509   * Finish off the clusters and go home!
1510   */
1511  if (hb_options ().uniscribe_bug_compatible)
1512  {
1513    /* Uniscribe merges the entire cluster.
1514     * This means, half forms are submerged into the main consonants cluster.
1515     * This is unnecessary, and makes cursor positioning harder, but that's what
1516     * Uniscribe does. */
1517    buffer->merge_clusters (start, end);
1518  }
1519}
1520
1521
1522static void
1523final_reordering (const hb_ot_shape_plan_t *plan,
1524		  hb_font_t *font HB_UNUSED,
1525		  hb_buffer_t *buffer)
1526{
1527  unsigned int count = buffer->len;
1528  if (unlikely (!count)) return;
1529
1530  hb_glyph_info_t *info = buffer->info;
1531  unsigned int last = 0;
1532  unsigned int last_syllable = info[0].syllable();
1533  for (unsigned int i = 1; i < count; i++)
1534    if (last_syllable != info[i].syllable()) {
1535      final_reordering_syllable (plan, buffer, last, i);
1536      last = i;
1537      last_syllable = info[last].syllable();
1538    }
1539  final_reordering_syllable (plan, buffer, last, count);
1540
1541  /* Zero syllables now... */
1542  for (unsigned int i = 0; i < count; i++)
1543    info[i].syllable() = 0;
1544
1545  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category);
1546  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position);
1547}
1548
1549
1550static hb_ot_shape_normalization_mode_t
1551normalization_preference_indic (const hb_segment_properties_t *props HB_UNUSED)
1552{
1553  return HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT;
1554}
1555
1556static bool
1557decompose_indic (const hb_ot_shape_normalize_context_t *c,
1558		 hb_codepoint_t  ab,
1559		 hb_codepoint_t *a,
1560		 hb_codepoint_t *b)
1561{
1562  switch (ab)
1563  {
1564    /* Don't decompose these. */
1565    case 0x0931  : return false;
1566    case 0x0B94  : return false;
1567
1568
1569    /*
1570     * Decompose split matras that don't have Unicode decompositions.
1571     */
1572
1573    case 0x0F77  : *a = 0x0FB2; *b= 0x0F81; return true;
1574    case 0x0F79  : *a = 0x0FB3; *b= 0x0F81; return true;
1575    case 0x17BE  : *a = 0x17C1; *b= 0x17BE; return true;
1576    case 0x17BF  : *a = 0x17C1; *b= 0x17BF; return true;
1577    case 0x17C0  : *a = 0x17C1; *b= 0x17C0; return true;
1578    case 0x17C4  : *a = 0x17C1; *b= 0x17C4; return true;
1579    case 0x17C5  : *a = 0x17C1; *b= 0x17C5; return true;
1580    case 0x1925  : *a = 0x1920; *b= 0x1923; return true;
1581    case 0x1926  : *a = 0x1920; *b= 0x1924; return true;
1582    case 0x1B3C  : *a = 0x1B42; *b= 0x1B3C; return true;
1583    case 0x1112E  : *a = 0x11127; *b= 0x11131; return true;
1584    case 0x1112F  : *a = 0x11127; *b= 0x11132; return true;
1585#if 0
1586    /* This one has no decomposition in Unicode, but needs no decomposition either. */
1587    /* case 0x0AC9  : return false; */
1588    case 0x0B57  : *a = no decomp, -> RIGHT; return true;
1589    case 0x1C29  : *a = no decomp, -> LEFT; return true;
1590    case 0xA9C0  : *a = no decomp, -> RIGHT; return true;
1591    case 0x111BF  : *a = no decomp, -> ABOVE; return true;
1592#endif
1593  }
1594
1595  if ((ab == 0x0DDA || hb_in_range<hb_codepoint_t> (ab, 0x0DDC, 0x0DDE)))
1596  {
1597    /*
1598     * Sinhala split matras...  Let the fun begin.
1599     *
1600     * These four characters have Unicode decompositions.  However, Uniscribe
1601     * decomposes them "Khmer-style", that is, it uses the character itself to
1602     * get the second half.  The first half of all four decompositions is always
1603     * U+0DD9.
1604     *
1605     * Now, there are buggy fonts, namely, the widely used lklug.ttf, that are
1606     * broken with Uniscribe.  But we need to support them.  As such, we only
1607     * do the Uniscribe-style decomposition if the character is transformed into
1608     * its "sec.half" form by the 'pstf' feature.  Otherwise, we fall back to
1609     * Unicode decomposition.
1610     *
1611     * Note that we can't unconditionally use Unicode decomposition.  That would
1612     * break some other fonts, that are designed to work with Uniscribe, and
1613     * don't have positioning features for the Unicode-style decomposition.
1614     *
1615     * Argh...
1616     *
1617     * The Uniscribe behavior is now documented in the newly published Sinhala
1618     * spec in 2012:
1619     *
1620     *   http://www.microsoft.com/typography/OpenTypeDev/sinhala/intro.htm#shaping
1621     */
1622
1623    const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) c->plan->data;
1624
1625    hb_codepoint_t glyph;
1626
1627    if (hb_options ().uniscribe_bug_compatible ||
1628	(c->font->get_glyph (ab, 0, &glyph) &&
1629	 indic_plan->pstf.would_substitute (&glyph, 1, true, c->font->face)))
1630    {
1631      /* Ok, safe to use Uniscribe-style decomposition. */
1632      *a = 0x0DD9;
1633      *b = ab;
1634      return true;
1635    }
1636  }
1637
1638  return c->unicode->decompose (ab, a, b);
1639}
1640
1641static bool
1642compose_indic (const hb_ot_shape_normalize_context_t *c,
1643	       hb_codepoint_t  a,
1644	       hb_codepoint_t  b,
1645	       hb_codepoint_t *ab)
1646{
1647  /* Avoid recomposing split matras. */
1648  if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (c->unicode->general_category (a)))
1649    return false;
1650
1651  /* Composition-exclusion exceptions that we want to recompose. */
1652  if (a == 0x09AF && b == 0x09BC) { *ab = 0x09DF; return true; }
1653
1654  return c->unicode->compose (a, b, ab);
1655}
1656
1657
1658const hb_ot_complex_shaper_t _hb_ot_complex_shaper_indic =
1659{
1660  "indic",
1661  collect_features_indic,
1662  override_features_indic,
1663  data_create_indic,
1664  data_destroy_indic,
1665  NULL, /* preprocess_text */
1666  normalization_preference_indic,
1667  decompose_indic,
1668  compose_indic,
1669  setup_masks_indic,
1670  HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE,
1671  false, /* fallback_position */
1672};
1673