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 string pointers: info and out_info. They
42 * always have same allocated size, but different length and positions.
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 mostly
50 * As soon as out_info gets longer than info, out_info is moved over
51 * to an alternate buffer (which we reuse the positions buffer for!), and its
52 * current contents (out_len entries) are copied to the alt buffer.
53 * This should all remain transparent to the user. swap() then switches
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_info == buffer->info)
67 assert (buffer->have_output);
69 buffer->pos = (hb_internal_glyph_position_t *) calloc (buffer->allocated, sizeof (buffer->pos[0]));
71 buffer->out_info = (hb_internal_glyph_info_t *) buffer->pos;
72 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;
187 buffer->out_info = buffer->info;
188 buffer->max_lig_id = 0;
192 hb_buffer_ensure (hb_buffer_t *buffer, unsigned int size)
194 unsigned int new_allocated = buffer->allocated;
196 if (size > new_allocated)
198 while (size > new_allocated)
199 new_allocated += (new_allocated >> 1) + 8;
202 buffer->pos = (hb_internal_glyph_position_t *) realloc (buffer->pos, new_allocated * sizeof (buffer->pos[0]));
204 if (buffer->out_info != buffer->info)
206 buffer->info = (hb_internal_glyph_info_t *) realloc (buffer->info, new_allocated * sizeof (buffer->info[0]));
207 buffer->out_info = (hb_internal_glyph_info_t *) buffer->pos;
211 buffer->info = (hb_internal_glyph_info_t *) realloc (buffer->info, new_allocated * sizeof (buffer->info[0]));
212 buffer->out_info = buffer->info;
215 buffer->allocated = new_allocated;
220 hb_buffer_add_glyph (hb_buffer_t *buffer,
221 hb_codepoint_t codepoint,
223 unsigned int cluster)
225 hb_internal_glyph_info_t *glyph;
227 hb_buffer_ensure (buffer, buffer->len + 1);
229 glyph = &buffer->info[buffer->len];
230 glyph->codepoint = codepoint;
232 glyph->cluster = cluster;
233 glyph->component = 0;
235 glyph->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
241 /* HarfBuzz-Internal API */
244 _hb_buffer_clear_output (hb_buffer_t *buffer)
246 buffer->have_output = TRUE;
247 buffer->have_positions = FALSE;
249 buffer->out_info = buffer->info;
253 hb_buffer_clear_positions (hb_buffer_t *buffer)
255 _hb_buffer_clear_output (buffer);
256 buffer->have_output = FALSE;
257 buffer->have_positions = TRUE;
259 if (unlikely (!buffer->pos))
261 buffer->pos = (hb_internal_glyph_position_t *) calloc (buffer->allocated, sizeof (buffer->pos[0]));
265 memset (buffer->pos, 0, sizeof (buffer->pos[0]) * buffer->len);
269 _hb_buffer_swap (hb_buffer_t *buffer)
273 assert (buffer->have_output);
275 if (buffer->out_info != buffer->info)
277 hb_internal_glyph_info_t *tmp_string;
278 tmp_string = buffer->info;
279 buffer->info = buffer->out_info;
280 buffer->out_info = tmp_string;
281 buffer->pos = (hb_internal_glyph_position_t *) buffer->out_info;
285 buffer->len = buffer->out_len;
286 buffer->out_len = tmp;
291 /* The following function copies `num_out' elements from `glyph_data'
292 to `buffer->out_info', advancing the in array pointer in the structure
293 by `num_in' elements, and the out array pointer by `num_out' elements.
294 Finally, it sets the `length' field of `out' equal to
295 `pos' of the `out' structure.
297 If `component' is 0xFFFF, the component value from buffer->i
298 will copied `num_out' times, otherwise `component' itself will
299 be used to fill the `component' fields.
301 If `lig_id' is 0xFFFF, the lig_id value from buffer->i
302 will copied `num_out' times, otherwise `lig_id' itself will
303 be used to fill the `lig_id' fields.
305 The mask for all replacement glyphs are taken
306 from the glyph at position `buffer->i'.
308 The cluster value for the glyph at position buffer->i is used
309 for all replacement glyphs */
312 _hb_buffer_add_output_glyphs (hb_buffer_t *buffer,
314 unsigned int num_out,
315 const hb_codepoint_t *glyph_data,
316 unsigned short component,
317 unsigned short lig_id)
321 unsigned int cluster;
323 if (buffer->out_info != buffer->info ||
324 buffer->out_len + num_out > buffer->i + num_in)
326 hb_buffer_ensure_separate (buffer, buffer->out_len + num_out);
329 mask = buffer->info[buffer->i].mask;
330 cluster = buffer->info[buffer->i].cluster;
331 if (component == 0xFFFF)
332 component = buffer->info[buffer->i].component;
333 if (lig_id == 0xFFFF)
334 lig_id = buffer->info[buffer->i].lig_id;
336 for (i = 0; i < num_out; i++)
338 hb_internal_glyph_info_t *info = &buffer->out_info[buffer->out_len + i];
339 info->codepoint = glyph_data[i];
341 info->cluster = cluster;
342 info->component = component;
343 info->lig_id = lig_id;
344 info->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
348 buffer->out_len += num_out;
352 _hb_buffer_add_output_glyphs_be16 (hb_buffer_t *buffer,
354 unsigned int num_out,
355 const uint16_t *glyph_data_be,
356 unsigned short component,
357 unsigned short lig_id)
361 unsigned int cluster;
363 if (buffer->out_info != buffer->info ||
364 buffer->out_len + num_out > buffer->i + num_in)
366 hb_buffer_ensure_separate (buffer, buffer->out_len + num_out);
369 mask = buffer->info[buffer->i].mask;
370 cluster = buffer->info[buffer->i].cluster;
371 if (component == 0xFFFF)
372 component = buffer->info[buffer->i].component;
373 if (lig_id == 0xFFFF)
374 lig_id = buffer->info[buffer->i].lig_id;
376 for (i = 0; i < num_out; i++)
378 hb_internal_glyph_info_t *info = &buffer->out_info[buffer->out_len + i];
379 info->codepoint = hb_be_uint16 (glyph_data_be[i]);
381 info->cluster = cluster;
382 info->component = component;
383 info->lig_id = lig_id;
384 info->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
388 buffer->out_len += num_out;
392 _hb_buffer_add_output_glyph (hb_buffer_t *buffer,
393 hb_codepoint_t glyph_index,
394 unsigned short component,
395 unsigned short lig_id)
397 hb_internal_glyph_info_t *info;
399 if (buffer->out_info != buffer->info)
401 hb_buffer_ensure (buffer, buffer->out_len + 1);
402 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
404 else if (buffer->out_len != buffer->i)
405 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
407 info = &buffer->out_info[buffer->out_len];
408 info->codepoint = glyph_index;
409 if (component != 0xFFFF)
410 info->component = component;
411 if (lig_id != 0xFFFF)
412 info->lig_id = lig_id;
413 info->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
420 _hb_buffer_next_glyph (hb_buffer_t *buffer)
422 if (buffer->have_output)
424 if (buffer->out_info != buffer->info)
426 hb_buffer_ensure (buffer, buffer->out_len + 1);
427 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
429 else if (buffer->out_len != buffer->i)
430 buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
440 hb_buffer_get_length (hb_buffer_t *buffer)
445 /* Return value valid as long as buffer not modified */
447 hb_buffer_get_glyph_infos (hb_buffer_t *buffer)
449 return (hb_glyph_info_t *) buffer->info;
452 /* Return value valid as long as buffer not modified */
453 hb_glyph_position_t *
454 hb_buffer_get_glyph_positions (hb_buffer_t *buffer)
456 if (!buffer->have_positions)
457 hb_buffer_clear_positions (buffer);
459 return (hb_glyph_position_t *) buffer->pos;
464 reverse_range (hb_buffer_t *buffer,
470 for (i = start, j = end - 1; i < j; i++, j--) {
471 hb_internal_glyph_info_t t;
474 buffer->info[i] = buffer->info[j];
479 for (i = 0, j = end - 1; i < j; i++, j--) {
480 hb_internal_glyph_position_t t;
483 buffer->pos[i] = buffer->pos[j];
490 hb_buffer_reverse (hb_buffer_t *buffer)
492 if (unlikely (!buffer->len))
495 reverse_range (buffer, 0, buffer->len);
499 hb_buffer_reverse_clusters (hb_buffer_t *buffer)
501 unsigned int i, start, count, last_cluster;
503 if (unlikely (!buffer->len))
506 hb_buffer_reverse (buffer);
510 last_cluster = buffer->info[0].cluster;
511 for (i = 1; i < count; i++) {
512 if (last_cluster != buffer->info[i].cluster) {
513 reverse_range (buffer, start, i);
515 last_cluster = buffer->info[i].cluster;
518 reverse_range (buffer, start, i);
524 const T *next = (const T *) text + item_offset; \
525 const T *end = next + item_length; \
526 while (next < end) { \
528 const T *old_next = next; \
529 next = UTF_NEXT (next, end, u); \
530 hb_buffer_add_glyph (buffer, u, 0, old_next - (const T *) text); \
535 #define UTF8_COMPUTE(Char, Mask, Len) \
536 if (Char < 128) { Len = 1; Mask = 0x7f; } \
537 else if ((Char & 0xe0) == 0xc0) { Len = 2; Mask = 0x1f; } \
538 else if ((Char & 0xf0) == 0xe0) { Len = 3; Mask = 0x0f; } \
539 else if ((Char & 0xf8) == 0xf0) { Len = 4; Mask = 0x07; } \
542 static inline const uint8_t *
543 hb_utf8_next (const uint8_t *text,
545 hb_codepoint_t *unicode)
548 unsigned int mask, len;
550 UTF8_COMPUTE (c, mask, len);
551 if (unlikely (!len || (unsigned int) (end - text) < len)) {
555 hb_codepoint_t result;
558 for (i = 1; i < len; i++)
560 if (unlikely ((text[i] & 0xc0) != 0x80))
566 result |= (text[i] & 0x3f);
574 hb_buffer_add_utf8 (hb_buffer_t *buffer,
576 unsigned int text_length HB_UNUSED,
577 unsigned int item_offset,
578 unsigned int item_length)
580 #define UTF_NEXT(S, E, U) hb_utf8_next (S, E, &(U))
585 static inline const uint16_t *
586 hb_utf16_next (const uint16_t *text,
588 hb_codepoint_t *unicode)
590 uint16_t c = *text++;
592 if (unlikely (c >= 0xd800 && c < 0xdc00)) {
595 if (text < end && ((l = *text), unlikely (l >= 0xdc00 && l < 0xe000))) {
597 *unicode = ((hb_codepoint_t) ((c) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000);
608 hb_buffer_add_utf16 (hb_buffer_t *buffer,
609 const uint16_t *text,
610 unsigned int text_length HB_UNUSED,
611 unsigned int item_offset,
612 unsigned int item_length)
614 #define UTF_NEXT(S, E, U) hb_utf16_next (S, E, &(U))
620 hb_buffer_add_utf32 (hb_buffer_t *buffer,
621 const uint32_t *text,
622 unsigned int text_length HB_UNUSED,
623 unsigned int item_offset,
624 unsigned int item_length)
626 #define UTF_NEXT(S, E, U) ((U) = *(S), (S)+1)