Coverage Report

Created: 2026-11-10 07:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/harfbuzz/src/hb-ot-shaper-indic.cc
Line
Count
Source
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.hh"
28
29
#ifndef HB_NO_OT_SHAPE
30
31
#include "hb-ot-shaper-indic.hh"
32
#include "hb-ot-shaper-indic-machine.hh"
33
#include "hb-ot-shaper-vowel-constraints.hh"
34
#include "hb-ot-layout.hh"
35
36
37
/*
38
 * Indic shaper.
39
 */
40
41
42
static inline void
43
set_indic_properties (hb_glyph_info_t &info)
44
0
{
45
0
  hb_codepoint_t u = info.codepoint;
46
0
  unsigned int type = hb_indic_get_categories (u);
47
48
0
  info.indic_category() = (indic_category_t) (type & 0xFFu);
49
0
  info.indic_position() = (indic_position_t) (type >> 8);
50
0
}
51
52
53
static inline bool
54
is_one_of (const hb_glyph_info_t &info, unsigned int flags)
55
0
{
56
  /* If it ligated, all bets are off. */
57
0
  if (_hb_glyph_info_ligated (&info)) return false;
58
0
  return !!(FLAG_UNSAFE (info.indic_category()) & flags);
59
0
}
60
61
/* Note:
62
 *
63
 * We treat Vowels and placeholders as if they were consonants.  This is safe because Vowels
64
 * cannot happen in a consonant syllable.  The plus side however is, we can call the
65
 * consonant syllable logic from the vowel syllable function and get it all right!
66
 *
67
 * Keep in sync with consonant_categories in the generator. */
68
0
#define CONSONANT_FLAGS_INDIC (FLAG (I_Cat(C)) | FLAG (I_Cat(CS)) | FLAG (I_Cat(Ra)) | FLAG (I_Cat(CM)) | FLAG (I_Cat(V)) | FLAG (I_Cat(PLACEHOLDER)) | FLAG (I_Cat(DOTTEDCIRCLE)))
69
70
static inline bool
71
is_consonant (const hb_glyph_info_t &info)
72
0
{
73
0
  return is_one_of (info, CONSONANT_FLAGS_INDIC);
74
0
}
75
76
0
#define JOINER_FLAGS (FLAG (I_Cat(ZWJ)) | FLAG (I_Cat(ZWNJ)))
77
78
static inline bool
79
is_joiner (const hb_glyph_info_t &info)
80
0
{
81
0
  return is_one_of (info, JOINER_FLAGS);
82
0
}
83
84
static inline bool
85
is_halant (const hb_glyph_info_t &info)
86
0
{
87
0
  return is_one_of (info, FLAG (I_Cat(H)));
88
0
}
89
90
struct hb_indic_would_substitute_feature_t
91
{
92
  void init (const hb_ot_map_t *map, hb_tag_t feature_tag, bool zero_context_)
93
0
  {
94
0
    zero_context = zero_context_;
95
0
    lookups = map->get_stage_lookups (0/*GSUB*/,
96
0
              map->get_feature_stage (0/*GSUB*/, feature_tag));
97
0
  }
98
99
  bool would_substitute (const hb_codepoint_t *glyphs,
100
       unsigned int          glyphs_count,
101
       hb_face_t            *face,
102
       hb_buffer_t          *buffer) const
103
0
  {
104
0
    if (unlikely (!buffer->successful)) return false;
105
0
    for (const auto &lookup : lookups)
106
0
      if (hb_ot_layout_lookup_would_substitute_with_buffer (face, lookup.index,
107
0
                  glyphs, glyphs_count,
108
0
                  zero_context, buffer))
109
0
  return true;
110
0
    return false;
111
0
  }
112
113
  private:
114
  hb_array_t<const hb_ot_map_t::lookup_map_t> lookups;
115
  bool zero_context;
116
};
117
118
119
/*
120
 * Indic configurations.  Note that we do not want to keep every single script-specific
121
 * behavior in these tables necessarily.  This should mainly be used for per-script
122
 * properties that are cheaper keeping here, than in the code.  Ie. if, say, one and
123
 * only one script has an exception, that one script can be if'ed directly in the code,
124
 * instead of adding a new flag in these structs.
125
 */
126
127
enum reph_position_t {
128
  REPH_POS_AFTER_MAIN  = POS_AFTER_MAIN,
129
  REPH_POS_BEFORE_SUB  = POS_BEFORE_SUB,
130
  REPH_POS_AFTER_SUB   = POS_AFTER_SUB,
131
  REPH_POS_BEFORE_POST = POS_BEFORE_POST,
132
  REPH_POS_AFTER_POST  = POS_AFTER_POST
133
};
134
enum reph_mode_t {
135
  REPH_MODE_IMPLICIT,  /* Reph formed out of initial Ra,H sequence. */
136
  REPH_MODE_EXPLICIT,  /* Reph formed out of initial Ra,H,ZWJ sequence. */
137
  REPH_MODE_LOG_REPHA  /* Encoded Repha character, needs reordering. */
138
};
139
enum blwf_mode_t {
140
  BLWF_MODE_PRE_AND_POST, /* Below-forms feature applied to pre-base and post-base. */
141
  BLWF_MODE_POST_ONLY     /* Below-forms feature applied to post-base only. */
142
};
143
struct indic_config_t
144
{
145
  hb_script_t     script;
146
  bool            has_old_spec;
147
  hb_codepoint_t  virama;
148
  reph_position_t reph_pos;
149
  reph_mode_t     reph_mode;
150
  blwf_mode_t     blwf_mode;
151
};
152
153
static const indic_config_t indic_configs[] =
154
{
155
  /* Default.  Should be first. */
156
  {HB_SCRIPT_INVALID, false,      0,REPH_POS_BEFORE_POST,REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST},
157
  {HB_SCRIPT_DEVANAGARI,true, 0x094Du,REPH_POS_BEFORE_POST,REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST},
158
  {HB_SCRIPT_BENGALI, true, 0x09CDu,REPH_POS_AFTER_SUB,  REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST},
159
  {HB_SCRIPT_GURMUKHI,  true, 0x0A4Du,REPH_POS_BEFORE_SUB, REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST},
160
  {HB_SCRIPT_GUJARATI,  true, 0x0ACDu,REPH_POS_BEFORE_POST,REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST},
161
  {HB_SCRIPT_ORIYA, true, 0x0B4Du,REPH_POS_AFTER_MAIN, REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST},
162
  {HB_SCRIPT_TAMIL, true, 0x0BCDu,REPH_POS_AFTER_POST, REPH_MODE_IMPLICIT, BLWF_MODE_PRE_AND_POST},
163
  {HB_SCRIPT_TELUGU,  true, 0x0C4Du,REPH_POS_AFTER_POST, REPH_MODE_EXPLICIT, BLWF_MODE_POST_ONLY},
164
  {HB_SCRIPT_KANNADA, true, 0x0CCDu,REPH_POS_AFTER_POST, REPH_MODE_IMPLICIT, BLWF_MODE_POST_ONLY},
165
  {HB_SCRIPT_MALAYALAM, true, 0x0D4Du,REPH_POS_AFTER_MAIN, REPH_MODE_LOG_REPHA,BLWF_MODE_PRE_AND_POST},
166
};
167
168
169
static const hb_ot_map_feature_t
170
indic_features[] =
171
{
172
  /*
173
   * Basic features.
174
   * These features are applied in order, one at a time, after initial_reordering,
175
   * constrained to the syllable.
176
   */
177
  {HB_TAG('n','u','k','t'), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE},
178
  {HB_TAG('a','k','h','n'), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE},
179
  {HB_TAG('r','p','h','f'),        F_MANUAL_JOINERS | F_PER_SYLLABLE},
180
  {HB_TAG('r','k','r','f'), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE},
181
  {HB_TAG('p','r','e','f'),        F_MANUAL_JOINERS | F_PER_SYLLABLE},
182
  {HB_TAG('b','l','w','f'),        F_MANUAL_JOINERS | F_PER_SYLLABLE},
183
  {HB_TAG('a','b','v','f'),        F_MANUAL_JOINERS | F_PER_SYLLABLE},
184
  {HB_TAG('h','a','l','f'),        F_MANUAL_JOINERS | F_PER_SYLLABLE},
185
  {HB_TAG('p','s','t','f'),        F_MANUAL_JOINERS | F_PER_SYLLABLE},
186
  {HB_TAG('v','a','t','u'), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE},
187
  {HB_TAG('c','j','c','t'), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE},
188
  /*
189
   * Other features.
190
   * These features are applied all at once, after final_reordering, constrained
191
   * to the syllable.
192
   * Default Bengali font in Windows for example has intermixed
193
   * lookups for init,pres,abvs,blws features.
194
   */
195
  {HB_TAG('i','n','i','t'),        F_MANUAL_JOINERS | F_PER_SYLLABLE},
196
  {HB_TAG('p','r','e','s'), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE},
197
  {HB_TAG('a','b','v','s'), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE},
198
  {HB_TAG('b','l','w','s'), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE},
199
  {HB_TAG('p','s','t','s'), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE},
200
  {HB_TAG('h','a','l','n'), F_GLOBAL_MANUAL_JOINERS | F_PER_SYLLABLE},
201
};
202
203
/*
204
 * Must be in the same order as the indic_features array.
205
 */
