Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

1165 wiersze
37 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  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 various 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. static inline int sprintu32(char *s, uint32_t x)
  27. {
  28. s[0] = (uint8_t)(x >> 24);
  29. s[1] = (uint8_t)(x >> 16) & 0xff;
  30. s[2] = (uint8_t)(x >> 8) & 0xff;
  31. s[3] = (uint8_t)(x ) & 0xff;
  32. return 4;
  33. }
  34. static inline int sprintu16(char *s, uint16_t x)
  35. {
  36. s[0] = (uint8_t)(x >> 8) & 0xff;
  37. s[1] = (uint8_t)(x ) & 0xff;
  38. return 2;
  39. }
  40. static void *export_caca(caca_canvas_t const *, size_t *);
  41. static void *export_ansi(caca_canvas_t const *, size_t *);
  42. static void *export_utf8(caca_canvas_t const *, size_t *, int);
  43. static void *export_html(caca_canvas_t const *, size_t *);
  44. static void *export_html3(caca_canvas_t const *, size_t *);
  45. static void *export_bbfr(caca_canvas_t const *, size_t *);
  46. static void *export_irc(caca_canvas_t const *, size_t *);
  47. static void *export_ps(caca_canvas_t const *, size_t *);
  48. static void *export_svg(caca_canvas_t const *, size_t *);
  49. static void *export_tga(caca_canvas_t const *, size_t *);
  50. /** \brief Export a canvas into a foreign format.
  51. *
  52. * This function exports a libcaca canvas into various foreign formats such
  53. * as ANSI art, HTML, IRC colours, etc. The returned pointer should be passed
  54. * to free() to release the allocated storage when it is no longer needed.
  55. *
  56. * Valid values for \c format are:
  57. * - \c "caca": export native libcaca files.
  58. * - \c "ansi": export ANSI art (CP437 charset with ANSI colour codes).
  59. * - \c "html": export an HTML page with CSS information.
  60. * - \c "html3": export an HTML table that should be compatible with
  61. * most navigators, including textmode ones.
  62. * - \c "irc": export UTF-8 text with mIRC colour codes.
  63. * - \c "ps": export a PostScript document.
  64. * - \c "svg": export an SVG vector image.
  65. * - \c "tga": export a TGA image.
  66. *
  67. * If an error occurs, NULL is returned and \b errno is set accordingly:
  68. * - \c EINVAL Unsupported format requested.
  69. * - \c ENOMEM Not enough memory to allocate output buffer.
  70. *
  71. * \param cv A libcaca canvas
  72. * \param format A string describing the requested output format.
  73. * \param bytes A pointer to a size_t where the number of allocated bytes
  74. * will be written.
  75. * \return A pointer to the exported memory area, or NULL in case of error.
  76. */
  77. void *caca_export_memory(caca_canvas_t const *cv, char const *format,
  78. size_t *bytes)
  79. {
  80. if(!strcasecmp("caca", format))
  81. return export_caca(cv, bytes);
  82. if(!strcasecmp("ansi", format))
  83. return export_ansi(cv, bytes);
  84. if(!strcasecmp("utf8", format))
  85. return export_utf8(cv, bytes, 0);
  86. if(!strcasecmp("utf8cr", format))
  87. return export_utf8(cv, bytes, 1);
  88. if(!strcasecmp("html", format))
  89. return export_html(cv, bytes);
  90. if(!strcasecmp("html3", format))
  91. return export_html3(cv, bytes);
  92. if(!strcasecmp("bbfr", format))
  93. return export_bbfr(cv, bytes);
  94. if(!strcasecmp("irc", format))
  95. return export_irc(cv, bytes);
  96. if(!strcasecmp("ps", format))
  97. return export_ps(cv, bytes);
  98. if(!strcasecmp("svg", format))
  99. return export_svg(cv, bytes);
  100. if(!strcasecmp("tga", format))
  101. return export_tga(cv, bytes);
  102. seterrno(EINVAL);
  103. return NULL;
  104. }
  105. /** \brief Get available export formats
  106. *
  107. * Return a list of available export formats. The list is a NULL-terminated
  108. * array of strings, interleaving a string containing the internal value for
  109. * the export format, to be used with caca_export_memory(), and a string
  110. * containing the natural language description for that export format.
  111. *
  112. * This function never fails.
  113. *
  114. * \return An array of strings.
  115. */
  116. char const * const * caca_get_export_list(void)
  117. {
  118. static char const * const list[] =
  119. {
  120. "caca", "native libcaca format",
  121. "ansi", "ANSI",
  122. "utf8", "UTF-8 with ANSI escape codes",
  123. "utf8cr", "UTF-8 with ANSI escape codes and MS-DOS \\r",
  124. "html", "HTML",
  125. "html3", "backwards-compatible HTML",
  126. "bbfr", "BBCode (French)",
  127. "irc", "IRC with mIRC colours",
  128. "ps", "PostScript document",
  129. "svg", "SVG vector image",
  130. "tga", "TGA image",
  131. NULL, NULL
  132. };
  133. return list;
  134. }
  135. /*
  136. * XXX: the following functions are local.
  137. */
  138. /* Generate a native libcaca canvas file. */
  139. static void *export_caca(caca_canvas_t const *cv, size_t *bytes)
  140. {
  141. char *data, *cur;
  142. int f, n;
  143. /* 52 bytes for the header:
  144. * - 4 bytes for "\xCA\xCA" + "CV"
  145. * - 16 bytes for the canvas header
  146. * - 32 bytes for the frame info
  147. * 8 bytes for each character cell */
  148. *bytes = 20 + (32 + 8 * cv->width * cv->height) * cv->framecount;
  149. cur = data = malloc(*bytes);
  150. /* magic */
  151. cur += sprintf(cur, "%s", "\xCA\xCA" "CV");
  152. /* canvas_header */
  153. cur += sprintu32(cur, 16 + 32 * cv->framecount);
  154. cur += sprintu32(cur, cv->width * cv->height * 8 * cv->framecount);
  155. cur += sprintu16(cur, 0x0001);
  156. cur += sprintu32(cur, cv->framecount);
  157. cur += sprintu16(cur, 0x0000);
  158. /* frame_info */
  159. for(f = 0; f < cv->framecount; f++)
  160. {
  161. cur += sprintu32(cur, cv->width);
  162. cur += sprintu32(cur, cv->height);
  163. cur += sprintu32(cur, 0);
  164. cur += sprintu32(cur, cv->curattr);
  165. cur += sprintu32(cur, cv->frames[f].x);
  166. cur += sprintu32(cur, cv->frames[f].y);
  167. cur += sprintu32(cur, cv->frames[f].handlex);
  168. cur += sprintu32(cur, cv->frames[f].handley);
  169. }
  170. /* canvas_data */
  171. for(f = 0; f < cv->framecount; f++)
  172. {
  173. uint32_t *attrs = cv->frames[f].attrs;
  174. uint32_t *chars = cv->frames[f].chars;
  175. for(n = cv->height * cv->width; n--; )
  176. {
  177. cur += sprintu32(cur, *chars++);
  178. cur += sprintu32(cur, *attrs++);
  179. }
  180. }
  181. return data;
  182. }
  183. /* Generate UTF-8 representation of current canvas. */
  184. static void *export_utf8(caca_canvas_t const *cv, size_t *bytes,
  185. int cr)
  186. {
  187. static uint8_t const palette[] =
  188. {
  189. 0, 4, 2, 6, 1, 5, 3, 7,
  190. 8, 12, 10, 14, 9, 13, 11, 15
  191. };
  192. char *data, *cur;
  193. int x, y;
  194. /* 23 bytes assumed for max length per pixel ('\e[5;1;3x;4y;9x;10ym' plus
  195. * 4 max bytes for a UTF-8 character).
  196. * Add height*9 to that (zeroes color at the end and jump to next line) */
  197. *bytes = (cv->height * 9) + (cv->width * cv->height * 23);
  198. cur = data = malloc(*bytes);
  199. for(y = 0; y < cv->height; y++)
  200. {
  201. uint32_t *lineattr = cv->attrs + y * cv->width;
  202. uint32_t *linechar = cv->chars + y * cv->width;
  203. uint8_t prevfg = 0x10;
  204. uint8_t prevbg = 0x10;
  205. for(x = 0; x < cv->width; x++)
  206. {
  207. uint32_t attr = lineattr[x];
  208. uint32_t ch = linechar[x];
  209. uint8_t ansifg, ansibg, fg, bg;
  210. if(ch == CACA_MAGIC_FULLWIDTH)
  211. continue;
  212. ansifg = caca_attr_to_ansi_fg(attr);
  213. ansibg = caca_attr_to_ansi_bg(attr);
  214. fg = ansifg < 0x10 ? palette[ansifg] : 0x10;
  215. bg = ansibg < 0x10 ? palette[ansibg] : 0x10;
  216. /* TODO: the [0 could be omitted in some cases */
  217. if(fg != prevfg || bg != prevbg)
  218. {
  219. cur += sprintf(cur, "\033[0");
  220. if(fg < 8)
  221. cur += sprintf(cur, ";3%d", fg);
  222. else if(fg < 16)
  223. cur += sprintf(cur, ";1;3%d;9%d", fg - 8, fg - 8);
  224. if(bg < 8)
  225. cur += sprintf(cur, ";4%d", bg);
  226. else if(bg < 16)
  227. cur += sprintf(cur, ";5;4%d;10%d", bg - 8, bg - 8);
  228. cur += sprintf(cur, "m");
  229. }
  230. cur += caca_utf32_to_utf8(cur, ch);
  231. prevfg = fg;
  232. prevbg = bg;
  233. }
  234. if(prevfg != 0x10 || prevbg != 0x10)
  235. cur += sprintf(cur, "\033[0m");
  236. cur += sprintf(cur, cr ? "\r\n" : "\n");
  237. }
  238. /* Crop to really used size */
  239. debug("utf8 export: alloc %lu bytes, realloc %lu",
  240. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  241. *bytes = (uintptr_t)(cur - data);
  242. data = realloc(data, *bytes);
  243. return data;
  244. }
  245. /* Generate ANSI representation of current canvas. */
  246. static void *export_ansi(caca_canvas_t const *cv, size_t *bytes)
  247. {
  248. static uint8_t const palette[] =
  249. {
  250. 0, 4, 2, 6, 1, 5, 3, 7,
  251. 8, 12, 10, 14, 9, 13, 11, 15
  252. };
  253. char *data, *cur;
  254. int x, y;
  255. uint8_t prevfg = -1;
  256. uint8_t prevbg = -1;
  257. /* 16 bytes assumed for max length per pixel ('\e[5;1;3x;4ym' plus
  258. * 1 byte for a CP437 character).
  259. * Add height*9 to that (zeroes color at the end and jump to next line) */
  260. *bytes = (cv->height * 9) + (cv->width * cv->height * 16);
  261. cur = data = malloc(*bytes);
  262. for(y = 0; y < cv->height; y++)
  263. {
  264. uint32_t *lineattr = cv->attrs + y * cv->width;
  265. uint32_t *linechar = cv->chars + y * cv->width;
  266. for(x = 0; x < cv->width; x++)
  267. {
  268. uint8_t ansifg = caca_attr_to_ansi_fg(lineattr[x]);
  269. uint8_t ansibg = caca_attr_to_ansi_bg(lineattr[x]);
  270. uint8_t fg = ansifg < 0x10 ? palette[ansifg] : CACA_LIGHTGRAY;
  271. uint8_t bg = ansibg < 0x10 ? palette[ansibg] : CACA_BLACK;
  272. uint32_t ch = linechar[x];
  273. if(ch == CACA_MAGIC_FULLWIDTH)
  274. ch = '?';
  275. if(fg != prevfg || bg != prevbg)
  276. {
  277. cur += sprintf(cur, "\033[0;");
  278. if(fg < 8)
  279. if(bg < 8)
  280. cur += sprintf(cur, "3%d;4%dm", fg, bg);
  281. else
  282. cur += sprintf(cur, "5;3%d;4%dm", fg, bg - 8);
  283. else
  284. if(bg < 8)
  285. cur += sprintf(cur, "1;3%d;4%dm", fg - 8, bg);
  286. else
  287. cur += sprintf(cur, "5;1;3%d;4%dm", fg - 8, bg - 8);
  288. }
  289. *cur++ = caca_utf32_to_cp437(ch);
  290. prevfg = fg;
  291. prevbg = bg;
  292. }
  293. if(cv->width == 80)
  294. {
  295. cur += sprintf(cur, "\033[s\n\033[u");
  296. }
  297. else
  298. {
  299. cur += sprintf(cur, "\033[0m\r\n");
  300. prevfg = -1;
  301. prevbg = -1;
  302. }
  303. }
  304. /* Crop to really used size */
  305. debug("ansi export: alloc %lu bytes, realloc %lu",
  306. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  307. *bytes = (uintptr_t)(cur - data);
  308. data = realloc(data, *bytes);
  309. return data;
  310. }
  311. /* Generate HTML representation of current canvas. */
  312. static void *export_html(caca_canvas_t const *cv, size_t *bytes)
  313. {
  314. char *data, *cur;
  315. int x, y, len;
  316. /* The HTML header: less than 1000 bytes
  317. * A line: 7 chars for "<br />\n"
  318. * A glyph: 47 chars for "<span style="color:#xxx;background-color:#xxx">"
  319. * 83 chars for ";font-weight..."
  320. * up to 9 chars for "&#xxxxxx;", far less for pure ASCII
  321. * 7 chars for "</span>" */
  322. *bytes = 1000 + cv->height * (7 + cv->width * (47 + 83 + 9 + 7));
  323. cur = data = malloc(*bytes);
  324. /* HTML header */
  325. cur += sprintf(cur, "<html><head>\n");
  326. cur += sprintf(cur, "<title>Generated by libcaca %s</title>\n",
  327. caca_get_version());
  328. cur += sprintf(cur, "</head><body>\n");
  329. cur += sprintf(cur, "<div cellpadding='0' cellspacing='0' style='%s'>\n",
  330. "font-family: monospace, fixed; font-weight: bold;");
  331. for(y = 0; y < cv->height; y++)
  332. {
  333. uint32_t *lineattr = cv->attrs + y * cv->width;
  334. uint32_t *linechar = cv->chars + y * cv->width;
  335. for(x = 0; x < cv->width; x += len)
  336. {
  337. cur += sprintf(cur, "<span style=\"");
  338. if(caca_attr_to_ansi_fg(lineattr[x]) < 0x10)
  339. cur += sprintf(cur, ";color:#%.03x",
  340. caca_attr_to_rgb12_fg(lineattr[x]));
  341. if(caca_attr_to_ansi_bg(lineattr[x]) < 0x10)
  342. cur += sprintf(cur, ";background-color:#%.03x",
  343. caca_attr_to_rgb12_bg(lineattr[x]));
  344. if(lineattr[x] & CACA_BOLD)
  345. cur += sprintf(cur, ";font-weight:bold");
  346. if(lineattr[x] & CACA_ITALICS)
  347. cur += sprintf(cur, ";font-style:italic");
  348. if(lineattr[x] & CACA_UNDERLINE)
  349. cur += sprintf(cur, ";text-decoration:underline");
  350. if(lineattr[x] & CACA_BLINK)
  351. cur += sprintf(cur, ";text-decoration:blink");
  352. cur += sprintf(cur, "\">");
  353. for(len = 0;
  354. x + len < cv->width && lineattr[x + len] == lineattr[x];
  355. len++)
  356. {
  357. if(linechar[x + len] == CACA_MAGIC_FULLWIDTH)
  358. ;
  359. else if(linechar[x + len] <= 0x00000020)
  360. cur += sprintf(cur, "&nbsp;");
  361. else if(linechar[x + len] < 0x00000080)
  362. cur += sprintf(cur, "%c", (uint8_t)linechar[x + len]);
  363. else
  364. cur += sprintf(cur, "&#%i;",
  365. (unsigned int)linechar[x + len]);
  366. }
  367. cur += sprintf(cur, "</span>");
  368. }
  369. /* New line */
  370. cur += sprintf(cur, "<br />\n");
  371. }
  372. cur += sprintf(cur, "</div></body></html>\n");
  373. /* Crop to really used size */
  374. debug("html export: alloc %lu bytes, realloc %lu",
  375. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  376. *bytes = (uintptr_t)(cur - data);
  377. data = realloc(data, *bytes);
  378. return data;
  379. }
  380. /* Export an HTML3 document. This function is way bigger than export_html(),
  381. * but permits viewing in old browsers (or limited ones such as links). It
  382. * will not work under gecko (mozilla rendering engine) unless you set a
  383. * correct header. */
  384. static void *export_html3(caca_canvas_t const *cv, size_t *bytes)
  385. {
  386. char *data, *cur;
  387. int x, y, len;
  388. int has_multi_cell_row = 0;
  389. unsigned char *cell_boundary_bitmap;
  390. /* Table */
  391. cell_boundary_bitmap = (unsigned char *) malloc((cv->width + 7) / 8);
  392. if(cell_boundary_bitmap)
  393. memset((void *) cell_boundary_bitmap, 0, (cv->width + 7) / 8);
  394. for(y = 0; y < cv->height; y++)
  395. {
  396. uint32_t *lineattr = cv->attrs + y * cv->width;
  397. for(x = 1; x < cv->width; x++)
  398. if((! (cell_boundary_bitmap
  399. ?
  400. (cell_boundary_bitmap[x / 8] & (1 << (x % 8)))
  401. :
  402. has_multi_cell_row))
  403. &&
  404. ((caca_attr_to_ansi_bg(lineattr[x - 1])
  405. !=
  406. caca_attr_to_ansi_bg(lineattr[x]))
  407. ||
  408. ((caca_attr_to_ansi_bg(lineattr[x]) < 0x10)
  409. ?
  410. (_caca_attr_to_rgb24bg(lineattr[x - 1])
  411. !=
  412. _caca_attr_to_rgb24bg(lineattr[x]))
  413. :
  414. 0)))
  415. {
  416. has_multi_cell_row = 1;
  417. if(cell_boundary_bitmap)
  418. cell_boundary_bitmap[x / 8] |= 1 << (x % 8);
  419. }
  420. }
  421. /* The HTML table markup: less than 1000 bytes
  422. * A line: 10 chars for "<tr></tr>\n"
  423. * A glyph: up to 48 chars for "<td bgcolor=\"#xxxxxx\"><tt><font color=\"#xxxxxx\">"
  424. * up to 36 chars for "<b><i><u><blink></blink></u></i></b>"
  425. * up to 10 chars for "&#xxxxxxx;" (far less for pure ASCII)
  426. * 17 chars for "</font></tt></td>" */
  427. *bytes = 1000 + cv->height * (10 + cv->width * (48 + 36 + 10 + 17));
  428. cur = data = malloc(*bytes);
  429. cur += sprintf(cur, "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" summary=\"[libcaca canvas export]\">\n");
  430. for(y = 0; y < cv->height; y++)
  431. {
  432. uint32_t *lineattr = cv->attrs + y * cv->width;
  433. uint32_t *linechar = cv->chars + y * cv->width;
  434. cur += sprintf(cur, "<tr>");
  435. for(x = 0; x < cv->width; x += len)
  436. {
  437. int i, needfont = 0;
  438. int nonblank = 0;
  439. /* Use colspan option to factor cells with same attributes
  440. * (see below) */
  441. len = 1;
  442. while((x + len < cv->width)
  443. &&
  444. (y
  445. ||
  446. (! (cell_boundary_bitmap
  447. ?
  448. (cell_boundary_bitmap[(x + len) / 8] & (1 << ((x + len) % 8)))
  449. :
  450. has_multi_cell_row))
  451. ||
  452. (linechar[x + len] == CACA_MAGIC_FULLWIDTH)
  453. ||
  454. (cv->height == 1))
  455. &&
  456. (caca_attr_to_ansi_bg(lineattr[x + len])
  457. ==
  458. caca_attr_to_ansi_bg(lineattr[x]))
  459. &&
  460. ((caca_attr_to_ansi_bg(lineattr[x]) < 0x10)
  461. ?
  462. (_caca_attr_to_rgb24bg(lineattr[x + len])
  463. ==
  464. _caca_attr_to_rgb24bg(lineattr[x]))
  465. :
  466. 1))
  467. len++;
  468. for(i = 0; i < len; i++)
  469. if(! ((linechar[x + i] <= 0x00000020)
  470. ||
  471. ((linechar[x + i] >= 0x0000007f)
  472. &&
  473. (linechar[x + i] <= 0x000000a0))))
  474. nonblank = 1;
  475. cur += sprintf(cur, "<td");
  476. if(caca_attr_to_ansi_bg(lineattr[x]) < 0x10)
  477. cur += sprintf(cur, " bgcolor=\"#%.06lx\"", (unsigned long int)
  478. _caca_attr_to_rgb24bg(lineattr[x]));
  479. if(has_multi_cell_row && (len > 1))
  480. {
  481. int colspan;
  482. colspan = len;
  483. if(cell_boundary_bitmap)
  484. for(i = 0; i < len; i ++)
  485. if(i
  486. &&
  487. ! (cell_boundary_bitmap[(x + i) / 8]
  488. &
  489. (1 << ((x + i) % 8))))
  490. colspan --;
  491. if(colspan > 1)
  492. cur += sprintf(cur, " colspan=\"%d\"", colspan);
  493. }
  494. cur += sprintf(cur, ">");
  495. cur += sprintf(cur, "<tt>");
  496. for(i = 0; i < len; i++)
  497. {
  498. if(nonblank
  499. &&
  500. ((! i)
  501. ||
  502. (lineattr[x + i] != lineattr[x + i - 1])))
  503. {
  504. needfont = (caca_attr_to_ansi_fg(lineattr[x + i])
  505. !=
  506. CACA_DEFAULT);
  507. if(needfont)
  508. cur += sprintf(cur, "<font color=\"#%.06lx\">",
  509. (unsigned long int)
  510. _caca_attr_to_rgb24fg(lineattr[x + i]));
  511. if(lineattr[x + i] & CACA_BOLD)
  512. cur += sprintf(cur, "<b>");
  513. if(lineattr[x + i] & CACA_ITALICS)
  514. cur += sprintf(cur, "<i>");
  515. if(lineattr[x + i] & CACA_UNDERLINE)
  516. cur += sprintf(cur, "<u>");
  517. if(lineattr[x + i] & CACA_BLINK)
  518. cur += sprintf(cur, "<blink>");
  519. }
  520. if(linechar[x + i] == CACA_MAGIC_FULLWIDTH)
  521. ;
  522. else if((linechar[x + i] <= 0x00000020)
  523. ||
  524. ((linechar[x + i] >= 0x0000007f)
  525. &&
  526. (linechar[x + i] <= 0x000000a0)))
  527. {
  528. /* Control characters and space converted to
  529. * U+00A0 NO-BREAK SPACE, a.k.a. "&nbsp;" in HTML,
  530. * but we use the equivalent numeric character
  531. * reference &#160; so this will work in plain
  532. * XHTML with no DTD too. */
  533. cur += sprintf(cur, "&#160;");
  534. }
  535. else if(linechar[x + i] == '&')
  536. cur += sprintf(cur, "&amp;");
  537. else if(linechar[x + i] == '<')
  538. cur += sprintf(cur, "&lt;");
  539. else if(linechar[x + i] == '>')
  540. cur += sprintf(cur, "&gt;");
  541. else if(linechar[x + i] == '\"')
  542. cur += sprintf(cur, "&quot;");
  543. else if(linechar[x + i] == '\'')
  544. cur += sprintf(cur, "&#39;");
  545. else if(linechar[x + i] < 0x00000080)
  546. cur += sprintf(cur, "%c", (uint8_t)linechar[x + i]);
  547. else if((linechar[x + i] <= 0x0010fffd)
  548. &&
  549. ((linechar[x + i] & 0x0000fffe) != 0x0000fffe)
  550. &&
  551. ((linechar[x + i] < 0x0000d800)
  552. ||
  553. (linechar[x + i] > 0x0000dfff)))
  554. cur += sprintf(cur, "&#%i;", (unsigned int)linechar[x + i]);
  555. else
  556. /* non-character codepoints become U+FFFD
  557. * REPLACEMENT CHARACTER */
  558. cur += sprintf(cur, "&#%i;", (unsigned int)0x0000fffd);
  559. if (nonblank
  560. &&
  561. (((i + 1) == len)
  562. ||
  563. (lineattr[x + i + 1] != lineattr[x + i])))
  564. {
  565. if(lineattr[x + i] & CACA_BLINK)
  566. cur += sprintf(cur, "</blink>");
  567. if(lineattr[x + i] & CACA_UNDERLINE)
  568. cur += sprintf(cur, "</u>");
  569. if(lineattr[x + i] & CACA_ITALICS)
  570. cur += sprintf(cur, "</i>");
  571. if(lineattr[x + i] & CACA_BOLD)
  572. cur += sprintf(cur, "</b>");
  573. if(needfont)
  574. cur += sprintf(cur, "</font>");
  575. }
  576. }
  577. cur += sprintf(cur, "</tt>");
  578. cur += sprintf(cur, "</td>");
  579. }
  580. cur += sprintf(cur, "</tr>\n");
  581. }
  582. /* Footer */
  583. cur += sprintf(cur, "</table>\n");
  584. /* Free working memory */
  585. if (cell_boundary_bitmap)
  586. free((void *) cell_boundary_bitmap);
  587. /* Crop to really used size */
  588. debug("html3 export: alloc %lu bytes, realloc %lu",
  589. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  590. *bytes = (uintptr_t)(cur - data);
  591. data = realloc(data, *bytes);
  592. return data;
  593. }
  594. static void *export_bbfr(caca_canvas_t const *cv, size_t *bytes)
  595. {
  596. char *data, *cur;
  597. int x, y, len;
  598. /* The font markup: less than 100 bytes
  599. * A line: 1 char for "\n"
  600. * A glyph: 22 chars for "[f=#xxxxxx][c=#xxxxxx]"
  601. * up to 21 chars for "[g][i][s][/s][/i][/g]"
  602. * up to 6 chars for the UTF-8 glyph
  603. * 8 chars for "[/c][/f]" */
  604. *bytes = 100 + cv->height * (1 + cv->width * (22 + 21 + 6 + 8));
  605. cur = data = malloc(*bytes);
  606. /* Table */
  607. cur += sprintf(cur, "[font=Courier New]");
  608. for(y = 0; y < cv->height; y++)
  609. {
  610. uint32_t *lineattr = cv->attrs + y * cv->width;
  611. uint32_t *linechar = cv->chars + y * cv->width;
  612. for(x = 0; x < cv->width; x += len)
  613. {
  614. int i, needback, needfront;
  615. /* Use colspan option to factor cells with same attributes
  616. * (see below) */
  617. len = 1;
  618. if(linechar[x] == ' ')
  619. while(x + len < cv->width && lineattr[x + len] == lineattr[x]
  620. && linechar[x] == ' ')
  621. len++;
  622. else
  623. while(x + len < cv->width && lineattr[x + len] == lineattr[x]
  624. && linechar[x] != ' ')
  625. len++;
  626. needback = caca_attr_to_ansi_bg(lineattr[x]) < 0x10;
  627. needfront = caca_attr_to_ansi_fg(lineattr[x]) < 0x10;
  628. if(needback)
  629. cur += sprintf(cur, "[f=#%.06lx]", (unsigned long int)
  630. _caca_attr_to_rgb24bg(lineattr[x]));
  631. if(linechar[x] == ' ')
  632. cur += sprintf(cur, "[c=#%.06lx]", (unsigned long int)
  633. _caca_attr_to_rgb24bg(lineattr[x]));
  634. else if(needfront)
  635. cur += sprintf(cur, "[c=#%.06lx]", (unsigned long int)
  636. _caca_attr_to_rgb24fg(lineattr[x]));
  637. if(lineattr[x] & CACA_BOLD)
  638. cur += sprintf(cur, "[g]");
  639. if(lineattr[x] & CACA_ITALICS)
  640. cur += sprintf(cur, "[i]");
  641. if(lineattr[x] & CACA_UNDERLINE)
  642. cur += sprintf(cur, "[s]");
  643. if(lineattr[x] & CACA_BLINK)
  644. ; /* FIXME */
  645. for(i = 0; i < len; i++)
  646. {
  647. if(linechar[x + i] == CACA_MAGIC_FULLWIDTH)
  648. ;
  649. else if(linechar[x + i] == ' ')
  650. *cur++ = '_';
  651. else
  652. cur += caca_utf32_to_utf8(cur, linechar[x + i]);
  653. }
  654. if(lineattr[x] & CACA_BLINK)
  655. ; /* FIXME */
  656. if(lineattr[x] & CACA_UNDERLINE)
  657. cur += sprintf(cur, "[/s]");
  658. if(lineattr[x] & CACA_ITALICS)
  659. cur += sprintf(cur, "[/i]");
  660. if(lineattr[x] & CACA_BOLD)
  661. cur += sprintf(cur, "[/g]");
  662. if(linechar[x] == ' ' || needfront)
  663. cur += sprintf(cur, "[/c]");
  664. if(needback)
  665. cur += sprintf(cur, "[/f]");
  666. }
  667. cur += sprintf(cur, "\n");
  668. }
  669. /* Footer */
  670. cur += sprintf(cur, "[/font]\n");
  671. /* Crop to really used size */
  672. debug("bbfr export: alloc %lu bytes, realloc %lu",
  673. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  674. *bytes = (uintptr_t)(cur - data);
  675. data = realloc(data, *bytes);
  676. return data;
  677. }
  678. /* Export a text file with IRC colours */
  679. static void *export_irc(caca_canvas_t const *cv, size_t *bytes)
  680. {
  681. static uint8_t const palette[] =
  682. {
  683. 1, 2, 3, 10, 5, 6, 7, 15, /* Dark */
  684. 14, 12, 9, 11, 4, 13, 8, 0, /* Light */
  685. };
  686. char *data, *cur;
  687. int x, y;
  688. /* 14 bytes assumed for max length per pixel. Worst case scenario:
  689. * ^Cxx,yy 6 bytes
  690. * ^B^B 2 bytes
  691. * ch 6 bytes
  692. * 3 bytes for max length per line. Worst case scenario:
  693. * <spc> 1 byte (for empty lines)
  694. * \r\n 2 bytes
  695. * In real life, the average bytes per pixel value will be around 5.
  696. */
  697. *bytes = 2 + cv->height * (3 + cv->width * 14);
  698. cur = data = malloc(*bytes);
  699. for(y = 0; y < cv->height; y++)
  700. {
  701. uint32_t *lineattr = cv->attrs + y * cv->width;
  702. uint32_t *linechar = cv->chars + y * cv->width;
  703. uint8_t prevfg = 0x10;
  704. uint8_t prevbg = 0x10;
  705. for(x = 0; x < cv->width; x++)
  706. {
  707. uint32_t attr = lineattr[x];
  708. uint32_t ch = linechar[x];
  709. uint8_t ansifg, ansibg, fg, bg;
  710. if(ch == CACA_MAGIC_FULLWIDTH)
  711. continue;
  712. ansifg = caca_attr_to_ansi_fg(attr);
  713. ansibg = caca_attr_to_ansi_bg(attr);
  714. fg = ansifg < 0x10 ? palette[ansifg] : 0x10;
  715. bg = ansibg < 0x10 ? palette[ansibg] : 0x10;
  716. /* TODO: optimise series of same fg / same bg
  717. * don't change fg value if ch == ' '
  718. * make sure the \x03,%d trick works everywhere */
  719. if(bg != prevbg || fg != prevfg)
  720. {
  721. int need_escape = 0;
  722. if(bg == 0x10)
  723. {
  724. if(fg == 0x10)
  725. cur += sprintf(cur, "\x0f");
  726. else
  727. {
  728. if(prevbg == 0x10)
  729. cur += sprintf(cur, "\x03%d", fg);
  730. else
  731. cur += sprintf(cur, "\x0f\x03%d", fg);
  732. if(ch == (uint32_t)',')
  733. need_escape = 1;
  734. }
  735. }
  736. else
  737. {
  738. if(fg == 0x10)
  739. cur += sprintf(cur, "\x0f\x03,%d", bg);
  740. else
  741. cur += sprintf(cur, "\x03%d,%d", fg, bg);
  742. }
  743. if(ch >= (uint32_t)'0' && ch <= (uint32_t)'9')
  744. need_escape = 1;
  745. if(need_escape)
  746. cur += sprintf(cur, "\x02\x02");
  747. }
  748. cur += caca_utf32_to_utf8(cur, ch);
  749. prevfg = fg;
  750. prevbg = bg;
  751. }
  752. /* TODO: do the same the day we optimise whole lines above */
  753. if(!cv->width)
  754. *cur++ = ' ';
  755. *cur++ = '\r';
  756. *cur++ = '\n';
  757. }
  758. /* Crop to really used size */
  759. debug("IRC export: alloc %lu bytes, realloc %lu",
  760. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  761. *bytes = (uintptr_t)(cur - data);
  762. data = realloc(data, *bytes);
  763. return data;
  764. }
  765. /* Export a PostScript document. */
  766. static void *export_ps(caca_canvas_t const *cv, size_t *bytes)
  767. {
  768. static char const *ps_header =
  769. "%!\n"
  770. "%% libcaca PDF export\n"
  771. "%%LanguageLevel: 2\n"
  772. "%%Pages: 1\n"
  773. "%%DocumentData: Clean7Bit\n"
  774. "/csquare {\n"
  775. " newpath\n"
  776. " 0 0 moveto\n"
  777. " 0 1 rlineto\n"
  778. " 1 0 rlineto\n"
  779. " 0 -1 rlineto\n"
  780. " closepath\n"
  781. " setrgbcolor\n"
  782. " fill\n"
  783. "} def\n"
  784. "/S {\n"
  785. " Show\n"
  786. "} bind def\n"
  787. "/Courier-Bold findfont\n"
  788. "8 scalefont\n"
  789. "setfont\n"
  790. "gsave\n"
  791. "6 10 scale\n";
  792. char *data, *cur;
  793. int x, y;
  794. /* 200 is arbitrary but should be ok */
  795. *bytes = strlen(ps_header) + 100 + cv->height * (32 + cv->width * 200);
  796. cur = data = malloc(*bytes);
  797. /* Header */
  798. cur += sprintf(cur, "%s", ps_header);
  799. cur += sprintf(cur, "0 %d translate\n", cv->height);
  800. /* Background, drawn using csquare macro defined in header */
  801. for(y = cv->height; y--; )
  802. {
  803. uint32_t *lineattr = cv->attrs + y * cv->width;
  804. for(x = 0; x < cv->width; x++)
  805. {
  806. uint8_t argb[8];
  807. caca_attr_to_argb64(*lineattr++, argb);
  808. cur += sprintf(cur, "1 0 translate\n %f %f %f csquare\n",
  809. (float)argb[1] * (1.0 / 0xf),
  810. (float)argb[2] * (1.0 / 0xf),
  811. (float)argb[3] * (1.0 / 0xf));
  812. }
  813. /* Return to beginning of the line, and jump to the next one */
  814. cur += sprintf(cur, "-%d 1 translate\n", cv->width);
  815. }
  816. cur += sprintf(cur, "grestore\n"); /* Restore transformation matrix */
  817. cur += sprintf(cur, "0 %d translate\n", cv->height*10);
  818. for(y = cv->height; y--; )
  819. {
  820. uint32_t *lineattr = cv->attrs + (cv->height - y - 1) * cv->width;
  821. uint32_t *linechar = cv->chars + (cv->height - y - 1) * cv->width;
  822. for(x = 0; x < cv->width; x++)
  823. {
  824. uint8_t argb[8];
  825. uint32_t ch = *linechar++;
  826. caca_attr_to_argb64(*lineattr++, argb);
  827. cur += sprintf(cur, "newpath\n");
  828. cur += sprintf(cur, "%d %d moveto\n", (x + 1) * 6, y * 10 + 2);
  829. cur += sprintf(cur, "%f %f %f setrgbcolor\n",
  830. (float)argb[5] * (1.0 / 0xf),
  831. (float)argb[6] * (1.0 / 0xf),
  832. (float)argb[7] * (1.0 / 0xf));
  833. if(ch < 0x00000020)
  834. cur += sprintf(cur, "(?) show\n");
  835. else if(ch >= 0x00000080)
  836. cur += sprintf(cur, "(?) show\n");
  837. else switch((uint8_t)(ch & 0x7f))
  838. {
  839. case '\\':
  840. case '(':
  841. case ')':
  842. cur += sprintf(cur, "(\\%c) show\n", (uint8_t)ch);
  843. break;
  844. default:
  845. cur += sprintf(cur, "(%c) show\n", (uint8_t)ch);
  846. break;
  847. }
  848. }
  849. }
  850. cur += sprintf(cur, "showpage\n");
  851. /* Crop to really used size */
  852. debug("PS export: alloc %lu bytes, realloc %lu",
  853. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  854. *bytes = (uintptr_t)(cur - data);
  855. data = realloc(data, *bytes);
  856. return data;
  857. }
  858. /* Export an SVG vector image */
  859. static void *export_svg(caca_canvas_t const *cv, size_t *bytes)
  860. {
  861. static char const svg_header[] =
  862. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  863. "<svg width=\"%d\" height=\"%d\" viewBox=\"0 0 %d %d\""
  864. " xmlns=\"http://www.w3.org/2000/svg\""
  865. " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
  866. " xml:space=\"preserve\" version=\"1.1\" baseProfile=\"full\">\n";
  867. char *data, *cur;
  868. int x, y;
  869. /* 200 is arbitrary but should be ok */
  870. *bytes = strlen(svg_header) + 128 + cv->width * cv->height * 200;
  871. cur = data = malloc(*bytes);
  872. /* Header */
  873. cur += sprintf(cur, svg_header, cv->width * 6, cv->height * 10,
  874. cv->width * 6, cv->height * 10);
  875. cur += sprintf(cur, " <g id=\"mainlayer\" font-size=\"10\""
  876. " style=\"font-family: monospace\">\n");
  877. /* Background */
  878. for(y = 0; y < cv->height; y++)
  879. {
  880. uint32_t *lineattr = cv->attrs + y * cv->width;
  881. for(x = 0; x < cv->width; x++)
  882. {
  883. cur += sprintf(cur, "<rect style=\"fill:#%.03x\" x=\"%d\" y=\"%d\""
  884. " width=\"6\" height=\"10\"/>\n",
  885. caca_attr_to_rgb12_bg(*lineattr++),
  886. x * 6, y * 10);
  887. }
  888. }
  889. /* Text */
  890. for(y = 0; y < cv->height; y++)
  891. {
  892. uint32_t *lineattr = cv->attrs + y * cv->width;
  893. uint32_t *linechar = cv->chars + y * cv->width;
  894. for(x = 0; x < cv->width; x++)
  895. {
  896. uint32_t ch = *linechar++;
  897. if(ch == ' ' || ch == CACA_MAGIC_FULLWIDTH)
  898. {
  899. lineattr++;
  900. continue;
  901. }
  902. cur += sprintf(cur, "<text style=\"fill:#%.03x\" "
  903. "x=\"%d\" y=\"%d\">",
  904. caca_attr_to_rgb12_fg(*lineattr++),
  905. x * 6, (y * 10) + 8);
  906. if(ch < 0x00000020)
  907. *cur++ = '?';
  908. else if(ch > 0x0000007f)
  909. cur += caca_utf32_to_utf8(cur, ch);
  910. else switch((uint8_t)ch)
  911. {
  912. case '>': cur += sprintf(cur, "&gt;"); break;
  913. case '<': cur += sprintf(cur, "&lt;"); break;
  914. case '&': cur += sprintf(cur, "&amp;"); break;
  915. default: *cur++ = ch; break;
  916. }
  917. cur += sprintf(cur, "</text>\n");
  918. }
  919. }
  920. cur += sprintf(cur, " </g>\n");
  921. cur += sprintf(cur, "</svg>\n");
  922. /* Crop to really used size */
  923. debug("SVG export: alloc %lu bytes, realloc %lu",
  924. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  925. *bytes = (uintptr_t)(cur - data);
  926. data = realloc(data, *bytes);
  927. return data;
  928. }
  929. /* Export a TGA image */
  930. static void *export_tga(caca_canvas_t const *cv, size_t *bytes)
  931. {
  932. char const * const *fontlist;
  933. char *data, *cur;
  934. caca_font_t *f;
  935. int i, w, h;
  936. fontlist = caca_get_font_list();
  937. if(!fontlist[0])
  938. {
  939. seterrno(EINVAL);
  940. return NULL;
  941. }
  942. f = caca_load_font(fontlist[0], 0);
  943. w = caca_get_canvas_width(cv) * caca_get_font_width(f);
  944. h = caca_get_canvas_height(cv) * caca_get_font_height(f);
  945. *bytes = w * h * 4 + 18; /* 32 bpp + 18 bytes for the header */
  946. cur = data = malloc(*bytes);
  947. /* ID Length */
  948. cur += sprintf(cur, "%c", 0);
  949. /* Color Map Type: no colormap */
  950. cur += sprintf(cur, "%c", 0);
  951. /* Image Type: uncompressed truecolor */
  952. cur += sprintf(cur, "%c", 2);
  953. /* Color Map Specification: no color map */
  954. memset(cur, 0, 5); cur += 5;
  955. /* Image Specification */
  956. cur += sprintf(cur, "%c%c", 0, 0); /* X Origin */
  957. cur += sprintf(cur, "%c%c", 0, 0); /* Y Origin */
  958. cur += sprintf(cur, "%c%c", w & 0xff, w >> 8); /* Width */
  959. cur += sprintf(cur, "%c%c", h & 0xff, h >> 8); /* Height */
  960. cur += sprintf(cur, "%c", 32); /* Pixel Depth */
  961. cur += sprintf(cur, "%c", 40); /* Image Descriptor */
  962. /* Image ID: no ID */
  963. /* Color Map Data: no colormap */
  964. /* Image Data */
  965. caca_render_canvas(cv, f, cur, w, h, 4 * w);
  966. /* Swap bytes. What a waste of time. */
  967. for(i = 0; i < w * h * 4; i += 4)
  968. {
  969. char c;
  970. c = cur[i]; cur[i] = cur[i + 3]; cur[i + 3] = c;
  971. c = cur[i + 1]; cur[i + 1] = cur[i + 2]; cur[i + 2] = c;
  972. }
  973. caca_free_font(f);
  974. return data;
  975. }
  976. /*
  977. * XXX: The following functions are aliases.
  978. */
  979. void *cucul_export_memory(cucul_canvas_t const *, char const *,
  980. size_t *) CACA_ALIAS(caca_export_memory);
  981. char const * const * cucul_get_export_list(void)
  982. CACA_ALIAS(caca_get_export_list);