Coverage Report

Created: 2026-06-30 06:08

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
313k
{ 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
53.5k
{
62
  /* https://github.com/harfbuzz/harfbuzz/issues/2124 */
63
53.5k
  return hb_aat_layout_has_substitution (face) &&
64
4.12k
   (HB_DIRECTION_IS_HORIZONTAL (props.direction) || !hb_ot_layout_has_substitution (face));
65
53.5k
}
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
53.5k
            face (face),
86
53.5k
            props (props),
87
53.5k
            map (face, props)
88
#ifndef HB_NO_AAT_SHAPE
89
53.5k
            , aat_map (face, props)
90
53.5k
            , apply_morx (_hb_apply_morx (face, props))
91
#endif
92
53.5k
{
93
53.5k
  shaper = hb_ot_shaper_categorize (props.script, props.direction, map.chosen_script[0]);
94
95
53.5k
  script_zero_marks = shaper->zero_width_marks != HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE;
96
53.5k
  script_fallback_position = shaper->fallback_position;
97
98
53.5k
#ifndef HB_NO_AAT_SHAPE
99
  /* https://github.com/harfbuzz/harfbuzz/issues/1528 */
100
53.5k
  if (apply_morx && shaper != &_hb_ot_shaper_default)
101
283
    shaper = &_hb_ot_shaper_dumber;
102
53.5k
#endif
103
53.5k
}
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
53.5k
{
109
53.5k
  plan.props = props;
110
53.5k
  plan.shaper = shaper;
111
53.5k
  map.compile (plan.map, key);
112
53.5k
#ifndef HB_NO_AAT_SHAPE
113
53.5k
  if (apply_morx)
114
4.12k
    aat_map.compile (plan.aat_map);
115
53.5k
#endif
116
117
53.5k
#ifndef HB_NO_OT_SHAPE_FRACTIONS
118
53.5k
  plan.frac_mask = plan.map.get_1_mask (HB_TAG ('f','r','a','c'));
119
53.5k
  plan.numr_mask = plan.map.get_1_mask (HB_TAG ('n','u','m','r'));
120
53.5k
  plan.dnom_mask = plan.map.get_1_mask (HB_TAG ('d','n','o','m'));
121
53.5k
  plan.has_frac = plan.frac_mask || (plan.numr_mask && plan.dnom_mask);
122
53.5k
#endif
123
124
53.5k
  plan.rtlm_mask = plan.map.get_1_mask (HB_TAG ('r','t','l','m'));
125
53.5k
  plan.has_vert = !!plan.map.get_1_mask (HB_TAG ('v','e','r','t'));
126
127
53.5k
  hb_tag_t kern_tag = HB_DIRECTION_IS_HORIZONTAL (props.direction) ?
128
53.5k
          HB_TAG ('k','e','r','n') : HB_TAG ('v','k','r','n');
129
53.5k
#ifndef HB_NO_OT_KERN
130
53.5k
  plan.kern_mask = plan.map.get_mask (kern_tag);
131
53.5k
  plan.requested_kerning = !!plan.kern_mask;
132
53.5k
#endif
133
134
53.5k
  bool has_gpos_kern = plan.map.get_feature_index (1, kern_tag) != HB_OT_LAYOUT_NO_FEATURE_INDEX;
135
53.5k
  bool disable_gpos = plan.shaper->gpos_tag &&
136
530
          plan.shaper->gpos_tag != plan.map.chosen_script[1];
137
138
  /*
139
   * Decide who provides glyph classes. GDEF or Unicode.
140
   */
141
142
53.5k
  if (!hb_ot_layout_has_glyph_classes (face))
143
52.3k
    plan.fallback_glyph_classes = true;
144
145
  /*
146
   * Decide who does substitutions. GSUB, morx, or fallback.
147
   */
148
149
53.5k
#ifndef HB_NO_AAT_SHAPE
150
53.5k
  plan.apply_morx = apply_morx;
151
53.5k
#endif
152
153
  /*
154
   * Decide who does positioning. GPOS, kerx, kern, or fallback.
155
   */
156
157
53.5k
#ifndef HB_NO_AAT_SHAPE
158
53.5k
  bool has_kerx = hb_aat_layout_has_positioning (face);
159
53.5k
  bool has_gsub = !apply_morx && hb_ot_layout_has_substitution (face);
160
53.5k
#endif
161
53.5k
  bool has_gpos = !disable_gpos && hb_ot_layout_has_positioning (face);
162
53.5k
  if (false)
163
0
    {}
164
53.5k
#ifndef HB_NO_AAT_SHAPE
165
  /* Prefer GPOS over kerx if GSUB is present;
166
   * https://github.com/harfbuzz/harfbuzz/issues/3008 */
167
53.5k
  else if (has_kerx && !(has_gsub && has_gpos))
168
2.32k
    plan.apply_kerx = true;
169
51.1k
#endif
170
51.1k
  else if (has_gpos)
171
5.36k
    plan.apply_gpos = true;
172
173
53.5k
  if (!plan.apply_kerx && (!has_gpos_kern || !plan.apply_gpos))
174
50.7k
  {
175
50.7k
    if (false) {}
176
50.7k
#ifndef HB_NO_AAT_SHAPE
177
50.7k
    else if (has_kerx)
178
6
      plan.apply_kerx = true;
179
50.7k
#endif
180
50.7k
#ifndef HB_NO_OT_KERN
181
50.7k
    else if (hb_ot_layout_has_kerning (face))
182
2.10k
      plan.apply_kern = script_fallback_position; // Not all shapers apply legacy `kern`
183
48.6k
#endif
184
48.6k
    else {}
185
50.7k
  }
186
187
53.5k
  plan.apply_fallback_kern = script_fallback_position && !(plan.apply_gpos || plan.apply_kerx || plan.apply_kern);
188
189
53.5k
  plan.zero_marks = script_zero_marks &&
190
49.7k
        !plan.apply_kerx &&
191
47.4k
        (!plan.apply_kern
192
2.01k
#ifndef HB_NO_OT_KERN
193
2.01k
         || !hb_ot_layout_has_machine_kerning (face)
194
47.4k
#endif
195
47.4k
        );
196
53.5k
  plan.has_gpos_mark = !!plan.map.get_1_mask (HB_TAG ('m','a','r','k'));
197
198
53.5k
  plan.adjust_mark_positioning_when_zeroing = !plan.apply_gpos &&
199
48.1k
                !plan.apply_kerx &&
200
45.8k
                (!plan.apply_kern
201
2.03k
#ifndef HB_NO_OT_KERN
202
2.03k
                 || !hb_ot_layout_has_cross_kerning (face)
203
45.8k
#endif
204
45.8k
                );
205
206
53.5k
  plan.fallback_mark_positioning = plan.adjust_mark_positioning_when_zeroing &&
207
44.7k
           script_fallback_position;
208
209
53.5k
#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
53.5k
  if (plan.apply_morx)
214
4.12k
    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
53.5k
#ifndef HB_NO_STYLE
218
53.5k
  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
53.5k
#endif
224
53.5k
}
225
226
bool
227
hb_ot_shape_plan_t::init0 (hb_face_t                     *face,
228
         const hb_shape_plan_key_t     *key)
229
53.5k
{
230
53.5k
  map.init ();
231
232
53.5k
  hb_ot_shape_planner_t planner (face,
233
53.5k
         key->props);
234
235
53.5k
  hb_ot_shape_collect_features (&planner,
236
53.5k
        key->user_features,
237
53.5k
        key->num_user_features);
238
239
53.5k
  planner.compile (*this, key->ot);
240
241
53.5k
  if (shaper->data_create)
242
7.60k
  {
243
7.60k
    data = shaper->data_create (this);
244
7.60k
    if (unlikely (!data))
245
55
    {
246
55
      map.fini ();
247
55
      return false;
248
55
    }
249
7.60k
  }
250
251
53.4k
  return true;
252
53.5k
}
253
254
void
255
hb_ot_shape_plan_t::fini ()
256
53.4k
{
257
53.4k
  if (shaper->data_destroy)
258
7.55k
    shaper->data_destroy (const_cast<void *> (data));
259
260
53.4k
  map.fini ();
261
53.4k
}
262
263
void
264
hb_ot_shape_plan_t::substitute (hb_font_t   *font,
265
        hb_buffer_t *buffer) const
266
94.2k
{
267
94.2k
  map.substitute (this, font, buffer);
268
94.2k
}
269
270
void
271
hb_ot_shape_plan_t::position (hb_font_t   *font,
272
            hb_buffer_t *buffer) const
273
104k
{
274
104k
  if (this->apply_gpos)
275
9.89k
    map.position (this, font, buffer);
276
94.6k
#ifndef HB_NO_AAT_SHAPE
277
94.6k
  else if (this->apply_kerx)
278
4.32k
    hb_aat_layout_position (this, font, buffer);
279
104k
#endif
280
281
104k
#ifndef HB_NO_OT_KERN
282
104k
  if (this->apply_kern)
283
4.27k
    hb_ot_layout_kern (this, font, buffer);
284
100k
#endif
285
100k
  else if (this->apply_fallback_kern)
286
80.5k
    _hb_ot_shape_fallback_kern (this, font, buffer);
287
288
104k
#ifndef HB_NO_AAT_SHAPE
289
104k
  if (this->apply_trak)
290
120
    hb_aat_layout_track (this, font, buffer);
291
104k
#endif
292
104k
}
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
53.5k
{
325
53.5k
  hb_ot_map_builder_t *map = &planner->map;
326
327
53.5k
  map->is_simple = true;
328
329
53.5k
  map->enable_feature (HB_TAG('r','v','r','n'));
330
53.5k
  map->add_gsub_pause (nullptr);
331
332
53.5k
  switch (planner->props.direction)
333
53.5k
  {
334
50.7k
    case HB_DIRECTION_LTR:
335
50.7k
      map->enable_feature (HB_TAG ('l','t','r','a'));
336
50.7k
      map->enable_feature (HB_TAG ('l','t','r','m'));
337
50.7k
      break;
338
2.78k
    case HB_DIRECTION_RTL:
339
2.78k
      map->enable_feature (HB_TAG ('r','t','l','a'));
340
2.78k
      map->add_feature (HB_TAG ('r','t','l','m'));
341
2.78k
      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
53.5k
  }
348
349
53.5k
#ifndef HB_NO_OT_SHAPE_FRACTIONS
350
  /* Automatic fractions. */
351
53.5k
  map->add_feature (HB_TAG ('f','r','a','c'));
352
53.5k
  map->add_feature (HB_TAG ('n','u','m','r'));
353
53.5k
  map->add_feature (HB_TAG ('d','n','o','m'));
354
53.5k
#endif
355
356
  /* Random! */
357
53.5k
  map->enable_feature (HB_TAG ('r','a','n','d'), F_RANDOM, HB_OT_MAP_MAX_VALUE);
358
359
53.5k
  map->enable_feature (HB_TAG ('H','a','r','f')); /* Considered required. */
360
53.5k
  map->enable_feature (HB_TAG ('H','A','R','F')); /* Considered discretionary. */
361
362
53.5k
  if (planner->shaper->collect_features)
363
8.02k
  {
364
8.02k
    map->is_simple = false;
365
8.02k
    planner->shaper->collect_features (planner);
366
8.02k
  }
367
368
53.5k
  map->enable_feature (HB_TAG ('B','u','z','z')); /* Considered required. */
369
53.5k
  map->enable_feature (HB_TAG ('B','U','Z','Z')); /* Considered discretionary. */
370
371
428k
  for (unsigned int i = 0; i < ARRAY_LENGTH (common_features); i++)
372
374k
    map->add_feature (common_features[i]);
373
374
53.5k
  if (HB_DIRECTION_IS_HORIZONTAL (planner->props.direction))
375
428k
    for (unsigned int i = 0; i < ARRAY_LENGTH (horizontal_features); i++)
376
374k
      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
53.5k
  if (num_user_features)
391
0
    map->is_simple = false;
392
53.5k
  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
53.5k
  if (planner->shaper->override_features)
402
3.68k
    planner->shaper->override_features (planner);
403
53.5k
}
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
35.9k
{
415
35.9k
  return (hb_ot_face_data_t *) HB_SHAPER_DATA_SUCCEEDED;
416
35.9k
}
417
418
void
419
_hb_ot_shaper_face_data_destroy (hb_ot_face_data_t *data)
420
35.9k
{
421
35.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
35.8k
{
435
35.8k
  const OT::ItemVariationStore &var_store = font->face->table.GDEF->table->get_var_store ();
436
35.8k
  return (hb_ot_font_data_t *) var_store.create_cache ();
437
35.8k
}
438
439
void
440
_hb_ot_shaper_font_data_destroy (hb_ot_font_data_t *data)
441
35.8k
{
442
35.8k
  OT::ItemVariationStore::destroy_cache ((OT::hb_scalar_cache_t *) data);
443
35.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
104k
{
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
104k
  unsigned int count = buffer->len;
482
104k
  hb_glyph_info_t *info = buffer->info;
483
1.38M
  for (unsigned int i = 0; i < count; i++)
484
1.27M
  {
485
1.27M
    _hb_glyph_info_set_unicode_props (&info[i], buffer);
486
487
1.27M
    if (info[i].codepoint < 0x80)
488
853k
      continue;
489
490
422k
    unsigned gen_cat = _hb_glyph_info_get_general_category (&info[i]);
491
422k
    if (FLAG_UNSAFE (gen_cat) &
492
422k
  (FLAG (HB_UNICODE_GENERAL_CATEGORY_LOWERCASE_LETTER) |
493
422k
   FLAG (HB_UNICODE_GENERAL_CATEGORY_UPPERCASE_LETTER) |
494
422k
   FLAG (HB_UNICODE_GENERAL_CATEGORY_TITLECASE_LETTER) |
495
422k
   FLAG (HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER) |
496
422k
   FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACE_SEPARATOR)))
497
86.5k
      continue;
498
499
    /* Marks are already set as continuation by the above line.
500
     * Handle Emoji_Modifier and ZWJ-continuation. */
501
335k
    if (unlikely (gen_cat == HB_UNICODE_GENERAL_CATEGORY_MODIFIER_SYMBOL &&
502
335k
      hb_in_range<hb_codepoint_t> (info[i].codepoint, 0x1F3FBu, 0x1F3FFu)))
503
26
    {
504
26
      _hb_glyph_info_set_continuation (&info[i], buffer);
505
26
    }
506
    /* Regional_Indicators are hairy as hell...
507
     * https://github.com/harfbuzz/harfbuzz/issues/2265 */
508
335k
    else if (unlikely (i && _hb_codepoint_is_regional_indicator (info[i].codepoint)))
509
41
    {
510
41
      if (_hb_codepoint_is_regional_indicator (info[i - 1].codepoint) &&
511
25
    !_hb_glyph_info_is_continuation (&info[i - 1]))
512
14
  _hb_glyph_info_set_continuation (&info[i], buffer);
513
41
    }
514
335k
#ifndef HB_NO_EMOJI_SEQUENCES
515
335k
    else if (unlikely (_hb_glyph_info_is_zwj (&info[i])))
516
1.70k
    {
517
1.70k
      _hb_glyph_info_set_continuation (&info[i], buffer);
518
1.70k
      if (i + 1 < count &&
519
1.64k
    _hb_unicode_is_emoji_Extended_Pictographic (info[i + 1].codepoint))
520
6
      {
521
6
  i++;
522
6
  _hb_glyph_info_set_unicode_props (&info[i], buffer);
523
6
  _hb_glyph_info_set_continuation (&info[i], buffer);
524
6
      }
525
1.70k
    }
526
334k
#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
334k
    else if (unlikely (hb_in_ranges<hb_codepoint_t> (info[i].codepoint, 0xFF9Eu, 0xFF9Fu, 0xE0020u, 0xE007Fu)))
542
225
      _hb_glyph_info_set_continuation (&info[i], buffer);
543
333k
    else if (unlikely (info[i].codepoint == 0x2044u /* FRACTION SLASH */))
544
91
      buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_FRACTION_SLASH;
545
335k
  }
546
104k
}
547
548
static void
549
hb_insert_dotted_circle (hb_buffer_t *buffer, hb_font_t *font)
550
104k
{
551
104k
  if (unlikely (buffer->flags & HB_BUFFER_FLAG_DO_NOT_INSERT_DOTTED_CIRCLE))
552
0
    return;
553
554
104k
  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
104k
    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
104k
{
580
104k
  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_CONTINUATIONS))
581
91.1k
    return;
582
583
13.3k
  foreach_grapheme (buffer, start, end)
584
184k
    buffer->merge_grapheme_clusters (start, end);
585
13.3k
}
586
587
static void
588
hb_ensure_native_direction (hb_buffer_t *buffer)
589
104k
{
590
104k
  hb_direction_t direction = buffer->props.direction;
591
104k
  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
104k
  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
104k
  if ((HB_DIRECTION_IS_HORIZONTAL (direction) &&
637
104k
       direction != horiz_dir && HB_DIRECTION_IS_VALID (horiz_dir)) ||
638
104k
      (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
104k
}
645
646
647
/*
648
 * Substitute
649
 */
650
651
static inline void
652
hb_ot_rotate_chars (const hb_ot_shape_context_t *c)
653
104k
{
654
104k
  hb_buffer_t *buffer = c->buffer;
655
104k
  unsigned int count = buffer->len;
656
104k
  hb_glyph_info_t *info = buffer->info;
657
658
104k
  if (HB_DIRECTION_IS_BACKWARD (c->target_direction))
659
2.75k
  {
660
2.75k
    hb_unicode_funcs_t *unicode = buffer->unicode;
661
2.75k
    hb_mask_t rtlm_mask = c->plan->rtlm_mask;
662
663
46.8k
    for (unsigned int i = 0; i < count; i++) {
664
44.0k
      hb_codepoint_t codepoint = unicode->mirroring (info[i].codepoint);
665
44.0k
      if (unlikely (codepoint != info[i].codepoint && c->font->has_glyph (codepoint)))
666
25
  info[i].codepoint = codepoint;
667
44.0k
      else
668
44.0k
  info[i].mask |= rtlm_mask;
669
44.0k
    }
670
2.75k
  }
671
672
104k
#ifndef HB_NO_VERTICAL
673
104k
  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
104k
#endif
682
104k
}
683
684
static inline void
685
hb_ot_shape_setup_masks_fraction (const hb_ot_shape_context_t *c)
686
104k
{
687
#ifdef HB_NO_OT_SHAPE_FRACTIONS
688
  return;
689
#endif
690
691
104k
  if (!(c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_FRACTION_SLASH) ||
692
30
      !c->plan->has_frac)
693
104k
    return;
694
695
27
  hb_buffer_t *buffer = c->buffer;
696
697
27
  hb_mask_t pre_mask, post_mask;
698
27
  if (HB_DIRECTION_IS_FORWARD (buffer->props.direction))
699
25
  {
700
25
    pre_mask = c->plan->numr_mask | c->plan->frac_mask;
701
25
    post_mask = c->plan->frac_mask | c->plan->dnom_mask;
702
25
  }
703
2
  else
704
2
  {
705
2
    pre_mask = c->plan->frac_mask | c->plan->dnom_mask;
706
2
    post_mask = c->plan->numr_mask | c->plan->frac_mask;
707
2
  }
708
709
27
  unsigned int count = buffer->len;
710
27
  hb_glyph_info_t *info = buffer->info;
711
414
  for (unsigned int i = 0; i < count; i++)
712
387
  {
713
387
    if (info[i].codepoint == 0x2044u) /* FRACTION SLASH */
714
79
    {
715
79
      unsigned int start = i, end = i + 1;
716
137
      while (start &&
717
132
       _hb_glyph_info_get_general_category (&info[start - 1]) ==
718
132
       HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
719
58
  start--;
720
141
      while (end < count &&
721
127
       _hb_glyph_info_get_general_category (&info[end]) ==
722
127
       HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
723
62
  end++;
724
79
      if (start == i || end == i + 1)
725
58
      {
726
58
        if (start == i)
727
46
    buffer->unsafe_to_concat (start, start + 1);
728
58
  if (end == i + 1)
729
46
    buffer->unsafe_to_concat (end - 1, end);
730
58
  continue;
731
58
      }
732
733
21
      buffer->unsafe_to_break (start, end);
734
735
52
      for (unsigned int j = start; j < i; j++)
736
31
  info[j].mask |= pre_mask;
737
21
      info[i].mask |= c->plan->frac_mask;
738
67
      for (unsigned int j = i + 1; j < end; j++)
739
46
  info[j].mask |= post_mask;
740
741
21
      i = end - 1;
742
21
    }
743
387
  }
744
27
}
745
746
static inline void
747
hb_ot_shape_initialize_masks (const hb_ot_shape_context_t *c)
748
104k
{
749
104k
  hb_ot_map_t *map = &c->plan->map;
750
104k
  hb_buffer_t *buffer = c->buffer;
751
752
104k
  hb_mask_t global_mask = map->get_global_mask ();
753
104k
  buffer->reset_masks (global_mask);
754
104k
}
755
756
static inline void
757
hb_ot_shape_setup_masks (const hb_ot_shape_context_t *c)
758
104k
{
759
104k
  hb_ot_map_t *map = &c->plan->map;
760
104k
  hb_buffer_t *buffer = c->buffer;
761
762
104k
  hb_ot_shape_setup_masks_fraction (c);
763
764
104k
  if (c->plan->shaper->setup_masks)
765
7.96k
    c->plan->shaper->setup_masks (c->plan, buffer, c->font);
766
767
104k
  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
104k
}
777
778
static void
779
hb_ot_zero_width_default_ignorables (const hb_buffer_t *buffer)
780
104k
{
781
104k
  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
782
5.75k
      (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES) ||
783
5.75k
      (buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES))
784
98.7k
    return;
785
786
5.75k
  unsigned int count = buffer->len;
787
5.75k
  hb_glyph_info_t *info = buffer->info;
788
5.75k
  hb_glyph_position_t *pos = buffer->pos;
789
5.75k
  unsigned int i = 0;
790
3.27M
  for (i = 0; i < count; i++)
791
3.27M
    if (unlikely (_hb_glyph_info_is_default_ignorable (&info[i])))
792
144k
    {
793
144k
      pos[i].x_advance = pos[i].y_advance = 0;
794
144k
      if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
795
144k
  pos[i].x_offset = 0;
796
0
      else
797
0
        pos[i].y_offset = 0;
798
144k
    }
799
5.75k
}
800
801
static void
802
hb_ot_deal_with_variation_selectors (hb_buffer_t *buffer)
803
104k
{
804
104k
  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_VARIATION_SELECTOR_FALLBACK) ||
805
1.15k
  buffer->not_found_variation_selector == HB_CODEPOINT_INVALID)
806
104k
    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
104k
{
827
104k
  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
828
5.75k
      (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES))
829
98.7k
    return;
830
831
5.75k
  unsigned int count = buffer->len;
832
5.75k
  hb_glyph_info_t *info = buffer->info;
833
834
5.75k
  hb_codepoint_t invisible = buffer->invisible;
835
5.75k
  if (!(buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES) &&
836
5.75k
      (invisible || font->get_nominal_glyph (' ', &invisible)))
837
213
  {
838
    /* Replace default-ignorables with a zero-advance invisible glyph. */
839
194k
    for (unsigned int i = 0; i < count; i++)
840
194k
    {
841
194k
      if (_hb_glyph_info_is_default_ignorable (&info[i]))
842
1.21k
  info[i].codepoint = invisible;
843
194k
    }
844
213
  }
845
5.54k
  else
846
5.54k
    buffer->delete_glyphs_inplace (_hb_glyph_info_is_default_ignorable);
847
5.75k
}
848
849
850
static inline void
851
hb_ot_map_glyphs_fast (hb_buffer_t  *buffer)
852
104k
{
853
  /* Normalization process sets up normalizer_glyph_index(), we just copy it. */
854
104k
  unsigned int count = buffer->len;
855
104k
  hb_glyph_info_t *info = buffer->info;
856
1.38M
  for (unsigned int i = 0; i < count; i++)
857
1.27M
    info[i].codepoint = info[i].normalizer_glyph_index();
858
859
104k
  buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
860
104k
}
861
862
static inline void
863
hb_synthesize_glyph_classes (hb_buffer_t *buffer)
864
102k
{
865
102k
  unsigned int count = buffer->len;
866
102k
  hb_glyph_info_t *info = buffer->info;
867
1.35M
  for (unsigned int i = 0; i < count; i++)
868
1.25M
  {
869
1.25M
    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.25M
    klass = (_hb_glyph_info_get_general_category (&info[i]) !=
880
1.25M
       HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK ||
881
25.4k
       _hb_glyph_info_is_default_ignorable (&info[i])) ?
882
1.22M
      HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH :
883
1.25M
      HB_OT_LAYOUT_GLYPH_PROPS_MARK;
884
1.25M
    _hb_glyph_info_set_glyph_props (&info[i], klass);
885
1.25M
  }
886
102k
}
887
888
static inline void
889
hb_ot_substitute_default (const hb_ot_shape_context_t *c)
890
104k
{
891
104k
  hb_buffer_t *buffer = c->buffer;
892
893
104k
  hb_ot_rotate_chars (c);
894
895
104k
  HB_BUFFER_ALLOCATE_VAR (buffer, normalizer_glyph_index);
896
897
104k
  _hb_ot_shape_normalize (c->plan, buffer, c->font);
898
899
104k
  hb_ot_shape_setup_masks (c);
900
901
  /* This is unfortunate to go here, but necessary... */
902
104k
  if (c->plan->fallback_mark_positioning)
903
82.6k
    _hb_ot_shape_fallback_mark_position_recategorize_marks (c->plan, c->font, buffer);
904
905
104k
  hb_ot_map_glyphs_fast (buffer);
906
907
104k
  HB_BUFFER_DEALLOCATE_VAR (buffer, normalizer_glyph_index);
908
104k
}
909
910
static inline void
911
hb_ot_substitute_plan (const hb_ot_shape_context_t *c)
912
104k
{
913
104k
  hb_buffer_t *buffer = c->buffer;
914
915
104k
  hb_ot_layout_substitute_start (c->font, buffer);
916
917
104k
  if (c->plan->fallback_glyph_classes)
918
102k
    hb_synthesize_glyph_classes (c->buffer);
919
920
104k
#ifndef HB_NO_AAT_SHAPE
921
104k
  if (unlikely (c->plan->apply_morx))
922
10.2k
  {
923
10.2k
    hb_aat_layout_substitute (c->plan, c->font, c->buffer,
924
10.2k
            c->user_features, c->num_user_features);
925
10.2k
    c->buffer->update_digest ();
926
10.2k
  }
927
94.2k
  else
928
94.2k
#endif
929
94.2k
  {
930
94.2k
    c->buffer->update_digest ();
931
94.2k
    c->plan->substitute (c->font, buffer);
932
94.2k
  }
933
104k
}
934
935
static inline void
936
hb_ot_substitute_pre (const hb_ot_shape_context_t *c)
937
104k
{
938
104k
  hb_ot_substitute_default (c);
939
940
104k
  _hb_buffer_allocate_gsubgpos_vars (c->buffer);
941
942
104k
  hb_ot_substitute_plan (c);
943
944
104k
#ifndef HB_NO_AAT_SHAPE
945
104k
  if (c->plan->apply_morx && c->plan->apply_gpos)
946
202
    hb_aat_layout_remove_deleted_glyphs (c->buffer);
947
104k
#endif
948
104k
}
949
950
static inline void
951
hb_ot_substitute_post (const hb_ot_shape_context_t *c)
952
104k
{
953
104k
#ifndef HB_NO_AAT_SHAPE
954
104k
  if (c->plan->apply_morx && !c->plan->apply_gpos)
955
10.0k
    hb_aat_layout_remove_deleted_glyphs (c->buffer);
956
104k
#endif
957
958
104k
  hb_ot_deal_with_variation_selectors (c->buffer);
959
104k
  hb_ot_hide_default_ignorables (c->buffer, c->font);
960
961
104k
  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
104k
}
967
968
969
/*
970
 * Position
971
 */
972
973
static inline void
974
adjust_mark_offsets (hb_glyph_position_t *pos)
975
843k
{
976
843k
  pos->x_offset -= pos->x_advance;
977
843k
  pos->y_offset -= pos->y_advance;
978
843k
}
979
980
static inline void
981
zero_mark_width (hb_glyph_position_t *pos)
982
2.44M
{
983
2.44M
  pos->x_advance = 0;
984
2.44M
  pos->y_advance = 0;
985
2.44M
}
986
987
static inline void
988
zero_mark_widths_by_gdef (hb_buffer_t *buffer, bool adjust_offsets)
989
95.0k
{
990
95.0k
  unsigned int count = buffer->len;
991
95.0k
  hb_glyph_info_t *info = buffer->info;
992
58.1M
  for (unsigned int i = 0; i < count; i++)
993
58.0M
    if (_hb_glyph_info_is_mark (&info[i]))
994
2.44M
    {
995
2.44M
      if (adjust_offsets)
996
843k
  adjust_mark_offsets (&buffer->pos[i]);
997
2.44M
      zero_mark_width (&buffer->pos[i]);
998
2.44M
    }
999
95.0k
}
1000
1001
static inline void
1002
hb_ot_position_default (const hb_ot_shape_context_t *c)
1003
104k
{
1004
104k
  hb_direction_t direction = c->buffer->props.direction;
1005
104k
  unsigned int count = c->buffer->len;
1006
104k
  hb_glyph_info_t *info = c->buffer->info;
1007
104k
  hb_glyph_position_t *pos = c->buffer->pos;
1008
1009
104k
  if (HB_DIRECTION_IS_HORIZONTAL (direction))
1010
104k
  {
1011
104k
    c->font->get_glyph_h_advances (count, &info[0].codepoint, sizeof(info[0]),
1012
104k
           &pos[0].x_advance, sizeof(pos[0]));
1013
    // h_origin defaults to zero; only apply it if the font has it.
1014
104k
    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
104k
  }
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
104k
  if (c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK)
1025
138
    _hb_ot_shape_fallback_spaces (c->plan, c->font, c->buffer);
1026
104k
}
1027
1028
static inline void
1029
hb_ot_position_plan (const hb_ot_shape_context_t *c)
1030
104k
{
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
104k
  bool adjust_offsets_when_zeroing = c->plan->adjust_mark_positioning_when_zeroing &&
1041
78.1k
             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
104k
  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
104k
  hb_ot_layout_position_start (c->font, c->buffer);
1050
1051
104k
  if (c->plan->zero_marks)
1052
95.2k
    switch (c->plan->shaper->zero_width_marks)
1053
95.2k
    {
1054
2.78k
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
1055
2.78k
  zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
1056
2.78k
  break;
1057
1058
0
      default:
1059
189
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
1060
92.4k
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
1061
92.4k
  break;
1062
95.2k
    }
1063
1064
104k
  c->plan->position (c->font, c->buffer);
1065
1066
104k
  if (c->plan->zero_marks)
1067
95.2k
    switch (c->plan->shaper->zero_width_marks)
1068
95.2k
    {
1069
92.2k
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
1070
92.2k
  zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
1071
92.2k
  break;
1072
1073
0
      default:
1074
189
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
1075
2.96k
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
1076
2.96k
  break;
1077
95.2k
    }
1078
1079
  /* Finish off.  Has to follow a certain order. */
1080
104k
  hb_ot_layout_position_finish_advances (c->font, c->buffer);
1081
104k
  hb_ot_zero_width_default_ignorables (c->buffer);
1082
104k
  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
104k
  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
104k
  if (c->plan->fallback_mark_positioning)
1089
82.6k
    _hb_ot_shape_fallback_mark_position (c->plan, c->font, c->buffer,
1090
82.6k
           adjust_offsets_when_zeroing);
1091
104k
}
1092
1093
static inline void
1094
hb_ot_position (const hb_ot_shape_context_t *c)
1095
104k
{
1096
104k
  c->buffer->clear_positions ();
1097
1098
104k
  hb_ot_position_default (c);
1099
1100
104k
  hb_ot_position_plan (c);
1101
1102
104k
  if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
1103
2.75k
    hb_buffer_reverse (c->buffer);
1104
1105
104k
  _hb_buffer_deallocate_gsubgpos_vars (c->buffer);
1106
104k
}
1107
1108
static inline void
1109
hb_propagate_flags (hb_buffer_t *buffer)
1110
104k
{
1111
  /* Propagate cluster-level glyph flags to be the same on all cluster glyphs.
1112
   * Simplifies using them. */
1113
1114
104k
  hb_mask_t and_mask = HB_GLYPH_FLAG_DEFINED;
1115
104k
  if ((buffer->flags & HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT) == 0)
1116
104k
    and_mask &= ~HB_GLYPH_FLAG_UNSAFE_TO_CONCAT;
1117
1118
104k
  hb_glyph_info_t *info = buffer->info;
1119
1120
104k
  if ((buffer->flags & HB_BUFFER_FLAG_PRODUCE_SAFE_TO_INSERT_TATWEEL) == 0)
1121
104k
  {
1122
104k
    foreach_cluster (buffer, start, end)
1123
1.20M
    {
1124
1.20M
      if (end - start == 1)
1125
1.11M
      {
1126
1.11M
        info[start].mask &= and_mask;
1127
1.11M
  continue;
1128
1.11M
      }
1129
1130
88.7k
      unsigned int mask = 0;
1131
45.1M
      for (unsigned int i = start; i < end; i++)
1132
45.0M
  mask |= info[i].mask;
1133
1134
88.7k
      mask &= and_mask;
1135
1136
45.1M
      for (unsigned int i = start; i < end; i++)
1137
45.0M
  info[i].mask = mask;
1138
88.7k
    }
1139
104k
    return;
1140
104k
  }
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
104k
{
1173
  /* Save the original direction, we use it later. */
1174
104k
  c->target_direction = c->buffer->props.direction;
1175
1176
104k
  _hb_buffer_allocate_unicode_vars (c->buffer);
1177
1178
104k
  hb_ot_shape_initialize_masks (c);
1179
104k
  hb_set_unicode_props (c->buffer);
1180
104k
  hb_insert_dotted_circle (c->buffer, c->font);
1181
1182
104k
  hb_form_clusters (c->buffer);
1183
1184
104k
  hb_ensure_native_direction (c->buffer);
1185
1186
104k
  if (c->plan->shaper->preprocess_text &&
1187
6.18k
      c->buffer->message(c->font, "start preprocess-text"))
1188
6.18k
  {
1189
6.18k
    c->plan->shaper->preprocess_text (c->plan, c->buffer, c->font);
1190
6.18k
    (void) c->buffer->message(c->font, "end preprocess-text");
1191
6.18k
  }
1192
1193
104k
  hb_ot_substitute_pre (c);
1194
104k
  hb_ot_position (c);
1195
104k
  hb_ot_substitute_post (c);
1196
1197
104k
  hb_propagate_flags (c->buffer);
1198
1199
104k
  _hb_buffer_deallocate_unicode_vars (c->buffer);
1200
1201
104k
  c->buffer->props.direction = c->target_direction;
1202
104k
}
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
104k
{
1212
104k
  hb_ot_shape_context_t c = {&shape_plan->ot, font, font->face, buffer, features, num_features};
1213
104k
  hb_ot_shape_internal (&c);
1214
1215
104k
  return true;
1216
104k
}
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
/**
1332
 * hb_ot_shape_get_buffer_format_serial:
1333
 *
1334
 * Returns the serial number of the current internal buffer format.
1335
 * See #HB_OT_SHAPE_BUFFER_FORMAT_SERIAL for more information.
1336
 *
1337
 * Return value: The current buffer-format serial number.
1338
 *
1339
 * Since: 13.2.0
1340
 **/
1341
unsigned int
1342
hb_ot_shape_get_buffer_format_serial (void)
1343
0
{
1344
0
  return HB_OT_SHAPE_BUFFER_FORMAT_SERIAL;
1345
0
}
1346
1347
#endif