Coverage Report

Created: 2026-08-31 07:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/harfbuzz/src/hb-buffer.cc
Line
Count
Source
1
/*
2
 * Copyright © 1998-2004  David Turner and Werner Lemberg
3
 * Copyright © 2004,2007,2009,2010  Red Hat, Inc.
4
 * Copyright © 2011,2012  Google, Inc.
5
 *
6
 *  This is part of HarfBuzz, a text shaping library.
7
 *
8
 * Permission is hereby granted, without written agreement and without
9
 * license or royalty fees, to use, copy, modify, and distribute this
10
 * software and its documentation for any purpose, provided that the
11
 * above copyright notice and the following two paragraphs appear in
12
 * all copies of this software.
13
 *
14
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
15
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
17
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18
 * DAMAGE.
19
 *
20
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
21
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
23
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
24
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25
 *
26
 * Red Hat Author(s): Owen Taylor, Behdad Esfahbod
27
 * Google Author(s): Behdad Esfahbod
28
 */
29
30
#include "hb-buffer.hh"
31
#include "hb-utf.hh"
32
33
34
/**
35
 * SECTION: hb-buffer
36
 * @title: hb-buffer
37
 * @short_description: Input and output buffers
38
 * @include: hb.h
39
 *
40
 * Buffers serve a dual role in HarfBuzz; before shaping, they hold
41
 * the input characters that are passed to hb_shape(), and after
42
 * shaping they hold the output glyphs.
43
 *
44
 * The input buffer is a sequence of Unicode codepoints, with
45
 * associated attributes such as direction and script.  The output
46
 * buffer is a sequence of glyphs, with associated attributes such
47
 * as position and cluster.
48
 **/
49
50
51
/**
52
 * hb_segment_properties_equal:
53
 * @a: first #hb_segment_properties_t to compare.
54
 * @b: second #hb_segment_properties_t to compare.
55
 *
56
 * Checks the equality of two #hb_segment_properties_t's.
57
 *
58
 * Return value:
59
 * `true` if all properties of @a equal those of @b, `false` otherwise.
60
 *
61
 * Since: 0.9.7
62
 **/
63
hb_bool_t
64
hb_segment_properties_equal (const hb_segment_properties_t *a,
65
           const hb_segment_properties_t *b)
66
260k
{
67
260k
  return a->direction == b->direction &&
68
260k
   a->script    == b->script    &&
69
207k
   a->language  == b->language  &&
70
207k
   a->reserved1 == b->reserved1 &&
71
207k
   a->reserved2 == b->reserved2;
72
73
260k
}
74
75
/**
76
 * hb_segment_properties_hash:
77
 * @p: #hb_segment_properties_t to hash.
78
 *
79
 * Creates a hash representing @p.
80
 *
81
 * Return value:
82
 * A hash of @p.
83
 *
84
 * Since: 0.9.7
85
 **/
86
unsigned int
87
hb_segment_properties_hash (const hb_segment_properties_t *p)
88
0
{
89
0
  return ((unsigned int) p->direction * 31 +
90
0
    (unsigned int) p->script) * 31 +
91
0
   (intptr_t) (p->language);
92
0
}
93
94
/**
95
 * hb_segment_properties_overlay:
96
 * @p: #hb_segment_properties_t to fill in.
97
 * @src: #hb_segment_properties_t to fill in from.
98
 *
99
 * Fills in missing fields of @p from @src in a considered manner.
100
 *
101
 * First, if @p does not have direction set, direction is copied from @src.
102
 *
103
 * Next, if @p and @src have the same direction (which can be unset), if @p
104
 * does not have script set, script is copied from @src.
105
 *
106
 * Finally, if @p and @src have the same direction and script (which either
107
 * can be unset), if @p does not have language set, language is copied from
108
 * @src.
109
 *
110
 * Since: 3.3.0
111
 **/
112
void
113
hb_segment_properties_overlay (hb_segment_properties_t *p,
114
             const hb_segment_properties_t *src)
115
0
{
116
0
  if (unlikely (!p || !src))
117
0
    return;
118
119
0
  if (!p->direction)
120
0
    p->direction = src->direction;
121
122
0
  if (p->direction != src->direction)
123
0
    return;
124
125
0
  if (!p->script)
126
0
    p->script = src->script;
127
128
0
  if (p->script != src->script)
129
0
    return;
130
131
0
  if (!p->language)
132
0
    p->language = src->language;
133
0
}
134
135
/* Here is how the buffer works internally:
136
 *
137
 * There are two info pointers: info and out_info.  They always have
138
 * the same allocated size, but different lengths.
139
 *
140
 * As an optimization, both info and out_info may point to the
141
 * same piece of memory, which is owned by info.  This remains the
142
 * case as long as out_len doesn't exceed i at any time.
143
 * In that case, sync() is mostly no-op and the glyph operations
144
 * operate mostly in-place.
145
 *
146
 * As soon as out_info gets longer than info, out_info is moved over
147
 * to an alternate buffer (which we reuse the pos buffer for), and its
148
 * current contents (out_len entries) are copied to the new place.
149
 *
150
 * This should all remain transparent to the user.  sync() then
151
 * switches info over to out_info and does housekeeping.
152
 */
153
154
155
156
/* Internal API */
157
158
bool
159
hb_buffer_t::enlarge (unsigned int size)
160
1.53k
{
161
1.53k
  if (unlikely (size > max_len))
162
0
  {
163
0
    successful = false;
164
0
    return false;
165
0
  }
166
167
1.53k
  if (unlikely (!successful))
168
0
    return false;
169
170
1.53k
  unsigned int new_allocated = allocated;
171
1.53k
  hb_glyph_position_t *new_pos = nullptr;
172
1.53k
  hb_glyph_info_t *new_info = nullptr;
173
1.53k
  bool separate_out = out_info != info;
174
175
1.53k
  if (unlikely (hb_unsigned_mul_overflows (size, sizeof (info[0]))))
176
0
    goto done;
177
178
3.08k
  while (size >= new_allocated)
179
1.55k
    new_allocated += (new_allocated >> 1) + 32;
180
181
1.53k
  unsigned new_bytes;
182
1.53k
  if (unlikely (hb_unsigned_mul_overflows (new_allocated, sizeof (info[0]), &new_bytes)))
183
0
    goto done;
184
185
1.53k
  static_assert (sizeof (info[0]) == sizeof (pos[0]), "");
186
1.53k
  new_pos = (hb_glyph_position_t *) hb_realloc (pos, new_bytes);
187
1.53k
  new_info = (hb_glyph_info_t *) hb_realloc (info, new_bytes);
188
189
1.53k
done:
190
1.53k
  if (unlikely (!new_pos || !new_info))
191
0
    successful = false;
192
193
1.53k
  if (likely (new_pos))
194
1.53k
    pos = new_pos;
195
196
1.53k
  if (likely (new_info))
197
1.53k
    info = new_info;
198
199
1.53k
  out_info = separate_out ? (hb_glyph_info_t *) pos : info;
200
1.53k
  if (likely (successful))
201
1.53k
    allocated = new_allocated;
202
203
1.53k
  return likely (successful);
204
1.53k
}
205
206
bool
207
hb_buffer_t::make_room_for (unsigned int num_in,
208
          unsigned int num_out)
209
0
{
210
0
  if (unlikely (!ensure (out_len + num_out))) return false;
211
212
0
  if (out_info == info &&
213
0
      out_len + num_out > idx + num_in)
214
0
  {
215
0
    assert (have_output);
216
217
0
    out_info = (hb_glyph_info_t *) pos;
218
0
    hb_memcpy (out_info, info, out_len * sizeof (out_info[0]));
219
0
  }
220
221
0
  return true;
222
0
}
223
224
bool
225
hb_buffer_t::shift_forward (unsigned int count)
226
0
{
227
0
  assert (have_output);
228
0
  if (unlikely (!ensure (len + count))) return false;
229
230
0
  max_ops -= len - idx;
231
0
  if (unlikely (max_ops < 0))
232
0
  {
233
0
    successful = false;
234
0
    return false;
235
0
  }
236
237
0
  memmove (info + idx + count, info + idx, (len - idx) * sizeof (info[0]));
238
0
  if (idx + count > len)
239
0
  {
240
    /* Under memory failure we might expose this area.  At least
241
     * clean it up.  Oh well...
242
     *
243
     * Ideally, we should at least set Default_Ignorable bits on
244
     * these, as well as consistent cluster values.  But the former
245
     * is layering violation... */
246
0
    hb_memset (info + len, 0, (idx + count - len) * sizeof (info[0]));
247
0
  }
248
0
  len += count;
249
0
  idx += count;
250
251
0
  return true;
252
0
}
253
254
hb_buffer_t::scratch_buffer_t *
255
hb_buffer_t::get_scratch_buffer (unsigned int *size)
256
0
{
257
0
  have_output = false;
258
0
  have_positions = false;
259
260
0
  out_len = 0;
261
0
  out_info = info;
262
263
0
  assert ((uintptr_t) pos % sizeof (scratch_buffer_t) == 0);
264
0
  *size = allocated * sizeof (pos[0]) / sizeof (scratch_buffer_t);
265
0
  return (scratch_buffer_t *) (void *) pos;
266
0
}
267
268
269
270
/* HarfBuzz-Internal API */
271
272
void
273
hb_buffer_t::similar (const hb_buffer_t &src)
274
0
{
275
0
  hb_unicode_funcs_destroy (unicode);
276
0
  unicode = hb_unicode_funcs_reference (src.unicode);
277
0
  flags = src.flags;
278
0
  cluster_level = src.cluster_level;
279
0
  replacement = src.replacement;
280
0
  invisible = src.invisible;
281
0
  not_found = src.not_found;
282
0
  not_found_variation_selector = src.not_found_variation_selector;
283
0
}
284
285
void
286
hb_buffer_t::reset ()
287
117k
{
288
117k
  hb_unicode_funcs_destroy (unicode);
289
117k
  unicode = hb_unicode_funcs_reference (hb_unicode_funcs_get_default ());
290
117k
  flags = HB_BUFFER_FLAG_DEFAULT;
291
117k
  cluster_level = HB_BUFFER_CLUSTER_LEVEL_DEFAULT;
292
117k
  replacement = HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT;
293
117k
  invisible = 0;
294
117k
  not_found = 0;
295
117k
  not_found_variation_selector = HB_CODEPOINT_INVALID;
296
297
117k
  clear ();
298
117k
}
299
300
void
301
hb_buffer_t::clear ()
302
117k
{
303
117k
  content_type = HB_BUFFER_CONTENT_TYPE_INVALID;
304
117k
  hb_segment_properties_t default_props = HB_SEGMENT_PROPERTIES_DEFAULT;
305
117k
  props = default_props;
306
307
117k
  successful = true;
308
117k
  have_output = false;
309
117k
  have_positions = false;
310
311
117k
  idx = 0;
312
117k
  len = 0;
313
117k
  out_len = 0;
314
117k
  out_info = info;
315
316
117k
  hb_memset (context, 0, sizeof context);
317
117k
  hb_memset (context_len, 0, sizeof context_len);
318
319
117k
  deallocate_var_all ();
320
117k
  serial = 0;
321
117k
  random_state = 1;
322
117k
  scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;
323
117k
}
324
325
void
326
hb_buffer_t::enter ()
327
104k
{
328
104k
  deallocate_var_all ();
329
104k
  serial = 0;
330
104k
  scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;
331
104k
  unsigned mul;
332
104k
  if (likely (!hb_unsigned_mul_overflows (len, HB_BUFFER_MAX_LEN_FACTOR, &mul)))
333
104k
  {
334
104k
    max_len = hb_max (mul, (unsigned) HB_BUFFER_MAX_LEN_MIN);
335
104k
  }
336
104k
  if (likely (!hb_unsigned_mul_overflows (len, HB_BUFFER_MAX_OPS_FACTOR, &mul)))
337
104k
  {
338
104k
    max_ops = hb_max (mul, (unsigned) HB_BUFFER_MAX_OPS_MIN);
339
104k
  }
340
104k
}
341
void
342
hb_buffer_t::leave ()
343
104k
{
344
104k
  max_len = HB_BUFFER_MAX_LEN_DEFAULT;
345
104k
  max_ops = HB_BUFFER_MAX_OPS_DEFAULT;
346
104k
  deallocate_var_all ();
347
104k
  serial = 0;
348
104k
}
349
350
351
void
352
hb_buffer_t::add (hb_codepoint_t  codepoint,
353
      unsigned int    cluster)
