Coverage Report

Created: 2025-08-29 06:20

/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
467k
{ 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.19k
{
62
  /* https://github.com/harfbuzz/harfbuzz/issues/2124 */
63
2.19k
  return hb_aat_layout_has_substitution (face) &&
64
2.19k
   (HB_DIRECTION_IS_HORIZONTAL (props.direction) || !hb_ot_layout_has_substitution (face));
65
2.19k
}
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.19k
            face (face),
86
2.19k
            props (props),
87
2.19k
            map (face, props)
88
#ifndef HB_NO_AAT_SHAPE
89
2.19k
            , aat_map (face, props)
90
2.19k
            , apply_morx (_hb_apply_morx (face, props))
91
#endif
92
2.19k
{
93
2.19k
  shaper = hb_ot_shaper_categorize (props.script, props.direction, map.chosen_script[0]);
94
95
2.19k
  script_zero_marks = shaper->zero_width_marks != HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE;
96
2.19k
  script_fallback_mark_positioning = shaper->fallback_position;
97
98
2.19k
#ifndef HB_NO_AAT_SHAPE
99
  /* https://github.com/harfbuzz/harfbuzz/issues/1528 */
100
2.19k
  if (apply_morx && shaper != &_hb_ot_shaper_default)
101
0
    shaper = &_hb_ot_shaper_dumber;
102
2.19k
#endif
103
2.19k
}
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.19k
{
109
2.19k
  plan.props = props;
110
2.19k
  plan.shaper = shaper;
111
2.19k
  map.compile (plan.map, key);
112
2.19k
#ifndef HB_NO_AAT_SHAPE
113
2.19k
  if (apply_morx)
114
0
    aat_map.compile (plan.aat_map);
115
2.19k
#endif
116
117
2.19k
#ifndef HB_NO_OT_SHAPE_FRACTIONS
118
2.19k
  plan.frac_mask = plan.map.get_1_mask (HB_TAG ('f','r','a','c'));
119
2.19k
  plan.numr_mask = plan.map.get_1_mask (HB_TAG ('n','u','m','r'));
120
2.19k
  plan.dnom_mask = plan.map.get_1_mask (HB_TAG ('d','n','o','m'));
121
2.19k
  plan.has_frac = plan.frac_mask || (plan.numr_mask && plan.dnom_mask);
122
2.19k
#endif
123
124
2.19k
  plan.rtlm_mask = plan.map.get_1_mask (HB_TAG ('r','t','l','m'));
125
2.19k
  plan.has_vert = !!plan.map.get_1_mask (HB_TAG ('v','e','r','t'));
126
127
2.19k
  hb_tag_t kern_tag = HB_DIRECTION_IS_HORIZONTAL (props.direction) ?
128
2.19k
          HB_TAG ('k','e','r','n') : HB_TAG ('v','k','r','n');
129
2.19k
#ifndef HB_NO_OT_KERN
130
2.19k
  plan.kern_mask = plan.map.get_mask (kern_tag);
131
2.19k
  plan.requested_kerning = !!plan.kern_mask;
132
2.19k
#endif
133
134
2.19k
  bool has_gpos_kern = plan.map.get_feature_index (1, kern_tag) != HB_OT_LAYOUT_NO_FEATURE_INDEX;
135
2.19k
  bool disable_gpos = plan.shaper->gpos_tag &&
136
2.19k
          plan.shaper->gpos_tag != plan.map.chosen_script[1];
137
138
  /*
139
   * Decide who provides glyph classes. GDEF or Unicode.
140
   */
141
142
2.19k
  if (!hb_ot_layout_has_glyph_classes (face))
143
2.17k
    plan.fallback_glyph_classes = true;
144
145
  /*
146
   * Decide who does substitutions. GSUB, morx, or fallback.
147
   */
148
149
2.19k
#ifndef HB_NO_AAT_SHAPE
150
2.19k
  plan.apply_morx = apply_morx;
151
2.19k
#endif
152
153
  /*
154
   * Decide who does positioning. GPOS, kerx, kern, or fallback.
155
   */
156
157
2.19k
#ifndef HB_NO_AAT_SHAPE
158
2.19k
  bool has_kerx = hb_aat_layout_has_positioning (face);
159
2.19k
  bool has_gsub = !apply_morx && hb_ot_layout_has_substitution (face);
160
2.19k
#endif
161
2.19k
  bool has_gpos = !disable_gpos && hb_ot_layout_has_positioning (face);
162
2.19k
  if (false)
163
0
    {}
164
2.19k
#ifndef HB_NO_AAT_SHAPE
165
  /* Prefer GPOS over kerx if GSUB is present;
166
   * https://github.com/harfbuzz/harfbuzz/issues/3008 */
167
2.19k
  else if (has_kerx && !(has_gsub && has_gpos))
168
0
    plan.apply_kerx = true;
169
2.19k
#endif
170
2.19k
  else if (has_gpos)
171
2.19k
    plan.apply_gpos = true;
172
173
2.19k
  if (!plan.apply_kerx && (!has_gpos_kern || !plan.apply_gpos))
174
2.19k
  {
175
2.19k
    if (false) {}
176
2.19k
#ifndef HB_NO_AAT_SHAPE
177
2.19k
    else if (has_kerx)
178
0
      plan.apply_kerx = true;
179
2.19k
#endif
180
2.19k
#ifndef HB_NO_OT_KERN
181
2.19k
    else if (hb_ot_layout_has_kerning (face))
182
20
      plan.apply_kern = true;
183
2.17k
#endif
184
2.17k
    else {}
185
2.19k
  }
186
187
2.19k
  plan.apply_fallback_kern = !(plan.apply_gpos || plan.apply_kerx || plan.apply_kern);
188
189
2.19k
  plan.zero_marks = script_zero_marks &&
190
2.19k
        !plan.apply_kerx &&
191
2.19k
        (!plan.apply_kern
192
2.18k
#ifndef HB_NO_OT_KERN
193
2.18k
         || !hb_ot_layout_has_machine_kerning (face)
194
2.18k
#endif
195
2.18k
        );
196
2.19k
  plan.has_gpos_mark = !!plan.map.get_1_mask (HB_TAG ('m','a','r','k'));
197
198
2.19k
  plan.adjust_mark_positioning_when_zeroing = !plan.apply_gpos &&
199
2.19k
                !plan.apply_kerx &&
200
2.19k
                (!plan.apply_kern
201
1
#ifndef HB_NO_OT_KERN
202
1
                 || !hb_ot_layout_has_cross_kerning (face)
203
1
#endif
204
1
                );
205
206
2.19k
  plan.fallback_mark_positioning = plan.adjust_mark_positioning_when_zeroing &&
207
2.19k
           script_fallback_mark_positioning;
208
209
2.19k
#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.19k
  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.19k
#ifndef HB_NO_STYLE
218
2.19k
  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.19k
#endif
224
2.19k
}
225
226
bool
227
hb_ot_shape_plan_t::init0 (hb_face_t                     *face,
228
         const hb_shape_plan_key_t     *key)
229
2.19k
{
230
2.19k
  map.init ();
231
232
2.19k
  hb_ot_shape_planner_t planner (face,
233
2.19k
         key->props);
234
235
2.19k
  hb_ot_shape_collect_features (&planner,
236
2.19k
        key->user_features,
237
2.19k
        key->num_user_features);
238
239
2.19k
  planner.compile (*this, key->ot);
240
241
2.19k
  if (shaper->data_create)
242
5
  {
243
5
    data = shaper->data_create (this);
244
5
    if (unlikely (!data))
245
0
    {
246
0
      map.fini ();
247
0
      return false;
248
0
    }
249
5
  }
250
251
2.19k
  return true;
252
2.19k
}
253
254
void
255
hb_ot_shape_plan_t::fini ()
256
2.19k
{
257
2.19k
  if (shaper->data_destroy)
258
5
    shaper->data_destroy (const_cast<void *> (data));
259
260
2.19k
  map.fini ();
261
2.19k
}
262
263
void
264
hb_ot_shape_plan_t::substitute (hb_font_t   *font,
265
        hb_buffer_t *buffer) const
266
123k
{
267
123k
  map.substitute (this, font, buffer);
268
123k
}
269
270
void
271
hb_ot_shape_plan_t::position (hb_font_t   *font,
272
            hb_buffer_t *buffer) const
273
123k
{
274
123k
  if (this->apply_gpos)
275
123k
    map.position (this, font, buffer);
276
32
#ifndef HB_NO_AAT_SHAPE
277
32
  else if (this->apply_kerx)
278
0
    hb_aat_layout_position (this, font, buffer);
279
123k
#endif
280
281
123k
#ifndef HB_NO_OT_KERN
282
123k
  if (this->apply_kern)
283
906
    hb_ot_layout_kern (this, font, buffer);
284
122k
#endif
285
122k
  else if (this->apply_fallback_kern)
286
32
    _hb_ot_shape_fallback_kern (this, font, buffer);
287
288
123k
#ifndef HB_NO_AAT_SHAPE
289
123k
  if (this->apply_trak)
290
0
    hb_aat_layout_track (this, font, buffer);
291
123k
#endif
292
123k
}
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.19k
{
325
2.19k
  hb_ot_map_builder_t *map = &planner->map;
326
327
2.19k
  map->is_simple = true;
328
329
2.19k
  map->enable_feature (HB_TAG('r','v','r','n'));
330
2.19k
  map->add_gsub_pause (nullptr);
331
332
2.19k
  switch (planner->props.direction)
333
2.19k
  {
334
2.19k
    case HB_DIRECTION_LTR:
335
2.19k
      map->enable_feature (HB_TAG ('l','t','r','a'));
336
2.19k
      map->enable_feature (HB_TAG ('l','t','r','m'));
337
2.19k
      break;
338
2
    case HB_DIRECTION_RTL:
339
2
      map->enable_feature (HB_TAG ('r','t','l','a'));
340
2
      map->add_feature (HB_TAG ('r','t','l','m'));
341
2
      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.19k
  }
348
349
2.19k
#ifndef HB_NO_OT_SHAPE_FRACTIONS
350
  /* Automatic fractions. */
351
2.19k
  map->add_feature (HB_TAG ('f','r','a','c'));
352
2.19k
  map->add_feature (HB_TAG ('n','u','m','r'));
353
2.19k
  map->add_feature (HB_TAG ('d','n','o','m'));
354
2.19k
#endif
355
356
  /* Random! */
357
2.19k
  map->enable_feature (HB_TAG ('r','a','n','d'), F_RANDOM, HB_OT_MAP_MAX_VALUE);
358
359
2.19k
  map->enable_feature (HB_TAG ('H','a','r','f')); /* Considered required. */
360
2.19k
  map->enable_feature (HB_TAG ('H','A','R','F')); /* Considered discretionary. */
361
362
2.19k
  if (planner->shaper->collect_features)
363
5
  {
364
5
    map->is_simple = false;
365
5
    planner->shaper->collect_features (planner);
366
5
  }
367
368
2.19k
  map->enable_feature (HB_TAG ('B','u','z','z')); /* Considered required. */
369
2.19k
  map->enable_feature (HB_TAG ('B','U','Z','Z')); /* Considered discretionary. */
370
371
17.5k
  for (unsigned int i = 0; i < ARRAY_LENGTH (common_features); i++)
372
15.3k
    map->add_feature (common_features[i]);
373
374
2.19k
  if (HB_DIRECTION_IS_HORIZONTAL (planner->props.direction))
375
17.5k
    for (unsigned int i = 0; i < ARRAY_LENGTH (horizontal_features); i++)
376
15.3k
      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.19k
  if (num_user_features)
391
2.19k
    map->is_simple = false;
392
13.1k
  for (unsigned int i = 0; i < num_user_features; i++)
393
10.9k
  {
394
10.9k
    const hb_feature_t *feature = &user_features[i];
395
10.9k
    map->add_feature (feature->tag,
396
10.9k
          (feature->start == HB_FEATURE_GLOBAL_START &&
397
10.9k
           feature->end == HB_FEATURE_GLOBAL_END) ?  F_GLOBAL : F_NONE,
398
10.9k
          feature->value);
399
10.9k
  }
400
401
2.19k
  if (planner->shaper->override_features)
402
3
    planner->shaper->override_features (planner);
403
2.19k
}
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
1.88k
{
415
1.88k
  return (hb_ot_face_data_t *) HB_SHAPER_DATA_SUCCEEDED;
416
1.88k
}
417
418
void
419
_hb_ot_shaper_face_data_destroy (hb_ot_face_data_t *data)
420
1.88k
{
421
1.88k
}
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
123k
{
435
123k
  const OT::ItemVariationStore &var_store = font->face->table.GDEF->table->get_var_store ();
436
123k
  return (hb_ot_font_data_t *) var_store.create_cache ();
437
123k
}
438
439
void
440
_hb_ot_shaper_font_data_destroy (hb_ot_font_data_t *data)
441
123k
{
442
123k
  OT::ItemVariationStore::destroy_cache ((OT::hb_scalar_cache_t *) data);
443
123k
}
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
123k
{
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
123k
  unsigned int count = buffer->len;
482
123k
  hb_glyph_info_t *info = buffer->info;
483
1.07M
  for (unsigned int i = 0; i < count; i++)
484
947k
  {
485
947k
    _hb_glyph_info_set_unicode_props (&info[i], buffer);
486
487
947k
    unsigned gen_cat = _hb_glyph_info_get_general_category (&info[i]);
488
947k
    if (FLAG_UNSAFE (gen_cat) &
489
947k
  (FLAG (HB_UNICODE_GENERAL_CATEGORY_LOWERCASE_LETTER) |
490
947k
   FLAG (HB_UNICODE_GENERAL_CATEGORY_UPPERCASE_LETTER) |
491
947k
   FLAG (HB_UNICODE_GENERAL_CATEGORY_TITLECASE_LETTER) |
492
947k
   FLAG (HB_UNICODE_GENERAL_CATEGORY_OTHER_LETTER) |
493
947k
   FLAG (HB_UNICODE_GENERAL_CATEGORY_SPACE_SEPARATOR)))
494
398k
      continue;
495
496
    /* Marks are already set as continuation by the above line.
497
     * Handle Emoji_Modifier and ZWJ-continuation. */
498
548k
    if (unlikely (gen_cat == HB_UNICODE_GENERAL_CATEGORY_MODIFIER_SYMBOL &&
499
548k
      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
548k
    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
548k
#ifndef HB_NO_EMOJI_SEQUENCES
512
548k
    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
548k
#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
548k
    else if (unlikely (hb_in_ranges<hb_codepoint_t> (info[i].codepoint, 0xFF9Eu, 0xFF9Fu, 0xE0020u, 0xE007Fu)))
539
1.35k
      _hb_glyph_info_set_continuation (&info[i]);
540
548k
  }
541
123k
}
542
543
static void
544
hb_insert_dotted_circle (hb_buffer_t *buffer, hb_font_t *font)
545
123k
{
546
123k
  if (unlikely (buffer->flags & HB_BUFFER_FLAG_DO_NOT_INSERT_DOTTED_CIRCLE))
547
0
    return;
548
549
123k
  if (!(buffer->flags & HB_BUFFER_FLAG_BOT) ||
550
123k
      buffer->context_len[0] ||
551
123k
      !_hb_glyph_info_is_unicode_mark (&buffer->info[0]))
552
123k
    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
123k
{
575
123k
  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII))
576
84.2k
    return;
577
578
39.1k
  if (HB_BUFFER_CLUSTER_LEVEL_IS_GRAPHEMES (buffer->cluster_level))
579
39.1k
    foreach_grapheme (buffer, start, end)
580
457k
      buffer->merge_clusters (start, end);
581
0
  else
582
0
    foreach_grapheme (buffer, start, end)
583
0
      buffer->unsafe_to_break (start, end);
584
39.1k
}
585
586
static void
587
hb_ensure_native_direction (hb_buffer_t *buffer)
588
123k
{
589
123k
  hb_direction_t direction = buffer->props.direction;
590
123k
  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
123k
  if (unlikely (horiz_dir == HB_DIRECTION_RTL && direction == HB_DIRECTION_LTR))
610
35
  {
611
35
    bool found_number = false, found_letter = false, found_ri = false;
612
35
    const auto* info = buffer->info;
613
35
    const auto count = buffer->len;
614
134
    for (unsigned i = 0; i < count; i++)
615
99
    {
616
99
      auto gc = _hb_glyph_info_get_general_category (&info[i]);
617
99
      if (HB_UNICODE_GENERAL_CATEGORY_IS_LETTER (gc))
618
0
      {
619
0
  found_letter = true;
620
0
  break;
621
0
      }
622
99
      else if (gc == HB_UNICODE_GENERAL_CATEGORY_DECIMAL_NUMBER)
623
3
  found_number = true;
624
96
      else if (unlikely (_hb_codepoint_is_regional_indicator (info[i].codepoint)))
625
0
  found_ri = true;
626
99
    }
627
35
    if ((found_number || found_ri) && !found_letter)
628
3
      horiz_dir = HB_DIRECTION_LTR;
629
35
  }
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
123k
  if ((HB_DIRECTION_IS_HORIZONTAL (direction) &&
636
123k
       direction != horiz_dir && HB_DIRECTION_IS_VALID (horiz_dir)) ||
637
123k
      (HB_DIRECTION_IS_VERTICAL   (direction) &&
638
123k
       direction != HB_DIRECTION_TTB))
639
52
  {
640
52
    _hb_ot_layout_reverse_graphemes (buffer);
641
52
    buffer->props.direction = HB_DIRECTION_REVERSE (buffer->props.direction);
642
52
  }
643
123k
}
644
645
646
/*
647
 * Substitute
648
 */
649
650
static inline void
651
hb_ot_rotate_chars (const hb_ot_shape_context_t *c)
652
123k
{
653
123k
  hb_buffer_t *buffer = c->buffer;
654
123k
  unsigned int count = buffer->len;
655
123k
  hb_glyph_info_t *info = buffer->info;
656
657
123k
  if (HB_DIRECTION_IS_BACKWARD (c->target_direction))
658
23
  {
659
23
    hb_unicode_funcs_t *unicode = buffer->unicode;
660
23
    hb_mask_t rtlm_mask = c->plan->rtlm_mask;
661
662
46
    for (unsigned int i = 0; i < count; i++) {
663
23
      hb_codepoint_t codepoint = unicode->mirroring (info[i].codepoint);
664
23
      if (unlikely (codepoint != info[i].codepoint && c->font->has_glyph (codepoint)))
665
0
  info[i].codepoint = codepoint;
666
23
      else
667
23
  info[i].mask |= rtlm_mask;
668
23
    }
669
23
  }
670
671
123k
#ifndef HB_NO_VERTICAL
672
123k
  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
123k
#endif
681
123k
}
682
683
static inline void
684
hb_ot_shape_setup_masks_fraction (const hb_ot_shape_context_t *c)
685
123k
{
686
#ifdef HB_NO_OT_SHAPE_FRACTIONS
687
  return;
688
#endif
689
690
123k
  if (!(c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII) ||
691
123k
      !c->plan->has_frac)
692
123k
    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
123k
{
748
123k
  hb_ot_map_t *map = &c->plan->map;
749
123k
  hb_buffer_t *buffer = c->buffer;
750
751
123k
  hb_mask_t global_mask = map->get_global_mask ();
752
123k
  buffer->reset_masks (global_mask);
753
123k
}
754
755
static inline void
756
hb_ot_shape_setup_masks (const hb_ot_shape_context_t *c)
757
123k
{
758
123k
  hb_ot_map_t *map = &c->plan->map;
759
123k
  hb_buffer_t *buffer = c->buffer;
760
761
123k
  hb_ot_shape_setup_masks_fraction (c);
762
763
123k
  if (c->plan->shaper->setup_masks)
764
488
    c->plan->shaper->setup_masks (c->plan, buffer, c->font);
765
766
740k
  for (unsigned int i = 0; i < c->num_user_features; i++)
767
617k
  {
768
617k
    const hb_feature_t *feature = &c->user_features[i];
769
617k
    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
617k
  }
775
123k
}
776
777
static void
778
hb_ot_zero_width_default_ignorables (const hb_buffer_t *buffer)
779
123k
{
780
123k
  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
781
123k
      (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES) ||
782
123k
      (buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES))
783
122k
    return;
784
785
1.36k
  unsigned int count = buffer->len;
786
1.36k
  hb_glyph_info_t *info = buffer->info;
787
1.36k
  hb_glyph_position_t *pos = buffer->pos;
788
1.36k
  unsigned int i = 0;
789
10.3k
  for (i = 0; i < count; i++)
790
9.02k
    if (unlikely (_hb_glyph_info_is_default_ignorable (&info[i])))
791
1.44k
    {
792
1.44k
      pos[i].x_advance = pos[i].y_advance = 0;
793
1.44k
      if (HB_DIRECTION_IS_HORIZONTAL (buffer->props.direction))
794
1.44k
  pos[i].x_offset = 0;
795
0
      else
796
0
        pos[i].y_offset = 0;
797
1.44k
    }
798
1.36k
}
799
800
static void
801
hb_ot_deal_with_variation_selectors (hb_buffer_t *buffer)
802
123k
{
803
123k
  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_VARIATION_SELECTOR_FALLBACK) ||
804
123k
  buffer->not_found_variation_selector == HB_CODEPOINT_INVALID)
805
123k
    return;
806
807
0
  unsigned int count = buffer->len;
808
0
  hb_glyph_info_t *info = buffer->info;
809
0
  hb_glyph_position_t *pos = buffer->pos;
810
811
0
  for (unsigned int i = 0; i < count; i++)
812
0
  {
813
0
    if (_hb_glyph_info_is_variation_selector (&info[i]))
814
0
    {
815
0
      info[i].codepoint = buffer->not_found_variation_selector;
816
0
      pos[i].x_advance = pos[i].y_advance = pos[i].x_offset = pos[i].y_offset = 0;
817
0
      _hb_glyph_info_set_variation_selector (&info[i], false);
818
0
    }
819
0
  }
820
0
}
821
822
static void
823
hb_ot_hide_default_ignorables (hb_buffer_t *buffer,
824
             hb_font_t   *font)
