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_ensure (hb_buffer_t *buffer, unsigned int size)
260 return _hb_buffer_ensure (buffer, size);
264 hb_buffer_add_glyph (hb_buffer_t *buffer,
265 hb_codepoint_t codepoint,
267 unsigned int cluster)
269 hb_glyph_info_t *glyph;
271 if (unlikely (!_hb_buffer_ensure (buffer, buffer->len + 1))) return;
273 glyph = &buffer->info[buffer->len];
275 memset (glyph, 0, sizeof (*glyph));
276 glyph->codepoint = codepoint;
278 glyph->cluster = cluster;
283 /* HarfBuzz-Internal API */
286 _hb_buffer_clear_output (hb_buffer_t *buffer)
288 buffer->have_output = TRUE;
289 buffer->have_positions = FALSE;
292 buffer->out_info = buffer->info;
296 _hb_buffer_clear_positions (hb_buffer_t *buffer)
298 buffer->have_output = FALSE;
299 buffer->have_positions = TRUE;
301 memset (buffer->pos, 0, sizeof (buffer->pos[0]) * buffer->len);
305 _hb_buffer_swap (hb_buffer_t *buffer)
309 assert (buffer->have_output);
311 if (unlikely (buffer->in_error)) return;
313 if (buffer->out_info != buffer->info)
315 hb_glyph_info_t *tmp_string;
316 tmp_string = buffer->info;
317 buffer->info = buffer->out_info;
318 buffer->out_info = tmp_string;
319 buffer->pos = (hb_glyph_position_t *) buffer->out_info;
323 buffer->len = buffer->out_len;
324 buffer->out_len = tmp;
330 _hb_buffer_replace_glyphs_be16 (hb_buffer_t *buffer,
332 unsigned int num_out,
333 const uint16_t *glyph_data_be)
335 if (buffer->out_info != buffer->info ||
336 buffer->out_len + num_out > buffer->i + num_in)
338 if (unlikely (!_hb_buffer_ensure_separate (buffer, buffer->out_len + num_out)))
342 hb_glyph_info_t orig_info = buffer->info[buffer->i];
344 for (unsigned int i = 0; i < num_out; i++)
346 hb_glyph_info_t *info = &buffer->out_info[buffer->out_len + i];
348 info->codepoint = hb_be_uint16 (glyph_data_be[i]);
352 buffer->out_len += num_out;
356 _hb_buffer_replace_glyph (hb_buffer_t *buffer,
357 hb_codepoint_t glyph_index)
359 hb_glyph_info_t *info;
361 if (buffer->out_info != buffer->info)
363 if (unlikely (!_hb_buffer_ensure (buffer, buffer->out_len + 1))) return;
364 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
366 else if (buffer->out_len != buffer->i)
367 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
369 info = &buffer->out_info[buffer->out_len];
370 info->codepoint = glyph_index;
377 _hb_buffer_next_glyph (hb_buffer_t *buffer)
379 if (buffer->have_output)
381 if (buffer->out_info != buffer->info)
383 if (unlikely (!_hb_buffer_ensure (buffer, buffer->out_len + 1))) return;
384 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
386 else if (buffer->out_len != buffer->i)
387 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
396 _hb_buffer_reset_masks (hb_buffer_t *buffer,
399 unsigned int count = buffer->len;
400 for (unsigned int i = 0; i < count; i++)
401 buffer->info[i].mask = mask;
405 _hb_buffer_add_masks (hb_buffer_t *buffer,
408 unsigned int count = buffer->len;
409 for (unsigned int i = 0; i < count; i++)
410 buffer->info[i].mask |= mask;
414 _hb_buffer_set_masks (hb_buffer_t *buffer,
417 unsigned int cluster_start,
418 unsigned int cluster_end)
420 hb_mask_t not_mask = ~mask;
426 if (cluster_start == 0 && cluster_end == (unsigned int)-1) {
427 unsigned int count = buffer->len;
428 for (unsigned int i = 0; i < count; i++)
429 buffer->info[i].mask = (buffer->info[i].mask & not_mask) | value;
433 /* XXX can't bsearch since .cluster may not be sorted. */
434 /* Binary search to find the start position and go from there. */
435 unsigned int min = 0, max = buffer->len;
438 unsigned int mid = min + ((max - min) / 2);
439 if (buffer->info[mid].cluster < cluster_start)
444 unsigned int count = buffer->len;
445 for (unsigned int i = min; i < count && buffer->info[i].cluster < cluster_end; i++)
446 buffer->info[i].mask = (buffer->info[i].mask & not_mask) | value;
450 /* Public API again */
453 hb_buffer_set_length (hb_buffer_t *buffer,
456 if (!hb_buffer_ensure (buffer, length))
459 /* Wipe the new space */
460 if (length > buffer->len) {
461 memset (buffer->info + buffer->len, 0, sizeof (buffer->info[0]) * (length - buffer->len));
462 if (buffer->have_positions)
463 memset (buffer->pos + buffer->len, 0, sizeof (buffer->pos[0]) * (length - buffer->len));
466 buffer->len = length;
471 hb_buffer_get_length (hb_buffer_t *buffer)
476 /* Return value valid as long as buffer not modified */
478 hb_buffer_get_glyph_infos (hb_buffer_t *buffer)
480 return (hb_glyph_info_t *) buffer->info;
483 /* Return value valid as long as buffer not modified */
484 hb_glyph_position_t *
485 hb_buffer_get_glyph_positions (hb_buffer_t *buffer)
487 if (!buffer->have_positions)
488 _hb_buffer_clear_positions (buffer);
490 return (hb_glyph_position_t *) buffer->pos;
495 reverse_range (hb_buffer_t *buffer,
501 for (i = start, j = end - 1; i < j; i++, j--) {
505 buffer->info[i] = buffer->info[j];
510 for (i = 0, j = end - 1; i < j; i++, j--) {
511 hb_glyph_position_t t;
514 buffer->pos[i] = buffer->pos[j];
521 hb_buffer_reverse (hb_buffer_t *buffer)
523 if (unlikely (!buffer->len))
526 reverse_range (buffer, 0, buffer->len);
530 hb_buffer_reverse_clusters (hb_buffer_t *buffer)
532 unsigned int i, start, count, last_cluster;
534 if (unlikely (!buffer->len))
537 hb_buffer_reverse (buffer);
541 last_cluster = buffer->info[0].cluster;
542 for (i = 1; i < count; i++) {
543 if (last_cluster != buffer->info[i].cluster) {
544 reverse_range (buffer, start, i);
546 last_cluster = buffer->info[i].cluster;
549 reverse_range (buffer, start, i);
555 const T *next = (const T *) text + item_offset; \
556 const T *end = next + item_length; \
557 while (next < end) { \
559 const T *old_next = next; \
560 next = UTF_NEXT (next, end, u); \
561 hb_buffer_add_glyph (buffer, u, 1, old_next - (const T *) text); \
566 #define UTF8_COMPUTE(Char, Mask, Len) \
567 if (Char < 128) { Len = 1; Mask = 0x7f; } \
568 else if ((Char & 0xe0) == 0xc0) { Len = 2; Mask = 0x1f; } \
569 else if ((Char & 0xf0) == 0xe0) { Len = 3; Mask = 0x0f; } \
570 else if ((Char & 0xf8) == 0xf0) { Len = 4; Mask = 0x07; } \
573 static inline const uint8_t *
574 hb_utf8_next (const uint8_t *text,
576 hb_codepoint_t *unicode)
579 unsigned int mask, len;
581 /* TODO check for overlong sequences? also: optimize? */
583 UTF8_COMPUTE (c, mask, len);
584 if (unlikely (!len || (unsigned int) (end - text) < len)) {
588 hb_codepoint_t result;
591 for (i = 1; i < len; i++)
593 if (unlikely ((text[i] & 0xc0) != 0x80))
599 result |= (text[i] & 0x3f);
607 hb_buffer_add_utf8 (hb_buffer_t *buffer,
609 unsigned int text_length HB_UNUSED,
610 unsigned int item_offset,
611 unsigned int item_length)
613 #define UTF_NEXT(S, E, U) hb_utf8_next (S, E, &(U))
618 static inline const uint16_t *
619 hb_utf16_next (const uint16_t *text,
621 hb_codepoint_t *unicode)
623 uint16_t c = *text++;
625 if (unlikely (c >= 0xd800 && c < 0xdc00)) {
628 if (text < end && ((l = *text), likely (l >= 0xdc00 && l < 0xe000))) {
630 *unicode = ((hb_codepoint_t) ((c) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000);
641 hb_buffer_add_utf16 (hb_buffer_t *buffer,
642 const uint16_t *text,
643 unsigned int text_length HB_UNUSED,
644 unsigned int item_offset,
645 unsigned int item_length)
647 #define UTF_NEXT(S, E, U) hb_utf16_next (S, E, &(U))
653 hb_buffer_add_utf32 (hb_buffer_t *buffer,
654 const uint32_t *text,
655 unsigned int text_length HB_UNUSED,
656 unsigned int item_offset,
657 unsigned int item_length)
659 #define UTF_NEXT(S, E, U) ((U) = *(S), (S)+1)