Coverage Report

Created: 2026-11-06 11:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/workdir/UnpackedTarball/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
16.6M
{ 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
18.0k
{
62
  /* https://github.com/harfbuzz/harfbuzz/issues/2124 */
63
18.0k
  return hb_aat_layout_has_substitution (face) &&
64
0
   (HB_DIRECTION_IS_HORIZONTAL (props.direction) || !hb_ot_layout_has_substitution (face));
65
18.0k
}
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
18.0k
            face (face),
86
18.0k
            props (props),
87
18.0k
            map (face, props)
88
#ifndef HB_NO_AAT_SHAPE
89
18.0k
            , aat_map (face, props)
90
18.0k
            , apply_morx (_hb_apply_morx (face, props))
91
#endif
92
18.0k
{
93
18.0k
  shaper = hb_ot_shaper_categorize (props.script, props.direction, map.chosen_script[0]);
94
95
18.0k
  script_zero_marks = shaper->zero_width_marks != HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE;
96
18.0k
  script_fallback_position = shaper->fallback_position;
97
98
18.0k
#ifndef HB_NO_AAT_SHAPE
99
  /* https://github.com/harfbuzz/harfbuzz/issues/1528 */
100
18.0k
  if (apply_morx && shaper != &_hb_ot_shaper_default)
101
0
    shaper = &_hb_ot_shaper_dumber;
102
18.0k
#endif
103
18.0k
}
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
18.0k
{
109
18.0k
  plan.props = props;
110
18.0k
  plan.shaper = shaper;
111
18.0k
  map.compile (plan.map, key);
112
18.0k
#ifndef HB_NO_AAT_SHAPE
113
18.0k
  if (apply_morx)
114
0
    aat_map.compile (plan.aat_map);
115
18.0k
#endif
116
117
18.0k
#ifndef HB_NO_OT_SHAPE_FRACTIONS
118
18.0k
  plan.frac_mask = plan.map.get_1_mask (HB_TAG ('f','r','a','c'));
119
18.0k
  plan.numr_mask = plan.map.get_1_mask (HB_TAG ('n','u','m','r'));
120
18.0k
  plan.dnom_mask = plan.map.get_1_mask (HB_TAG ('d','n','o','m'));
121
18.0k
  plan.has_frac = plan.frac_mask || (plan.numr_mask && plan.dnom_mask);
122
18.0k
#endif
123
124
18.0k
  plan.rtlm_mask = plan.map.get_1_mask (HB_TAG ('r','t','l','m'));
125
18.0k
  plan.has_vert = !!plan.map.get_1_mask (HB_TAG ('v','e','r','t'));
126
127
18.0k
  hb_tag_t kern_tag = HB_DIRECTION_IS_HORIZONTAL (props.direction) ?
128
17.8k
          HB_TAG ('k','e','r','n') : HB_TAG ('v','k','r','n');
129
18.0k
#ifndef HB_NO_OT_KERN
130
18.0k
  plan.kern_mask = plan.map.get_mask (kern_tag);
131
18.0k
  plan.requested_kerning = !!plan.kern_mask;
132
18.0k
#endif
133
134
18.0k
  bool has_gpos_kern = plan.map.get_feature_index (1, kern_tag) != HB_OT_LAYOUT_NO_FEATURE_INDEX;
135
18.0k
  bool disable_gpos = plan.shaper->gpos_tag &&
136
114
          plan.shaper->gpos_tag != plan.map.chosen_script[1];
137
138
  /*
139
   * Decide who provides glyph classes. GDEF or Unicode.
140
   */
141
142
18.0k
  if (!hb_ot_layout_has_glyph_classes (face))
143
11.2k
    plan.fallback_glyph_classes = true;
144
145
  /*
146
   * Decide who does substitutions. GSUB, morx, or fallback.
147
   */
148
149
18.0k
#ifndef HB_NO_AAT_SHAPE
150
18.0k
  plan.apply_morx = apply_morx;
151
18.0k
#endif
152
153
  /*
154
   * Decide who does positioning. GPOS, kerx, kern, or fallback.
155
   */
156
157
18.0k
#ifndef HB_NO_AAT_SHAPE
158
18.0k
  bool has_kerx = hb_aat_layout_has_positioning (face);
159
18.0k
  bool has_gsub = !apply_morx && hb_ot_layout_has_substitution (face);
160
18.0k
#endif
161
18.0k
  bool has_gpos = !disable_gpos && hb_ot_layout_has_positioning (face);
162
18.0k
  if (false)
163
0
    {}
164
18.0k
#ifndef HB_NO_AAT_SHAPE
165
  /* Prefer GPOS over kerx if GSUB is present;
166
   * https://github.com/harfbuzz/harfbuzz/issues/3008 */
167
18.0k
  else if (has_kerx && !(has_gsub && has_gpos))
168
2
    plan.apply_kerx = true;
169
18.0k
#endif
170
18.0k
  else if (has_gpos)
171
6.55k
    plan.apply_gpos = true;
172
173
18.0k
  if (!plan.apply_kerx && (!has_gpos_kern || !plan.apply_gpos))
174
16.9k
  {
175
16.9k
    if (false) {}
176
16.9k
#ifndef HB_NO_AAT_SHAPE
177
16.9k
    else if (has_kerx)
178
0
      plan.apply_kerx = true;
179
16.9k
#endif
180
16.9k
#ifndef HB_NO_OT_KERN
181
16.9k
    else if (hb_ot_layout_has_kerning (face))
182
5.97k
      plan.apply_kern = script_fallback_position; // Not all shapers apply legacy `kern`
183
10.9k
#endif
184
10.9k
    else {}
185
16.9k
  }
186
187
18.0k
  plan.apply_fallback_kern = script_fallback_position && !(plan.apply_gpos || plan.apply_kerx || plan.apply_kern);
188
189
18.0k
  plan.zero_marks = script_zero_marks &&
190
17.8k
        !plan.apply_kerx &&
191
17.8k
        (!plan.apply_kern
192
5.66k
#ifndef HB_NO_OT_KERN
193
5.66k
         || !hb_ot_layout_has_machine_kerning (face)
194
17.8k
#endif
195
17.8k
        );
196
18.0k
  plan.has_gpos_mark = !!plan.map.get_1_mask (HB_TAG ('m','a','r','k'));
197
198
18.0k
  plan.adjust_mark_positioning_when_zeroing = !plan.apply_gpos &&
199
11.4k
                !plan.apply_kerx &&
200
11.4k
                (!plan.apply_kern
201
601
#ifndef HB_NO_OT_KERN
202
601
                 || !hb_ot_layout_has_cross_kerning (face)
203
11.4k
#endif
204
11.4k
                );
205
206
18.0k
  plan.fallback_mark_positioning = plan.adjust_mark_positioning_when_zeroing &&
207
11.3k
           script_fallback_position;
208
209
18.0k
#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
18.0k
  if (plan.apply_morx)
214
0
    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
18.0k
#ifndef HB_NO_STYLE
218
18.0k
  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
18.0k
#endif
224
18.0k
}
225
226
bool
227
hb_ot_shape_plan_t::init0 (hb_face_t                     *face,
228
         const hb_shape_plan_key_t     *key)
229
18.0k
{
230
18.0k
  map.init ();
231
232
18.0k
  hb_ot_shape_planner_t planner (face,
233
18.0k
         key->props);
234
235
18.0k
  hb_ot_shape_collect_features (&planner,
236
18.0k
        key->user_features,
237
18.0k
        key->num_user_features);
238
239
18.0k
  planner.compile (*this, key->ot);
240
241
18.0k
  if (shaper->data_create)
242
366
  {
243
366
    data = shaper->data_create (this);
244
366
    if (unlikely (!data))
245
0
    {
246
0
      map.fini ();
247
0
      return false;
248
0
    }
249
366
  }
250
251
18.0k
  return true;
252
18.0k
}
253
254
void
255
hb_ot_shape_plan_t::fini ()
256
11.5k
{
257
11.5k
  if (shaper->data_destroy)
258
0
    shaper->data_destroy (const_cast<void *> (data));
259
260
11.5k
  map.fini ();
261
11.5k
}
262
263
void
264
hb_ot_shape_plan_t::substitute (hb_font_t   *font,
265
        hb_buffer_t *buffer) const
266
25.4M
{
267
25.4M
  map.substitute (this, font, buffer);
268
25.4M
}
269
270
void
271
hb_ot_shape_plan_t::position (hb_font_t   *font,
272
            hb_buffer_t *buffer) const
273
25.4M
{
274
25.4M
  if (this->apply_gpos)
275
25.4M
    map.position (this, font, buffer);
276
38.0k
#ifndef HB_NO_AAT_SHAPE
277
38.0k
  else if (this->apply_kerx)
278
5
    hb_aat_layout_position (this, font, buffer);
279
25.4M
#endif
280
281
25.4M
#ifndef HB_NO_OT_KERN
282
25.4M
  if (this->apply_kern)
283
18.2M
    hb_ot_layout_kern (this, font, buffer);
284
7.20M
#endif
285
7.20M
  else if (this->apply_fallback_kern)
286
36.2k
    _hb_ot_shape_fallback_kern (this, font, buffer);
287
288
25.4M
#ifndef HB_NO_AAT_SHAPE
289
25.4M
  if (this->apply_trak)
290
0
    hb_aat_layout_track (this, font, buffer);
291
25.4M
#endif
292
25.4M
}
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
18.0k
{
325
18.0k
  hb_ot_map_builder_t *map = &planner->map;
326
327
18.0k
  map->is_simple = true;
328
329
18.0k
  map->enable_feature (HB_TAG('r','v','r','n'));
330
18.0k
  map->add_gsub_pause (nullptr);
331
332
18.0k
  switch (planner->props.direction)
333
18.0k
  {
334
16.7k
    case HB_DIRECTION_LTR:
335
16.7k
      map->enable_feature (HB_TAG ('l','t','r','a'));
336
16.7k
      map->enable_feature (HB_TAG ('l','t','r','m'));
337
16.7k
      break;
338
1.05k
    case HB_DIRECTION_RTL:
339
1.05k
      map->enable_feature (HB_TAG ('r','t','l','a'));
340
1.05k
      map->add_feature (HB_TAG ('r','t','l','m'));
341
1.05k
      break;
342
209
    case HB_DIRECTION_TTB:
343
209
    case HB_DIRECTION_BTT:
344
209
    case HB_DIRECTION_INVALID:
345
209
    default:
346
209
      break;
347
18.0k
  }
348
349
18.0k
#ifndef HB_NO_OT_SHAPE_FRACTIONS
350
  /* Automatic fractions. */
351
18.0k
  map->add_feature (HB_TAG ('f','r','a','c'));
352
18.0k
  map->add_feature (HB_TAG ('n','u','m','r'));
353
18.0k
  map->add_feature (HB_TAG ('d','n','o','m'));
354
18.0k
#endif
355
356
  /* Random! */
357
18.0k
  map->enable_feature (HB_TAG ('r','a','n','d'), F_RANDOM, HB_OT_MAP_MAX_VALUE);
358
359
18.0k
  map->enable_feature (HB_TAG ('H','a','r','f')); /* Considered required. */
360
18.0k
  map->enable_feature (HB_TAG ('H','A','R','F')); /* Considered discretionary. */
361
362
18.0k
  if (planner->shaper->collect_features)
363
366
  {
364
366
    map->is_simple = false;
365
366
    planner->shaper->collect_features (planner);
366
366
  }
367
368
18.0k
  map->enable_feature (HB_TAG ('B','u','z','z')); /* Considered required. */
369
18.0k
  map->enable_feature (HB_TAG ('B','U','Z','Z')); /* Considered discretionary. */
370
371
144k
  for (unsigned int i = 0; i < ARRAY_LENGTH (common_features); i++)
372
126k
    map->add_feature (common_features[i]);
373
374
18.0k
  if (HB_DIRECTION_IS_HORIZONTAL (planner->props.direction))
375
142k
    for (unsigned int i = 0; i < ARRAY_LENGTH (horizontal_features); i++)
376
124k
      map->add_feature (horizontal_features[i]);
377
209
  else
378
209
  {
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
209
    map->enable_feature (HB_TAG ('v','e','r','t'), F_GLOBAL_SEARCH);
388
209
  }
389
390
18.0k
  if (num_user_features)
391
5.35k
    map->is_simple = false;
392
321k
  for (unsigned int i = 0; i < num_user_features; i++)
393
303k
  {
394
303k
    const hb_feature_t *feature = &user_features[i];
395
303k
    map->add_feature (feature->tag,
396
303k
          (feature->start == HB_FEATURE_GLOBAL_START &&
397
303k
           feature->end == HB_FEATURE_GLOBAL_END) ?  F_GLOBAL : F_NONE,
398
303k
          feature->value);
399
303k
  }
400
401
18.0k
  if (planner->shaper->override_features)
402
213
    planner->shaper->override_features (planner);
403
18.0k
}
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
9.02k
{
415
9.02k
  return (hb_ot_face_data_t *) HB_SHAPER_DATA_SUCCEEDED;
416
9.02k
}
417
418
void
419
_hb_ot_shaper_face_data_destroy (hb_ot_face_data_t *data)
420
8.97k
{
421
8.97k
}
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
160k
{
435
160k
  const OT::ItemVariationStore &var_store = font->face->table.GDEF->table->get_var_store ();
436
160k
  return (hb_ot_font_data_t *) var_store.create_cache ();
437
160k
}
438
439
void
440
_hb_ot_shaper_font_data_destroy (hb_ot_font_data_t *data)
441
159k
{
442
159k
  OT::ItemVariationStore::destroy_cache ((OT::hb_scalar_cache_t *) data);
443
159k
}
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
          hb_mask_t    global_mask)