825
123k
{
826
123k
  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES) ||
827
123k
      (buffer->flags & HB_BUFFER_FLAG_PRESERVE_DEFAULT_IGNORABLES))
828
122k
    return;
829
830
1.36k
  unsigned int count = buffer->len;
831
1.36k
  hb_glyph_info_t *info = buffer->info;
832
833
1.36k
  hb_codepoint_t invisible = buffer->invisible;
834
1.36k
  if (!(buffer->flags & HB_BUFFER_FLAG_REMOVE_DEFAULT_IGNORABLES) &&
835
1.36k
      (invisible || font->get_nominal_glyph (' ', &invisible)))
836
1.36k
  {
837
    /* Replace default-ignorables with a zero-advance invisible glyph. */
838
10.3k
    for (unsigned int i = 0; i < count; i++)
839
9.02k
    {
840
9.02k
      if (_hb_glyph_info_is_default_ignorable (&info[i]))
841
1.44k
  info[i].codepoint = invisible;
842
9.02k
    }
843
1.36k
  }
844
0
  else
845
0
    buffer->delete_glyphs_inplace (_hb_glyph_info_is_default_ignorable);
846
1.36k
}
847
848
849
static inline void
850
hb_ot_map_glyphs_fast (hb_buffer_t  *buffer)
851
123k
{
852
  /* Normalization process sets up normalizer_glyph_index(), we just copy it. */
853
123k
  unsigned int count = buffer->len;
854
123k
  hb_glyph_info_t *info = buffer->info;
855
1.07M
  for (unsigned int i = 0; i < count; i++)
856
947k
    info[i].codepoint = info[i].normalizer_glyph_index();
857
858
123k
  buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
859
123k
}
860
861
static inline void
862
hb_synthesize_glyph_classes (hb_buffer_t *buffer)
863
122k
{
864
122k
  unsigned int count = buffer->len;
865
122k
  hb_glyph_info_t *info = buffer->info;
866
1.05M
  for (unsigned int i = 0; i < count; i++)
867
931k
  {
868
931k
    hb_ot_layout_glyph_props_flags_t klass;
869
870
    /* Never mark default-ignorables as marks.
871
     * They won't get in the way of lookups anyway,
872
     * but having them as mark will cause them to be skipped
873
     * over if the lookup-flag says so, but at least for the
874
     * Mongolian variation selectors, looks like Uniscribe
875
     * marks them as non-mark.  Some Mongolian fonts without
876
     * GDEF rely on this.  Another notable character that
877
     * this applies to is COMBINING GRAPHEME JOINER. */
878
931k
    klass = (_hb_glyph_info_get_general_category (&info[i]) !=
879
931k
       HB_UNICODE_GENERAL_CATEGORY_NON_SPACING_MARK ||
880
931k
       _hb_glyph_info_is_default_ignorable (&info[i])) ?
881
931k
      HB_OT_LAYOUT_GLYPH_PROPS_BASE_GLYPH :
882
931k
      HB_OT_LAYOUT_GLYPH_PROPS_MARK;
883
931k
    _hb_glyph_info_set_glyph_props (&info[i], klass);
884
931k
  }
885
122k
}
886
887
static inline void
888
hb_ot_substitute_default (const hb_ot_shape_context_t *c)
889
123k
{
890
123k
  hb_buffer_t *buffer = c->buffer;
891
892
123k
  hb_ot_rotate_chars (c);
893
894
123k
  HB_BUFFER_ALLOCATE_VAR (buffer, normalizer_glyph_index);
895
896
123k
  _hb_ot_shape_normalize (c->plan, buffer, c->font);
897
898
123k
  hb_ot_shape_setup_masks (c);
899
900
  /* This is unfortunate to go here, but necessary... */
901
123k
  if (c->plan->fallback_mark_positioning)
902
32
    _hb_ot_shape_fallback_mark_position_recategorize_marks (c->plan, c->font, buffer);
903
904
123k
  hb_ot_map_glyphs_fast (buffer);
905
906
123k
  HB_BUFFER_DEALLOCATE_VAR (buffer, normalizer_glyph_index);
907
123k
}
908
909
static inline void
910
hb_ot_substitute_plan (const hb_ot_shape_context_t *c)
911
123k
{
912
123k
  hb_buffer_t *buffer = c->buffer;
913
914
123k
  hb_ot_layout_substitute_start (c->font, buffer);
915
916
123k
  if (c->plan->fallback_glyph_classes)
917
122k
    hb_synthesize_glyph_classes (c->buffer);
918
919
123k
#ifndef HB_NO_AAT_SHAPE
920
123k
  if (unlikely (c->plan->apply_morx))
921
0
    hb_aat_layout_substitute (c->plan, c->font, c->buffer,
922
0
            c->user_features, c->num_user_features);
923
123k
  else
924
123k
#endif
925
123k
    c->plan->substitute (c->font, buffer);
926
123k
}
927
928
static inline void
929
hb_ot_substitute_pre (const hb_ot_shape_context_t *c)
930
123k
{
931
123k
  hb_ot_substitute_default (c);
932
933
123k
  _hb_buffer_allocate_gsubgpos_vars (c->buffer);
934
935
123k
  hb_ot_substitute_plan (c);
936
937
123k
#ifndef HB_NO_AAT_SHAPE
938
123k
  if (c->plan->apply_morx && c->plan->apply_gpos)
939
0
    hb_aat_layout_remove_deleted_glyphs (c->buffer);
940
123k
#endif
941
123k
}
942
943
static inline void
944
hb_ot_substitute_post (const hb_ot_shape_context_t *c)
945
123k
{
946
123k
#ifndef HB_NO_AAT_SHAPE
947
123k
  if (c->plan->apply_morx && !c->plan->apply_gpos)
948
0
    hb_aat_layout_remove_deleted_glyphs (c->buffer);
949
123k
#endif
950
951
123k
  hb_ot_deal_with_variation_selectors (c->buffer);
952
123k
  hb_ot_hide_default_ignorables (c->buffer, c->font);
953
954
123k
  if (c->plan->shaper->postprocess_glyphs &&
955
123k
    c->buffer->message(c->font, "start postprocess-glyphs")) {
956
6
    c->plan->shaper->postprocess_glyphs (c->plan, c->buffer, c->font);
957
6
    (void) c->buffer->message(c->font, "end postprocess-glyphs");
958
6
  }
959
123k
}
960
961
962
/*
963
 * Position
964
 */
