Coverage Report

Created: 2026-03-07 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/harfbuzz/src/hb-ot-shape.cc
Line
Count
Source
1
/*
2
 * Copyright © 2009,2010  Red Hat, Inc.
3
 * Copyright © 2010,2011,2012  Google, Inc.
4
 *
5
 *  This is part of HarfBuzz, a text shaping library.
6
 *
7
 * Permission is hereby granted, without written agreement and without
8
 * license or royalty fees, to use, copy, modify, and distribute this
9
 * software and its documentation for any purpose, provided that the
10
 * above copyright notice and the following two paragraphs appear in
11
 * all copies of this software.
12
 *
13
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
17
 * DAMAGE.
18
 *
19
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
22
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24
 *
25
 * Red Hat Author(s): Behdad Esfahbod
26
 * Google Author(s): Behdad Esfahbod
27
 */
28
29
#include "hb.hh"
30
31
#ifndef HB_NO_OT_SHAPE
32
33
#ifdef HB_NO_OT_LAYOUT
34
#error "Cannot compile 'ot' shaper with HB_NO_OT_LAYOUT."
35
#endif
36
37
#include "hb-shaper-impl.hh"
38
39
#include "hb-ot-shape.hh"
40
#include "hb-ot-shaper.hh"
41
#include "hb-ot-shape-fallback.hh"
42
#include "hb-ot-shape-normalize.hh"
43
44
#include "hb-ot-face.hh"
45
46
#include "hb-set.hh"
47
#include "hb-unicode.hh"
48
49
#include "hb-aat-layout.hh"
50
#include "hb-ot-layout-gdef-table.hh"
51
#include "hb-ot-stat-table.hh"
52
53
54
static inline bool
55
_hb_codepoint_is_regional_indicator (hb_codepoint_t u)
56
308k
{ return hb_in_range<hb_codepoint_t> (u, 0x1F1E6u, 0x1F1FFu); }
57
58
#ifndef HB_NO_AAT_SHAPE
59
static inline bool
60
_hb_apply_morx (hb_face_t *face, const hb_segment_properties_t &props)
61
52.3k
{
62
  /* https://github.com/harfbuzz/harfbuzz/issues/2124 */
63
52.3k
  return hb_aat_layout_has_substitution (face) &&
64
4.04k
   (HB_DIRECTION_IS_HORIZONTAL (props.direction) || !hb_ot_layout_has_substitution (face));
65
52.3k
}
66
#endif
67
68
/**
69
 * SECTION:hb-ot-shape
70
 * @title: hb-ot-shape
71
 * @short_description: OpenType shaping support
72
 * @include: hb-ot.h
73
 *
74
 * Support functions for OpenType shaping related queries.
75
 **/
76
77
78
static void
79
hb_ot_shape_collect_features (hb_ot_shape_planner_t          *planner,
80
            const hb_feature_t             *user_features,
81
            unsigned int                    num_user_features);
82
83
hb_ot_shape_planner_t::hb_ot_shape_planner_t (hb_face_t                     *face,
84
                const hb_segment_properties_t &props) :
85
52.3k
            face (face),
86
52.3k
            props (props),
87
52.3k
            map (face, props)
88
#ifndef HB_NO_AAT_SHAPE
89
52.3k
            , aat_map (face, props)
90
52.3k
            , apply_morx (_hb_apply_morx (face, props))
91
#endif
92
52.3k
{
93
52.3k
  shaper = hb_ot_shaper_categorize (props.script, props.direction, map.chosen_script[0]);
94
95
52.3k
  script_zero_marks = shaper->zero_width_marks != HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE;
96
52.3k
  script_fallback_position = shaper->fallback_position;
97
98
52.3k
#ifndef HB_NO_AAT_SHAPE
99
  /* https://github.com/harfbuzz/harfbuzz/issues/1528 */
100
52.3k
  if (apply_morx && shaper != &_hb_ot_shaper_default)
101
266
    shaper = &_hb_ot_shaper_dumber;
102
52.3k
#endif
103
52.3k
}
104
105
void
106
hb_ot_shape_planner_t::compile (hb_ot_shape_plan_t           &plan,
107
        const hb_ot_shape_plan_key_t &key)
108
52.3k
{
109
52.3k
  plan.props = props;
110
52.3k
  plan.shaper = shaper;
111
52.3k
  map.compile (plan.map, key);
112
52.3k
#ifndef HB_NO_AAT_SHAPE
113
52.3k
  if (apply_morx)
114
4.04k
    aat_map.compile (plan.aat_map);
115
52.3k
#endif
116
117
52.3k
#ifndef HB_NO_OT_SHAPE_FRACTIONS
118
52.3k
  plan.frac_mask = plan.map.get_1_mask (HB_TAG ('f','r','a','c'));
119
52.3k
  plan.numr_mask = plan.map.get_1_mask (HB_TAG ('n','u','m','r'));
120
52.3k
  plan.dnom_mask = plan.map.get_1_mask (HB_TAG ('d','n','o','m'));
121
52.3k
  plan.has_frac = plan.frac_mask || (plan.numr_mask && plan.dnom_mask);
122
52.3k
#endif
123
124
52.3k
  plan.rtlm_mask = plan.map.get_1_mask (HB_TAG ('r','t','l','m'));
125
52.3k
  plan.has_vert = !!plan.map.get_1_mask (HB_TAG ('v','e','r','t'));
126
127
52.3k
  hb_tag_t kern_tag = HB_DIRECTION_IS_HORIZONTAL (props.direction) ?
128
52.3k
          HB_TAG ('k','e','r','n') : HB_TAG ('v','k','r','n');
129
52.3k
#ifndef HB_NO_OT_KERN
130
52.3k
  plan.kern_mask = plan.map.get_mask (kern_tag);
131
52.3k
  plan.requested_kerning = !!plan.kern_mask;
132
52.3k
#endif
133
134
52.3k
  bool has_gpos_kern = plan.map.get_feature_index (1, kern_tag) != HB_OT_LAYOUT_NO_FEATURE_INDEX;
135
52.3k
  bool disable_gpos = plan.shaper->gpos_tag &&
136
517
          plan.shaper->gpos_tag != plan.map.chosen_script[1];
137
138
  /*
139
   * Decide who provides glyph classes. GDEF or Unicode.
140
   */
141
142
52.3k
  if (!hb_ot_layout_has_glyph_classes (face))
143
51.0k
    plan.fallback_glyph_classes = true;
144
145
  /*
146
   * Decide who does substitutions. GSUB, morx, or fallback.
147
   */
148
149
52.3k
#ifndef HB_NO_AAT_SHAPE
150
52.3k
  plan.apply_morx = apply_morx;
151
52.3k
#endif
152
153
  /*
154
   * Decide who does positioning. GPOS, kerx, kern, or fallback.
155
   */
156
157
52.3k
#ifndef HB_NO_AAT_SHAPE
158
52.3k
  bool has_kerx = hb_aat_layout_has_positioning (face);
159
52.3k
  bool has_gsub = !apply_morx && hb_ot_layout_has_substitution (face);
160
52.3k
#endif
161
52.3k
  bool has_gpos = !disable_gpos && hb_ot_layout_has_positioning (face);
162
52.3k
  if (false)
163
0
    {}
164
52.3k
#ifndef HB_NO_AAT_SHAPE
165
  /* Prefer GPOS over kerx if GSUB is present;
166
   * https://github.com/harfbuzz/harfbuzz/issues/3008 */
167
52.3k
  else if (has_kerx && !(has_gsub && has_gpos))
168
2.05k
    plan.apply_kerx = true;
169
50.3k
#endif
170
50.3k
  else if (has_gpos)
171
5.79k
    plan.apply_gpos = true;
172
173
52.3k
  if (!plan.apply_kerx && (!has_gpos_kern || !plan.apply_gpos))
174
49.7k
  {
175
49.7k
    if (false) {}
176
49.7k
#ifndef HB_NO_AAT_SHAPE
177
49.7k
    else if (has_kerx)
178
6
      plan.apply_kerx = true;
179
49.7k
#endif
180
49.7k
#ifndef HB_NO_OT_KERN
181
49.7k
    else if (hb_ot_layout_has_kerning (face))
182
2.08k
      plan.apply_kern = script_fallback_position; // Not all shapers apply legacy `kern`
183
47.6k
#endif
184
47.6k
    else {}
185
49.7k
  }
186
187
52.3k
  plan.apply_fallback_kern = script_fallback_position && !(plan.apply_gpos || plan.apply_kerx || plan.apply_kern);
188
189
52.3k
  plan.zero_marks = script_zero_marks &&
190
48.6k
        !plan.apply_kerx &&
191
46.6k
        (!plan.apply_kern
192
2.03k
#ifndef HB_NO_OT_KERN
193
2.03k
         || !hb_ot_layout_has_machine_kerning (face)
194
46.6k
#endif
195
46.6k
        );
196
52.3k
  plan.has_gpos_mark = !!plan.map.get_1_mask (HB_TAG ('m','a','r','k'));
197
198
52.3k
  plan.adjust_mark_positioning_when_zeroing = !plan.apply_gpos &&
199
46.5k
                !plan.apply_kerx &&
200
44.5k
                (!plan.apply_kern
201
2.03k
#ifndef HB_NO_OT_KERN
202
2.03k
                 || !hb_ot_layout_has_cross_kerning (face)
203
44.5k
#endif
204
44.5k
                );
205
206
52.3k
  plan.fallback_mark_positioning = plan.adjust_mark_positioning_when_zeroing &&
207
43.5k
           script_fallback_position;
208
209
52.3k
#ifndef HB_NO_AAT_SHAPE
210
  /* If we're using morx shaping, we cancel mark position adjustment because
211
     Apple Color Emoji assumes this will NOT be done when forming emoji sequences;
212
     https://github.com/harfbuzz/harfbuzz/issues/2967. */
213
52.3k
  if (plan.apply_morx)
214
4.04k
    plan.adjust_mark_positioning_when_zeroing = false;
215
216
  /* According to Ned, trak is applied by default for "modern fonts", as detected by presence of STAT table. */
217
52.3k
#ifndef HB_NO_STYLE
218
52.3k
  plan.apply_trak = hb_aat_layout_has_tracking (face) && face->table.STAT->has_data ();
219
#else
220
  plan.apply_trak = false;
221
#endif
222
223
52.3k
#endif
224
52.3k
}
225
226
bool
227
hb_ot_shape_plan_t::init0 (hb_face_t                     *face,
228
         const hb_shape_plan_key_t     *key)
