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 */
43 /* Here is how the buffer works internally:
45 * There are two info pointers: info and out_info. They always have
46 * the same allocated size, but different lengths.
48 * As an optimization, both info and out_info may point to the
49 * same piece of memory, which is owned by info. This remains the
50 * case as long as out_len doesn't exceed i at any time.
51 * In that case, swap() is no-op and the glyph operations operate
54 * As soon as out_info gets longer than info, out_info is moved over
55 * to an alternate buffer (which we reuse the pos buffer for!), and its
56 * current contents (out_len entries) are copied to the new place.
57 * This should all remain transparent to the user. swap() then
58 * switches info and out_info.
63 _hb_buffer_enlarge (hb_buffer_t *buffer, unsigned int size)
65 if (unlikely (buffer->in_error))
68 unsigned int new_allocated = buffer->allocated;
69 hb_glyph_position_t *new_pos;
70 hb_glyph_info_t *new_info;
73 separate_out = buffer->out_info != buffer->info;
75 while (size > new_allocated)
76 new_allocated += (new_allocated >> 1) + 8;
78 ASSERT_STATIC (sizeof (buffer->info[0]) == sizeof (buffer->pos[0]));
79 bool overflows = new_allocated >= ((unsigned int) -1) / sizeof (buffer->info[0]);
81 if (unlikely (overflows)) {
85 new_pos = (hb_glyph_position_t *) realloc (buffer->pos, new_allocated * sizeof (buffer->pos[0]));
86 new_info = (hb_glyph_info_t *) realloc (buffer->info, new_allocated * sizeof (buffer->info[0]));
89 if (unlikely (!new_pos || !new_info))
90 buffer->in_error = TRUE;
93 buffer->pos = new_pos;
95 if (likely (new_info))
96 buffer->info = new_info;
98 buffer->out_info = separate_out ? (hb_glyph_info_t *) buffer->pos : buffer->info;
99 if (likely (!buffer->in_error))
100 buffer->allocated = new_allocated;
102 return likely (!buffer->in_error);
105 static inline hb_bool_t
106 _hb_buffer_ensure (hb_buffer_t *buffer, unsigned int size)
108 return likely (size <= buffer->allocated) ? TRUE : _hb_buffer_enlarge (buffer, size);
111 static inline hb_bool_t
112 _hb_buffer_ensure_separate (hb_buffer_t *buffer, unsigned int size)
114 if (unlikely (!_hb_buffer_ensure (buffer, size))) return FALSE;
116 if (buffer->out_info == buffer->info)
118 assert (buffer->have_output);
120 buffer->out_info = (hb_glyph_info_t *) buffer->pos;
121 memcpy (buffer->out_info, buffer->info, buffer->out_len * sizeof (buffer->out_info[0]));
131 hb_buffer_create (unsigned int pre_alloc_size)
135 if (!HB_OBJECT_DO_CREATE (hb_buffer_t, buffer))
136 return &_hb_buffer_nil;
139 _hb_buffer_ensure (buffer, pre_alloc_size);
141 buffer->unicode = &_hb_unicode_funcs_nil;
147 hb_buffer_reference (hb_buffer_t *buffer)
149 HB_OBJECT_DO_REFERENCE (buffer);
153 hb_buffer_get_reference_count (hb_buffer_t *buffer)
155 HB_OBJECT_DO_GET_REFERENCE_COUNT (buffer);
159 hb_buffer_destroy (hb_buffer_t *buffer)
161 HB_OBJECT_DO_DESTROY (buffer);
163 hb_unicode_funcs_destroy (buffer->unicode);
173 hb_buffer_set_unicode_funcs (hb_buffer_t *buffer,
174 hb_unicode_funcs_t *unicode)
177 unicode = &_hb_unicode_funcs_nil;
179 hb_unicode_funcs_reference (unicode);
180 hb_unicode_funcs_destroy (buffer->unicode);
181 buffer->unicode = unicode;
185 hb_buffer_get_unicode_funcs (hb_buffer_t *buffer)
187 return buffer->unicode;
191 hb_buffer_set_direction (hb_buffer_t *buffer,
192 hb_direction_t direction)
195 buffer->props.direction = direction;
199 hb_buffer_get_direction (hb_buffer_t *buffer)
201 return buffer->props.direction;
205 hb_buffer_set_script (hb_buffer_t *buffer,
208 buffer->props.script = script;
212 hb_buffer_get_script (hb_buffer_t *buffer)
214 return buffer->props.script;
218 hb_buffer_set_language (hb_buffer_t *buffer,
219 hb_language_t language)
221 buffer->props.language = language;
225 hb_buffer_get_language (hb_buffer_t *buffer)
227 return buffer->props.language;
232 hb_buffer_reset (hb_buffer_t *buffer)
234 hb_buffer_clear (buffer);
236 buffer->props = _hb_buffer_nil.props;
238 hb_unicode_funcs_destroy (buffer->unicode);
239 buffer->unicode = _hb_buffer_nil.unicode;
243 hb_buffer_clear (hb_buffer_t *buffer)
245 buffer->have_output = FALSE;
246 buffer->have_positions = FALSE;
247 buffer->in_error = FALSE;
251 buffer->out_info = buffer->info;
256 hb_buffer_ensure (hb_buffer_t *buffer, unsigned int size)
258 return _hb_buffer_ensure (buffer, size);
262 hb_buffer_add_glyph (hb_buffer_t *buffer,
263 hb_codepoint_t codepoint,
265 unsigned int cluster)
267 hb_glyph_info_t *glyph;
269 if (unlikely (!_hb_buffer_ensure (buffer, buffer->len + 1))) return;
271 glyph = &buffer->info[buffer->len];
273 memset (glyph, 0, sizeof (*glyph));
274 glyph->codepoint = codepoint;
276 glyph->cluster = cluster;
282 hb_buffer_clear_positions (hb_buffer_t *buffer)
284 _hb_buffer_clear_output (buffer);
285 buffer->have_output = FALSE;
286 buffer->have_positions = TRUE;
288 memset (buffer->pos, 0, sizeof (buffer->pos[0]) * buffer->len);
291 /* HarfBuzz-Internal API */
294 _hb_buffer_clear_output (hb_buffer_t *buffer)
296 buffer->have_output = TRUE;
297 buffer->have_positions = FALSE;
299 buffer->out_info = buffer->info;
303 _hb_buffer_swap (hb_buffer_t *buffer)
307 assert (buffer->have_output);
309 if (unlikely (buffer->in_error)) return;
311 if (buffer->out_info != buffer->info)
313 hb_glyph_info_t *tmp_string;
314 tmp_string = buffer->info;
315 buffer->info = buffer->out_info;
316 buffer->out_info = tmp_string;
317 buffer->pos = (hb_glyph_position_t *) buffer->out_info;
321 buffer->len = buffer->out_len;
322 buffer->out_len = tmp;
328 _hb_buffer_replace_glyphs_be16 (hb_buffer_t *buffer,
330 unsigned int num_out,
331 const uint16_t *glyph_data_be)
333 if (buffer->out_info != buffer->info ||
334 buffer->out_len + num_out > buffer->i + num_in)
336 if (unlikely (!_hb_buffer_ensure_separate (buffer, buffer->out_len + num_out)))
340 hb_glyph_info_t orig_info = buffer->info[buffer->i];
342 for (unsigned int i = 0; i < num_out; i++)
344 hb_glyph_info_t *info = &buffer->out_info[buffer->out_len + i];
346 info->codepoint = hb_be_uint16 (glyph_data_be[i]);
350 buffer->out_len += num_out;
354 _hb_buffer_replace_glyph (hb_buffer_t *buffer,
355 hb_codepoint_t glyph_index)
357 hb_glyph_info_t *info;
359 if (buffer->out_info != buffer->info)
361 if (unlikely (!_hb_buffer_ensure (buffer, buffer->out_len + 1))) return;
362 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
364 else if (buffer->out_len != buffer->i)
365 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
367 info = &buffer->out_info[buffer->out_len];
368 info->codepoint = glyph_index;
375 _hb_buffer_next_glyph (hb_buffer_t *buffer)
377 if (buffer->have_output)
379 if (buffer->out_info != buffer->info)
381 if (unlikely (!_hb_buffer_ensure (buffer, buffer->out_len + 1))) return;
382 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
384 else if (buffer->out_len != buffer->i)
385 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
394 _hb_buffer_reset_masks (hb_buffer_t *buffer,
397 unsigned int count = buffer->len;
398 for (unsigned int i = 0; i < count; i++)
399 buffer->info[i].mask = mask;
403 _hb_buffer_add_masks (hb_buffer_t *buffer,
406 unsigned int count = buffer->len;
407 for (unsigned int i = 0; i < count; i++)
408 buffer->info[i].mask |= mask;
412 _hb_buffer_set_masks (hb_buffer_t *buffer,
415 unsigned int cluster_start,
416 unsigned int cluster_end)
418 hb_mask_t not_mask = ~mask;
424 if (cluster_start == 0 && cluster_end == (unsigned int)-1) {
425 unsigned int count = buffer->len;
426 for (unsigned int i = 0; i < count; i++)
427 buffer->info[i].mask = (buffer->info[i].mask & not_mask) | value;
431 /* XXX can't bsearch since .cluster may not be sorted. */
432 /* Binary search to find the start position and go from there. */
433 unsigned int min = 0, max = buffer->len;
436 unsigned int mid = min + ((max - min) / 2);
437 if (buffer->info[mid].cluster < cluster_start)
442 unsigned int count = buffer->len;
443 for (unsigned int i = min; i < count && buffer->info[i].cluster < cluster_end; i++)
444 buffer->info[i].mask = (buffer->info[i].mask & not_mask) | value;
448 /* Public API again */
451 hb_buffer_set_length (hb_buffer_t *buffer,
454 if (!hb_buffer_ensure (buffer, length))
457 /* Wipe the new space */
458 if (length > buffer->len) {
459 memset (buffer->info + buffer->len, 0, sizeof (buffer->info[0]) * (length - buffer->len));
460 if (buffer->have_positions)
461 memset (buffer->pos + buffer->len, 0, sizeof (buffer->pos[0]) * (length - buffer->len));
464 buffer->len = length;
469 hb_buffer_get_length (hb_buffer_t *buffer)
474 /* Return value valid as long as buffer not modified */
476 hb_buffer_get_glyph_infos (hb_buffer_t *buffer)
478 return (hb_glyph_info_t *) buffer->info;
481 /* Return value valid as long as buffer not modified */
482 hb_glyph_position_t *
483 hb_buffer_get_glyph_positions (hb_buffer_t *buffer)
485 if (!buffer->have_positions)
486 hb_buffer_clear_positions (buffer);
488 return (hb_glyph_position_t *) buffer->pos;
493 reverse_range (hb_buffer_t *buffer,
499 for (i = start, j = end - 1; i < j; i++, j--) {
503 buffer->info[i] = buffer->info[j];
508 for (i = 0, j = end - 1; i < j; i++, j--) {
509 hb_glyph_position_t t;
512 buffer->pos[i] = buffer->pos[j];
519 hb_buffer_reverse (hb_buffer_t *buffer)
521 if (unlikely (!buffer->len))
524 reverse_range (buffer, 0, buffer->len);
528 hb_buffer_reverse_clusters (hb_buffer_t *buffer)
530 unsigned int i, start, count, last_cluster;
532 if (unlikely (!buffer->len))
535 hb_buffer_reverse (buffer);
539 last_cluster = buffer->info[0].cluster;
540 for (i = 1; i < count; i++) {
541 if (last_cluster != buffer->info[i].cluster) {
542 reverse_range (buffer, start, i);
544 last_cluster = buffer->info[i].cluster;
547 reverse_range (buffer, start, i);
553 const T *next = (const T *) text + item_offset; \
554 const T *end = next + item_length; \
555 while (next < end) { \
557 const T *old_next = next; \
558 next = UTF_NEXT (next, end, u); \
559 hb_buffer_add_glyph (buffer, u, 1, old_next - (const T *) text); \
564 #define UTF8_COMPUTE(Char, Mask, Len) \
565 if (Char < 128) { Len = 1; Mask = 0x7f; } \
566 else if ((Char & 0xe0) == 0xc0) { Len = 2; Mask = 0x1f; } \
567 else if ((Char & 0xf0) == 0xe0) { Len = 3; Mask = 0x0f; } \
568 else if ((Char & 0xf8) == 0xf0) { Len = 4; Mask = 0x07; } \
571 static inline const uint8_t *
572 hb_utf8_next (const uint8_t *text,
574 hb_codepoint_t *unicode)
577 unsigned int mask, len;
579 /* TODO check for overlong sequences? also: optimize? */
581 UTF8_COMPUTE (c, mask, len);
582 if (unlikely (!len || (unsigned int) (end - text) < len)) {
586 hb_codepoint_t result;
589 for (i = 1; i < len; i++)
591 if (unlikely ((text[i] & 0xc0) != 0x80))
597 result |= (text[i] & 0x3f);
605 hb_buffer_add_utf8 (hb_buffer_t *buffer,
607 unsigned int text_length HB_UNUSED,
608 unsigned int item_offset,
609 unsigned int item_length)
611 #define UTF_NEXT(S, E, U) hb_utf8_next (S, E, &(U))
616 static inline const uint16_t *
617 hb_utf16_next (const uint16_t *text,
619 hb_codepoint_t *unicode)
621 uint16_t c = *text++;
623 if (unlikely (c >= 0xd800 && c < 0xdc00)) {
626 if (text < end && ((l = *text), likely (l >= 0xdc00 && l < 0xe000))) {
628 *unicode = ((hb_codepoint_t) ((c) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000);
639 hb_buffer_add_utf16 (hb_buffer_t *buffer,
640 const uint16_t *text,
641 unsigned int text_length HB_UNUSED,
642 unsigned int item_offset,
643 unsigned int item_length)
645 #define UTF_NEXT(S, E, U) hb_utf16_next (S, E, &(U))
651 hb_buffer_add_utf32 (hb_buffer_t *buffer,
652 const uint32_t *text,
653 unsigned int text_length HB_UNUSED,
654 unsigned int item_offset,
655 unsigned int item_length)
657 #define UTF_NEXT(S, E, U) ((U) = *(S), (S)+1)