Git init
[external/pango1.0.git] / pango / opentype / hb-buffer.c
1 /*
2  * Copyright (C) 1998-2004  David Turner and Werner Lemberg
3  * Copyright (C) 2004,2007  Red Hat, Inc.
4  *
5  * This is part of HarfBuzz, an OpenType Layout engine library.
6  *
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.
12  *
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
17  * DAMAGE.
18  *
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.
24  *
25  * Red Hat Author(s): Owen Taylor, Behdad Esfahbod
26  */
27
28 #include "hb-buffer-private.h"
29
30 #include <string.h>
31
32
33 static hb_buffer_t _hb_buffer_nil = {
34   HB_REFERENCE_COUNT_INVALID /* ref_count */
35 };
36
37 /* Here is how the buffer works internally:
38  *
39  * There are two string pointers: in_string and out_string.  They
40  * always have same allocated size, but different length and positions.
41  *
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
46  * in-place.
47  *
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.
53  */
54
55 /* XXX err handling */
56
57 /* Internal API */
58
59 static void
60 hb_buffer_ensure_separate (hb_buffer_t *buffer, unsigned int size)
61 {
62   hb_buffer_ensure (buffer, size);
63   if (buffer->out_string == buffer->in_string)
64   {
65     assert (buffer->have_output);
66     if (!buffer->positions)
67       buffer->positions = calloc (buffer->allocated, sizeof (buffer->positions[0]));
68
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]));
71   }
72 }
73
74 /* Public API */
75
76 hb_buffer_t *
77 hb_buffer_create (unsigned int pre_alloc_size)
78 {
79   hb_buffer_t *buffer;
80
81   if (!HB_OBJECT_DO_CREATE (hb_buffer_t, buffer))
82     return &_hb_buffer_nil;
83
84   if (pre_alloc_size)
85     hb_buffer_ensure(buffer, pre_alloc_size);
86
87   return buffer;
88 }
89
90 hb_buffer_t *
91 hb_buffer_reference (hb_buffer_t *buffer)
92 {
93   HB_OBJECT_DO_REFERENCE (buffer);
94 }
95
96 unsigned int
97 hb_buffer_get_reference_count (hb_buffer_t *buffer)
98 {
99   HB_OBJECT_DO_GET_REFERENCE_COUNT (buffer);
100 }
101
102 void
103 hb_buffer_destroy (hb_buffer_t *buffer)
104 {
105   HB_OBJECT_DO_DESTROY (buffer);
106
107   free (buffer->in_string);
108   free (buffer->positions);
109
110   free (buffer);
111 }
112
113 void
114 hb_buffer_clear (hb_buffer_t *buffer)
115 {
116   buffer->have_output = FALSE;
117   buffer->in_length = 0;
118   buffer->out_length = 0;
119   buffer->in_pos = 0;
120   buffer->out_pos = 0;
121   buffer->out_string = buffer->in_string;
122   buffer->max_lig_id = 0;
123 }
124
125 void
126 hb_buffer_ensure (hb_buffer_t *buffer, unsigned int size)
127 {
128   unsigned int new_allocated = buffer->allocated;
129
130   if (size > new_allocated)
131   {
132     while (size > new_allocated)
133       new_allocated += (new_allocated >> 1) + 8;
134
135     if (buffer->positions)
136       buffer->positions = realloc (buffer->positions, new_allocated * sizeof (buffer->positions[0]));
137
138     if (buffer->out_string != buffer->in_string)
139     {
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;
142     }
143     else
144     {
145       buffer->in_string = realloc (buffer->in_string, new_allocated * sizeof (buffer->in_string[0]));
146       buffer->out_string = buffer->in_string;
147     }
148
149     buffer->allocated = new_allocated;
150   }
151 }
152
153 void
154 hb_buffer_add_glyph (hb_buffer_t    *buffer,
155                      hb_codepoint_t  codepoint,
156                      hb_mask_t       mask,
157                      unsigned int    cluster)
158 {
159   hb_internal_glyph_info_t *glyph;
160
161   hb_buffer_ensure (buffer, buffer->in_length + 1);
162
163   glyph = &buffer->in_string[buffer->in_length];
164   glyph->codepoint = codepoint;
165   glyph->mask = mask;
166   glyph->cluster = cluster;
167   glyph->component = 0;
168   glyph->lig_id = 0;
169   glyph->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
170
171   buffer->in_length++;
172 }
173
174 void
175 hb_buffer_set_direction (hb_buffer_t    *buffer,
176                          hb_direction_t  direction)
177
178 {
179   buffer->direction = direction;
180 }
181
182
183 /* HarfBuzz-Internal API */
184
185 void
186 _hb_buffer_clear_output (hb_buffer_t *buffer)
187 {
188   buffer->have_output = TRUE;
189   buffer->out_length = 0;
190   buffer->out_pos = 0;
191   buffer->out_string = buffer->in_string;
192 }
193
194 void
195 hb_buffer_clear_positions (hb_buffer_t *buffer)
196 {
197   _hb_buffer_clear_output (buffer);
198   buffer->have_output = FALSE;
199
200   if (HB_UNLIKELY (!buffer->positions))
201   {
202     buffer->positions = calloc (buffer->allocated, sizeof (buffer->positions[0]));
203     return;
204   }
205
206   memset (buffer->positions, 0, sizeof (buffer->positions[0]) * buffer->in_length);
207 }
208
209 void
210 _hb_buffer_swap (hb_buffer_t *buffer)
211 {
212   unsigned int tmp;
213
214   assert (buffer->have_output);
215
216   if (buffer->out_string != buffer->in_string)
217   {
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;
223   }
224
225   tmp = buffer->in_length;
226   buffer->in_length = buffer->out_length;
227   buffer->out_length = tmp;
228
229   tmp = buffer->in_pos;
230   buffer->in_pos = buffer->out_pos;
231   buffer->out_pos = tmp;
232 }
233
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.
239
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.
243
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.
247
248    The mask for all replacement glyphs are taken
249    from the glyph at position `buffer->in_pos'.
250
251    The cluster value for the glyph at position buffer->in_pos is used
252    for all replacement glyphs */
253 void
254 _hb_buffer_add_output_glyphs (hb_buffer_t *buffer,
255                               unsigned int num_in,
256                               unsigned int num_out,
257                               const uint16_t *glyph_data_be,
258                               unsigned short component,
259                               unsigned short lig_id)
260 {
261   unsigned int i;
262   unsigned int mask;
263   unsigned int cluster;
264
265   if (buffer->out_string != buffer->in_string ||
266       buffer->out_pos + num_out > buffer->in_pos + num_in)
267   {
268     hb_buffer_ensure_separate (buffer, buffer->out_pos + num_out);
269   }
270
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;
277
278   for (i = 0; i < num_out; i++)
279   {
280     hb_internal_glyph_info_t *info = &buffer->out_string[buffer->out_pos + i];
281     info->codepoint = hb_be_uint16 (glyph_data_be[i]);
282     info->mask = mask;
283     info->cluster = cluster;
284     info->component = component;
285     info->lig_id = lig_id;
286     info->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
287   }
288
289   buffer->in_pos  += num_in;
290   buffer->out_pos += num_out;
291   buffer->out_length = buffer->out_pos;
292 }
293
294
295 void
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)
300 {
301   hb_internal_glyph_info_t *info;
302
303   if (buffer->out_string != buffer->in_string)
304   {
305     hb_buffer_ensure (buffer, buffer->out_pos + 1);
306     buffer->out_string[buffer->out_pos] = buffer->in_string[buffer->in_pos];
307   }
308   else if (buffer->out_pos != buffer->in_pos)
309     buffer->out_string[buffer->out_pos] = buffer->in_string[buffer->in_pos];
310
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;
318
319   buffer->in_pos++;
320   buffer->out_pos++;
321   buffer->out_length = buffer->out_pos;
322 }
323
324 void
325 _hb_buffer_next_glyph (hb_buffer_t *buffer)
326 {
327   if (!buffer->have_output)
328   {
329     buffer->in_pos++;
330     return;
331   }
332
333   if (buffer->out_string != buffer->in_string)
334   {
335     hb_buffer_ensure (buffer, buffer->out_pos + 1);
336     buffer->out_string[buffer->out_pos] = buffer->in_string[buffer->in_pos];
337   }
338   else if (buffer->out_pos != buffer->in_pos)
339     buffer->out_string[buffer->out_pos] = buffer->in_string[buffer->in_pos];
340
341   buffer->in_pos++;
342   buffer->out_pos++;
343   buffer->out_length = buffer->out_pos;
344 }
345
346 void
347 _hb_buffer_replace_glyph (hb_buffer_t *buffer,
348                           hb_codepoint_t glyph_index)
349 {
350   _hb_buffer_add_output_glyph (buffer, glyph_index, 0xFFFF, 0xFFFF);
351 }
352
353 unsigned short
354 _hb_buffer_allocate_lig_id (hb_buffer_t *buffer)
355 {
356   return ++buffer->max_lig_id;
357 }
358
359
360 unsigned int
361 hb_buffer_get_len (hb_buffer_t *buffer)
362 {
363   return buffer->in_length;
364 }
365
366 /* Return value valid as long as buffer not modified */
367 hb_glyph_info_t *
368 hb_buffer_get_glyph_infos (hb_buffer_t *buffer)
369 {
370   return (hb_glyph_info_t *) buffer->in_string;
371 }
372
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)
376 {
377   if (buffer->have_output || (buffer->in_length && !buffer->positions))
378     hb_buffer_clear_positions (buffer);
379
380   return (hb_glyph_position_t *) buffer->positions;
381 }
382
383
384 void
385 hb_buffer_reverse (hb_buffer_t *buffer)
386 {
387   unsigned int i, j;
388
389   for (i = 0, j = buffer->in_length - 1; i < buffer->in_length / 2; i++, j--) {
390     hb_internal_glyph_info_t t;
391
392     t = buffer->in_string[i];
393     buffer->in_string[i] = buffer->in_string[j];
394     buffer->in_string[j] = t;
395   }
396
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;
400
401       t = buffer->positions[i];
402       buffer->positions[i] = buffer->positions[j];
403       buffer->positions[j] = t;
404     }
405   }
406 }
407
408
409 #define ADD_UTF(T) \
410         HB_STMT_START { \
411           const T *next = (const T *) text + item_offset; \
412           const T *end = next + item_length; \
413           while (next < end) { \
414             hb_codepoint_t u; \
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); \
418           } \
419         } HB_STMT_END
420
421
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; } \
427   else Len = 0;
428
429 static inline const uint8_t *
430 hb_utf8_next (const uint8_t *text,
431               const uint8_t *end,
432               hb_codepoint_t *unicode)
433 {
434   uint8_t c = *text;
435   unsigned int mask, len;
436
437   UTF8_COMPUTE (c, mask, len);
438   if (HB_UNLIKELY (!len || (unsigned int) (end - text) < len)) {
439     *unicode = -1;
440     return text + 1;
441   } else {
442     hb_codepoint_t result;
443     unsigned int i;
444     result = c & mask;
445     for (i = 1; i < len; i++)
446       {
447         if (HB_UNLIKELY ((text[i] & 0xc0) != 0x80))
448           {
449             *unicode = -1;
450             return text + 1;
451           }
452         result <<= 6;
453         result |= (text[i] & 0x3f);
454       }
455     *unicode = result;
456     return text + len;
457   }
458 }
459
460 void
461 hb_buffer_add_utf8 (hb_buffer_t  *buffer,
462                     const char   *text,
463                     unsigned int  text_length,
464                     unsigned int  item_offset,
465                     unsigned int  item_length)
466 {
467 #define UTF_NEXT(S, E, U)       hb_utf8_next (S, E, &(U))
468   ADD_UTF (uint8_t);
469 #undef UTF_NEXT
470 }
471
472 static inline const uint16_t *
473 hb_utf16_next (const uint16_t *text,
474                const uint16_t *end,
475                hb_codepoint_t *unicode)
476 {
477   uint16_t c = *text++;
478
479   if (HB_UNLIKELY (c >= 0xd800 && c < 0xdc00)) {
480     /* high surrogate */
481     uint16_t l;
482     if (text < end && ((l = *text), HB_UNLIKELY (l >= 0xdc00 && l < 0xe000))) {
483       /* low surrogate */
484       *unicode = ((hb_codepoint_t) ((c) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000);
485        text++;
486     } else
487       *unicode = -1;
488   } else
489     *unicode = c;
490
491   return text;
492 }
493
494 void
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)
500 {
501 #define UTF_NEXT(S, E, U)       hb_utf16_next (S, E, &(U))
502   ADD_UTF (uint16_t);
503 #undef UTF_NEXT
504 }
505
506 void
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)
512 {
513 #define UTF_NEXT(S, E, U)       ((U) = *(S), (S)+1)
514   ADD_UTF (uint32_t);
515 #undef UTF_NEXT
516 }