Coverage Report

Created: 2025-08-24 06:26

/src/harfbuzz/src/hb-buffer.hh
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
#ifndef HB_BUFFER_HH
31
#define HB_BUFFER_HH
32
33
#include "hb.hh"
34
#include "hb-unicode.hh"
35
36
37
static_assert ((sizeof (hb_glyph_info_t) == 20), "");
38
static_assert ((sizeof (hb_glyph_info_t) == sizeof (hb_glyph_position_t)), "");
39
40
HB_MARK_AS_FLAG_T (hb_glyph_flags_t);
41
HB_MARK_AS_FLAG_T (hb_buffer_flags_t);
42
HB_MARK_AS_FLAG_T (hb_buffer_serialize_flags_t);
43
HB_MARK_AS_FLAG_T (hb_buffer_diff_flags_t);
44
45
enum hb_buffer_scratch_flags_t {
46
  HB_BUFFER_SCRATCH_FLAG_DEFAULT      = 0x00000000u,
47
  HB_BUFFER_SCRATCH_FLAG_HAS_NON_ASCII      = 0x00000001u,
48
  HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES   = 0x00000002u,
49
  HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK   = 0x00000004u,
50
  HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT    = 0x00000008u,
51
  HB_BUFFER_SCRATCH_FLAG_HAS_CGJ      = 0x00000010u,
52
  HB_BUFFER_SCRATCH_FLAG_HAS_GLYPH_FLAGS    = 0x00000020u,
53
  HB_BUFFER_SCRATCH_FLAG_HAS_BROKEN_SYLLABLE    = 0x00000040u,
54
  HB_BUFFER_SCRATCH_FLAG_HAS_VARIATION_SELECTOR_FALLBACK= 0x00000080u,
55
56
  /* Reserved for shapers' internal use. */
57
  HB_BUFFER_SCRATCH_FLAG_SHAPER0      = 0x01000000u,
58
  HB_BUFFER_SCRATCH_FLAG_SHAPER1      = 0x02000000u,
59
  HB_BUFFER_SCRATCH_FLAG_SHAPER2      = 0x04000000u,
60
  HB_BUFFER_SCRATCH_FLAG_SHAPER3      = 0x08000000u,
61
};
62
HB_MARK_AS_FLAG_T (hb_buffer_scratch_flags_t);
63
64
65
/*
66
 * hb_buffer_t
67
 */
