No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

284 líneas
8.6 KiB

  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * This file contains functions for converting characters between
  16. * various character sets.
  17. */
  18. #include "config.h"
  19. #include "common.h"
  20. #if !defined(__KERNEL__)
  21. # include <string.h>
  22. #endif
  23. #include "cucul.h"
  24. #include "cucul_internals.h"
  25. /*
  26. * UTF-8 handling
  27. */
  28. static char 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 read A pointer to an unsigned integer to store the number of
  99. * bytes in the character, or NULL.
  100. * \return The corresponding UTF-32 character, or zero if the character
  101. * is incomplete.
  102. */
  103. unsigned long int cucul_utf8_to_utf32(char const *s, unsigned int *read)
  104. {
  105. unsigned int bytes = trailing[(int)(unsigned char)*s];
  106. unsigned int i = 0;
  107. uint32_t ret = 0;
  108. for(;;)
  109. {
  110. if(!*s)
  111. {
  112. if(read)
  113. *read = 0;
  114. return 0;
  115. }
  116. ret += ((uint32_t)(unsigned char)*s++) << (6 * (bytes - i));
  117. if(bytes == i++)
  118. {
  119. if(read)
  120. *read = i;
  121. return ret - offsets[bytes];
  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. unsigned int cucul_utf32_to_utf8(char *buf, unsigned long int ch)
  139. {
  140. static const uint8_t mark[7] =
  141. {
  142. 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC
  143. };
  144. char *parser = buf;
  145. int 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. unsigned char cucul_utf32_to_cp437(unsigned long int 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. unsigned long int cucul_cp437_to_utf32(unsigned char 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 Tell whether a UTF-32 character is fullwidth.
  209. *
  210. * Check whether the given UTF-32 character should be printed at twice
  211. * the normal width (fullwidth characters). If the character is unknown
  212. * or if its status cannot be decided, it is treated as a standard-width
  213. * character.
  214. *
  215. * This function never fails.
  216. *
  217. * \param ch The UTF-32 character.
  218. * \return 1 if the character is fullwidth, 0 otherwise.
  219. */
  220. int cucul_utf32_is_fullwidth(unsigned long int ch)
  221. {
  222. if(ch < 0x2e80) /* Standard stuff */
  223. return 0;
  224. if(ch < 0xa700) /* Japanese, Korean, CJK, Yi... */
  225. return 1;
  226. if(ch < 0xac00) /* Modified Tone Letters, Syloti Nagri */
  227. return 0;
  228. if(ch < 0xd800) /* Hangul Syllables */
  229. return 1;
  230. if(ch < 0xf900) /* Misc crap */
  231. return 0;
  232. if(ch < 0xfb00) /* More CJK */
  233. return 1;
  234. if(ch < 0xfe20) /* Misc crap */
  235. return 0;
  236. if(ch < 0xfe70) /* More CJK */
  237. return 1;
  238. if(ch < 0xff00) /* Misc crap */
  239. return 0;
  240. if(ch < 0xff61) /* Fullwidth forms */
  241. return 1;
  242. if(ch < 0xffe0) /* Halfwidth forms */
  243. return 0;
  244. if(ch < 0xffe8) /* More fullwidth forms */
  245. return 1;
  246. if(ch < 0x20000) /* Misc crap */
  247. return 0;
  248. if(ch < 0xe0000) /* More CJK */
  249. return 1;
  250. return 0;
  251. }