xref: /aosp_15_r20/external/harfbuzz_ng/src/hb-buffer.hh (revision 2d1272b857b1f7575e6e246373e1cb218663db8a)
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_NON_ASCII			= 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_GLYPH_FLAGS		= 0x00000020u,
54   HB_BUFFER_SCRATCH_FLAG_HAS_BROKEN_SYLLABLE		= 0x00000040u,
55   HB_BUFFER_SCRATCH_FLAG_HAS_VARIATION_SELECTOR_FALLBACK= 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 shaping_failed; /* Shaping failure */
95   bool have_output; /* Whether we have an output buffer going on */
96   bool have_positions; /* Whether we have positions */
97 
98   unsigned int idx; /* Cursor into ->info and ->pos arrays */
99   unsigned int len; /* Length of ->info and ->pos arrays */
100   unsigned int out_len; /* Length of ->out_info array if have_output */
101 
102   unsigned int allocated; /* Length of allocated arrays */
103   hb_glyph_info_t     *info;
104   hb_glyph_info_t     *out_info;
105   hb_glyph_position_t *pos;
106 
107   /* Text before / after the main buffer contents.
108    * Always in Unicode, and ordered outward.
109    * Index 0 is for "pre-context", 1 for "post-context". */
110   static constexpr unsigned CONTEXT_LENGTH = 5u;
111   hb_codepoint_t context[2][CONTEXT_LENGTH];
112   unsigned int context_len[2];
113 
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 
in_errorhb_buffer_t145   HB_NODISCARD bool in_error () const { return !successful; }
146 
allocate_varhb_buffer_t147   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   }
try_allocate_varhb_buffer_t155   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   }
deallocate_varhb_buffer_t165   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   }
assert_varhb_buffer_t173   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   }
deallocate_var_allhb_buffer_t180   void deallocate_var_all ()
181   {
182     allocated_var_bits = 0;
183   }
184 
curhb_buffer_t185   hb_glyph_info_t &cur (unsigned int i = 0) { return info[idx + i]; }
curhb_buffer_t186   hb_glyph_info_t cur (unsigned int i = 0) const { return info[idx + i]; }
187 
cur_poshb_buffer_t188   hb_glyph_position_t &cur_pos (unsigned int i = 0) { return pos[idx + i]; }
cur_poshb_buffer_t189   hb_glyph_position_t cur_pos (unsigned int i = 0) const { return pos[idx + i]; }
190 
prevhb_buffer_t191   hb_glyph_info_t &prev ()      { return out_info[out_len ? out_len - 1 : 0]; }
prevhb_buffer_t192   hb_glyph_info_t prev () const { return out_info[out_len ? out_len - 1 : 0]; }
193 
digesthb_buffer_t194   hb_set_digest_t digest () const
195   {
196     hb_set_digest_t d;
197     d.init ();
198     d.add_array (&info[0].codepoint, len, sizeof (info[0]));
199     return d;
200   }
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 
backtrack_lenhb_buffer_t224   unsigned int backtrack_len () const { return have_output ? out_len : idx; }
lookahead_lenhb_buffer_t225   unsigned int lookahead_len () const { return len - idx; }
next_serialhb_buffer_t226   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 
reverse_rangehb_buffer_t232   void reverse_range (unsigned start, unsigned end)
233   {
234     hb_array_t<hb_glyph_info_t> (info, len).reverse (start, end);
235     if (have_positions)
236       hb_array_t<hb_glyph_position_t> (pos, len).reverse (start, end);
237   }
reversehb_buffer_t238   void reverse () { reverse_range (0, len); }
239 
240   template <typename FuncType>
reverse_groupshb_buffer_t241   void reverse_groups (const FuncType& group,
242 		       bool merge_clusters = false)
243   {
244     if (unlikely (!len))
245       return;
246 
247     unsigned start = 0;
248     unsigned i;
249     for (i = 1; i < len; i++)
250     {
251       if (!group (info[i - 1], info[i]))
252       {
253 	if (merge_clusters)
254 	  this->merge_clusters (start, i);
255 	reverse_range (start, i);
256 	start = i;
257       }
258     }
259     if (merge_clusters)
260       this->merge_clusters (start, i);
261     reverse_range (start, i);
262 
263     reverse ();
264   }
265 
266   template <typename FuncType>
group_endhb_buffer_t267   unsigned group_end (unsigned start, const FuncType& group) const
268   {
269     while (++start < len && group (info[start - 1], info[start]))
270       ;
271 
272     return start;
273   }
274 
_cluster_group_funchb_buffer_t275   static bool _cluster_group_func (const hb_glyph_info_t& a,
276 				   const hb_glyph_info_t& b)
277   { return a.cluster == b.cluster; }
278 
reverse_clustershb_buffer_t279   void reverse_clusters () { reverse_groups (_cluster_group_func); }
280 
281   HB_INTERNAL void guess_segment_properties ();
282 
283   HB_INTERNAL bool sync ();
284   HB_INTERNAL int sync_so_far ();
285   HB_INTERNAL void clear_output ();
286   HB_INTERNAL void clear_positions ();
287 
288   template <typename T>
replace_glyphshb_buffer_t289   HB_NODISCARD bool replace_glyphs (unsigned int num_in,
290 				    unsigned int num_out,
291 				    const T *glyph_data)
292   {
293     if (unlikely (!make_room_for (num_in, num_out))) return false;
294 
295     assert (idx + num_in <= len);
296 
297     merge_clusters (idx, idx + num_in);
298 
299     hb_glyph_info_t &orig_info = idx < len ? cur() : prev();
300 
301     hb_glyph_info_t *pinfo = &out_info[out_len];
302     for (unsigned int i = 0; i < num_out; i++)
303     {
304       *pinfo = orig_info;
305       pinfo->codepoint = glyph_data[i];
306       pinfo++;
307     }
308 
309     idx  += num_in;
310     out_len += num_out;
311     return true;
312   }
313 
replace_glyphhb_buffer_t314   HB_NODISCARD bool replace_glyph (hb_codepoint_t glyph_index)
315   { return replace_glyphs (1, 1, &glyph_index); }
316 
317   /* Makes a copy of the glyph at idx to output and replace glyph_index */
output_glyphhb_buffer_t318   HB_NODISCARD bool output_glyph (hb_codepoint_t glyph_index)
319   { return replace_glyphs (0, 1, &glyph_index); }
320 
output_infohb_buffer_t321   HB_NODISCARD bool output_info (const hb_glyph_info_t &glyph_info)
322   {
323     if (unlikely (!make_room_for (0, 1))) return false;
324 
325     out_info[out_len] = glyph_info;
326 
327     out_len++;
328     return true;
329   }
330   /* Copies glyph at idx to output but doesn't advance idx */
copy_glyphhb_buffer_t331   HB_NODISCARD bool copy_glyph ()
332   {
333     /* Extra copy because cur()'s return can be freed within
334      * output_info() call if buffer reallocates. */
335     return output_info (hb_glyph_info_t (cur()));
336   }
337 
338   /* Copies glyph at idx to output and advance idx.
339    * If there's no output, just advance idx. */
next_glyphhb_buffer_t340   HB_NODISCARD bool next_glyph ()
341   {
342     if (have_output)
343     {
344       if (out_info != info || out_len != idx)
345       {
346 	if (unlikely (!make_room_for (1, 1))) return false;
347 	out_info[out_len] = info[idx];
348       }
349       out_len++;
350     }
351 
352     idx++;
353     return true;
354   }
355   /* Copies n glyphs at idx to output and advance idx.
356    * If there's no output, just advance idx. */
next_glyphshb_buffer_t357   HB_NODISCARD bool next_glyphs (unsigned int n)
358   {
359     if (have_output)
360     {
361       if (out_info != info || out_len != idx)
362       {
363 	if (unlikely (!make_room_for (n, n))) return false;
364 	memmove (out_info + out_len, info + idx, n * sizeof (out_info[0]));
365       }
366       out_len += n;
367     }
368 
369     idx += n;
370     return true;
371   }
372   /* Advance idx without copying to output. */
skip_glyphhb_buffer_t373   void skip_glyph () { idx++; }
reset_maskshb_buffer_t374   void reset_masks (hb_mask_t mask)
375   {
376     for (unsigned int j = 0; j < len; j++)
377       info[j].mask = mask;
378   }
add_maskshb_buffer_t379   void add_masks (hb_mask_t mask)
380   {
381     for (unsigned int j = 0; j < len; j++)
382       info[j].mask |= mask;
383   }
384   HB_INTERNAL void set_masks (hb_mask_t value, hb_mask_t mask,
385 			      unsigned int cluster_start, unsigned int cluster_end);
386 
merge_clustershb_buffer_t387   void merge_clusters (unsigned int start, unsigned int end)
388   {
389     if (end - start < 2)
390       return;
391     merge_clusters_impl (start, end);
392   }
393   HB_INTERNAL void merge_clusters_impl (unsigned int start, unsigned int end);
394   HB_INTERNAL void merge_out_clusters (unsigned int start, unsigned int end);
395   /* Merge clusters for deleting current glyph, and skip it. */
396   HB_INTERNAL void delete_glyph ();
397   HB_INTERNAL void delete_glyphs_inplace (bool (*filter) (const hb_glyph_info_t *info));
398 
399 
400 
401   /* Adds glyph flags in mask to infos with clusters between start and end.
402    * The start index will be from out-buffer if from_out_buffer is true.
403    * If interior is true, then the cluster having the minimum value is skipped. */
_set_glyph_flagshb_buffer_t404   void _set_glyph_flags (hb_mask_t mask,
405 			 unsigned start = 0,
406 			 unsigned end = (unsigned) -1,
407 			 bool interior = false,
408 			 bool from_out_buffer = false)
409   {
410     end = hb_min (end, len);
411 
412     if (interior && !from_out_buffer && end - start < 2)
413       return;
414 
415     scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GLYPH_FLAGS;
416 
417     if (!from_out_buffer || !have_output)
418     {
419       if (!interior)
420       {
421 	for (unsigned i = start; i < end; i++)
422 	  info[i].mask |= mask;
423       }
424       else
425       {
426 	unsigned cluster = _infos_find_min_cluster (info, start, end);
427 	_infos_set_glyph_flags (info, start, end, cluster, mask);
428       }
429     }
430     else
431     {
432       assert (start <= out_len);
433       assert (idx <= end);
434 
435       if (!interior)
436       {
437 	for (unsigned i = start; i < out_len; i++)
438 	  out_info[i].mask |= mask;
439 	for (unsigned i = idx; i < end; i++)
440 	  info[i].mask |= mask;
441       }
442       else
443       {
444 	unsigned cluster = _infos_find_min_cluster (info, idx, end);
445 	cluster = _infos_find_min_cluster (out_info, start, out_len, cluster);
446 
447 	_infos_set_glyph_flags (out_info, start, out_len, cluster, mask);
448 	_infos_set_glyph_flags (info, idx, end, cluster, mask);
449       }
450     }
451   }
452 
unsafe_to_breakhb_buffer_t453   void unsafe_to_break (unsigned int start = 0, unsigned int end = -1)
454   {
455     _set_glyph_flags (HB_GLYPH_FLAG_UNSAFE_TO_BREAK | HB_GLYPH_FLAG_UNSAFE_TO_CONCAT,
456 		      start, end,
457 		      true);
458   }
safe_to_insert_tatweelhb_buffer_t459   void safe_to_insert_tatweel (unsigned int start = 0, unsigned int end = -1)
460   {
461     if ((flags & HB_BUFFER_FLAG_PRODUCE_SAFE_TO_INSERT_TATWEEL) == 0)
462     {
463       unsafe_to_break (start, end);
464       return;
465     }
466     _set_glyph_flags (HB_GLYPH_FLAG_SAFE_TO_INSERT_TATWEEL,
467 		      start, end,
468 		      true);
469   }
470 #ifndef HB_OPTIMIZE_SIZE
471   HB_ALWAYS_INLINE
472 #endif
unsafe_to_concathb_buffer_t473   void unsafe_to_concat (unsigned int start = 0, unsigned int end = -1)
474   {
475     if (likely ((flags & HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT) == 0))
476       return;
477     _set_glyph_flags (HB_GLYPH_FLAG_UNSAFE_TO_CONCAT,
478 		      start, end,
479 		      false);
480   }
unsafe_to_break_from_outbufferhb_buffer_t481   void unsafe_to_break_from_outbuffer (unsigned int start = 0, unsigned int end = -1)
482   {
483     _set_glyph_flags (HB_GLYPH_FLAG_UNSAFE_TO_BREAK | HB_GLYPH_FLAG_UNSAFE_TO_CONCAT,
484 		      start, end,
485 		      true, true);
486   }
487 #ifndef HB_OPTIMIZE_SIZE
488   HB_ALWAYS_INLINE
489 #endif
unsafe_to_concat_from_outbufferhb_buffer_t490   void unsafe_to_concat_from_outbuffer (unsigned int start = 0, unsigned int end = -1)
491   {
492     if (likely ((flags & HB_BUFFER_FLAG_PRODUCE_UNSAFE_TO_CONCAT) == 0))
493       return;
494     _set_glyph_flags (HB_GLYPH_FLAG_UNSAFE_TO_CONCAT,
495 		      start, end,
496 		      false, true);
497   }
498 
499 
500   /* Internal methods */
501   HB_NODISCARD HB_INTERNAL bool move_to (unsigned int i); /* i is output-buffer index. */
502 
503   HB_NODISCARD HB_INTERNAL bool enlarge (unsigned int size);
504 
resizehb_buffer_t505   HB_NODISCARD bool resize (unsigned length)
506   {
507     assert (!have_output);
508     if (unlikely (!ensure (length))) return false;
509     len = length;
510     return true;
511   }
ensurehb_buffer_t512   HB_NODISCARD bool ensure (unsigned int size)
513   { return likely (!size || size < allocated) ? true : enlarge (size); }
514 
ensure_inplacehb_buffer_t515   HB_NODISCARD bool ensure_inplace (unsigned int size)
516   { return likely (!size || size < allocated); }
517 
assert_glyphshb_buffer_t518   void assert_glyphs ()
519   {
520     assert ((content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS) ||
521 	    (!len && (content_type == HB_BUFFER_CONTENT_TYPE_INVALID)));
522   }
assert_unicodehb_buffer_t523   void assert_unicode ()
524   {
525     assert ((content_type == HB_BUFFER_CONTENT_TYPE_UNICODE) ||
526 	    (!len && (content_type == HB_BUFFER_CONTENT_TYPE_INVALID)));
527   }
ensure_glyphshb_buffer_t528   HB_NODISCARD bool ensure_glyphs ()
529   {
530     if (unlikely (content_type != HB_BUFFER_CONTENT_TYPE_GLYPHS))
531     {
532       if (content_type != HB_BUFFER_CONTENT_TYPE_INVALID)
533 	return false;
534       assert (len == 0);
535       content_type = HB_BUFFER_CONTENT_TYPE_GLYPHS;
536     }
537     return true;
538   }
ensure_unicodehb_buffer_t539   HB_NODISCARD bool ensure_unicode ()
540   {
541     if (unlikely (content_type != HB_BUFFER_CONTENT_TYPE_UNICODE))
542     {
543       if (content_type != HB_BUFFER_CONTENT_TYPE_INVALID)
544 	return false;
545       assert (len == 0);
546       content_type = HB_BUFFER_CONTENT_TYPE_UNICODE;
547     }
548     return true;
549   }
550 
551   HB_NODISCARD HB_INTERNAL bool make_room_for (unsigned int num_in, unsigned int num_out);
552   HB_NODISCARD HB_INTERNAL bool shift_forward (unsigned int count);
553 
554   typedef long scratch_buffer_t;
555   HB_INTERNAL scratch_buffer_t *get_scratch_buffer (unsigned int *size);
556 
clear_contexthb_buffer_t557   void clear_context (unsigned int side) { context_len[side] = 0; }
558 
559   HB_INTERNAL void sort (unsigned int start, unsigned int end, int(*compar)(const hb_glyph_info_t *, const hb_glyph_info_t *));
560 
messaginghb_buffer_t561   bool messaging ()
562   {
563 #ifdef HB_NO_BUFFER_MESSAGE
564     return false;
565 #else
566     return unlikely (message_func);
567 #endif
568   }
messagehb_buffer_t569   bool message (hb_font_t *font, const char *fmt, ...) HB_PRINTF_FUNC(3, 4)
570   {
571 #ifdef HB_NO_BUFFER_MESSAGE
572     return true;
573 #else
574     if (likely (!messaging ()))
575       return true;
576 
577     va_list ap;
578     va_start (ap, fmt);
579     bool ret = message_impl (font, fmt, ap);
580     va_end (ap);
581 
582     return ret;
583 #endif
584   }
585   HB_INTERNAL bool message_impl (hb_font_t *font, const char *fmt, va_list ap) HB_PRINTF_FUNC(3, 0);
586 
587   static void
set_clusterhb_buffer_t588   set_cluster (hb_glyph_info_t &inf, unsigned int cluster, unsigned int mask = 0)
589   {
590     if (inf.cluster != cluster)
591       inf.mask = (inf.mask & ~HB_GLYPH_FLAG_DEFINED) | (mask & HB_GLYPH_FLAG_DEFINED);
592     inf.cluster = cluster;
593   }
594   void
_infos_set_glyph_flagshb_buffer_t595   _infos_set_glyph_flags (hb_glyph_info_t *infos,
596 			  unsigned int start, unsigned int end,
597 			  unsigned int cluster,
598 			  hb_mask_t mask)
599   {
600     if (unlikely (start == end))
601       return;
602 
603     unsigned cluster_first = infos[start].cluster;
604     unsigned cluster_last = infos[end - 1].cluster;
605 
606     if (cluster_level == HB_BUFFER_CLUSTER_LEVEL_CHARACTERS ||
607 	(cluster != cluster_first && cluster != cluster_last))
608     {
609       for (unsigned int i = start; i < end; i++)
610 	if (cluster != infos[i].cluster)
611 	{
612 	  scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GLYPH_FLAGS;
613 	  infos[i].mask |= mask;
614 	}
615       return;
616     }
617 
618     /* Monotone clusters */
619 
620     if (cluster == cluster_first)
621     {
622       for (unsigned int i = end; start < i && infos[i - 1].cluster != cluster_first; i--)
623       {
624 	scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GLYPH_FLAGS;
625 	infos[i - 1].mask |= mask;
626       }
627     }
628     else /* cluster == cluster_last */
629     {
630       for (unsigned int i = start; i < end && infos[i].cluster != cluster_last; i++)
631       {
632 	scratch_flags |= HB_BUFFER_SCRATCH_FLAG_HAS_GLYPH_FLAGS;
633 	infos[i].mask |= mask;
634       }
635     }
636   }
637   unsigned
_infos_find_min_clusterhb_buffer_t638   _infos_find_min_cluster (const hb_glyph_info_t *infos,
639 			   unsigned start, unsigned end,
640 			   unsigned cluster = UINT_MAX)
641   {
642     if (unlikely (start == end))
643       return cluster;
644 
645     if (cluster_level == HB_BUFFER_CLUSTER_LEVEL_CHARACTERS)
646     {
647       for (unsigned int i = start; i < end; i++)
648 	cluster = hb_min (cluster, infos[i].cluster);
649       return cluster;
650     }
651 
652     return hb_min (cluster, hb_min (infos[start].cluster, infos[end - 1].cluster));
653   }
654 
clear_glyph_flagshb_buffer_t655   void clear_glyph_flags (hb_mask_t mask = 0)
656   {
657     for (unsigned int i = 0; i < len; i++)
658       info[i].mask = (info[i].mask & ~HB_GLYPH_FLAG_DEFINED) | (mask & HB_GLYPH_FLAG_DEFINED);
659   }
660 };
661 DECLARE_NULL_INSTANCE (hb_buffer_t);
662 
663 
664 #define foreach_group(buffer, start, end, group_func) \
665   for (unsigned int \
666        _count = buffer->len, \
667        start = 0, end = _count ? buffer->group_end (0, group_func) : 0; \
668        start < _count; \
669        start = end, end = buffer->group_end (start, group_func))
670 
671 #define foreach_cluster(buffer, start, end) \
672 	foreach_group (buffer, start, end, hb_buffer_t::_cluster_group_func)
673 
674 
675 #define HB_BUFFER_XALLOCATE_VAR(b, func, var) \
676   b->func (offsetof (hb_glyph_info_t, var) - offsetof(hb_glyph_info_t, var1), \
677 	   sizeof (b->info[0].var))
678 #define HB_BUFFER_ALLOCATE_VAR(b, var)		HB_BUFFER_XALLOCATE_VAR (b, allocate_var,     var ())
679 #define HB_BUFFER_TRY_ALLOCATE_VAR(b, var)	HB_BUFFER_XALLOCATE_VAR (b, try_allocate_var, var ())
680 #define HB_BUFFER_DEALLOCATE_VAR(b, var)	HB_BUFFER_XALLOCATE_VAR (b, deallocate_var,   var ())
681 #define HB_BUFFER_ASSERT_VAR(b, var)		HB_BUFFER_XALLOCATE_VAR (b, assert_var,       var ())
682 
683 
684 #endif /* HB_BUFFER_HH */
685