229
52.3k
{
230
52.3k
  map.init ();
231
232
52.3k
  hb_ot_shape_planner_t planner (face,
233
52.3k
         key->props);
234
235
52.3k
  hb_ot_shape_collect_features (&planner,
236
52.3k
        key->user_features,
237
52.3k
        key->num_user_features);
238
239
52.3k
  planner.compile (*this, key->ot);
240
241
52.3k
  if (shaper->data_create)
242
7.75k
  {
243
7.75k
    data = shaper->data_create (this);
244
7.75k
    if (unlikely (!data))
245
40
    {
246
40
      map.fini ();
247
40
      return false;
248
40
    }
249
7.75k
  }
250
251
52.3k
  return true;
252
52.3k
}
253
254
void
255
hb_ot_shape_plan_t::fini ()
256
52.3k
{
257
52.3k
  if (shaper->data_destroy)
258
7.71k
    shaper->data_destroy (const_cast<void *> (data));
259
260
52.3k
  map.fini ();
261
52.3k
}
262
263
void
264
hb_ot_shape_plan_t::substitute (hb_font_t   *font,
265
        hb_buffer_t *buffer) const
266
91.6k
{
267
91.6k
  map.substitute (this, font, buffer);
268
91.6k
}
269
270
void
271
hb_ot_shape_plan_t::position (hb_font_t   *font,
272
            hb_buffer_t *buffer) const
273
101k
{
274
101k
  if (this->apply_gpos)
275
10.6k
    map.position (this, font, buffer);
276
90.4k
#ifndef HB_NO_AAT_SHAPE
277
90.4k
  else if (this->apply_kerx)
278
3.86k
    hb_aat_layout_position (this, font, buffer);
279
101k
#endif
280
281
101k
#ifndef HB_NO_OT_KERN
282
101k
  if (this->apply_kern)
283
4.14k
    hb_ot_layout_kern (this, font, buffer);
284
96.9k
#endif
285
96.9k
  else if (this->apply_fallback_kern)
286
76.6k
    _hb_ot_shape_fallback_kern (this, font, buffer);
287
288
101k
#ifndef HB_NO_AAT_SHAPE
289
101k
  if (this->apply_trak)
290
122
    hb_aat_layout_track (this, font, buffer);
291
101k
#endif
292
101k
}
293
294
295
static const hb_ot_map_feature_t
296
common_features[] =
297
{
298
  {HB_TAG('a','b','v','m'), F_GLOBAL},
299
  {HB_TAG('b','l','w','m'), F_GLOBAL},
300
  {HB_TAG('c','c','m','p'), F_GLOBAL},
301
  {HB_TAG('l','o','c','l'), F_GLOBAL},
302
  {HB_TAG('m','a','r','k'), F_GLOBAL_MANUAL_JOINERS},
303
  {HB_TAG('m','k','m','k'), F_GLOBAL_MANUAL_JOINERS},
304
  {HB_TAG('r','l','i','g'), F_GLOBAL},
305
};
306
307
308
static const hb_ot_map_feature_t
309
horizontal_features[] =
310
{
311
  {HB_TAG('c','a','l','t'), F_GLOBAL},
312
  {HB_TAG('c','l','i','g'), F_GLOBAL},
313
  {HB_TAG('c','u','r','s'), F_GLOBAL},
314
  {HB_TAG('d','i','s','t'), F_GLOBAL},
315
  {HB_TAG('k','e','r','n'), F_GLOBAL_HAS_FALLBACK},
316
  {HB_TAG('l','i','g','a'), F_GLOBAL},
317
  {HB_TAG('r','c','l','t'), F_GLOBAL},
318
};
319
320
static void
321
hb_ot_shape_collect_features (hb_ot_shape_planner_t *planner,
322
            const hb_feature_t    *user_features,
323
            unsigned int           num_user_features)