354
635k
{
355
635k
  hb_glyph_info_t *glyph;
356
357
635k
  if (unlikely (!ensure (len + 1))) return;
358
359
635k
  glyph = &info[len];
360
361
635k
  hb_memset (glyph, 0, sizeof (*glyph));
362
635k
  glyph->codepoint = codepoint;
363
635k
  glyph->mask = 0;
364
635k
  glyph->cluster = cluster;
365
366
635k
  len++;
367
635k
}
368
369
void
370
hb_buffer_t::add_info (const hb_glyph_info_t &glyph_info)
371
0
{
372
0
  if (unlikely (!ensure (len + 1))) return;
373
374
0
  info[len] = glyph_info;
375
376
0
  len++;
377
0
}
378
void
379
hb_buffer_t::add_info_and_pos (const hb_glyph_info_t &glyph_info,
380
             const hb_glyph_position_t &glyph_pos)
381
0
{
382
0
  if (unlikely (!ensure (len + 1))) return;
383
384
0
  info[len] = glyph_info;
385
0
  assert (have_positions);
386
0
  pos[len] = glyph_pos;
387
388
0
  len++;
389
0
}
390
391
392
void
393
hb_buffer_t::clear_output ()
394
104k
{
395
104k
  have_output = true;
396
104k
  have_positions = false;
397
398
104k
  idx = 0;
399
104k
  out_len = 0;
400
104k
  out_info = info;
401
104k
}
402
403
void
404
hb_buffer_t::clear_positions ()
405
104k
{
406
104k
  have_output = false;
407
104k
  have_positions = true;
408
409
104k
  out_len = 0;
410
104k
  out_info = info;
411
412
104k
  hb_memset (pos, 0, sizeof (pos[0]) * len);
413
104k
}
414
415
bool
416
hb_buffer_t::sync ()
417
104k
{
418
104k
  bool ret = false;
419
420
104k
  assert (have_output);
421
422
104k
  assert (idx <= len);
423
424
104k
  if (unlikely (!successful || !next_glyphs (len - idx)))
425
0
    goto reset;
426
427
104k
  if (out_info != info)
428
0
  {
429
0
    pos = (hb_glyph_position_t *) info;
430
0
    info = out_info;
431
0
  }
432
104k
  len = out_len;
433
104k
  ret = true;
434
435
104k
reset:
436
104k
  have_output = false;
437
104k
  out_len = 0;
438
104k
  out_info = info;
439
104k
  idx = 0;
440
441
104k
  return ret;
442
104k
}
443
444
int
445
hb_buffer_t::sync_so_far ()
446
0
{
447
0
  bool had_output = have_output;
448
0
  unsigned out_i = out_len;
449
0
  unsigned i = idx;
450
0
  unsigned old_idx = idx;
451
452
0
  if (sync ())
453
0
    idx = out_i;
454
0
  else
455
0
    idx = i;
456
457
0
  if (had_output)
458
0
  {
459
0
    have_output = true;
460
0
    out_len = idx;
461
0
  }
462
463
0
  assert (idx <= len);
464
465
0
  return idx - old_idx;
466
0
}
467
468
bool
469
hb_buffer_t::move_to (unsigned int i)
470
0
{
471
0
  if (!have_output)
472
0
  {
473
0
    assert (i <= len);
474
0
    idx = i;
475
0
    return true;
476
0
  }
477
0
  if (unlikely (!successful))
478
0
    return false;
479
480
0
  assert (i <= out_len + (len - idx));
481
482
0
  if (out_len < i)
483
0
  {
484
0
    unsigned int count = i - out_len;
485
0
    if (unlikely (!make_room_for (count, count))) return false;
486
487
0
    max_ops -= count;
488
0
    if (unlikely (max_ops < 0))
489
0
    {
490
0
      successful = false;
491
0
      return false;
492
0
    }
493
494
0
    memmove (out_info + out_len, info + idx, count * sizeof (out_info[0]));
495
0
    idx += count;
496
0
    out_len += count;
497
0
  }
498
0
  else if (out_len > i)
499
0
  {
500
    /* Tricky part: rewinding... */
501
0
    unsigned int count = out_len - i;
502
503
    /* This will blow in our face if memory allocation fails later
504
     * in this same lookup...
505
     *
506
     * We used to shift with extra 32 items.
507
     * But that would leave empty slots in the buffer in case of allocation
508
     * failures.  See comments in shift_forward().  This can cause O(N^2)
509
     * behavior more severely than adding 32 empty slots can... */
510
0
    if (unlikely (idx < count && !shift_forward (count - idx))) return false;
511
512
0
    assert (idx >= count);
513
514
0
    max_ops -= count;
515
0
    if (unlikely (max_ops < 0))
516
0
    {
517
0
      successful = false;
518
0
      return false;
519
0
    }
520
521
0
    idx -= count;
522
0
    out_len -= count;
523
0
    memmove (info + idx, out_info + out_len, count * sizeof (out_info[0]));
524
0
  }
525
526
0
  return true;
527
0
}
528
529
530
void
531
hb_buffer_t::set_masks (hb_mask_t    value,
532
      hb_mask_t    mask,
533
      unsigned int cluster_start,
534
      unsigned int cluster_end)
535
0
{
536
0
  if (!mask)
537
0
    return;
538
539
0
  hb_mask_t not_mask = ~mask;
540
0
  value &= mask;
541
542
0
  max_ops -= len;
543
0
  if (unlikely (max_ops < 0))
544
0
    successful = false;
545
546
0
  unsigned int count = len;
547
548
0
  if (cluster_start == 0 && cluster_end == (unsigned int) -1)
549
0
  {
550
0
    for (unsigned int i = 0; i < count; i++)
551
0
      info[i].mask = (info[i].mask & not_mask) | value;
552
0
    return;
553
0
  }
554
555
0
  for (unsigned int i = 0; i < count; i++)
556
0
    if (cluster_start <= info[i].cluster && info[i].cluster < cluster_end)
557
0
      info[i].mask = (info[i].mask & not_mask) | value;
558
0
}
559
560
void
561
hb_buffer_t::merge_clusters_impl (unsigned int start,
562
          unsigned int end)
563
101
{
564
101
  max_ops -= end - start;
565
101
  if (unlikely (max_ops < 0))
566
0
    successful = false;
567
568
101
  unsigned int cluster = info[start].cluster;
569
570
202
  for (unsigned int i = start + 1; i < end; i++)
571
101
    cluster = hb_min (cluster, info[i].cluster);
572
573
  /* Extend end */
574
101
  if (cluster != info[end - 1].cluster)
575
101
    while (end < len && info[end - 1].cluster == info[end].cluster)
576
0
      end++;
577
578
  /* Extend start */
579
101
  if (cluster != info[start].cluster)
580
0
    while (idx < start && info[start - 1].cluster == info[start].cluster)
581
0
      start--;
582
583
  /* If we hit the start of buffer, continue in out-buffer. */
584
101
  if (idx == start && info[start].cluster != cluster)
585
0
    for (unsigned int i = out_len; i && out_info[i - 1].cluster == info[start].cluster; i--)
586
0
      set_cluster (out_info[i - 1], cluster);
587
588
303
  for (unsigned int i = start; i < end; i++)
589
202
    set_cluster (info[i], cluster);
590
101
}
591
void
592
hb_buffer_t::merge_out_clusters_impl (unsigned int start,
593
              unsigned int end)
594
0
{
595
0
  max_ops -= end - start;
596
0
  if (unlikely (max_ops < 0))
597
0
    successful = false;
598
599
0
  unsigned int cluster = out_info[start].cluster;
600
601
0
  for (unsigned int i = start + 1; i < end; i++)
602
0
    cluster = hb_min (cluster, out_info[i].cluster);
603
604
  /* Extend start */
605
0
  while (start && out_info[start - 1].cluster == out_info[start].cluster)
606
0
    start--;
607
608
  /* Extend end */
609
0
  while (end < out_len && out_info[end - 1].cluster == out_info[end].cluster)
610
0
    end++;
611
612
  /* If we hit the end of out-buffer, continue in buffer. */
613
0
  if (end == out_len)
614
0
    for (unsigned int i = idx; i < len && info[i].cluster == out_info[end - 1].cluster; i++)
615
0
      set_cluster (info[i], cluster);
616
617
0
  for (unsigned int i = start; i < end; i++)
618
0
    set_cluster (out_info[i], cluster);
619
0
}
620
void
621
hb_buffer_t::delete_glyph ()
622
0
{
623
  /* The logic here is duplicated in hb_ot_hide_default_ignorables(). */
624
625
0
  unsigned int cluster = info[idx].cluster;
626
0
  if ((idx + 1 < len && cluster == info[idx + 1].cluster) ||
627
0
      (out_len && cluster == out_info[out_len - 1].cluster))
628
0
  {
629
    /* Cluster survives; do nothing. */
630
0
    goto done;
631
0
  }
632
633
0
  if (out_len)
634
0
  {
635
    /* Merge cluster backward. */
636
0
    if (cluster < out_info[out_len - 1].cluster)
637
0
    {
638
0
      unsigned int mask = info[idx].mask;
639
0
      unsigned int old_cluster = out_info[out_len - 1].cluster;
640
0
      for (unsigned i = out_len; i && out_info[i - 1].cluster == old_cluster; i--)
641
0
  set_cluster (out_info[i - 1], cluster, mask);
642
0
    }
643
0
    goto done;
644
0
  }
645
646
0
  if (idx + 1 < len)
647
0
  {
648
    /* Merge cluster forward. */
649
0
    merge_clusters (idx, idx + 2);
650
0
    goto done;
651
0
  }
652
653
0
done:
654
0
  skip_glyph ();
655
0
}
656
657
void
658
hb_buffer_t::delete_glyphs_inplace (bool (*filter) (const hb_glyph_info_t *info))
659
0
{
660
  /* Merge clusters and delete filtered glyphs.
661
   * NOTE! We can't use out-buffer as we have positioning data. */
662
0
  unsigned int j = 0;
663
0
  unsigned int count = len;
664
0
  for (unsigned int i = 0; i < count; i++)
665
0
  {
666
0
    if (filter (&info[i]))
667
0
    {
668
      /* Merge clusters.
669
       * Same logic as delete_glyph(), but for in-place removal. */
670
671
0
      unsigned int cluster = info[i].cluster;
672
0
      if (i + 1 < count && cluster == info[i + 1].cluster)
673
0
  continue; /* Cluster survives; do nothing. */
674
675
0
      if (j)
676
0
      {
677
  /* Merge cluster backward. */
678
0
  if (cluster < info[j - 1].cluster)
679
0
  {
680
0
    unsigned int mask = info[i].mask;
681
0
    unsigned int old_cluster = info[j - 1].cluster;
682
0
    for (unsigned k = j; k && info[k - 1].cluster == old_cluster; k--)
683
0
      set_cluster (info[k - 1], cluster, mask);
684
0
  }
685
0
  continue;
686
0
      }
687
688
0
      if (i + 1 < count)
689
0
  merge_clusters (i, i + 2); /* Merge cluster forward. */
690
691
0
      continue;
692
0
    }
693
694
0
    if (j != i)
695
0
    {
696
0
      info[j] = info[i];
697
0
      pos[j] = pos[i];
698
0
    }
699
0
    j++;
700
0
  }
701
0
  len = j;
702
0
}
703
704
void
705
hb_buffer_t::guess_segment_properties ()
706
0
{
707
0
  assert_unicode ();
708
709
  /* If script is set to INVALID, guess from buffer contents */
710
0
  if (props.script == HB_SCRIPT_INVALID) {
711
0
    for (unsigned int i = 0; i < len; i++) {
712
0
      hb_script_t script = unicode->script (info[i].codepoint);
713
0
      if (likely (script != HB_SCRIPT_COMMON &&
714
0
      script != HB_SCRIPT_INHERITED &&
715
0
      script != HB_SCRIPT_UNKNOWN)) {
716
0
  props.script = script;
717
0
  break;
718
0
      }
719
0
    }
720
0
  }
721
722
  /* If direction is set to INVALID, guess from script */
723
0
  if (props.direction == HB_DIRECTION_INVALID) {
724
0
    props.direction = hb_script_get_horizontal_direction (props.script);
725
0
    if (props.direction == HB_DIRECTION_INVALID)
726
0
      props.direction = HB_DIRECTION_LTR;
727
0
  }
728
729
  /* If language is not set, use default language from locale */
730
0
  if (props.language == HB_LANGUAGE_INVALID) {
731
    /* TODO get_default_for_script? using $LANGUAGE */
732
0
    props.language = hb_language_get_default ();
733
0
  }
734
0
}
735
736
737
/* Public API */
738
739
DEFINE_NULL_INSTANCE (hb_buffer_t) =
740
{
741
  HB_OBJECT_HEADER_STATIC,
742
743
  const_cast<hb_unicode_funcs_t *> (&_hb_Null_hb_unicode_funcs_t),
744
  HB_BUFFER_FLAG_DEFAULT,
745
  HB_BUFFER_CLUSTER_LEVEL_DEFAULT,
746
  HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT,
747
  0, /* invisible */
748
  0, /* not_found */
749
  HB_CODEPOINT_INVALID, /* not_found_variation_selector */
750
751
752
  HB_BUFFER_CONTENT_TYPE_INVALID,
753
  HB_SEGMENT_PROPERTIES_DEFAULT,
754
755
  false, /* successful */
756
  false, /* have_output */
757
  true  /* have_positions */
758
759
  /* Zero is good enough for everything else. */
760
};
761
762
763
/**
764
 * hb_buffer_create:
765
 *
766
 * Creates a new #hb_buffer_t with all properties to defaults.
767
 *
768
 * Return value: (transfer full):
769
 * A newly allocated #hb_buffer_t with a reference count of 1. The initial
770
 * reference count should be released with hb_buffer_destroy() when you are done
771
 * using the #hb_buffer_t. This function never returns `NULL`. If memory cannot
772
 * be allocated, a special #hb_buffer_t object will be returned on which
773
 * hb_buffer_allocation_successful() returns `false`.
774
 *
775
 * Since: 0.9.2
776
 **/
