Coverage Report

Created: 2025-07-07 10:01

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