324
52.3k
{
325
52.3k
  hb_ot_map_builder_t *map = &planner->map;
326
327
52.3k
  map->is_simple = true;
328
329
52.3k
  map->enable_feature (HB_TAG('r','v','r','n'));
330
52.3k
  map->add_gsub_pause (nullptr);
331
332
52.3k
  switch (planner->props.direction)
333
52.3k
  {
334
49.6k
    case HB_DIRECTION_LTR:
335
49.6k
      map->enable_feature (HB_TAG ('l','t','r','a'));
336
49.6k
      map->enable_feature (HB_TAG ('l','t','r','m'));
337
49.6k
      break;
338
2.72k
    case HB_DIRECTION_RTL:
339
2.72k
      map->enable_feature (HB_TAG ('r','t','l','a'));
340
2.72k
      map->add_feature (HB_TAG ('r','t','l','m'));
341
2.72k
      break;
342
0
    case HB_DIRECTION_TTB:
343
0
    case HB_DIRECTION_BTT:
344
0
    case HB_DIRECTION_INVALID:
345
0
    default:
346
0
      break;
347
52.3k
  }
348
349
52.3k
#ifndef HB_NO_OT_SHAPE_FRACTIONS
350
  /* Automatic fractions. */
351
52.3k
  map->add_feature (HB_TAG ('f','r','a','c'));
352
52.3k
  map->add_feature (HB_TAG ('n','u','m','r'));
353
52.3k
  map->add_feature (HB_TAG ('d','n','o','m'));
354
52.3k
#endif
355
356
  /* Random! */
357
52.3k
  map->enable_feature (HB_TAG ('r','a','n','d'), F_RANDOM, HB_OT_MAP_MAX_VALUE);
358
359
52.3k
  map->enable_feature (HB_TAG ('H','a','r','f')); /* Considered required. */
360
52.3k
  map->enable_feature (HB_TAG ('H','A','R','F')); /* Considered discretionary. */
361
362
52.3k
  if (planner->shaper->collect_features)
363
8.17k
  {
364
8.17k
    map->is_simple = false;
365
8.17k
    planner->shaper->collect_features (planner);
366
8.17k
  }
367
368
52.3k
  map->enable_feature (HB_TAG ('B','u','z','z')); /* Considered required. */
369
52.3k
  map->enable_feature (HB_TAG ('B','U','Z','Z')); /* Considered discretionary. */
370
371
418k
  for (unsigned int i = 0; i < ARRAY_LENGTH (common_features); i++)
372
366k
    map->add_feature (common_features[i]);
373
374
52.3k
  if (HB_DIRECTION_IS_HORIZONTAL (planner->props.direction))
375
418k
    for (unsigned int i = 0; i < ARRAY_LENGTH (horizontal_features); i++)
376
366k
      map->add_feature (horizontal_features[i]);
377
0
  else
378
0
  {
379
    /* We only apply `vert` feature. See:
380
     * https://github.com/harfbuzz/harfbuzz/commit/d71c0df2d17f4590d5611239577a6cb532c26528
381
     * https://lists.freedesktop.org/archives/harfbuzz/2013-August/003490.html */
382
383
    /* We really want to find a 'vert' feature if there's any in the font, no
384
     * matter which script/langsys it is listed (or not) under.
385
     * See various bugs referenced from:
386
     * https://github.com/harfbuzz/harfbuzz/issues/63 */
387
0
    map->enable_feature (HB_TAG ('v','e','r','t'), F_GLOBAL_SEARCH);
388
0
  }
389
390
52.3k
  if (num_user_features)
391
0
    map->is_simple = false;
392
52.3k
  for (unsigned int i = 0; i < num_user_features; i++)
393
0
  {
394
0
    const hb_feature_t *feature = &user_features[i];
395
0
    map->add_feature (feature->tag,
396
0
          (feature->start == HB_FEATURE_GLOBAL_START &&
397
0
           feature->end == HB_FEATURE_GLOBAL_END) ?  F_GLOBAL : F_NONE,
398
0
          feature->value);
399
0
  }
400
401
52.3k
  if (planner->shaper->override_features)
402
3.64k
    planner->shaper->override_features (planner);
403
52.3k
}
404
405
406
/*
407
 * shaper face data
408
 */
409
410
struct hb_ot_face_data_t {};
411
412
hb_ot_face_data_t *
413
_hb_ot_shaper_face_data_create (hb_face_t *face)
414
34.9k
{
415
34.9k
  return (hb_ot_face_data_t *) HB_SHAPER_DATA_SUCCEEDED;
416
34.9k
}
417
418
void
419
_hb_ot_shaper_face_data_destroy (hb_ot_face_data_t *data)
420
34.9k
{
421
34.9k
}
422
423
424
/*
425
 * shaper font data
426
 */
427
428
struct hb_ot_font_data_t {
429
  OT::hb_scalar_cache_t unused; // Just for alignment
430
};
431
432
hb_ot_font_data_t *
433
_hb_ot_shaper_font_data_create (hb_font_t *font)
434
34.8k
{
435
34.8k
  const OT::ItemVariationStore &var_store = font->face->table.GDEF->table->get_var_store ();
436
34.8k
  return (hb_ot_font_data_t *) var_store.create_cache ();
437
34.8k
}
438
439
void
440
_hb_ot_shaper_font_data_destroy (hb_ot_font_data_t *data)
441
34.8k
{
442
34.8k
  OT::ItemVariationStore::destroy_cache ((OT::hb_scalar_cache_t *) data);
443
34.8k
}
444
445
446
/*
447
 * shaper
448
 */
