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.
 
 
 
 
 
 

894 lines
28 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 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 long int import_caca(cucul_canvas_t *, void const *, unsigned int);
  47. static long int import_text(cucul_canvas_t *, void const *, unsigned int);
  48. static long int import_ansi(cucul_canvas_t *, void const *, unsigned int, int);
  49. static void ansi_parse_grcm(cucul_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 libcucul 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 libcucul 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. long int cucul_import_memory(cucul_canvas_t *cv, void const *data,
  79. unsigned long int 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] == 0x1b) && (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 libcucul 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. * cucul_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 libcucul 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. long int cucul_import_file(cucul_canvas_t *cv, char const *filename,
  138. char const *format)
  139. {
  140. #if defined __KERNEL__
  141. seterrno(ENOSYS);
  142. return -1;
  143. #else
  144. FILE *fp;
  145. void *data;
  146. long int size;
  147. int ret;
  148. fp = fopen(filename, "rb");
  149. if(!fp)
  150. return -1; /* fopen already set errno */
  151. fseek(fp, 0, SEEK_END);
  152. size = ftell(fp);
  153. data = malloc(size);
  154. if(!data)
  155. {
  156. fclose(fp);
  157. seterrno(ENOMEM);
  158. return -1;
  159. }
  160. fseek(fp, 0, SEEK_SET);
  161. fread(data, size, 1, fp);
  162. fclose(fp);
  163. ret = cucul_import_memory(cv, data, size, format);
  164. free(data);
  165. return ret;
  166. #endif
  167. }
  168. /** \brief Get available import formats
  169. *
  170. * Return a list of available import formats. The list is a NULL-terminated
  171. * array of strings, interleaving a string containing the internal value for
  172. * the import format, to be used with cucul_import_canvas(), and a string
  173. * containing the natural language description for that import format.
  174. *
  175. * This function never fails.
  176. *
  177. * \return An array of strings.
  178. */
  179. char const * const * cucul_get_import_list(void)
  180. {
  181. static char const * const list[] =
  182. {
  183. "", "autodetect",
  184. "caca", "native libcaca format",
  185. "text", "plain text",
  186. "ansi", "ANSI coloured text",
  187. "utf8", "UTF-8 files with ANSI colour codes",
  188. NULL, NULL
  189. };
  190. return list;
  191. }
  192. /*
  193. * XXX: the following functions are local.
  194. */
  195. static long int import_caca(cucul_canvas_t *cv,
  196. void const *data, unsigned int size)
  197. {
  198. uint8_t const *buf = (uint8_t const *)data;
  199. unsigned int control_size, data_size, expected_size, frames, f, n;
  200. uint16_t version, flags;
  201. if(size < 20)
  202. return 0;
  203. if(buf[0] != 0xca || buf[1] != 0xca || buf[2] != 'C' || buf[3] != 'V')
  204. {
  205. debug("caca import error: expected \\xca\\xcaCV header");
  206. goto invalid_caca;
  207. }
  208. control_size = sscanu32(buf + 4);
  209. data_size = sscanu32(buf + 8);
  210. version = sscanu16(buf + 12);
  211. frames = sscanu32(buf + 14);
  212. flags = sscanu16(buf + 18);
  213. if(size < 4 + control_size + data_size)
  214. return 0;
  215. if(control_size < 16 + frames * 32)
  216. {
  217. debug("caca import error: control size %lu < expected %lu",
  218. (unsigned long int)control_size, 16 + frames * 32);
  219. goto invalid_caca;
  220. }
  221. for(expected_size = 0, f = 0; f < frames; f++)
  222. {
  223. unsigned int width, height, duration;
  224. uint32_t attr;
  225. int x, y, handlex, handley;
  226. width = sscanu32(buf + 4 + 16 + f * 24);
  227. height = sscanu32(buf + 4 + 16 + f * 24 + 4);
  228. duration = sscanu32(buf + 4 + 16 + f * 24 + 8);
  229. attr = sscanu32(buf + 4 + 16 + f * 24 + 12);
  230. x = (int32_t)sscanu32(buf + 4 + 16 + f * 24 + 16);
  231. y = (int32_t)sscanu32(buf + 4 + 16 + f * 24 + 20);
  232. handlex = (int32_t)sscanu32(buf + 4 + 16 + f * 24 + 24);
  233. handley = (int32_t)sscanu32(buf + 4 + 16 + f * 24 + 28);
  234. expected_size += width * height * 8;
  235. }
  236. if(expected_size != data_size)
  237. {
  238. debug("caca import error: data size %lu < expected %lu",
  239. (unsigned long int)data_size, (unsigned long int)expected_size);
  240. goto invalid_caca;
  241. }
  242. /* FIXME: read all frames, not only the first one */
  243. cucul_set_canvas_size(cv, 0, 0);
  244. cucul_set_canvas_size(cv, sscanu32(buf + 4 + 16),
  245. sscanu32(buf + 4 + 16 + 4));
  246. /* FIXME: check for return value */
  247. for(n = sscanu32(buf + 4 + 16) * sscanu32(buf + 4 + 16 + 4); n--; )
  248. {
  249. cv->chars[n] = sscanu32(buf + 4 + control_size + 8 * n);
  250. cv->attrs[n] = sscanu32(buf + 4 + control_size + 8 * n + 4);
  251. }
  252. cv->curattr = sscanu32(buf + 4 + 16 + 12);
  253. cv->frames[0].x = (int32_t)sscanu32(buf + 4 + 16 + 0 * 24 + 16);
  254. cv->frames[0].y = (int32_t)sscanu32(buf + 4 + 16 + 0 * 24 + 20);
  255. cv->frames[0].handlex = (int32_t)sscanu32(buf + 4 + 16 + 0 * 24 + 24);
  256. cv->frames[0].handley = (int32_t)sscanu32(buf + 4 + 16 + 0 * 24 + 28);
  257. return 4 + control_size + data_size;
  258. invalid_caca:
  259. seterrno(EINVAL);
  260. return -1;
  261. }
  262. static long int import_text(cucul_canvas_t *cv,
  263. void const *data, unsigned int size)
  264. {
  265. char const *text = (char const *)data;
  266. unsigned int width = 0, height = 0, x = 0, y = 0, i;
  267. cucul_set_canvas_size(cv, width, height);
  268. for(i = 0; i < size; i++)
  269. {
  270. unsigned char ch = *text++;
  271. if(ch == '\r')
  272. continue;
  273. if(ch == '\n')
  274. {
  275. x = 0;
  276. y++;
  277. continue;
  278. }
  279. if(x >= width || y >= height)
  280. {
  281. if(x >= width)
  282. width = x + 1;
  283. if(y >= height)
  284. height = y + 1;
  285. cucul_set_canvas_size(cv, width, height);
  286. }
  287. cucul_put_char(cv, x, y, ch);
  288. x++;
  289. }
  290. if(y > height)
  291. cucul_set_canvas_size(cv, width, height = y);
  292. return size;
  293. }
  294. static long int import_ansi(cucul_canvas_t *cv,
  295. void const *data, unsigned int size, int utf8)
  296. {
  297. struct import *im;
  298. unsigned char const *buffer = (unsigned char const*)data;
  299. unsigned int i, j, init, skip, growx = 0, growy = 0, dummy = 0;
  300. unsigned int width, height;
  301. uint32_t savedattr;
  302. int x = 0, y = 0, save_x = 0, save_y = 0;
  303. init = !!cv->frames[cv->frame].import;
  304. if(!init)
  305. {
  306. cv->frames[cv->frame].import = malloc(sizeof(struct import));
  307. memset(cv->frames[cv->frame].import, 0, sizeof(struct import));
  308. }
  309. im = (struct import *)cv->frames[cv->frame].import;
  310. if(utf8)
  311. {
  312. width = cv->width;
  313. height = cv->height;
  314. growx = !width;
  315. growy = !height;
  316. x = cv->frames[cv->frame].x;
  317. y = cv->frames[cv->frame].y;
  318. }
  319. else
  320. {
  321. cucul_set_canvas_size(cv, width = 80, height = 0);
  322. growx = 0;
  323. growy = 1;
  324. }
  325. if(!init)
  326. {
  327. if(utf8)
  328. {
  329. im->dfg = CUCUL_DEFAULT;
  330. im->dbg = CUCUL_TRANSPARENT;
  331. }
  332. else
  333. {
  334. im->dfg = CUCUL_LIGHTGRAY;
  335. im->dbg = CUCUL_BLACK;
  336. }
  337. cucul_set_color_ansi(cv, im->dfg, im->dbg);
  338. im->clearattr = cucul_get_attr(cv, -1, -1);
  339. ansi_parse_grcm(cv, im, 1, &dummy);
  340. }
  341. for(i = 0; i < size; i += skip)
  342. {
  343. uint32_t ch = 0;
  344. int wch = 0;
  345. skip = 1;
  346. if(!utf8 && buffer[i] == '\x1a' && i + 7 < size
  347. && !memcmp(buffer + i + 1, "SAUCE00", 7))
  348. break; /* End before SAUCE data */
  349. else if(buffer[i] == '\r')
  350. {
  351. x = 0;
  352. }
  353. else if(buffer[i] == '\n')
  354. {
  355. x = 0;
  356. y++;
  357. }
  358. else if(buffer[i] == '\t')
  359. {
  360. x = (x + 7) & ~7;
  361. }
  362. else if(buffer[i] == '\x08')
  363. {
  364. if(x > 0)
  365. x--;
  366. }
  367. /* If there are not enough characters to parse the escape sequence,
  368. * wait until the next try. We require 3. */
  369. else if(buffer[i] == '\x1b' && i + 2 >= size)
  370. break;
  371. /* XXX: What the fuck is this shit? */
  372. else if(buffer[i] == '\x1b' && buffer[i + 1] == '('
  373. && buffer[i + 2] == 'B')
  374. {
  375. skip += 2;
  376. }
  377. /* Interpret escape commands, as per Standard ECMA-48 "Control
  378. * Functions for Coded Character Sets", 5.4. Control sequences. */
  379. else if(buffer[i] == '\x1b' && buffer[i + 1] == '[')
  380. {
  381. unsigned int argc = 0, argv[101];
  382. unsigned int param, inter, final;
  383. /* Compute offsets to parameter bytes, intermediate bytes and
  384. * to the final byte. Only the final byte is mandatory, there
  385. * can be zero of the others.
  386. * 0 param=2 inter final final+1
  387. * +-----+------------------+---------------------+-----------------+
  388. * | CSI | parameter bytes | intermediate bytes | final byte |
  389. * | | 0x30 - 0x3f | 0x20 - 0x2f | 0x40 - 0x7e |
  390. * | ^[[ | 0123456789:;<=>? | SPC !"#$%&'()*+,-./ | azAZ@[\]^_`{|}~ |
  391. * +-----+------------------+---------------------+-----------------+
  392. */
  393. param = 2;
  394. for(inter = param; i + inter < size; inter++)
  395. if(buffer[i + inter] < 0x30 || buffer[i + inter] > 0x3f)
  396. break;
  397. for(final = inter; i + final < size; final++)
  398. if(buffer[i + final] < 0x20 || buffer[i + final] > 0x2f)
  399. break;
  400. if(i + final >= size
  401. || buffer[i + final] < 0x40 || buffer[i + final] > 0x7e)
  402. break; /* Invalid Final Byte */
  403. skip += final;
  404. /* Sanity checks */
  405. if(param < inter && buffer[i + param] >= 0x3c)
  406. {
  407. /* Private sequence, only parse what we know */
  408. debug("ansi import: private sequence \"^[[%.*s\"",
  409. final - param + 1, buffer + i + param);
  410. continue; /* Private sequence, skip it entirely */
  411. }
  412. if(final - param > 100)
  413. continue; /* Suspiciously long sequence, skip it */
  414. /* Parse parameter bytes as per ECMA-48 5.4.2: Parameter string
  415. * format */
  416. if(param < inter)
  417. {
  418. argv[0] = 0;
  419. for(j = param; j < inter; j++)
  420. {
  421. if(buffer[i + j] == ';')
  422. argv[++argc] = 0;
  423. else if(buffer[i + j] >= '0' && buffer[i + j] <= '9')
  424. argv[argc] = 10 * argv[argc] + (buffer[i + j] - '0');
  425. }
  426. argc++;
  427. }
  428. /* Interpret final byte. The code representations are given in
  429. * ECMA-48 5.4: Control sequences, and the code definitions are
  430. * given in ECMA-48 8.3: Definition of control functions. */
  431. switch(buffer[i + final])
  432. {
  433. case 'H': /* CUP (0x48) - Cursor Position */
  434. x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0;
  435. y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0;
  436. break;
  437. case 'A': /* CUU (0x41) - Cursor Up */
  438. y -= argc ? argv[0] : 1;
  439. if(y < 0)
  440. y = 0;
  441. break;
  442. case 'B': /* CUD (0x42) - Cursor Down */
  443. y += argc ? argv[0] : 1;
  444. break;
  445. case 'C': /* CUF (0x43) - Cursor Right */
  446. x += argc ? argv[0] : 1;
  447. break;
  448. case 'D': /* CUB (0x44) - Cursor Left */
  449. x -= argc ? argv[0] : 1;
  450. if(x < 0)
  451. x = 0;
  452. break;
  453. case 'G': /* CHA (0x47) - Cursor Character Absolute */
  454. x = (argc && argv[0] > 0) ? argv[0] - 1 : 0;
  455. break;
  456. case 'J': /* ED (0x4a) - Erase In Page */
  457. savedattr = cucul_get_attr(cv, -1, -1);
  458. cucul_set_attr(cv, im->clearattr);
  459. if(!argc || argv[0] == 0)
  460. {
  461. cucul_draw_line(cv, x, y, width, y, ' ');
  462. cucul_fill_box(cv, 0, y + 1, width - 1, height - 1, ' ');
  463. }
  464. else if(argv[0] == 1)
  465. {
  466. cucul_fill_box(cv, 0, 0, width - 1, y - 1, ' ');
  467. cucul_draw_line(cv, 0, y, x, y, ' ');
  468. }
  469. else if(argv[0] == 2)
  470. //x = y = 0;
  471. cucul_fill_box(cv, 0, 0, width - 1, height - 1, ' ');
  472. cucul_set_attr(cv, savedattr);
  473. break;
  474. case 'K': /* EL (0x4b) - Erase In Line */
  475. if(!argc || argv[0] == 0)
  476. cucul_draw_line(cv, x, y, width, y, ' ');
  477. else if(argv[0] == 1)
  478. cucul_draw_line(cv, 0, y, x, y, ' ');
  479. else if(argv[0] == 2)
  480. if((unsigned int)x < width)
  481. cucul_draw_line(cv, x, y, width - 1, y, ' ');
  482. //x = width;
  483. break;
  484. case 'P': /* DCH (0x50) - Delete Character */
  485. if(!argc || argv[0] == 0)
  486. argv[0] = 1; /* echo -ne 'foobar\r\e[0P\n' */
  487. for(j = 0; (unsigned int)(j + argv[0]) < width; j++)
  488. {
  489. cucul_put_char(cv, j, y,
  490. cucul_get_char(cv, j + argv[0], y));
  491. cucul_put_attr(cv, j, y,
  492. cucul_get_attr(cv, j + argv[0], y));
  493. }
  494. #if 0
  495. savedattr = cucul_get_attr(cv, -1, -1);
  496. cucul_set_attr(cv, im->clearattr);
  497. for( ; (unsigned int)j < width; j++)
  498. cucul_put_char(cv, j, y, ' ');
  499. cucul_set_attr(cv, savedattr);
  500. #endif
  501. case 'X': /* ECH (0x58) - Erase Character */
  502. if(argc && argv[0])
  503. {
  504. savedattr = cucul_get_attr(cv, -1, -1);
  505. cucul_set_attr(cv, im->clearattr);
  506. cucul_draw_line(cv, x, y, x + argv[0] - 1, y, ' ');
  507. cucul_set_attr(cv, savedattr);
  508. }
  509. case 'd': /* VPA (0x64) - Line Position Absolute */
  510. y = (argc && argv[0] > 0) ? argv[0] - 1 : 0;
  511. break;
  512. case 'f': /* HVP (0x66) - Character And Line Position */
  513. x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0;
  514. y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0;
  515. break;
  516. case 'h': /* SM (0x68) - FIXME */
  517. debug("ansi import: set mode %i", argc ? (int)argv[0] : -1);
  518. break;
  519. case 'l': /* RM (0x6c) - FIXME */
  520. debug("ansi import: reset mode %i", argc ? (int)argv[0] : -1);
  521. break;
  522. case 'm': /* SGR (0x6d) - Select Graphic Rendition */
  523. if(argc)
  524. ansi_parse_grcm(cv, im, argc, argv);
  525. else
  526. ansi_parse_grcm(cv, im, 1, &dummy);
  527. break;
  528. case 's': /* Private (save cursor position) */
  529. save_x = x;
  530. save_y = y;
  531. break;
  532. case 'u': /* Private (reload cursor position) */
  533. x = save_x;
  534. y = save_y;
  535. break;
  536. default:
  537. debug("ansi import: unknown command \"^[[%.*s\"",
  538. final - param + 1, buffer + i + param);
  539. break;
  540. }
  541. }
  542. /* Parse OSC stuff. */
  543. else if(buffer[i] == '\x1b' && buffer[i + 1] == ']')
  544. {
  545. char *string;
  546. unsigned int command = 0;
  547. unsigned int mode = 2, semicolon, final;
  548. for(semicolon = mode; i + semicolon < size; semicolon++)
  549. {
  550. if(buffer[i + semicolon] < '0' || buffer[i + semicolon] > '9')
  551. break;
  552. command = 10 * command + (buffer[i + semicolon] - '0');
  553. }
  554. if(i + semicolon >= size || buffer[i + semicolon] != ';')
  555. break; /* Invalid Mode */
  556. for(final = semicolon + 1; i + final < size; final++)
  557. if(buffer[i + final] < 0x20)
  558. break;
  559. if(i + final >= size || buffer[i + final] != '\a')
  560. break; /* Not enough data or no bell found */
  561. /* FIXME: XTerm also reacts to <ESC><backslash> and <ST> */
  562. /* FIXME: differenciate between not enough data (try again)
  563. * and invalid data (print shit) */
  564. skip += final;
  565. string = malloc(final - (semicolon + 1) + 1);
  566. memcpy(string, buffer + (semicolon + 1), final - (semicolon + 1));
  567. string[final - (semicolon + 1)] = '\0';
  568. debug("ansi import: got OSC command %i string '%s'", command,
  569. string);
  570. free(string);
  571. }
  572. /* Get the character we’re going to paste */
  573. else if(utf8)
  574. {
  575. unsigned int bytes;
  576. if(i + 6 < size)
  577. ch = cucul_utf8_to_utf32((char const *)(buffer + i), &bytes);
  578. else
  579. {
  580. /* Add a trailing zero to what we're going to read */
  581. char tmp[7];
  582. memcpy(tmp, buffer + i, size - i);
  583. tmp[size - i] = '\0';
  584. ch = cucul_utf8_to_utf32(tmp, &bytes);
  585. }
  586. if(!bytes)
  587. {
  588. /* If the Unicode is invalid, assume it was latin1. */
  589. ch = buffer[i];
  590. bytes = 1;
  591. }
  592. wch = cucul_utf32_is_fullwidth(ch) ? 2 : 1;
  593. skip += bytes - 1;
  594. }
  595. else
  596. {
  597. ch = cucul_cp437_to_utf32(buffer[i]);
  598. wch = 1;
  599. }
  600. /* Wrap long lines or grow horizontally */
  601. while((unsigned int)x + wch > width)
  602. {
  603. if(growx)
  604. {
  605. savedattr = cucul_get_attr(cv, -1, -1);
  606. cucul_set_attr(cv, im->clearattr);
  607. cucul_set_canvas_size(cv, width = x + wch, height);
  608. cucul_set_attr(cv, savedattr);
  609. }
  610. else
  611. {
  612. x -= width;
  613. y++;
  614. }
  615. }
  616. /* Scroll or grow vertically */
  617. if((unsigned int)y >= height)
  618. {
  619. savedattr = cucul_get_attr(cv, -1, -1);
  620. cucul_set_attr(cv, im->clearattr);
  621. if(growy)
  622. {
  623. cucul_set_canvas_size(cv, width, height = y + 1);
  624. }
  625. else
  626. {
  627. int lines = (y - height) + 1;
  628. for(j = 0; j + lines < height; j++)
  629. {
  630. memcpy(cv->attrs + j * cv->width,
  631. cv->attrs + (j + lines) * cv->width, cv->width * 4);
  632. memcpy(cv->chars + j * cv->width,
  633. cv->chars + (j + lines) * cv->width, cv->width * 4);
  634. }
  635. cucul_fill_box(cv, 0, height - lines,
  636. cv->width - 1, height - 1, ' ');
  637. y -= lines;
  638. }
  639. cucul_set_attr(cv, savedattr);
  640. }
  641. /* Now paste our character, if any */
  642. if(wch)
  643. {
  644. cucul_put_char(cv, x, y, ch);
  645. x += wch;
  646. }
  647. }
  648. if(growy && (unsigned int)y > height)
  649. {
  650. savedattr = cucul_get_attr(cv, -1, -1);
  651. cucul_set_attr(cv, im->clearattr);
  652. cucul_set_canvas_size(cv, width, height = y);
  653. cucul_set_attr(cv, savedattr);
  654. }
  655. cv->frames[cv->frame].x = x;
  656. cv->frames[cv->frame].y = y;
  657. // if(utf8)
  658. // cucul_set_attr(cv, savedattr);
  659. return i;
  660. }
  661. /* XXX : ANSI loader helper */
  662. static void ansi_parse_grcm(cucul_canvas_t *cv, struct import *im,
  663. unsigned int argc, unsigned int const *argv)
  664. {
  665. static uint8_t const ansi2cucul[] =
  666. {
  667. CUCUL_BLACK, CUCUL_RED, CUCUL_GREEN, CUCUL_BROWN,
  668. CUCUL_BLUE, CUCUL_MAGENTA, CUCUL_CYAN, CUCUL_LIGHTGRAY
  669. };
  670. unsigned int j;
  671. uint8_t efg, ebg; /* Effective (libcucul) fg/bg */
  672. for(j = 0; j < argc; j++)
  673. {
  674. /* Defined in ECMA-48 8.3.117: SGR - SELECT GRAPHIC RENDITION */
  675. if(argv[j] >= 30 && argv[j] <= 37)
  676. im->fg = ansi2cucul[argv[j] - 30];
  677. else if(argv[j] >= 40 && argv[j] <= 47)
  678. im->bg = ansi2cucul[argv[j] - 40];
  679. else if(argv[j] >= 90 && argv[j] <= 97)
  680. im->fg = ansi2cucul[argv[j] - 90] + 8;
  681. else if(argv[j] >= 100 && argv[j] <= 107)
  682. im->bg = ansi2cucul[argv[j] - 100] + 8;
  683. else switch(argv[j])
  684. {
  685. case 0: /* default rendition */
  686. im->fg = im->dfg;
  687. im->bg = im->dbg;
  688. im->bold = im->blink = im->italics = im->negative
  689. = im->concealed = im->underline = im->faint = im->strike
  690. = im->proportional = 0;
  691. break;
  692. case 1: /* bold or increased intensity */
  693. im->bold = 1;
  694. break;
  695. case 2: /* faint, decreased intensity or second colour */
  696. im->faint = 1;
  697. break;
  698. case 3: /* italicized */
  699. im->italics = 1;
  700. break;
  701. case 4: /* singly underlined */
  702. im->underline = 1;
  703. break;
  704. case 5: /* slowly blinking (less then 150 per minute) */
  705. case 6: /* rapidly blinking (150 per minute or more) */
  706. im->blink = 1;
  707. break;
  708. case 7: /* negative image */
  709. im->negative = 1;
  710. break;
  711. case 8: /* concealed characters */
  712. im->concealed = 1;
  713. break;
  714. case 9: /* crossed-out (characters still legible but marked as to be
  715. * deleted */
  716. im->strike = 1;
  717. break;
  718. case 21: /* doubly underlined */
  719. im->underline = 1;
  720. break;
  721. case 22: /* normal colour or normal intensity (neither bold nor
  722. * faint) */
  723. im->bold = im->faint = 0;
  724. break;
  725. case 23: /* not italicized, not fraktur */
  726. im->italics = 0;
  727. break;
  728. case 24: /* not underlined (neither singly nor doubly) */
  729. im->underline = 0;
  730. break;
  731. case 25: /* steady (not blinking) */
  732. im->blink = 0;
  733. break;
  734. case 26: /* (reserved for proportional spacing as specified in CCITT
  735. * Recommendation T.61) */
  736. im->proportional = 1;
  737. break;
  738. case 27: /* positive image */
  739. im->negative = 0;
  740. break;
  741. case 28: /* revealed characters */
  742. im->concealed = 0;
  743. break;
  744. case 29: /* not crossed out */
  745. im->strike = 0;
  746. break;
  747. case 38: /* (reserved for future standardization, intended for setting
  748. * character foreground colour as specified in ISO 8613-6
  749. * [CCITT Recommendation T.416]) */
  750. break;
  751. case 39: /* default display colour (implementation-defined) */
  752. im->fg = im->dfg;
  753. break;
  754. case 48: /* (reserved for future standardization, intended for setting
  755. * character background colour as specified in ISO 8613-6
  756. * [CCITT Recommendation T.416]) */
  757. break;
  758. case 49: /* default background colour (implementation-defined) */
  759. im->bg = im->dbg;
  760. break;
  761. case 50: /* (reserved for cancelling the effect of the rendering
  762. * aspect established by parameter value 26) */
  763. im->proportional = 0;
  764. break;
  765. default:
  766. debug("ansi import: unknown sgr %i", argv[j]);
  767. break;
  768. }
  769. }
  770. if(im->concealed)
  771. {
  772. efg = ebg = CUCUL_TRANSPARENT;
  773. }
  774. else
  775. {
  776. efg = im->negative ? im->bg : im->fg;
  777. ebg = im->negative ? im->fg : im->bg;
  778. if(im->bold)
  779. {
  780. if(efg < 8)
  781. efg += 8;
  782. else if(efg == CUCUL_DEFAULT)
  783. efg = CUCUL_WHITE;
  784. }
  785. }
  786. cucul_set_color_ansi(cv, efg, ebg);
  787. }