Coverage Report

Created: 2026-12-01 07:15

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