Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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