473
25.4M
{
474
  /* Implement enough of Unicode Graphemes here that shaping
475
   * in reverse-direction wouldn't break graphemes.  Namely,
476
   * we mark all marks and ZWJ and ZWJ,Extended_Pictographic
477
   * sequences as continuations.  The foreach_grapheme()
478
   * macro uses this bit.
479
   *
480
   * https://www.unicode.org/reports/tr29/#Regex_Definitions
481
   */
482
25.4M
  unsigned int count = buffer->len;
483
25.4M
  hb_glyph_info_t *info = buffer->info;
484
309M
  for (unsigned int i = 0; i < count; i++)
485
284M
  {
486
284M
    info[i].mask = global_mask;
487
284M
    _hb_glyph_info_set_unicode_props (&info[i], buffer);
488
489
284M
    if (info[i].codepoint < 0x80)
490
213M
      continue;
491
492
71.1M
    unsigned gen_cat = _hb_glyph_info_get_general_category (&info[i]);
493
71.1M
    if (FLAG_UNSAFE (gen_cat) &
494
71.1M
  (FLAG (HB_UNICODE_GENERAL_CATEGORY_LOWERCASE_LETTER) |
495
71.1M
   FLAG (HB_UNICODE_GENERAL_CATEGORY_UPPERCASE_LETTER) |
496
71.1M
   FLAG (HB_UNICODE_GENERAL_CATEGORY_TITLECASE_LETTER) |
497
71.1M
   FLAG (HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER) |
498
71.1M
   FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACE_SEPARATOR)))
499
50.3M
      continue;
500
501
    /* Marks are already set as continuation by the above line.
502
     * Handle Emoji_Modifier and ZWJ-continuation. */
503
20.8M
    if (unlikely (gen_cat == HB_UNICODE_GENERAL_CATEGORY_MODIFIER_SYMBOL &&
504
20.8M
      hb_in_range<hb_codepoint_t> (info[i].codepoint, 0x1F3FBu, 0x1F3FFu)))
505
0
    {
506
0
      _hb_glyph_info_set_continuation (&info[i], buffer);
507
0
    }
508
    /* Regional_Indicators are hairy as hell...
509
     * https://github.com/harfbuzz/harfbuzz/issues/2265 */
510
20.8M
    else if (unlikely (i && _hb_codepoint_is_regional_indicator (info[i].codepoint)))
511
0
    {
512
0
      if (_hb_codepoint_is_regional_indicator (info[i - 1].codepoint) &&
513
0
    !_hb_glyph_info_is_continuation (&info[i - 1]))
514
0
  _hb_glyph_info_set_continuation (&info[i], buffer);
515
0
    }
516
20.8M
#ifndef HB_NO_EMOJI_SEQUENCES
517
20.8M
    else if (unlikely (_hb_glyph_info_is_zwj (&info[i])))
518
5.27k
    {
519
5.27k
      _hb_glyph_info_set_continuation (&info[i], buffer);
520
5.27k
      if (i + 1 < count &&
521
4.42k
    _hb_unicode_is_emoji_Extended_Pictographic (info[i + 1].codepoint))
522
450
      {
523
450
  i++;
524
450
  info[i].mask = global_mask;
525
450
  _hb_glyph_info_set_unicode_props (&info[i], buffer);
526
450
  _hb_glyph_info_set_continuation (&info[i], buffer);
527
450
      }
528
5.27k
    }
529
20.8M
#endif
530
    /* Or part of the Other_Grapheme_Extend that is not marks.
531
     * As of Unicode 15 that is just:
532
     *
533
     * 200C          ; Other_Grapheme_Extend # Cf       ZERO WIDTH NON-JOINER
534
     * FF9E..FF9F    ; Other_Grapheme_Extend # Lm   [2] HALFWIDTH KATAKANA VOICED SOUND MARK..HALFWIDTH KATAKANA SEMI-VOICED SOUND MARK
535
     * E0020..E007F  ; Other_Grapheme_Extend # Cf  [96] TAG SPACE..CANCEL TAG
536
     *
537
     * ZWNJ is special, we don't want to merge it as there's no need, and keeping
538
     * it separate results in more granular clusters.
539
     * Tags are used for Emoji sub-region flag sequences:
540
     * https://github.com/harfbuzz/harfbuzz/issues/1556
541
     * Katakana ones were requested:
542
     * https://github.com/harfbuzz/harfbuzz/issues/3844
543
     */
544
20.8M
    else if (unlikely (hb_in_ranges<hb_codepoint_t> (info[i].codepoint, 0xFF9Eu, 0xFF9Fu, 0xE0020u, 0xE007Fu)))
545
52.7k
      _hb_glyph_info_set_continuation (&info[i], buffer);
546
20.7M
    else if (unlikely (info[i].codepoint == 0x2044u /* FRACTION SLASH */))
547
1.33k
      buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_FRACTION_SLASH;
548
20.8M
  }
