Coverage Report

Created: 2026-10-14 09:33

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/workdir/UnpackedTarball/harfbuzz/src/hb-buffer.hh
Line
Count
Source
1
/*
2
 * Copyright © 1998-2004  David Turner and Werner Lemberg
3
 * Copyright © 2004,2007,2009,2010  Red Hat, Inc.
4
 * Copyright © 2011,2012  Google, Inc.
5
 *
6
 *  This is part of HarfBuzz, a text shaping library.
7
 *
8
 * Permission is hereby granted, without written agreement and without
9
 * license or royalty fees, to use, copy, modify, and distribute this
10
 * software and its documentation for any purpose, provided that the
11
 * above copyright notice and the following two paragraphs appear in
12
 * all copies of this software.
13
 *
14
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
15
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
17
 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18
 * DAMAGE.
19
 *
20
 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
21
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22
 * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
23
 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
24
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25
 *
26
 * Red Hat Author(s): Owen Taylor, Behdad Esfahbod
27
 * Google Author(s): Behdad Esfahbod
28
 */
29
30
#ifndef HB_BUFFER_HH
31
#define HB_BUFFER_HH
32
33
#include "hb.hh"
34
#include "hb-unicode.hh"
35
#include "hb-set-digest.hh"
36
37
38
static_assert ((sizeof (hb_glyph_info_t) == 20), "");
39
static_assert ((sizeof (hb_glyph_info_t) == sizeof (hb_glyph_position_t)), "");
40
41
HB_MARK_AS_FLAG_T (hb_glyph_flags_t);
42
HB_MARK_AS_FLAG_T (hb_buffer_flags_t);
43
HB_MARK_AS_FLAG_T (hb_buffer_serialize_flags_t);
44
HB_MARK_AS_FLAG_T (hb_buffer_diff_flags_t);
45
46
enum hb_buffer_scratch_flags_t {
47
  HB_BUFFER_SCRATCH_FLAG_DEFAULT      = 0x00000000u,
48
  HB_BUFFER_SCRATCH_FLAG_HAS_FRACTION_SLASH   = 0x00000001u,
49
  HB_BUFFER_SCRATCH_FLAG_HAS_DEFAULT_IGNORABLES   = 0x00000002u,
50
  HB_BUFFER_SCRATCH_FLAG_HAS_SPACE_FALLBACK   = 0x00000004u,
51
  HB_BUFFER_SCRATCH_FLAG_HAS_GPOS_ATTACHMENT    = 0x00000008u,
52
  HB_BUFFER_SCRATCH_FLAG_HAS_CGJ      = 0x00000010u,
53
  HB_BUFFER_SCRATCH_FLAG_HAS_BROKEN_SYLLABLE    = 0x00000020u,
54
  HB_BUFFER_SCRATCH_FLAG_HAS_VARIATION_SELECTOR_FALLBACK= 0x00000040u,
55
  HB_BUFFER_SCRATCH_FLAG_HAS_CONTINUATIONS    = 0x00000080u,
56
57
  /* Reserved for shapers' internal use. */
58
  HB_BUFFER_SCRATCH_FLAG_SHAPER0      = 0x01000000u,
59
  HB_BUFFER_SCRATCH_FLAG_SHAPER1      = 0x02000000u,
60
  HB_BUFFER_SCRATCH_FLAG_SHAPER2      = 0x04000000u,
61
  HB_BUFFER_SCRATCH_FLAG_SHAPER3      = 0x08000000u,
62
};
63
HB_MARK_AS_FLAG_T (hb_buffer_scratch_flags_t);
64
65
66
/*
67
 * hb_buffer_t
68
 */
