Coverage Report

Created: 2026-09-28 10:59

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
12.5M
{ 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
5.14k
{
62
  /* https://github.com/harfbuzz/harfbuzz/issues/2124 */
63
5.14k
  return hb_aat_layout_has_substitution (face) &&
64
0
   (HB_DIRECTION_IS_HORIZONTAL (props.direction) || !hb_ot_layout_has_substitution (face));
65
5.14k
}
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
5.14k
            face (face),
86
5.14k
            props (props),
87
5.14k
            map (face, props)
88
#ifndef HB_NO_AAT_SHAPE
89
5.14k
            , aat_map (face, props)
90
5.14k
            , apply_morx (_hb_apply_morx (face, props))
91
#endif
92
5.14k
{
93
5.14k
  shaper = hb_ot_shaper_categorize (props.script, props.direction, map.chosen_script[0]);
94
95
5.14k
  script_zero_marks = shaper->zero_width_marks != HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE;
96
5.14k
  script_fallback_position = shaper->fallback_position;
97
98
5.14k
#ifndef HB_NO_AAT_SHAPE
99
  /* https://github.com/harfbuzz/harfbuzz/issues/1528 */
100
5.14k
  if (apply_morx && shaper != &_hb_ot_shaper_default)
101
0
    shaper = &_hb_ot_shaper_dumber;
102
5.14k
#endif
103
5.14k
}
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
5.14k
{
109
5.14k
  plan.props = props;
110
5.14k
  plan.shaper = shaper;
111
5.14k
  map.compile (plan.map, key);
112
5.14k
#ifndef HB_NO_AAT_SHAPE
113
5.14k
  if (apply_morx)
114
0
    aat_map.compile (plan.aat_map);
115
5.14k
#endif
116
117
5.14k
#ifndef HB_NO_OT_SHAPE_FRACTIONS
118
5.14k
  plan.frac_mask = plan.map.get_1_mask (HB_TAG ('f','r','a','c'));
119
5.14k
  plan.numr_mask = plan.map.get_1_mask (HB_TAG ('n','u','m','r'));
120
5.14k
  plan.dnom_mask = plan.map.get_1_mask (HB_TAG ('d','n','o','m'));
121
5.14k
  plan.has_frac = plan.frac_mask || (plan.numr_mask && plan.dnom_mask);
122
5.14k
#endif
123
124
5.14k
  plan.rtlm_mask = plan.map.get_1_mask (HB_TAG ('r','t','l','m'));
125
5.14k
  plan.has_vert = !!plan.map.get_1_mask (HB_TAG ('v','e','r','t'));
126
127
5.14k
  hb_tag_t kern_tag = HB_DIRECTION_IS_HORIZONTAL (props.direction) ?
128
4.94k
          HB_TAG ('k','e','r','n') : HB_TAG ('v','k','r','n');
129
5.14k
#ifndef HB_NO_OT_KERN
130
5.14k
  plan.kern_mask = plan.map.get_mask (kern_tag);
131
5.14k
  plan.requested_kerning = !!plan.kern_mask;
132
5.14k
#endif
133
134
5.14k
  bool has_gpos_kern = plan.map.get_feature_index (1, kern_tag) != HB_OT_LAYOUT_NO_FEATURE_INDEX;
135
5.14k
  bool disable_gpos = plan.shaper->gpos_tag &&
136
93
          plan.shaper->gpos_tag != plan.map.chosen_script[1];
137
138
  /*
139
   * Decide who provides glyph classes. GDEF or Unicode.
140
   */
141
142
5.14k
  if (!hb_ot_layout_has_glyph_classes (face))
143
0
    plan.fallback_glyph_classes = true;
144
145
  /*
146
   * Decide who does substitutions. GSUB, morx, or fallback.
147
   */
148
149
5.14k
#ifndef HB_NO_AAT_SHAPE
150
5.14k
  plan.apply_morx = apply_morx;
151
5.14k
#endif
152
153
  /*
154
   * Decide who does positioning. GPOS, kerx, kern, or fallback.
155
   */
156
157
5.14k
#ifndef HB_NO_AAT_SHAPE
158
5.14k
  bool has_kerx = hb_aat_layout_has_positioning (face);
159
5.14k
  bool has_gsub = !apply_morx && hb_ot_layout_has_substitution (face);
160
5.14k
#endif
161
5.14k
  bool has_gpos = !disable_gpos && hb_ot_layout_has_positioning (face);
162
5.14k
  if (false)
163
0
    {}
164
5.14k
#ifndef HB_NO_AAT_SHAPE
165
  /* Prefer GPOS over kerx if GSUB is present;
166
   * https://github.com/harfbuzz/harfbuzz/issues/3008 */
167
5.14k
  else if (has_kerx && !(has_gsub && has_gpos))
168
0
    plan.apply_kerx = true;
169
5.14k
#endif
170
5.14k
  else if (has_gpos)
171
5.14k
    plan.apply_gpos = true;
172
173
5.14k
  if (!plan.apply_kerx && (!has_gpos_kern || !plan.apply_gpos))
174
4.34k
  {
175
4.34k
    if (false) {}
176
4.34k
#ifndef HB_NO_AAT_SHAPE
177
4.34k
    else if (has_kerx)
178
0
      plan.apply_kerx = true;
179
4.34k
#endif
180
4.34k
#ifndef HB_NO_OT_KERN
181
4.34k
    else if (hb_ot_layout_has_kerning (face))
182
4.34k
      plan.apply_kern = script_fallback_position; // Not all shapers apply legacy `kern`
183
0
#endif
184
0
    else {}
185
4.34k
  }
186
187
5.14k
  plan.apply_fallback_kern = script_fallback_position && !(plan.apply_gpos || plan.apply_kerx || plan.apply_kern);
188
189
5.14k
  plan.zero_marks = script_zero_marks &&
190
4.95k
        !plan.apply_kerx &&
191
4.95k
        (!plan.apply_kern
192
4.08k
#ifndef HB_NO_OT_KERN
193
4.08k
         || !hb_ot_layout_has_machine_kerning (face)
194
4.95k
#endif
195
4.95k
        );
196
5.14k
  plan.has_gpos_mark = !!plan.map.get_1_mask (HB_TAG ('m','a','r','k'));
197
198
5.14k
  plan.adjust_mark_positioning_when_zeroing = !plan.apply_gpos &&
199
0
                !plan.apply_kerx &&
200
0
                (!plan.apply_kern
201
0
#ifndef HB_NO_OT_KERN
202
0
                 || !hb_ot_layout_has_cross_kerning (face)
203
0
#endif
204
0
                );
205
206
5.14k
  plan.fallback_mark_positioning = plan.adjust_mark_positioning_when_zeroing &&
207
0
           script_fallback_position;
208
209
5.14k
#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
5.14k
  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
5.14k
#ifndef HB_NO_STYLE
218
5.14k
  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
5.14k
#endif
224
5.14k
}
225
226
bool
227
hb_ot_shape_plan_t::init0 (hb_face_t                     *face,
228
         const hb_shape_plan_key_t     *key)
229
5.14k
{
230
5.14k
  map.init ();
231
232
5.14k
  hb_ot_shape_planner_t planner (face,
233
5.14k
         key->props);
234
235
5.14k
  hb_ot_shape_collect_features (&planner,
236
5.14k
        key->user_features,
237
5.14k
        key->num_user_features);
238
239
5.14k
  planner.compile (*this, key->ot);
240
241
5.14k
  if (shaper->data_create)
242
315
  {
243
315
    data = shaper->data_create (this);
244
315
    if (unlikely (!data))
245
0
    {
246
0
      map.fini ();
247
0
      return false;
248
0
    }
249
315
  }
250
251
5.14k
  return true;
252
5.14k
}
253
254
void
255
hb_ot_shape_plan_t::fini ()
256
0
{
257
0
  if (shaper->data_destroy)
258
0
    shaper->data_destroy (const_cast<void *> (data));
259
260
0
  map.fini ();
261
0
}
262
263
void
264
hb_ot_shape_plan_t::substitute (hb_font_t   *font,
265
        hb_buffer_t *buffer) const
266
13.9M
{
267
13.9M
  map.substitute (this, font, buffer);
268
13.9M
}
269
270
void
271
hb_ot_shape_plan_t::position (hb_font_t   *font,
272
            hb_buffer_t *buffer) const
273
13.9M
{
274
13.9M
  if (this->apply_gpos)
275
13.9M
    map.position (this, font, buffer);
276
0
#ifndef HB_NO_AAT_SHAPE
277
0
  else if (this->apply_kerx)
278
0
    hb_aat_layout_position (this, font, buffer);
279
13.9M
#endif
280
281
13.9M
#ifndef HB_NO_OT_KERN
282
13.9M
  if (this->apply_kern)
283
9.90M
    hb_ot_layout_kern (this, font, buffer);
284
4.07M
#endif
285
4.07M
  else if (this->apply_fallback_kern)
286
0
    _hb_ot_shape_fallback_kern (this, font, buffer);
287
288
13.9M
#ifndef HB_NO_AAT_SHAPE
289
13.9M
  if (this->apply_trak)
290
0
    hb_aat_layout_track (this, font, buffer);
291
13.9M
#endif
292
13.9M
}
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
5.14k
{
325
5.14k
  hb_ot_map_builder_t *map = &planner->map;
326
327
5.14k
  map->is_simple = true;
328
329
5.14k
  map->enable_feature (HB_TAG('r','v','r','n'));
330
5.14k
  map->add_gsub_pause (nullptr);
331
332
5.14k
  switch (planner->props.direction)
333
5.14k
  {
334
4.24k
    case HB_DIRECTION_LTR:
335
4.24k
      map->enable_feature (HB_TAG ('l','t','r','a'));
336
4.24k
      map->enable_feature (HB_TAG ('l','t','r','m'));
337
4.24k
      break;
338
702
    case HB_DIRECTION_RTL:
339
702
      map->enable_feature (HB_TAG ('r','t','l','a'));
340
702
      map->add_feature (HB_TAG ('r','t','l','m'));
341
702
      break;
342
198
    case HB_DIRECTION_TTB:
343
198
    case HB_DIRECTION_BTT:
344
198
    case HB_DIRECTION_INVALID:
345
198
    default:
346
198
      break;
347
5.14k
  }
348
349
5.14k
#ifndef HB_NO_OT_SHAPE_FRACTIONS
350
  /* Automatic fractions. */
351
5.14k
  map->add_feature (HB_TAG ('f','r','a','c'));
352
5.14k
  map->add_feature (HB_TAG ('n','u','m','r'));
353
5.14k
  map->add_feature (HB_TAG ('d','n','o','m'));
354
5.14k
#endif
355
356
  /* Random! */
357
5.14k
  map->enable_feature (HB_TAG ('r','a','n','d'), F_RANDOM, HB_OT_MAP_MAX_VALUE);
358
359
5.14k
  map->enable_feature (HB_TAG ('H','a','r','f')); /* Considered required. */
360
5.14k
  map->enable_feature (HB_TAG ('H','A','R','F')); /* Considered discretionary. */
361
362
5.14k
  if (planner->shaper->collect_features)
363
315
  {
364
315
    map->is_simple = false;
365
315
    planner->shaper->collect_features (planner);
366
315
  }
367
368
5.14k
  map->enable_feature (HB_TAG ('B','u','z','z')); /* Considered required. */
369
5.14k
  map->enable_feature (HB_TAG ('B','U','Z','Z')); /* Considered discretionary. */
370
371
41.1k
  for (unsigned int i = 0; i < ARRAY_LENGTH (common_features); i++)
372
35.9k
    map->add_feature (common_features[i]);
373
374
5.14k
  if (HB_DIRECTION_IS_HORIZONTAL (planner->props.direction))
375
39.5k
    for (unsigned int i = 0; i < ARRAY_LENGTH (horizontal_features); i++)
376
34.6k
      map->add_feature (horizontal_features[i]);
377
198
  else
378
198
  {
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
198
    map->enable_feature (HB_TAG ('v','e','r','t'), F_GLOBAL_SEARCH);
388
198
  }
389
390
5.14k
  if (num_user_features)
391
4.55k
    map->is_simple = false;
392
343k
  for (unsigned int i = 0; i < num_user_features; i++)
393
338k
  {
394
338k
    const hb_feature_t *feature = &user_features[i];
395
338k
    map->add_feature (feature->tag,
396
338k
          (feature->start == HB_FEATURE_GLOBAL_START &&
397
338k
           feature->end == HB_FEATURE_GLOBAL_END) ?  F_GLOBAL : F_NONE,
398
338k
          feature->value);
399
338k
  }
400
401
5.14k
  if (planner->shaper->override_features)
402
184
    planner->shaper->override_features (planner);
403
5.14k
}
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
48
{
415
48
  return (hb_ot_face_data_t *) HB_SHAPER_DATA_SUCCEEDED;
416
48
}
417
418
void
419
_hb_ot_shaper_face_data_destroy (hb_ot_face_data_t *data)
420
0
{
421
0
}
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
150k
{
435
150k
  const OT::ItemVariationStore &var_store = font->face->table.GDEF->table->get_var_store ();
436
150k
  return (hb_ot_font_data_t *) var_store.create_cache ();
437
150k
}
438
439
void
440
_hb_ot_shaper_font_data_destroy (hb_ot_font_data_t *data)
441
149k
{
442
149k
  OT::ItemVariationStore::destroy_cache ((OT::hb_scalar_cache_t *) data);
443
149k
}
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
13.9M
{
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
13.9M
  unsigned int count = buffer->len;
483
13.9M
  hb_glyph_info_t *info = buffer->info;
484
237M
  for (unsigned int i = 0; i < count; i++)
485
223M
  {
486
223M
    info[i].mask = global_mask;
487
223M
    _hb_glyph_info_set_unicode_props (&info[i], buffer);
488
489
223M
    if (info[i].codepoint < 0x80)
490
177M
      continue;
491
492
46.1M
    unsigned gen_cat = _hb_glyph_info_get_general_category (&info[i]);
493
46.1M
    if (FLAG_UNSAFE (gen_cat) &
494
46.1M
  (FLAG (HB_UNICODE_GENERAL_CATEGORY_LOWERCASE_LETTER) |
495
46.1M
   FLAG (HB_UNICODE_GENERAL_CATEGORY_UPPERCASE_LETTER) |
496
46.1M
   FLAG (HB_UNICODE_GENERAL_CATEGORY_TITLECASE_LETTER) |
497
46.1M
   FLAG (HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER) |
498
46.1M
   FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACE_SEPARATOR)))
499
31.2M
      continue;
500
501
    /* Marks are already set as continuation by the above line.
502
     * Handle Emoji_Modifier and ZWJ-continuation. */
503
14.9M
    if (unlikely (gen_cat == HB_UNICODE_GENERAL_CATEGORY_MODIFIER_SYMBOL &&
504
14.9M
      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
14.9M
    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
14.9M
#ifndef HB_NO_EMOJI_SEQUENCES
517
14.9M
    else if (unlikely (_hb_glyph_info_is_zwj (&info[i])))
518
1.71k
    {
519
1.71k
      _hb_glyph_info_set_continuation (&info[i], buffer);
520
1.71k
      if (i + 1 < count &&
521
1.01k
    _hb_unicode_is_emoji_Extended_Pictographic (info[i + 1].codepoint))
522
0
      {
523
0
  i++;
524
0
  info[i].mask = global_mask;
525
0
  _hb_glyph_info_set_unicode_props (&info[i], buffer);
526
0
  _hb_glyph_info_set_continuation (&info[i], buffer);
527
0
      }
528
1.71k
    }
529
14.9M
#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
14.9M
    else if (unlikely (hb_in_ranges<hb_codepoint_t> (info[i].codepoint, 0xFF9Eu, 0xFF9Fu, 0xE0020u, 0xE007Fu)))
545
42.0k
      _hb_glyph_info_set_continuation (&info[i], buffer);
546
14.9M
    else if (unlikely (info[i].codepoint == 0x2044u /* FRACTION SLASH */))
547
2.39k
      buffer->scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_FRACTION_SLASH;
548
14.9M
  }
549
13.9M
}
550
551
static void
552
hb_insert_dotted_circle (hb_buffer_t *buffer, hb_font_t *font)
553
13.9M
{
554
13.9M
  if (unlikely (buffer->flags & HB_BUFFER_FLAG_DO_NOT_INSERT_DOTTED_CIRCLE))
555
0
    return;
556
557
13.9M
  if (!(buffer->flags & HB_BUFFER_FLAG_BOT) ||
558
2.06M
      buffer->context_len[0] ||
559
2.06M
      !_hb_glyph_info_is_unicode_mark (&buffer->info[0]))
560
13.9M
    return;
561
562
1.84k
  if (!font->has_glyph (0x25CCu))
563
0
    return;
564
565
1.84k
  hb_glyph_info_t dottedcircle = {0};
566
1.84k
  dottedcircle.codepoint = 0x25CCu;
567
1.84k
  _hb_glyph_info_set_unicode_props (&dottedcircle, buffer);
568
569
1.84k
  buffer->clear_output ();
570
571
1.84k
  buffer->idx = 0;
572
1.84k
  hb_glyph_info_t info = dottedcircle;
573
1.84k
  info.cluster = buffer->cur().cluster;
574
1.84k
  info.mask = buffer->cur().mask;
575
1.84k
  (void) buffer->output_info (info);
576
577
1.84k
  buffer->sync ();
578
1.84k
}
579
580
static void
581
hb_form_clusters (hb_buffer_t *buffer)
582
13.9M
{
583
13.9M
  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_CONTINUATIONS))
584
13.3M
    return;
585
586
601k
  foreach_grapheme (buffer, start, end)
587
9.70M
    buffer->merge_grapheme_clusters (start, end);
588
601k
}
589
590
static void
591
hb_ensure_native_direction (hb_buffer_t *buffer)
592
13.9M
{
593
13.9M
  hb_direction_t direction = buffer->props.direction;
594
13.9M
  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
13.9M
  if (unlikely (horiz_dir == HB_DIRECTION_RTL && direction == HB_DIRECTION_LTR))
614
182k
  {
615
182k
    bool found_number = false, found_letter = false, found_ri = false;
616
182k
    const auto* info = buffer->info;
617
182k
    const auto count = buffer->len;
618
763k
    for (unsigned i = 0; i < count; i++)
619
587k
    {
620
587k
      auto gc = _hb_glyph_info_get_general_category (&info[i]);
621
587k
      if (HB_UNICODE_GENERAL_CATEGORY_IS_LETTER (gc))
622
6.44k
      {
623
6.44k
  found_letter = true;
624
6.44k
  break;
625
6.44k
      }
626
581k
      else if (gc == HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
627
40.1k
  found_number = true;
628
540k
      else if (unlikely (_hb_codepoint_is_regional_indicator (info[i].codepoint)))
629
0
  found_ri = true;
630
587k
    }
631
182k
    if ((found_number || found_ri) && !found_letter)
632
19.5k
      horiz_dir = HB_DIRECTION_LTR;
633
182k
  }
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
13.9M
  if ((HB_DIRECTION_IS_HORIZONTAL (direction) &&
640
13.8M
       direction != horiz_dir && HB_DIRECTION_IS_VALID (horiz_dir)) ||
641
13.3M
      (HB_DIRECTION_IS_VERTICAL   (direction) &&
642
163k
       direction != HB_DIRECTION_TTB))
643
655k
  {
644
655k
    _hb_ot_layout_reverse_graphemes (buffer);
645
655k
    buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
646
655k
  }
647
13.9M
}
648
649
650
/*
651
 * Substitute
652
 */
653
654
static inline void
655
hb_ot_rotate_chars (const hb_ot_shape_context_t *c)
656
13.9M
{
657
13.9M
  hb_buffer_t *buffer = c->buffer;
658
13.9M
  unsigned int count = buffer->len;
659
13.9M
  hb_glyph_info_t *info = buffer->info;
660
661
13.9M
  if (HB_DIRECTION_IS_BACKWARD (c->target_direction))
662
800k
  {
663
800k
    hb_unicode_funcs_t *unicode = buffer->unicode;
664
800k
    hb_mask_t rtlm_mask = c->plan->rtlm_mask;
665
666
44.5M
    for (unsigned int i = 0; i < count; i++) {
667
43.7M
      hb_codepoint_t codepoint = unicode->mirroring (info[i].codepoint);
668
43.7M
      if (unlikely (codepoint != info[i].codepoint && c->font->has_glyph (codepoint)))
669
188k
  info[i].codepoint = codepoint;
670
43.5M
      else
671
43.5M
  info[i].mask |= rtlm_mask;
672
43.7M
    }
673
800k
  }
674
675
13.9M
#ifndef HB_NO_VERTICAL
676
13.9M
  if (HB_DIRECTION_IS_VERTICAL (c->target_direction) && !c->plan->has_vert)
677
163k
  {
678
810k
    for (unsigned int i = 0; i < count; i++) {
679
646k
      hb_codepoint_t codepoint = hb_unicode_funcs_t::vertical_char_for (info[i].codepoint);
680
646k
      if (unlikely (codepoint != info[i].codepoint && c->font->has_glyph (codepoint)))
681
0
  info[i].codepoint = codepoint;
682
646k
    }
683
163k
  }
684
13.9M
#endif
685
13.9M
}
686
687
static inline void
688
hb_ot_shape_setup_masks_fraction (const hb_ot_shape_context_t *c)
689
13.9M
{
690
#ifdef HB_NO_OT_SHAPE_FRACTIONS
691
  return;
692
#endif
693
694
13.9M
  if (!(c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_FRACTION_SLASH) ||
695
507
      !c->plan->has_frac)
696
13.9M
    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
13.9M
{
752
13.9M
  hb_ot_map_t *map = &c->plan->map;
753
13.9M
  hb_buffer_t *buffer = c->buffer;
754
755
13.9M
  hb_ot_shape_setup_masks_fraction (c);
756
757
13.9M
  if (c->plan->shaper->setup_masks)
758
978k
    c->plan->shaper->setup_masks (c->plan, buffer, c->font);
759
760
92.2M
  for (unsigned int i = 0; i < c->num_user_features; i++)
761
78.2M
  {
762
78.2M
    const hb_feature_t *feature = &c->user_features[i];
763
78.2M
    if (!(feature->start == HB_FEATURE_GLOBAL_START && feature->end == HB_FEATURE_GLOBAL_END)) {
764
18.1k
      unsigned int shift;
765
18.1k
      hb_mask_t mask = map->get_mask (feature->tag, &shift);
766
18.1k
      buffer->set_masks (feature->value << shift, mask, feature->start, feature->end);
767
18.1k
    }
768
78.2M
  }
769
13.9M
}
770
771
static void
772
hb_ot_zero_width_default_ignorables (const hb_buffer_t *buffer)
773
13.9M
{
774
13.9M
  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
775
129k
      (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES) ||
776
129k
      (buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES))
777
13.8M
    return;
778
779
129k
  unsigned int count = buffer->len;
780
129k
  hb_glyph_info_t *info = buffer->info;
781
129k
  hb_glyph_position_t *pos = buffer->pos;
782
129k
  unsigned int i = 0;
783
9.17M
  for (i = 0; i < count; i++)
784
9.04M
    if (unlikely (_hb_glyph_info_is_default_ignorable (&info[i])))
785
188k
    {
786
188k
      pos[i].x_advance = pos[i].y_advance = 0;
787
188k
      if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
788
175k
  pos[i].x_offset = 0;
789
12.9k
      else
790
12.9k
        pos[i].y_offset = 0;
791
188k
    }
792
129k
}
793
794
static void
795
hb_ot_deal_with_variation_selectors (hb_buffer_t *buffer)
796
13.9M
{
797
13.9M
  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_VARIATION_SELECTOR_FALLBACK) ||
798
21.9k
  buffer->not_found_variation_selector == HB_CODEPOINT_INVALID)
