Start ft glue
[framework/uifw/harfbuzz.git] / src / 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   &_hb_unicode_funcs_nil  /* unicode */
37 };
38
39 /* Here is how the buffer works internally:
40  *
41  * There are two string pointers: in_string and out_string.  They
42  * always have same allocated size, but different length and positions.
43  *
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
48  * in-place.
49  *
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.
55  */
56
57 /* XXX err handling */
58
59 /* Internal API */
60
61 static void
62 hb_buffer_ensure_separate (hb_buffer_t *buffer, unsigned int size)
63 {
64   hb_buffer_ensure (buffer, size);
65   if (buffer->out_string == buffer->in_string)
66   {
67     assert (buffer->have_output);
68     if (!buffer->positions)
69       buffer->positions = calloc (buffer->allocated, sizeof (buffer->positions[0]));
70
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]));
73   }
74 }
75
76 /* Public API */
77
78 hb_buffer_t *
79 hb_buffer_create (unsigned int pre_alloc_size)
80 {
81   hb_buffer_t *buffer;
82
83   if (!HB_OBJECT_DO_CREATE (hb_buffer_t, buffer))
84     return &_hb_buffer_nil;
85
86   if (pre_alloc_size)
87     hb_buffer_ensure(buffer, pre_alloc_size);
88
89   buffer->unicode = &_hb_unicode_funcs_nil;
90
91   return buffer;
92 }
93
94 hb_buffer_t *
95 hb_buffer_reference (hb_buffer_t *buffer)
96 {
97   HB_OBJECT_DO_REFERENCE (buffer);
98 }
99
100 unsigned int
101 hb_buffer_get_reference_count (hb_buffer_t *buffer)
102 {
103   HB_OBJECT_DO_GET_REFERENCE_COUNT (buffer);
104 }
105
106 void
107 hb_buffer_destroy (hb_buffer_t *buffer)
108 {
109   HB_OBJECT_DO_DESTROY (buffer);
110
111   hb_unicode_funcs_destroy (buffer->unicode);
112
113   free (buffer->in_string);
114   free (buffer->positions);
115
116   free (buffer);
117 }
118
119
120 void
121 hb_buffer_set_unicode_funcs (hb_buffer_t        *buffer,
122                              hb_unicode_funcs_t *unicode)
123 {
124   hb_unicode_funcs_reference (unicode);
125   hb_unicode_funcs_destroy (buffer->unicode);
126   buffer->unicode = unicode;
127 }
128
129 hb_unicode_funcs_t *
130 hb_buffer_get_unicode_funcs (hb_buffer_t        *buffer)
131 {
132   return buffer->unicode;
133 }
134
135 void
136 hb_buffer_set_direction (hb_buffer_t    *buffer,
137                          hb_direction_t  direction)
138
139 {
140   buffer->direction = direction;
141 }
142
143 hb_direction_t
144 hb_buffer_get_direction (hb_buffer_t    *buffer)
145 {
146   return buffer->direction;
147 }
148
149
150 void
151 hb_buffer_clear (hb_buffer_t *buffer)
152 {
153   buffer->have_output = FALSE;
154   buffer->in_length = 0;
155   buffer->out_length = 0;
156   buffer->in_pos = 0;
157   buffer->out_pos = 0;
158   buffer->out_string = buffer->in_string;
159   buffer->max_lig_id = 0;
160 }
161
162 void
163 hb_buffer_ensure (hb_buffer_t *buffer, unsigned int size)
164 {
165   unsigned int new_allocated = buffer->allocated;
166
167   if (size > new_allocated)
168   {
169     while (size > new_allocated)
170       new_allocated += (new_allocated >> 1) + 8;
171
172     if (buffer->positions)
173       buffer->positions = realloc (buffer->positions, new_allocated * sizeof (buffer->positions[0]));
174
175     if (buffer->out_string != buffer->in_string)
176     {
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;
179     }
180     else
181     {
182       buffer->in_string = realloc (buffer->in_string, new_allocated * sizeof (buffer->in_string[0]));
183       buffer->out_string = buffer->in_string;
184     }
185
186     buffer->allocated = new_allocated;
187   }
188 }
189
190 void
191 hb_buffer_add_glyph (hb_buffer_t    *buffer,
192                      hb_codepoint_t  codepoint,
193                      hb_mask_t       mask,
194                      unsigned int    cluster)
195 {
196   hb_internal_glyph_info_t *glyph;
197
198   hb_buffer_ensure (buffer, buffer->in_length + 1);
199
200   glyph = &buffer->in_string[buffer->in_length];
201   glyph->codepoint = codepoint;
202   glyph->mask = mask;
203   glyph->cluster = cluster;
204   glyph->component = 0;
205   glyph->lig_id = 0;
206   glyph->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
207
208   buffer->in_length++;
209 }
210
211
212 /* HarfBuzz-Internal API */
213
214 void
215 _hb_buffer_clear_output (hb_buffer_t *buffer)
216 {
217   buffer->have_output = TRUE;
218   buffer->out_length = 0;
219   buffer->out_pos = 0;
220   buffer->out_string = buffer->in_string;
221 }
222
223 void
224 hb_buffer_clear_positions (hb_buffer_t *buffer)
225 {
226   _hb_buffer_clear_output (buffer);
227   buffer->have_output = FALSE;
228
229   if (HB_UNLIKELY (!buffer->positions))
230   {
231     buffer->positions = calloc (buffer->allocated, sizeof (buffer->positions[0]));
232     return;
233   }
234
235   memset (buffer->positions, 0, sizeof (buffer->positions[0]) * buffer->in_length);
236 }
237
238 void
239 _hb_buffer_swap (hb_buffer_t *buffer)
240 {
241   unsigned int tmp;
242
243   assert (buffer->have_output);
244
245   if (buffer->out_string != buffer->in_string)
246   {
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;
252   }
253
254   tmp = buffer->in_length;
255   buffer->in_length = buffer->out_length;
256   buffer->out_length = tmp;
257
258   tmp = buffer->in_pos;
259   buffer->in_pos = buffer->out_pos;
260   buffer->out_pos = tmp;
261 }
262
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.
268
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.
272
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.
276
277    The mask for all replacement glyphs are taken
278    from the glyph at position `buffer->in_pos'.
279
280    The cluster value for the glyph at position buffer->in_pos is used
281    for all replacement glyphs */
282 void
283 _hb_buffer_add_output_glyphs (hb_buffer_t *buffer,
284                               unsigned int num_in,
285                               unsigned int num_out,
286                               const uint16_t *glyph_data_be,
287                               unsigned short component,
288                               unsigned short lig_id)
289 {
290   unsigned int i;
291   unsigned int mask;
292   unsigned int cluster;
293
294   if (buffer->out_string != buffer->in_string ||
295       buffer->out_pos + num_out > buffer->in_pos + num_in)
296   {
297     hb_buffer_ensure_separate (buffer, buffer->out_pos + num_out);
298   }
299
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;
306
307   for (i = 0; i < num_out; i++)
308   {
309     hb_internal_glyph_info_t *info = &buffer->out_string[buffer->out_pos + i];
310     info->codepoint = hb_be_uint16 (glyph_data_be[i]);
311     info->mask = mask;
312     info->cluster = cluster;
313     info->component = component;
314     info->lig_id = lig_id;
315     info->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
316   }
317
318   buffer->in_pos  += num_in;
319   buffer->out_pos += num_out;
320   buffer->out_length = buffer->out_pos;
321 }
322
323
324 void
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)
329 {
330   hb_internal_glyph_info_t *info;
331
332   if (buffer->out_string != buffer->in_string)
333   {
334     hb_buffer_ensure (buffer, buffer->out_pos + 1);
335     buffer->out_string[buffer->out_pos] = buffer->in_string[buffer->in_pos];
336   }
337   else if (buffer->out_pos != buffer->in_pos)
338     buffer->out_string[buffer->out_pos] = buffer->in_string[buffer->in_pos];
339
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;
347
348   buffer->in_pos++;
349   buffer->out_pos++;
350   buffer->out_length = buffer->out_pos;
351 }
352
353 void
354 _hb_buffer_next_glyph (hb_buffer_t *buffer)
355 {
356   if (!buffer->have_output)
357   {
358     buffer->in_pos++;
359     return;
360   }
361
362   if (buffer->out_string != buffer->in_string)
363   {
364     hb_buffer_ensure (buffer, buffer->out_pos + 1);
365     buffer->out_string[buffer->out_pos] = buffer->in_string[buffer->in_pos];
366   }
367   else if (buffer->out_pos != buffer->in_pos)
368     buffer->out_string[buffer->out_pos] = buffer->in_string[buffer->in_pos];
369
370   buffer->in_pos++;
371   buffer->out_pos++;
372   buffer->out_length = buffer->out_pos;
373 }
374
375 void
376 _hb_buffer_replace_glyph (hb_buffer_t *buffer,
377                           hb_codepoint_t glyph_index)
378 {
379   _hb_buffer_add_output_glyph (buffer, glyph_index, 0xFFFF, 0xFFFF);
380 }
381
382 unsigned short
383 _hb_buffer_allocate_lig_id (hb_buffer_t *buffer)
384 {
385   return ++buffer->max_lig_id;
386 }
387
388
389 unsigned int
390 hb_buffer_get_len (hb_buffer_t *buffer)
391 {
392   return buffer->in_length;
393 }
394
395 /* Return value valid as long as buffer not modified */
396 hb_glyph_info_t *
397 hb_buffer_get_glyph_infos (hb_buffer_t *buffer)
398 {
399   return (hb_glyph_info_t *) buffer->in_string;
400 }
401
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)
405 {
406   if (buffer->have_output || (buffer->in_length && !buffer->positions))
407     hb_buffer_clear_positions (buffer);
408
409   return (hb_glyph_position_t *) buffer->positions;
410 }
411
412
413 void
414 hb_buffer_reverse (hb_buffer_t *buffer)
415 {
416   unsigned int i, j;
417
418   for (i = 0, j = buffer->in_length - 1; i < buffer->in_length / 2; i++, j--) {
419     hb_internal_glyph_info_t t;
420
421     t = buffer->in_string[i];
422     buffer->in_string[i] = buffer->in_string[j];
423     buffer->in_string[j] = t;
424   }
425
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;
429
430       t = buffer->positions[i];
431       buffer->positions[i] = buffer->positions[j];
432       buffer->positions[j] = t;
433     }
434   }
435 }
436
437
438 #define ADD_UTF(T) \
439         HB_STMT_START { \
440           const T *next = (const T *) text + item_offset; \
441           const T *end = next + item_length; \
442           while (next < end) { \
443             hb_codepoint_t u; \
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); \
447           } \
448         } HB_STMT_END
449
450
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; } \
456   else Len = 0;
457
458 static inline const uint8_t *
459 hb_utf8_next (const uint8_t *text,
460               const uint8_t *end,
461               hb_codepoint_t *unicode)
462 {
463   uint8_t c = *text;
464   unsigned int mask, len;
465
466   UTF8_COMPUTE (c, mask, len);
467   if (HB_UNLIKELY (!len || (unsigned int) (end - text) < len)) {
468     *unicode = -1;
469     return text + 1;
470   } else {
471     hb_codepoint_t result;
472     unsigned int i;
473     result = c & mask;
474     for (i = 1; i < len; i++)
475       {
476         if (HB_UNLIKELY ((text[i] & 0xc0) != 0x80))
477           {
478             *unicode = -1;
479             return text + 1;
480           }
481         result <<= 6;
482         result |= (text[i] & 0x3f);
483       }
484     *unicode = result;
485     return text + len;
486   }
487 }
488
489 void
490 hb_buffer_add_utf8 (hb_buffer_t  *buffer,
491                     const char   *text,
492                     unsigned int  text_length,
493                     unsigned int  item_offset,
494                     unsigned int  item_length)
495 {
496 #define UTF_NEXT(S, E, U)       hb_utf8_next (S, E, &(U))
497   ADD_UTF (uint8_t);
498 #undef UTF_NEXT
499 }
500
501 static inline const uint16_t *
502 hb_utf16_next (const uint16_t *text,
503                const uint16_t *end,
504                hb_codepoint_t *unicode)
505 {
506   uint16_t c = *text++;
507
508   if (HB_UNLIKELY (c >= 0xd800 && c < 0xdc00)) {
509     /* high surrogate */
510     uint16_t l;
511     if (text < end && ((l = *text), HB_UNLIKELY (l >= 0xdc00 && l < 0xe000))) {
512       /* low surrogate */
513       *unicode = ((hb_codepoint_t) ((c) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000);
514        text++;
515     } else
516       *unicode = -1;
517   } else
518     *unicode = c;
519
520   return text;
521 }
522
523 void
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)
529 {
530 #define UTF_NEXT(S, E, U)       hb_utf16_next (S, E, &(U))
531   ADD_UTF (uint16_t);
532 #undef UTF_NEXT
533 }
534
535 void
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)
541 {
542 #define UTF_NEXT(S, E, U)       ((U) = *(S), (S)+1)
543   ADD_UTF (uint32_t);
544 #undef UTF_NEXT
545 }