Coverage Report

Created: 2023-06-07 06:14

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