549
25.4M
}
550
551
static void
552
hb_insert_dotted_circle (hb_buffer_t *buffer, hb_font_t *font)
553
25.4M
{
554
25.4M
  if (unlikely (buffer->flags & HB_BUFFER_FLAG_DO_NOT_INSERT_DOTTED_CIRCLE))
555
0
    return;
556
557
25.4M
  if (!(buffer->flags & HB_BUFFER_FLAG_BOT) ||
558
2.10M
      buffer->context_len[0] ||
559
2.10M
      !_hb_glyph_info_is_unicode_mark (&buffer->info[0]))
560
25.4M
    return;
561
562
2.56k
  if (!font->has_glyph (0x25CCu))
563
0
    return;
564
565
2.56k
  hb_glyph_info_t dottedcircle = {0};
566
2.56k
  dottedcircle.codepoint = 0x25CCu;
567
2.56k
  _hb_glyph_info_set_unicode_props (&dottedcircle, buffer);
568
569
2.56k
  buffer->clear_output ();
570
571
2.56k
  buffer->idx = 0;
572
2.56k
  hb_glyph_info_t info = dottedcircle;
573
2.56k
  info.cluster = buffer->cur().cluster;
574
2.56k
  info.mask = buffer->cur().mask;
575
2.56k
  (void) buffer->output_info (info);
576
577
2.56k
  buffer->sync ();
578
2.56k
}
579
580
static void
581
hb_form_clusters (hb_buffer_t *buffer)
582
25.4M
{
583
25.4M
  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_CONTINUATIONS))