206
enum {
207
  _INDIC_NUKT,
208
  _INDIC_AKHN,
209
  INDIC_RPHF,
210
  _INDIC_RKRF,
211
  INDIC_PREF,
212
  INDIC_BLWF,
213
  INDIC_ABVF,
214
  INDIC_HALF,
215
  INDIC_PSTF,
216
  _INDIC_VATU,
217
  _INDIC_CJCT,
218
219
  INDIC_INIT,
220
  _INDIC_PRES,
221
  _INDIC_ABVS,
222
  _INDIC_BLWS,
223
  _INDIC_PSTS,
224
  _INDIC_HALN,
225
226
  INDIC_NUM_FEATURES,
227
  INDIC_BASIC_FEATURES = INDIC_INIT, /* Don't forget to update this! */
228
};
229
230
static bool
231
setup_syllables_indic (const hb_ot_shape_plan_t *plan,
232
           hb_font_t *font,
233
           hb_buffer_t *buffer);
234
static bool
235
initial_reordering_indic (const hb_ot_shape_plan_t *plan,
236
        hb_font_t *font,
237
        hb_buffer_t *buffer);
238
static bool
239
final_reordering_indic (const hb_ot_shape_plan_t *plan,
240
      hb_font_t *font,
241
      hb_buffer_t *buffer);
242
243
static void
244
collect_features_indic (hb_ot_shape_planner_t *plan)
245
0
{
246
0
  hb_ot_map_builder_t *map = &plan->map;
247
248
  /* Do this before any lookups have been applied. */
249
0
  map->add_gsub_pause (setup_syllables_indic);
250
251
0
  map->enable_feature (HB_TAG('l','o','c','l'), F_PER_SYLLABLE);
252
  /* The Indic specs do not require ccmp, but we apply it here since if
253
   * there is a use of it, it's typically at the beginning. */
254
0
  map->enable_feature (HB_TAG('c','c','m','p'), F_PER_SYLLABLE);
255
256
257
0
  unsigned int i = 0;
258
0
  map->add_gsub_pause (initial_reordering_indic);
259
260
0
  for (; i < INDIC_BASIC_FEATURES; i++) {
261
0
    map->add_feature (indic_features[i]);
262
0
    map->add_gsub_pause (nullptr);
263
0
  }
264
265
0
  map->add_gsub_pause (final_reordering_indic);
266
267
0
  for (; i < INDIC_NUM_FEATURES; i++)
268
0
    map->add_feature (indic_features[i]);
269
0
}
270
271
static void
272
override_features_indic (hb_ot_shape_planner_t *plan)
273
0
{
274
0
  plan->map.disable_feature (HB_TAG('l','i','g','a'));
275
0
  plan->map.add_gsub_pause (hb_syllabic_clear_var); // Don't need syllables anymore, use stop to free buffer var
276
0
}
277
278
279
struct indic_shape_plan_t
280
{
281
  bool load_virama_glyph (hb_font_t *font, hb_codepoint_t *pglyph) const
282
0
  {
283
0
    hb_codepoint_t glyph = virama_glyph;
284
0
    if (unlikely (glyph == (hb_codepoint_t) -1))
285
0
    {
286
0
      if (!config->virama || !font->get_nominal_glyph (config->virama, &glyph))
287
0
  glyph = 0;
288
      /* Technically speaking, the spec says we should apply 'locl' to virama too.
289
       * Maybe one day... */
290
291
      /* Our get_nominal_glyph() function needs a font, so we can't get the virama glyph
292
       * during shape planning...  Instead, overwrite it here. */
293
0
      virama_glyph = (int) glyph;
294
0
    }
295
296
0
    *pglyph = glyph;
297
0
    return glyph != 0;
298
0
  }
299
300
  const indic_config_t *config;
301
302
  bool is_old_spec;
303
  mutable hb_atomic_t<hb_codepoint_t> virama_glyph;
304
305
  hb_indic_would_substitute_feature_t rphf;
306
  hb_indic_would_substitute_feature_t pref;
307
  hb_indic_would_substitute_feature_t blwf;
308
  hb_indic_would_substitute_feature_t pstf;
309
  hb_indic_would_substitute_feature_t vatu;
310
311
  hb_mask_t mask_array[INDIC_NUM_FEATURES];
312
};
313
314
static void *
315
data_create_indic (const hb_ot_shape_plan_t *plan)
316
0
{
317
0
  indic_shape_plan_t *indic_plan = (indic_shape_plan_t *) hb_calloc (1, sizeof (indic_shape_plan_t));
318
0
  if (unlikely (!indic_plan))
319
0
    return nullptr;
320
321
0
  indic_plan->config = &indic_configs[0];
322
0
  for (unsigned int i = 1; i < ARRAY_LENGTH (indic_configs); i++)
323
0
    if (plan->props.script == indic_configs[i].script) {
324
0
      indic_plan->config = &indic_configs[i];
325
0
      break;
326
0
    }
327
328
0
  indic_plan->is_old_spec = indic_plan->config->has_old_spec && ((plan->map.chosen_script[0] & 0x000000FFu) != '2');
329
0
  indic_plan->virama_glyph = -1;
330
331
  /* Use zero-context would_substitute() matching for new-spec of the main
332
   * Indic scripts, and scripts with one spec only, but not for old-specs.
333
   * The new-spec for all dual-spec scripts says zero-context matching happens.
334
   *
335
   * However, testing with Malayalam shows that old and new spec both allow
336
   * context.  Testing with Bengali new-spec however shows that it doesn't.
337
   * So, the heuristic here is the way it is.  It should *only* be changed,
338
   * as we discover more cases of what Windows does.  DON'T TOUCH OTHERWISE.
339
   */
340
0
  bool zero_context = !indic_plan->is_old_spec && plan->props.script != HB_SCRIPT_MALAYALAM;
341
0
  indic_plan->rphf.init (&plan->map, HB_TAG('r','p','h','f'), zero_context);
342
0
  indic_plan->pref.init (&plan->map, HB_TAG('p','r','e','f'), zero_context);
343
0
  indic_plan->blwf.init (&plan->map, HB_TAG('b','l','w','f'), zero_context);
344
0
  indic_plan->pstf.init (&plan->map, HB_TAG('p','s','t','f'), zero_context);
345
0
  indic_plan->vatu.init (&plan->map, HB_TAG('v','a','t','u'), zero_context);
346
347
0
  for (unsigned int i = 0; i < ARRAY_LENGTH (indic_plan->mask_array); i++)
348
0
    indic_plan->mask_array[i] = (indic_features[i].flags & F_GLOBAL) ?
349
0
         0 : plan->map.get_1_mask (indic_features[i].tag);
350
351
0
  return indic_plan;
352
0
}
353
354
static void
355
data_destroy_indic (void *data)
356
0
{
357
0
  hb_free (data);
358
0
}
359
360
static indic_position_t
361
consonant_position_from_face (const indic_shape_plan_t *indic_plan,
362
            const hb_codepoint_t consonant,
363
            const hb_codepoint_t virama,
364
            hb_face_t *face,
365
            hb_buffer_t *buffer)