449
450
struct hb_ot_shape_context_t
451
{
452
  hb_ot_shape_plan_t *plan;
453
  hb_font_t *font;
454
  hb_face_t *face;
455
  hb_buffer_t  *buffer;
456
  const hb_feature_t *user_features;
457
  unsigned int        num_user_features;
458
459
  /* Transient stuff */
460
  hb_direction_t target_direction;
461
};
462
463
464
465
/* Main shaper */
466
467
468
/* Prepare */
469
470
static void
471
hb_set_unicode_props (hb_buffer_t *buffer)
472
101k
{
473
  /* Implement enough of Unicode Graphemes here that shaping
474
   * in reverse-direction wouldn't break graphemes.  Namely,
475
   * we mark all marks and ZWJ and ZWJ,Extended_Pictographic
476
   * sequences as continuations.  The foreach_grapheme()
477
   * macro uses this bit.
478
   *
479
   * https://www.unicode.org/reports/tr29/#Regex_Definitions
480
   */
481
101k
  unsigned int count = buffer->len;
482
101k
  hb_glyph_info_t *info = buffer->info;
483
1.34M
  for (unsigned int i = 0; i < count; i++)
484
1.24M
  {
485
1.24M
    _hb_glyph_info_set_unicode_props (&info[i], buffer);
486
487
1.24M
    if (info[i].codepoint < 0x80)
488
825k
      continue;
489
490
415k
    unsigned gen_cat = _hb_glyph_info_get_general_category (&info[i]);
491
415k
    if (FLAG_UNSAFE (gen_cat) &
492
415k
  (FLAG (HB_UNICODE_GENERAL_CATEGORY_LOWERCASE_LETTER) |
493
415k
   FLAG (HB_UNICODE_GENERAL_CATEGORY_UPPERCASE_LETTER) |
494
415k
   FLAG (HB_UNICODE_GENERAL_CATEGORY_TITLECASE_LETTER) |
495
415k
   FLAG (HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER) |
496
415k
   FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACE_SEPARATOR)))
497
84.6k
      continue;
498
499
    /* Marks are already set as continuation by the above line.
500
     * Handle Emoji_Modifier and ZWJ-continuation. */
501
330k
    if (unlikely (gen_cat == HB_UNICODE_GENERAL_CATEGORY_MODIFIER_SYMBOL &&
502
330k
      hb_in_range<hb_codepoint_t> (info[i].codepoint, 0x1F3FBu, 0x1F3FFu)))
503
18
    {
504
18
      _hb_glyph_info_set_continuation (&info[i], buffer);
505
18
    }
506
    /* Regional_Indicators are hairy as hell...
507
     * https://github.com/harfbuzz/harfbuzz/issues/2265 */
508
330k
    else if (unlikely (i && _hb_codepoint_is_regional_indicator (info[i].codepoint)))
509
43
    {
510
43
      if (_hb_codepoint_is_regional_indicator (info[i - 1].codepoint) &&
511
30
    !_hb_glyph_info_is_continuation (&info[i - 1]))
512
17
  _hb_glyph_info_set_continuation (&info[i], buffer);
513
43
    }
514
330k
#ifndef HB_NO_EMOJI_SEQUENCES
515
330k
    else if (unlikely (_hb_glyph_info_is_zwj (&info[i])))
516
2.01k
    {
517
2.01k
      _hb_glyph_info_set_continuation (&info[i], buffer);
518
2.01k
      if (i + 1 < count &&
519
1.92k
    _hb_unicode_is_emoji_Extended_Pictographic (info[i + 1].codepoint))
520
11
      {
521
11
  i++;
522
11
  _hb_glyph_info_set_unicode_props (&info[i], buffer);
523
11
  _hb_glyph_info_set_continuation (&info[i], buffer);
524
11
      }
525
2.01k
    }
526
328k
#endif
527
    /* Or part of the Other_Grapheme_Extend that is not marks.
528
     * As of Unicode 15 that is just:
529
     *
530
     * 200C          ; Other_Grapheme_Extend # Cf       ZERO WIDTH NON-JOINER
531
     * FF9E..FF9F    ; Other_Grapheme_Extend # Lm   [2] HALFWIDTH KATAKANA VOICED SOUND MARK..HALFWIDTH KATAKANA SEMI-VOICED SOUND MARK
532
     * E0020..E007F  ; Other_Grapheme_Extend # Cf  [96] TAG SPACE..CANCEL TAG
533
     *
534
     * ZWNJ is special, we don't want to merge it as there's no need, and keeping
535
     * it separate results in more granular clusters.
536
     * Tags are used for Emoji sub-region flag sequences:
537
     * https://github.com/harfbuzz/harfbuzz/issues/1556
538
     * Katakana ones were requested:
539
     * https://github.com/harfbuzz/harfbuzz/issues/3844
540
     */
541
328k
    else if (unlikely (hb_in_ranges<hb_codepoint_t> (info[i].codepoint, 0xFF9Eu, 0xFF9Fu, 0xE0020u, 0xE007Fu)))
542
307
      _hb_glyph_info_set_continuation (&info[i], buffer);
543
328k
    else if (unlikely (info[i].codepoint == 0x2044u /* FRACTION SLASH */))
544
64
      buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_FRACTION_SLASH;
545
330k
  }
546
101k
}
547
548
static void
549
hb_insert_dotted_circle (hb_buffer_t *buffer, hb_font_t *font)
550
101k
{
551
101k
  if (unlikely (buffer->flags & HB_BUFFER_FLAG_DO_NOT_INSERT_DOTTED_CIRCLE))
552
0
    return;
553
554
101k
  if (!(buffer->flags & HB_BUFFER_FLAG_BOT) ||
555
0
      buffer->context_len[0] ||
556
0
      !_hb_glyph_info_is_unicode_mark (&buffer->info[0]))
557
101k
    return;
558
559
0
  if (!font->has_glyph (0x25CCu))
560
0
    return;
561
562
0
  hb_glyph_info_t dottedcircle = {0};
563
0
  dottedcircle.codepoint = 0x25CCu;
564
0
  _hb_glyph_info_set_unicode_props (&dottedcircle, buffer);
565
566
0
  buffer->clear_output ();
567
568
0
  buffer->idx = 0;
569
0
  hb_glyph_info_t info = dottedcircle;
570
0
  info.cluster = buffer->cur().cluster;
571
0
  info.mask = buffer->cur().mask;
572
0
  (void) buffer->output_info (info);
573
574
0
  buffer->sync ();
575
0
}
576
577
static void
578
hb_form_clusters (hb_buffer_t *buffer)
579
101k
{
580
101k
  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_CONTINUATIONS))
581
87.3k
    return;
582
583
13.7k
  foreach_grapheme (buffer, start, end)
584
187k
    buffer->merge_grapheme_clusters (start, end);
