Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

897 řádky
28 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2014 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://www.wtfpl.net/ 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 + 8) & ~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. break;
  264. case 'X': /* ECH (0x58) - Erase Character */
  265. if(argc && argv[0])
  266. {
  267. savedattr = caca_get_attr(cv, -1, -1);
  268. caca_set_attr(cv, im.clearattr);
  269. caca_draw_line(cv, x, y, x + argv[0] - 1, y, ' ');
  270. caca_set_attr(cv, savedattr);
  271. }
  272. break;
  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. }