2 * Copyright © 2011,2012,2014 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
32 #include "hb-open-type.hh"
37 typedef uint8_t codepoint_t;
39 static const codepoint_t *
40 next (const codepoint_t *text,
41 const codepoint_t *end,
42 hb_codepoint_t *unicode,
43 hb_codepoint_t replacement)
45 /* Written to only accept well-formed sequences.
46 * Based on ideas from ICU's U8_NEXT.
47 * Generates one "replacement" for each ill-formed byte. */
49 hb_codepoint_t c = *text++;
53 if (hb_in_range<hb_codepoint_t> (c, 0xC2u, 0xDFu)) /* Two-byte */
56 if (likely (text < end &&
57 (t1 = text[0] - 0x80u) <= 0x3Fu))
59 c = ((c&0x1Fu)<<6) | t1;
65 else if (hb_in_range<hb_codepoint_t> (c, 0xE0u, 0xEFu)) /* Three-byte */
68 if (likely (1 < end - text &&
69 (t1 = text[0] - 0x80u) <= 0x3Fu &&
70 (t2 = text[1] - 0x80u) <= 0x3Fu))
72 c = ((c&0xFu)<<12) | (t1<<6) | t2;
73 if (unlikely (c < 0x0800u || hb_in_range<hb_codepoint_t> (c, 0xD800u, 0xDFFFu)))
80 else if (hb_in_range<hb_codepoint_t> (c, 0xF0u, 0xF4u)) /* Four-byte */
82 unsigned int t1, t2, t3;
83 if (likely (2 < end - text &&
84 (t1 = text[0] - 0x80u) <= 0x3Fu &&
85 (t2 = text[1] - 0x80u) <= 0x3Fu &&
86 (t3 = text[2] - 0x80u) <= 0x3Fu))
88 c = ((c&0x7u)<<18) | (t1<<12) | (t2<<6) | t3;
89 if (unlikely (!hb_in_range<hb_codepoint_t> (c, 0x10000u, 0x10FFFFu)))
104 *unicode = replacement;
108 static const codepoint_t *
109 prev (const codepoint_t *text,
110 const codepoint_t *start,
111 hb_codepoint_t *unicode,
112 hb_codepoint_t replacement)
114 const codepoint_t *end = text--;
115 while (start < text && (*text & 0xc0) == 0x80 && end - text < 4)
118 if (likely (next (text, end, unicode, replacement) == end))
121 *unicode = replacement;
126 strlen (const codepoint_t *text)
127 { return ::strlen ((const char *) text); }
130 encode_len (hb_codepoint_t unicode)
132 if (unicode < 0x0080u) return 1;
133 if (unicode < 0x0800u) return 2;
134 if (unicode < 0x10000u) return 3;
135 if (unicode < 0x110000u) return 4;
140 encode (codepoint_t *text,
141 const codepoint_t *end,
142 hb_codepoint_t unicode)
144 if (unlikely (unicode >= 0xD800u && (unicode <= 0xDFFFu || unicode > 0x10FFFFu)))
146 if (unicode < 0x0080u)
148 else if (unicode < 0x0800u)
152 *text++ = 0xC0u + (0x1Fu & (unicode >> 6));
153 *text++ = 0x80u + (0x3Fu & (unicode ));
156 else if (unicode < 0x10000u)
160 *text++ = 0xE0u + (0x0Fu & (unicode >> 12));
161 *text++ = 0x80u + (0x3Fu & (unicode >> 6));
162 *text++ = 0x80u + (0x3Fu & (unicode ));
169 *text++ = 0xF0u + (0x07u & (unicode >> 18));
170 *text++ = 0x80u + (0x3Fu & (unicode >> 12));
171 *text++ = 0x80u + (0x3Fu & (unicode >> 6));
172 *text++ = 0x80u + (0x3Fu & (unicode ));
180 template <typename TCodepoint>
183 static_assert (sizeof (TCodepoint) == 2, "");
184 typedef TCodepoint codepoint_t;
186 static const codepoint_t *
187 next (const codepoint_t *text,
188 const codepoint_t *end,
189 hb_codepoint_t *unicode,
190 hb_codepoint_t replacement)
192 hb_codepoint_t c = *text++;
194 if (likely (!hb_in_range<hb_codepoint_t> (c, 0xD800u, 0xDFFFu)))
200 if (likely (c <= 0xDBFFu && text < end))
202 /* High-surrogate in c */
203 hb_codepoint_t l = *text;
204 if (likely (hb_in_range<hb_codepoint_t> (l, 0xDC00u, 0xDFFFu)))
206 /* Low-surrogate in l */
207 *unicode = (c << 10) + l - ((0xD800u << 10) - 0x10000u + 0xDC00u);
213 /* Lonely / out-of-order surrogate. */
214 *unicode = replacement;
218 static const codepoint_t *
219 prev (const codepoint_t *text,
220 const codepoint_t *start,
221 hb_codepoint_t *unicode,
222 hb_codepoint_t replacement)
224 hb_codepoint_t c = *--text;
226 if (likely (!hb_in_range<hb_codepoint_t> (c, 0xD800u, 0xDFFFu)))
232 if (likely (c >= 0xDC00u && start < text))
234 /* Low-surrogate in c */
235 hb_codepoint_t h = text[-1];
236 if (likely (hb_in_range<hb_codepoint_t> (h, 0xD800u, 0xDBFFu)))
238 /* High-surrogate in h */
239 *unicode = (h << 10) + c - ((0xD800u << 10) - 0x10000u + 0xDC00u);
245 /* Lonely / out-of-order surrogate. */
246 *unicode = replacement;
252 strlen (const codepoint_t *text)
260 encode_len (hb_codepoint_t unicode)
262 return unicode < 0x10000 ? 1 : 2;
266 encode (codepoint_t *text,
267 const codepoint_t *end,
268 hb_codepoint_t unicode)
270 if (unlikely (unicode >= 0xD800u && (unicode <= 0xDFFFu || unicode > 0x10FFFFu)))
272 if (unicode < 0x10000u)
274 else if (end - text >= 2)
277 *text++ = 0xD800u + (unicode >> 10);
278 *text++ = 0xDC00u + (unicode & 0x03FFu);
284 typedef hb_utf16_xe_t<uint16_t> hb_utf16_t;
285 typedef hb_utf16_xe_t<OT::HBUINT16> hb_utf16_be_t;
288 template <typename TCodepoint, bool validate=true>
291 static_assert (sizeof (TCodepoint) == 4, "");
292 typedef TCodepoint codepoint_t;
294 static const TCodepoint *
295 next (const TCodepoint *text,
296 const TCodepoint *end HB_UNUSED,
297 hb_codepoint_t *unicode,
298 hb_codepoint_t replacement)
300 hb_codepoint_t c = *unicode = *text++;
301 if (validate && unlikely (c >= 0xD800u && (c <= 0xDFFFu || c > 0x10FFFFu)))
302 *unicode = replacement;
306 static const TCodepoint *
307 prev (const TCodepoint *text,
308 const TCodepoint *start HB_UNUSED,
309 hb_codepoint_t *unicode,
310 hb_codepoint_t replacement)
312 hb_codepoint_t c = *unicode = *--text;
313 if (validate && unlikely (c >= 0xD800u && (c <= 0xDFFFu || c > 0x10FFFFu)))
314 *unicode = replacement;
319 strlen (const TCodepoint *text)
327 encode_len (hb_codepoint_t unicode HB_UNUSED)
333 encode (codepoint_t *text,
334 const codepoint_t *end HB_UNUSED,
335 hb_codepoint_t unicode)
337 if (validate && unlikely (unicode >= 0xD800u && (unicode <= 0xDFFFu || unicode > 0x10FFFFu)))
344 typedef hb_utf32_xe_t<uint32_t> hb_utf32_t;
345 typedef hb_utf32_xe_t<uint32_t, false> hb_utf32_novalidate_t;
350 typedef uint8_t codepoint_t;
352 static const codepoint_t *
353 next (const codepoint_t *text,
354 const codepoint_t *end HB_UNUSED,
355 hb_codepoint_t *unicode,
356 hb_codepoint_t replacement HB_UNUSED)
362 static const codepoint_t *
363 prev (const codepoint_t *text,
364 const codepoint_t *start HB_UNUSED,
365 hb_codepoint_t *unicode,
366 hb_codepoint_t replacement HB_UNUSED)
373 strlen (const codepoint_t *text)
381 encode_len (hb_codepoint_t unicode HB_UNUSED)
387 encode (codepoint_t *text,
388 const codepoint_t *end HB_UNUSED,
389 hb_codepoint_t unicode)
391 if (unlikely (unicode >= 0x0100u))
401 typedef uint8_t codepoint_t;
403 static const codepoint_t *
404 next (const codepoint_t *text,
405 const codepoint_t *end HB_UNUSED,
406 hb_codepoint_t *unicode,
407 hb_codepoint_t replacement HB_UNUSED)
410 if (*unicode >= 0x0080u)
411 *unicode = replacement;
415 static const codepoint_t *
416 prev (const codepoint_t *text,
417 const codepoint_t *start HB_UNUSED,
418 hb_codepoint_t *unicode,
419 hb_codepoint_t replacement)
422 if (*unicode >= 0x0080u)
423 *unicode = replacement;
428 strlen (const codepoint_t *text)
436 encode_len (hb_codepoint_t unicode HB_UNUSED)
442 encode (codepoint_t *text,
443 const codepoint_t *end HB_UNUSED,
444 hb_codepoint_t unicode)
446 if (unlikely (unicode >= 0x0080u))
453 #endif /* HB_UTF_HH */