2 * Copyright (C) 1998-2004 David Turner and Werner Lemberg
3 * Copyright (C) 2004,2007,2009,2010 Red Hat, Inc.
4 * Copyright (C) 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_REFERENCE_COUNT_INVALID, /* ref_count */
40 &_hb_unicode_funcs_nil, /* unicode */
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) + 8;
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 (!HB_OBJECT_DO_CREATE (hb_buffer_t, buffer))
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 HB_OBJECT_DO_REFERENCE (buffer);
158 hb_buffer_get_reference_count (hb_buffer_t *buffer)
160 HB_OBJECT_DO_GET_REFERENCE_COUNT (buffer);
164 hb_buffer_destroy (hb_buffer_t *buffer)
166 HB_OBJECT_DO_DESTROY (buffer);
168 hb_unicode_funcs_destroy (buffer->unicode);
178 hb_buffer_set_unicode_funcs (hb_buffer_t *buffer,
179 hb_unicode_funcs_t *unicode)
182 unicode = &_hb_unicode_funcs_nil;
184 hb_unicode_funcs_reference (unicode);
185 hb_unicode_funcs_destroy (buffer->unicode);
186 buffer->unicode = unicode;
190 hb_buffer_get_unicode_funcs (hb_buffer_t *buffer)
192 return buffer->unicode;
196 hb_buffer_set_direction (hb_buffer_t *buffer,
197 hb_direction_t direction)
200 buffer->props.direction = direction;
204 hb_buffer_get_direction (hb_buffer_t *buffer)
206 return buffer->props.direction;
210 hb_buffer_set_script (hb_buffer_t *buffer,
213 buffer->props.script = script;
217 hb_buffer_get_script (hb_buffer_t *buffer)
219 return buffer->props.script;
223 hb_buffer_set_language (hb_buffer_t *buffer,
224 hb_language_t language)
226 buffer->props.language = language;
230 hb_buffer_get_language (hb_buffer_t *buffer)
232 return buffer->props.language;
237 hb_buffer_reset (hb_buffer_t *buffer)
239 hb_unicode_funcs_destroy (buffer->unicode);
240 buffer->unicode = _hb_buffer_nil.unicode;
242 buffer->props = _hb_buffer_nil.props;
244 buffer->have_output = FALSE;
245 buffer->have_positions = FALSE;
246 buffer->in_error = FALSE;
254 buffer->out_info = buffer->info;
258 hb_buffer_pre_allocate (hb_buffer_t *buffer, unsigned int size)
260 return _hb_buffer_ensure (buffer, size);
264 hb_buffer_allocation_successful (hb_buffer_t *buffer)
266 return !buffer->in_error;
270 hb_buffer_add_glyph (hb_buffer_t *buffer,
271 hb_codepoint_t codepoint,
273 unsigned int cluster)
275 hb_glyph_info_t *glyph;
277 if (unlikely (!_hb_buffer_ensure (buffer, buffer->len + 1))) return;
279 glyph = &buffer->info[buffer->len];
281 memset (glyph, 0, sizeof (*glyph));
282 glyph->codepoint = codepoint;
284 glyph->cluster = cluster;
289 /* HarfBuzz-Internal API */
292 _hb_buffer_clear_output (hb_buffer_t *buffer)
294 buffer->have_output = TRUE;
295 buffer->have_positions = FALSE;
298 buffer->out_info = buffer->info;
302 _hb_buffer_clear_positions (hb_buffer_t *buffer)
304 buffer->have_output = FALSE;
305 buffer->have_positions = TRUE;
307 memset (buffer->pos, 0, sizeof (buffer->pos[0]) * buffer->len);
311 _hb_buffer_swap (hb_buffer_t *buffer)
315 assert (buffer->have_output);
317 if (unlikely (buffer->in_error)) return;
319 if (buffer->out_info != buffer->info)
321 hb_glyph_info_t *tmp_string;
322 tmp_string = buffer->info;
323 buffer->info = buffer->out_info;
324 buffer->out_info = tmp_string;
325 buffer->pos = (hb_glyph_position_t *) buffer->out_info;
329 buffer->len = buffer->out_len;
330 buffer->out_len = tmp;
336 _hb_buffer_replace_glyphs_be16 (hb_buffer_t *buffer,
338 unsigned int num_out,
339 const uint16_t *glyph_data_be)
341 if (buffer->out_info != buffer->info ||
342 buffer->out_len + num_out > buffer->i + num_in)
344 if (unlikely (!_hb_buffer_ensure_separate (buffer, buffer->out_len + num_out)))
348 hb_glyph_info_t orig_info = buffer->info[buffer->i];
350 for (unsigned int i = 0; i < num_out; i++)
352 hb_glyph_info_t *info = &buffer->out_info[buffer->out_len + i];
354 info->codepoint = hb_be_uint16 (glyph_data_be[i]);
358 buffer->out_len += num_out;
362 _hb_buffer_replace_glyph (hb_buffer_t *buffer,
363 hb_codepoint_t glyph_index)
365 hb_glyph_info_t *info;
367 if (buffer->out_info != buffer->info)
369 if (unlikely (!_hb_buffer_ensure (buffer, buffer->out_len + 1))) return;
370 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
372 else if (buffer->out_len != buffer->i)
373 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
375 info = &buffer->out_info[buffer->out_len];
376 info->codepoint = glyph_index;
383 _hb_buffer_next_glyph (hb_buffer_t *buffer)
385 if (buffer->have_output)
387 if (buffer->out_info != buffer->info)
389 if (unlikely (!_hb_buffer_ensure (buffer, buffer->out_len + 1))) return;
390 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
392 else if (buffer->out_len != buffer->i)
393 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
402 _hb_buffer_reset_masks (hb_buffer_t *buffer,
405 unsigned int count = buffer->len;
406 for (unsigned int i = 0; i < count; i++)
407 buffer->info[i].mask = mask;
411 _hb_buffer_add_masks (hb_buffer_t *buffer,
414 unsigned int count = buffer->len;
415 for (unsigned int i = 0; i < count; i++)
416 buffer->info[i].mask |= mask;
420 _hb_buffer_set_masks (hb_buffer_t *buffer,
423 unsigned int cluster_start,
424 unsigned int cluster_end)
426 hb_mask_t not_mask = ~mask;
432 if (cluster_start == 0 && cluster_end == (unsigned int)-1) {
433 unsigned int count = buffer->len;
434 for (unsigned int i = 0; i < count; i++)
435 buffer->info[i].mask = (buffer->info[i].mask & not_mask) | value;
439 /* XXX can't bsearch since .cluster may not be sorted. */
440 /* Binary search to find the start position and go from there. */
441 unsigned int min = 0, max = buffer->len;
444 unsigned int mid = min + ((max - min) / 2);
445 if (buffer->info[mid].cluster < cluster_start)
450 unsigned int count = buffer->len;
451 for (unsigned int i = min; i < count && buffer->info[i].cluster < cluster_end; i++)
452 buffer->info[i].mask = (buffer->info[i].mask & not_mask) | value;
456 /* Public API again */
459 hb_buffer_set_length (hb_buffer_t *buffer,
462 if (!_hb_buffer_ensure (buffer, length))
465 /* Wipe the new space */
466 if (length > buffer->len) {
467 memset (buffer->info + buffer->len, 0, sizeof (buffer->info[0]) * (length - buffer->len));
468 if (buffer->have_positions)
469 memset (buffer->pos + buffer->len, 0, sizeof (buffer->pos[0]) * (length - buffer->len));
472 buffer->len = length;
477 hb_buffer_get_length (hb_buffer_t *buffer)
482 /* Return value valid as long as buffer not modified */
484 hb_buffer_get_glyph_infos (hb_buffer_t *buffer,
485 unsigned int *length)
488 *length = buffer->len;
490 return (hb_glyph_info_t *) buffer->info;
493 /* Return value valid as long as buffer not modified */
494 hb_glyph_position_t *
495 hb_buffer_get_glyph_positions (hb_buffer_t *buffer,
496 unsigned int *length)
498 if (!buffer->have_positions)
499 _hb_buffer_clear_positions (buffer);
502 *length = buffer->len;
504 return (hb_glyph_position_t *) buffer->pos;
509 reverse_range (hb_buffer_t *buffer,
515 for (i = start, j = end - 1; i < j; i++, j--) {
519 buffer->info[i] = buffer->info[j];
524 for (i = 0, j = end - 1; i < j; i++, j--) {
525 hb_glyph_position_t t;
528 buffer->pos[i] = buffer->pos[j];
535 hb_buffer_reverse (hb_buffer_t *buffer)
537 if (unlikely (!buffer->len))
540 reverse_range (buffer, 0, buffer->len);
544 hb_buffer_reverse_clusters (hb_buffer_t *buffer)
546 unsigned int i, start, count, last_cluster;
548 if (unlikely (!buffer->len))
551 hb_buffer_reverse (buffer);
555 last_cluster = buffer->info[0].cluster;
556 for (i = 1; i < count; i++) {
557 if (last_cluster != buffer->info[i].cluster) {
558 reverse_range (buffer, start, i);
560 last_cluster = buffer->info[i].cluster;
563 reverse_range (buffer, start, i);
569 const T *next = (const T *) text + item_offset; \
570 const T *end = next + item_length; \
571 while (next < end) { \
573 const T *old_next = next; \
574 next = UTF_NEXT (next, end, u); \
575 hb_buffer_add_glyph (buffer, u, 1, old_next - (const T *) text); \
580 #define UTF8_COMPUTE(Char, Mask, Len) \
581 if (Char < 128) { Len = 1; Mask = 0x7f; } \
582 else if ((Char & 0xe0) == 0xc0) { Len = 2; Mask = 0x1f; } \
583 else if ((Char & 0xf0) == 0xe0) { Len = 3; Mask = 0x0f; } \
584 else if ((Char & 0xf8) == 0xf0) { Len = 4; Mask = 0x07; } \
587 static inline const uint8_t *
588 hb_utf8_next (const uint8_t *text,
590 hb_codepoint_t *unicode)
593 unsigned int mask, len;
595 /* TODO check for overlong sequences? also: optimize? */
597 UTF8_COMPUTE (c, mask, len);
598 if (unlikely (!len || (unsigned int) (end - text) < len)) {
602 hb_codepoint_t result;
605 for (i = 1; i < len; i++)
607 if (unlikely ((text[i] & 0xc0) != 0x80))
613 result |= (text[i] & 0x3f);
621 hb_buffer_add_utf8 (hb_buffer_t *buffer,
623 unsigned int text_length HB_UNUSED,
624 unsigned int item_offset,
625 unsigned int item_length)
627 #define UTF_NEXT(S, E, U) hb_utf8_next (S, E, &(U))
632 static inline const uint16_t *
633 hb_utf16_next (const uint16_t *text,
635 hb_codepoint_t *unicode)
637 uint16_t c = *text++;
639 if (unlikely (c >= 0xd800 && c < 0xdc00)) {
642 if (text < end && ((l = *text), likely (l >= 0xdc00 && l < 0xe000))) {
644 *unicode = ((hb_codepoint_t) ((c) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000);
655 hb_buffer_add_utf16 (hb_buffer_t *buffer,
656 const uint16_t *text,
657 unsigned int text_length HB_UNUSED,
658 unsigned int item_offset,
659 unsigned int item_length)
661 #define UTF_NEXT(S, E, U) hb_utf16_next (S, E, &(U))
667 hb_buffer_add_utf32 (hb_buffer_t *buffer,
668 const uint32_t *text,
669 unsigned int text_length HB_UNUSED,
670 unsigned int item_offset,
671 unsigned int item_length)
673 #define UTF_NEXT(S, E, U) ((U) = *(S), (S)+1)