Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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