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() 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() then
67 * switches info and out_info.
72 _hb_buffer_enlarge (hb_buffer_t *buffer, unsigned int size)
74 if (unlikely (buffer->in_error))
77 unsigned int new_allocated = buffer->allocated;
78 hb_glyph_position_t *new_pos = NULL;
79 hb_glyph_info_t *new_info = NULL;
80 bool overflows = FALSE;
81 bool separate_out = buffer->out_info != buffer->info;
83 overflows = size >= ((unsigned int) -1) / sizeof (buffer->info[0]);
84 if (unlikely (overflows))
87 while (size > new_allocated)
88 new_allocated += (new_allocated >> 1) + 32;
90 ASSERT_STATIC (sizeof (buffer->info[0]) == sizeof (buffer->pos[0]));
91 overflows = new_allocated >= ((unsigned int) -1) / sizeof (buffer->info[0]);
92 if (unlikely (overflows))
95 new_pos = (hb_glyph_position_t *) realloc (buffer->pos, new_allocated * sizeof (buffer->pos[0]));
96 new_info = (hb_glyph_info_t *) realloc (buffer->info, new_allocated * sizeof (buffer->info[0]));
99 if (unlikely (!new_pos || !new_info))
100 buffer->in_error = TRUE;
102 if (likely (new_pos))
103 buffer->pos = new_pos;
105 if (likely (new_info))
106 buffer->info = new_info;
108 buffer->out_info = separate_out ? (hb_glyph_info_t *) buffer->pos : buffer->info;
109 if (likely (!buffer->in_error))
110 buffer->allocated = new_allocated;
112 return likely (!buffer->in_error);
115 static inline hb_bool_t
116 _hb_buffer_ensure (hb_buffer_t *buffer, unsigned int size)
118 return likely (size <= buffer->allocated) ? TRUE : _hb_buffer_enlarge (buffer, size);
121 static inline hb_bool_t
122 _hb_buffer_ensure_separate (hb_buffer_t *buffer, unsigned int size)
124 if (unlikely (!size || !_hb_buffer_ensure (buffer, size))) return FALSE;
126 if (buffer->out_info == buffer->info)
128 assert (buffer->have_output);
130 buffer->out_info = (hb_glyph_info_t *) buffer->pos;
131 memcpy (buffer->out_info, buffer->info, buffer->out_len * sizeof (buffer->out_info[0]));
141 hb_buffer_create (unsigned int pre_alloc_size)
145 if (!(buffer = hb_object_create<hb_buffer_t> ()))
146 return &_hb_buffer_nil;
149 _hb_buffer_ensure (buffer, pre_alloc_size);
151 hb_buffer_reset (buffer);
157 hb_buffer_reference (hb_buffer_t *buffer)
159 return hb_object_reference (buffer);
163 hb_buffer_destroy (hb_buffer_t *buffer)
165 if (!hb_object_destroy (buffer)) return;
167 hb_unicode_funcs_destroy (buffer->unicode);
176 hb_buffer_set_user_data (hb_buffer_t *buffer,
177 hb_user_data_key_t *key,
179 hb_destroy_func_t destroy)
181 return hb_object_set_user_data (buffer, key, data, destroy);
185 hb_buffer_get_user_data (hb_buffer_t *buffer,
186 hb_user_data_key_t *key)
188 return hb_object_get_user_data (buffer, key);
193 hb_buffer_set_unicode_funcs (hb_buffer_t *buffer,
194 hb_unicode_funcs_t *unicode)
196 if (unlikely (hb_object_is_inert (buffer)))
200 unicode = _hb_buffer_nil.unicode;
202 hb_unicode_funcs_reference (unicode);
203 hb_unicode_funcs_destroy (buffer->unicode);
204 buffer->unicode = unicode;
208 hb_buffer_get_unicode_funcs (hb_buffer_t *buffer)
210 return buffer->unicode;
214 hb_buffer_set_direction (hb_buffer_t *buffer,
215 hb_direction_t direction)
218 if (unlikely (hb_object_is_inert (buffer)))
221 buffer->props.direction = direction;
225 hb_buffer_get_direction (hb_buffer_t *buffer)
227 return buffer->props.direction;
231 hb_buffer_set_script (hb_buffer_t *buffer,
234 if (unlikely (hb_object_is_inert (buffer)))
237 buffer->props.script = script;
241 hb_buffer_get_script (hb_buffer_t *buffer)
243 return buffer->props.script;
247 hb_buffer_set_language (hb_buffer_t *buffer,
248 hb_language_t language)
250 if (unlikely (hb_object_is_inert (buffer)))
253 buffer->props.language = language;
257 hb_buffer_get_language (hb_buffer_t *buffer)
259 return buffer->props.language;
264 hb_buffer_reset (hb_buffer_t *buffer)
266 if (unlikely (hb_object_is_inert (buffer)))
269 hb_unicode_funcs_destroy (buffer->unicode);
270 buffer->unicode = _hb_buffer_nil.unicode;
272 buffer->props = _hb_buffer_nil.props;
274 buffer->in_error = FALSE;
275 buffer->have_output = FALSE;
276 buffer->have_positions = FALSE;
284 buffer->out_info = buffer->info;
288 hb_buffer_pre_allocate (hb_buffer_t *buffer, unsigned int size)
290 return _hb_buffer_ensure (buffer, size);
294 hb_buffer_allocation_successful (hb_buffer_t *buffer)
296 return !buffer->in_error;
300 hb_buffer_add (hb_buffer_t *buffer,
301 hb_codepoint_t codepoint,
303 unsigned int cluster)
305 hb_glyph_info_t *glyph;
307 if (unlikely (!_hb_buffer_ensure (buffer, buffer->len + 1))) return;
309 glyph = &buffer->info[buffer->len];
311 memset (glyph, 0, sizeof (*glyph));
312 glyph->codepoint = codepoint;
314 glyph->cluster = cluster;
319 /* HarfBuzz-Internal API */
322 _hb_buffer_clear_output (hb_buffer_t *buffer)
324 if (unlikely (hb_object_is_inert (buffer)))
327 buffer->have_output = TRUE;
328 buffer->have_positions = FALSE;
331 buffer->out_info = buffer->info;
335 _hb_buffer_clear_positions (hb_buffer_t *buffer)
337 if (unlikely (hb_object_is_inert (buffer)))
340 buffer->have_output = FALSE;
341 buffer->have_positions = TRUE;
343 memset (buffer->pos, 0, sizeof (buffer->pos[0]) * buffer->len);
347 _hb_buffer_swap (hb_buffer_t *buffer)
349 if (unlikely (buffer->in_error)) return;
351 assert (buffer->have_output);
353 if (buffer->out_info != buffer->info)
355 hb_glyph_info_t *tmp_string;
356 tmp_string = buffer->info;
357 buffer->info = buffer->out_info;
358 buffer->out_info = tmp_string;
359 buffer->pos = (hb_glyph_position_t *) buffer->out_info;
364 buffer->len = buffer->out_len;
365 buffer->out_len = tmp;
371 _hb_buffer_replace_glyphs_be16 (hb_buffer_t *buffer,
373 unsigned int num_out,
374 const uint16_t *glyph_data_be)
376 if (buffer->out_info != buffer->info ||
377 buffer->out_len + num_out > buffer->i + num_in)
379 if (unlikely (!_hb_buffer_ensure_separate (buffer, buffer->out_len + num_out)))
383 hb_glyph_info_t orig_info = buffer->info[buffer->i];
385 for (unsigned int i = 0; i < num_out; i++)
387 hb_glyph_info_t *info = &buffer->out_info[buffer->out_len + i];
389 info->codepoint = hb_be_uint16 (glyph_data_be[i]);
393 buffer->out_len += num_out;
397 _hb_buffer_replace_glyph (hb_buffer_t *buffer,
398 hb_codepoint_t glyph_index)
400 hb_glyph_info_t *info;
402 if (buffer->out_info != buffer->info)
404 if (unlikely (!_hb_buffer_ensure (buffer, buffer->out_len + 1))) return;
405 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
407 else if (buffer->out_len != buffer->i)
408 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
410 info = &buffer->out_info[buffer->out_len];
411 info->codepoint = glyph_index;
418 _hb_buffer_next_glyph (hb_buffer_t *buffer)
420 if (buffer->have_output)
422 if (buffer->out_info != buffer->info)
424 if (unlikely (!_hb_buffer_ensure (buffer, buffer->out_len + 1))) return;
425 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
427 else if (buffer->out_len != buffer->i)
428 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
437 _hb_buffer_reset_masks (hb_buffer_t *buffer,
440 unsigned int count = buffer->len;
441 for (unsigned int i = 0; i < count; i++)
442 buffer->info[i].mask = mask;
446 _hb_buffer_add_masks (hb_buffer_t *buffer,
449 unsigned int count = buffer->len;
450 for (unsigned int i = 0; i < count; i++)
451 buffer->info[i].mask |= mask;
455 _hb_buffer_set_masks (hb_buffer_t *buffer,
458 unsigned int cluster_start,
459 unsigned int cluster_end)
461 hb_mask_t not_mask = ~mask;
467 if (cluster_start == 0 && cluster_end == (unsigned int)-1) {
468 unsigned int count = buffer->len;
469 for (unsigned int i = 0; i < count; i++)
470 buffer->info[i].mask = (buffer->info[i].mask & not_mask) | value;
474 /* XXX can't bsearch since .cluster may not be sorted. */
475 /* Binary search to find the start position and go from there. */
476 unsigned int min = 0, max = buffer->len;
479 unsigned int mid = min + ((max - min) / 2);
480 if (buffer->info[mid].cluster < cluster_start)
485 unsigned int count = buffer->len;
486 for (unsigned int i = min; i < count && buffer->info[i].cluster < cluster_end; i++)
487 buffer->info[i].mask = (buffer->info[i].mask & not_mask) | value;
491 /* Public API again */
494 hb_buffer_set_length (hb_buffer_t *buffer,
497 if (!_hb_buffer_ensure (buffer, length))
500 /* Wipe the new space */
501 if (length > buffer->len) {
502 memset (buffer->info + buffer->len, 0, sizeof (buffer->info[0]) * (length - buffer->len));
503 if (buffer->have_positions)
504 memset (buffer->pos + buffer->len, 0, sizeof (buffer->pos[0]) * (length - buffer->len));
507 buffer->len = length;
512 hb_buffer_get_length (hb_buffer_t *buffer)
517 /* Return value valid as long as buffer not modified */
519 hb_buffer_get_glyph_infos (hb_buffer_t *buffer,
520 unsigned int *length)
523 *length = buffer->len;
525 return (hb_glyph_info_t *) buffer->info;
528 /* Return value valid as long as buffer not modified */
529 hb_glyph_position_t *
530 hb_buffer_get_glyph_positions (hb_buffer_t *buffer,
531 unsigned int *length)
533 if (!buffer->have_positions)
534 _hb_buffer_clear_positions (buffer);
537 *length = buffer->len;
539 return (hb_glyph_position_t *) buffer->pos;
544 reverse_range (hb_buffer_t *buffer,
550 for (i = start, j = end - 1; i < j; i++, j--) {
554 buffer->info[i] = buffer->info[j];
559 for (i = 0, j = end - 1; i < j; i++, j--) {
560 hb_glyph_position_t t;
563 buffer->pos[i] = buffer->pos[j];
570 hb_buffer_reverse (hb_buffer_t *buffer)
572 if (unlikely (!buffer->len))
575 reverse_range (buffer, 0, buffer->len);
579 hb_buffer_reverse_clusters (hb_buffer_t *buffer)
581 unsigned int i, start, count, last_cluster;
583 if (unlikely (!buffer->len))
586 hb_buffer_reverse (buffer);
590 last_cluster = buffer->info[0].cluster;
591 for (i = 1; i < count; i++) {
592 if (last_cluster != buffer->info[i].cluster) {
593 reverse_range (buffer, start, i);
595 last_cluster = buffer->info[i].cluster;
598 reverse_range (buffer, start, i);
604 const T *next = (const T *) text + item_offset; \
605 const T *end = next + item_length; \
606 while (next < end) { \
608 const T *old_next = next; \
609 next = UTF_NEXT (next, end, u); \
610 hb_buffer_add (buffer, u, 1, old_next - (const T *) text); \
615 #define UTF8_COMPUTE(Char, Mask, Len) \
616 if (Char < 128) { Len = 1; Mask = 0x7f; } \
617 else if ((Char & 0xe0) == 0xc0) { Len = 2; Mask = 0x1f; } \
618 else if ((Char & 0xf0) == 0xe0) { Len = 3; Mask = 0x0f; } \
619 else if ((Char & 0xf8) == 0xf0) { Len = 4; Mask = 0x07; } \
622 static inline const uint8_t *
623 hb_utf8_next (const uint8_t *text,
625 hb_codepoint_t *unicode)
628 unsigned int mask, len;
630 /* TODO check for overlong sequences? also: optimize? */
632 UTF8_COMPUTE (c, mask, len);
633 if (unlikely (!len || (unsigned int) (end - text) < len)) {
637 hb_codepoint_t result;
640 for (i = 1; i < len; i++)
642 if (unlikely ((text[i] & 0xc0) != 0x80))
648 result |= (text[i] & 0x3f);
656 hb_buffer_add_utf8 (hb_buffer_t *buffer,
658 unsigned int text_length HB_UNUSED,
659 unsigned int item_offset,
660 unsigned int item_length)
662 #define UTF_NEXT(S, E, U) hb_utf8_next (S, E, &(U))
667 static inline const uint16_t *
668 hb_utf16_next (const uint16_t *text,
670 hb_codepoint_t *unicode)
672 uint16_t c = *text++;
674 if (unlikely (c >= 0xd800 && c < 0xdc00)) {
677 if (text < end && ((l = *text), likely (l >= 0xdc00 && l < 0xe000))) {
679 *unicode = ((hb_codepoint_t) ((c) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000);
690 hb_buffer_add_utf16 (hb_buffer_t *buffer,
691 const uint16_t *text,
692 unsigned int text_length HB_UNUSED,
693 unsigned int item_offset,
694 unsigned int item_length)
696 #define UTF_NEXT(S, E, U) hb_utf16_next (S, E, &(U))
702 hb_buffer_add_utf32 (hb_buffer_t *buffer,
703 const uint32_t *text,
704 unsigned int text_length HB_UNUSED,
705 unsigned int item_offset,
706 unsigned int item_length)
708 #define UTF_NEXT(S, E, U) ((U) = *(S), (S)+1)