Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

422 wiersze
12 KiB

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