69
70
struct hb_buffer_t
71
{
72
  hb_object_header_t header;
73
74
  /*
75
   * Information about how the text in the buffer should be treated.
76
   */
77
78
  hb_unicode_funcs_t *unicode; /* Unicode functions */
79
  hb_buffer_flags_t flags; /* BOT / EOT / etc. */
80
  hb_buffer_cluster_level_t cluster_level;
81
  hb_codepoint_t replacement; /* U+FFFD or something else. */
82
  hb_codepoint_t invisible; /* 0 or something else. */
83
  hb_codepoint_t not_found; /* 0 or something else. */
84
  hb_codepoint_t not_found_variation_selector; /* HB_CODEPOINT_INVALID or something else. */
85
86
  /*
87
   * Buffer contents
88
   */
89
90
  hb_buffer_content_type_t content_type;
91
  hb_segment_properties_t props; /* Script, language, direction */
92
93
  bool successful; /* Allocations successful */
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
  hb_set_digest_t digest; /* Manually updated sometimes */
114
115
  /*
116
   * Managed by enter / leave
117
   */
118
119
  uint8_t allocated_var_bits;
120
  uint8_t serial;
121
  uint32_t random_state;
122
  hb_buffer_scratch_flags_t scratch_flags; /* Have space-fallback, etc. */
123
  unsigned int max_len; /* Maximum allowed len. */
124
  int max_ops; /* Maximum allowed operations. */
125
  /* The bits here reflect current allocations of the bytes in glyph_info_t's var1 and var2. */
126
127
128
  /*
129
   * Messaging callback
130
   */
131
132
#ifndef HB_NO_BUFFER_MESSAGE
133
  typedef void (*changed_func_t) (hb_buffer_t *buffer, void *user_data);
134
135
  hb_buffer_message_func_t message_func;
136
  void *message_data;
137
  hb_destroy_func_t message_destroy;
138
  changed_func_t changed_func;
139
  void *changed_data;
140
  unsigned message_depth; /* How deeply are we inside a message callback? */
141
#else
142
  static constexpr unsigned message_depth = 0u;
143
#endif
144
145
146
147
  /* Methods */
148
149
0
  HB_NODISCARD bool in_error () const { return !successful; }
150
151
  void allocate_var (unsigned int start, unsigned int count)
152
71.5M
  {
153
71.5M
    unsigned int end = start + count;
154
71.5M
    assert (end <= 8);
155
71.5M
    unsigned int bits = (1u<<end) - (1u<<start);
156
71.5M
    assert (0 == (allocated_var_bits & bits));
157
71.5M
    allocated_var_bits |= bits;
158
71.5M
  }
159
  bool try_allocate_var (unsigned int start, unsigned int count)
160
0
  {
161
0
    unsigned int end = start + count;
162
0
    assert (end <= 8);
163
0
    unsigned int bits = (1u<<end) - (1u<<start);
164
0
    if (allocated_var_bits & bits)
165
0
      return false;
166
0
    allocated_var_bits |= bits;
167
0
    return true;
168
0
  }
169
  void deallocate_var (unsigned int start, unsigned int count)
170
71.5M
  {
171
71.5M
    unsigned int end = start + count;
172
71.5M
    assert (end <= 8);
173
71.5M
    unsigned int bits = (1u<<end) - (1u<<start);
174
71.5M
    assert (bits == (allocated_var_bits & bits));
175
71.5M
    allocated_var_bits &= ~bits;
176
71.5M
  }
177
  void assert_var (unsigned int start, unsigned int count)
178
87.8M
  {
179
87.8M
    unsigned int end = start + count;
180
87.8M
    assert (end <= 8);
181
87.8M
    HB_UNUSED unsigned int bits = (1u<<end) - (1u<<start);
182
87.8M
    assert (bits == (allocated_var_bits & bits));
183
87.8M
  }
184
  void deallocate_var_all ()
185
58.8M
  {
186
58.8M
    allocated_var_bits = 0;
187
58.8M
  }
188
189
  HB_ALWAYS_INLINE
190
318M
  hb_glyph_info_t &cur (unsigned int i = 0) { return info[idx + i]; }
191
  HB_ALWAYS_INLINE
192
0
  hb_glyph_info_t cur (unsigned int i = 0) const { return info[idx + i]; }
193
194
  HB_ALWAYS_INLINE
195
2.33M
  hb_glyph_position_t &cur_pos (unsigned int i = 0) { return pos[idx + i]; }
196
  HB_ALWAYS_INLINE
197
0
  hb_glyph_position_t cur_pos (unsigned int i = 0) const { return pos[idx + i]; }
198
199
  HB_ALWAYS_INLINE
200
4.72M
  hb_glyph_info_t &prev ()      { return out_info[out_len ? out_len - 1 : 0]; }
201
  HB_ALWAYS_INLINE
202
0
  hb_glyph_info_t prev () const { return out_info[out_len ? out_len - 1 : 0]; }
203
204
  template <typename set_t>
205
  void collect_codepoints (set_t &d) const
206
2.17M
  { 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
206
1.78M
  { 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
206
387k
  { d.clear (); d.add_array (&info[0].codepoint, len, sizeof (info[0])); }
207
208
  void update_digest ()
209
387k
  {
210
387k
    digest = hb_set_digest_t ();
211
387k
    collect_codepoints (digest);
212
387k
  }
213
214
  HB_INTERNAL void similar (const hb_buffer_t &src);
215
  HB_INTERNAL void reset ();
216
  HB_INTERNAL void clear ();
217
218
  /* Called around shape() */
219
  HB_INTERNAL void enter ();
220
  HB_INTERNAL void leave ();
221
222
#ifndef HB_NO_BUFFER_VERIFY
223
  HB_INTERNAL
224
#endif
225
  bool verify (hb_buffer_t        *text_buffer,
226
         hb_font_t          *font,
227
         const hb_feature_t *features,
228
         unsigned int        num_features,
229
         const char * const *shapers)
230
#ifndef HB_NO_BUFFER_VERIFY
231
  ;
232
#else
233
  { return true; }
234
#endif
235
236
1.43M
  unsigned int backtrack_len () const { return have_output ? out_len : idx; }
237
39.5k
  unsigned int lookahead_len () const { return len - idx; }
238
0
  uint8_t next_serial () { return ++serial ? serial : ++serial; }
239
240
  HB_INTERNAL void add (hb_codepoint_t  codepoint,
241
      unsigned int    cluster);
242
  HB_INTERNAL void add_info (const hb_glyph_info_t &glyph_info);
243
  HB_INTERNAL void add_info_and_pos (const hb_glyph_info_t &glyph_info,
244
             const hb_glyph_position_t &glyph_pos);
245
246
  void reverse_range (unsigned start, unsigned end)
247
46.4M
  {
248
46.4M
    hb_array_t<hb_glyph_info_t> (info, len).reverse (start, end);
249
46.4M
    if (have_positions)
250
1.02M
      hb_array_t<hb_glyph_position_t> (pos, len).reverse (start, end);
251
46.4M
  }
252
1.96M
  void reverse () { reverse_range (0, len); }
253
254
  template <typename FuncType>
255
  void reverse_groups (const FuncType& group,
256
           bool merge_clusters = false)
257
943k
  {
258
943k
    if (unlikely (!len))
259
0
      return;
260
261
943k
    unsigned start = 0;
262
943k
    unsigned i;
263
44.5M
    for (i = 1; i < len; i++)
264
43.6M
    {
265
43.6M
      if (!group (info[i - 1], info[i]))
266
43.4M
      {
267
43.4M
  if (merge_clusters)
268
0
    this->merge_clusters (start, i);
269
43.4M
  reverse_range (start, i);
270
43.4M
  start = i;
271
43.4M
      }
272
43.6M
    }
273
943k
    if (merge_clusters)
274
0
      this->merge_clusters (start, i);
275
943k
    reverse_range (start, i);
276
277
943k
    reverse ();
278
943k
  }
279
280
  template <typename FuncType>
281
  unsigned group_end (unsigned start, const FuncType& group) const
282
266M
  {
283
269M
    while (++start < len && group (info[start - 1], info[start]))
284
3.29M
      ;
285
286
266M
    return start;
287
266M
  }
288
289
  static bool _cluster_group_func (const hb_glyph_info_t& a,
290
           const hb_glyph_info_t& b)
291
221M
  { return a.cluster == b.cluster; }
292
293
0
  void reverse_clusters () { reverse_groups (_cluster_group_func); }
294
295
  HB_INTERNAL void guess_segment_properties ();
296
297
  HB_INTERNAL bool sync ();
298
  HB_INTERNAL int sync_so_far ();
299
  HB_INTERNAL void clear_output ();
300
  HB_INTERNAL void clear_positions ();
301
302
  template <typename T>
303
  HB_NODISCARD bool replace_glyphs (unsigned int num_in,
304
            unsigned int num_out,
305
            const T *glyph_data)
306
199k
  {
307
199k
    if (unlikely (!make_room_for (num_in, num_out))) return false;
308
309
199k
    assert (idx + num_in <= len);
310
311
199k
    merge_clusters (idx, idx + num_in);
312
313
199k
    hb_glyph_info_t &orig_info = idx < len ? cur() : prev();
314
315
199k
    hb_glyph_info_t *pinfo = &out_info[out_len];
316
398k
    for (unsigned int i = 0; i < num_out; i++)
317
199k
    {
318
199k
      *pinfo = orig_info;
319
199k
      pinfo->codepoint = glyph_data[i];
320
199k
      pinfo++;
321
199k
    }
322
323
199k
    idx  += num_in;
324
199k
    out_len += num_out;
325
199k
    return true;
326
199k
  }
327
328
  HB_NODISCARD bool replace_glyph (hb_codepoint_t glyph_index)
329
86.0k
  { return replace_glyphs (1, 1, &glyph_index); }
330
331
  /* Makes a copy of the glyph at idx to output and replace glyph_index */
332
  HB_NODISCARD bool output_glyph (hb_codepoint_t glyph_index)
333
112k
  { return replace_glyphs (0, 1, &glyph_index); }
334
335
  HB_NODISCARD bool output_info (const hb_glyph_info_t &glyph_info)
336
3.28k
  {
337
3.28k
    if (unlikely (!make_room_for (0, 1))) return false;
338
339
3.28k
    out_info[out_len] = glyph_info;
340
341
3.28k
    out_len++;
342
3.28k
    return true;
343
3.28k
  }
344
  /* Copies glyph at idx to output but doesn't advance idx */
345
  HB_NODISCARD bool copy_glyph ()
346
0
  {
347
    /* Extra copy because cur()'s return can be freed within
348
     * output_info() call if buffer reallocates. */
349
0
    return output_info (hb_glyph_info_t (cur()));
350
0
  }
351
352
  /* Copies glyph at idx to output and advance idx.
353
   * If there's no output, just advance idx. */
354
  HB_NODISCARD bool next_glyph ()
355
206M
  {
356
206M
    if (have_output)
357
50.1M
    {
358
50.1M
      if (out_info != info || out_len != idx)
359
284k
      {
360
284k
  if (unlikely (!ensure (out_len + 1))) return false;
361
284k
  out_info[out_len] = info[idx];
362
284k
      }
363
50.1M
      out_len++;
364
50.1M
    }
365
366
206M
    idx++;
367
206M
    return true;
368
206M
  }
369
  /* Copies n glyphs at idx to output and advance idx.
370
   * If there's no output, just advance idx. */
371
  HB_NODISCARD bool next_glyphs (unsigned int n)
372
52.6M
  {
373
52.6M
    if (have_output)
374
37.2M
    {
375
37.2M
      if (out_info != info || out_len != idx)
376
57.4k
      {
377
57.4k
  if (unlikely (!ensure (out_len + n))) return false;
378
57.4k
  memmove (out_info + out_len, info + idx, n * sizeof (out_info[0]));
379
57.4k
      }
380
37.2M
      out_len += n;
381
37.2M
    }
382
383
52.6M
    idx += n;
384
52.6M
    return true;
385
52.6M
  }
386
  /* Advance idx without copying to output. */
387
20.1k
  void skip_glyph () { idx++; }
388
  void add_masks (hb_mask_t mask)
389
0
  {
390
0
    for (unsigned int j = 0; j < len; j++)
391
0
      info[j].mask |= mask;
392
0
  }
393
  HB_INTERNAL void set_masks (hb_mask_t value, hb_mask_t mask,
394
            unsigned int cluster_start, unsigned int cluster_end);
395
396
  void merge_clusters (unsigned int start, unsigned int end)
397
229k
  {
398
229k
    if (end - start < 2)
399
199k
      return;
400
29.9k
    if (!HB_BUFFER_CLUSTER_LEVEL_IS_MONOTONE (cluster_level))
401
4
    {
402
4
      unsafe_to_break (start, end);
403
4
      return;
404
4
    }
405
29.9k
    merge_clusters_impl (start, end);
406
29.9k
  }
407
  void merge_grapheme_clusters (unsigned int start, unsigned int end)
408
10.2M
  {
409
10.2M
    if (end - start < 2)
410
9.53M
      return;
411
711k
    if (!HB_BUFFER_CLUSTER_LEVEL_IS_GRAPHEMES (cluster_level))
412
7.75k
    {
413
7.75k
      unsafe_to_break (start, end);
414
7.75k
      return;
415
7.75k
    }
416
703k
    merge_clusters_impl (start, end);
417
703k
  }
418
  HB_INTERNAL void merge_clusters_impl (unsigned int start, unsigned int end);
419
  void merge_out_clusters (unsigned int start, unsigned int end)
420
20.1k
  {
421
20.1k
    if (end - start < 2)
422
0
      return;
423
20.1k
    if (!HB_BUFFER_CLUSTER_LEVEL_IS_MONOTONE (cluster_level))
424
54
      return;
425
20.0k
    merge_out_clusters_impl (start, end);
426
20.0k
  }
427
  void merge_out_grapheme_clusters (unsigned int start, unsigned int end)
428
71.6k
  {
429
71.6k
    if (end - start < 2)
430
0
      return;
431
71.6k
    if (!HB_BUFFER_CLUSTER_LEVEL_IS_GRAPHEMES (cluster_level))
432
0
      return;
433
71.6k
    merge_out_clusters_impl (start, end);
434
71.6k
  }
435
  HB_INTERNAL void merge_out_clusters_impl (unsigned int start, unsigned int end);
436
  /* Merge clusters for deleting current glyph, and skip it. */
437
  HB_INTERNAL void delete_glyph ();
438
  HB_INTERNAL void delete_glyphs_inplace (bool (*filter) (const hb_glyph_info_t *info));
439
440
441
442
  /* Adds glyph flags in mask to infos with clusters between start and end.
443
   * The start index will be from out-buffer if from_out_buffer is true.
444
   * If interior is true, then the cluster having the minimum value is skipped. */
445
  void _set_glyph_flags_impl (hb_mask_t mask,
446
            unsigned start,
447
            unsigned end,
448
            bool interior,
449
            bool from_out_buffer)
450
36.0M
  {
451
36.0M
    if (!from_out_buffer || !have_output)
452
36.0M
    {
453
36.0M
      if (!interior)
454
33.5M
      {
455
132M
  for (unsigned i = start; i < end; i++)
456
98.9M
    info[i].mask |= mask;
457
33.5M
      }
458
2.47M
      else
459
2.47M
      {
460
2.47M
  unsigned cluster = _infos_find_min_cluster (info, start, end);
461
2.47M
  _infos_set_glyph_flags (info, start, end, cluster, mask);
462
2.47M
      }
463
36.0M
    }
464
6.30k
    else
465
6.30k
    {
466
6.30k
      assert (start <= out_len);
467
6.30k
      assert (idx <= end);
468
469
6.30k
      if (!interior)
470
0
      {
471
0
  for (unsigned i = start; i < out_len; i++)
472
0
    out_info[i].mask |= mask;
473
0
  for (unsigned i = idx; i < end; i++)
474
0
    info[i].mask |= mask;
475
0
      }
476
6.30k
      else
477
6.30k
      {
478
6.30k
  unsigned cluster = _infos_find_min_cluster (info, idx, end);
479
6.30k
  cluster = _infos_find_min_cluster (out_info, start, out_len, cluster);
480
481
6.30k
  _infos_set_glyph_flags (out_info, start, out_len, cluster, mask);
482
6.30k
  _infos_set_glyph_flags (info, idx, end, cluster, mask);
483
6.30k
      }
484
6.30k
    }
485
36.0M
  }
486
487
  HB_ALWAYS_INLINE
488
  void _set_glyph_flags (hb_mask_t mask,
489
       unsigned start = 0,
490
       unsigned end = (unsigned) -1,
491
       bool interior = false,
492
       bool from_out_buffer = false)
493
36.2M
  {
494
36.2M
    if (unlikely (end != (unsigned) -1 && end - start > 255))
495
84.3k
      return;
496
497
36.1M
    end = hb_min (end, len);
498
499
36.1M
    if (interior && !from_out_buffer && end - start < 2)
500
119k
      return;
501
502
36.0M
    _set_glyph_flags_impl (mask, start, end, interior, from_out_buffer);
503
36.0M
  }
504
505
506
  void unsafe_to_break (unsigned int start = 0, unsigned int end = -1)
507
2.54M
  {
508
2.54M
    _set_glyph_flags (HB_GLYPH_FLAG_UNSAFE_TO_BREAK | HB_GLYPH_FLAG_UNSAFE_TO_CONCAT,
509
2.54M
          start, end,
510
2.54M
          true);
511
2.54M
  }
512
  void safe_to_insert_tatweel (unsigned int start = 0, unsigned int end = -1)
513
66.4k
  {
514
66.4k
    if ((flags & HB_BUFFER_FLAG_PRODUCE_SAFE_TO_INSERT_TATWEEL) == 0)
515
0
    {
516
0
      unsafe_to_break (start, end);
517
0
      return;
518
0
    }
519
66.4k
    _set_glyph_flags (HB_GLYPH_FLAG_SAFE_TO_INSERT_TATWEEL,
520
66.4k
          start, end,
521
66.4k
          true);
522
66.4k
  }
523
#ifndef HB_OPTIMIZE_SIZE
524
  HB_ALWAYS_INLINE
525
#endif
526
  void unsafe_to_concat (unsigned int start = 0, unsigned int end = -1)
527
32.3M
  {
528
32.3M
    if (likely ((flags & HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT) == 0))
529
684k
      return;
530
31.6M
    _set_glyph_flags (HB_GLYPH_FLAG_UNSAFE_TO_CONCAT,
531
31.6M
          start, end,
532
31.6M
          false);
533
31.6M
  }
534
  void unsafe_to_break_from_outbuffer (unsigned int start = 0, unsigned int end = -1)
535
19.9k
  {
536
19.9k
    _set_glyph_flags (HB_GLYPH_FLAG_UNSAFE_TO_BREAK | HB_GLYPH_FLAG_UNSAFE_TO_CONCAT,
537
19.9k
          start, end,
538
19.9k
          true, true);
539
19.9k
  }
540
#ifndef HB_OPTIMIZE_SIZE
541
  HB_ALWAYS_INLINE
542
#endif
543
  void unsafe_to_concat_from_outbuffer (unsigned int start = 0, unsigned int end = -1)
544
1.94M
  {
545
1.94M
    if (likely ((flags & HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT) == 0))
546
7.95k
      return;
547
1.93M
    _set_glyph_flags (HB_GLYPH_FLAG_UNSAFE_TO_CONCAT,
548
1.93M
          start, end,
549
1.93M
          false, true);
550
1.93M
  }
551
552
553
  /* Internal methods */
554
  HB_NODISCARD HB_INTERNAL bool move_to (unsigned int i); /* i is output-buffer index. */
555
556
  HB_NODISCARD HB_INTERNAL bool enlarge (unsigned int size);
557
558
  HB_NODISCARD bool resize (unsigned length)
559
0
  {
560
0
    assert (!have_output);
561
0
    if (unlikely (!ensure (length))) return false;
562
0
    len = length;
563
0
    return true;
564
0
  }
565
  HB_NODISCARD bool ensure (unsigned int size)
566
263M
  { return likely (!size || size < allocated) ? true : enlarge (size); }
567
568
  HB_NODISCARD bool ensure_inplace (unsigned int size)
569
0
  { return likely (!size || size < allocated); }
570
571
  void assert_glyphs ()
572
0
  {
573
0
    assert ((content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS) ||
574
0
      (!len && (content_type == HB_BUFFER_CONTENT_TYPE_INVALID)));
575
0
  }
576
  void assert_unicode ()
577
35.1M
  {
578
35.1M
    assert ((content_type == HB_BUFFER_CONTENT_TYPE_UNICODE) ||
579
35.1M
      (!len && (content_type == HB_BUFFER_CONTENT_TYPE_INVALID)));
580
35.1M
  }
581
  HB_NODISCARD bool ensure_glyphs ()
582
0
  {
583
0
    if (unlikely (content_type != HB_BUFFER_CONTENT_TYPE_GLYPHS))
584
0
    {
585
0
      if (content_type != HB_BUFFER_CONTENT_TYPE_INVALID)
586
0
  return false;
587
0
      assert (len == 0);
588
0
      content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
589
0
    }
590
0
    return true;
591
0
  }
592
  HB_NODISCARD bool ensure_unicode ()
593
0
  {
594
0
    if (unlikely (content_type != HB_BUFFER_CONTENT_TYPE_UNICODE))
595
0
    {
596
0
      if (content_type != HB_BUFFER_CONTENT_TYPE_INVALID)
597
0
  return false;
598
0
      assert (len == 0);
599
0
      content_type = HB_BUFFER_CONTENT_TYPE_UNICODE;
600
0
    }
601
0
    return true;
602
0
  }
603
604
  HB_NODISCARD HB_INTERNAL bool make_room_for (unsigned int num_in, unsigned int num_out);
605
  HB_NODISCARD HB_INTERNAL bool shift_forward (unsigned int count);
606
607
  typedef long scratch_buffer_t;
608
  HB_INTERNAL scratch_buffer_t *get_scratch_buffer (unsigned int *size);
609
610
32.9M
  void clear_context (unsigned int side) { context_len[side] = 0; }
611
612
  HB_INTERNAL void sort (unsigned int start, unsigned int end, int(*compar)(const hb_glyph_info_t *, const hb_glyph_info_t *));
613
614
  bool messaging ()
615
573M
  {
616
#ifdef HB_NO_BUFFER_MESSAGE
617
    return false;
618
#else
619
573M
    return unlikely (message_func);
620
573M
#endif
621
573M
  }
622
  bool message (hb_font_t *font, const char *fmt, ...) HB_PRINTF_FUNC(3, 4)
623
76.7M
  {
624
#ifdef HB_NO_BUFFER_MESSAGE
625
    return true;
626
#else
627
76.7M
    if (likely (!messaging ()))
628
76.7M
      return true;
629
630
0
    va_list ap;
631
0
    va_start (ap, fmt);
632
0
    bool ret = message_impl (font, fmt, ap);
633
0
    va_end (ap);
634
635
0
    return ret;
636
76.7M
#endif
637
76.7M
  }
638
  HB_INTERNAL bool message_impl (hb_font_t *font, const char *fmt, va_list ap) HB_PRINTF_FUNC(3, 0);
639
  HB_INTERNAL void changed ();
640
641
  static void
642
  set_cluster (hb_glyph_info_t &inf, unsigned int cluster, unsigned int mask = 0)
643
7.48M
  {
644
7.48M
    if (inf.cluster != cluster)
645
1.72M
      inf.mask = (inf.mask & ~HB_GLYPH_FLAG_DEFINED) | (mask & HB_GLYPH_FLAG_DEFINED);
646
7.48M
    inf.cluster = cluster;
647
7.48M
  }
648
  void
649
  _infos_set_glyph_flags (hb_glyph_info_t *infos,
650
        unsigned int start, unsigned int end,
651
        unsigned int cluster,
652
        hb_mask_t mask)
653
2.48M
  {
654
2.48M
    if (unlikely (start == end))
655
6.30k
      return;
656
657
2.47M
    max_ops -= end - start;
658
2.47M
    if (unlikely (max_ops < 0))
659
0
      successful = false;
660
661
2.47M
    unsigned cluster_first = infos[start].cluster;
662
2.47M
    unsigned cluster_last = infos[end - 1].cluster;
663
664
2.47M
    if (cluster_level == HB_BUFFER_CLUSTER_LEVEL_CHARACTERS ||
665
2.34M
  (cluster != cluster_first && cluster != cluster_last))
666
132k
    {
667
399k
      for (unsigned int i = start; i < end; i++)
668
266k
  if (cluster != infos[i].cluster)
669
133k
    infos[i].mask |= mask;
670
132k
      return;
671
132k
    }
672
673
    /* Monotone clusters */
674
675
2.34M
    if (cluster == cluster_first)
676
2.10M
    {
677
4.07M
      for (unsigned int i = end; start < i && infos[i - 1].cluster != cluster_first; i--)
678
1.97M
  infos[i - 1].mask |= mask;
679
2.10M
    }
680
242k
    else /* cluster == cluster_last */
681
242k
    {
682
485k
      for (unsigned int i = start; i < end && infos[i].cluster != cluster_last; i++)
683
242k
  infos[i].mask |= mask;
684
242k
    }
685
2.34M
  }
686
  unsigned
687
  _infos_find_min_cluster (const hb_glyph_info_t *infos,
688
         unsigned start, unsigned end,
689
         unsigned cluster = UINT_MAX)
690
2.48M
  {
691
2.48M
    if (unlikely (start == end))
692
6.30k
      return cluster;
693
694
2.47M
    if (cluster_level == HB_BUFFER_CLUSTER_LEVEL_CHARACTERS)
695
132k
    {
696
399k
      for (unsigned int i = start; i < end; i++)
697
266k
  cluster = hb_min (cluster, infos[i].cluster);
698
132k
      return cluster;
699
132k
    }
700
701
2.34M
    return hb_min (cluster, hb_min (infos[start].cluster, infos[end - 1].cluster));
702
2.47M
  }
703
704
  void clear_glyph_flags (hb_mask_t mask = 0)
705
0
  {
706
0
    for (unsigned int i = 0; i < len; i++)
707
0
      info[i].mask = (info[i].mask & ~HB_GLYPH_FLAG_DEFINED) | (mask & HB_GLYPH_FLAG_DEFINED);
708
0
  }
709
};
710
DECLARE_NULL_INSTANCE (hb_buffer_t);
711
712
713
#define foreach_group(buffer, start, end, group_func) \
714
18.3M
  for (unsigned int \
715
18.3M
       _count = buffer->len, \
716
18.3M
       start = 0, end = _count ? buffer->group_end (0, group_func) : 0; \
717
266M
       start < _count; \
718
247M
       start = end, end = buffer->group_end (start, group_func))
719
720
#define foreach_cluster(buffer, start, end) \
721
17.5M
  foreach_group (buffer, start, end, hb_buffer_t::_cluster_group_func)
722
723
724
#define HB_BUFFER_XALLOCATE_VAR(b, func, var) \
725
230M
  b->func (offsetof (hb_glyph_info_t, var) - offsetof(hb_glyph_info_t, var1), \
726
230M
     sizeof (b->info[0].var))
727
71.5M
#define HB_BUFFER_ALLOCATE_VAR(b, var)    HB_BUFFER_XALLOCATE_VAR (b, allocate_var,     var ())
728
0
#define HB_BUFFER_TRY_ALLOCATE_VAR(b, var)  HB_BUFFER_XALLOCATE_VAR (b, try_allocate_var, var ())
729
71.5M
#define HB_BUFFER_DEALLOCATE_VAR(b, var)  HB_BUFFER_XALLOCATE_VAR (b, deallocate_var,   var ())
730
87.8M
#define HB_BUFFER_ASSERT_VAR(b, var)    HB_BUFFER_XALLOCATE_VAR (b, assert_var,       var ())
731
732
733
#endif /* HB_BUFFER_HH */