584
24.5M
    return;
585
586
857k
  foreach_grapheme (buffer, start, end)
587
12.3M
    buffer->merge_grapheme_clusters (start, end);
588
857k
}
589
590
static void
591
hb_ensure_native_direction (hb_buffer_t *buffer)
592
25.4M
{
593
25.4M
  hb_direction_t direction = buffer->props.direction;
594
25.4M
  hb_direction_t horiz_dir = hb_script_get_horizontal_direction (buffer->props.script);
595
596
  /* Numeric runs in natively-RTL scripts are actually native-LTR, so we reset
597
   * the horiz_dir if the run contains at least one decimal-number char, and no
598
   * letter chars (ideally we should be checking for chars with strong
599
   * directionality but hb-unicode currently lacks bidi categories).
600
   *
601
   * This allows digit sequences in Arabic etc to be shaped in "native"
602
   * direction, so that features like ligatures will work as intended.
603
   *
604
   * https://github.com/harfbuzz/harfbuzz/issues/501
605
   *
606
   * Similar thing about Regional_Indicators; They are bidi=L, but Script=Common.
607
   * If they are present in a run of natively-RTL text, they get assigned a script
608
   * with natively RTL direction, which would result in wrong shaping if we
609
   * assign such native RTL direction to them then. Detect that as well.
610
   *
611
   * https://github.com/harfbuzz/harfbuzz/issues/3314
612
   */
613
25.4M
  if (unlikely (horiz_dir == HB_DIRECTION_RTL && direction == HB_DIRECTION_LTR))
614
382k
  {
615
382k
    bool found_number = false, found_letter = false, found_ri = false;
616
382k
    const auto* info = buffer->info;
617
382k
    const auto count = buffer->len;
618
1.70M
    for (unsigned i = 0; i < count; i++)
619
1.33M
    {
620
1.33M
      auto gc = _hb_glyph_info_get_general_category (&info[i]);
621
1.33M
      if (HB_UNICODE_GENERAL_CATEGORY_IS_LETTER (gc))
622
9.86k
      {
623
9.86k
  found_letter = true;
624
9.86k
  break;
625
9.86k
      }
626
1.32M
      else if (gc == HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
627
124k
  found_number = true;
628
1.19M
      else if (unlikely (_hb_codepoint_is_regional_indicator (info[i].codepoint)))
629
0
  found_ri = true;
630
1.33M
    }
631
382k
    if ((found_number || found_ri) && !found_letter)
632
87.8k
      horiz_dir = HB_DIRECTION_LTR;
633
382k
  }
634
635
  /* TODO vertical:
636
   * The only BTT vertical script is Ogham, but it's not clear to me whether OpenType
637
   * Ogham fonts are supposed to be implemented BTT or not.  Need to research that
638
   * first. */
639
25.4M
  if ((HB_DIRECTION_IS_HORIZONTAL (direction) &&
640
25.2M
       direction != horiz_dir && HB_DIRECTION_IS_VALID (horiz_dir)) ||
641
23.9M
      (HB_DIRECTION_IS_VERTICAL   (direction) &&
642
164k
       direction != HB_DIRECTION_TTB))
643
1.54M
  {
644
1.54M
    _hb_ot_layout_reverse_graphemes (buffer);
645
1.54M
    buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
646
1.54M
  }
647
25.4M
}
648
649
650
/*
651
 * Substitute
652
 */
653
654
static inline void
655
hb_ot_rotate_chars (const hb_ot_shape_context_t *c)
656
25.4M
{
657
25.4M
  hb_buffer_t *buffer = c->buffer;
658
25.4M
  unsigned int count = buffer->len;
659
25.4M
  hb_glyph_info_t *info = buffer->info;
660
661
25.4M
  if (HB_DIRECTION_IS_BACKWARD (c->target_direction))
662
1.96M
  {
663
1.96M
    hb_unicode_funcs_t *unicode = buffer->unicode;
664
1.96M
    hb_mask_t rtlm_mask = c->plan->rtlm_mask;
665
666
61.6M
    for (unsigned int i = 0; i < count; i++) {
667
59.6M
      hb_codepoint_t codepoint = unicode->mirroring (info[i].codepoint);
668
59.6M
      if (unlikely (codepoint != info[i].codepoint && c->font->has_glyph (codepoint)))
669
400k
  info[i].codepoint = codepoint;
670
59.2M
      else
671
59.2M
  info[i].mask |= rtlm_mask;
672
59.6M
    }
673
1.96M
  }
674
675
25.4M
#ifndef HB_NO_VERTICAL
676
25.4M
  if (HB_DIRECTION_IS_VERTICAL (c->target_direction) && !c->plan->has_vert)
677
164k
  {
678
816k
    for (unsigned int i = 0; i < count; i++) {
679
652k
      hb_codepoint_t codepoint = hb_unicode_funcs_t::vertical_char_for (info[i].codepoint);
680
652k
      if (unlikely (codepoint != info[i].codepoint && c->font->has_glyph (codepoint)))
681
0
  info[i].codepoint = codepoint;
682
652k
    }
683
164k
  }
684
25.4M
#endif
685
25.4M
}
686
687
static inline void
688
hb_ot_shape_setup_masks_fraction (const hb_ot_shape_context_t *c)
689
25.4M
{
690
#ifdef HB_NO_OT_SHAPE_FRACTIONS
691
  return;
692
#endif
693
694
25.4M
  if (!(c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_FRACTION_SLASH) ||
695
658
      !c->plan->has_frac)
696
25.4M
    return;
697
698
0
  hb_buffer_t *buffer = c->buffer;
699
700
0
  hb_mask_t pre_mask, post_mask;
701
0
  if (HB_DIRECTION_IS_FORWARD (buffer->props.direction))
702
0
  {
703
0
    pre_mask = c->plan->numr_mask | c->plan->frac_mask;
704
0
    post_mask = c->plan->frac_mask | c->plan->dnom_mask;
705
0
  }
706
0
  else
707
0
  {
708
0
    pre_mask = c->plan->frac_mask | c->plan->dnom_mask;
709
0
    post_mask = c->plan->numr_mask | c->plan->frac_mask;
710
0
  }
711
712
0
  unsigned int count = buffer->len;
713
0
  hb_glyph_info_t *info = buffer->info;
714
0
  for (unsigned int i = 0; i < count; i++)
715
0
  {
716
0
    if (info[i].codepoint == 0x2044u) /* FRACTION SLASH */
717
0
    {
718
0
      unsigned int start = i, end = i + 1;
719
0
      while (start &&
720
0
       _hb_glyph_info_get_general_category (&info[start - 1]) ==
721
0
       HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
722
0
  start--;
723
0
      while (end < count &&
724
0
       _hb_glyph_info_get_general_category (&info[end]) ==
725
0
       HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
726
0
  end++;
727
0
      if (start == i || end == i + 1)
728
0
      {
729
0
        if (start == i)
730
0
    buffer->unsafe_to_concat (start, start + 1);
731
0
  if (end == i + 1)
732
0
    buffer->unsafe_to_concat (end - 1, end);
733
0
  continue;
734
0
      }
735
736
0
      buffer->unsafe_to_break (start, end);
737
738
0
      for (unsigned int j = start; j < i; j++)
739
0
  info[j].mask |= pre_mask;
740
0
      info[i].mask |= c->plan->frac_mask;
741
0
      for (unsigned int j = i + 1; j < end; j++)
742
0
  info[j].mask |= post_mask;
743
744
0
      i = end - 1;
745
0
    }
746
0
  }
747
0
}
748
749
static inline void
750
hb_ot_shape_setup_masks (const hb_ot_shape_context_t *c)
751
25.4M
{
752
25.4M
  hb_ot_map_t *map = &c->plan->map;
753
25.4M
  hb_buffer_t *buffer = c->buffer;
754
755
25.4M
  hb_ot_shape_setup_masks_fraction (c);
756
757
25.4M
  if (c->plan->shaper->setup_masks)
758
1.34M
    c->plan->shaper->setup_masks (c->plan, buffer, c->font);
759
760
109M
  for (unsigned int i = 0; i < c->num_user_features; i++)
761
84.0M
  {
762
84.0M
    const hb_feature_t *feature = &c->user_features[i];
763
84.0M
    if (!(feature->start == HB_FEATURE_GLOBAL_START && feature->end == HB_FEATURE_GLOBAL_END)) {
764
36.8k
      unsigned int shift;
765
36.8k
      hb_mask_t mask = map->get_mask (feature->tag, &shift);
766
36.8k
      buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
767
36.8k
    }
768
84.0M
  }
769
25.4M
}
770
771
static void
772
hb_ot_zero_width_default_ignorables (const hb_buffer_t *buffer)
773
25.4M
{
774
25.4M
  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
775
495k
      (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES) ||
776
495k
      (buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES))
777
24.9M
    return;
778
779
495k
  unsigned int count = buffer->len;
780
495k
  hb_glyph_info_t *info = buffer->info;
781
495k
  hb_glyph_position_t *pos = buffer->pos;
782
495k
  unsigned int i = 0;
783
20.2M
  for (i = 0; i < count; i++)
784
19.7M
    if (unlikely (_hb_glyph_info_is_default_ignorable (&info[i])))
785
2.03M
    {
786
2.03M
      pos[i].x_advance = pos[i].y_advance = 0;
787
2.03M
      if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
788
2.02M
  pos[i].x_offset = 0;
789
9.01k
      else
790
9.01k
        pos[i].y_offset = 0;
791
2.03M
    }
792
495k
}
793
794
static void
795
hb_ot_deal_with_variation_selectors (hb_buffer_t *buffer)
796
25.4M
{
797
25.4M
  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_VARIATION_SELECTOR_FALLBACK) ||
798
21.3k
  buffer->not_found_variation_selector == HB_CODEPOINT_INVALID)
799
25.4M
    return;
800
801
0
  unsigned int count = buffer->len;
802
0
  hb_glyph_info_t *info = buffer->info;
803
0
  hb_glyph_position_t *pos = buffer->pos;
804
805
0
  for (unsigned int i = 0; i < count; i++)
806
0
  {
807
0
    if (_hb_glyph_info_is_variation_selector (&info[i]))
808
0
    {
809
0
      info[i].codepoint = buffer->not_found_variation_selector;
810
0
      pos[i].x_advance = pos[i].y_advance = pos[i].x_offset = pos[i].y_offset = 0;
811
0
      _hb_glyph_info_set_variation_selector (&info[i], false);
812
0
    }
813
0
  }
814
0
}
815
816
static void
817
hb_ot_hide_default_ignorables (hb_buffer_t *buffer,
818
             hb_font_t   *font)
