You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

435 lines
13 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2007 Sam Hocevar <sam@zoy.org>
  4. * 2007 Ben Wiley Sittler <bsittler@gmail.com>
  5. * All Rights Reserved
  6. *
  7. * $Id$
  8. *
  9. * This library is free software. It comes without any warranty, to
  10. * the extent permitted by applicable law. You can redistribute it
  11. * and/or modify it under the terms of the Do What The Fuck You Want
  12. * To Public License, Version 2, as published by Sam Hocevar. See
  13. * http://sam.zoy.org/wtfpl/COPYING for more details.
  14. */
  15. /*
  16. * This file contains functions for converting characters between
  17. * various character sets.
  18. */
  19. #include "config.h"
  20. #if !defined(__KERNEL__)
  21. # include <string.h>
  22. #endif
  23. #include "caca.h"
  24. #include "caca_internals.h"
  25. /*
  26. * UTF-8 handling
  27. */
  28. static uint8_t const trailing[256] =
  29. {
  30. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  31. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  32. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  33. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  34. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  35. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  36. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  37. 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5
  38. };
  39. static uint32_t const offsets[6] =
  40. {
  41. 0x00000000UL, 0x00003080UL, 0x000E2080UL,
  42. 0x03C82080UL, 0xFA082080UL, 0x82082080UL
  43. };
  44. /*
  45. * CP437 handling
  46. */
  47. static uint32_t const cp437_lookup1[] =
  48. {
  49. /* 0x01 - 0x0f: ☺ ☻ ♥ ♦ ♣ ♠ • ◘ ○ ◙ ♂ ♀ ♪ ♫ ☼ */
  50. 0x263a, 0x263b, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022,
  51. 0x25d8, 0x25cb, 0x25d9, 0x2642, 0x2640, 0x266a, 0x266b, 0x263c,
  52. /* 0x10 - 0x1f: ► ◄ ↕ ‼ ¶ § ▬ ↨ ↑ ↓ → ← ∟ ↔ ▲ ▼ */
  53. 0x25ba, 0x25c4, 0x2195, 0x203c, 0xb6, 0xa7, 0x25ac, 0x21a8,
  54. 0x2191, 0x2193, 0x2192, 0x2190, 0x221f, 0x2194, 0x25b2, 0x25bc
  55. };
  56. static uint32_t const cp437_lookup2[] =
  57. {
  58. /* 0x7f: ⌂ */
  59. 0x2302,
  60. /* 0x80 - 0x8f: Ç ü é â ä à å ç ê ë è ï î ì Ä Å */
  61. 0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,
  62. 0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,
  63. /* 0x90 - 0x9f: É æ Æ ô ö ò û ù ÿ Ö Ü ¢ £ ¥ ₧ ƒ */
  64. 0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,
  65. 0xff, 0xd6, 0xdc, 0xa2, 0xa3, 0xa5, 0x20a7, 0x192,
  66. /* 0xa0 - 0xaf: á í ó ú ñ Ñ ª º ¿ ⌐ ¬ ½ ¼ ¡ « » */
  67. 0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,
  68. 0xbf, 0x2310, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,
  69. /* 0xb0 - 0xbf: ░ ▒ ▓ │ ┤ ╡ ╢ ╖ ╕ ╣ ║ ╗ ╝ ╜ ╛ ┐ */
  70. 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,
  71. 0x2555, 0x2563, 0x2551, 0x2557, 0x255d, 0x255c, 0x255b, 0x2510,
  72. /* 0xc0 - 0xcf: └ ┴ ┬ ├ ─ ┼ ╞ ╟ ╚ ╔ ╩ ╦ ╠ ═ ╬ ╧ */
  73. 0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x255e, 0x255f,
  74. 0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x2567,
  75. /* 0xd0 - 0xdf: ╨ ╤ ╥ ╙ ╘ ╒ ╓ ╫ ╪ ┘ ┌ █ ▄ ▌ ▐ ▀ */
  76. 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256b,
  77. 0x256a, 0x2518, 0x250c, 0x2588, 0x2584, 0x258c, 0x2590, 0x2580,
  78. /* 0xe0 - 0xef: α ß Γ π Σ σ µ τ Φ Θ Ω δ ∞ φ ε ∩ */
  79. 0x3b1, 0xdf, 0x393, 0x3c0, 0x3a3, 0x3c3, 0xb5, 0x3c4,
  80. 0x3a6, 0x398, 0x3a9, 0x3b4, 0x221e, 0x3c6, 0x3b5, 0x2229,
  81. /* 0xf0 - 0xff: ≡ ± ≥ ≤ ⌠ ⌡ ÷ ≈ ° ∙ · √ ⁿ ² ■ <nbsp> */
  82. 0x2261, 0xb1, 0x2265, 0x2264, 0x2320, 0x2321, 0xf7, 0x2248,
  83. 0xb0, 0x2219, 0xb7, 0x221a, 0x207f, 0xb2, 0x25a0, 0xa0
  84. };
  85. /** \brief Convert a UTF-8 character to UTF-32.
  86. *
  87. * Convert a UTF-8 character read from a string and return its value in
  88. * the UTF-32 character set. If the second argument is not null, the total
  89. * number of read bytes is written in it.
  90. *
  91. * If a null byte was reached before the expected end of the UTF-8 sequence,
  92. * this function returns zero and the number of read bytes is set to zero.
  93. *
  94. * This function never fails, but its behaviour with illegal UTF-8 sequences
  95. * is undefined.
  96. *
  97. * \param s A string containing the UTF-8 character.
  98. * \param bytes A pointer to a size_t to store the number of bytes in the
  99. * character, or NULL.
  100. * \return The corresponding UTF-32 character, or zero if the character
  101. * is incomplete.
  102. */
  103. uint32_t caca_utf8_to_utf32(char const *s, size_t *bytes)
  104. {
  105. int todo = trailing[(int)(unsigned char)*s];
  106. int i = 0;
  107. uint32_t ret = 0;
  108. for(;;)
  109. {
  110. if(!*s)
  111. {
  112. if(bytes)
  113. *bytes = 0;
  114. return 0;
  115. }
  116. ret += ((uint32_t)(unsigned char)*s++) << (6 * (todo - i));
  117. if(todo == i++)
  118. {
  119. if(bytes)
  120. *bytes = i;
  121. return ret - offsets[todo];
  122. }
  123. }
  124. }
  125. /** \brief Convert a UTF-32 character to UTF-8.
  126. *
  127. * Convert a UTF-32 character read from a string and write its value in
  128. * the UTF-8 character set into the given buffer.
  129. *
  130. * This function never fails, but its behaviour with illegal UTF-32 characters
  131. * is undefined.
  132. *
  133. * \param buf A pointer to a character buffer where the UTF-8 sequence will
  134. * be written.
  135. * \param ch The UTF-32 character.
  136. * \return The number of bytes written.
  137. */
  138. size_t caca_utf32_to_utf8(char *buf, uint32_t ch)
  139. {
  140. static const uint8_t mark[7] =
  141. {
  142. 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC
  143. };
  144. char *parser = buf;
  145. size_t bytes;
  146. if(ch < 0x80)
  147. {
  148. *parser++ = ch;
  149. return 1;
  150. }
  151. bytes = (ch < 0x800) ? 2 : (ch < 0x10000) ? 3 : 4;
  152. parser += bytes;
  153. switch(bytes)
  154. {
  155. case 4: *--parser = (ch | 0x80) & 0xbf; ch >>= 6;
  156. case 3: *--parser = (ch | 0x80) & 0xbf; ch >>= 6;
  157. case 2: *--parser = (ch | 0x80) & 0xbf; ch >>= 6;
  158. }
  159. *--parser = ch | mark[bytes];
  160. return bytes;
  161. }
  162. /** \brief Convert a UTF-32 character to CP437.
  163. *
  164. * Convert a UTF-32 character read from a string and return its value in
  165. * the CP437 character set, or "?" if the character has no equivalent.
  166. *
  167. * This function never fails.
  168. *
  169. * \param ch The UTF-32 character.
  170. * \return The corresponding CP437 character, or "?" if not representable.
  171. */
  172. uint8_t caca_utf32_to_cp437(uint32_t ch)
  173. {
  174. unsigned int i;
  175. if(ch < 0x00000020)
  176. return '?';
  177. if(ch < 0x00000080)
  178. return ch;
  179. for(i = 0; i < sizeof(cp437_lookup1) / sizeof(*cp437_lookup1); i++)
  180. if(cp437_lookup1[i] == ch)
  181. return 0x01 + i;
  182. for(i = 0; i < sizeof(cp437_lookup2) / sizeof(*cp437_lookup2); i++)
  183. if(cp437_lookup2[i] == ch)
  184. return 0x7f + i;
  185. return '?';
  186. }
  187. /** \brief Convert a CP437 character to UTF-32.
  188. *
  189. * Convert a CP437 character read from a string and return its value in
  190. * the UTF-32 character set, or zero if the character is a CP437 control
  191. * character.
  192. *
  193. * This function never fails.
  194. *
  195. * \param ch The CP437 character.
  196. * \return The corresponding UTF-32 character, or zero if not representable.
  197. */
  198. uint32_t caca_cp437_to_utf32(uint8_t ch)
  199. {
  200. if(ch > 0x7f)
  201. return cp437_lookup2[ch - 0x7f];
  202. if(ch >= 0x20)
  203. return (uint32_t)ch;
  204. if(ch > 0)
  205. return cp437_lookup1[ch - 0x01];
  206. return 0x00000000;
  207. }
  208. /** \brief Convert a UTF-32 character to ASCII.
  209. *
  210. * Convert a UTF-32 character into an ASCII character. When no equivalent
  211. * exists, a graphically close equivalent is sought.
  212. *
  213. * This function never fails, but its behaviour with illegal UTF-32 characters
  214. * is undefined.
  215. *
  216. * \param ch The UTF-32 character.
  217. * \return The corresponding ASCII character, or a graphically close
  218. * equivalent if found, or "?" if not representable.
  219. */
  220. char caca_utf32_to_ascii(uint32_t ch)
  221. {
  222. /* Standard ASCII */
  223. if(ch < 0x80)
  224. return ch;
  225. /* Fullwidth Forms */
  226. if(ch > 0x0000ff00 && ch < 0x0000ff5f)
  227. return ' ' + (ch - 0x0000ff00);
  228. switch (ch)
  229. {
  230. case 0x000000a0: /*   (nbsp) */
  231. case 0x00003000: /*   (ideographic space) */
  232. return ' ';
  233. case 0x000000a3: /* £ */
  234. return 'f';
  235. case 0x000000b0: /* ° */
  236. return '\'';
  237. case 0x000000b1: /* ± */
  238. return '#';
  239. case 0x000000b7: /* · */
  240. case 0x00002219: /* ∙ */
  241. case 0x000030fb: /* ・ */
  242. return '.';
  243. case 0x000003c0: /* π */
  244. return '*';
  245. case 0x00002018: /* ‘ */
  246. case 0x00002019: /* ’ */
  247. return '\'';
  248. case 0x0000201c: /* “ */
  249. case 0x0000201d: /* ” */
  250. return '"';
  251. case 0x00002190: /* ← */
  252. return '<';
  253. case 0x00002191: /* ↑ */
  254. return '^';
  255. case 0x00002192: /* → */
  256. return '>';
  257. case 0x00002193: /* ↓ */
  258. return 'v';
  259. case 0x00002260: /* ≠ */
  260. return '!';
  261. case 0x00002261: /* ≡ */
  262. return '=';
  263. case 0x00002264: /* ≤ */
  264. return '<';
  265. case 0x00002265: /* ≥ */
  266. return '>';
  267. case 0x000023ba: /* ⎺ */
  268. case 0x000023bb: /* ⎻ */
  269. case 0x000023bc: /* ⎼ */
  270. case 0x000023bd: /* ⎽ */
  271. case 0x00002500: /* ─ */
  272. case 0x00002550: /* ═ */
  273. return '-';
  274. case 0x00002502: /* │ */
  275. case 0x00002551: /* ║ */
  276. return '|';
  277. case 0x0000250c: /* ┌ */
  278. case 0x00002552: /* ╒ */
  279. case 0x00002553: /* ╓ */
  280. case 0x00002554: /* ╔ */
  281. case 0x00002514: /* └ */
  282. case 0x00002558: /* ╘ */
  283. case 0x00002559: /* ╙ */
  284. case 0x0000255a: /* ╚ */
  285. case 0x0000251c: /* ├ */
  286. case 0x0000255e: /* ╞ */
  287. case 0x0000255f: /* ╟ */
  288. case 0x00002560: /* ╠ */
  289. case 0x0000252c: /* ┬ */
  290. case 0x00002564: /* ╤ */
  291. case 0x00002565: /* ╥ */
  292. case 0x00002566: /* ╦ */
  293. case 0x00002534: /* ┴ */
  294. case 0x00002567: /* ╧ */
  295. case 0x00002568: /* ╨ */
  296. case 0x00002569: /* ╩ */
  297. case 0x0000253c: /* ┼ */
  298. case 0x0000256a: /* ╪ */
  299. case 0x0000256b: /* ╫ */
  300. case 0x0000256c: /* ╬ */
  301. return '+';
  302. case 0x00002510: /* ┐ */
  303. case 0x00002555: /* ╕ */
  304. case 0x00002556: /* ╖ */
  305. case 0x00002557: /* ╗ */
  306. case 0x00002518: /* ┘ */
  307. case 0x0000255b: /* ╛ */
  308. case 0x0000255c: /* ╜ */
  309. case 0x0000255d: /* ╝ */
  310. case 0x00002524: /* ┤ */
  311. case 0x00002561: /* ╡ */
  312. case 0x00002562: /* ╢ */
  313. case 0x00002563: /* ╣ */
  314. return '+';
  315. case 0x00002591: /* ░ */
  316. case 0x00002592: /* ▒ */
  317. case 0x00002593: /* ▓ */
  318. case 0x00002588: /* █ */
  319. case 0x0000258c: /* ▌ */
  320. case 0x00002590: /* ▐ */
  321. case 0x000025a0: /* ■ */
  322. case 0x000025ac: /* ▬ */
  323. case 0x000025ae: /* ▮ */
  324. return '#';
  325. case 0x00002580: /* ▀ */
  326. return '"';
  327. case 0x00002584: /* ▄ */
  328. return ',';
  329. case 0x000025c6: /* ◆ */
  330. case 0x00002666: /* ♦ */
  331. return '+';
  332. case 0x00002022: /* • */
  333. case 0x000025cb: /* ○ */
  334. case 0x000025cf: /* ● */
  335. case 0x00002603: /* ☃ */
  336. case 0x0000263c: /* ☼ */
  337. return 'o';
  338. case 0x0000301c: /* 〜 */
  339. return '~';
  340. }
  341. return '?';
  342. }
  343. /** \brief Tell whether a UTF-32 character is fullwidth.
  344. *
  345. * Check whether the given UTF-32 character should be printed at twice
  346. * the normal width (fullwidth characters). If the character is unknown
  347. * or if its status cannot be decided, it is treated as a standard-width
  348. * character.
  349. *
  350. * This function never fails.
  351. *
  352. * \param ch The UTF-32 character.
  353. * \return 1 if the character is fullwidth, 0 otherwise.
  354. */
  355. int caca_utf32_is_fullwidth(uint32_t ch)
  356. {
  357. if(ch < 0x2e80) /* Standard stuff */
  358. return 0;
  359. if(ch < 0xa700) /* Japanese, Korean, CJK, Yi... */
  360. return 1;
  361. if(ch < 0xac00) /* Modified Tone Letters, Syloti Nagri */
  362. return 0;
  363. if(ch < 0xd800) /* Hangul Syllables */
  364. return 1;
  365. if(ch < 0xf900) /* Misc crap */
  366. return 0;
  367. if(ch < 0xfb00) /* More CJK */
  368. return 1;
  369. if(ch < 0xfe20) /* Misc crap */
  370. return 0;
  371. if(ch < 0xfe70) /* More CJK */
  372. return 1;
  373. if(ch < 0xff00) /* Misc crap */
  374. return 0;
  375. if(ch < 0xff61) /* Fullwidth forms */
  376. return 1;
  377. if(ch < 0xffe0) /* Halfwidth forms */
  378. return 0;
  379. if(ch < 0xffe8) /* More fullwidth forms */
  380. return 1;
  381. if(ch < 0x20000) /* Misc crap */
  382. return 0;
  383. if(ch < 0xe0000) /* More CJK */
  384. return 1;
  385. return 0;
  386. }
  387. /*
  388. * XXX: The following functions are aliases.
  389. */
  390. uint32_t cucul_utf8_to_utf32(char const *, size_t *)
  391. CACA_ALIAS(caca_utf8_to_utf32);
  392. size_t cucul_utf32_to_utf8(char *, uint32_t) CACA_ALIAS(caca_utf32_to_utf8);
  393. uint8_t cucul_utf32_to_cp437(uint32_t) CACA_ALIAS(caca_utf32_to_cp437);
  394. uint32_t cucul_cp437_to_utf32(uint8_t) CACA_ALIAS(caca_cp437_to_utf32);
  395. char cucul_utf32_to_ascii(uint32_t) CACA_ALIAS(caca_utf32_to_ascii);
  396. int cucul_utf32_is_fullwidth(uint32_t) CACA_ALIAS(caca_utf32_is_fullwidth);