585
13.7k
}
586
587
static void
588
hb_ensure_native_direction (hb_buffer_t *buffer)
589
101k
{
590
101k
  hb_direction_t direction = buffer->props.direction;
591
101k
  hb_direction_t horiz_dir = hb_script_get_horizontal_direction (buffer->props.script);
592
593
  /* Numeric runs in natively-RTL scripts are actually native-LTR, so we reset
594
   * the horiz_dir if the run contains at least one decimal-number char, and no
595
   * letter chars (ideally we should be checking for chars with strong
596
   * directionality but hb-unicode currently lacks bidi categories).
597
   *
598
   * This allows digit sequences in Arabic etc to be shaped in "native"
599
   * direction, so that features like ligatures will work as intended.
600
   *
601
   * https://github.com/harfbuzz/harfbuzz/issues/501
602
   *
603
   * Similar thing about Regional_Indicators; They are bidi=L, but Script=Common.
604
   * If they are present in a run of natively-RTL text, they get assigned a script
605
   * with natively RTL direction, which would result in wrong shaping if we
606
   * assign such native RTL direction to them then. Detect that as well.
607
   *
608
   * https://github.com/harfbuzz/harfbuzz/issues/3314
609
   */
610
101k
  if (unlikely (horiz_dir == HB_DIRECTION_RTL && direction == HB_DIRECTION_LTR))
611
0
  {
612
0
    bool found_number = false, found_letter = false, found_ri = false;
613
0
    const auto* info = buffer->info;
614
0
    const auto count = buffer->len;
615
0
    for (unsigned i = 0; i < count; i++)
616
0
    {
617
0
      auto gc = _hb_glyph_info_get_general_category (&info[i]);
618
0
      if (HB_UNICODE_GENERAL_CATEGORY_IS_LETTER (gc))
619
0
      {
620
0
  found_letter = true;
621
0
  break;
622
0
      }
623
0
      else if (gc == HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
624
0
  found_number = true;
625
0
      else if (unlikely (_hb_codepoint_is_regional_indicator (info[i].codepoint)))
626
0
  found_ri = true;
627
0
    }
628
0
    if ((found_number || found_ri) && !found_letter)
629
0
      horiz_dir = HB_DIRECTION_LTR;
630
0
  }
631
632
  /* TODO vertical:
633
   * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
634
   * Ogham fonts are supposed to be implemented BTT or not.  Need to research that
635
   * first. */
636
101k
  if ((HB_DIRECTION_IS_HORIZONTAL (direction) &&
637
101k
       direction != horiz_dir && HB_DIRECTION_IS_VALID (horiz_dir)) ||
638
101k
      (HB_DIRECTION_IS_VERTICAL   (direction) &&
639
0
       direction != HB_DIRECTION_TTB))
640
0
  {
641
0
    _hb_ot_layout_reverse_graphemes (buffer);
642
0
    buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
643
0
  }
644
101k
}
645
646
647
/*
648
 * Substitute
649
 */
650
651
static inline void
652
hb_ot_rotate_chars (const hb_ot_shape_context_t *c)
653
101k
{
654
101k
  hb_buffer_t *buffer = c->buffer;
655
101k
  unsigned int count = buffer->len;
656
101k
  hb_glyph_info_t *info = buffer->info;
657
658
101k
  if (HB_DIRECTION_IS_BACKWARD (c->target_direction))
659
2.70k
  {
660
2.70k
    hb_unicode_funcs_t *unicode = buffer->unicode;
661
2.70k
    hb_mask_t rtlm_mask = c->plan->rtlm_mask;
662
663
46.0k
    for (unsigned int i = 0; i < count; i++) {
664
43.3k
      hb_codepoint_t codepoint = unicode->mirroring (info[i].codepoint);
665
43.3k
      if (unlikely (codepoint != info[i].codepoint && c->font->has_glyph (codepoint)))
666
19
  info[i].codepoint = codepoint;
667
43.3k
      else
668
43.3k
  info[i].mask |= rtlm_mask;
669
43.3k
    }
670
2.70k
  }
671
672
101k
#ifndef HB_NO_VERTICAL
673
101k
  if (HB_DIRECTION_IS_VERTICAL (c->target_direction) && !c->plan->has_vert)
674
0
  {
675
0
    for (unsigned int i = 0; i < count; i++) {
676
0
      hb_codepoint_t codepoint = hb_unicode_funcs_t::vertical_char_for (info[i].codepoint);
677
0
      if (unlikely (codepoint != info[i].codepoint && c->font->has_glyph (codepoint)))
678
0
  info[i].codepoint = codepoint;
679
0
    }
680
0
  }
681
101k
#endif
682
101k
}
683
684
static inline void
685
hb_ot_shape_setup_masks_fraction (const hb_ot_shape_context_t *c)
686
101k
{
687
#ifdef HB_NO_OT_SHAPE_FRACTIONS
688
  return;
689
#endif
690
691
101k
  if (!(c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_FRACTION_SLASH) ||
692
22
      !c->plan->has_frac)
693
101k
    return;
694
695
18
  hb_buffer_t *buffer = c->buffer;
696
697
18
  hb_mask_t pre_mask, post_mask;
698
18
  if (HB_DIRECTION_IS_FORWARD (buffer->props.direction))
699
17
  {
700
17
    pre_mask = c->plan->numr_mask | c->plan->frac_mask;
701
17
    post_mask = c->plan->frac_mask | c->plan->dnom_mask;
702
17
  }
703
1
  else
704
1
  {
705
1
    pre_mask = c->plan->frac_mask | c->plan->dnom_mask;
706
1
    post_mask = c->plan->numr_mask | c->plan->frac_mask;
707
1
  }
708
709
18
  unsigned int count = buffer->len;
710
18
  hb_glyph_info_t *info = buffer->info;
711
261
  for (unsigned int i = 0; i < count; i++)
712
243
  {
713
243
    if (info[i].codepoint == 0x2044u) /* FRACTION SLASH */
714
50
    {
715
50
      unsigned int start = i, end = i + 1;
716
96
      while (start &&
717
92
       _hb_glyph_info_get_general_category (&info[start - 1]) ==
718
92
       HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
719
46
  start--;
720
105
      while (end < count &&
721
95
       _hb_glyph_info_get_general_category (&info[end]) ==
722
95
       HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
723
55
  end++;
724
50
      if (start == i || end == i + 1)
725
31
      {
726
31
        if (start == i)
727
24
    buffer->unsafe_to_concat (start, start + 1);
728
31
  if (end == i + 1)
729
22
    buffer->unsafe_to_concat (end - 1, end);
730
31
  continue;
731
31
      }
732
733
19
      buffer->unsafe_to_break (start, end);
734
735
50
      for (unsigned int j = start; j < i; j++)
736
31
  info[j].mask |= pre_mask;
737
19
      info[i].mask |= c->plan->frac_mask;
738
65
      for (unsigned int j = i + 1; j < end; j++)
739
46
  info[j].mask |= post_mask;
740
741
19
      i = end - 1;
742
19
    }
743
243
  }
744
18
}
745
746
static inline void
747
hb_ot_shape_initialize_masks (const hb_ot_shape_context_t *c)
748
101k
{
749
101k
  hb_ot_map_t *map = &c->plan->map;
750
101k
  hb_buffer_t *buffer = c->buffer;
751
752
101k
  hb_mask_t global_mask = map->get_global_mask ();
753
101k
  buffer->reset_masks (global_mask);
754
101k
}
755
756
static inline void
757
hb_ot_shape_setup_masks (const hb_ot_shape_context_t *c)
758
101k
{
759
101k
  hb_ot_map_t *map = &c->plan->map;
760
101k
  hb_buffer_t *buffer = c->buffer;
761
762
101k
  hb_ot_shape_setup_masks_fraction (c);
763
764
101k
  if (c->plan->shaper->setup_masks)
765
8.13k
    c->plan->shaper->setup_masks (c->plan, buffer, c->font);
766
767
101k
  for (unsigned int i = 0; i < c->num_user_features; i++)
768
0
  {
769
0
    const hb_feature_t *feature = &c->user_features[i];
770
0
    if (!(feature->start == HB_FEATURE_GLOBAL_START && feature->end == HB_FEATURE_GLOBAL_END)) {
771
0
      unsigned int shift;
772
0
      hb_mask_t mask = map->get_mask (feature->tag, &shift);
773
0
      buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
774
0
    }
775
0
  }
776
101k
}
777
778
static void
779
hb_ot_zero_width_default_ignorables (const hb_buffer_t *buffer)
780
101k
{
781
101k
  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
782
6.05k
      (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES) ||
783
6.05k
      (buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES))
784
95.0k
    return;
785
786
6.05k
  unsigned int count = buffer->len;
787
6.05k
  hb_glyph_info_t *info = buffer->info;
788
6.05k
  hb_glyph_position_t *pos = buffer->pos;
789
6.05k
  unsigned int i = 0;
790
2.84M
  for (i = 0; i < count; i++)
791
2.83M
    if (unlikely (_hb_glyph_info_is_default_ignorable (&info[i])))
792
122k
    {
793
122k
      pos[i].x_advance = pos[i].y_advance = 0;
794
122k
      if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
795
122k
  pos[i].x_offset = 0;
796
0
      else
797
0
        pos[i].y_offset = 0;
798
122k
    }
799
6.05k
}
800
801
static void
802
hb_ot_deal_with_variation_selectors (hb_buffer_t *buffer)
803
101k
{
804
101k
  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_VARIATION_SELECTOR_FALLBACK) ||
805
1.31k
  buffer->not_found_variation_selector == HB_CODEPOINT_INVALID)
806
101k
    return;
807
808
0
  unsigned int count = buffer->len;
809
0
  hb_glyph_info_t *info = buffer->info;
810
0
  hb_glyph_position_t *pos = buffer->pos;
811
812
0
  for (unsigned int i = 0; i < count; i++)
813
0
  {
814
0
    if (_hb_glyph_info_is_variation_selector (&info[i]))
815
0
    {
816
0
      info[i].codepoint = buffer->not_found_variation_selector;
817
0
      pos[i].x_advance = pos[i].y_advance = pos[i].x_offset = pos[i].y_offset = 0;
818
0
      _hb_glyph_info_set_variation_selector (&info[i], false);
819
0
    }
820
0
  }
821
0
}
822
823
static void
824
hb_ot_hide_default_ignorables (hb_buffer_t *buffer,
825
             hb_font_t   *font)
