Coverage Report

Created: 2025-07-12 07:03

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