2 * Copyright © 1998-2004 David Turner and Werner Lemberg
3 * Copyright © 2004,2007,2009,2010 Red Hat, Inc.
4 * Copyright © 2011 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"
36 #ifndef HB_DEBUG_BUFFER
37 #define HB_DEBUG_BUFFER (HB_DEBUG+0)
40 /* Here is how the buffer works internally:
42 * There are two info pointers: info and out_info. They always have
43 * the same allocated size, but different lengths.
45 * As an optimization, both info and out_info may point to the
46 * same piece of memory, which is owned by info. This remains the
47 * case as long as out_len doesn't exceed i at any time.
48 * In that case, swap_buffers() is no-op and the glyph operations operate
51 * As soon as out_info gets longer than info, out_info is moved over
52 * to an alternate buffer (which we reuse the pos buffer for!), and its
53 * current contents (out_len entries) are copied to the new place.
54 * This should all remain transparent to the user. swap_buffers() then
55 * switches info and out_info.
63 hb_buffer_t::enlarge (unsigned int size)
65 if (unlikely (in_error))
68 unsigned int new_allocated = allocated;
69 hb_glyph_position_t *new_pos = NULL;
70 hb_glyph_info_t *new_info = NULL;
71 bool separate_out = out_info != info;
73 if (unlikely (_hb_unsigned_int_mul_overflows (size, sizeof (info[0]))))
76 while (size > new_allocated)
77 new_allocated += (new_allocated >> 1) + 32;
79 ASSERT_STATIC (sizeof (info[0]) == sizeof (pos[0]));
80 if (unlikely (_hb_unsigned_int_mul_overflows (new_allocated, sizeof (info[0]))))
83 new_pos = (hb_glyph_position_t *) realloc (pos, new_allocated * sizeof (pos[0]));
84 new_info = (hb_glyph_info_t *) realloc (info, new_allocated * sizeof (info[0]));
87 if (unlikely (!new_pos || !new_info))
93 if (likely (new_info))
96 out_info = separate_out ? (hb_glyph_info_t *) pos : info;
97 if (likely (!in_error))
98 allocated = new_allocated;
100 return likely (!in_error);
104 hb_buffer_t::make_room_for (unsigned int num_in,
105 unsigned int num_out)
107 if (unlikely (!ensure (out_len + num_out))) return false;
109 if (out_info == info &&
110 out_len + num_out > idx + num_in)
112 assert (have_output);
114 out_info = (hb_glyph_info_t *) pos;
115 memcpy (out_info, info, out_len * sizeof (out_info[0]));
122 hb_buffer_t::get_scratch_buffer (unsigned int *size)
125 have_positions = false;
130 *size = allocated * sizeof (pos[0]);
136 /* HarfBuzz-Internal API */
139 hb_buffer_t::reset (void)
141 if (unlikely (hb_object_is_inert (this)))
144 hb_unicode_funcs_destroy (unicode);
145 unicode = hb_unicode_funcs_get_default ();
147 hb_segment_properties_t default_props = _HB_BUFFER_PROPS_DEFAULT;
148 props = default_props;
152 have_positions = false;
160 memset (allocated_var_bytes, 0, sizeof allocated_var_bytes);
161 memset (allocated_var_owner, 0, sizeof allocated_var_owner);
165 hb_buffer_t::add (hb_codepoint_t codepoint,
167 unsigned int cluster)
169 hb_glyph_info_t *glyph;
171 if (unlikely (!ensure (len + 1))) return;
175 memset (glyph, 0, sizeof (*glyph));
176 glyph->codepoint = codepoint;
178 glyph->cluster = cluster;
184 hb_buffer_t::clear_output (void)
186 if (unlikely (hb_object_is_inert (this)))
190 have_positions = false;
197 hb_buffer_t::clear_positions (void)
199 if (unlikely (hb_object_is_inert (this)))
203 have_positions = true;
208 memset (pos, 0, sizeof (pos[0]) * len);
212 hb_buffer_t::swap_buffers (void)
214 if (unlikely (in_error)) return;
216 assert (have_output);
219 if (out_info != info)
221 hb_glyph_info_t *tmp_string;
224 out_info = tmp_string;
225 pos = (hb_glyph_position_t *) out_info;
238 hb_buffer_t::replace_glyphs (unsigned int num_in,
239 unsigned int num_out,
240 const uint32_t *glyph_data)
242 if (unlikely (!make_room_for (num_in, num_out))) return;
244 merge_clusters (idx, idx + num_in);
246 hb_glyph_info_t orig_info = info[idx];
247 hb_glyph_info_t *pinfo = &out_info[out_len];
248 for (unsigned int i = 0; i < num_out; i++)
251 pinfo->codepoint = glyph_data[i];
260 hb_buffer_t::output_glyph (hb_codepoint_t glyph_index)
262 if (unlikely (!make_room_for (0, 1))) return;
264 out_info[out_len] = info[idx];
265 out_info[out_len].codepoint = glyph_index;
271 hb_buffer_t::copy_glyph (void)
273 if (unlikely (!make_room_for (0, 1))) return;
275 out_info[out_len] = info[idx];
281 hb_buffer_t::replace_glyph (hb_codepoint_t glyph_index)
283 if (unlikely (out_info != info || out_len != idx)) {
284 if (unlikely (!make_room_for (1, 1))) return;
285 out_info[out_len] = info[idx];
287 out_info[out_len].codepoint = glyph_index;
294 hb_buffer_t::next_glyph (void)
298 if (unlikely (out_info != info || out_len != idx)) {
299 if (unlikely (!make_room_for (1, 1))) return;
300 out_info[out_len] = info[idx];
310 hb_buffer_t::set_masks (hb_mask_t value,
312 unsigned int cluster_start,
313 unsigned int cluster_end)
315 hb_mask_t not_mask = ~mask;
321 if (cluster_start == 0 && cluster_end == (unsigned int)-1) {
322 unsigned int count = len;
323 for (unsigned int i = 0; i < count; i++)
324 info[i].mask = (info[i].mask & not_mask) | value;
328 unsigned int count = len;
329 for (unsigned int i = 0; i < count; i++)
330 if (cluster_start <= info[i].cluster && info[i].cluster < cluster_end)
331 info[i].mask = (info[i].mask & not_mask) | value;
335 hb_buffer_t::reverse_range (unsigned int start,
340 if (start == end - 1)
343 for (i = start, j = end - 1; i < j; i++, j--) {
352 for (i = start, j = end - 1; i < j; i++, j--) {
353 hb_glyph_position_t t;
363 hb_buffer_t::reverse (void)
368 reverse_range (0, len);
372 hb_buffer_t::reverse_clusters (void)
374 unsigned int i, start, count, last_cluster;
383 last_cluster = info[0].cluster;
384 for (i = 1; i < count; i++) {
385 if (last_cluster != info[i].cluster) {
386 reverse_range (start, i);
388 last_cluster = info[i].cluster;
391 reverse_range (start, i);
395 hb_buffer_t::merge_clusters (unsigned int start,
398 if (unlikely (end - start < 2))
401 unsigned int cluster = info[start].cluster;
403 for (unsigned int i = start + 1; i < end; i++)
404 cluster = MIN (cluster, info[i].cluster);
407 while (end < len && info[end - 1].cluster == info[end].cluster)
411 while (idx < start && info[start - 1].cluster == info[start].cluster)
414 /* If we hit the start of buffer, continue in out-buffer. */
416 for (unsigned i = out_len; i && out_info[i - 1].cluster == info[start].cluster; i--)
417 out_info[i - 1].cluster = cluster;
419 for (unsigned int i = start; i < end; i++)
420 info[i].cluster = cluster;
423 hb_buffer_t::merge_out_clusters (unsigned int start,
426 if (unlikely (end - start < 2))
429 unsigned int cluster = out_info[start].cluster;
431 for (unsigned int i = start + 1; i < end; i++)
432 cluster = MIN (cluster, out_info[i].cluster);
435 while (start && out_info[start - 1].cluster == out_info[start].cluster)
439 while (end < out_len && out_info[end - 1].cluster == out_info[end].cluster)
442 /* If we hit the end of out-buffer, continue in buffer. */
444 for (unsigned i = idx; i < len && info[i].cluster == out_info[end - 1].cluster; i++)
445 info[i].cluster = cluster;
447 for (unsigned int i = start; i < end; i++)
448 out_info[i].cluster = cluster;
452 hb_buffer_t::guess_properties (void)
454 /* If script is set to INVALID, guess from buffer contents */
455 if (props.script == HB_SCRIPT_INVALID) {
456 for (unsigned int i = 0; i < len; i++) {
457 hb_script_t script = unicode->script (info[i].codepoint);
458 if (likely (script != HB_SCRIPT_COMMON &&
459 script != HB_SCRIPT_INHERITED &&
460 script != HB_SCRIPT_UNKNOWN)) {
461 props.script = script;
467 /* If direction is set to INVALID, guess from script */
468 if (props.direction == HB_DIRECTION_INVALID) {
469 props.direction = hb_script_get_horizontal_direction (props.script);
472 /* If language is not set, use default language from locale */
473 if (props.language == HB_LANGUAGE_INVALID) {
474 /* TODO get_default_for_script? using $LANGUAGE */
475 props.language = hb_language_get_default ();
481 dump_var_allocation (const hb_buffer_t *buffer)
484 for (unsigned int i = 0; i < 8; i++)
485 buf[i] = '0' + buffer->allocated_var_bytes[7 - i];
487 DEBUG_MSG (BUFFER, buffer,
488 "Current var allocation: %s",
492 void hb_buffer_t::allocate_var (unsigned int byte_i, unsigned int count, const char *owner)
494 assert (byte_i < 8 && byte_i + count <= 8);
497 dump_var_allocation (this);
498 DEBUG_MSG (BUFFER, this,
499 "Allocating var bytes %d..%d for %s",
500 byte_i, byte_i + count - 1, owner);
502 for (unsigned int i = byte_i; i < byte_i + count; i++) {
503 assert (!allocated_var_bytes[i]);
504 allocated_var_bytes[i]++;
505 allocated_var_owner[i] = owner;
509 void hb_buffer_t::deallocate_var (unsigned int byte_i, unsigned int count, const char *owner)
512 dump_var_allocation (this);
514 DEBUG_MSG (BUFFER, this,
515 "Deallocating var bytes %d..%d for %s",
516 byte_i, byte_i + count - 1, owner);
518 assert (byte_i < 8 && byte_i + count <= 8);
519 for (unsigned int i = byte_i; i < byte_i + count; i++) {
520 assert (allocated_var_bytes[i]);
521 assert (0 == strcmp (allocated_var_owner[i], owner));
522 allocated_var_bytes[i]--;
526 void hb_buffer_t::deallocate_var_all (void)
528 memset (allocated_var_bytes, 0, sizeof (allocated_var_bytes));
529 memset (allocated_var_owner, 0, sizeof (allocated_var_owner));
539 if (!(buffer = hb_object_create<hb_buffer_t> ()))
540 return hb_buffer_get_empty ();
548 hb_buffer_get_empty (void)
550 static const hb_buffer_t _hb_buffer_nil = {
551 HB_OBJECT_HEADER_STATIC,
553 const_cast<hb_unicode_funcs_t *> (&_hb_unicode_funcs_nil),
554 _HB_BUFFER_PROPS_DEFAULT,
557 true, /* have_output */
558 true /* have_positions */
561 return const_cast<hb_buffer_t *> (&_hb_buffer_nil);
565 hb_buffer_reference (hb_buffer_t *buffer)
567 return hb_object_reference (buffer);
571 hb_buffer_destroy (hb_buffer_t *buffer)
573 if (!hb_object_destroy (buffer)) return;
575 hb_unicode_funcs_destroy (buffer->unicode);
584 hb_buffer_set_user_data (hb_buffer_t *buffer,
585 hb_user_data_key_t *key,
587 hb_destroy_func_t destroy,
590 return hb_object_set_user_data (buffer, key, data, destroy, replace);
594 hb_buffer_get_user_data (hb_buffer_t *buffer,
595 hb_user_data_key_t *key)
597 return hb_object_get_user_data (buffer, key);
602 hb_buffer_set_unicode_funcs (hb_buffer_t *buffer,
603 hb_unicode_funcs_t *unicode)
605 if (unlikely (hb_object_is_inert (buffer)))
609 unicode = hb_unicode_funcs_get_default ();
612 hb_unicode_funcs_reference (unicode);
613 hb_unicode_funcs_destroy (buffer->unicode);
614 buffer->unicode = unicode;
618 hb_buffer_get_unicode_funcs (hb_buffer_t *buffer)
620 return buffer->unicode;
624 hb_buffer_set_direction (hb_buffer_t *buffer,
625 hb_direction_t direction)
628 if (unlikely (hb_object_is_inert (buffer)))
631 buffer->props.direction = direction;
635 hb_buffer_get_direction (hb_buffer_t *buffer)
637 return buffer->props.direction;
641 hb_buffer_set_script (hb_buffer_t *buffer,
644 if (unlikely (hb_object_is_inert (buffer)))
647 buffer->props.script = script;
651 hb_buffer_get_script (hb_buffer_t *buffer)
653 return buffer->props.script;
657 hb_buffer_set_language (hb_buffer_t *buffer,
658 hb_language_t language)
660 if (unlikely (hb_object_is_inert (buffer)))
663 buffer->props.language = language;
667 hb_buffer_get_language (hb_buffer_t *buffer)
669 return buffer->props.language;
674 hb_buffer_reset (hb_buffer_t *buffer)
680 hb_buffer_pre_allocate (hb_buffer_t *buffer, unsigned int size)
682 return buffer->ensure (size);
686 hb_buffer_allocation_successful (hb_buffer_t *buffer)
688 return !buffer->in_error;
692 hb_buffer_add (hb_buffer_t *buffer,
693 hb_codepoint_t codepoint,
695 unsigned int cluster)
697 buffer->add (codepoint, mask, cluster);
701 hb_buffer_set_length (hb_buffer_t *buffer,
704 if (unlikely (hb_object_is_inert (buffer)))
707 if (!buffer->ensure (length))
710 /* Wipe the new space */
711 if (length > buffer->len) {
712 memset (buffer->info + buffer->len, 0, sizeof (buffer->info[0]) * (length - buffer->len));
713 if (buffer->have_positions)
714 memset (buffer->pos + buffer->len, 0, sizeof (buffer->pos[0]) * (length - buffer->len));
717 buffer->len = length;
722 hb_buffer_get_length (hb_buffer_t *buffer)
727 /* Return value valid as long as buffer not modified */
729 hb_buffer_get_glyph_infos (hb_buffer_t *buffer,
730 unsigned int *length)
733 *length = buffer->len;
735 return (hb_glyph_info_t *) buffer->info;
738 /* Return value valid as long as buffer not modified */
739 hb_glyph_position_t *
740 hb_buffer_get_glyph_positions (hb_buffer_t *buffer,
741 unsigned int *length)
743 if (!buffer->have_positions)
744 buffer->clear_positions ();
747 *length = buffer->len;
749 return (hb_glyph_position_t *) buffer->pos;
753 hb_buffer_reverse (hb_buffer_t *buffer)
759 hb_buffer_reverse_clusters (hb_buffer_t *buffer)
761 buffer->reverse_clusters ();
765 hb_buffer_guess_properties (hb_buffer_t *buffer)
767 buffer->guess_properties ();
772 if (text_length == -1) { \
774 const T *p = (const T *) text; \
780 if (item_length == -1) \
781 item_length = text_length - item_offset; \
782 buffer->ensure (buffer->len + item_length * sizeof (T) / 4); \
783 const T *next = (const T *) text + item_offset; \
784 const T *end = next + item_length; \
785 while (next < end) { \
787 const T *old_next = next; \
788 next = UTF_NEXT (next, end, u); \
789 hb_buffer_add (buffer, u, 1, old_next - (const T *) text); \
794 #define UTF8_COMPUTE(Char, Mask, Len) \
795 if (Char < 128) { Len = 1; Mask = 0x7f; } \
796 else if ((Char & 0xe0) == 0xc0) { Len = 2; Mask = 0x1f; } \
797 else if ((Char & 0xf0) == 0xe0) { Len = 3; Mask = 0x0f; } \
798 else if ((Char & 0xf8) == 0xf0) { Len = 4; Mask = 0x07; } \
801 static inline const uint8_t *
802 hb_utf8_next (const uint8_t *text,
804 hb_codepoint_t *unicode)
807 unsigned int mask, len;
809 /* TODO check for overlong sequences? */
811 UTF8_COMPUTE (c, mask, len);
812 if (unlikely (!len || (unsigned int) (end - text) < len)) {
816 hb_codepoint_t result;
819 for (i = 1; i < len; i++)
821 if (unlikely ((text[i] & 0xc0) != 0x80))
827 result |= (text[i] & 0x3f);
835 hb_buffer_add_utf8 (hb_buffer_t *buffer,
838 unsigned int item_offset,
841 #define UTF_NEXT(S, E, U) hb_utf8_next (S, E, &(U))
846 static inline const uint16_t *
847 hb_utf16_next (const uint16_t *text,
849 hb_codepoint_t *unicode)
851 uint16_t c = *text++;
853 if (unlikely (c >= 0xd800 && c < 0xdc00)) {
856 if (text < end && ((l = *text), likely (l >= 0xdc00 && l < 0xe000))) {
858 *unicode = ((hb_codepoint_t) ((c) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000);
869 hb_buffer_add_utf16 (hb_buffer_t *buffer,
870 const uint16_t *text,
872 unsigned int item_offset,
875 #define UTF_NEXT(S, E, U) hb_utf16_next (S, E, &(U))
881 hb_buffer_add_utf32 (hb_buffer_t *buffer,
882 const uint32_t *text,
884 unsigned int item_offset,
887 #define UTF_NEXT(S, E, U) ((U) = *(S), (S)+1)
894 compare_info_codepoint (const hb_glyph_info_t *pa,
895 const hb_glyph_info_t *pb)
897 return (int) pb->codepoint - (int) pa->codepoint;
901 normalize_glyphs_cluster (hb_buffer_t *buffer,
906 hb_glyph_position_t *pos = buffer->pos;
908 /* Total cluster advance */
909 hb_position_t total_x_advance = 0, total_y_advance = 0;
910 for (unsigned int i = start; i < end; i++)
912 total_x_advance += pos[i].x_advance;
913 total_y_advance += pos[i].y_advance;
916 hb_position_t x_advance = 0, y_advance = 0;
917 for (unsigned int i = start; i < end; i++)
919 pos[i].x_offset += x_advance;
920 pos[i].y_offset += y_advance;
922 x_advance += pos[i].x_advance;
923 y_advance += pos[i].y_advance;
925 pos[i].x_advance = 0;
926 pos[i].y_advance = 0;
931 /* Transfer all cluster advance to the last glyph. */
932 pos[end - 1].x_advance = total_x_advance;
933 pos[end - 1].y_advance = total_y_advance;
935 hb_bubble_sort (buffer->info + start, end - start - 1, compare_info_codepoint, buffer->pos + start);
937 /* Transfer all cluster advance to the first glyph. */
938 pos[start].x_advance += total_x_advance;
939 pos[start].y_advance += total_y_advance;
940 for (unsigned int i = start + 1; i < end; i++) {
941 pos[i].x_offset -= total_x_advance;
942 pos[i].y_offset -= total_y_advance;
944 hb_bubble_sort (buffer->info + start + 1, end - start - 1, compare_info_codepoint, buffer->pos + start + 1);
949 hb_buffer_normalize_glyphs (hb_buffer_t *buffer)
951 assert (buffer->have_positions);
952 /* XXX assert (buffer->have_glyphs); */
954 bool backward = HB_DIRECTION_IS_BACKWARD (buffer->props.direction);
956 unsigned int count = buffer->len;
957 if (unlikely (!count)) return;
958 hb_glyph_info_t *info = buffer->info;
960 unsigned int start = 0;
962 for (end = start + 1; end < count; end++)
963 if (info[start].cluster != info[end].cluster) {
964 normalize_glyphs_cluster (buffer, start, end, backward);
967 normalize_glyphs_cluster (buffer, start, end, backward);