hb-buffer.cc revision 2fc56edff6d64f190271454ccb1b5fd347d4f172
1a2a9a023f6472ba262f89e5327318996b8258d25Behdad Esfahbod/*
2a2a9a023f6472ba262f89e5327318996b8258d25Behdad Esfahbod * Copyright (C) 1998-2004  David Turner and Werner Lemberg
322da7fd94d6318c52df69d70470a85464ffc533dBehdad Esfahbod * Copyright (C) 2004,2007,2009,2010  Red Hat, Inc.
4c910bec863215f918c659f58debbc7fe5264d7b6Behdad Esfahbod * Copyright (C) 2011  Google, Inc.
59f8da38cd108590514b71756b752d98952a9221fBehdad Esfahbod *
6c755cb3e3ac55156d0d2ec05adea7a650b97cc41Behdad Esfahbod * This is part of HarfBuzz, a text shaping library.
79f8da38cd108590514b71756b752d98952a9221fBehdad Esfahbod *
8a2a9a023f6472ba262f89e5327318996b8258d25Behdad Esfahbod * Permission is hereby granted, without written agreement and without
9a2a9a023f6472ba262f89e5327318996b8258d25Behdad Esfahbod * license or royalty fees, to use, copy, modify, and distribute this
10a2a9a023f6472ba262f89e5327318996b8258d25Behdad Esfahbod * software and its documentation for any purpose, provided that the
11a2a9a023f6472ba262f89e5327318996b8258d25Behdad Esfahbod * above copyright notice and the following two paragraphs appear in
12a2a9a023f6472ba262f89e5327318996b8258d25Behdad Esfahbod * all copies of this software.
13a2a9a023f6472ba262f89e5327318996b8258d25Behdad Esfahbod *
14a2a9a023f6472ba262f89e5327318996b8258d25Behdad Esfahbod * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
15a2a9a023f6472ba262f89e5327318996b8258d25Behdad Esfahbod * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16a2a9a023f6472ba262f89e5327318996b8258d25Behdad Esfahbod * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
17a2a9a023f6472ba262f89e5327318996b8258d25Behdad Esfahbod * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18a2a9a023f6472ba262f89e5327318996b8258d25Behdad Esfahbod * DAMAGE.
19a2a9a023f6472ba262f89e5327318996b8258d25Behdad Esfahbod *
20a2a9a023f6472ba262f89e5327318996b8258d25Behdad Esfahbod * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
21a2a9a023f6472ba262f89e5327318996b8258d25Behdad Esfahbod * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
22a2a9a023f6472ba262f89e5327318996b8258d25Behdad Esfahbod * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
23a2a9a023f6472ba262f89e5327318996b8258d25Behdad Esfahbod * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
24a2a9a023f6472ba262f89e5327318996b8258d25Behdad Esfahbod * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25a2a9a023f6472ba262f89e5327318996b8258d25Behdad Esfahbod *
26a2a9a023f6472ba262f89e5327318996b8258d25Behdad Esfahbod * Red Hat Author(s): Owen Taylor, Behdad Esfahbod
27c910bec863215f918c659f58debbc7fe5264d7b6Behdad Esfahbod * Google Author(s): Behdad Esfahbod
289f8da38cd108590514b71756b752d98952a9221fBehdad Esfahbod */
299f8da38cd108590514b71756b752d98952a9221fBehdad Esfahbod
3022da7fd94d6318c52df69d70470a85464ffc533dBehdad Esfahbod#include "hb-buffer-private.hh"
319f8da38cd108590514b71756b752d98952a9221fBehdad Esfahbod
3288a5f5a49b6809d88560791f9cf6b8f78f22a4adBehdad Esfahbod#include <string.h>
3388a5f5a49b6809d88560791f9cf6b8f78f22a4adBehdad Esfahbod
34acdba3f90b232fc12fcb200dca2584481b339118Behdad EsfahbodHB_BEGIN_DECLS
35acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
36f1322e52d557726baa010be8d35a594748e8fa1aBehdad Esfahbod
3711fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbodstatic hb_buffer_t _hb_buffer_nil = {
385ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod  HB_REFERENCE_COUNT_INVALID, /* ref_count */
395ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod
405ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod  &_hb_unicode_funcs_nil  /* unicode */
4111fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod};
4211fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod
43a8abb8b994c3cd89808e8f7128a0c04b23eb3edeBehdad Esfahbod/* Here is how the buffer works internally:
44a8abb8b994c3cd89808e8f7128a0c04b23eb3edeBehdad Esfahbod *
45910a33fe8457a8e13f7eb77fc92fa59c31f5e8fdBehdad Esfahbod * There are two info pointers: info and out_info.  They always have
46910a33fe8457a8e13f7eb77fc92fa59c31f5e8fdBehdad Esfahbod * the same allocated size, but different lengths.
47a8abb8b994c3cd89808e8f7128a0c04b23eb3edeBehdad Esfahbod *
489d5e26df0877aa5b187764ba09bd7bf221e92968Behdad Esfahbod * As an optimization, both info and out_info may point to the
497e7007a1c9bf2c07a8369752126ece8fa6164248Behdad Esfahbod * same piece of memory, which is owned by info.  This remains the
50cc1a8a938b4c13e76b58825a9e1951c4134e634aBehdad Esfahbod * case as long as out_len doesn't exceed i at any time.
51910a33fe8457a8e13f7eb77fc92fa59c31f5e8fdBehdad Esfahbod * In that case, swap() is no-op and the glyph operations operate
52910a33fe8457a8e13f7eb77fc92fa59c31f5e8fdBehdad Esfahbod * mostly in-place.
53a8abb8b994c3cd89808e8f7128a0c04b23eb3edeBehdad Esfahbod *
549d5e26df0877aa5b187764ba09bd7bf221e92968Behdad Esfahbod * As soon as out_info gets longer than info, out_info is moved over
55910a33fe8457a8e13f7eb77fc92fa59c31f5e8fdBehdad Esfahbod * to an alternate buffer (which we reuse the pos buffer for!), and its
56910a33fe8457a8e13f7eb77fc92fa59c31f5e8fdBehdad Esfahbod * current contents (out_len entries) are copied to the new place.
57910a33fe8457a8e13f7eb77fc92fa59c31f5e8fdBehdad Esfahbod * This should all remain transparent to the user.  swap() then
58910a33fe8457a8e13f7eb77fc92fa59c31f5e8fdBehdad Esfahbod * switches info and out_info.
59a8abb8b994c3cd89808e8f7128a0c04b23eb3edeBehdad Esfahbod */
60a8abb8b994c3cd89808e8f7128a0c04b23eb3edeBehdad Esfahbod
61c968fc2dc87cf85b53f60a40db59d5ee7b992edfBehdad Esfahbod
623567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbodstatic hb_bool_t
633567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod_hb_buffer_enlarge (hb_buffer_t *buffer, unsigned int size)
643567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod{
653567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod  if (unlikely (buffer->in_error))
663567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod    return FALSE;
673567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod
683567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod  unsigned int new_allocated = buffer->allocated;
6988474c6fdaf35c56368694a5b164f4988a004d49Behdad Esfahbod  hb_glyph_position_t *new_pos;
7088474c6fdaf35c56368694a5b164f4988a004d49Behdad Esfahbod  hb_glyph_info_t *new_info;
713567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod  bool separate_out;
723567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod
733567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod  separate_out = buffer->out_info != buffer->info;
743567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod
753567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod  while (size > new_allocated)
763567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod    new_allocated += (new_allocated >> 1) + 8;
773567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod
78b5dd44e24669cd35affcd92788d39ff56cac94dbBehdad Esfahbod  ASSERT_STATIC (sizeof (buffer->info[0]) == sizeof (buffer->pos[0]));
79b5dd44e24669cd35affcd92788d39ff56cac94dbBehdad Esfahbod  bool overflows = new_allocated >= ((unsigned int) -1) / sizeof (buffer->info[0]);
80b5dd44e24669cd35affcd92788d39ff56cac94dbBehdad Esfahbod
81b5dd44e24669cd35affcd92788d39ff56cac94dbBehdad Esfahbod  if (unlikely (overflows)) {
82b5dd44e24669cd35affcd92788d39ff56cac94dbBehdad Esfahbod    new_pos = NULL;
83b5dd44e24669cd35affcd92788d39ff56cac94dbBehdad Esfahbod    new_info = NULL;
84b5dd44e24669cd35affcd92788d39ff56cac94dbBehdad Esfahbod  } else {
85b5dd44e24669cd35affcd92788d39ff56cac94dbBehdad Esfahbod    new_pos = (hb_glyph_position_t *) realloc (buffer->pos, new_allocated * sizeof (buffer->pos[0]));
86b5dd44e24669cd35affcd92788d39ff56cac94dbBehdad Esfahbod    new_info = (hb_glyph_info_t *) realloc (buffer->info, new_allocated * sizeof (buffer->info[0]));
87b5dd44e24669cd35affcd92788d39ff56cac94dbBehdad Esfahbod  }
883567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod
893567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod  if (unlikely (!new_pos || !new_info))
903567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod    buffer->in_error = TRUE;
913567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod
923567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod  if (likely (new_pos))
933567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod    buffer->pos = new_pos;
943567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod
953567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod  if (likely (new_info))
963567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod    buffer->info = new_info;
973567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod
9888474c6fdaf35c56368694a5b164f4988a004d49Behdad Esfahbod  buffer->out_info = separate_out ? (hb_glyph_info_t *) buffer->pos : buffer->info;
993567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod  if (likely (!buffer->in_error))
1003567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod    buffer->allocated = new_allocated;
1013567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod
1023567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod  return likely (!buffer->in_error);
1033567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod}
1043567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod
1053567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbodstatic inline hb_bool_t
1063567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod_hb_buffer_ensure (hb_buffer_t *buffer, unsigned int size)
1073567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod{
1083567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod  return likely (size <= buffer->allocated) ? TRUE : _hb_buffer_enlarge (buffer, size);
1093567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod}
1106b347138b597c41af24453f630336ba2fc033dc5Behdad Esfahbod
111acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbodstatic inline hb_bool_t
1123567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod_hb_buffer_ensure_separate (hb_buffer_t *buffer, unsigned int size)
113c968fc2dc87cf85b53f60a40db59d5ee7b992edfBehdad Esfahbod{
1143567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod  if (unlikely (!_hb_buffer_ensure (buffer, size))) return FALSE;
115a6a79df5fe2ed2cd307e7a991346faee164e70d9Behdad Esfahbod
1169d5e26df0877aa5b187764ba09bd7bf221e92968Behdad Esfahbod  if (buffer->out_info == buffer->info)
117e35bbd570a5d914f86f1ea83941ee4328f268059Behdad Esfahbod  {
118e4679d9fae43f3219c476c5b9e411d1f5d0d5baeBehdad Esfahbod    assert (buffer->have_output);
119e35bbd570a5d914f86f1ea83941ee4328f268059Behdad Esfahbod
12088474c6fdaf35c56368694a5b164f4988a004d49Behdad Esfahbod    buffer->out_info = (hb_glyph_info_t *) buffer->pos;
12129427c5c51ac70aca53ed523fa5ddb3de4355fb0Behdad Esfahbod    memcpy (buffer->out_info, buffer->info, buffer->out_len * sizeof (buffer->out_info[0]));
122e35bbd570a5d914f86f1ea83941ee4328f268059Behdad Esfahbod  }
123a6a79df5fe2ed2cd307e7a991346faee164e70d9Behdad Esfahbod
124a6a79df5fe2ed2cd307e7a991346faee164e70d9Behdad Esfahbod  return TRUE;
125a8abb8b994c3cd89808e8f7128a0c04b23eb3edeBehdad Esfahbod}
126a8abb8b994c3cd89808e8f7128a0c04b23eb3edeBehdad Esfahbod
1273567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod
1286b347138b597c41af24453f630336ba2fc033dc5Behdad Esfahbod/* Public API */
1296b347138b597c41af24453f630336ba2fc033dc5Behdad Esfahbod
130b857b49c82782d29d6d189f1a9f4a84d39cd84eaBehdad Esfahbodhb_buffer_t *
13111fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbodhb_buffer_create (unsigned int pre_alloc_size)
1329f8da38cd108590514b71756b752d98952a9221fBehdad Esfahbod{
133b857b49c82782d29d6d189f1a9f4a84d39cd84eaBehdad Esfahbod  hb_buffer_t *buffer;
134b857b49c82782d29d6d189f1a9f4a84d39cd84eaBehdad Esfahbod
1355fc22e647c8a2bf6d3cb59185e351ac625e7e322Behdad Esfahbod  if (!HB_OBJECT_DO_CREATE (hb_buffer_t, buffer))
13611fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod    return &_hb_buffer_nil;
1379f8da38cd108590514b71756b752d98952a9221fBehdad Esfahbod
13811fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod  if (pre_alloc_size)
1393567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod    _hb_buffer_ensure (buffer, pre_alloc_size);
1409f8da38cd108590514b71756b752d98952a9221fBehdad Esfahbod
1415ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod  buffer->unicode = &_hb_unicode_funcs_nil;
1425ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod
14311fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod  return buffer;
14411fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod}
1457a26864308bd1ca8d5f47d798411cac7239b7d38Behdad Esfahbod
14611fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbodhb_buffer_t *
14711fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbodhb_buffer_reference (hb_buffer_t *buffer)
14811fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod{
14911fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod  HB_OBJECT_DO_REFERENCE (buffer);
15011fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod}
151f9cd1014f8f4d0394b5e0e9eefc1e2af13c59cabBehdad Esfahbod
15211fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbodunsigned int
15311fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbodhb_buffer_get_reference_count (hb_buffer_t *buffer)
15411fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod{
15511fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod  HB_OBJECT_DO_GET_REFERENCE_COUNT (buffer);
156a8abb8b994c3cd89808e8f7128a0c04b23eb3edeBehdad Esfahbod}
1579f8da38cd108590514b71756b752d98952a9221fBehdad Esfahbod
1587a26864308bd1ca8d5f47d798411cac7239b7d38Behdad Esfahbodvoid
15911fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbodhb_buffer_destroy (hb_buffer_t *buffer)
1607a26864308bd1ca8d5f47d798411cac7239b7d38Behdad Esfahbod{
16111fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod  HB_OBJECT_DO_DESTROY (buffer);
16211fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod
1635ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod  hb_unicode_funcs_destroy (buffer->unicode);
1645ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod
1657e7007a1c9bf2c07a8369752126ece8fa6164248Behdad Esfahbod  free (buffer->info);
1661b621823f3e31b48c80cc8b0691dfa873ba086cdBehdad Esfahbod  free (buffer->pos);
16711fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod
168b857b49c82782d29d6d189f1a9f4a84d39cd84eaBehdad Esfahbod  free (buffer);
1697a26864308bd1ca8d5f47d798411cac7239b7d38Behdad Esfahbod}
1707a26864308bd1ca8d5f47d798411cac7239b7d38Behdad Esfahbod
1715ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod
1725ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbodvoid
1735ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbodhb_buffer_set_unicode_funcs (hb_buffer_t        *buffer,
1745ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod			     hb_unicode_funcs_t *unicode)
1755ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod{
1760465e69832393cc1ed36508ec5d597fbab64877aBehdad Esfahbod  if (!unicode)
1770465e69832393cc1ed36508ec5d597fbab64877aBehdad Esfahbod    unicode = &_hb_unicode_funcs_nil;
1780465e69832393cc1ed36508ec5d597fbab64877aBehdad Esfahbod
1795ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod  hb_unicode_funcs_reference (unicode);
1805ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod  hb_unicode_funcs_destroy (buffer->unicode);
1815ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod  buffer->unicode = unicode;
1825ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod}
1835ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod
1845ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbodhb_unicode_funcs_t *
1855ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbodhb_buffer_get_unicode_funcs (hb_buffer_t        *buffer)
1865ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod{
1875ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod  return buffer->unicode;
1885ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod}
1895ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod
1905ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbodvoid
1915ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbodhb_buffer_set_direction (hb_buffer_t    *buffer,
1925ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod			 hb_direction_t  direction)
1935ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod
1945ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod{
1954e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod  buffer->props.direction = direction;
1965ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod}
1975ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod
1985ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbodhb_direction_t
1995ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbodhb_buffer_get_direction (hb_buffer_t    *buffer)
2005ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod{
2014e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod  return buffer->props.direction;
2025ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod}
2035ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod
204ae070b7d39d03bd8bc1244f687b24db505f4af3fBehdad Esfahbodvoid
205ae070b7d39d03bd8bc1244f687b24db505f4af3fBehdad Esfahbodhb_buffer_set_script (hb_buffer_t *buffer,
206ae070b7d39d03bd8bc1244f687b24db505f4af3fBehdad Esfahbod		      hb_script_t  script)
207ae070b7d39d03bd8bc1244f687b24db505f4af3fBehdad Esfahbod{
2084e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod  buffer->props.script = script;
209ae070b7d39d03bd8bc1244f687b24db505f4af3fBehdad Esfahbod}
210ae070b7d39d03bd8bc1244f687b24db505f4af3fBehdad Esfahbod
211ae070b7d39d03bd8bc1244f687b24db505f4af3fBehdad Esfahbodhb_script_t
212ae070b7d39d03bd8bc1244f687b24db505f4af3fBehdad Esfahbodhb_buffer_get_script (hb_buffer_t *buffer)
213ae070b7d39d03bd8bc1244f687b24db505f4af3fBehdad Esfahbod{
2144e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod  return buffer->props.script;
215ae070b7d39d03bd8bc1244f687b24db505f4af3fBehdad Esfahbod}
216ae070b7d39d03bd8bc1244f687b24db505f4af3fBehdad Esfahbod
217ae070b7d39d03bd8bc1244f687b24db505f4af3fBehdad Esfahbodvoid
218ae070b7d39d03bd8bc1244f687b24db505f4af3fBehdad Esfahbodhb_buffer_set_language (hb_buffer_t   *buffer,
219ae070b7d39d03bd8bc1244f687b24db505f4af3fBehdad Esfahbod			hb_language_t  language)
220ae070b7d39d03bd8bc1244f687b24db505f4af3fBehdad Esfahbod{
2214e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod  buffer->props.language = language;
222ae070b7d39d03bd8bc1244f687b24db505f4af3fBehdad Esfahbod}
223ae070b7d39d03bd8bc1244f687b24db505f4af3fBehdad Esfahbod
224ae070b7d39d03bd8bc1244f687b24db505f4af3fBehdad Esfahbodhb_language_t
225ae070b7d39d03bd8bc1244f687b24db505f4af3fBehdad Esfahbodhb_buffer_get_language (hb_buffer_t *buffer)
226ae070b7d39d03bd8bc1244f687b24db505f4af3fBehdad Esfahbod{
2274e4ef24e46f273ad2bdda2f718223e05b37dd50fBehdad Esfahbod  return buffer->props.language;
228ae070b7d39d03bd8bc1244f687b24db505f4af3fBehdad Esfahbod}
229ae070b7d39d03bd8bc1244f687b24db505f4af3fBehdad Esfahbod
2305ebabecef382c3e8b0a2a5657b2c01f7ff37d796Behdad Esfahbod
2317a26864308bd1ca8d5f47d798411cac7239b7d38Behdad Esfahbodvoid
232c910bec863215f918c659f58debbc7fe5264d7b6Behdad Esfahbodhb_buffer_reset (hb_buffer_t *buffer)
233c910bec863215f918c659f58debbc7fe5264d7b6Behdad Esfahbod{
234e4679d9fae43f3219c476c5b9e411d1f5d0d5baeBehdad Esfahbod  buffer->have_output = FALSE;
235314905d7548d5be58354546d660754b807b6efb2Behdad Esfahbod  buffer->have_positions = FALSE;
236a6a79df5fe2ed2cd307e7a991346faee164e70d9Behdad Esfahbod  buffer->in_error = FALSE;
2376960350be97f24e97140391025b56369c393a3dfBehdad Esfahbod  buffer->len = 0;
23829427c5c51ac70aca53ed523fa5ddb3de4355fb0Behdad Esfahbod  buffer->out_len = 0;
23936b73c80df91e96492357c6da945e081e9046a93Behdad Esfahbod  buffer->i = 0;
2409d5e26df0877aa5b187764ba09bd7bf221e92968Behdad Esfahbod  buffer->out_info = buffer->info;
241dbf56b1d94910f04823e53e39ace1e5145bddc04Behdad Esfahbod  buffer->serial = 0;
2422fc56edff6d64f190271454ccb1b5fd347d4f172Behdad Esfahbod
2432fc56edff6d64f190271454ccb1b5fd347d4f172Behdad Esfahbod  buffer->props = _hb_buffer_nil.props;
2442fc56edff6d64f190271454ccb1b5fd347d4f172Behdad Esfahbod
2452fc56edff6d64f190271454ccb1b5fd347d4f172Behdad Esfahbod  hb_unicode_funcs_destroy (buffer->unicode);
2462fc56edff6d64f190271454ccb1b5fd347d4f172Behdad Esfahbod  buffer->unicode = _hb_buffer_nil.unicode;
2477a26864308bd1ca8d5f47d798411cac7239b7d38Behdad Esfahbod}
2487a26864308bd1ca8d5f47d798411cac7239b7d38Behdad Esfahbod
249a6a79df5fe2ed2cd307e7a991346faee164e70d9Behdad Esfahbodhb_bool_t
250f9cd1014f8f4d0394b5e0e9eefc1e2af13c59cabBehdad Esfahbodhb_buffer_ensure (hb_buffer_t *buffer, unsigned int size)
251f9cd1014f8f4d0394b5e0e9eefc1e2af13c59cabBehdad Esfahbod{
2523567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod  return _hb_buffer_ensure (buffer, size);
253f9cd1014f8f4d0394b5e0e9eefc1e2af13c59cabBehdad Esfahbod}
254f9cd1014f8f4d0394b5e0e9eefc1e2af13c59cabBehdad Esfahbod
255f9cd1014f8f4d0394b5e0e9eefc1e2af13c59cabBehdad Esfahbodvoid
2563015c4175179a1816aad2a4950da9a3b8baf2578Behdad Esfahbodhb_buffer_add_glyph (hb_buffer_t    *buffer,
257f1322e52d557726baa010be8d35a594748e8fa1aBehdad Esfahbod		     hb_codepoint_t  codepoint,
258468769b8f5332940278244e744ec2bd5a5dc5ee9Behdad Esfahbod		     hb_mask_t       mask,
2593015c4175179a1816aad2a4950da9a3b8baf2578Behdad Esfahbod		     unsigned int    cluster)
2606b347138b597c41af24453f630336ba2fc033dc5Behdad Esfahbod{
26188474c6fdaf35c56368694a5b164f4988a004d49Behdad Esfahbod  hb_glyph_info_t *glyph;
2625c0adce1ccc739415c4b26ff13ffd2d77ea4bc6cBehdad Esfahbod
2633567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod  if (unlikely (!_hb_buffer_ensure (buffer, buffer->len + 1))) return;
2646b347138b597c41af24453f630336ba2fc033dc5Behdad Esfahbod
2656960350be97f24e97140391025b56369c393a3dfBehdad Esfahbod  glyph = &buffer->info[buffer->len];
266f6a23a0b9171958f76c1d0473b09fc08d2b3a0d0Behdad Esfahbod
267f6a23a0b9171958f76c1d0473b09fc08d2b3a0d0Behdad Esfahbod  memset (glyph, 0, sizeof (*glyph));
268f1322e52d557726baa010be8d35a594748e8fa1aBehdad Esfahbod  glyph->codepoint = codepoint;
269468769b8f5332940278244e744ec2bd5a5dc5ee9Behdad Esfahbod  glyph->mask = mask;
2706b347138b597c41af24453f630336ba2fc033dc5Behdad Esfahbod  glyph->cluster = cluster;
2716b347138b597c41af24453f630336ba2fc033dc5Behdad Esfahbod
2726960350be97f24e97140391025b56369c393a3dfBehdad Esfahbod  buffer->len++;
2736b347138b597c41af24453f630336ba2fc033dc5Behdad Esfahbod}
2746b347138b597c41af24453f630336ba2fc033dc5Behdad Esfahbod
27511fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbodvoid
27611fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbodhb_buffer_clear_positions (hb_buffer_t *buffer)
27706003908ccf2473366816935dd1b144cde587be9Behdad Esfahbod{
27815c3e75b39797a153b6bc0598f87b27c4a487228Behdad Esfahbod  _hb_buffer_clear_output (buffer);
279e4679d9fae43f3219c476c5b9e411d1f5d0d5baeBehdad Esfahbod  buffer->have_output = FALSE;
280314905d7548d5be58354546d660754b807b6efb2Behdad Esfahbod  buffer->have_positions = TRUE;
28115c3e75b39797a153b6bc0598f87b27c4a487228Behdad Esfahbod
2826960350be97f24e97140391025b56369c393a3dfBehdad Esfahbod  memset (buffer->pos, 0, sizeof (buffer->pos[0]) * buffer->len);
28306003908ccf2473366816935dd1b144cde587be9Behdad Esfahbod}
28406003908ccf2473366816935dd1b144cde587be9Behdad Esfahbod
2853567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod/* HarfBuzz-Internal API */
2863567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod
2873567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbodvoid
2883567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod_hb_buffer_clear_output (hb_buffer_t *buffer)
2893567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod{
2903567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod  buffer->have_output = TRUE;
2913567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod  buffer->have_positions = FALSE;
2923567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod  buffer->out_len = 0;
2933567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod  buffer->out_info = buffer->info;
2943567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod}
2953567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod
296081819ea8b98c0a4b4dffe8d4aca3512f9798719Behdad Esfahbodvoid
297c968fc2dc87cf85b53f60a40db59d5ee7b992edfBehdad Esfahbod_hb_buffer_swap (hb_buffer_t *buffer)
2989f8da38cd108590514b71756b752d98952a9221fBehdad Esfahbod{
299c968fc2dc87cf85b53f60a40db59d5ee7b992edfBehdad Esfahbod  unsigned int tmp;
3009f8da38cd108590514b71756b752d98952a9221fBehdad Esfahbod
301e4679d9fae43f3219c476c5b9e411d1f5d0d5baeBehdad Esfahbod  assert (buffer->have_output);
302e4679d9fae43f3219c476c5b9e411d1f5d0d5baeBehdad Esfahbod
303a6a79df5fe2ed2cd307e7a991346faee164e70d9Behdad Esfahbod  if (unlikely (buffer->in_error)) return;
304a6a79df5fe2ed2cd307e7a991346faee164e70d9Behdad Esfahbod
3059d5e26df0877aa5b187764ba09bd7bf221e92968Behdad Esfahbod  if (buffer->out_info != buffer->info)
306e35bbd570a5d914f86f1ea83941ee4328f268059Behdad Esfahbod  {
30788474c6fdaf35c56368694a5b164f4988a004d49Behdad Esfahbod    hb_glyph_info_t *tmp_string;
3087e7007a1c9bf2c07a8369752126ece8fa6164248Behdad Esfahbod    tmp_string = buffer->info;
3099d5e26df0877aa5b187764ba09bd7bf221e92968Behdad Esfahbod    buffer->info = buffer->out_info;
3109d5e26df0877aa5b187764ba09bd7bf221e92968Behdad Esfahbod    buffer->out_info = tmp_string;
31188474c6fdaf35c56368694a5b164f4988a004d49Behdad Esfahbod    buffer->pos = (hb_glyph_position_t *) buffer->out_info;
312e35bbd570a5d914f86f1ea83941ee4328f268059Behdad Esfahbod  }
3139f8da38cd108590514b71756b752d98952a9221fBehdad Esfahbod
3146960350be97f24e97140391025b56369c393a3dfBehdad Esfahbod  tmp = buffer->len;
31529427c5c51ac70aca53ed523fa5ddb3de4355fb0Behdad Esfahbod  buffer->len = buffer->out_len;
31629427c5c51ac70aca53ed523fa5ddb3de4355fb0Behdad Esfahbod  buffer->out_len = tmp;
3179f8da38cd108590514b71756b752d98952a9221fBehdad Esfahbod
31836b73c80df91e96492357c6da945e081e9046a93Behdad Esfahbod  buffer->i = 0;
3199f8da38cd108590514b71756b752d98952a9221fBehdad Esfahbod}
3209f8da38cd108590514b71756b752d98952a9221fBehdad Esfahbod
321081819ea8b98c0a4b4dffe8d4aca3512f9798719Behdad Esfahbodvoid
322dd2ffd282c059194fd87fb1664e2e0cdb56a87a0Behdad Esfahbod_hb_buffer_replace_glyphs_be16 (hb_buffer_t *buffer,
323dd2ffd282c059194fd87fb1664e2e0cdb56a87a0Behdad Esfahbod				unsigned int num_in,
324dd2ffd282c059194fd87fb1664e2e0cdb56a87a0Behdad Esfahbod				unsigned int num_out,
325dd2ffd282c059194fd87fb1664e2e0cdb56a87a0Behdad Esfahbod				const uint16_t *glyph_data_be)
32625e7ef704633447f109b148620336c42d6fb310eBehdad Esfahbod{
3279d5e26df0877aa5b187764ba09bd7bf221e92968Behdad Esfahbod  if (buffer->out_info != buffer->info ||
32836b73c80df91e96492357c6da945e081e9046a93Behdad Esfahbod      buffer->out_len + num_out > buffer->i + num_in)
32925e7ef704633447f109b148620336c42d6fb310eBehdad Esfahbod  {
3303567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod    if (unlikely (!_hb_buffer_ensure_separate (buffer, buffer->out_len + num_out)))
331a6a79df5fe2ed2cd307e7a991346faee164e70d9Behdad Esfahbod      return;
33225e7ef704633447f109b148620336c42d6fb310eBehdad Esfahbod  }
33325e7ef704633447f109b148620336c42d6fb310eBehdad Esfahbod
334fe263272a2b26204bc39829a94d90ab537517f3fBehdad Esfahbod  hb_glyph_info_t orig_info = buffer->info[buffer->i];
33525e7ef704633447f109b148620336c42d6fb310eBehdad Esfahbod
336fe263272a2b26204bc39829a94d90ab537517f3fBehdad Esfahbod  for (unsigned int i = 0; i < num_out; i++)
33725e7ef704633447f109b148620336c42d6fb310eBehdad Esfahbod  {
33888474c6fdaf35c56368694a5b164f4988a004d49Behdad Esfahbod    hb_glyph_info_t *info = &buffer->out_info[buffer->out_len + i];
339fe263272a2b26204bc39829a94d90ab537517f3fBehdad Esfahbod    *info = orig_info;
34025e7ef704633447f109b148620336c42d6fb310eBehdad Esfahbod    info->codepoint = hb_be_uint16 (glyph_data_be[i]);
34125e7ef704633447f109b148620336c42d6fb310eBehdad Esfahbod  }
34225e7ef704633447f109b148620336c42d6fb310eBehdad Esfahbod
34336b73c80df91e96492357c6da945e081e9046a93Behdad Esfahbod  buffer->i  += num_in;
34429427c5c51ac70aca53ed523fa5ddb3de4355fb0Behdad Esfahbod  buffer->out_len += num_out;
34525e7ef704633447f109b148620336c42d6fb310eBehdad Esfahbod}
34688a5f5a49b6809d88560791f9cf6b8f78f22a4adBehdad Esfahbod
347081819ea8b98c0a4b4dffe8d4aca3512f9798719Behdad Esfahbodvoid
348dd2ffd282c059194fd87fb1664e2e0cdb56a87a0Behdad Esfahbod_hb_buffer_replace_glyph (hb_buffer_t *buffer,
349dd2ffd282c059194fd87fb1664e2e0cdb56a87a0Behdad Esfahbod			  hb_codepoint_t glyph_index)
3509f8da38cd108590514b71756b752d98952a9221fBehdad Esfahbod{
35188474c6fdaf35c56368694a5b164f4988a004d49Behdad Esfahbod  hb_glyph_info_t *info;
352c968fc2dc87cf85b53f60a40db59d5ee7b992edfBehdad Esfahbod
3539d5e26df0877aa5b187764ba09bd7bf221e92968Behdad Esfahbod  if (buffer->out_info != buffer->info)
354e35bbd570a5d914f86f1ea83941ee4328f268059Behdad Esfahbod  {
3553567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod    if (unlikely (!_hb_buffer_ensure (buffer, buffer->out_len + 1))) return;
35636b73c80df91e96492357c6da945e081e9046a93Behdad Esfahbod    buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
357e35bbd570a5d914f86f1ea83941ee4328f268059Behdad Esfahbod  }
35836b73c80df91e96492357c6da945e081e9046a93Behdad Esfahbod  else if (buffer->out_len != buffer->i)
35936b73c80df91e96492357c6da945e081e9046a93Behdad Esfahbod    buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
3609f8da38cd108590514b71756b752d98952a9221fBehdad Esfahbod
36129427c5c51ac70aca53ed523fa5ddb3de4355fb0Behdad Esfahbod  info = &buffer->out_info[buffer->out_len];
362f1322e52d557726baa010be8d35a594748e8fa1aBehdad Esfahbod  info->codepoint = glyph_index;
363c968fc2dc87cf85b53f60a40db59d5ee7b992edfBehdad Esfahbod
36436b73c80df91e96492357c6da945e081e9046a93Behdad Esfahbod  buffer->i++;
36529427c5c51ac70aca53ed523fa5ddb3de4355fb0Behdad Esfahbod  buffer->out_len++;
3669f8da38cd108590514b71756b752d98952a9221fBehdad Esfahbod}
3679f8da38cd108590514b71756b752d98952a9221fBehdad Esfahbod
368081819ea8b98c0a4b4dffe8d4aca3512f9798719Behdad Esfahbodvoid
369c968fc2dc87cf85b53f60a40db59d5ee7b992edfBehdad Esfahbod_hb_buffer_next_glyph (hb_buffer_t *buffer)
37015c3e75b39797a153b6bc0598f87b27c4a487228Behdad Esfahbod{
371314905d7548d5be58354546d660754b807b6efb2Behdad Esfahbod  if (buffer->have_output)
372e4679d9fae43f3219c476c5b9e411d1f5d0d5baeBehdad Esfahbod  {
3739d5e26df0877aa5b187764ba09bd7bf221e92968Behdad Esfahbod    if (buffer->out_info != buffer->info)
374314905d7548d5be58354546d660754b807b6efb2Behdad Esfahbod    {
3753567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod      if (unlikely (!_hb_buffer_ensure (buffer, buffer->out_len + 1))) return;
37636b73c80df91e96492357c6da945e081e9046a93Behdad Esfahbod      buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
377314905d7548d5be58354546d660754b807b6efb2Behdad Esfahbod    }
37836b73c80df91e96492357c6da945e081e9046a93Behdad Esfahbod    else if (buffer->out_len != buffer->i)
37936b73c80df91e96492357c6da945e081e9046a93Behdad Esfahbod      buffer->out_info[buffer->out_len] = buffer->info[buffer->i];
380e4679d9fae43f3219c476c5b9e411d1f5d0d5baeBehdad Esfahbod
38129427c5c51ac70aca53ed523fa5ddb3de4355fb0Behdad Esfahbod    buffer->out_len++;
382e35bbd570a5d914f86f1ea83941ee4328f268059Behdad Esfahbod  }
383a8abb8b994c3cd89808e8f7128a0c04b23eb3edeBehdad Esfahbod
38436b73c80df91e96492357c6da945e081e9046a93Behdad Esfahbod  buffer->i++;
385a8abb8b994c3cd89808e8f7128a0c04b23eb3edeBehdad Esfahbod}
386a8abb8b994c3cd89808e8f7128a0c04b23eb3edeBehdad Esfahbod
3871ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbodvoid
38857ac0ecb7843533b2e6e6d6c8a12b2a44437cc1cBehdad Esfahbod_hb_buffer_reset_masks (hb_buffer_t *buffer,
38957ac0ecb7843533b2e6e6d6c8a12b2a44437cc1cBehdad Esfahbod			hb_mask_t    mask)
3901ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod{
3911ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod  unsigned int count = buffer->len;
3921ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod  for (unsigned int i = 0; i < count; i++)
393961f9baa7bc3556f1e4e7135859cebe1351f73a4Behdad Esfahbod    buffer->info[i].mask = mask;
3941ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod}
3951ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod
3961ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbodvoid
39757ac0ecb7843533b2e6e6d6c8a12b2a44437cc1cBehdad Esfahbod_hb_buffer_add_masks (hb_buffer_t *buffer,
398bd7378b2ef9793de4e7f57b920f29f48ac9d0c25Behdad Esfahbod		      hb_mask_t    mask)
399bd7378b2ef9793de4e7f57b920f29f48ac9d0c25Behdad Esfahbod{
400bd7378b2ef9793de4e7f57b920f29f48ac9d0c25Behdad Esfahbod  unsigned int count = buffer->len;
401bd7378b2ef9793de4e7f57b920f29f48ac9d0c25Behdad Esfahbod  for (unsigned int i = 0; i < count; i++)
402bd7378b2ef9793de4e7f57b920f29f48ac9d0c25Behdad Esfahbod    buffer->info[i].mask |= mask;
403bd7378b2ef9793de4e7f57b920f29f48ac9d0c25Behdad Esfahbod}
404bd7378b2ef9793de4e7f57b920f29f48ac9d0c25Behdad Esfahbod
405bd7378b2ef9793de4e7f57b920f29f48ac9d0c25Behdad Esfahbodvoid
406bd7378b2ef9793de4e7f57b920f29f48ac9d0c25Behdad Esfahbod_hb_buffer_set_masks (hb_buffer_t *buffer,
40781c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod		      hb_mask_t    value,
40881c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod		      hb_mask_t    mask,
40981c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod		      unsigned int cluster_start,
41081c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod		      unsigned int cluster_end)
4111ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod{
41281c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod  hb_mask_t not_mask = ~mask;
4135c1c8c9c50ddbe66ea595afb245a208b7775b27cBehdad Esfahbod  value &= mask;
41481c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod
4153506b2e78db27e7835bd2c09c053a9807c9cac40Behdad Esfahbod  if (!mask)
4163506b2e78db27e7835bd2c09c053a9807c9cac40Behdad Esfahbod    return;
4173506b2e78db27e7835bd2c09c053a9807c9cac40Behdad Esfahbod
4181ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod  if (cluster_start == 0 && cluster_end == (unsigned int)-1) {
4191ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod    unsigned int count = buffer->len;
4201ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod    for (unsigned int i = 0; i < count; i++)
42181c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod      buffer->info[i].mask = (buffer->info[i].mask & not_mask) | value;
4221ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod    return;
4231ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod  }
4241ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod
42534db6f031d7ac009f554386ef990bad44886b9eeBehdad Esfahbod  /* XXX can't bsearch since .cluster may not be sorted. */
4261ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod  /* Binary search to find the start position and go from there. */
4271ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod  unsigned int min = 0, max = buffer->len;
4281ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod  while (min < max)
4291ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod  {
4301ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod    unsigned int mid = min + ((max - min) / 2);
4311ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod    if (buffer->info[mid].cluster < cluster_start)
4321ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod      min = mid + 1;
4331ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod    else
4341ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod      max = mid;
4351ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod  }
4361ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod  unsigned int count = buffer->len;
4371ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod  for (unsigned int i = min; i < count && buffer->info[i].cluster < cluster_end; i++)
43881c5e8724b740c6e42ed3a45e0574c7c5f3ad8e6Behdad Esfahbod    buffer->info[i].mask = (buffer->info[i].mask & not_mask) | value;
4391ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod}
4401ce7b87c4d8d1ab3ec1d5198351d71b7199f7c64Behdad Esfahbod
44111fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod
4423567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod/* Public API again */
4433567b87cce541dfb0af7caf024ec67c9d3c09214Behdad Esfahbod
444c910bec863215f918c659f58debbc7fe5264d7b6Behdad Esfahbodhb_bool_t
445c910bec863215f918c659f58debbc7fe5264d7b6Behdad Esfahbodhb_buffer_set_length (hb_buffer_t  *buffer,
446c910bec863215f918c659f58debbc7fe5264d7b6Behdad Esfahbod		      unsigned int  length)
447c910bec863215f918c659f58debbc7fe5264d7b6Behdad Esfahbod{
448c910bec863215f918c659f58debbc7fe5264d7b6Behdad Esfahbod  if (!hb_buffer_ensure (buffer, length))
449c910bec863215f918c659f58debbc7fe5264d7b6Behdad Esfahbod    return FALSE;
450c910bec863215f918c659f58debbc7fe5264d7b6Behdad Esfahbod
451c910bec863215f918c659f58debbc7fe5264d7b6Behdad Esfahbod  /* Wipe the new space */
452c910bec863215f918c659f58debbc7fe5264d7b6Behdad Esfahbod  if (length > buffer->len) {
453c910bec863215f918c659f58debbc7fe5264d7b6Behdad Esfahbod    memset (buffer->info + buffer->len, 0, sizeof (buffer->info[0]) * (length - buffer->len));
454c910bec863215f918c659f58debbc7fe5264d7b6Behdad Esfahbod    if (buffer->have_positions)
455c910bec863215f918c659f58debbc7fe5264d7b6Behdad Esfahbod      memset (buffer->pos + buffer->len, 0, sizeof (buffer->pos[0]) * (length - buffer->len));
456c910bec863215f918c659f58debbc7fe5264d7b6Behdad Esfahbod  }
457c910bec863215f918c659f58debbc7fe5264d7b6Behdad Esfahbod
458c910bec863215f918c659f58debbc7fe5264d7b6Behdad Esfahbod  buffer->len = length;
459c910bec863215f918c659f58debbc7fe5264d7b6Behdad Esfahbod  return TRUE;
460c910bec863215f918c659f58debbc7fe5264d7b6Behdad Esfahbod}
461c910bec863215f918c659f58debbc7fe5264d7b6Behdad Esfahbod
46211fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbodunsigned int
4633d14528b8b2e7da425a9df7057fc9fb326d8298cBehdad Esfahbodhb_buffer_get_length (hb_buffer_t *buffer)
46411fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod{
4656960350be97f24e97140391025b56369c393a3dfBehdad Esfahbod  return buffer->len;
46611fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod}
46711fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod
46811fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod/* Return value valid as long as buffer not modified */
46911fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbodhb_glyph_info_t *
47011fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbodhb_buffer_get_glyph_infos (hb_buffer_t *buffer)
47111fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod{
4727e7007a1c9bf2c07a8369752126ece8fa6164248Behdad Esfahbod  return (hb_glyph_info_t *) buffer->info;
47311fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod}
47411fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod
47511fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod/* Return value valid as long as buffer not modified */
47611fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbodhb_glyph_position_t *
47711fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbodhb_buffer_get_glyph_positions (hb_buffer_t *buffer)
47811fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod{
479314905d7548d5be58354546d660754b807b6efb2Behdad Esfahbod  if (!buffer->have_positions)
48011fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod    hb_buffer_clear_positions (buffer);
48111fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod
4821b621823f3e31b48c80cc8b0691dfa873ba086cdBehdad Esfahbod  return (hb_glyph_position_t *) buffer->pos;
48311fbb5487d9900a019440ef8235f35c9f525decbBehdad Esfahbod}
484fbaf8ffa098bd2b6fb4f4bc2d04b360a319c4af5Behdad Esfahbod
485fbaf8ffa098bd2b6fb4f4bc2d04b360a319c4af5Behdad Esfahbod
486ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbodstatic void
487ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbodreverse_range (hb_buffer_t *buffer,
488ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbod	       unsigned int start,
489ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbod	       unsigned int end)
490fbaf8ffa098bd2b6fb4f4bc2d04b360a319c4af5Behdad Esfahbod{
491fbaf8ffa098bd2b6fb4f4bc2d04b360a319c4af5Behdad Esfahbod  unsigned int i, j;
492fbaf8ffa098bd2b6fb4f4bc2d04b360a319c4af5Behdad Esfahbod
493ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbod  for (i = start, j = end - 1; i < j; i++, j--) {
49488474c6fdaf35c56368694a5b164f4988a004d49Behdad Esfahbod    hb_glyph_info_t t;
495fbaf8ffa098bd2b6fb4f4bc2d04b360a319c4af5Behdad Esfahbod
4967e7007a1c9bf2c07a8369752126ece8fa6164248Behdad Esfahbod    t = buffer->info[i];
4977e7007a1c9bf2c07a8369752126ece8fa6164248Behdad Esfahbod    buffer->info[i] = buffer->info[j];
4987e7007a1c9bf2c07a8369752126ece8fa6164248Behdad Esfahbod    buffer->info[j] = t;
499fbaf8ffa098bd2b6fb4f4bc2d04b360a319c4af5Behdad Esfahbod  }
500fbaf8ffa098bd2b6fb4f4bc2d04b360a319c4af5Behdad Esfahbod
5011b621823f3e31b48c80cc8b0691dfa873ba086cdBehdad Esfahbod  if (buffer->pos) {
502ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbod    for (i = 0, j = end - 1; i < j; i++, j--) {
50388474c6fdaf35c56368694a5b164f4988a004d49Behdad Esfahbod      hb_glyph_position_t t;
504fbaf8ffa098bd2b6fb4f4bc2d04b360a319c4af5Behdad Esfahbod
5051b621823f3e31b48c80cc8b0691dfa873ba086cdBehdad Esfahbod      t = buffer->pos[i];
5061b621823f3e31b48c80cc8b0691dfa873ba086cdBehdad Esfahbod      buffer->pos[i] = buffer->pos[j];
5071b621823f3e31b48c80cc8b0691dfa873ba086cdBehdad Esfahbod      buffer->pos[j] = t;
508fbaf8ffa098bd2b6fb4f4bc2d04b360a319c4af5Behdad Esfahbod    }
509fbaf8ffa098bd2b6fb4f4bc2d04b360a319c4af5Behdad Esfahbod  }
510fbaf8ffa098bd2b6fb4f4bc2d04b360a319c4af5Behdad Esfahbod}
511299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod
512ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbodvoid
513ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbodhb_buffer_reverse (hb_buffer_t *buffer)
514ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbod{
5156960350be97f24e97140391025b56369c393a3dfBehdad Esfahbod  if (unlikely (!buffer->len))
516ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbod    return;
517ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbod
5186960350be97f24e97140391025b56369c393a3dfBehdad Esfahbod  reverse_range (buffer, 0, buffer->len);
519ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbod}
520ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbod
521ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbodvoid
522ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbodhb_buffer_reverse_clusters (hb_buffer_t *buffer)
523ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbod{
524ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbod  unsigned int i, start, count, last_cluster;
525ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbod
5266960350be97f24e97140391025b56369c393a3dfBehdad Esfahbod  if (unlikely (!buffer->len))
527ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbod    return;
528ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbod
529ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbod  hb_buffer_reverse (buffer);
530ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbod
5316960350be97f24e97140391025b56369c393a3dfBehdad Esfahbod  count = buffer->len;
532ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbod  start = 0;
5337e7007a1c9bf2c07a8369752126ece8fa6164248Behdad Esfahbod  last_cluster = buffer->info[0].cluster;
534ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbod  for (i = 1; i < count; i++) {
5357e7007a1c9bf2c07a8369752126ece8fa6164248Behdad Esfahbod    if (last_cluster != buffer->info[i].cluster) {
536ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbod      reverse_range (buffer, start, i);
537ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbod      start = i;
5387e7007a1c9bf2c07a8369752126ece8fa6164248Behdad Esfahbod      last_cluster = buffer->info[i].cluster;
539ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbod    }
540ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbod  }
541ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbod  reverse_range (buffer, start, i);
542ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbod}
543ff44f88df2c46920f3ec2384ef321a4c7bb0f6efBehdad Esfahbod
544299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod
545299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod#define ADD_UTF(T) \
546299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod	HB_STMT_START { \
547299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod	  const T *next = (const T *) text + item_offset; \
548299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod	  const T *end = next + item_length; \
549299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod	  while (next < end) { \
550299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod	    hb_codepoint_t u; \
551299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod	    const T *old_next = next; \
552299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod	    next = UTF_NEXT (next, end, u); \
553009aad567863c05ee2ec4a3ee76fe0ee79c767bbBehdad Esfahbod	    hb_buffer_add_glyph (buffer, u, 1,  old_next - (const T *) text); \
554299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod	  } \
555299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod	} HB_STMT_END
556299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod
557299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod
558299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod#define UTF8_COMPUTE(Char, Mask, Len) \
559299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod  if (Char < 128) { Len = 1; Mask = 0x7f; } \
560299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod  else if ((Char & 0xe0) == 0xc0) { Len = 2; Mask = 0x1f; } \
561299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod  else if ((Char & 0xf0) == 0xe0) { Len = 3; Mask = 0x0f; } \
562299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod  else if ((Char & 0xf8) == 0xf0) { Len = 4; Mask = 0x07; } \
563299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod  else Len = 0;
564299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod
565299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbodstatic inline const uint8_t *
566299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbodhb_utf8_next (const uint8_t *text,
567299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod	      const uint8_t *end,
568299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod	      hb_codepoint_t *unicode)
569299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod{
570299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod  uint8_t c = *text;
571299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod  unsigned int mask, len;
572299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod
5732163afbf35044f59dbf449254e65b8c9feb6cdebBehdad Esfahbod  /* TODO check for overlong sequences?  also: optimize? */
5742163afbf35044f59dbf449254e65b8c9feb6cdebBehdad Esfahbod
575299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod  UTF8_COMPUTE (c, mask, len);
57664d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod  if (unlikely (!len || (unsigned int) (end - text) < len)) {
577299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod    *unicode = -1;
578299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod    return text + 1;
579299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod  } else {
580299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod    hb_codepoint_t result;
581299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod    unsigned int i;
582299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod    result = c & mask;
583299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod    for (i = 1; i < len; i++)
584299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod      {
58564d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod	if (unlikely ((text[i] & 0xc0) != 0x80))
586299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod	  {
587299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod	    *unicode = -1;
588299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod	    return text + 1;
589299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod	  }
590299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod	result <<= 6;
591299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod	result |= (text[i] & 0x3f);
592299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod      }
593299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod    *unicode = result;
594299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod    return text + len;
595299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod  }
596299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod}
597299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod
598299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbodvoid
599299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbodhb_buffer_add_utf8 (hb_buffer_t  *buffer,
600299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod		    const char   *text,
60133d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod		    unsigned int  text_length HB_UNUSED,
602299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod		    unsigned int  item_offset,
603299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod		    unsigned int  item_length)
604299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod{
605299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod#define UTF_NEXT(S, E, U)	hb_utf8_next (S, E, &(U))
606299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod  ADD_UTF (uint8_t);
607299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod#undef UTF_NEXT
608299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod}
609299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod
610299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbodstatic inline const uint16_t *
611299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbodhb_utf16_next (const uint16_t *text,
612299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod	       const uint16_t *end,
613299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod	       hb_codepoint_t *unicode)
614299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod{
615299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod  uint16_t c = *text++;
616299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod
61764d3fc8d0dada673245cc8c0b1c12cd849b30997Behdad Esfahbod  if (unlikely (c >= 0xd800 && c < 0xdc00)) {
618299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod    /* high surrogate */
619299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod    uint16_t l;
62069ea23cb5d47dd1cfd3129f68375021ef79bf63bBehdad Esfahbod    if (text < end && ((l = *text), likely (l >= 0xdc00 && l < 0xe000))) {
621299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod      /* low surrogate */
622299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod      *unicode = ((hb_codepoint_t) ((c) - 0xd800) * 0x400 + (l) - 0xdc00 + 0x10000);
623299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod       text++;
624299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod    } else
625299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod      *unicode = -1;
626299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod  } else
627299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod    *unicode = c;
628299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod
629299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod  return text;
630299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod}
631299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod
632299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbodvoid
633299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbodhb_buffer_add_utf16 (hb_buffer_t    *buffer,
634299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod		     const uint16_t *text,
63533d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod		     unsigned int    text_length HB_UNUSED,
636299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod		     unsigned int    item_offset,
637299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod		     unsigned int    item_length)
638299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod{
639299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod#define UTF_NEXT(S, E, U)	hb_utf16_next (S, E, &(U))
640299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod  ADD_UTF (uint16_t);
641299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod#undef UTF_NEXT
642299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod}
643299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod
644299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbodvoid
645299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbodhb_buffer_add_utf32 (hb_buffer_t    *buffer,
646299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod		     const uint32_t *text,
64733d13fdda99acaeffa9600737e8870278d053ebeBehdad Esfahbod		     unsigned int    text_length HB_UNUSED,
648299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod		     unsigned int    item_offset,
649299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod		     unsigned int    item_length)
650299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod{
651299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod#define UTF_NEXT(S, E, U)	((U) = *(S), (S)+1)
652299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod  ADD_UTF (uint32_t);
653299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod#undef UTF_NEXT
654299f08961ffcea27e8def4f0743d0c86ef8dadf1Behdad Esfahbod}
655acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
656acdba3f90b232fc12fcb200dca2584481b339118Behdad Esfahbod
657acdba3f90b232fc12fcb200dca2584481b339118Behdad EsfahbodHB_END_DECLS
658