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.
 
 
 
 
 
 

433 regels
13 KiB

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