2 * Copyright (C) 1998-2004 David Turner and Werner Lemberg
3 * Copyright (C) 2004,2007 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.h"
33 static hb_buffer_t _hb_buffer_nil = {
34 HB_REFERENCE_COUNT_INVALID, /* ref_count */
36 &_hb_unicode_funcs_nil /* unicode */
39 /* Here is how the buffer works internally:
41 * There are two string pointers: in_string and out_string. They
42 * always have same allocated size, but different length and positions.
44 * As an optimization, both in_string and out_string may point to the
45 * same piece of memory, which is owned by in_string. This remains the
46 * case as long as out_length doesn't exceed in_length at any time.
47 * In that case, swap() is no-op and the glyph operations operate mostly
50 * As soon as out_string gets longer than in_string, out_string is moved over
51 * to an alternate buffer (which we reuse the positions buffer for!), and its
52 * current contents (out_length entries) are copied to the alt buffer.
53 * This should all remain transparent to the user. swap() then switches
54 * in_string and out_string.
57 /* XXX err handling */
62 hb_buffer_ensure_separate (hb_buffer_t *buffer, unsigned int size)
64 hb_buffer_ensure (buffer, size);
65 if (buffer->out_string == buffer->in_string)
67 assert (buffer->have_output);
68 if (!buffer->positions)
69 buffer->positions = calloc (buffer->allocated, sizeof (buffer->positions[0]));
71 buffer->out_string = (hb_internal_glyph_info_t *) buffer->positions;
72 memcpy (buffer->out_string, buffer->in_string, buffer->out_length * sizeof (buffer->out_string[0]));
79 hb_buffer_create (unsigned int pre_alloc_size)
83 if (!HB_OBJECT_DO_CREATE (hb_buffer_t, buffer))
84 return &_hb_buffer_nil;
87 hb_buffer_ensure(buffer, pre_alloc_size);
89 buffer->unicode = &_hb_unicode_funcs_nil;
95 hb_buffer_reference (hb_buffer_t *buffer)
97 HB_OBJECT_DO_REFERENCE (buffer);
101 hb_buffer_get_reference_count (hb_buffer_t *buffer)
103 HB_OBJECT_DO_GET_REFERENCE_COUNT (buffer);
107 hb_buffer_destroy (hb_buffer_t *buffer)
109 HB_OBJECT_DO_DESTROY (buffer);
111 hb_unicode_funcs_destroy (buffer->unicode);
113 free (buffer->in_string);
114 free (buffer->positions);
121 hb_buffer_set_unicode_funcs (hb_buffer_t *buffer,
122 hb_unicode_funcs_t *unicode)
125 unicode = &_hb_unicode_funcs_nil;
127 hb_unicode_funcs_reference (unicode);
128 hb_unicode_funcs_destroy (buffer->unicode);
129 buffer->unicode = unicode;
133 hb_buffer_get_unicode_funcs (hb_buffer_t *buffer)
135 return buffer->unicode;
139 hb_buffer_set_direction (hb_buffer_t *buffer,
140 hb_direction_t direction)
143 buffer->direction = direction;
147 hb_buffer_get_direction (hb_buffer_t *buffer)
149 return buffer->direction;
153 hb_buffer_set_script (hb_buffer_t *buffer,
156 buffer->script = script;
160 hb_buffer_get_script (hb_buffer_t *buffer)
162 return buffer->script;
166 hb_buffer_set_language (hb_buffer_t *buffer,
167 hb_language_t language)
169 buffer->language = language;
173 hb_buffer_get_language (hb_buffer_t *buffer)
175 return buffer->language;
180 hb_buffer_clear (hb_buffer_t *buffer)
182 buffer->have_output = FALSE;
183 buffer->have_positions = FALSE;
184 buffer->in_length = 0;
185 buffer->out_length = 0;
188 buffer->out_string = buffer->in_string;
189 buffer->max_lig_id = 0;
193 hb_buffer_ensure (hb_buffer_t *buffer, unsigned int size)
195 unsigned int new_allocated = buffer->allocated;
197 if (size > new_allocated)
199 while (size > new_allocated)
200 new_allocated += (new_allocated >> 1) + 8;
202 if (buffer->positions)
203 buffer->positions = realloc (buffer->positions, new_allocated * sizeof (buffer->positions[0]));
205 if (buffer->out_string != buffer->in_string)
207 buffer->in_string = realloc (buffer->in_string, new_allocated * sizeof (buffer->in_string[0]));
208 buffer->out_string = (hb_internal_glyph_info_t *) buffer->positions;
212 buffer->in_string = realloc (buffer->in_string, new_allocated * sizeof (buffer->in_string[0]));
213 buffer->out_string = buffer->in_string;
216 buffer->allocated = new_allocated;
221 hb_buffer_add_glyph (hb_buffer_t *buffer,
222 hb_codepoint_t codepoint,
224 unsigned int cluster)
226 hb_internal_glyph_info_t *glyph;
228 hb_buffer_ensure (buffer, buffer->in_length + 1);
230 glyph = &buffer->in_string[buffer->in_length];
231 glyph->codepoint = codepoint;
233 glyph->cluster = cluster;
234 glyph->component = 0;
236 glyph->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
242 /* HarfBuzz-Internal API */
245 _hb_buffer_clear_output (hb_buffer_t *buffer)
247 buffer->have_output = TRUE;
248 buffer->have_positions = FALSE;
249 buffer->out_length = 0;
251 buffer->out_string = buffer->in_string;
255 hb_buffer_clear_positions (hb_buffer_t *buffer)
257 _hb_buffer_clear_output (buffer);
258 buffer->have_output = FALSE;
259 buffer->have_positions = TRUE;
261 if (unlikely (!buffer->positions))
263 buffer->positions = calloc (buffer->allocated, sizeof (buffer->positions[0]));
267 memset (buffer->positions, 0, sizeof (buffer->positions[0]) * buffer->in_length);
271 _hb_buffer_swap (hb_buffer_t *buffer)
275 assert (buffer->have_output);
277 if (buffer->out_string != buffer->in_string)
279 hb_internal_glyph_info_t *tmp_string;
280 tmp_string = buffer->in_string;
281 buffer->in_string = buffer->out_string;
282 buffer->out_string = tmp_string;
283 buffer->positions = (hb_internal_glyph_position_t *) buffer->out_string;
286 tmp = buffer->in_length;
287 buffer->in_length = buffer->out_length;
288 buffer->out_length = tmp;
290 tmp = buffer->in_pos;
291 buffer->in_pos = buffer->out_pos;
292 buffer->out_pos = tmp;
295 /* The following function copies `num_out' elements from `glyph_data'
296 to `buffer->out_string', advancing the in array pointer in the structure
297 by `num_in' elements, and the out array pointer by `num_out' elements.
298 Finally, it sets the `length' field of `out' equal to
299 `pos' of the `out' structure.
301 If `component' is 0xFFFF, the component value from buffer->in_pos
302 will copied `num_out' times, otherwise `component' itself will
303 be used to fill the `component' fields.
305 If `lig_id' is 0xFFFF, the lig_id value from buffer->in_pos
306 will copied `num_out' times, otherwise `lig_id' itself will
307 be used to fill the `lig_id' fields.
309 The mask for all replacement glyphs are taken
310 from the glyph at position `buffer->in_pos'.
312 The cluster value for the glyph at position buffer->in_pos is used
313 for all replacement glyphs */
316 _hb_buffer_add_output_glyphs (hb_buffer_t *buffer,
318 unsigned int num_out,
319 const hb_codepoint_t *glyph_data,
320 unsigned short component,
321 unsigned short lig_id)
325 unsigned int cluster;
327 if (buffer->out_string != buffer->in_string ||
328 buffer->out_pos + num_out > buffer->in_pos + num_in)
330 hb_buffer_ensure_separate (buffer, buffer->out_pos + num_out);
333 mask = buffer->in_string[buffer->in_pos].mask;
334 cluster = buffer->in_string[buffer->in_pos].cluster;
335 if (component == 0xFFFF)
336 component = buffer->in_string[buffer->in_pos].component;
337 if (lig_id == 0xFFFF)
338 lig_id = buffer->in_string[buffer->in_pos].lig_id;
340 for (i = 0; i < num_out; i++)
342 hb_internal_glyph_info_t *info = &buffer->out_string[buffer->out_pos + i];
343 info->codepoint = glyph_data[i];
345 info->cluster = cluster;
346 info->component = component;
347 info->lig_id = lig_id;
348 info->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
351 buffer->in_pos += num_in;
352 buffer->out_pos += num_out;
353 buffer->out_length = buffer->out_pos;
357 _hb_buffer_add_output_glyphs_be16 (hb_buffer_t *buffer,
359 unsigned int num_out,
360 const uint16_t *glyph_data_be,
361 unsigned short component,
362 unsigned short lig_id)
366 unsigned int cluster;
368 if (buffer->out_string != buffer->in_string ||
369 buffer->out_pos + num_out > buffer->in_pos + num_in)
371 hb_buffer_ensure_separate (buffer, buffer->out_pos + num_out);
374 mask = buffer->in_string[buffer->in_pos].mask;
375 cluster = buffer->in_string[buffer->in_pos].cluster;
376 if (component == 0xFFFF)
377 component = buffer->in_string[buffer->in_pos].component;
378 if (lig_id == 0xFFFF)
379 lig_id = buffer->in_string[buffer->in_pos].lig_id;
381 for (i = 0; i < num_out; i++)
383 hb_internal_glyph_info_t *info = &buffer->out_string[buffer->out_pos + i];
384 info->codepoint = hb_be_uint16 (glyph_data_be[i]);
386 info->cluster = cluster;
387 info->component = component;
388 info->lig_id = lig_id;
389 info->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
392 buffer->in_pos += num_in;
393 buffer->out_pos += num_out;
394 buffer->out_length = buffer->out_pos;
398 _hb_buffer_add_output_glyph (hb_buffer_t *buffer,
399 hb_codepoint_t glyph_index,
400 unsigned short component,
401 unsigned short lig_id)
403 hb_internal_glyph_info_t *info;
405 if (buffer->out_string != buffer->in_string)
407 hb_buffer_ensure (buffer, buffer->out_pos + 1);
408 buffer->out_string[buffer->out_pos] = buffer->in_string[buffer->in_pos];
410 else if (buffer->out_pos != buffer->in_pos)
411 buffer->out_string[buffer->out_pos] = buffer->in_string[buffer->in_pos];
413 info = &buffer->out_string[buffer->out_pos];
414 info->codepoint = glyph_index;
415 if (component != 0xFFFF)
416 info->component = component;
417 if (lig_id != 0xFFFF)
418 info->lig_id = lig_id;
419 info->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
423 buffer->out_length = buffer->out_pos;
427 _hb_buffer_next_glyph (hb_buffer_t *buffer)
429 if (buffer->have_output)
431 if (buffer->out_string != buffer->in_string)
433 hb_buffer_ensure (buffer, buffer->out_pos + 1);
434 buffer->out_string[buffer->out_pos] = buffer->in_string[buffer->in_pos];
436 else if (buffer->out_pos != buffer->in_pos)
437 buffer->out_string[buffer->out_pos] = buffer->in_string[buffer->in_pos];
440 buffer->out_length = buffer->out_pos;
447 _hb_buffer_replace_glyph (hb_buffer_t *buffer,
448 hb_codepoint_t glyph_index)
450 _hb_buffer_add_output_glyph (buffer, glyph_index, 0xFFFF, 0xFFFF);
454 _hb_buffer_allocate_lig_id (hb_buffer_t *buffer)
456 return ++buffer->max_lig_id;
461 hb_buffer_get_length (hb_buffer_t *buffer)
463 return buffer->in_length;
466 /* Return value valid as long as buffer not modified */
468 hb_buffer_get_glyph_infos (hb_buffer_t *buffer)
470 return (hb_glyph_info_t *) buffer->in_string;
473 /* Return value valid as long as buffer not modified */
474 hb_glyph_position_t *
475 hb_buffer_get_glyph_positions (hb_buffer_t *buffer)
477 if (!buffer->have_positions)
478 hb_buffer_clear_positions (buffer);
480 return (hb_glyph_position_t *) buffer->positions;
485 reverse_range (hb_buffer_t *buffer,
491 for (i = start, j = end - 1; i < j; i++, j--) {
492 hb_internal_glyph_info_t t;
494 t = buffer->in_string[i];
495 buffer->in_string[i] = buffer->in_string[j];
496 buffer->in_string[j] = t;
499 if (buffer->positions) {
500 for (i = 0, j = end - 1; i < j; i++, j--) {
501 hb_internal_glyph_position_t t;
503 t = buffer->positions[i];
504 buffer->positions[i] = buffer->positions[j];
505 buffer->positions[j] = t;
511 hb_buffer_reverse (hb_buffer_t *buffer)
513 if (unlikely (!buffer->in_length))
516 reverse_range (buffer, 0, buffer->in_length);
520 hb_buffer_reverse_clusters (hb_buffer_t *buffer)
522 unsigned int i, start, count, last_cluster;
524 if (unlikely (!buffer->in_length))
527 hb_buffer_reverse (buffer);
529 count = buffer->in_length;
531 last_cluster = buffer->in_string[0].cluster;
532 for (i = 1; i < count; i++) {
533 if (last_cluster != buffer->in_string[i].cluster) {
534 reverse_range (buffer, start, i);
536 last_cluster = buffer->in_string[i].cluster;
539 reverse_range (buffer, start, i);
545 const T *next = (const T *) text + item_offset; \
546 const T *end = next + item_length; \
547 while (next < end) { \
549 const T *old_next = next; \
550 next = UTF_NEXT (next, end, u); \
551 hb_buffer_add_glyph (buffer, u, 0, old_next - (const T *) text); \
556 #define UTF8_COMPUTE(Char, Mask, Len) \
557 if (Char < 128) { Len = 1; Mask = 0x7f; } \
558 else if ((Char & 0xe0) == 0xc0) { Len = 2; Mask = 0x1f; } \
559 else if ((Char & 0xf0) == 0xe0) { Len = 3; Mask = 0x0f; } \
560 else if ((Char & 0xf8) == 0xf0) { Len = 4; Mask = 0x07; } \
563 static inline const uint8_t *
564 hb_utf8_next (const uint8_t *text,
566 hb_codepoint_t *unicode)
569 unsigned int mask, len;
571 UTF8_COMPUTE (c, mask, len);
572 if (unlikely (!len || (unsigned int) (end - text) < len)) {
576 hb_codepoint_t result;
579 for (i = 1; i < len; i++)
581 if (unlikely ((text[i] & 0xc0) != 0x80))
587 result |= (text[i] & 0x3f);
595 hb_buffer_add_utf8 (hb_buffer_t *buffer,
597 unsigned int text_length HB_UNUSED,
598 unsigned int item_offset,
599 unsigned int item_length)
601 #define UTF_NEXT(S, E, U) hb_utf8_next (S, E, &(U))
606 static inline const uint16_t *
607 hb_utf16_next (const uint16_t *text,
609 hb_codepoint_t *unicode)
611 uint16_t c = *text++;
613 if (unlikely (c >= 0xd800 && c < 0xdc00)) {
616 if (text < end && ((l = *text), unlikely (l >= 0xdc00 && l < 0xe000))) {
618 *unicode = ((hb_codepoint_t) ((c) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000);
629 hb_buffer_add_utf16 (hb_buffer_t *buffer,
630 const uint16_t *text,
631 unsigned int text_length HB_UNUSED,
632 unsigned int item_offset,
633 unsigned int item_length)
635 #define UTF_NEXT(S, E, U) hb_utf16_next (S, E, &(U))
641 hb_buffer_add_utf32 (hb_buffer_t *buffer,
642 const uint32_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) ((U) = *(S), (S)+1)