hb-buffer.cc revision dd2ffd282c059194fd87fb1664e2e0cdb56a87a0
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
32HB_BEGIN_DECLS
33
34
35static hb_buffer_t _hb_buffer_nil = {
36  HB_REFERENCE_COUNT_INVALID, /* ref_count */
37
38  &_hb_unicode_funcs_nil  /* unicode */
39};
40
41/* Here is how the buffer works internally:
42 *
43 * There are two info pointers: info and out_info.  They always have
44 * the same allocated size, but different lengths.
45 *
46 * As an optimization, both info and out_info may point to the
47 * same piece of memory, which is owned by info.  This remains the
48 * case as long as out_len doesn't exceed len at any time.
49 * In that case, swap() is no-op and the glyph operations operate
50 * mostly in-place.
51 *
52 * As soon as out_info gets longer than info, out_info is moved over
53 * to an alternate buffer (which we reuse the pos buffer for!), and its
54 * current contents (out_len entries) are copied to the new place.
55 * This should all remain transparent to the user.  swap() then
56 * switches info and out_info.
57 */
58
59
60static hb_bool_t
61_hb_buffer_enlarge (hb_buffer_t *buffer, unsigned int size)
62{
63  if (unlikely (buffer->in_error))
64    return FALSE;
65
66  unsigned int new_allocated = buffer->allocated;
67  hb_glyph_position_t *new_pos;
68  hb_glyph_info_t *new_info;
69  bool separate_out;
70
71  separate_out = buffer->out_info != buffer->info;
72
73  while (size > new_allocated)
74    new_allocated += (new_allocated >> 1) + 8;
75
76  new_pos = (hb_glyph_position_t *) realloc (buffer->pos, new_allocated * sizeof (buffer->pos[0]));
77  new_info = (hb_glyph_info_t *) realloc (buffer->info, new_allocated * sizeof (buffer->info[0]));
78
79  if (unlikely (!new_pos || !new_info))
80    buffer->in_error = TRUE;
81
82  if (likely (new_pos))
83    buffer->pos = new_pos;
84
85  if (likely (new_info))
86    buffer->info = new_info;
87
88  buffer->out_info = separate_out ? (hb_glyph_info_t *) buffer->pos : buffer->info;
89  if (likely (!buffer->in_error))
90    buffer->allocated = new_allocated;
91
92  return likely (!buffer->in_error);
93}
94
95static inline hb_bool_t
96_hb_buffer_ensure (hb_buffer_t *buffer, unsigned int size)
97{
98  return likely (size <= buffer->allocated) ? TRUE : _hb_buffer_enlarge (buffer, size);
99}
100
101static inline hb_bool_t
102_hb_buffer_ensure_separate (hb_buffer_t *buffer, unsigned int size)
103{
104  if (unlikely (!_hb_buffer_ensure (buffer, size))) return FALSE;
105
106  if (buffer->out_info == buffer->info)
107  {
108    assert (buffer->have_output);
109
110    buffer->out_info = (hb_glyph_info_t *) buffer->pos;
111    memcpy (buffer->out_info, buffer->info, buffer->out_len * sizeof (buffer->out_info[0]));
112  }
113
114  return TRUE;
115}
116
117
118/* Public API */
119
120hb_buffer_t *
121hb_buffer_create (unsigned int pre_alloc_size)
122{
123  hb_buffer_t *buffer;
124
125  if (!HB_OBJECT_DO_CREATE (hb_buffer_t, buffer))
126    return &_hb_buffer_nil;
127
128  if (pre_alloc_size)
129    _hb_buffer_ensure (buffer, pre_alloc_size);
130
131  buffer->unicode = &_hb_unicode_funcs_nil;
132
133  return buffer;
134}
135
136hb_buffer_t *
137hb_buffer_reference (hb_buffer_t *buffer)
138{
139  HB_OBJECT_DO_REFERENCE (buffer);
140}
141
142unsigned int
143hb_buffer_get_reference_count (hb_buffer_t *buffer)
144{
145  HB_OBJECT_DO_GET_REFERENCE_COUNT (buffer);
146}
147
148void
149hb_buffer_destroy (hb_buffer_t *buffer)
150{
151  HB_OBJECT_DO_DESTROY (buffer);
152
153  hb_unicode_funcs_destroy (buffer->unicode);
154
155  free (buffer->info);
156  free (buffer->pos);
157
158  free (buffer);
159}
160
161
162void
163hb_buffer_set_unicode_funcs (hb_buffer_t        *buffer,
164			     hb_unicode_funcs_t *unicode)
165{
166  if (!unicode)
167    unicode = &_hb_unicode_funcs_nil;
168
169  hb_unicode_funcs_reference (unicode);
170  hb_unicode_funcs_destroy (buffer->unicode);
171  buffer->unicode = unicode;
172}
173
174hb_unicode_funcs_t *
175hb_buffer_get_unicode_funcs (hb_buffer_t        *buffer)
176{
177  return buffer->unicode;
178}
179
180void
181hb_buffer_set_direction (hb_buffer_t    *buffer,
182			 hb_direction_t  direction)
183
184{
185  buffer->props.direction = direction;
186}
187
188hb_direction_t
189hb_buffer_get_direction (hb_buffer_t    *buffer)
190{
191  return buffer->props.direction;
192}
193
194void
195hb_buffer_set_script (hb_buffer_t *buffer,
196		      hb_script_t  script)
197{
198  buffer->props.script = script;
199}
200
201hb_script_t
202hb_buffer_get_script (hb_buffer_t *buffer)
203{
204  return buffer->props.script;
205}
206
207void
208hb_buffer_set_language (hb_buffer_t   *buffer,
209			hb_language_t  language)
210{
211  buffer->props.language = language;
212}
213
214hb_language_t
215hb_buffer_get_language (hb_buffer_t *buffer)
216{
217  return buffer->props.language;
218}
219
220
221void
222hb_buffer_clear (hb_buffer_t *buffer)
223{
224  buffer->have_output = FALSE;
225  buffer->have_positions = FALSE;
226  buffer->in_error = FALSE;
227  buffer->len = 0;
228  buffer->out_len = 0;
229  buffer->i = 0;
230  buffer->out_info = buffer->info;
231  buffer->max_lig_id = 0;
232}
233
234hb_bool_t
235hb_buffer_ensure (hb_buffer_t *buffer, unsigned int size)
236{
237  return _hb_buffer_ensure (buffer, size);
238}
239
240void
241hb_buffer_add_glyph (hb_buffer_t    *buffer,
242		     hb_codepoint_t  codepoint,
243		     hb_mask_t       mask,
244		     unsigned int    cluster)
245{
246  hb_glyph_info_t *glyph;
247
248  if (unlikely (!_hb_buffer_ensure (buffer, buffer->len + 1))) return;
249
250  glyph = &buffer->info[buffer->len];
251  glyph->codepoint = codepoint;
252  glyph->mask = mask;
253  glyph->cluster = cluster;
254  glyph->component() = 0;
255  glyph->lig_id() = 0;
256  glyph->gproperty() = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
257
258  buffer->len++;
259}
260
261void
262hb_buffer_clear_positions (hb_buffer_t *buffer)
263{
264  _hb_buffer_clear_output (buffer);
265  buffer->have_output = FALSE;
266  buffer->have_positions = TRUE;
267
268  if (unlikely (!buffer->pos))
269  {
270    buffer->pos = (hb_glyph_position_t *) calloc (buffer->allocated, sizeof (buffer->pos[0]));
271    return;
272  }
273
274  memset (buffer->pos, 0, sizeof (buffer->pos[0]) * buffer->len);
275}
276
277/* HarfBuzz-Internal API */
278
279void
280_hb_buffer_clear_output (hb_buffer_t *buffer)
281{
282  buffer->have_output = TRUE;
283  buffer->have_positions = FALSE;
284  buffer->out_len = 0;
285  buffer->out_info = buffer->info;
286}
287
288void
289_hb_buffer_swap (hb_buffer_t *buffer)
290{
291  unsigned int tmp;
292
293  assert (buffer->have_output);
294
295  if (unlikely (buffer->in_error)) return;
296
297  if (buffer->out_info != buffer->info)
298  {
299    hb_glyph_info_t *tmp_string;
300    tmp_string = buffer->info;
301    buffer->info = buffer->out_info;
302    buffer->out_info = tmp_string;
303    buffer->pos = (hb_glyph_position_t *) buffer->out_info;
304  }
305
306  tmp = buffer->len;
307  buffer->len = buffer->out_len;
308  buffer->out_len = tmp;
309
310  buffer->i = 0;
311}
312
313void
314_hb_buffer_replace_glyphs (hb_buffer_t *buffer,
315			   unsigned int num_in,
316			   unsigned int num_out,
317			   const hb_codepoint_t *glyph_data)
318{
319  if (buffer->out_info != buffer->info ||
320      buffer->out_len + num_out > buffer->i + num_in)
321  {
322    if (unlikely (!_hb_buffer_ensure_separate (buffer, buffer->out_len + num_out)))
323      return;
324  }
325
326  hb_glyph_info_t orig_info = buffer->info[buffer->i];
327
328  for (unsigned int i = 0; i < num_out; i++)
329  {
330    hb_glyph_info_t *info = &buffer->out_info[buffer->out_len + i];
331    *info = orig_info;
332    info->codepoint = glyph_data[i];
333    info->gproperty() = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
334  }
335
336  buffer->i  += num_in;
337  buffer->out_len += num_out;
338}
339
340void
341_hb_buffer_replace_glyphs_be16 (hb_buffer_t *buffer,
342				unsigned int num_in,
343				unsigned int num_out,
344				const uint16_t *glyph_data_be)
345{
346  if (buffer->out_info != buffer->info ||
347      buffer->out_len + num_out > buffer->i + num_in)
348  {
349    if (unlikely (!_hb_buffer_ensure_separate (buffer, buffer->out_len + num_out)))
350      return;
351  }
352
353  hb_glyph_info_t orig_info = buffer->info[buffer->i];
354
355  for (unsigned int i = 0; i < num_out; i++)
356  {
357    hb_glyph_info_t *info = &buffer->out_info[buffer->out_len + i];
358    *info = orig_info;
359    info->codepoint = hb_be_uint16 (glyph_data_be[i]);
360    info->gproperty() = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
361  }
362
363  buffer->i  += num_in;
364  buffer->out_len += num_out;
365}
366
367void
368_hb_buffer_replace_glyph (hb_buffer_t *buffer,
369			  hb_codepoint_t glyph_index)
370{
371  hb_glyph_info_t *info;
372
373  if (buffer->out_info != buffer->info)
374  {
375    if (unlikely (!_hb_buffer_ensure (buffer, buffer->out_len + 1))) return;
376    buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
377  }
378  else if (buffer->out_len != buffer->i)
379    buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
380
381  info = &buffer->out_info[buffer->out_len];
382  info->codepoint = glyph_index;
383  info->gproperty() = HB_BUFFER_GLYPH_PROPERTIES_UNKNOWN;
384
385  buffer->i++;
386  buffer->out_len++;
387}
388
389void
390_hb_buffer_next_glyph (hb_buffer_t *buffer)
391{
392  if (buffer->have_output)
393  {
394    if (buffer->out_info != buffer->info)
395    {
396      if (unlikely (!_hb_buffer_ensure (buffer, buffer->out_len + 1))) return;
397      buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
398    }
399    else if (buffer->out_len != buffer->i)
400      buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
401
402    buffer->out_len++;
403  }
404
405  buffer->i++;
406}
407
408void
409_hb_buffer_reset_masks (hb_buffer_t *buffer,
410			hb_mask_t    mask)
411{
412  unsigned int count = buffer->len;
413  for (unsigned int i = 0; i < count; i++)
414    buffer->info[i].mask = mask;
415}
416
417void
418_hb_buffer_add_masks (hb_buffer_t *buffer,
419		      hb_mask_t    mask)
420{
421  unsigned int count = buffer->len;
422  for (unsigned int i = 0; i < count; i++)
423    buffer->info[i].mask |= mask;
424}
425
426void
427_hb_buffer_set_masks (hb_buffer_t *buffer,
428		      hb_mask_t    value,
429		      hb_mask_t    mask,
430		      unsigned int cluster_start,
431		      unsigned int cluster_end)
432{
433  hb_mask_t not_mask = ~mask;
434  value &= mask;
435
436  if (!mask)
437    return;
438
439  if (cluster_start == 0 && cluster_end == (unsigned int)-1) {
440    unsigned int count = buffer->len;
441    for (unsigned int i = 0; i < count; i++)
442      buffer->info[i].mask = (buffer->info[i].mask & not_mask) | value;
443    return;
444  }
445
446  /* XXX can't bsearch since .cluster may not be sorted. */
447  /* Binary search to find the start position and go from there. */
448  unsigned int min = 0, max = buffer->len;
449  while (min < max)
450  {
451    unsigned int mid = min + ((max - min) / 2);
452    if (buffer->info[mid].cluster < cluster_start)
453      min = mid + 1;
454    else
455      max = mid;
456  }
457  unsigned int count = buffer->len;
458  for (unsigned int i = min; i < count && buffer->info[i].cluster < cluster_end; i++)
459    buffer->info[i].mask = (buffer->info[i].mask & not_mask) | value;
460}
461
462
463/* Public API again */
464
465unsigned int
466hb_buffer_get_length (hb_buffer_t *buffer)
467{
468  return buffer->len;
469}
470
471/* Return value valid as long as buffer not modified */
472hb_glyph_info_t *
473hb_buffer_get_glyph_infos (hb_buffer_t *buffer)
474{
475  return (hb_glyph_info_t *) buffer->info;
476}
477
478/* Return value valid as long as buffer not modified */
479hb_glyph_position_t *
480hb_buffer_get_glyph_positions (hb_buffer_t *buffer)
481{
482  if (!buffer->have_positions)
483    hb_buffer_clear_positions (buffer);
484
485  return (hb_glyph_position_t *) buffer->pos;
486}
487
488
489static void
490reverse_range (hb_buffer_t *buffer,
491	       unsigned int start,
492	       unsigned int end)
493{
494  unsigned int i, j;
495
496  for (i = start, j = end - 1; i < j; i++, j--) {
497    hb_glyph_info_t t;
498
499    t = buffer->info[i];
500    buffer->info[i] = buffer->info[j];
501    buffer->info[j] = t;
502  }
503
504  if (buffer->pos) {
505    for (i = 0, j = end - 1; i < j; i++, j--) {
506      hb_glyph_position_t t;
507
508      t = buffer->pos[i];
509      buffer->pos[i] = buffer->pos[j];
510      buffer->pos[j] = t;
511    }
512  }
513}
514
515void
516hb_buffer_reverse (hb_buffer_t *buffer)
517{
518  if (unlikely (!buffer->len))
519    return;
520
521  reverse_range (buffer, 0, buffer->len);
522}
523
524void
525hb_buffer_reverse_clusters (hb_buffer_t *buffer)
526{
527  unsigned int i, start, count, last_cluster;
528
529  if (unlikely (!buffer->len))
530    return;
531
532  hb_buffer_reverse (buffer);
533
534  count = buffer->len;
535  start = 0;
536  last_cluster = buffer->info[0].cluster;
537  for (i = 1; i < count; i++) {
538    if (last_cluster != buffer->info[i].cluster) {
539      reverse_range (buffer, start, i);
540      start = i;
541      last_cluster = buffer->info[i].cluster;
542    }
543  }
544  reverse_range (buffer, start, i);
545}
546
547
548#define ADD_UTF(T) \
549	HB_STMT_START { \
550	  const T *next = (const T *) text + item_offset; \
551	  const T *end = next + item_length; \
552	  while (next < end) { \
553	    hb_codepoint_t u; \
554	    const T *old_next = next; \
555	    next = UTF_NEXT (next, end, u); \
556	    hb_buffer_add_glyph (buffer, u, 1,  old_next - (const T *) text); \
557	  } \
558	} HB_STMT_END
559
560
561#define UTF8_COMPUTE(Char, Mask, Len) \
562  if (Char < 128) { Len = 1; Mask = 0x7f; } \
563  else if ((Char & 0xe0) == 0xc0) { Len = 2; Mask = 0x1f; } \
564  else if ((Char & 0xf0) == 0xe0) { Len = 3; Mask = 0x0f; } \
565  else if ((Char & 0xf8) == 0xf0) { Len = 4; Mask = 0x07; } \
566  else Len = 0;
567
568static inline const uint8_t *
569hb_utf8_next (const uint8_t *text,
570	      const uint8_t *end,
571	      hb_codepoint_t *unicode)
572{
573  uint8_t c = *text;
574  unsigned int mask, len;
575
576  /* TODO check for overlong sequences?  also: optimize? */
577
578  UTF8_COMPUTE (c, mask, len);
579  if (unlikely (!len || (unsigned int) (end - text) < len)) {
580    *unicode = -1;
581    return text + 1;
582  } else {
583    hb_codepoint_t result;
584    unsigned int i;
585    result = c & mask;
586    for (i = 1; i < len; i++)
587      {
588	if (unlikely ((text[i] & 0xc0) != 0x80))
589	  {
590	    *unicode = -1;
591	    return text + 1;
592	  }
593	result <<= 6;
594	result |= (text[i] & 0x3f);
595      }
596    *unicode = result;
597    return text + len;
598  }
599}
600
601void
602hb_buffer_add_utf8 (hb_buffer_t  *buffer,
603		    const char   *text,
604		    unsigned int  text_length HB_UNUSED,
605		    unsigned int  item_offset,
606		    unsigned int  item_length)
607{
608#define UTF_NEXT(S, E, U)	hb_utf8_next (S, E, &(U))
609  ADD_UTF (uint8_t);
610#undef UTF_NEXT
611}
612
613static inline const uint16_t *
614hb_utf16_next (const uint16_t *text,
615	       const uint16_t *end,
616	       hb_codepoint_t *unicode)
617{
618  uint16_t c = *text++;
619
620  if (unlikely (c >= 0xd800 && c < 0xdc00)) {
621    /* high surrogate */
622    uint16_t l;
623    if (text < end && ((l = *text), unlikely (l >= 0xdc00 && l < 0xe000))) {
624      /* low surrogate */
625      *unicode = ((hb_codepoint_t) ((c) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000);
626       text++;
627    } else
628      *unicode = -1;
629  } else
630    *unicode = c;
631
632  return text;
633}
634
635void
636hb_buffer_add_utf16 (hb_buffer_t    *buffer,
637		     const uint16_t *text,
638		     unsigned int    text_length HB_UNUSED,
639		     unsigned int    item_offset,
640		     unsigned int    item_length)
641{
642#define UTF_NEXT(S, E, U)	hb_utf16_next (S, E, &(U))
643  ADD_UTF (uint16_t);
644#undef UTF_NEXT
645}
646
647void
648hb_buffer_add_utf32 (hb_buffer_t    *buffer,
649		     const uint32_t *text,
650		     unsigned int    text_length HB_UNUSED,
651		     unsigned int    item_offset,
652		     unsigned int    item_length)
653{
654#define UTF_NEXT(S, E, U)	((U) = *(S), (S)+1)
655  ADD_UTF (uint32_t);
656#undef UTF_NEXT
657}
658
659
660HB_END_DECLS
661