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-private.hh"
31 #include "hb-utf-private.hh"
34 #ifndef HB_DEBUG_BUFFER
35 #define HB_DEBUG_BUFFER (HB_DEBUG+0)
41 * @short_description: Input and output buffers
44 * Buffers serve dual role in HarfBuzz; they hold the input characters that are
45 * passed hb_shape(), and after shaping they hold the output glyphs.
49 * hb_segment_properties_equal:
50 * @a: first #hb_segment_properties_t to compare.
51 * @b: second #hb_segment_properties_t to compare.
53 * Checks the equality of two #hb_segment_properties_t's.
56 * %true if all properties of @a equal those of @b, false otherwise.
61 hb_segment_properties_equal (const hb_segment_properties_t *a,
62 const hb_segment_properties_t *b)
64 return a->direction == b->direction &&
65 a->script == b->script &&
66 a->language == b->language &&
67 a->reserved1 == b->reserved1 &&
68 a->reserved2 == b->reserved2;
73 * hb_segment_properties_hash:
74 * @p: #hb_segment_properties_t to hash.
76 * Creates a hash representing @p.
84 hb_segment_properties_hash (const hb_segment_properties_t *p)
86 return (unsigned int) p->direction ^
87 (unsigned int) p->script ^
88 (intptr_t) (p->language);
93 /* Here is how the buffer works internally:
95 * There are two info pointers: info and out_info. They always have
96 * the same allocated size, but different lengths.
98 * As an optimization, both info and out_info may point to the
99 * same piece of memory, which is owned by info. This remains the
100 * case as long as out_len doesn't exceed i at any time.
101 * In that case, swap_buffers() is no-op and the glyph operations operate
104 * As soon as out_info gets longer than info, out_info is moved over
105 * to an alternate buffer (which we reuse the pos buffer for!), and its
106 * current contents (out_len entries) are copied to the new place.
107 * This should all remain transparent to the user. swap_buffers() then
108 * switches info and out_info.
116 hb_buffer_t::enlarge (unsigned int size)
118 if (unlikely (in_error))
120 if (unlikely (size > max_len))
126 unsigned int new_allocated = allocated;
127 hb_glyph_position_t *new_pos = NULL;
128 hb_glyph_info_t *new_info = NULL;
129 bool separate_out = out_info != info;
131 if (unlikely (_hb_unsigned_int_mul_overflows (size, sizeof (info[0]))))
134 while (size >= new_allocated)
135 new_allocated += (new_allocated >> 1) + 32;
137 ASSERT_STATIC (sizeof (info[0]) == sizeof (pos[0]));
138 if (unlikely (_hb_unsigned_int_mul_overflows (new_allocated, sizeof (info[0]))))
141 new_pos = (hb_glyph_position_t *) realloc (pos, new_allocated * sizeof (pos[0]));
142 new_info = (hb_glyph_info_t *) realloc (info, new_allocated * sizeof (info[0]));
145 if (unlikely (!new_pos || !new_info))
148 if (likely (new_pos))
151 if (likely (new_info))
154 out_info = separate_out ? (hb_glyph_info_t *) pos : info;
155 if (likely (!in_error))
156 allocated = new_allocated;
158 return likely (!in_error);
162 hb_buffer_t::make_room_for (unsigned int num_in,
163 unsigned int num_out)
165 if (unlikely (!ensure (out_len + num_out))) return false;
167 if (out_info == info &&
168 out_len + num_out > idx + num_in)
170 assert (have_output);
172 out_info = (hb_glyph_info_t *) pos;
173 memcpy (out_info, info, out_len * sizeof (out_info[0]));
180 hb_buffer_t::shift_forward (unsigned int count)
182 assert (have_output);
183 if (unlikely (!ensure (len + count))) return false;
185 memmove (info + idx + count, info + idx, (len - idx) * sizeof (info[0]));
186 if (idx + count > len)
188 /* Under memory failure we might expose this area. At least
189 * clean it up. Oh well... */
190 memset (info + len, 0, (idx + count - len) * sizeof (info[0]));
198 hb_buffer_t::scratch_buffer_t *
199 hb_buffer_t::get_scratch_buffer (unsigned int *size)
202 have_positions = false;
207 assert ((uintptr_t) pos % sizeof (scratch_buffer_t) == 0);
208 *size = allocated * sizeof (pos[0]) / sizeof (scratch_buffer_t);
209 return (scratch_buffer_t *) (void *) pos;
214 /* HarfBuzz-Internal API */
217 hb_buffer_t::reset (void)
219 if (unlikely (hb_object_is_inert (this)))
222 hb_unicode_funcs_destroy (unicode);
223 unicode = hb_unicode_funcs_get_default ();
224 flags = HB_BUFFER_FLAG_DEFAULT;
225 replacement = HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT;
231 hb_buffer_t::clear (void)
233 if (unlikely (hb_object_is_inert (this)))
236 hb_segment_properties_t default_props = HB_SEGMENT_PROPERTIES_DEFAULT;
237 props = default_props;
238 scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;
240 content_type = HB_BUFFER_CONTENT_TYPE_INVALID;
243 have_positions = false;
252 memset (context, 0, sizeof context);
253 memset (context_len, 0, sizeof context_len);
255 deallocate_var_all ();
259 hb_buffer_t::add (hb_codepoint_t codepoint,
260 unsigned int cluster)
262 hb_glyph_info_t *glyph;
264 if (unlikely (!ensure (len + 1))) return;
268 memset (glyph, 0, sizeof (*glyph));
269 glyph->codepoint = codepoint;
271 glyph->cluster = cluster;
277 hb_buffer_t::add_info (const hb_glyph_info_t &glyph_info)
279 if (unlikely (!ensure (len + 1))) return;
281 info[len] = glyph_info;
288 hb_buffer_t::remove_output (void)
290 if (unlikely (hb_object_is_inert (this)))
294 have_positions = false;
301 hb_buffer_t::clear_output (void)
303 if (unlikely (hb_object_is_inert (this)))
307 have_positions = false;
314 hb_buffer_t::clear_positions (void)
316 if (unlikely (hb_object_is_inert (this)))
320 have_positions = true;
325 memset (pos, 0, sizeof (pos[0]) * len);
329 hb_buffer_t::swap_buffers (void)
331 if (unlikely (in_error)) return;
333 assert (have_output);
336 if (out_info != info)
338 hb_glyph_info_t *tmp_string;
341 out_info = tmp_string;
342 pos = (hb_glyph_position_t *) out_info;
355 hb_buffer_t::replace_glyphs (unsigned int num_in,
356 unsigned int num_out,
357 const uint32_t *glyph_data)
359 if (unlikely (!make_room_for (num_in, num_out))) return;
361 merge_clusters (idx, idx + num_in);
363 hb_glyph_info_t orig_info = info[idx];
364 hb_glyph_info_t *pinfo = &out_info[out_len];
365 for (unsigned int i = 0; i < num_out; i++)
368 pinfo->codepoint = glyph_data[i];
377 hb_buffer_t::output_glyph (hb_codepoint_t glyph_index)
379 if (unlikely (!make_room_for (0, 1))) return;
381 out_info[out_len] = info[idx];
382 out_info[out_len].codepoint = glyph_index;
388 hb_buffer_t::output_info (const hb_glyph_info_t &glyph_info)
390 if (unlikely (!make_room_for (0, 1))) return;
392 out_info[out_len] = glyph_info;
398 hb_buffer_t::copy_glyph (void)
400 if (unlikely (!make_room_for (0, 1))) return;
402 out_info[out_len] = info[idx];
408 hb_buffer_t::move_to (unsigned int i)
416 if (unlikely (in_error))
419 assert (i <= out_len + (len - idx));
423 unsigned int count = i - out_len;
424 if (unlikely (!make_room_for (count, count))) return false;
426 memmove (out_info + out_len, info + idx, count * sizeof (out_info[0]));
430 else if (out_len > i)
432 /* Tricky part: rewinding... */
433 unsigned int count = out_len - i;
435 /* This will blow in our face if memory allocation fails later
436 * in this same lookup... */
437 if (unlikely (idx < count && !shift_forward (count + 32))) return false;
439 assert (idx >= count);
443 memmove (info + idx, out_info + out_len, count * sizeof (out_info[0]));
450 hb_buffer_t::replace_glyph (hb_codepoint_t glyph_index)
452 if (unlikely (out_info != info || out_len != idx)) {
453 if (unlikely (!make_room_for (1, 1))) return;
454 out_info[out_len] = info[idx];
456 out_info[out_len].codepoint = glyph_index;
464 hb_buffer_t::set_masks (hb_mask_t value,
466 unsigned int cluster_start,
467 unsigned int cluster_end)
469 hb_mask_t not_mask = ~mask;
475 if (cluster_start == 0 && cluster_end == (unsigned int)-1) {
476 unsigned int count = len;
477 for (unsigned int i = 0; i < count; i++)
478 info[i].mask = (info[i].mask & not_mask) | value;
482 unsigned int count = len;
483 for (unsigned int i = 0; i < count; i++)
484 if (cluster_start <= info[i].cluster && info[i].cluster < cluster_end)
485 info[i].mask = (info[i].mask & not_mask) | value;
489 hb_buffer_t::reverse_range (unsigned int start,
497 for (i = start, j = end - 1; i < j; i++, j--) {
505 if (have_positions) {
506 for (i = start, j = end - 1; i < j; i++, j--) {
507 hb_glyph_position_t t;
517 hb_buffer_t::reverse (void)
522 reverse_range (0, len);
526 hb_buffer_t::reverse_clusters (void)
528 unsigned int i, start, count, last_cluster;
537 last_cluster = info[0].cluster;
538 for (i = 1; i < count; i++) {
539 if (last_cluster != info[i].cluster) {
540 reverse_range (start, i);
542 last_cluster = info[i].cluster;
545 reverse_range (start, i);
549 hb_buffer_t::merge_clusters_impl (unsigned int start,
552 if (cluster_level == HB_BUFFER_CLUSTER_LEVEL_CHARACTERS)
555 unsigned int cluster = info[start].cluster;
557 for (unsigned int i = start + 1; i < end; i++)
558 cluster = MIN (cluster, info[i].cluster);
561 while (end < len && info[end - 1].cluster == info[end].cluster)
565 while (idx < start && info[start - 1].cluster == info[start].cluster)
568 /* If we hit the start of buffer, continue in out-buffer. */
570 for (unsigned int i = out_len; i && out_info[i - 1].cluster == info[start].cluster; i--)
571 out_info[i - 1].cluster = cluster;
573 for (unsigned int i = start; i < end; i++)
574 info[i].cluster = cluster;
577 hb_buffer_t::merge_out_clusters (unsigned int start,
580 if (cluster_level == HB_BUFFER_CLUSTER_LEVEL_CHARACTERS)
583 if (unlikely (end - start < 2))
586 unsigned int cluster = out_info[start].cluster;
588 for (unsigned int i = start + 1; i < end; i++)
589 cluster = MIN (cluster, out_info[i].cluster);
592 while (start && out_info[start - 1].cluster == out_info[start].cluster)
596 while (end < out_len && out_info[end - 1].cluster == out_info[end].cluster)
599 /* If we hit the end of out-buffer, continue in buffer. */
601 for (unsigned int i = idx; i < len && info[i].cluster == out_info[end - 1].cluster; i++)
602 info[i].cluster = cluster;
604 for (unsigned int i = start; i < end; i++)
605 out_info[i].cluster = cluster;
608 hb_buffer_t::delete_glyph ()
610 unsigned int cluster = info[idx].cluster;
611 if (idx + 1 < len && cluster == info[idx + 1].cluster)
613 /* Cluster survives; do nothing. */
619 /* Merge cluster backward. */
620 if (cluster < out_info[out_len - 1].cluster)
622 unsigned int old_cluster = out_info[out_len - 1].cluster;
623 for (unsigned i = out_len; i && out_info[i - 1].cluster == old_cluster; i--)
624 out_info[i - 1].cluster = cluster;
631 /* Merge cluster forward. */
632 merge_clusters (idx, idx + 2);
641 hb_buffer_t::guess_segment_properties (void)
643 assert (content_type == HB_BUFFER_CONTENT_TYPE_UNICODE ||
644 (!len && content_type == HB_BUFFER_CONTENT_TYPE_INVALID));
646 /* If script is set to INVALID, guess from buffer contents */
647 if (props.script == HB_SCRIPT_INVALID) {
648 for (unsigned int i = 0; i < len; i++) {
649 hb_script_t script = unicode->script (info[i].codepoint);
650 if (likely (script != HB_SCRIPT_COMMON &&
651 script != HB_SCRIPT_INHERITED &&
652 script != HB_SCRIPT_UNKNOWN)) {
653 props.script = script;
659 /* If direction is set to INVALID, guess from script */
660 if (props.direction == HB_DIRECTION_INVALID) {
661 props.direction = hb_script_get_horizontal_direction (props.script);
664 /* If language is not set, use default language from locale */
665 if (props.language == HB_LANGUAGE_INVALID) {
666 /* TODO get_default_for_script? using $LANGUAGE */
667 props.language = hb_language_get_default ();
675 * hb_buffer_create: (Xconstructor)
677 * Creates a new #hb_buffer_t with all properties to defaults.
679 * Return value: (transfer full):
680 * A newly allocated #hb_buffer_t with a reference count of 1. The initial
681 * reference count should be released with hb_buffer_destroy() when you are done
682 * using the #hb_buffer_t. This function never returns %NULL. If memory cannot
683 * be allocated, a special #hb_buffer_t object will be returned on which
684 * hb_buffer_allocation_successful() returns %false.
689 hb_buffer_create (void)
693 if (!(buffer = hb_object_create<hb_buffer_t> ()))
694 return hb_buffer_get_empty ();
696 buffer->max_len = HB_BUFFER_MAX_LEN_DEFAULT;
704 * hb_buffer_get_empty:
708 * Return value: (transfer full):
713 hb_buffer_get_empty (void)
715 static const hb_buffer_t _hb_buffer_nil = {
716 HB_OBJECT_HEADER_STATIC,
718 const_cast<hb_unicode_funcs_t *> (&_hb_unicode_funcs_nil),
719 HB_BUFFER_FLAG_DEFAULT,
720 HB_BUFFER_CLUSTER_LEVEL_DEFAULT,
721 HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT,
722 HB_BUFFER_SCRATCH_FLAG_DEFAULT,
723 HB_BUFFER_MAX_LEN_DEFAULT,
725 HB_BUFFER_CONTENT_TYPE_INVALID,
726 HB_SEGMENT_PROPERTIES_DEFAULT,
728 true, /* have_output */
729 true /* have_positions */
731 /* Zero is good enough for everything else. */
734 return const_cast<hb_buffer_t *> (&_hb_buffer_nil);
738 * hb_buffer_reference: (skip)
739 * @buffer: an #hb_buffer_t.
741 * Increases the reference count on @buffer by one. This prevents @buffer from
742 * being destroyed until a matching call to hb_buffer_destroy() is made.
744 * Return value: (transfer full):
745 * The referenced #hb_buffer_t.
750 hb_buffer_reference (hb_buffer_t *buffer)
752 return hb_object_reference (buffer);
756 * hb_buffer_destroy: (skip)
757 * @buffer: an #hb_buffer_t.
759 * Deallocate the @buffer.
760 * Decreases the reference count on @buffer by one. If the result is zero, then
761 * @buffer and all associated resources are freed. See hb_buffer_reference().
766 hb_buffer_destroy (hb_buffer_t *buffer)
768 if (!hb_object_destroy (buffer)) return;
770 hb_unicode_funcs_destroy (buffer->unicode);
774 if (buffer->message_destroy)
775 buffer->message_destroy (buffer->message_data);
781 * hb_buffer_set_user_data: (skip)
782 * @buffer: an #hb_buffer_t.
795 hb_buffer_set_user_data (hb_buffer_t *buffer,
796 hb_user_data_key_t *key,
798 hb_destroy_func_t destroy,
801 return hb_object_set_user_data (buffer, key, data, destroy, replace);
805 * hb_buffer_get_user_data: (skip)
806 * @buffer: an #hb_buffer_t.
816 hb_buffer_get_user_data (hb_buffer_t *buffer,
817 hb_user_data_key_t *key)
819 return hb_object_get_user_data (buffer, key);
824 * hb_buffer_set_content_type:
825 * @buffer: an #hb_buffer_t.
826 * @content_type: the type of buffer contents to set
828 * Sets the type of @buffer contents, buffers are either empty, contain
829 * characters (before shaping) or glyphs (the result of shaping).
834 hb_buffer_set_content_type (hb_buffer_t *buffer,
835 hb_buffer_content_type_t content_type)
837 buffer->content_type = content_type;
841 * hb_buffer_get_content_type:
842 * @buffer: an #hb_buffer_t.
844 * see hb_buffer_set_content_type().
847 * The type of @buffer contents.
851 hb_buffer_content_type_t
852 hb_buffer_get_content_type (hb_buffer_t *buffer)
854 return buffer->content_type;
859 * hb_buffer_set_unicode_funcs:
860 * @buffer: an #hb_buffer_t.
868 hb_buffer_set_unicode_funcs (hb_buffer_t *buffer,
869 hb_unicode_funcs_t *unicode_funcs)
871 if (unlikely (hb_object_is_inert (buffer)))
875 unicode_funcs = hb_unicode_funcs_get_default ();
878 hb_unicode_funcs_reference (unicode_funcs);
879 hb_unicode_funcs_destroy (buffer->unicode);
880 buffer->unicode = unicode_funcs;
884 * hb_buffer_get_unicode_funcs:
885 * @buffer: an #hb_buffer_t.
894 hb_buffer_get_unicode_funcs (hb_buffer_t *buffer)
896 return buffer->unicode;
900 * hb_buffer_set_direction:
901 * @buffer: an #hb_buffer_t.
902 * @direction: the #hb_direction_t of the @buffer
904 * Set the text flow direction of the buffer. No shaping can happen without
905 * setting @buffer direction, and it controls the visual direction for the
906 * output glyphs; for RTL direction the glyphs will be reversed. Many layout
907 * features depend on the proper setting of the direction, for example,
908 * reversing RTL text before shaping, then shaping with LTR direction is not
909 * the same as keeping the text in logical order and shaping with RTL
915 hb_buffer_set_direction (hb_buffer_t *buffer,
916 hb_direction_t direction)
919 if (unlikely (hb_object_is_inert (buffer)))
922 buffer->props.direction = direction;
926 * hb_buffer_get_direction:
927 * @buffer: an #hb_buffer_t.
929 * See hb_buffer_set_direction()
932 * The direction of the @buffer.
937 hb_buffer_get_direction (hb_buffer_t *buffer)
939 return buffer->props.direction;
943 * hb_buffer_set_script:
944 * @buffer: an #hb_buffer_t.
945 * @script: an #hb_script_t to set.
947 * Sets the script of @buffer to @script.
949 * Script is crucial for choosing the proper shaping behaviour for scripts that
950 * require it (e.g. Arabic) and the which OpenType features defined in the font
953 * You can pass one of the predefined #hb_script_t values, or use
954 * hb_script_from_string() or hb_script_from_iso15924_tag() to get the
955 * corresponding script from an ISO 15924 script tag.
960 hb_buffer_set_script (hb_buffer_t *buffer,
963 if (unlikely (hb_object_is_inert (buffer)))
966 buffer->props.script = script;
970 * hb_buffer_get_script:
971 * @buffer: an #hb_buffer_t.
973 * See hb_buffer_set_script().
976 * The #hb_script_t of the @buffer.
981 hb_buffer_get_script (hb_buffer_t *buffer)
983 return buffer->props.script;
987 * hb_buffer_set_language:
988 * @buffer: an #hb_buffer_t.
989 * @language: an hb_language_t to set.
991 * Sets the language of @buffer to @language.
993 * Languages are crucial for selecting which OpenType feature to apply to the
994 * buffer which can result in applying language-specific behaviour. Languages
995 * are orthogonal to the scripts, and though they are related, they are
996 * different concepts and should not be confused with each other.
998 * Use hb_language_from_string() to convert from ISO 639 language codes to
1004 hb_buffer_set_language (hb_buffer_t *buffer,
1005 hb_language_t language)
1007 if (unlikely (hb_object_is_inert (buffer)))
1010 buffer->props.language = language;
1014 * hb_buffer_get_language:
1015 * @buffer: an #hb_buffer_t.
1017 * See hb_buffer_set_language().
1019 * Return value: (transfer none):
1020 * The #hb_language_t of the buffer. Must not be freed by the caller.
1025 hb_buffer_get_language (hb_buffer_t *buffer)
1027 return buffer->props.language;
1031 * hb_buffer_set_segment_properties:
1032 * @buffer: an #hb_buffer_t.
1033 * @props: an #hb_segment_properties_t to use.
1035 * Sets the segment properties of the buffer, a shortcut for calling
1036 * hb_buffer_set_direction(), hb_buffer_set_script() and
1037 * hb_buffer_set_language() individually.
1042 hb_buffer_set_segment_properties (hb_buffer_t *buffer,
1043 const hb_segment_properties_t *props)
1045 if (unlikely (hb_object_is_inert (buffer)))
1048 buffer->props = *props;
1052 * hb_buffer_get_segment_properties:
1053 * @buffer: an #hb_buffer_t.
1054 * @props: (out): the output #hb_segment_properties_t.
1056 * Sets @props to the #hb_segment_properties_t of @buffer.
1061 hb_buffer_get_segment_properties (hb_buffer_t *buffer,
1062 hb_segment_properties_t *props)
1064 *props = buffer->props;
1069 * hb_buffer_set_flags:
1070 * @buffer: an #hb_buffer_t.
1071 * @flags: the buffer flags to set.
1073 * Sets @buffer flags to @flags. See #hb_buffer_flags_t.
1078 hb_buffer_set_flags (hb_buffer_t *buffer,
1079 hb_buffer_flags_t flags)
1081 if (unlikely (hb_object_is_inert (buffer)))
1084 buffer->flags = flags;
1088 * hb_buffer_get_flags:
1089 * @buffer: an #hb_buffer_t.
1091 * See hb_buffer_set_flags().
1094 * The @buffer flags.
1099 hb_buffer_get_flags (hb_buffer_t *buffer)
1101 return buffer->flags;
1105 * hb_buffer_set_cluster_level:
1106 * @buffer: an #hb_buffer_t.
1114 hb_buffer_set_cluster_level (hb_buffer_t *buffer,
1115 hb_buffer_cluster_level_t cluster_level)
1117 if (unlikely (hb_object_is_inert (buffer)))
1120 buffer->cluster_level = cluster_level;
1124 * hb_buffer_get_cluster_level:
1125 * @buffer: an #hb_buffer_t.
1133 hb_buffer_cluster_level_t
1134 hb_buffer_get_cluster_level (hb_buffer_t *buffer)
1136 return buffer->cluster_level;
1141 * hb_buffer_set_replacement_codepoint:
1142 * @buffer: an #hb_buffer_t.
1143 * @replacement: the replacement #hb_codepoint_t
1145 * Sets the #hb_codepoint_t that replaces invalid entries for a given encoding
1146 * when adding text to @buffer.
1148 * Default is %HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT.
1153 hb_buffer_set_replacement_codepoint (hb_buffer_t *buffer,
1154 hb_codepoint_t replacement)
1156 if (unlikely (hb_object_is_inert (buffer)))
1159 buffer->replacement = replacement;
1163 * hb_buffer_get_replacement_codepoint:
1164 * @buffer: an #hb_buffer_t.
1166 * See hb_buffer_set_replacement_codepoint().
1169 * The @buffer replacement #hb_codepoint_t.
1174 hb_buffer_get_replacement_codepoint (hb_buffer_t *buffer)
1176 return buffer->replacement;
1182 * @buffer: an #hb_buffer_t.
1184 * Resets the buffer to its initial status, as if it was just newly created
1185 * with hb_buffer_create().
1190 hb_buffer_reset (hb_buffer_t *buffer)
1196 * hb_buffer_clear_contents:
1197 * @buffer: an #hb_buffer_t.
1199 * Similar to hb_buffer_reset(), but does not clear the Unicode functions and
1200 * the replacement code point.
1205 hb_buffer_clear_contents (hb_buffer_t *buffer)
1211 * hb_buffer_pre_allocate:
1212 * @buffer: an #hb_buffer_t.
1213 * @size: number of items to pre allocate.
1215 * Pre allocates memory for @buffer to fit at least @size number of items.
1218 * %true if @buffer memory allocation succeeded, %false otherwise.
1223 hb_buffer_pre_allocate (hb_buffer_t *buffer, unsigned int size)
1225 return buffer->ensure (size);
1229 * hb_buffer_allocation_successful:
1230 * @buffer: an #hb_buffer_t.
1232 * Check if allocating memory for the buffer succeeded.
1235 * %true if @buffer memory allocation succeeded, %false otherwise.
1240 hb_buffer_allocation_successful (hb_buffer_t *buffer)
1242 return !buffer->in_error;
1247 * @buffer: an #hb_buffer_t.
1248 * @codepoint: a Unicode code point.
1249 * @cluster: the cluster value of @codepoint.
1251 * Appends a character with the Unicode value of @codepoint to @buffer, and
1252 * gives it the initial cluster value of @cluster. Clusters can be any thing
1253 * the client wants, they are usually used to refer to the index of the
1254 * character in the input text stream and are output in
1255 * #hb_glyph_info_t.cluster field.
1257 * This function does not check the validity of @codepoint, it is up to the
1258 * caller to ensure it is a valid Unicode code point.
1263 hb_buffer_add (hb_buffer_t *buffer,
1264 hb_codepoint_t codepoint,
1265 unsigned int cluster)
1267 buffer->add (codepoint, cluster);
1268 buffer->clear_context (1);
1272 * hb_buffer_set_length:
1273 * @buffer: an #hb_buffer_t.
1274 * @length: the new length of @buffer.
1276 * Similar to hb_buffer_pre_allocate(), but clears any new items added at the
1280 * %true if @buffer memory allocation succeeded, %false otherwise.
1285 hb_buffer_set_length (hb_buffer_t *buffer,
1286 unsigned int length)
1288 if (unlikely (hb_object_is_inert (buffer)))
1291 if (!buffer->ensure (length))
1294 /* Wipe the new space */
1295 if (length > buffer->len) {
1296 memset (buffer->info + buffer->len, 0, sizeof (buffer->info[0]) * (length - buffer->len));
1297 if (buffer->have_positions)
1298 memset (buffer->pos + buffer->len, 0, sizeof (buffer->pos[0]) * (length - buffer->len));
1301 buffer->len = length;
1305 buffer->content_type = HB_BUFFER_CONTENT_TYPE_INVALID;
1306 buffer->clear_context (0);
1308 buffer->clear_context (1);
1314 * hb_buffer_get_length:
1315 * @buffer: an #hb_buffer_t.
1317 * Returns the number of items in the buffer.
1320 * The @buffer length.
1321 * The value valid as long as buffer has not been modified.
1326 hb_buffer_get_length (hb_buffer_t *buffer)
1332 * hb_buffer_get_glyph_infos:
1333 * @buffer: an #hb_buffer_t.
1334 * @length: (out): output array length.
1336 * Returns @buffer glyph information array. Returned pointer
1337 * is valid as long as @buffer contents are not modified.
1339 * Return value: (transfer none) (array length=length):
1340 * The @buffer glyph information array.
1341 * The value valid as long as buffer has not been modified.
1346 hb_buffer_get_glyph_infos (hb_buffer_t *buffer,
1347 unsigned int *length)
1350 *length = buffer->len;
1352 return (hb_glyph_info_t *) buffer->info;
1356 * hb_buffer_get_glyph_positions:
1357 * @buffer: an #hb_buffer_t.
1358 * @length: (out): output length.
1360 * Returns @buffer glyph position array. Returned pointer
1361 * is valid as long as @buffer contents are not modified.
1363 * Return value: (transfer none) (array length=length):
1364 * The @buffer glyph position array.
1365 * The value valid as long as buffer has not been modified.
1369 hb_glyph_position_t *
1370 hb_buffer_get_glyph_positions (hb_buffer_t *buffer,
1371 unsigned int *length)
1373 if (!buffer->have_positions)
1374 buffer->clear_positions ();
1377 *length = buffer->len;
1379 return (hb_glyph_position_t *) buffer->pos;
1383 * hb_buffer_reverse:
1384 * @buffer: an #hb_buffer_t.
1386 * Reverses buffer contents.
1391 hb_buffer_reverse (hb_buffer_t *buffer)
1397 * hb_buffer_reverse_range:
1398 * @buffer: an #hb_buffer_t.
1399 * @start: start index.
1402 * Reverses buffer contents between start to end.
1407 hb_buffer_reverse_range (hb_buffer_t *buffer,
1408 unsigned int start, unsigned int end)
1410 buffer->reverse_range (start, end);
1414 * hb_buffer_reverse_clusters:
1415 * @buffer: an #hb_buffer_t.
1417 * Reverses buffer clusters. That is, the buffer contents are
1418 * reversed, then each cluster (consecutive items having the
1419 * same cluster number) are reversed again.
1424 hb_buffer_reverse_clusters (hb_buffer_t *buffer)
1426 buffer->reverse_clusters ();
1430 * hb_buffer_guess_segment_properties:
1431 * @buffer: an #hb_buffer_t.
1433 * Sets unset buffer segment properties based on buffer Unicode
1434 * contents. If buffer is not empty, it must have content type
1435 * %HB_BUFFER_CONTENT_TYPE_UNICODE.
1437 * If buffer script is not set (ie. is %HB_SCRIPT_INVALID), it
1438 * will be set to the Unicode script of the first character in
1439 * the buffer that has a script other than %HB_SCRIPT_COMMON,
1440 * %HB_SCRIPT_INHERITED, and %HB_SCRIPT_UNKNOWN.
1442 * Next, if buffer direction is not set (ie. is %HB_DIRECTION_INVALID),
1443 * it will be set to the natural horizontal direction of the
1444 * buffer script as returned by hb_script_get_horizontal_direction().
1446 * Finally, if buffer language is not set (ie. is %HB_LANGUAGE_INVALID),
1447 * it will be set to the process's default language as returned by
1448 * hb_language_get_default(). This may change in the future by
1449 * taking buffer script into consideration when choosing a language.
1454 hb_buffer_guess_segment_properties (hb_buffer_t *buffer)
1456 buffer->guess_segment_properties ();
1459 template <typename utf_t>
1461 hb_buffer_add_utf (hb_buffer_t *buffer,
1462 const typename utf_t::codepoint_t *text,
1464 unsigned int item_offset,
1467 typedef typename utf_t::codepoint_t T;
1468 const hb_codepoint_t replacement = buffer->replacement;
1470 assert (buffer->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE ||
1471 (!buffer->len && buffer->content_type == HB_BUFFER_CONTENT_TYPE_INVALID));
1473 if (unlikely (hb_object_is_inert (buffer)))
1476 if (text_length == -1)
1477 text_length = utf_t::strlen (text);
1479 if (item_length == -1)
1480 item_length = text_length - item_offset;
1482 buffer->ensure (buffer->len + item_length * sizeof (T) / 4);
1484 /* If buffer is empty and pre-context provided, install it.
1485 * This check is written this way, to make sure people can
1486 * provide pre-context in one add_utf() call, then provide
1487 * text in a follow-up call. See:
1489 * https://bugzilla.mozilla.org/show_bug.cgi?id=801410#c13
1491 if (!buffer->len && item_offset > 0)
1493 /* Add pre-context */
1494 buffer->clear_context (0);
1495 const T *prev = text + item_offset;
1496 const T *start = text;
1497 while (start < prev && buffer->context_len[0] < buffer->CONTEXT_LENGTH)
1500 prev = utf_t::prev (prev, start, &u, replacement);
1501 buffer->context[0][buffer->context_len[0]++] = u;
1505 const T *next = text + item_offset;
1506 const T *end = next + item_length;
1510 const T *old_next = next;
1511 next = utf_t::next (next, end, &u, replacement);
1512 buffer->add (u, old_next - (const T *) text);
1515 /* Add post-context */
1516 buffer->clear_context (1);
1517 end = text + text_length;
1518 while (next < end && buffer->context_len[1] < buffer->CONTEXT_LENGTH)
1521 next = utf_t::next (next, end, &u, replacement);
1522 buffer->context[1][buffer->context_len[1]++] = u;
1525 buffer->content_type = HB_BUFFER_CONTENT_TYPE_UNICODE;
1529 * hb_buffer_add_utf8:
1530 * @buffer: an #hb_buffer_t.
1531 * @text: (array length=text_length) (element-type uint8_t): an array of UTF-8
1532 * characters to append.
1533 * @text_length: the length of the @text, or -1 if it is %NULL terminated.
1534 * @item_offset: the offset of the first character to add to the @buffer.
1535 * @item_length: the number of characters to add to the @buffer, or -1 for the
1536 * end of @text (assuming it is %NULL terminated).
1538 * See hb_buffer_add_codepoints().
1540 * Replaces invalid UTF-8 characters with the @buffer replacement code point,
1541 * see hb_buffer_set_replacement_codepoint().
1546 hb_buffer_add_utf8 (hb_buffer_t *buffer,
1549 unsigned int item_offset,
1552 hb_buffer_add_utf<hb_utf8_t> (buffer, (const uint8_t *) text, text_length, item_offset, item_length);
1556 * hb_buffer_add_utf16:
1557 * @buffer: an #hb_buffer_t.
1558 * @text: (array length=text_length): an array of UTF-16 characters to append.
1559 * @text_length: the length of the @text, or -1 if it is %NULL terminated.
1560 * @item_offset: the offset of the first character to add to the @buffer.
1561 * @item_length: the number of characters to add to the @buffer, or -1 for the
1562 * end of @text (assuming it is %NULL terminated).
1564 * See hb_buffer_add_codepoints().
1566 * Replaces invalid UTF-16 characters with the @buffer replacement code point,
1567 * see hb_buffer_set_replacement_codepoint().
1572 hb_buffer_add_utf16 (hb_buffer_t *buffer,
1573 const uint16_t *text,
1575 unsigned int item_offset,
1578 hb_buffer_add_utf<hb_utf16_t> (buffer, text, text_length, item_offset, item_length);
1582 * hb_buffer_add_utf32:
1583 * @buffer: an #hb_buffer_t.
1584 * @text: (array length=text_length): an array of UTF-32 characters to append.
1585 * @text_length: the length of the @text, or -1 if it is %NULL terminated.
1586 * @item_offset: the offset of the first character to add to the @buffer.
1587 * @item_length: the number of characters to add to the @buffer, or -1 for the
1588 * end of @text (assuming it is %NULL terminated).
1590 * See hb_buffer_add_codepoints().
1592 * Replaces invalid UTF-32 characters with the @buffer replacement code point,
1593 * see hb_buffer_set_replacement_codepoint().
1598 hb_buffer_add_utf32 (hb_buffer_t *buffer,
1599 const uint32_t *text,
1601 unsigned int item_offset,
1604 hb_buffer_add_utf<hb_utf32_t<> > (buffer, text, text_length, item_offset, item_length);
1608 * hb_buffer_add_latin1:
1609 * @buffer: an #hb_buffer_t.
1610 * @text: (array length=text_length) (element-type uint8_t): an array of UTF-8
1611 * characters to append.
1612 * @text_length: the length of the @text, or -1 if it is %NULL terminated.
1613 * @item_offset: the offset of the first character to add to the @buffer.
1614 * @item_length: the number of characters to add to the @buffer, or -1 for the
1615 * end of @text (assuming it is %NULL terminated).
1617 * Similar to hb_buffer_add_codepoints(), but allows only access to first 256
1618 * Unicode code points that can fit in 8-bit strings.
1620 * <note>Has nothing to do with non-Unicode Latin-1 encoding.</note>
1625 hb_buffer_add_latin1 (hb_buffer_t *buffer,
1626 const uint8_t *text,
1628 unsigned int item_offset,
1631 hb_buffer_add_utf<hb_latin1_t> (buffer, text, text_length, item_offset, item_length);
1635 * hb_buffer_add_codepoints:
1636 * @buffer: a #hb_buffer_t to append characters to.
1637 * @text: (array length=text_length): an array of Unicode code points to append.
1638 * @text_length: the length of the @text, or -1 if it is %NULL terminated.
1639 * @item_offset: the offset of the first code point to add to the @buffer.
1640 * @item_length: the number of code points to add to the @buffer, or -1 for the
1641 * end of @text (assuming it is %NULL terminated).
1643 * Appends characters from @text array to @buffer. The @item_offset is the
1644 * position of the first character from @text that will be appended, and
1645 * @item_length is the number of character. When shaping part of a larger text
1646 * (e.g. a run of text from a paragraph), instead of passing just the substring
1647 * corresponding to the run, it is preferable to pass the whole
1648 * paragraph and specify the run start and length as @item_offset and
1649 * @item_length, respectively, to give HarfBuzz the full context to be able,
1650 * for example, to do cross-run Arabic shaping or properly handle combining
1651 * marks at stat of run.
1653 * This function does not check the validity of @text, it is up to the caller
1654 * to ensure it contains a valid Unicode code points.
1659 hb_buffer_add_codepoints (hb_buffer_t *buffer,
1660 const hb_codepoint_t *text,
1662 unsigned int item_offset,
1665 hb_buffer_add_utf<hb_utf32_t<false> > (buffer, text, text_length, item_offset, item_length);
1670 compare_info_codepoint (const hb_glyph_info_t *pa,
1671 const hb_glyph_info_t *pb)
1673 return (int) pb->codepoint - (int) pa->codepoint;
1677 normalize_glyphs_cluster (hb_buffer_t *buffer,
1682 hb_glyph_position_t *pos = buffer->pos;
1684 /* Total cluster advance */
1685 hb_position_t total_x_advance = 0, total_y_advance = 0;
1686 for (unsigned int i = start; i < end; i++)
1688 total_x_advance += pos[i].x_advance;
1689 total_y_advance += pos[i].y_advance;
1692 hb_position_t x_advance = 0, y_advance = 0;
1693 for (unsigned int i = start; i < end; i++)
1695 pos[i].x_offset += x_advance;
1696 pos[i].y_offset += y_advance;
1698 x_advance += pos[i].x_advance;
1699 y_advance += pos[i].y_advance;
1701 pos[i].x_advance = 0;
1702 pos[i].y_advance = 0;
1707 /* Transfer all cluster advance to the last glyph. */
1708 pos[end - 1].x_advance = total_x_advance;
1709 pos[end - 1].y_advance = total_y_advance;
1711 hb_stable_sort (buffer->info + start, end - start - 1, compare_info_codepoint, buffer->pos + start);
1713 /* Transfer all cluster advance to the first glyph. */
1714 pos[start].x_advance += total_x_advance;
1715 pos[start].y_advance += total_y_advance;
1716 for (unsigned int i = start + 1; i < end; i++) {
1717 pos[i].x_offset -= total_x_advance;
1718 pos[i].y_offset -= total_y_advance;
1720 hb_stable_sort (buffer->info + start + 1, end - start - 1, compare_info_codepoint, buffer->pos + start + 1);
1725 * hb_buffer_normalize_glyphs:
1726 * @buffer: an #hb_buffer_t.
1728 * Reorders a glyph buffer to have canonical in-cluster glyph order / position.
1729 * The resulting clusters should behave identical to pre-reordering clusters.
1731 * <note>This has nothing to do with Unicode normalization.</note>
1736 hb_buffer_normalize_glyphs (hb_buffer_t *buffer)
1738 assert (buffer->have_positions);
1739 assert (buffer->content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS);
1741 bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
1743 unsigned int count = buffer->len;
1744 if (unlikely (!count)) return;
1745 hb_glyph_info_t *info = buffer->info;
1747 unsigned int start = 0;
1749 for (end = start + 1; end < count; end++)
1750 if (info[start].cluster != info[end].cluster) {
1751 normalize_glyphs_cluster (buffer, start, end, backward);
1754 normalize_glyphs_cluster (buffer, start, end, backward);
1758 hb_buffer_t::sort (unsigned int start, unsigned int end, int(*compar)(const hb_glyph_info_t *, const hb_glyph_info_t *))
1760 assert (!have_positions);
1761 for (unsigned int i = start + 1; i < end; i++)
1764 while (j > start && compar (&info[j - 1], &info[i]) > 0)
1768 /* Move item i to occupy place for item j, shift what's in between. */
1769 merge_clusters (j, i + 1);
1771 hb_glyph_info_t t = info[i];
1772 memmove (&info[j + 1], &info[j], (i - j) * sizeof (hb_glyph_info_t));
1783 * hb_buffer_set_message_func:
1784 * @buffer: an #hb_buffer_t.
1785 * @func: (closure user_data) (destroy destroy) (scope notified):
1794 hb_buffer_set_message_func (hb_buffer_t *buffer,
1795 hb_buffer_message_func_t func,
1796 void *user_data, hb_destroy_func_t destroy)
1798 if (buffer->message_destroy)
1799 buffer->message_destroy (buffer->message_data);
1802 buffer->message_func = func;
1803 buffer->message_data = user_data;
1804 buffer->message_destroy = destroy;
1806 buffer->message_func = NULL;
1807 buffer->message_data = NULL;
1808 buffer->message_destroy = NULL;
1813 hb_buffer_t::message_impl (hb_font_t *font, const char *fmt, va_list ap)
1816 vsnprintf (buf, sizeof (buf), fmt, ap);
1817 return (bool) this->message_func (this, font, buf, this->message_data);