Coverage Report

Created: 2026-03-31 11:00

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