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"
37 #ifndef HB_DEBUG_BUFFER
38 #define HB_DEBUG_BUFFER (HB_DEBUG+0)
42 static hb_buffer_t _hb_buffer_nil = {
43 HB_OBJECT_HEADER_STATIC,
45 &_hb_unicode_funcs_default,
53 TRUE, /* have_output */
54 TRUE /* have_positions */
57 /* Here is how the buffer works internally:
59 * There are two info pointers: info and out_info. They always have
60 * the same allocated size, but different lengths.
62 * As an optimization, both info and out_info may point to the
63 * same piece of memory, which is owned by info. This remains the
64 * case as long as out_len doesn't exceed i at any time.
65 * In that case, swap_buffers() is no-op and the glyph operations operate
68 * As soon as out_info gets longer than info, out_info is moved over
69 * to an alternate buffer (which we reuse the pos buffer for!), and its
70 * current contents (out_len entries) are copied to the new place.
71 * This should all remain transparent to the user. swap_buffers() then
72 * switches info and out_info.
80 hb_buffer_t::enlarge (unsigned int size)
82 if (unlikely (in_error))
85 unsigned int new_allocated = allocated;
86 hb_glyph_position_t *new_pos = NULL;
87 hb_glyph_info_t *new_info = NULL;
88 bool separate_out = out_info != info;
90 if (unlikely (_hb_unsigned_int_mul_overflows (size, sizeof (info[0]))))
93 while (size > new_allocated)
94 new_allocated += (new_allocated >> 1) + 32;
96 ASSERT_STATIC (sizeof (info[0]) == sizeof (pos[0]));
97 if (unlikely (_hb_unsigned_int_mul_overflows (new_allocated, sizeof (info[0]))))
100 new_pos = (hb_glyph_position_t *) realloc (pos, new_allocated * sizeof (pos[0]));
101 new_info = (hb_glyph_info_t *) realloc (info, new_allocated * sizeof (info[0]));
104 if (unlikely (!new_pos || !new_info))
107 if (likely (new_pos))
110 if (likely (new_info))
113 out_info = separate_out ? (hb_glyph_info_t *) pos : info;
114 if (likely (!in_error))
115 allocated = new_allocated;
117 return likely (!in_error);
121 hb_buffer_t::make_room_for (unsigned int num_in,
122 unsigned int num_out)
124 if (unlikely (!ensure (out_len + num_out))) return FALSE;
126 if (out_info == info &&
127 out_len + num_out > idx + num_in)
129 assert (have_output);
131 out_info = (hb_glyph_info_t *) pos;
132 memcpy (out_info, info, out_len * sizeof (out_info[0]));
139 /* HarfBuzz-Internal API */
142 hb_buffer_t::reset (void)
144 if (unlikely (hb_object_is_inert (this)))
147 hb_unicode_funcs_destroy (unicode);
148 unicode = _hb_buffer_nil.unicode;
150 props = _hb_buffer_nil.props;
154 have_positions = FALSE;
161 memset (allocated_var_bytes, 0, sizeof allocated_var_bytes);
162 memset (allocated_var_owner, 0, sizeof allocated_var_owner);
168 hb_buffer_t::add (hb_codepoint_t codepoint,
170 unsigned int cluster)
172 hb_glyph_info_t *glyph;
174 if (unlikely (!ensure (len + 1))) return;
178 memset (glyph, 0, sizeof (*glyph));
179 glyph->codepoint = codepoint;
181 glyph->cluster = cluster;
187 hb_buffer_t::clear_output (void)
189 if (unlikely (hb_object_is_inert (this)))
193 have_positions = FALSE;
200 hb_buffer_t::clear_positions (void)
202 if (unlikely (hb_object_is_inert (this)))
206 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);
218 if (out_info != info)
220 hb_glyph_info_t *tmp_string;
223 out_info = tmp_string;
224 pos = (hb_glyph_position_t *) out_info;
236 hb_buffer_t::replace_glyphs_be16 (unsigned int num_in,
237 unsigned int num_out,
238 const uint16_t *glyph_data_be)
240 if (!make_room_for (num_in, num_out)) return;
242 hb_glyph_info_t orig_info = info[idx];
243 for (unsigned int i = 1; i < num_in; i++)
245 hb_glyph_info_t *inf = &info[idx + i];
246 orig_info.cluster = MIN (orig_info.cluster, inf->cluster);
249 hb_glyph_info_t *pinfo = &out_info[out_len];
250 for (unsigned int i = 0; i < num_out; i++)
253 pinfo->codepoint = hb_be_uint16 (glyph_data_be[i]);
262 hb_buffer_t::output_glyph (hb_codepoint_t glyph_index)
264 if (!make_room_for (0, 1)) return;
266 out_info[out_len] = info[idx];
267 out_info[out_len].codepoint = glyph_index;
273 hb_buffer_t::copy_glyph (void)
275 if (!make_room_for (0, 1)) return;
277 out_info[out_len] = info[idx];
283 hb_buffer_t::replace_glyph (hb_codepoint_t glyph_index)
285 out_info[out_len] = info[idx];
286 out_info[out_len].codepoint = glyph_index;
293 hb_buffer_t::next_glyph (void)
297 if (out_info != info)
299 if (unlikely (!ensure (out_len + 1))) return;
300 out_info[out_len] = info[idx];
302 else if (out_len != idx)
303 out_info[out_len] = info[idx];
312 hb_buffer_t::set_masks (hb_mask_t value,
314 unsigned int cluster_start,
315 unsigned int cluster_end)
317 hb_mask_t not_mask = ~mask;
323 if (cluster_start == 0 && cluster_end == (unsigned int)-1) {
324 unsigned int count = len;
325 for (unsigned int i = 0; i < count; i++)
326 info[i].mask = (info[i].mask & not_mask) | value;
330 unsigned int count = len;
331 for (unsigned int i = 0; i < count; i++)
332 if (cluster_start <= info[i].cluster && info[i].cluster < cluster_end)
333 info[i].mask = (info[i].mask & not_mask) | value;
337 hb_buffer_t::reverse_range (unsigned int start,
342 if (start == end - 1)
345 for (i = start, j = end - 1; i < j; i++, j--) {
354 for (i = start, j = end - 1; i < j; i++, j--) {
355 hb_glyph_position_t t;
365 hb_buffer_t::reverse (void)
370 reverse_range (0, len);
374 hb_buffer_t::reverse_clusters (void)
376 unsigned int i, start, count, last_cluster;
385 last_cluster = info[0].cluster;
386 for (i = 1; i < count; i++) {
387 if (last_cluster != info[i].cluster) {
388 reverse_range (start, i);
390 last_cluster = info[i].cluster;
393 reverse_range (start, i);
397 dump_var_allocation (const hb_buffer_t *buffer)
400 for (unsigned int i = 0; i < 8; i++)
401 buf[i] = '0' + buffer->allocated_var_bytes[i];
403 DEBUG_MSG (BUFFER, buffer,
404 "Current var allocation: %s",
408 void hb_buffer_t::allocate_var (unsigned int byte_i, unsigned int count, const char *owner)
410 assert (byte_i < 8 && byte_i + count < 8);
413 dump_var_allocation (this);
414 DEBUG_MSG (BUFFER, this,
415 "Allocating var bytes %d..%d for %s",
416 byte_i, byte_i + count - 1, owner);
418 for (unsigned int i = byte_i; i < byte_i + count; i++) {
419 assert (!allocated_var_bytes[i]);
420 allocated_var_bytes[i]++;
421 allocated_var_owner[i] = owner;
425 void hb_buffer_t::deallocate_var (unsigned int byte_i, unsigned int count, const char *owner)
427 DEBUG_MSG (BUFFER, this,
428 "Deallocating var bytes %d..%d for %s",
429 byte_i, byte_i + count - 1, owner);
431 assert (byte_i < 8 && byte_i + count < 8);
432 for (unsigned int i = byte_i; i < byte_i + count; i++) {
433 assert (allocated_var_bytes[i] && allocated_var_owner[i] == owner);
434 allocated_var_bytes[i]--;
438 dump_var_allocation (this);
441 void hb_buffer_t::deallocate_var_all (void)
443 memset (allocated_var_bytes, 0, sizeof (allocated_var_bytes));
444 memset (allocated_var_owner, 0, sizeof (allocated_var_owner));
450 hb_buffer_create (unsigned int pre_alloc_size)
454 if (!(buffer = hb_object_create<hb_buffer_t> ()))
455 return &_hb_buffer_nil;
459 if (pre_alloc_size && !buffer->ensure (pre_alloc_size)) {
460 hb_buffer_destroy (buffer);
461 return &_hb_buffer_nil;
468 hb_buffer_get_empty (void)
470 return &_hb_buffer_nil;
474 hb_buffer_reference (hb_buffer_t *buffer)
476 return hb_object_reference (buffer);
480 hb_buffer_destroy (hb_buffer_t *buffer)
482 if (!hb_object_destroy (buffer)) return;
484 hb_unicode_funcs_destroy (buffer->unicode);
493 hb_buffer_set_user_data (hb_buffer_t *buffer,
494 hb_user_data_key_t *key,
496 hb_destroy_func_t destroy)
498 return hb_object_set_user_data (buffer, key, data, destroy);
502 hb_buffer_get_user_data (hb_buffer_t *buffer,
503 hb_user_data_key_t *key)
505 return hb_object_get_user_data (buffer, key);
510 hb_buffer_set_unicode_funcs (hb_buffer_t *buffer,
511 hb_unicode_funcs_t *unicode)
513 if (unlikely (hb_object_is_inert (buffer)))
517 unicode = _hb_buffer_nil.unicode;
519 hb_unicode_funcs_reference (unicode);
520 hb_unicode_funcs_destroy (buffer->unicode);
521 buffer->unicode = unicode;
525 hb_buffer_get_unicode_funcs (hb_buffer_t *buffer)
527 return buffer->unicode;
531 hb_buffer_set_direction (hb_buffer_t *buffer,
532 hb_direction_t direction)
535 if (unlikely (hb_object_is_inert (buffer)))
538 buffer->props.direction = direction;
542 hb_buffer_get_direction (hb_buffer_t *buffer)
544 return buffer->props.direction;
548 hb_buffer_set_script (hb_buffer_t *buffer,
551 if (unlikely (hb_object_is_inert (buffer)))
554 buffer->props.script = script;
558 hb_buffer_get_script (hb_buffer_t *buffer)
560 return buffer->props.script;
564 hb_buffer_set_language (hb_buffer_t *buffer,
565 hb_language_t language)
567 if (unlikely (hb_object_is_inert (buffer)))
570 buffer->props.language = language;
574 hb_buffer_get_language (hb_buffer_t *buffer)
576 return buffer->props.language;
581 hb_buffer_reset (hb_buffer_t *buffer)
587 hb_buffer_pre_allocate (hb_buffer_t *buffer, unsigned int size)
589 return buffer->ensure (size);
593 hb_buffer_allocation_successful (hb_buffer_t *buffer)
595 return !buffer->in_error;
599 hb_buffer_add (hb_buffer_t *buffer,
600 hb_codepoint_t codepoint,
602 unsigned int cluster)
604 buffer->add (codepoint, mask, cluster);
608 hb_buffer_set_length (hb_buffer_t *buffer,
611 if (!buffer->ensure (length))
614 /* Wipe the new space */
615 if (length > buffer->len) {
616 memset (buffer->info + buffer->len, 0, sizeof (buffer->info[0]) * (length - buffer->len));
617 if (buffer->have_positions)
618 memset (buffer->pos + buffer->len, 0, sizeof (buffer->pos[0]) * (length - buffer->len));
621 buffer->len = length;
626 hb_buffer_get_length (hb_buffer_t *buffer)
631 /* Return value valid as long as buffer not modified */
633 hb_buffer_get_glyph_infos (hb_buffer_t *buffer,
634 unsigned int *length)
637 *length = buffer->len;
639 return (hb_glyph_info_t *) buffer->info;
642 /* Return value valid as long as buffer not modified */
643 hb_glyph_position_t *
644 hb_buffer_get_glyph_positions (hb_buffer_t *buffer,
645 unsigned int *length)
647 if (!buffer->have_positions)
648 buffer->clear_positions ();
651 *length = buffer->len;
653 return (hb_glyph_position_t *) buffer->pos;
657 hb_buffer_reverse (hb_buffer_t *buffer)
663 hb_buffer_reverse_clusters (hb_buffer_t *buffer)
665 buffer->reverse_clusters ();
670 const T *next = (const T *) text + item_offset; \
671 const T *end = next + item_length; \
672 while (next < end) { \
674 const T *old_next = next; \
675 next = UTF_NEXT (next, end, u); \
676 hb_buffer_add (buffer, u, 1, old_next - (const T *) text); \
681 #define UTF8_COMPUTE(Char, Mask, Len) \
682 if (Char < 128) { Len = 1; Mask = 0x7f; } \
683 else if ((Char & 0xe0) == 0xc0) { Len = 2; Mask = 0x1f; } \
684 else if ((Char & 0xf0) == 0xe0) { Len = 3; Mask = 0x0f; } \
685 else if ((Char & 0xf8) == 0xf0) { Len = 4; Mask = 0x07; } \
688 static inline const uint8_t *
689 hb_utf8_next (const uint8_t *text,
691 hb_codepoint_t *unicode)
694 unsigned int mask, len;
696 /* TODO check for overlong sequences? */
698 UTF8_COMPUTE (c, mask, len);
699 if (unlikely (!len || (unsigned int) (end - text) < len)) {
703 hb_codepoint_t result;
706 for (i = 1; i < len; i++)
708 if (unlikely ((text[i] & 0xc0) != 0x80))
714 result |= (text[i] & 0x3f);
722 hb_buffer_add_utf8 (hb_buffer_t *buffer,
724 unsigned int text_length HB_UNUSED,
725 unsigned int item_offset,
726 unsigned int item_length)
728 #define UTF_NEXT(S, E, U) hb_utf8_next (S, E, &(U))
733 static inline const uint16_t *
734 hb_utf16_next (const uint16_t *text,
736 hb_codepoint_t *unicode)
738 uint16_t c = *text++;
740 if (unlikely (c >= 0xd800 && c < 0xdc00)) {
743 if (text < end && ((l = *text), likely (l >= 0xdc00 && l < 0xe000))) {
745 *unicode = ((hb_codepoint_t) ((c) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000);
756 hb_buffer_add_utf16 (hb_buffer_t *buffer,
757 const uint16_t *text,
758 unsigned int text_length HB_UNUSED,
759 unsigned int item_offset,
760 unsigned int item_length)
762 #define UTF_NEXT(S, E, U) hb_utf16_next (S, E, &(U))
768 hb_buffer_add_utf32 (hb_buffer_t *buffer,
769 const uint32_t *text,
770 unsigned int text_length HB_UNUSED,
771 unsigned int item_offset,
772 unsigned int item_length)
774 #define UTF_NEXT(S, E, U) ((U) = *(S), (S)+1)