選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

881 行
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, 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. if(utf8)
  304. {
  305. width = cv->width;
  306. height = cv->height;
  307. growx = !width;
  308. growy = !height;
  309. x = cv->frames[cv->frame].x;
  310. y = cv->frames[cv->frame].y;
  311. }
  312. else
  313. {
  314. cucul_set_canvas_size(cv, width = 80, height = 0);
  315. growx = 0;
  316. growy = 1;
  317. }
  318. if(utf8)
  319. {
  320. im.dfg = CUCUL_DEFAULT;
  321. im.dbg = CUCUL_TRANSPARENT;
  322. }
  323. else
  324. {
  325. im.dfg = CUCUL_LIGHTGRAY;
  326. im.dbg = CUCUL_BLACK;
  327. }
  328. cucul_set_color_ansi(cv, im.dfg, im.dbg);
  329. im.clearattr = cucul_get_attr(cv, -1, -1);
  330. ansi_parse_grcm(cv, &im, 1, &dummy);
  331. for(i = 0; i < size; i += skip)
  332. {
  333. uint32_t ch = 0;
  334. int wch = 0;
  335. skip = 1;
  336. if(!utf8 && buffer[i] == '\x1a' && i + 7 < size
  337. && !memcmp(buffer + i + 1, "SAUCE00", 7))
  338. break; /* End before SAUCE data */
  339. else if(buffer[i] == '\r')
  340. {
  341. x = 0;
  342. }
  343. else if(buffer[i] == '\n')
  344. {
  345. x = 0;
  346. y++;
  347. }
  348. else if(buffer[i] == '\t')
  349. {
  350. x = (x + 7) & ~7;
  351. }
  352. else if(buffer[i] == '\x08')
  353. {
  354. if(x > 0)
  355. x--;
  356. }
  357. /* If there are not enough characters to parse the escape sequence,
  358. * wait until the next try. We require 3. */
  359. else if(buffer[i] == '\x1b' && i + 2 >= size)
  360. break;
  361. /* XXX: What the fuck is this shit? */
  362. else if(buffer[i] == '\x1b' && buffer[i + 1] == '('
  363. && buffer[i + 2] == 'B')
  364. {
  365. skip += 2;
  366. }
  367. /* Interpret escape commands, as per Standard ECMA-48 "Control
  368. * Functions for Coded Character Sets", 5.4. Control sequences. */
  369. else if(buffer[i] == '\x1b' && buffer[i + 1] == '[')
  370. {
  371. unsigned int argc = 0, argv[101];
  372. unsigned int param, inter, final;
  373. /* Compute offsets to parameter bytes, intermediate bytes and
  374. * to the final byte. Only the final byte is mandatory, there
  375. * can be zero of the others.
  376. * 0 param=2 inter final final+1
  377. * +-----+------------------+---------------------+-----------------+
  378. * | CSI | parameter bytes | intermediate bytes | final byte |
  379. * | | 0x30 - 0x3f | 0x20 - 0x2f | 0x40 - 0x7e |
  380. * | ^[[ | 0123456789:;<=>? | SPC !"#$%&'()*+,-./ | azAZ@[\]^_`{|}~ |
  381. * +-----+------------------+---------------------+-----------------+
  382. */
  383. param = 2;
  384. for(inter = param; i + inter < size; inter++)
  385. if(buffer[i + inter] < 0x30 || buffer[i + inter] > 0x3f)
  386. break;
  387. for(final = inter; i + final < size; final++)
  388. if(buffer[i + final] < 0x20 || buffer[i + final] > 0x2f)
  389. break;
  390. if(i + final >= size
  391. || buffer[i + final] < 0x40 || buffer[i + final] > 0x7e)
  392. break; /* Invalid Final Byte */
  393. skip += final;
  394. /* Sanity checks */
  395. if(param < inter && buffer[i + param] >= 0x3c)
  396. {
  397. /* Private sequence, only parse what we know */
  398. debug("ansi import: private sequence \"^[[%.*s\"",
  399. final - param + 1, buffer + i + param);
  400. continue; /* Private sequence, skip it entirely */
  401. }
  402. if(final - param > 100)
  403. continue; /* Suspiciously long sequence, skip it */
  404. /* Parse parameter bytes as per ECMA-48 5.4.2: Parameter string
  405. * format */
  406. if(param < inter)
  407. {
  408. argv[0] = 0;
  409. for(j = param; j < inter; j++)
  410. {
  411. if(buffer[i + j] == ';')
  412. argv[++argc] = 0;
  413. else if(buffer[i + j] >= '0' && buffer[i + j] <= '9')
  414. argv[argc] = 10 * argv[argc] + (buffer[i + j] - '0');
  415. }
  416. argc++;
  417. }
  418. /* Interpret final byte. The code representations are given in
  419. * ECMA-48 5.4: Control sequences, and the code definitions are
  420. * given in ECMA-48 8.3: Definition of control functions. */
  421. switch(buffer[i + final])
  422. {
  423. case 'H': /* CUP (0x48) - Cursor Position */
  424. x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0;
  425. y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0;
  426. break;
  427. case 'A': /* CUU (0x41) - Cursor Up */
  428. y -= argc ? argv[0] : 1;
  429. if(y < 0)
  430. y = 0;
  431. break;
  432. case 'B': /* CUD (0x42) - Cursor Down */
  433. y += argc ? argv[0] : 1;
  434. break;
  435. case 'C': /* CUF (0x43) - Cursor Right */
  436. x += argc ? argv[0] : 1;
  437. break;
  438. case 'D': /* CUB (0x44) - Cursor Left */
  439. x -= argc ? argv[0] : 1;
  440. if(x < 0)
  441. x = 0;
  442. break;
  443. case 'G': /* CHA (0x47) - Cursor Character Absolute */
  444. x = (argc && argv[0] > 0) ? argv[0] - 1 : 0;
  445. break;
  446. case 'J': /* ED (0x4a) - Erase In Page */
  447. savedattr = cucul_get_attr(cv, -1, -1);
  448. cucul_set_attr(cv, im.clearattr);
  449. if(!argc || argv[0] == 0)
  450. {
  451. cucul_draw_line(cv, x, y, width, y, ' ');
  452. cucul_fill_box(cv, 0, y + 1, width - 1, height - 1, ' ');
  453. }
  454. else if(argv[0] == 1)
  455. {
  456. cucul_fill_box(cv, 0, 0, width - 1, y - 1, ' ');
  457. cucul_draw_line(cv, 0, y, x, y, ' ');
  458. }
  459. else if(argv[0] == 2)
  460. //x = y = 0;
  461. cucul_fill_box(cv, 0, 0, width - 1, height - 1, ' ');
  462. cucul_set_attr(cv, savedattr);
  463. break;
  464. case 'K': /* EL (0x4b) - Erase In Line */
  465. if(!argc || argv[0] == 0)
  466. cucul_draw_line(cv, x, y, width, y, ' ');
  467. else if(argv[0] == 1)
  468. cucul_draw_line(cv, 0, y, x, y, ' ');
  469. else if(argv[0] == 2)
  470. if((unsigned int)x < width)
  471. cucul_draw_line(cv, x, y, width - 1, y, ' ');
  472. //x = width;
  473. break;
  474. case 'P': /* DCH (0x50) - Delete Character */
  475. if(!argc || argv[0] == 0)
  476. argv[0] = 1; /* echo -ne 'foobar\r\e[0P\n' */
  477. for(j = 0; (unsigned int)(j + argv[0]) < width; j++)
  478. {
  479. cucul_put_char(cv, j, y,
  480. cucul_get_char(cv, j + argv[0], y));
  481. cucul_put_attr(cv, j, y,
  482. cucul_get_attr(cv, j + argv[0], y));
  483. }
  484. #if 0
  485. savedattr = cucul_get_attr(cv, -1, -1);
  486. cucul_set_attr(cv, im.clearattr);
  487. for( ; (unsigned int)j < width; j++)
  488. cucul_put_char(cv, j, y, ' ');
  489. cucul_set_attr(cv, savedattr);
  490. #endif
  491. case 'X': /* ECH (0x58) - Erase Character */
  492. if(argc && argv[0])
  493. {
  494. savedattr = cucul_get_attr(cv, -1, -1);
  495. cucul_set_attr(cv, im.clearattr);
  496. cucul_draw_line(cv, x, y, x + argv[0] - 1, y, ' ');
  497. cucul_set_attr(cv, savedattr);
  498. }
  499. case 'd': /* VPA (0x64) - Line Position Absolute */
  500. y = (argc && argv[0] > 0) ? argv[0] - 1 : 0;
  501. break;
  502. case 'f': /* HVP (0x66) - Character And Line Position */
  503. x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0;
  504. y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0;
  505. break;
  506. case 'h': /* SM (0x68) - FIXME */
  507. debug("ansi import: set mode %i", argc ? (int)argv[0] : -1);
  508. break;
  509. case 'l': /* RM (0x6c) - FIXME */
  510. debug("ansi import: reset mode %i", argc ? (int)argv[0] : -1);
  511. break;
  512. case 'm': /* SGR (0x6d) - Select Graphic Rendition */
  513. if(argc)
  514. ansi_parse_grcm(cv, &im, argc, argv);
  515. else
  516. ansi_parse_grcm(cv, &im, 1, &dummy);
  517. break;
  518. case 's': /* Private (save cursor position) */
  519. save_x = x;
  520. save_y = y;
  521. break;
  522. case 'u': /* Private (reload cursor position) */
  523. x = save_x;
  524. y = save_y;
  525. break;
  526. default:
  527. debug("ansi import: unknown command \"^[[%.*s\"",
  528. final - param + 1, buffer + i + param);
  529. break;
  530. }
  531. }
  532. /* Parse OSC stuff. */
  533. else if(buffer[i] == '\x1b' && buffer[i + 1] == ']')
  534. {
  535. char *string;
  536. unsigned int command = 0;
  537. unsigned int mode = 2, semicolon, final;
  538. for(semicolon = mode; i + semicolon < size; semicolon++)
  539. {
  540. if(buffer[i + semicolon] < '0' || buffer[i + semicolon] > '9')
  541. break;
  542. command = 10 * command + (buffer[i + semicolon] - '0');
  543. }
  544. if(i + semicolon >= size || buffer[i + semicolon] != ';')
  545. break; /* Invalid Mode */
  546. for(final = semicolon + 1; i + final < size; final++)
  547. if(buffer[i + final] < 0x20)
  548. break;
  549. if(i + final >= size || buffer[i + final] != '\a')
  550. break; /* Not enough data or no bell found */
  551. /* FIXME: XTerm also reacts to <ESC><backslash> and <ST> */
  552. /* FIXME: differenciate between not enough data (try again)
  553. * and invalid data (print shit) */
  554. skip += final;
  555. string = malloc(final - (semicolon + 1) + 1);
  556. memcpy(string, buffer + (semicolon + 1), final - (semicolon + 1));
  557. string[final - (semicolon + 1)] = '\0';
  558. debug("ansi import: got OSC command %i string '%s'", command,
  559. string);
  560. free(string);
  561. }
  562. /* Get the character we’re going to paste */
  563. else if(utf8)
  564. {
  565. unsigned int bytes;
  566. if(i + 6 < size)
  567. ch = cucul_utf8_to_utf32((char const *)(buffer + i), &bytes);
  568. else
  569. {
  570. /* Add a trailing zero to what we're going to read */
  571. char tmp[7];
  572. memcpy(tmp, buffer + i, size - i);
  573. tmp[size - i] = '\0';
  574. ch = cucul_utf8_to_utf32(tmp, &bytes);
  575. }
  576. if(!bytes)
  577. {
  578. /* If the Unicode is invalid, assume it was latin1. */
  579. ch = buffer[i];
  580. bytes = 1;
  581. }
  582. wch = cucul_utf32_is_fullwidth(ch) ? 2 : 1;
  583. skip += bytes - 1;
  584. }
  585. else
  586. {
  587. ch = cucul_cp437_to_utf32(buffer[i]);
  588. wch = 1;
  589. }
  590. /* Wrap long lines or grow horizontally */
  591. while((unsigned int)x + wch > width)
  592. {
  593. if(growx)
  594. {
  595. savedattr = cucul_get_attr(cv, -1, -1);
  596. cucul_set_attr(cv, im.clearattr);
  597. cucul_set_canvas_size(cv, width = x + wch, height);
  598. cucul_set_attr(cv, savedattr);
  599. }
  600. else
  601. {
  602. x -= width;
  603. y++;
  604. }
  605. }
  606. /* Scroll or grow vertically */
  607. if((unsigned int)y >= height)
  608. {
  609. savedattr = cucul_get_attr(cv, -1, -1);
  610. cucul_set_attr(cv, im.clearattr);
  611. if(growy)
  612. {
  613. cucul_set_canvas_size(cv, width, height = y + 1);
  614. }
  615. else
  616. {
  617. int lines = (y - height) + 1;
  618. for(j = 0; j + lines < height; j++)
  619. {
  620. memcpy(cv->attrs + j * cv->width,
  621. cv->attrs + (j + lines) * cv->width, cv->width * 4);
  622. memcpy(cv->chars + j * cv->width,
  623. cv->chars + (j + lines) * cv->width, cv->width * 4);
  624. }
  625. cucul_fill_box(cv, 0, height - lines,
  626. cv->width - 1, height - 1, ' ');
  627. y -= lines;
  628. }
  629. cucul_set_attr(cv, savedattr);
  630. }
  631. /* Now paste our character, if any */
  632. if(wch)
  633. {
  634. cucul_put_char(cv, x, y, ch);
  635. x += wch;
  636. }
  637. }
  638. if(growy && (unsigned int)y > height)
  639. {
  640. savedattr = cucul_get_attr(cv, -1, -1);
  641. cucul_set_attr(cv, im.clearattr);
  642. cucul_set_canvas_size(cv, width, height = y);
  643. cucul_set_attr(cv, savedattr);
  644. }
  645. cv->frames[cv->frame].x = x;
  646. cv->frames[cv->frame].y = y;
  647. // if(utf8)
  648. // cucul_set_attr(cv, savedattr);
  649. return i;
  650. }
  651. /* XXX : ANSI loader helper */
  652. static void ansi_parse_grcm(cucul_canvas_t *cv, struct import *im,
  653. unsigned int argc, unsigned int const *argv)
  654. {
  655. static uint8_t const ansi2cucul[] =
  656. {
  657. CUCUL_BLACK, CUCUL_RED, CUCUL_GREEN, CUCUL_BROWN,
  658. CUCUL_BLUE, CUCUL_MAGENTA, CUCUL_CYAN, CUCUL_LIGHTGRAY
  659. };
  660. unsigned int j;
  661. uint8_t efg, ebg; /* Effective (libcucul) fg/bg */
  662. for(j = 0; j < argc; j++)
  663. {
  664. /* Defined in ECMA-48 8.3.117: SGR - SELECT GRAPHIC RENDITION */
  665. if(argv[j] >= 30 && argv[j] <= 37)
  666. im->fg = ansi2cucul[argv[j] - 30];
  667. else if(argv[j] >= 40 && argv[j] <= 47)
  668. im->bg = ansi2cucul[argv[j] - 40];
  669. else if(argv[j] >= 90 && argv[j] <= 97)
  670. im->fg = ansi2cucul[argv[j] - 90] + 8;
  671. else if(argv[j] >= 100 && argv[j] <= 107)
  672. im->bg = ansi2cucul[argv[j] - 100] + 8;
  673. else switch(argv[j])
  674. {
  675. case 0: /* default rendition */
  676. im->fg = im->dfg;
  677. im->bg = im->dbg;
  678. im->bold = im->blink = im->italics = im->negative
  679. = im->concealed = im->underline = im->faint = im->strike
  680. = im->proportional = 0;
  681. break;
  682. case 1: /* bold or increased intensity */
  683. im->bold = 1;
  684. break;
  685. case 2: /* faint, decreased intensity or second colour */
  686. im->faint = 1;
  687. break;
  688. case 3: /* italicized */
  689. im->italics = 1;
  690. break;
  691. case 4: /* singly underlined */
  692. im->underline = 1;
  693. break;
  694. case 5: /* slowly blinking (less then 150 per minute) */
  695. case 6: /* rapidly blinking (150 per minute or more) */
  696. im->blink = 1;
  697. break;
  698. case 7: /* negative image */
  699. im->negative = 1;
  700. break;
  701. case 8: /* concealed characters */
  702. im->concealed = 1;
  703. break;
  704. case 9: /* crossed-out (characters still legible but marked as to be
  705. * deleted */
  706. im->strike = 1;
  707. break;
  708. case 21: /* doubly underlined */
  709. im->underline = 1;
  710. break;
  711. case 22: /* normal colour or normal intensity (neither bold nor
  712. * faint) */
  713. im->bold = im->faint = 0;
  714. break;
  715. case 23: /* not italicized, not fraktur */
  716. im->italics = 0;
  717. break;
  718. case 24: /* not underlined (neither singly nor doubly) */
  719. im->underline = 0;
  720. break;
  721. case 25: /* steady (not blinking) */
  722. im->blink = 0;
  723. break;
  724. case 26: /* (reserved for proportional spacing as specified in CCITT
  725. * Recommendation T.61) */
  726. im->proportional = 1;
  727. break;
  728. case 27: /* positive image */
  729. im->negative = 0;
  730. break;
  731. case 28: /* revealed characters */
  732. im->concealed = 0;
  733. break;
  734. case 29: /* not crossed out */
  735. im->strike = 0;
  736. break;
  737. case 38: /* (reserved for future standardization, intended for setting
  738. * character foreground colour as specified in ISO 8613-6
  739. * [CCITT Recommendation T.416]) */
  740. break;
  741. case 39: /* default display colour (implementation-defined) */
  742. im->fg = im->dfg;
  743. break;
  744. case 48: /* (reserved for future standardization, intended for setting
  745. * character background colour as specified in ISO 8613-6
  746. * [CCITT Recommendation T.416]) */
  747. break;
  748. case 49: /* default background colour (implementation-defined) */
  749. im->bg = im->dbg;
  750. break;
  751. case 50: /* (reserved for cancelling the effect of the rendering
  752. * aspect established by parameter value 26) */
  753. im->proportional = 0;
  754. break;
  755. default:
  756. debug("ansi import: unknown sgr %i", argv[j]);
  757. break;
  758. }
  759. }
  760. if(im->concealed)
  761. {
  762. efg = ebg = CUCUL_TRANSPARENT;
  763. }
  764. else
  765. {
  766. efg = im->negative ? im->bg : im->fg;
  767. ebg = im->negative ? im->fg : im->bg;
  768. if(im->bold)
  769. {
  770. if(efg < 8)
  771. efg += 8;
  772. else if(efg == CUCUL_DEFAULT)
  773. efg = CUCUL_WHITE;
  774. }
  775. }
  776. cucul_set_color_ansi(cv, efg, ebg);
  777. }