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 separate_out = buffer->out_info != buffer->info;
82 if (unlikely (_hb_unsigned_int_mul_overflows (size, sizeof (buffer->info[0]))))
85 while (size > new_allocated)
86 new_allocated += (new_allocated >> 1) + 32;
88 ASSERT_STATIC (sizeof (buffer->info[0]) == sizeof (buffer->pos[0]));
89 if (unlikely (_hb_unsigned_int_mul_overflows (new_allocated, sizeof (buffer->info[0]))))
92 new_pos = (hb_glyph_position_t *) realloc (buffer->pos, new_allocated * sizeof (buffer->pos[0]));
93 new_info = (hb_glyph_info_t *) realloc (buffer->info, new_allocated * sizeof (buffer->info[0]));
96 if (unlikely (!new_pos || !new_info))
97 buffer->in_error = TRUE;
100 buffer->pos = new_pos;
102 if (likely (new_info))
103 buffer->info = new_info;
105 buffer->out_info = separate_out ? (hb_glyph_info_t *) buffer->pos : buffer->info;
106 if (likely (!buffer->in_error))
107 buffer->allocated = new_allocated;
109 return likely (!buffer->in_error);
112 static inline hb_bool_t
113 _hb_buffer_ensure (hb_buffer_t *buffer, unsigned int size)
115 return likely (size <= buffer->allocated) ? TRUE : _hb_buffer_enlarge (buffer, size);
118 static inline hb_bool_t
119 _hb_buffer_ensure_separate (hb_buffer_t *buffer, unsigned int size)
121 if (unlikely (!size || !_hb_buffer_ensure (buffer, size))) return FALSE;
123 if (buffer->out_info == buffer->info)
125 assert (buffer->have_output);
127 buffer->out_info = (hb_glyph_info_t *) buffer->pos;
128 memcpy (buffer->out_info, buffer->info, buffer->out_len * sizeof (buffer->out_info[0]));
138 hb_buffer_create (unsigned int pre_alloc_size)
142 if (!(buffer = hb_object_create<hb_buffer_t> ()))
143 return &_hb_buffer_nil;
145 hb_buffer_reset (buffer);
147 if (pre_alloc_size && !_hb_buffer_ensure (buffer, pre_alloc_size)) {
148 hb_buffer_destroy (buffer);
149 return &_hb_buffer_nil;
156 hb_buffer_reference (hb_buffer_t *buffer)
158 return hb_object_reference (buffer);
162 hb_buffer_destroy (hb_buffer_t *buffer)
164 if (!hb_object_destroy (buffer)) return;
166 hb_unicode_funcs_destroy (buffer->unicode);
175 hb_buffer_set_user_data (hb_buffer_t *buffer,
176 hb_user_data_key_t *key,
178 hb_destroy_func_t destroy)
180 return hb_object_set_user_data (buffer, key, data, destroy);
184 hb_buffer_get_user_data (hb_buffer_t *buffer,
185 hb_user_data_key_t *key)
187 return hb_object_get_user_data (buffer, key);
192 hb_buffer_set_unicode_funcs (hb_buffer_t *buffer,
193 hb_unicode_funcs_t *unicode)
195 if (unlikely (hb_object_is_inert (buffer)))
199 unicode = _hb_buffer_nil.unicode;
201 hb_unicode_funcs_reference (unicode);
202 hb_unicode_funcs_destroy (buffer->unicode);
203 buffer->unicode = unicode;
207 hb_buffer_get_unicode_funcs (hb_buffer_t *buffer)
209 return buffer->unicode;
213 hb_buffer_set_direction (hb_buffer_t *buffer,
214 hb_direction_t direction)
217 if (unlikely (hb_object_is_inert (buffer)))
220 buffer->props.direction = direction;
224 hb_buffer_get_direction (hb_buffer_t *buffer)
226 return buffer->props.direction;
230 hb_buffer_set_script (hb_buffer_t *buffer,
233 if (unlikely (hb_object_is_inert (buffer)))
236 buffer->props.script = script;
240 hb_buffer_get_script (hb_buffer_t *buffer)
242 return buffer->props.script;
246 hb_buffer_set_language (hb_buffer_t *buffer,
247 hb_language_t language)
249 if (unlikely (hb_object_is_inert (buffer)))
252 buffer->props.language = language;
256 hb_buffer_get_language (hb_buffer_t *buffer)
258 return buffer->props.language;
263 hb_buffer_reset (hb_buffer_t *buffer)
265 if (unlikely (hb_object_is_inert (buffer)))
268 hb_unicode_funcs_destroy (buffer->unicode);
269 buffer->unicode = _hb_buffer_nil.unicode;
271 buffer->props = _hb_buffer_nil.props;
273 buffer->in_error = FALSE;
274 buffer->have_output = FALSE;
275 buffer->have_positions = FALSE;
283 buffer->out_info = buffer->info;
287 hb_buffer_pre_allocate (hb_buffer_t *buffer, unsigned int size)
289 return _hb_buffer_ensure (buffer, size);
293 hb_buffer_allocation_successful (hb_buffer_t *buffer)
295 return !buffer->in_error;
299 hb_buffer_add (hb_buffer_t *buffer,
300 hb_codepoint_t codepoint,
302 unsigned int cluster)
304 hb_glyph_info_t *glyph;
306 if (unlikely (!_hb_buffer_ensure (buffer, buffer->len + 1))) return;
308 glyph = &buffer->info[buffer->len];
310 memset (glyph, 0, sizeof (*glyph));
311 glyph->codepoint = codepoint;
313 glyph->cluster = cluster;
318 /* HarfBuzz-Internal API */
321 _hb_buffer_clear_output (hb_buffer_t *buffer)
323 if (unlikely (hb_object_is_inert (buffer)))
326 buffer->have_output = TRUE;
327 buffer->have_positions = FALSE;
330 buffer->out_info = buffer->info;
334 _hb_buffer_clear_positions (hb_buffer_t *buffer)
336 if (unlikely (hb_object_is_inert (buffer)))
339 buffer->have_output = FALSE;
340 buffer->have_positions = TRUE;
342 memset (buffer->pos, 0, sizeof (buffer->pos[0]) * buffer->len);
346 _hb_buffer_swap (hb_buffer_t *buffer)
348 if (unlikely (buffer->in_error)) return;
350 assert (buffer->have_output);
352 if (buffer->out_info != buffer->info)
354 hb_glyph_info_t *tmp_string;
355 tmp_string = buffer->info;
356 buffer->info = buffer->out_info;
357 buffer->out_info = tmp_string;
358 buffer->pos = (hb_glyph_position_t *) buffer->out_info;
363 buffer->len = buffer->out_len;
364 buffer->out_len = tmp;
370 _hb_buffer_replace_glyphs_be16 (hb_buffer_t *buffer,
372 unsigned int num_out,
373 const uint16_t *glyph_data_be)
375 if (buffer->out_info != buffer->info ||
376 buffer->out_len + num_out > buffer->i + num_in)
378 if (unlikely (!_hb_buffer_ensure_separate (buffer, buffer->out_len + num_out)))
382 hb_glyph_info_t orig_info = buffer->info[buffer->i];
384 for (unsigned int i = 0; i < num_out; i++)
386 hb_glyph_info_t *info = &buffer->out_info[buffer->out_len + i];
388 info->codepoint = hb_be_uint16 (glyph_data_be[i]);
392 buffer->out_len += num_out;
396 _hb_buffer_replace_glyph (hb_buffer_t *buffer,
397 hb_codepoint_t glyph_index)
399 hb_glyph_info_t *info;
401 if (buffer->out_info != buffer->info)
403 if (unlikely (!_hb_buffer_ensure (buffer, buffer->out_len + 1))) return;
404 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
406 else if (buffer->out_len != buffer->i)
407 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
409 info = &buffer->out_info[buffer->out_len];
410 info->codepoint = glyph_index;
417 _hb_buffer_next_glyph (hb_buffer_t *buffer)
419 if (buffer->have_output)
421 if (buffer->out_info != buffer->info)
423 if (unlikely (!_hb_buffer_ensure (buffer, buffer->out_len + 1))) return;
424 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
426 else if (buffer->out_len != buffer->i)
427 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
436 _hb_buffer_reset_masks (hb_buffer_t *buffer,
439 unsigned int count = buffer->len;
440 for (unsigned int i = 0; i < count; i++)
441 buffer->info[i].mask = mask;
445 _hb_buffer_add_masks (hb_buffer_t *buffer,
448 unsigned int count = buffer->len;
449 for (unsigned int i = 0; i < count; i++)
450 buffer->info[i].mask |= mask;
454 _hb_buffer_set_masks (hb_buffer_t *buffer,
457 unsigned int cluster_start,
458 unsigned int cluster_end)
460 hb_mask_t not_mask = ~mask;
466 if (cluster_start == 0 && cluster_end == (unsigned int)-1) {
467 unsigned int count = buffer->len;
468 for (unsigned int i = 0; i < count; i++)
469 buffer->info[i].mask = (buffer->info[i].mask & not_mask) | value;
473 unsigned int count = buffer->len;
474 for (unsigned int i = 0; i < count; i++)
475 if (cluster_start <= buffer->info[i].cluster && buffer->info[i].cluster < cluster_end)
476 buffer->info[i].mask = (buffer->info[i].mask & not_mask) | value;
480 /* Public API again */
483 hb_buffer_set_length (hb_buffer_t *buffer,
486 if (!_hb_buffer_ensure (buffer, length))
489 /* Wipe the new space */
490 if (length > buffer->len) {
491 memset (buffer->info + buffer->len, 0, sizeof (buffer->info[0]) * (length - buffer->len));
492 if (buffer->have_positions)
493 memset (buffer->pos + buffer->len, 0, sizeof (buffer->pos[0]) * (length - buffer->len));
496 buffer->len = length;
501 hb_buffer_get_length (hb_buffer_t *buffer)
506 /* Return value valid as long as buffer not modified */
508 hb_buffer_get_glyph_infos (hb_buffer_t *buffer,
509 unsigned int *length)
512 *length = buffer->len;
514 return (hb_glyph_info_t *) buffer->info;
517 /* Return value valid as long as buffer not modified */
518 hb_glyph_position_t *
519 hb_buffer_get_glyph_positions (hb_buffer_t *buffer,
520 unsigned int *length)
522 if (!buffer->have_positions)
523 _hb_buffer_clear_positions (buffer);
526 *length = buffer->len;
528 return (hb_glyph_position_t *) buffer->pos;
533 reverse_range (hb_buffer_t *buffer,
539 for (i = start, j = end - 1; i < j; i++, j--) {
543 buffer->info[i] = buffer->info[j];
548 for (i = 0, j = end - 1; i < j; i++, j--) {
549 hb_glyph_position_t t;
552 buffer->pos[i] = buffer->pos[j];
559 hb_buffer_reverse (hb_buffer_t *buffer)
561 if (unlikely (!buffer->len))
564 reverse_range (buffer, 0, buffer->len);
568 hb_buffer_reverse_clusters (hb_buffer_t *buffer)
570 unsigned int i, start, count, last_cluster;
572 if (unlikely (!buffer->len))
575 hb_buffer_reverse (buffer);
579 last_cluster = buffer->info[0].cluster;
580 for (i = 1; i < count; i++) {
581 if (last_cluster != buffer->info[i].cluster) {
582 reverse_range (buffer, start, i);
584 last_cluster = buffer->info[i].cluster;
587 reverse_range (buffer, start, i);
593 const T *next = (const T *) text + item_offset; \
594 const T *end = next + item_length; \
595 while (next < end) { \
597 const T *old_next = next; \
598 next = UTF_NEXT (next, end, u); \
599 hb_buffer_add (buffer, u, 1, old_next - (const T *) text); \
604 #define UTF8_COMPUTE(Char, Mask, Len) \
605 if (Char < 128) { Len = 1; Mask = 0x7f; } \
606 else if ((Char & 0xe0) == 0xc0) { Len = 2; Mask = 0x1f; } \
607 else if ((Char & 0xf0) == 0xe0) { Len = 3; Mask = 0x0f; } \
608 else if ((Char & 0xf8) == 0xf0) { Len = 4; Mask = 0x07; } \
611 static inline const uint8_t *
612 hb_utf8_next (const uint8_t *text,
614 hb_codepoint_t *unicode)
617 unsigned int mask, len;
619 /* TODO check for overlong sequences? */
621 UTF8_COMPUTE (c, mask, len);
622 if (unlikely (!len || (unsigned int) (end - text) < len)) {
626 hb_codepoint_t result;
629 for (i = 1; i < len; i++)
631 if (unlikely ((text[i] & 0xc0) != 0x80))
637 result |= (text[i] & 0x3f);
645 hb_buffer_add_utf8 (hb_buffer_t *buffer,
647 unsigned int text_length HB_UNUSED,
648 unsigned int item_offset,
649 unsigned int item_length)
651 #define UTF_NEXT(S, E, U) hb_utf8_next (S, E, &(U))
656 static inline const uint16_t *
657 hb_utf16_next (const uint16_t *text,
659 hb_codepoint_t *unicode)
661 uint16_t c = *text++;
663 if (unlikely (c >= 0xd800 && c < 0xdc00)) {
666 if (text < end && ((l = *text), likely (l >= 0xdc00 && l < 0xe000))) {
668 *unicode = ((hb_codepoint_t) ((c) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000);
679 hb_buffer_add_utf16 (hb_buffer_t *buffer,
680 const uint16_t *text,
681 unsigned int text_length HB_UNUSED,
682 unsigned int item_offset,
683 unsigned int item_length)
685 #define UTF_NEXT(S, E, U) hb_utf16_next (S, E, &(U))
691 hb_buffer_add_utf32 (hb_buffer_t *buffer,
692 const uint32_t *text,
693 unsigned int text_length HB_UNUSED,
694 unsigned int item_offset,
695 unsigned int item_length)
697 #define UTF_NEXT(S, E, U) ((U) = *(S), (S)+1)