799
13.9M
    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
13.9M
{
820
13.9M
  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
821
129k
      (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES))
822
13.8M
    return;
823
824
129k
  unsigned int count = buffer->len;
825
129k
  hb_glyph_info_t *info = buffer->info;
826
827
129k
  hb_codepoint_t invisible = buffer->invisible;
828
129k
  if (!(buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES) &&
829
129k
      (invisible || font->get_nominal_glyph (' ', &invisible)))
830
129k
  {
831
    /* Replace default-ignorables with a zero-advance invisible glyph. */
832
9.17M
    for (unsigned int i = 0; i < count; i++)
833
9.04M
    {
834
9.04M
      if (_hb_glyph_info_is_default_ignorable (&info[i]))
835
188k
  info[i].codepoint = invisible;
836
9.04M
    }
837
129k
  }
838
0
  else
839
0
    buffer->delete_glyphs_inplace (_hb_glyph_info_is_default_ignorable);
840
129k
}
841
842
843
static inline void
844
hb_ot_map_glyphs_fast (hb_buffer_t  *buffer)
845
13.9M
{
846
  /* Normalization process sets up normalizer_glyph_index(), we just copy it. */
847
13.9M
  unsigned int count = buffer->len;
848
13.9M
  hb_glyph_info_t *info = buffer->info;
849
237M
  for (unsigned int i = 0; i < count; i++)
850
223M
    info[i].codepoint = info[i].normalizer_glyph_index();
851
852
13.9M
  buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
853
13.9M
}
854
855
static inline void
856
hb_synthesize_glyph_classes (hb_buffer_t *buffer)
857
0
{
858
0
  unsigned int count = buffer->len;
859
0
  hb_glyph_info_t *info = buffer->info;
860
0
  for (unsigned int i = 0; i < count; i++)
861
0
  {
862
0
    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
0
    klass = (_hb_glyph_info_get_general_category (&info[i]) !=
873
0
       HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK ||
874
0
       _hb_glyph_info_is_default_ignorable (&info[i])) ?
875
0
      HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH :
876
0
      HB_OT_LAYOUT_GLYPH_PROPS_MARK;
877
0
    _hb_glyph_info_set_glyph_props (&info[i], klass);
878
0
  }
879
0
}
880
881
static inline void
882
hb_ot_substitute_default (const hb_ot_shape_context_t *c)
883
13.9M
{
884
13.9M
  hb_buffer_t *buffer = c->buffer;
885
886
13.9M
  hb_ot_rotate_chars (c);
887
888
13.9M
  HB_BUFFER_ALLOCATE_VAR (buffer, normalizer_glyph_index);
889
890
13.9M
  _hb_ot_shape_normalize (c->plan, buffer, c->font);
891
892
13.9M
  hb_ot_shape_setup_masks (c);
893
894
  /* This is unfortunate to go here, but necessary... */
895
13.9M
  if (c->plan->fallback_mark_positioning)
896
0
    _hb_ot_shape_fallback_mark_position_recategorize_marks (c->plan, c->font, buffer);
897
898
13.9M
  hb_ot_map_glyphs_fast (buffer);
899
900
13.9M
  HB_BUFFER_DEALLOCATE_VAR (buffer, normalizer_glyph_index);
901
13.9M
}
902
903
static inline void
904
hb_ot_substitute_plan (const hb_ot_shape_context_t *c)
905
13.9M
{
906
13.9M
  hb_buffer_t *buffer = c->buffer;
907
908
13.9M
#ifndef HB_NO_AAT_SHAPE
909
13.9M
  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
13.9M
  else
924
13.9M
#endif
925
13.9M
  {
926
13.9M
    hb_ot_layout_substitute_start_with_digest (c->font, buffer);
927
928
13.9M
    if (c->plan->fallback_glyph_classes)
929
0
      hb_synthesize_glyph_classes (buffer);
930
931
13.9M
    c->plan->substitute (c->font, buffer);
932
13.9M
  }
933
13.9M
}
934
935
static inline void
936
hb_ot_substitute_pre (const hb_ot_shape_context_t *c)
937
13.9M
{
938
13.9M
  hb_ot_substitute_default (c);
939
940
13.9M
  _hb_buffer_allocate_gsubgpos_vars (c->buffer);
941
942
13.9M
  hb_ot_substitute_plan (c);
943
944
13.9M
#ifndef HB_NO_AAT_SHAPE
945
13.9M
  if (c->plan->apply_morx && c->plan->apply_gpos)
946
0
    hb_aat_layout_remove_deleted_glyphs (c->buffer);
947
13.9M
#endif
948
13.9M
}
949
950
static inline void
951
hb_ot_substitute_post (const hb_ot_shape_context_t *c)
952
13.9M
{
953
13.9M
#ifndef HB_NO_AAT_SHAPE
954
13.9M
  if (c->plan->apply_morx && !c->plan->apply_gpos)
955
0
    hb_aat_layout_remove_deleted_glyphs (c->buffer);
956
13.9M
#endif
957
958
13.9M
  hb_ot_deal_with_variation_selectors (c->buffer);
959
13.9M
  hb_ot_hide_default_ignorables (c->buffer, c->font);
960
961
13.9M
  if (c->plan->shaper->postprocess_glyphs &&
962
234k
    c->buffer->message(c->font, "start postprocess-glyphs")) {
963
234k
    c->plan->shaper->postprocess_glyphs (c->plan, c->buffer, c->font);
964
234k
    (void) c->buffer->message(c->font, "end postprocess-glyphs");
965
234k
  }
966
13.9M
}
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
53.3k
{
983
53.3k
  pos->x_advance = 0;
984
53.3k
  pos->y_advance = 0;
985
53.3k
}
986
987
static inline void
988
zero_mark_widths_by_gdef (hb_buffer_t *buffer, bool adjust_offsets)
989
13.2M
{
990
13.2M
  unsigned int count = buffer->len;
991
13.2M
  hb_glyph_info_t *info = buffer->info;
992
235M
  for (unsigned int i = 0; i < count; i++)
993
222M
    if (_hb_glyph_info_is_mark (&info[i]))
994
53.3k
    {
995
53.3k
      if (adjust_offsets)
996
0
  adjust_mark_offsets (&buffer->pos[i]);
997
53.3k
      zero_mark_width (&buffer->pos[i]);
998
53.3k
    }
999
13.2M
}
1000
1001
static inline void
1002
hb_ot_position_default (const hb_ot_shape_context_t *c)
1003
13.9M
{
1004
13.9M
  hb_direction_t direction = c->buffer->props.direction;
1005
13.9M
  unsigned int count = c->buffer->len;
1006
13.9M
  hb_glyph_info_t *info = c->buffer->info;
1007
13.9M
  hb_glyph_position_t *pos = c->buffer->pos;
1008
1009
13.9M
  if (HB_DIRECTION_IS_HORIZONTAL (direction))
1010
13.8M
  {
1011
13.8M
    c->font->get_glyph_h_advances (count, &info[0].codepoint, sizeof(info[0]),
1012
13.8M
           &pos[0].x_advance, sizeof(pos[0]));
1013
    // h_origin defaults to zero; only apply it if the font has it.
1014
13.8M
    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
13.8M
  }
1017
163k
  else
1018
163k
  {
1019
163k
    c->font->get_glyph_v_advances (count, &info[0].codepoint, sizeof(info[0]),
1020
163k
           &pos[0].y_advance, sizeof(pos[0]));
1021
    // v_origin defaults to non-zero; apply even if only fallback is there.
1022
163k
    c->font->subtract_glyph_v_origins (c->buffer);
1023
163k
  }
1024
13.9M
  if (c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK)
1025
6.53k
    _hb_ot_shape_fallback_spaces (c->plan, c->font, c->buffer);
1026
13.9M
}
1027
1028
static inline void
1029
hb_ot_position_plan (const hb_ot_shape_context_t *c)
1030
13.9M
{
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
13.9M
  bool adjust_offsets_when_zeroing = c->plan->adjust_mark_positioning_when_zeroing &&
1041
0
             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
13.9M
  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
13.9M
  hb_ot_layout_position_start (c->font, c->buffer);
1050
1051
13.9M
  if (c->plan->zero_marks)
1052
13.2M
    switch (c->plan->shaper->zero_width_marks)
1053
13.2M
    {
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
13.2M
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
1061
13.2M
  break;
1062
13.2M
    }
1063
1064
13.9M
  c->plan->position (c->font, c->buffer);
1065
1066
13.9M
  if (c->plan->zero_marks)
1067
13.2M
    switch (c->plan->shaper->zero_width_marks)
1068
13.2M
    {
1069
13.2M
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
1070
13.2M
  zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
1071
13.2M
  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
13.2M
    }
1078
1079
  /* Finish off.  Has to follow a certain order. */
1080
13.9M
  hb_ot_layout_position_finish_advances (c->font, c->buffer);
1081
13.9M
  hb_ot_zero_width_default_ignorables (c->buffer);
1082
13.9M
  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
13.9M
  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
13.9M
  if (c->plan->fallback_mark_positioning)
1089
0
    _hb_ot_shape_fallback_mark_position (c->plan, c->font, c->buffer,
1090
0
           adjust_offsets_when_zeroing);
1091
13.9M
}
1092
1093
static inline void
1094
hb_ot_position (const hb_ot_shape_context_t *c)
1095
13.9M
{
1096
13.9M
  c->buffer->clear_positions ();
1097
1098
13.9M
  hb_ot_position_default (c);
1099
1100
13.9M
  hb_ot_position_plan (c);
1101
1102
13.9M
  if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
1103
469k
    hb_buffer_reverse (c->buffer);
1104
1105
13.9M
  _hb_buffer_deallocate_gsubgpos_vars (c->buffer);
1106
13.9M
}
1107
1108
static inline void
1109
hb_propagate_flags (hb_buffer_t *buffer)
1110
13.9M
{
1111
  /* Propagate cluster-level glyph flags to be the same on all cluster glyphs.
1112
   * Simplifies using them. */
1113
1114
13.9M
  hb_mask_t and_mask = HB_GLYPH_FLAG_DEFINED;
1115
13.9M
  if ((buffer->flags & HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT) == 0)
1116
79.9k
    and_mask &= ~HB_GLYPH_FLAG_UNSAFE_TO_CONCAT;
1117
1118
13.9M
  hb_glyph_info_t *info = buffer->info;
1119
1120
13.9M
  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
13.9M
  foreach_cluster (buffer, start, end)
1151
222M
  {
1152
222M
    unsigned int mask = 0;
1153
446M
    for (unsigned int i = start; i < end; i++)
1154
223M
      mask |= info[i].mask;
1155
1156
222M
    if (mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK)
1157
2.18M
      mask &= ~HB_GLYPH_FLAG_SAFE_TO_INSERT_TATWEEL;
1158
222M
    if (mask & HB_GLYPH_FLAG_SAFE_TO_INSERT_TATWEEL)
1159
18.7k
      mask |= HB_GLYPH_FLAG_UNSAFE_TO_BREAK | HB_GLYPH_FLAG_UNSAFE_TO_CONCAT;
1160
1161
222M
    mask &= and_mask;
1162
1163
446M
    for (unsigned int i = start; i < end; i++)
1164
223M
      info[i].mask = mask;
1165
222M
  }
1166
13.9M
}
1167
1168
/* Pull it all together! */
1169
1170
static void
1171
hb_ot_shape_internal (hb_ot_shape_context_t *c)
1172
13.9M
{
1173
  /* Save the original direction, we use it later. */
1174
13.9M
  c->target_direction = c->buffer->props.direction;
1175
1176
13.9M
  _hb_buffer_allocate_unicode_vars (c->buffer);
1177
1178
13.9M
  hb_set_unicode_props (c->buffer, c->plan->map.get_global_mask ());
1179
13.9M
  hb_insert_dotted_circle (c->buffer, c->font);
1180
1181
13.9M
  hb_form_clusters (c->buffer);
1182
1183
13.9M
  hb_ensure_native_direction (c->buffer);
1184
1185
13.9M
  if (c->plan->shaper->preprocess_text &&
1186
855k
      c->buffer->message(c->font, "start preprocess-text"))
1187
855k
  {
1188
855k
    c->plan->shaper->preprocess_text (c->plan, c->buffer, c->font);
1189
855k
    (void) c->buffer->message(c->font, "end preprocess-text");
1190
855k
  }
1191
1192
13.9M
  hb_ot_substitute_pre (c);
1193
13.9M
  hb_ot_position (c);
1194
13.9M
  hb_ot_substitute_post (c);
1195
1196
13.9M
  hb_propagate_flags (c->buffer);
1197
1198
13.9M
  _hb_buffer_deallocate_unicode_vars (c->buffer);
1199
1200
13.9M
  c->buffer->props.direction = c->target_direction;
1201
13.9M
}
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
13.9M
{
1211
13.9M
  hb_ot_shape_context_t c = {&shape_plan->ot, font, font->face, buffer, features, num_features};
1212
13.9M
  hb_ot_shape_internal (&c);
1213
1214
13.9M
  return true;
1215
13.9M
}
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