965
966
static inline void
967
adjust_mark_offsets (hb_glyph_position_t *pos)
968
0
{
969
0
  pos->x_offset -= pos->x_advance;
970
0
  pos->y_offset -= pos->y_advance;
971
0
}
972
973
static inline void
974
zero_mark_width (hb_glyph_position_t *pos)
975
35
{
976
35
  pos->x_advance = 0;
977
35
  pos->y_advance = 0;
978
35
}
979
980
static inline void
981
zero_mark_widths_by_gdef (hb_buffer_t *buffer, bool adjust_offsets)
982
122k
{
983
122k
  unsigned int count = buffer->len;
984
122k
  hb_glyph_info_t *info = buffer->info;
985
1.06M
  for (unsigned int i = 0; i < count; i++)
986
945k
    if (_hb_glyph_info_is_mark (&info[i]))
987
35
    {
988
35
      if (adjust_offsets)
989
0
  adjust_mark_offsets (&buffer->pos[i]);
990
35
      zero_mark_width (&buffer->pos[i]);
991
35
    }
992
122k
}
993
994
static inline void
995
hb_ot_position_default (const hb_ot_shape_context_t *c)
996
123k
{
997
123k
  hb_direction_t direction = c->buffer->props.direction;
998
123k
  unsigned int count = c->buffer->len;
999
123k
  hb_glyph_info_t *info = c->buffer->info;
1000
123k
  hb_glyph_position_t *pos = c->buffer->pos;
1001
1002
123k
  if (HB_DIRECTION_IS_HORIZONTAL (direction))
1003
123k
  {
1004
123k
    c->font->get_glyph_h_advances (count, &info[0].codepoint, sizeof(info[0]),
1005
123k
           &pos[0].x_advance, sizeof(pos[0]));
1006
    // h_origin defaults to zero; only apply it if the font has it.
1007
123k
    if (c->font->has_glyph_h_origin_func () || c->font->has_glyph_h_origins_func ())
1008
123k
      c->font->subtract_glyph_h_origins (c->buffer);
1009
123k
  }
1010
0
  else
1011
0
  {
1012
0
    c->font->get_glyph_v_advances (count, &info[0].codepoint, sizeof(info[0]),
1013
0
           &pos[0].y_advance, sizeof(pos[0]));
1014
    // v_origin defaults to non-zero; apply even if only fallback is there.
1015
0
    c->font->subtract_glyph_v_origins (c->buffer);
1016
0
  }
1017
123k
  if (c->buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK)
1018
0
    _hb_ot_shape_fallback_spaces (c->plan, c->font, c->buffer);
1019
123k
}
1020
1021
static inline void
1022
hb_ot_position_plan (const hb_ot_shape_context_t *c)
1023
123k
{
1024
  /* If the font has no GPOS and direction is forward, then when
1025
   * zeroing mark widths, we shift the mark with it, such that the
1026
   * mark is positioned hanging over the previous glyph.  When
1027
   * direction is backward we don't shift and it will end up
1028
   * hanging over the next glyph after the final reordering.
1029
   *
1030
   * Note: If fallback positioning happens, we don't care about
1031
   * this as it will be overridden.
1032
   */
1033
123k
  bool adjust_offsets_when_zeroing = c->plan->adjust_mark_positioning_when_zeroing &&
1034
123k
             HB_DIRECTION_IS_FORWARD (c->buffer->props.direction);
1035
1036
  /* We change glyph origin to what GPOS expects (horizontal), apply GPOS, change it back. */
1037
1038
  // h_origin defaults to zero; only apply it if the font has it.
1039
123k
  if (c->font->has_glyph_h_origin_func () || c->font->has_glyph_h_origins_func ())
1040
123k
    c->font->add_glyph_h_origins (c->buffer);
1041
1042
123k
  hb_ot_layout_position_start (c->font, c->buffer);
1043
1044
123k
  if (c->plan->zero_marks)
1045
122k
    switch (c->plan->shaper->zero_width_marks)
1046
122k
    {
1047
0
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
1048
0
  zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
1049
0
  break;
1050
1051
0
      default:
1052
0
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
1053
122k
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
1054
122k
  break;
1055
122k
    }
1056
1057
123k
  c->plan->position (c->font, c->buffer);
1058
1059
123k
  if (c->plan->zero_marks)
1060
122k
    switch (c->plan->shaper->zero_width_marks)
1061
122k
    {
1062
122k
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_LATE:
1063
122k
  zero_mark_widths_by_gdef (c->buffer, adjust_offsets_when_zeroing);
1064
122k
  break;
1065
1066
0
      default:
1067
0
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_NONE:
1068
0
      case HB_OT_SHAPE_ZERO_WIDTH_MARKS_BY_GDEF_EARLY:
1069
0
  break;
1070
122k
    }
1071
1072
  /* Finish off.  Has to follow a certain order. */
1073
123k
  hb_ot_layout_position_finish_advances (c->font, c->buffer);
1074
123k
  hb_ot_zero_width_default_ignorables (c->buffer);
1075
123k
  hb_ot_layout_position_finish_offsets (c->font, c->buffer);
1076
1077
  // h_origin defaults to zero; only apply it if the font has it.
1078
123k
  if (c->font->has_glyph_h_origin_func () || c->font->has_glyph_h_origins_func ())
1079
123k
    c->font->subtract_glyph_h_origins (c->buffer);
1080
1081
123k
  if (c->plan->fallback_mark_positioning)
1082
32
    _hb_ot_shape_fallback_mark_position (c->plan, c->font, c->buffer,
1083
32
           adjust_offsets_when_zeroing);
1084
123k
}
1085
1086
static inline void
1087
hb_ot_position (const hb_ot_shape_context_t *c)
1088
123k
{
1089
123k
  c->buffer->clear_positions ();
1090
1091
123k
  hb_ot_position_default (c);
1092
1093
123k
  hb_ot_position_plan (c);
1094
1095
123k
  if (HB_DIRECTION_IS_BACKWARD (c->buffer->props.direction))
1096
35
    hb_buffer_reverse (c->buffer);
1097
1098
123k
  _hb_buffer_deallocate_gsubgpos_vars (c->buffer);
1099
123k
}
1100
1101
static inline void
1102
hb_propagate_flags (hb_buffer_t *buffer)
1103
123k
{
1104
  /* Propagate cluster-level glyph flags to be the same on all cluster glyphs.
1105
   * Simplifies using them. */
1106
1107
123k
  if (!(buffer->scratch_flags & HB_BUFFER_SCRATCH_FLAG_HAS_GLYPH_FLAGS))
1108
123k
    return;
1109
1110
  /* If we are producing SAFE_TO_INSERT_TATWEEL, then do two things:
1111
   *
1112
   * - If the places that the Arabic shaper marked as SAFE_TO_INSERT_TATWEEL,
1113
   *   are UNSAFE_TO_BREAK, then clear the SAFE_TO_INSERT_TATWEEL,
1114
   * - Any place that is SAFE_TO_INSERT_TATWEEL, is also now UNSAFE_TO_BREAK.
1115
   *
1116
   * We couldn't make this interaction earlier. It has to be done here.
1117
   */
1118
32
  bool flip_tatweel = buffer->flags & HB_BUFFER_FLAG_PRODUCE_SAFE_TO_INSERT_TATWEEL;
1119
1120
32
  bool clear_concat = (buffer->flags & HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT) == 0;
1121
1122
32
  hb_glyph_info_t *info = buffer->info;
1123
1124
32
  foreach_cluster (buffer, start, end)
1125
64
  {
1126
64
    unsigned int mask = 0;
1127
160
    for (unsigned int i = start; i < end; i++)
1128
96
      mask |= info[i].mask & HB_GLYPH_FLAG_DEFINED;
1129
1130
64
    if (flip_tatweel)
1131
0
    {
1132
0
      if (mask & HB_GLYPH_FLAG_UNSAFE_TO_BREAK)
1133
0
  mask &= ~HB_GLYPH_FLAG_SAFE_TO_INSERT_TATWEEL;
1134
0
      if (mask & HB_GLYPH_FLAG_SAFE_TO_INSERT_TATWEEL)
1135
0
  mask |= HB_GLYPH_FLAG_UNSAFE_TO_BREAK | HB_GLYPH_FLAG_UNSAFE_TO_CONCAT;
1136
0
    }
1137
1138
64
    if (clear_concat)
1139
64
  mask &= ~HB_GLYPH_FLAG_UNSAFE_TO_CONCAT;
1140
1141
160
    for (unsigned int i = start; i < end; i++)
1142
96
      info[i].mask = mask;
1143
64
  }
1144
32
}
1145
1146
/* Pull it all together! */
1147
1148
static void
1149
hb_ot_shape_internal (hb_ot_shape_context_t *c)
1150
123k
{
1151
  /* Save the original direction, we use it later. */
1152
123k
  c->target_direction = c->buffer->props.direction;
1153
1154
123k
  _hb_buffer_allocate_unicode_vars (c->buffer);
1155
1156
123k
  hb_ot_shape_initialize_masks (c);
1157
123k
  hb_set_unicode_props (c->buffer);
1158
123k
  hb_insert_dotted_circle (c->buffer, c->font);
1159
1160
123k
  hb_form_clusters (c->buffer);
1161
1162
123k
  hb_ensure_native_direction (c->buffer);
1163
1164
123k
  if (c->plan->shaper->preprocess_text &&
1165
123k
      c->buffer->message(c->font, "start preprocess-text"))
1166
485
  {
1167
485
    c->plan->shaper->preprocess_text (c->plan, c->buffer, c->font);
1168
485
    (void) c->buffer->message(c->font, "end preprocess-text");
1169
485
  }
1170
1171
123k
  hb_ot_substitute_pre (c);
1172
123k
  hb_ot_position (c);
1173
123k
  hb_ot_substitute_post (c);
1174
1175
123k
  hb_propagate_flags (c->buffer);
1176
1177
123k
  _hb_buffer_deallocate_unicode_vars (c->buffer);
1178
1179
123k
  c->buffer->props.direction = c->target_direction;
1180
123k
}
1181
1182
1183
hb_bool_t
1184
_hb_ot_shape (hb_shape_plan_t    *shape_plan,
1185
        hb_font_t          *font,
1186
        hb_buffer_t        *buffer,
1187
        const hb_feature_t *features,
1188
        unsigned int        num_features)