777
hb_buffer_t *
778
hb_buffer_create ()
779
12.1k
{
780
12.1k
  hb_buffer_t *buffer;
781
782
12.1k
  if (!(buffer = hb_object_create<hb_buffer_t> ()))
783
0
    return hb_buffer_get_empty ();
784
785
12.1k
  buffer->max_len = HB_BUFFER_MAX_LEN_DEFAULT;
786
12.1k
  buffer->max_ops = HB_BUFFER_MAX_OPS_DEFAULT;
787
788
12.1k
  buffer->reset ();
789
790
12.1k
  return buffer;
791
12.1k
}
792
793
/**
794
 * hb_buffer_create_similar:
795
 * @src: An #hb_buffer_t
796
 *
797
 * Creates a new #hb_buffer_t, similar to hb_buffer_create(). The only
798
 * difference is that the buffer is configured similarly to @src.
799
 *
800
 * Return value: (transfer full):
801
 * A newly allocated #hb_buffer_t, similar to hb_buffer_create().
802
 *
803
 * Since: 3.3.0
804
 **/
805
hb_buffer_t *
806
hb_buffer_create_similar (const hb_buffer_t *src)
807
0
{
808
0
  hb_buffer_t *buffer = hb_buffer_create ();
809
810
0
  buffer->similar (*src);
811
812
0
  return buffer;
813
0
}
814
815
/**
816
 * hb_buffer_reset:
817
 * @buffer: An #hb_buffer_t
818
 *
819
 * Resets the buffer to its initial status, as if it was just newly created
820
 * with hb_buffer_create().
821
 *
822
 * Since: 0.9.2
823
 **/
824
void
825
hb_buffer_reset (hb_buffer_t *buffer)
826
104k
{
827
104k
  if (unlikely (hb_object_is_immutable (buffer)))
828
0
    return;
829
830
104k
  buffer->reset ();
831
104k
}
832
833
/**
834
 * hb_buffer_get_empty:
835
 *
836
 * Fetches an empty #hb_buffer_t.
837
 *
838
 * Return value: (transfer full): The empty buffer
839
 *
840
 * Since: 0.9.2
841
 **/
842
hb_buffer_t *
843
hb_buffer_get_empty ()
844
0
{
845
0
  return const_cast<hb_buffer_t *> (&Null (hb_buffer_t));
846
0
}
847
848
/**
849
 * hb_buffer_reference: (skip)
850
 * @buffer: An #hb_buffer_t
851
 *
852
 * Increases the reference count on @buffer by one. This prevents @buffer from
853
 * being destroyed until a matching call to hb_buffer_destroy() is made.
854
 *
855
 * Return value: (transfer full):
856
 * The referenced #hb_buffer_t.
857
 *
858
 * Since: 0.9.2
859
 **/
860
hb_buffer_t *
861
hb_buffer_reference (hb_buffer_t *buffer)
862
0
{
863
0
  return hb_object_reference (buffer);
864
0
}
865
866
/**
867
 * hb_buffer_destroy: (skip)
868
 * @buffer: An #hb_buffer_t
869
 *
870
 * Deallocate the @buffer.
871
 * Decreases the reference count on @buffer by one. If the result is zero, then
872
 * @buffer and all associated resources are freed. See hb_buffer_reference().
873
 *
874
 * Since: 0.9.2
875
 **/
876
void
877
hb_buffer_destroy (hb_buffer_t *buffer)
878
12.1k
{
879
12.1k
  if (!hb_object_destroy (buffer)) return;
880
881
12.1k
  hb_unicode_funcs_destroy (buffer->unicode);
882
883
12.1k
  hb_free (buffer->info);
884
12.1k
  hb_free (buffer->pos);
885
12.1k
#ifndef HB_NO_BUFFER_MESSAGE
886
12.1k
  if (buffer->message_destroy)
887
0
    buffer->message_destroy (buffer->message_data);
888
12.1k
#endif
889
890
12.1k
  hb_free (buffer);
891
12.1k
}
892
893
/**
894
 * hb_buffer_set_user_data: (skip)
895
 * @buffer: An #hb_buffer_t
896
 * @key: The user-data key
897
 * @data: A pointer to the user data
898
 * @destroy: (nullable): A callback to call when @data is not needed anymore
899
 * @replace: Whether to replace an existing data with the same key
900
 *
901
 * Attaches a user-data key/data pair to the specified buffer.
902
 *
903
 * Return value: `true` if success, `false` otherwise
904
 *
905
 * Since: 0.9.2
906
 **/
907
hb_bool_t
908
hb_buffer_set_user_data (hb_buffer_t        *buffer,
909
       hb_user_data_key_t *key,
910
       void *              data,
911
       hb_destroy_func_t   destroy,
912
       hb_bool_t           replace)
913
0
{
914
0
  return hb_object_set_user_data (buffer, key, data, destroy, replace);
915
0
}
916
917
/**
918
 * hb_buffer_get_user_data: (skip)
919
 * @buffer: An #hb_buffer_t
920
 * @key: The user-data key to query
921
 *
922
 * Fetches the user data associated with the specified key,
923
 * attached to the specified buffer.
924
 *
925
 * Return value: (transfer none): A pointer to the user data
926
 *
927
 * Since: 0.9.2
928
 **/
929
void *
930
hb_buffer_get_user_data (const hb_buffer_t  *buffer,
931
       hb_user_data_key_t *key)
932
0
{
933
0
  return hb_object_get_user_data (buffer, key);
934
0
}
935
936
937
/**
938
 * hb_buffer_set_content_type:
939
 * @buffer: An #hb_buffer_t
940
 * @content_type: The type of buffer contents to set
941
 *
942
 * Sets the type of @buffer contents. Buffers are either empty, contain
943
 * characters (before shaping), or contain glyphs (the result of shaping).
944
 *
945
 * You rarely need to call this function, since a number of other
946
 * functions transition the content type for you. Namely:
947
 *
948
 * - A newly created buffer starts with content type
949
 *   %HB_BUFFER_CONTENT_TYPE_INVALID. Calling hb_buffer_reset(),
950
 *   hb_buffer_clear_contents(), as well as calling hb_buffer_set_length()
951
 *   with an argument of zero all set the buffer content type to invalid
952
 *   as well.
953
 *
954
 * - Calling hb_buffer_add_utf8(), hb_buffer_add_utf16(),
955
 *   hb_buffer_add_utf32(), hb_buffer_add_codepoints() and
956
 *   hb_buffer_add_latin1() expect that buffer is either empty and
957
 *   have a content type of invalid, or that buffer content type is
958
 *   %HB_BUFFER_CONTENT_TYPE_UNICODE, and they also set the content
959
 *   type to Unicode if they added anything to an empty buffer.
960
 *
961
 * - Finally hb_shape() and hb_shape_full() expect that the buffer
962
 *   is either empty and have content type of invalid, or that buffer
963
 *   content type is %HB_BUFFER_CONTENT_TYPE_UNICODE, and upon
964
 *   success they set the buffer content type to
965
 *   %HB_BUFFER_CONTENT_TYPE_GLYPHS.
966
 *
967
 * The above transitions are designed such that one can use a buffer
968
 * in a loop of "reset : add-text : shape" without needing to ever
969
 * modify the content type manually.
970
 *
971
 * Since: 0.9.5
972
 **/
973
void
974
hb_buffer_set_content_type (hb_buffer_t              *buffer,
975
          hb_buffer_content_type_t  content_type)
976
0
{
977
0
  if (unlikely (hb_object_is_immutable (buffer)))
978
0
    return;
979
980
0
  buffer->content_type = content_type;
981
0
}
982
983
/**
984
 * hb_buffer_get_content_type:
985
 * @buffer: An #hb_buffer_t
986
 *
987
 * Fetches the type of @buffer contents. Buffers are either empty, contain
988
 * characters (before shaping), or contain glyphs (the result of shaping).
989
 *
990
 * Return value:
991
 * The type of @buffer contents
992
 *
993
 * Since: 0.9.5
994
 **/
995
hb_buffer_content_type_t
996
hb_buffer_get_content_type (const hb_buffer_t *buffer)
997
0
{
998
0
  return buffer->content_type;
999
0
}
1000
1001
1002
/**
1003
 * hb_buffer_set_unicode_funcs:
1004
 * @buffer: An #hb_buffer_t
1005
 * @unicode_funcs: The Unicode-functions structure
1006
 *
1007
 * Sets the Unicode-functions structure of a buffer to
1008
 * @unicode_funcs.
1009
 *
1010
 * Since: 0.9.2
1011
 **/
1012
void
1013
hb_buffer_set_unicode_funcs (hb_buffer_t        *buffer,
1014
           hb_unicode_funcs_t *unicode_funcs)