826
101k
{
827
101k
  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
828
6.05k
      (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES))
829
95.0k
    return;
830
831
6.05k
  unsigned int count = buffer->len;
832
6.05k
  hb_glyph_info_t *info = buffer->info;
833
834
6.05k
  hb_codepoint_t invisible = buffer->invisible;
835
6.05k
  if (!(buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES) &&
836
6.05k
      (invisible || font->get_nominal_glyph (' ', &invisible)))
837
167
  {
838
    /* Replace default-ignorables with a zero-advance invisible glyph. */
839
11.3k
    for (unsigned int i = 0; i < count; i++)
840
11.1k
    {
841
11.1k
      if (_hb_glyph_info_is_default_ignorable (&info[i]))
842
535
  info[i].codepoint = invisible;
843
11.1k
    }
844
167
  }
845
5.88k
  else
846
5.88k
    buffer->delete_glyphs_inplace (_hb_glyph_info_is_default_ignorable);
847
6.05k
}
848
849
850
static inline void
851
hb_ot_map_glyphs_fast (hb_buffer_t  *buffer)
852
101k
{
853
  /* Normalization process sets up normalizer_glyph_index(), we just copy it. */
854
101k
  unsigned int count = buffer->len;
855
101k
  hb_glyph_info_t *info = buffer->info;
856
1.34M
  for (unsigned int i = 0; i < count; i++)
857
1.24M
    info[i].codepoint = info[i].normalizer_glyph_index();
858
859
101k
  buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
860
101k
}
861
862
static inline void
863
hb_synthesize_glyph_classes (hb_buffer_t *buffer)
864
98.4k
{
865
98.4k
  unsigned int count = buffer->len;
866
98.4k
  hb_glyph_info_t *info = buffer->info;
867
1.31M
  for (unsigned int i = 0; i < count; i++)
868
1.21M
  {
869
1.21M
    hb_ot_layout_glyph_props_flags_t klass;
870
871
    /* Never mark default-ignorables as marks.
872
     * They won't get in the way of lookups anyway,
873
     * but having them as mark will cause them to be skipped
874
     * over if the lookup-flag says so, but at least for the
875
     * Mongolian variation selectors, looks like Uniscribe
876
     * marks them as non-mark.  Some Mongolian fonts without
877
     * GDEF rely on this.  Another notable character that
878
     * this applies to is COMBINING GRAPHEME JOINER. */
879
1.21M
    klass = (_hb_glyph_info_get_general_category (&info[i]) !=
880
1.21M
       HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK ||
881
27.3k
       _hb_glyph_info_is_default_ignorable (&info[i])) ?
882
1.18M
      HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH :
883
1.21M
      HB_OT_LAYOUT_GLYPH_PROPS_MARK;
884
1.21M
    _hb_glyph_info_set_glyph_props (&info[i], klass);
885
1.21M
  }
886
98.4k
}
887
888
static inline void
889
hb_ot_substitute_default (const hb_ot_shape_context_t *c)
890
101k
{
891
101k
  hb_buffer_t *buffer = c->buffer;
892
893
101k
  hb_ot_rotate_chars (c);
894
895
101k
  HB_BUFFER_ALLOCATE_VAR (buffer, normalizer_glyph_index);
896
897
101k
  _hb_ot_shape_normalize (c->plan, buffer, c->font);
898
899
101k
  hb_ot_shape_setup_masks (c);
900
901
  /* This is unfortunate to go here, but necessary... */
902
101k
  if (c->plan->fallback_mark_positioning)
903
78.9k
    _hb_ot_shape_fallback_mark_position_recategorize_marks (c->plan, c->font, buffer);
904
905
101k
  hb_ot_map_glyphs_fast (buffer);
906
907
101k
  HB_BUFFER_DEALLOCATE_VAR (buffer, normalizer_glyph_index);
908
101k
}
909
910
static inline void
911
hb_ot_substitute_plan (const hb_ot_shape_context_t *c)
912
101k
{
913
101k
  hb_buffer_t *buffer = c->buffer;
914
915
101k
  hb_ot_layout_substitute_start (c->font, buffer);
916
917
101k
  if (c->plan->fallback_glyph_classes)
918
98.4k
    hb_synthesize_glyph_classes (c->buffer);
919
920
101k
#ifndef HB_NO_AAT_SHAPE
921
101k
  if (unlikely (c->plan->apply_morx))
922
9.46k
  {
923
9.46k
    hb_aat_layout_substitute (c->plan, c->font, c->buffer,
924
9.46k
            c->user_features, c->num_user_features);
925
9.46k
    c->buffer->update_digest ();
926
9.46k
  }
927
91.6k
  else
928
91.6k
#endif
929
91.6k
  {
930
91.6k
    c->buffer->update_digest ();
931
91.6k
    c->plan->substitute (c->font, buffer);
932
91.6k
  }
933
101k
}
934
935
static inline void
936
hb_ot_substitute_pre (const hb_ot_shape_context_t *c)
937
101k
{
938
101k
  hb_ot_substitute_default (c);
939
940
101k
  _hb_buffer_allocate_gsubgpos_vars (c->buffer);
941
942
101k
  hb_ot_substitute_plan (c);
943
944
101k
#ifndef HB_NO_AAT_SHAPE
945
101k
  if (c->plan->apply_morx && c->plan->apply_gpos)
946
122
    hb_aat_layout_remove_deleted_glyphs (c->buffer);
947
101k
#endif
948
101k
}
949
950
static inline void
951
hb_ot_substitute_post (const hb_ot_shape_context_t *c)
952
101k
{
953
101k
#ifndef HB_NO_AAT_SHAPE
954
101k
  if (c->plan->apply_morx && !c->plan->apply_gpos)
955
9.33k
    hb_aat_layout_remove_deleted_glyphs (c->buffer);
956
101k
#endif
957
958
101k
  hb_ot_deal_with_variation_selectors (c->buffer);
959
101k
  hb_ot_hide_default_ignorables (c->buffer, c->font);
960
961
101k
  if (c->plan->shaper->postprocess_glyphs &&
962
1.48k
    c->buffer->message(c->font, "start postprocess-glyphs")) {
963
1.48k
    c->plan->shaper->postprocess_glyphs (c->plan, c->buffer, c->font);
964
1.48k
    (void) c->buffer->message(c->font, "end postprocess-glyphs");
965
1.48k
  }
966
101k
}
967
968
969
/*
970
 * Position
971
 */