366
0
{
367
  /* For old-spec, the order of glyphs is Consonant,Virama,
368
   * whereas for new-spec, it's Virama,Consonant.  However,
369
   * some broken fonts (like Free Sans) simply copied lookups
370
   * from old-spec to new-spec without modification.
371
   * And oddly enough, Uniscribe seems to respect those lookups.
372
   * Eg. in the sequence U+0924,U+094D,U+0930, Uniscribe finds
373
   * base at 0.  The font however, only has lookups matching
374
   * 930,94D in 'blwf', not the expected 94D,930 (with new-spec
375
   * table).  As such, we simply match both sequences.  Seems
376
   * to work.
377
   *
378
   * Vatu is done as well, for:
379
   * https://github.com/harfbuzz/harfbuzz/issues/1587
380
   */
381
0
  hb_codepoint_t glyphs[3] = {virama, consonant, virama};
382
0
  if (indic_plan->blwf.would_substitute (glyphs  , 2, face, buffer) ||
383
0
      indic_plan->blwf.would_substitute (glyphs+1, 2, face, buffer) ||
384
0
      indic_plan->vatu.would_substitute (glyphs  , 2, face, buffer) ||
385
0
      indic_plan->vatu.would_substitute (glyphs+1, 2, face, buffer))
386
0
    return POS_BELOW_C;
387
0
  if (indic_plan->pstf.would_substitute (glyphs  , 2, face, buffer) ||
388
0
      indic_plan->pstf.would_substitute (glyphs+1, 2, face, buffer))
389
0
    return POS_POST_C;
390
0
  if (indic_plan->pref.would_substitute (glyphs  , 2, face, buffer) ||
391
0
      indic_plan->pref.would_substitute (glyphs+1, 2, face, buffer))
392
0
    return POS_POST_C;
393
0
  return POS_BASE_C;
394
0
}
395
396
static void
397
setup_masks_indic (const hb_ot_shape_plan_t *plan HB_UNUSED,
398
       hb_buffer_t              *buffer,
399
       hb_font_t                *font HB_UNUSED)
400
0
{
401
0
  HB_BUFFER_ALLOCATE_VAR (buffer, indic_category);
402
0
  HB_BUFFER_ALLOCATE_VAR (buffer, indic_position);
403
404
  /* We cannot setup masks here.  We save information about characters
405
   * and setup masks later on in a pause-callback. */
406
407
0
  unsigned int count = buffer->len;
408
0
  hb_glyph_info_t *info = buffer->info;
409
0
  for (unsigned int i = 0; i < count; i++)
410
0
    set_indic_properties (info[i]);
411
0
}
412
413
static bool
414
setup_syllables_indic (const hb_ot_shape_plan_t *plan HB_UNUSED,
415
           hb_font_t *font HB_UNUSED,
416
           hb_buffer_t *buffer)
417
0
{
418
0
  HB_BUFFER_ALLOCATE_VAR (buffer, syllable);
419
0
  find_syllables_indic (buffer);
420
0
  foreach_syllable (buffer, start, end)
421
0
    buffer->unsafe_to_break (start, end);
422
0
  return false;
423
0
}
424
425
static int
426
compare_indic_order (const hb_glyph_info_t *pa, const hb_glyph_info_t *pb)
427
0
{
428
0
  int a = pa->indic_position();
429
0
  int b = pb->indic_position();
430
431
0
  return (int) a - (int) b;
432
0
}
433
434
435
436
static void
437
update_consonant_positions_indic (const hb_ot_shape_plan_t *plan,
438
          hb_font_t         *font,
439
          hb_buffer_t       *buffer)
440
0
{
441
0
  const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data;
442
443
0
  hb_codepoint_t virama;
444
0
  if (indic_plan->load_virama_glyph (font, &virama))
445
0
  {
446
0
    hb_face_t *face = font->face;
447
0
    unsigned int count = buffer->len;
448
0
    hb_glyph_info_t *info = buffer->info;
449
0
    for (unsigned int i = 0; i < count; i++)
450
0
      if (info[i].indic_position() == POS_BASE_C)
451
0
      {
452
0
  hb_codepoint_t consonant = info[i].codepoint;
453
0
  info[i].indic_position() = consonant_position_from_face (indic_plan, consonant, virama, face, buffer);
454
0
      }
455
0
  }
456
0
}
457
458
459
/* Rules from:
460
 * https://docs.microsqoft.com/en-us/typography/script-development/devanagari */
461
462
static void
463
initial_reordering_consonant_syllable (const hb_ot_shape_plan_t *plan,
464
               hb_face_t *face,
465
               hb_buffer_t *buffer,
466
               unsigned int start, unsigned int end)
