Coverage Report

Created: 2026-02-26 06:23

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