819
25.4M
{
820
25.4M
  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
821
495k
      (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES))
822
24.9M
    return;
823
824
495k
  unsigned int count = buffer->len;
825
495k
  hb_glyph_info_t *info = buffer->info;
826
827
495k
  hb_codepoint_t invisible = buffer->invisible;
828
495k
  if (!(buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES) &&
829
495k
      (invisible || font->get_nominal_glyph (' ', &invisible)))
830
495k
  {
831
    /* Replace default-ignorables with a zero-advance invisible glyph. */
832
20.2M
    for (unsigned int i = 0; i < count; i++)
833
19.7M
    {
834
19.7M
      if (_hb_glyph_info_is_default_ignorable (&info[i]))
835
2.03M
  info[i].codepoint = invisible;
836
19.7M
    }
837
495k
  }
838
0
  else
839
0
    buffer->delete_glyphs_inplace (_hb_glyph_info_is_default_ignorable);
840
495k
}
841
842
843
static inline void
844
hb_ot_map_glyphs_fast (hb_buffer_t  *buffer)
845
25.4M
{
846
  /* Normalization process sets up normalizer_glyph_index(), we just copy it. */
847
25.4M
  unsigned int count = buffer->len;
848
25.4M
  hb_glyph_info_t *info = buffer->info;
849
309M
  for (unsigned int i = 0; i < count; i++)
850
284M
    info[i].codepoint = info[i].normalizer_glyph_index();
851
852
25.4M
  buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
853
25.4M
}
854
855
static inline void
856
hb_synthesize_glyph_classes (hb_buffer_t *buffer)
857
37.4k
{
858
37.4k
  unsigned int count = buffer->len;
859
37.4k
  hb_glyph_info_t *info = buffer->info;
860
209k
  for (unsigned int i = 0; i < count; i++)
861
172k
  {
862
172k
    hb_ot_layout_glyph_props_flags_t klass;
863
864
    /* Never mark default-ignorables as marks.
865
     * They won't get in the way of lookups anyway,
866
     * but having them as mark will cause them to be skipped
867
     * over if the lookup-flag says so, but at least for the
868
     * Mongolian variation selectors, looks like Uniscribe
869
     * marks them as non-mark.  Some Mongolian fonts without
870
     * GDEF rely on this.  Another notable character that
871
     * this applies to is COMBINING GRAPHEME JOINER. */
872
172k
    klass = (_hb_glyph_info_get_general_category (&info[i]) !=
873
172k
       HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK ||
874
0
       _hb_glyph_info_is_default_ignorable (&info[i])) ?
875
172k
      HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH :
876
172k
      HB_OT_LAYOUT_GLYPH_PROPS_MARK;
877
172k
    _hb_glyph_info_set_glyph_props (&info[i], klass);
878
172k
  }
879
37.4k
}
880
881
static inline void
882
hb_ot_substitute_default (const hb_ot_shape_context_t *c)
883
25.4M
{
884
25.4M
  hb_buffer_t *buffer = c->buffer;
885
886
25.4M
  hb_ot_rotate_chars (c);
887
888
25.4M
  HB_BUFFER_ALLOCATE_VAR (buffer, normalizer_glyph_index);
889
890
25.4M
  _hb_ot_shape_normalize (c->plan, buffer, c->font);
891
892
25.4M
  hb_ot_shape_setup_masks (c);
893
894
  /* This is unfortunate to go here, but necessary... */
895
25.4M
  if (c->plan->fallback_mark_positioning)
896
37.7k
    _hb_ot_shape_fallback_mark_position_recategorize_marks (c->plan, c->font, buffer);
897
898
25.4M
  hb_ot_map_glyphs_fast (buffer);
899
900
25.4M
  HB_BUFFER_DEALLOCATE_VAR (buffer, normalizer_glyph_index);
901
25.4M
}
902
903
static inline void
904
hb_ot_substitute_plan (const hb_ot_shape_context_t *c)
905
25.4M
{
906
25.4M
  hb_buffer_t *buffer = c->buffer;
907
908
25.4M
#ifndef HB_NO_AAT_SHAPE
909
25.4M
  if (unlikely (c->plan->apply_morx))
910
0
  {
911
0
    hb_ot_layout_substitute_start (c->font, buffer);
912
913
0
    if (c->plan->fallback_glyph_classes)
914
0
      hb_synthesize_glyph_classes (buffer);
915
916
0
    hb_aat_layout_substitute (c->plan, c->font, c->buffer,
917
0
            c->user_features, c->num_user_features);
918
    /* The buffer digest is only used by the OT lookup-apply loop;
919
     * without GPOS ahead, nothing consumes it. */
920
0
    if (c->plan->apply_gpos)
921
0
      c->buffer->update_digest ();
922
0
  }
923
25.4M
  else
924
25.4M
#endif
925
25.4M
  {
926
25.4M
    hb_ot_layout_substitute_start_with_digest (c->font, buffer);
927
928
25.4M
    if (c->plan->fallback_glyph_classes)
929
37.4k
      hb_synthesize_glyph_classes (buffer);
930
931
25.4M
    c->plan->substitute (c->font, buffer);
932
25.4M
  }
933
25.4M
}
934
935
static inline void
936
hb_ot_substitute_pre (const hb_ot_shape_context_t *c)
937
25.4M
{
938
25.4M
  hb_ot_substitute_default (c);
939
940
25.4M
  _hb_buffer_allocate_gsubgpos_vars (c->buffer);
941
942
25.4M
  hb_ot_substitute_plan (c);
943
944
25.4M
#ifndef HB_NO_AAT_SHAPE
945
25.4M
  if (c->plan->apply_morx && c->plan->apply_gpos)
946
0
    hb_aat_layout_remove_deleted_glyphs (c->buffer);
947
25.4M
#endif
948
25.4M
}
949
950
static inline void
951
hb_ot_substitute_post (const hb_ot_shape_context_t *c)
952
25.4M
{
953
25.4M
#ifndef HB_NO_AAT_SHAPE
954
25.4M
  if (c->plan->apply_morx && !c->plan->apply_gpos)
955
0
    hb_aat_layout_remove_deleted_glyphs (c->buffer);
956
25.4M
#endif
957
958
25.4M
  hb_ot_deal_with_variation_selectors (c->buffer);
959
25.4M
  hb_ot_hide_default_ignorables (c->buffer, c->font);
960
961
25.4M
  if (c->plan->shaper->postprocess_glyphs &&
962
398k
    c->buffer->message(c->font, "start postprocess-glyphs")) {
963
398k
    c->plan->shaper->postprocess_glyphs (c->plan, c->buffer, c->font);
964
398k
    (void) c->buffer->message(c->font, "end postprocess-glyphs");
965
398k
  }
966
25.4M
}
967
968
969
/*
970
 * Position
971
 */
