Handle malloc failture in the buffer
[framework/uifw/harfbuzz.git] / src / hb-buffer.cc
1 /*
2  * Copyright (C) 1998-2004  David Turner and Werner Lemberg
3  * Copyright (C) 2004,2007,2009,2010  Red Hat, Inc.
4  *
5  * This is part of HarfBuzz, a text shaping 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.hh"
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 info pointers: info and out_info.  They always have
42  * the same allocated size, but different lengths.
43  *
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
48  * mostly in-place.
49  *
50  * As soon as out_info gets longer than info, out_info is moved over
51  * to an alternate buffer (which we reuse the pos buffer for!), and its
52  * current contents (out_len entries) are copied to the new place.
53  * This should all remain transparent to the user.  swap() then
54  * switches info and out_info.
55  */
56
57
58 /* Internal API */
59
60 static hb_bool_t
61 hb_buffer_ensure_separate (hb_buffer_t *buffer, unsigned int size)
62 {
63   if (unlikely (!hb_buffer_ensure (buffer, size))) return FALSE;
64
65   if (buffer->out_info == buffer->info)
66   {
67     assert (buffer->have_output);
68
69     buffer->out_info = (hb_internal_glyph_info_t *) buffer->pos;
70     memcpy (buffer->out_info, buffer->info, buffer->out_len * sizeof (buffer->out_info[0]));
71   }
72
73   return TRUE;
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->info);
114   free (buffer->pos);
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   if (!unicode)
125     unicode = &_hb_unicode_funcs_nil;
126
127   hb_unicode_funcs_reference (unicode);
128   hb_unicode_funcs_destroy (buffer->unicode);
129   buffer->unicode = unicode;
130 }
131
132 hb_unicode_funcs_t *
133 hb_buffer_get_unicode_funcs (hb_buffer_t        *buffer)
134 {
135   return buffer->unicode;
136 }
137
138 void
139 hb_buffer_set_direction (hb_buffer_t    *buffer,
140                          hb_direction_t  direction)
141
142 {
143   buffer->direction = direction;
144 }
145
146 hb_direction_t
147 hb_buffer_get_direction (hb_buffer_t    *buffer)
148 {
149   return buffer->direction;
150 }
151
152 void
153 hb_buffer_set_script (hb_buffer_t *buffer,
154                       hb_script_t  script)
155 {
156   buffer->script = script;
157 }
158
159 hb_script_t
160 hb_buffer_get_script (hb_buffer_t *buffer)
161 {
162   return buffer->script;
163 }
164
165 void
166 hb_buffer_set_language (hb_buffer_t   *buffer,
167                         hb_language_t  language)
168 {
169   buffer->language = language;
170 }
171
172 hb_language_t
173 hb_buffer_get_language (hb_buffer_t *buffer)
174 {
175   return buffer->language;
176 }
177
178
179 void
180 hb_buffer_clear (hb_buffer_t *buffer)
181 {
182   buffer->have_output = FALSE;
183   buffer->have_positions = FALSE;
184   buffer->in_error = FALSE;
185   buffer->len = 0;
186   buffer->out_len = 0;
187   buffer->i = 0;
188   buffer->out_info = buffer->info;
189   buffer->max_lig_id = 0;
190 }
191
192 hb_bool_t
193 hb_buffer_ensure (hb_buffer_t *buffer, unsigned int size)
194 {
195   if (unlikely (size > buffer->allocated))
196   {
197     if (unlikely (buffer->in_error))
198       return FALSE;
199
200     unsigned int new_allocated = buffer->allocated;
201     hb_internal_glyph_position_t *new_pos;
202     hb_internal_glyph_info_t *new_info;
203     bool separate_out;
204
205     separate_out = buffer->out_info != buffer->info;
206
207     while (size > new_allocated)
208       new_allocated += (new_allocated >> 1) + 8;
209
210     new_pos = (hb_internal_glyph_position_t *) realloc (buffer->pos, new_allocated * sizeof (buffer->pos[0]));
211     new_info = (hb_internal_glyph_info_t *) realloc (buffer->info, new_allocated * sizeof (buffer->info[0]));
212
213     if (unlikely (!new_pos || !new_info))
214       buffer->in_error = TRUE;
215
216     if (likely (new_pos))
217       buffer->pos = new_pos;
218
219     if (likely (new_info))
220       buffer->info = new_info;
221
222     buffer->out_info = separate_out ? (hb_internal_glyph_info_t *) buffer->pos : buffer->info;
223     if (likely (!buffer->in_error))
224       buffer->allocated = new_allocated;
225   }
226
227   return likely (!buffer->in_error);
228 }
229
230 void
231 hb_buffer_add_glyph (hb_buffer_t    *buffer,
232                      hb_codepoint_t  codepoint,
233                      hb_mask_t       mask,
234                      unsigned int    cluster)
235 {
236   hb_internal_glyph_info_t *glyph;
237
238   if (unlikely (!hb_buffer_ensure (buffer, buffer->len + 1))) return;
239
240   glyph = &buffer->info[buffer->len];
241   glyph->codepoint = codepoint;
242   glyph->mask = mask;
243   glyph->cluster = cluster;
244   glyph->component = 0;
245   glyph->lig_id = 0;
246   glyph->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
247
248   buffer->len++;
249 }
250
251
252 /* HarfBuzz-Internal API */
253
254 void
255 _hb_buffer_clear_output (hb_buffer_t *buffer)
256 {
257   buffer->have_output = TRUE;
258   buffer->have_positions = FALSE;
259   buffer->out_len = 0;
260   buffer->out_info = buffer->info;
261 }
262
263 void
264 hb_buffer_clear_positions (hb_buffer_t *buffer)
265 {
266   _hb_buffer_clear_output (buffer);
267   buffer->have_output = FALSE;
268   buffer->have_positions = TRUE;
269
270   if (unlikely (!buffer->pos))
271   {
272     buffer->pos = (hb_internal_glyph_position_t *) calloc (buffer->allocated, sizeof (buffer->pos[0]));
273     return;
274   }
275
276   memset (buffer->pos, 0, sizeof (buffer->pos[0]) * buffer->len);
277 }
278
279 void
280 _hb_buffer_swap (hb_buffer_t *buffer)
281 {
282   unsigned int tmp;
283
284   assert (buffer->have_output);
285
286   if (unlikely (buffer->in_error)) return;
287
288   if (buffer->out_info != buffer->info)
289   {
290     hb_internal_glyph_info_t *tmp_string;
291     tmp_string = buffer->info;
292     buffer->info = buffer->out_info;
293     buffer->out_info = tmp_string;
294     buffer->pos = (hb_internal_glyph_position_t *) buffer->out_info;
295   }
296
297   tmp = buffer->len;
298   buffer->len = buffer->out_len;
299   buffer->out_len = tmp;
300
301   buffer->i = 0;
302 }
303
304 /* The following function copies `num_out' elements from `glyph_data'
305    to `buffer->out_info', advancing the in array pointer in the structure
306    by `num_in' elements, and the out array pointer by `num_out' elements.
307    Finally, it sets the `length' field of `out' equal to
308    `pos' of the `out' structure.
309
310    If `component' is 0xFFFF, the component value from buffer->i
311    will copied `num_out' times, otherwise `component' itself will
312    be used to fill the `component' fields.
313
314    If `lig_id' is 0xFFFF, the lig_id value from buffer->i
315    will copied `num_out' times, otherwise `lig_id' itself will
316    be used to fill the `lig_id' fields.
317
318    The mask for all replacement glyphs are taken
319    from the glyph at position `buffer->i'.
320
321    The cluster value for the glyph at position buffer->i is used
322    for all replacement glyphs */
323
324 void
325 _hb_buffer_add_output_glyphs (hb_buffer_t *buffer,
326                               unsigned int num_in,
327                               unsigned int num_out,
328                               const hb_codepoint_t *glyph_data,
329                               unsigned short component,
330                               unsigned short lig_id)
331 {
332   unsigned int i;
333   unsigned int mask;
334   unsigned int cluster;
335
336   if (buffer->out_info != buffer->info ||
337       buffer->out_len + num_out > buffer->i + num_in)
338   {
339     if (unlikely (!hb_buffer_ensure_separate (buffer, buffer->out_len + num_out)))
340       return;
341   }
342
343   mask = buffer->info[buffer->i].mask;
344   cluster = buffer->info[buffer->i].cluster;
345   if (component == 0xFFFF)
346     component = buffer->info[buffer->i].component;
347   if (lig_id == 0xFFFF)
348     lig_id = buffer->info[buffer->i].lig_id;
349
350   for (i = 0; i < num_out; i++)
351   {
352     hb_internal_glyph_info_t *info = &buffer->out_info[buffer->out_len + i];
353     info->codepoint = glyph_data[i];
354     info->mask = mask;
355     info->cluster = cluster;
356     info->component = component;
357     info->lig_id = lig_id;
358     info->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
359   }
360
361   buffer->i  += num_in;
362   buffer->out_len += num_out;
363 }
364
365 void
366 _hb_buffer_add_output_glyphs_be16 (hb_buffer_t *buffer,
367                                    unsigned int num_in,
368                                    unsigned int num_out,
369                                    const uint16_t *glyph_data_be,
370                                    unsigned short component,
371                                    unsigned short lig_id)
372 {
373   unsigned int i;
374   unsigned int mask;
375   unsigned int cluster;
376
377   if (buffer->out_info != buffer->info ||
378       buffer->out_len + num_out > buffer->i + num_in)
379   {
380     if (unlikely (!hb_buffer_ensure_separate (buffer, buffer->out_len + num_out)))
381       return;
382   }
383
384   mask = buffer->info[buffer->i].mask;
385   cluster = buffer->info[buffer->i].cluster;
386   if (component == 0xFFFF)
387     component = buffer->info[buffer->i].component;
388   if (lig_id == 0xFFFF)
389     lig_id = buffer->info[buffer->i].lig_id;
390
391   for (i = 0; i < num_out; i++)
392   {
393     hb_internal_glyph_info_t *info = &buffer->out_info[buffer->out_len + i];
394     info->codepoint = hb_be_uint16 (glyph_data_be[i]);
395     info->mask = mask;
396     info->cluster = cluster;
397     info->component = component;
398     info->lig_id = lig_id;
399     info->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
400   }
401
402   buffer->i  += num_in;
403   buffer->out_len += num_out;
404 }
405
406 void
407 _hb_buffer_add_output_glyph (hb_buffer_t *buffer,
408                              hb_codepoint_t glyph_index,
409                              unsigned short component,
410                              unsigned short lig_id)
411 {
412   hb_internal_glyph_info_t *info;
413
414   if (buffer->out_info != buffer->info)
415   {
416     if (unlikely (!hb_buffer_ensure (buffer, buffer->out_len + 1))) return;
417     buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
418   }
419   else if (buffer->out_len != buffer->i)
420     buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
421
422   info = &buffer->out_info[buffer->out_len];
423   info->codepoint = glyph_index;
424   if (component != 0xFFFF)
425     info->component = component;
426   if (lig_id != 0xFFFF)
427     info->lig_id = lig_id;
428   info->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
429
430   buffer->i++;
431   buffer->out_len++;
432 }
433
434 void
435 _hb_buffer_next_glyph (hb_buffer_t *buffer)
436 {
437   if (buffer->have_output)
438   {
439     if (buffer->out_info != buffer->info)
440     {
441       if (unlikely (!hb_buffer_ensure (buffer, buffer->out_len + 1))) return;
442       buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
443     }
444     else if (buffer->out_len != buffer->i)
445       buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
446
447     buffer->out_len++;
448   }
449
450   buffer->i++;
451 }
452
453
454 unsigned int
455 hb_buffer_get_length (hb_buffer_t *buffer)
456 {
457   return buffer->len;
458 }
459
460 /* Return value valid as long as buffer not modified */
461 hb_glyph_info_t *
462 hb_buffer_get_glyph_infos (hb_buffer_t *buffer)
463 {
464   return (hb_glyph_info_t *) buffer->info;
465 }
466
467 /* Return value valid as long as buffer not modified */
468 hb_glyph_position_t *
469 hb_buffer_get_glyph_positions (hb_buffer_t *buffer)
470 {
471   if (!buffer->have_positions)
472     hb_buffer_clear_positions (buffer);
473
474   return (hb_glyph_position_t *) buffer->pos;
475 }
476
477
478 static void
479 reverse_range (hb_buffer_t *buffer,
480                unsigned int start,
481                unsigned int end)
482 {
483   unsigned int i, j;
484
485   for (i = start, j = end - 1; i < j; i++, j--) {
486     hb_internal_glyph_info_t t;
487
488     t = buffer->info[i];
489     buffer->info[i] = buffer->info[j];
490     buffer->info[j] = t;
491   }
492
493   if (buffer->pos) {
494     for (i = 0, j = end - 1; i < j; i++, j--) {
495       hb_internal_glyph_position_t t;
496
497       t = buffer->pos[i];
498       buffer->pos[i] = buffer->pos[j];
499       buffer->pos[j] = t;
500     }
501   }
502 }
503
504 void
505 hb_buffer_reverse (hb_buffer_t *buffer)
506 {
507   if (unlikely (!buffer->len))
508     return;
509
510   reverse_range (buffer, 0, buffer->len);
511 }
512
513 void
514 hb_buffer_reverse_clusters (hb_buffer_t *buffer)
515 {
516   unsigned int i, start, count, last_cluster;
517
518   if (unlikely (!buffer->len))
519     return;
520
521   hb_buffer_reverse (buffer);
522
523   count = buffer->len;
524   start = 0;
525   last_cluster = buffer->info[0].cluster;
526   for (i = 1; i < count; i++) {
527     if (last_cluster != buffer->info[i].cluster) {
528       reverse_range (buffer, start, i);
529       start = i;
530       last_cluster = buffer->info[i].cluster;
531     }
532   }
533   reverse_range (buffer, start, i);
534 }
535
536
537 #define ADD_UTF(T) \
538         HB_STMT_START { \
539           const T *next = (const T *) text + item_offset; \
540           const T *end = next + item_length; \
541           while (next < end) { \
542             hb_codepoint_t u; \
543             const T *old_next = next; \
544             next = UTF_NEXT (next, end, u); \
545             hb_buffer_add_glyph (buffer, u, 0,  old_next - (const T *) text); \
546           } \
547         } HB_STMT_END
548
549
550 #define UTF8_COMPUTE(Char, Mask, Len) \
551   if (Char < 128) { Len = 1; Mask = 0x7f; } \
552   else if ((Char & 0xe0) == 0xc0) { Len = 2; Mask = 0x1f; } \
553   else if ((Char & 0xf0) == 0xe0) { Len = 3; Mask = 0x0f; } \
554   else if ((Char & 0xf8) == 0xf0) { Len = 4; Mask = 0x07; } \
555   else Len = 0;
556
557 static inline const uint8_t *
558 hb_utf8_next (const uint8_t *text,
559               const uint8_t *end,
560               hb_codepoint_t *unicode)
561 {
562   uint8_t c = *text;
563   unsigned int mask, len;
564
565   UTF8_COMPUTE (c, mask, len);
566   if (unlikely (!len || (unsigned int) (end - text) < len)) {
567     *unicode = -1;
568     return text + 1;
569   } else {
570     hb_codepoint_t result;
571     unsigned int i;
572     result = c & mask;
573     for (i = 1; i < len; i++)
574       {
575         if (unlikely ((text[i] & 0xc0) != 0x80))
576           {
577             *unicode = -1;
578             return text + 1;
579           }
580         result <<= 6;
581         result |= (text[i] & 0x3f);
582       }
583     *unicode = result;
584     return text + len;
585   }
586 }
587
588 void
589 hb_buffer_add_utf8 (hb_buffer_t  *buffer,
590                     const char   *text,
591                     unsigned int  text_length HB_UNUSED,
592                     unsigned int  item_offset,
593                     unsigned int  item_length)
594 {
595 #define UTF_NEXT(S, E, U)       hb_utf8_next (S, E, &(U))
596   ADD_UTF (uint8_t);
597 #undef UTF_NEXT
598 }
599
600 static inline const uint16_t *
601 hb_utf16_next (const uint16_t *text,
602                const uint16_t *end,
603                hb_codepoint_t *unicode)
604 {
605   uint16_t c = *text++;
606
607   if (unlikely (c >= 0xd800 && c < 0xdc00)) {
608     /* high surrogate */
609     uint16_t l;
610     if (text < end && ((l = *text), unlikely (l >= 0xdc00 && l < 0xe000))) {
611       /* low surrogate */
612       *unicode = ((hb_codepoint_t) ((c) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000);
613        text++;
614     } else
615       *unicode = -1;
616   } else
617     *unicode = c;
618
619   return text;
620 }
621
622 void
623 hb_buffer_add_utf16 (hb_buffer_t    *buffer,
624                      const uint16_t *text,
625                      unsigned int    text_length HB_UNUSED,
626                      unsigned int    item_offset,
627                      unsigned int    item_length)
628 {
629 #define UTF_NEXT(S, E, U)       hb_utf16_next (S, E, &(U))
630   ADD_UTF (uint16_t);
631 #undef UTF_NEXT
632 }
633
634 void
635 hb_buffer_add_utf32 (hb_buffer_t    *buffer,
636                      const uint32_t *text,
637                      unsigned int    text_length HB_UNUSED,
638                      unsigned int    item_offset,
639                      unsigned int    item_length)
640 {
641 #define UTF_NEXT(S, E, U)       ((U) = *(S), (S)+1)
642   ADD_UTF (uint32_t);
643 #undef UTF_NEXT
644 }