467
0
{
468
0
  const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data;
469
0
  hb_glyph_info_t *info = buffer->info;
470
471
  /* https://github.com/harfbuzz/harfbuzz/issues/435#issuecomment-335560167
472
   * // For compatibility with legacy usage in Kannada,
473
   * // Ra+h+ZWJ must behave like Ra+ZWJ+h...
474
   */
475
0
  if (buffer->props.script == HB_SCRIPT_KANNADA &&
476
0
      start + 3 <= end &&
477
0
      is_one_of (info[start  ], FLAG (I_Cat(Ra))) &&
478
0
      is_one_of (info[start+1], FLAG (I_Cat(H))) &&
479
0
      is_one_of (info[start+2], FLAG (I_Cat(ZWJ))))
480
0
  {
481
0
    buffer->merge_clusters (start+1, start+3);
482
0
    hb_swap (info[start+1], info[start+2]);
483
0
  }
484
485
  /* 1. Find base consonant:
486
   *
487
   * The shaping engine finds the base consonant of the syllable, using the
488
   * following algorithm: starting from the end of the syllable, move backwards
489
   * until a consonant is found that does not have a below-base or post-base
490
   * form (post-base forms have to follow below-base forms), or that is not a
491
   * pre-base-reordering Ra, or arrive at the first consonant. The consonant
492
   * stopped at will be the base.
493
   *
494
   *   o If the syllable starts with Ra + Halant (in a script that has Reph)
495
   *     and has more than one consonant, Ra is excluded from candidates for
496
   *     base consonants.
497
   */
498
499
0
  unsigned int base = end;
500
0
  bool has_reph = false;
501
502
0
  {
503
    /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
504
     *    and has more than one consonant, Ra is excluded from candidates for
505
     *    base consonants. */
506
0
    unsigned int limit = start;
507
0
    if (indic_plan->mask_array[INDIC_RPHF] &&
508
0
  start + 3 <= end &&
509
0
  (
510
0
   (indic_plan->config->reph_mode == REPH_MODE_IMPLICIT && !is_joiner (info[start + 2])) ||
511
0
   (indic_plan->config->reph_mode == REPH_MODE_EXPLICIT && info[start + 2].indic_category() == I_Cat(ZWJ))
512
0
  ))
513
0
    {
514
      /* See if it matches the 'rphf' feature. */
515
0
      hb_codepoint_t glyphs[3] = {info[start].codepoint,
516
0
          info[start + 1].codepoint,
517
0
          indic_plan->config->reph_mode == REPH_MODE_EXPLICIT ?
518
0
            info[start + 2].codepoint : 0};
519
0
      if (indic_plan->rphf.would_substitute (glyphs, 2, face, buffer) ||
520
0
    (indic_plan->config->reph_mode == REPH_MODE_EXPLICIT &&
521
0
     indic_plan->rphf.would_substitute (glyphs, 3, face, buffer)))
522
0
      {
523
0
  limit += 2;
524
0
  while (limit < end && is_joiner (info[limit]))
525
0
    limit++;
526
0
  base = start;
527
0
  has_reph = true;
528
0
      }
529
0
    } else if (indic_plan->config->reph_mode == REPH_MODE_LOG_REPHA && info[start].indic_category() == I_Cat(Repha))
530
0
    {
531
0
  limit += 1;
532
0
  while (limit < end && is_joiner (info[limit]))
533
0
    limit++;
534
0
  base = start;
535
0
  has_reph = true;
536
0
    }
537
538
0
    {
539
      /* -> starting from the end of the syllable, move backwards */
540
0
      unsigned int i = end;
541
0
      bool seen_below = false;
542
0
      do {
543
0
  i--;
544
  /* -> until a consonant is found */
545
0
  if (is_consonant (info[i]))
546
0
  {
547
    /* -> that does not have a below-base or post-base form
548
     * (post-base forms have to follow below-base forms), */
549
0
    if (info[i].indic_position() != POS_BELOW_C &&
550
0
        (info[i].indic_position() != POS_POST_C || seen_below))
551
0
    {
552
0
      base = i;
553
0
      break;
554
0
    }
555
0
    if (info[i].indic_position() == POS_BELOW_C)
556
0
      seen_below = true;
557
558
    /* -> or that is not a pre-base-reordering Ra,
559
     *
560
     * IMPLEMENTATION NOTES:
561
     *
562
     * Our pre-base-reordering Ra's are marked POS_POST_C, so will be skipped
563
     * by the logic above already.
564
     */
565
566
    /* -> or arrive at the first consonant. The consonant stopped at will
567
     * be the base. */
568
0
    base = i;
569
0
  }
570
0
  else
571
0
  {
572
    /* A ZWJ after a Halant stops the base search, and requests an explicit
573
     * half form.
574
     * A ZWJ before a Halant, requests a subjoined form instead, and hence
575
     * search continues.  This is particularly important for Bengali
576
     * sequence Ra,H,Ya that should form Ya-Phalaa by subjoining Ya. */
577
0
    if (start < i &&
578
0
        info[i].indic_category() == I_Cat(ZWJ) &&
579
0
        info[i - 1].indic_category() == I_Cat(H))
580
0
      break;
581
0
  }
582
0
      } while (i > limit);
583
0
    }
584
585
    /* -> If the syllable starts with Ra + Halant (in a script that has Reph)
586
     *    and has more than one consonant, Ra is excluded from candidates for
587
     *    base consonants.
588
     *
589
     *  Only do this for unforced Reph. (ie. not for Ra,H,ZWJ. */
590
0
    if (has_reph && base == start && limit - base <= 2) {
591
      /* Have no other consonant, so Reph is not formed and Ra becomes base. */
592
0
      has_reph = false;
593
0
    }
594
0
  }
595
596
597
  /* 2. Decompose and reorder Matras:
598
   *
599
   * Each matra and any syllable modifier sign in the syllable are moved to the
600
   * appropriate position relative to the consonant(s) in the syllable. The
601
   * shaping engine decomposes two- or three-part matras into their constituent
602
   * parts before any repositioning. Matra characters are classified by which
603
   * consonant in a conjunct they have affinity for and are reordered to the
604
   * following positions:
605
   *
606
   *   o Before first half form in the syllable
607
   *   o After subjoined consonants
608
   *   o After post-form consonant
609
   *   o After main consonant (for above marks)
610
   *
611
   * IMPLEMENTATION NOTES:
612
   *
613
   * The normalize() routine has already decomposed matras for us, so we don't
614
   * need to worry about that.
615
   */
616
617
618
  /* 3.  Reorder marks to canonical order:
619
   *
620
   * Adjacent nukta and halant or nukta and vedic sign are always repositioned
621
   * if necessary, so that the nukta is first.
622
   *
623
   * IMPLEMENTATION NOTES:
624
   *
625
   * We don't need to do this: the normalize() routine already did this for us.
626
   */
627
628
629
  /* Reorder characters */
630
631
0
  for (unsigned int i = start; i < base; i++)
632
0
    info[i].indic_position() = hb_min (POS_PRE_C, (indic_position_t) info[i].indic_position());
633
634
0
  if (base < end)
635
0
    info[base].indic_position() = POS_BASE_C;
636
637
  /* Handle beginning Ra */
638
0
  if (has_reph)
639
0
    info[start].indic_position() = POS_RA_TO_BECOME_REPH;
640
641
  /* For old-style Indic script tags, move the first post-base Halant after
642
   * last consonant.
643
   *
644
   * Reports suggest that in some scripts Uniscribe does this only if there
645
   * is *not* a Halant after last consonant already.  We know that is the
646
   * case for Kannada, while it reorders unconditionally in other scripts,
647
   * eg. Malayalam, Bengali, and Devanagari.  We don't currently know about
648
   * other scripts, so we block Kannada.
649
   *
650
   * Kannada test case:
651
   * U+0C9A,U+0CCD,U+0C9A,U+0CCD
652
   * With some versions of Lohit Kannada.
653
   * https://bugs.freedesktop.org/show_bug.cgi?id=59118
654
   *
655
   * Malayalam test case:
656
   * U+0D38,U+0D4D,U+0D31,U+0D4D,U+0D31,U+0D4D
657
   * With lohit-ttf-20121122/Lohit-Malayalam.ttf
658
   *
659
   * Bengali test case:
660
   * U+0998,U+09CD,U+09AF,U+09CD
661
   * With Windows XP vrinda.ttf
662
   * https://github.com/harfbuzz/harfbuzz/issues/1073
663
   *
664
   * Devanagari test case:
665
   * U+091F,U+094D,U+0930,U+094D
666
   * With chandas.ttf
667
   * https://github.com/harfbuzz/harfbuzz/issues/1071
668
   */
669
0
  if (indic_plan->is_old_spec)
670
0
  {
671
0
    bool disallow_double_halants = buffer->props.script == HB_SCRIPT_KANNADA;
672
0
    for (unsigned int i = base + 1; i < end; i++)
673
0
      if (info[i].indic_category() == I_Cat(H))
674
0
      {
675
0
  unsigned int j;
676
0
  for (j = end - 1; j > i; j--)
677
0
    if (is_consonant (info[j]) ||
678
0
        (disallow_double_halants && info[j].indic_category() == I_Cat(H)))
679
0
      break;
680
0
  if (info[j].indic_category() != I_Cat(H) && j > i) {
681
    /* Move Halant to after last consonant. */
682
0
    hb_glyph_info_t t = info[i];
683
0
    memmove (&info[i], &info[i + 1], (j - i) * sizeof (info[0]));
684
0
    info[j] = t;
685
0
  }
686
0
  break;
687
0
      }
688
0
  }
689
690
  /* Attach misc marks to previous char to move with them. */
691
0
  {
692
0
    indic_position_t last_pos = POS_START;
693
0
    for (unsigned int i = start; i < end; i++)
694
0
    {
695
0
      if ((FLAG_UNSAFE (info[i].indic_category()) & (JOINER_FLAGS | FLAG (I_Cat(N)) | FLAG (I_Cat(RS)) | FLAG (I_Cat(CM)) | FLAG (I_Cat(H)))))
696
0
      {
697
0
  info[i].indic_position() = last_pos;
698
0
  if (unlikely (info[i].indic_category() == I_Cat(H) &&
699
0
          info[i].indic_position() == POS_PRE_M))
700
0
  {
701
    /*
702
     * Uniscribe doesn't move the Halant with Left Matra.
703
     * TEST: U+092B,U+093F,U+094D
704
     * We follow.
705
     */
706
0
    for (unsigned int j = i; j > start; j--)
707
0
      if (info[j - 1].indic_position() != POS_PRE_M) {
708
0
        info[i].indic_position() = info[j - 1].indic_position();
709
0
        break;
710
0
      }
711
0
  }
712
0
      } else if (info[i].indic_position() != POS_SMVD) {
713
0
  if (info[i].indic_category() == I_Cat(MPst) &&
714
0
      i > start && info[i - 1].indic_category() == I_Cat(SM))
715
0
    info[i - 1].indic_position() = info[i].indic_position();
716
0
  last_pos = (indic_position_t) info[i].indic_position();
717
0
      }
718
0
    }
719
0
  }
720
  /* For post-base consonants let them own anything before them
721
   * since the last consonant or matra. */
722
0
  {
723
0
    unsigned int last = base;
724
0
    for (unsigned int i = base + 1; i < end; i++)
725
0
      if (is_consonant (info[i]))
726
0
      {
727
0
  for (unsigned int j = last + 1; j < i; j++)
728
0
    if (info[j].indic_position() < POS_SMVD)
729
0
      info[j].indic_position() = info[i].indic_position();
730
0
  last = i;
731
0
      } else if (FLAG_UNSAFE (info[i].indic_category()) & (FLAG (I_Cat(M)) | FLAG (I_Cat(MPst))))
732
0
  last = i;
733
0
  }
734
735
736
0
  {
737
    /* Use syllable() for sort accounting temporarily. */
738
0
    unsigned int syllable = info[start].syllable();
739
0
    for (unsigned int i = start; i < end; i++)
740
0
      info[i].syllable() = i - start;
741
742
    /* Sit tight, rock 'n roll! */
743
0
    hb_stable_sort (info + start, end - start, compare_indic_order);
744
745
    /* Find base again; also flip left-matra sequence. */
746
0
    unsigned first_left_matra = end;
747
0
    unsigned last_left_matra = end;
748
0
    base = end;
749
0
    for (unsigned int i = start; i < end; i++)
750
0
    {
751
0
      if (info[i].indic_position() == POS_BASE_C)
752
0
      {
753
0
  base = i;
754
0
  break;
755
0
      }
756
0
      else if (info[i].indic_position() == POS_PRE_M)
757
0
      {
758
0
        if (first_left_matra == end)
759
0
    first_left_matra = i;
760
0
  last_left_matra = i;
761
0
      }
762
0
    }
763
    /* https://github.com/harfbuzz/harfbuzz/issues/3863 */
764
0
    if (first_left_matra < last_left_matra)
765
0
    {
766
      /* No need to merge clusters, handled later. */
767
0
      buffer->reverse_range (first_left_matra, last_left_matra + 1);
768
      /* Reverse back nuktas, etc. */
769
0
      unsigned i = first_left_matra;
770
0
      for (unsigned j = i; j <= last_left_matra; j++)
771
0
  if (FLAG_UNSAFE (info[j].indic_category()) & (FLAG (I_Cat(M)) | FLAG (I_Cat(MPst))))
772
0
  {
773
0
    buffer->reverse_range (i, j + 1);
774
0
    i = j + 1;
775
0
  }
776
0
    }
777
778
    /* Things are out-of-control for post base positions, they may shuffle
779
     * around like crazy.  In old-spec mode, we move halants around, so in
780
     * that case merge all clusters after base.  Otherwise, check the sort
781
     * order and merge as needed.
782
     * For pre-base stuff, we handle cluster issues in final reordering.
783
     *
784
     * We could use buffer->sort() for this, if there was no special
785
     * reordering of pre-base stuff happening later...
786
     * We don't want to merge_clusters all of that, which buffer->sort()
787
     * would.  Here's a concrete example:
788
     *
789
     * Assume there's a pre-base consonant and explicit Halant before base,
790
     * followed by a prebase-reordering (left) Matra:
791
     *
792
     *   C,H,ZWNJ,B,M
793
     *
794
     * At this point in reordering we would have:
795
     *
796
     *   M,C,H,ZWNJ,B
797
     *
798
     * whereas in final reordering we will bring the Matra closer to Base:
799
     *
800
     *   C,H,ZWNJ,M,B
801
     *
802
     * That's why we don't want to merge-clusters anything before the Base
803
     * at this point.  But if something moved from after Base to before it,
804
     * we should merge clusters from base to them.  In final-reordering, we
805
     * only move things around before base, and merge-clusters up to base.
806
     * These two merge-clusters from the two sides of base will interlock
807
     * to merge things correctly.  See:
808
     * https://github.com/harfbuzz/harfbuzz/issues/2272
809
     */
810
0
    if (indic_plan->is_old_spec || end - start > 127)
811
0
      buffer->merge_clusters (base, end);
812
0
    else
813
0
    {
814
      /* Note!  syllable() is a one-byte field. */
815
0
      for (unsigned int i = base; i < end; i++)
816
0
  if (info[i].syllable() != 255)
817
0
  {
818
0
    unsigned int min = i;
819
0
    unsigned int max = i;
820
0
    unsigned int j = start + info[i].syllable();
821
0
    while (j != i)
822
0
    {
823
0
      min = hb_min (min, j);
824
0
      max = hb_max (max, j);
825
0
      unsigned int next = start + info[j].syllable();
826
0
      info[j].syllable() = 255; /* So we don't process j later again. */
827
0
      j = next;
828
0
    }
829
0
    buffer->merge_clusters (hb_max (base, min), max + 1);
830
0
  }
831
0
    }
832
833
    /* Put syllable back in. */
834
0
    for (unsigned int i = start; i < end; i++)
835
0
      info[i].syllable() = syllable;
836
0
  }
837
838
  /* Setup masks now */
839
840
0
  {
841
0
    hb_mask_t mask;
842
843
    /* Reph */
844
0
    for (unsigned int i = start; i < end && info[i].indic_position() == POS_RA_TO_BECOME_REPH; i++)
845
0
      info[i].mask |= indic_plan->mask_array[INDIC_RPHF];
846
847
    /* Pre-base */
848
0
    mask = indic_plan->mask_array[INDIC_HALF];
849
0
    if (!indic_plan->is_old_spec &&
850
0
  indic_plan->config->blwf_mode == BLWF_MODE_PRE_AND_POST)
851
0
      mask |= indic_plan->mask_array[INDIC_BLWF];
852
0
    for (unsigned int i = start; i < base; i++)
853
0
      info[i].mask  |= mask;
854
    /* Base */
855
0
    mask = 0;
856
0
    if (base < end)
857
0
      info[base].mask |= mask;
858
    /* Post-base */
859
0
    mask = indic_plan->mask_array[INDIC_BLWF] |
860
0
     indic_plan->mask_array[INDIC_ABVF] |
861
0
     indic_plan->mask_array[INDIC_PSTF];
862
0
    for (unsigned int i = base + 1; i < end; i++)
863
0
      info[i].mask  |= mask;
864
0
  }
865
866
0
  if (indic_plan->is_old_spec &&
867
0
      buffer->props.script == HB_SCRIPT_DEVANAGARI)
868
0
  {
869
    /* Old-spec eye-lash Ra needs special handling.  From the
870
     * spec:
871
     *
872
     * "The feature 'below-base form' is applied to consonants
873
     * having below-base forms and following the base consonant.
874
     * The exception is vattu, which may appear below half forms
875
     * as well as below the base glyph. The feature 'below-base
876
     * form' will be applied to all such occurrences of Ra as well."
877
     *
878
     * Test case: U+0924,U+094D,U+0930,U+094d,U+0915
879
     * with Sanskrit 2003 font.
880
     *
881
     * However, note that Ra,Halant,ZWJ is the correct way to
882
     * request eyelash form of Ra, so we wouldbn't inhibit it
883
     * in that sequence.
884
     *
885
     * Test case: U+0924,U+094D,U+0930,U+094d,U+200D,U+0915
886
     */
887
0
    for (unsigned int i = start; i + 1 < base; i++)
888
0
      if (info[i  ].indic_category() == I_Cat(Ra) &&
889
0
    info[i+1].indic_category() == I_Cat(H)  &&
890
0
    (i + 2 == base ||
891
0
     info[i+2].indic_category() != I_Cat(ZWJ)))
892
0
      {
893
0
  info[i  ].mask |= indic_plan->mask_array[INDIC_BLWF];
894
0
  info[i+1].mask |= indic_plan->mask_array[INDIC_BLWF];
895
0
      }
896
0
  }
897
898
0
  unsigned int pref_len = 2;
899
0
  if (indic_plan->mask_array[INDIC_PREF] && base + pref_len < end)
900
0
  {
901
    /* Find a Halant,Ra sequence and mark it for pre-base-reordering processing. */
902
0
    for (unsigned int i = base + 1; i + pref_len - 1 < end; i++) {
903
0
      hb_codepoint_t glyphs[2];
904
0
      for (unsigned int j = 0; j < pref_len; j++)
905
0
  glyphs[j] = info[i + j].codepoint;
906
0
      if (indic_plan->pref.would_substitute (glyphs, pref_len, face, buffer))
907
0
      {
908
0
  for (unsigned int j = 0; j < pref_len; j++)
909
0
    info[i++].mask |= indic_plan->mask_array[INDIC_PREF];
910
0
  break;
911
0
      }
912
0
    }
913
0
  }
914
915
  /* Apply ZWJ/ZWNJ effects */
916
0
  for (unsigned int i = start + 1; i < end; i++)
917
0
    if (is_joiner (info[i])) {
918
0
      bool non_joiner = info[i].indic_category() == I_Cat(ZWNJ);
919
0
      unsigned int j = i;
920
921
0
      do {
922
0
  j--;
923
924
  /* ZWJ/ZWNJ should disable CJCT.  They do that by simply
925
   * being there, since we don't skip them for the CJCT
926
   * feature (ie. F_MANUAL_ZWJ) */
927
928
  /* A ZWNJ disables HALF. */
929
0
  if (non_joiner)
930
0
    info[j].mask &= ~indic_plan->mask_array[INDIC_HALF];
931
932
0
      } while (j > start && !is_consonant (info[j]));
933
0
    }
934
0
}
935
936
static void
937
initial_reordering_standalone_cluster (const hb_ot_shape_plan_t *plan,
938
               hb_face_t *face,
939
               hb_buffer_t *buffer,
940
               unsigned int start, unsigned int end)
