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

895 рядки
28 KiB

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