972
973
static inline void
974
adjust_mark_offsets (hb_glyph_position_t *pos)
975
0
{
976
0
  pos->x_offset = hb_saturate_sub (pos->x_offset, pos->x_advance);
977
0
  pos->y_offset = hb_saturate_sub (pos->y_offset, pos->y_advance);
978
0
}
979
980
static inline void
981
zero_mark_width (hb_glyph_position_t *pos)
982
498k
{
983
498k
  pos->x_advance = 0;
984
498k
  pos->y_advance = 0;
985
498k
}
986
987
static inline void
988
zero_mark_widths_by_gdef (hb_buffer_t *buffer, bool adjust_offsets)
989
24.5M
{
990
24.5M
  unsigned int count = buffer->len;
991
24.5M
  hb_glyph_info_t *info = buffer->info;
992
318M
  for (unsigned int i = 0; i < count; i++)
993
293M
    if (_hb_glyph_info_is_mark (&info[i]))
994
498k
    {
995
498k
      if (adjust_offsets)
996
0
  adjust_mark_offsets (&buffer->pos[i]);
997
498k
      zero_mark_width (&buffer->pos[i]);
998
498k
    }
999
24.5M
}
1000
1001
static inline void
1002
hb_ot_position_default (const hb_ot_shape_context_t *c)
1003
25.4M
{
1004
25.4M
  hb_direction_t direction = c->buffer->props.direction;
1005
25.4M
  unsigned int count = c->buffer->len;
1006
25.4M
  hb_glyph_info_t *info = c->buffer->info;
1007
25.4M
  hb_glyph_position_t *pos = c->buffer->pos;
1008
1009
25.4M
  if (HB_DIRECTION_IS_HORIZONTAL (direction))
1010
25.2M
  {
1011
25.2M
    c->font->get_glyph_h_advances (count, &info[0].codepoint, sizeof(info[0]),
1012
25.2M
           &pos[0].x_advance, sizeof(pos[0]));
1013
    // h_origin defaults to zero; only apply it if the font has it.
1014
25.2M
    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
25.2M
  }
1017
164k
  else
1018
164k
  {
1019
164k
    c->font->get_glyph_v_advances (count, &info[0].codepoint, sizeof(info[0]),
1020
164k
           &pos[0].y_advance, sizeof(pos[0]));
1021
    // v_origin defaults to non-zero; apply even if only fallback is there.
1022
164k
    c->font->subtract_glyph_v_origins (c->buffer);
1023
164k
  }
1024
25.4M
  if (c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK)
1025
8.85k
    _hb_ot_shape_fallback_spaces (c->plan, c->font, c->buffer);
1026
25.4M
}
1027
1028
static inline void
1029
hb_ot_position_plan (const hb_ot_shape_context_t *c)
1030
25.4M
{
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
25.4M
  bool adjust_offsets_when_zeroing = c->plan->adjust_mark_positioning_when_zeroing &&
1041
37.7k
             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
25.4M
  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
25.4M
  hb_ot_layout_position_start (c->font, c->buffer);
1050
1051
25.4M
  if (c->plan->zero_marks)
1052
24.5M
    switch (c->plan->shaper->zero_width_marks)
1053
24.5M
    {
1054
0
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
1055
0
  zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
1056
0
  break;
1057
1058
0
      default:
1059
0
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
1060
24.5M
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
1061
24.5M
  break;
1062
24.5M
    }
1063
1064
25.4M
  c->plan->position (c->font, c->buffer);
1065
1066
25.4M
  if (c->plan->zero_marks)
1067
24.5M
    switch (c->plan->shaper->zero_width_marks)
1068
24.5M
    {
1069
24.5M
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
1070
24.5M
  zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
1071
24.5M
  break;
1072
1073
0
      default:
1074
0
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
1075
0
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
1076
0
  break;
1077
24.5M
    }
1078
1079
  /* Finish off.  Has to follow a certain order. */
1080
25.4M
  hb_ot_layout_position_finish_advances (c->font, c->buffer);
1081
25.4M
  hb_ot_zero_width_default_ignorables (c->buffer);
1082
25.4M
  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
25.4M
  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
25.4M
  if (c->plan->fallback_mark_positioning)
1089
37.7k
    _hb_ot_shape_fallback_mark_position (c->plan, c->font, c->buffer,
1090
37.7k
           adjust_offsets_when_zeroing);
1091
25.4M
}
1092
1093
static inline void
1094
hb_ot_position (const hb_ot_shape_context_t *c)
1095
25.4M
{
1096
25.4M
  c->buffer->clear_positions ();
1097
1098
25.4M
  hb_ot_position_default (c);
1099
1100
25.4M
  hb_ot_position_plan (c);
1101
1102
25.4M
  if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
1103
1.00M
    hb_buffer_reverse (c->buffer);
1104
1105
25.4M
  _hb_buffer_deallocate_gsubgpos_vars (c->buffer);
1106
25.4M
}
1107
1108
static inline void
1109
hb_propagate_flags (hb_buffer_t *buffer)
1110
25.4M
{
1111
  /* Propagate cluster-level glyph flags to be the same on all cluster glyphs.
1112
   * Simplifies using them. */
1113
1114
25.4M
  hb_mask_t and_mask = HB_GLYPH_FLAG_DEFINED;
1115
25.4M
  if ((buffer->flags & HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT) == 0)
1116
66
    and_mask &= ~HB_GLYPH_FLAG_UNSAFE_TO_CONCAT;
1117
1118
25.4M
  hb_glyph_info_t *info = buffer->info;
1119
1120
25.4M
  if ((buffer->flags & HB_BUFFER_FLAG_PRODUCE_SAFE_TO_INSERT_TATWEEL) == 0)
1121
0
  {
1122
0
    foreach_cluster (buffer, start, end)
1123
0
    {
1124
0
      if (end - start == 1)
1125
0
      {
1126
0
        info[start].mask &= and_mask;
1127
0
  continue;
1128
0
      }
1129
1130
0
      unsigned int mask = 0;
1131
0
      for (unsigned int i = start; i < end; i++)
1132
0
  mask |= info[i].mask;
1133
1134
0
      mask &= and_mask;
1135
1136
0
      for (unsigned int i = start; i < end; i++)
1137
0
  info[i].mask = mask;
1138
0
    }
1139
0
    return;
1140
0
  }
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
25.4M
  foreach_cluster (buffer, start, end)
1151
282M
  {
1152
282M
    unsigned int mask = 0;
1153
577M
    for (unsigned int i = start; i < end; i++)
1154
295M
      mask |= info[i].mask;
1155
1156
282M
    if (mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK)
1157
1.51M
      mask &= ~HB_GLYPH_FLAG_SAFE_TO_INSERT_TATWEEL;
1158
282M
    if (mask & HB_GLYPH_FLAG_SAFE_TO_INSERT_TATWEEL)
1159
156k
      mask |= HB_GLYPH_FLAG_UNSAFE_TO_BREAK | HB_GLYPH_FLAG_UNSAFE_TO_CONCAT;
1160
1161
282M
    mask &= and_mask;
1162
1163
577M
    for (unsigned int i = start; i < end; i++)
1164
295M
      info[i].mask = mask;
1165
282M
  }
1166
25.4M
}
1167
1168
/* Pull it all together! */
1169
1170
static void
1171
hb_ot_shape_internal (hb_ot_shape_context_t *c)
1172
25.4M
{
1173
  /* Save the original direction, we use it later. */
1174
25.4M
  c->target_direction = c->buffer->props.direction;
1175
1176
25.4M
  _hb_buffer_allocate_unicode_vars (c->buffer);
1177
1178
25.4M
  hb_set_unicode_props (c->buffer, c->plan->map.get_global_mask ());
1179
25.4M
  hb_insert_dotted_circle (c->buffer, c->font);
1180
1181
25.4M
  hb_form_clusters (c->buffer);
1182
1183
25.4M
  hb_ensure_native_direction (c->buffer);
1184
1185
25.4M
  if (c->plan->shaper->preprocess_text &&
1186
1.08M
      c->buffer->message(c->font, "start preprocess-text"))
1187
1.08M
  {
1188
1.08M
    c->plan->shaper->preprocess_text (c->plan, c->buffer, c->font);
1189
1.08M
    (void) c->buffer->message(c->font, "end preprocess-text");
1190
1.08M
  }
1191
1192
25.4M
  hb_ot_substitute_pre (c);
1193
25.4M
  hb_ot_position (c);
1194
25.4M
  hb_ot_substitute_post (c);
1195
1196
25.4M
  hb_propagate_flags (c->buffer);
1197
1198
25.4M
  _hb_buffer_deallocate_unicode_vars (c->buffer);
1199
1200
25.4M
  c->buffer->props.direction = c->target_direction;
1201
25.4M
}
1202
1203
1204
hb_bool_t
1205
_hb_ot_shape (hb_shape_plan_t    *shape_plan,
1206
        hb_font_t          *font,
1207
        hb_buffer_t        *buffer,
1208
        const hb_feature_t *features,
1209
        unsigned int        num_features)