941
0
{
942
  /* We treat placeholder/dotted-circle as if they are consonants, so we
943
   * should just chain... */
944
945
0
  initial_reordering_consonant_syllable (plan, face, buffer, start, end);
946
0
}
947
948
static void
949
initial_reordering_syllable_indic (const hb_ot_shape_plan_t *plan,
950
           hb_face_t *face,
951
           hb_buffer_t *buffer,
952
           unsigned int start, unsigned int end)
953
0
{
954
0
  indic_syllable_type_t syllable_type = (indic_syllable_type_t) (buffer->info[start].syllable() & 0x0F);
955
0
  switch (syllable_type)
956
0
  {
957
0
    case indic_vowel_syllable: /* We made the vowels look like consonants.  So let's call the consonant logic! */
958
0
    case indic_consonant_syllable:
959
0
     initial_reordering_consonant_syllable (plan, face, buffer, start, end);
960
0
     break;
961
962
0
    case indic_broken_cluster: /* We already inserted dotted-circles, so just call the standalone_cluster. */
963
0
    case indic_standalone_cluster:
964
0
     initial_reordering_standalone_cluster (plan, face, buffer, start, end);
965
0
     break;
966
967
0
    case indic_symbol_cluster:
968
0
    case indic_non_indic_cluster:
969
0
      break;
970
0
  }
971
0
}
972
973
static bool
974
initial_reordering_indic (const hb_ot_shape_plan_t *plan,
975
        hb_font_t *font,
976
        hb_buffer_t *buffer)
