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 /* Here is how the buffer works internally:
50 * There are two info pointers: info and out_info. They always have
51 * the same allocated size, but different lengths.
53 * As an optimization, both info and out_info may point to the
54 * same piece of memory, which is owned by info. This remains the
55 * case as long as out_len doesn't exceed i at any time.
56 * In that case, swap() is no-op and the glyph operations operate
59 * As soon as out_info gets longer than info, out_info is moved over
60 * to an alternate buffer (which we reuse the pos buffer for!), and its
61 * current contents (out_len entries) are copied to the new place.
62 * This should all remain transparent to the user. swap() then
63 * switches info and out_info.
68 _hb_buffer_enlarge (hb_buffer_t *buffer, unsigned int size)
70 if (unlikely (buffer->in_error))
73 unsigned int new_allocated = buffer->allocated;
74 hb_glyph_position_t *new_pos;
75 hb_glyph_info_t *new_info;
78 separate_out = buffer->out_info != buffer->info;
80 while (size > new_allocated)
81 new_allocated += (new_allocated >> 1) + 32;
83 ASSERT_STATIC (sizeof (buffer->info[0]) == sizeof (buffer->pos[0]));
84 bool overflows = new_allocated >= ((unsigned int) -1) / sizeof (buffer->info[0]);
86 if (unlikely (overflows)) {
90 new_pos = (hb_glyph_position_t *) realloc (buffer->pos, new_allocated * sizeof (buffer->pos[0]));
91 new_info = (hb_glyph_info_t *) realloc (buffer->info, new_allocated * sizeof (buffer->info[0]));
94 if (unlikely (!new_pos || !new_info))
95 buffer->in_error = TRUE;
98 buffer->pos = new_pos;
100 if (likely (new_info))
101 buffer->info = new_info;
103 buffer->out_info = separate_out ? (hb_glyph_info_t *) buffer->pos : buffer->info;
104 if (likely (!buffer->in_error))
105 buffer->allocated = new_allocated;
107 return likely (!buffer->in_error);
110 static inline hb_bool_t
111 _hb_buffer_ensure (hb_buffer_t *buffer, unsigned int size)
113 return likely (size <= buffer->allocated) ? TRUE : _hb_buffer_enlarge (buffer, size);
116 static inline hb_bool_t
117 _hb_buffer_ensure_separate (hb_buffer_t *buffer, unsigned int size)
119 if (unlikely (!_hb_buffer_ensure (buffer, size))) return FALSE;
121 if (buffer->out_info == buffer->info)
123 assert (buffer->have_output);
125 buffer->out_info = (hb_glyph_info_t *) buffer->pos;
126 memcpy (buffer->out_info, buffer->info, buffer->out_len * sizeof (buffer->out_info[0]));
136 hb_buffer_create (unsigned int pre_alloc_size)
140 if (!(buffer = hb_object_create<hb_buffer_t> ()))
141 return &_hb_buffer_nil;
144 _hb_buffer_ensure (buffer, pre_alloc_size);
146 hb_buffer_reset (buffer);
152 hb_buffer_reference (hb_buffer_t *buffer)
154 return hb_object_reference (buffer);
158 hb_buffer_destroy (hb_buffer_t *buffer)
160 if (!hb_object_destroy (buffer)) return;
162 hb_unicode_funcs_destroy (buffer->unicode);
171 hb_buffer_set_user_data (hb_buffer_t *buffer,
172 hb_user_data_key_t *key,
174 hb_destroy_func_t destroy)
176 return hb_object_set_user_data (buffer, key, data, destroy);
180 hb_buffer_get_user_data (hb_buffer_t *buffer,
181 hb_user_data_key_t *key)
183 return hb_object_get_user_data (buffer, key);
188 hb_buffer_set_unicode_funcs (hb_buffer_t *buffer,
189 hb_unicode_funcs_t *unicode)
192 unicode = &_hb_unicode_funcs_default;
194 hb_unicode_funcs_reference (unicode);
195 hb_unicode_funcs_destroy (buffer->unicode);
196 buffer->unicode = unicode;
200 hb_buffer_get_unicode_funcs (hb_buffer_t *buffer)
202 return buffer->unicode;
206 hb_buffer_set_direction (hb_buffer_t *buffer,
207 hb_direction_t direction)
210 buffer->props.direction = direction;
214 hb_buffer_get_direction (hb_buffer_t *buffer)
216 return buffer->props.direction;
220 hb_buffer_set_script (hb_buffer_t *buffer,
223 buffer->props.script = script;
227 hb_buffer_get_script (hb_buffer_t *buffer)
229 return buffer->props.script;
233 hb_buffer_set_language (hb_buffer_t *buffer,
234 hb_language_t language)
236 buffer->props.language = language;
240 hb_buffer_get_language (hb_buffer_t *buffer)
242 return buffer->props.language;
247 hb_buffer_reset (hb_buffer_t *buffer)
249 hb_unicode_funcs_destroy (buffer->unicode);
250 buffer->unicode = _hb_buffer_nil.unicode;
252 buffer->props = _hb_buffer_nil.props;
254 buffer->have_output = FALSE;
255 buffer->have_positions = FALSE;
256 buffer->in_error = FALSE;
264 buffer->out_info = buffer->info;
268 hb_buffer_pre_allocate (hb_buffer_t *buffer, unsigned int size)
270 return _hb_buffer_ensure (buffer, size);
274 hb_buffer_allocation_successful (hb_buffer_t *buffer)
276 return !buffer->in_error;
280 hb_buffer_add (hb_buffer_t *buffer,
281 hb_codepoint_t codepoint,
283 unsigned int cluster)
285 hb_glyph_info_t *glyph;
287 if (unlikely (!_hb_buffer_ensure (buffer, buffer->len + 1))) return;
289 glyph = &buffer->info[buffer->len];
291 memset (glyph, 0, sizeof (*glyph));
292 glyph->codepoint = codepoint;
294 glyph->cluster = cluster;
299 /* HarfBuzz-Internal API */
302 _hb_buffer_clear_output (hb_buffer_t *buffer)
304 buffer->have_output = TRUE;
305 buffer->have_positions = FALSE;
308 buffer->out_info = buffer->info;
312 _hb_buffer_clear_positions (hb_buffer_t *buffer)
314 buffer->have_output = FALSE;
315 buffer->have_positions = TRUE;
317 memset (buffer->pos, 0, sizeof (buffer->pos[0]) * buffer->len);
321 _hb_buffer_swap (hb_buffer_t *buffer)
325 assert (buffer->have_output);
327 if (unlikely (buffer->in_error)) return;
329 if (buffer->out_info != buffer->info)
331 hb_glyph_info_t *tmp_string;
332 tmp_string = buffer->info;
333 buffer->info = buffer->out_info;
334 buffer->out_info = tmp_string;
335 buffer->pos = (hb_glyph_position_t *) buffer->out_info;
339 buffer->len = buffer->out_len;
340 buffer->out_len = tmp;
346 _hb_buffer_replace_glyphs_be16 (hb_buffer_t *buffer,
348 unsigned int num_out,
349 const uint16_t *glyph_data_be)
351 if (buffer->out_info != buffer->info ||
352 buffer->out_len + num_out > buffer->i + num_in)
354 if (unlikely (!_hb_buffer_ensure_separate (buffer, buffer->out_len + num_out)))
358 hb_glyph_info_t orig_info = buffer->info[buffer->i];
360 for (unsigned int i = 0; i < num_out; i++)
362 hb_glyph_info_t *info = &buffer->out_info[buffer->out_len + i];
364 info->codepoint = hb_be_uint16 (glyph_data_be[i]);
368 buffer->out_len += num_out;
372 _hb_buffer_replace_glyph (hb_buffer_t *buffer,
373 hb_codepoint_t glyph_index)
375 hb_glyph_info_t *info;
377 if (buffer->out_info != buffer->info)
379 if (unlikely (!_hb_buffer_ensure (buffer, buffer->out_len + 1))) return;
380 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
382 else if (buffer->out_len != buffer->i)
383 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
385 info = &buffer->out_info[buffer->out_len];
386 info->codepoint = glyph_index;
393 _hb_buffer_next_glyph (hb_buffer_t *buffer)
395 if (buffer->have_output)
397 if (buffer->out_info != buffer->info)
399 if (unlikely (!_hb_buffer_ensure (buffer, buffer->out_len + 1))) return;
400 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
402 else if (buffer->out_len != buffer->i)
403 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
412 _hb_buffer_reset_masks (hb_buffer_t *buffer,
415 unsigned int count = buffer->len;
416 for (unsigned int i = 0; i < count; i++)
417 buffer->info[i].mask = mask;
421 _hb_buffer_add_masks (hb_buffer_t *buffer,
424 unsigned int count = buffer->len;
425 for (unsigned int i = 0; i < count; i++)
426 buffer->info[i].mask |= mask;
430 _hb_buffer_set_masks (hb_buffer_t *buffer,
433 unsigned int cluster_start,
434 unsigned int cluster_end)
436 hb_mask_t not_mask = ~mask;
442 if (cluster_start == 0 && cluster_end == (unsigned int)-1) {
443 unsigned int count = buffer->len;
444 for (unsigned int i = 0; i < count; i++)
445 buffer->info[i].mask = (buffer->info[i].mask & not_mask) | value;
449 /* XXX can't bsearch since .cluster may not be sorted. */
450 /* Binary search to find the start position and go from there. */
451 unsigned int min = 0, max = buffer->len;
454 unsigned int mid = min + ((max - min) / 2);
455 if (buffer->info[mid].cluster < cluster_start)
460 unsigned int count = buffer->len;
461 for (unsigned int i = min; i < count && buffer->info[i].cluster < cluster_end; i++)
462 buffer->info[i].mask = (buffer->info[i].mask & not_mask) | value;
466 /* Public API again */
469 hb_buffer_set_length (hb_buffer_t *buffer,
472 if (!_hb_buffer_ensure (buffer, length))
475 /* Wipe the new space */
476 if (length > buffer->len) {
477 memset (buffer->info + buffer->len, 0, sizeof (buffer->info[0]) * (length - buffer->len));
478 if (buffer->have_positions)
479 memset (buffer->pos + buffer->len, 0, sizeof (buffer->pos[0]) * (length - buffer->len));
482 buffer->len = length;
487 hb_buffer_get_length (hb_buffer_t *buffer)
492 /* Return value valid as long as buffer not modified */
494 hb_buffer_get_glyph_infos (hb_buffer_t *buffer,
495 unsigned int *length)
498 *length = buffer->len;
500 return (hb_glyph_info_t *) buffer->info;
503 /* Return value valid as long as buffer not modified */
504 hb_glyph_position_t *
505 hb_buffer_get_glyph_positions (hb_buffer_t *buffer,
506 unsigned int *length)
508 if (!buffer->have_positions)
509 _hb_buffer_clear_positions (buffer);
512 *length = buffer->len;
514 return (hb_glyph_position_t *) buffer->pos;
519 reverse_range (hb_buffer_t *buffer,
525 for (i = start, j = end - 1; i < j; i++, j--) {
529 buffer->info[i] = buffer->info[j];
534 for (i = 0, j = end - 1; i < j; i++, j--) {
535 hb_glyph_position_t t;
538 buffer->pos[i] = buffer->pos[j];
545 hb_buffer_reverse (hb_buffer_t *buffer)
547 if (unlikely (!buffer->len))
550 reverse_range (buffer, 0, buffer->len);
554 hb_buffer_reverse_clusters (hb_buffer_t *buffer)
556 unsigned int i, start, count, last_cluster;
558 if (unlikely (!buffer->len))
561 hb_buffer_reverse (buffer);
565 last_cluster = buffer->info[0].cluster;
566 for (i = 1; i < count; i++) {
567 if (last_cluster != buffer->info[i].cluster) {
568 reverse_range (buffer, start, i);
570 last_cluster = buffer->info[i].cluster;
573 reverse_range (buffer, start, i);
579 const T *next = (const T *) text + item_offset; \
580 const T *end = next + item_length; \
581 while (next < end) { \
583 const T *old_next = next; \
584 next = UTF_NEXT (next, end, u); \
585 hb_buffer_add (buffer, u, 1, old_next - (const T *) text); \
590 #define UTF8_COMPUTE(Char, Mask, Len) \
591 if (Char < 128) { Len = 1; Mask = 0x7f; } \
592 else if ((Char & 0xe0) == 0xc0) { Len = 2; Mask = 0x1f; } \
593 else if ((Char & 0xf0) == 0xe0) { Len = 3; Mask = 0x0f; } \
594 else if ((Char & 0xf8) == 0xf0) { Len = 4; Mask = 0x07; } \
597 static inline const uint8_t *
598 hb_utf8_next (const uint8_t *text,
600 hb_codepoint_t *unicode)
603 unsigned int mask, len;
605 /* TODO check for overlong sequences? also: optimize? */
607 UTF8_COMPUTE (c, mask, len);
608 if (unlikely (!len || (unsigned int) (end - text) < len)) {
612 hb_codepoint_t result;
615 for (i = 1; i < len; i++)
617 if (unlikely ((text[i] & 0xc0) != 0x80))
623 result |= (text[i] & 0x3f);
631 hb_buffer_add_utf8 (hb_buffer_t *buffer,
633 unsigned int text_length HB_UNUSED,
634 unsigned int item_offset,
635 unsigned int item_length)
637 #define UTF_NEXT(S, E, U) hb_utf8_next (S, E, &(U))
642 static inline const uint16_t *
643 hb_utf16_next (const uint16_t *text,
645 hb_codepoint_t *unicode)
647 uint16_t c = *text++;
649 if (unlikely (c >= 0xd800 && c < 0xdc00)) {
652 if (text < end && ((l = *text), likely (l >= 0xdc00 && l < 0xe000))) {
654 *unicode = ((hb_codepoint_t) ((c) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000);
665 hb_buffer_add_utf16 (hb_buffer_t *buffer,
666 const uint16_t *text,
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_utf16_next (S, E, &(U))
677 hb_buffer_add_utf32 (hb_buffer_t *buffer,
678 const uint32_t *text,
679 unsigned int text_length HB_UNUSED,
680 unsigned int item_offset,
681 unsigned int item_length)
683 #define UTF_NEXT(S, E, U) ((U) = *(S), (S)+1)