972
973
static inline void
974
adjust_mark_offsets (hb_glyph_position_t *pos)
975
755k
{
976
755k
  pos->x_offset -= pos->x_advance;
977
755k
  pos->y_offset -= pos->y_advance;
978
755k
}
979
980
static inline void
981
zero_mark_width (hb_glyph_position_t *pos)
982
1.70M
{
983
1.70M
  pos->x_advance = 0;
984
1.70M
  pos->y_advance = 0;
985
1.70M
}
986
987
static inline void
988
zero_mark_widths_by_gdef (hb_buffer_t *buffer, bool adjust_offsets)
989
92.1k
{
990
92.1k
  unsigned int count = buffer->len;
991
92.1k
  hb_glyph_info_t *info = buffer->info;
992
43.3M
  for (unsigned int i = 0; i < count; i++)
993
43.2M
    if (_hb_glyph_info_is_mark (&info[i]))
994
1.70M
    {
995
1.70M
      if (adjust_offsets)
996
755k
  adjust_mark_offsets (&buffer->pos[i]);
997
1.70M
      zero_mark_width (&buffer->pos[i]);
998
1.70M
    }
999
92.1k
}
1000
1001
static inline void
1002
hb_ot_position_default (const hb_ot_shape_context_t *c)
1003
101k
{
1004
101k
  hb_direction_t direction = c->buffer->props.direction;
1005
101k
  unsigned int count = c->buffer->len;
1006
101k
  hb_glyph_info_t *info = c->buffer->info;
1007
101k
  hb_glyph_position_t *pos = c->buffer->pos;
1008
1009
101k
  if (HB_DIRECTION_IS_HORIZONTAL (direction))
1010
101k
  {
1011
101k
    c->font->get_glyph_h_advances (count, &info[0].codepoint, sizeof(info[0]),
1012
101k
           &pos[0].x_advance, sizeof(pos[0]));
1013
    // h_origin defaults to zero; only apply it if the font has it.
1014
101k
    if (c->font->has_glyph_h_origin_func () || c->font->has_glyph_h_origins_func ())
1015
0
      c->font->subtract_glyph_h_origins (c->buffer);
1016
101k
  }
1017
0
  else
1018
0
  {
1019
0
    c->font->get_glyph_v_advances (count, &info[0].codepoint, sizeof(info[0]),
1020
0
           &pos[0].y_advance, sizeof(pos[0]));
1021
    // v_origin defaults to non-zero; apply even if only fallback is there.
1022
0
    c->font->subtract_glyph_v_origins (c->buffer);
1023
0
  }
1024
101k
  if (c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK)
1025
157
    _hb_ot_shape_fallback_spaces (c->plan, c->font, c->buffer);
1026
101k
}
1027
1028
static inline void
1029
hb_ot_position_plan (const hb_ot_shape_context_t *c)
1030
101k
{
1031
  /* If the font has no GPOS and direction is forward, then when
1032
   * zeroing mark widths, we shift the mark with it, such that the
1033
   * mark is positioned hanging over the previous glyph.  When
1034
   * direction is backward we don't shift and it will end up
1035
   * hanging over the next glyph after the final reordering.
1036
   *
1037
   * Note: If fallback positioning happens, we don't care about
1038
   * this as it will be overridden.
1039
   */
1040
101k
  bool adjust_offsets_when_zeroing = c->plan->adjust_mark_positioning_when_zeroing &&
1041
75.4k
             HB_DIRECTION_IS_FORWARD (c->buffer->props.direction);
1042
1043
  /* We change glyph origin to what GPOS expects (horizontal), apply GPOS, change it back. */
1044
1045
  // h_origin defaults to zero; only apply it if the font has it.
1046
101k
  if (c->font->has_glyph_h_origin_func () || c->font->has_glyph_h_origins_func ())
1047
0
    c->font->add_glyph_h_origins (c->buffer);
1048
1049
101k
  hb_ot_layout_position_start (c->font, c->buffer);
1050
1051
101k
  if (c->plan->zero_marks)
1052
92.3k
    switch (c->plan->shaper->zero_width_marks)
1053
92.3k
    {
1054
2.99k
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
1055
2.99k
  zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
1056
2.99k
  break;
1057
1058
0
      default:
1059
169
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
1060
89.3k
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
1061
89.3k
  break;
1062
92.3k
    }
1063
1064
101k
  c->plan->position (c->font, c->buffer);
1065
1066
101k
  if (c->plan->zero_marks)
1067
92.3k
    switch (c->plan->shaper->zero_width_marks)
1068
92.3k
    {
1069
89.1k
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
1070
89.1k
  zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
1071
89.1k
  break;
1072
1073
0
      default:
1074
169
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
1075
3.16k
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
1076
3.16k
  break;
1077
92.3k
    }
1078
1079
  /* Finish off.  Has to follow a certain order. */
1080
101k
  hb_ot_layout_position_finish_advances (c->font, c->buffer);
1081
101k
  hb_ot_zero_width_default_ignorables (c->buffer);
1082
101k
  hb_ot_layout_position_finish_offsets (c->font, c->buffer);
1083
1084
  // h_origin defaults to zero; only apply it if the font has it.
1085
101k
  if (c->font->has_glyph_h_origin_func () || c->font->has_glyph_h_origins_func ())
1086
0
    c->font->subtract_glyph_h_origins (c->buffer);
1087
1088
101k
  if (c->plan->fallback_mark_positioning)
1089
78.9k
    _hb_ot_shape_fallback_mark_position (c->plan, c->font, c->buffer,
1090
78.9k
           adjust_offsets_when_zeroing);
1091
101k
}
1092
1093
static inline void
1094
hb_ot_position (const hb_ot_shape_context_t *c)
1095
101k
{
1096
101k
  c->buffer->clear_positions ();
1097
1098
101k
  hb_ot_position_default (c);
1099
1100
101k
  hb_ot_position_plan (c);
1101
1102
101k
  if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
1103
2.70k
    hb_buffer_reverse (c->buffer);
1104
1105
101k
  _hb_buffer_deallocate_gsubgpos_vars (c->buffer);
1106
101k
}
1107
1108
static inline void
1109
hb_propagate_flags (hb_buffer_t *buffer)
1110
101k
{
1111
  /* Propagate cluster-level glyph flags to be the same on all cluster glyphs.
1112
   * Simplifies using them. */
1113
1114
101k
  hb_mask_t and_mask = HB_GLYPH_FLAG_DEFINED;
1115
101k
  if ((buffer->flags & HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT) == 0)
1116
101k
    and_mask &= ~HB_GLYPH_FLAG_UNSAFE_TO_CONCAT;
1117
1118
101k
  hb_glyph_info_t *info = buffer->info;
1119
1120
101k
  if ((buffer->flags & HB_BUFFER_FLAG_PRODUCE_SAFE_TO_INSERT_TATWEEL) == 0)
1121
101k
  {
1122
101k
    foreach_cluster (buffer, start, end)
1123
1.16M
    {
1124
1.16M
      if (end - start == 1)
1125
1.08M
      {
1126
1.08M
        info[start].mask &= and_mask;
1127
1.08M
  continue;
1128
1.08M
      }
1129
1130
86.3k
      unsigned int mask = 0;
1131
35.3M
      for (unsigned int i = start; i < end; i++)
1132
35.2M
  mask |= info[i].mask;
1133
1134
86.3k
      mask &= and_mask;
1135
1136
35.3M
      for (unsigned int i = start; i < end; i++)
1137
35.2M
  info[i].mask = mask;
1138
86.3k
    }
1139
101k
    return;
1140
101k
  }
1141
1142
  /* If we are producing SAFE_TO_INSERT_TATWEEL, then do two things:
1143
   *
1144
   * - If the places that the Arabic shaper marked as SAFE_TO_INSERT_TATWEEL,
1145
   *   are UNSAFE_TO_BREAK, then clear the SAFE_TO_INSERT_TATWEEL,
1146
   * - Any place that is SAFE_TO_INSERT_TATWEEL, is also now UNSAFE_TO_BREAK.
1147
   *
1148
   * We couldn't make this interaction earlier. It has to be done this way.
1149
   */
1150
0
  foreach_cluster (buffer, start, end)
1151
0
  {
1152
0
    unsigned int mask = 0;
1153
0
    for (unsigned int i = start; i < end; i++)
1154
0
      mask |= info[i].mask;
1155
1156
0
    if (mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK)
1157
0
      mask &= ~HB_GLYPH_FLAG_SAFE_TO_INSERT_TATWEEL;
1158
0
    if (mask & HB_GLYPH_FLAG_SAFE_TO_INSERT_TATWEEL)
1159
0
      mask |= HB_GLYPH_FLAG_UNSAFE_TO_BREAK | HB_GLYPH_FLAG_UNSAFE_TO_CONCAT;
1160
1161
0
    mask &= and_mask;
1162
1163
0
    for (unsigned int i = start; i < end; i++)
1164
0
      info[i].mask = mask;
1165
0
  }
1166
0
}
1167
1168
/* Pull it all together! */
1169
1170
static void
1171
hb_ot_shape_internal (hb_ot_shape_context_t *c)
1172
101k
{
1173
  /* Save the original direction, we use it later. */
1174
101k
  c->target_direction = c->buffer->props.direction;
1175
1176
101k
  _hb_buffer_allocate_unicode_vars (c->buffer);
1177
1178
101k
  hb_ot_shape_initialize_masks (c);
1179
101k
  hb_set_unicode_props (c->buffer);
1180
101k
  hb_insert_dotted_circle (c->buffer, c->font);
1181
1182
101k
  hb_form_clusters (c->buffer);
1183
1184
101k
  hb_ensure_native_direction (c->buffer);
1185
1186
101k
  if (c->plan->shaper->preprocess_text &&
1187
6.31k
      c->buffer->message(c->font, "start preprocess-text"))
1188
6.31k
  {
1189
6.31k
    c->plan->shaper->preprocess_text (c->plan, c->buffer, c->font);
1190
6.31k
    (void) c->buffer->message(c->font, "end preprocess-text");
1191
6.31k
  }
1192
1193
101k
  hb_ot_substitute_pre (c);
1194
101k
  hb_ot_position (c);
1195
101k
  hb_ot_substitute_post (c);
1196
1197
101k
  hb_propagate_flags (c->buffer);
1198
1199
101k
  _hb_buffer_deallocate_unicode_vars (c->buffer);
1200
1201
101k
  c->buffer->props.direction = c->target_direction;
1202
101k
}
1203
1204
1205
hb_bool_t
1206
_hb_ot_shape (hb_shape_plan_t    *shape_plan,
1207
        hb_font_t          *font,
1208
        hb_buffer_t        *buffer,
1209
        const hb_feature_t *features,
1210
        unsigned int        num_features)