977
0
{
978
0
  bool ret = false;
979
0
  if (!buffer->message (font, "start reordering indic initial"))
980
0
    return ret;
981
982
0
  update_consonant_positions_indic (plan, font, buffer);
983
0
  if (hb_syllabic_insert_dotted_circles (font, buffer,
984
0
           indic_broken_cluster,
985
0
           I_Cat(DOTTEDCIRCLE),
986
0
           I_Cat(Repha),
987
0
           POS_END))
988
0
    ret = true;
989
990
0
  foreach_syllable (buffer, start, end)
991
0
    initial_reordering_syllable_indic (plan, font->face, buffer, start, end);
992
993
0
  (void) buffer->message (font, "end reordering indic initial");
994
995
0
  return ret;
996
0
}
997
998
static void
999
final_reordering_syllable_indic (const hb_ot_shape_plan_t *plan,
1000
         hb_buffer_t *buffer,
1001
         unsigned int start, unsigned int end)
1002
0
{
1003
0
  const indic_shape_plan_t *indic_plan = (const indic_shape_plan_t *) plan->data;
1004
0
  hb_glyph_info_t *info = buffer->info;
1005
1006
1007
  /* This function relies heavily on halant glyphs.  Lots of ligation
1008
   * and possibly multiple substitutions happened prior to this
1009
   * phase, and that might have messed up our properties.  Recover
1010
   * from a particular case of that where we're fairly sure that a
1011
   * class of I_Cat(H) is desired but has been lost. */
1012
  /* We don't call load_virama_glyph(), since we know it's already
1013
   * loaded. */
1014
0
  hb_codepoint_t virama_glyph = indic_plan->virama_glyph;
1015
0
  if (virama_glyph)
1016
0
  {
1017
0
    for (unsigned int i = start; i < end; i++)
1018
0
      if (info[i].codepoint == virama_glyph &&
1019
0
    _hb_glyph_info_ligated (&info[i]) &&
1020
0
    _hb_glyph_info_multiplied (&info[i]))
1021
0
      {
1022
  /* This will make sure that this glyph passes is_halant() test. */
1023
0
  info[i].indic_category() = I_Cat(H);
1024
0
  _hb_glyph_info_clear_ligated_and_multiplied (&info[i]);
1025
0
      }
1026
0
  }
1027
1028
1029
  /* 4. Final reordering:
1030
   *
1031
   * After the localized forms and basic shaping forms GSUB features have been
1032
   * applied (see below), the shaping engine performs some final glyph
1033
   * reordering before applying all the remaining font features to the entire
1034
   * syllable.
1035
   */
1036
1037
0
  bool try_pref = !!indic_plan->mask_array[INDIC_PREF];
1038
1039
  /* Find base again */
1040
0
  unsigned int base;
1041
0
  for (base = start; base < end; base++)
1042
0
    if (info[base].indic_position() >= POS_BASE_C)
1043
0
    {
1044
0
      if (try_pref && base + 1 < end)
1045
0
      {
1046
0
  for (unsigned int i = base + 1; i < end; i++)
1047
0
    if ((info[i].mask & indic_plan->mask_array[INDIC_PREF]) != 0)
1048
0
    {
1049
0
      if (!(_hb_glyph_info_substituted (&info[i]) &&
1050
0
      _hb_glyph_info_ligated_and_didnt_multiply (&info[i])))
1051
0
      {
1052
        /* Ok, this was a 'pref' candidate but didn't form any.
1053
         * Base is around here... */
1054
0
        base = i;
1055
0
        while (base < end && is_halant (info[base]))
1056
0
    base++;
1057
0
        if (base < end)
1058
0
    info[base].indic_position() = POS_BASE_C;
1059
1060
0
        try_pref = false;
1061
0
      }
1062
0
      break;
1063
0
    }
1064
0
  if (base == end)
1065
0
    break;
1066
0
      }
1067
      /* For Malayalam, skip over unformed below- (but NOT post-) forms. */
1068
0
      if (buffer->props.script == HB_SCRIPT_MALAYALAM)
1069
0
      {
1070
0
  for (unsigned int i = base + 1; i < end; i++)
1071
0
  {
1072
0
    while (i < end && is_joiner (info[i]))
1073
0
      i++;
1074
0
    if (i == end || !is_halant (info[i]))
1075
0
      break;
1076
0
    i++; /* Skip halant. */
1077
0
    while (i < end && is_joiner (info[i]))
1078
0
      i++;
1079
0
    if (i < end && is_consonant (info[i]) && info[i].indic_position() == POS_BELOW_C)
1080
0
    {
1081
0
      base = i;
1082
0
      info[base].indic_position() = POS_BASE_C;
1083
0
    }
1084
0
  }
1085
0
      }
1086
1087
0
      if (start < base && info[base].indic_position() > POS_BASE_C)
1088
0
  base--;
1089
0
      break;
1090
0
    }
1091
0
  if (base == end && start < base &&
1092
0
      is_one_of (info[base - 1], FLAG (I_Cat(ZWJ))))
1093
0
    base--;
1094
0
  if (base < end)
1095
0
    while (start < base &&
1096
0
     is_one_of (info[base], (FLAG (I_Cat(N)) | FLAG (I_Cat(H)))))
1097
0
      base--;
1098
1099
1100
  /*   o Reorder matras:
1101
   *
1102
   *     If a pre-base matra character had been reordered before applying basic
1103
   *     features, the glyph can be moved closer to the main consonant based on
1104
   *     whether half-forms had been formed. Actual position for the matra is
1105
   *     defined as “after last standalone halant glyph, after initial matra
1106
   *     position and before the main consonant”. If ZWJ or ZWNJ follow this
1107
   *     halant, position is moved after it.
1108
   *
1109
   * IMPLEMENTATION NOTES:
1110
   *
1111
   * It looks like the last sentence is wrong.  Testing, with Windows 7 Uniscribe
1112
   * and Devanagari shows that the behavior is best described as:
1113
   *
1114
   * "If ZWJ follows this halant, matra is NOT repositioned after this halant.
1115
   *  If ZWNJ follows this halant, position is moved after it."
1116
   *
1117
   * Test case, with Adobe Devanagari or Nirmala UI:
1118
   *
1119
   *   U+091F,U+094D,U+200C,U+092F,U+093F
1120
   *   (Matra moves to the middle, after ZWNJ.)
1121
   *
1122
   *   U+091F,U+094D,U+200D,U+092F,U+093F
1123
   *   (Matra does NOT move, stays to the left.)
1124
   *
1125
   * https://github.com/harfbuzz/harfbuzz/issues/1070
1126
   */
1127
1128
0
  if (start + 1 < end && start < base) /* Otherwise there can't be any pre-base matra characters. */
1129
0
  {
1130
    /* If we lost track of base, alas, position before last thingy. */
1131
0
    unsigned int new_pos = base == end ? base - 2 : base - 1;
1132
1133
    /* Malayalam / Tamil do not have "half" forms or explicit virama forms.
1134
     * The glyphs formed by 'half' are Chillus or ligated explicit viramas.
1135
     * We want to position matra after them.
1136
     */
1137
0
    if (buffer->props.script != HB_SCRIPT_MALAYALAM && buffer->props.script != HB_SCRIPT_TAMIL)
1138
0
    {
1139
0
    search:
1140
0
      while (new_pos > start &&
1141
0
       !(is_one_of (info[new_pos], (FLAG (I_Cat(M)) | FLAG (I_Cat(MPst)) | FLAG (I_Cat(H))))))
1142
0
  new_pos--;
1143
1144
      /* If we found no Halant we are done.
1145
       * Otherwise only proceed if the Halant does
1146
       * not belong to the Matra itself! */
1147
0
      if (is_halant (info[new_pos]) &&
1148
0
    info[new_pos].indic_position() != POS_PRE_M)
1149
0
      {
1150
#if 0 // See comment above
1151
  /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
1152
  if (new_pos + 1 < end && is_joiner (info[new_pos + 1]))
1153
    new_pos++;
1154
#endif
1155
0
  if (new_pos + 1 < end)
1156
0
  {
1157
    /* -> If ZWJ follows this halant, matra is NOT repositioned after this halant. */
1158
0
    if (info[new_pos + 1].indic_category() == I_Cat(ZWJ))
1159
0
    {
1160
      /* Keep searching. */
1161
0
      if (new_pos > start)
1162
0
      {
1163
0
        new_pos--;
1164
0
        goto search;
1165
0
      }
1166
0
    }
1167
    /* -> If ZWNJ follows this halant, position is moved after it.
1168
     *
1169
     * IMPLEMENTATION NOTES:
1170
     *
1171
     * This is taken care of by the state-machine. A Halant,ZWNJ is a terminating
1172
     * sequence for a consonant syllable; any pre-base matras occurring after it
1173
     * will belong to the subsequent syllable.
1174
     */
1175
0
  }
1176
0
      }
1177
0
      else
1178
0
  new_pos = start; /* No move. */
1179
0
    }
1180
1181
0
    if (start < new_pos && info[new_pos].indic_position () != POS_PRE_M)
1182
0
    {
1183
      /* Now go see if there's actually any matras... */
1184
0
      for (unsigned int i = new_pos; i > start; i--)
1185
0
  if (info[i - 1].indic_position () == POS_PRE_M)
1186
0
  {
1187
0
    unsigned int old_pos = i - 1;
1188
0
    if (old_pos < base && base <= new_pos) /* Shouldn't actually happen. */
1189
0
      base--;
1190
1191
0
    hb_glyph_info_t tmp = info[old_pos];
1192
0
    memmove (&info[old_pos], &info[old_pos + 1], (new_pos - old_pos) * sizeof (info[0]));
1193
0
    info[new_pos] = tmp;
1194
1195
    /* Note: this merge_clusters() is intentionally *after* the reordering.
1196
     * Indic matra reordering is special and tricky... */
1197
0
    buffer->merge_clusters (new_pos, hb_min (end, base + 1));
1198
1199
0
    new_pos--;
1200
0
  }
1201
0
    } else {
1202
0
      for (unsigned int i = start; i < base; i++)
1203
0
  if (info[i].indic_position () == POS_PRE_M) {
1204
0
    buffer->merge_clusters (i, hb_min (end, base + 1));
1205
0
    break;
1206
0
  }
1207
0
    }
1208
0
  }
1209
1210
1211
  /*   o Reorder reph:
1212
   *
1213
   *     Reph’s original position is always at the beginning of the syllable,
1214
   *     (i.e. it is not reordered at the character reordering stage). However,
1215
   *     it will be reordered according to the basic-forms shaping results.
1216
   *     Possible positions for reph, depending on the script, are; after main,
1217
   *     before post-base consonant forms, and after post-base consonant forms.
1218
   */
1219
1220
  /* Two cases:
1221
   *
1222
   * - If repha is encoded as a sequence of characters (Ra,H or Ra,H,ZWJ), then
1223
   *   we should only move it if the sequence ligated to the repha form.
1224
   *
1225
   * - If repha is encoded separately and in the logical position, we should only
1226
   *   move it if it did NOT ligate.  If it ligated, it's probably the font trying
1227
   *   to make it work without the reordering.
1228
   */
1229
0
  if (start + 1 < end &&
1230
0
      info[start].indic_position() == POS_RA_TO_BECOME_REPH &&
1231
0
      ((info[start].indic_category() == I_Cat(Repha)) ^
1232
0
       _hb_glyph_info_ligated_and_didnt_multiply (&info[start])))
1233
0
  {
1234
0
    unsigned int new_reph_pos;
1235
0
    reph_position_t reph_pos = indic_plan->config->reph_pos;
1236
1237
    /*       1. If reph should be positioned after post-base consonant forms,
1238
     *          proceed to step 5.
1239
     */
1240
0
    if (reph_pos == REPH_POS_AFTER_POST)
1241
0
    {
1242
0
      goto reph_step_5;
1243
0
    }
1244
1245
    /*       2. If the reph repositioning class is not after post-base: target
1246
     *          position is after the first explicit halant glyph between the
1247
     *          first post-reph consonant and last main consonant. If ZWJ or ZWNJ
1248
     *          are following this halant, position is moved after it. If such
1249
     *          position is found, this is the target position. Otherwise,
1250
     *          proceed to the next step.
1251
     *
1252
     *          Note: in old-implementation fonts, where classifications were
1253
     *          fixed in shaping engine, there was no case where reph position
1254
     *          will be found on this step.
1255
     */
1256
0
    {
1257
0
      new_reph_pos = start + 1;
1258
0
      while (new_reph_pos < base && !is_halant (info[new_reph_pos]))
1259
0
  new_reph_pos++;
1260
1261
0
      if (new_reph_pos < base && is_halant (info[new_reph_pos]))
1262
0
      {
1263
  /* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */
1264
0
  if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1]))
1265
0
    new_reph_pos++;
1266
0
  goto reph_move;
1267
0
      }