1189
123k
{
1190
123k
  hb_ot_shape_context_t c = {&shape_plan->ot, font, font->face, buffer, features, num_features};
1191
123k
  hb_ot_shape_internal (&c);
1192
1193
123k
  return true;
1194
123k
}
1195
1196
1197
/**
1198
 * hb_ot_shape_plan_collect_lookups:
1199
 * @shape_plan: #hb_shape_plan_t to query
1200
 * @table_tag: GSUB or GPOS
1201
 * @lookup_indexes: (out): The #hb_set_t set of lookups returned
1202
 *
1203
 * Computes the complete set of GSUB or GPOS lookups that are applicable
1204
 * under a given @shape_plan.
1205
 *
1206
 * Since: 0.9.7
1207
 **/
1208
void
1209
hb_ot_shape_plan_collect_lookups (hb_shape_plan_t *shape_plan,
1210
          hb_tag_t         table_tag,
1211
          hb_set_t        *lookup_indexes /* OUT */)
1212
0
{
1213
0
  shape_plan->ot.collect_lookups (table_tag, lookup_indexes);
1214
0
}
1215
1216
1217
/**
1218
 * hb_ot_shape_plan_get_feature_tags:
1219
 * @shape_plan: A shaping plan
1220
 * @start_offset: The index of first feature to retrieve
1221
 * @tag_count: (inout): Input = the maximum number of features to return;
1222
 *                      Output = the actual number of features returned (may be zero)
1223
 * @tags: (out) (array length=tag_count): The array of enabled feature
1224
 *
1225
 * Fetches the list of OpenType feature tags enabled for a shaping plan, if possible.
1226
 *
1227
 * Return value: Total number of feature tagss.
1228
 *
1229
 * Since: 10.3.0
1230
 */
