Coverage Report

Created: 2026-12-01 07:15

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