gfx/harfbuzz/src/hb-utf-private.hh

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/harfbuzz/src/hb-utf-private.hh	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,204 @@
     1.4 +/*
     1.5 + * Copyright © 2011,2012  Google, Inc.
     1.6 + *
     1.7 + *  This is part of HarfBuzz, a text shaping library.
     1.8 + *
     1.9 + * Permission is hereby granted, without written agreement and without
    1.10 + * license or royalty fees, to use, copy, modify, and distribute this
    1.11 + * software and its documentation for any purpose, provided that the
    1.12 + * above copyright notice and the following two paragraphs appear in
    1.13 + * all copies of this software.
    1.14 + *
    1.15 + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
    1.16 + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
    1.17 + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
    1.18 + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
    1.19 + * DAMAGE.
    1.20 + *
    1.21 + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
    1.22 + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
    1.23 + * FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
    1.24 + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
    1.25 + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
    1.26 + *
    1.27 + * Google Author(s): Behdad Esfahbod
    1.28 + */
    1.29 +
    1.30 +#ifndef HB_UTF_PRIVATE_HH
    1.31 +#define HB_UTF_PRIVATE_HH
    1.32 +
    1.33 +#include "hb-private.hh"
    1.34 +
    1.35 +
    1.36 +/* UTF-8 */
    1.37 +
    1.38 +#define HB_UTF8_COMPUTE(Char, Mask, Len) \
    1.39 +  if (Char < 128) { Len = 1; Mask = 0x7f; } \
    1.40 +  else if ((Char & 0xe0) == 0xc0) { Len = 2; Mask = 0x1f; } \
    1.41 +  else if ((Char & 0xf0) == 0xe0) { Len = 3; Mask = 0x0f; } \
    1.42 +  else if ((Char & 0xf8) == 0xf0) { Len = 4; Mask = 0x07; } \
    1.43 +  else Len = 0;
    1.44 +
    1.45 +static inline const uint8_t *
    1.46 +hb_utf_next (const uint8_t *text,
    1.47 +	     const uint8_t *end,
    1.48 +	     hb_codepoint_t *unicode)
    1.49 +{
    1.50 +  hb_codepoint_t c = *text, mask;
    1.51 +  unsigned int len;
    1.52 +
    1.53 +  /* TODO check for overlong sequences? */
    1.54 +
    1.55 +  HB_UTF8_COMPUTE (c, mask, len);
    1.56 +  if (unlikely (!len || (unsigned int) (end - text) < len)) {
    1.57 +    *unicode = -1;
    1.58 +    return text + 1;
    1.59 +  } else {
    1.60 +    hb_codepoint_t result;
    1.61 +    unsigned int i;
    1.62 +    result = c & mask;
    1.63 +    for (i = 1; i < len; i++)
    1.64 +      {
    1.65 +	if (unlikely ((text[i] & 0xc0) != 0x80))
    1.66 +	  {
    1.67 +	    *unicode = -1;
    1.68 +	    return text + 1;
    1.69 +	  }
    1.70 +	result <<= 6;
    1.71 +	result |= (text[i] & 0x3f);
    1.72 +      }
    1.73 +    *unicode = result;
    1.74 +    return text + len;
    1.75 +  }
    1.76 +}
    1.77 +
    1.78 +static inline const uint8_t *
    1.79 +hb_utf_prev (const uint8_t *text,
    1.80 +	     const uint8_t *start,
    1.81 +	     hb_codepoint_t *unicode)
    1.82 +{
    1.83 +  const uint8_t *end = text--;
    1.84 +  while (start < text && (*text & 0xc0) == 0x80 && end - text < 4)
    1.85 +    text--;
    1.86 +
    1.87 +  hb_codepoint_t c = *text, mask;
    1.88 +  unsigned int len;
    1.89 +
    1.90 +  /* TODO check for overlong sequences? */
    1.91 +
    1.92 +  HB_UTF8_COMPUTE (c, mask, len);
    1.93 +  if (unlikely (!len || (unsigned int) (end - text) != len)) {
    1.94 +    *unicode = -1;
    1.95 +    return end - 1;
    1.96 +  } else {
    1.97 +    hb_codepoint_t result;
    1.98 +    unsigned int i;
    1.99 +    result = c & mask;
   1.100 +    for (i = 1; i < len; i++)
   1.101 +      {
   1.102 +	result <<= 6;
   1.103 +	result |= (text[i] & 0x3f);
   1.104 +      }
   1.105 +    *unicode = result;
   1.106 +    return text;
   1.107 +  }
   1.108 +}
   1.109 +
   1.110 +
   1.111 +static inline unsigned int
   1.112 +hb_utf_strlen (const uint8_t *text)
   1.113 +{
   1.114 +  return strlen ((const char *) text);
   1.115 +}
   1.116 +
   1.117 +
   1.118 +/* UTF-16 */
   1.119 +
   1.120 +static inline const uint16_t *
   1.121 +hb_utf_next (const uint16_t *text,
   1.122 +	     const uint16_t *end,
   1.123 +	     hb_codepoint_t *unicode)
   1.124 +{
   1.125 +  hb_codepoint_t c = *text++;
   1.126 +
   1.127 +  if (unlikely (hb_in_range<hb_codepoint_t> (c, 0xd800, 0xdbff)))
   1.128 +  {
   1.129 +    /* high surrogate */
   1.130 +    hb_codepoint_t l;
   1.131 +    if (text < end && ((l = *text), likely (hb_in_range<hb_codepoint_t> (l, 0xdc00, 0xdfff))))
   1.132 +    {
   1.133 +      /* low surrogate */
   1.134 +      *unicode = (c << 10) + l - ((0xd800 << 10) - 0x10000 + 0xdc00);
   1.135 +       text++;
   1.136 +    } else
   1.137 +      *unicode = -1;
   1.138 +  } else
   1.139 +    *unicode = c;
   1.140 +
   1.141 +  return text;
   1.142 +}
   1.143 +
   1.144 +static inline const uint16_t *
   1.145 +hb_utf_prev (const uint16_t *text,
   1.146 +	     const uint16_t *start,
   1.147 +	     hb_codepoint_t *unicode)
   1.148 +{
   1.149 +  hb_codepoint_t c = *--text;
   1.150 +
   1.151 +  if (unlikely (hb_in_range<hb_codepoint_t> (c, 0xdc00, 0xdfff)))
   1.152 +  {
   1.153 +    /* low surrogate */
   1.154 +    hb_codepoint_t h;
   1.155 +    if (start < text && ((h = *(text - 1)), likely (hb_in_range<hb_codepoint_t> (h, 0xd800, 0xdbff))))
   1.156 +    {
   1.157 +      /* high surrogate */
   1.158 +      *unicode = (h << 10) + c - ((0xd800 << 10) - 0x10000 + 0xdc00);
   1.159 +       text--;
   1.160 +    } else
   1.161 +      *unicode = -1;
   1.162 +  } else
   1.163 +    *unicode = c;
   1.164 +
   1.165 +  return text;
   1.166 +}
   1.167 +
   1.168 +
   1.169 +static inline unsigned int
   1.170 +hb_utf_strlen (const uint16_t *text)
   1.171 +{
   1.172 +  unsigned int l = 0;
   1.173 +  while (*text++) l++;
   1.174 +  return l;
   1.175 +}
   1.176 +
   1.177 +
   1.178 +/* UTF-32 */
   1.179 +
   1.180 +static inline const uint32_t *
   1.181 +hb_utf_next (const uint32_t *text,
   1.182 +	     const uint32_t *end HB_UNUSED,
   1.183 +	     hb_codepoint_t *unicode)
   1.184 +{
   1.185 +  *unicode = *text++;
   1.186 +  return text;
   1.187 +}
   1.188 +
   1.189 +static inline const uint32_t *
   1.190 +hb_utf_prev (const uint32_t *text,
   1.191 +	     const uint32_t *start HB_UNUSED,
   1.192 +	     hb_codepoint_t *unicode)
   1.193 +{
   1.194 +  *unicode = *--text;
   1.195 +  return text;
   1.196 +}
   1.197 +
   1.198 +static inline unsigned int
   1.199 +hb_utf_strlen (const uint32_t *text)
   1.200 +{
   1.201 +  unsigned int l = 0;
   1.202 +  while (*text++) l++;
   1.203 +  return l;
   1.204 +}
   1.205 +
   1.206 +
   1.207 +#endif /* HB_UTF_PRIVATE_HH */

mercurial