選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

charset.c 5.4 KiB

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