Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

897 lignes
28 KiB

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