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.
55 * Return value: (transfer full):
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]));
192 hb_buffer_t::scratch_buffer_t *
193 hb_buffer_t::get_scratch_buffer (unsigned int *size)
196 have_positions = false;
201 assert ((uintptr_t) pos % sizeof (scratch_buffer_t) == 0);
202 *size = allocated * sizeof (pos[0]) / sizeof (scratch_buffer_t);
203 return (scratch_buffer_t *) (void *) pos;
208 /* HarfBuzz-Internal API */
211 hb_buffer_t::reset (void)
213 if (unlikely (hb_object_is_inert (this)))
216 hb_unicode_funcs_destroy (unicode);
217 unicode = hb_unicode_funcs_get_default ();
218 flags = HB_BUFFER_FLAG_DEFAULT;
219 replacement = HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT;
225 hb_buffer_t::clear (void)
227 if (unlikely (hb_object_is_inert (this)))
230 hb_segment_properties_t default_props = HB_SEGMENT_PROPERTIES_DEFAULT;
231 props = default_props;
232 scratch_flags = HB_BUFFER_SCRATCH_FLAG_DEFAULT;
234 content_type = HB_BUFFER_CONTENT_TYPE_INVALID;
237 have_positions = false;
246 memset (context, 0, sizeof context);
247 memset (context_len, 0, sizeof context_len);
249 deallocate_var_all ();
253 hb_buffer_t::add (hb_codepoint_t codepoint,
254 unsigned int cluster)
256 hb_glyph_info_t *glyph;
258 if (unlikely (!ensure (len + 1))) return;
262 memset (glyph, 0, sizeof (*glyph));
263 glyph->codepoint = codepoint;
265 glyph->cluster = cluster;
271 hb_buffer_t::add_info (const hb_glyph_info_t &glyph_info)
273 if (unlikely (!ensure (len + 1))) return;
275 info[len] = glyph_info;
282 hb_buffer_t::remove_output (void)
284 if (unlikely (hb_object_is_inert (this)))
288 have_positions = false;
295 hb_buffer_t::clear_output (void)
297 if (unlikely (hb_object_is_inert (this)))
301 have_positions = false;
308 hb_buffer_t::clear_positions (void)
310 if (unlikely (hb_object_is_inert (this)))
314 have_positions = true;
319 memset (pos, 0, sizeof (pos[0]) * len);
323 hb_buffer_t::swap_buffers (void)
325 if (unlikely (in_error)) return;
327 assert (have_output);
330 if (out_info != info)
332 hb_glyph_info_t *tmp_string;
335 out_info = tmp_string;
336 pos = (hb_glyph_position_t *) out_info;
349 hb_buffer_t::replace_glyphs (unsigned int num_in,
350 unsigned int num_out,
351 const uint32_t *glyph_data)
353 if (unlikely (!make_room_for (num_in, num_out))) return;
355 merge_clusters (idx, idx + num_in);
357 hb_glyph_info_t orig_info = info[idx];
358 hb_glyph_info_t *pinfo = &out_info[out_len];
359 for (unsigned int i = 0; i < num_out; i++)
362 pinfo->codepoint = glyph_data[i];
371 hb_buffer_t::output_glyph (hb_codepoint_t glyph_index)
373 if (unlikely (!make_room_for (0, 1))) return;
375 out_info[out_len] = info[idx];
376 out_info[out_len].codepoint = glyph_index;
382 hb_buffer_t::output_info (const hb_glyph_info_t &glyph_info)
384 if (unlikely (!make_room_for (0, 1))) return;
386 out_info[out_len] = glyph_info;
392 hb_buffer_t::copy_glyph (void)
394 if (unlikely (!make_room_for (0, 1))) return;
396 out_info[out_len] = info[idx];
402 hb_buffer_t::move_to (unsigned int i)
410 if (unlikely (in_error))
413 assert (i <= out_len + (len - idx));
417 unsigned int count = i - out_len;
418 if (unlikely (!make_room_for (count, count))) return false;
420 memmove (out_info + out_len, info + idx, count * sizeof (out_info[0]));
424 else if (out_len > i)
426 /* Tricky part: rewinding... */
427 unsigned int count = out_len - i;
429 if (unlikely (idx < count && !shift_forward (count + 32))) return false;
431 assert (idx >= count);
435 memmove (info + idx, out_info + out_len, count * sizeof (out_info[0]));
442 hb_buffer_t::replace_glyph (hb_codepoint_t glyph_index)
444 if (unlikely (out_info != info || out_len != idx)) {
445 if (unlikely (!make_room_for (1, 1))) return;
446 out_info[out_len] = info[idx];
448 out_info[out_len].codepoint = glyph_index;
456 hb_buffer_t::set_masks (hb_mask_t value,
458 unsigned int cluster_start,
459 unsigned int cluster_end)
461 hb_mask_t not_mask = ~mask;
467 if (cluster_start == 0 && cluster_end == (unsigned int)-1) {
468 unsigned int count = len;
469 for (unsigned int i = 0; i < count; i++)
470 info[i].mask = (info[i].mask & not_mask) | value;
474 unsigned int count = len;
475 for (unsigned int i = 0; i < count; i++)
476 if (cluster_start <= info[i].cluster && info[i].cluster < cluster_end)
477 info[i].mask = (info[i].mask & not_mask) | value;
481 hb_buffer_t::reverse_range (unsigned int start,
489 for (i = start, j = end - 1; i < j; i++, j--) {
497 if (have_positions) {
498 for (i = start, j = end - 1; i < j; i++, j--) {
499 hb_glyph_position_t t;
509 hb_buffer_t::reverse (void)
514 reverse_range (0, len);
518 hb_buffer_t::reverse_clusters (void)
520 unsigned int i, start, count, last_cluster;
529 last_cluster = info[0].cluster;
530 for (i = 1; i < count; i++) {
531 if (last_cluster != info[i].cluster) {
532 reverse_range (start, i);
534 last_cluster = info[i].cluster;
537 reverse_range (start, i);
541 hb_buffer_t::merge_clusters_impl (unsigned int start,
544 if (cluster_level == HB_BUFFER_CLUSTER_LEVEL_CHARACTERS)
547 unsigned int cluster = info[start].cluster;
549 for (unsigned int i = start + 1; i < end; i++)
550 cluster = MIN (cluster, info[i].cluster);
553 while (end < len && info[end - 1].cluster == info[end].cluster)
557 while (idx < start && info[start - 1].cluster == info[start].cluster)
560 /* If we hit the start of buffer, continue in out-buffer. */
562 for (unsigned int i = out_len; i && out_info[i - 1].cluster == info[start].cluster; i--)
563 out_info[i - 1].cluster = cluster;
565 for (unsigned int i = start; i < end; i++)
566 info[i].cluster = cluster;
569 hb_buffer_t::merge_out_clusters (unsigned int start,
572 if (cluster_level == HB_BUFFER_CLUSTER_LEVEL_CHARACTERS)
575 if (unlikely (end - start < 2))
578 unsigned int cluster = out_info[start].cluster;
580 for (unsigned int i = start + 1; i < end; i++)
581 cluster = MIN (cluster, out_info[i].cluster);
584 while (start && out_info[start - 1].cluster == out_info[start].cluster)
588 while (end < out_len && out_info[end - 1].cluster == out_info[end].cluster)
591 /* If we hit the end of out-buffer, continue in buffer. */
593 for (unsigned int i = idx; i < len && info[i].cluster == out_info[end - 1].cluster; i++)
594 info[i].cluster = cluster;
596 for (unsigned int i = start; i < end; i++)
597 out_info[i].cluster = cluster;
600 hb_buffer_t::delete_glyph ()
602 unsigned int cluster = info[idx].cluster;
603 if (idx + 1 < len && cluster == info[idx + 1].cluster)
605 /* Cluster survives; do nothing. */
611 /* Merge cluster backward. */
612 if (cluster < out_info[out_len - 1].cluster)
614 unsigned int old_cluster = out_info[out_len - 1].cluster;
615 for (unsigned i = out_len; i && out_info[i - 1].cluster == old_cluster; i--)
616 out_info[i - 1].cluster = cluster;
623 /* Merge cluster forward. */
624 merge_clusters (idx, idx + 2);
633 hb_buffer_t::guess_segment_properties (void)
635 assert (content_type == HB_BUFFER_CONTENT_TYPE_UNICODE ||
636 (!len && content_type == HB_BUFFER_CONTENT_TYPE_INVALID));
638 /* If script is set to INVALID, guess from buffer contents */
639 if (props.script == HB_SCRIPT_INVALID) {
640 for (unsigned int i = 0; i < len; i++) {
641 hb_script_t script = unicode->script (info[i].codepoint);
642 if (likely (script != HB_SCRIPT_COMMON &&
643 script != HB_SCRIPT_INHERITED &&
644 script != HB_SCRIPT_UNKNOWN)) {
645 props.script = script;
651 /* If direction is set to INVALID, guess from script */
652 if (props.direction == HB_DIRECTION_INVALID) {
653 props.direction = hb_script_get_horizontal_direction (props.script);
656 /* If language is not set, use default language from locale */
657 if (props.language == HB_LANGUAGE_INVALID) {
658 /* TODO get_default_for_script? using $LANGUAGE */
659 props.language = hb_language_get_default ();
667 * hb_buffer_create: (Xconstructor)
669 * Creates a new #hb_buffer_t with all properties to defaults.
671 * Return value: (transfer full):
672 * A newly allocated #hb_buffer_t with a reference count of 1. The initial
673 * reference count should be released with hb_buffer_destroy() when you are done
674 * using the #hb_buffer_t. This function never returns %NULL. If memory cannot
675 * be allocated, a special #hb_buffer_t object will be returned on which
676 * hb_buffer_allocation_successful() returns %false.
681 hb_buffer_create (void)
685 if (!(buffer = hb_object_create<hb_buffer_t> ()))
686 return hb_buffer_get_empty ();
688 buffer->max_len = HB_BUFFER_MAX_LEN_DEFAULT;
696 * hb_buffer_get_empty:
700 * Return value: (transfer full):
705 hb_buffer_get_empty (void)
707 static const hb_buffer_t _hb_buffer_nil = {
708 HB_OBJECT_HEADER_STATIC,
710 const_cast<hb_unicode_funcs_t *> (&_hb_unicode_funcs_nil),
711 HB_BUFFER_FLAG_DEFAULT,
712 HB_BUFFER_CLUSTER_LEVEL_DEFAULT,
713 HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT,
714 HB_BUFFER_SCRATCH_FLAG_DEFAULT,
715 HB_BUFFER_MAX_LEN_DEFAULT,
717 HB_BUFFER_CONTENT_TYPE_INVALID,
718 HB_SEGMENT_PROPERTIES_DEFAULT,
720 true, /* have_output */
721 true /* have_positions */
723 /* Zero is good enough for everything else. */
726 return const_cast<hb_buffer_t *> (&_hb_buffer_nil);
730 * hb_buffer_reference: (skip)
731 * @buffer: an #hb_buffer_t.
733 * Increases the reference count on @buffer by one. This prevents @buffer from
734 * being destroyed until a matching call to hb_buffer_destroy() is made.
736 * Return value: (transfer full):
737 * The referenced #hb_buffer_t.
742 hb_buffer_reference (hb_buffer_t *buffer)
744 return hb_object_reference (buffer);
748 * hb_buffer_destroy: (skip)
749 * @buffer: an #hb_buffer_t.
751 * Deallocate the @buffer.
752 * Decreases the reference count on @buffer by one. If the result is zero, then
753 * @buffer and all associated resources are freed. See hb_buffer_reference().
758 hb_buffer_destroy (hb_buffer_t *buffer)
760 if (!hb_object_destroy (buffer)) return;
762 hb_unicode_funcs_destroy (buffer->unicode);
766 if (buffer->message_destroy)
767 buffer->message_destroy (buffer->message_data);
773 * hb_buffer_set_user_data: (skip)
774 * @buffer: an #hb_buffer_t.
787 hb_buffer_set_user_data (hb_buffer_t *buffer,
788 hb_user_data_key_t *key,
790 hb_destroy_func_t destroy,
793 return hb_object_set_user_data (buffer, key, data, destroy, replace);
797 * hb_buffer_get_user_data: (skip)
798 * @buffer: an #hb_buffer_t.
808 hb_buffer_get_user_data (hb_buffer_t *buffer,
809 hb_user_data_key_t *key)
811 return hb_object_get_user_data (buffer, key);
816 * hb_buffer_set_content_type:
817 * @buffer: an #hb_buffer_t.
818 * @content_type: the type of buffer contents to set
820 * Sets the type of @buffer contents, buffers are either empty, contain
821 * characters (before shaping) or glyphs (the result of shaping).
826 hb_buffer_set_content_type (hb_buffer_t *buffer,
827 hb_buffer_content_type_t content_type)
829 buffer->content_type = content_type;
833 * hb_buffer_get_content_type:
834 * @buffer: an #hb_buffer_t.
836 * see hb_buffer_set_content_type().
839 * The type of @buffer contents.
843 hb_buffer_content_type_t
844 hb_buffer_get_content_type (hb_buffer_t *buffer)
846 return buffer->content_type;
851 * hb_buffer_set_unicode_funcs:
852 * @buffer: an #hb_buffer_t.
860 hb_buffer_set_unicode_funcs (hb_buffer_t *buffer,
861 hb_unicode_funcs_t *unicode_funcs)
863 if (unlikely (hb_object_is_inert (buffer)))
867 unicode_funcs = hb_unicode_funcs_get_default ();
870 hb_unicode_funcs_reference (unicode_funcs);
871 hb_unicode_funcs_destroy (buffer->unicode);
872 buffer->unicode = unicode_funcs;
876 * hb_buffer_get_unicode_funcs:
877 * @buffer: an #hb_buffer_t.
886 hb_buffer_get_unicode_funcs (hb_buffer_t *buffer)
888 return buffer->unicode;
892 * hb_buffer_set_direction:
893 * @buffer: an #hb_buffer_t.
894 * @direction: the #hb_direction_t of the @buffer
896 * Set the text flow direction of the buffer. No shaping can happen without
897 * setting @buffer direction, and it controls the visual direction for the
898 * output glyphs; for RTL direction the glyphs will be reversed. Many layout
899 * features depend on the proper setting of the direction, for example,
900 * reversing RTL text before shaping, then shaping with LTR direction is not
901 * the same as keeping the text in logical order and shaping with RTL
907 hb_buffer_set_direction (hb_buffer_t *buffer,
908 hb_direction_t direction)
911 if (unlikely (hb_object_is_inert (buffer)))
914 buffer->props.direction = direction;
918 * hb_buffer_get_direction:
919 * @buffer: an #hb_buffer_t.
921 * See hb_buffer_set_direction()
924 * The direction of the @buffer.
929 hb_buffer_get_direction (hb_buffer_t *buffer)
931 return buffer->props.direction;
935 * hb_buffer_set_script:
936 * @buffer: an #hb_buffer_t.
937 * @script: an #hb_script_t to set.
939 * Sets the script of @buffer to @script.
941 * Script is crucial for choosing the proper shaping behaviour for scripts that
942 * require it (e.g. Arabic) and the which OpenType features defined in the font
945 * You can pass one of the predefined #hb_script_t values, or use
946 * hb_script_from_string() or hb_script_from_iso15924_tag() to get the
947 * corresponding script from an ISO 15924 script tag.
952 hb_buffer_set_script (hb_buffer_t *buffer,
955 if (unlikely (hb_object_is_inert (buffer)))
958 buffer->props.script = script;
962 * hb_buffer_get_script:
963 * @buffer: an #hb_buffer_t.
965 * See hb_buffer_set_script().
968 * The #hb_script_t of the @buffer.
973 hb_buffer_get_script (hb_buffer_t *buffer)
975 return buffer->props.script;
979 * hb_buffer_set_language:
980 * @buffer: an #hb_buffer_t.
981 * @language: an hb_language_t to set.
983 * Sets the language of @buffer to @language.
985 * Languages are crucial for selecting which OpenType feature to apply to the
986 * buffer which can result in applying language-specific behaviour. Languages
987 * are orthogonal to the scripts, and though they are related, they are
988 * different concepts and should not be confused with each other.
990 * Use hb_language_from_string() to convert from ISO 639 language codes to
996 hb_buffer_set_language (hb_buffer_t *buffer,
997 hb_language_t language)
999 if (unlikely (hb_object_is_inert (buffer)))
1002 buffer->props.language = language;
1006 * hb_buffer_get_language:
1007 * @buffer: an #hb_buffer_t.
1009 * See hb_buffer_set_language().
1011 * Return value: (transfer none):
1012 * The #hb_language_t of the buffer. Must not be freed by the caller.
1017 hb_buffer_get_language (hb_buffer_t *buffer)
1019 return buffer->props.language;
1023 * hb_buffer_set_segment_properties:
1024 * @buffer: an #hb_buffer_t.
1025 * @props: an #hb_segment_properties_t to use.
1027 * Sets the segment properties of the buffer, a shortcut for calling
1028 * hb_buffer_set_direction(), hb_buffer_set_script() and
1029 * hb_buffer_set_language() individually.
1034 hb_buffer_set_segment_properties (hb_buffer_t *buffer,
1035 const hb_segment_properties_t *props)
1037 if (unlikely (hb_object_is_inert (buffer)))
1040 buffer->props = *props;
1044 * hb_buffer_get_segment_properties:
1045 * @buffer: an #hb_buffer_t.
1046 * @props: (out): the output #hb_segment_properties_t.
1048 * Sets @props to the #hb_segment_properties_t of @buffer.
1053 hb_buffer_get_segment_properties (hb_buffer_t *buffer,
1054 hb_segment_properties_t *props)
1056 *props = buffer->props;
1061 * hb_buffer_set_flags:
1062 * @buffer: an #hb_buffer_t.
1063 * @flags: the buffer flags to set.
1065 * Sets @buffer flags to @flags. See #hb_buffer_flags_t.
1070 hb_buffer_set_flags (hb_buffer_t *buffer,
1071 hb_buffer_flags_t flags)
1073 if (unlikely (hb_object_is_inert (buffer)))
1076 buffer->flags = flags;
1080 * hb_buffer_get_flags:
1081 * @buffer: an #hb_buffer_t.
1083 * See hb_buffer_set_flags().
1086 * The @buffer flags.
1091 hb_buffer_get_flags (hb_buffer_t *buffer)
1093 return buffer->flags;
1097 * hb_buffer_set_cluster_level:
1098 * @buffer: an #hb_buffer_t.
1106 hb_buffer_set_cluster_level (hb_buffer_t *buffer,
1107 hb_buffer_cluster_level_t cluster_level)
1109 if (unlikely (hb_object_is_inert (buffer)))
1112 buffer->cluster_level = cluster_level;
1116 * hb_buffer_get_cluster_level:
1117 * @buffer: an #hb_buffer_t.
1125 hb_buffer_cluster_level_t
1126 hb_buffer_get_cluster_level (hb_buffer_t *buffer)
1128 return buffer->cluster_level;
1133 * hb_buffer_set_replacement_codepoint:
1134 * @buffer: an #hb_buffer_t.
1135 * @replacement: the replacement #hb_codepoint_t
1137 * Sets the #hb_codepoint_t that replaces invalid entries for a given encoding
1138 * when adding text to @buffer.
1140 * Default is %HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT.
1145 hb_buffer_set_replacement_codepoint (hb_buffer_t *buffer,
1146 hb_codepoint_t replacement)
1148 if (unlikely (hb_object_is_inert (buffer)))
1151 buffer->replacement = replacement;
1155 * hb_buffer_get_replacement_codepoint:
1156 * @buffer: an #hb_buffer_t.
1158 * See hb_buffer_set_replacement_codepoint().
1161 * The @buffer replacement #hb_codepoint_t.
1166 hb_buffer_get_replacement_codepoint (hb_buffer_t *buffer)
1168 return buffer->replacement;
1174 * @buffer: an #hb_buffer_t.
1176 * Resets the buffer to its initial status, as if it was just newly created
1177 * with hb_buffer_create().
1182 hb_buffer_reset (hb_buffer_t *buffer)
1188 * hb_buffer_clear_contents:
1189 * @buffer: an #hb_buffer_t.
1191 * Similar to hb_buffer_reset(), but does not clear the Unicode functions and
1192 * the replacement code point.
1197 hb_buffer_clear_contents (hb_buffer_t *buffer)
1203 * hb_buffer_pre_allocate:
1204 * @buffer: an #hb_buffer_t.
1205 * @size: number of items to pre allocate.
1207 * Pre allocates memory for @buffer to fit at least @size number of items.
1210 * %true if @buffer memory allocation succeeded, %false otherwise.
1215 hb_buffer_pre_allocate (hb_buffer_t *buffer, unsigned int size)
1217 return buffer->ensure (size);
1221 * hb_buffer_allocation_successful:
1222 * @buffer: an #hb_buffer_t.
1224 * Check if allocating memory for the buffer succeeded.
1227 * %true if @buffer memory allocation succeeded, %false otherwise.
1232 hb_buffer_allocation_successful (hb_buffer_t *buffer)
1234 return !buffer->in_error;
1239 * @buffer: an #hb_buffer_t.
1240 * @codepoint: a Unicode code point.
1241 * @cluster: the cluster value of @codepoint.
1243 * Appends a character with the Unicode value of @codepoint to @buffer, and
1244 * gives it the initial cluster value of @cluster. Clusters can be any thing
1245 * the client wants, they are usually used to refer to the index of the
1246 * character in the input text stream and are output in
1247 * #hb_glyph_info_t.cluster field.
1249 * This function does not check the validity of @codepoint, it is up to the
1250 * caller to ensure it is a valid Unicode code point.
1255 hb_buffer_add (hb_buffer_t *buffer,
1256 hb_codepoint_t codepoint,
1257 unsigned int cluster)
1259 buffer->add (codepoint, cluster);
1260 buffer->clear_context (1);
1264 * hb_buffer_set_length:
1265 * @buffer: an #hb_buffer_t.
1266 * @length: the new length of @buffer.
1268 * Similar to hb_buffer_pre_allocate(), but clears any new items added at the
1272 * %true if @buffer memory allocation succeeded, %false otherwise.
1277 hb_buffer_set_length (hb_buffer_t *buffer,
1278 unsigned int length)
1280 if (unlikely (hb_object_is_inert (buffer)))
1283 if (!buffer->ensure (length))
1286 /* Wipe the new space */
1287 if (length > buffer->len) {
1288 memset (buffer->info + buffer->len, 0, sizeof (buffer->info[0]) * (length - buffer->len));
1289 if (buffer->have_positions)
1290 memset (buffer->pos + buffer->len, 0, sizeof (buffer->pos[0]) * (length - buffer->len));
1293 buffer->len = length;
1297 buffer->content_type = HB_BUFFER_CONTENT_TYPE_INVALID;
1298 buffer->clear_context (0);
1300 buffer->clear_context (1);
1306 * hb_buffer_get_length:
1307 * @buffer: an #hb_buffer_t.
1309 * Returns the number of items in the buffer.
1312 * The @buffer length.
1313 * The value valid as long as buffer has not been modified.
1318 hb_buffer_get_length (hb_buffer_t *buffer)
1324 * hb_buffer_get_glyph_infos:
1325 * @buffer: an #hb_buffer_t.
1326 * @length: (out): output array length.
1328 * Returns @buffer glyph information array. Returned pointer
1329 * is valid as long as @buffer contents are not modified.
1331 * Return value: (transfer none) (array length=length):
1332 * The @buffer glyph information array.
1333 * The value valid as long as buffer has not been modified.
1338 hb_buffer_get_glyph_infos (hb_buffer_t *buffer,
1339 unsigned int *length)
1342 *length = buffer->len;
1344 return (hb_glyph_info_t *) buffer->info;
1348 * hb_buffer_get_glyph_positions:
1349 * @buffer: an #hb_buffer_t.
1350 * @length: (out): output length.
1352 * Returns @buffer glyph position array. Returned pointer
1353 * is valid as long as @buffer contents are not modified.
1355 * Return value: (transfer none) (array length=length):
1356 * The @buffer glyph position array.
1357 * The value valid as long as buffer has not been modified.
1361 hb_glyph_position_t *
1362 hb_buffer_get_glyph_positions (hb_buffer_t *buffer,
1363 unsigned int *length)
1365 if (!buffer->have_positions)
1366 buffer->clear_positions ();
1369 *length = buffer->len;
1371 return (hb_glyph_position_t *) buffer->pos;
1375 * hb_buffer_reverse:
1376 * @buffer: an #hb_buffer_t.
1378 * Reverses buffer contents.
1383 hb_buffer_reverse (hb_buffer_t *buffer)
1389 * hb_buffer_reverse_range:
1390 * @buffer: an #hb_buffer_t.
1391 * @start: start index.
1394 * Reverses buffer contents between start to end.
1399 hb_buffer_reverse_range (hb_buffer_t *buffer,
1400 unsigned int start, unsigned int end)
1402 buffer->reverse_range (start, end);
1406 * hb_buffer_reverse_clusters:
1407 * @buffer: an #hb_buffer_t.
1409 * Reverses buffer clusters. That is, the buffer contents are
1410 * reversed, then each cluster (consecutive items having the
1411 * same cluster number) are reversed again.
1416 hb_buffer_reverse_clusters (hb_buffer_t *buffer)
1418 buffer->reverse_clusters ();
1422 * hb_buffer_guess_segment_properties:
1423 * @buffer: an #hb_buffer_t.
1425 * Sets unset buffer segment properties based on buffer Unicode
1426 * contents. If buffer is not empty, it must have content type
1427 * %HB_BUFFER_CONTENT_TYPE_UNICODE.
1429 * If buffer script is not set (ie. is %HB_SCRIPT_INVALID), it
1430 * will be set to the Unicode script of the first character in
1431 * the buffer that has a script other than %HB_SCRIPT_COMMON,
1432 * %HB_SCRIPT_INHERITED, and %HB_SCRIPT_UNKNOWN.
1434 * Next, if buffer direction is not set (ie. is %HB_DIRECTION_INVALID),
1435 * it will be set to the natural horizontal direction of the
1436 * buffer script as returned by hb_script_get_horizontal_direction().
1438 * Finally, if buffer language is not set (ie. is %HB_LANGUAGE_INVALID),
1439 * it will be set to the process's default language as returned by
1440 * hb_language_get_default(). This may change in the future by
1441 * taking buffer script into consideration when choosing a language.
1446 hb_buffer_guess_segment_properties (hb_buffer_t *buffer)
1448 buffer->guess_segment_properties ();
1451 template <typename utf_t>
1453 hb_buffer_add_utf (hb_buffer_t *buffer,
1454 const typename utf_t::codepoint_t *text,
1456 unsigned int item_offset,
1459 typedef typename utf_t::codepoint_t T;
1460 const hb_codepoint_t replacement = buffer->replacement;
1462 assert (buffer->content_type == HB_BUFFER_CONTENT_TYPE_UNICODE ||
1463 (!buffer->len && buffer->content_type == HB_BUFFER_CONTENT_TYPE_INVALID));
1465 if (unlikely (hb_object_is_inert (buffer)))
1468 if (text_length == -1)
1469 text_length = utf_t::strlen (text);
1471 if (item_length == -1)
1472 item_length = text_length - item_offset;
1474 buffer->ensure (buffer->len + item_length * sizeof (T) / 4);
1476 /* If buffer is empty and pre-context provided, install it.
1477 * This check is written this way, to make sure people can
1478 * provide pre-context in one add_utf() call, then provide
1479 * text in a follow-up call. See:
1481 * https://bugzilla.mozilla.org/show_bug.cgi?id=801410#c13
1483 if (!buffer->len && item_offset > 0)
1485 /* Add pre-context */
1486 buffer->clear_context (0);
1487 const T *prev = text + item_offset;
1488 const T *start = text;
1489 while (start < prev && buffer->context_len[0] < buffer->CONTEXT_LENGTH)
1492 prev = utf_t::prev (prev, start, &u, replacement);
1493 buffer->context[0][buffer->context_len[0]++] = u;
1497 const T *next = text + item_offset;
1498 const T *end = next + item_length;
1502 const T *old_next = next;
1503 next = utf_t::next (next, end, &u, replacement);
1504 buffer->add (u, old_next - (const T *) text);
1507 /* Add post-context */
1508 buffer->clear_context (1);
1509 end = text + text_length;
1510 while (next < end && buffer->context_len[1] < buffer->CONTEXT_LENGTH)
1513 next = utf_t::next (next, end, &u, replacement);
1514 buffer->context[1][buffer->context_len[1]++] = u;
1517 buffer->content_type = HB_BUFFER_CONTENT_TYPE_UNICODE;
1521 * hb_buffer_add_utf8:
1522 * @buffer: an #hb_buffer_t.
1523 * @text: (array length=text_length) (element-type uint8_t): an array of UTF-8
1524 * characters to append.
1525 * @text_length: the length of the @text, or -1 if it is %NULL terminated.
1526 * @item_offset: the offset of the first character to add to the @buffer.
1527 * @item_length: the number of characters to add to the @buffer, or -1 for the
1528 * end of @text (assuming it is %NULL terminated).
1530 * See hb_buffer_add_codepoints().
1532 * Replaces invalid UTF-8 characters with the @buffer replacement code point,
1533 * see hb_buffer_set_replacement_codepoint().
1538 hb_buffer_add_utf8 (hb_buffer_t *buffer,
1541 unsigned int item_offset,
1544 hb_buffer_add_utf<hb_utf8_t> (buffer, (const uint8_t *) text, text_length, item_offset, item_length);
1548 * hb_buffer_add_utf16:
1549 * @buffer: an #hb_buffer_t.
1550 * @text: (array length=text_length): an array of UTF-16 characters to append.
1551 * @text_length: the length of the @text, or -1 if it is %NULL terminated.
1552 * @item_offset: the offset of the first character to add to the @buffer.
1553 * @item_length: the number of characters to add to the @buffer, or -1 for the
1554 * end of @text (assuming it is %NULL terminated).
1556 * See hb_buffer_add_codepoints().
1558 * Replaces invalid UTF-16 characters with the @buffer replacement code point,
1559 * see hb_buffer_set_replacement_codepoint().
1564 hb_buffer_add_utf16 (hb_buffer_t *buffer,
1565 const uint16_t *text,
1567 unsigned int item_offset,
1570 hb_buffer_add_utf<hb_utf16_t> (buffer, text, text_length, item_offset, item_length);
1574 * hb_buffer_add_utf32:
1575 * @buffer: an #hb_buffer_t.
1576 * @text: (array length=text_length): an array of UTF-32 characters to append.
1577 * @text_length: the length of the @text, or -1 if it is %NULL terminated.
1578 * @item_offset: the offset of the first character to add to the @buffer.
1579 * @item_length: the number of characters to add to the @buffer, or -1 for the
1580 * end of @text (assuming it is %NULL terminated).
1582 * See hb_buffer_add_codepoints().
1584 * Replaces invalid UTF-32 characters with the @buffer replacement code point,
1585 * see hb_buffer_set_replacement_codepoint().
1590 hb_buffer_add_utf32 (hb_buffer_t *buffer,
1591 const uint32_t *text,
1593 unsigned int item_offset,
1596 hb_buffer_add_utf<hb_utf32_t<> > (buffer, text, text_length, item_offset, item_length);
1600 * hb_buffer_add_latin1:
1601 * @buffer: an #hb_buffer_t.
1602 * @text: (array length=text_length) (element-type uint8_t): an array of UTF-8
1603 * characters to append.
1604 * @text_length: the length of the @text, or -1 if it is %NULL terminated.
1605 * @item_offset: the offset of the first character to add to the @buffer.
1606 * @item_length: the number of characters to add to the @buffer, or -1 for the
1607 * end of @text (assuming it is %NULL terminated).
1609 * Similar to hb_buffer_add_codepoints(), but allows only access to first 256
1610 * Unicode code points that can fit in 8-bit strings.
1612 * <note>Has nothing to do with non-Unicode Latin-1 encoding.</note>
1617 hb_buffer_add_latin1 (hb_buffer_t *buffer,
1618 const uint8_t *text,
1620 unsigned int item_offset,
1623 hb_buffer_add_utf<hb_latin1_t> (buffer, text, text_length, item_offset, item_length);
1627 * hb_buffer_add_codepoints:
1628 * @buffer: a #hb_buffer_t to append characters to.
1629 * @text: (array length=text_length): an array of Unicode code points to append.
1630 * @text_length: the length of the @text, or -1 if it is %NULL terminated.
1631 * @item_offset: the offset of the first code point to add to the @buffer.
1632 * @item_length: the number of code points to add to the @buffer, or -1 for the
1633 * end of @text (assuming it is %NULL terminated).
1635 * Appends characters from @text array to @buffer. The @item_offset is the
1636 * position of the first character from @text that will be appended, and
1637 * @item_length is the number of character. When shaping part of a larger text
1638 * (e.g. a run of text from a paragraph), instead of passing just the substring
1639 * corresponding to the run, it is preferable to pass the whole
1640 * paragraph and specify the run start and length as @item_offset and
1641 * @item_length, respectively, to give HarfBuzz the full context to be able,
1642 * for example, to do cross-run Arabic shaping or properly handle combining
1643 * marks at stat of run.
1645 * This function does not check the validity of @text, it is up to the caller
1646 * to ensure it contains a valid Unicode code points.
1651 hb_buffer_add_codepoints (hb_buffer_t *buffer,
1652 const hb_codepoint_t *text,
1654 unsigned int item_offset,
1657 hb_buffer_add_utf<hb_utf32_t<false> > (buffer, text, text_length, item_offset, item_length);
1662 compare_info_codepoint (const hb_glyph_info_t *pa,
1663 const hb_glyph_info_t *pb)
1665 return (int) pb->codepoint - (int) pa->codepoint;
1669 normalize_glyphs_cluster (hb_buffer_t *buffer,
1674 hb_glyph_position_t *pos = buffer->pos;
1676 /* Total cluster advance */
1677 hb_position_t total_x_advance = 0, total_y_advance = 0;
1678 for (unsigned int i = start; i < end; i++)
1680 total_x_advance += pos[i].x_advance;
1681 total_y_advance += pos[i].y_advance;
1684 hb_position_t x_advance = 0, y_advance = 0;
1685 for (unsigned int i = start; i < end; i++)
1687 pos[i].x_offset += x_advance;
1688 pos[i].y_offset += y_advance;
1690 x_advance += pos[i].x_advance;
1691 y_advance += pos[i].y_advance;
1693 pos[i].x_advance = 0;
1694 pos[i].y_advance = 0;
1699 /* Transfer all cluster advance to the last glyph. */
1700 pos[end - 1].x_advance = total_x_advance;
1701 pos[end - 1].y_advance = total_y_advance;
1703 hb_stable_sort (buffer->info + start, end - start - 1, compare_info_codepoint, buffer->pos + start);
1705 /* Transfer all cluster advance to the first glyph. */
1706 pos[start].x_advance += total_x_advance;
1707 pos[start].y_advance += total_y_advance;
1708 for (unsigned int i = start + 1; i < end; i++) {
1709 pos[i].x_offset -= total_x_advance;
1710 pos[i].y_offset -= total_y_advance;
1712 hb_stable_sort (buffer->info + start + 1, end - start - 1, compare_info_codepoint, buffer->pos + start + 1);
1717 * hb_buffer_normalize_glyphs:
1718 * @buffer: an #hb_buffer_t.
1720 * Reorders a glyph buffer to have canonical in-cluster glyph order / position.
1721 * The resulting clusters should behave identical to pre-reordering clusters.
1723 * <note>This has nothing to do with Unicode normalization.</note>
1728 hb_buffer_normalize_glyphs (hb_buffer_t *buffer)
1730 assert (buffer->have_positions);
1731 assert (buffer->content_type == HB_BUFFER_CONTENT_TYPE_GLYPHS);
1733 bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
1735 unsigned int count = buffer->len;
1736 if (unlikely (!count)) return;
1737 hb_glyph_info_t *info = buffer->info;
1739 unsigned int start = 0;
1741 for (end = start + 1; end < count; end++)
1742 if (info[start].cluster != info[end].cluster) {
1743 normalize_glyphs_cluster (buffer, start, end, backward);
1746 normalize_glyphs_cluster (buffer, start, end, backward);
1750 hb_buffer_t::sort (unsigned int start, unsigned int end, int(*compar)(const hb_glyph_info_t *, const hb_glyph_info_t *))
1752 assert (!have_positions);
1753 for (unsigned int i = start + 1; i < end; i++)
1756 while (j > start && compar (&info[j - 1], &info[i]) > 0)
1760 /* Move item i to occupy place for item j, shift what's in between. */
1761 merge_clusters (j, i + 1);
1763 hb_glyph_info_t t = info[i];
1764 memmove (&info[j + 1], &info[j], (i - j) * sizeof (hb_glyph_info_t));
1775 * hb_buffer_set_message_func:
1776 * @buffer: an #hb_buffer_t.
1777 * @func: (closure user_data) (destroy destroy) (scope notified):
1786 hb_buffer_set_message_func (hb_buffer_t *buffer,
1787 hb_buffer_message_func_t func,
1788 void *user_data, hb_destroy_func_t destroy)
1790 if (buffer->message_destroy)
1791 buffer->message_destroy (buffer->message_data);
1794 buffer->message_func = func;
1795 buffer->message_data = user_data;
1796 buffer->message_destroy = destroy;
1798 buffer->message_func = NULL;
1799 buffer->message_data = NULL;
1800 buffer->message_destroy = NULL;
1805 hb_buffer_t::message_impl (hb_font_t *font, const char *fmt, va_list ap)
1808 vsnprintf (buf, sizeof (buf), fmt, ap);
1809 return (bool) this->message_func (this, font, buf, this->message_data);