1015
0
{
1016
0
  if (unlikely (hb_object_is_immutable (buffer)))
1017
0
    return;
1018
1019
0
  if (!unicode_funcs)
1020
0
    unicode_funcs = hb_unicode_funcs_get_default ();
1021
1022
0
  hb_unicode_funcs_reference (unicode_funcs);
1023
0
  hb_unicode_funcs_destroy (buffer->unicode);
1024
0
  buffer->unicode = unicode_funcs;
1025
0
}
1026
1027
/**
1028
 * hb_buffer_get_unicode_funcs:
1029
 * @buffer: An #hb_buffer_t
1030
 *
1031
 * Fetches the Unicode-functions structure of a buffer.
1032
 *
1033
 * Return value: The Unicode-functions structure
1034
 *
1035
 * Since: 0.9.2
1036
 **/
1037
hb_unicode_funcs_t *
1038
hb_buffer_get_unicode_funcs (const hb_buffer_t *buffer)
1039
0
{
1040
0
  return buffer->unicode;
1041
0
}
1042
1043
/**
1044
 * hb_buffer_set_direction:
1045
 * @buffer: An #hb_buffer_t
1046
 * @direction: the #hb_direction_t of the @buffer
1047
 *
1048
 * Set the text flow direction of the buffer. No shaping can happen without
1049
 * setting @buffer direction, and it controls the visual direction for the
1050
 * output glyphs; for RTL direction the glyphs will be reversed. Many layout
1051
 * features depend on the proper setting of the direction, for example,
1052
 * reversing RTL text before shaping, then shaping with LTR direction is not
1053
 * the same as keeping the text in logical order and shaping with RTL
1054
 * direction.
1055
 *
1056
 * Since: 0.9.2
1057
 **/
1058
void
1059
hb_buffer_set_direction (hb_buffer_t    *buffer,
1060
       hb_direction_t  direction)
1061
0
{
1062
0
  if (unlikely (hb_object_is_immutable (buffer)))
1063
0
    return;
1064
1065
0
  buffer->props.direction = direction;
1066
0
}
1067
1068
/**
1069
 * hb_buffer_get_direction:
1070
 * @buffer: An #hb_buffer_t
1071
 *
1072
 * See hb_buffer_set_direction()
1073
 *
1074
 * Return value:
1075
 * The direction of the @buffer.
1076
 *
1077
 * Since: 0.9.2
1078
 **/
1079
hb_direction_t
1080
hb_buffer_get_direction (const hb_buffer_t *buffer)
1081
0
{
1082
0
  return buffer->props.direction;
1083
0
}
1084
1085
/**
1086
 * hb_buffer_set_script:
1087
 * @buffer: An #hb_buffer_t
1088
 * @script: An #hb_script_t to set.
1089
 *
1090
 * Sets the script of @buffer to @script.
1091
 *
1092
 * Script is crucial for choosing the proper shaping behaviour for scripts that
1093
 * require it (e.g. Arabic) and the which OpenType features defined in the font
1094
 * to be applied.
1095
 *
1096
 * You can pass one of the predefined #hb_script_t values, or use
1097
 * hb_script_from_string() or hb_script_from_iso15924_tag() to get the
1098
 * corresponding script from an ISO 15924 script tag.
1099
 *
1100
 * Since: 0.9.2
1101
 **/
1102
void
1103
hb_buffer_set_script (hb_buffer_t *buffer,
1104
          hb_script_t  script)
1105
0
{
1106
0
  if (unlikely (hb_object_is_immutable (buffer)))
1107
0
    return;
1108
1109
0
  buffer->props.script = script;
1110
0
}
1111
1112
/**
1113
 * hb_buffer_get_script:
1114
 * @buffer: An #hb_buffer_t
1115
 *
1116
 * Fetches the script of @buffer.
1117
 *
1118
 * Return value:
1119
 * The #hb_script_t of the @buffer
1120
 *
1121
 * Since: 0.9.2
1122
 **/
1123
hb_script_t
1124
hb_buffer_get_script (const hb_buffer_t *buffer)
1125
0
{
1126
0
  return buffer->props.script;
1127
0
}
1128
1129
/**
1130
 * hb_buffer_set_language:
1131
 * @buffer: An #hb_buffer_t
1132
 * @language: An hb_language_t to set
1133
 *
1134
 * Sets the language of @buffer to @language.
1135
 *
1136
 * Languages are crucial for selecting which OpenType feature to apply to the
1137
 * buffer which can result in applying language-specific behaviour. Languages
1138
 * are orthogonal to the scripts, and though they are related, they are
1139
 * different concepts and should not be confused with each other.
1140
 *
1141
 * Use hb_language_from_string() to convert from BCP 47 language tags to
1142
 * #hb_language_t.
1143
 *
1144
 * Since: 0.9.2
1145
 **/
1146
void
1147
hb_buffer_set_language (hb_buffer_t   *buffer,
1148
      hb_language_t  language)
1149
0
{
1150
0
  if (unlikely (hb_object_is_immutable (buffer)))
1151
0
    return;
1152
1153
0
  buffer->props.language = language;
1154
0
}
1155
1156
/**
1157
 * hb_buffer_get_language:
1158
 * @buffer: An #hb_buffer_t
1159
 *
1160
 * See hb_buffer_set_language().
1161
 *
1162
 * Return value: (transfer none):
1163
 * The #hb_language_t of the buffer. Must not be freed by the caller.
1164
 *
1165
 * Since: 0.9.2
1166
 **/
1167
hb_language_t
1168
hb_buffer_get_language (const hb_buffer_t *buffer)
1169
0
{
1170
0
  return buffer->props.language;
1171
0
}
1172
1173
/**
1174
 * hb_buffer_set_segment_properties:
1175
 * @buffer: An #hb_buffer_t
1176
 * @props: An #hb_segment_properties_t to use
1177
 *
1178
 * Sets the segment properties of the buffer, a shortcut for calling
1179
 * hb_buffer_set_direction(), hb_buffer_set_script() and
1180
 * hb_buffer_set_language() individually.
1181
 *
1182
 * Since: 0.9.7
1183
 **/
1184
void
1185
hb_buffer_set_segment_properties (hb_buffer_t *buffer,
1186
          const hb_segment_properties_t *props)
1187
104k
{
1188
104k
  if (unlikely (hb_object_is_immutable (buffer)))
1189
0
    return;
1190
1191
104k
  buffer->props = *props;
1192
104k
}
1193
1194
/**
1195
 * hb_buffer_get_segment_properties:
1196
 * @buffer: An #hb_buffer_t
1197
 * @props: (out): The output #hb_segment_properties_t
1198
 *
1199
 * Sets @props to the #hb_segment_properties_t of @buffer.
1200
 *
1201
 * Since: 0.9.7
1202
 **/
1203
void
1204
hb_buffer_get_segment_properties (const hb_buffer_t *buffer,
1205
          hb_segment_properties_t *props)
1206
0
{
1207
0
  *props = buffer->props;
1208
0
}
1209
1210
1211
/**
1212
 * hb_buffer_set_flags:
1213
 * @buffer: An #hb_buffer_t
1214
 * @flags: The buffer flags to set
1215
 *
1216
 * Sets @buffer flags to @flags. See #hb_buffer_flags_t.
1217
 *
1218
 * Since: 0.9.7
1219
 **/
1220
void
1221
hb_buffer_set_flags (hb_buffer_t       *buffer,
1222
         hb_buffer_flags_t  flags)
1223
0
{
1224
0
  if (unlikely (hb_object_is_immutable (buffer)))
1225
0
    return;
1226
1227
0
  buffer->flags = flags;
1228
0
}
1229
1230
/**
1231
 * hb_buffer_get_flags:
1232
 * @buffer: An #hb_buffer_t
1233
 *
1234
 * Fetches the #hb_buffer_flags_t of @buffer.
1235
 *
1236
 * Return value:
1237
 * The @buffer flags
1238
 *
1239
 * Since: 0.9.7
1240
 **/
1241
hb_buffer_flags_t
1242
hb_buffer_get_flags (const hb_buffer_t *buffer)
1243
0
{
1244
0
  return buffer->flags;
1245
0
}
1246
1247
/**
1248
 * hb_buffer_set_cluster_level:
1249
 * @buffer: An #hb_buffer_t
1250
 * @cluster_level: The cluster level to set on the buffer
1251
 *
1252
 * Sets the cluster level of a buffer. The #hb_buffer_cluster_level_t
1253
 * dictates one aspect of how HarfBuzz will treat non-base characters
1254
 * during shaping.
1255
 *
1256
 * Since: 0.9.42
1257
 **/
1258
void
1259
hb_buffer_set_cluster_level (hb_buffer_t               *buffer,
1260
           hb_buffer_cluster_level_t  cluster_level)
1261
0
{
1262
0
  if (unlikely (hb_object_is_immutable (buffer)))
1263
0
    return;
1264
1265
0
  buffer->cluster_level = cluster_level;
1266
0
}
1267
1268
/**
1269
 * hb_buffer_get_cluster_level:
1270
 * @buffer: An #hb_buffer_t
1271
 *
1272
 * Fetches the cluster level of a buffer. The #hb_buffer_cluster_level_t
1273
 * dictates one aspect of how HarfBuzz will treat non-base characters
1274
 * during shaping.
1275
 *
1276
 * Return value: The cluster level of @buffer
1277
 *
1278
 * Since: 0.9.42
1279
 **/
1280
hb_buffer_cluster_level_t
1281
hb_buffer_get_cluster_level (const hb_buffer_t *buffer)
1282
0
{
1283
0
  return buffer->cluster_level;
1284
0
}
1285
1286
1287
/**
1288
 * hb_buffer_set_replacement_codepoint:
1289
 * @buffer: An #hb_buffer_t
1290
 * @replacement: the replacement #hb_codepoint_t
1291
 *
1292
 * Sets the #hb_codepoint_t that replaces invalid entries for a given encoding
1293
 * when adding text to @buffer.
1294
 *
1295
 * Default is #HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT.
1296
 *
1297
 * Since: 0.9.31
1298
 **/
1299
void
1300
hb_buffer_set_replacement_codepoint (hb_buffer_t    *buffer,
1301
             hb_codepoint_t  replacement)
1302
0
{
1303
0
  if (unlikely (hb_object_is_immutable (buffer)))
1304
0
    return;
1305
1306
0
  buffer->replacement = replacement;
1307
0
}
1308
1309
/**
1310
 * hb_buffer_get_replacement_codepoint:
1311
 * @buffer: An #hb_buffer_t
1312
 *
1313
 * Fetches the #hb_codepoint_t that replaces invalid entries for a given encoding
1314
 * when adding text to @buffer.
1315
 *
1316
 * Return value:
1317
 * The @buffer replacement #hb_codepoint_t
1318
 *
1319
 * Since: 0.9.31
1320
 **/
1321
hb_codepoint_t
1322
hb_buffer_get_replacement_codepoint (const hb_buffer_t *buffer)
1323
0
{
1324
0
  return buffer->replacement;
1325
0
}
1326
1327
1328
/**
1329
 * hb_buffer_set_invisible_glyph:
1330
 * @buffer: An #hb_buffer_t
1331
 * @invisible: the invisible #hb_codepoint_t
1332
 *
1333
 * Sets the #hb_codepoint_t that replaces invisible characters in
1334
 * the shaping result.  If set to zero (default), the glyph for the
1335
 * U+0020 SPACE character is used.  Otherwise, this value is used
1336
 * verbatim.
1337
 *
1338
 * Since: 2.0.0
1339
 **/
1340
void
1341
hb_buffer_set_invisible_glyph (hb_buffer_t    *buffer,
1342
             hb_codepoint_t  invisible)