1231
unsigned int
1232
hb_ot_shape_plan_get_feature_tags (hb_shape_plan_t *shape_plan,
1233
           unsigned int     start_offset,
1234
           unsigned int    *tag_count, /* IN/OUT */
1235
           hb_tag_t        *tags /* OUT */)
1236
0
{
1237
0
#ifndef HB_NO_OT_SHAPE
1238
0
  return shape_plan->ot.map.get_feature_tags (start_offset, tag_count, tags);
1239
#else
1240
  if (tag_count)
1241
  *tag_count = 0;
1242
  return 0;
1243
#endif
1244
0
}
1245
1246
1247
/* TODO Move this to hb-ot-shape-normalize, make it do decompose, and make it public. */
1248
static void
1249
add_char (hb_font_t          *font,
1250
    hb_unicode_funcs_t *unicode,
1251
    hb_bool_t           mirror,
1252
    hb_codepoint_t      u,
1253
    hb_set_t           *glyphs)
1254
0
{
1255
0
  hb_codepoint_t glyph;
1256
0
  if (font->get_nominal_glyph (u, &glyph))
1257
0
    glyphs->add (glyph);
1258
0
  if (mirror)
1259
0
  {
1260
0
    hb_codepoint_t m = unicode->mirroring (u);
1261
0
    if (m != u && font->get_nominal_glyph (m, &glyph))
1262
0
      glyphs->add (glyph);
1263
0
  }
1264
0
}
1265
1266
1267
/**
1268
 * hb_ot_shape_glyphs_closure:
1269
 * @font: #hb_font_t to work upon
1270
 * @buffer: The input buffer to compute from
1271
 * @features: (array length=num_features): The features enabled on the buffer
1272
 * @num_features: The number of features enabled on the buffer
1273
 * @glyphs: (out): The #hb_set_t set of glyphs comprising the transitive closure of the query
1274
 *
1275
 * Computes the transitive closure of glyphs needed for a specified
1276
 * input buffer under the given font and feature list. The closure is
1277
 * computed as a set, not as a list.
1278
 *
1279
 * Since: 0.9.2
1280
 **/
