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 #define _HB_BUFFER_UNICODE_FUNCS_DEFAULT (const_cast<hb_unicode_funcs_t *> (&_hb_unicode_funcs_default))
41 #define _HB_BUFFER_PROPS_DEFAULT { HB_DIRECTION_INVALID, HB_SCRIPT_INVALID, HB_LANGUAGE_INVALID }
43 /* Here is how the buffer works internally:
45 * There are two info pointers: info and out_info. They always have
46 * the same allocated size, but different lengths.
48 * As an optimization, both info and out_info may point to the
49 * same piece of memory, which is owned by info. This remains the
50 * case as long as out_len doesn't exceed i at any time.
51 * In that case, swap_buffers() is no-op and the glyph operations operate
54 * As soon as out_info gets longer than info, out_info is moved over
55 * to an alternate buffer (which we reuse the pos buffer for!), and its
56 * current contents (out_len entries) are copied to the new place.
57 * This should all remain transparent to the user. swap_buffers() then
58 * switches info and out_info.
66 hb_buffer_t::enlarge (unsigned int size)
68 if (unlikely (in_error))
71 unsigned int new_allocated = allocated;
72 hb_glyph_position_t *new_pos = NULL;
73 hb_glyph_info_t *new_info = NULL;
74 bool separate_out = out_info != info;
76 if (unlikely (_hb_unsigned_int_mul_overflows (size, sizeof (info[0]))))
79 while (size > new_allocated)
80 new_allocated += (new_allocated >> 1) + 32;
82 ASSERT_STATIC (sizeof (info[0]) == sizeof (pos[0]));
83 if (unlikely (_hb_unsigned_int_mul_overflows (new_allocated, sizeof (info[0]))))
86 new_pos = (hb_glyph_position_t *) realloc (pos, new_allocated * sizeof (pos[0]));
87 new_info = (hb_glyph_info_t *) realloc (info, new_allocated * sizeof (info[0]));
90 if (unlikely (!new_pos || !new_info))
96 if (likely (new_info))
99 out_info = separate_out ? (hb_glyph_info_t *) pos : info;
100 if (likely (!in_error))
101 allocated = new_allocated;
103 return likely (!in_error);
107 hb_buffer_t::make_room_for (unsigned int num_in,
108 unsigned int num_out)
110 if (unlikely (!ensure (out_len + num_out))) return false;
112 if (out_info == info &&
113 out_len + num_out > idx + num_in)
115 assert (have_output);
117 out_info = (hb_glyph_info_t *) pos;
118 memcpy (out_info, info, out_len * sizeof (out_info[0]));
125 hb_buffer_t::get_scratch_buffer (unsigned int *size)
128 have_positions = false;
130 *size = allocated * sizeof (pos[0]);
135 /* HarfBuzz-Internal API */
138 hb_buffer_t::reset (void)
140 if (unlikely (hb_object_is_inert (this)))
143 hb_unicode_funcs_destroy (unicode);
144 unicode = _HB_BUFFER_UNICODE_FUNCS_DEFAULT;
146 hb_segment_properties_t default_props = _HB_BUFFER_PROPS_DEFAULT;
147 props = default_props;
151 have_positions = false;
158 memset (allocated_var_bytes, 0, sizeof allocated_var_bytes);
159 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;
205 memset (pos, 0, sizeof (pos[0]) * len);
209 hb_buffer_t::swap_buffers (void)
211 if (unlikely (in_error)) return;
213 assert (have_output);
216 if (out_info != info)
218 hb_glyph_info_t *tmp_string;
221 out_info = tmp_string;
222 pos = (hb_glyph_position_t *) out_info;
234 hb_buffer_t::replace_glyphs_be16 (unsigned int num_in,
235 unsigned int num_out,
236 const uint16_t *glyph_data_be)
238 if (!make_room_for (num_in, num_out)) return;
240 hb_glyph_info_t orig_info = info[idx];
241 for (unsigned int i = 1; i < num_in; i++)
243 hb_glyph_info_t *inf = &info[idx + i];
244 orig_info.cluster = MIN (orig_info.cluster, inf->cluster);
247 hb_glyph_info_t *pinfo = &out_info[out_len];
248 for (unsigned int i = 0; i < num_out; i++)
251 pinfo->codepoint = hb_be_uint16 (glyph_data_be[i]);
260 hb_buffer_t::replace_glyphs (unsigned int num_in,
261 unsigned int num_out,
262 const uint32_t *glyph_data)
264 if (!make_room_for (num_in, num_out)) return;
266 hb_glyph_info_t orig_info = info[idx];
267 for (unsigned int i = 1; i < num_in; i++)
269 hb_glyph_info_t *inf = &info[idx + i];
270 orig_info.cluster = MIN (orig_info.cluster, inf->cluster);
273 hb_glyph_info_t *pinfo = &out_info[out_len];
274 for (unsigned int i = 0; i < num_out; i++)
277 pinfo->codepoint = glyph_data[i];
286 hb_buffer_t::output_glyph (hb_codepoint_t glyph_index)
288 if (!make_room_for (0, 1)) return;
290 out_info[out_len] = info[idx];
291 out_info[out_len].codepoint = glyph_index;
297 hb_buffer_t::copy_glyph (void)
299 if (!make_room_for (0, 1)) return;
301 out_info[out_len] = info[idx];
307 hb_buffer_t::replace_glyph (hb_codepoint_t glyph_index)
309 if (!make_room_for (1, 1)) return;
311 out_info[out_len] = info[idx];
312 out_info[out_len].codepoint = glyph_index;
319 hb_buffer_t::next_glyph (void)
323 if (out_info != info)
325 if (unlikely (!ensure (out_len + 1))) return;
326 out_info[out_len] = info[idx];
328 else if (out_len != idx)
329 out_info[out_len] = info[idx];
338 hb_buffer_t::set_masks (hb_mask_t value,
340 unsigned int cluster_start,
341 unsigned int cluster_end)
343 hb_mask_t not_mask = ~mask;
349 if (cluster_start == 0 && cluster_end == (unsigned int)-1) {
350 unsigned int count = len;
351 for (unsigned int i = 0; i < count; i++)
352 info[i].mask = (info[i].mask & not_mask) | value;
356 unsigned int count = len;
357 for (unsigned int i = 0; i < count; i++)
358 if (cluster_start <= info[i].cluster && info[i].cluster < cluster_end)
359 info[i].mask = (info[i].mask & not_mask) | value;
363 hb_buffer_t::reverse_range (unsigned int start,
368 if (start == end - 1)
371 for (i = start, j = end - 1; i < j; i++, j--) {
380 for (i = start, j = end - 1; i < j; i++, j--) {
381 hb_glyph_position_t t;
391 hb_buffer_t::reverse (void)
396 reverse_range (0, len);
400 hb_buffer_t::reverse_clusters (void)
402 unsigned int i, start, count, last_cluster;
411 last_cluster = info[0].cluster;
412 for (i = 1; i < count; i++) {
413 if (last_cluster != info[i].cluster) {
414 reverse_range (start, i);
416 last_cluster = info[i].cluster;
419 reverse_range (start, i);
423 hb_buffer_t::merge_clusters (unsigned int start,
426 unsigned int cluster = this->info[start].cluster;
428 for (unsigned int i = start + 1; i < end; i++)
429 cluster = MIN (cluster, this->info[i].cluster);
430 for (unsigned int i = start; i < end; i++)
431 this->info[i].cluster = cluster;
434 hb_buffer_t::merge_out_clusters (unsigned int start,
437 unsigned int cluster = this->out_info[start].cluster;
439 for (unsigned int i = start + 1; i < end; i++)
440 cluster = MIN (cluster, this->out_info[i].cluster);
441 for (unsigned int i = start; i < end; i++)
442 this->out_info[i].cluster = cluster;
446 hb_buffer_t::guess_properties (void)
448 /* If script is set to INVALID, guess from buffer contents */
449 if (props.script == HB_SCRIPT_INVALID) {
450 for (unsigned int i = 0; i < len; i++) {
451 hb_script_t script = hb_unicode_script (unicode, info[i].codepoint);
452 if (likely (script != HB_SCRIPT_COMMON &&
453 script != HB_SCRIPT_INHERITED &&
454 script != HB_SCRIPT_UNKNOWN)) {
455 props.script = script;
461 /* If direction is set to INVALID, guess from script */
462 if (props.direction == HB_DIRECTION_INVALID) {
463 props.direction = hb_script_get_horizontal_direction (props.script);
466 /* If language is not set, use default language from locale */
467 if (props.language == HB_LANGUAGE_INVALID) {
468 /* TODO get_default_for_script? using $LANGUAGE */
469 props.language = hb_language_get_default ();
475 dump_var_allocation (const hb_buffer_t *buffer)
478 for (unsigned int i = 0; i < 8; i++)
479 buf[i] = '0' + buffer->allocated_var_bytes[7 - i];
481 DEBUG_MSG (BUFFER, buffer,
482 "Current var allocation: %s",
486 void hb_buffer_t::allocate_var (unsigned int byte_i, unsigned int count, const char *owner)
488 assert (byte_i < 8 && byte_i + count <= 8);
491 dump_var_allocation (this);
492 DEBUG_MSG (BUFFER, this,
493 "Allocating var bytes %d..%d for %s",
494 byte_i, byte_i + count - 1, owner);
496 for (unsigned int i = byte_i; i < byte_i + count; i++) {
497 assert (!allocated_var_bytes[i]);
498 allocated_var_bytes[i]++;
499 allocated_var_owner[i] = owner;
503 void hb_buffer_t::deallocate_var (unsigned int byte_i, unsigned int count, const char *owner)
506 dump_var_allocation (this);
508 DEBUG_MSG (BUFFER, this,
509 "Deallocating var bytes %d..%d for %s",
510 byte_i, byte_i + count - 1, owner);
512 assert (byte_i < 8 && byte_i + count <= 8);
513 for (unsigned int i = byte_i; i < byte_i + count; i++) {
514 assert (allocated_var_bytes[i]);
515 assert (0 == strcmp (allocated_var_owner[i], owner));
516 allocated_var_bytes[i]--;
520 void hb_buffer_t::deallocate_var_all (void)
522 memset (allocated_var_bytes, 0, sizeof (allocated_var_bytes));
523 memset (allocated_var_owner, 0, sizeof (allocated_var_owner));
533 if (!(buffer = hb_object_create<hb_buffer_t> ()))
534 return hb_buffer_get_empty ();
542 hb_buffer_get_empty (void)
544 static const hb_buffer_t _hb_buffer_nil = {
545 HB_OBJECT_HEADER_STATIC,
547 _HB_BUFFER_UNICODE_FUNCS_DEFAULT,
548 _HB_BUFFER_PROPS_DEFAULT,
551 true, /* have_output */
552 true /* have_positions */
555 return const_cast<hb_buffer_t *> (&_hb_buffer_nil);
559 hb_buffer_reference (hb_buffer_t *buffer)
561 return hb_object_reference (buffer);
565 hb_buffer_destroy (hb_buffer_t *buffer)
567 if (!hb_object_destroy (buffer)) return;
569 hb_unicode_funcs_destroy (buffer->unicode);
578 hb_buffer_set_user_data (hb_buffer_t *buffer,
579 hb_user_data_key_t *key,
581 hb_destroy_func_t destroy,
584 return hb_object_set_user_data (buffer, key, data, destroy, replace);
588 hb_buffer_get_user_data (hb_buffer_t *buffer,
589 hb_user_data_key_t *key)
591 return hb_object_get_user_data (buffer, key);
596 hb_buffer_set_unicode_funcs (hb_buffer_t *buffer,
597 hb_unicode_funcs_t *unicode)
599 if (unlikely (hb_object_is_inert (buffer)))
603 unicode = _HB_BUFFER_UNICODE_FUNCS_DEFAULT;
605 hb_unicode_funcs_reference (unicode);
606 hb_unicode_funcs_destroy (buffer->unicode);
607 buffer->unicode = unicode;
611 hb_buffer_get_unicode_funcs (hb_buffer_t *buffer)
613 return buffer->unicode;
617 hb_buffer_set_direction (hb_buffer_t *buffer,
618 hb_direction_t direction)
621 if (unlikely (hb_object_is_inert (buffer)))
624 buffer->props.direction = direction;
628 hb_buffer_get_direction (hb_buffer_t *buffer)
630 return buffer->props.direction;
634 hb_buffer_set_script (hb_buffer_t *buffer,
637 if (unlikely (hb_object_is_inert (buffer)))
640 buffer->props.script = script;
644 hb_buffer_get_script (hb_buffer_t *buffer)
646 return buffer->props.script;
650 hb_buffer_set_language (hb_buffer_t *buffer,
651 hb_language_t language)
653 if (unlikely (hb_object_is_inert (buffer)))
656 buffer->props.language = language;
660 hb_buffer_get_language (hb_buffer_t *buffer)
662 return buffer->props.language;
667 hb_buffer_reset (hb_buffer_t *buffer)
673 hb_buffer_pre_allocate (hb_buffer_t *buffer, unsigned int size)
675 return buffer->ensure (size);
679 hb_buffer_allocation_successful (hb_buffer_t *buffer)
681 return !buffer->in_error;
685 hb_buffer_add (hb_buffer_t *buffer,
686 hb_codepoint_t codepoint,
688 unsigned int cluster)
690 buffer->add (codepoint, mask, cluster);
694 hb_buffer_set_length (hb_buffer_t *buffer,
697 if (unlikely (hb_object_is_inert (buffer)))
700 if (!buffer->ensure (length))
703 /* Wipe the new space */
704 if (length > buffer->len) {
705 memset (buffer->info + buffer->len, 0, sizeof (buffer->info[0]) * (length - buffer->len));
706 if (buffer->have_positions)
707 memset (buffer->pos + buffer->len, 0, sizeof (buffer->pos[0]) * (length - buffer->len));
710 buffer->len = length;
715 hb_buffer_get_length (hb_buffer_t *buffer)
720 /* Return value valid as long as buffer not modified */
722 hb_buffer_get_glyph_infos (hb_buffer_t *buffer,
723 unsigned int *length)
726 *length = buffer->len;
728 return (hb_glyph_info_t *) buffer->info;
731 /* Return value valid as long as buffer not modified */
732 hb_glyph_position_t *
733 hb_buffer_get_glyph_positions (hb_buffer_t *buffer,
734 unsigned int *length)
736 if (!buffer->have_positions)
737 buffer->clear_positions ();
740 *length = buffer->len;
742 return (hb_glyph_position_t *) buffer->pos;
746 hb_buffer_reverse (hb_buffer_t *buffer)
752 hb_buffer_reverse_clusters (hb_buffer_t *buffer)
754 buffer->reverse_clusters ();
758 hb_buffer_guess_properties (hb_buffer_t *buffer)
760 buffer->guess_properties ();
765 if (text_length == -1) { \
767 const T *p = (const T *) text; \
773 if (item_length == -1) \
774 item_length = text_length - item_offset; \
775 buffer->ensure (buffer->len + item_length * sizeof (T) / 4); \
776 const T *next = (const T *) text + item_offset; \
777 const T *end = next + item_length; \
778 while (next < end) { \
780 const T *old_next = next; \
781 next = UTF_NEXT (next, end, u); \
782 hb_buffer_add (buffer, u, 1, old_next - (const T *) text); \
787 #define UTF8_COMPUTE(Char, Mask, Len) \
788 if (Char < 128) { Len = 1; Mask = 0x7f; } \
789 else if ((Char & 0xe0) == 0xc0) { Len = 2; Mask = 0x1f; } \
790 else if ((Char & 0xf0) == 0xe0) { Len = 3; Mask = 0x0f; } \
791 else if ((Char & 0xf8) == 0xf0) { Len = 4; Mask = 0x07; } \
794 static inline const uint8_t *
795 hb_utf8_next (const uint8_t *text,
797 hb_codepoint_t *unicode)
800 unsigned int mask, len;
802 /* TODO check for overlong sequences? */
804 UTF8_COMPUTE (c, mask, len);
805 if (unlikely (!len || (unsigned int) (end - text) < len)) {
809 hb_codepoint_t result;
812 for (i = 1; i < len; i++)
814 if (unlikely ((text[i] & 0xc0) != 0x80))
820 result |= (text[i] & 0x3f);
828 hb_buffer_add_utf8 (hb_buffer_t *buffer,
831 unsigned int item_offset,
834 #define UTF_NEXT(S, E, U) hb_utf8_next (S, E, &(U))
839 static inline const uint16_t *
840 hb_utf16_next (const uint16_t *text,
842 hb_codepoint_t *unicode)
844 uint16_t c = *text++;
846 if (unlikely (c >= 0xd800 && c < 0xdc00)) {
849 if (text < end && ((l = *text), likely (l >= 0xdc00 && l < 0xe000))) {
851 *unicode = ((hb_codepoint_t) ((c) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000);
862 hb_buffer_add_utf16 (hb_buffer_t *buffer,
863 const uint16_t *text,
865 unsigned int item_offset,
868 #define UTF_NEXT(S, E, U) hb_utf16_next (S, E, &(U))
874 hb_buffer_add_utf32 (hb_buffer_t *buffer,
875 const uint32_t *text,
877 unsigned int item_offset,
880 #define UTF_NEXT(S, E, U) ((U) = *(S), (S)+1)