25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

936 lines
30 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * This file contains various import functions.
  16. */
  17. #include "config.h"
  18. #if !defined __KERNEL__
  19. # include <stdlib.h>
  20. # include <string.h>
  21. # include <stdio.h>
  22. #endif
  23. #include "caca.h"
  24. #include "caca_internals.h"
  25. static inline uint32_t sscanu32(void const *s)
  26. {
  27. uint32_t x;
  28. memcpy(&x, s, 4);
  29. return hton32(x);
  30. }
  31. static inline uint16_t sscanu16(void const *s)
  32. {
  33. uint16_t x;
  34. memcpy(&x, s, 2);
  35. return hton16(x);
  36. }
  37. struct import
  38. {
  39. uint32_t clearattr;
  40. /* ANSI Graphic Rendition Combination Mode */
  41. uint8_t fg, bg; /* ANSI-context fg/bg */
  42. uint8_t dfg, dbg; /* Default fg/bg */
  43. uint8_t bold, blink, italics, negative, concealed, underline;
  44. uint8_t faint, strike, proportional; /* unsupported */
  45. };
  46. static ssize_t import_caca(caca_canvas_t *, void const *, size_t);
  47. static ssize_t import_text(caca_canvas_t *, void const *, size_t);
  48. static ssize_t import_ansi(caca_canvas_t *, void const *, size_t, int);
  49. static void ansi_parse_grcm(caca_canvas_t *, struct import *,
  50. unsigned int, unsigned int const *);
  51. /** \brief Import a memory buffer into a canvas
  52. *
  53. * Import a memory buffer into the given libcaca canvas's current
  54. * frame. The current frame is resized accordingly and its contents are
  55. * replaced with the imported data.
  56. *
  57. * Valid values for \c format are:
  58. * - \c "": attempt to autodetect the file format.
  59. * - \c "caca": import native libcaca files.
  60. * - \c "text": import ASCII text files.
  61. * - \c "ansi": import ANSI files.
  62. * - \c "utf8": import UTF-8 files with ANSI colour codes.
  63. *
  64. * The number of bytes read is returned. If the file format is valid, but
  65. * not enough data was available, 0 is returned.
  66. *
  67. * If an error occurs, -1 is returned and \b errno is set accordingly:
  68. * - \c ENOMEM Not enough memory to allocate canvas.
  69. * - \c EINVAL Invalid format requested.
  70. *
  71. * \param cv A libcaca canvas in which to import the file.
  72. * \param data A memory area containing the data to be loaded into the canvas.
  73. * \param len The size in bytes of the memory area.
  74. * \param format A string describing the input format.
  75. * \return The number of bytes read, or 0 if there was not enough data,
  76. * or -1 if an error occurred.
  77. */
  78. ssize_t caca_import_memory(caca_canvas_t *cv, void const *data,
  79. size_t len, char const *format)
  80. {
  81. if(!strcasecmp("caca", format))
  82. return import_caca(cv, data, len);
  83. if(!strcasecmp("utf8", format))
  84. return import_ansi(cv, data, len, 1);
  85. if(!strcasecmp("text", format))
  86. return import_text(cv, data, len);
  87. if(!strcasecmp("ansi", format))
  88. return import_ansi(cv, data, len, 0);
  89. /* Autodetection */
  90. if(!strcasecmp("", format))
  91. {
  92. unsigned char const *str = data;
  93. unsigned int i;
  94. /* If 4 first bytes are 0xcaca + 'CV' */
  95. if(len >= 4 && str[0] == 0xca &&
  96. str[1] == 0xca && str[2] == 'C' && str[3] == 'V')
  97. return import_caca(cv, data, len);
  98. /* If we find ESC[ argv, we guess it's an ANSI file */
  99. for(i = 0; i + 1 < len; i++)
  100. if((str[i] == '\033') && (str[i + 1] == '['))
  101. return import_ansi(cv, data, len, 0);
  102. /* Otherwise, import it as text */
  103. return import_text(cv, data, len);
  104. }
  105. seterrno(EINVAL);
  106. return -1;
  107. }
  108. /** \brief Import a file into a canvas
  109. *
  110. * Import a file into the given libcaca canvas's current frame. The
  111. * current frame is resized accordingly and its contents are replaced
  112. * with the imported data.
  113. *
  114. * Valid values for \c format are:
  115. * - \c "": attempt to autodetect the file format.
  116. * - \c "caca": import native libcaca files.
  117. * - \c "text": import ASCII text files.
  118. * - \c "ansi": import ANSI files.
  119. * - \c "utf8": import UTF-8 files with ANSI colour codes.
  120. *
  121. * The number of bytes read is returned. If the file format is valid, but
  122. * not enough data was available, 0 is returned.
  123. *
  124. * If an error occurs, -1 is returned and \b errno is set accordingly:
  125. * - \c ENOSYS File access is not implemented on this system.
  126. * - \c ENOMEM Not enough memory to allocate canvas.
  127. * - \c EINVAL Invalid format requested.
  128. * caca_import_file() may also fail and set \b errno for any of the
  129. * errors specified for the routine fopen().
  130. *
  131. * \param cv A libcaca canvas in which to import the file.
  132. * \param filename The name of the file to load.
  133. * \param format A string describing the input format.
  134. * \return The number of bytes read, or 0 if there was not enough data,
  135. * or -1 if an error occurred.
  136. */
  137. ssize_t caca_import_file(caca_canvas_t *cv, char const *filename,
  138. char const *format)
  139. {
  140. #if defined __KERNEL__
  141. seterrno(ENOSYS);
  142. return -1;
  143. #else
  144. caca_file_t *f;
  145. char *data = NULL;
  146. ssize_t size = 0;
  147. int ret;
  148. f = caca_file_open(filename, "rb");
  149. if(!f)
  150. return -1; /* fopen already set errno */
  151. while(!caca_file_eof(f))
  152. {
  153. data = realloc(data, size + 1024);
  154. if(!data)
  155. {
  156. caca_file_close(f);
  157. seterrno(ENOMEM);
  158. return -1;
  159. }
  160. ret = caca_file_read(f, data + size, 1024);
  161. if(ret >= 0)
  162. size += ret;
  163. }
  164. caca_file_close(f);
  165. ret = caca_import_memory(cv, data, size, format);
  166. free(data);
  167. return ret;
  168. #endif
  169. }
  170. /** \brief Get available import formats
  171. *
  172. * Return a list of available import formats. The list is a NULL-terminated
  173. * array of strings, interleaving a string containing the internal value for
  174. * the import format, to be used with caca_import_canvas(), and a string
  175. * containing the natural language description for that import format.
  176. *
  177. * This function never fails.
  178. *
  179. * \return An array of strings.
  180. */
  181. char const * const * caca_get_import_list(void)
  182. {
  183. static char const * const list[] =
  184. {
  185. "", "autodetect",
  186. "caca", "native libcaca format",
  187. "text", "plain text",
  188. "ansi", "ANSI coloured text",
  189. "utf8", "UTF-8 files with ANSI colour codes",
  190. NULL, NULL
  191. };
  192. return list;
  193. }
  194. /*
  195. * XXX: the following functions are local.
  196. */
  197. static ssize_t import_caca(caca_canvas_t *cv, void const *data, size_t size)
  198. {
  199. uint8_t const *buf = (uint8_t const *)data;
  200. size_t control_size, data_size, expected_size;
  201. unsigned int frames, f, n, offset;
  202. uint16_t version, flags;
  203. int32_t xmin = 0, ymin = 0, xmax = 0, ymax = 0;
  204. if(size < 20)
  205. return 0;
  206. if(buf[0] != 0xca || buf[1] != 0xca || buf[2] != 'C' || buf[3] != 'V')
  207. {
  208. debug("caca import error: expected \\xca\\xcaCV header");
  209. goto invalid_caca;
  210. }
  211. control_size = sscanu32(buf + 4);
  212. data_size = sscanu32(buf + 8);
  213. version = sscanu16(buf + 12);
  214. frames = sscanu32(buf + 14);
  215. flags = sscanu16(buf + 18);
  216. if(size < 4 + control_size + data_size)
  217. return 0;
  218. if(control_size < 16 + frames * 32)
  219. {
  220. debug("caca import error: control size %u < expected %u",
  221. (unsigned int)control_size, 16 + frames * 32);
  222. goto invalid_caca;
  223. }
  224. for(expected_size = 0, f = 0; f < frames; f++)
  225. {
  226. unsigned int width, height, duration;
  227. uint32_t attr;
  228. int x, y, handlex, handley;
  229. width = sscanu32(buf + 4 + 16 + f * 32);
  230. height = sscanu32(buf + 4 + 16 + f * 32 + 4);
  231. duration = sscanu32(buf + 4 + 16 + f * 32 + 8);
  232. attr = sscanu32(buf + 4 + 16 + f * 32 + 12);
  233. x = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 16);
  234. y = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 20);
  235. handlex = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 24);
  236. handley = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 28);
  237. expected_size += width * height * 8;
  238. if(-handlex < xmin)
  239. xmin = -handlex;
  240. if(-handley < ymin)
  241. ymin = -handley;
  242. if((((int32_t) width) - handlex) > xmax)
  243. xmax = ((int32_t) width) - handlex;
  244. if((((int32_t) height) - handley) > ymax)
  245. ymax = ((int32_t) height) - handley;
  246. }
  247. if(expected_size != data_size)
  248. {
  249. debug("caca import error: data size %u < expected %u",
  250. (unsigned int)data_size, (unsigned int)expected_size);
  251. goto invalid_caca;
  252. }
  253. caca_set_canvas_size(cv, 0, 0);
  254. caca_set_canvas_size(cv, xmax - xmin, ymax - ymin);
  255. for (f = caca_get_frame_count(cv); f--; )
  256. {
  257. caca_free_frame(cv, f);
  258. }
  259. for (offset = 0, f = 0; f < frames; f ++)
  260. {
  261. unsigned int width, height;
  262. width = sscanu32(buf + 4 + 16 + f * 32);
  263. height = sscanu32(buf + 4 + 16 + f * 32 + 4);
  264. caca_create_frame(cv, f);
  265. caca_set_frame(cv, f);
  266. cv->curattr = sscanu32(buf + 4 + 16 + f * 32 + 12);
  267. cv->frames[f].x = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 16);
  268. cv->frames[f].y = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 20);
  269. cv->frames[f].handlex = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 24);
  270. cv->frames[f].handley = (int32_t)sscanu32(buf + 4 + 16 + f * 32 + 28);
  271. /* FIXME: check for return value */
  272. for(n = width * height; n--; )
  273. {
  274. int x = (n % width) - cv->frames[f].handlex - xmin;
  275. int y = (n / width) - cv->frames[f].handley - ymin;
  276. caca_put_char(cv, x, y, sscanu32(buf + 4 + control_size
  277. + offset + 8 * n));
  278. caca_put_attr(cv, x, y, sscanu32(buf + 4 + control_size
  279. + offset + 8 * n + 4));
  280. }
  281. offset += width * height * 8;
  282. cv->frames[f].x -= cv->frames[f].handlex;
  283. cv->frames[f].y -= cv->frames[f].handley;
  284. cv->frames[f].handlex = -xmin;
  285. cv->frames[f].handley = -ymin;
  286. }
  287. caca_set_frame(cv, 0);
  288. return 4 + control_size + data_size;
  289. invalid_caca:
  290. seterrno(EINVAL);
  291. return -1;
  292. }
  293. static ssize_t import_text(caca_canvas_t *cv, void const *data, size_t size)
  294. {
  295. char const *text = (char const *)data;
  296. unsigned int width = 0, height = 0, x = 0, y = 0, i;
  297. caca_set_canvas_size(cv, width, height);
  298. for(i = 0; i < size; i++)
  299. {
  300. unsigned char ch = *text++;
  301. if(ch == '\r')
  302. continue;
  303. if(ch == '\n')
  304. {
  305. x = 0;
  306. y++;
  307. continue;
  308. }
  309. if(x >= width || y >= height)
  310. {
  311. if(x >= width)
  312. width = x + 1;
  313. if(y >= height)
  314. height = y + 1;
  315. caca_set_canvas_size(cv, width, height);
  316. }
  317. caca_put_char(cv, x, y, ch);
  318. x++;
  319. }
  320. if(y > height)
  321. caca_set_canvas_size(cv, width, height = y);
  322. return size;
  323. }
  324. static ssize_t import_ansi(caca_canvas_t *cv, void const *data,
  325. size_t size, int utf8)
  326. {
  327. struct import im;
  328. unsigned char const *buffer = (unsigned char const*)data;
  329. unsigned int i, j, skip, growx = 0, growy = 0, dummy = 0;
  330. unsigned int width, height;
  331. uint32_t savedattr;
  332. int x = 0, y = 0, save_x = 0, save_y = 0;
  333. if(utf8)
  334. {
  335. width = cv->width;
  336. height = cv->height;
  337. growx = !width;
  338. growy = !height;
  339. x = cv->frames[cv->frame].x;
  340. y = cv->frames[cv->frame].y;
  341. }
  342. else
  343. {
  344. caca_set_canvas_size(cv, width = 80, height = 0);
  345. growx = 0;
  346. growy = 1;
  347. }
  348. if(utf8)
  349. {
  350. im.dfg = CACA_DEFAULT;
  351. im.dbg = CACA_TRANSPARENT;
  352. }
  353. else
  354. {
  355. im.dfg = CACA_LIGHTGRAY;
  356. im.dbg = CACA_BLACK;
  357. }
  358. caca_set_color_ansi(cv, im.dfg, im.dbg);
  359. im.clearattr = caca_get_attr(cv, -1, -1);
  360. ansi_parse_grcm(cv, &im, 1, &dummy);
  361. for(i = 0; i < size; i += skip)
  362. {
  363. uint32_t ch = 0;
  364. int wch = 0;
  365. skip = 1;
  366. if(!utf8 && buffer[i] == '\x1a' && i + 7 < size
  367. && !memcmp(buffer + i + 1, "SAUCE00", 7))
  368. break; /* End before SAUCE data */
  369. else if(buffer[i] == '\r')
  370. {
  371. x = 0;
  372. }
  373. else if(buffer[i] == '\n')
  374. {
  375. x = 0;
  376. y++;
  377. }
  378. else if(buffer[i] == '\t')
  379. {
  380. x = (x + 7) & ~7;
  381. }
  382. else if(buffer[i] == '\x08')
  383. {
  384. if(x > 0)
  385. x--;
  386. }
  387. /* If there are not enough characters to parse the escape sequence,
  388. * wait until the next try. We require 3. */
  389. else if(buffer[i] == '\033' && i + 2 >= size)
  390. break;
  391. /* XXX: What the fuck is this shit? */
  392. else if(buffer[i] == '\033' && buffer[i + 1] == '('
  393. && buffer[i + 2] == 'B')
  394. {
  395. skip += 2;
  396. }
  397. /* Interpret escape commands, as per Standard ECMA-48 "Control
  398. * Functions for Coded Character Sets", 5.4. Control sequences. */
  399. else if(buffer[i] == '\033' && buffer[i + 1] == '[')
  400. {
  401. unsigned int argc = 0, argv[101];
  402. unsigned int param, inter, final;
  403. /* Compute offsets to parameter bytes, intermediate bytes and
  404. * to the final byte. Only the final byte is mandatory, there
  405. * can be zero of the others.
  406. * 0 param=2 inter final final+1
  407. * +-----+------------------+---------------------+-----------------+
  408. * | CSI | parameter bytes | intermediate bytes | final byte |
  409. * | | 0x30 - 0x3f | 0x20 - 0x2f | 0x40 - 0x7e |
  410. * | ^[[ | 0123456789:;<=>? | SPC !"#$%&'()*+,-./ | azAZ@[\]^_`{|}~ |
  411. * +-----+------------------+---------------------+-----------------+
  412. */
  413. param = 2;
  414. for(inter = param; i + inter < size; inter++)
  415. if(buffer[i + inter] < 0x30 || buffer[i + inter] > 0x3f)
  416. break;
  417. for(final = inter; i + final < size; final++)
  418. if(buffer[i + final] < 0x20 || buffer[i + final] > 0x2f)
  419. break;
  420. if(i + final >= size
  421. || buffer[i + final] < 0x40 || buffer[i + final] > 0x7e)
  422. break; /* Invalid Final Byte */
  423. skip += final;
  424. /* Sanity checks */
  425. if(param < inter && buffer[i + param] >= 0x3c)
  426. {
  427. /* Private sequence, only parse what we know */
  428. debug("ansi import: private sequence \"^[[%.*s\"",
  429. final - param + 1, buffer + i + param);
  430. continue; /* Private sequence, skip it entirely */
  431. }
  432. if(final - param > 100)
  433. continue; /* Suspiciously long sequence, skip it */
  434. /* Parse parameter bytes as per ECMA-48 5.4.2: Parameter string
  435. * format */
  436. if(param < inter)
  437. {
  438. argv[0] = 0;
  439. for(j = param; j < inter; j++)
  440. {
  441. if(buffer[i + j] == ';')
  442. argv[++argc] = 0;
  443. else if(buffer[i + j] >= '0' && buffer[i + j] <= '9')
  444. argv[argc] = 10 * argv[argc] + (buffer[i + j] - '0');
  445. }
  446. argc++;
  447. }
  448. /* Interpret final byte. The code representations are given in
  449. * ECMA-48 5.4: Control sequences, and the code definitions are
  450. * given in ECMA-48 8.3: Definition of control functions. */
  451. switch(buffer[i + final])
  452. {
  453. case 'H': /* CUP (0x48) - Cursor Position */
  454. x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0;
  455. y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0;
  456. break;
  457. case 'A': /* CUU (0x41) - Cursor Up */
  458. y -= argc ? argv[0] : 1;
  459. if(y < 0)
  460. y = 0;
  461. break;
  462. case 'B': /* CUD (0x42) - Cursor Down */
  463. y += argc ? argv[0] : 1;
  464. break;
  465. case 'C': /* CUF (0x43) - Cursor Right */
  466. x += argc ? argv[0] : 1;
  467. break;
  468. case 'D': /* CUB (0x44) - Cursor Left */
  469. x -= argc ? argv[0] : 1;
  470. if(x < 0)
  471. x = 0;
  472. break;
  473. case 'G': /* CHA (0x47) - Cursor Character Absolute */
  474. x = (argc && argv[0] > 0) ? argv[0] - 1 : 0;
  475. break;
  476. case 'J': /* ED (0x4a) - Erase In Page */
  477. savedattr = caca_get_attr(cv, -1, -1);
  478. caca_set_attr(cv, im.clearattr);
  479. if(!argc || argv[0] == 0)
  480. {
  481. caca_draw_line(cv, x, y, width, y, ' ');
  482. caca_fill_box(cv, 0, y + 1, width - 1, height - 1, ' ');
  483. }
  484. else if(argv[0] == 1)
  485. {
  486. caca_fill_box(cv, 0, 0, width - 1, y - 1, ' ');
  487. caca_draw_line(cv, 0, y, x, y, ' ');
  488. }
  489. else if(argv[0] == 2)
  490. //x = y = 0;
  491. caca_fill_box(cv, 0, 0, width - 1, height - 1, ' ');
  492. caca_set_attr(cv, savedattr);
  493. break;
  494. case 'K': /* EL (0x4b) - Erase In Line */
  495. if(!argc || argv[0] == 0)
  496. caca_draw_line(cv, x, y, width, y, ' ');
  497. else if(argv[0] == 1)
  498. caca_draw_line(cv, 0, y, x, y, ' ');
  499. else if(argv[0] == 2)
  500. if((unsigned int)x < width)
  501. caca_draw_line(cv, x, y, width - 1, y, ' ');
  502. //x = width;
  503. break;
  504. case 'P': /* DCH (0x50) - Delete Character */
  505. if(!argc || argv[0] == 0)
  506. argv[0] = 1; /* echo -ne 'foobar\r\e[0P\n' */
  507. for(j = 0; (unsigned int)(j + argv[0]) < width; j++)
  508. {
  509. caca_put_char(cv, j, y,
  510. caca_get_char(cv, j + argv[0], y));
  511. caca_put_attr(cv, j, y,
  512. caca_get_attr(cv, j + argv[0], y));
  513. }
  514. #if 0
  515. savedattr = caca_get_attr(cv, -1, -1);
  516. caca_set_attr(cv, im.clearattr);
  517. for( ; (unsigned int)j < width; j++)
  518. caca_put_char(cv, j, y, ' ');
  519. caca_set_attr(cv, savedattr);
  520. #endif
  521. case 'X': /* ECH (0x58) - Erase Character */
  522. if(argc && argv[0])
  523. {
  524. savedattr = caca_get_attr(cv, -1, -1);
  525. caca_set_attr(cv, im.clearattr);
  526. caca_draw_line(cv, x, y, x + argv[0] - 1, y, ' ');
  527. caca_set_attr(cv, savedattr);
  528. }
  529. case 'd': /* VPA (0x64) - Line Position Absolute */
  530. y = (argc && argv[0] > 0) ? argv[0] - 1 : 0;
  531. break;
  532. case 'f': /* HVP (0x66) - Character And Line Position */
  533. x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0;
  534. y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0;
  535. break;
  536. case 'h': /* SM (0x68) - FIXME */
  537. debug("ansi import: set mode %i", argc ? (int)argv[0] : -1);
  538. break;
  539. case 'l': /* RM (0x6c) - FIXME */
  540. debug("ansi import: reset mode %i", argc ? (int)argv[0] : -1);
  541. break;
  542. case 'm': /* SGR (0x6d) - Select Graphic Rendition */
  543. if(argc)
  544. ansi_parse_grcm(cv, &im, argc, argv);
  545. else
  546. ansi_parse_grcm(cv, &im, 1, &dummy);
  547. break;
  548. case 's': /* Private (save cursor position) */
  549. save_x = x;
  550. save_y = y;
  551. break;
  552. case 'u': /* Private (reload cursor position) */
  553. x = save_x;
  554. y = save_y;
  555. break;
  556. default:
  557. debug("ansi import: unknown command \"^[[%.*s\"",
  558. final - param + 1, buffer + i + param);
  559. break;
  560. }
  561. }
  562. /* Parse OSC stuff. */
  563. else if(buffer[i] == '\033' && buffer[i + 1] == ']')
  564. {
  565. char *string;
  566. unsigned int command = 0;
  567. unsigned int mode = 2, semicolon, final;
  568. for(semicolon = mode; i + semicolon < size; semicolon++)
  569. {
  570. if(buffer[i + semicolon] < '0' || buffer[i + semicolon] > '9')
  571. break;
  572. command = 10 * command + (buffer[i + semicolon] - '0');
  573. }
  574. if(i + semicolon >= size || buffer[i + semicolon] != ';')
  575. break; /* Invalid Mode */
  576. for(final = semicolon + 1; i + final < size; final++)
  577. if(buffer[i + final] < 0x20)
  578. break;
  579. if(i + final >= size || buffer[i + final] != '\a')
  580. break; /* Not enough data or no bell found */
  581. /* FIXME: XTerm also reacts to <ESC><backslash> and <ST> */
  582. /* FIXME: differenciate between not enough data (try again)
  583. * and invalid data (print shit) */
  584. skip += final;
  585. string = malloc(final - (semicolon + 1) + 1);
  586. memcpy(string, buffer + (semicolon + 1), final - (semicolon + 1));
  587. string[final - (semicolon + 1)] = '\0';
  588. debug("ansi import: got OSC command %i string '%s'", command,
  589. string);
  590. free(string);
  591. }
  592. /* Form feed means a new frame */
  593. else if(buffer[i] == '\f' && buffer[i + 1] == '\n')
  594. {
  595. int f = caca_get_frame_count(cv);
  596. caca_create_frame(cv, f);
  597. caca_set_frame(cv, f);
  598. x = y = 0;
  599. skip++;
  600. }
  601. /* Get the character we’re going to paste */
  602. else if(utf8)
  603. {
  604. size_t bytes;
  605. if(i + 6 < size)
  606. ch = caca_utf8_to_utf32((char const *)(buffer + i), &bytes);
  607. else
  608. {
  609. /* Add a trailing zero to what we're going to read */
  610. char tmp[7];
  611. memcpy(tmp, buffer + i, size - i);
  612. tmp[size - i] = '\0';
  613. ch = caca_utf8_to_utf32(tmp, &bytes);
  614. }
  615. if(!bytes)
  616. {
  617. /* If the Unicode is invalid, assume it was latin1. */
  618. ch = buffer[i];
  619. bytes = 1;
  620. }
  621. wch = caca_utf32_is_fullwidth(ch) ? 2 : 1;
  622. skip += bytes - 1;
  623. }
  624. else
  625. {
  626. ch = caca_cp437_to_utf32(buffer[i]);
  627. wch = 1;
  628. }
  629. /* Wrap long lines or grow horizontally */
  630. while((unsigned int)x + wch > width)
  631. {
  632. if(growx)
  633. {
  634. savedattr = caca_get_attr(cv, -1, -1);
  635. caca_set_attr(cv, im.clearattr);
  636. caca_set_canvas_size(cv, width = x + wch, height);
  637. caca_set_attr(cv, savedattr);
  638. }
  639. else
  640. {
  641. x -= width;
  642. y++;
  643. }
  644. }
  645. /* Scroll or grow vertically */
  646. if((unsigned int)y >= height)
  647. {
  648. savedattr = caca_get_attr(cv, -1, -1);
  649. caca_set_attr(cv, im.clearattr);
  650. if(growy)
  651. {
  652. caca_set_canvas_size(cv, width, height = y + 1);
  653. }
  654. else
  655. {
  656. int lines = (y - height) + 1;
  657. for(j = 0; j + lines < height; j++)
  658. {
  659. memcpy(cv->attrs + j * cv->width,
  660. cv->attrs + (j + lines) * cv->width, cv->width * 4);
  661. memcpy(cv->chars + j * cv->width,
  662. cv->chars + (j + lines) * cv->width, cv->width * 4);
  663. }
  664. caca_fill_box(cv, 0, height - lines,
  665. cv->width - 1, height - 1, ' ');
  666. y -= lines;
  667. }
  668. caca_set_attr(cv, savedattr);
  669. }
  670. /* Now paste our character, if any */
  671. if(wch)
  672. {
  673. caca_put_char(cv, x, y, ch);
  674. x += wch;
  675. }
  676. }
  677. if(growy && (unsigned int)y > height)
  678. {
  679. savedattr = caca_get_attr(cv, -1, -1);
  680. caca_set_attr(cv, im.clearattr);
  681. caca_set_canvas_size(cv, width, height = y);
  682. caca_set_attr(cv, savedattr);
  683. }
  684. cv->frames[cv->frame].x = x;
  685. cv->frames[cv->frame].y = y;
  686. // if(utf8)
  687. // caca_set_attr(cv, savedattr);
  688. return i;
  689. }
  690. /* XXX : ANSI loader helper */
  691. static void ansi_parse_grcm(caca_canvas_t *cv, struct import *im,
  692. unsigned int argc, unsigned int const *argv)
  693. {
  694. static uint8_t const ansi2caca[] =
  695. {
  696. CACA_BLACK, CACA_RED, CACA_GREEN, CACA_BROWN,
  697. CACA_BLUE, CACA_MAGENTA, CACA_CYAN, CACA_LIGHTGRAY
  698. };
  699. unsigned int j;
  700. uint8_t efg, ebg; /* Effective (libcaca) fg/bg */
  701. for(j = 0; j < argc; j++)
  702. {
  703. /* Defined in ECMA-48 8.3.117: SGR - SELECT GRAPHIC RENDITION */
  704. if(argv[j] >= 30 && argv[j] <= 37)
  705. im->fg = ansi2caca[argv[j] - 30];
  706. else if(argv[j] >= 40 && argv[j] <= 47)
  707. im->bg = ansi2caca[argv[j] - 40];
  708. else if(argv[j] >= 90 && argv[j] <= 97)
  709. im->fg = ansi2caca[argv[j] - 90] + 8;
  710. else if(argv[j] >= 100 && argv[j] <= 107)
  711. im->bg = ansi2caca[argv[j] - 100] + 8;
  712. else switch(argv[j])
  713. {
  714. case 0: /* default rendition */
  715. im->fg = im->dfg;
  716. im->bg = im->dbg;
  717. im->bold = im->blink = im->italics = im->negative
  718. = im->concealed = im->underline = im->faint = im->strike
  719. = im->proportional = 0;
  720. break;
  721. case 1: /* bold or increased intensity */
  722. im->bold = 1;
  723. break;
  724. case 2: /* faint, decreased intensity or second colour */
  725. im->faint = 1;
  726. break;
  727. case 3: /* italicized */
  728. im->italics = 1;
  729. break;
  730. case 4: /* singly underlined */
  731. im->underline = 1;
  732. break;
  733. case 5: /* slowly blinking (less then 150 per minute) */
  734. case 6: /* rapidly blinking (150 per minute or more) */
  735. im->blink = 1;
  736. break;
  737. case 7: /* negative image */
  738. im->negative = 1;
  739. break;
  740. case 8: /* concealed characters */
  741. im->concealed = 1;
  742. break;
  743. case 9: /* crossed-out (characters still legible but marked as to be
  744. * deleted */
  745. im->strike = 1;
  746. break;
  747. case 21: /* doubly underlined */
  748. im->underline = 1;
  749. break;
  750. case 22: /* normal colour or normal intensity (neither bold nor
  751. * faint) */
  752. im->bold = im->faint = 0;
  753. break;
  754. case 23: /* not italicized, not fraktur */
  755. im->italics = 0;
  756. break;
  757. case 24: /* not underlined (neither singly nor doubly) */
  758. im->underline = 0;
  759. break;
  760. case 25: /* steady (not blinking) */
  761. im->blink = 0;
  762. break;
  763. case 26: /* (reserved for proportional spacing as specified in CCITT
  764. * Recommendation T.61) */
  765. im->proportional = 1;
  766. break;
  767. case 27: /* positive image */
  768. im->negative = 0;
  769. break;
  770. case 28: /* revealed characters */
  771. im->concealed = 0;
  772. break;
  773. case 29: /* not crossed out */
  774. im->strike = 0;
  775. break;
  776. case 38: /* (reserved for future standardization, intended for setting
  777. * character foreground colour as specified in ISO 8613-6
  778. * [CCITT Recommendation T.416]) */
  779. break;
  780. case 39: /* default display colour (implementation-defined) */
  781. im->fg = im->dfg;
  782. break;
  783. case 48: /* (reserved for future standardization, intended for setting
  784. * character background colour as specified in ISO 8613-6
  785. * [CCITT Recommendation T.416]) */
  786. break;
  787. case 49: /* default background colour (implementation-defined) */
  788. im->bg = im->dbg;
  789. break;
  790. case 50: /* (reserved for cancelling the effect of the rendering
  791. * aspect established by parameter value 26) */
  792. im->proportional = 0;
  793. break;
  794. default:
  795. debug("ansi import: unknown sgr %i", argv[j]);
  796. break;
  797. }
  798. }
  799. if(im->concealed)
  800. {
  801. efg = ebg = CACA_TRANSPARENT;
  802. }
  803. else
  804. {
  805. efg = im->negative ? im->bg : im->fg;
  806. ebg = im->negative ? im->fg : im->bg;
  807. if(im->bold)
  808. {
  809. if(efg < 8)
  810. efg += 8;
  811. else if(efg == CACA_DEFAULT)
  812. efg = CACA_WHITE;
  813. }
  814. }
  815. caca_set_color_ansi(cv, efg, ebg);
  816. }
  817. /*
  818. * XXX: The following functions are aliases.
  819. */
  820. ssize_t cucul_import_memory(cucul_canvas_t *, void const *, size_t,
  821. char const *) CACA_ALIAS(caca_import_memory);
  822. ssize_t cucul_import_file(cucul_canvas_t *, char const *,
  823. char const *) CACA_ALIAS(caca_import_file);
  824. char const * const * cucul_get_import_list(void)
  825. CACA_ALIAS(caca_get_import_list);