1281
void
1282
hb_ot_shape_glyphs_closure (hb_font_t          *font,
1283
          hb_buffer_t        *buffer,
1284
          const hb_feature_t *features,
1285
          unsigned int        num_features,
1286
          hb_set_t           *glyphs)
1287
0
{
1288
0
  const char *shapers[] = {"ot", nullptr};
1289
0
  hb_shape_plan_t *shape_plan = hb_shape_plan_create_cached (font->face, &buffer->props,
1290
0
                   features, num_features, shapers);
1291
1292
0
  bool mirror = hb_script_get_horizontal_direction (buffer->props.script) == HB_DIRECTION_RTL;
1293
1294
0
  unsigned int count = buffer->len;
1295
0
  hb_glyph_info_t *info = buffer->info;
1296
0
  for (unsigned int i = 0; i < count; i++)
1297
0
    add_char (font, buffer->unicode, mirror, info[i].codepoint, glyphs);
1298
1299
0
  hb_set_t *lookups = hb_set_create ();
1300
0
  hb_ot_shape_plan_collect_lookups (shape_plan, HB_OT_TAG_GSUB, lookups);
1301
0
  hb_ot_layout_lookups_substitute_closure (font->face, lookups, glyphs);
1302
1303
0
  hb_set_destroy (lookups);
1304
1305
0
  hb_shape_plan_destroy (shape_plan);
1306
0
}
1307
1308
1309
#endif