1268
0
    }
1269
1270
    /*       3. If reph should be repositioned after the main consonant: find the
1271
     *          first consonant not ligated with main, or find the first
1272
     *          consonant that is not a potential pre-base-reordering Ra.
1273
     */
1274
0
    if (reph_pos == REPH_POS_AFTER_MAIN)
1275
0
    {
1276
0
      new_reph_pos = base;
1277
0
      while (new_reph_pos + 1 < end && info[new_reph_pos + 1].indic_position() <= POS_AFTER_MAIN)
1278
0
  new_reph_pos++;
1279
0
      if (new_reph_pos < end)
1280
0
  goto reph_move;
1281
0
    }
1282
1283
    /*       4. If reph should be positioned before post-base consonant, find
1284
     *          first post-base classified consonant not ligated with main. If no
1285
     *          consonant is found, the target position should be before the
1286
     *          first matra, syllable modifier sign or vedic sign.
1287
     */
1288
    /* This is our take on what step 4 is trying to say (and failing, BADLY). */
1289
0
    if (reph_pos == REPH_POS_AFTER_SUB)
1290
0
    {
1291
0
      new_reph_pos = base;
1292
0
      while (new_reph_pos + 1 < end &&
1293
0
       !( FLAG_UNSAFE (info[new_reph_pos + 1].indic_position()) & (FLAG (POS_POST_C) | FLAG (POS_AFTER_POST) | FLAG (POS_SMVD))))
1294
0
  new_reph_pos++;
1295
0
      if (new_reph_pos < end)
1296
0
  goto reph_move;
1297
0
    }
1298
1299
    /*       5. If no consonant is found in steps 3 or 4, move reph to a position
1300
     *          immediately before the first post-base matra, syllable modifier
1301
     *          sign or vedic sign that has a reordering class after the intended
1302
     *          reph position. For example, if the reordering position for reph
1303
     *          is post-main, it will skip above-base matras that also have a
1304
     *          post-main position.
1305
     */
1306
0
    reph_step_5:
1307
0
    {
1308
      /* Copied from step 2. */
1309
0
      new_reph_pos = start + 1;
1310
0
      while (new_reph_pos < base && !is_halant (info[new_reph_pos]))
1311
0
  new_reph_pos++;
1312
1313
0
      if (new_reph_pos < base && is_halant (info[new_reph_pos]))
1314
0
      {
1315
  /* ->If ZWJ or ZWNJ are following this halant, position is moved after it. */
1316
0
  if (new_reph_pos + 1 < base && is_joiner (info[new_reph_pos + 1]))
1317
0
    new_reph_pos++;
1318
0
  goto reph_move;
1319
0
      }
1320
0
    }
1321
    /* See https://github.com/harfbuzz/harfbuzz/issues/2298#issuecomment-615318654 */
1322
1323
    /*       6. Otherwise, reorder reph to the end of the syllable.
1324
     */
1325
0
    {
1326
0
      new_reph_pos = end - 1;
1327
0
      while (new_reph_pos > start && info[new_reph_pos].indic_position() == POS_SMVD)
1328
0
  new_reph_pos--;
1329
1330
      /*
1331
       * If the Reph is to be ending up after a Matra,Halant sequence,
1332
       * position it before that Halant so it can interact with the Matra.
1333
       * However, if it's a plain Consonant,Halant we shouldn't do that.
1334
       * Uniscribe doesn't do this.
1335
       * TEST: U+0930,U+094D,U+0915,U+094B,U+094D
1336
       */
1337
0
      if (unlikely (is_halant (info[new_reph_pos])))
1338
0
      {
1339
0
  for (unsigned int i = base + 1; i < new_reph_pos; i++)
1340
0
    if (FLAG_UNSAFE (info[i].indic_category()) & (FLAG (I_Cat(M)) | FLAG (I_Cat(MPst))))
1341
0
    {
1342
      /* Ok, got it. */
1343
0
      new_reph_pos--;
1344
0
    }
1345
0
      }
1346
1347
0
      goto reph_move;
1348
0
    }