1343
0
{
1344
0
  if (unlikely (hb_object_is_immutable (buffer)))
1345
0
    return;
1346
1347
0
  buffer->invisible = invisible;
1348
0
}
1349
1350
/**
1351
 * hb_buffer_get_invisible_glyph:
1352
 * @buffer: An #hb_buffer_t
1353
 *
1354
 * See hb_buffer_set_invisible_glyph().
1355
 *
1356
 * Return value:
1357
 * The @buffer invisible #hb_codepoint_t
1358
 *
1359
 * Since: 2.0.0
1360
 **/
1361
hb_codepoint_t
1362
hb_buffer_get_invisible_glyph (const hb_buffer_t *buffer)
1363
0
{
1364
0
  return buffer->invisible;
1365
0
}
1366
1367
/**
1368
 * hb_buffer_set_not_found_glyph:
1369
 * @buffer: An #hb_buffer_t
1370
 * @not_found: the not-found #hb_codepoint_t
1371
 *
1372
 * Sets the #hb_codepoint_t that replaces characters not found in
1373
 * the font during shaping.
1374
 *
1375
 * The not-found glyph defaults to zero, sometimes known as the
1376
 * ".notdef" glyph.  This API allows for differentiating the two.
1377
 *
1378
 * Since: 3.1.0
1379
 **/
1380
void
1381
hb_buffer_set_not_found_glyph (hb_buffer_t    *buffer,
1382
             hb_codepoint_t  not_found)
1383
0
{
1384
0
  if (unlikely (hb_object_is_immutable (buffer)))
1385
0
    return;
1386
1387
0
  buffer->not_found = not_found;
1388
0
}
1389
1390
/**
1391
 * hb_buffer_get_not_found_glyph:
1392
 * @buffer: An #hb_buffer_t
1393
 *
1394
 * See hb_buffer_set_not_found_glyph().
1395
 *
1396
 * Return value:
1397
 * The @buffer not-found #hb_codepoint_t
1398
 *
1399
 * Since: 3.1.0
1400
 **/
1401
hb_codepoint_t
1402
hb_buffer_get_not_found_glyph (const hb_buffer_t *buffer)
1403
0
{
1404
0
  return buffer->not_found;
1405
0
}
1406
1407
/**
1408
 * hb_buffer_set_not_found_variation_selector_glyph:
1409
 * @buffer: An #hb_buffer_t
1410
 * @not_found_variation_selector: the not-found-variation-selector #hb_codepoint_t
1411
 *
1412
 * Sets the #hb_codepoint_t that replaces variation-selector characters not resolved
1413
 * in the font during shaping.
1414
 *
1415
 * The not-found-variation-selector glyph defaults to #HB_CODEPOINT_INVALID,
1416
 * in which case an unresolved variation-selector will be removed from the glyph
1417
 * string during shaping. This API allows for changing that and retaining a glyph,
1418
 * such that the situation can be detected by the client and handled accordingly
1419
 * (e.g. by using a different font).
1420
 *
1421
 * Since: 10.0.0
1422
 **/
1423
void
1424
hb_buffer_set_not_found_variation_selector_glyph (hb_buffer_t    *buffer,
1425
              hb_codepoint_t  not_found_variation_selector)
1426
0
{
1427
0
  buffer->not_found_variation_selector = not_found_variation_selector;
1428
0
}
1429
1430
/**
1431
 * hb_buffer_get_not_found_variation_selector_glyph:
1432
 * @buffer: An #hb_buffer_t
1433
 *
1434
 * See hb_buffer_set_not_found_variation_selector_glyph().
1435
 *
1436
 * Return value:
1437
 * The @buffer not-found-variation-selector #hb_codepoint_t
1438
 *
1439
 * Since: 10.0.0
1440
 **/
1441
hb_codepoint_t
1442
hb_buffer_get_not_found_variation_selector_glyph (const hb_buffer_t *buffer)
1443
0
{
1444
0
  return buffer->not_found_variation_selector;
1445
0
}
1446
1447
/**
1448
 * hb_buffer_set_random_state:
1449
 * @buffer: An #hb_buffer_t
1450
 * @state: the new random state
1451
 *
1452
 * Sets the random state of the buffer. The state changes
1453
 * every time a glyph uses randomness (eg. the `rand`
1454
 * OpenType feature). This function together with
1455
 * hb_buffer_get_random_state() allow for transferring
1456
 * the current random state to a subsequent buffer, to
1457
 * get better randomness distribution.
1458
 *
1459
 * Defaults to 1 and when buffer contents are cleared.
1460
 * A value of 0 disables randomness during shaping.
1461
 *
1462
 * Since: 8.4.0
1463
 **/
1464
void
1465
hb_buffer_set_random_state (hb_buffer_t    *buffer,
1466
          unsigned        state)
1467
0
{
1468
0
  if (unlikely (hb_object_is_immutable (buffer)))
1469
0
    return;
1470
1471
0
  buffer->random_state = state;
1472
0
}
1473
1474
/**
1475
 * hb_buffer_get_random_state:
1476
 * @buffer: An #hb_buffer_t
1477
 *
1478
 * See hb_buffer_set_random_state().
1479
 *
1480
 * Return value:
1481
 * The @buffer random state
1482
 *
1483
 * Since: 8.4.0
1484
 **/
1485
unsigned
1486
hb_buffer_get_random_state (const hb_buffer_t *buffer)
1487
0
{
1488
0
  return buffer->random_state;
1489
0
}
1490
1491
/**
1492
 * hb_buffer_clear_contents:
1493
 * @buffer: An #hb_buffer_t
1494
 *
1495
 * Similar to hb_buffer_reset(), but does not clear the Unicode functions and
1496
 * the replacement code point.
1497
 *
1498
 * Since: 0.9.11
1499
 **/
1500
void
1501
hb_buffer_clear_contents (hb_buffer_t *buffer)
1502
0
{
1503
0
  if (unlikely (hb_object_is_immutable (buffer)))
1504
0
    return;
1505
1506
0
  buffer->clear ();
1507
0
}
1508
1509
/**
1510
 * hb_buffer_pre_allocate:
1511
 * @buffer: An #hb_buffer_t
1512
 * @size: Number of items to pre allocate.
1513
 *
1514
 * Pre allocates memory for @buffer to fit at least @size number of items.
1515
 *
1516
 * Return value:
1517
 * `true` if @buffer memory allocation succeeded, `false` otherwise
1518
 *
1519
 * Since: 0.9.2
1520
 **/
1521
hb_bool_t
1522
hb_buffer_pre_allocate (hb_buffer_t *buffer, unsigned int size)
1523
104k
{
1524
104k
  return buffer->ensure (size);
1525
104k
}
1526
1527
/**
1528
 * hb_buffer_allocation_successful:
1529
 * @buffer: An #hb_buffer_t
1530
 *
1531
 * Check if allocating memory for the buffer succeeded.
1532
 *
1533
 * Return value:
1534
 * `true` if @buffer memory allocation succeeded, `false` otherwise.
1535
 *
1536
 * Since: 0.9.2
1537
 **/
1538
hb_bool_t
1539
hb_buffer_allocation_successful (hb_buffer_t  *buffer)
1540
12.1k
{
1541
12.1k
  return buffer->successful;
1542
12.1k
}
1543
1544
/**
1545
 * hb_buffer_add:
1546
 * @buffer: An #hb_buffer_t
1547
 * @codepoint: A Unicode code point.
1548
 * @cluster: The cluster value of @codepoint.
1549
 *
1550
 * Appends a character with the Unicode value of @codepoint to @buffer, and
1551
 * gives it the initial cluster value of @cluster. Clusters can be any thing
1552
 * the client wants, they are usually used to refer to the index of the
1553
 * character in the input text stream and are output in
1554
 * #hb_glyph_info_t.cluster field.
1555
 *
1556
 * This function does not check the validity of @codepoint, it is up to the
1557
 * caller to ensure it is a valid Unicode code point.
1558
 *
1559
 * Since: 0.9.7
1560
 **/
1561
void
1562
hb_buffer_add (hb_buffer_t    *buffer,
1563
         hb_codepoint_t  codepoint,
1564
         unsigned int    cluster)
1565
0
{
1566
0
  buffer->add (codepoint, cluster);
1567
0
  buffer->clear_context (1);
1568
0
}
1569
1570
/**
1571
 * hb_buffer_set_length:
1572
 * @buffer: An #hb_buffer_t
1573
 * @length: The new length of @buffer
1574
 *
1575
 * Similar to hb_buffer_pre_allocate(), but clears any new items added at the
1576
 * end.
1577
 *
1578
 * Return value:
1579
 * `true` if @buffer memory allocation succeeded, `false` otherwise.
1580
 *
1581
 * Since: 0.9.2
1582
 **/
1583
hb_bool_t
1584
hb_buffer_set_length (hb_buffer_t  *buffer,
1585
          unsigned int  length)
1586
0
{
1587
0
  if (unlikely (hb_object_is_immutable (buffer)))
1588
0
    return length == 0;
1589
1590
0
  if (unlikely (!buffer->ensure (length)))
1591
0
    return false;
1592
1593
  /* Wipe the new space */
1594
0
  if (length > buffer->len) {
1595
0
    hb_memset (buffer->info + buffer->len, 0, sizeof (buffer->info[0]) * (length - buffer->len));
1596
0
    if (buffer->have_positions)
1597
0
      hb_memset (buffer->pos + buffer->len, 0, sizeof (buffer->pos[0]) * (length - buffer->len));
1598
0
  }
1599
1600
0
  buffer->len = length;
1601
1602
0
  if (!length)
1603
0
  {
1604
0
    buffer->content_type = HB_BUFFER_CONTENT_TYPE_INVALID;
1605
0
    buffer->clear_context (0);
1606
0
  }
1607
0
  buffer->clear_context (1);
1608
1609
0
  return true;
1610
0
}
1611
1612
/**
1613
 * hb_buffer_get_length:
1614
 * @buffer: An #hb_buffer_t
1615
 *
1616
 * Returns the number of items in the buffer.
1617
 *
1618
 * Return value:
1619
 * The @buffer length.
1620
 * The value valid as long as buffer has not been modified.
1621
 *
1622
 * Since: 0.9.2
1623
 **/
1624
unsigned int
1625
hb_buffer_get_length (const hb_buffer_t *buffer)
1626
104k
{
1627
104k
  return buffer->len;
1628
104k
}
1629
1630
/**
1631
 * hb_buffer_get_glyph_infos:
1632
 * @buffer: An #hb_buffer_t
1633
 * @length: (out): The output-array length.
1634
 *
1635
 * Returns @buffer glyph information array.  Returned pointer
1636
 * is valid as long as @buffer contents are not modified.
1637
 *
1638
 * Return value: (transfer none) (array length=length):
1639
 * The @buffer glyph information array.
1640
 * The value valid as long as buffer has not been modified.
1641
 *
1642
 * Since: 0.9.2
1643
 **/
1644
hb_glyph_info_t *
1645
hb_buffer_get_glyph_infos (hb_buffer_t  *buffer,
1646
         unsigned int *length)
1647
104k
{
1648
104k
  if (length)
1649
0
    *length = buffer->len;
1650
1651
104k
  return (hb_glyph_info_t *) buffer->info;
1652
104k
}
1653
1654
/**
1655
 * hb_buffer_get_glyph_positions:
1656
 * @buffer: An #hb_buffer_t
1657
 * @length: (out): The output length
1658
 *
1659
 * Returns @buffer glyph position array.  Returned pointer
1660
 * is valid as long as @buffer contents are not modified.
1661
 *
1662
 * If buffer did not have positions before, the positions will be
1663
 * initialized to zeros, unless this function is called from
1664
 * within a buffer message callback (see hb_buffer_set_message_func()),
1665
 * in which case `NULL` is returned.
1666
 *
1667
 * Return value: (transfer none) (array length=length):
1668
 * The @buffer glyph position array.
1669
 * The value valid as long as buffer has not been modified.
1670
 *
1671
 * Since: 0.9.2
1672
 **/