1211
101k
{
1212
101k
  hb_ot_shape_context_t c = {&shape_plan->ot, font, font->face, buffer, features, num_features};
1213
101k
  hb_ot_shape_internal (&c);
1214
1215
101k
  return true;
1216
101k
}
1217
1218
1219
/**
1220
 * hb_ot_shape_plan_collect_lookups:
1221
 * @shape_plan: #hb_shape_plan_t to query
1222
 * @table_tag: GSUB or GPOS
1223
 * @lookup_indexes: (out): The #hb_set_t set of lookups returned
1224
 *
1225
 * Computes the complete set of GSUB or GPOS lookups that are applicable
1226
 * under a given @shape_plan.
1227
 *
1228
 * Since: 0.9.7
1229
 **/
1230
void
1231
hb_ot_shape_plan_collect_lookups (hb_shape_plan_t *shape_plan,
1232
          hb_tag_t         table_tag,
1233
          hb_set_t        *lookup_indexes /* OUT */)
1234
0
{
1235
0
  shape_plan->ot.collect_lookups (table_tag, lookup_indexes);
1236
0
}
1237
1238
1239
/**
1240
 * hb_ot_shape_plan_get_feature_tags:
1241
 * @shape_plan: A shaping plan
1242
 * @start_offset: The index of first feature to retrieve
1243
 * @tag_count: (inout): Input = the maximum number of features to return;
1244
 *                      Output = the actual number of features returned (may be zero)
1245
 * @tags: (out) (array length=tag_count): The array of enabled feature
1246
 *
1247
 * Fetches the list of OpenType feature tags enabled for a shaping plan, if possible.
1248
 *
1249
 * Return value: Total number of feature tagss.
1250
 *
1251
 * Since: 10.3.0
1252
 */
1253
unsigned int
1254
hb_ot_shape_plan_get_feature_tags (hb_shape_plan_t *shape_plan,
1255
           unsigned int     start_offset,
1256
           unsigned int    *tag_count, /* IN/OUT */
1257
           hb_tag_t        *tags /* OUT */)
1258
0
{
1259
0
#ifndef HB_NO_OT_SHAPE
1260
0
  return shape_plan->ot.map.get_feature_tags (start_offset, tag_count, tags);
1261
#else
1262
  if (tag_count)
1263
  *tag_count = 0;
1264
  return 0;
1265
#endif
1266
0
}
1267
1268
1269
/* TODO Move this to hb-ot-shape-normalize, make it do decompose, and make it public. */
1270
static void
1271
add_char (hb_font_t          *font,
1272
    hb_unicode_funcs_t *unicode,
1273
    hb_bool_t           mirror,
1274
    hb_codepoint_t      u,
1275
    hb_set_t           *glyphs)
1276
0
{
1277
0
  hb_codepoint_t glyph;
1278
0
  if (font->get_nominal_glyph (u, &glyph))
1279
0
    glyphs->add (glyph);
1280
0
  if (mirror)
1281
0
  {
1282
0
    hb_codepoint_t m = unicode->mirroring (u);
1283
0
    if (m != u && font->get_nominal_glyph (m, &glyph))
1284
0
      glyphs->add (glyph);
1285
0
  }
1286
0
}
1287
1288
1289
/**
1290
 * hb_ot_shape_glyphs_closure:
1291
 * @font: #hb_font_t to work upon
1292
 * @buffer: The input buffer to compute from
1293
 * @features: (array length=num_features): The features enabled on the buffer
1294
 * @num_features: The number of features enabled on the buffer
1295
 * @glyphs: (out): The #hb_set_t set of glyphs comprising the transitive closure of the query
1296
 *
1297
 * Computes the transitive closure of glyphs needed for a specified
1298
 * input buffer under the given font and feature list. The closure is
1299
 * computed as a set, not as a list.
1300
 *
1301
 * Since: 0.9.2
1302
 **/
1303
void
1304
hb_ot_shape_glyphs_closure (hb_font_t          *font,
1305
          hb_buffer_t        *buffer,
1306
          const hb_feature_t *features,
1307
          unsigned int        num_features,
1308
          hb_set_t           *glyphs)
1309
0
{
1310
0
  const char *shapers[] = {"ot", nullptr};
1311
0
  hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props,
1312
0
                   features, num_features, shapers);
1313
1314
0
  bool mirror = hb_script_get_horizontal_direction (buffer->props.script) == HB_DIRECTION_RTL;
1315
1316
0
  unsigned int count = buffer->len;
1317
0
  hb_glyph_info_t *info = buffer->info;
1318
0
  for (unsigned int i = 0; i < count; i++)
1319
0
    add_char (font, buffer->unicode, mirror, info[i].codepoint, glyphs);
1320
1321
0
  hb_set_t *lookups = hb_set_create ();
1322
0
  hb_ot_shape_plan_collect_lookups (shape_plan, HB_OT_TAG_GSUB, lookups);
1323
0
  hb_ot_layout_lookups_substitute_closure (font->face, lookups, glyphs);
1324
1325
0
  hb_set_destroy (lookups);
1326
1327
0
  hb_shape_plan_destroy (shape_plan);
1328
0
}
1329
1330
1331
#endif