You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

508 lines
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. # if defined(HAVE_ERRNO_H)
  20. # include <errno.h>
  21. # endif
  22. # include <stdio.h>
  23. # include <stdlib.h>
  24. # include <string.h>
  25. #endif
  26. #include "cucul.h"
  27. #include "cucul_internals.h"
  28. /* ANSI Graphic Rendition Combination Mode */
  29. struct ansi_grcm
  30. {
  31. uint8_t fg, bg; /* ANSI-context fg/bg */
  32. uint8_t efg, ebg; /* Effective (libcucul) fg/bg */
  33. uint8_t bold, negative, concealed;
  34. };
  35. static cucul_canvas_t *import_caca(void const *, unsigned int);
  36. static cucul_canvas_t *import_text(void const *, unsigned int);
  37. static cucul_canvas_t *import_ansi(void const *, unsigned int);
  38. static void ansi_parse_grcm(cucul_canvas_t *, struct ansi_grcm *,
  39. unsigned int, unsigned int const *);
  40. /** \brief Import a buffer into a canvas
  41. *
  42. * This function imports a libcucul buffer as returned by cucul_load_memory()
  43. * or cucul_load_file() into an internal libcucul canvas.
  44. *
  45. * Valid values for \c format are:
  46. *
  47. * \li \c "": attempt to autodetect the file format.
  48. *
  49. * \li \c "ansi": import ANSI files.
  50. *
  51. * \li \c "caca": import native libcaca files.
  52. *
  53. * If an error occurs, NULL is returned and \b errno is set accordingly:
  54. * - \c ENOMEM Not enough memory to allocate canvas.
  55. * - \c EINVAL Invalid format requested.
  56. *
  57. * \param buffer A \e libcucul buffer containing the data to be loaded
  58. * into a canvas.
  59. * \param format A string describing the input format.
  60. * \return A libcucul canvas, or NULL in case of error.
  61. */
  62. cucul_canvas_t * cucul_import_canvas(cucul_buffer_t *buffer, char const *format)
  63. {
  64. char const *buf = (char const*)buffer->data;
  65. if(!strcasecmp("caca", format))
  66. return import_caca(buffer->data, buffer->size);
  67. if(!strcasecmp("text", format))
  68. return import_text(buffer->data, buffer->size);
  69. if(!strcasecmp("ansi", format))
  70. return import_ansi(buffer->data, buffer->size);
  71. /* Autodetection */
  72. if(!strcasecmp("", format))
  73. {
  74. unsigned int i;
  75. /* If 4 first letters are CACA */
  76. if(buffer->size >= 4 &&
  77. buf[0] == 'C' && buf[1] == 'A' && buf[2] == 'C' && buf[3] != 'A')
  78. return import_caca(buffer->data, buffer->size);
  79. /* If we find ESC[ argv, we guess it's an ANSI file */
  80. for(i = 0; i < buffer->size - 1; i++)
  81. if((buf[i] == 0x1b) && (buf[i + 1] == '['))
  82. return import_ansi(buffer->data, buffer->size);
  83. /* Otherwise, import it as text */
  84. return import_text(buffer->data, buffer->size);
  85. }
  86. #if defined(HAVE_ERRNO_H)
  87. errno = EINVAL;
  88. #endif
  89. return NULL;
  90. }
  91. /** \brief Get available import formats
  92. *
  93. * Return a list of available import formats. The list is a NULL-terminated
  94. * array of strings, interleaving a string containing the internal value for
  95. * the import format, to be used with cucul_import_canvas(), and a string
  96. * containing the natural language description for that import format.
  97. *
  98. * This function never fails.
  99. *
  100. * \return An array of strings.
  101. */
  102. char const * const * cucul_get_import_list(void)
  103. {
  104. static char const * const list[] =
  105. {
  106. "", "autodetect",
  107. "text", "plain text",
  108. "caca", "native libcaca format",
  109. "ansi", "ANSI coloured text",
  110. NULL, NULL
  111. };
  112. return list;
  113. }
  114. /*
  115. * XXX: the following functions are local.
  116. */
  117. static cucul_canvas_t *import_caca(void const *data, unsigned int size)
  118. {
  119. cucul_canvas_t *cv;
  120. uint8_t const *buf = (uint8_t const *)data;
  121. unsigned int width, height, n;
  122. if(size < 16)
  123. goto invalid_caca;
  124. if(buf[0] != 'C' || buf[1] != 'A' || buf[2] != 'C' || buf[3] != 'A')
  125. goto invalid_caca;
  126. if(buf[4] != 'C' || buf[5] != 'A' || buf[6] != 'N' || buf[7] != 'V')
  127. goto invalid_caca;
  128. width = ((uint32_t)buf[8] << 24) | ((uint32_t)buf[9] << 16)
  129. | ((uint32_t)buf[10] << 8) | (uint32_t)buf[11];
  130. height = ((uint32_t)buf[12] << 24) | ((uint32_t)buf[13] << 16)
  131. | ((uint32_t)buf[14] << 8) | (uint32_t)buf[15];
  132. if(!width || !height)
  133. goto invalid_caca;
  134. if(size != 16 + width * height * 8)
  135. goto invalid_caca;
  136. cv = cucul_create_canvas(width, height);
  137. if(!cv)
  138. {
  139. #if defined(HAVE_ERRNO_H)
  140. errno = ENOMEM;
  141. #endif
  142. return NULL;
  143. }
  144. for(n = height * width; n--; )
  145. {
  146. cv->chars[n] = ((uint32_t)buf[16 + 0 + 8 * n] << 24)
  147. | ((uint32_t)buf[16 + 1 + 8 * n] << 16)
  148. | ((uint32_t)buf[16 + 2 + 8 * n] << 8)
  149. | (uint32_t)buf[16 + 3 + 8 * n];
  150. cv->attr[n] = ((uint32_t)buf[16 + 4 + 8 * n] << 24)
  151. | ((uint32_t)buf[16 + 5 + 8 * n] << 16)
  152. | ((uint32_t)buf[16 + 6 + 8 * n] << 8)
  153. | (uint32_t)buf[16 + 7 + 8 * n];
  154. }
  155. return cv;
  156. invalid_caca:
  157. #if defined(HAVE_ERRNO_H)
  158. errno = EINVAL;
  159. #endif
  160. return NULL;
  161. }
  162. static cucul_canvas_t *import_text(void const *data, unsigned int size)
  163. {
  164. cucul_canvas_t *cv;
  165. char const *text = (char const *)data;
  166. unsigned int width = 1, height = 1, x = 0, y = 0, i;
  167. cv = cucul_create_canvas(width, height);
  168. if(!cv)
  169. {
  170. #if defined(HAVE_ERRNO_H)
  171. errno = ENOMEM;
  172. #endif
  173. return NULL;
  174. }
  175. cucul_set_color(cv, CUCUL_COLOR_DEFAULT, CUCUL_COLOR_TRANSPARENT);
  176. for(i = 0; i < size; i++)
  177. {
  178. unsigned char ch = *text++;
  179. if(ch == '\r')
  180. continue;
  181. if(ch == '\n')
  182. {
  183. x = 0;
  184. y++;
  185. continue;
  186. }
  187. if(x >= width || y >= height)
  188. {
  189. if(x >= width)
  190. width = x + 1;
  191. if(y >= height)
  192. height = y + 1;
  193. cucul_set_canvas_size(cv, width, height);
  194. }
  195. cucul_putchar(cv, x, y, ch);
  196. x++;
  197. }
  198. return cv;
  199. }
  200. static cucul_canvas_t *import_ansi(void const *data, unsigned int size)
  201. {
  202. struct ansi_grcm grcm;
  203. unsigned char const *buffer = (unsigned char const*)data;
  204. cucul_canvas_t *cv;
  205. unsigned int i, j, skip, dummy = 0;
  206. unsigned int width = 1, height = 1;
  207. int x = 0, y = 0, save_x = 0, save_y = 0;
  208. cv = cucul_create_canvas(width, height);
  209. if(!cv)
  210. {
  211. #if defined(HAVE_ERRNO_H)
  212. errno = ENOMEM;
  213. #endif
  214. return NULL;
  215. }
  216. ansi_parse_grcm(cv, &grcm, 1, &dummy);
  217. for(i = 0; i < size; i += skip)
  218. {
  219. skip = 1;
  220. /* Wrap long lines */
  221. if((unsigned int)x >= 80)
  222. {
  223. x = 0;
  224. y++;
  225. }
  226. if(buffer[i] == '\x1a' && size - i >= 8
  227. && !memcmp(buffer + i + 1, "SAUCE00", 7))
  228. break; /* End before SAUCE data */
  229. if(buffer[i] == '\r')
  230. continue; /* DOS sucks */
  231. if(buffer[i] == '\n')
  232. {
  233. x = 0;
  234. y++;
  235. continue;
  236. }
  237. /* Interpret escape commands, as per Standard ECMA-48 "Control
  238. * Functions for Coded Character Sets", 5.4. Control sequences. */
  239. if(buffer[i] == '\x1b' && buffer[i + 1] == '[')
  240. {
  241. unsigned int argc = 0, argv[101];
  242. unsigned int param, inter, final;
  243. /* Compute offsets to parameter bytes, intermediate bytes and
  244. * to the final byte. Only the final byte is mandatory, there
  245. * can be zero of the others.
  246. * 0 param=2 inter final final+1
  247. * +-----+------------------+---------------------+-----------------+
  248. * | CSI | parameter bytes | intermediate bytes | final byte |
  249. * | | 0x30 - 0x3f | 0x20 - 0x2f | 0x40 - 0x7e |
  250. * | ^[[ | 0123456789:;<=>? | SPC !"#$%&'()*+,-./ | azAZ@[\]^_`{|}~ |
  251. * +-----+------------------+---------------------+-----------------+
  252. */
  253. param = 2;
  254. for(inter = param; i + inter < size; inter++)
  255. if(buffer[i + inter] < 0x30 || buffer[i + inter] > 0x3f)
  256. break;
  257. for(final = inter; i + final < size; final++)
  258. if(buffer[i + final] < 0x20 || buffer[i + final] > 0x2f)
  259. break;
  260. if(buffer[i + final] < 0x40 || buffer[i + final] > 0x7e)
  261. break; /* Invalid Final Byte */
  262. skip += final;
  263. /* Sanity checks */
  264. if(param < inter && buffer[i + param] >= 0x3c)
  265. {
  266. fprintf(stderr, "private sequence \"^[[%.*s\"\n",
  267. final - param + 1, buffer + i + param);
  268. continue; /* Private sequence, skip it entirely */
  269. }
  270. if(final - param > 100)
  271. continue; /* Suspiciously long sequence, skip it */
  272. /* Parse parameter bytes as per ECMA-48 5.4.2: Parameter string
  273. * format */
  274. if(param < inter)
  275. {
  276. argv[0] = 0;
  277. for(j = param; j < inter; j++)
  278. {
  279. if(buffer[i + j] == ';')
  280. argv[++argc] = 0;
  281. else if(buffer[i + j] >= '0' && buffer[i + j] <= '9')
  282. argv[argc] = 10 * argv[argc] + (buffer[i + j] - '0');
  283. }
  284. argc++;
  285. }
  286. /* Interpret final byte. The code representations are given in
  287. * ECMA-48 5.4: Control sequences, and the code definitions are
  288. * given in ECMA-48 8.3: Definition of control functions. */
  289. switch(buffer[i + final])
  290. {
  291. case 'f': /* CUP - Cursor Position */
  292. case 'H': /* HVP - Character And Line Position */
  293. x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0;
  294. y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0;
  295. break;
  296. case 'A': /* CUU - Cursor Up */
  297. y -= argc ? argv[0] : 1;
  298. if(y < 0)
  299. y = 0;
  300. break;
  301. case 'B': /* CUD - Cursor Down */
  302. y += argc ? argv[0] : 1;
  303. break;
  304. case 'C': /* CUF - Cursor Right */
  305. x += argc ? argv[0] : 1;
  306. break;
  307. case 'D': /* CUB - Cursor Left */
  308. x -= argc ? argv[0] : 1;
  309. if(x < 0)
  310. x = 0;
  311. break;
  312. case 's': /* Private (save cursor position) */
  313. save_x = x;
  314. save_y = y;
  315. break;
  316. case 'u': /* Private (reload cursor position) */
  317. x = save_x;
  318. y = save_y;
  319. break;
  320. case 'J': /* ED - Erase In Page */
  321. if(argv[0] == 2)
  322. x = y = 0;
  323. break;
  324. case 'K': /* EL - Erase In Line */
  325. if(width < 80)
  326. cucul_set_color(cv, CUCUL_COLOR_DEFAULT,
  327. CUCUL_COLOR_TRANSPARENT);
  328. cucul_set_canvas_size(cv, width = 80, height);
  329. for(j = x; j < 80; j++)
  330. cucul_putchar(cv, j, y, ' ');
  331. x = 80;
  332. break;
  333. case 'm': /* SGR - Select Graphic Rendition */
  334. ansi_parse_grcm(cv, &grcm, argc, argv);
  335. break;
  336. default:
  337. fprintf(stderr, "unknown command %c\n", buffer[i + final]);
  338. break;
  339. }
  340. continue;
  341. }
  342. /* We're going to paste a character. First make sure the canvas
  343. * is big enough. */
  344. if((unsigned int)x >= width)
  345. {
  346. cucul_set_color(cv, CUCUL_COLOR_DEFAULT, CUCUL_COLOR_TRANSPARENT);
  347. cucul_set_canvas_size(cv, width = x + 1, height);
  348. }
  349. if((unsigned int)y >= height)
  350. {
  351. cucul_set_color(cv, CUCUL_COLOR_DEFAULT, CUCUL_COLOR_TRANSPARENT);
  352. cucul_set_canvas_size(cv, width, height = y + 1);
  353. }
  354. /* Now paste our character */
  355. cucul_set_color(cv, grcm.efg, grcm.ebg);
  356. cucul_putchar(cv, x, y, cucul_cp437_to_utf32(buffer[i]));
  357. x++;
  358. }
  359. return cv;
  360. }
  361. /* XXX : ANSI loader helper */
  362. static void ansi_parse_grcm(cucul_canvas_t *cv, struct ansi_grcm *g,
  363. unsigned int argc, unsigned int const *argv)
  364. {
  365. static uint8_t const ansi2cucul[] =
  366. {
  367. CUCUL_COLOR_BLACK, CUCUL_COLOR_RED,
  368. CUCUL_COLOR_GREEN, CUCUL_COLOR_BROWN,
  369. CUCUL_COLOR_BLUE, CUCUL_COLOR_MAGENTA,
  370. CUCUL_COLOR_CYAN, CUCUL_COLOR_LIGHTGRAY
  371. };
  372. unsigned int j;
  373. for(j = 0; j < argc; j++)
  374. {
  375. /* Defined in ECMA-48 8.3.117: SGR - SELECT GRAPHIC RENDITION */
  376. if(argv[j] >= 30 && argv[j] <= 37)
  377. g->fg = ansi2cucul[argv[j] - 30];
  378. else if(argv[j] >= 40 && argv[j] <= 47)
  379. g->bg = ansi2cucul[argv[j] - 40];
  380. else if(argv[j] >= 90 && argv[j] <= 97)
  381. g->fg = ansi2cucul[argv[j] - 90] + 8;
  382. else if(argv[j] >= 100 && argv[j] <= 107)
  383. g->bg = ansi2cucul[argv[j] - 100] + 8;
  384. else switch(argv[j])
  385. {
  386. case 0: /* default rendition */
  387. g->fg = CUCUL_COLOR_DEFAULT;
  388. g->bg = CUCUL_COLOR_DEFAULT;
  389. g->bold = g->negative = g->concealed = 0;
  390. break;
  391. case 1: /* bold or increased intensity */
  392. g->bold = 1;
  393. break;
  394. case 4: /* singly underlined */
  395. break;
  396. case 5: /* slowly blinking (less then 150 per minute) */
  397. break;
  398. case 7: /* negative image */
  399. g->negative = 1;
  400. break;
  401. case 8: /* concealed characters */
  402. g->concealed = 1;
  403. break;
  404. case 22: /* normal colour or normal intensity (neither bold nor faint) */
  405. g->bold = 0;
  406. break;
  407. case 28: /* revealed characters */
  408. g->concealed = 0;
  409. break;
  410. case 39: /* default display colour (implementation-defined) */
  411. g->fg = CUCUL_COLOR_DEFAULT;
  412. break;
  413. case 49: /* default background colour (implementation-defined) */
  414. g->bg = CUCUL_COLOR_DEFAULT;
  415. break;
  416. default:
  417. fprintf(stderr, "unknown sgr %i\n", argv[j]);
  418. break;
  419. }
  420. }
  421. if(g->concealed)
  422. {
  423. g->efg = g->ebg = CUCUL_COLOR_TRANSPARENT;
  424. }
  425. else
  426. {
  427. g->efg = g->negative ? g->bg : g->fg;
  428. g->ebg = g->negative ? g->fg : g->bg;
  429. if(g->bold)
  430. {
  431. if(g->efg < 8)
  432. g->efg += 8;
  433. else if(g->efg == CUCUL_COLOR_DEFAULT)
  434. g->efg = CUCUL_COLOR_WHITE;
  435. }
  436. }
  437. }