Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

463 рядки
12 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 various import functions.
  15. */
  16. #include "config.h"
  17. #include "common.h"
  18. #if !defined(__KERNEL__)
  19. # include <stdio.h>
  20. # include <stdlib.h>
  21. # include <string.h>
  22. #endif
  23. #include "cucul.h"
  24. #include "cucul_internals.h"
  25. static cucul_canvas_t *import_caca(void const *, unsigned int);
  26. static cucul_canvas_t *import_text(void const *, unsigned int);
  27. static cucul_canvas_t *import_ansi(void const *, unsigned int);
  28. /** \brief Import a buffer into a canvas
  29. *
  30. * This function imports a memory area into an internal libcucul canvas.
  31. *
  32. * Valid values for \c format are:
  33. *
  34. * \li \c "": attempt to autodetect the file format.
  35. *
  36. * \li \c "ansi": import ANSI files.
  37. *
  38. * \li \c "caca": import native libcaca files.
  39. *
  40. * \param data The memory area to be loaded into a canvas.
  41. * \param size The length of the memory area.
  42. * \param format A string describing the input format.
  43. * \return A libcucul canvas, or NULL in case of error.
  44. */
  45. cucul_canvas_t * cucul_import_canvas(void const *data, unsigned int size,
  46. char const *format)
  47. {
  48. char const *buf = (char const*) data;
  49. if(size==0 || data==NULL)
  50. return NULL;
  51. if(!strcasecmp("caca", format))
  52. return import_caca(data, size);
  53. if(!strcasecmp("text", format))
  54. return import_text(data, size);
  55. if(!strcasecmp("ansi", format))
  56. return import_ansi(data, size);
  57. /* Autodetection */
  58. if(!strcasecmp("", format))
  59. {
  60. /* if 4 first letters are CACA */
  61. if(size >= 4 &&
  62. buf[0] == 'C' && buf[1] == 'A' && buf[2] == 'C' && buf[3] != 'A')
  63. return import_caca(data, size);
  64. /* If 2 first characters are ESC[ (not reliable at all) */
  65. if(size >= 2 && buf[0] == 0x1b && buf[1] == '[')
  66. return import_ansi(data, size);
  67. /* Otherwise, import it as text */
  68. return import_text(data, size);
  69. }
  70. return NULL;
  71. }
  72. /** \brief Get available import formats
  73. *
  74. * Return a list of available import formats. The list is a NULL-terminated
  75. * array of strings, interleaving a string containing the internal value for
  76. * the import format, to be used with cucul_import_canvas(), and a string
  77. * containing the natural language description for that import format.
  78. *
  79. * \return An array of strings.
  80. */
  81. char const * const * cucul_get_import_list(void)
  82. {
  83. static char const * const list[] =
  84. {
  85. "", "autodetect",
  86. "text", "plain text",
  87. "caca", "native libcaca format",
  88. "ansi", "ANSI coloured text",
  89. NULL, NULL
  90. };
  91. return list;
  92. }
  93. /*
  94. * XXX: the following functions are local.
  95. */
  96. static cucul_canvas_t *import_caca(void const *data, unsigned int size)
  97. {
  98. cucul_canvas_t *cv;
  99. uint8_t const *buf = (uint8_t const *)data;
  100. unsigned int width, height, n;
  101. if(size < 16)
  102. return NULL;
  103. if(buf[0] != 'C' || buf[1] != 'A' || buf[2] != 'C' || buf[3] != 'A')
  104. return NULL;
  105. if(buf[4] != 'C' || buf[5] != 'A' || buf[6] != 'N' || buf[7] != 'V')
  106. return NULL;
  107. width = ((uint32_t)buf[8] << 24) | ((uint32_t)buf[9] << 16)
  108. | ((uint32_t)buf[10] << 8) | (uint32_t)buf[11];
  109. height = ((uint32_t)buf[12] << 24) | ((uint32_t)buf[13] << 16)
  110. | ((uint32_t)buf[14] << 8) | (uint32_t)buf[15];
  111. if(!width || !height)
  112. return NULL;
  113. if(size != 16 + width * height * 8)
  114. return NULL;
  115. cv = cucul_create_canvas(width, height);
  116. if(!cv)
  117. return NULL;
  118. for(n = height * width; n--; )
  119. {
  120. cv->chars[n] = ((uint32_t)buf[16 + 0 + 8 * n] << 24)
  121. | ((uint32_t)buf[16 + 1 + 8 * n] << 16)
  122. | ((uint32_t)buf[16 + 2 + 8 * n] << 8)
  123. | (uint32_t)buf[16 + 3 + 8 * n];
  124. cv->attr[n] = ((uint32_t)buf[16 + 4 + 8 * n] << 24)
  125. | ((uint32_t)buf[16 + 5 + 8 * n] << 16)
  126. | ((uint32_t)buf[16 + 6 + 8 * n] << 8)
  127. | (uint32_t)buf[16 + 7 + 8 * n];
  128. }
  129. return cv;
  130. }
  131. static cucul_canvas_t *import_text(void const *data, unsigned int size)
  132. {
  133. cucul_canvas_t *cv;
  134. char const *text = (char const *)data;
  135. unsigned int width = 1, height = 1, x = 0, y = 0, i;
  136. cv = cucul_create_canvas(width, height);
  137. cucul_set_color(cv, CUCUL_COLOR_DEFAULT, CUCUL_COLOR_TRANSPARENT);
  138. for(i = 0; i < size; i++)
  139. {
  140. unsigned char ch = *text++;
  141. if(ch == '\r')
  142. continue;
  143. if(ch == '\n')
  144. {
  145. x = 0;
  146. y++;
  147. continue;
  148. }
  149. if(x >= width || y >= height)
  150. {
  151. if(x >= width)
  152. width = x + 1;
  153. if(y >= height)
  154. height = y + 1;
  155. cucul_set_canvas_size(cv, width, height);
  156. }
  157. cucul_putchar(cv, x, y, ch);
  158. x++;
  159. }
  160. return cv;
  161. }
  162. #define IS_ALPHA(x) (x>='A' && x<='z')
  163. #define END_TUP 0x1337
  164. unsigned char _get_ansi_command(unsigned char const *buffer, int size);
  165. int _parse_tuple(unsigned int *ret, unsigned char const *buffer, int size);
  166. void _manage_modifiers(char c, int *fg, int *bg, int *save_fg, int *save_bg);
  167. static cucul_canvas_t *import_ansi(void const *data, unsigned int size)
  168. {
  169. cucul_canvas_t *cv;
  170. unsigned char const *buffer = (unsigned char const*)data;
  171. unsigned int i, sent_size = 0;
  172. unsigned char c;
  173. unsigned int count = 0;
  174. unsigned int tuple[1024]; /* Should be enough. Will it be ? */
  175. int x = 0, y = 0, width = 80, height = 25;
  176. int save_x = 0, save_y = 0;
  177. unsigned int j, add = 0;
  178. int fg, bg, save_fg, save_bg;
  179. fg = save_fg = CUCUL_COLOR_LIGHTGRAY;
  180. bg = save_bg = CUCUL_COLOR_BLACK;
  181. cv = cucul_create_canvas(width, height);
  182. for(i = 0; i < size; i++)
  183. {
  184. if((buffer[i] == 0x1b) && (buffer[i + 1] == '[')) /* ESC code */
  185. {
  186. i++; // ESC
  187. i++; // [
  188. sent_size = size - i;
  189. c = _get_ansi_command(&buffer[i], sent_size);
  190. add = _parse_tuple(tuple, &buffer[i], sent_size);
  191. count = 0;
  192. while(tuple[count] != END_TUP)
  193. count++; /* Gruik */
  194. switch(c)
  195. {
  196. case 'f':
  197. case 'H':
  198. if(tuple[0] != END_TUP)
  199. {
  200. y = tuple[0] - 1;
  201. x = tuple[1] == END_TUP ? 0 : tuple[1] - 1;
  202. }
  203. else
  204. {
  205. y = 0;
  206. x = 0;
  207. }
  208. break;
  209. case 'A':
  210. y -= tuple[0] == END_TUP ? 1 : tuple[0];
  211. if(y < 0)
  212. y = 0;
  213. break;
  214. case 'B':
  215. y += tuple[0] == END_TUP ? 1 : tuple[0];
  216. break;
  217. case 'C':
  218. x += tuple[0] == END_TUP ? 1 : tuple[0];
  219. break;
  220. case 'D':
  221. x -= tuple[0] == END_TUP ? 1 : tuple[0];
  222. if(x < 0)
  223. x = 0;
  224. break;
  225. case 's':
  226. save_x = x;
  227. save_y = y;
  228. break;
  229. case 'u':
  230. x = save_x;
  231. y = save_y;
  232. break;
  233. case 'J':
  234. if(tuple[0] == 2)
  235. {
  236. x = 0;
  237. y = 0;
  238. }
  239. break;
  240. case 'K':
  241. // CLEAR END OF LINE
  242. break;
  243. case 'm':
  244. for(j = 0; j < count; j++)
  245. _manage_modifiers(tuple[j], &fg, &bg, &save_fg, &save_bg);
  246. cucul_set_color(cv, fg, bg);
  247. break;
  248. default:
  249. /*printf("Unknow command %c (%c)\n", c, IS_ALPHA(c)?c:'.');*/
  250. break;
  251. }
  252. }
  253. else
  254. {
  255. if(buffer[i] == '\n')
  256. {
  257. x = 0;
  258. y++;
  259. }
  260. else if(buffer[i] == '\r')
  261. {
  262. // DOS sucks.
  263. }
  264. else
  265. {
  266. if((buffer[i] >= 0x20) || (buffer[i] <= 0x7E))
  267. {
  268. _cucul_putchar32(cv, x, y,_cucul_cp437_to_utf32(buffer[i]));
  269. // cucul_putchar(cv, x, y, buffer[i]);
  270. }
  271. else
  272. cucul_putchar(cv, x, y, '?');
  273. x++;
  274. }
  275. }
  276. if(x >= width || y >= height)
  277. {
  278. if(x >= width)
  279. width = x + 1;
  280. if(y >= height)
  281. height = y + 1;
  282. cucul_set_canvas_size(cv, width, height);
  283. }
  284. i += add; // add is tuple char count, then +[ +command
  285. add = 0;
  286. }
  287. return cv;
  288. }
  289. /* XXX : ANSI loader helpers */
  290. unsigned char _get_ansi_command(unsigned char const *buffer, int size)
  291. {
  292. int i;
  293. for(i = 0; i < size; i++)
  294. if(IS_ALPHA(buffer[i]))
  295. return buffer[i];
  296. return 0;
  297. }
  298. int _parse_tuple(unsigned int *ret, unsigned char const *buffer, int size)
  299. {
  300. int i = 0;
  301. int j = 0;
  302. int t = 0;
  303. unsigned char nbr[1024];
  304. ret[0] = END_TUP;
  305. for(i = 0; i < size; i++)
  306. {
  307. if(IS_ALPHA(buffer[i]))
  308. {
  309. if(j != 0)
  310. {
  311. ret[t] = atoi((char*)nbr);
  312. t++;
  313. }
  314. ret[t] = END_TUP;
  315. j = 0;
  316. return i;
  317. }
  318. if(buffer[i] != ';')
  319. {
  320. nbr[j] = buffer[i];
  321. nbr[j + 1] = 0;
  322. j++;
  323. }
  324. else
  325. {
  326. ret[t] = atoi((char*)nbr);
  327. t++;
  328. ret[t] = END_TUP;
  329. j = 0;
  330. }
  331. }
  332. return size;
  333. }
  334. void _manage_modifiers(char c, int *fg, int *bg, int *save_fg, int *save_bg)
  335. {
  336. switch(c)
  337. {
  338. case 0:
  339. *fg = CUCUL_COLOR_DEFAULT;
  340. *bg = CUCUL_COLOR_DEFAULT;
  341. break;
  342. case 1: // BOLD
  343. break;
  344. case 4: // Underline
  345. break;
  346. case 5: // blink
  347. break;
  348. case 7: // reverse
  349. *fg = 15 - *fg;
  350. *bg = 15 - *bg;
  351. break;
  352. case 8: // invisible
  353. *save_fg = *fg;
  354. *save_bg = *bg;
  355. *fg = CUCUL_COLOR_TRANSPARENT;
  356. *bg = CUCUL_COLOR_TRANSPARENT;
  357. break;
  358. case 28: // not invisible
  359. *fg = *save_fg;
  360. *bg = *save_bg;
  361. break;
  362. case 30: *fg = CUCUL_COLOR_BLACK; break;
  363. case 31: *fg = CUCUL_COLOR_RED; break;
  364. case 32: *fg = CUCUL_COLOR_GREEN; break;
  365. case 33: *fg = CUCUL_COLOR_BROWN; break;
  366. case 34: *fg = CUCUL_COLOR_BLUE; break;
  367. case 35: *fg = CUCUL_COLOR_MAGENTA; break;
  368. case 36: *fg = CUCUL_COLOR_CYAN; break;
  369. case 37: *fg = CUCUL_COLOR_WHITE; break;
  370. case 39: *fg = CUCUL_COLOR_LIGHTGRAY; break;
  371. case 40: *bg = CUCUL_COLOR_BLACK; break;
  372. case 41: *bg = CUCUL_COLOR_RED; break;
  373. case 42: *bg = CUCUL_COLOR_GREEN; break;
  374. case 43: *bg = CUCUL_COLOR_BROWN; break;
  375. case 44: *bg = CUCUL_COLOR_BLUE; break;
  376. case 45: *bg = CUCUL_COLOR_MAGENTA; break;
  377. case 46: *bg = CUCUL_COLOR_CYAN; break;
  378. case 47: *bg = CUCUL_COLOR_WHITE; break;
  379. case 49: *bg = CUCUL_COLOR_BLACK; break;
  380. case 90: *fg = CUCUL_COLOR_DARKGRAY; break;
  381. case 91: *fg = CUCUL_COLOR_LIGHTRED; break;
  382. case 92: *fg = CUCUL_COLOR_LIGHTGREEN; break;
  383. case 93: *fg = CUCUL_COLOR_YELLOW; break;
  384. case 94: *fg = CUCUL_COLOR_LIGHTBLUE; break;
  385. case 95: *fg = CUCUL_COLOR_LIGHTMAGENTA; break;
  386. case 96: *fg = CUCUL_COLOR_LIGHTCYAN; break;
  387. case 97: *fg = CUCUL_COLOR_WHITE; break;
  388. case 100: *bg = CUCUL_COLOR_DARKGRAY; break;
  389. case 101: *bg = CUCUL_COLOR_LIGHTRED; break;
  390. case 102: *bg = CUCUL_COLOR_LIGHTGREEN; break;
  391. case 103: *bg = CUCUL_COLOR_YELLOW; break;
  392. case 104: *bg = CUCUL_COLOR_LIGHTBLUE; break;
  393. case 105: *bg = CUCUL_COLOR_LIGHTMAGENTA; break;
  394. case 106: *bg = CUCUL_COLOR_LIGHTCYAN; break;
  395. case 107: *bg = CUCUL_COLOR_WHITE; break;
  396. default:
  397. /* printf("Unknow option to 'm' %d (%c)\n", c, IS_ALPHA(c)?c:'.'); */
  398. break;
  399. }
  400. }