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 */
37 /* Here is how the buffer works internally:
39 * There are two string pointers: in_string and out_string. They
40 * always have same allocated size, but different length and positions.
42 * As an optimization, both in_string and out_string may point to the
43 * same piece of memory, which is owned by in_string. This remains the
44 * case as long as out_length doesn't exceed in_length at any time.
45 * In that case, swap() is no-op and the glyph operations operate mostly
48 * As soon as out_string gets longer than in_string, out_string is moved over
49 * to an alternate buffer (which we reuse the positions buffer for!), and its
50 * current contents (out_length entries) are copied to the alt buffer.
51 * This should all remain transparent to the user. swap() then switches
52 * in_string and out_string.
55 /* XXX err handling */
60 hb_buffer_ensure_separate (hb_buffer_t *buffer, unsigned int size)
62 hb_buffer_ensure (buffer, size);
63 if (buffer->out_string == buffer->in_string)
65 assert (buffer->have_output);
66 if (!buffer->positions)
67 buffer->positions = calloc (buffer->allocated, sizeof (buffer->positions[0]));
69 buffer->out_string = (hb_internal_glyph_info_t *) buffer->positions;
70 memcpy (buffer->out_string, buffer->in_string, buffer->out_length * sizeof (buffer->out_string[0]));
77 hb_buffer_create (unsigned int pre_alloc_size)
81 if (!HB_OBJECT_DO_CREATE (hb_buffer_t, buffer))
82 return &_hb_buffer_nil;
85 hb_buffer_ensure(buffer, pre_alloc_size);
91 hb_buffer_reference (hb_buffer_t *buffer)
93 HB_OBJECT_DO_REFERENCE (buffer);
97 hb_buffer_get_reference_count (hb_buffer_t *buffer)
99 HB_OBJECT_DO_GET_REFERENCE_COUNT (buffer);
103 hb_buffer_destroy (hb_buffer_t *buffer)
105 HB_OBJECT_DO_DESTROY (buffer);
107 free (buffer->in_string);
108 free (buffer->positions);
114 hb_buffer_clear (hb_buffer_t *buffer)
116 buffer->have_output = FALSE;
117 buffer->in_length = 0;
118 buffer->out_length = 0;
121 buffer->out_string = buffer->in_string;
122 buffer->max_lig_id = 0;
126 hb_buffer_ensure (hb_buffer_t *buffer, unsigned int size)
128 unsigned int new_allocated = buffer->allocated;
130 if (size > new_allocated)
132 while (size > new_allocated)
133 new_allocated += (new_allocated >> 1) + 8;
135 if (buffer->positions)
136 buffer->positions = realloc (buffer->positions, new_allocated * sizeof (buffer->positions[0]));
138 if (buffer->out_string != buffer->in_string)
140 buffer->in_string = realloc (buffer->in_string, new_allocated * sizeof (buffer->in_string[0]));
141 buffer->out_string = (hb_internal_glyph_info_t *) buffer->positions;
145 buffer->in_string = realloc (buffer->in_string, new_allocated * sizeof (buffer->in_string[0]));
146 buffer->out_string = buffer->in_string;
149 buffer->allocated = new_allocated;
154 hb_buffer_add_glyph (hb_buffer_t *buffer,
155 hb_codepoint_t codepoint,
157 unsigned int cluster)
159 hb_internal_glyph_info_t *glyph;
161 hb_buffer_ensure (buffer, buffer->in_length + 1);
163 glyph = &buffer->in_string[buffer->in_length];
164 glyph->codepoint = codepoint;
166 glyph->cluster = cluster;
167 glyph->component = 0;
169 glyph->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
175 hb_buffer_set_direction (hb_buffer_t *buffer,
176 hb_direction_t direction)
179 buffer->direction = direction;
183 /* HarfBuzz-Internal API */
186 _hb_buffer_clear_output (hb_buffer_t *buffer)
188 buffer->have_output = TRUE;
189 buffer->out_length = 0;
191 buffer->out_string = buffer->in_string;
195 hb_buffer_clear_positions (hb_buffer_t *buffer)
197 _hb_buffer_clear_output (buffer);
198 buffer->have_output = FALSE;
200 if (HB_UNLIKELY (!buffer->positions))
202 buffer->positions = calloc (buffer->allocated, sizeof (buffer->positions[0]));
206 memset (buffer->positions, 0, sizeof (buffer->positions[0]) * buffer->in_length);
210 _hb_buffer_swap (hb_buffer_t *buffer)
214 assert (buffer->have_output);
216 if (buffer->out_string != buffer->in_string)
218 hb_internal_glyph_info_t *tmp_string;
219 tmp_string = buffer->in_string;
220 buffer->in_string = buffer->out_string;
221 buffer->out_string = tmp_string;
222 buffer->positions = (hb_internal_glyph_position_t *) buffer->out_string;
225 tmp = buffer->in_length;
226 buffer->in_length = buffer->out_length;
227 buffer->out_length = tmp;
229 tmp = buffer->in_pos;
230 buffer->in_pos = buffer->out_pos;
231 buffer->out_pos = tmp;
234 /* The following function copies `num_out' elements from `glyph_data'
235 to `buffer->out_string', advancing the in array pointer in the structure
236 by `num_in' elements, and the out array pointer by `num_out' elements.
237 Finally, it sets the `length' field of `out' equal to
238 `pos' of the `out' structure.
240 If `component' is 0xFFFF, the component value from buffer->in_pos
241 will copied `num_out' times, otherwise `component' itself will
242 be used to fill the `component' fields.
244 If `lig_id' is 0xFFFF, the lig_id value from buffer->in_pos
245 will copied `num_out' times, otherwise `lig_id' itself will
246 be used to fill the `lig_id' fields.
248 The mask for all replacement glyphs are taken
249 from the glyph at position `buffer->in_pos'.
251 The cluster value for the glyph at position buffer->in_pos is used
252 for all replacement glyphs */
254 _hb_buffer_add_output_glyphs (hb_buffer_t *buffer,
256 unsigned int num_out,
257 const uint16_t *glyph_data_be,
258 unsigned short component,
259 unsigned short lig_id)
263 unsigned int cluster;
265 if (buffer->out_string != buffer->in_string ||
266 buffer->out_pos + num_out > buffer->in_pos + num_in)
268 hb_buffer_ensure_separate (buffer, buffer->out_pos + num_out);
271 mask = buffer->in_string[buffer->in_pos].mask;
272 cluster = buffer->in_string[buffer->in_pos].cluster;
273 if (component == 0xFFFF)
274 component = buffer->in_string[buffer->in_pos].component;
275 if (lig_id == 0xFFFF)
276 lig_id = buffer->in_string[buffer->in_pos].lig_id;
278 for (i = 0; i < num_out; i++)
280 hb_internal_glyph_info_t *info = &buffer->out_string[buffer->out_pos + i];
281 info->codepoint = hb_be_uint16 (glyph_data_be[i]);
283 info->cluster = cluster;
284 info->component = component;
285 info->lig_id = lig_id;
286 info->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
289 buffer->in_pos += num_in;
290 buffer->out_pos += num_out;
291 buffer->out_length = buffer->out_pos;
296 _hb_buffer_add_output_glyph (hb_buffer_t *buffer,
297 hb_codepoint_t glyph_index,
298 unsigned short component,
299 unsigned short lig_id)
301 hb_internal_glyph_info_t *info;
303 if (buffer->out_string != buffer->in_string)
305 hb_buffer_ensure (buffer, buffer->out_pos + 1);
306 buffer->out_string[buffer->out_pos] = buffer->in_string[buffer->in_pos];
308 else if (buffer->out_pos != buffer->in_pos)
309 buffer->out_string[buffer->out_pos] = buffer->in_string[buffer->in_pos];
311 info = &buffer->out_string[buffer->out_pos];
312 info->codepoint = glyph_index;
313 if (component != 0xFFFF)
314 info->component = component;
315 if (lig_id != 0xFFFF)
316 info->lig_id = lig_id;
317 info->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
321 buffer->out_length = buffer->out_pos;
325 _hb_buffer_next_glyph (hb_buffer_t *buffer)
327 if (!buffer->have_output)
333 if (buffer->out_string != buffer->in_string)
335 hb_buffer_ensure (buffer, buffer->out_pos + 1);
336 buffer->out_string[buffer->out_pos] = buffer->in_string[buffer->in_pos];
338 else if (buffer->out_pos != buffer->in_pos)
339 buffer->out_string[buffer->out_pos] = buffer->in_string[buffer->in_pos];
343 buffer->out_length = buffer->out_pos;
347 _hb_buffer_replace_glyph (hb_buffer_t *buffer,
348 hb_codepoint_t glyph_index)
350 _hb_buffer_add_output_glyph (buffer, glyph_index, 0xFFFF, 0xFFFF);
354 _hb_buffer_allocate_lig_id (hb_buffer_t *buffer)
356 return ++buffer->max_lig_id;
361 hb_buffer_get_len (hb_buffer_t *buffer)
363 return buffer->in_length;
366 /* Return value valid as long as buffer not modified */
368 hb_buffer_get_glyph_infos (hb_buffer_t *buffer)
370 return (hb_glyph_info_t *) buffer->in_string;
373 /* Return value valid as long as buffer not modified */
374 hb_glyph_position_t *
375 hb_buffer_get_glyph_positions (hb_buffer_t *buffer)
377 if (buffer->have_output || (buffer->in_length && !buffer->positions))
378 hb_buffer_clear_positions (buffer);
380 return (hb_glyph_position_t *) buffer->positions;
385 hb_buffer_reverse (hb_buffer_t *buffer)
389 for (i = 0, j = buffer->in_length - 1; i < buffer->in_length / 2; i++, j--) {
390 hb_internal_glyph_info_t t;
392 t = buffer->in_string[i];
393 buffer->in_string[i] = buffer->in_string[j];
394 buffer->in_string[j] = t;
397 if (buffer->positions) {
398 for (i = 0, j = buffer->in_length - 1; i < buffer->in_length / 2; i++, j--) {
399 hb_internal_glyph_position_t t;
401 t = buffer->positions[i];
402 buffer->positions[i] = buffer->positions[j];
403 buffer->positions[j] = t;
411 const T *next = (const T *) text + item_offset; \
412 const T *end = next + item_length; \
413 while (next < end) { \
415 const T *old_next = next; \
416 next = UTF_NEXT (next, end, u); \
417 hb_buffer_add_glyph (buffer, u, 0, old_next - (const T *) text); \
422 #define UTF8_COMPUTE(Char, Mask, Len) \
423 if (Char < 128) { Len = 1; Mask = 0x7f; } \
424 else if ((Char & 0xe0) == 0xc0) { Len = 2; Mask = 0x1f; } \
425 else if ((Char & 0xf0) == 0xe0) { Len = 3; Mask = 0x0f; } \
426 else if ((Char & 0xf8) == 0xf0) { Len = 4; Mask = 0x07; } \
429 static inline const uint8_t *
430 hb_utf8_next (const uint8_t *text,
432 hb_codepoint_t *unicode)
435 unsigned int mask, len;
437 UTF8_COMPUTE (c, mask, len);
438 if (HB_UNLIKELY (!len || (unsigned int) (end - text) < len)) {
442 hb_codepoint_t result;
445 for (i = 1; i < len; i++)
447 if (HB_UNLIKELY ((text[i] & 0xc0) != 0x80))
453 result |= (text[i] & 0x3f);
461 hb_buffer_add_utf8 (hb_buffer_t *buffer,
463 unsigned int text_length,
464 unsigned int item_offset,
465 unsigned int item_length)
467 #define UTF_NEXT(S, E, U) hb_utf8_next (S, E, &(U))
472 static inline const uint16_t *
473 hb_utf16_next (const uint16_t *text,
475 hb_codepoint_t *unicode)
477 uint16_t c = *text++;
479 if (HB_UNLIKELY (c >= 0xd800 && c < 0xdc00)) {
482 if (text < end && ((l = *text), HB_UNLIKELY (l >= 0xdc00 && l < 0xe000))) {
484 *unicode = ((hb_codepoint_t) ((c) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000);
495 hb_buffer_add_utf16 (hb_buffer_t *buffer,
496 const uint16_t *text,
497 unsigned int text_length,
498 unsigned int item_offset,
499 unsigned int item_length)
501 #define UTF_NEXT(S, E, U) hb_utf16_next (S, E, &(U))
507 hb_buffer_add_utf32 (hb_buffer_t *buffer,
508 const uint32_t *text,
509 unsigned int text_length,
510 unsigned int item_offset,
511 unsigned int item_length)
513 #define UTF_NEXT(S, E, U) ((U) = *(S), (S)+1)