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.
44 * The input buffer is a sequence of Unicode codepoints, with
45 * associated attributes such as direction and script. The output
46 * buffer is a sequence of glyphs, with associated attributes such
47 * as position and cluster.
52 * hb_segment_properties_equal:
53 * @a: first #hb_segment_properties_t to compare.
54 * @b: second #hb_segment_properties_t to compare.
56 * Checks the equality of two #hb_segment_properties_t's.
59 * `true` if all properties of @a equal those of @b, `false` otherwise.
64 hb_segment_properties_equal (const hb_segment_properties_t *a,
65 const hb_segment_properties_t *b)
67 return a->direction == b->direction &&
68 a->script == b->script &&
69 a->language == b->language &&
70 a->reserved1 == b->reserved1 &&
71 a->reserved2 == b->reserved2;
76 * hb_segment_properties_hash:
77 * @p: #hb_segment_properties_t to hash.
79 * Creates a hash representing @p.
87 hb_segment_properties_hash (const hb_segment_properties_t *p)
89 return ((unsigned int) p->direction * 31 +
90 (unsigned int) p->script) * 31 +
91 (intptr_t) (p->language);
95 * hb_segment_properties_overlay:
96 * @p: #hb_segment_properties_t to fill in.
97 * @src: #hb_segment_properties_t to fill in from.
99 * Fills in missing fields of @p from @src in a considered manner.
101 * First, if @p does not have direction set, direction is copied from @src.
103 * Next, if @p and @src have the same direction (which can be unset), if @p
104 * does not have script set, script is copied from @src.
106 * Finally, if @p and @src have the same direction and script (which either
107 * can be unset), if @p does not have language set, language is copied from
113 hb_segment_properties_overlay (hb_segment_properties_t *p,
114 const hb_segment_properties_t *src)
116 if (unlikely (!p || !src))
120 p->direction = src->direction;
122 if (p->direction != src->direction)
126 p->script = src->script;
128 if (p->script != src->script)
132 p->language = src->language;
135 /* Here is how the buffer works internally:
137 * There are two info pointers: info and out_info. They always have
138 * the same allocated size, but different lengths.
140 * As an optimization, both info and out_info may point to the
141 * same piece of memory, which is owned by info. This remains the
142 * case as long as out_len doesn't exceed i at any time.
143 * In that case, sync() is mostly no-op and the glyph operations
144 * operate mostly in-place.
146 * As soon as out_info gets longer than info, out_info is moved over
147 * to an alternate buffer (which we reuse the pos buffer for), and its
148 * current contents (out_len entries) are copied to the new place.
150 * This should all remain transparent to the user. sync() then
151 * switches info over to out_info and does housekeeping.
159 hb_buffer_t::enlarge (unsigned int size)
161 if (unlikely (!successful))
163 if (unlikely (size > max_len))
169 unsigned int new_allocated = allocated;
170 hb_glyph_position_t *new_pos = nullptr;
171 hb_glyph_info_t *new_info = nullptr;
172 bool separate_out = out_info != info;
174 if (unlikely (hb_unsigned_mul_overflows (size, sizeof (info[0]))))
177 while (size >= new_allocated)
178 new_allocated += (new_allocated >> 1) + 32;
181 if (unlikely (hb_unsigned_mul_overflows (new_allocated, sizeof (info[0]), &new_bytes)))
184 static_assert (sizeof (info[0]) == sizeof (pos[0]), "");
185 new_pos = (hb_glyph_position_t *) hb_realloc (pos, new_bytes);
186 new_info = (hb_glyph_info_t *) hb_realloc (info, new_bytes);
189 if (unlikely (!new_pos || !new_info))
192 if (likely (new_pos))
195 if (likely (new_info))
198 out_info = separate_out ? (hb_glyph_info_t *) pos : info;
199 if (likely (successful))
200 allocated = new_allocated;
202 return likely (successful);
206 hb_buffer_t::make_room_for (unsigned int num_in,
207 unsigned int num_out)
209 if (unlikely (!ensure (out_len + num_out))) return false;
211 if (out_info == info &&
212 out_len + num_out > idx + num_in)
214 assert (have_output);
216 out_info = (hb_glyph_info_t *) pos;
217 hb_memcpy (out_info, info, out_len * sizeof (out_info[0]));
224 hb_buffer_t::shift_forward (unsigned int count)
226 assert (have_output);
227 if (unlikely (!ensure (len + count))) return false;
229 memmove (info + idx + count, info + idx, (len - idx) * sizeof (info[0]));
230 if (idx + count > len)
232 /* Under memory failure we might expose this area. At least
233 * clean it up. Oh well...
235 * Ideally, we should at least set Default_Ignorable bits on
236 * these, as well as consistent cluster values. But the former
237 * is layering violation... */
238 hb_memset (info + len, 0, (idx + count - len) * sizeof (info[0]));
246 hb_buffer_t::scratch_buffer_t *
247 hb_buffer_t::get_scratch_buffer (unsigned int *size)
250 have_positions = false;
255 assert ((uintptr_t) pos % sizeof (scratch_buffer_t) == 0);
256 *size = allocated * sizeof (pos[0]) / sizeof (scratch_buffer_t);
257 return (scratch_buffer_t *) (void *) pos;
262 /* HarfBuzz-Internal API */
265 hb_buffer_t::similar (const hb_buffer_t &src)
267 hb_unicode_funcs_destroy (unicode);
268 unicode = hb_unicode_funcs_reference (src.unicode);
270 cluster_level = src.cluster_level;
271 replacement = src.replacement;
272 invisible = src.invisible;
273 not_found = src.not_found;
277 hb_buffer_t::reset ()
279 hb_unicode_funcs_destroy (unicode);
280 unicode = hb_unicode_funcs_reference (hb_unicode_funcs_get_default ());
281 flags = HB_BUFFER_FLAG_DEFAULT;
282 cluster_level = HB_BUFFER_CLUSTER_LEVEL_DEFAULT;
283 replacement = HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT;
291 hb_buffer_t::clear ()
293 content_type = HB_BUFFER_CONTENT_TYPE_INVALID;
294 hb_segment_properties_t default_props = HB_SEGMENT_PROPERTIES_DEFAULT;
295 props = default_props;
298 shaping_failed = false;
300 have_positions = false;
307 hb_memset (context, 0, sizeof context);
308 hb_memset (context_len, 0, sizeof context_len);
310 deallocate_var_all ();
312 scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;
316 hb_buffer_t::enter ()
318 deallocate_var_all ();
320 shaping_failed = false;
321 scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;
323 if (likely (!hb_unsigned_mul_overflows (len, HB_BUFFER_MAX_LEN_FACTOR, &mul)))
325 max_len = hb_max (mul, (unsigned) HB_BUFFER_MAX_LEN_MIN);
327 if (likely (!hb_unsigned_mul_overflows (len, HB_BUFFER_MAX_OPS_FACTOR, &mul)))
329 max_ops = hb_max (mul, (unsigned) HB_BUFFER_MAX_OPS_MIN);
333 hb_buffer_t::leave ()
335 max_len = HB_BUFFER_MAX_LEN_DEFAULT;
336 max_ops = HB_BUFFER_MAX_OPS_DEFAULT;
337 deallocate_var_all ();
339 // Intentionally not reseting shaping_failed, such that it can be inspected.
344 hb_buffer_t::add (hb_codepoint_t codepoint,
345 unsigned int cluster)
347 hb_glyph_info_t *glyph;
349 if (unlikely (!ensure (len + 1))) return;
353 hb_memset (glyph, 0, sizeof (*glyph));
354 glyph->codepoint = codepoint;
356 glyph->cluster = cluster;
362 hb_buffer_t::add_info (const hb_glyph_info_t &glyph_info)
364 if (unlikely (!ensure (len + 1))) return;
366 info[len] = glyph_info;
373 hb_buffer_t::clear_output ()
376 have_positions = false;
384 hb_buffer_t::clear_positions ()
387 have_positions = true;
392 hb_memset (pos, 0, sizeof (pos[0]) * len);
400 assert (have_output);
404 if (unlikely (!successful || !next_glyphs (len - idx)))
407 if (out_info != info)
409 pos = (hb_glyph_position_t *) info;
425 hb_buffer_t::sync_so_far ()
427 bool had_output = have_output;
428 unsigned out_i = out_len;
430 unsigned old_idx = idx;
445 return idx - old_idx;
449 hb_buffer_t::move_to (unsigned int i)
457 if (unlikely (!successful))
460 assert (i <= out_len + (len - idx));
464 unsigned int count = i - out_len;
465 if (unlikely (!make_room_for (count, count))) return false;
467 memmove (out_info + out_len, info + idx, count * sizeof (out_info[0]));
471 else if (out_len > i)
473 /* Tricky part: rewinding... */
474 unsigned int count = out_len - i;
476 /* This will blow in our face if memory allocation fails later
477 * in this same lookup...
479 * We used to shift with extra 32 items.
480 * But that would leave empty slots in the buffer in case of allocation
481 * failures. See comments in shift_forward(). This can cause O(N^2)
482 * behavior more severely than adding 32 empty slots can... */
483 if (unlikely (idx < count && !shift_forward (count - idx))) return false;
485 assert (idx >= count);
489 memmove (info + idx, out_info + out_len, count * sizeof (out_info[0]));
497 hb_buffer_t::set_masks (hb_mask_t value,
499 unsigned int cluster_start,
500 unsigned int cluster_end)
505 hb_mask_t not_mask = ~mask;
508 unsigned int count = len;
509 for (unsigned int i = 0; i < count; i++)
510 if (cluster_start <= info[i].cluster && info[i].cluster < cluster_end)
511 info[i].mask = (info[i].mask & not_mask) | value;
515 hb_buffer_t::merge_clusters_impl (unsigned int start,
518 if (cluster_level == HB_BUFFER_CLUSTER_LEVEL_CHARACTERS)
520 unsafe_to_break (start, end);
524 unsigned int cluster = info[start].cluster;
526 for (unsigned int i = start + 1; i < end; i++)
527 cluster = hb_min (cluster, info[i].cluster);
530 if (cluster != info[end - 1].cluster)
531 while (end < len && info[end - 1].cluster == info[end].cluster)
535 if (cluster != info[start].cluster)
536 while (idx < start && info[start - 1].cluster == info[start].cluster)
539 /* If we hit the start of buffer, continue in out-buffer. */
540 if (idx == start && info[start].cluster != cluster)
541 for (unsigned int i = out_len; i && out_info[i - 1].cluster == info[start].cluster; i--)
542 set_cluster (out_info[i - 1], cluster);
544 for (unsigned int i = start; i < end; i++)
545 set_cluster (info[i], cluster);
548 hb_buffer_t::merge_out_clusters (unsigned int start,
551 if (cluster_level == HB_BUFFER_CLUSTER_LEVEL_CHARACTERS)
554 if (unlikely (end - start < 2))
557 unsigned int cluster = out_info[start].cluster;
559 for (unsigned int i = start + 1; i < end; i++)
560 cluster = hb_min (cluster, out_info[i].cluster);
563 while (start && out_info[start - 1].cluster == out_info[start].cluster)
567 while (end < out_len && out_info[end - 1].cluster == out_info[end].cluster)
570 /* If we hit the end of out-buffer, continue in buffer. */
572 for (unsigned int i = idx; i < len && info[i].cluster == out_info[end - 1].cluster; i++)
573 set_cluster (info[i], cluster);
575 for (unsigned int i = start; i < end; i++)
576 set_cluster (out_info[i], cluster);
579 hb_buffer_t::delete_glyph ()
581 /* The logic here is duplicated in hb_ot_hide_default_ignorables(). */
583 unsigned int cluster = info[idx].cluster;
584 if ((idx + 1 < len && cluster == info[idx + 1].cluster) ||
585 (out_len && cluster == out_info[out_len - 1].cluster))
587 /* Cluster survives; do nothing. */
593 /* Merge cluster backward. */
594 if (cluster < out_info[out_len - 1].cluster)
596 unsigned int mask = info[idx].mask;
597 unsigned int old_cluster = out_info[out_len - 1].cluster;
598 for (unsigned i = out_len; i && out_info[i - 1].cluster == old_cluster; i--)
599 set_cluster (out_info[i - 1], cluster, mask);
606 /* Merge cluster forward. */
607 merge_clusters (idx, idx + 2);
616 hb_buffer_t::delete_glyphs_inplace (bool (*filter) (const hb_glyph_info_t *info))
618 /* Merge clusters and delete filtered glyphs.
619 * NOTE! We can't use out-buffer as we have positioning data. */
621 unsigned int count = len;
622 for (unsigned int i = 0; i < count; i++)
624 if (filter (&info[i]))
627 * Same logic as delete_glyph(), but for in-place removal. */
629 unsigned int cluster = info[i].cluster;
630 if (i + 1 < count && cluster == info[i + 1].cluster)
631 continue; /* Cluster survives; do nothing. */
635 /* Merge cluster backward. */
636 if (cluster < info[j - 1].cluster)
638 unsigned int mask = info[i].mask;
639 unsigned int old_cluster = info[j - 1].cluster;
640 for (unsigned k = j; k && info[k - 1].cluster == old_cluster; k--)
641 set_cluster (info[k - 1], cluster, mask);
647 merge_clusters (i, i + 2); /* Merge cluster forward. */
663 hb_buffer_t::guess_segment_properties ()
667 /* If script is set to INVALID, guess from buffer contents */
668 if (props.script == HB_SCRIPT_INVALID) {
669 for (unsigned int i = 0; i < len; i++) {
670 hb_script_t script = unicode->script (info[i].codepoint);
671 if (likely (script != HB_SCRIPT_COMMON &&
672 script != HB_SCRIPT_INHERITED &&
673 script != HB_SCRIPT_UNKNOWN)) {
674 props.script = script;
680 /* If direction is set to INVALID, guess from script */
681 if (props.direction == HB_DIRECTION_INVALID) {
682 props.direction = hb_script_get_horizontal_direction (props.script);
683 if (props.direction == HB_DIRECTION_INVALID)
684 props.direction = HB_DIRECTION_LTR;
687 /* If language is not set, use default language from locale */
688 if (props.language == HB_LANGUAGE_INVALID) {
689 /* TODO get_default_for_script? using $LANGUAGE */
690 props.language = hb_language_get_default ();
697 DEFINE_NULL_INSTANCE (hb_buffer_t) =
699 HB_OBJECT_HEADER_STATIC,
701 const_cast<hb_unicode_funcs_t *> (&_hb_Null_hb_unicode_funcs_t),
702 HB_BUFFER_FLAG_DEFAULT,
703 HB_BUFFER_CLUSTER_LEVEL_DEFAULT,
704 HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT,
709 HB_BUFFER_CONTENT_TYPE_INVALID,
710 HB_SEGMENT_PROPERTIES_DEFAULT,
712 false, /* successful */
713 true, /* shaping_failed */
714 false, /* have_output */
715 true /* have_positions */
717 /* Zero is good enough for everything else. */
724 * Creates a new #hb_buffer_t with all properties to defaults.
726 * Return value: (transfer full):
727 * A newly allocated #hb_buffer_t with a reference count of 1. The initial
728 * reference count should be released with hb_buffer_destroy() when you are done
729 * using the #hb_buffer_t. This function never returns `NULL`. If memory cannot
730 * be allocated, a special #hb_buffer_t object will be returned on which
731 * hb_buffer_allocation_successful() returns `false`.
740 if (!(buffer = hb_object_create<hb_buffer_t> ()))
741 return hb_buffer_get_empty ();
743 buffer->max_len = HB_BUFFER_MAX_LEN_DEFAULT;
744 buffer->max_ops = HB_BUFFER_MAX_OPS_DEFAULT;
752 * hb_buffer_create_similar:
753 * @src: An #hb_buffer_t
755 * Creates a new #hb_buffer_t, similar to hb_buffer_create(). The only
756 * difference is that the buffer is configured similarly to @src.
758 * Return value: (transfer full):
759 * A newly allocated #hb_buffer_t, similar to hb_buffer_create().
764 hb_buffer_create_similar (const hb_buffer_t *src)
766 hb_buffer_t *buffer = hb_buffer_create ();
768 buffer->similar (*src);
775 * @buffer: An #hb_buffer_t
777 * Resets the buffer to its initial status, as if it was just newly created
778 * with hb_buffer_create().
783 hb_buffer_reset (hb_buffer_t *buffer)
785 if (unlikely (hb_object_is_immutable (buffer)))
792 * hb_buffer_get_empty:
794 * Fetches an empty #hb_buffer_t.
796 * Return value: (transfer full): The empty buffer
801 hb_buffer_get_empty ()
803 return const_cast<hb_buffer_t *> (&Null (hb_buffer_t));
807 * hb_buffer_reference: (skip)
808 * @buffer: An #hb_buffer_t
810 * Increases the reference count on @buffer by one. This prevents @buffer from
811 * being destroyed until a matching call to hb_buffer_destroy() is made.
813 * Return value: (transfer full):
814 * The referenced #hb_buffer_t.
819 hb_buffer_reference (hb_buffer_t *buffer)
821 return hb_object_reference (buffer);
825 * hb_buffer_destroy: (skip)
826 * @buffer: An #hb_buffer_t
828 * Deallocate the @buffer.
829 * Decreases the reference count on @buffer by one. If the result is zero, then
830 * @buffer and all associated resources are freed. See hb_buffer_reference().
835 hb_buffer_destroy (hb_buffer_t *buffer)
837 if (!hb_object_destroy (buffer)) return;
839 hb_unicode_funcs_destroy (buffer->unicode);
841 hb_free (buffer->info);
842 hb_free (buffer->pos);
843 #ifndef HB_NO_BUFFER_MESSAGE
844 if (buffer->message_destroy)
845 buffer->message_destroy (buffer->message_data);
852 * hb_buffer_set_user_data: (skip)
853 * @buffer: An #hb_buffer_t
854 * @key: The user-data key
855 * @data: A pointer to the user data
856 * @destroy: (nullable): A callback to call when @data is not needed anymore
857 * @replace: Whether to replace an existing data with the same key
859 * Attaches a user-data key/data pair to the specified buffer.
861 * Return value: `true` if success, `false` otherwise
866 hb_buffer_set_user_data (hb_buffer_t *buffer,
867 hb_user_data_key_t *key,
869 hb_destroy_func_t destroy,
872 return hb_object_set_user_data (buffer, key, data, destroy, replace);
876 * hb_buffer_get_user_data: (skip)
877 * @buffer: An #hb_buffer_t
878 * @key: The user-data key to query
880 * Fetches the user data associated with the specified key,
881 * attached to the specified buffer.
883 * Return value: (transfer none): A pointer to the user data
888 hb_buffer_get_user_data (const hb_buffer_t *buffer,
889 hb_user_data_key_t *key)
891 return hb_object_get_user_data (buffer, key);
896 * hb_buffer_set_content_type:
897 * @buffer: An #hb_buffer_t
898 * @content_type: The type of buffer contents to set
900 * Sets the type of @buffer contents. Buffers are either empty, contain
901 * characters (before shaping), or contain glyphs (the result of shaping).
903 * You rarely need to call this function, since a number of other
904 * functions transition the content type for you. Namely:
906 * - A newly created buffer starts with content type
907 * %HB_BUFFER_CONTENT_TYPE_INVALID. Calling hb_buffer_reset(),
908 * hb_buffer_clear_contents(), as well as calling hb_buffer_set_length()
909 * with an argument of zero all set the buffer content type to invalid
912 * - Calling hb_buffer_add_utf8(), hb_buffer_add_utf16(),
913 * hb_buffer_add_utf32(), hb_buffer_add_codepoints() and
914 * hb_buffer_add_latin1() expect that buffer is either empty and
915 * have a content type of invalid, or that buffer content type is
916 * %HB_BUFFER_CONTENT_TYPE_UNICODE, and they also set the content
917 * type to Unicode if they added anything to an empty buffer.
919 * - Finally hb_shape() and hb_shape_full() expect that the buffer
920 * is either empty and have content type of invalid, or that buffer
921 * content type is %HB_BUFFER_CONTENT_TYPE_UNICODE, and upon
922 * success they set the buffer content type to
923 * %HB_BUFFER_CONTENT_TYPE_GLYPHS.
925 * The above transitions are designed such that one can use a buffer
926 * in a loop of "reset : add-text : shape" without needing to ever
927 * modify the content type manually.
932 hb_buffer_set_content_type (hb_buffer_t *buffer,
933 hb_buffer_content_type_t content_type)
935 buffer->content_type = content_type;
939 * hb_buffer_get_content_type:
940 * @buffer: An #hb_buffer_t
942 * Fetches the type of @buffer contents. Buffers are either empty, contain
943 * characters (before shaping), or contain glyphs (the result of shaping).
946 * The type of @buffer contents
950 hb_buffer_content_type_t
951 hb_buffer_get_content_type (const hb_buffer_t *buffer)
953 return buffer->content_type;
958 * hb_buffer_set_unicode_funcs:
959 * @buffer: An #hb_buffer_t
960 * @unicode_funcs: The Unicode-functions structure
962 * Sets the Unicode-functions structure of a buffer to
968 hb_buffer_set_unicode_funcs (hb_buffer_t *buffer,
969 hb_unicode_funcs_t *unicode_funcs)
971 if (unlikely (hb_object_is_immutable (buffer)))
975 unicode_funcs = hb_unicode_funcs_get_default ();
977 hb_unicode_funcs_reference (unicode_funcs);
978 hb_unicode_funcs_destroy (buffer->unicode);
979 buffer->unicode = unicode_funcs;
983 * hb_buffer_get_unicode_funcs:
984 * @buffer: An #hb_buffer_t
986 * Fetches the Unicode-functions structure of a buffer.
988 * Return value: The Unicode-functions structure
993 hb_buffer_get_unicode_funcs (const hb_buffer_t *buffer)
995 return buffer->unicode;
999 * hb_buffer_set_direction:
1000 * @buffer: An #hb_buffer_t
1001 * @direction: the #hb_direction_t of the @buffer
1003 * Set the text flow direction of the buffer. No shaping can happen without
1004 * setting @buffer direction, and it controls the visual direction for the
1005 * output glyphs; for RTL direction the glyphs will be reversed. Many layout
1006 * features depend on the proper setting of the direction, for example,
1007 * reversing RTL text before shaping, then shaping with LTR direction is not
1008 * the same as keeping the text in logical order and shaping with RTL
1014 hb_buffer_set_direction (hb_buffer_t *buffer,
1015 hb_direction_t direction)
1017 if (unlikely (hb_object_is_immutable (buffer)))
1020 buffer->props.direction = direction;
1024 * hb_buffer_get_direction:
1025 * @buffer: An #hb_buffer_t
1027 * See hb_buffer_set_direction()
1030 * The direction of the @buffer.
1035 hb_buffer_get_direction (const hb_buffer_t *buffer)
1037 return buffer->props.direction;
1041 * hb_buffer_set_script:
1042 * @buffer: An #hb_buffer_t
1043 * @script: An #hb_script_t to set.
1045 * Sets the script of @buffer to @script.
1047 * Script is crucial for choosing the proper shaping behaviour for scripts that
1048 * require it (e.g. Arabic) and the which OpenType features defined in the font
1051 * You can pass one of the predefined #hb_script_t values, or use
1052 * hb_script_from_string() or hb_script_from_iso15924_tag() to get the
1053 * corresponding script from an ISO 15924 script tag.
1058 hb_buffer_set_script (hb_buffer_t *buffer,
1061 if (unlikely (hb_object_is_immutable (buffer)))
1064 buffer->props.script = script;
1068 * hb_buffer_get_script:
1069 * @buffer: An #hb_buffer_t
1071 * Fetches the script of @buffer.
1074 * The #hb_script_t of the @buffer
1079 hb_buffer_get_script (const hb_buffer_t *buffer)
1081 return buffer->props.script;
1085 * hb_buffer_set_language:
1086 * @buffer: An #hb_buffer_t
1087 * @language: An hb_language_t to set
1089 * Sets the language of @buffer to @language.
1091 * Languages are crucial for selecting which OpenType feature to apply to the
1092 * buffer which can result in applying language-specific behaviour. Languages
1093 * are orthogonal to the scripts, and though they are related, they are
1094 * different concepts and should not be confused with each other.
1096 * Use hb_language_from_string() to convert from BCP 47 language tags to
1102 hb_buffer_set_language (hb_buffer_t *buffer,
1103 hb_language_t language)
1105 if (unlikely (hb_object_is_immutable (buffer)))
1108 buffer->props.language = language;
1112 * hb_buffer_get_language:
1113 * @buffer: An #hb_buffer_t
1115 * See hb_buffer_set_language().
1117 * Return value: (transfer none):
1118 * The #hb_language_t of the buffer. Must not be freed by the caller.
1123 hb_buffer_get_language (const hb_buffer_t *buffer)
1125 return buffer->props.language;
1129 * hb_buffer_set_segment_properties:
1130 * @buffer: An #hb_buffer_t
1131 * @props: An #hb_segment_properties_t to use
1133 * Sets the segment properties of the buffer, a shortcut for calling
1134 * hb_buffer_set_direction(), hb_buffer_set_script() and
1135 * hb_buffer_set_language() individually.
1140 hb_buffer_set_segment_properties (hb_buffer_t *buffer,
1141 const hb_segment_properties_t *props)
1143 if (unlikely (hb_object_is_immutable (buffer)))
1146 buffer->props = *props;
1150 * hb_buffer_get_segment_properties:
1151 * @buffer: An #hb_buffer_t
1152 * @props: (out): The output #hb_segment_properties_t
1154 * Sets @props to the #hb_segment_properties_t of @buffer.
1159 hb_buffer_get_segment_properties (const hb_buffer_t *buffer,
1160 hb_segment_properties_t *props)
1162 *props = buffer->props;
1167 * hb_buffer_set_flags:
1168 * @buffer: An #hb_buffer_t
1169 * @flags: The buffer flags to set
1171 * Sets @buffer flags to @flags. See #hb_buffer_flags_t.
1176 hb_buffer_set_flags (hb_buffer_t *buffer,
1177 hb_buffer_flags_t flags)
1179 if (unlikely (hb_object_is_immutable (buffer)))
1182 buffer->flags = flags;
1186 * hb_buffer_get_flags:
1187 * @buffer: An #hb_buffer_t
1189 * Fetches the #hb_buffer_flags_t of @buffer.
1197 hb_buffer_get_flags (const hb_buffer_t *buffer)
1199 return buffer->flags;
1203 * hb_buffer_set_cluster_level:
1204 * @buffer: An #hb_buffer_t
1205 * @cluster_level: The cluster level to set on the buffer
1207 * Sets the cluster level of a buffer. The #hb_buffer_cluster_level_t
1208 * dictates one aspect of how HarfBuzz will treat non-base characters
1214 hb_buffer_set_cluster_level (hb_buffer_t *buffer,
1215 hb_buffer_cluster_level_t cluster_level)
1217 if (unlikely (hb_object_is_immutable (buffer)))
1220 buffer->cluster_level = cluster_level;
1224 * hb_buffer_get_cluster_level:
1225 * @buffer: An #hb_buffer_t
1227 * Fetches the cluster level of a buffer. The #hb_buffer_cluster_level_t
1228 * dictates one aspect of how HarfBuzz will treat non-base characters
1231 * Return value: The cluster level of @buffer
1235 hb_buffer_cluster_level_t
1236 hb_buffer_get_cluster_level (const hb_buffer_t *buffer)
1238 return buffer->cluster_level;
1243 * hb_buffer_set_replacement_codepoint:
1244 * @buffer: An #hb_buffer_t
1245 * @replacement: the replacement #hb_codepoint_t
1247 * Sets the #hb_codepoint_t that replaces invalid entries for a given encoding
1248 * when adding text to @buffer.
1250 * Default is #HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT.
1255 hb_buffer_set_replacement_codepoint (hb_buffer_t *buffer,
1256 hb_codepoint_t replacement)
1258 if (unlikely (hb_object_is_immutable (buffer)))
1261 buffer->replacement = replacement;
1265 * hb_buffer_get_replacement_codepoint:
1266 * @buffer: An #hb_buffer_t
1268 * Fetches the #hb_codepoint_t that replaces invalid entries for a given encoding
1269 * when adding text to @buffer.
1272 * The @buffer replacement #hb_codepoint_t
1277 hb_buffer_get_replacement_codepoint (const hb_buffer_t *buffer)
1279 return buffer->replacement;
1284 * hb_buffer_set_invisible_glyph:
1285 * @buffer: An #hb_buffer_t
1286 * @invisible: the invisible #hb_codepoint_t
1288 * Sets the #hb_codepoint_t that replaces invisible characters in
1289 * the shaping result. If set to zero (default), the glyph for the
1290 * U+0020 SPACE character is used. Otherwise, this value is used
1296 hb_buffer_set_invisible_glyph (hb_buffer_t *buffer,
1297 hb_codepoint_t invisible)
1299 if (unlikely (hb_object_is_immutable (buffer)))
1302 buffer->invisible = invisible;
1306 * hb_buffer_get_invisible_glyph:
1307 * @buffer: An #hb_buffer_t
1309 * See hb_buffer_set_invisible_glyph().
1312 * The @buffer invisible #hb_codepoint_t
1317 hb_buffer_get_invisible_glyph (const hb_buffer_t *buffer)
1319 return buffer->invisible;
1323 * hb_buffer_set_not_found_glyph:
1324 * @buffer: An #hb_buffer_t
1325 * @not_found: the not-found #hb_codepoint_t
1327 * Sets the #hb_codepoint_t that replaces characters not found in
1328 * the font during shaping.
1330 * The not-found glyph defaults to zero, sometimes known as the
1331 * ".notdef" glyph. This API allows for differentiating the two.
1336 hb_buffer_set_not_found_glyph (hb_buffer_t *buffer,
1337 hb_codepoint_t not_found)
1339 if (unlikely (hb_object_is_immutable (buffer)))
1342 buffer->not_found = not_found;
1346 * hb_buffer_get_not_found_glyph:
1347 * @buffer: An #hb_buffer_t
1349 * See hb_buffer_set_not_found_glyph().
1352 * The @buffer not-found #hb_codepoint_t
1357 hb_buffer_get_not_found_glyph (const hb_buffer_t *buffer)
1359 return buffer->not_found;
1364 * hb_buffer_clear_contents:
1365 * @buffer: An #hb_buffer_t
1367 * Similar to hb_buffer_reset(), but does not clear the Unicode functions and
1368 * the replacement code point.
1373 hb_buffer_clear_contents (hb_buffer_t *buffer)
1375 if (unlikely (hb_object_is_immutable (buffer)))
1382 * hb_buffer_pre_allocate:
1383 * @buffer: An #hb_buffer_t
1384 * @size: Number of items to pre allocate.
1386 * Pre allocates memory for @buffer to fit at least @size number of items.
1389 * `true` if @buffer memory allocation succeeded, `false` otherwise
1394 hb_buffer_pre_allocate (hb_buffer_t *buffer, unsigned int size)
1396 return buffer->ensure (size);
1400 * hb_buffer_allocation_successful:
1401 * @buffer: An #hb_buffer_t
1403 * Check if allocating memory for the buffer succeeded.
1406 * `true` if @buffer memory allocation succeeded, `false` otherwise.
1411 hb_buffer_allocation_successful (hb_buffer_t *buffer)
1413 return buffer->successful;
1418 * @buffer: An #hb_buffer_t
1419 * @codepoint: A Unicode code point.
1420 * @cluster: The cluster value of @codepoint.
1422 * Appends a character with the Unicode value of @codepoint to @buffer, and
1423 * gives it the initial cluster value of @cluster. Clusters can be any thing
1424 * the client wants, they are usually used to refer to the index of the
1425 * character in the input text stream and are output in
1426 * #hb_glyph_info_t.cluster field.
1428 * This function does not check the validity of @codepoint, it is up to the
1429 * caller to ensure it is a valid Unicode code point.
1434 hb_buffer_add (hb_buffer_t *buffer,
1435 hb_codepoint_t codepoint,
1436 unsigned int cluster)
1438 buffer->add (codepoint, cluster);
1439 buffer->clear_context (1);
1443 * hb_buffer_set_length:
1444 * @buffer: An #hb_buffer_t
1445 * @length: The new length of @buffer
1447 * Similar to hb_buffer_pre_allocate(), but clears any new items added at the
1451 * `true` if @buffer memory allocation succeeded, `false` otherwise.
1456 hb_buffer_set_length (hb_buffer_t *buffer,
1457 unsigned int length)
1459 if (unlikely (hb_object_is_immutable (buffer)))
1462 if (unlikely (!buffer->ensure (length)))
1465 /* Wipe the new space */
1466 if (length > buffer->len) {
1467 hb_memset (buffer->info + buffer->len, 0, sizeof (buffer->info[0]) * (length - buffer->len));
1468 if (buffer->have_positions)
1469 hb_memset (buffer->pos + buffer->len, 0, sizeof (buffer->pos[0]) * (length - buffer->len));
1472 buffer->len = length;
1476 buffer->content_type = HB_BUFFER_CONTENT_TYPE_INVALID;
1477 buffer->clear_context (0);
1479 buffer->clear_context (1);
1485 * hb_buffer_get_length:
1486 * @buffer: An #hb_buffer_t
1488 * Returns the number of items in the buffer.
1491 * The @buffer length.
1492 * The value valid as long as buffer has not been modified.
1497 hb_buffer_get_length (const hb_buffer_t *buffer)
1503 * hb_buffer_get_glyph_infos:
1504 * @buffer: An #hb_buffer_t
1505 * @length: (out): The output-array length.
1507 * Returns @buffer glyph information array. Returned pointer
1508 * is valid as long as @buffer contents are not modified.
1510 * Return value: (transfer none) (array length=length):
1511 * The @buffer glyph information array.
1512 * The value valid as long as buffer has not been modified.
1517 hb_buffer_get_glyph_infos (hb_buffer_t *buffer,
1518 unsigned int *length)
1521 *length = buffer->len;
1523 return (hb_glyph_info_t *) buffer->info;
1527 * hb_buffer_get_glyph_positions:
1528 * @buffer: An #hb_buffer_t
1529 * @length: (out): The output length
1531 * Returns @buffer glyph position array. Returned pointer
1532 * is valid as long as @buffer contents are not modified.
1534 * If buffer did not have positions before, the positions will be
1535 * initialized to zeros, unless this function is called from
1536 * within a buffer message callback (see hb_buffer_set_message_func()),
1537 * in which case `NULL` is returned.
1539 * Return value: (transfer none) (array length=length):
1540 * The @buffer glyph position array.
1541 * The value valid as long as buffer has not been modified.
1545 hb_glyph_position_t *
1546 hb_buffer_get_glyph_positions (hb_buffer_t *buffer,
1547 unsigned int *length)
1550 *length = buffer->len;
1552 if (!buffer->have_positions)
1554 if (unlikely (buffer->message_depth))
1557 buffer->clear_positions ();
1560 return (hb_glyph_position_t *) buffer->pos;
1564 * hb_buffer_has_positions:
1565 * @buffer: an #hb_buffer_t.
1567 * Returns whether @buffer has glyph position data.
1568 * A buffer gains position data when hb_buffer_get_glyph_positions() is called on it,
1569 * and cleared of position data when hb_buffer_clear_contents() is called.
1572 * `true` if the @buffer has position array, `false` otherwise.
1577 hb_buffer_has_positions (hb_buffer_t *buffer)
1579 return buffer->have_positions;
1583 * hb_glyph_info_get_glyph_flags:
1584 * @info: a #hb_glyph_info_t
1586 * Returns glyph flags encoded within a #hb_glyph_info_t.
1589 * The #hb_glyph_flags_t encoded within @info
1594 (hb_glyph_info_get_glyph_flags) (const hb_glyph_info_t *info)
1596 return hb_glyph_info_get_glyph_flags (info);
1600 * hb_buffer_reverse:
1601 * @buffer: An #hb_buffer_t
1603 * Reverses buffer contents.
1608 hb_buffer_reverse (hb_buffer_t *buffer)
1614 * hb_buffer_reverse_range:
1615 * @buffer: An #hb_buffer_t
1616 * @start: start index
1619 * Reverses buffer contents between @start and @end.
1624 hb_buffer_reverse_range (hb_buffer_t *buffer,
1625 unsigned int start, unsigned int end)
1627 buffer->reverse_range (start, end);
1631 * hb_buffer_reverse_clusters:
1632 * @buffer: An #hb_buffer_t
1634 * Reverses buffer clusters. That is, the buffer contents are
1635 * reversed, then each cluster (consecutive items having the
1636 * same cluster number) are reversed again.
1641 hb_buffer_reverse_clusters (hb_buffer_t *buffer)
1643 buffer->reverse_clusters ();
1647 * hb_buffer_guess_segment_properties:
1648 * @buffer: An #hb_buffer_t
1650 * Sets unset buffer segment properties based on buffer Unicode
1651 * contents. If buffer is not empty, it must have content type
1652 * #HB_BUFFER_CONTENT_TYPE_UNICODE.
1654 * If buffer script is not set (ie. is #HB_SCRIPT_INVALID), it
1655 * will be set to the Unicode script of the first character in
1656 * the buffer that has a script other than #HB_SCRIPT_COMMON,
1657 * #HB_SCRIPT_INHERITED, and #HB_SCRIPT_UNKNOWN.
1659 * Next, if buffer direction is not set (ie. is #HB_DIRECTION_INVALID),
1660 * it will be set to the natural horizontal direction of the
1661 * buffer script as returned by hb_script_get_horizontal_direction().
1662 * If hb_script_get_horizontal_direction() returns #HB_DIRECTION_INVALID,
1663 * then #HB_DIRECTION_LTR is used.
1665 * Finally, if buffer language is not set (ie. is #HB_LANGUAGE_INVALID),
1666 * it will be set to the process's default language as returned by
1667 * hb_language_get_default(). This may change in the future by
1668 * taking buffer script into consideration when choosing a language.
1669 * Note that hb_language_get_default() is NOT threadsafe the first time
1670 * it is called. See documentation for that function for details.
1675 hb_buffer_guess_segment_properties (hb_buffer_t *buffer)
1677 buffer->guess_segment_properties ();
1680 template <typename utf_t>
1682 hb_buffer_add_utf (hb_buffer_t *buffer,
1683 const typename utf_t::codepoint_t *text,
1685 unsigned int item_offset,
1688 typedef typename utf_t::codepoint_t T;
1689 const hb_codepoint_t replacement = buffer->replacement;
1691 buffer->assert_unicode ();
1693 if (unlikely (hb_object_is_immutable (buffer)))
1696 if (text_length == -1)
1697 text_length = utf_t::strlen (text);
1699 if (item_length == -1)
1700 item_length = text_length - item_offset;
1702 if (unlikely (item_length < 0 ||
1703 item_length > INT_MAX / 8 ||
1704 !buffer->ensure (buffer->len + item_length * sizeof (T) / 4)))
1707 /* If buffer is empty and pre-context provided, install it.
1708 * This check is written this way, to make sure people can
1709 * provide pre-context in one add_utf() call, then provide
1710 * text in a follow-up call. See:
1712 * https://bugzilla.mozilla.org/show_bug.cgi?id=801410#c13
1714 if (!buffer->len && item_offset > 0)
1716 /* Add pre-context */
1717 buffer->clear_context (0);
1718 const T *prev = text + item_offset;
1719 const T *start = text;
1720 while (start < prev && buffer->context_len[0] < buffer->CONTEXT_LENGTH)
1723 prev = utf_t::prev (prev, start, &u, replacement);
1724 buffer->context[0][buffer->context_len[0]++] = u;
1728 const T *next = text + item_offset;
1729 const T *end = next + item_length;
1733 const T *old_next = next;
1734 next = utf_t::next (next, end, &u, replacement);
1735 buffer->add (u, old_next - (const T *) text);
1738 /* Add post-context */
1739 buffer->clear_context (1);
1740 end = text + text_length;
1741 while (next < end && buffer->context_len[1] < buffer->CONTEXT_LENGTH)
1744 next = utf_t::next (next, end, &u, replacement);
1745 buffer->context[1][buffer->context_len[1]++] = u;
1748 buffer->content_type = HB_BUFFER_CONTENT_TYPE_UNICODE;
1752 * hb_buffer_add_utf8:
1753 * @buffer: An #hb_buffer_t
1754 * @text: (array length=text_length) (element-type uint8_t): An array of UTF-8
1755 * characters to append.
1756 * @text_length: The length of the @text, or -1 if it is `NULL` terminated.
1757 * @item_offset: The offset of the first character to add to the @buffer.
1758 * @item_length: The number of characters to add to the @buffer, or -1 for the
1759 * end of @text (assuming it is `NULL` terminated).
1761 * See hb_buffer_add_codepoints().
1763 * Replaces invalid UTF-8 characters with the @buffer replacement code point,
1764 * see hb_buffer_set_replacement_codepoint().
1769 hb_buffer_add_utf8 (hb_buffer_t *buffer,
1772 unsigned int item_offset,
1775 hb_buffer_add_utf<hb_utf8_t> (buffer, (const uint8_t *) text, text_length, item_offset, item_length);
1779 * hb_buffer_add_utf16:
1780 * @buffer: An #hb_buffer_t
1781 * @text: (array length=text_length): An array of UTF-16 characters to append
1782 * @text_length: The length of the @text, or -1 if it is `NULL` terminated
1783 * @item_offset: The offset of the first character to add to the @buffer
1784 * @item_length: The number of characters to add to the @buffer, or -1 for the
1785 * end of @text (assuming it is `NULL` terminated)
1787 * See hb_buffer_add_codepoints().
1789 * Replaces invalid UTF-16 characters with the @buffer replacement code point,
1790 * see hb_buffer_set_replacement_codepoint().
1795 hb_buffer_add_utf16 (hb_buffer_t *buffer,
1796 const uint16_t *text,
1798 unsigned int item_offset,
1801 hb_buffer_add_utf<hb_utf16_t> (buffer, text, text_length, item_offset, item_length);
1805 * hb_buffer_add_utf32:
1806 * @buffer: An #hb_buffer_t
1807 * @text: (array length=text_length): An array of UTF-32 characters to append
1808 * @text_length: The length of the @text, or -1 if it is `NULL` terminated
1809 * @item_offset: The offset of the first character to add to the @buffer
1810 * @item_length: The number of characters to add to the @buffer, or -1 for the
1811 * end of @text (assuming it is `NULL` terminated)
1813 * See hb_buffer_add_codepoints().
1815 * Replaces invalid UTF-32 characters with the @buffer replacement code point,
1816 * see hb_buffer_set_replacement_codepoint().
1821 hb_buffer_add_utf32 (hb_buffer_t *buffer,
1822 const uint32_t *text,
1824 unsigned int item_offset,
1827 hb_buffer_add_utf<hb_utf32_t> (buffer, text, text_length, item_offset, item_length);
1831 * hb_buffer_add_latin1:
1832 * @buffer: An #hb_buffer_t
1833 * @text: (array length=text_length) (element-type uint8_t): an array of UTF-8
1834 * characters to append
1835 * @text_length: the length of the @text, or -1 if it is `NULL` terminated
1836 * @item_offset: the offset of the first character to add to the @buffer
1837 * @item_length: the number of characters to add to the @buffer, or -1 for the
1838 * end of @text (assuming it is `NULL` terminated)
1840 * Similar to hb_buffer_add_codepoints(), but allows only access to first 256
1841 * Unicode code points that can fit in 8-bit strings.
1843 * <note>Has nothing to do with non-Unicode Latin-1 encoding.</note>
1848 hb_buffer_add_latin1 (hb_buffer_t *buffer,
1849 const uint8_t *text,
1851 unsigned int item_offset,
1854 hb_buffer_add_utf<hb_latin1_t> (buffer, text, text_length, item_offset, item_length);
1858 * hb_buffer_add_codepoints:
1859 * @buffer: a #hb_buffer_t to append characters to.
1860 * @text: (array length=text_length): an array of Unicode code points to append.
1861 * @text_length: the length of the @text, or -1 if it is `NULL` terminated.
1862 * @item_offset: the offset of the first code point to add to the @buffer.
1863 * @item_length: the number of code points to add to the @buffer, or -1 for the
1864 * end of @text (assuming it is `NULL` terminated).
1866 * Appends characters from @text array to @buffer. The @item_offset is the
1867 * position of the first character from @text that will be appended, and
1868 * @item_length is the number of character. When shaping part of a larger text
1869 * (e.g. a run of text from a paragraph), instead of passing just the substring
1870 * corresponding to the run, it is preferable to pass the whole
1871 * paragraph and specify the run start and length as @item_offset and
1872 * @item_length, respectively, to give HarfBuzz the full context to be able,
1873 * for example, to do cross-run Arabic shaping or properly handle combining
1874 * marks at stat of run.
1876 * This function does not check the validity of @text, it is up to the caller
1877 * to ensure it contains a valid Unicode scalar values. In contrast,
1878 * hb_buffer_add_utf32() can be used that takes similar input but performs
1879 * sanity-check on the input.
1884 hb_buffer_add_codepoints (hb_buffer_t *buffer,
1885 const hb_codepoint_t *text,
1887 unsigned int item_offset,
1890 hb_buffer_add_utf<hb_utf32_novalidate_t> (buffer, text, text_length, item_offset, item_length);
1896 * @buffer: An #hb_buffer_t
1897 * @source: source #hb_buffer_t
1898 * @start: start index into source buffer to copy. Use 0 to copy from start of buffer.
1899 * @end: end index into source buffer to copy. Use @HB_FEATURE_GLOBAL_END to copy to end of buffer.
1901 * Append (part of) contents of another buffer to this buffer.
1906 hb_buffer_append (hb_buffer_t *buffer,
1907 const hb_buffer_t *source,
1911 assert (!buffer->have_output && !source->have_output);
1912 assert (buffer->have_positions == source->have_positions ||
1913 !buffer->len || !source->len);
1914 assert (buffer->content_type == source->content_type ||
1915 !buffer->len || !source->len);
1917 if (end > source->len)
1924 if (buffer->len + (end - start) < buffer->len) /* Overflows. */
1926 buffer->successful = false;
1930 unsigned int orig_len = buffer->len;
1931 hb_buffer_set_length (buffer, buffer->len + (end - start));
1932 if (unlikely (!buffer->successful))
1936 buffer->content_type = source->content_type;
1937 if (!buffer->have_positions && source->have_positions)
1938 buffer->clear_positions ();
1940 hb_segment_properties_overlay (&buffer->props, &source->props);
1942 hb_memcpy (buffer->info + orig_len, source->info + start, (end - start) * sizeof (buffer->info[0]));
1943 if (buffer->have_positions)
1944 hb_memcpy (buffer->pos + orig_len, source->pos + start, (end - start) * sizeof (buffer->pos[0]));
1946 if (source->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE)
1948 /* See similar logic in add_utf. */
1951 if (!orig_len && start + source->context_len[0] > 0)
1953 buffer->clear_context (0);
1954 while (start > 0 && buffer->context_len[0] < buffer->CONTEXT_LENGTH)
1955 buffer->context[0][buffer->context_len[0]++] = source->info[--start].codepoint;
1956 for (auto i = 0u; i < source->context_len[0] && buffer->context_len[0] < buffer->CONTEXT_LENGTH; i++)
1957 buffer->context[0][buffer->context_len[0]++] = source->context[0][i];
1961 buffer->clear_context (1);
1962 while (end < source->len && buffer->context_len[1] < buffer->CONTEXT_LENGTH)
1963 buffer->context[1][buffer->context_len[1]++] = source->info[end++].codepoint;
1964 for (auto i = 0u; i < source->context_len[1] && buffer->context_len[1] < buffer->CONTEXT_LENGTH; i++)
1965 buffer->context[1][buffer->context_len[1]++] = source->context[1][i];
1971 compare_info_codepoint (const hb_glyph_info_t *pa,
1972 const hb_glyph_info_t *pb)
1974 return (int) pb->codepoint - (int) pa->codepoint;
1978 normalize_glyphs_cluster (hb_buffer_t *buffer,
1983 hb_glyph_position_t *pos = buffer->pos;
1985 /* Total cluster advance */
1986 hb_position_t total_x_advance = 0, total_y_advance = 0;
1987 for (unsigned int i = start; i < end; i++)
1989 total_x_advance += pos[i].x_advance;
1990 total_y_advance += pos[i].y_advance;
1993 hb_position_t x_advance = 0, y_advance = 0;
1994 for (unsigned int i = start; i < end; i++)
1996 pos[i].x_offset += x_advance;
1997 pos[i].y_offset += y_advance;
1999 x_advance += pos[i].x_advance;
2000 y_advance += pos[i].y_advance;
2002 pos[i].x_advance = 0;
2003 pos[i].y_advance = 0;
2008 /* Transfer all cluster advance to the last glyph. */
2009 pos[end - 1].x_advance = total_x_advance;
2010 pos[end - 1].y_advance = total_y_advance;
2012 hb_stable_sort (buffer->info + start, end - start - 1, compare_info_codepoint, buffer->pos + start);
2014 /* Transfer all cluster advance to the first glyph. */
2015 pos[start].x_advance += total_x_advance;
2016 pos[start].y_advance += total_y_advance;
2017 for (unsigned int i = start + 1; i < end; i++) {
2018 pos[i].x_offset -= total_x_advance;
2019 pos[i].y_offset -= total_y_advance;
2021 hb_stable_sort (buffer->info + start + 1, end - start - 1, compare_info_codepoint, buffer->pos + start + 1);
2026 * hb_buffer_normalize_glyphs:
2027 * @buffer: An #hb_buffer_t
2029 * Reorders a glyph buffer to have canonical in-cluster glyph order / position.
2030 * The resulting clusters should behave identical to pre-reordering clusters.
2032 * <note>This has nothing to do with Unicode normalization.</note>
2037 hb_buffer_normalize_glyphs (hb_buffer_t *buffer)
2039 assert (buffer->have_positions);
2041 buffer->assert_glyphs ();
2043 bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
2045 foreach_cluster (buffer, start, end)
2046 normalize_glyphs_cluster (buffer, start, end, backward);
2050 hb_buffer_t::sort (unsigned int start, unsigned int end, int(*compar)(const hb_glyph_info_t *, const hb_glyph_info_t *))
2052 assert (!have_positions);
2053 for (unsigned int i = start + 1; i < end; i++)
2056 while (j > start && compar (&info[j - 1], &info[i]) > 0)
2060 /* Move item i to occupy place for item j, shift what's in between. */
2061 merge_clusters (j, i + 1);
2063 hb_glyph_info_t t = info[i];
2064 memmove (&info[j + 1], &info[j], (i - j) * sizeof (hb_glyph_info_t));
2072 * Comparing buffers.
2077 * @buffer: a buffer.
2078 * @reference: other buffer to compare to.
2079 * @dottedcircle_glyph: glyph id of U+25CC DOTTED CIRCLE, or (hb_codepoint_t) -1.
2080 * @position_fuzz: allowed absolute difference in position values.
2082 * If dottedcircle_glyph is (hb_codepoint_t) -1 then #HB_BUFFER_DIFF_FLAG_DOTTED_CIRCLE_PRESENT
2083 * and #HB_BUFFER_DIFF_FLAG_NOTDEF_PRESENT are never returned. This should be used by most
2084 * callers if just comparing two buffers is needed.
2088 hb_buffer_diff_flags_t
2089 hb_buffer_diff (hb_buffer_t *buffer,
2090 hb_buffer_t *reference,
2091 hb_codepoint_t dottedcircle_glyph,
2092 unsigned int position_fuzz)
2094 if (buffer->content_type != reference->content_type && buffer->len && reference->len)
2095 return HB_BUFFER_DIFF_FLAG_CONTENT_TYPE_MISMATCH;
2097 hb_buffer_diff_flags_t result = HB_BUFFER_DIFF_FLAG_EQUAL;
2098 bool contains = dottedcircle_glyph != (hb_codepoint_t) -1;
2100 unsigned int count = reference->len;
2102 if (buffer->len != count)
2105 * we can't compare glyph-by-glyph, but we do want to know if there
2106 * are .notdef or dottedcircle glyphs present in the reference buffer
2108 const hb_glyph_info_t *info = reference->info;
2110 for (i = 0; i < count; i++)
2112 if (contains && info[i].codepoint == dottedcircle_glyph)
2113 result |= HB_BUFFER_DIFF_FLAG_DOTTED_CIRCLE_PRESENT;
2114 if (contains && info[i].codepoint == 0)
2115 result |= HB_BUFFER_DIFF_FLAG_NOTDEF_PRESENT;
2117 result |= HB_BUFFER_DIFF_FLAG_LENGTH_MISMATCH;
2118 return hb_buffer_diff_flags_t (result);
2122 return hb_buffer_diff_flags_t (result);
2124 const hb_glyph_info_t *buf_info = buffer->info;
2125 const hb_glyph_info_t *ref_info = reference->info;
2126 for (unsigned int i = 0; i < count; i++)
2128 if (buf_info->codepoint != ref_info->codepoint)
2129 result |= HB_BUFFER_DIFF_FLAG_CODEPOINT_MISMATCH;
2130 if (buf_info->cluster != ref_info->cluster)
2131 result |= HB_BUFFER_DIFF_FLAG_CLUSTER_MISMATCH;
2132 if ((buf_info->mask ^ ref_info->mask) & HB_GLYPH_FLAG_DEFINED)
2133 result |= HB_BUFFER_DIFF_FLAG_GLYPH_FLAGS_MISMATCH;
2134 if (contains && ref_info->codepoint == dottedcircle_glyph)
2135 result |= HB_BUFFER_DIFF_FLAG_DOTTED_CIRCLE_PRESENT;
2136 if (contains && ref_info->codepoint == 0)
2137 result |= HB_BUFFER_DIFF_FLAG_NOTDEF_PRESENT;
2142 if (buffer->content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS)
2144 assert (buffer->have_positions);
2145 const hb_glyph_position_t *buf_pos = buffer->pos;
2146 const hb_glyph_position_t *ref_pos = reference->pos;
2147 for (unsigned int i = 0; i < count; i++)
2149 if ((unsigned int) abs (buf_pos->x_advance - ref_pos->x_advance) > position_fuzz ||
2150 (unsigned int) abs (buf_pos->y_advance - ref_pos->y_advance) > position_fuzz ||
2151 (unsigned int) abs (buf_pos->x_offset - ref_pos->x_offset) > position_fuzz ||
2152 (unsigned int) abs (buf_pos->y_offset - ref_pos->y_offset) > position_fuzz)
2154 result |= HB_BUFFER_DIFF_FLAG_POSITION_MISMATCH;
2170 #ifndef HB_NO_BUFFER_MESSAGE
2172 * hb_buffer_set_message_func:
2173 * @buffer: An #hb_buffer_t
2174 * @func: (closure user_data) (destroy destroy) (scope notified): Callback function
2175 * @user_data: (nullable): Data to pass to @func
2176 * @destroy: (nullable): The function to call when @user_data is not needed anymore
2178 * Sets the implementation function for #hb_buffer_message_func_t.
2183 hb_buffer_set_message_func (hb_buffer_t *buffer,
2184 hb_buffer_message_func_t func,
2185 void *user_data, hb_destroy_func_t destroy)
2187 if (unlikely (hb_object_is_immutable (buffer)))
2190 destroy (user_data);
2194 if (buffer->message_destroy)
2195 buffer->message_destroy (buffer->message_data);
2198 buffer->message_func = func;
2199 buffer->message_data = user_data;
2200 buffer->message_destroy = destroy;
2202 buffer->message_func = nullptr;
2203 buffer->message_data = nullptr;
2204 buffer->message_destroy = nullptr;
2208 hb_buffer_t::message_impl (hb_font_t *font, const char *fmt, va_list ap)
2210 assert (!have_output || (out_info == info && out_len == idx));
2215 vsnprintf (buf, sizeof (buf), fmt, ap);
2216 bool ret = (bool) this->message_func (this, font, buf, this->message_data);