Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

479 строки
13 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. width = x + 1;
  152. height = y + 1;
  153. cucul_set_canvas_size(cv, width, height);
  154. }
  155. cucul_putchar(cv, x, y, ch);
  156. x++;
  157. }
  158. return cv;
  159. }
  160. #define IS_ALPHA(x) (x>='A' && x<='z')
  161. void updateCanvasSize(cucul_canvas_t *cv, int x, int y, int *max_x, int *max_y);
  162. unsigned char get_ansi_command(unsigned char const *buffer, int size);
  163. int parse_tuple(unsigned int *ret, unsigned char const *buffer, int size);
  164. void manage_modifiers(char c, int *fg, int *bg, int *old_fg, int *old_bg);
  165. static cucul_canvas_t *import_ansi(void const *data, unsigned int size)
  166. {
  167. cucul_canvas_t *cv;
  168. unsigned char const *buffer = (unsigned char const*)data;
  169. unsigned int i, sent_size = 0;
  170. unsigned char c;
  171. unsigned int count = 0;
  172. unsigned int tuple[1024]; /* Should be enough. Will it be ? */
  173. int x = 0, y = 0, max_x = 80, max_y = 25;
  174. int save_x = 0, save_y = 0;
  175. unsigned int j, add = 0;
  176. int fg, bg, old_fg, old_bg;
  177. fg = old_fg = CUCUL_COLOR_LIGHTGRAY;
  178. bg = old_bg = CUCUL_COLOR_BLACK;
  179. cv = cucul_create_canvas(max_x, max_y);
  180. for(i = 0; i < size; i++)
  181. {
  182. if((buffer[i] == 0x1b) && (buffer[i + 1] == '[')) /* ESC code */
  183. {
  184. i++; // ESC
  185. i++; // [
  186. sent_size = size - i;
  187. c = get_ansi_command(&buffer[i], sent_size);
  188. add = parse_tuple(tuple, &buffer[i], sent_size);
  189. count = 0;
  190. while(tuple[count] != 0x1337)
  191. count++; /* Gruik */
  192. switch(c)
  193. {
  194. case 'f':
  195. case 'H':
  196. if(tuple[0] != 0x1337)
  197. {
  198. x = tuple[0];
  199. if(tuple[1] != 0x1337)
  200. y = tuple[1];
  201. }
  202. else
  203. {
  204. x = 0;
  205. y = 0;
  206. }
  207. updateCanvasSize(cv, x, y, &max_x, &max_y);
  208. break;
  209. case 'A':
  210. if(tuple[0] == 0x1337)
  211. y -= 1;
  212. else
  213. y -= tuple[0];
  214. if(y < 0) y = 0;
  215. updateCanvasSize(cv, x, y, &max_x, &max_y);
  216. break;
  217. case 'B':
  218. if(tuple[0] == 0x1337)
  219. y++;
  220. else
  221. y += tuple[0];
  222. updateCanvasSize(cv, x, y, &max_x, &max_y);
  223. break;
  224. case 'C':
  225. if(tuple[0] == 0x1337)
  226. x++;
  227. else
  228. x += tuple[0];
  229. updateCanvasSize(cv, x, y, &max_x, &max_y);
  230. break;
  231. case 'D':
  232. if(tuple[0] == 0x1337)
  233. x--;
  234. else
  235. x -= tuple[0];
  236. if(x < 0) x = 0;
  237. updateCanvasSize(cv, x, y, &max_x, &max_y);
  238. break;
  239. case 's':
  240. save_x = x;
  241. save_y = y;
  242. break;
  243. case 'u':
  244. x = save_x;
  245. y = save_y;
  246. updateCanvasSize(cv, x, y, &max_x, &max_y);
  247. break;
  248. case 'J':
  249. if(tuple[0] == 2)
  250. {
  251. x = 0;
  252. y = 0;
  253. updateCanvasSize(cv, x, y, &max_x, &max_y);
  254. }
  255. break;
  256. case 'K':
  257. // CLEAR END OF LINE
  258. break;
  259. case 'm':
  260. for(j = 0; j < count; j++)
  261. manage_modifiers(tuple[j], &fg, &bg, &old_fg, &old_bg);
  262. cucul_set_color(cv, fg, bg);
  263. break;
  264. default:
  265. /*printf("Unknow command %c (%c)\n", c, IS_ALPHA(c)?c:'.');*/
  266. break;
  267. }
  268. }
  269. else
  270. {
  271. if(buffer[i] == '\n')
  272. {
  273. x = 0;
  274. y++;
  275. updateCanvasSize(cv, x, y, &max_x, &max_y);
  276. }
  277. else if(buffer[i] == '\r')
  278. {
  279. // DOS sucks.
  280. }
  281. else
  282. {
  283. if((buffer[i] >= 0x20) || (buffer[i] <= 0x7E))
  284. {
  285. _cucul_putchar32(cv, x, y,_cucul_cp437_to_utf32(buffer[i]));
  286. // cucul_putchar(cv, x, y, buffer[i]);
  287. }
  288. else
  289. cucul_putchar(cv, x, y, '?');
  290. x++;
  291. updateCanvasSize(cv, x, y, &max_x, &max_y);
  292. }
  293. }
  294. i += add; // add is tuple char count, then +[ +command
  295. add = 0;
  296. }
  297. return cv;
  298. }
  299. /* XXX : ANSI loader helpers */
  300. unsigned char get_ansi_command(unsigned char const *buffer, int size)
  301. {
  302. int i;
  303. for(i = 0; i < size; i++)
  304. if(IS_ALPHA(buffer[i]))
  305. return buffer[i];
  306. return 0;
  307. }
  308. int parse_tuple(unsigned int *ret, unsigned char const *buffer, int size)
  309. {
  310. int i = 0;
  311. int j = 0;
  312. int t = 0;
  313. unsigned char nbr[1024];
  314. ret[0] = 0x1337;
  315. for(i = 0; i < size; i++)
  316. {
  317. if(IS_ALPHA(buffer[i]))
  318. {
  319. if(j != 0)
  320. {
  321. ret[t] = atoi((char*)nbr);
  322. t++;
  323. }
  324. ret[t] = 0x1337;
  325. j = 0;
  326. return i;
  327. }
  328. if(buffer[i] != ';')
  329. {
  330. nbr[j] = buffer[i];
  331. nbr[j + 1] = 0;
  332. j++;
  333. }
  334. else
  335. {
  336. ret[t] = atoi((char*)nbr);
  337. t++;
  338. ret[t] = 0x1337;
  339. j = 0;
  340. }
  341. }
  342. return size;
  343. }
  344. void manage_modifiers(char c, int *fg, int *bg, int *old_fg, int *old_bg)
  345. {
  346. switch(c)
  347. {
  348. case 0:
  349. *fg = CUCUL_COLOR_DEFAULT;
  350. *bg = CUCUL_COLOR_DEFAULT;
  351. break;
  352. case 1: // BOLD
  353. break;
  354. case 4: // Underline
  355. break;
  356. case 5: // blink
  357. break;
  358. case 7: // reverse
  359. *fg = 15 - *fg;
  360. *bg = 15 - *bg;
  361. break;
  362. case 8: // invisible
  363. *old_fg = *fg;
  364. *old_bg = *bg;
  365. *fg = CUCUL_COLOR_TRANSPARENT;
  366. *bg = CUCUL_COLOR_TRANSPARENT;
  367. break;
  368. case 28: // not invisible
  369. *fg = *old_fg;
  370. *bg = *old_bg;
  371. break;
  372. case 30: *fg = CUCUL_COLOR_BLACK; break;
  373. case 31: *fg = CUCUL_COLOR_RED; break;
  374. case 32: *fg = CUCUL_COLOR_GREEN; break;
  375. case 33: *fg = CUCUL_COLOR_BROWN; break;
  376. case 34: *fg = CUCUL_COLOR_BLUE; break;
  377. case 35: *fg = CUCUL_COLOR_MAGENTA; break;
  378. case 36: *fg = CUCUL_COLOR_CYAN; break;
  379. case 37: *fg = CUCUL_COLOR_WHITE; break;
  380. case 39: *fg = CUCUL_COLOR_LIGHTGRAY; break;
  381. case 40: *bg = CUCUL_COLOR_BLACK; break;
  382. case 41: *bg = CUCUL_COLOR_RED; break;
  383. case 42: *bg = CUCUL_COLOR_GREEN; break;
  384. case 43: *bg = CUCUL_COLOR_BROWN; break;
  385. case 44: *bg = CUCUL_COLOR_BLUE; break;
  386. case 45: *bg = CUCUL_COLOR_MAGENTA; break;
  387. case 46: *bg = CUCUL_COLOR_CYAN; break;
  388. case 47: *bg = CUCUL_COLOR_WHITE; break;
  389. case 49: *bg = CUCUL_COLOR_BLACK; break;
  390. case 90: *fg = CUCUL_COLOR_DARKGRAY; break;
  391. case 91: *fg = CUCUL_COLOR_LIGHTRED; break;
  392. case 92: *fg = CUCUL_COLOR_LIGHTGREEN; break;
  393. case 93: *fg = CUCUL_COLOR_YELLOW; break;
  394. case 94: *fg = CUCUL_COLOR_LIGHTBLUE; break;
  395. case 95: *fg = CUCUL_COLOR_LIGHTMAGENTA; break;
  396. case 96: *fg = CUCUL_COLOR_LIGHTCYAN; break;
  397. case 97: *fg = CUCUL_COLOR_WHITE; break;
  398. case 100: *bg = CUCUL_COLOR_DARKGRAY; break;
  399. case 101: *bg = CUCUL_COLOR_LIGHTRED; break;
  400. case 102: *bg = CUCUL_COLOR_LIGHTGREEN; break;
  401. case 103: *bg = CUCUL_COLOR_YELLOW; break;
  402. case 104: *bg = CUCUL_COLOR_LIGHTBLUE; break;
  403. case 105: *bg = CUCUL_COLOR_LIGHTMAGENTA; break;
  404. case 106: *bg = CUCUL_COLOR_LIGHTCYAN; break;
  405. case 107: *bg = CUCUL_COLOR_WHITE; break;
  406. default:
  407. /* printf("Unknow option to 'm' %d (%c)\n", c, IS_ALPHA(c)?c:'.'); */
  408. break;
  409. }
  410. }
  411. void updateCanvasSize(cucul_canvas_t *cv, int x, int y, int *max_x, int *max_y)
  412. {
  413. if(x > *max_x)
  414. *max_x = x;
  415. if(y > *max_y)
  416. *max_y = y;
  417. cucul_set_canvas_size(cv, *max_x, *max_y);
  418. }