68
69
struct hb_buffer_t
70
{
71
  hb_object_header_t header;
72
73
  /*
74
   * Information about how the text in the buffer should be treated.
75
   */
76
77
  hb_unicode_funcs_t *unicode; /* Unicode functions */
78
  hb_buffer_flags_t flags; /* BOT / EOT / etc. */
79
  hb_buffer_cluster_level_t cluster_level;
80
  hb_codepoint_t replacement; /* U+FFFD or something else. */
81
  hb_codepoint_t invisible; /* 0 or something else. */
82
  hb_codepoint_t not_found; /* 0 or something else. */
83
  hb_codepoint_t not_found_variation_selector; /* HB_CODEPOINT_INVALID or something else. */
84
85
  /*
86
   * Buffer contents
87
   */
88
89
  hb_buffer_content_type_t content_type;
90
  hb_segment_properties_t props; /* Script, language, direction */
91
92
  bool successful; /* Allocations successful */
93
  bool have_output; /* Whether we have an output buffer going on */
94
  bool have_positions; /* Whether we have positions */
95
96
  unsigned int idx; /* Cursor into ->info and ->pos arrays */
97
  unsigned int len; /* Length of ->info and ->pos arrays */
98
  unsigned int out_len; /* Length of ->out_info array if have_output */
99
100
  unsigned int allocated; /* Length of allocated arrays */
101
  hb_glyph_info_t     *info;
102
  hb_glyph_info_t     *out_info;
103
  hb_glyph_position_t *pos;
104
105
  /* Text before / after the main buffer contents.
106
   * Always in Unicode, and ordered outward.
107
   * Index 0 is for "pre-context", 1 for "post-context". */
108
  static constexpr unsigned CONTEXT_LENGTH = 5u;
109
  hb_codepoint_t context[2][CONTEXT_LENGTH];
110
  unsigned int context_len[2];
111
112
113
  /*
114
   * Managed by enter / leave
115
   */
116
117
  uint8_t allocated_var_bits;
118
  uint8_t serial;
119
  uint32_t random_state;
120
  hb_buffer_scratch_flags_t scratch_flags; /* Have space-fallback, etc. */
121
  unsigned int max_len; /* Maximum allowed len. */
122
  int max_ops; /* Maximum allowed operations. */
123
  /* The bits here reflect current allocations of the bytes in glyph_info_t's var1 and var2. */
124
125
126
  /*
127
   * Messaging callback
128
   */
129
130
#ifndef HB_NO_BUFFER_MESSAGE
131
  hb_buffer_message_func_t message_func;
132
  void *message_data;
133
  hb_destroy_func_t message_destroy;
134
  unsigned message_depth; /* How deeply are we inside a message callback? */
135
#else
136
  static constexpr unsigned message_depth = 0u;
137
#endif
138
139
140
141
  /* Methods */
142
143
0
  HB_NODISCARD bool in_error () const { return !successful; }
144
145
  void allocate_var (unsigned int start, unsigned int count)
146
500k
  {
147
500k
    unsigned int end = start + count;
148
500k
    assert (end <= 8);
149
500k
    unsigned int bits = (1u<<end) - (1u<<start);
150
500k
    assert (0 == (allocated_var_bits & bits));
151
500k
    allocated_var_bits |= bits;
152
500k
  }
153
  bool try_allocate_var (unsigned int start, unsigned int count)
154
0
  {
155
0
    unsigned int end = start + count;
156
0
    assert (end <= 8);
157
0
    unsigned int bits = (1u<<end) - (1u<<start);
158
0
    if (allocated_var_bits & bits)
159
0
      return false;
160
0
    allocated_var_bits |= bits;
161
0
    return true;
162
0
  }
163
  void deallocate_var (unsigned int start, unsigned int count)
164
500k
  {
165
500k
    unsigned int end = start + count;
166
500k
    assert (end <= 8);
167
500k
    unsigned int bits = (1u<<end) - (1u<<start);
168
500k
    assert (bits == (allocated_var_bits & bits));
169
500k
    allocated_var_bits &= ~bits;
170
500k
  }
171
  void assert_var (unsigned int start, unsigned int count)
172
625k
  {
173
625k
    unsigned int end = start + count;
174
625k
    assert (end <= 8);
175
625k
    HB_UNUSED unsigned int bits = (1u<<end) - (1u<<start);
176
625k
    assert (bits == (allocated_var_bits & bits));
177
625k
  }
178
  void deallocate_var_all ()
179
386k
  {
180
386k
    allocated_var_bits = 0;
181
386k
  }
182
183
  HB_ALWAYS_INLINE
184
675k
  hb_glyph_info_t &cur (unsigned int i = 0) { return info[idx + i]; }
185
  HB_ALWAYS_INLINE
186
0
  hb_glyph_info_t cur (unsigned int i = 0) const { return info[idx + i]; }
187
188
  HB_ALWAYS_INLINE
189
0
  hb_glyph_position_t &cur_pos (unsigned int i = 0) { return pos[idx + i]; }
190
  HB_ALWAYS_INLINE
191
0
  hb_glyph_position_t cur_pos (unsigned int i = 0) const { return pos[idx + i]; }
192
193
  HB_ALWAYS_INLINE
194
1.00k
  hb_glyph_info_t &prev ()      { return out_info[out_len ? out_len - 1 : 0]; }
195
  HB_ALWAYS_INLINE
196
0
  hb_glyph_info_t prev () const { return out_info[out_len ? out_len - 1 : 0]; }
197
198
  template <typename set_t>
199
  void collect_codepoints (set_t &d) const
200
250k
  { d.clear (); d.add_array (&info[0].codepoint, len, sizeof (info[0])); }
void hb_buffer_t::collect_codepoints<hb_bit_set_t>(hb_bit_set_t&) const
Line
Count
Source
200
512
  { d.clear (); d.add_array (&info[0].codepoint, len, sizeof (info[0])); }
void hb_buffer_t::collect_codepoints<hb_set_digest_t>(hb_set_digest_t&) const
Line
Count
Source
200
250k
  { d.clear (); d.add_array (&info[0].codepoint, len, sizeof (info[0])); }
201
202
  HB_INTERNAL void similar (const hb_buffer_t &src);
203
  HB_INTERNAL void reset ();
204
  HB_INTERNAL void clear ();
205
206
  /* Called around shape() */
207
  HB_INTERNAL void enter ();
208
  HB_INTERNAL void leave ();
209
210
#ifndef HB_NO_BUFFER_VERIFY
211
  HB_INTERNAL
212
#endif
213
  bool verify (hb_buffer_t        *text_buffer,
214
         hb_font_t          *font,
215
         const hb_feature_t *features,
216
         unsigned int        num_features,
217
         const char * const *shapers)
218
#ifndef HB_NO_BUFFER_VERIFY
219
  ;
220
#else
221
  { return true; }
222
#endif
223
224
0
  unsigned int backtrack_len () const { return have_output ? out_len : idx; }
225
0
  unsigned int lookahead_len () const { return len - idx; }
226
0
  uint8_t next_serial () { return ++serial ? serial : ++serial; }
227
228
  HB_INTERNAL void add (hb_codepoint_t  codepoint,
229
      unsigned int    cluster);
230
  HB_INTERNAL void add_info (const hb_glyph_info_t &glyph_info);
231
  HB_INTERNAL void add_info_and_pos (const hb_glyph_info_t &glyph_info,
232
             const hb_glyph_position_t &glyph_pos);
233
234
  void reverse_range (unsigned start, unsigned end)
235
244
  {
236
244
    hb_array_t<hb_glyph_info_t> (info, len).reverse (start, end);
237
244
    if (have_positions)
238
108
      hb_array_t<hb_glyph_position_t> (pos, len).reverse (start, end);
239
244
  }
240
160
  void reverse () { reverse_range (0, len); }
241
242
  template <typename FuncType>
243
  void reverse_groups (const FuncType& group,
244
           bool merge_clusters = false)
245
52
  {
246
52
    if (unlikely (!len))
247
0
      return;
248
249
52
    unsigned start = 0;
250
52
    unsigned i;
251
116
    for (i = 1; i < len; i++)
252
64
    {
253
64
      if (!group (info[i - 1], info[i]))
254
32
      {
255
32
  if (merge_clusters)
256
0
    this->merge_clusters (start, i);
257
32
  reverse_range (start, i);
258
32
  start = i;
259
32
      }
260
64
    }
261
52
    if (merge_clusters)
262
0
      this->merge_clusters (start, i);
263
52
    reverse_range (start, i);
264
265
52
    reverse ();
266
52
  }
267
268
  template <typename FuncType>
269
  unsigned group_end (unsigned start, const FuncType& group) const
270
521k
  {
271
521k
    while (++start < len && group (info[start - 1], info[start]))
272
472
      ;
273
274
521k
    return start;
275
521k
  }
276
277
  static bool _cluster_group_func (const hb_glyph_info_t& a,
278
           const hb_glyph_info_t& b)
279
64
  { return a.cluster == b.cluster; }
280
281
0
  void reverse_clusters () { reverse_groups (_cluster_group_func); }
282
283
  HB_INTERNAL void guess_segment_properties ();
284
285
  HB_INTERNAL bool sync ();
286
  HB_INTERNAL int sync_so_far ();
287
  HB_INTERNAL void clear_output ();
288
  HB_INTERNAL void clear_positions ();
289
290
  template <typename T>
291
  HB_NODISCARD bool replace_glyphs (unsigned int num_in,
292
            unsigned int num_out,
293
            const T *glyph_data)
294
0
  {
295
0
    if (unlikely (!make_room_for (num_in, num_out))) return false;
296
297
0
    assert (idx + num_in <= len);
298
299
0
    merge_clusters (idx, idx + num_in);
300
301
0
    hb_glyph_info_t &orig_info = idx < len ? cur() : prev();
302
303
0
    hb_glyph_info_t *pinfo = &out_info[out_len];
304
0
    for (unsigned int i = 0; i < num_out; i++)
305
0
    {
306
0
      *pinfo = orig_info;
307
0
      pinfo->codepoint = glyph_data[i];
308
0
      pinfo++;
309
0
    }
310
311
0
    idx  += num_in;
312
0
    out_len += num_out;
313
0
    return true;
314
0
  }
315
316
  HB_NODISCARD bool replace_glyph (hb_codepoint_t glyph_index)
317
0
  { return replace_glyphs (1, 1, &glyph_index); }
318
319
  /* Makes a copy of the glyph at idx to output and replace glyph_index */
320
  HB_NODISCARD bool output_glyph (hb_codepoint_t glyph_index)
321
0
  { return replace_glyphs (0, 1, &glyph_index); }
322
323
  HB_NODISCARD bool output_info (const hb_glyph_info_t &glyph_info)
324
0
  {
325
0
    if (unlikely (!make_room_for (0, 1))) return false;
326
327
0
    out_info[out_len] = glyph_info;
328
329
0
    out_len++;
330
0
    return true;
331
0
  }
332
  /* Copies glyph at idx to output but doesn't advance idx */
333
  HB_NODISCARD bool copy_glyph ()
334
0
  {
335
    /* Extra copy because cur()'s return can be freed within
336
     * output_info() call if buffer reallocates. */
337
0
    return output_info (hb_glyph_info_t (cur()));
338
0
  }
339
340
  /* Copies glyph at idx to output and advance idx.
341
   * If there's no output, just advance idx. */
342
  HB_NODISCARD bool next_glyph ()
343
199k
  {
344
199k
    if (have_output)
345
199k
    {
346
199k
      if (out_info != info || out_len != idx)
347
0
      {
348
0
  if (unlikely (!ensure (out_len + 1))) return false;
349
0
  out_info[out_len] = info[idx];
350
0
      }
351
199k
      out_len++;
352
199k
    }
353
354
199k
    idx++;
355
199k
    return true;
356
199k
  }
357
  /* Copies n glyphs at idx to output and advance idx.
358
   * If there's no output, just advance idx. */
359
  HB_NODISCARD bool next_glyphs (unsigned int n)
360
250k
  {
361
250k
    if (have_output)
362
250k
    {
363
250k
      if (out_info != info || out_len != idx)
364
0
      {
365
0
  if (unlikely (!make_room_for (n, n))) return false;
366
0
  memmove (out_info + out_len, info + idx, n * sizeof (out_info[0]));
367
0
      }
368
250k
      out_len += n;
369
250k
    }
370
371
250k
    idx += n;
372
250k
    return true;
373
250k
  }
374
  /* Advance idx without copying to output. */
375
0
  void skip_glyph () { idx++; }
376
  void reset_masks (hb_mask_t mask)
377
125k
  {
378
1.10M
    for (unsigned int j = 0; j < len; j++)
379
979k
      info[j].mask = mask;
380
125k
  }
381
  void add_masks (hb_mask_t mask)
382
0
  {
383
0
    for (unsigned int j = 0; j < len; j++)
384
0
      info[j].mask |= mask;
385
0
  }
386
  HB_INTERNAL void set_masks (hb_mask_t value, hb_mask_t mask,
387
            unsigned int cluster_start, unsigned int cluster_end);
388
389
  void merge_clusters (unsigned int start, unsigned int end)
390
480k
  {
391
480k
    if (end - start < 2)
392
480k
      return;
393
440
    merge_clusters_impl (start, end);
394
440
  }
395
  HB_INTERNAL void merge_clusters_impl (unsigned int start, unsigned int end);
396
  HB_INTERNAL void merge_out_clusters (unsigned int start, unsigned int end);
397
  /* Merge clusters for deleting current glyph, and skip it. */
398
  HB_INTERNAL void delete_glyph ();
399
  HB_INTERNAL void delete_glyphs_inplace (bool (*filter) (const hb_glyph_info_t *info));
400
401
402
403
  /* Adds glyph flags in mask to infos with clusters between start and end.
404
   * The start index will be from out-buffer if from_out_buffer is true.
405
   * If interior is true, then the cluster having the minimum value is skipped. */
406
  void _set_glyph_flags (hb_mask_t mask,
407
       unsigned start = 0,
408
       unsigned end = (unsigned) -1,
409
       bool interior = false,
410
       bool from_out_buffer = false)
411
35
  {
412
35
    if (unlikely (end != (unsigned) -1 && end - start > 255))
413
0
      return;
414
415
35
    end = hb_min (end, len);
416
417
35
    if (interior && !from_out_buffer && end - start < 2)
418
3
      return;
419
420
32
    scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GLYPH_FLAGS;
421
422
32
    if (!from_out_buffer || !have_output)
423
32
    {
424
32
      if (!interior)
425
0
      {
426
0
  for (unsigned i = start; i < end; i++)
427
0
    info[i].mask |= mask;
428
0
      }
429
32
      else
430
32
      {
431
32
  unsigned cluster = _infos_find_min_cluster (info, start, end);
432
32
  _infos_set_glyph_flags (info, start, end, cluster, mask);
433
32
      }
434
32
    }
435
0
    else
436
0
    {
437
0
      assert (start <= out_len);
438
0
      assert (idx <= end);
439
440
0
      if (!interior)
441
0
      {
442
0
  for (unsigned i = start; i < out_len; i++)
443
0
    out_info[i].mask |= mask;
444
0
  for (unsigned i = idx; i < end; i++)
445
0
    info[i].mask |= mask;
446
0
      }
447
0
      else
448
0
      {
449
0
  unsigned cluster = _infos_find_min_cluster (info, idx, end);
450
0
  cluster = _infos_find_min_cluster (out_info, start, out_len, cluster);
451
452
0
  _infos_set_glyph_flags (out_info, start, out_len, cluster, mask);
453
0
  _infos_set_glyph_flags (info, idx, end, cluster, mask);
454
0
      }
455
0
    }
456
32
  }
457
458
  void unsafe_to_break (unsigned int start = 0, unsigned int end = -1)
459
35
  {
460
35
    _set_glyph_flags (HB_GLYPH_FLAG_UNSAFE_TO_BREAK | HB_GLYPH_FLAG_UNSAFE_TO_CONCAT,
461
35
          start, end,
462
35
          true);
463
35
  }
464
  void safe_to_insert_tatweel (unsigned int start = 0, unsigned int end = -1)
465
0
  {
466
0
    if ((flags & HB_BUFFER_FLAG_PRODUCE_SAFE_TO_INSERT_TATWEEL) == 0)
467
0
    {
468
0
      unsafe_to_break (start, end);
469
0
      return;
470
0
    }
471
0
    _set_glyph_flags (HB_GLYPH_FLAG_SAFE_TO_INSERT_TATWEEL,
472
0
          start, end,
473
0
          true);
474
0
  }
475
#ifndef HB_OPTIMIZE_SIZE
476
  HB_ALWAYS_INLINE
477
#endif
478
  void unsafe_to_concat (unsigned int start = 0, unsigned int end = -1)
479
1.19k
  {
480
1.19k
    if (likely ((flags & HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT) == 0))
481
1.19k
      return;
482
0
    _set_glyph_flags (HB_GLYPH_FLAG_UNSAFE_TO_CONCAT,
483
0
          start, end,
484
0
          false);
485
0
  }
486
  void unsafe_to_break_from_outbuffer (unsigned int start = 0, unsigned int end = -1)
487
0
  {
488
0
    _set_glyph_flags (HB_GLYPH_FLAG_UNSAFE_TO_BREAK | HB_GLYPH_FLAG_UNSAFE_TO_CONCAT,
489
0
          start, end,
490
0
          true, true);
491
0
  }
492
#ifndef HB_OPTIMIZE_SIZE
493
  HB_ALWAYS_INLINE
494
#endif
495
  void unsafe_to_concat_from_outbuffer (unsigned int start = 0, unsigned int end = -1)
496
0
  {
497
0
    if (likely ((flags & HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT) == 0))
498
0
      return;
499
0
    _set_glyph_flags (HB_GLYPH_FLAG_UNSAFE_TO_CONCAT,
500
0
          start, end,
501
0
          false, true);
502
0
  }
503
504
505
  /* Internal methods */
506
  HB_NODISCARD HB_INTERNAL bool move_to (unsigned int i); /* i is output-buffer index. */
507
508
  HB_NODISCARD HB_INTERNAL bool enlarge (unsigned int size);
509
510
  HB_NODISCARD bool resize (unsigned length)
511
0
  {
512
0
    assert (!have_output);
513
0
    if (unlikely (!ensure (length))) return false;
514
0
    len = length;
515
0
    return true;
516
0
  }
517
  HB_NODISCARD bool ensure (unsigned int size)
518
1.22M
  { return likely (!size || size < allocated) ? true : enlarge (size); }
519
520
  HB_NODISCARD bool ensure_inplace (unsigned int size)
521
0
  { return likely (!size || size < allocated); }
522
523
  void assert_glyphs ()
524
0
  {
525
0
    assert ((content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS) ||
526
0
      (!len && (content_type == HB_BUFFER_CONTENT_TYPE_INVALID)));
527
0
  }
528
  void assert_unicode ()
529
250k
  {
530
250k
    assert ((content_type == HB_BUFFER_CONTENT_TYPE_UNICODE) ||
531
250k
      (!len && (content_type == HB_BUFFER_CONTENT_TYPE_INVALID)));
532
250k
  }
533
  HB_NODISCARD bool ensure_glyphs ()
534
0
  {
535
0
    if (unlikely (content_type != HB_BUFFER_CONTENT_TYPE_GLYPHS))
536
0
    {
537
0
      if (content_type != HB_BUFFER_CONTENT_TYPE_INVALID)
538
0
  return false;
539
0
      assert (len == 0);
540
0
      content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
541
0
    }
542
0
    return true;
543
0
  }
544
  HB_NODISCARD bool ensure_unicode ()
545
0
  {
546
0
    if (unlikely (content_type != HB_BUFFER_CONTENT_TYPE_UNICODE))
547
0
    {
548
0
      if (content_type != HB_BUFFER_CONTENT_TYPE_INVALID)
549
0
  return false;
550
0
      assert (len == 0);
551
0
      content_type = HB_BUFFER_CONTENT_TYPE_UNICODE;
552
0
    }
553
0
    return true;
554
0
  }
555
556
  HB_NODISCARD HB_INTERNAL bool make_room_for (unsigned int num_in, unsigned int num_out);
557
  HB_NODISCARD HB_INTERNAL bool shift_forward (unsigned int count);
558
559
  typedef long scratch_buffer_t;
560
  HB_INTERNAL scratch_buffer_t *get_scratch_buffer (unsigned int *size);
561
562
125k
  void clear_context (unsigned int side) { context_len[side] = 0; }
563
564
  HB_INTERNAL void sort (unsigned int start, unsigned int end, int(*compar)(const hb_glyph_info_t *, const hb_glyph_info_t *));
565
566
  bool messaging ()
567
524k
  {
568
#ifdef HB_NO_BUFFER_MESSAGE
569
    return false;
570
#else
571
524k
    return unlikely (message_func);
572
524k
#endif
573
524k
  }
574
  bool message (hb_font_t *font, const char *fmt, ...) HB_PRINTF_FUNC(3, 4)
575
6.49k
  {
576
#ifdef HB_NO_BUFFER_MESSAGE
577
    return true;
578
#else
579
6.49k
    if (likely (!messaging ()))
580
6.49k
      return true;
581
582
0
    va_list ap;
583
0
    va_start (ap, fmt);
584
0
    bool ret = message_impl (font, fmt, ap);
585
0
    va_end (ap);
586
587
0
    return ret;
588
6.49k
#endif
589
6.49k
  }
590
  HB_INTERNAL bool message_impl (hb_font_t *font, const char *fmt, va_list ap) HB_PRINTF_FUNC(3, 0);
591
592
  static void
593
  set_cluster (hb_glyph_info_t &inf, unsigned int cluster, unsigned int mask = 0)
594
880
  {
595
880
    if (inf.cluster != cluster)
596
440
      inf.mask = (inf.mask & ~HB_GLYPH_FLAG_DEFINED) | (mask & HB_GLYPH_FLAG_DEFINED);
597
880
    inf.cluster = cluster;
598
880
  }
599
  void
600
  _infos_set_glyph_flags (hb_glyph_info_t *infos,
601
        unsigned int start, unsigned int end,
602
        unsigned int cluster,
603
        hb_mask_t mask)
604
32
  {
605
32
    if (unlikely (start == end))
606
0
      return;
607
608
32
    unsigned cluster_first = infos[start].cluster;
609
32
    unsigned cluster_last = infos[end - 1].cluster;
610
611
32
    if (cluster_level == HB_BUFFER_CLUSTER_LEVEL_CHARACTERS ||
612
32
  (cluster != cluster_first && cluster != cluster_last))
613
0
    {
614
0
      for (unsigned int i = start; i < end; i++)
615
0
  if (cluster != infos[i].cluster)
616
0
  {
617
0
    scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GLYPH_FLAGS;
618
0
    infos[i].mask |= mask;
619
0
  }
620
0
      return;
621
0
    }
622
623
    /* Monotone clusters */
624
625
32
    if (cluster == cluster_first)
626
32
    {
627
32
      for (unsigned int i = end; start < i && infos[i - 1].cluster != cluster_first; i--)
628
0
      {
629
0
  scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GLYPH_FLAGS;
630
0
  infos[i - 1].mask |= mask;
631
0
      }
632
32
    }
633
0
    else /* cluster == cluster_last */
634
0
    {
635
0
      for (unsigned int i = start; i < end && infos[i].cluster != cluster_last; i++)
636
0
      {
637
0
  scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GLYPH_FLAGS;
638
0
  infos[i].mask |= mask;
639
0
      }
640
0
    }
641
32
  }
642
  unsigned
643
  _infos_find_min_cluster (const hb_glyph_info_t *infos,
644
         unsigned start, unsigned end,
645
         unsigned cluster = UINT_MAX)
646
32
  {
647
32
    if (unlikely (start == end))
648
0
      return cluster;
649
650
32
    if (cluster_level == HB_BUFFER_CLUSTER_LEVEL_CHARACTERS)
651
0
    {
652
0
      for (unsigned int i = start; i < end; i++)
653
0
  cluster = hb_min (cluster, infos[i].cluster);
654
0
      return cluster;
655
0
    }
656
657
32
    return hb_min (cluster, hb_min (infos[start].cluster, infos[end - 1].cluster));
658
32
  }
659
660
  void clear_glyph_flags (hb_mask_t mask = 0)
661
0
  {
662
0
    for (unsigned int i = 0; i < len; i++)
663
0
      info[i].mask = (info[i].mask & ~HB_GLYPH_FLAG_DEFINED) | (mask & HB_GLYPH_FLAG_DEFINED);
664
0
  }
665
};
666
DECLARE_NULL_INSTANCE (hb_buffer_t);
667
668
669
#define foreach_group(buffer, start, end, group_func) \
670
40.7k
  for (unsigned int \
671
40.7k
       _count = buffer->len, \
672
40.7k
       start = 0, end = _count ? buffer->group_end (0, group_func) : 0; \
673
521k
       start < _count; \
674
480k
       start = end, end = buffer->group_end (start, group_func))
675
676
#define foreach_cluster(buffer, start, end) \
677
32
  foreach_group (buffer, start, end, hb_buffer_t::_cluster_group_func)
678
679
680
#define HB_BUFFER_XALLOCATE_VAR(b, func, var) \
681
1.62M
  b->func (offsetof (hb_glyph_info_t, var) - offsetof(hb_glyph_info_t, var1), \
682
1.62M
     sizeof (b->info[0].var))
683
500k
#define HB_BUFFER_ALLOCATE_VAR(b, var)    HB_BUFFER_XALLOCATE_VAR (b, allocate_var,     var ())
684
0
#define HB_BUFFER_TRY_ALLOCATE_VAR(b, var)  HB_BUFFER_XALLOCATE_VAR (b, try_allocate_var, var ())
685
500k
#define HB_BUFFER_DEALLOCATE_VAR(b, var)  HB_BUFFER_XALLOCATE_VAR (b, deallocate_var,   var ())
686
625k
#define HB_BUFFER_ASSERT_VAR(b, var)    HB_BUFFER_XALLOCATE_VAR (b, assert_var,       var ())
687
688
689
#endif /* HB_BUFFER_HH */