1349
1350
0
    reph_move:
1351
0
    {
1352
      /* Move */
1353
0
      buffer->merge_clusters (start, new_reph_pos + 1);
1354
0
      hb_glyph_info_t reph = info[start];
1355
0
      memmove (&info[start], &info[start + 1], (new_reph_pos - start) * sizeof (info[0]));
1356
0
      info[new_reph_pos] = reph;
1357
1358
0
      if (start < base && base <= new_reph_pos)
1359
0
  base--;
1360
0
    }
1361
0
  }
1362
1363
1364
  /*   o Reorder pre-base-reordering consonants:
1365
   *
1366
   *     If a pre-base-reordering consonant is found, reorder it according to
1367
   *     the following rules:
1368
   */
1369
1370
0
  if (try_pref && base + 1 < end) /* Otherwise there can't be any pre-base-reordering Ra. */
1371
0
  {
1372
0
    for (unsigned int i = base + 1; i < end; i++)
1373
0
      if ((info[i].mask & indic_plan->mask_array[INDIC_PREF]) != 0)
1374
0
      {
1375
  /*       1. Only reorder a glyph produced by substitution during application
1376
   *          of the <pref> feature. (Note that a font may shape a Ra consonant with
1377
   *          the feature generally but block it in certain contexts.)
1378
   */
1379
  /* Note: We just check that something got substituted.  We don't check that
1380
   * the <pref> feature actually did it...
1381
   *
1382
   * Reorder pref only if it ligated. */
1383
0
  if (_hb_glyph_info_ligated_and_didnt_multiply (&info[i]))
1384
0
  {
1385
    /*
1386
     *       2. Try to find a target position the same way as for pre-base matra.
1387
     *          If it is found, reorder pre-base consonant glyph.
1388
     *
1389
     *       3. If position is not found, reorder immediately before main
1390
     *          consonant.
1391
     */
1392
1393
0
    unsigned int new_pos = base;
1394
    /* Malayalam / Tamil do not have "half" forms or explicit virama forms.
1395
     * The glyphs formed by 'half' are Chillus or ligated explicit viramas.
1396
     * We want to position matra after them.
1397
     */
1398
0
    if (buffer->props.script != HB_SCRIPT_MALAYALAM && buffer->props.script != HB_SCRIPT_TAMIL)
1399
0
    {
1400
0
      while (new_pos > start &&
1401
0
       !(is_one_of (info[new_pos - 1], FLAG (I_Cat(M)) | FLAG (I_Cat(MPst)) | FLAG (I_Cat(H)))))
1402
0
        new_pos--;
1403
0
    }
1404
1405
0
    if (new_pos > start && is_halant (info[new_pos - 1]))
1406
0
    {
1407
      /* -> If ZWJ or ZWNJ follow this halant, position is moved after it. */
1408
0
      if (new_pos < end && is_joiner (info[new_pos]))
1409
0
        new_pos++;
1410
0
    }
1411
1412
0
    {
1413
0
      unsigned int old_pos = i;
1414
1415
0
      buffer->merge_clusters (new_pos, old_pos + 1);
1416
0
      hb_glyph_info_t tmp = info[old_pos];
1417
0
      memmove (&info[new_pos + 1], &info[new_pos], (old_pos - new_pos) * sizeof (info[0]));
1418
0
      info[new_pos] = tmp;
1419
1420
0
      if (new_pos <= base && base < old_pos)
1421
0
        base++;
1422
0
    }
1423
0
  }
1424
1425
0
  break;
1426
0
      }
1427
0
  }
1428
1429
1430
  /* Apply 'init' to the Left Matra if it's a word start. */
1431
0
  if (info[start].indic_position () == POS_PRE_M)
1432
0
  {
1433
0
    if (!start ||
1434
0
  !(FLAG_UNSAFE (_hb_glyph_info_get_general_category (&info[start - 1])) &
1435
0
   FLAG_RANGE (HB_UNICODE_GENERAL_CATEGORY_FORMAT, HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK)))
1436
0
      info[start].mask |= indic_plan->mask_array[INDIC_INIT];
1437
0
    else
1438
0
      buffer->unsafe_to_break (start - 1, start + 1);
1439
0
  }
1440
0
}
1441
1442
1443
static bool
1444
final_reordering_indic (const hb_ot_shape_plan_t *plan,
1445
      hb_font_t *font HB_UNUSED,
1446
      hb_buffer_t *buffer)
1447
0
{
1448
0
  unsigned int count = buffer->len;
1449
0
  if (unlikely (!count)) return false;
1450
1451
0
  if (buffer->message (font, "start reordering indic final")) {
1452
0
    foreach_syllable (buffer, start, end)
1453
0
      final_reordering_syllable_indic (plan, buffer, start, end);
1454
0
    (void) buffer->message (font, "end reordering indic final");
1455
0
  }
1456
1457
0
  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_category);
1458
0
  HB_BUFFER_DEALLOCATE_VAR (buffer, indic_position);
1459
1460
0
  return false;
1461
0
}
1462
1463
1464
static void
1465
preprocess_text_indic (const hb_ot_shape_plan_t *plan,
1466
           hb_buffer_t              *buffer,
1467
           hb_font_t                *font)
1468
0
{
1469
0
  _hb_preprocess_text_vowel_constraints (plan, buffer, font);
1470
0
}
1471
1472
static bool
1473
decompose_indic (const hb_ot_shape_normalize_context_t *c,
1474
     hb_codepoint_t  ab,
1475
     hb_codepoint_t *a,
1476
     hb_codepoint_t *b)
1477
0
{
1478
0
  switch (ab)
1479
0
  {
1480
    /* Don't decompose these. */
1481
0
    case 0x0931u  : return false; /* DEVANAGARI LETTER RRA */
1482
    // https://github.com/harfbuzz/harfbuzz/issues/779
1483
0
    case 0x09DCu  : return false; /* BENGALI LETTER RRA */
1484
0
    case 0x09DDu  : return false; /* BENGALI LETTER RHA */
1485
0
    case 0x0B94u  : return false; /* TAMIL LETTER AU */
1486
1487
1488
    /*
1489
     * Decompose split matras that don't have Unicode decompositions.
1490
     */
1491
1492
#if 0
1493
    /* Gujarati */
1494
    /* This one has no decomposition in Unicode, but needs no decomposition either. */
1495
    /* case 0x0AC9u  : return false; */
1496
1497
    /* Oriya */
1498
    case 0x0B57u  : *a = no decomp, -> RIGHT; return true;
1499
#endif
1500
0
  }
1501
1502
0
  return (bool) c->unicode->decompose (ab, a, b);
1503
0
}
1504
1505
static bool
1506
compose_indic (const hb_ot_shape_normalize_context_t *c,
1507
         hb_codepoint_t  a,
1508
         hb_codepoint_t  b,
1509
         hb_codepoint_t *ab)
1510
0
{
1511
  /* Avoid recomposing split matras. */
1512
0
  if (HB_UNICODE_GENERAL_CATEGORY_IS_MARK (c->unicode->general_category (a)))
1513
0
    return false;
1514
1515
  /* Composition-exclusion exceptions that we want to recompose. */
1516
0
  if (a == 0x09AFu && b == 0x09BCu) { *ab = 0x09DFu; return true; }
1517
1518
0
  return (bool) c->unicode->compose (a, b, ab);
1519
0
}
1520
1521
1522
const hb_ot_shaper_t _hb_ot_shaper_indic =
1523
{
1524
  collect_features_indic,
1525
  override_features_indic,
1526
  data_create_indic,
1527
  data_destroy_indic,
1528
  preprocess_text_indic,
1529
  nullptr, /* postprocess_glyphs */
1530
  decompose_indic,
1531
  compose_indic,
1532
  setup_masks_indic,
1533
  nullptr, /* reorder_marks */
1534
  HB_TAG_NONE, /* gpos_tag */
1535
  HB_OT_SHAPE_NORMALIZATION_MODE_COMPOSED_DIACRITICS_NO_SHORT_CIRCUIT,
1536
  HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE,
1537
  false, /* fallback_position */
1538
};
1539
1540
1541
#endif