2 * Copyright (C) 1998-2004 David Turner and Werner Lemberg
3 * Copyright (C) 2004,2007 Red Hat, Inc.
5 * This is part of HarfBuzz, an OpenType Layout engine 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)
124 hb_unicode_funcs_reference (unicode);
125 hb_unicode_funcs_destroy (buffer->unicode);
126 buffer->unicode = unicode;
130 hb_buffer_get_unicode_funcs (hb_buffer_t *buffer)
132 return buffer->unicode;
136 hb_buffer_set_direction (hb_buffer_t *buffer,
137 hb_direction_t direction)
140 buffer->direction = direction;
144 hb_buffer_get_direction (hb_buffer_t *buffer)
146 return buffer->direction;
151 hb_buffer_clear (hb_buffer_t *buffer)
153 buffer->have_output = FALSE;
154 buffer->in_length = 0;
155 buffer->out_length = 0;
158 buffer->out_string = buffer->in_string;
159 buffer->max_lig_id = 0;
163 hb_buffer_ensure (hb_buffer_t *buffer, unsigned int size)
165 unsigned int new_allocated = buffer->allocated;
167 if (size > new_allocated)
169 while (size > new_allocated)
170 new_allocated += (new_allocated >> 1) + 8;
172 if (buffer->positions)
173 buffer->positions = realloc (buffer->positions, new_allocated * sizeof (buffer->positions[0]));
175 if (buffer->out_string != buffer->in_string)
177 buffer->in_string = realloc (buffer->in_string, new_allocated * sizeof (buffer->in_string[0]));
178 buffer->out_string = (hb_internal_glyph_info_t *) buffer->positions;
182 buffer->in_string = realloc (buffer->in_string, new_allocated * sizeof (buffer->in_string[0]));
183 buffer->out_string = buffer->in_string;
186 buffer->allocated = new_allocated;
191 hb_buffer_add_glyph (hb_buffer_t *buffer,
192 hb_codepoint_t codepoint,
194 unsigned int cluster)
196 hb_internal_glyph_info_t *glyph;
198 hb_buffer_ensure (buffer, buffer->in_length + 1);
200 glyph = &buffer->in_string[buffer->in_length];
201 glyph->codepoint = codepoint;
203 glyph->cluster = cluster;
204 glyph->component = 0;
206 glyph->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
212 /* HarfBuzz-Internal API */
215 _hb_buffer_clear_output (hb_buffer_t *buffer)
217 buffer->have_output = TRUE;
218 buffer->out_length = 0;
220 buffer->out_string = buffer->in_string;
224 hb_buffer_clear_positions (hb_buffer_t *buffer)
226 _hb_buffer_clear_output (buffer);
227 buffer->have_output = FALSE;
229 if (HB_UNLIKELY (!buffer->positions))
231 buffer->positions = calloc (buffer->allocated, sizeof (buffer->positions[0]));
235 memset (buffer->positions, 0, sizeof (buffer->positions[0]) * buffer->in_length);
239 _hb_buffer_swap (hb_buffer_t *buffer)
243 assert (buffer->have_output);
245 if (buffer->out_string != buffer->in_string)
247 hb_internal_glyph_info_t *tmp_string;
248 tmp_string = buffer->in_string;
249 buffer->in_string = buffer->out_string;
250 buffer->out_string = tmp_string;
251 buffer->positions = (hb_internal_glyph_position_t *) buffer->out_string;
254 tmp = buffer->in_length;
255 buffer->in_length = buffer->out_length;
256 buffer->out_length = tmp;
258 tmp = buffer->in_pos;
259 buffer->in_pos = buffer->out_pos;
260 buffer->out_pos = tmp;
263 /* The following function copies `num_out' elements from `glyph_data'
264 to `buffer->out_string', advancing the in array pointer in the structure
265 by `num_in' elements, and the out array pointer by `num_out' elements.
266 Finally, it sets the `length' field of `out' equal to
267 `pos' of the `out' structure.
269 If `component' is 0xFFFF, the component value from buffer->in_pos
270 will copied `num_out' times, otherwise `component' itself will
271 be used to fill the `component' fields.
273 If `lig_id' is 0xFFFF, the lig_id value from buffer->in_pos
274 will copied `num_out' times, otherwise `lig_id' itself will
275 be used to fill the `lig_id' fields.
277 The mask for all replacement glyphs are taken
278 from the glyph at position `buffer->in_pos'.
280 The cluster value for the glyph at position buffer->in_pos is used
281 for all replacement glyphs */
283 _hb_buffer_add_output_glyphs (hb_buffer_t *buffer,
285 unsigned int num_out,
286 const uint16_t *glyph_data_be,
287 unsigned short component,
288 unsigned short lig_id)
292 unsigned int cluster;
294 if (buffer->out_string != buffer->in_string ||
295 buffer->out_pos + num_out > buffer->in_pos + num_in)
297 hb_buffer_ensure_separate (buffer, buffer->out_pos + num_out);
300 mask = buffer->in_string[buffer->in_pos].mask;
301 cluster = buffer->in_string[buffer->in_pos].cluster;
302 if (component == 0xFFFF)
303 component = buffer->in_string[buffer->in_pos].component;
304 if (lig_id == 0xFFFF)
305 lig_id = buffer->in_string[buffer->in_pos].lig_id;
307 for (i = 0; i < num_out; i++)
309 hb_internal_glyph_info_t *info = &buffer->out_string[buffer->out_pos + i];
310 info->codepoint = hb_be_uint16 (glyph_data_be[i]);
312 info->cluster = cluster;
313 info->component = component;
314 info->lig_id = lig_id;
315 info->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
318 buffer->in_pos += num_in;
319 buffer->out_pos += num_out;
320 buffer->out_length = buffer->out_pos;
325 _hb_buffer_add_output_glyph (hb_buffer_t *buffer,
326 hb_codepoint_t glyph_index,
327 unsigned short component,
328 unsigned short lig_id)
330 hb_internal_glyph_info_t *info;
332 if (buffer->out_string != buffer->in_string)
334 hb_buffer_ensure (buffer, buffer->out_pos + 1);
335 buffer->out_string[buffer->out_pos] = buffer->in_string[buffer->in_pos];
337 else if (buffer->out_pos != buffer->in_pos)
338 buffer->out_string[buffer->out_pos] = buffer->in_string[buffer->in_pos];
340 info = &buffer->out_string[buffer->out_pos];
341 info->codepoint = glyph_index;
342 if (component != 0xFFFF)
343 info->component = component;
344 if (lig_id != 0xFFFF)
345 info->lig_id = lig_id;
346 info->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
350 buffer->out_length = buffer->out_pos;
354 _hb_buffer_next_glyph (hb_buffer_t *buffer)
356 if (!buffer->have_output)
362 if (buffer->out_string != buffer->in_string)
364 hb_buffer_ensure (buffer, buffer->out_pos + 1);
365 buffer->out_string[buffer->out_pos] = buffer->in_string[buffer->in_pos];
367 else if (buffer->out_pos != buffer->in_pos)
368 buffer->out_string[buffer->out_pos] = buffer->in_string[buffer->in_pos];
372 buffer->out_length = buffer->out_pos;
376 _hb_buffer_replace_glyph (hb_buffer_t *buffer,
377 hb_codepoint_t glyph_index)
379 _hb_buffer_add_output_glyph (buffer, glyph_index, 0xFFFF, 0xFFFF);
383 _hb_buffer_allocate_lig_id (hb_buffer_t *buffer)
385 return ++buffer->max_lig_id;
390 hb_buffer_get_len (hb_buffer_t *buffer)
392 return buffer->in_length;
395 /* Return value valid as long as buffer not modified */
397 hb_buffer_get_glyph_infos (hb_buffer_t *buffer)
399 return (hb_glyph_info_t *) buffer->in_string;
402 /* Return value valid as long as buffer not modified */
403 hb_glyph_position_t *
404 hb_buffer_get_glyph_positions (hb_buffer_t *buffer)
406 if (buffer->have_output || (buffer->in_length && !buffer->positions))
407 hb_buffer_clear_positions (buffer);
409 return (hb_glyph_position_t *) buffer->positions;
414 hb_buffer_reverse (hb_buffer_t *buffer)
418 for (i = 0, j = buffer->in_length - 1; i < buffer->in_length / 2; i++, j--) {
419 hb_internal_glyph_info_t t;
421 t = buffer->in_string[i];
422 buffer->in_string[i] = buffer->in_string[j];
423 buffer->in_string[j] = t;
426 if (buffer->positions) {
427 for (i = 0, j = buffer->in_length - 1; i < buffer->in_length / 2; i++, j--) {
428 hb_internal_glyph_position_t t;
430 t = buffer->positions[i];
431 buffer->positions[i] = buffer->positions[j];
432 buffer->positions[j] = t;
440 const T *next = (const T *) text + item_offset; \
441 const T *end = next + item_length; \
442 while (next < end) { \
444 const T *old_next = next; \
445 next = UTF_NEXT (next, end, u); \
446 hb_buffer_add_glyph (buffer, u, 0, old_next - (const T *) text); \
451 #define UTF8_COMPUTE(Char, Mask, Len) \
452 if (Char < 128) { Len = 1; Mask = 0x7f; } \
453 else if ((Char & 0xe0) == 0xc0) { Len = 2; Mask = 0x1f; } \
454 else if ((Char & 0xf0) == 0xe0) { Len = 3; Mask = 0x0f; } \
455 else if ((Char & 0xf8) == 0xf0) { Len = 4; Mask = 0x07; } \
458 static inline const uint8_t *
459 hb_utf8_next (const uint8_t *text,
461 hb_codepoint_t *unicode)
464 unsigned int mask, len;
466 UTF8_COMPUTE (c, mask, len);
467 if (HB_UNLIKELY (!len || (unsigned int) (end - text) < len)) {
471 hb_codepoint_t result;
474 for (i = 1; i < len; i++)
476 if (HB_UNLIKELY ((text[i] & 0xc0) != 0x80))
482 result |= (text[i] & 0x3f);
490 hb_buffer_add_utf8 (hb_buffer_t *buffer,
492 unsigned int text_length,
493 unsigned int item_offset,
494 unsigned int item_length)
496 #define UTF_NEXT(S, E, U) hb_utf8_next (S, E, &(U))
501 static inline const uint16_t *
502 hb_utf16_next (const uint16_t *text,
504 hb_codepoint_t *unicode)
506 uint16_t c = *text++;
508 if (HB_UNLIKELY (c >= 0xd800 && c < 0xdc00)) {
511 if (text < end && ((l = *text), HB_UNLIKELY (l >= 0xdc00 && l < 0xe000))) {
513 *unicode = ((hb_codepoint_t) ((c) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000);
524 hb_buffer_add_utf16 (hb_buffer_t *buffer,
525 const uint16_t *text,
526 unsigned int text_length,
527 unsigned int item_offset,
528 unsigned int item_length)
530 #define UTF_NEXT(S, E, U) hb_utf16_next (S, E, &(U))
536 hb_buffer_add_utf32 (hb_buffer_t *buffer,
537 const uint32_t *text,
538 unsigned int text_length,
539 unsigned int item_offset,
540 unsigned int item_length)
542 #define UTF_NEXT(S, E, U) ((U) = *(S), (S)+1)