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 Xhb_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 = Xhb_buffer_nil.unicode;
145 props = Xhb_buffer_nil.props;
149 have_positions = FALSE;
161 hb_buffer_t::add (hb_codepoint_t codepoint,
163 unsigned int cluster)
165 hb_glyph_info_t *glyph;
167 if (unlikely (!ensure (len + 1))) return;
171 memset (glyph, 0, sizeof (*glyph));
172 glyph->codepoint = codepoint;
174 glyph->cluster = cluster;
180 hb_buffer_t::clear_output (void)
182 if (unlikely (hb_object_is_inert (this)))
186 have_positions = FALSE;
193 hb_buffer_t::clear_positions (void)
195 if (unlikely (hb_object_is_inert (this)))
199 have_positions = TRUE;
201 memset (pos, 0, sizeof (pos[0]) * len);
205 hb_buffer_t::swap_buffers (void)
207 if (unlikely (in_error)) return;
209 assert (have_output);
211 if (out_info != info)
213 hb_glyph_info_t *tmp_string;
216 out_info = tmp_string;
217 pos = (hb_glyph_position_t *) out_info;
229 hb_buffer_t::replace_glyphs_be16 (unsigned int num_in,
230 unsigned int num_out,
231 const uint16_t *glyph_data_be)
233 if (!make_room_for (num_in, num_out)) return;
235 hb_glyph_info_t orig_info = info[idx];
236 for (unsigned int i = 1; i < num_in; i++)
238 hb_glyph_info_t *inf = &info[idx + i];
239 orig_info.cluster = MIN (orig_info.cluster, inf->cluster);
242 hb_glyph_info_t *pinfo = &out_info[out_len];
243 for (unsigned int i = 0; i < num_out; i++)
246 pinfo->codepoint = hb_be_uint16 (glyph_data_be[i]);
255 hb_buffer_t::output_glyph (hb_codepoint_t glyph_index)
257 if (!make_room_for (0, 1)) return;
259 out_info[out_len] = info[idx];
260 out_info[out_len].codepoint = glyph_index;
266 hb_buffer_t::copy_glyph (void)
268 if (!make_room_for (0, 1)) return;
270 out_info[out_len] = info[idx];
276 hb_buffer_t::replace_glyph (hb_codepoint_t glyph_index)
278 out_info[out_len] = info[idx];
279 out_info[out_len].codepoint = glyph_index;
286 hb_buffer_t::next_glyph (void)
290 if (out_info != info)
292 if (unlikely (!ensure (out_len + 1))) return;
293 out_info[out_len] = info[idx];
295 else if (out_len != idx)
296 out_info[out_len] = info[idx];
305 hb_buffer_t::set_masks (hb_mask_t value,
307 unsigned int cluster_start,
308 unsigned int cluster_end)
310 hb_mask_t not_mask = ~mask;
316 if (cluster_start == 0 && cluster_end == (unsigned int)-1) {
317 unsigned int count = len;
318 for (unsigned int i = 0; i < count; i++)
319 info[i].mask = (info[i].mask & not_mask) | value;
323 unsigned int count = len;
324 for (unsigned int i = 0; i < count; i++)
325 if (cluster_start <= info[i].cluster && info[i].cluster < cluster_end)
326 info[i].mask = (info[i].mask & not_mask) | value;
330 hb_buffer_t::reverse_range (unsigned int start,
335 if (start == end - 1)
338 for (i = start, j = end - 1; i < j; i++, j--) {
347 for (i = start, j = end - 1; i < j; i++, j--) {
348 hb_glyph_position_t t;
358 hb_buffer_t::reverse (void)
363 reverse_range (0, len);
367 hb_buffer_t::reverse_clusters (void)
369 unsigned int i, start, count, last_cluster;
378 last_cluster = info[0].cluster;
379 for (i = 1; i < count; i++) {
380 if (last_cluster != info[i].cluster) {
381 reverse_range (start, i);
383 last_cluster = info[i].cluster;
386 reverse_range (start, i);
393 hb_buffer_create (unsigned int pre_alloc_size)
397 if (!(buffer = hb_object_create<hb_buffer_t> ()))
398 return &Xhb_buffer_nil;
402 if (pre_alloc_size && !buffer->ensure (pre_alloc_size)) {
403 hb_buffer_destroy (buffer);
404 return &Xhb_buffer_nil;
411 hb_buffer_get_empty (void)
413 return &Xhb_buffer_nil;
417 hb_buffer_reference (hb_buffer_t *buffer)
419 return hb_object_reference (buffer);
423 hb_buffer_destroy (hb_buffer_t *buffer)
425 if (!hb_object_destroy (buffer)) return;
427 hb_unicode_funcs_destroy (buffer->unicode);
436 hb_buffer_set_user_data (hb_buffer_t *buffer,
437 hb_user_data_key_t *key,
439 hb_destroy_func_t destroy)
441 return hb_object_set_user_data (buffer, key, data, destroy);
445 hb_buffer_get_user_data (hb_buffer_t *buffer,
446 hb_user_data_key_t *key)
448 return hb_object_get_user_data (buffer, key);
453 hb_buffer_set_unicode_funcs (hb_buffer_t *buffer,
454 hb_unicode_funcs_t *unicode)
456 if (unlikely (hb_object_is_inert (buffer)))
460 unicode = Xhb_buffer_nil.unicode;
462 hb_unicode_funcs_reference (unicode);
463 hb_unicode_funcs_destroy (buffer->unicode);
464 buffer->unicode = unicode;
468 hb_buffer_get_unicode_funcs (hb_buffer_t *buffer)
470 return buffer->unicode;
474 hb_buffer_set_direction (hb_buffer_t *buffer,
475 hb_direction_t direction)
478 if (unlikely (hb_object_is_inert (buffer)))
481 buffer->props.direction = direction;
485 hb_buffer_get_direction (hb_buffer_t *buffer)
487 return buffer->props.direction;
491 hb_buffer_set_script (hb_buffer_t *buffer,
494 if (unlikely (hb_object_is_inert (buffer)))
497 buffer->props.script = script;
501 hb_buffer_get_script (hb_buffer_t *buffer)
503 return buffer->props.script;
507 hb_buffer_set_language (hb_buffer_t *buffer,
508 hb_language_t language)
510 if (unlikely (hb_object_is_inert (buffer)))
513 buffer->props.language = language;
517 hb_buffer_get_language (hb_buffer_t *buffer)
519 return buffer->props.language;
524 hb_buffer_reset (hb_buffer_t *buffer)
530 hb_buffer_pre_allocate (hb_buffer_t *buffer, unsigned int size)
532 return buffer->ensure (size);
536 hb_buffer_allocation_successful (hb_buffer_t *buffer)
538 return !buffer->in_error;
542 hb_buffer_add (hb_buffer_t *buffer,
543 hb_codepoint_t codepoint,
545 unsigned int cluster)
547 buffer->add (codepoint, mask, cluster);
551 hb_buffer_set_length (hb_buffer_t *buffer,
554 if (!buffer->ensure (length))
557 /* Wipe the new space */
558 if (length > buffer->len) {
559 memset (buffer->info + buffer->len, 0, sizeof (buffer->info[0]) * (length - buffer->len));
560 if (buffer->have_positions)
561 memset (buffer->pos + buffer->len, 0, sizeof (buffer->pos[0]) * (length - buffer->len));
564 buffer->len = length;
569 hb_buffer_get_length (hb_buffer_t *buffer)
574 /* Return value valid as long as buffer not modified */
576 hb_buffer_get_glyph_infos (hb_buffer_t *buffer,
577 unsigned int *length)
580 *length = buffer->len;
582 return (hb_glyph_info_t *) buffer->info;
585 /* Return value valid as long as buffer not modified */
586 hb_glyph_position_t *
587 hb_buffer_get_glyph_positions (hb_buffer_t *buffer,
588 unsigned int *length)
590 if (!buffer->have_positions)
591 buffer->clear_positions ();
594 *length = buffer->len;
596 return (hb_glyph_position_t *) buffer->pos;
600 hb_buffer_reverse (hb_buffer_t *buffer)
606 hb_buffer_reverse_clusters (hb_buffer_t *buffer)
608 buffer->reverse_clusters ();
613 const T *next = (const T *) text + item_offset; \
614 const T *end = next + item_length; \
615 while (next < end) { \
617 const T *old_next = next; \
618 next = UTF_NEXT (next, end, u); \
619 hb_buffer_add (buffer, u, 1, old_next - (const T *) text); \
624 #define UTF8_COMPUTE(Char, Mask, Len) \
625 if (Char < 128) { Len = 1; Mask = 0x7f; } \
626 else if ((Char & 0xe0) == 0xc0) { Len = 2; Mask = 0x1f; } \
627 else if ((Char & 0xf0) == 0xe0) { Len = 3; Mask = 0x0f; } \
628 else if ((Char & 0xf8) == 0xf0) { Len = 4; Mask = 0x07; } \
631 static inline const uint8_t *
632 hb_utf8_next (const uint8_t *text,
634 hb_codepoint_t *unicode)
637 unsigned int mask, len;
639 /* TODO check for overlong sequences? */
641 UTF8_COMPUTE (c, mask, len);
642 if (unlikely (!len || (unsigned int) (end - text) < len)) {
646 hb_codepoint_t result;
649 for (i = 1; i < len; i++)
651 if (unlikely ((text[i] & 0xc0) != 0x80))
657 result |= (text[i] & 0x3f);
665 hb_buffer_add_utf8 (hb_buffer_t *buffer,
667 unsigned int text_length HB_UNUSED,
668 unsigned int item_offset,
669 unsigned int item_length)
671 #define UTF_NEXT(S, E, U) hb_utf8_next (S, E, &(U))
676 static inline const uint16_t *
677 hb_utf16_next (const uint16_t *text,
679 hb_codepoint_t *unicode)
681 uint16_t c = *text++;
683 if (unlikely (c >= 0xd800 && c < 0xdc00)) {
686 if (text < end && ((l = *text), likely (l >= 0xdc00 && l < 0xe000))) {
688 *unicode = ((hb_codepoint_t) ((c) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000);
699 hb_buffer_add_utf16 (hb_buffer_t *buffer,
700 const uint16_t *text,
701 unsigned int text_length HB_UNUSED,
702 unsigned int item_offset,
703 unsigned int item_length)
705 #define UTF_NEXT(S, E, U) hb_utf16_next (S, E, &(U))
711 hb_buffer_add_utf32 (hb_buffer_t *buffer,
712 const uint32_t *text,
713 unsigned int text_length HB_UNUSED,
714 unsigned int item_offset,
715 unsigned int item_length)
717 #define UTF_NEXT(S, E, U) ((U) = *(S), (S)+1)