1210
25.4M
{
1211
25.4M
  hb_ot_shape_context_t c = {&shape_plan->ot, font, font->face, buffer, features, num_features};
1212
25.4M
  hb_ot_shape_internal (&c);
1213
1214
25.4M
  return true;
1215
25.4M
}
1216
1217
1218
/**
1219
 * hb_ot_shape_plan_collect_lookups:
1220
 * @shape_plan: #hb_shape_plan_t to query
1221
 * @table_tag: GSUB or GPOS
1222
 * @lookup_indexes: (out): The #hb_set_t set of lookups returned
1223
 *
1224
 * Computes the complete set of GSUB or GPOS lookups that are applicable
1225
 * under a given @shape_plan.
1226
 *
1227
 * Since: 0.9.7
1228
 **/
1229
void
1230
hb_ot_shape_plan_collect_lookups (hb_shape_plan_t *shape_plan,
1231
          hb_tag_t         table_tag,
1232
          hb_set_t        *lookup_indexes /* OUT */)
1233
0
{
1234
0
  shape_plan->ot.collect_lookups (table_tag, lookup_indexes);
1235
0
}
1236
1237
1238
/**
1239
 * hb_ot_shape_plan_get_feature_tags:
1240
 * @shape_plan: A shaping plan
1241
 * @start_offset: The index of first feature to retrieve
1242
 * @tag_count: (inout): Input = the maximum number of features to return;
1243
 *                      Output = the actual number of features returned (may be zero)
1244
 * @tags: (out) (array length=tag_count): The array of enabled feature
1245
 *
1246
 * Fetches the list of OpenType feature tags enabled for a shaping plan, if possible.
1247
 *
1248
 * Return value: Total number of feature tagss.
1249
 *
1250
 * Since: 10.3.0
1251
 */
