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