2 * Copyright © 2011,2012 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
6 * Permission is hereby granted, without written agreement and without
7 * license or royalty fees, to use, copy, modify, and distribute this
8 * software and its documentation for any purpose, provided that the
9 * above copyright notice and the following two paragraphs appear in
10 * all copies of this software.
12 * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
13 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
14 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
15 * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
18 * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
19 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
20 * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21 * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
22 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
24 * Google Author(s): Behdad Esfahbod
27 #ifndef HB_UTF_PRIVATE_HH
28 #define HB_UTF_PRIVATE_HH
30 #include "hb-private.hh"
35 #define HB_UTF8_COMPUTE(Char, Mask, Len) \
36 if (Char < 128) { Len = 1; Mask = 0x7f; } \
37 else if ((Char & 0xe0) == 0xc0) { Len = 2; Mask = 0x1f; } \
38 else if ((Char & 0xf0) == 0xe0) { Len = 3; Mask = 0x0f; } \
39 else if ((Char & 0xf8) == 0xf0) { Len = 4; Mask = 0x07; } \
42 static inline const uint8_t *
43 hb_utf_next (const uint8_t *text,
45 hb_codepoint_t *unicode)
47 hb_codepoint_t c = *text, mask;
50 /* TODO check for overlong sequences? */
52 HB_UTF8_COMPUTE (c, mask, len);
53 if (unlikely (!len || (unsigned int) (end - text) < len)) {
57 hb_codepoint_t result;
60 for (i = 1; i < len; i++)
62 if (unlikely ((text[i] & 0xc0) != 0x80))
68 result |= (text[i] & 0x3f);
75 static inline const uint8_t *
76 hb_utf_prev (const uint8_t *text,
78 hb_codepoint_t *unicode)
80 const uint8_t *end = text--;
81 while (start < text && (*text & 0xc0) == 0x80 && end - text < 4)
84 hb_codepoint_t c = *text, mask;
87 /* TODO check for overlong sequences? */
89 HB_UTF8_COMPUTE (c, mask, len);
90 if (unlikely (!len || (unsigned int) (end - text) != len)) {
94 hb_codepoint_t result;
97 for (i = 1; i < len; i++)
100 result |= (text[i] & 0x3f);
108 static inline unsigned int
109 hb_utf_strlen (const uint8_t *text)
111 return strlen ((const char *) text);
117 static inline const uint16_t *
118 hb_utf_next (const uint16_t *text,
120 hb_codepoint_t *unicode)
122 hb_codepoint_t c = *text++;
124 if (unlikely (hb_in_range<hb_codepoint_t> (c, 0xd800, 0xdbff)))
128 if (text < end && ((l = *text), likely (hb_in_range<hb_codepoint_t> (l, 0xdc00, 0xdfff))))
131 *unicode = (c << 10) + l - ((0xd800 << 10) - 0x10000 + 0xdc00);
141 static inline const uint16_t *
142 hb_utf_prev (const uint16_t *text,
143 const uint16_t *start,
144 hb_codepoint_t *unicode)
146 hb_codepoint_t c = *--text;
148 if (unlikely (hb_in_range<hb_codepoint_t> (c, 0xdc00, 0xdfff)))
152 if (start < text && ((h = *(text - 1)), likely (hb_in_range<hb_codepoint_t> (h, 0xd800, 0xdbff))))
155 *unicode = (h << 10) + c - ((0xd800 << 10) - 0x10000 + 0xdc00);
166 static inline unsigned int
167 hb_utf_strlen (const uint16_t *text)
177 static inline const uint32_t *
178 hb_utf_next (const uint32_t *text,
179 const uint32_t *end HB_UNUSED,
180 hb_codepoint_t *unicode)
186 static inline const uint32_t *
187 hb_utf_prev (const uint32_t *text,
188 const uint32_t *start HB_UNUSED,
189 hb_codepoint_t *unicode)
195 static inline unsigned int
196 hb_utf_strlen (const uint32_t *text)
204 #endif /* HB_UTF_PRIVATE_HH */