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.
 
 
 
 
 
 

183 líneas
5.3 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; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * 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 "cucul.h"
  22. #include "cucul_internals.h"
  23. static char const trailing[256] =
  24. {
  25. 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,
  26. 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,
  27. 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,
  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. 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,
  32. 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
  33. };
  34. static uint32_t const offsets[6] =
  35. {
  36. 0x00000000UL, 0x00003080UL, 0x000E2080UL,
  37. 0x03C82080UL, 0xFA082080UL, 0x82082080UL
  38. };
  39. unsigned int _cucul_strlen_utf8(char const *s)
  40. {
  41. int len = 0;
  42. char const *parser = s;
  43. while(*parser)
  44. {
  45. int i;
  46. int bytes = 1 + trailing[(int)(unsigned char)*parser];
  47. for(i = 1; i < bytes; i++)
  48. if(!parser[i])
  49. return len;
  50. parser += bytes;
  51. len++;
  52. }
  53. return len;
  54. }
  55. char const *_cucul_skip_utf8(char const *s, unsigned int x)
  56. {
  57. char const *parser = s;
  58. while(x)
  59. {
  60. int i;
  61. int bytes = 1 + trailing[(int)(unsigned char)*parser];
  62. for(i = 1; i < bytes; i++)
  63. if(!parser[i])
  64. return parser;
  65. parser += bytes;
  66. x--;
  67. }
  68. return parser;
  69. }
  70. uint32_t _cucul_utf8_to_utf32(char const *s)
  71. {
  72. int bytes = trailing[(int)(unsigned char)*s];
  73. uint32_t ret = 0;
  74. switch(bytes)
  75. {
  76. /* FIXME: do something for invalid sequences (4 and 5) */
  77. case 3: ret += (uint8_t)*s++; ret <<= 6;
  78. case 2: ret += (uint8_t)*s++; ret <<= 6;
  79. case 1: ret += (uint8_t)*s++; ret <<= 6;
  80. case 0: ret += (uint8_t)*s++;
  81. }
  82. ret -= offsets[bytes];
  83. return ret;
  84. }
  85. /*
  86. * CP437 handling
  87. */
  88. static uint32_t const cp437_lookup1[] =
  89. {
  90. /* 0x01 - 0x0f: ☺ ☻ ♥ ♦ ♣ ♠ • ◘ ○ ◙ ♂ ♀ ♪ ♫ ☼ */
  91. 0x263a, 0x263b, 0x2665, 0x2666, 0x2663, 0x2660, 0x2022,
  92. 0x25d8, 0x25cb, 0x25d9, 0x2642, 0x2640, 0x266a, 0x266b, 0x263c,
  93. /* 0x10 - 0x1f: ► ◄ ↕ ‼ ¶ § ▬ ↨ ↑ ↓ → ← ∟ ↔ ▲ ▼ */
  94. 0x25ba, 0x25c4, 0x2195, 0x203c, 0xb6, 0xa7, 0x25ac, 0x21a8,
  95. 0x2191, 0x2193, 0x2192, 0x2190, 0x221f, 0x2194, 0x25b2, 0x25bc
  96. };
  97. static uint32_t const cp437_lookup2[] =
  98. {
  99. /* 0x7f: ⌂ */
  100. 0x2302,
  101. /* 0x80 - 0x8f: Ç ü é â ä à å ç ê ë è ï î ì Ä Å */
  102. 0xc7, 0xfc, 0xe9, 0xe2, 0xe4, 0xe0, 0xe5, 0xe7,
  103. 0xea, 0xeb, 0xe8, 0xef, 0xee, 0xec, 0xc4, 0xc5,
  104. /* 0x90 - 0x9f: É æ Æ ô ö ò û ù ÿ Ö Ü ¢ £ ¥ ₧ ƒ */
  105. 0xc9, 0xe6, 0xc6, 0xf4, 0xf6, 0xf2, 0xfb, 0xf9,
  106. 0xff, 0xd6, 0xdc, 0xa2, 0xa3, 0xa5, 0x20a7, 0x192,
  107. /* 0xa0 - 0xaf: á í ó ú ñ Ñ ª º ¿ ⌐ ¬ ½ ¼ ¡ « » */
  108. 0xe1, 0xed, 0xf3, 0xfa, 0xf1, 0xd1, 0xaa, 0xba,
  109. 0xbf, 0x2310, 0xac, 0xbd, 0xbc, 0xa1, 0xab, 0xbb,
  110. /* 0xb0 - 0xbf: ░ ▒ ▓ │ ┤ ╡ ╢ ╖ ╕ ╣ ║ ╗ ╝ ╜ ╛ ┐ */
  111. 0x2591, 0x2592, 0x2593, 0x2502, 0x2524, 0x2561, 0x2562, 0x2556,
  112. 0x2555, 0x2563, 0x2551, 0x2557, 0x255d, 0x255c, 0x255b, 0x2510,
  113. /* 0xc0 - 0xcf: └ ┴ ┬ ├ ─ ┼ ╞ ╟ ╚ ╔ ╩ ╦ ╠ ═ ╬ ╧ */
  114. 0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c, 0x255e, 0x255f,
  115. 0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c, 0x2567,
  116. /* 0xd0 - 0xdf: ╨ ╤ ╥ ╙ ╘ ╒ ╓ ╫ ╪ ┘ ┌ █ ▄ ▌ ▐ ▀ */
  117. 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256b,
  118. 0x256a, 0x2518, 0x250c, 0x2588, 0x2584, 0x258c, 0x2590, 0x2580,
  119. /* 0xe0 - 0xef: α ß Γ π Σ σ µ τ Φ Θ Ω δ ∞ φ ε ∩ */
  120. 0x3b1, 0xdf, 0x393, 0x3c0, 0x3a3, 0x3c3, 0xb5, 0x3c4,
  121. 0x3a6, 0x398, 0x3a9, 0x3b4, 0x221e, 0x3c6, 0x3b5, 0x2229,
  122. /* 0xf0 - 0xff: ≡ ± ≥ ≤ ⌠ ⌡ ÷ ≈ ° ∙ · √ ⁿ ² ■ <nbsp> */
  123. 0x2261, 0xb1, 0x2265, 0x2264, 0x2320, 0x2321, 0xf7, 0x2248,
  124. 0xb0, 0x2219, 0xb7, 0x221a, 0x207f, 0xb2, 0x25a0, 0xa0
  125. };
  126. uint8_t _cucul_utf32_to_cp437(uint32_t ch)
  127. {
  128. unsigned int i;
  129. if(ch < 0x00000020)
  130. return '?';
  131. if(ch < 0x00000080)
  132. return ch;
  133. for(i = 0; i < sizeof(cp437_lookup1) / sizeof(*cp437_lookup1); i++)
  134. if(cp437_lookup1[i] == ch)
  135. return 0x01 + i;
  136. for(i = 0; i < sizeof(cp437_lookup2) / sizeof(*cp437_lookup2); i++)
  137. if(cp437_lookup2[i] == ch)
  138. return 0x7f + i;
  139. return '?';
  140. }
  141. uint32_t _cucul_cp437_to_utf32(uint8_t ch)
  142. {
  143. if(ch > 0x7f)
  144. return cp437_lookup2[ch - 0x7f];
  145. if(ch >= 0x20)
  146. return (uint32_t)ch;
  147. if(ch > 0)
  148. return cp437_lookup1[ch - 0x01];
  149. return 0x00000000;
  150. }