2 * Copyright © 1998-2004 David Turner and Werner Lemberg
3 * Copyright © 2004,2007,2009,2010 Red Hat, Inc.
4 * Copyright © 2011,2012 Google, Inc.
6 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
26 * Red Hat Author(s): Owen Taylor, Behdad Esfahbod
27 * Google Author(s): Behdad Esfahbod
30 #include "hb-buffer.hh"
37 * @short_description: Input and output buffers
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.
47 * hb_segment_properties_equal:
48 * @a: first #hb_segment_properties_t to compare.
49 * @b: second #hb_segment_properties_t to compare.
51 * Checks the equality of two #hb_segment_properties_t's.
54 * %true if all properties of @a equal those of @b, %false otherwise.
59 hb_segment_properties_equal (const hb_segment_properties_t *a,
60 const hb_segment_properties_t *b)
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;
71 * hb_segment_properties_hash:
72 * @p: #hb_segment_properties_t to hash.
74 * Creates a hash representing @p.
82 hb_segment_properties_hash (const hb_segment_properties_t *p)
84 return (unsigned int) p->direction ^
85 (unsigned int) p->script ^
86 (intptr_t) (p->language);
90 * hb_segment_properties_overlay:
91 * @p: #hb_segment_properties_t to fill in.
92 * @src: #hb_segment_properties_t to fill in from.
94 * Fills in missing fields of @p from @src in a considered manner.
96 * First, if @p does not have direction set, direction is copied from @src.
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.
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
108 hb_segment_properties_overlay (hb_segment_properties_t *p,
109 const hb_segment_properties_t *src)
111 if (unlikely (!p || !src))
115 p->direction = src->direction;
117 if (p->direction != src->direction)
121 p->script = src->script;
123 if (p->script != src->script)
127 p->language = src->language;
130 /* Here is how the buffer works internally:
132 * There are two info pointers: info and out_info. They always have
133 * the same allocated size, but different lengths.
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.
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.
145 * This should all remain transparent to the user. sync() then
146 * switches info over to out_info and does housekeeping.
154 hb_buffer_t::enlarge (unsigned int size)
156 if (unlikely (!successful))
158 if (unlikely (size > max_len))
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;
169 if (unlikely (hb_unsigned_mul_overflows (size, sizeof (info[0]))))
172 while (size >= new_allocated)
173 new_allocated += (new_allocated >> 1) + 32;
175 static_assert ((sizeof (info[0]) == sizeof (pos[0])), "");
176 if (unlikely (hb_unsigned_mul_overflows (new_allocated, sizeof (info[0]))))
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]));
183 if (unlikely (!new_pos || !new_info))
186 if (likely (new_pos))
189 if (likely (new_info))
192 out_info = separate_out ? (hb_glyph_info_t *) pos : info;
193 if (likely (successful))
194 allocated = new_allocated;
196 return likely (successful);
200 hb_buffer_t::make_room_for (unsigned int num_in,
201 unsigned int num_out)
203 if (unlikely (!ensure (out_len + num_out))) return false;
205 if (out_info == info &&
206 out_len + num_out > idx + num_in)
208 assert (have_output);
210 out_info = (hb_glyph_info_t *) pos;
211 memcpy (out_info, info, out_len * sizeof (out_info[0]));
218 hb_buffer_t::shift_forward (unsigned int count)
220 assert (have_output);
221 if (unlikely (!ensure (len + count))) return false;
223 memmove (info + idx + count, info + idx, (len - idx) * sizeof (info[0]));
224 if (idx + count > len)
226 /* Under memory failure we might expose this area. At least
227 * clean it up. Oh well...
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]));
240 hb_buffer_t::scratch_buffer_t *
241 hb_buffer_t::get_scratch_buffer (unsigned int *size)
244 have_positions = false;
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;
256 /* HarfBuzz-Internal API */
259 hb_buffer_t::similar (const hb_buffer_t &src)
261 hb_unicode_funcs_destroy (unicode);
262 unicode = hb_unicode_funcs_reference (src.unicode);
264 cluster_level = src.cluster_level;
265 replacement = src.invisible;
266 invisible = src.invisible;
267 not_found = src.not_found;
271 hb_buffer_t::reset ()
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;
285 hb_buffer_t::clear ()
287 content_type = HB_BUFFER_CONTENT_TYPE_INVALID;
288 hb_segment_properties_t default_props = HB_SEGMENT_PROPERTIES_DEFAULT;
289 props = default_props;
293 have_positions = false;
301 memset (context, 0, sizeof context);
302 memset (context_len, 0, sizeof context_len);
304 deallocate_var_all ();
306 scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;
310 hb_buffer_t::enter ()
312 deallocate_var_all ();
314 scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;
315 if (likely (!hb_unsigned_mul_overflows (len, HB_BUFFER_MAX_LEN_FACTOR)))
317 max_len = hb_max (len * HB_BUFFER_MAX_LEN_FACTOR,
318 (unsigned) HB_BUFFER_MAX_LEN_MIN);
320 if (likely (!hb_unsigned_mul_overflows (len, HB_BUFFER_MAX_OPS_FACTOR)))
322 max_ops = hb_max (len * HB_BUFFER_MAX_OPS_FACTOR,
323 (unsigned) HB_BUFFER_MAX_OPS_MIN);
327 hb_buffer_t::leave ()
329 max_len = HB_BUFFER_MAX_LEN_DEFAULT;
330 max_ops = HB_BUFFER_MAX_OPS_DEFAULT;
331 deallocate_var_all ();
337 hb_buffer_t::add (hb_codepoint_t codepoint,
338 unsigned int cluster)
340 hb_glyph_info_t *glyph;
342 if (unlikely (!ensure (len + 1))) return;
346 memset (glyph, 0, sizeof (*glyph));
347 glyph->codepoint = codepoint;
349 glyph->cluster = cluster;
355 hb_buffer_t::add_info (const hb_glyph_info_t &glyph_info)
357 if (unlikely (!ensure (len + 1))) return;
359 info[len] = glyph_info;
366 hb_buffer_t::clear_output ()
369 have_positions = false;
377 hb_buffer_t::clear_positions ()
380 have_positions = true;
385 hb_memset (pos, 0, sizeof (pos[0]) * len);
391 assert (have_output);
395 if (unlikely (!successful || !next_glyphs (len - idx)))
398 if (out_info != info)
400 pos = (hb_glyph_position_t *) info;
412 hb_buffer_t::move_to (unsigned int i)
420 if (unlikely (!successful))
423 assert (i <= out_len + (len - idx));
427 unsigned int count = i - out_len;
428 if (unlikely (!make_room_for (count, count))) return false;
430 memmove (out_info + out_len, info + idx, count * sizeof (out_info[0]));
434 else if (out_len > i)
436 /* Tricky part: rewinding... */
437 unsigned int count = out_len - i;
439 /* This will blow in our face if memory allocation fails later
440 * in this same lookup...
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;
448 assert (idx >= count);
452 memmove (info + idx, out_info + out_len, count * sizeof (out_info[0]));
460 hb_buffer_t::set_masks (hb_mask_t value,
462 unsigned int cluster_start,
463 unsigned int cluster_end)
465 hb_mask_t not_mask = ~mask;
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;
478 hb_buffer_t::merge_clusters_impl (unsigned int start,
481 if (cluster_level == HB_BUFFER_CLUSTER_LEVEL_CHARACTERS)
483 unsafe_to_break (start, end);
487 unsigned int cluster = info[start].cluster;
489 for (unsigned int i = start + 1; i < end; i++)
490 cluster = hb_min (cluster, info[i].cluster);
493 while (end < len && info[end - 1].cluster == info[end].cluster)
497 while (idx < start && info[start - 1].cluster == info[start].cluster)
500 /* If we hit the start of buffer, continue in out-buffer. */
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);
505 for (unsigned int i = start; i < end; i++)
506 set_cluster (info[i], cluster);
509 hb_buffer_t::merge_out_clusters (unsigned int start,
512 if (cluster_level == HB_BUFFER_CLUSTER_LEVEL_CHARACTERS)
515 if (unlikely (end - start < 2))
518 unsigned int cluster = out_info[start].cluster;
520 for (unsigned int i = start + 1; i < end; i++)
521 cluster = hb_min (cluster, out_info[i].cluster);
524 while (start && out_info[start - 1].cluster == out_info[start].cluster)
528 while (end < out_len && out_info[end - 1].cluster == out_info[end].cluster)
531 /* If we hit the end of out-buffer, continue in buffer. */
533 for (unsigned int i = idx; i < len && info[i].cluster == out_info[end - 1].cluster; i++)
534 set_cluster (info[i], cluster);
536 for (unsigned int i = start; i < end; i++)
537 set_cluster (out_info[i], cluster);
540 hb_buffer_t::delete_glyph ()
542 /* The logic here is duplicated in hb_ot_hide_default_ignorables(). */
544 unsigned int cluster = info[idx].cluster;
545 if (idx + 1 < len && cluster == info[idx + 1].cluster)
547 /* Cluster survives; do nothing. */
553 /* Merge cluster backward. */
554 if (cluster < out_info[out_len - 1].cluster)
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);
566 /* Merge cluster forward. */
567 merge_clusters (idx, idx + 2);
576 hb_buffer_t::guess_segment_properties ()
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;
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;
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 ();
610 DEFINE_NULL_INSTANCE (hb_buffer_t) =
612 HB_OBJECT_HEADER_STATIC,
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,
622 HB_BUFFER_CONTENT_TYPE_INVALID,
623 HB_SEGMENT_PROPERTIES_DEFAULT,
625 false, /* successful */
626 false, /* have_output */
627 true /* have_positions */
629 /* Zero is good enough for everything else. */
634 * hb_buffer_create: (Xconstructor)
636 * Creates a new #hb_buffer_t with all properties to defaults.
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.
652 if (!(buffer = hb_object_create<hb_buffer_t> ()))
653 return hb_buffer_get_empty ();
655 buffer->max_len = HB_BUFFER_MAX_LEN_DEFAULT;
656 buffer->max_ops = HB_BUFFER_MAX_OPS_DEFAULT;
664 * hb_buffer_create_similar:
665 * @src: An #hb_buffer_t
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.
670 * Return value: (transfer full):
671 * A newly allocated #hb_buffer_t, similar to hb_buffer_create().
676 hb_buffer_create_similar (const hb_buffer_t *src)
678 hb_buffer_t *buffer = hb_buffer_create ();
680 buffer->similar (*src);
687 * @buffer: An #hb_buffer_t
689 * Resets the buffer to its initial status, as if it was just newly created
690 * with hb_buffer_create().
695 hb_buffer_reset (hb_buffer_t *buffer)
697 if (unlikely (hb_object_is_immutable (buffer)))
704 * hb_buffer_get_empty:
706 * Fetches an empty #hb_buffer_t.
708 * Return value: (transfer full): The empty buffer
713 hb_buffer_get_empty ()
715 return const_cast<hb_buffer_t *> (&Null (hb_buffer_t));
719 * hb_buffer_reference: (skip)
720 * @buffer: An #hb_buffer_t
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.
725 * Return value: (transfer full):
726 * The referenced #hb_buffer_t.
731 hb_buffer_reference (hb_buffer_t *buffer)
733 return hb_object_reference (buffer);
737 * hb_buffer_destroy: (skip)
738 * @buffer: An #hb_buffer_t
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().
747 hb_buffer_destroy (hb_buffer_t *buffer)
749 if (!hb_object_destroy (buffer)) return;
751 hb_unicode_funcs_destroy (buffer->unicode);
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);
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
771 * Attaches a user-data key/data pair to the specified buffer.
773 * Return value: %true if success, %false otherwise
778 hb_buffer_set_user_data (hb_buffer_t *buffer,
779 hb_user_data_key_t *key,
781 hb_destroy_func_t destroy,
784 return hb_object_set_user_data (buffer, key, data, destroy, replace);
788 * hb_buffer_get_user_data: (skip)
789 * @buffer: An #hb_buffer_t
790 * @key: The user-data key to query
792 * Fetches the user data associated with the specified key,
793 * attached to the specified buffer.
795 * Return value: (transfer none): A pointer to the user data
800 hb_buffer_get_user_data (hb_buffer_t *buffer,
801 hb_user_data_key_t *key)
803 return hb_object_get_user_data (buffer, key);
808 * hb_buffer_set_content_type:
809 * @buffer: An #hb_buffer_t
810 * @content_type: The type of buffer contents to set
812 * Sets the type of @buffer contents. Buffers are either empty, contain
813 * characters (before shaping), or contain glyphs (the result of shaping).
818 hb_buffer_set_content_type (hb_buffer_t *buffer,
819 hb_buffer_content_type_t content_type)
821 buffer->content_type = content_type;
825 * hb_buffer_get_content_type:
826 * @buffer: An #hb_buffer_t
828 * Fetches the type of @buffer contents. Buffers are either empty, contain
829 * characters (before shaping), or contain glyphs (the result of shaping).
832 * The type of @buffer contents
836 hb_buffer_content_type_t
837 hb_buffer_get_content_type (hb_buffer_t *buffer)
839 return buffer->content_type;
844 * hb_buffer_set_unicode_funcs:
845 * @buffer: An #hb_buffer_t
846 * @unicode_funcs: The Unicode-functions structure
848 * Sets the Unicode-functions structure of a buffer to
854 hb_buffer_set_unicode_funcs (hb_buffer_t *buffer,
855 hb_unicode_funcs_t *unicode_funcs)
857 if (unlikely (hb_object_is_immutable (buffer)))
861 unicode_funcs = hb_unicode_funcs_get_default ();
863 hb_unicode_funcs_reference (unicode_funcs);
864 hb_unicode_funcs_destroy (buffer->unicode);
865 buffer->unicode = unicode_funcs;
869 * hb_buffer_get_unicode_funcs:
870 * @buffer: An #hb_buffer_t
872 * Fetches the Unicode-functions structure of a buffer.
874 * Return value: The Unicode-functions structure
879 hb_buffer_get_unicode_funcs (hb_buffer_t *buffer)
881 return buffer->unicode;
885 * hb_buffer_set_direction:
886 * @buffer: An #hb_buffer_t
887 * @direction: the #hb_direction_t of the @buffer
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
900 hb_buffer_set_direction (hb_buffer_t *buffer,
901 hb_direction_t direction)
904 if (unlikely (hb_object_is_immutable (buffer)))
907 buffer->props.direction = direction;
911 * hb_buffer_get_direction:
912 * @buffer: An #hb_buffer_t
914 * See hb_buffer_set_direction()
917 * The direction of the @buffer.
922 hb_buffer_get_direction (hb_buffer_t *buffer)
924 return buffer->props.direction;
928 * hb_buffer_set_script:
929 * @buffer: An #hb_buffer_t
930 * @script: An #hb_script_t to set.
932 * Sets the script of @buffer to @script.
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
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.
945 hb_buffer_set_script (hb_buffer_t *buffer,
948 if (unlikely (hb_object_is_immutable (buffer)))
951 buffer->props.script = script;
955 * hb_buffer_get_script:
956 * @buffer: An #hb_buffer_t
958 * Fetches the script of @buffer.
961 * The #hb_script_t of the @buffer
966 hb_buffer_get_script (hb_buffer_t *buffer)
968 return buffer->props.script;
972 * hb_buffer_set_language:
973 * @buffer: An #hb_buffer_t
974 * @language: An hb_language_t to set
976 * Sets the language of @buffer to @language.
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.
983 * Use hb_language_from_string() to convert from BCP 47 language tags to
989 hb_buffer_set_language (hb_buffer_t *buffer,
990 hb_language_t language)
992 if (unlikely (hb_object_is_immutable (buffer)))
995 buffer->props.language = language;
999 * hb_buffer_get_language:
1000 * @buffer: An #hb_buffer_t
1002 * See hb_buffer_set_language().
1004 * Return value: (transfer none):
1005 * The #hb_language_t of the buffer. Must not be freed by the caller.
1010 hb_buffer_get_language (hb_buffer_t *buffer)
1012 return buffer->props.language;
1016 * hb_buffer_set_segment_properties:
1017 * @buffer: An #hb_buffer_t
1018 * @props: An #hb_segment_properties_t to use
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.
1027 hb_buffer_set_segment_properties (hb_buffer_t *buffer,
1028 const hb_segment_properties_t *props)
1030 if (unlikely (hb_object_is_immutable (buffer)))
1033 buffer->props = *props;
1037 * hb_buffer_get_segment_properties:
1038 * @buffer: An #hb_buffer_t
1039 * @props: (out): The output #hb_segment_properties_t
1041 * Sets @props to the #hb_segment_properties_t of @buffer.
1046 hb_buffer_get_segment_properties (hb_buffer_t *buffer,
1047 hb_segment_properties_t *props)
1049 *props = buffer->props;
1054 * hb_buffer_set_flags:
1055 * @buffer: An #hb_buffer_t
1056 * @flags: The buffer flags to set
1058 * Sets @buffer flags to @flags. See #hb_buffer_flags_t.
1063 hb_buffer_set_flags (hb_buffer_t *buffer,
1064 hb_buffer_flags_t flags)
1066 if (unlikely (hb_object_is_immutable (buffer)))
1069 buffer->flags = flags;
1073 * hb_buffer_get_flags:
1074 * @buffer: An #hb_buffer_t
1076 * Fetches the #hb_buffer_flags_t of @buffer.
1084 hb_buffer_get_flags (hb_buffer_t *buffer)
1086 return buffer->flags;
1090 * hb_buffer_set_cluster_level:
1091 * @buffer: An #hb_buffer_t
1092 * @cluster_level: The cluster level to set on the buffer
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
1101 hb_buffer_set_cluster_level (hb_buffer_t *buffer,
1102 hb_buffer_cluster_level_t cluster_level)
1104 if (unlikely (hb_object_is_immutable (buffer)))
1107 buffer->cluster_level = cluster_level;
1111 * hb_buffer_get_cluster_level:
1112 * @buffer: An #hb_buffer_t
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
1118 * Return value: The cluster level of @buffer
1122 hb_buffer_cluster_level_t
1123 hb_buffer_get_cluster_level (hb_buffer_t *buffer)
1125 return buffer->cluster_level;
1130 * hb_buffer_set_replacement_codepoint:
1131 * @buffer: An #hb_buffer_t
1132 * @replacement: the replacement #hb_codepoint_t
1134 * Sets the #hb_codepoint_t that replaces invalid entries for a given encoding
1135 * when adding text to @buffer.
1137 * Default is #HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT.
1142 hb_buffer_set_replacement_codepoint (hb_buffer_t *buffer,
1143 hb_codepoint_t replacement)
1145 if (unlikely (hb_object_is_immutable (buffer)))
1148 buffer->replacement = replacement;
1152 * hb_buffer_get_replacement_codepoint:
1153 * @buffer: An #hb_buffer_t
1155 * Fetches the #hb_codepoint_t that replaces invalid entries for a given encoding
1156 * when adding text to @buffer.
1159 * The @buffer replacement #hb_codepoint_t
1164 hb_buffer_get_replacement_codepoint (hb_buffer_t *buffer)
1166 return buffer->replacement;
1171 * hb_buffer_set_invisible_glyph:
1172 * @buffer: An #hb_buffer_t
1173 * @invisible: the invisible #hb_codepoint_t
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
1183 hb_buffer_set_invisible_glyph (hb_buffer_t *buffer,
1184 hb_codepoint_t invisible)
1186 if (unlikely (hb_object_is_immutable (buffer)))
1189 buffer->invisible = invisible;
1193 * hb_buffer_get_invisible_glyph:
1194 * @buffer: An #hb_buffer_t
1196 * See hb_buffer_set_invisible_glyph().
1199 * The @buffer invisible #hb_codepoint_t
1204 hb_buffer_get_invisible_glyph (hb_buffer_t *buffer)
1206 return buffer->invisible;
1210 * hb_buffer_set_not_found_glyph:
1211 * @buffer: An #hb_buffer_t
1212 * @not_found: the not-found #hb_codepoint_t
1214 * Sets the #hb_codepoint_t that replaces characters not found in
1215 * the font during shaping.
1217 * The not-found glyph defaults to zero, sometimes knows as the
1218 * ".notdef" glyph. This API allows for differentiating the two.
1223 hb_buffer_set_not_found_glyph (hb_buffer_t *buffer,
1224 hb_codepoint_t not_found)
1226 if (unlikely (hb_object_is_immutable (buffer)))
1229 buffer->not_found = not_found;
1233 * hb_buffer_get_not_found_glyph:
1234 * @buffer: An #hb_buffer_t
1236 * See hb_buffer_set_not_found_glyph().
1239 * The @buffer not-found #hb_codepoint_t
1244 hb_buffer_get_not_found_glyph (hb_buffer_t *buffer)
1246 return buffer->not_found;
1251 * hb_buffer_clear_contents:
1252 * @buffer: An #hb_buffer_t
1254 * Similar to hb_buffer_reset(), but does not clear the Unicode functions and
1255 * the replacement code point.
1260 hb_buffer_clear_contents (hb_buffer_t *buffer)
1262 if (unlikely (hb_object_is_immutable (buffer)))
1269 * hb_buffer_pre_allocate:
1270 * @buffer: An #hb_buffer_t
1271 * @size: Number of items to pre allocate.
1273 * Pre allocates memory for @buffer to fit at least @size number of items.
1276 * %true if @buffer memory allocation succeeded, %false otherwise
1281 hb_buffer_pre_allocate (hb_buffer_t *buffer, unsigned int size)
1283 return buffer->ensure (size);
1287 * hb_buffer_allocation_successful:
1288 * @buffer: An #hb_buffer_t
1290 * Check if allocating memory for the buffer succeeded.
1293 * %true if @buffer memory allocation succeeded, %false otherwise.
1298 hb_buffer_allocation_successful (hb_buffer_t *buffer)
1300 return buffer->successful;
1305 * @buffer: An #hb_buffer_t
1306 * @codepoint: A Unicode code point.
1307 * @cluster: The cluster value of @codepoint.
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.
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.
1321 hb_buffer_add (hb_buffer_t *buffer,
1322 hb_codepoint_t codepoint,
1323 unsigned int cluster)
1325 buffer->add (codepoint, cluster);
1326 buffer->clear_context (1);
1330 * hb_buffer_set_length:
1331 * @buffer: An #hb_buffer_t
1332 * @length: The new length of @buffer
1334 * Similar to hb_buffer_pre_allocate(), but clears any new items added at the
1338 * %true if @buffer memory allocation succeeded, %false otherwise.
1343 hb_buffer_set_length (hb_buffer_t *buffer,
1344 unsigned int length)
1346 if (unlikely (hb_object_is_immutable (buffer)))
1349 if (unlikely (!buffer->ensure (length)))
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));
1359 buffer->len = length;
1363 buffer->content_type = HB_BUFFER_CONTENT_TYPE_INVALID;
1364 buffer->clear_context (0);
1366 buffer->clear_context (1);
1372 * hb_buffer_get_length:
1373 * @buffer: An #hb_buffer_t
1375 * Returns the number of items in the buffer.
1378 * The @buffer length.
1379 * The value valid as long as buffer has not been modified.
1384 hb_buffer_get_length (hb_buffer_t *buffer)
1390 * hb_buffer_get_glyph_infos:
1391 * @buffer: An #hb_buffer_t
1392 * @length: (out): The output-array length.
1394 * Returns @buffer glyph information array. Returned pointer
1395 * is valid as long as @buffer contents are not modified.
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.
1404 hb_buffer_get_glyph_infos (hb_buffer_t *buffer,
1405 unsigned int *length)
1408 *length = buffer->len;
1410 return (hb_glyph_info_t *) buffer->info;
1414 * hb_buffer_get_glyph_positions:
1415 * @buffer: An #hb_buffer_t
1416 * @length: (out): The output length
1418 * Returns @buffer glyph position array. Returned pointer
1419 * is valid as long as @buffer contents are not modified.
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.
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.
1432 hb_glyph_position_t *
1433 hb_buffer_get_glyph_positions (hb_buffer_t *buffer,
1434 unsigned int *length)
1437 *length = buffer->len;
1439 if (!buffer->have_positions)
1441 if (unlikely (buffer->message_depth))
1444 buffer->clear_positions ();
1447 return (hb_glyph_position_t *) buffer->pos;
1451 * hb_buffer_has_positions:
1452 * @buffer: an #hb_buffer_t.
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.
1459 * %true if the @buffer has position array, %false otherwise.
1464 hb_buffer_has_positions (hb_buffer_t *buffer)
1466 return buffer->have_positions;
1470 * hb_glyph_info_get_glyph_flags:
1471 * @info: a #hb_glyph_info_t
1473 * Returns glyph flags encoded within a #hb_glyph_info_t.
1476 * The #hb_glyph_flags_t encoded within @info
1481 (hb_glyph_info_get_glyph_flags) (const hb_glyph_info_t *info)
1483 return hb_glyph_info_get_glyph_flags (info);
1487 * hb_buffer_reverse:
1488 * @buffer: An #hb_buffer_t
1490 * Reverses buffer contents.
1495 hb_buffer_reverse (hb_buffer_t *buffer)
1501 * hb_buffer_reverse_range:
1502 * @buffer: An #hb_buffer_t
1503 * @start: start index
1506 * Reverses buffer contents between @start and @end.
1511 hb_buffer_reverse_range (hb_buffer_t *buffer,
1512 unsigned int start, unsigned int end)
1514 buffer->reverse_range (start, end);
1518 * hb_buffer_reverse_clusters:
1519 * @buffer: An #hb_buffer_t
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.
1528 hb_buffer_reverse_clusters (hb_buffer_t *buffer)
1530 buffer->reverse_clusters ();
1534 * hb_buffer_guess_segment_properties:
1535 * @buffer: An #hb_buffer_t
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.
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.
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.
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.
1562 hb_buffer_guess_segment_properties (hb_buffer_t *buffer)
1564 buffer->guess_segment_properties ();
1567 template <typename utf_t>
1569 hb_buffer_add_utf (hb_buffer_t *buffer,
1570 const typename utf_t::codepoint_t *text,
1572 unsigned int item_offset,
1575 typedef typename utf_t::codepoint_t T;
1576 const hb_codepoint_t replacement = buffer->replacement;
1578 buffer->assert_unicode ();
1580 if (unlikely (hb_object_is_immutable (buffer)))
1583 if (text_length == -1)
1584 text_length = utf_t::strlen (text);
1586 if (item_length == -1)
1587 item_length = text_length - item_offset;
1589 if (unlikely (item_length < 0 ||
1590 item_length > INT_MAX / 8 ||
1591 !buffer->ensure (buffer->len + item_length * sizeof (T) / 4)))
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:
1599 * https://bugzilla.mozilla.org/show_bug.cgi?id=801410#c13
1601 if (!buffer->len && item_offset > 0)
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)
1610 prev = utf_t::prev (prev, start, &u, replacement);
1611 buffer->context[0][buffer->context_len[0]++] = u;
1615 const T *next = text + item_offset;
1616 const T *end = next + item_length;
1620 const T *old_next = next;
1621 next = utf_t::next (next, end, &u, replacement);
1622 buffer->add (u, old_next - (const T *) text);
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)
1631 next = utf_t::next (next, end, &u, replacement);
1632 buffer->context[1][buffer->context_len[1]++] = u;
1635 buffer->content_type = HB_BUFFER_CONTENT_TYPE_UNICODE;
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).
1648 * See hb_buffer_add_codepoints().
1650 * Replaces invalid UTF-8 characters with the @buffer replacement code point,
1651 * see hb_buffer_set_replacement_codepoint().
1656 hb_buffer_add_utf8 (hb_buffer_t *buffer,
1659 unsigned int item_offset,
1662 hb_buffer_add_utf<hb_utf8_t> (buffer, (const uint8_t *) text, text_length, item_offset, item_length);
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)
1674 * See hb_buffer_add_codepoints().
1676 * Replaces invalid UTF-16 characters with the @buffer replacement code point,
1677 * see hb_buffer_set_replacement_codepoint().
1682 hb_buffer_add_utf16 (hb_buffer_t *buffer,
1683 const uint16_t *text,
1685 unsigned int item_offset,
1688 hb_buffer_add_utf<hb_utf16_t> (buffer, text, text_length, item_offset, item_length);
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)
1700 * See hb_buffer_add_codepoints().
1702 * Replaces invalid UTF-32 characters with the @buffer replacement code point,
1703 * see hb_buffer_set_replacement_codepoint().
1708 hb_buffer_add_utf32 (hb_buffer_t *buffer,
1709 const uint32_t *text,
1711 unsigned int item_offset,
1714 hb_buffer_add_utf<hb_utf32_t> (buffer, text, text_length, item_offset, item_length);
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)
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.
1730 * <note>Has nothing to do with non-Unicode Latin-1 encoding.</note>
1735 hb_buffer_add_latin1 (hb_buffer_t *buffer,
1736 const uint8_t *text,
1738 unsigned int item_offset,
1741 hb_buffer_add_utf<hb_latin1_t> (buffer, text, text_length, item_offset, item_length);
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).
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.
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.
1769 hb_buffer_add_codepoints (hb_buffer_t *buffer,
1770 const hb_codepoint_t *text,
1772 unsigned int item_offset,
1775 hb_buffer_add_utf<hb_utf32_novalidate_t> (buffer, text, text_length, item_offset, item_length);
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.
1786 * Append (part of) contents of another buffer to this buffer.
1791 hb_buffer_append (hb_buffer_t *buffer,
1792 const hb_buffer_t *source,
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);
1802 if (end > source->len)
1809 if (buffer->len + (end - start) < buffer->len) /* Overflows. */
1811 buffer->successful = false;
1815 unsigned int orig_len = buffer->len;
1816 hb_buffer_set_length (buffer, buffer->len + (end - start));
1817 if (unlikely (!buffer->successful))
1821 buffer->content_type = source->content_type;
1822 if (!buffer->have_positions && source->have_positions)
1823 buffer->clear_positions ();
1825 hb_segment_properties_overlay (&buffer->props, &source->props);
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]));
1831 if (source->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE)
1833 /* See similar logic in add_utf. */
1836 if (!orig_len && start + source->context_len[0] > 0)
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];
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];
1856 compare_info_codepoint (const hb_glyph_info_t *pa,
1857 const hb_glyph_info_t *pb)
1859 return (int) pb->codepoint - (int) pa->codepoint;
1863 normalize_glyphs_cluster (hb_buffer_t *buffer,
1868 hb_glyph_position_t *pos = buffer->pos;
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++)
1874 total_x_advance += pos[i].x_advance;
1875 total_y_advance += pos[i].y_advance;
1878 hb_position_t x_advance = 0, y_advance = 0;
1879 for (unsigned int i = start; i < end; i++)
1881 pos[i].x_offset += x_advance;
1882 pos[i].y_offset += y_advance;
1884 x_advance += pos[i].x_advance;
1885 y_advance += pos[i].y_advance;
1887 pos[i].x_advance = 0;
1888 pos[i].y_advance = 0;
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;
1897 hb_stable_sort (buffer->info + start, end - start - 1, compare_info_codepoint, buffer->pos + start);
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;
1906 hb_stable_sort (buffer->info + start + 1, end - start - 1, compare_info_codepoint, buffer->pos + start + 1);
1911 * hb_buffer_normalize_glyphs:
1912 * @buffer: An #hb_buffer_t
1914 * Reorders a glyph buffer to have canonical in-cluster glyph order / position.
1915 * The resulting clusters should behave identical to pre-reordering clusters.
1917 * <note>This has nothing to do with Unicode normalization.</note>
1922 hb_buffer_normalize_glyphs (hb_buffer_t *buffer)
1924 assert (buffer->have_positions);
1926 buffer->assert_glyphs ();
1928 bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
1930 foreach_cluster (buffer, start, end)
1931 normalize_glyphs_cluster (buffer, start, end, backward);
1935 hb_buffer_t::sort (unsigned int start, unsigned int end, int(*compar)(const hb_glyph_info_t *, const hb_glyph_info_t *))
1937 assert (!have_positions);
1938 for (unsigned int i = start + 1; i < end; i++)
1941 while (j > start && compar (&info[j - 1], &info[i]) > 0)
1945 /* Move item i to occupy place for item j, shift what's in between. */
1946 merge_clusters (j, i + 1);
1948 hb_glyph_info_t t = info[i];
1949 memmove (&info[j + 1], &info[j], (i - j) * sizeof (hb_glyph_info_t));
1957 * Comparing buffers.
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.
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.
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)
1979 if (buffer->content_type != reference->content_type && buffer->len && reference->len)
1980 return HB_BUFFER_DIFF_FLAG_CONTENT_TYPE_MISMATCH;
1982 hb_buffer_diff_flags_t result = HB_BUFFER_DIFF_FLAG_EQUAL;
1983 bool contains = dottedcircle_glyph != (hb_codepoint_t) -1;
1985 unsigned int count = reference->len;
1987 if (buffer->len != count)
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
1993 const hb_glyph_info_t *info = reference->info;
1995 for (i = 0; i < count; i++)
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;
2002 result |= HB_BUFFER_DIFF_FLAG_LENGTH_MISMATCH;
2003 return hb_buffer_diff_flags_t (result);
2007 return hb_buffer_diff_flags_t (result);
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++)
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;
2027 if (buffer->content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS)
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++)
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)
2039 result |= HB_BUFFER_DIFF_FLAG_POSITION_MISMATCH;
2055 #ifndef HB_NO_BUFFER_MESSAGE
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
2063 * Sets the implementation function for #hb_buffer_message_func_t.
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)
2072 if (buffer->message_destroy)
2073 buffer->message_destroy (buffer->message_data);
2076 buffer->message_func = func;
2077 buffer->message_data = user_data;
2078 buffer->message_destroy = destroy;
2080 buffer->message_func = nullptr;
2081 buffer->message_data = nullptr;
2082 buffer->message_destroy = nullptr;
2086 hb_buffer_t::message_impl (hb_font_t *font, const char *fmt, va_list ap)
2089 vsnprintf (buf, sizeof (buf), fmt, ap);
2090 return (bool) this->message_func (this, font, buf, this->message_data);