hb-utf.hh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. * Copyright © 2011,2012,2014 Google, Inc.
  3. *
  4. * This is part of HarfBuzz, a text shaping library.
  5. *
  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.
  11. *
  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
  16. * DAMAGE.
  17. *
  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.
  23. *
  24. * Google Author(s): Behdad Esfahbod
  25. */
  26. #ifndef HB_UTF_HH
  27. #define HB_UTF_HH
  28. #include "hb.hh"
  29. #include "hb-open-type.hh"
  30. struct hb_utf8_t
  31. {
  32. typedef uint8_t codepoint_t;
  33. static const codepoint_t *
  34. next (const codepoint_t *text,
  35. const codepoint_t *end,
  36. hb_codepoint_t *unicode,
  37. hb_codepoint_t replacement)
  38. {
  39. /* Written to only accept well-formed sequences.
  40. * Based on ideas from ICU's U8_NEXT.
  41. * Generates one "replacement" for each ill-formed byte. */
  42. hb_codepoint_t c = *text++;
  43. if (c > 0x7Fu)
  44. {
  45. if (hb_in_range<hb_codepoint_t> (c, 0xC2u, 0xDFu)) /* Two-byte */
  46. {
  47. unsigned int t1;
  48. if (likely (text < end &&
  49. (t1 = text[0] - 0x80u) <= 0x3Fu))
  50. {
  51. c = ((c&0x1Fu)<<6) | t1;
  52. text++;
  53. }
  54. else
  55. goto error;
  56. }
  57. else if (hb_in_range<hb_codepoint_t> (c, 0xE0u, 0xEFu)) /* Three-byte */
  58. {
  59. unsigned int t1, t2;
  60. if (likely (1 < end - text &&
  61. (t1 = text[0] - 0x80u) <= 0x3Fu &&
  62. (t2 = text[1] - 0x80u) <= 0x3Fu))
  63. {
  64. c = ((c&0xFu)<<12) | (t1<<6) | t2;
  65. if (unlikely (c < 0x0800u || hb_in_range<hb_codepoint_t> (c, 0xD800u, 0xDFFFu)))
  66. goto error;
  67. text += 2;
  68. }
  69. else
  70. goto error;
  71. }
  72. else if (hb_in_range<hb_codepoint_t> (c, 0xF0u, 0xF4u)) /* Four-byte */
  73. {
  74. unsigned int t1, t2, t3;
  75. if (likely (2 < end - text &&
  76. (t1 = text[0] - 0x80u) <= 0x3Fu &&
  77. (t2 = text[1] - 0x80u) <= 0x3Fu &&
  78. (t3 = text[2] - 0x80u) <= 0x3Fu))
  79. {
  80. c = ((c&0x7u)<<18) | (t1<<12) | (t2<<6) | t3;
  81. if (unlikely (!hb_in_range<hb_codepoint_t> (c, 0x10000u, 0x10FFFFu)))
  82. goto error;
  83. text += 3;
  84. }
  85. else
  86. goto error;
  87. }
  88. else
  89. goto error;
  90. }
  91. *unicode = c;
  92. return text;
  93. error:
  94. *unicode = replacement;
  95. return text;
  96. }
  97. static const codepoint_t *
  98. prev (const codepoint_t *text,
  99. const codepoint_t *start,
  100. hb_codepoint_t *unicode,
  101. hb_codepoint_t replacement)
  102. {
  103. const codepoint_t *end = text--;
  104. while (start < text && (*text & 0xc0) == 0x80 && end - text < 4)
  105. text--;
  106. if (likely (next (text, end, unicode, replacement) == end))
  107. return text;
  108. *unicode = replacement;
  109. return end - 1;
  110. }
  111. static unsigned int
  112. strlen (const codepoint_t *text)
  113. { return ::strlen ((const char *) text); }
  114. static unsigned int
  115. encode_len (hb_codepoint_t unicode)
  116. {
  117. if (unicode < 0x0080u) return 1;
  118. if (unicode < 0x0800u) return 2;
  119. if (unicode < 0x10000u) return 3;
  120. if (unicode < 0x110000u) return 4;
  121. return 3;
  122. }
  123. static codepoint_t *
  124. encode (codepoint_t *text,
  125. const codepoint_t *end,
  126. hb_codepoint_t unicode)
  127. {
  128. if (unlikely (unicode >= 0xD800u && (unicode <= 0xDFFFu || unicode > 0x10FFFFu)))
  129. unicode = 0xFFFDu;
  130. if (unicode < 0x0080u)
  131. *text++ = unicode;
  132. else if (unicode < 0x0800u)
  133. {
  134. if (end - text >= 2)
  135. {
  136. *text++ = 0xC0u + (0x1Fu & (unicode >> 6));
  137. *text++ = 0x80u + (0x3Fu & (unicode ));
  138. }
  139. }
  140. else if (unicode < 0x10000u)
  141. {
  142. if (end - text >= 3)
  143. {
  144. *text++ = 0xE0u + (0x0Fu & (unicode >> 12));
  145. *text++ = 0x80u + (0x3Fu & (unicode >> 6));
  146. *text++ = 0x80u + (0x3Fu & (unicode ));
  147. }
  148. }
  149. else
  150. {
  151. if (end - text >= 4)
  152. {
  153. *text++ = 0xF0u + (0x07u & (unicode >> 18));
  154. *text++ = 0x80u + (0x3Fu & (unicode >> 12));
  155. *text++ = 0x80u + (0x3Fu & (unicode >> 6));
  156. *text++ = 0x80u + (0x3Fu & (unicode ));
  157. }
  158. }
  159. return text;
  160. }
  161. };
  162. template <typename TCodepoint>
  163. struct hb_utf16_xe_t
  164. {
  165. static_assert (sizeof (TCodepoint) == 2, "");
  166. typedef TCodepoint codepoint_t;
  167. static const codepoint_t *
  168. next (const codepoint_t *text,
  169. const codepoint_t *end,
  170. hb_codepoint_t *unicode,
  171. hb_codepoint_t replacement)
  172. {
  173. hb_codepoint_t c = *text++;
  174. if (likely (!hb_in_range<hb_codepoint_t> (c, 0xD800u, 0xDFFFu)))
  175. {
  176. *unicode = c;
  177. return text;
  178. }
  179. if (likely (c <= 0xDBFFu && text < end))
  180. {
  181. /* High-surrogate in c */
  182. hb_codepoint_t l = *text;
  183. if (likely (hb_in_range<hb_codepoint_t> (l, 0xDC00u, 0xDFFFu)))
  184. {
  185. /* Low-surrogate in l */
  186. *unicode = (c << 10) + l - ((0xD800u << 10) - 0x10000u + 0xDC00u);
  187. text++;
  188. return text;
  189. }
  190. }
  191. /* Lonely / out-of-order surrogate. */
  192. *unicode = replacement;
  193. return text;
  194. }
  195. static const codepoint_t *
  196. prev (const codepoint_t *text,
  197. const codepoint_t *start,
  198. hb_codepoint_t *unicode,
  199. hb_codepoint_t replacement)
  200. {
  201. hb_codepoint_t c = *--text;
  202. if (likely (!hb_in_range<hb_codepoint_t> (c, 0xD800u, 0xDFFFu)))
  203. {
  204. *unicode = c;
  205. return text;
  206. }
  207. if (likely (c >= 0xDC00u && start < text))
  208. {
  209. /* Low-surrogate in c */
  210. hb_codepoint_t h = text[-1];
  211. if (likely (hb_in_range<hb_codepoint_t> (h, 0xD800u, 0xDBFFu)))
  212. {
  213. /* High-surrogate in h */
  214. *unicode = (h << 10) + c - ((0xD800u << 10) - 0x10000u + 0xDC00u);
  215. text--;
  216. return text;
  217. }
  218. }
  219. /* Lonely / out-of-order surrogate. */
  220. *unicode = replacement;
  221. return text;
  222. }
  223. static unsigned int
  224. strlen (const codepoint_t *text)
  225. {
  226. unsigned int l = 0;
  227. while (*text++) l++;
  228. return l;
  229. }
  230. static unsigned int
  231. encode_len (hb_codepoint_t unicode)
  232. {
  233. return unicode < 0x10000 ? 1 : 2;
  234. }
  235. static codepoint_t *
  236. encode (codepoint_t *text,
  237. const codepoint_t *end,
  238. hb_codepoint_t unicode)
  239. {
  240. if (unlikely (unicode >= 0xD800u && (unicode <= 0xDFFFu || unicode > 0x10FFFFu)))
  241. unicode = 0xFFFDu;
  242. if (unicode < 0x10000u)
  243. *text++ = unicode;
  244. else if (end - text >= 2)
  245. {
  246. unicode -= 0x10000u;
  247. *text++ = 0xD800u + (unicode >> 10);
  248. *text++ = 0xDC00u + (unicode & 0x03FFu);
  249. }
  250. return text;
  251. }
  252. };
  253. typedef hb_utf16_xe_t<uint16_t> hb_utf16_t;
  254. typedef hb_utf16_xe_t<OT::HBUINT16> hb_utf16_be_t;
  255. template <typename TCodepoint, bool validate=true>
  256. struct hb_utf32_xe_t
  257. {
  258. static_assert (sizeof (TCodepoint) == 4, "");
  259. typedef TCodepoint codepoint_t;
  260. static const TCodepoint *
  261. next (const TCodepoint *text,
  262. const TCodepoint *end HB_UNUSED,
  263. hb_codepoint_t *unicode,
  264. hb_codepoint_t replacement)
  265. {
  266. hb_codepoint_t c = *unicode = *text++;
  267. if (validate && unlikely (c >= 0xD800u && (c <= 0xDFFFu || c > 0x10FFFFu)))
  268. *unicode = replacement;
  269. return text;
  270. }
  271. static const TCodepoint *
  272. prev (const TCodepoint *text,
  273. const TCodepoint *start HB_UNUSED,
  274. hb_codepoint_t *unicode,
  275. hb_codepoint_t replacement)
  276. {
  277. hb_codepoint_t c = *unicode = *--text;
  278. if (validate && unlikely (c >= 0xD800u && (c <= 0xDFFFu || c > 0x10FFFFu)))
  279. *unicode = replacement;
  280. return text;
  281. }
  282. static unsigned int
  283. strlen (const TCodepoint *text)
  284. {
  285. unsigned int l = 0;
  286. while (*text++) l++;
  287. return l;
  288. }
  289. static unsigned int
  290. encode_len (hb_codepoint_t unicode HB_UNUSED)
  291. {
  292. return 1;
  293. }
  294. static codepoint_t *
  295. encode (codepoint_t *text,
  296. const codepoint_t *end HB_UNUSED,
  297. hb_codepoint_t unicode)
  298. {
  299. if (validate && unlikely (unicode >= 0xD800u && (unicode <= 0xDFFFu || unicode > 0x10FFFFu)))
  300. unicode = 0xFFFDu;
  301. *text++ = unicode;
  302. return text;
  303. }
  304. };
  305. typedef hb_utf32_xe_t<uint32_t> hb_utf32_t;
  306. typedef hb_utf32_xe_t<uint32_t, false> hb_utf32_novalidate_t;
  307. struct hb_latin1_t
  308. {
  309. typedef uint8_t codepoint_t;
  310. static const codepoint_t *
  311. next (const codepoint_t *text,
  312. const codepoint_t *end HB_UNUSED,
  313. hb_codepoint_t *unicode,
  314. hb_codepoint_t replacement HB_UNUSED)
  315. {
  316. *unicode = *text++;
  317. return text;
  318. }
  319. static const codepoint_t *
  320. prev (const codepoint_t *text,
  321. const codepoint_t *start HB_UNUSED,
  322. hb_codepoint_t *unicode,
  323. hb_codepoint_t replacement HB_UNUSED)
  324. {
  325. *unicode = *--text;
  326. return text;
  327. }
  328. static unsigned int
  329. strlen (const codepoint_t *text)
  330. {
  331. unsigned int l = 0;
  332. while (*text++) l++;
  333. return l;
  334. }
  335. static unsigned int
  336. encode_len (hb_codepoint_t unicode HB_UNUSED)
  337. {
  338. return 1;
  339. }
  340. static codepoint_t *
  341. encode (codepoint_t *text,
  342. const codepoint_t *end HB_UNUSED,
  343. hb_codepoint_t unicode)
  344. {
  345. if (unlikely (unicode >= 0x0100u))
  346. unicode = '?';
  347. *text++ = unicode;
  348. return text;
  349. }
  350. };
  351. struct hb_ascii_t
  352. {
  353. typedef uint8_t codepoint_t;
  354. static const codepoint_t *
  355. next (const codepoint_t *text,
  356. const codepoint_t *end HB_UNUSED,
  357. hb_codepoint_t *unicode,
  358. hb_codepoint_t replacement HB_UNUSED)
  359. {
  360. *unicode = *text++;
  361. if (*unicode >= 0x0080u)
  362. *unicode = replacement;
  363. return text;
  364. }
  365. static const codepoint_t *
  366. prev (const codepoint_t *text,
  367. const codepoint_t *start HB_UNUSED,
  368. hb_codepoint_t *unicode,
  369. hb_codepoint_t replacement)
  370. {
  371. *unicode = *--text;
  372. if (*unicode >= 0x0080u)
  373. *unicode = replacement;
  374. return text;
  375. }
  376. static unsigned int
  377. strlen (const codepoint_t *text)
  378. {
  379. unsigned int l = 0;
  380. while (*text++) l++;
  381. return l;
  382. }
  383. static unsigned int
  384. encode_len (hb_codepoint_t unicode HB_UNUSED)
  385. {
  386. return 1;
  387. }
  388. static codepoint_t *
  389. encode (codepoint_t *text,
  390. const codepoint_t *end HB_UNUSED,
  391. hb_codepoint_t unicode)
  392. {
  393. if (unlikely (unicode >= 0x0080u))
  394. unicode = '?';
  395. *text++ = unicode;
  396. return text;
  397. }
  398. };
  399. #endif /* HB_UTF_HH */