• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "hb-buffer.hh"
31 #include "hb-utf.hh"
32 
33 
34 /**
35  * SECTION: hb-buffer
36  * @title: hb-buffer
37  * @short_description: Input and output buffers
38  * @include: hb.h
39  *
40  * Buffers serve a dual role in HarfBuzz; before shaping, they hold
41  * the input characters that are passed to hb_shape(), and after
42  * shaping they hold the output glyphs.
43  **/
44 
45 
46 /**
47  * hb_segment_properties_equal:
48  * @a: first #hb_segment_properties_t to compare.
49  * @b: second #hb_segment_properties_t to compare.
50  *
51  * Checks the equality of two #hb_segment_properties_t's.
52  *
53  * Return value:
54  * %true if all properties of @a equal those of @b, %false otherwise.
55  *
56  * Since: 0.9.7
57  **/
58 hb_bool_t
hb_segment_properties_equal(const hb_segment_properties_t * a,const hb_segment_properties_t * b)59 hb_segment_properties_equal (const hb_segment_properties_t *a,
60 			     const hb_segment_properties_t *b)
61 {
62   return a->direction == b->direction &&
63 	 a->script    == b->script    &&
64 	 a->language  == b->language  &&
65 	 a->reserved1 == b->reserved1 &&
66 	 a->reserved2 == b->reserved2;
67 
68 }
69 
70 /**
71  * hb_segment_properties_hash:
72  * @p: #hb_segment_properties_t to hash.
73  *
74  * Creates a hash representing @p.
75  *
76  * Return value:
77  * A hash of @p.
78  *
79  * Since: 0.9.7
80  **/
81 unsigned int
hb_segment_properties_hash(const hb_segment_properties_t * p)82 hb_segment_properties_hash (const hb_segment_properties_t *p)
83 {
84   return (unsigned int) p->direction ^
85 	 (unsigned int) p->script ^
86 	 (intptr_t) (p->language);
87 }
88 
89 
90 
91 /* Here is how the buffer works internally:
92  *
93  * There are two info pointers: info and out_info.  They always have
94  * the same allocated size, but different lengths.
95  *
96  * As an optimization, both info and out_info may point to the
97  * same piece of memory, which is owned by info.  This remains the
98  * case as long as out_len doesn't exceed i at any time.
99  * In that case, swap_buffers() is mostly no-op and the glyph operations
100  * operate mostly in-place.
101  *
102  * As soon as out_info gets longer than info, out_info is moved over
103  * to an alternate buffer (which we reuse the pos buffer for), and its
104  * current contents (out_len entries) are copied to the new place.
105  *
106  * This should all remain transparent to the user.  swap_buffers() then
107  * switches info over to out_info and does housekeeping.
108  */
109 
110 
111 
112 /* Internal API */
113 
114 bool
enlarge(unsigned int size)115 hb_buffer_t::enlarge (unsigned int size)
116 {
117   if (unlikely (!successful))
118     return false;
119   if (unlikely (size > max_len))
120   {
121     successful = false;
122     return false;
123   }
124 
125   unsigned int new_allocated = allocated;
126   hb_glyph_position_t *new_pos = nullptr;
127   hb_glyph_info_t *new_info = nullptr;
128   bool separate_out = out_info != info;
129 
130   if (unlikely (hb_unsigned_mul_overflows (size, sizeof (info[0]))))
131     goto done;
132 
133   while (size >= new_allocated)
134     new_allocated += (new_allocated >> 1) + 32;
135 
136   static_assert ((sizeof (info[0]) == sizeof (pos[0])), "");
137   if (unlikely (hb_unsigned_mul_overflows (new_allocated, sizeof (info[0]))))
138     goto done;
139 
140   new_pos = (hb_glyph_position_t *) hb_realloc (pos, new_allocated * sizeof (pos[0]));
141   new_info = (hb_glyph_info_t *) hb_realloc (info, new_allocated * sizeof (info[0]));
142 
143 done:
144   if (unlikely (!new_pos || !new_info))
145     successful = false;
146 
147   if (likely (new_pos))
148     pos = new_pos;
149 
150   if (likely (new_info))
151     info = new_info;
152 
153   out_info = separate_out ? (hb_glyph_info_t *) pos : info;
154   if (likely (successful))
155     allocated = new_allocated;
156 
157   return likely (successful);
158 }
159 
160 bool
make_room_for(unsigned int num_in,unsigned int num_out)161 hb_buffer_t::make_room_for (unsigned int num_in,
162 			    unsigned int num_out)
163 {
164   if (unlikely (!ensure (out_len + num_out))) return false;
165 
166   if (out_info == info &&
167       out_len + num_out > idx + num_in)
168   {
169     assert (have_output);
170 
171     out_info = (hb_glyph_info_t *) pos;
172     memcpy (out_info, info, out_len * sizeof (out_info[0]));
173   }
174 
175   return true;
176 }
177 
178 bool
shift_forward(unsigned int count)179 hb_buffer_t::shift_forward (unsigned int count)
180 {
181   assert (have_output);
182   if (unlikely (!ensure (len + count))) return false;
183 
184   memmove (info + idx + count, info + idx, (len - idx) * sizeof (info[0]));
185   if (idx + count > len)
186   {
187     /* Under memory failure we might expose this area.  At least
188      * clean it up.  Oh well...
189      *
190      * Ideally, we should at least set Default_Ignorable bits on
191      * these, as well as consistent cluster values.  But the former
192      * is layering violation... */
193     memset (info + len, 0, (idx + count - len) * sizeof (info[0]));
194   }
195   len += count;
196   idx += count;
197 
198   return true;
199 }
200 
201 hb_buffer_t::scratch_buffer_t *
get_scratch_buffer(unsigned int * size)202 hb_buffer_t::get_scratch_buffer (unsigned int *size)
203 {
204   have_output = false;
205   have_positions = false;
206 
207   out_len = 0;
208   out_info = info;
209 
210   assert ((uintptr_t) pos % sizeof (scratch_buffer_t) == 0);
211   *size = allocated * sizeof (pos[0]) / sizeof (scratch_buffer_t);
212   return (scratch_buffer_t *) (void *) pos;
213 }
214 
215 
216 
217 /* HarfBuzz-Internal API */
218 
219 void
reset()220 hb_buffer_t::reset ()
221 {
222   hb_unicode_funcs_destroy (unicode);
223   unicode = hb_unicode_funcs_reference (hb_unicode_funcs_get_default ());
224   flags = HB_BUFFER_FLAG_DEFAULT;
225   replacement = HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT;
226   invisible = 0;
227   not_found = 0;
228 
229   clear ();
230 }
231 
232 void
clear()233 hb_buffer_t::clear ()
234 {
235   hb_segment_properties_t default_props = HB_SEGMENT_PROPERTIES_DEFAULT;
236   props = default_props;
237   scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;
238 
239   content_type = HB_BUFFER_CONTENT_TYPE_INVALID;
240   successful = true;
241   have_output = false;
242   have_positions = false;
243 
244   idx = 0;
245   len = 0;
246   out_len = 0;
247   out_info = info;
248 
249   serial = 0;
250 
251   memset (context, 0, sizeof context);
252   memset (context_len, 0, sizeof context_len);
253 
254   deallocate_var_all ();
255 }
256 
257 void
add(hb_codepoint_t codepoint,unsigned int cluster)258 hb_buffer_t::add (hb_codepoint_t  codepoint,
259 		  unsigned int    cluster)
260 {
261   hb_glyph_info_t *glyph;
262 
263   if (unlikely (!ensure (len + 1))) return;
264 
265   glyph = &info[len];
266 
267   memset (glyph, 0, sizeof (*glyph));
268   glyph->codepoint = codepoint;
269   glyph->mask = 0;
270   glyph->cluster = cluster;
271 
272   len++;
273 }
274 
275 void
add_info(const hb_glyph_info_t & glyph_info)276 hb_buffer_t::add_info (const hb_glyph_info_t &glyph_info)
277 {
278   if (unlikely (!ensure (len + 1))) return;
279 
280   info[len] = glyph_info;
281 
282   len++;
283 }
284 
285 
286 void
clear_output()287 hb_buffer_t::clear_output ()
288 {
289   have_output = true;
290   have_positions = false;
291 
292   idx = 0;
293   out_len = 0;
294   out_info = info;
295 }
296 
297 void
clear_positions()298 hb_buffer_t::clear_positions ()
299 {
300   have_output = false;
301   have_positions = true;
302 
303   out_len = 0;
304   out_info = info;
305 
306   hb_memset (pos, 0, sizeof (pos[0]) * len);
307 }
308 
309 void
swap_buffers()310 hb_buffer_t::swap_buffers ()
311 {
312   assert (have_output);
313 
314   assert (idx <= len);
315 
316   if (unlikely (!successful || !next_glyphs (len - idx)))
317     goto reset;
318 
319   if (out_info != info)
320   {
321     pos = (hb_glyph_position_t *) info;
322     info = out_info;
323   }
324   len = out_len;
325 
326 reset:
327   have_output = false;
328   out_len = 0;
329   idx = 0;
330 }
331 
332 bool
move_to(unsigned int i)333 hb_buffer_t::move_to (unsigned int i)
334 {
335   if (!have_output)
336   {
337     assert (i <= len);
338     idx = i;
339     return true;
340   }
341   if (unlikely (!successful))
342     return false;
343 
344   assert (i <= out_len + (len - idx));
345 
346   if (out_len < i)
347   {
348     unsigned int count = i - out_len;
349     if (unlikely (!make_room_for (count, count))) return false;
350 
351     memmove (out_info + out_len, info + idx, count * sizeof (out_info[0]));
352     idx += count;
353     out_len += count;
354   }
355   else if (out_len > i)
356   {
357     /* Tricky part: rewinding... */
358     unsigned int count = out_len - i;
359 
360     /* This will blow in our face if memory allocation fails later
361      * in this same lookup...
362      *
363      * We used to shift with extra 32 items.
364      * But that would leave empty slots in the buffer in case of allocation
365      * failures.  See comments in shift_forward().  This can cause O(N^2)
366      * behavior more severely than adding 32 empty slots can... */
367     if (unlikely (idx < count && !shift_forward (count - idx))) return false;
368 
369     assert (idx >= count);
370 
371     idx -= count;
372     out_len -= count;
373     memmove (info + idx, out_info + out_len, count * sizeof (out_info[0]));
374   }
375 
376   return true;
377 }
378 
379 
380 void
set_masks(hb_mask_t value,hb_mask_t mask,unsigned int cluster_start,unsigned int cluster_end)381 hb_buffer_t::set_masks (hb_mask_t    value,
382 			hb_mask_t    mask,
383 			unsigned int cluster_start,
384 			unsigned int cluster_end)
385 {
386   hb_mask_t not_mask = ~mask;
387   value &= mask;
388 
389   if (!mask)
390     return;
391 
392   unsigned int count = len;
393   for (unsigned int i = 0; i < count; i++)
394     if (cluster_start <= info[i].cluster && info[i].cluster < cluster_end)
395       info[i].mask = (info[i].mask & not_mask) | value;
396 }
397 
398 void
reverse_range(unsigned int start,unsigned int end)399 hb_buffer_t::reverse_range (unsigned int start,
400 			    unsigned int end)
401 {
402   if (end - start < 2)
403     return;
404 
405   hb_array_t<hb_glyph_info_t> (info, len).reverse (start, end);
406 
407   if (have_positions) {
408     hb_array_t<hb_glyph_position_t> (pos, len).reverse (start, end);
409   }
410 }
411 
412 void
reverse()413 hb_buffer_t::reverse ()
414 {
415   if (unlikely (!len))
416     return;
417 
418   reverse_range (0, len);
419 }
420 
421 void
reverse_clusters()422 hb_buffer_t::reverse_clusters ()
423 {
424   unsigned int i, start, count, last_cluster;
425 
426   if (unlikely (!len))
427     return;
428 
429   reverse ();
430 
431   count = len;
432   start = 0;
433   last_cluster = info[0].cluster;
434   for (i = 1; i < count; i++) {
435     if (last_cluster != info[i].cluster) {
436       reverse_range (start, i);
437       start = i;
438       last_cluster = info[i].cluster;
439     }
440   }
441   reverse_range (start, i);
442 }
443 
444 void
merge_clusters_impl(unsigned int start,unsigned int end)445 hb_buffer_t::merge_clusters_impl (unsigned int start,
446 				  unsigned int end)
447 {
448   if (cluster_level == HB_BUFFER_CLUSTER_LEVEL_CHARACTERS)
449   {
450     unsafe_to_break (start, end);
451     return;
452   }
453 
454   unsigned int cluster = info[start].cluster;
455 
456   for (unsigned int i = start + 1; i < end; i++)
457     cluster = hb_min (cluster, info[i].cluster);
458 
459   /* Extend end */
460   while (end < len && info[end - 1].cluster == info[end].cluster)
461     end++;
462 
463   /* Extend start */
464   while (idx < start && info[start - 1].cluster == info[start].cluster)
465     start--;
466 
467   /* If we hit the start of buffer, continue in out-buffer. */
468   if (idx == start)
469     for (unsigned int i = out_len; i && out_info[i - 1].cluster == info[start].cluster; i--)
470       set_cluster (out_info[i - 1], cluster);
471 
472   for (unsigned int i = start; i < end; i++)
473     set_cluster (info[i], cluster);
474 }
475 void
merge_out_clusters(unsigned int start,unsigned int end)476 hb_buffer_t::merge_out_clusters (unsigned int start,
477 				 unsigned int end)
478 {
479   if (cluster_level == HB_BUFFER_CLUSTER_LEVEL_CHARACTERS)
480     return;
481 
482   if (unlikely (end - start < 2))
483     return;
484 
485   unsigned int cluster = out_info[start].cluster;
486 
487   for (unsigned int i = start + 1; i < end; i++)
488     cluster = hb_min (cluster, out_info[i].cluster);
489 
490   /* Extend start */
491   while (start && out_info[start - 1].cluster == out_info[start].cluster)
492     start--;
493 
494   /* Extend end */
495   while (end < out_len && out_info[end - 1].cluster == out_info[end].cluster)
496     end++;
497 
498   /* If we hit the end of out-buffer, continue in buffer. */
499   if (end == out_len)
500     for (unsigned int i = idx; i < len && info[i].cluster == out_info[end - 1].cluster; i++)
501       set_cluster (info[i], cluster);
502 
503   for (unsigned int i = start; i < end; i++)
504     set_cluster (out_info[i], cluster);
505 }
506 void
delete_glyph()507 hb_buffer_t::delete_glyph ()
508 {
509   /* The logic here is duplicated in hb_ot_hide_default_ignorables(). */
510 
511   unsigned int cluster = info[idx].cluster;
512   if (idx + 1 < len && cluster == info[idx + 1].cluster)
513   {
514     /* Cluster survives; do nothing. */
515     goto done;
516   }
517 
518   if (out_len)
519   {
520     /* Merge cluster backward. */
521     if (cluster < out_info[out_len - 1].cluster)
522     {
523       unsigned int mask = info[idx].mask;
524       unsigned int old_cluster = out_info[out_len - 1].cluster;
525       for (unsigned i = out_len; i && out_info[i - 1].cluster == old_cluster; i--)
526 	set_cluster (out_info[i - 1], cluster, mask);
527     }
528     goto done;
529   }
530 
531   if (idx + 1 < len)
532   {
533     /* Merge cluster forward. */
534     merge_clusters (idx, idx + 2);
535     goto done;
536   }
537 
538 done:
539   skip_glyph ();
540 }
541 
542 void
unsafe_to_break_impl(unsigned int start,unsigned int end)543 hb_buffer_t::unsafe_to_break_impl (unsigned int start, unsigned int end)
544 {
545   unsigned int cluster = UINT_MAX;
546   cluster = _unsafe_to_break_find_min_cluster (info, start, end, cluster);
547   _unsafe_to_break_set_mask (info, start, end, cluster);
548 }
549 void
unsafe_to_break_from_outbuffer(unsigned int start,unsigned int end)550 hb_buffer_t::unsafe_to_break_from_outbuffer (unsigned int start, unsigned int end)
551 {
552   if (!have_output)
553   {
554     unsafe_to_break_impl (start, end);
555     return;
556   }
557 
558   assert (start <= out_len);
559   assert (idx <= end);
560 
561   unsigned int cluster = UINT_MAX;
562   cluster = _unsafe_to_break_find_min_cluster (out_info, start, out_len, cluster);
563   cluster = _unsafe_to_break_find_min_cluster (info, idx, end, cluster);
564   _unsafe_to_break_set_mask (out_info, start, out_len, cluster);
565   _unsafe_to_break_set_mask (info, idx, end, cluster);
566 }
567 
568 void
guess_segment_properties()569 hb_buffer_t::guess_segment_properties ()
570 {
571   assert_unicode ();
572 
573   /* If script is set to INVALID, guess from buffer contents */
574   if (props.script == HB_SCRIPT_INVALID) {
575     for (unsigned int i = 0; i < len; i++) {
576       hb_script_t script = unicode->script (info[i].codepoint);
577       if (likely (script != HB_SCRIPT_COMMON &&
578 		  script != HB_SCRIPT_INHERITED &&
579 		  script != HB_SCRIPT_UNKNOWN)) {
580 	props.script = script;
581 	break;
582       }
583     }
584   }
585 
586   /* If direction is set to INVALID, guess from script */
587   if (props.direction == HB_DIRECTION_INVALID) {
588     props.direction = hb_script_get_horizontal_direction (props.script);
589     if (props.direction == HB_DIRECTION_INVALID)
590       props.direction = HB_DIRECTION_LTR;
591   }
592 
593   /* If language is not set, use default language from locale */
594   if (props.language == HB_LANGUAGE_INVALID) {
595     /* TODO get_default_for_script? using $LANGUAGE */
596     props.language = hb_language_get_default ();
597   }
598 }
599 
600 
601 /* Public API */
602 
603 DEFINE_NULL_INSTANCE (hb_buffer_t) =
604 {
605   HB_OBJECT_HEADER_STATIC,
606 
607   const_cast<hb_unicode_funcs_t *> (&_hb_Null_hb_unicode_funcs_t),
608   HB_BUFFER_FLAG_DEFAULT,
609   HB_BUFFER_CLUSTER_LEVEL_DEFAULT,
610   HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT,
611   0, /* invisible */
612   0, /* not_found */
613   HB_BUFFER_SCRATCH_FLAG_DEFAULT,
614   HB_BUFFER_MAX_LEN_DEFAULT,
615   HB_BUFFER_MAX_OPS_DEFAULT,
616 
617   HB_BUFFER_CONTENT_TYPE_INVALID,
618   HB_SEGMENT_PROPERTIES_DEFAULT,
619   false, /* successful */
620   false, /* have_output */
621   true  /* have_positions */
622 
623   /* Zero is good enough for everything else. */
624 };
625 
626 
627 /**
628  * hb_buffer_create: (Xconstructor)
629  *
630  * Creates a new #hb_buffer_t with all properties to defaults.
631  *
632  * Return value: (transfer full):
633  * A newly allocated #hb_buffer_t with a reference count of 1. The initial
634  * reference count should be released with hb_buffer_destroy() when you are done
635  * using the #hb_buffer_t. This function never returns %NULL. If memory cannot
636  * be allocated, a special #hb_buffer_t object will be returned on which
637  * hb_buffer_allocation_successful() returns %false.
638  *
639  * Since: 0.9.2
640  **/
641 hb_buffer_t *
hb_buffer_create()642 hb_buffer_create ()
643 {
644   hb_buffer_t *buffer;
645 
646   if (!(buffer = hb_object_create<hb_buffer_t> ()))
647     return hb_buffer_get_empty ();
648 
649   buffer->max_len = HB_BUFFER_MAX_LEN_DEFAULT;
650   buffer->max_ops = HB_BUFFER_MAX_OPS_DEFAULT;
651 
652   buffer->reset ();
653 
654   return buffer;
655 }
656 
657 /**
658  * hb_buffer_get_empty:
659  *
660  * Fetches an empty #hb_buffer_t.
661  *
662  * Return value: (transfer full): The empty buffer
663  *
664  * Since: 0.9.2
665  **/
666 hb_buffer_t *
hb_buffer_get_empty()667 hb_buffer_get_empty ()
668 {
669   return const_cast<hb_buffer_t *> (&Null (hb_buffer_t));
670 }
671 
672 /**
673  * hb_buffer_reference: (skip)
674  * @buffer: An #hb_buffer_t
675  *
676  * Increases the reference count on @buffer by one. This prevents @buffer from
677  * being destroyed until a matching call to hb_buffer_destroy() is made.
678  *
679  * Return value: (transfer full):
680  * The referenced #hb_buffer_t.
681  *
682  * Since: 0.9.2
683  **/
684 hb_buffer_t *
hb_buffer_reference(hb_buffer_t * buffer)685 hb_buffer_reference (hb_buffer_t *buffer)
686 {
687   return hb_object_reference (buffer);
688 }
689 
690 /**
691  * hb_buffer_destroy: (skip)
692  * @buffer: An #hb_buffer_t
693  *
694  * Deallocate the @buffer.
695  * Decreases the reference count on @buffer by one. If the result is zero, then
696  * @buffer and all associated resources are freed. See hb_buffer_reference().
697  *
698  * Since: 0.9.2
699  **/
700 void
hb_buffer_destroy(hb_buffer_t * buffer)701 hb_buffer_destroy (hb_buffer_t *buffer)
702 {
703   if (!hb_object_destroy (buffer)) return;
704 
705   hb_unicode_funcs_destroy (buffer->unicode);
706 
707   hb_free (buffer->info);
708   hb_free (buffer->pos);
709 #ifndef HB_NO_BUFFER_MESSAGE
710   if (buffer->message_destroy)
711     buffer->message_destroy (buffer->message_data);
712 #endif
713 
714   hb_free (buffer);
715 }
716 
717 /**
718  * hb_buffer_set_user_data: (skip)
719  * @buffer: An #hb_buffer_t
720  * @key: The user-data key
721  * @data: A pointer to the user data
722  * @destroy: (nullable): A callback to call when @data is not needed anymore
723  * @replace: Whether to replace an existing data with the same key
724  *
725  * Attaches a user-data key/data pair to the specified buffer.
726  *
727  * Return value: %true if success, %false otherwise
728  *
729  * Since: 0.9.2
730  **/
731 hb_bool_t
hb_buffer_set_user_data(hb_buffer_t * buffer,hb_user_data_key_t * key,void * data,hb_destroy_func_t destroy,hb_bool_t replace)732 hb_buffer_set_user_data (hb_buffer_t        *buffer,
733 			 hb_user_data_key_t *key,
734 			 void *              data,
735 			 hb_destroy_func_t   destroy,
736 			 hb_bool_t           replace)
737 {
738   return hb_object_set_user_data (buffer, key, data, destroy, replace);
739 }
740 
741 /**
742  * hb_buffer_get_user_data: (skip)
743  * @buffer: An #hb_buffer_t
744  * @key: The user-data key to query
745  *
746  * Fetches the user data associated with the specified key,
747  * attached to the specified buffer.
748  *
749  * Return value: (transfer none): A pointer to the user data
750  *
751  * Since: 0.9.2
752  **/
753 void *
hb_buffer_get_user_data(hb_buffer_t * buffer,hb_user_data_key_t * key)754 hb_buffer_get_user_data (hb_buffer_t        *buffer,
755 			 hb_user_data_key_t *key)
756 {
757   return hb_object_get_user_data (buffer, key);
758 }
759 
760 
761 /**
762  * hb_buffer_set_content_type:
763  * @buffer: An #hb_buffer_t
764  * @content_type: The type of buffer contents to set
765  *
766  * Sets the type of @buffer contents. Buffers are either empty, contain
767  * characters (before shaping), or contain glyphs (the result of shaping).
768  *
769  * Since: 0.9.5
770  **/
771 void
hb_buffer_set_content_type(hb_buffer_t * buffer,hb_buffer_content_type_t content_type)772 hb_buffer_set_content_type (hb_buffer_t              *buffer,
773 			    hb_buffer_content_type_t  content_type)
774 {
775   buffer->content_type = content_type;
776 }
777 
778 /**
779  * hb_buffer_get_content_type:
780  * @buffer: An #hb_buffer_t
781  *
782  * Fetches the type of @buffer contents. Buffers are either empty, contain
783  * characters (before shaping), or contain glyphs (the result of shaping).
784  *
785  * Return value:
786  * The type of @buffer contents
787  *
788  * Since: 0.9.5
789  **/
790 hb_buffer_content_type_t
hb_buffer_get_content_type(hb_buffer_t * buffer)791 hb_buffer_get_content_type (hb_buffer_t *buffer)
792 {
793   return buffer->content_type;
794 }
795 
796 
797 /**
798  * hb_buffer_set_unicode_funcs:
799  * @buffer: An #hb_buffer_t
800  * @unicode_funcs: The Unicode-functions structure
801  *
802  * Sets the Unicode-functions structure of a buffer to
803  * @unicode_funcs.
804  *
805  * Since: 0.9.2
806  **/
807 void
hb_buffer_set_unicode_funcs(hb_buffer_t * buffer,hb_unicode_funcs_t * unicode_funcs)808 hb_buffer_set_unicode_funcs (hb_buffer_t        *buffer,
809 			     hb_unicode_funcs_t *unicode_funcs)
810 {
811   if (unlikely (hb_object_is_immutable (buffer)))
812     return;
813 
814   if (!unicode_funcs)
815     unicode_funcs = hb_unicode_funcs_get_default ();
816 
817   hb_unicode_funcs_reference (unicode_funcs);
818   hb_unicode_funcs_destroy (buffer->unicode);
819   buffer->unicode = unicode_funcs;
820 }
821 
822 /**
823  * hb_buffer_get_unicode_funcs:
824  * @buffer: An #hb_buffer_t
825  *
826  * Fetches the Unicode-functions structure of a buffer.
827  *
828  * Return value: The Unicode-functions structure
829  *
830  * Since: 0.9.2
831  **/
832 hb_unicode_funcs_t *
hb_buffer_get_unicode_funcs(hb_buffer_t * buffer)833 hb_buffer_get_unicode_funcs (hb_buffer_t        *buffer)
834 {
835   return buffer->unicode;
836 }
837 
838 /**
839  * hb_buffer_set_direction:
840  * @buffer: An #hb_buffer_t
841  * @direction: the #hb_direction_t of the @buffer
842  *
843  * Set the text flow direction of the buffer. No shaping can happen without
844  * setting @buffer direction, and it controls the visual direction for the
845  * output glyphs; for RTL direction the glyphs will be reversed. Many layout
846  * features depend on the proper setting of the direction, for example,
847  * reversing RTL text before shaping, then shaping with LTR direction is not
848  * the same as keeping the text in logical order and shaping with RTL
849  * direction.
850  *
851  * Since: 0.9.2
852  **/
853 void
hb_buffer_set_direction(hb_buffer_t * buffer,hb_direction_t direction)854 hb_buffer_set_direction (hb_buffer_t    *buffer,
855 			 hb_direction_t  direction)
856 
857 {
858   if (unlikely (hb_object_is_immutable (buffer)))
859     return;
860 
861   buffer->props.direction = direction;
862 }
863 
864 /**
865  * hb_buffer_get_direction:
866  * @buffer: An #hb_buffer_t
867  *
868  * See hb_buffer_set_direction()
869  *
870  * Return value:
871  * The direction of the @buffer.
872  *
873  * Since: 0.9.2
874  **/
875 hb_direction_t
hb_buffer_get_direction(hb_buffer_t * buffer)876 hb_buffer_get_direction (hb_buffer_t    *buffer)
877 {
878   return buffer->props.direction;
879 }
880 
881 /**
882  * hb_buffer_set_script:
883  * @buffer: An #hb_buffer_t
884  * @script: An #hb_script_t to set.
885  *
886  * Sets the script of @buffer to @script.
887  *
888  * Script is crucial for choosing the proper shaping behaviour for scripts that
889  * require it (e.g. Arabic) and the which OpenType features defined in the font
890  * to be applied.
891  *
892  * You can pass one of the predefined #hb_script_t values, or use
893  * hb_script_from_string() or hb_script_from_iso15924_tag() to get the
894  * corresponding script from an ISO 15924 script tag.
895  *
896  * Since: 0.9.2
897  **/
898 void
hb_buffer_set_script(hb_buffer_t * buffer,hb_script_t script)899 hb_buffer_set_script (hb_buffer_t *buffer,
900 		      hb_script_t  script)
901 {
902   if (unlikely (hb_object_is_immutable (buffer)))
903     return;
904 
905   buffer->props.script = script;
906 }
907 
908 /**
909  * hb_buffer_get_script:
910  * @buffer: An #hb_buffer_t
911  *
912  * Fetches the script of @buffer.
913  *
914  * Return value:
915  * The #hb_script_t of the @buffer
916  *
917  * Since: 0.9.2
918  **/
919 hb_script_t
hb_buffer_get_script(hb_buffer_t * buffer)920 hb_buffer_get_script (hb_buffer_t *buffer)
921 {
922   return buffer->props.script;
923 }
924 
925 /**
926  * hb_buffer_set_language:
927  * @buffer: An #hb_buffer_t
928  * @language: An hb_language_t to set
929  *
930  * Sets the language of @buffer to @language.
931  *
932  * Languages are crucial for selecting which OpenType feature to apply to the
933  * buffer which can result in applying language-specific behaviour. Languages
934  * are orthogonal to the scripts, and though they are related, they are
935  * different concepts and should not be confused with each other.
936  *
937  * Use hb_language_from_string() to convert from BCP 47 language tags to
938  * #hb_language_t.
939  *
940  * Since: 0.9.2
941  **/
942 void
hb_buffer_set_language(hb_buffer_t * buffer,hb_language_t language)943 hb_buffer_set_language (hb_buffer_t   *buffer,
944 			hb_language_t  language)
945 {
946   if (unlikely (hb_object_is_immutable (buffer)))
947     return;
948 
949   buffer->props.language = language;
950 }
951 
952 /**
953  * hb_buffer_get_language:
954  * @buffer: An #hb_buffer_t
955  *
956  * See hb_buffer_set_language().
957  *
958  * Return value: (transfer none):
959  * The #hb_language_t of the buffer. Must not be freed by the caller.
960  *
961  * Since: 0.9.2
962  **/
963 hb_language_t
hb_buffer_get_language(hb_buffer_t * buffer)964 hb_buffer_get_language (hb_buffer_t *buffer)
965 {
966   return buffer->props.language;
967 }
968 
969 /**
970  * hb_buffer_set_segment_properties:
971  * @buffer: An #hb_buffer_t
972  * @props: An #hb_segment_properties_t to use
973  *
974  * Sets the segment properties of the buffer, a shortcut for calling
975  * hb_buffer_set_direction(), hb_buffer_set_script() and
976  * hb_buffer_set_language() individually.
977  *
978  * Since: 0.9.7
979  **/
980 void
hb_buffer_set_segment_properties(hb_buffer_t * buffer,const hb_segment_properties_t * props)981 hb_buffer_set_segment_properties (hb_buffer_t *buffer,
982 				  const hb_segment_properties_t *props)
983 {
984   if (unlikely (hb_object_is_immutable (buffer)))
985     return;
986 
987   buffer->props = *props;
988 }
989 
990 /**
991  * hb_buffer_get_segment_properties:
992  * @buffer: An #hb_buffer_t
993  * @props: (out): The output #hb_segment_properties_t
994  *
995  * Sets @props to the #hb_segment_properties_t of @buffer.
996  *
997  * Since: 0.9.7
998  **/
999 void
hb_buffer_get_segment_properties(hb_buffer_t * buffer,hb_segment_properties_t * props)1000 hb_buffer_get_segment_properties (hb_buffer_t *buffer,
1001 				  hb_segment_properties_t *props)
1002 {
1003   *props = buffer->props;
1004 }
1005 
1006 
1007 /**
1008  * hb_buffer_set_flags:
1009  * @buffer: An #hb_buffer_t
1010  * @flags: The buffer flags to set
1011  *
1012  * Sets @buffer flags to @flags. See #hb_buffer_flags_t.
1013  *
1014  * Since: 0.9.7
1015  **/
1016 void
hb_buffer_set_flags(hb_buffer_t * buffer,hb_buffer_flags_t flags)1017 hb_buffer_set_flags (hb_buffer_t       *buffer,
1018 		     hb_buffer_flags_t  flags)
1019 {
1020   if (unlikely (hb_object_is_immutable (buffer)))
1021     return;
1022 
1023   buffer->flags = flags;
1024 }
1025 
1026 /**
1027  * hb_buffer_get_flags:
1028  * @buffer: An #hb_buffer_t
1029  *
1030  * Fetches the #hb_buffer_flags_t of @buffer.
1031  *
1032  * Return value:
1033  * The @buffer flags
1034  *
1035  * Since: 0.9.7
1036  **/
1037 hb_buffer_flags_t
hb_buffer_get_flags(hb_buffer_t * buffer)1038 hb_buffer_get_flags (hb_buffer_t *buffer)
1039 {
1040   return buffer->flags;
1041 }
1042 
1043 /**
1044  * hb_buffer_set_cluster_level:
1045  * @buffer: An #hb_buffer_t
1046  * @cluster_level: The cluster level to set on the buffer
1047  *
1048  * Sets the cluster level of a buffer. The #hb_buffer_cluster_level_t
1049  * dictates one aspect of how HarfBuzz will treat non-base characters
1050  * during shaping.
1051  *
1052  * Since: 0.9.42
1053  **/
1054 void
hb_buffer_set_cluster_level(hb_buffer_t * buffer,hb_buffer_cluster_level_t cluster_level)1055 hb_buffer_set_cluster_level (hb_buffer_t               *buffer,
1056 			     hb_buffer_cluster_level_t  cluster_level)
1057 {
1058   if (unlikely (hb_object_is_immutable (buffer)))
1059     return;
1060 
1061   buffer->cluster_level = cluster_level;
1062 }
1063 
1064 /**
1065  * hb_buffer_get_cluster_level:
1066  * @buffer: An #hb_buffer_t
1067  *
1068  * Fetches the cluster level of a buffer. The #hb_buffer_cluster_level_t
1069  * dictates one aspect of how HarfBuzz will treat non-base characters
1070  * during shaping.
1071  *
1072  * Return value: The cluster level of @buffer
1073  *
1074  * Since: 0.9.42
1075  **/
1076 hb_buffer_cluster_level_t
hb_buffer_get_cluster_level(hb_buffer_t * buffer)1077 hb_buffer_get_cluster_level (hb_buffer_t *buffer)
1078 {
1079   return buffer->cluster_level;
1080 }
1081 
1082 
1083 /**
1084  * hb_buffer_set_replacement_codepoint:
1085  * @buffer: An #hb_buffer_t
1086  * @replacement: the replacement #hb_codepoint_t
1087  *
1088  * Sets the #hb_codepoint_t that replaces invalid entries for a given encoding
1089  * when adding text to @buffer.
1090  *
1091  * Default is #HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT.
1092  *
1093  * Since: 0.9.31
1094  **/
1095 void
hb_buffer_set_replacement_codepoint(hb_buffer_t * buffer,hb_codepoint_t replacement)1096 hb_buffer_set_replacement_codepoint (hb_buffer_t    *buffer,
1097 				     hb_codepoint_t  replacement)
1098 {
1099   if (unlikely (hb_object_is_immutable (buffer)))
1100     return;
1101 
1102   buffer->replacement = replacement;
1103 }
1104 
1105 /**
1106  * hb_buffer_get_replacement_codepoint:
1107  * @buffer: An #hb_buffer_t
1108  *
1109  * Fetches the #hb_codepoint_t that replaces invalid entries for a given encoding
1110  * when adding text to @buffer.
1111  *
1112  * Return value:
1113  * The @buffer replacement #hb_codepoint_t
1114  *
1115  * Since: 0.9.31
1116  **/
1117 hb_codepoint_t
hb_buffer_get_replacement_codepoint(hb_buffer_t * buffer)1118 hb_buffer_get_replacement_codepoint (hb_buffer_t    *buffer)
1119 {
1120   return buffer->replacement;
1121 }
1122 
1123 
1124 /**
1125  * hb_buffer_set_invisible_glyph:
1126  * @buffer: An #hb_buffer_t
1127  * @invisible: the invisible #hb_codepoint_t
1128  *
1129  * Sets the #hb_codepoint_t that replaces invisible characters in
1130  * the shaping result.  If set to zero (default), the glyph for the
1131  * U+0020 SPACE character is used.  Otherwise, this value is used
1132  * verbatim.
1133  *
1134  * Since: 2.0.0
1135  **/
1136 void
hb_buffer_set_invisible_glyph(hb_buffer_t * buffer,hb_codepoint_t invisible)1137 hb_buffer_set_invisible_glyph (hb_buffer_t    *buffer,
1138 			       hb_codepoint_t  invisible)
1139 {
1140   if (unlikely (hb_object_is_immutable (buffer)))
1141     return;
1142 
1143   buffer->invisible = invisible;
1144 }
1145 
1146 /**
1147  * hb_buffer_get_invisible_glyph:
1148  * @buffer: An #hb_buffer_t
1149  *
1150  * See hb_buffer_set_invisible_glyph().
1151  *
1152  * Return value:
1153  * The @buffer invisible #hb_codepoint_t
1154  *
1155  * Since: 2.0.0
1156  **/
1157 hb_codepoint_t
hb_buffer_get_invisible_glyph(hb_buffer_t * buffer)1158 hb_buffer_get_invisible_glyph (hb_buffer_t    *buffer)
1159 {
1160   return buffer->invisible;
1161 }
1162 
1163 /**
1164  * hb_buffer_set_not_found_glyph:
1165  * @buffer: An #hb_buffer_t
1166  * @not_found: the not-found #hb_codepoint_t
1167  *
1168  * Sets the #hb_codepoint_t that replaces characters not found in
1169  * the font during shaping.
1170  *
1171  * The not-found glyph defaults to zero, sometimes knows as the
1172  * ".notdef" glyph.  This API allows for differentiating the two.
1173  *
1174  * Since: 3.1.0
1175  **/
1176 void
hb_buffer_set_not_found_glyph(hb_buffer_t * buffer,hb_codepoint_t not_found)1177 hb_buffer_set_not_found_glyph (hb_buffer_t    *buffer,
1178 			       hb_codepoint_t  not_found)
1179 {
1180   if (unlikely (hb_object_is_immutable (buffer)))
1181     return;
1182 
1183   buffer->not_found = not_found;
1184 }
1185 
1186 /**
1187  * hb_buffer_get_not_found_glyph:
1188  * @buffer: An #hb_buffer_t
1189  *
1190  * See hb_buffer_set_not_found_glyph().
1191  *
1192  * Return value:
1193  * The @buffer not-found #hb_codepoint_t
1194  *
1195  * Since: 3.1.0
1196  **/
1197 hb_codepoint_t
hb_buffer_get_not_found_glyph(hb_buffer_t * buffer)1198 hb_buffer_get_not_found_glyph (hb_buffer_t    *buffer)
1199 {
1200   return buffer->not_found;
1201 }
1202 
1203 
1204 /**
1205  * hb_buffer_reset:
1206  * @buffer: An #hb_buffer_t
1207  *
1208  * Resets the buffer to its initial status, as if it was just newly created
1209  * with hb_buffer_create().
1210  *
1211  * Since: 0.9.2
1212  **/
1213 void
hb_buffer_reset(hb_buffer_t * buffer)1214 hb_buffer_reset (hb_buffer_t *buffer)
1215 {
1216   if (unlikely (hb_object_is_immutable (buffer)))
1217     return;
1218 
1219   buffer->reset ();
1220 }
1221 
1222 /**
1223  * hb_buffer_clear_contents:
1224  * @buffer: An #hb_buffer_t
1225  *
1226  * Similar to hb_buffer_reset(), but does not clear the Unicode functions and
1227  * the replacement code point.
1228  *
1229  * Since: 0.9.11
1230  **/
1231 void
hb_buffer_clear_contents(hb_buffer_t * buffer)1232 hb_buffer_clear_contents (hb_buffer_t *buffer)
1233 {
1234   if (unlikely (hb_object_is_immutable (buffer)))
1235     return;
1236 
1237   buffer->clear ();
1238 }
1239 
1240 /**
1241  * hb_buffer_pre_allocate:
1242  * @buffer: An #hb_buffer_t
1243  * @size: Number of items to pre allocate.
1244  *
1245  * Pre allocates memory for @buffer to fit at least @size number of items.
1246  *
1247  * Return value:
1248  * %true if @buffer memory allocation succeeded, %false otherwise
1249  *
1250  * Since: 0.9.2
1251  **/
1252 hb_bool_t
hb_buffer_pre_allocate(hb_buffer_t * buffer,unsigned int size)1253 hb_buffer_pre_allocate (hb_buffer_t *buffer, unsigned int size)
1254 {
1255   return buffer->ensure (size);
1256 }
1257 
1258 /**
1259  * hb_buffer_allocation_successful:
1260  * @buffer: An #hb_buffer_t
1261  *
1262  * Check if allocating memory for the buffer succeeded.
1263  *
1264  * Return value:
1265  * %true if @buffer memory allocation succeeded, %false otherwise.
1266  *
1267  * Since: 0.9.2
1268  **/
1269 hb_bool_t
hb_buffer_allocation_successful(hb_buffer_t * buffer)1270 hb_buffer_allocation_successful (hb_buffer_t  *buffer)
1271 {
1272   return buffer->successful;
1273 }
1274 
1275 /**
1276  * hb_buffer_add:
1277  * @buffer: An #hb_buffer_t
1278  * @codepoint: A Unicode code point.
1279  * @cluster: The cluster value of @codepoint.
1280  *
1281  * Appends a character with the Unicode value of @codepoint to @buffer, and
1282  * gives it the initial cluster value of @cluster. Clusters can be any thing
1283  * the client wants, they are usually used to refer to the index of the
1284  * character in the input text stream and are output in
1285  * #hb_glyph_info_t.cluster field.
1286  *
1287  * This function does not check the validity of @codepoint, it is up to the
1288  * caller to ensure it is a valid Unicode code point.
1289  *
1290  * Since: 0.9.7
1291  **/
1292 void
hb_buffer_add(hb_buffer_t * buffer,hb_codepoint_t codepoint,unsigned int cluster)1293 hb_buffer_add (hb_buffer_t    *buffer,
1294 	       hb_codepoint_t  codepoint,
1295 	       unsigned int    cluster)
1296 {
1297   buffer->add (codepoint, cluster);
1298   buffer->clear_context (1);
1299 }
1300 
1301 /**
1302  * hb_buffer_set_length:
1303  * @buffer: An #hb_buffer_t
1304  * @length: The new length of @buffer
1305  *
1306  * Similar to hb_buffer_pre_allocate(), but clears any new items added at the
1307  * end.
1308  *
1309  * Return value:
1310  * %true if @buffer memory allocation succeeded, %false otherwise.
1311  *
1312  * Since: 0.9.2
1313  **/
1314 hb_bool_t
hb_buffer_set_length(hb_buffer_t * buffer,unsigned int length)1315 hb_buffer_set_length (hb_buffer_t  *buffer,
1316 		      unsigned int  length)
1317 {
1318   if (unlikely (hb_object_is_immutable (buffer)))
1319     return length == 0;
1320 
1321   if (unlikely (!buffer->ensure (length)))
1322     return false;
1323 
1324   /* Wipe the new space */
1325   if (length > buffer->len) {
1326     memset (buffer->info + buffer->len, 0, sizeof (buffer->info[0]) * (length - buffer->len));
1327     if (buffer->have_positions)
1328       memset (buffer->pos + buffer->len, 0, sizeof (buffer->pos[0]) * (length - buffer->len));
1329   }
1330 
1331   buffer->len = length;
1332 
1333   if (!length)
1334   {
1335     buffer->content_type = HB_BUFFER_CONTENT_TYPE_INVALID;
1336     buffer->clear_context (0);
1337   }
1338   buffer->clear_context (1);
1339 
1340   return true;
1341 }
1342 
1343 /**
1344  * hb_buffer_get_length:
1345  * @buffer: An #hb_buffer_t
1346  *
1347  * Returns the number of items in the buffer.
1348  *
1349  * Return value:
1350  * The @buffer length.
1351  * The value valid as long as buffer has not been modified.
1352  *
1353  * Since: 0.9.2
1354  **/
1355 unsigned int
hb_buffer_get_length(hb_buffer_t * buffer)1356 hb_buffer_get_length (hb_buffer_t *buffer)
1357 {
1358   return buffer->len;
1359 }
1360 
1361 /**
1362  * hb_buffer_get_glyph_infos:
1363  * @buffer: An #hb_buffer_t
1364  * @length: (out): The output-array length.
1365  *
1366  * Returns @buffer glyph information array.  Returned pointer
1367  * is valid as long as @buffer contents are not modified.
1368  *
1369  * Return value: (transfer none) (array length=length):
1370  * The @buffer glyph information array.
1371  * The value valid as long as buffer has not been modified.
1372  *
1373  * Since: 0.9.2
1374  **/
1375 hb_glyph_info_t *
hb_buffer_get_glyph_infos(hb_buffer_t * buffer,unsigned int * length)1376 hb_buffer_get_glyph_infos (hb_buffer_t  *buffer,
1377 			   unsigned int *length)
1378 {
1379   if (length)
1380     *length = buffer->len;
1381 
1382   return (hb_glyph_info_t *) buffer->info;
1383 }
1384 
1385 /**
1386  * hb_buffer_get_glyph_positions:
1387  * @buffer: An #hb_buffer_t
1388  * @length: (out): The output length
1389  *
1390  * Returns @buffer glyph position array.  Returned pointer
1391  * is valid as long as @buffer contents are not modified.
1392  *
1393  * If buffer did not have positions before, the positions will be
1394  * initialized to zeros, unless this function is called from
1395  * within a buffer message callback (see hb_buffer_set_message_func()),
1396  * in which case %NULL is returned.
1397  *
1398  * Return value: (transfer none) (array length=length):
1399  * The @buffer glyph position array.
1400  * The value valid as long as buffer has not been modified.
1401  *
1402  * Since: 0.9.2
1403  **/
1404 hb_glyph_position_t *
hb_buffer_get_glyph_positions(hb_buffer_t * buffer,unsigned int * length)1405 hb_buffer_get_glyph_positions (hb_buffer_t  *buffer,
1406 			       unsigned int *length)
1407 {
1408   if (length)
1409     *length = buffer->len;
1410 
1411   if (!buffer->have_positions)
1412   {
1413     if (unlikely (buffer->message_depth))
1414       return nullptr;
1415 
1416     buffer->clear_positions ();
1417   }
1418 
1419   return (hb_glyph_position_t *) buffer->pos;
1420 }
1421 
1422 /**
1423  * hb_buffer_has_positions:
1424  * @buffer: an #hb_buffer_t.
1425  *
1426  * Returns whether @buffer has glyph position data.
1427  * A buffer gains position data when hb_buffer_get_glyph_positions() is called on it,
1428  * and cleared of position data when hb_buffer_clear_contents() is called.
1429  *
1430  * Return value:
1431  * %true if the @buffer has position array, %false otherwise.
1432  *
1433  * Since: 2.7.3
1434  **/
1435 HB_EXTERN hb_bool_t
hb_buffer_has_positions(hb_buffer_t * buffer)1436 hb_buffer_has_positions (hb_buffer_t  *buffer)
1437 {
1438   return buffer->have_positions;
1439 }
1440 
1441 /**
1442  * hb_glyph_info_get_glyph_flags:
1443  * @info: a #hb_glyph_info_t
1444  *
1445  * Returns glyph flags encoded within a #hb_glyph_info_t.
1446  *
1447  * Return value:
1448  * The #hb_glyph_flags_t encoded within @info
1449  *
1450  * Since: 1.5.0
1451  **/
hb_glyph_flags_t(hb_glyph_info_get_glyph_flags)1452 hb_glyph_flags_t
1453 (hb_glyph_info_get_glyph_flags) (const hb_glyph_info_t *info)
1454 {
1455   return hb_glyph_info_get_glyph_flags (info);
1456 }
1457 
1458 /**
1459  * hb_buffer_reverse:
1460  * @buffer: An #hb_buffer_t
1461  *
1462  * Reverses buffer contents.
1463  *
1464  * Since: 0.9.2
1465  **/
1466 void
hb_buffer_reverse(hb_buffer_t * buffer)1467 hb_buffer_reverse (hb_buffer_t *buffer)
1468 {
1469   buffer->reverse ();
1470 }
1471 
1472 /**
1473  * hb_buffer_reverse_range:
1474  * @buffer: An #hb_buffer_t
1475  * @start: start index
1476  * @end: end index
1477  *
1478  * Reverses buffer contents between @start and @end.
1479  *
1480  * Since: 0.9.41
1481  **/
1482 void
hb_buffer_reverse_range(hb_buffer_t * buffer,unsigned int start,unsigned int end)1483 hb_buffer_reverse_range (hb_buffer_t *buffer,
1484 			 unsigned int start, unsigned int end)
1485 {
1486   buffer->reverse_range (start, end);
1487 }
1488 
1489 /**
1490  * hb_buffer_reverse_clusters:
1491  * @buffer: An #hb_buffer_t
1492  *
1493  * Reverses buffer clusters.  That is, the buffer contents are
1494  * reversed, then each cluster (consecutive items having the
1495  * same cluster number) are reversed again.
1496  *
1497  * Since: 0.9.2
1498  **/
1499 void
hb_buffer_reverse_clusters(hb_buffer_t * buffer)1500 hb_buffer_reverse_clusters (hb_buffer_t *buffer)
1501 {
1502   buffer->reverse_clusters ();
1503 }
1504 
1505 /**
1506  * hb_buffer_guess_segment_properties:
1507  * @buffer: An #hb_buffer_t
1508  *
1509  * Sets unset buffer segment properties based on buffer Unicode
1510  * contents.  If buffer is not empty, it must have content type
1511  * #HB_BUFFER_CONTENT_TYPE_UNICODE.
1512  *
1513  * If buffer script is not set (ie. is #HB_SCRIPT_INVALID), it
1514  * will be set to the Unicode script of the first character in
1515  * the buffer that has a script other than #HB_SCRIPT_COMMON,
1516  * #HB_SCRIPT_INHERITED, and #HB_SCRIPT_UNKNOWN.
1517  *
1518  * Next, if buffer direction is not set (ie. is #HB_DIRECTION_INVALID),
1519  * it will be set to the natural horizontal direction of the
1520  * buffer script as returned by hb_script_get_horizontal_direction().
1521  * If hb_script_get_horizontal_direction() returns #HB_DIRECTION_INVALID,
1522  * then #HB_DIRECTION_LTR is used.
1523  *
1524  * Finally, if buffer language is not set (ie. is #HB_LANGUAGE_INVALID),
1525  * it will be set to the process's default language as returned by
1526  * hb_language_get_default().  This may change in the future by
1527  * taking buffer script into consideration when choosing a language.
1528  * Note that hb_language_get_default() is NOT threadsafe the first time
1529  * it is called.  See documentation for that function for details.
1530  *
1531  * Since: 0.9.7
1532  **/
1533 void
hb_buffer_guess_segment_properties(hb_buffer_t * buffer)1534 hb_buffer_guess_segment_properties (hb_buffer_t *buffer)
1535 {
1536   buffer->guess_segment_properties ();
1537 }
1538 
1539 template <typename utf_t>
1540 static inline void
hb_buffer_add_utf(hb_buffer_t * buffer,const typename utf_t::codepoint_t * text,int text_length,unsigned int item_offset,int item_length)1541 hb_buffer_add_utf (hb_buffer_t  *buffer,
1542 		   const typename utf_t::codepoint_t *text,
1543 		   int           text_length,
1544 		   unsigned int  item_offset,
1545 		   int           item_length)
1546 {
1547   typedef typename utf_t::codepoint_t T;
1548   const hb_codepoint_t replacement = buffer->replacement;
1549 
1550   buffer->assert_unicode ();
1551 
1552   if (unlikely (hb_object_is_immutable (buffer)))
1553     return;
1554 
1555   if (text_length == -1)
1556     text_length = utf_t::strlen (text);
1557 
1558   if (item_length == -1)
1559     item_length = text_length - item_offset;
1560 
1561   if (unlikely (item_length < 0 ||
1562 		item_length > INT_MAX / 8 ||
1563 		!buffer->ensure (buffer->len + item_length * sizeof (T) / 4)))
1564     return;
1565 
1566   /* If buffer is empty and pre-context provided, install it.
1567    * This check is written this way, to make sure people can
1568    * provide pre-context in one add_utf() call, then provide
1569    * text in a follow-up call.  See:
1570    *
1571    * https://bugzilla.mozilla.org/show_bug.cgi?id=801410#c13
1572    */
1573   if (!buffer->len && item_offset > 0)
1574   {
1575     /* Add pre-context */
1576     buffer->clear_context (0);
1577     const T *prev = text + item_offset;
1578     const T *start = text;
1579     while (start < prev && buffer->context_len[0] < buffer->CONTEXT_LENGTH)
1580     {
1581       hb_codepoint_t u;
1582       prev = utf_t::prev (prev, start, &u, replacement);
1583       buffer->context[0][buffer->context_len[0]++] = u;
1584     }
1585   }
1586 
1587   const T *next = text + item_offset;
1588   const T *end = next + item_length;
1589   while (next < end)
1590   {
1591     hb_codepoint_t u;
1592     const T *old_next = next;
1593     next = utf_t::next (next, end, &u, replacement);
1594     buffer->add (u, old_next - (const T *) text);
1595   }
1596 
1597   /* Add post-context */
1598   buffer->clear_context (1);
1599   end = text + text_length;
1600   while (next < end && buffer->context_len[1] < buffer->CONTEXT_LENGTH)
1601   {
1602     hb_codepoint_t u;
1603     next = utf_t::next (next, end, &u, replacement);
1604     buffer->context[1][buffer->context_len[1]++] = u;
1605   }
1606 
1607   buffer->content_type = HB_BUFFER_CONTENT_TYPE_UNICODE;
1608 }
1609 
1610 /**
1611  * hb_buffer_add_utf8:
1612  * @buffer: An #hb_buffer_t
1613  * @text: (array length=text_length) (element-type uint8_t): An array of UTF-8
1614  *               characters to append.
1615  * @text_length: The length of the @text, or -1 if it is %NULL terminated.
1616  * @item_offset: The offset of the first character to add to the @buffer.
1617  * @item_length: The number of characters to add to the @buffer, or -1 for the
1618  *               end of @text (assuming it is %NULL terminated).
1619  *
1620  * See hb_buffer_add_codepoints().
1621  *
1622  * Replaces invalid UTF-8 characters with the @buffer replacement code point,
1623  * see hb_buffer_set_replacement_codepoint().
1624  *
1625  * Since: 0.9.2
1626  **/
1627 void
hb_buffer_add_utf8(hb_buffer_t * buffer,const char * text,int text_length,unsigned int item_offset,int item_length)1628 hb_buffer_add_utf8 (hb_buffer_t  *buffer,
1629 		    const char   *text,
1630 		    int           text_length,
1631 		    unsigned int  item_offset,
1632 		    int           item_length)
1633 {
1634   hb_buffer_add_utf<hb_utf8_t> (buffer, (const uint8_t *) text, text_length, item_offset, item_length);
1635 }
1636 
1637 /**
1638  * hb_buffer_add_utf16:
1639  * @buffer: An #hb_buffer_t
1640  * @text: (array length=text_length): An array of UTF-16 characters to append
1641  * @text_length: The length of the @text, or -1 if it is %NULL terminated
1642  * @item_offset: The offset of the first character to add to the @buffer
1643  * @item_length: The number of characters to add to the @buffer, or -1 for the
1644  *               end of @text (assuming it is %NULL terminated)
1645  *
1646  * See hb_buffer_add_codepoints().
1647  *
1648  * Replaces invalid UTF-16 characters with the @buffer replacement code point,
1649  * see hb_buffer_set_replacement_codepoint().
1650  *
1651  * Since: 0.9.2
1652  **/
1653 void
hb_buffer_add_utf16(hb_buffer_t * buffer,const uint16_t * text,int text_length,unsigned int item_offset,int item_length)1654 hb_buffer_add_utf16 (hb_buffer_t    *buffer,
1655 		     const uint16_t *text,
1656 		     int             text_length,
1657 		     unsigned int    item_offset,
1658 		     int             item_length)
1659 {
1660   hb_buffer_add_utf<hb_utf16_t> (buffer, text, text_length, item_offset, item_length);
1661 }
1662 
1663 /**
1664  * hb_buffer_add_utf32:
1665  * @buffer: An #hb_buffer_t
1666  * @text: (array length=text_length): An array of UTF-32 characters to append
1667  * @text_length: The length of the @text, or -1 if it is %NULL terminated
1668  * @item_offset: The offset of the first character to add to the @buffer
1669  * @item_length: The number of characters to add to the @buffer, or -1 for the
1670  *               end of @text (assuming it is %NULL terminated)
1671  *
1672  * See hb_buffer_add_codepoints().
1673  *
1674  * Replaces invalid UTF-32 characters with the @buffer replacement code point,
1675  * see hb_buffer_set_replacement_codepoint().
1676  *
1677  * Since: 0.9.2
1678  **/
1679 void
hb_buffer_add_utf32(hb_buffer_t * buffer,const uint32_t * text,int text_length,unsigned int item_offset,int item_length)1680 hb_buffer_add_utf32 (hb_buffer_t    *buffer,
1681 		     const uint32_t *text,
1682 		     int             text_length,
1683 		     unsigned int    item_offset,
1684 		     int             item_length)
1685 {
1686   hb_buffer_add_utf<hb_utf32_t> (buffer, text, text_length, item_offset, item_length);
1687 }
1688 
1689 /**
1690  * hb_buffer_add_latin1:
1691  * @buffer: An #hb_buffer_t
1692  * @text: (array length=text_length) (element-type uint8_t): an array of UTF-8
1693  *               characters to append
1694  * @text_length: the length of the @text, or -1 if it is %NULL terminated
1695  * @item_offset: the offset of the first character to add to the @buffer
1696  * @item_length: the number of characters to add to the @buffer, or -1 for the
1697  *               end of @text (assuming it is %NULL terminated)
1698  *
1699  * Similar to hb_buffer_add_codepoints(), but allows only access to first 256
1700  * Unicode code points that can fit in 8-bit strings.
1701  *
1702  * <note>Has nothing to do with non-Unicode Latin-1 encoding.</note>
1703  *
1704  * Since: 0.9.39
1705  **/
1706 void
hb_buffer_add_latin1(hb_buffer_t * buffer,const uint8_t * text,int text_length,unsigned int item_offset,int item_length)1707 hb_buffer_add_latin1 (hb_buffer_t   *buffer,
1708 		      const uint8_t *text,
1709 		      int            text_length,
1710 		      unsigned int   item_offset,
1711 		      int            item_length)
1712 {
1713   hb_buffer_add_utf<hb_latin1_t> (buffer, text, text_length, item_offset, item_length);
1714 }
1715 
1716 /**
1717  * hb_buffer_add_codepoints:
1718  * @buffer: a #hb_buffer_t to append characters to.
1719  * @text: (array length=text_length): an array of Unicode code points to append.
1720  * @text_length: the length of the @text, or -1 if it is %NULL terminated.
1721  * @item_offset: the offset of the first code point to add to the @buffer.
1722  * @item_length: the number of code points to add to the @buffer, or -1 for the
1723  *               end of @text (assuming it is %NULL terminated).
1724  *
1725  * Appends characters from @text array to @buffer. The @item_offset is the
1726  * position of the first character from @text that will be appended, and
1727  * @item_length is the number of character. When shaping part of a larger text
1728  * (e.g. a run of text from a paragraph), instead of passing just the substring
1729  * corresponding to the run, it is preferable to pass the whole
1730  * paragraph and specify the run start and length as @item_offset and
1731  * @item_length, respectively, to give HarfBuzz the full context to be able,
1732  * for example, to do cross-run Arabic shaping or properly handle combining
1733  * marks at stat of run.
1734  *
1735  * This function does not check the validity of @text, it is up to the caller
1736  * to ensure it contains a valid Unicode code points.
1737  *
1738  * Since: 0.9.31
1739  **/
1740 void
hb_buffer_add_codepoints(hb_buffer_t * buffer,const hb_codepoint_t * text,int text_length,unsigned int item_offset,int item_length)1741 hb_buffer_add_codepoints (hb_buffer_t          *buffer,
1742 			  const hb_codepoint_t *text,
1743 			  int                   text_length,
1744 			  unsigned int          item_offset,
1745 			  int                   item_length)
1746 {
1747   hb_buffer_add_utf<hb_utf32_novalidate_t> (buffer, text, text_length, item_offset, item_length);
1748 }
1749 
1750 
1751 /**
1752  * hb_buffer_append:
1753  * @buffer: An #hb_buffer_t
1754  * @source: source #hb_buffer_t
1755  * @start: start index into source buffer to copy.  Use 0 to copy from start of buffer.
1756  * @end: end index into source buffer to copy.  Use @HB_FEATURE_GLOBAL_END to copy to end of buffer.
1757  *
1758  * Append (part of) contents of another buffer to this buffer.
1759  *
1760  * Since: 1.5.0
1761  **/
1762 HB_EXTERN void
hb_buffer_append(hb_buffer_t * buffer,hb_buffer_t * source,unsigned int start,unsigned int end)1763 hb_buffer_append (hb_buffer_t *buffer,
1764 		  hb_buffer_t *source,
1765 		  unsigned int start,
1766 		  unsigned int end)
1767 {
1768   assert (!buffer->have_output && !source->have_output);
1769   assert (buffer->have_positions == source->have_positions ||
1770 	  !buffer->len || !source->len);
1771   assert (buffer->content_type == source->content_type ||
1772 	  !buffer->len || !source->len);
1773 
1774   if (end > source->len)
1775     end = source->len;
1776   if (start > end)
1777     start = end;
1778   if (start == end)
1779     return;
1780 
1781   if (buffer->len + (end - start) < buffer->len) /* Overflows. */
1782   {
1783     buffer->successful = false;
1784     return;
1785   }
1786 
1787   unsigned int orig_len = buffer->len;
1788   hb_buffer_set_length (buffer, buffer->len + (end - start));
1789   if (unlikely (!buffer->successful))
1790     return;
1791 
1792   if (!orig_len)
1793     buffer->content_type = source->content_type;
1794   if (!buffer->have_positions && source->have_positions)
1795     buffer->clear_positions ();
1796 
1797   memcpy (buffer->info + orig_len, source->info + start, (end - start) * sizeof (buffer->info[0]));
1798   if (buffer->have_positions)
1799     memcpy (buffer->pos + orig_len, source->pos + start, (end - start) * sizeof (buffer->pos[0]));
1800 
1801   if (source->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE)
1802   {
1803     /* See similar logic in add_utf. */
1804 
1805     /* pre-context */
1806     if (!orig_len && start + source->context_len[0] > 0)
1807     {
1808       buffer->clear_context (0);
1809       while (start > 0 && buffer->context_len[0] < buffer->CONTEXT_LENGTH)
1810 	buffer->context[0][buffer->context_len[0]++] = source->info[--start].codepoint;
1811       for (auto i = 0u; i < source->context_len[0] && buffer->context_len[0] < buffer->CONTEXT_LENGTH; i++)
1812 	buffer->context[0][buffer->context_len[0]++] = source->context[0][i];
1813     }
1814 
1815     /* post-context */
1816     buffer->clear_context (1);
1817     while (end < source->len && buffer->context_len[1] < buffer->CONTEXT_LENGTH)
1818       buffer->context[1][buffer->context_len[1]++] = source->info[end++].codepoint;
1819     for (auto i = 0u; i < source->context_len[1] && buffer->context_len[1] < buffer->CONTEXT_LENGTH; i++)
1820       buffer->context[1][buffer->context_len[1]++] = source->context[1][i];
1821   }
1822 }
1823 
1824 
1825 static int
compare_info_codepoint(const hb_glyph_info_t * pa,const hb_glyph_info_t * pb)1826 compare_info_codepoint (const hb_glyph_info_t *pa,
1827 			const hb_glyph_info_t *pb)
1828 {
1829   return (int) pb->codepoint - (int) pa->codepoint;
1830 }
1831 
1832 static inline void
normalize_glyphs_cluster(hb_buffer_t * buffer,unsigned int start,unsigned int end,bool backward)1833 normalize_glyphs_cluster (hb_buffer_t *buffer,
1834 			  unsigned int start,
1835 			  unsigned int end,
1836 			  bool backward)
1837 {
1838   hb_glyph_position_t *pos = buffer->pos;
1839 
1840   /* Total cluster advance */
1841   hb_position_t total_x_advance = 0, total_y_advance = 0;
1842   for (unsigned int i = start; i < end; i++)
1843   {
1844     total_x_advance += pos[i].x_advance;
1845     total_y_advance += pos[i].y_advance;
1846   }
1847 
1848   hb_position_t x_advance = 0, y_advance = 0;
1849   for (unsigned int i = start; i < end; i++)
1850   {
1851     pos[i].x_offset += x_advance;
1852     pos[i].y_offset += y_advance;
1853 
1854     x_advance += pos[i].x_advance;
1855     y_advance += pos[i].y_advance;
1856 
1857     pos[i].x_advance = 0;
1858     pos[i].y_advance = 0;
1859   }
1860 
1861   if (backward)
1862   {
1863     /* Transfer all cluster advance to the last glyph. */
1864     pos[end - 1].x_advance = total_x_advance;
1865     pos[end - 1].y_advance = total_y_advance;
1866 
1867     hb_stable_sort (buffer->info + start, end - start - 1, compare_info_codepoint, buffer->pos + start);
1868   } else {
1869     /* Transfer all cluster advance to the first glyph. */
1870     pos[start].x_advance += total_x_advance;
1871     pos[start].y_advance += total_y_advance;
1872     for (unsigned int i = start + 1; i < end; i++) {
1873       pos[i].x_offset -= total_x_advance;
1874       pos[i].y_offset -= total_y_advance;
1875     }
1876     hb_stable_sort (buffer->info + start + 1, end - start - 1, compare_info_codepoint, buffer->pos + start + 1);
1877   }
1878 }
1879 
1880 /**
1881  * hb_buffer_normalize_glyphs:
1882  * @buffer: An #hb_buffer_t
1883  *
1884  * Reorders a glyph buffer to have canonical in-cluster glyph order / position.
1885  * The resulting clusters should behave identical to pre-reordering clusters.
1886  *
1887  * <note>This has nothing to do with Unicode normalization.</note>
1888  *
1889  * Since: 0.9.2
1890  **/
1891 void
hb_buffer_normalize_glyphs(hb_buffer_t * buffer)1892 hb_buffer_normalize_glyphs (hb_buffer_t *buffer)
1893 {
1894   assert (buffer->have_positions);
1895 
1896   buffer->assert_glyphs ();
1897 
1898   bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
1899 
1900   foreach_cluster (buffer, start, end)
1901     normalize_glyphs_cluster (buffer, start, end, backward);
1902 }
1903 
1904 void
sort(unsigned int start,unsigned int end,int (* compar)(const hb_glyph_info_t *,const hb_glyph_info_t *))1905 hb_buffer_t::sort (unsigned int start, unsigned int end, int(*compar)(const hb_glyph_info_t *, const hb_glyph_info_t *))
1906 {
1907   assert (!have_positions);
1908   for (unsigned int i = start + 1; i < end; i++)
1909   {
1910     unsigned int j = i;
1911     while (j > start && compar (&info[j - 1], &info[i]) > 0)
1912       j--;
1913     if (i == j)
1914       continue;
1915     /* Move item i to occupy place for item j, shift what's in between. */
1916     merge_clusters (j, i + 1);
1917     {
1918       hb_glyph_info_t t = info[i];
1919       memmove (&info[j + 1], &info[j], (i - j) * sizeof (hb_glyph_info_t));
1920       info[j] = t;
1921     }
1922   }
1923 }
1924 
1925 
1926 /*
1927  * Comparing buffers.
1928  */
1929 
1930 /**
1931  * hb_buffer_diff:
1932  * @buffer: a buffer.
1933  * @reference: other buffer to compare to.
1934  * @dottedcircle_glyph: glyph id of U+25CC DOTTED CIRCLE, or (hb_codepont_t) -1.
1935  * @position_fuzz: allowed absolute difference in position values.
1936  *
1937  * If dottedcircle_glyph is (hb_codepoint_t) -1 then #HB_BUFFER_DIFF_FLAG_DOTTED_CIRCLE_PRESENT
1938  * and #HB_BUFFER_DIFF_FLAG_NOTDEF_PRESENT are never returned.  This should be used by most
1939  * callers if just comparing two buffers is needed.
1940  *
1941  * Since: 1.5.0
1942  **/
1943 hb_buffer_diff_flags_t
hb_buffer_diff(hb_buffer_t * buffer,hb_buffer_t * reference,hb_codepoint_t dottedcircle_glyph,unsigned int position_fuzz)1944 hb_buffer_diff (hb_buffer_t *buffer,
1945 		hb_buffer_t *reference,
1946 		hb_codepoint_t dottedcircle_glyph,
1947 		unsigned int position_fuzz)
1948 {
1949   if (buffer->content_type != reference->content_type && buffer->len && reference->len)
1950     return HB_BUFFER_DIFF_FLAG_CONTENT_TYPE_MISMATCH;
1951 
1952   hb_buffer_diff_flags_t result = HB_BUFFER_DIFF_FLAG_EQUAL;
1953   bool contains = dottedcircle_glyph != (hb_codepoint_t) -1;
1954 
1955   unsigned int count = reference->len;
1956 
1957   if (buffer->len != count)
1958   {
1959     /*
1960      * we can't compare glyph-by-glyph, but we do want to know if there
1961      * are .notdef or dottedcircle glyphs present in the reference buffer
1962      */
1963     const hb_glyph_info_t *info = reference->info;
1964     unsigned int i;
1965     for (i = 0; i < count; i++)
1966     {
1967       if (contains && info[i].codepoint == dottedcircle_glyph)
1968 	result |= HB_BUFFER_DIFF_FLAG_DOTTED_CIRCLE_PRESENT;
1969       if (contains && info[i].codepoint == 0)
1970 	result |= HB_BUFFER_DIFF_FLAG_NOTDEF_PRESENT;
1971     }
1972     result |= HB_BUFFER_DIFF_FLAG_LENGTH_MISMATCH;
1973     return hb_buffer_diff_flags_t (result);
1974   }
1975 
1976   if (!count)
1977     return hb_buffer_diff_flags_t (result);
1978 
1979   const hb_glyph_info_t *buf_info = buffer->info;
1980   const hb_glyph_info_t *ref_info = reference->info;
1981   for (unsigned int i = 0; i < count; i++)
1982   {
1983     if (buf_info->codepoint != ref_info->codepoint)
1984       result |= HB_BUFFER_DIFF_FLAG_CODEPOINT_MISMATCH;
1985     if (buf_info->cluster != ref_info->cluster)
1986       result |= HB_BUFFER_DIFF_FLAG_CLUSTER_MISMATCH;
1987     if ((buf_info->mask & ~ref_info->mask & HB_GLYPH_FLAG_DEFINED))
1988       result |= HB_BUFFER_DIFF_FLAG_GLYPH_FLAGS_MISMATCH;
1989     if (contains && ref_info->codepoint == dottedcircle_glyph)
1990       result |= HB_BUFFER_DIFF_FLAG_DOTTED_CIRCLE_PRESENT;
1991     if (contains && ref_info->codepoint == 0)
1992       result |= HB_BUFFER_DIFF_FLAG_NOTDEF_PRESENT;
1993     buf_info++;
1994     ref_info++;
1995   }
1996 
1997   if (buffer->content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS)
1998   {
1999     assert (buffer->have_positions);
2000     const hb_glyph_position_t *buf_pos = buffer->pos;
2001     const hb_glyph_position_t *ref_pos = reference->pos;
2002     for (unsigned int i = 0; i < count; i++)
2003     {
2004       if ((unsigned int) abs (buf_pos->x_advance - ref_pos->x_advance) > position_fuzz ||
2005 	  (unsigned int) abs (buf_pos->y_advance - ref_pos->y_advance) > position_fuzz ||
2006 	  (unsigned int) abs (buf_pos->x_offset - ref_pos->x_offset) > position_fuzz ||
2007 	  (unsigned int) abs (buf_pos->y_offset - ref_pos->y_offset) > position_fuzz)
2008       {
2009 	result |= HB_BUFFER_DIFF_FLAG_POSITION_MISMATCH;
2010 	break;
2011       }
2012       buf_pos++;
2013       ref_pos++;
2014     }
2015   }
2016 
2017   return result;
2018 }
2019 
2020 
2021 /*
2022  * Debugging.
2023  */
2024 
2025 #ifndef HB_NO_BUFFER_MESSAGE
2026 /**
2027  * hb_buffer_set_message_func:
2028  * @buffer: An #hb_buffer_t
2029  * @func: (closure user_data) (destroy destroy) (scope notified): Callback function
2030  * @user_data: (nullable): Data to pass to @func
2031  * @destroy: (nullable): The function to call when @user_data is not needed anymore
2032  *
2033  * Sets the implementation function for #hb_buffer_message_func_t.
2034  *
2035  * Since: 1.1.3
2036  **/
2037 void
hb_buffer_set_message_func(hb_buffer_t * buffer,hb_buffer_message_func_t func,void * user_data,hb_destroy_func_t destroy)2038 hb_buffer_set_message_func (hb_buffer_t *buffer,
2039 			    hb_buffer_message_func_t func,
2040 			    void *user_data, hb_destroy_func_t destroy)
2041 {
2042   if (buffer->message_destroy)
2043     buffer->message_destroy (buffer->message_data);
2044 
2045   if (func) {
2046     buffer->message_func = func;
2047     buffer->message_data = user_data;
2048     buffer->message_destroy = destroy;
2049   } else {
2050     buffer->message_func = nullptr;
2051     buffer->message_data = nullptr;
2052     buffer->message_destroy = nullptr;
2053   }
2054 }
2055 bool
message_impl(hb_font_t * font,const char * fmt,va_list ap)2056 hb_buffer_t::message_impl (hb_font_t *font, const char *fmt, va_list ap)
2057 {
2058   char buf[100];
2059   vsnprintf (buf, sizeof (buf), fmt, ap);
2060   return (bool) this->message_func (this, font, buf, this->message_data);
2061 }
2062 #endif
2063