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