2 * Copyright (C) 1998-2004 David Turner and Werner Lemberg
3 * Copyright (C) 2004,2007,2009,2010 Red Hat, Inc.
5 * This is part of HarfBuzz, a text shaping library.
7 * Permission is hereby granted, without written agreement and without
8 * license or royalty fees, to use, copy, modify, and distribute this
9 * software and its documentation for any purpose, provided that the
10 * above copyright notice and the following two paragraphs appear in
11 * all copies of this software.
13 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
14 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
15 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
16 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
19 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
20 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
22 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
23 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 * Red Hat Author(s): Owen Taylor, Behdad Esfahbod
28 #include "hb-buffer-private.hh"
35 static hb_buffer_t _hb_buffer_nil = {
36 HB_REFERENCE_COUNT_INVALID, /* ref_count */
38 &_hb_unicode_funcs_nil /* unicode */
41 /* Here is how the buffer works internally:
43 * There are two info pointers: info and out_info. They always have
44 * the same allocated size, but different lengths.
46 * As an optimization, both info and out_info may point to the
47 * same piece of memory, which is owned by info. This remains the
48 * case as long as out_len doesn't exceed len at any time.
49 * In that case, swap() is no-op and the glyph operations operate
52 * As soon as out_info gets longer than info, out_info is moved over
53 * to an alternate buffer (which we reuse the pos buffer for!), and its
54 * current contents (out_len entries) are copied to the new place.
55 * This should all remain transparent to the user. swap() then
56 * switches info and out_info.
61 _hb_buffer_enlarge (hb_buffer_t *buffer, unsigned int size)
63 if (unlikely (buffer->in_error))
66 unsigned int new_allocated = buffer->allocated;
67 hb_glyph_position_t *new_pos;
68 hb_glyph_info_t *new_info;
71 separate_out = buffer->out_info != buffer->info;
73 while (size > new_allocated)
74 new_allocated += (new_allocated >> 1) + 8;
76 new_pos = (hb_glyph_position_t *) realloc (buffer->pos, new_allocated * sizeof (buffer->pos[0]));
77 new_info = (hb_glyph_info_t *) realloc (buffer->info, new_allocated * sizeof (buffer->info[0]));
79 if (unlikely (!new_pos || !new_info))
80 buffer->in_error = TRUE;
83 buffer->pos = new_pos;
85 if (likely (new_info))
86 buffer->info = new_info;
88 buffer->out_info = separate_out ? (hb_glyph_info_t *) buffer->pos : buffer->info;
89 if (likely (!buffer->in_error))
90 buffer->allocated = new_allocated;
92 return likely (!buffer->in_error);
95 static inline hb_bool_t
96 _hb_buffer_ensure (hb_buffer_t *buffer, unsigned int size)
98 return likely (size <= buffer->allocated) ? TRUE : _hb_buffer_enlarge (buffer, size);
101 static inline hb_bool_t
102 _hb_buffer_ensure_separate (hb_buffer_t *buffer, unsigned int size)
104 if (unlikely (!_hb_buffer_ensure (buffer, size))) return FALSE;
106 if (buffer->out_info == buffer->info)
108 assert (buffer->have_output);
110 buffer->out_info = (hb_glyph_info_t *) buffer->pos;
111 memcpy (buffer->out_info, buffer->info, buffer->out_len * sizeof (buffer->out_info[0]));
121 hb_buffer_create (unsigned int pre_alloc_size)
125 if (!HB_OBJECT_DO_CREATE (hb_buffer_t, buffer))
126 return &_hb_buffer_nil;
129 _hb_buffer_ensure (buffer, pre_alloc_size);
131 buffer->unicode = &_hb_unicode_funcs_nil;
137 hb_buffer_reference (hb_buffer_t *buffer)
139 HB_OBJECT_DO_REFERENCE (buffer);
143 hb_buffer_get_reference_count (hb_buffer_t *buffer)
145 HB_OBJECT_DO_GET_REFERENCE_COUNT (buffer);
149 hb_buffer_destroy (hb_buffer_t *buffer)
151 HB_OBJECT_DO_DESTROY (buffer);
153 hb_unicode_funcs_destroy (buffer->unicode);
163 hb_buffer_set_unicode_funcs (hb_buffer_t *buffer,
164 hb_unicode_funcs_t *unicode)
167 unicode = &_hb_unicode_funcs_nil;
169 hb_unicode_funcs_reference (unicode);
170 hb_unicode_funcs_destroy (buffer->unicode);
171 buffer->unicode = unicode;
175 hb_buffer_get_unicode_funcs (hb_buffer_t *buffer)
177 return buffer->unicode;
181 hb_buffer_set_direction (hb_buffer_t *buffer,
182 hb_direction_t direction)
185 buffer->props.direction = direction;
189 hb_buffer_get_direction (hb_buffer_t *buffer)
191 return buffer->props.direction;
195 hb_buffer_set_script (hb_buffer_t *buffer,
198 buffer->props.script = script;
202 hb_buffer_get_script (hb_buffer_t *buffer)
204 return buffer->props.script;
208 hb_buffer_set_language (hb_buffer_t *buffer,
209 hb_language_t language)
211 buffer->props.language = language;
215 hb_buffer_get_language (hb_buffer_t *buffer)
217 return buffer->props.language;
222 hb_buffer_clear (hb_buffer_t *buffer)
224 buffer->have_output = FALSE;
225 buffer->have_positions = FALSE;
226 buffer->in_error = FALSE;
230 buffer->out_info = buffer->info;
231 buffer->max_lig_id = 0;
235 hb_buffer_ensure (hb_buffer_t *buffer, unsigned int size)
237 return _hb_buffer_ensure (buffer, size);
241 hb_buffer_add_glyph (hb_buffer_t *buffer,
242 hb_codepoint_t codepoint,
244 unsigned int cluster)
246 hb_glyph_info_t *glyph;
248 if (unlikely (!_hb_buffer_ensure (buffer, buffer->len + 1))) return;
250 glyph = &buffer->info[buffer->len];
251 glyph->codepoint = codepoint;
253 glyph->cluster = cluster;
254 glyph->component() = 0;
256 glyph->gproperty() = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
262 hb_buffer_clear_positions (hb_buffer_t *buffer)
264 _hb_buffer_clear_output (buffer);
265 buffer->have_output = FALSE;
266 buffer->have_positions = TRUE;
268 if (unlikely (!buffer->pos))
270 buffer->pos = (hb_glyph_position_t *) calloc (buffer->allocated, sizeof (buffer->pos[0]));
274 memset (buffer->pos, 0, sizeof (buffer->pos[0]) * buffer->len);
277 /* HarfBuzz-Internal API */
280 _hb_buffer_clear_output (hb_buffer_t *buffer)
282 buffer->have_output = TRUE;
283 buffer->have_positions = FALSE;
285 buffer->out_info = buffer->info;
289 _hb_buffer_swap (hb_buffer_t *buffer)
293 assert (buffer->have_output);
295 if (unlikely (buffer->in_error)) return;
297 if (buffer->out_info != buffer->info)
299 hb_glyph_info_t *tmp_string;
300 tmp_string = buffer->info;
301 buffer->info = buffer->out_info;
302 buffer->out_info = tmp_string;
303 buffer->pos = (hb_glyph_position_t *) buffer->out_info;
307 buffer->len = buffer->out_len;
308 buffer->out_len = tmp;
314 _hb_buffer_add_output_glyphs (hb_buffer_t *buffer,
316 unsigned int num_out,
317 const hb_codepoint_t *glyph_data,
318 unsigned short component,
319 unsigned short lig_id)
323 unsigned int cluster;
325 if (buffer->out_info != buffer->info ||
326 buffer->out_len + num_out > buffer->i + num_in)
328 if (unlikely (!_hb_buffer_ensure_separate (buffer, buffer->out_len + num_out)))
332 mask = buffer->info[buffer->i].mask;
333 cluster = buffer->info[buffer->i].cluster;
334 if (component == 0xFFFF)
335 component = buffer->info[buffer->i].component();
336 if (lig_id == 0xFFFF)
337 lig_id = buffer->info[buffer->i].lig_id();
339 for (i = 0; i < num_out; i++)
341 hb_glyph_info_t *info = &buffer->out_info[buffer->out_len + i];
342 info->codepoint = glyph_data[i];
344 info->cluster = cluster;
345 info->component() = component;
346 info->lig_id() = lig_id;
347 info->gproperty() = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
351 buffer->out_len += num_out;
355 _hb_buffer_add_output_glyphs_be16 (hb_buffer_t *buffer,
357 unsigned int num_out,
358 const uint16_t *glyph_data_be,
359 unsigned short component,
360 unsigned short lig_id)
364 unsigned int cluster;
366 if (buffer->out_info != buffer->info ||
367 buffer->out_len + num_out > buffer->i + num_in)
369 if (unlikely (!_hb_buffer_ensure_separate (buffer, buffer->out_len + num_out)))
373 mask = buffer->info[buffer->i].mask;
374 cluster = buffer->info[buffer->i].cluster;
375 if (component == 0xFFFF)
376 component = buffer->info[buffer->i].component();
377 if (lig_id == 0xFFFF)
378 lig_id = buffer->info[buffer->i].lig_id();
380 for (i = 0; i < num_out; i++)
382 hb_glyph_info_t *info = &buffer->out_info[buffer->out_len + i];
383 info->codepoint = hb_be_uint16 (glyph_data_be[i]);
385 info->cluster = cluster;
386 info->component() = component;
387 info->lig_id() = lig_id;
388 info->gproperty() = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
392 buffer->out_len += num_out;
396 _hb_buffer_add_output_glyph (hb_buffer_t *buffer,
397 hb_codepoint_t glyph_index,
398 unsigned short component,
399 unsigned short lig_id)
401 hb_glyph_info_t *info;
403 if (buffer->out_info != buffer->info)
405 if (unlikely (!_hb_buffer_ensure (buffer, buffer->out_len + 1))) return;
406 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
408 else if (buffer->out_len != buffer->i)
409 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
411 info = &buffer->out_info[buffer->out_len];
412 info->codepoint = glyph_index;
413 if (component != 0xFFFF)
414 info->component() = component;
415 if (lig_id != 0xFFFF)
416 info->lig_id() = lig_id;
417 info->gproperty() = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
424 _hb_buffer_next_glyph (hb_buffer_t *buffer)
426 if (buffer->have_output)
428 if (buffer->out_info != buffer->info)
430 if (unlikely (!_hb_buffer_ensure (buffer, buffer->out_len + 1))) return;
431 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
433 else if (buffer->out_len != buffer->i)
434 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
443 _hb_buffer_reset_masks (hb_buffer_t *buffer,
446 unsigned int count = buffer->len;
447 for (unsigned int i = 0; i < count; i++)
448 buffer->info[i].mask = mask;
452 _hb_buffer_add_masks (hb_buffer_t *buffer,
455 unsigned int count = buffer->len;
456 for (unsigned int i = 0; i < count; i++)
457 buffer->info[i].mask |= mask;
461 _hb_buffer_set_masks (hb_buffer_t *buffer,
464 unsigned int cluster_start,
465 unsigned int cluster_end)
467 hb_mask_t not_mask = ~mask;
473 if (cluster_start == 0 && cluster_end == (unsigned int)-1) {
474 unsigned int count = buffer->len;
475 for (unsigned int i = 0; i < count; i++)
476 buffer->info[i].mask = (buffer->info[i].mask & not_mask) | value;
480 /* XXX can't bsearch since .cluster may not be sorted. */
481 /* Binary search to find the start position and go from there. */
482 unsigned int min = 0, max = buffer->len;
485 unsigned int mid = min + ((max - min) / 2);
486 if (buffer->info[mid].cluster < cluster_start)
491 unsigned int count = buffer->len;
492 for (unsigned int i = min; i < count && buffer->info[i].cluster < cluster_end; i++)
493 buffer->info[i].mask = (buffer->info[i].mask & not_mask) | value;
497 /* Public API again */
500 hb_buffer_get_length (hb_buffer_t *buffer)
505 /* Return value valid as long as buffer not modified */
507 hb_buffer_get_glyph_infos (hb_buffer_t *buffer)
509 return (hb_glyph_info_t *) buffer->info;
512 /* Return value valid as long as buffer not modified */
513 hb_glyph_position_t *
514 hb_buffer_get_glyph_positions (hb_buffer_t *buffer)
516 if (!buffer->have_positions)
517 hb_buffer_clear_positions (buffer);
519 return (hb_glyph_position_t *) buffer->pos;
524 reverse_range (hb_buffer_t *buffer,
530 for (i = start, j = end - 1; i < j; i++, j--) {
534 buffer->info[i] = buffer->info[j];
539 for (i = 0, j = end - 1; i < j; i++, j--) {
540 hb_glyph_position_t t;
543 buffer->pos[i] = buffer->pos[j];
550 hb_buffer_reverse (hb_buffer_t *buffer)
552 if (unlikely (!buffer->len))
555 reverse_range (buffer, 0, buffer->len);
559 hb_buffer_reverse_clusters (hb_buffer_t *buffer)
561 unsigned int i, start, count, last_cluster;
563 if (unlikely (!buffer->len))
566 hb_buffer_reverse (buffer);
570 last_cluster = buffer->info[0].cluster;
571 for (i = 1; i < count; i++) {
572 if (last_cluster != buffer->info[i].cluster) {
573 reverse_range (buffer, start, i);
575 last_cluster = buffer->info[i].cluster;
578 reverse_range (buffer, start, i);
584 const T *next = (const T *) text + item_offset; \
585 const T *end = next + item_length; \
586 while (next < end) { \
588 const T *old_next = next; \
589 next = UTF_NEXT (next, end, u); \
590 hb_buffer_add_glyph (buffer, u, 1, old_next - (const T *) text); \
595 #define UTF8_COMPUTE(Char, Mask, Len) \
596 if (Char < 128) { Len = 1; Mask = 0x7f; } \
597 else if ((Char & 0xe0) == 0xc0) { Len = 2; Mask = 0x1f; } \
598 else if ((Char & 0xf0) == 0xe0) { Len = 3; Mask = 0x0f; } \
599 else if ((Char & 0xf8) == 0xf0) { Len = 4; Mask = 0x07; } \
602 static inline const uint8_t *
603 hb_utf8_next (const uint8_t *text,
605 hb_codepoint_t *unicode)
608 unsigned int mask, len;
610 /* TODO check for overlong sequences? also: optimize? */
612 UTF8_COMPUTE (c, mask, len);
613 if (unlikely (!len || (unsigned int) (end - text) < len)) {
617 hb_codepoint_t result;
620 for (i = 1; i < len; i++)
622 if (unlikely ((text[i] & 0xc0) != 0x80))
628 result |= (text[i] & 0x3f);
636 hb_buffer_add_utf8 (hb_buffer_t *buffer,
638 unsigned int text_length HB_UNUSED,
639 unsigned int item_offset,
640 unsigned int item_length)
642 #define UTF_NEXT(S, E, U) hb_utf8_next (S, E, &(U))
647 static inline const uint16_t *
648 hb_utf16_next (const uint16_t *text,
650 hb_codepoint_t *unicode)
652 uint16_t c = *text++;
654 if (unlikely (c >= 0xd800 && c < 0xdc00)) {
657 if (text < end && ((l = *text), unlikely (l >= 0xdc00 && l < 0xe000))) {
659 *unicode = ((hb_codepoint_t) ((c) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000);
670 hb_buffer_add_utf16 (hb_buffer_t *buffer,
671 const uint16_t *text,
672 unsigned int text_length HB_UNUSED,
673 unsigned int item_offset,
674 unsigned int item_length)
676 #define UTF_NEXT(S, E, U) hb_utf16_next (S, E, &(U))
682 hb_buffer_add_utf32 (hb_buffer_t *buffer,
683 const uint32_t *text,
684 unsigned int text_length HB_UNUSED,
685 unsigned int item_offset,
686 unsigned int item_length)
688 #define UTF_NEXT(S, E, U) ((U) = *(S), (S)+1)