Coverage Report

Created: 2026-09-28 10:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/workdir/UnpackedTarball/harfbuzz/src/hb-shape-plan.cc
Line
Count
Source
1
/*
2
 * Copyright © 2012  Google, Inc.
3
 *
4
 *  This is part of HarfBuzz, a text shaping library.
5
 *
6
 * Permission is hereby granted, without written agreement and without
7
 * license or royalty fees, to use, copy, modify, and distribute this
8
 * software and its documentation for any purpose, provided that the
9
 * above copyright notice and the following two paragraphs appear in
10
 * all copies of this software.
11
 *
12
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
16
 * DAMAGE.
17
 *
18
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
21
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
23
 *
24
 * Google Author(s): Behdad Esfahbod
25
 */
26
27
#include "hb.hh"
28
#include "hb-shape-plan.hh"
29
#include "hb-shaper.hh"
30
#include "hb-font.hh"
31
#include "hb-buffer.hh"
32
33
34
#ifndef HB_NO_SHAPER
35
36
/**
37
 * SECTION:hb-shape-plan
38
 * @title: hb-shape-plan
39
 * @short_description: Object representing a shaping plan
40
 * @include: hb.h
41
 *
42
 * Shape plans are an internal mechanism. Each plan contains state
43
 * describing how HarfBuzz will shape a particular text segment, based on
44
 * the combination of segment properties and the capabilities in the
45
 * font face in use.
46
 *
47
 * Shape plans are not used for shaping directly, but can be queried to
48
 * access certain information about how shaping will perform, given a set
49
 * of specific input parameters (script, language, direction, features,
50
 * etc.).
51
 *
52
 * Most client programs will not need to deal with shape plans directly.
53
 **/
54
55
#ifdef HAVE_HARFRUST
56
extern "C" void _hb_harfrust_shape_plan_destroy_rs (void *data);
57
#endif
58
59
hb_shape_plan_t::~hb_shape_plan_t ()
60
0
{
61
#ifdef HAVE_HARFRUST
62
  void *data = harfrust_data.get_relaxed ();
63
  if (data)
64
    _hb_harfrust_shape_plan_destroy_rs (data);
65
#endif
66
0
  key.fini ();
67
0
}
68
69
70
/*
71
 * hb_shape_plan_key_t
72
 */
73
74
bool
75
hb_shape_plan_key_t::init (bool                           copy,
76
         hb_face_t                     *face,
77
         const hb_segment_properties_t *props,
78
         const hb_feature_t            *user_features,
79
         unsigned int                   num_user_features,
80
         const int                     *coords,
81
         unsigned int                   num_coords,
82
         const char * const            *shaper_list)