1252
unsigned int
1253
hb_ot_shape_plan_get_feature_tags (hb_shape_plan_t *shape_plan,
1254
           unsigned int     start_offset,
1255
           unsigned int    *tag_count, /* IN/OUT */
1256
           hb_tag_t        *tags /* OUT */)
1257
0
{
1258
0
#ifndef HB_NO_OT_SHAPE
1259
0
  return shape_plan->ot.map.get_feature_tags (start_offset, tag_count, tags);
1260
#else
1261
  if (tag_count)
1262
  *tag_count = 0;
1263
  return 0;
1264
#endif
1265
0
}
1266
1267
1268
/* TODO Move this to hb-ot-shape-normalize, make it do decompose, and make it public. */
1269
static void
1270
add_char (hb_font_t          *font,
1271
    hb_unicode_funcs_t *unicode,
1272
    hb_bool_t           mirror,
1273
    hb_codepoint_t      u,
1274
    hb_set_t           *glyphs)
1275
0
{
1276
0
  hb_codepoint_t glyph;
1277
0
  if (font->get_nominal_glyph (u, &glyph))
1278
0
    glyphs->add (glyph);
1279
0
  if (mirror)
1280
0
  {
1281
0
    hb_codepoint_t m = unicode->mirroring (u);
1282
0
    if (m != u && font->get_nominal_glyph (m, &glyph))
1283
0
      glyphs->add (glyph);
1284
0
  }
1285
0
}
1286
1287
1288
/**
1289
 * hb_ot_shape_glyphs_closure:
1290
 * @font: #hb_font_t to work upon
1291
 * @buffer: The input buffer to compute from
1292
 * @features: (array length=num_features): The features enabled on the buffer
1293
 * @num_features: The number of features enabled on the buffer
1294
 * @glyphs: (out): The #hb_set_t set of glyphs comprising the transitive closure of the query
1295
 *
1296
 * Computes the transitive closure of glyphs needed for a specified
1297
 * input buffer under the given font and feature list. The closure is
1298
 * computed as a set, not as a list.
1299
 *
1300
 * Since: 0.9.2
1301
 **/
1302
void
1303
hb_ot_shape_glyphs_closure (hb_font_t          *font,
1304
          hb_buffer_t        *buffer,
1305
          const hb_feature_t *features,
1306
          unsigned int        num_features,
1307
          hb_set_t           *glyphs)
1308
0
{
1309
0
  const char *shapers[] = {"ot", nullptr};
1310
0
  hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props,
1311
0
                   features, num_features, shapers);
1312
1313
0
  bool mirror = hb_script_get_horizontal_direction (buffer->props.script) == HB_DIRECTION_RTL;
1314
1315
0
  unsigned int count = buffer->len;
1316
0
  hb_glyph_info_t *info = buffer->info;
1317
0
  for (unsigned int i = 0; i < count; i++)
1318
0
    add_char (font, buffer->unicode, mirror, info[i].codepoint, glyphs);
1319
1320
0
  hb_set_t *lookups = hb_set_create ();
1321
0
  hb_ot_shape_plan_collect_lookups (shape_plan, HB_OT_TAG_GSUB, lookups);
1322
0
  hb_ot_layout_lookups_substitute_closure (font->face, lookups, glyphs);
1323
1324
0
  hb_set_destroy (lookups);
1325
1326
0
  hb_shape_plan_destroy (shape_plan);
1327
0
}
1328
1329
1330
/**
1331
 * hb_ot_shape_get_buffer_format_serial:
1332
 *
1333
 * Returns the serial number of the current internal buffer format.
1334
 * See #HB_OT_SHAPE_BUFFER_FORMAT_SERIAL for more information.
1335
 *
1336
 * Return value: The current buffer-format serial number.
1337
 *
1338
 * Since: 13.2.0
1339
 **/
1340
unsigned int
1341
hb_ot_shape_get_buffer_format_serial (void)
1342
0
{
1343
0
  return HB_OT_SHAPE_BUFFER_FORMAT_SERIAL;
1344
0
}
1345
1346
#endif