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 static hb_buffer_t _hb_buffer_nil = {
38 HB_OBJECT_HEADER_STATIC,
40 &_hb_unicode_funcs_default,
48 TRUE, /* have_output */
49 TRUE /* have_positions */
52 /* Here is how the buffer works internally:
54 * There are two info pointers: info and out_info. They always have
55 * the same allocated size, but different lengths.
57 * As an optimization, both info and out_info may point to the
58 * same piece of memory, which is owned by info. This remains the
59 * case as long as out_len doesn't exceed i at any time.
60 * In that case, swap_buffers() is no-op and the glyph operations operate
63 * As soon as out_info gets longer than info, out_info is moved over
64 * to an alternate buffer (which we reuse the pos buffer for!), and its
65 * current contents (out_len entries) are copied to the new place.
66 * This should all remain transparent to the user. swap_buffers() then
67 * switches info and out_info.
75 hb_buffer_t::enlarge (unsigned int size)
77 if (unlikely (in_error))
80 unsigned int new_allocated = allocated;
81 hb_glyph_position_t *new_pos = NULL;
82 hb_glyph_info_t *new_info = NULL;
83 bool separate_out = out_info != info;
85 if (unlikely (_hb_unsigned_int_mul_overflows (size, sizeof (info[0]))))
88 while (size > new_allocated)
89 new_allocated += (new_allocated >> 1) + 32;
91 ASSERT_STATIC (sizeof (info[0]) == sizeof (pos[0]));
92 if (unlikely (_hb_unsigned_int_mul_overflows (new_allocated, sizeof (info[0]))))
95 new_pos = (hb_glyph_position_t *) realloc (pos, new_allocated * sizeof (pos[0]));
96 new_info = (hb_glyph_info_t *) realloc (info, new_allocated * sizeof (info[0]));
99 if (unlikely (!new_pos || !new_info))
102 if (likely (new_pos))
105 if (likely (new_info))
108 out_info = separate_out ? (hb_glyph_info_t *) pos : info;
109 if (likely (!in_error))
110 allocated = new_allocated;
112 return likely (!in_error);
116 hb_buffer_t::make_room_for (unsigned int num_in,
117 unsigned int num_out)
119 if (unlikely (!ensure (out_len + num_out))) return FALSE;
121 if (out_info == info &&
122 out_len + num_out > idx + num_in)
124 assert (have_output);
126 out_info = (hb_glyph_info_t *) pos;
127 memcpy (out_info, info, out_len * sizeof (out_info[0]));
134 /* HarfBuzz-Internal API */
137 hb_buffer_t::reset (void)
139 if (unlikely (hb_object_is_inert (this)))
142 hb_unicode_funcs_destroy (unicode);
143 unicode = _hb_buffer_nil.unicode;
145 props = _hb_buffer_nil.props;
149 have_positions = FALSE;
156 memset (allocated_var_bytes, 0, sizeof allocated_var_bytes);
157 memset (allocated_var_owner, 0, sizeof allocated_var_owner);
163 hb_buffer_t::add (hb_codepoint_t codepoint,
165 unsigned int cluster)
167 hb_glyph_info_t *glyph;
169 if (unlikely (!ensure (len + 1))) return;
173 memset (glyph, 0, sizeof (*glyph));
174 glyph->codepoint = codepoint;
176 glyph->cluster = cluster;
182 hb_buffer_t::clear_output (void)
184 if (unlikely (hb_object_is_inert (this)))
188 have_positions = FALSE;
195 hb_buffer_t::clear_positions (void)
197 if (unlikely (hb_object_is_inert (this)))
201 have_positions = TRUE;
203 memset (pos, 0, sizeof (pos[0]) * len);
207 hb_buffer_t::swap_buffers (void)
209 if (unlikely (in_error)) return;
211 assert (have_output);
213 if (out_info != info)
215 hb_glyph_info_t *tmp_string;
218 out_info = tmp_string;
219 pos = (hb_glyph_position_t *) out_info;
231 hb_buffer_t::replace_glyphs_be16 (unsigned int num_in,
232 unsigned int num_out,
233 const uint16_t *glyph_data_be)
235 if (!make_room_for (num_in, num_out)) return;
237 hb_glyph_info_t orig_info = info[idx];
238 for (unsigned int i = 1; i < num_in; i++)
240 hb_glyph_info_t *inf = &info[idx + i];
241 orig_info.cluster = MIN (orig_info.cluster, inf->cluster);
244 hb_glyph_info_t *pinfo = &out_info[out_len];
245 for (unsigned int i = 0; i < num_out; i++)
248 pinfo->codepoint = hb_be_uint16 (glyph_data_be[i]);
257 hb_buffer_t::output_glyph (hb_codepoint_t glyph_index)
259 if (!make_room_for (0, 1)) return;
261 out_info[out_len] = info[idx];
262 out_info[out_len].codepoint = glyph_index;
268 hb_buffer_t::copy_glyph (void)
270 if (!make_room_for (0, 1)) return;
272 out_info[out_len] = info[idx];
278 hb_buffer_t::replace_glyph (hb_codepoint_t glyph_index)
280 out_info[out_len] = info[idx];
281 out_info[out_len].codepoint = glyph_index;
288 hb_buffer_t::next_glyph (void)
292 if (out_info != info)
294 if (unlikely (!ensure (out_len + 1))) return;
295 out_info[out_len] = info[idx];
297 else if (out_len != idx)
298 out_info[out_len] = info[idx];
307 hb_buffer_t::set_masks (hb_mask_t value,
309 unsigned int cluster_start,
310 unsigned int cluster_end)
312 hb_mask_t not_mask = ~mask;
318 if (cluster_start == 0 && cluster_end == (unsigned int)-1) {
319 unsigned int count = len;
320 for (unsigned int i = 0; i < count; i++)
321 info[i].mask = (info[i].mask & not_mask) | value;
325 unsigned int count = len;
326 for (unsigned int i = 0; i < count; i++)
327 if (cluster_start <= info[i].cluster && info[i].cluster < cluster_end)
328 info[i].mask = (info[i].mask & not_mask) | value;
332 hb_buffer_t::reverse_range (unsigned int start,
337 if (start == end - 1)
340 for (i = start, j = end - 1; i < j; i++, j--) {
349 for (i = start, j = end - 1; i < j; i++, j--) {
350 hb_glyph_position_t t;
360 hb_buffer_t::reverse (void)
365 reverse_range (0, len);
369 hb_buffer_t::reverse_clusters (void)
371 unsigned int i, start, count, last_cluster;
380 last_cluster = info[0].cluster;
381 for (i = 1; i < count; i++) {
382 if (last_cluster != info[i].cluster) {
383 reverse_range (start, i);
385 last_cluster = info[i].cluster;
388 reverse_range (start, i);
392 void hb_buffer_t::allocate_var (unsigned int byte_i, unsigned int count, const char *owner)
394 assert (byte_i < 8 && byte_i + count < 8);
395 for (unsigned int i = byte_i; i < byte_i + count; i++) {
396 assert (!allocated_var_bytes[i]);
397 allocated_var_bytes[i]++;
398 allocated_var_owner[i] = owner;
402 void hb_buffer_t::deallocate_var (unsigned int byte_i, unsigned int count, const char *owner)
404 assert (byte_i < 8 && byte_i + count < 8);
405 for (unsigned int i = byte_i; i < byte_i + count; i++) {
406 assert (allocated_var_bytes[i] && allocated_var_owner[i] == owner);
407 allocated_var_bytes[i]--;
415 hb_buffer_create (unsigned int pre_alloc_size)
419 if (!(buffer = hb_object_create<hb_buffer_t> ()))
420 return &_hb_buffer_nil;
424 if (pre_alloc_size && !buffer->ensure (pre_alloc_size)) {
425 hb_buffer_destroy (buffer);
426 return &_hb_buffer_nil;
433 hb_buffer_get_empty (void)
435 return &_hb_buffer_nil;
439 hb_buffer_reference (hb_buffer_t *buffer)
441 return hb_object_reference (buffer);
445 hb_buffer_destroy (hb_buffer_t *buffer)
447 if (!hb_object_destroy (buffer)) return;
449 hb_unicode_funcs_destroy (buffer->unicode);
458 hb_buffer_set_user_data (hb_buffer_t *buffer,
459 hb_user_data_key_t *key,
461 hb_destroy_func_t destroy)
463 return hb_object_set_user_data (buffer, key, data, destroy);
467 hb_buffer_get_user_data (hb_buffer_t *buffer,
468 hb_user_data_key_t *key)
470 return hb_object_get_user_data (buffer, key);
475 hb_buffer_set_unicode_funcs (hb_buffer_t *buffer,
476 hb_unicode_funcs_t *unicode)
478 if (unlikely (hb_object_is_inert (buffer)))
482 unicode = _hb_buffer_nil.unicode;
484 hb_unicode_funcs_reference (unicode);
485 hb_unicode_funcs_destroy (buffer->unicode);
486 buffer->unicode = unicode;
490 hb_buffer_get_unicode_funcs (hb_buffer_t *buffer)
492 return buffer->unicode;
496 hb_buffer_set_direction (hb_buffer_t *buffer,
497 hb_direction_t direction)
500 if (unlikely (hb_object_is_inert (buffer)))
503 buffer->props.direction = direction;
507 hb_buffer_get_direction (hb_buffer_t *buffer)
509 return buffer->props.direction;
513 hb_buffer_set_script (hb_buffer_t *buffer,
516 if (unlikely (hb_object_is_inert (buffer)))
519 buffer->props.script = script;
523 hb_buffer_get_script (hb_buffer_t *buffer)
525 return buffer->props.script;
529 hb_buffer_set_language (hb_buffer_t *buffer,
530 hb_language_t language)
532 if (unlikely (hb_object_is_inert (buffer)))
535 buffer->props.language = language;
539 hb_buffer_get_language (hb_buffer_t *buffer)
541 return buffer->props.language;
546 hb_buffer_reset (hb_buffer_t *buffer)
552 hb_buffer_pre_allocate (hb_buffer_t *buffer, unsigned int size)
554 return buffer->ensure (size);
558 hb_buffer_allocation_successful (hb_buffer_t *buffer)
560 return !buffer->in_error;
564 hb_buffer_add (hb_buffer_t *buffer,
565 hb_codepoint_t codepoint,
567 unsigned int cluster)
569 buffer->add (codepoint, mask, cluster);
573 hb_buffer_set_length (hb_buffer_t *buffer,
576 if (!buffer->ensure (length))
579 /* Wipe the new space */
580 if (length > buffer->len) {
581 memset (buffer->info + buffer->len, 0, sizeof (buffer->info[0]) * (length - buffer->len));
582 if (buffer->have_positions)
583 memset (buffer->pos + buffer->len, 0, sizeof (buffer->pos[0]) * (length - buffer->len));
586 buffer->len = length;
591 hb_buffer_get_length (hb_buffer_t *buffer)
596 /* Return value valid as long as buffer not modified */
598 hb_buffer_get_glyph_infos (hb_buffer_t *buffer,
599 unsigned int *length)
602 *length = buffer->len;
604 return (hb_glyph_info_t *) buffer->info;
607 /* Return value valid as long as buffer not modified */
608 hb_glyph_position_t *
609 hb_buffer_get_glyph_positions (hb_buffer_t *buffer,
610 unsigned int *length)
612 if (!buffer->have_positions)
613 buffer->clear_positions ();
616 *length = buffer->len;
618 return (hb_glyph_position_t *) buffer->pos;
622 hb_buffer_reverse (hb_buffer_t *buffer)
628 hb_buffer_reverse_clusters (hb_buffer_t *buffer)
630 buffer->reverse_clusters ();
635 const T *next = (const T *) text + item_offset; \
636 const T *end = next + item_length; \
637 while (next < end) { \
639 const T *old_next = next; \
640 next = UTF_NEXT (next, end, u); \
641 hb_buffer_add (buffer, u, 1, old_next - (const T *) text); \
646 #define UTF8_COMPUTE(Char, Mask, Len) \
647 if (Char < 128) { Len = 1; Mask = 0x7f; } \
648 else if ((Char & 0xe0) == 0xc0) { Len = 2; Mask = 0x1f; } \
649 else if ((Char & 0xf0) == 0xe0) { Len = 3; Mask = 0x0f; } \
650 else if ((Char & 0xf8) == 0xf0) { Len = 4; Mask = 0x07; } \
653 static inline const uint8_t *
654 hb_utf8_next (const uint8_t *text,
656 hb_codepoint_t *unicode)
659 unsigned int mask, len;
661 /* TODO check for overlong sequences? */
663 UTF8_COMPUTE (c, mask, len);
664 if (unlikely (!len || (unsigned int) (end - text) < len)) {
668 hb_codepoint_t result;
671 for (i = 1; i < len; i++)
673 if (unlikely ((text[i] & 0xc0) != 0x80))
679 result |= (text[i] & 0x3f);
687 hb_buffer_add_utf8 (hb_buffer_t *buffer,
689 unsigned int text_length HB_UNUSED,
690 unsigned int item_offset,
691 unsigned int item_length)
693 #define UTF_NEXT(S, E, U) hb_utf8_next (S, E, &(U))
698 static inline const uint16_t *
699 hb_utf16_next (const uint16_t *text,
701 hb_codepoint_t *unicode)
703 uint16_t c = *text++;
705 if (unlikely (c >= 0xd800 && c < 0xdc00)) {
708 if (text < end && ((l = *text), likely (l >= 0xdc00 && l < 0xe000))) {
710 *unicode = ((hb_codepoint_t) ((c) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000);
721 hb_buffer_add_utf16 (hb_buffer_t *buffer,
722 const uint16_t *text,
723 unsigned int text_length HB_UNUSED,
724 unsigned int item_offset,
725 unsigned int item_length)
727 #define UTF_NEXT(S, E, U) hb_utf16_next (S, E, &(U))
733 hb_buffer_add_utf32 (hb_buffer_t *buffer,
734 const uint32_t *text,
735 unsigned int text_length HB_UNUSED,
736 unsigned int item_offset,
737 unsigned int item_length)
739 #define UTF_NEXT(S, E, U) ((U) = *(S), (S)+1)