1673
hb_glyph_position_t *
1674
hb_buffer_get_glyph_positions (hb_buffer_t  *buffer,
1675
             unsigned int *length)
1676
209k
{
1677
209k
  if (length)
1678
104k
    *length = buffer->len;
1679
1680
209k
  if (!buffer->have_positions)
1681
0
  {
1682
0
    if (unlikely (buffer->message_depth))
1683
0
      return nullptr;
1684
1685
0
    buffer->clear_positions ();
1686
0
  }
1687
1688
209k
  return (hb_glyph_position_t *) buffer->pos;
1689
209k
}
1690
1691
/**
1692
 * hb_buffer_has_positions:
1693
 * @buffer: an #hb_buffer_t.
1694
 *
1695
 * Returns whether @buffer has glyph position data.
1696
 * A buffer gains position data when hb_buffer_get_glyph_positions() is called on it,
1697
 * and cleared of position data when hb_buffer_clear_contents() is called.
1698
 *
1699
 * Return value:
1700
 * `true` if the @buffer has position array, `false` otherwise.
1701
 *
1702
 * Since: 2.7.3
1703
 **/
1704
HB_EXTERN hb_bool_t
1705
hb_buffer_has_positions (hb_buffer_t  *buffer)
1706
0
{
1707
0
  return buffer->have_positions;
1708
0
}
1709
1710
/**
1711
 * hb_glyph_info_get_glyph_flags:
1712
 * @info: a #hb_glyph_info_t
1713
 *
1714
 * Returns glyph flags encoded within a #hb_glyph_info_t.
1715
 *
1716
 * Return value:
1717
 * The #hb_glyph_flags_t encoded within @info
1718
 *
1719
 * Since: 1.5.0
1720
 **/
1721
hb_glyph_flags_t
1722
(hb_glyph_info_get_glyph_flags) (const hb_glyph_info_t *info)
1723
0
{
1724
0
  return hb_glyph_info_get_glyph_flags (info);
1725
0
}
1726
1727
/**
1728
 * hb_buffer_reverse:
1729
 * @buffer: An #hb_buffer_t
1730
 *
1731
 * Reverses buffer contents.
1732
 *
1733
 * Since: 0.9.2
1734
 **/
1735
void
1736
hb_buffer_reverse (hb_buffer_t *buffer)
1737
479
{
1738
479
  buffer->reverse ();
1739
479
}
1740
1741
/**
1742
 * hb_buffer_reverse_range:
1743
 * @buffer: An #hb_buffer_t
1744
 * @start: start index
1745
 * @end: end index
1746
 *
1747
 * Reverses buffer contents between @start and @end.
1748
 *
1749
 * Since: 0.9.41
1750
 **/
1751
void
1752
hb_buffer_reverse_range (hb_buffer_t *buffer,
1753
       unsigned int start, unsigned int end)
1754
0
{
1755
0
  buffer->reverse_range (start, end);
1756
0
}
1757
1758
/**
1759
 * hb_buffer_reverse_clusters:
1760
 * @buffer: An #hb_buffer_t
1761
 *
1762
 * Reverses buffer clusters.  That is, the buffer contents are
1763
 * reversed, then each cluster (consecutive items having the
1764
 * same cluster number) are reversed again.
1765
 *
1766
 * Since: 0.9.2
1767
 **/
1768
void
1769
hb_buffer_reverse_clusters (hb_buffer_t *buffer)
1770
0
{
1771
0
  buffer->reverse_clusters ();
1772
0
}
1773
1774
/**
1775
 * hb_buffer_guess_segment_properties:
1776
 * @buffer: An #hb_buffer_t
1777
 *
1778
 * Sets unset buffer segment properties based on buffer Unicode
1779
 * contents.  If buffer is not empty, it must have content type
1780
 * #HB_BUFFER_CONTENT_TYPE_UNICODE.
1781
 *
1782
 * If buffer script is not set (ie. is #HB_SCRIPT_INVALID), it
1783
 * will be set to the Unicode script of the first character in
1784
 * the buffer that has a script other than #HB_SCRIPT_COMMON,
1785
 * #HB_SCRIPT_INHERITED, and #HB_SCRIPT_UNKNOWN.
1786
 *
1787
 * Next, if buffer direction is not set (ie. is #HB_DIRECTION_INVALID),
1788
 * it will be set to the natural horizontal direction of the
1789
 * buffer script as returned by hb_script_get_horizontal_direction().
1790
 * If hb_script_get_horizontal_direction() returns #HB_DIRECTION_INVALID,
1791
 * then #HB_DIRECTION_LTR is used.
1792
 *
1793
 * Finally, if buffer language is not set (ie. is #HB_LANGUAGE_INVALID),
1794
 * it will be set to the process's default language as returned by
1795
 * hb_language_get_default().  This may change in the future by
1796
 * taking buffer script into consideration when choosing a language.
1797
 * Note that hb_language_get_default() is NOT threadsafe the first time
1798
 * it is called.  See documentation for that function for details.
1799
 *
1800
 * Since: 0.9.7
1801
 **/
1802
void
1803
hb_buffer_guess_segment_properties (hb_buffer_t *buffer)
1804
0
{
1805
0
  buffer->guess_segment_properties ();
1806
0
}
1807
1808
template <typename utf_t>
1809
static inline void
1810
hb_buffer_add_utf (hb_buffer_t  *buffer,
1811
       const typename utf_t::codepoint_t *text,
1812
       int           text_length,
1813
       unsigned int  item_offset,
1814
       int           item_length)
1815
104k
{
1816
104k
  typedef typename utf_t::codepoint_t T;
1817
104k
  const hb_codepoint_t replacement = buffer->replacement;
1818
1819
104k
  buffer->assert_unicode ();
1820
1821
104k
  if (unlikely (hb_object_is_immutable (buffer)))
1822
0
    return;
1823
1824
104k
  if (text_length == -1)
1825
0
    text_length = utf_t::strlen (text);
1826
1827
104k
  if (item_length == -1)
1828
0
    item_length = text_length - item_offset;
1829
1830
104k
  item_offset = hb_min (item_offset, (unsigned) text_length);
1831
104k
  item_length = hb_clamp (item_length, 0, text_length - (int) item_offset);
1832
1833
104k
  if (unlikely (item_length < 0 ||
1834
104k
    item_length > INT_MAX / 8 ||
1835
104k
    !buffer->ensure (buffer->len + item_length * sizeof (T) / 4)))
1836
0
    return;
1837
1838
  /* If buffer is empty and pre-context provided, install it.
1839
   * This check is written this way, to make sure people can
1840
   * provide pre-context in one add_utf() call, then provide
1841
   * text in a follow-up call.  See:
1842
   *
1843
   * https://bugzilla.mozilla.org/show_bug.cgi?id=801410#c13
1844
   */
1845
104k
  if (!buffer->len && item_offset > 0)
1846
0
  {
1847
    /* Add pre-context */
1848
0
    buffer->clear_context (0);
1849
0
    const T *prev = text + item_offset;
1850
0
    const T *start = text;
1851
0
    while (start < prev && buffer->context_len[0] < buffer->CONTEXT_LENGTH)
1852
0
    {
1853
0
      hb_codepoint_t u;
1854
0
      prev = utf_t::prev (prev, start, &u, replacement);
1855
0
      buffer->context[0][buffer->context_len[0]++] = u;
1856
0
    }
1857
0
  }
1858
1859
104k
  const T *next = text + item_offset;
1860
104k
  const T *end = next + item_length;
1861
739k
  while (next < end)
1862
635k
  {
1863
635k
    hb_codepoint_t u;
1864
635k
    const T *old_next = next;
1865
635k
    next = utf_t::next (next, end, &u, replacement);
1866
635k
    buffer->add (u, old_next - (const T *) text);
1867
635k
  }
1868
1869
  /* Add post-context */
1870
104k
  buffer->clear_context (1);
1871
104k
  end = text + text_length;
1872
104k
  while (next < end && buffer->context_len[1] < buffer->CONTEXT_LENGTH)
1873
0
  {
1874
0
    hb_codepoint_t u;
1875
0
    next = utf_t::next (next, end, &u, replacement);
1876
0
    buffer->context[1][buffer->context_len[1]++] = u;
1877
0
  }
1878
1879
104k
  buffer->content_type = HB_BUFFER_CONTENT_TYPE_UNICODE;
1880
104k
}
Unexecuted instantiation: hb-buffer.cc:void hb_buffer_add_utf<hb_utf8_t>(hb_buffer_t*, hb_utf8_t::codepoint_t const*, int, unsigned int, int)
Unexecuted instantiation: hb-buffer.cc:void hb_buffer_add_utf<hb_utf16_xe_t<unsigned short> >(hb_buffer_t*, hb_utf16_xe_t<unsigned short>::codepoint_t const*, int, unsigned int, int)
hb-buffer.cc:void hb_buffer_add_utf<hb_utf32_xe_t<unsigned int, true> >(hb_buffer_t*, hb_utf32_xe_t<unsigned int, true>::codepoint_t const*, int, unsigned int, int)
Line
Count
Source
1815
104k
{
1816
104k
  typedef typename utf_t::codepoint_t T;
1817
104k
  const hb_codepoint_t replacement = buffer->replacement;
1818
1819
104k
  buffer->assert_unicode ();
1820
1821
104k
  if (unlikely (hb_object_is_immutable (buffer)))
1822
0
    return;
1823
1824
104k
  if (text_length == -1)
1825
0
    text_length = utf_t::strlen (text);
1826
1827
104k
  if (item_length == -1)
1828
0
    item_length = text_length - item_offset;
1829
1830
104k
  item_offset = hb_min (item_offset, (unsigned) text_length);
1831
104k
  item_length = hb_clamp (item_length, 0, text_length - (int) item_offset);
1832
1833
104k
  if (unlikely (item_length < 0 ||
1834
104k
    item_length > INT_MAX / 8 ||
1835
104k
    !buffer->ensure (buffer->len + item_length * sizeof (T) / 4)))
1836
0
    return;
1837
1838
  /* If buffer is empty and pre-context provided, install it.
1839
   * This check is written this way, to make sure people can
1840
   * provide pre-context in one add_utf() call, then provide
1841
   * text in a follow-up call.  See:
1842
   *
1843
   * https://bugzilla.mozilla.org/show_bug.cgi?id=801410#c13
1844
   */
1845
104k
  if (!buffer->len && item_offset > 0)
1846
0
  {
1847
    /* Add pre-context */
1848
0
    buffer->clear_context (0);
1849
0
    const T *prev = text + item_offset;
1850
0
    const T *start = text;
1851
0
    while (start < prev && buffer->context_len[0] < buffer->CONTEXT_LENGTH)
1852
0
    {
1853
0
      hb_codepoint_t u;
1854
0
      prev = utf_t::prev (prev, start, &u, replacement);
1855
0
      buffer->context[0][buffer->context_len[0]++] = u;
1856
0
    }
1857
0
  }
1858
1859
104k
  const T *next = text + item_offset;
1860
104k
  const T *end = next + item_length;
1861
739k
  while (next < end)
1862
635k
  {
1863
635k
    hb_codepoint_t u;
1864
635k
    const T *old_next = next;
1865
635k
    next = utf_t::next (next, end, &u, replacement);
1866
635k
    buffer->add (u, old_next - (const T *) text);
1867
635k
  }
1868
1869
  /* Add post-context */
1870
104k
  buffer->clear_context (1);
1871
104k
  end = text + text_length;
1872
104k
  while (next < end && buffer->context_len[1] < buffer->CONTEXT_LENGTH)
1873
0
  {
1874
0
    hb_codepoint_t u;
1875
0
    next = utf_t::next (next, end, &u, replacement);
1876
0
    buffer->context[1][buffer->context_len[1]++] = u;
1877
0
  }
1878
1879
104k
  buffer->content_type = HB_BUFFER_CONTENT_TYPE_UNICODE;
1880
104k
}
Unexecuted instantiation: hb-buffer.cc:void hb_buffer_add_utf<hb_latin1_t>(hb_buffer_t*, hb_latin1_t::codepoint_t const*, int, unsigned int, int)
Unexecuted instantiation: hb-buffer.cc:void hb_buffer_add_utf<hb_utf32_xe_t<unsigned int, false> >(hb_buffer_t*, hb_utf32_xe_t<unsigned int, false>::codepoint_t const*, int, unsigned int, int)
1881
1882
/**
1883
 * hb_buffer_add_utf8:
1884
 * @buffer: An #hb_buffer_t
1885
 * @text: (array length=text_length) (element-type uint8_t): An array of UTF-8
1886
 *               characters to append.
1887
 * @text_length: The length of the @text, or -1 if it is `NULL` terminated.
1888
 * @item_offset: The offset of the first character to add to the @buffer.
1889
 * @item_length: The number of characters to add to the @buffer, or -1 for the
1890
 *               end of @text (assuming it is `NULL` terminated).
1891
 *
1892
 * See hb_buffer_add_codepoints().
1893
 *
1894
 * Replaces invalid UTF-8 characters with the @buffer replacement code point,
1895
 * see hb_buffer_set_replacement_codepoint().
1896
 *
1897
 * Since: 0.9.2
1898
 **/
