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"
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 info pointers: info and out_info. They always have
42 * the same allocated size, but different lengths.
44 * As an optimization, both info and out_info may point to the
45 * same piece of memory, which is owned by info. This remains the
46 * case as long as out_len doesn't exceed len at any time.
47 * In that case, swap() is no-op and the glyph operations operate
50 * As soon as out_info gets longer than info, out_info is moved over
51 * to an alternate buffer (which we reuse the pos buffer for!), and its
52 * current contents (out_len entries) are copied to the new place.
53 * This should all remain transparent to the user. swap() then
54 * switches info and out_info.
61 hb_buffer_ensure_separate (hb_buffer_t *buffer, unsigned int size)
63 if (unlikely (!hb_buffer_ensure (buffer, size))) return FALSE;
65 if (buffer->out_info == buffer->info)
67 assert (buffer->have_output);
69 buffer->out_info = (hb_internal_glyph_info_t *) buffer->pos;
70 memcpy (buffer->out_info, buffer->info, buffer->out_len * sizeof (buffer->out_info[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);
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_error = FALSE;
188 buffer->out_info = buffer->info;
189 buffer->max_lig_id = 0;
193 hb_buffer_ensure (hb_buffer_t *buffer, unsigned int size)
195 if (unlikely (size > buffer->allocated))
197 if (unlikely (buffer->in_error))
200 unsigned int new_allocated = buffer->allocated;
201 hb_internal_glyph_position_t *new_pos;
202 hb_internal_glyph_info_t *new_info;
205 separate_out = buffer->out_info != buffer->info;
207 while (size > new_allocated)
208 new_allocated += (new_allocated >> 1) + 8;
210 new_pos = (hb_internal_glyph_position_t *) realloc (buffer->pos, new_allocated * sizeof (buffer->pos[0]));
211 new_info = (hb_internal_glyph_info_t *) realloc (buffer->info, new_allocated * sizeof (buffer->info[0]));
213 if (unlikely (!new_pos || !new_info))
214 buffer->in_error = TRUE;
216 if (likely (new_pos))
217 buffer->pos = new_pos;
219 if (likely (new_info))
220 buffer->info = new_info;
222 buffer->out_info = separate_out ? (hb_internal_glyph_info_t *) buffer->pos : buffer->info;
223 if (likely (!buffer->in_error))
224 buffer->allocated = new_allocated;
227 return likely (!buffer->in_error);
231 hb_buffer_add_glyph (hb_buffer_t *buffer,
232 hb_codepoint_t codepoint,
234 unsigned int cluster)
236 hb_internal_glyph_info_t *glyph;
238 if (unlikely (!hb_buffer_ensure (buffer, buffer->len + 1))) return;
240 glyph = &buffer->info[buffer->len];
241 glyph->codepoint = codepoint;
243 glyph->cluster = cluster;
244 glyph->component = 0;
246 glyph->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
252 /* HarfBuzz-Internal API */
255 _hb_buffer_clear_output (hb_buffer_t *buffer)
257 buffer->have_output = TRUE;
258 buffer->have_positions = FALSE;
260 buffer->out_info = buffer->info;
264 hb_buffer_clear_positions (hb_buffer_t *buffer)
266 _hb_buffer_clear_output (buffer);
267 buffer->have_output = FALSE;
268 buffer->have_positions = TRUE;
270 if (unlikely (!buffer->pos))
272 buffer->pos = (hb_internal_glyph_position_t *) calloc (buffer->allocated, sizeof (buffer->pos[0]));
276 memset (buffer->pos, 0, sizeof (buffer->pos[0]) * buffer->len);
280 _hb_buffer_swap (hb_buffer_t *buffer)
284 assert (buffer->have_output);
286 if (unlikely (buffer->in_error)) return;
288 if (buffer->out_info != buffer->info)
290 hb_internal_glyph_info_t *tmp_string;
291 tmp_string = buffer->info;
292 buffer->info = buffer->out_info;
293 buffer->out_info = tmp_string;
294 buffer->pos = (hb_internal_glyph_position_t *) buffer->out_info;
298 buffer->len = buffer->out_len;
299 buffer->out_len = tmp;
304 /* The following function copies `num_out' elements from `glyph_data'
305 to `buffer->out_info', advancing the in array pointer in the structure
306 by `num_in' elements, and the out array pointer by `num_out' elements.
307 Finally, it sets the `length' field of `out' equal to
308 `pos' of the `out' structure.
310 If `component' is 0xFFFF, the component value from buffer->i
311 will copied `num_out' times, otherwise `component' itself will
312 be used to fill the `component' fields.
314 If `lig_id' is 0xFFFF, the lig_id value from buffer->i
315 will copied `num_out' times, otherwise `lig_id' itself will
316 be used to fill the `lig_id' fields.
318 The mask for all replacement glyphs are taken
319 from the glyph at position `buffer->i'.
321 The cluster value for the glyph at position buffer->i is used
322 for all replacement glyphs */
325 _hb_buffer_add_output_glyphs (hb_buffer_t *buffer,
327 unsigned int num_out,
328 const hb_codepoint_t *glyph_data,
329 unsigned short component,
330 unsigned short lig_id)
334 unsigned int cluster;
336 if (buffer->out_info != buffer->info ||
337 buffer->out_len + num_out > buffer->i + num_in)
339 if (unlikely (!hb_buffer_ensure_separate (buffer, buffer->out_len + num_out)))
343 mask = buffer->info[buffer->i].mask;
344 cluster = buffer->info[buffer->i].cluster;
345 if (component == 0xFFFF)
346 component = buffer->info[buffer->i].component;
347 if (lig_id == 0xFFFF)
348 lig_id = buffer->info[buffer->i].lig_id;
350 for (i = 0; i < num_out; i++)
352 hb_internal_glyph_info_t *info = &buffer->out_info[buffer->out_len + i];
353 info->codepoint = glyph_data[i];
355 info->cluster = cluster;
356 info->component = component;
357 info->lig_id = lig_id;
358 info->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
362 buffer->out_len += num_out;
366 _hb_buffer_add_output_glyphs_be16 (hb_buffer_t *buffer,
368 unsigned int num_out,
369 const uint16_t *glyph_data_be,
370 unsigned short component,
371 unsigned short lig_id)
375 unsigned int cluster;
377 if (buffer->out_info != buffer->info ||
378 buffer->out_len + num_out > buffer->i + num_in)
380 if (unlikely (!hb_buffer_ensure_separate (buffer, buffer->out_len + num_out)))
384 mask = buffer->info[buffer->i].mask;
385 cluster = buffer->info[buffer->i].cluster;
386 if (component == 0xFFFF)
387 component = buffer->info[buffer->i].component;
388 if (lig_id == 0xFFFF)
389 lig_id = buffer->info[buffer->i].lig_id;
391 for (i = 0; i < num_out; i++)
393 hb_internal_glyph_info_t *info = &buffer->out_info[buffer->out_len + i];
394 info->codepoint = hb_be_uint16 (glyph_data_be[i]);
396 info->cluster = cluster;
397 info->component = component;
398 info->lig_id = lig_id;
399 info->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
403 buffer->out_len += num_out;
407 _hb_buffer_add_output_glyph (hb_buffer_t *buffer,
408 hb_codepoint_t glyph_index,
409 unsigned short component,
410 unsigned short lig_id)
412 hb_internal_glyph_info_t *info;
414 if (buffer->out_info != buffer->info)
416 if (unlikely (!hb_buffer_ensure (buffer, buffer->out_len + 1))) return;
417 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
419 else if (buffer->out_len != buffer->i)
420 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
422 info = &buffer->out_info[buffer->out_len];
423 info->codepoint = glyph_index;
424 if (component != 0xFFFF)
425 info->component = component;
426 if (lig_id != 0xFFFF)
427 info->lig_id = lig_id;
428 info->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
435 _hb_buffer_next_glyph (hb_buffer_t *buffer)
437 if (buffer->have_output)
439 if (buffer->out_info != buffer->info)
441 if (unlikely (!hb_buffer_ensure (buffer, buffer->out_len + 1))) return;
442 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
444 else if (buffer->out_len != buffer->i)
445 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
455 hb_buffer_get_length (hb_buffer_t *buffer)
460 /* Return value valid as long as buffer not modified */
462 hb_buffer_get_glyph_infos (hb_buffer_t *buffer)
464 return (hb_glyph_info_t *) buffer->info;
467 /* Return value valid as long as buffer not modified */
468 hb_glyph_position_t *
469 hb_buffer_get_glyph_positions (hb_buffer_t *buffer)
471 if (!buffer->have_positions)
472 hb_buffer_clear_positions (buffer);
474 return (hb_glyph_position_t *) buffer->pos;
479 reverse_range (hb_buffer_t *buffer,
485 for (i = start, j = end - 1; i < j; i++, j--) {
486 hb_internal_glyph_info_t t;
489 buffer->info[i] = buffer->info[j];
494 for (i = 0, j = end - 1; i < j; i++, j--) {
495 hb_internal_glyph_position_t t;
498 buffer->pos[i] = buffer->pos[j];
505 hb_buffer_reverse (hb_buffer_t *buffer)
507 if (unlikely (!buffer->len))
510 reverse_range (buffer, 0, buffer->len);
514 hb_buffer_reverse_clusters (hb_buffer_t *buffer)
516 unsigned int i, start, count, last_cluster;
518 if (unlikely (!buffer->len))
521 hb_buffer_reverse (buffer);
525 last_cluster = buffer->info[0].cluster;
526 for (i = 1; i < count; i++) {
527 if (last_cluster != buffer->info[i].cluster) {
528 reverse_range (buffer, start, i);
530 last_cluster = buffer->info[i].cluster;
533 reverse_range (buffer, start, i);
539 const T *next = (const T *) text + item_offset; \
540 const T *end = next + item_length; \
541 while (next < end) { \
543 const T *old_next = next; \
544 next = UTF_NEXT (next, end, u); \
545 hb_buffer_add_glyph (buffer, u, 0, old_next - (const T *) text); \
550 #define UTF8_COMPUTE(Char, Mask, Len) \
551 if (Char < 128) { Len = 1; Mask = 0x7f; } \
552 else if ((Char & 0xe0) == 0xc0) { Len = 2; Mask = 0x1f; } \
553 else if ((Char & 0xf0) == 0xe0) { Len = 3; Mask = 0x0f; } \
554 else if ((Char & 0xf8) == 0xf0) { Len = 4; Mask = 0x07; } \
557 static inline const uint8_t *
558 hb_utf8_next (const uint8_t *text,
560 hb_codepoint_t *unicode)
563 unsigned int mask, len;
565 UTF8_COMPUTE (c, mask, len);
566 if (unlikely (!len || (unsigned int) (end - text) < len)) {
570 hb_codepoint_t result;
573 for (i = 1; i < len; i++)
575 if (unlikely ((text[i] & 0xc0) != 0x80))
581 result |= (text[i] & 0x3f);
589 hb_buffer_add_utf8 (hb_buffer_t *buffer,
591 unsigned int text_length HB_UNUSED,
592 unsigned int item_offset,
593 unsigned int item_length)
595 #define UTF_NEXT(S, E, U) hb_utf8_next (S, E, &(U))
600 static inline const uint16_t *
601 hb_utf16_next (const uint16_t *text,
603 hb_codepoint_t *unicode)
605 uint16_t c = *text++;
607 if (unlikely (c >= 0xd800 && c < 0xdc00)) {
610 if (text < end && ((l = *text), unlikely (l >= 0xdc00 && l < 0xe000))) {
612 *unicode = ((hb_codepoint_t) ((c) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000);
623 hb_buffer_add_utf16 (hb_buffer_t *buffer,
624 const uint16_t *text,
625 unsigned int text_length HB_UNUSED,
626 unsigned int item_offset,
627 unsigned int item_length)
629 #define UTF_NEXT(S, E, U) hb_utf16_next (S, E, &(U))
635 hb_buffer_add_utf32 (hb_buffer_t *buffer,
636 const uint32_t *text,
637 unsigned int text_length HB_UNUSED,
638 unsigned int item_offset,
639 unsigned int item_length)
641 #define UTF_NEXT(S, E, U) ((U) = *(S), (S)+1)