Coverage Report

Created: 2021-08-22 09:07

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