1899
void
1900
hb_buffer_add_utf8 (hb_buffer_t  *buffer,
1901
        const char   *text,
1902
        int           text_length,
1903
        unsigned int  item_offset,
1904
        int           item_length)
1905
0
{
1906
0
  hb_buffer_add_utf<hb_utf8_t> (buffer, (const uint8_t *) text, text_length, item_offset, item_length);
1907
0
}
1908
1909
/**
1910
 * hb_buffer_add_utf16:
1911
 * @buffer: An #hb_buffer_t
1912
 * @text: (array length=text_length): An array of UTF-16 characters to append
1913
 * @text_length: The length of the @text, or -1 if it is `NULL` terminated
1914
 * @item_offset: The offset of the first character to add to the @buffer
1915
 * @item_length: The number of characters to add to the @buffer, or -1 for the
1916
 *               end of @text (assuming it is `NULL` terminated)
1917
 *
1918
 * See hb_buffer_add_codepoints().
1919
 *
1920
 * Replaces invalid UTF-16 characters with the @buffer replacement code point,
1921
 * see hb_buffer_set_replacement_codepoint().
1922
 *
1923
 * Since: 0.9.2
1924
 **/
1925
void
1926
hb_buffer_add_utf16 (hb_buffer_t    *buffer,
1927
         const uint16_t *text,
1928
         int             text_length,
1929
         unsigned int    item_offset,
1930
         int             item_length)
1931
0
{
1932
0
  hb_buffer_add_utf<hb_utf16_t> (buffer, text, text_length, item_offset, item_length);
1933
0
}
1934
1935
/**
1936
 * hb_buffer_add_utf32:
1937
 * @buffer: An #hb_buffer_t
1938
 * @text: (array length=text_length): An array of UTF-32 characters to append
1939
 * @text_length: The length of the @text, or -1 if it is `NULL` terminated
1940
 * @item_offset: The offset of the first character to add to the @buffer
1941
 * @item_length: The number of characters to add to the @buffer, or -1 for the
1942
 *               end of @text (assuming it is `NULL` terminated)
1943
 *
1944
 * See hb_buffer_add_codepoints().
1945
 *
1946
 * Replaces invalid UTF-32 characters with the @buffer replacement code point,
1947
 * see hb_buffer_set_replacement_codepoint().
1948
 *
1949
 * Since: 0.9.2
1950
 **/
1951
void
1952
hb_buffer_add_utf32 (hb_buffer_t    *buffer,
1953
         const uint32_t *text,
1954
         int             text_length,
1955
         unsigned int    item_offset,
1956
         int             item_length)
1957
104k
{
1958
104k
  hb_buffer_add_utf<hb_utf32_t> (buffer, text, text_length, item_offset, item_length);
1959
104k
}
1960
1961
/**
1962
 * hb_buffer_add_latin1:
1963
 * @buffer: An #hb_buffer_t
1964
 * @text: (array length=text_length) (element-type uint8_t): an array of UTF-8
1965
 *               characters to append
1966
 * @text_length: the length of the @text, or -1 if it is `NULL` terminated
1967
 * @item_offset: the offset of the first character to add to the @buffer
1968
 * @item_length: the number of characters to add to the @buffer, or -1 for the
1969
 *               end of @text (assuming it is `NULL` terminated)
1970
 *
1971
 * Similar to hb_buffer_add_codepoints(), but allows only access to first 256
1972
 * Unicode code points that can fit in 8-bit strings.
1973
 *
1974
 * <note>Has nothing to do with non-Unicode Latin-1 encoding.</note>
1975
 *
1976
 * Since: 0.9.39
1977
 **/
1978
void
1979
hb_buffer_add_latin1 (hb_buffer_t   *buffer,
1980
          const uint8_t *text,
1981
          int            text_length,
1982
          unsigned int   item_offset,
1983
          int            item_length)
1984
0
{
1985
0
  hb_buffer_add_utf<hb_latin1_t> (buffer, text, text_length, item_offset, item_length);
1986
0
}
1987
1988
/**
1989
 * hb_buffer_add_codepoints:
1990
 * @buffer: a #hb_buffer_t to append characters to.
1991
 * @text: (array length=text_length): an array of Unicode code points to append.
1992
 * @text_length: the length of the @text, or -1 if it is `NULL` terminated.
1993
 * @item_offset: the offset of the first code point to add to the @buffer.
1994
 * @item_length: the number of code points to add to the @buffer, or -1 for the
1995
 *               end of @text (assuming it is `NULL` terminated).
1996
 *
1997
 * Appends characters from @text array to @buffer. The @item_offset is the
1998
 * position of the first character from @text that will be appended, and
1999
 * @item_length is the number of character. When shaping part of a larger text
2000
 * (e.g. a run of text from a paragraph), instead of passing just the substring
2001
 * corresponding to the run, it is preferable to pass the whole
2002
 * paragraph and specify the run start and length as @item_offset and
2003
 * @item_length, respectively, to give HarfBuzz the full context to be able,
2004
 * for example, to do cross-run Arabic shaping or properly handle combining
2005
 * marks at stat of run.
2006
 *
2007
 * This function does not check the validity of @text, it is up to the caller
2008
 * to ensure it contains a valid Unicode scalar values.  In contrast,
2009
 * hb_buffer_add_utf32() can be used that takes similar input but performs
2010
 * sanity-check on the input.
2011
 *
2012
 * Since: 0.9.31
2013
 **/
2014
void
2015
hb_buffer_add_codepoints (hb_buffer_t          *buffer,
2016
        const hb_codepoint_t *text,
2017
        int                   text_length,
2018
        unsigned int          item_offset,
2019
        int                   item_length)
2020
0
{
2021
0
  hb_buffer_add_utf<hb_utf32_novalidate_t> (buffer, text, text_length, item_offset, item_length);
2022
0
}
2023
2024
2025
/**
2026
 * hb_buffer_append:
2027
 * @buffer: An #hb_buffer_t
2028
 * @source: source #hb_buffer_t
2029
 * @start: start index into source buffer to copy.  Use 0 to copy from start of buffer.
2030
 * @end: end index into source buffer to copy.  Use @UINT_MAX (or ((unsigned int) -1)) to copy to end of buffer.
2031
 *
2032
 * Append (part of) contents of another buffer to this buffer.
2033
 *
2034
 * Since: 1.5.0
2035
 **/
2036
HB_EXTERN void
2037
hb_buffer_append (hb_buffer_t *buffer,
2038
      const hb_buffer_t *source,
2039
      unsigned int start,
2040
      unsigned int end)
2041
0
{
2042
0
  assert (!buffer->have_output && !source->have_output);
2043
0
  assert (buffer->have_positions == source->have_positions ||
2044
0
    !buffer->len || !source->len);
2045
0
  assert (buffer->content_type == source->content_type ||
2046
0
    !buffer->len || !source->len);
2047
2048
0
  if (end > source->len)
2049
0
    end = source->len;
2050
0
  if (start > end)
2051
0
    start = end;
2052
0
  if (start == end)
2053
0
    return;
2054
2055
0
  if (buffer->len + (end - start) < buffer->len) /* Overflows. */
2056
0
  {
2057
0
    buffer->successful = false;
2058
0
    return;
2059
0
  }
2060
2061
0
  unsigned int orig_len = buffer->len;
2062
0
  hb_buffer_set_length (buffer, buffer->len + (end - start));
2063
0
  if (unlikely (!buffer->successful))
2064
0
    return;
2065
2066
0
  if (!orig_len)
2067
0
    buffer->content_type = source->content_type;
2068
0
  if (!buffer->have_positions && source->have_positions)
2069
0
    buffer->clear_positions ();
2070
2071
0
  hb_segment_properties_overlay (&buffer->props, &source->props);
2072
2073
0
  hb_memcpy (buffer->info + orig_len, source->info + start, (end - start) * sizeof (buffer->info[0]));
2074
0
  if (buffer->have_positions)
2075
0
    hb_memcpy (buffer->pos + orig_len, source->pos + start, (end - start) * sizeof (buffer->pos[0]));
2076
2077
0
  if (source->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE)
2078
0
  {
2079
    /* See similar logic in add_utf. */
2080
2081
    /* pre-context */
2082
0
    if (!orig_len && start + source->context_len[0] > 0)
2083
0
    {
2084
0
      buffer->clear_context (0);
2085
0
      while (start > 0 && buffer->context_len[0] < buffer->CONTEXT_LENGTH)
2086
0
  buffer->context[0][buffer->context_len[0]++] = source->info[--start].codepoint;
2087
0
      for (auto i = 0u; i < source->context_len[0] && buffer->context_len[0] < buffer->CONTEXT_LENGTH; i++)
2088
0
  buffer->context[0][buffer->context_len[0]++] = source->context[0][i];
2089
0
    }
2090
2091
    /* post-context */
2092
0
    buffer->clear_context (1);
2093
0
    while (end < source->len && buffer->context_len[1] < buffer->CONTEXT_LENGTH)
2094
0
      buffer->context[1][buffer->context_len[1]++] = source->info[end++].codepoint;
2095
0
    for (auto i = 0u; i < source->context_len[1] && buffer->context_len[1] < buffer->CONTEXT_LENGTH; i++)
2096
0
      buffer->context[1][buffer->context_len[1]++] = source->context[1][i];
2097
0
  }
2098
0
}
2099
2100
2101
static int
2102
compare_info_codepoint (const hb_glyph_info_t *pa,
2103
      const hb_glyph_info_t *pb)
2104
0
{
2105
0
  return (int) pb->codepoint - (int) pa->codepoint;
2106
0
}
2107
2108
static inline void
2109
normalize_glyphs_cluster (hb_buffer_t *buffer,
2110
        unsigned int start,
2111
        unsigned int end,
2112
        bool backward)
