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.
 
 
 
 
 
 

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