Add a few other buffer methods
[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 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 = (hb_internal_glyph_position_t *) 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   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_length = 0;
185   buffer->out_length = 0;
186   buffer->in_pos = 0;
187   buffer->out_pos = 0;
188   buffer->out_string = buffer->in_string;
189   buffer->max_lig_id = 0;
190 }
191
192 void
193 hb_buffer_ensure (hb_buffer_t *buffer, unsigned int size)
194 {
195   unsigned int new_allocated = buffer->allocated;
196
197   if (size > new_allocated)
198   {
199     while (size > new_allocated)
200       new_allocated += (new_allocated >> 1) + 8;
201
202     if (buffer->positions)
203       buffer->positions = (hb_internal_glyph_position_t *) realloc (buffer->positions, new_allocated * sizeof (buffer->positions[0]));
204
205     if (buffer->out_string != buffer->in_string)
206     {
207       buffer->in_string = (hb_internal_glyph_info_t *) realloc (buffer->in_string, new_allocated * sizeof (buffer->in_string[0]));
208       buffer->out_string = (hb_internal_glyph_info_t *) buffer->positions;
209     }
210     else
211     {
212       buffer->in_string = (hb_internal_glyph_info_t *) realloc (buffer->in_string, new_allocated * sizeof (buffer->in_string[0]));
213       buffer->out_string = buffer->in_string;
214     }
215
216     buffer->allocated = new_allocated;
217   }
218 }
219
220 void
221 hb_buffer_add_glyph (hb_buffer_t    *buffer,
222                      hb_codepoint_t  codepoint,
223                      hb_mask_t       mask,
224                      unsigned int    cluster)
225 {
226   hb_internal_glyph_info_t *glyph;
227
228   hb_buffer_ensure (buffer, buffer->in_length + 1);
229
230   glyph = &buffer->in_string[buffer->in_length];
231   glyph->codepoint = codepoint;
232   glyph->mask = mask;
233   glyph->cluster = cluster;
234   glyph->component = 0;
235   glyph->lig_id = 0;
236   glyph->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
237
238   buffer->in_length++;
239 }
240
241
242 /* HarfBuzz-Internal API */
243
244 void
245 _hb_buffer_clear_output (hb_buffer_t *buffer)
246 {
247   buffer->have_output = TRUE;
248   buffer->have_positions = FALSE;
249   buffer->out_length = 0;
250   buffer->out_pos = 0;
251   buffer->out_string = buffer->in_string;
252 }
253
254 void
255 hb_buffer_clear_positions (hb_buffer_t *buffer)
256 {
257   _hb_buffer_clear_output (buffer);
258   buffer->have_output = FALSE;
259   buffer->have_positions = TRUE;
260
261   if (unlikely (!buffer->positions))
262   {
263     buffer->positions = (hb_internal_glyph_position_t *) calloc (buffer->allocated, sizeof (buffer->positions[0]));
264     return;
265   }
266
267   memset (buffer->positions, 0, sizeof (buffer->positions[0]) * buffer->in_length);
268 }
269
270 void
271 _hb_buffer_swap (hb_buffer_t *buffer)
272 {
273   unsigned int tmp;
274
275   assert (buffer->have_output);
276
277   if (buffer->out_string != buffer->in_string)
278   {
279     hb_internal_glyph_info_t *tmp_string;
280     tmp_string = buffer->in_string;
281     buffer->in_string = buffer->out_string;
282     buffer->out_string = tmp_string;
283     buffer->positions = (hb_internal_glyph_position_t *) buffer->out_string;
284   }
285
286   tmp = buffer->in_length;
287   buffer->in_length = buffer->out_length;
288   buffer->out_length = tmp;
289
290   tmp = buffer->in_pos;
291   buffer->in_pos = buffer->out_pos;
292   buffer->out_pos = tmp;
293 }
294
295 /* The following function copies `num_out' elements from `glyph_data'
296    to `buffer->out_string', advancing the in array pointer in the structure
297    by `num_in' elements, and the out array pointer by `num_out' elements.
298    Finally, it sets the `length' field of `out' equal to
299    `pos' of the `out' structure.
300
301    If `component' is 0xFFFF, the component value from buffer->in_pos
302    will copied `num_out' times, otherwise `component' itself will
303    be used to fill the `component' fields.
304
305    If `lig_id' is 0xFFFF, the lig_id value from buffer->in_pos
306    will copied `num_out' times, otherwise `lig_id' itself will
307    be used to fill the `lig_id' fields.
308
309    The mask for all replacement glyphs are taken
310    from the glyph at position `buffer->in_pos'.
311
312    The cluster value for the glyph at position buffer->in_pos is used
313    for all replacement glyphs */
314
315 void
316 _hb_buffer_add_output_glyphs (hb_buffer_t *buffer,
317                               unsigned int num_in,
318                               unsigned int num_out,
319                               const hb_codepoint_t *glyph_data,
320                               unsigned short component,
321                               unsigned short lig_id)
322 {
323   unsigned int i;
324   unsigned int mask;
325   unsigned int cluster;
326
327   if (buffer->out_string != buffer->in_string ||
328       buffer->out_pos + num_out > buffer->in_pos + num_in)
329   {
330     hb_buffer_ensure_separate (buffer, buffer->out_pos + num_out);
331   }
332
333   mask = buffer->in_string[buffer->in_pos].mask;
334   cluster = buffer->in_string[buffer->in_pos].cluster;
335   if (component == 0xFFFF)
336     component = buffer->in_string[buffer->in_pos].component;
337   if (lig_id == 0xFFFF)
338     lig_id = buffer->in_string[buffer->in_pos].lig_id;
339
340   for (i = 0; i < num_out; i++)
341   {
342     hb_internal_glyph_info_t *info = &buffer->out_string[buffer->out_pos + i];
343     info->codepoint = glyph_data[i];
344     info->mask = mask;
345     info->cluster = cluster;
346     info->component = component;
347     info->lig_id = lig_id;
348     info->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
349   }
350
351   buffer->in_pos  += num_in;
352   buffer->out_pos += num_out;
353   buffer->out_length = buffer->out_pos;
354 }
355
356 void
357 _hb_buffer_add_output_glyphs_be16 (hb_buffer_t *buffer,
358                                    unsigned int num_in,
359                                    unsigned int num_out,
360                                    const uint16_t *glyph_data_be,
361                                    unsigned short component,
362                                    unsigned short lig_id)
363 {
364   unsigned int i;
365   unsigned int mask;
366   unsigned int cluster;
367
368   if (buffer->out_string != buffer->in_string ||
369       buffer->out_pos + num_out > buffer->in_pos + num_in)
370   {
371     hb_buffer_ensure_separate (buffer, buffer->out_pos + num_out);
372   }
373
374   mask = buffer->in_string[buffer->in_pos].mask;
375   cluster = buffer->in_string[buffer->in_pos].cluster;
376   if (component == 0xFFFF)
377     component = buffer->in_string[buffer->in_pos].component;
378   if (lig_id == 0xFFFF)
379     lig_id = buffer->in_string[buffer->in_pos].lig_id;
380
381   for (i = 0; i < num_out; i++)
382   {
383     hb_internal_glyph_info_t *info = &buffer->out_string[buffer->out_pos + i];
384     info->codepoint = hb_be_uint16 (glyph_data_be[i]);
385     info->mask = mask;
386     info->cluster = cluster;
387     info->component = component;
388     info->lig_id = lig_id;
389     info->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
390   }
391
392   buffer->in_pos  += num_in;
393   buffer->out_pos += num_out;
394   buffer->out_length = buffer->out_pos;
395 }
396
397 void
398 _hb_buffer_add_output_glyph (hb_buffer_t *buffer,
399                              hb_codepoint_t glyph_index,
400                              unsigned short component,
401                              unsigned short lig_id)
402 {
403   hb_internal_glyph_info_t *info;
404
405   if (buffer->out_string != buffer->in_string)
406   {
407     hb_buffer_ensure (buffer, buffer->out_pos + 1);
408     buffer->out_string[buffer->out_pos] = buffer->in_string[buffer->in_pos];
409   }
410   else if (buffer->out_pos != buffer->in_pos)
411     buffer->out_string[buffer->out_pos] = buffer->in_string[buffer->in_pos];
412
413   info = &buffer->out_string[buffer->out_pos];
414   info->codepoint = glyph_index;
415   if (component != 0xFFFF)
416     info->component = component;
417   if (lig_id != 0xFFFF)
418     info->lig_id = lig_id;
419   info->gproperty = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
420
421   buffer->in_pos++;
422   buffer->out_pos++;
423   buffer->out_length = buffer->out_pos;
424 }
425
426 void
427 _hb_buffer_next_glyph (hb_buffer_t *buffer)
428 {
429   if (buffer->have_output)
430   {
431     if (buffer->out_string != buffer->in_string)
432     {
433       hb_buffer_ensure (buffer, buffer->out_pos + 1);
434       buffer->out_string[buffer->out_pos] = buffer->in_string[buffer->in_pos];
435     }
436     else if (buffer->out_pos != buffer->in_pos)
437       buffer->out_string[buffer->out_pos] = buffer->in_string[buffer->in_pos];
438
439     buffer->out_pos++;
440     buffer->out_length = buffer->out_pos;
441   }
442
443   buffer->in_pos++;
444 }
445
446
447 unsigned int
448 hb_buffer_get_length (hb_buffer_t *buffer)
449 {
450   return buffer->in_length;
451 }
452
453 /* Return value valid as long as buffer not modified */
454 hb_glyph_info_t *
455 hb_buffer_get_glyph_infos (hb_buffer_t *buffer)
456 {
457   return (hb_glyph_info_t *) buffer->in_string;
458 }
459
460 /* Return value valid as long as buffer not modified */
461 hb_glyph_position_t *
462 hb_buffer_get_glyph_positions (hb_buffer_t *buffer)
463 {
464   if (!buffer->have_positions)
465     hb_buffer_clear_positions (buffer);
466
467   return (hb_glyph_position_t *) buffer->positions;
468 }
469
470
471 static void
472 reverse_range (hb_buffer_t *buffer,
473                unsigned int start,
474                unsigned int end)
475 {
476   unsigned int i, j;
477
478   for (i = start, j = end - 1; i < j; i++, j--) {
479     hb_internal_glyph_info_t t;
480
481     t = buffer->in_string[i];
482     buffer->in_string[i] = buffer->in_string[j];
483     buffer->in_string[j] = t;
484   }
485
486   if (buffer->positions) {
487     for (i = 0, j = end - 1; i < j; i++, j--) {
488       hb_internal_glyph_position_t t;
489
490       t = buffer->positions[i];
491       buffer->positions[i] = buffer->positions[j];
492       buffer->positions[j] = t;
493     }
494   }
495 }
496
497 void
498 hb_buffer_reverse (hb_buffer_t *buffer)
499 {
500   if (unlikely (!buffer->in_length))
501     return;
502
503   reverse_range (buffer, 0, buffer->in_length);
504 }
505
506 void
507 hb_buffer_reverse_clusters (hb_buffer_t *buffer)
508 {
509   unsigned int i, start, count, last_cluster;
510
511   if (unlikely (!buffer->in_length))
512     return;
513
514   hb_buffer_reverse (buffer);
515
516   count = buffer->in_length;
517   start = 0;
518   last_cluster = buffer->in_string[0].cluster;
519   for (i = 1; i < count; i++) {
520     if (last_cluster != buffer->in_string[i].cluster) {
521       reverse_range (buffer, start, i);
522       start = i;
523       last_cluster = buffer->in_string[i].cluster;
524     }
525   }
526   reverse_range (buffer, start, i);
527 }
528
529
530 #define ADD_UTF(T) \
531         HB_STMT_START { \
532           const T *next = (const T *) text + item_offset; \
533           const T *end = next + item_length; \
534           while (next < end) { \
535             hb_codepoint_t u; \
536             const T *old_next = next; \
537             next = UTF_NEXT (next, end, u); \
538             hb_buffer_add_glyph (buffer, u, 0,  old_next - (const T *) text); \
539           } \
540         } HB_STMT_END
541
542
543 #define UTF8_COMPUTE(Char, Mask, Len) \
544   if (Char < 128) { Len = 1; Mask = 0x7f; } \
545   else if ((Char & 0xe0) == 0xc0) { Len = 2; Mask = 0x1f; } \
546   else if ((Char & 0xf0) == 0xe0) { Len = 3; Mask = 0x0f; } \
547   else if ((Char & 0xf8) == 0xf0) { Len = 4; Mask = 0x07; } \
548   else Len = 0;
549
550 static inline const uint8_t *
551 hb_utf8_next (const uint8_t *text,
552               const uint8_t *end,
553               hb_codepoint_t *unicode)
554 {
555   uint8_t c = *text;
556   unsigned int mask, len;
557
558   UTF8_COMPUTE (c, mask, len);
559   if (unlikely (!len || (unsigned int) (end - text) < len)) {
560     *unicode = -1;
561     return text + 1;
562   } else {
563     hb_codepoint_t result;
564     unsigned int i;
565     result = c & mask;
566     for (i = 1; i < len; i++)
567       {
568         if (unlikely ((text[i] & 0xc0) != 0x80))
569           {
570             *unicode = -1;
571             return text + 1;
572           }
573         result <<= 6;
574         result |= (text[i] & 0x3f);
575       }
576     *unicode = result;
577     return text + len;
578   }
579 }
580
581 void
582 hb_buffer_add_utf8 (hb_buffer_t  *buffer,
583                     const char   *text,
584                     unsigned int  text_length HB_UNUSED,
585                     unsigned int  item_offset,
586                     unsigned int  item_length)
587 {
588 #define UTF_NEXT(S, E, U)       hb_utf8_next (S, E, &(U))
589   ADD_UTF (uint8_t);
590 #undef UTF_NEXT
591 }
592
593 static inline const uint16_t *
594 hb_utf16_next (const uint16_t *text,
595                const uint16_t *end,
596                hb_codepoint_t *unicode)
597 {
598   uint16_t c = *text++;
599
600   if (unlikely (c >= 0xd800 && c < 0xdc00)) {
601     /* high surrogate */
602     uint16_t l;
603     if (text < end && ((l = *text), unlikely (l >= 0xdc00 && l < 0xe000))) {
604       /* low surrogate */
605       *unicode = ((hb_codepoint_t) ((c) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000);
606        text++;
607     } else
608       *unicode = -1;
609   } else
610     *unicode = c;
611
612   return text;
613 }
614
615 void
616 hb_buffer_add_utf16 (hb_buffer_t    *buffer,
617                      const uint16_t *text,
618                      unsigned int    text_length HB_UNUSED,
619                      unsigned int    item_offset,
620                      unsigned int    item_length)
621 {
622 #define UTF_NEXT(S, E, U)       hb_utf16_next (S, E, &(U))
623   ADD_UTF (uint16_t);
624 #undef UTF_NEXT
625 }
626
627 void
628 hb_buffer_add_utf32 (hb_buffer_t    *buffer,
629                      const uint32_t *text,
630                      unsigned int    text_length HB_UNUSED,
631                      unsigned int    item_offset,
632                      unsigned int    item_length)
633 {
634 #define UTF_NEXT(S, E, U)       ((U) = *(S), (S)+1)
635   ADD_UTF (uint32_t);
636 #undef UTF_NEXT
637 }