83
13.9M
{
84
13.9M
  hb_feature_t *features = nullptr;
85
13.9M
  if (copy && num_user_features && !(features = (hb_feature_t *) hb_calloc (num_user_features, sizeof (hb_feature_t))))
86
0
    goto bail;
87
88
13.9M
  this->props = *props;
89
13.9M
  this->num_user_features = num_user_features;
90
13.9M
  this->user_features = copy ? features : user_features;
91
13.9M
  if (copy && num_user_features)
92
4.55k
  {
93
4.55k
    hb_memcpy (features, user_features, num_user_features * sizeof (hb_feature_t));
94
    /* Make start/end uniform to easier catch bugs. */
95
343k
    for (unsigned int i = 0; i < num_user_features; i++)
96
338k
    {
97
338k
      if (features[0].start != HB_FEATURE_GLOBAL_START)
98
61
  features[0].start = 1;
99
338k
      if (features[0].end   != HB_FEATURE_GLOBAL_END)
100
83
  features[0].end   = 2;
101
338k
    }
102
4.55k
  }
103
13.9M
  this->shaper_func = nullptr;
104
13.9M
  this->shaper_name = nullptr;
105
13.9M
#ifndef HB_NO_OT_SHAPE
106
13.9M
  this->ot.init (face, coords, num_coords);
107
13.9M
#endif
108
109
  /*
110
   * Choose shaper.
111
   */
112
113
13.9M
#define HB_SHAPER_PLAN(shaper) \
114
27.9M
  HB_STMT_START { \
115
27.9M
    if (face->data.shaper) \
116
27.9M
    { \
117
13.9M
      this->shaper_func = _hb_##shaper##_shape; \
118
13.9M
      this->shaper_name = #shaper; \
119
13.9M
      return true; \
120
13.9M
    } \
121
27.9M
  } HB_STMT_END
122
123
13.9M
  if (unlikely (shaper_list))
124
13.9M
  {
125
27.9M
    for (; *shaper_list; shaper_list++)
126
27.9M
      if (false)
127
0
  ;
128
27.9M
#define HB_SHAPER_IMPLEMENT(shaper) \
129
41.9M
      else if (0 == strcmp (*shaper_list, #shaper)) \
130
55.9M
  HB_SHAPER_PLAN (shaper);
131
27.9M
#include "hb-shaper-list.hh"
132
133
13.9M
#undef HB_SHAPER_IMPLEMENT
134
13.9M
  }
135
0
  else
136
0
  {
137
0
    const HB_UNUSED hb_shaper_entry_t *shapers = _hb_shapers_get ();
138
0
    for (unsigned int i = 0; i < HB_SHAPERS_COUNT; i++)
139
0
      if (false)
140
0
  ;
141
0
#define HB_SHAPER_IMPLEMENT(shaper) \
142
0
      else if (shapers[i].func == _hb_##shaper##_shape) \
143
0
  HB_SHAPER_PLAN (shaper);
144
0
#include "hb-shaper-list.hh"
145
0
#undef HB_SHAPER_IMPLEMENT
146
0
  }
147
0
#undef HB_SHAPER_PLAN
148
149
0
bail:
150
0
  ::hb_free (features);
151
0
  return false;
152
13.9M
}
153
154
bool
155
hb_shape_plan_key_t::user_features_match (const hb_shape_plan_key_t *other)
156
275M
{
157
275M
  if (this->num_user_features != other->num_user_features)
158
259M
    return false;
159
93.8M
  for (unsigned int i = 0; i < num_user_features; i++)
160
79.8M
  {
161
79.8M
    if (this->user_features[i].tag   != other->user_features[i].tag   ||
162
78.2M
  this->user_features[i].value != other->user_features[i].value ||
163
78.1M
  (this->user_features[i].start == HB_FEATURE_GLOBAL_START &&
164
78.1M
   this->user_features[i].end   == HB_FEATURE_GLOBAL_END) !=
165
78.1M
  (other->user_features[i].start == HB_FEATURE_GLOBAL_START &&
166
78.1M
   other->user_features[i].end   == HB_FEATURE_GLOBAL_END))
167
1.70M
      return false;
168
79.8M
  }
169
13.9M
  return true;
170
15.6M
}
171
172
bool
173
hb_shape_plan_key_t::equal (const hb_shape_plan_key_t *other)
174
5.97G
{
175
5.97G
  return hb_segment_properties_equal (&this->props, &other->props) &&
176
275M
   this->user_features_match (other) &&
177
13.9M
#ifndef HB_NO_OT_SHAPE
178
13.9M
   this->ot.equal (&other->ot) &&
179
13.9M
#endif
180
13.9M
   this->shaper_func == other->shaper_func;
181
5.97G
}
182
183
184
/*
185
 * hb_shape_plan_t
186
 */
187
188
189
/**
190
 * hb_shape_plan_create:
191
 * @face: #hb_face_t to use
192
 * @props: The #hb_segment_properties_t of the segment
193
 * @user_features: (array length=num_user_features): The list of user-selected features
194
 * @num_user_features: The number of user-selected features
195
 * @shaper_list: (array zero-terminated=1): List of shapers to try
196
 *
197
 * Constructs a shaping plan for a combination of @face, @user_features, @props,
198
 * and @shaper_list.
199
 *
200
 * Return value: (transfer full): The shaping plan
201
 *
202
 * Since: 0.9.7
203
 **/
204
hb_shape_plan_t *
205
hb_shape_plan_create (hb_face_t                     *face,
206
          const hb_segment_properties_t *props,
207
          const hb_feature_t            *user_features,
208
          unsigned int                   num_user_features,
209
          const char * const            *shaper_list)
210
0
{
211
0
  return hb_shape_plan_create2 (face, props,
212
0
        user_features, num_user_features,
213
0
        nullptr, 0,
214
0
        shaper_list);
215
0
}
216
217
/**
218
 * hb_shape_plan_create2:
219
 * @face: #hb_face_t to use
220
 * @props: The #hb_segment_properties_t of the segment
221
 * @user_features: (array length=num_user_features): The list of user-selected features
222
 * @num_user_features: The number of user-selected features
223
 * @coords: (array length=num_coords): The list of variation-space coordinates
224
 * @num_coords: The number of variation-space coordinates
225
 * @shaper_list: (array zero-terminated=1): List of shapers to try
226
 *
227
 * The variable-font version of #hb_shape_plan_create.
228
 * Constructs a shaping plan for a combination of @face, @user_features, @props,
229
 * and @shaper_list, plus the variation-space coordinates @coords.
230
 *
231
 * Return value: (transfer full): The shaping plan
232
 *
233
 * Since: 1.4.0
234
 **/
235
hb_shape_plan_t *
236
hb_shape_plan_create2 (hb_face_t                     *face,
237
           const hb_segment_properties_t *props,
238
           const hb_feature_t            *user_features,
239
           unsigned int                   num_user_features,
240
           const int                     *coords,
241
           unsigned int                   num_coords,
242
           const char * const            *shaper_list)
243
5.14k
{
244
5.14k
  DEBUG_MSG_FUNC (SHAPE_PLAN, nullptr,
245
5.14k
      "face=%p num_features=%u num_coords=%u shaper_list=%p",
246
5.14k
      face,
247
5.14k
      num_user_features,
248
5.14k
      num_coords,
249
5.14k
      shaper_list);
250
251
5.14k
  if (unlikely (!HB_DIRECTION_IS_VALID (props->direction)))
252
0
    return hb_shape_plan_get_empty ();
253
254
5.14k
  hb_shape_plan_t *shape_plan;
255
256
5.14k
  if (unlikely (!props))
257
0
    goto bail;
258
5.14k
  if (!(shape_plan = hb_object_create<hb_shape_plan_t> ()))
259
0
    goto bail;
260
261
5.14k
  if (unlikely (!face))
262
0
    face = hb_face_get_empty ();
263
5.14k
  hb_face_make_immutable (face);
264
5.14k
  shape_plan->face_unsafe = face;
265
266
5.14k
  if (unlikely (!shape_plan->key.init (true,
267
5.14k
               face,
268
5.14k
               props,
269
5.14k
               user_features,
270
5.14k
               num_user_features,
271
5.14k
               coords,
272
5.14k
               num_coords,
273
5.14k
               shaper_list)))
274
0
    goto bail2;
275
5.14k
#ifndef HB_NO_OT_SHAPE
276
5.14k
  if (unlikely (!shape_plan->ot.init0 (face, &shape_plan->key)))
277
0
    goto bail3;
278
5.14k
#endif
279
280
5.14k
  return shape_plan;
281
282
0
#ifndef HB_NO_OT_SHAPE
283
0
bail3:
284
0
#endif
285
0
  shape_plan->key.fini ();
286
0
bail2:
287
0
  hb_free (shape_plan);
288
0
bail:
289
0
  return hb_shape_plan_get_empty ();
290
0
}
291
292
/**
293
 * hb_shape_plan_get_empty:
294
 *
295
 * Fetches the singleton empty shaping plan.
296
 *
297
 * Return value: (transfer full): The empty shaping plan
298
 *
299
 * Since: 0.9.7
300
 **/
301
hb_shape_plan_t *
302
hb_shape_plan_get_empty ()
303
0
{
304
0
  return const_cast<hb_shape_plan_t *> (&Null (hb_shape_plan_t));
305
0
}
306
307
/**
308
 * hb_shape_plan_reference: (skip)
309
 * @shape_plan: A shaping plan
310
 *
311
 * Increases the reference count on the given shaping plan.
312
 *
313
 * Return value: (transfer full): @shape_plan
314
 *
315
 * Since: 0.9.7
316
 **/
317
hb_shape_plan_t *
318
hb_shape_plan_reference (hb_shape_plan_t *shape_plan)
319
13.9M
{
320
13.9M
  return hb_object_reference (shape_plan);
321
13.9M
}
322
323
/**
324
 * hb_shape_plan_destroy: (skip)
325
 * @shape_plan: A shaping plan
326
 *
327
 * Decreases the reference count on the given shaping plan. When the
328
 * reference count reaches zero, the shaping plan is destroyed,
329
 * freeing all memory.
330
 *
331
 * Since: 0.9.7
332
 **/
333
void
334
hb_shape_plan_destroy (hb_shape_plan_t *shape_plan)
335
13.9M
{
336
13.9M
  if (!hb_object_destroy (shape_plan)) return;
337
338
0
  hb_free (shape_plan);
339
0
}
340
341
/**
342
 * hb_shape_plan_set_user_data: (skip)
343
 * @shape_plan: A shaping plan
344
 * @key: The user-data key to set
345
 * @data: A pointer to the user data
346
 * @destroy: (nullable): A callback to call when @data is not needed anymore
347
 * @replace: Whether to replace an existing data with the same key
348
 *
349
 * Attaches a user-data key/data pair to the given shaping plan.
350
 *
351
 * Return value: `true` if success, `false` otherwise.
352
 *
353
 * Since: 0.9.7
354
 **/
355
hb_bool_t
356
hb_shape_plan_set_user_data (hb_shape_plan_t    *shape_plan,
357
           hb_user_data_key_t *key,
358
           void *              data,
359
           hb_destroy_func_t   destroy,
360
           hb_bool_t           replace)
361
0
{
362
0
  return hb_object_set_user_data (shape_plan, key, data, destroy, replace);
363
0
}
364
365
/**
366
 * hb_shape_plan_get_user_data: (skip)
367
 * @shape_plan: A shaping plan
368
 * @key: The user-data key to query
369
 *
370
 * Fetches the user data associated with the specified key,
371
 * attached to the specified shaping plan.
372
 *
373
 * Return value: (transfer none): A pointer to the user data
374
 *
375
 * Since: 0.9.7
376
 **/
377
void *
378
hb_shape_plan_get_user_data (const hb_shape_plan_t *shape_plan,
379
           hb_user_data_key_t    *key)
380
0
{
381
0
  return hb_object_get_user_data (shape_plan, key);
382
0
}
383
384
/**
385
 * hb_shape_plan_get_shaper:
386
 * @shape_plan: A shaping plan
387
 *
388
 * Fetches the shaper from a given shaping plan.
389
 *
390
 * Return value: (transfer none): The shaper
391
 *
392
 * Since: 0.9.7
393
 **/
394
const char *
395
hb_shape_plan_get_shaper (hb_shape_plan_t *shape_plan)
396
0
{
397
0
  return shape_plan->key.shaper_name;
398
0
}
399
400
401
static bool
402
_hb_shape_plan_execute_internal (hb_shape_plan_t    *shape_plan,
403
         hb_font_t          *font,
404
         hb_buffer_t        *buffer,
405
         const hb_feature_t *features,
406
         unsigned int        num_features)
407
13.9M
{
408
13.9M
  DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan,
409
13.9M
      "num_features=%u shaper_func=%p, shaper_name=%s",
410
13.9M
      num_features,
411
13.9M
      shape_plan->key.shaper_func,
412
13.9M
      shape_plan->key.shaper_name);
413
414
13.9M
  if (unlikely (!buffer->len))
415
0
    return true;
416
417
13.9M
  assert (!hb_object_is_immutable (buffer));
418
419
13.9M
  buffer->assert_unicode ();
420
421
13.9M
  if (unlikely (!hb_object_is_valid (shape_plan)))
422
0
    return false;
423
424
13.9M
  assert (shape_plan->face_unsafe == font->face);
425
13.9M
  assert (hb_segment_properties_equal (&shape_plan->key.props, &buffer->props));
426
427
13.9M
#define HB_SHAPER_EXECUTE(shaper) \
428
13.9M
  HB_STMT_START { \
429
13.9M
    return font->data.shaper && \
430
13.9M
     _hb_##shaper##_shape (shape_plan, font, buffer, features, num_features); \
431
13.9M
  } HB_STMT_END
432
433
13.9M
  if (false)
434
0
    ;
435
13.9M
#define HB_SHAPER_IMPLEMENT(shaper) \
436
27.9M
  else if (shape_plan->key.shaper_func == _hb_##shaper##_shape) \
437
27.9M
    HB_SHAPER_EXECUTE (shaper);
438
13.9M
#include "hb-shaper-list.hh"
439
0
#undef HB_SHAPER_IMPLEMENT
440
441
0
#undef HB_SHAPER_EXECUTE
442
443
0
  return false;
444
13.9M
}
445
/**
446
 * hb_shape_plan_execute:
447
 * @shape_plan: A shaping plan
448
 * @font: The #hb_font_t to use
449
 * @buffer: The #hb_buffer_t to work upon
450
 * @features: (array length=num_features): Features to enable
451
 * @num_features: The number of features to enable
452
 *
453
 * Executes the given shaping plan on the specified buffer, using
454
 * the given @font and @features.
455
 *
456
 * Return value: `true` if success, `false` otherwise.
457
 *
458
 * Since: 0.9.7
459
 **/
460
hb_bool_t
461
hb_shape_plan_execute (hb_shape_plan_t    *shape_plan,
462
           hb_font_t          *font,
463
           hb_buffer_t        *buffer,
464
           const hb_feature_t *features,
465
           unsigned int        num_features)
466
13.9M
{
467
13.9M
  bool ret = _hb_shape_plan_execute_internal (shape_plan, font, buffer,
468
13.9M
                features, num_features);
469
470
13.9M
  if (ret && buffer->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE)
471
0
    buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
472
473
13.9M
  return ret;
474
13.9M
}
475
476
477
/*
478
 * Caching
479
 */
480
481
/**
482
 * hb_shape_plan_create_cached:
483
 * @face: #hb_face_t to use
484
 * @props: The #hb_segment_properties_t of the segment
485
 * @user_features: (array length=num_user_features): The list of user-selected features
486
 * @num_user_features: The number of user-selected features
487
 * @shaper_list: (array zero-terminated=1): List of shapers to try
488
 *
489
 * Creates a cached shaping plan suitable for reuse, for a combination
490
 * of @face, @user_features, @props, and @shaper_list.
491
 *
492
 * Return value: (transfer full): The shaping plan
493
 *
494
 * Since: 0.9.7
495
 **/
496
hb_shape_plan_t *
497
hb_shape_plan_create_cached (hb_face_t                     *face,
498
           const hb_segment_properties_t *props,
499
           const hb_feature_t            *user_features,
500
           unsigned int                   num_user_features,
501
           const char * const            *shaper_list)
502
0
{
503
0
  return hb_shape_plan_create_cached2 (face, props,
504
0
               user_features, num_user_features,
505
0
               nullptr, 0,
506
0
               shaper_list);
507
0
}
508
509
/**
510
 * hb_shape_plan_create_cached2:
511
 * @face: #hb_face_t to use
512
 * @props: The #hb_segment_properties_t of the segment
513
 * @user_features: (array length=num_user_features): The list of user-selected features
514
 * @num_user_features: The number of user-selected features
515
 * @coords: (array length=num_coords): The list of variation-space coordinates
516
 * @num_coords: The number of variation-space coordinates
517
 * @shaper_list: (array zero-terminated=1): List of shapers to try
518
 *
519
 * The variable-font version of #hb_shape_plan_create_cached.
520
 * Creates a cached shaping plan suitable for reuse, for a combination
521
 * of @face, @user_features, @props, and @shaper_list, plus the
522
 * variation-space coordinates @coords.
523
 *
524
 * Return value: (transfer full): The shaping plan
525
 *
526
 * Since: 1.4.0
527
 **/
528
hb_shape_plan_t *
529
hb_shape_plan_create_cached2 (hb_face_t                     *face,
530
            const hb_segment_properties_t *props,
531
            const hb_feature_t            *user_features,
532
            unsigned int                   num_user_features,
533
            const int                     *coords,
534
            unsigned int                   num_coords,
535
            const char * const            *shaper_list)
536
13.9M
{
537
13.9M
  DEBUG_MSG_FUNC (SHAPE_PLAN, nullptr,
538
13.9M
      "face=%p num_features=%u shaper_list=%p",
539
13.9M
      face,
540
13.9M
      num_user_features,
541
13.9M
      shaper_list);
542
543
13.9M
retry:
544
13.9M
  hb_face_t::plan_node_t *cached_plan_nodes = face->shape_plans;
545
546
13.9M
  bool dont_cache = !hb_object_is_valid (face);
547
548
13.9M
  if (likely (!dont_cache))
549
13.9M
  {
550
13.9M
    hb_shape_plan_key_t key;
551
13.9M
    if (!key.init (false,
552
13.9M
       face,
553
13.9M
       props,
554
13.9M
       user_features,
555
13.9M
       num_user_features,
556
13.9M
       coords,
557
13.9M
       num_coords,
558
13.9M
       shaper_list))
559
0
      return hb_shape_plan_get_empty ();
560
561
5.97G
    for (hb_face_t::plan_node_t *node = cached_plan_nodes; node; node = node->next)
562
5.97G
      if (node->shape_plan->key.equal (&key))
563
13.9M
      {
564
13.9M
  DEBUG_MSG_FUNC (SHAPE_PLAN, node->shape_plan, "fulfilled from cache");
565
13.9M
  return hb_shape_plan_reference (node->shape_plan);
566
13.9M
      }
567
13.9M
  }
568
569
5.14k
  hb_shape_plan_t *shape_plan = hb_shape_plan_create2 (face, props,
570
5.14k
                   user_features, num_user_features,
571
5.14k
                   coords, num_coords,
572
5.14k
                   shaper_list);
573
574
5.14k
  if (unlikely (dont_cache))
575
0
    return shape_plan;
576
577
5.14k
  hb_face_t::plan_node_t *node = (hb_face_t::plan_node_t *) hb_calloc (1, sizeof (hb_face_t::plan_node_t));
578
5.14k
  if (unlikely (!node))
579
0
    return shape_plan;
580
581
5.14k
  node->shape_plan = shape_plan;
582
5.14k
  node->next = cached_plan_nodes;
583
584
5.14k
  if (unlikely (!face->shape_plans.cmpexch (cached_plan_nodes, node)))
585
0
  {
586
0
    hb_shape_plan_destroy (shape_plan);
587
0
    hb_free (node);
588
0
    goto retry;
589
0
  }
590
5.14k
  DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan, "inserted into cache");
591
592
5.14k
  return hb_shape_plan_reference (shape_plan);
593
5.14k
}
594
595
596
#endif