tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

hb-buffer.hh (20754B)


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