2113
0
{
2114
0
  hb_glyph_position_t *pos = buffer->pos;
2115
2116
  /* Total cluster advance */
2117
0
  hb_position_t total_x_advance = 0, total_y_advance = 0;
2118
0
  for (unsigned int i = start; i < end; i++)
2119
0
  {
2120
0
    total_x_advance = hb_saturate_add (total_x_advance, pos[i].x_advance);
2121
0
    total_y_advance = hb_saturate_add (total_y_advance, pos[i].y_advance);
2122
0
  }
2123
2124
0
  hb_position_t x_advance = 0, y_advance = 0;
2125
0
  for (unsigned int i = start; i < end; i++)
2126
0
  {
2127
0
    pos[i].x_offset = hb_saturate_add (pos[i].x_offset, x_advance);
2128
0
    pos[i].y_offset = hb_saturate_add (pos[i].y_offset, y_advance);
2129
2130
0
    x_advance = hb_saturate_add (x_advance, pos[i].x_advance);
2131
0
    y_advance = hb_saturate_add (y_advance, pos[i].y_advance);
2132
2133
0
    pos[i].x_advance = 0;
2134
0
    pos[i].y_advance = 0;
2135
0
  }
2136
2137
0
  if (backward)
2138
0
  {
2139
    /* Transfer all cluster advance to the last glyph. */
2140
0
    pos[end - 1].x_advance = total_x_advance;
2141
0
    pos[end - 1].y_advance = total_y_advance;
2142
2143
0
    hb_stable_sort (buffer->info + start, end - start - 1, compare_info_codepoint, buffer->pos + start);
2144
0
  } else {
2145
    /* Transfer all cluster advance to the first glyph. */
2146
0
    pos[start].x_advance = hb_saturate_add (pos[start].x_advance, total_x_advance);
2147
0
    pos[start].y_advance = hb_saturate_add (pos[start].y_advance, total_y_advance);
2148
0
    for (unsigned int i = start + 1; i < end; i++) {
2149
0
      pos[i].x_offset = hb_saturate_sub (pos[i].x_offset, total_x_advance);
2150
0
      pos[i].y_offset = hb_saturate_sub (pos[i].y_offset, total_y_advance);
2151
0
    }
2152
0
    hb_stable_sort (buffer->info + start + 1, end - start - 1, compare_info_codepoint, buffer->pos + start + 1);
2153
0
  }
2154
0
}
2155
2156
/**
2157
 * hb_buffer_normalize_glyphs:
2158
 * @buffer: An #hb_buffer_t
2159
 *
2160
 * Reorders a glyph buffer to have canonical in-cluster glyph order / position.
2161
 * The resulting clusters should behave identical to pre-reordering clusters.
2162
 *
2163
 * <note>This has nothing to do with Unicode normalization.</note>
2164
 *
2165
 * Since: 0.9.2
2166
 **/
2167
void
2168
hb_buffer_normalize_glyphs (hb_buffer_t *buffer)
2169
0
{
2170
0
  assert (buffer->have_positions);
2171
2172
0
  buffer->assert_glyphs ();
2173
2174
0
  bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
2175
2176
0
  foreach_cluster (buffer, start, end)
2177
0
    normalize_glyphs_cluster (buffer, start, end, backward);
2178
0
}
2179
2180
void
2181
hb_buffer_t::sort (unsigned int start, unsigned int end, int(*compar)(const hb_glyph_info_t *, const hb_glyph_info_t *))
2182
12
{
2183
12
  assert (!have_positions);
2184
12
  for (unsigned int i = start + 1; i < end; i++)
2185
0
  {
2186
0
    unsigned int j = i;
2187
0
    while (j > start && compar (&info[j - 1], &info[i]) > 0)
2188
0
      j--;
2189
0
    if (i == j)
2190
0
      continue;
2191
    /* Move item i to occupy place for item j, shift what's in between. */
2192
0
    merge_clusters (j, i + 1);
2193
0
    {
2194
0
      hb_glyph_info_t t = info[i];
2195
0
      memmove (&info[j + 1], &info[j], (i - j) * sizeof (hb_glyph_info_t));
2196
0
      info[j] = t;
2197
0
    }
2198
0
  }
2199
12
}
2200
2201
2202
/*
2203
 * Comparing buffers.
2204
 */
2205
2206
/**
2207
 * hb_buffer_diff:
2208
 * @buffer: a buffer.
2209
 * @reference: other buffer to compare to.
2210
 * @dottedcircle_glyph: glyph id of U+25CC DOTTED CIRCLE, or (hb_codepoint_t) -1.
2211
 * @position_fuzz: allowed absolute difference in position values.
2212
 *
2213
 * If dottedcircle_glyph is (hb_codepoint_t) -1 then #HB_BUFFER_DIFF_FLAG_DOTTED_CIRCLE_PRESENT
2214
 * and #HB_BUFFER_DIFF_FLAG_NOTDEF_PRESENT are never returned.  This should be used by most
2215
 * callers if just comparing two buffers is needed.
2216
 *
2217
 * Since: 1.5.0
2218
 **/
2219
hb_buffer_diff_flags_t
2220
hb_buffer_diff (hb_buffer_t *buffer,
2221
    hb_buffer_t *reference,
2222
    hb_codepoint_t dottedcircle_glyph,
2223
    unsigned int position_fuzz)
2224
0
{
2225
0
  if (buffer->content_type != reference->content_type && buffer->len && reference->len)
2226
0
    return HB_BUFFER_DIFF_FLAG_CONTENT_TYPE_MISMATCH;
2227
2228
0
  hb_buffer_diff_flags_t result = HB_BUFFER_DIFF_FLAG_EQUAL;
2229
0
  bool contains = dottedcircle_glyph != (hb_codepoint_t) -1;
2230
2231
0
  unsigned int count = reference->len;
2232
2233
0
  if (buffer->len != count)
2234
0
  {
2235
    /*
2236
     * we can't compare glyph-by-glyph, but we do want to know if there
2237
     * are .notdef or dottedcircle glyphs present in the reference buffer
2238
     */
2239
0
    const hb_glyph_info_t *info = reference->info;
2240
0
    unsigned int i;
2241
0
    for (i = 0; i < count; i++)
2242
0
    {
2243
0
      if (contains && info[i].codepoint == dottedcircle_glyph)
2244
0
  result |= HB_BUFFER_DIFF_FLAG_DOTTED_CIRCLE_PRESENT;
2245
0
      if (contains && info[i].codepoint == 0)
2246
0
  result |= HB_BUFFER_DIFF_FLAG_NOTDEF_PRESENT;
2247
0
    }
2248
0
    result |= HB_BUFFER_DIFF_FLAG_LENGTH_MISMATCH;
2249
0
    return hb_buffer_diff_flags_t (result);
2250
0
  }
2251
2252
0
  if (!count)
2253
0
    return hb_buffer_diff_flags_t (result);
2254
2255
0
  const hb_glyph_info_t *buf_info = buffer->info;
2256
0
  const hb_glyph_info_t *ref_info = reference->info;
2257
0
  for (unsigned int i = 0; i < count; i++)
2258
0
  {
2259
0
    if (buf_info->codepoint != ref_info->codepoint)
2260
0
      result |= HB_BUFFER_DIFF_FLAG_CODEPOINT_MISMATCH;
2261
0
    if (buf_info->cluster != ref_info->cluster)
2262
0
      result |= HB_BUFFER_DIFF_FLAG_CLUSTER_MISMATCH;
2263
0
    if ((buf_info->mask ^ ref_info->mask) & HB_GLYPH_FLAG_DEFINED)
2264
0
      result |= HB_BUFFER_DIFF_FLAG_GLYPH_FLAGS_MISMATCH;
2265
0
    if (contains && ref_info->codepoint == dottedcircle_glyph)
2266
0
      result |= HB_BUFFER_DIFF_FLAG_DOTTED_CIRCLE_PRESENT;
2267
0
    if (contains && ref_info->codepoint == 0)
2268
0
      result |= HB_BUFFER_DIFF_FLAG_NOTDEF_PRESENT;
2269
0
    buf_info++;
2270
0
    ref_info++;
2271
0
  }
2272
2273
0
  if (buffer->content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS)
2274
0
  {
2275
0
    auto position_diff = [] (hb_position_t a, hb_position_t b) -> uint64_t
2276
0
    {
2277
0
      int64_t diff = (int64_t) a - b;
2278
0
      return diff < 0 ? -diff : diff;
2279
0
    };
2280
0
    assert (buffer->have_positions);
2281
0
    const hb_glyph_position_t *buf_pos = buffer->pos;
2282
0
    const hb_glyph_position_t *ref_pos = reference->pos;
2283
0
    for (unsigned int i = 0; i < count; i++)
2284
0
    {
2285
0
      if (position_diff (buf_pos->x_advance, ref_pos->x_advance) > position_fuzz ||
2286
0
    position_diff (buf_pos->y_advance, ref_pos->y_advance) > position_fuzz ||
2287
0
    position_diff (buf_pos->x_offset, ref_pos->x_offset) > position_fuzz ||
2288
0
    position_diff (buf_pos->y_offset, ref_pos->y_offset) > position_fuzz)
2289
0
      {
2290
0
  result |= HB_BUFFER_DIFF_FLAG_POSITION_MISMATCH;
2291
0
  break;
2292
0
      }
2293
0
      buf_pos++;
2294
0
      ref_pos++;
2295
0
    }
2296
0
  }
2297
2298
0
  return result;
2299
0
}
2300
2301
2302
/*
2303
 * Debugging.
2304
 */
2305
2306
void
2307
hb_buffer_t::changed ()
2308
0
{
2309
#ifdef HB_NO_BUFFER_MESSAGE
2310
  return;
2311
#else
2312
0
  if (!message_depth)
2313
0
    return;
2314
2315
0
  if (changed_func)
2316
0
    changed_func (this, changed_data);
2317
0
  else
2318
0
    update_digest ();
2319
0
#endif
2320
0
}
2321
2322
#ifndef HB_NO_BUFFER_MESSAGE
2323
/**
2324
 * hb_buffer_set_message_func:
2325
 * @buffer: An #hb_buffer_t
2326
 * @func: (closure user_data) (destroy destroy) (scope notified): Callback function
2327
 * @user_data: (nullable): Data to pass to @func
2328
 * @destroy: (nullable): The function to call when @user_data is not needed anymore
2329
 *
2330
 * Sets the implementation function for #hb_buffer_message_func_t.
2331
 *
2332
 * Since: 1.1.3
2333
 **/
2334
void
2335
hb_buffer_set_message_func (hb_buffer_t *buffer,
2336
          hb_buffer_message_func_t func,
2337
          void *user_data, hb_destroy_func_t destroy)
2338
0
{
2339
0
  if (unlikely (hb_object_is_immutable (buffer)) ||
2340
0
      unlikely (buffer->message_depth))
2341
0
  {
2342
0
    if (destroy)
2343
0
      destroy (user_data);
2344
0
    return;
2345
0
  }
2346
2347
0
  if (buffer->message_destroy)
2348
0
    buffer->message_destroy (buffer->message_data);
2349
2350
0
  if (func) {
2351
0
    buffer->message_func = func;
2352
0
    buffer->message_data = user_data;
2353
0
    buffer->message_destroy = destroy;
2354
0
  } else {
2355
0
    buffer->message_func = nullptr;
2356
0
    buffer->message_data = nullptr;
2357
0
    buffer->message_destroy = nullptr;
2358
0
  }
2359
0
}
2360
/**
2361
 * hb_buffer_changed:
2362
 * @buffer: An #hb_buffer_t
2363
 *
2364
 * Called by a message callback after modifying buffer glyph indices,
2365
 * to update internal caches.
2366
 *
2367
 * If not called from inside a message callback, does nothing.
2368
 *
2369
 * Since: 13.0.0
2370
 **/
2371
void
2372
hb_buffer_changed (hb_buffer_t *buffer)
2373
0
{
2374
0
  buffer->changed ();
2375
0
}
2376
2377
bool
2378
hb_buffer_t::message_impl (hb_font_t *font, const char *fmt, va_list ap)
2379
0
{
2380
0
  assert (!have_output || (out_info == info && out_len == idx));
2381
2382
0
  message_depth++;
2383
2384
0
  char buf[100];
2385
0
  vsnprintf (buf, sizeof (buf), fmt, ap);
2386
0
  bool ret = (bool) this->message_func (this, font, buf, this->message_data);
2387
2388
0
  message_depth--;
2389
2390
0
  return ret;
2391
0
}
2392
#endif