Coverage Report

Created: 2023-09-25 06:24

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