Coverage Report

Created: 2026-08-31 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/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
1.85k
{
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
1.85k
  key.fini ();
67
1.85k
}
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
106k
{
84
106k
  hb_feature_t *features = nullptr;
85
106k
  if (copy && num_user_features && !(features = (hb_feature_t *) hb_calloc (num_user_features, sizeof (hb_feature_t))))
86
0
    goto bail;
87
88
106k
  this->props = *props;
89
106k
  this->num_user_features = num_user_features;
90
106k
  this->user_features = copy ? features : user_features;
91
106k
  if (copy && num_user_features)
92
1.85k
  {
93
1.85k
    hb_memcpy (features, user_features, num_user_features * sizeof (hb_feature_t));
94
    /* Make start/end uniform to easier catch bugs. */
95
11.1k
    for (unsigned int i = 0; i < num_user_features; i++)
96
9.27k
    {
97
9.27k
      if (features[0].start != HB_FEATURE_GLOBAL_START)
98
0
  features[0].start = 1;
99
9.27k
      if (features[0].end   != HB_FEATURE_GLOBAL_END)
100
0
  features[0].end   = 2;
101
9.27k
    }
102
1.85k
  }
103
106k
  this->shaper_func = nullptr;
104
106k
  this->shaper_name = nullptr;
105
106k
#ifndef HB_NO_OT_SHAPE
106
106k
  this->ot.init (face, coords, num_coords);
107
106k
#endif
108
109
  /*
110
   * Choose shaper.
111
   */
112
113
106k
#define HB_SHAPER_PLAN(shaper) \
114
106k
  HB_STMT_START { \
115
106k
    if (face->data.shaper) \
116
106k
    { \
117
106k
      this->shaper_func = _hb_##shaper##_shape; \
118
106k
      this->shaper_name = #shaper; \
119
106k
      return true; \
120
106k
    } \
121
106k
  } HB_STMT_END
122
123
106k
  if (unlikely (shaper_list))
124
0
  {
125
0
    for (; *shaper_list; shaper_list++)
126
0
      if (false)
127
0
  ;
128
0
#define HB_SHAPER_IMPLEMENT(shaper) \
129
0
      else if (0 == strcmp (*shaper_list, #shaper)) \
130
0
  HB_SHAPER_PLAN (shaper);
131
0
#include "hb-shaper-list.hh"
132
133
0
#undef HB_SHAPER_IMPLEMENT
134
0
  }
135
106k
  else
136
106k
  {
137
106k
    const HB_UNUSED hb_shaper_entry_t *shapers = _hb_shapers_get ();
138
106k
    for (unsigned int i = 0; i < HB_SHAPERS_COUNT; i++)
139
106k
      if (false)
140
0
  ;
141
106k
#define HB_SHAPER_IMPLEMENT(shaper) \
142
106k
      else if (shapers[i].func == _hb_##shaper##_shape) \
143
106k
  HB_SHAPER_PLAN (shaper);
144
106k
#include "hb-shaper-list.hh"
145
106k
#undef HB_SHAPER_IMPLEMENT
146
106k
  }
147
0
#undef HB_SHAPER_PLAN
148
149
0
bail:
150
0
  ::hb_free (features);
151
0
  return false;
152
106k
}
153
154
bool
155
hb_shape_plan_key_t::user_features_match (const hb_shape_plan_key_t *other)
156
103k
{
157
103k
  if (this->num_user_features != other->num_user_features)
158
0
    return false;
159
618k
  for (unsigned int i = 0; i < num_user_features; i++)
160
515k
  {
161
515k
    if (this->user_features[i].tag   != other->user_features[i].tag   ||
162
515k
  this->user_features[i].value != other->user_features[i].value ||
163
515k
  (this->user_features[i].start == HB_FEATURE_GLOBAL_START &&
164
515k
   this->user_features[i].end   == HB_FEATURE_GLOBAL_END) !=
165
515k
  (other->user_features[i].start == HB_FEATURE_GLOBAL_START &&
166
515k
   other->user_features[i].end   == HB_FEATURE_GLOBAL_END))
167
0
      return false;
168
515k
  }
169
103k
  return true;
170
103k
}
171
172
bool
173
hb_shape_plan_key_t::equal (const hb_shape_plan_key_t *other)
174
155k
{
175
155k
  return hb_segment_properties_equal (&this->props, &other->props) &&
176
103k
   this->user_features_match (other) &&
177
103k
#ifndef HB_NO_OT_SHAPE
178
103k
   this->ot.equal (&other->ot) &&
179
103k
#endif
180
103k
   this->shaper_func == other->shaper_func;
181
155k
}
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
1.85k
{
244
1.85k
  DEBUG_MSG_FUNC (SHAPE_PLAN, nullptr,
245
1.85k
      "face=%p num_features=%u num_coords=%u shaper_list=%p",
246
1.85k
      face,
247
1.85k
      num_user_features,
248
1.85k
      num_coords,
249
1.85k
      shaper_list);
250
251
1.85k
  if (unlikely (!HB_DIRECTION_IS_VALID (props->direction)))
252
0
    return hb_shape_plan_get_empty ();
253
254
1.85k
  hb_shape_plan_t *shape_plan;
255
256
1.85k
  if (unlikely (!props))
257
0
    goto bail;
258
1.85k
  if (!(shape_plan = hb_object_create<hb_shape_plan_t> ()))
259
0
    goto bail;
260
261
1.85k
  if (unlikely (!face))
262
0
    face = hb_face_get_empty ();
263
1.85k
  hb_face_make_immutable (face);
264
1.85k
  shape_plan->face_unsafe = face;
265
266
1.85k
  if (unlikely (!shape_plan->key.init (true,
267
1.85k
               face,
268
1.85k
               props,
269
1.85k
               user_features,
270
1.85k
               num_user_features,
271
1.85k
               coords,
272
1.85k
               num_coords,
273
1.85k
               shaper_list)))
274
0
    goto bail2;
275
1.85k
#ifndef HB_NO_OT_SHAPE
276
1.85k
  if (unlikely (!shape_plan->ot.init0 (face, &shape_plan->key)))
277
0
    goto bail3;
278
1.85k
#endif
279
280
1.85k
  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
104k
{
320
104k
  return hb_object_reference (shape_plan);
321
104k
}
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
106k
{
336
106k
  if (!hb_object_destroy (shape_plan)) return;
337
338
1.85k
  hb_free (shape_plan);
339
1.85k
}
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
104k
{
408
104k
  DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan,
409
104k
      "num_features=%u shaper_func=%p, shaper_name=%s",
410
104k
      num_features,
411
104k
      shape_plan->key.shaper_func,
412
104k
      shape_plan->key.shaper_name);
413
414
104k
  if (unlikely (!buffer->len))
415
0
    return true;
416
417
104k
  assert (!hb_object_is_immutable (buffer));
418
419
104k
  buffer->assert_unicode ();
420
421
104k
  if (unlikely (!hb_object_is_valid (shape_plan)))
422
0
    return false;
423
424
104k
  assert (shape_plan->face_unsafe == font->face);
425
104k
  assert (hb_segment_properties_equal (&shape_plan->key.props, &buffer->props));
426
427
104k
#define HB_SHAPER_EXECUTE(shaper) \
428
104k
  HB_STMT_START { \
429
104k
    return font->data.shaper && \
430
104k
     _hb_##shaper##_shape (shape_plan, font, buffer, features, num_features); \
431
104k
  } HB_STMT_END
432
433
104k
  if (false)
434
0
    ;
435
104k
#define HB_SHAPER_IMPLEMENT(shaper) \
436
104k
  else if (shape_plan->key.shaper_func == _hb_##shaper##_shape) \
437
104k
    HB_SHAPER_EXECUTE (shaper);
438
104k
#include "hb-shaper-list.hh"
439
0
#undef HB_SHAPER_IMPLEMENT
440
441
0
#undef HB_SHAPER_EXECUTE
442
443
0
  return false;
444
104k
}
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
104k
{
467
104k
  bool ret = _hb_shape_plan_execute_internal (shape_plan, font, buffer,
468
104k
                features, num_features);
469
470
104k
  if (ret && buffer->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE)
471
0
    buffer->content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
472
473
104k
  return ret;
474
104k
}
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
104k
{
537
104k
  DEBUG_MSG_FUNC (SHAPE_PLAN, nullptr,
538
104k
      "face=%p num_features=%u shaper_list=%p",
539
104k
      face,
540
104k
      num_user_features,
541
104k
      shaper_list);
542
543
104k
retry:
544
104k
  hb_face_t::plan_node_t *cached_plan_nodes = face->shape_plans;
545
546
104k
  bool dont_cache = !hb_object_is_valid (face);
547
548
104k
  if (likely (!dont_cache))
549
104k
  {
550
104k
    hb_shape_plan_key_t key;
551
104k
    if (!key.init (false,
552
104k
       face,
553
104k
       props,
554
104k
       user_features,
555
104k
       num_user_features,
556
104k
       coords,
557
104k
       num_coords,
558
104k
       shaper_list))
559
0
      return hb_shape_plan_get_empty ();
560
561
156k
    for (hb_face_t::plan_node_t *node = cached_plan_nodes; node; node = node->next)
562
155k
      if (node->shape_plan->key.equal (&key))
563
103k
      {
564
103k
  DEBUG_MSG_FUNC (SHAPE_PLAN, node->shape_plan, "fulfilled from cache");
565
103k
  return hb_shape_plan_reference (node->shape_plan);
566
103k
      }
567
104k
  }
568
569
1.85k
  hb_shape_plan_t *shape_plan = hb_shape_plan_create2 (face, props,
570
1.85k
                   user_features, num_user_features,
571
1.85k
                   coords, num_coords,
572
1.85k
                   shaper_list);
573
574
1.85k
  if (unlikely (dont_cache))
575
0
    return shape_plan;
576
577
1.85k
  hb_face_t::plan_node_t *node = (hb_face_t::plan_node_t *) hb_calloc (1, sizeof (hb_face_t::plan_node_t));
578
1.85k
  if (unlikely (!node))
579
0
    return shape_plan;
580
581
1.85k
  node->shape_plan = shape_plan;
582
1.85k
  node->next = cached_plan_nodes;
583
584
1.85k
  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
1.85k
  DEBUG_MSG_FUNC (SHAPE_PLAN, shape_plan, "inserted into cache");
591
592
1.85k
  return hb_shape_plan_reference (shape_plan);
593
1.85k
}
594
595
596
#endif