You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1021 regels
32 KiB

  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  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 "cucul.h"
  25. #include "cucul_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(cucul_canvas_t const *, unsigned long int *);
  41. static void *export_ansi(cucul_canvas_t const *, unsigned long int *);
  42. static void *export_utf8(cucul_canvas_t const *, unsigned long int *, int);
  43. static void *export_html(cucul_canvas_t const *, unsigned long int *);
  44. static void *export_html3(cucul_canvas_t const *, unsigned long int *);
  45. static void *export_bbfr(cucul_canvas_t const *, unsigned long int *);
  46. static void *export_irc(cucul_canvas_t const *, unsigned long int *);
  47. static void *export_ps(cucul_canvas_t const *, unsigned long int *);
  48. static void *export_svg(cucul_canvas_t const *, unsigned long int *);
  49. static void *export_tga(cucul_canvas_t const *, unsigned long int *);
  50. /** \brief Export a canvas into a foreign format.
  51. *
  52. * This function exports a libcucul 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 libcucul canvas
  72. * \param format A string describing the requested output format.
  73. * \param bytes A pointer to an unsigned long integer where the number of
  74. * allocated bytes will be written.
  75. * \return A pointer to the exported memory area, or NULL in case of error.
  76. */
  77. void *cucul_export_memory(cucul_canvas_t const *cv, char const *format,
  78. unsigned long int *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 cucul_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 * cucul_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(cucul_canvas_t const *cv, unsigned long int *bytes)
  140. {
  141. char *data, *cur;
  142. unsigned 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(cucul_canvas_t const *cv, unsigned long int *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. unsigned 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 == CUCUL_MAGIC_FULLWIDTH)
  211. continue;
  212. ansifg = cucul_attr_to_ansi_fg(attr);
  213. ansibg = cucul_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 += cucul_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(cucul_canvas_t const *cv, unsigned long int *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. unsigned 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 = cucul_attr_to_ansi_fg(lineattr[x]);
  269. uint8_t ansibg = cucul_attr_to_ansi_bg(lineattr[x]);
  270. uint8_t fg = ansifg < 0x10 ? palette[ansifg] : CUCUL_LIGHTGRAY;
  271. uint8_t bg = ansibg < 0x10 ? palette[ansibg] : CUCUL_BLACK;
  272. uint32_t ch = linechar[x];
  273. if(ch == CUCUL_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++ = cucul_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(cucul_canvas_t const *cv, unsigned long int *bytes)
  313. {
  314. char *data, *cur;
  315. unsigned 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. cucul_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(cucul_attr_to_ansi_fg(lineattr[x]) < 0x10)
  339. cur += sprintf(cur, ";color:#%.03x",
  340. cucul_attr_to_rgb12_fg(lineattr[x]));
  341. if(cucul_attr_to_ansi_bg(lineattr[x]) < 0x10)
  342. cur += sprintf(cur, ";background-color:#%.03x",
  343. cucul_attr_to_rgb12_bg(lineattr[x]));
  344. if(lineattr[x] & CUCUL_BOLD)
  345. cur += sprintf(cur, ";font-weight:bold");
  346. if(lineattr[x] & CUCUL_ITALICS)
  347. cur += sprintf(cur, ";font-style:italic");
  348. if(lineattr[x] & CUCUL_UNDERLINE)
  349. cur += sprintf(cur, ";text-decoration:underline");
  350. if(lineattr[x] & CUCUL_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] == CUCUL_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",
  363. (unsigned char)linechar[x + len]);
  364. else
  365. cur += sprintf(cur, "&#%i;",
  366. (unsigned int)linechar[x + len]);
  367. }
  368. cur += sprintf(cur, "</span>");
  369. }
  370. /* New line */
  371. cur += sprintf(cur, "<br />\n");
  372. }
  373. cur += sprintf(cur, "</div></body></html>\n");
  374. /* Crop to really used size */
  375. debug("html export: alloc %lu bytes, realloc %lu",
  376. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  377. *bytes = (uintptr_t)(cur - data);
  378. data = realloc(data, *bytes);
  379. return data;
  380. }
  381. /* Export an HTML3 document. This function is way bigger than export_html(),
  382. * but permits viewing in old browsers (or limited ones such as links). It
  383. * will not work under gecko (mozilla rendering engine) unless you set a
  384. * correct header. */
  385. static void *export_html3(cucul_canvas_t const *cv, unsigned long int *bytes)
  386. {
  387. char *data, *cur;
  388. unsigned int x, y, len;
  389. /* The HTML table markup: less than 1000 bytes
  390. * A line: 10 chars for "<tr></tr>\n"
  391. * A glyph: 40 chars for "<td bgcolor=#xxxxxx><font color=#xxxxxx>"
  392. * up to 36 chars for "<b><i><u><blink></blink></u></i></b>"
  393. * up to 9 chars for "&#xxxxxx;", far less for pure ASCII
  394. * 12 chars for "</font></td>" */
  395. *bytes = 1000 + cv->height * (10 + cv->width * (40 + 36 + 9 + 12));
  396. cur = data = malloc(*bytes);
  397. /* Table */
  398. cur += sprintf(cur, "<table cols='%d' cellpadding='0' cellspacing='0'>\n",
  399. cv->height);
  400. for(y = 0; y < cv->height; y++)
  401. {
  402. uint32_t *lineattr = cv->attrs + y * cv->width;
  403. uint32_t *linechar = cv->chars + y * cv->width;
  404. cur += sprintf(cur, "<tr>");
  405. for(x = 0; x < cv->width; x += len)
  406. {
  407. unsigned int i, needfont;
  408. /* Use colspan option to factor cells with same attributes
  409. * (see below) */
  410. len = 1;
  411. while(x + len < cv->width && lineattr[x + len] == lineattr[x])
  412. len++;
  413. cur += sprintf(cur, "<td");
  414. if(cucul_attr_to_ansi_fg(lineattr[x]) < 0x10)
  415. cur += sprintf(cur, " bgcolor=#%.06lx", (unsigned long int)
  416. _cucul_attr_to_rgb24bg(lineattr[x]));
  417. if(len > 1)
  418. cur += sprintf(cur, " colspan=%d", len);
  419. cur += sprintf(cur, ">");
  420. needfont = cucul_attr_to_ansi_bg(lineattr[x]) < 0x10;
  421. if(needfont)
  422. cur += sprintf(cur, "<font color=#%.06lx>", (unsigned long int)
  423. _cucul_attr_to_rgb24fg(lineattr[x]));
  424. if(lineattr[x] & CUCUL_BOLD)
  425. cur += sprintf(cur, "<b>");
  426. if(lineattr[x] & CUCUL_ITALICS)
  427. cur += sprintf(cur, "<i>");
  428. if(lineattr[x] & CUCUL_UNDERLINE)
  429. cur += sprintf(cur, "<u>");
  430. if(lineattr[x] & CUCUL_BLINK)
  431. cur += sprintf(cur, "<blink>");
  432. for(i = 0; i < len; i++)
  433. {
  434. if(linechar[x + i] == CUCUL_MAGIC_FULLWIDTH)
  435. ;
  436. else if(linechar[x + i] <= 0x00000020)
  437. cur += sprintf(cur, "&nbsp;");
  438. else if(linechar[x + i] < 0x00000080)
  439. cur += sprintf(cur, "%c", (unsigned char)linechar[x + i]);
  440. else
  441. cur += sprintf(cur, "&#%i;", (unsigned int)linechar[x + i]);
  442. }
  443. if(lineattr[x] & CUCUL_BLINK)
  444. cur += sprintf(cur, "</blink>");
  445. if(lineattr[x] & CUCUL_UNDERLINE)
  446. cur += sprintf(cur, "</u>");
  447. if(lineattr[x] & CUCUL_ITALICS)
  448. cur += sprintf(cur, "</i>");
  449. if(lineattr[x] & CUCUL_BOLD)
  450. cur += sprintf(cur, "</b>");
  451. if(needfont)
  452. cur += sprintf(cur, "</font>");
  453. cur += sprintf(cur, "</td>");
  454. }
  455. cur += sprintf(cur, "</tr>\n");
  456. }
  457. /* Footer */
  458. cur += sprintf(cur, "</table>\n");
  459. /* Crop to really used size */
  460. debug("html3 export: alloc %lu bytes, realloc %lu",
  461. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  462. *bytes = (uintptr_t)(cur - data);
  463. data = realloc(data, *bytes);
  464. return data;
  465. }
  466. static void *export_bbfr(cucul_canvas_t const *cv, unsigned long int *bytes)
  467. {
  468. char *data, *cur;
  469. unsigned int x, y, len;
  470. /* The font markup: less than 100 bytes
  471. * A line: 1 char for "\n"
  472. * A glyph: 22 chars for "[f=#xxxxxx][c=#xxxxxx]"
  473. * up to 21 chars for "[g][i][s][/s][/i][/g]"
  474. * up to 6 chars for the UTF-8 glyph
  475. * 8 chars for "[/c][/f]" */
  476. *bytes = 100 + cv->height * (1 + cv->width * (22 + 21 + 6 + 8));
  477. cur = data = malloc(*bytes);
  478. /* Table */
  479. cur += sprintf(cur, "[font=Courier New]");
  480. for(y = 0; y < cv->height; y++)
  481. {
  482. uint32_t *lineattr = cv->attrs + y * cv->width;
  483. uint32_t *linechar = cv->chars + y * cv->width;
  484. for(x = 0; x < cv->width; x += len)
  485. {
  486. unsigned int i, needback, needfront;
  487. /* Use colspan option to factor cells with same attributes
  488. * (see below) */
  489. len = 1;
  490. if(linechar[x] == ' ')
  491. while(x + len < cv->width && lineattr[x + len] == lineattr[x]
  492. && linechar[x] == ' ')
  493. len++;
  494. else
  495. while(x + len < cv->width && lineattr[x + len] == lineattr[x]
  496. && linechar[x] != ' ')
  497. len++;
  498. needback = cucul_attr_to_ansi_bg(lineattr[x]) < 0x10;
  499. needfront = cucul_attr_to_ansi_fg(lineattr[x]) < 0x10;
  500. if(needback)
  501. cur += sprintf(cur, "[f=#%.06lx]", (unsigned long int)
  502. _cucul_attr_to_rgb24bg(lineattr[x]));
  503. if(linechar[x] == ' ')
  504. cur += sprintf(cur, "[c=#%.06lx]", (unsigned long int)
  505. _cucul_attr_to_rgb24bg(lineattr[x]));
  506. else if(needfront)
  507. cur += sprintf(cur, "[c=#%.06lx]", (unsigned long int)
  508. _cucul_attr_to_rgb24fg(lineattr[x]));
  509. if(lineattr[x] & CUCUL_BOLD)
  510. cur += sprintf(cur, "[g]");
  511. if(lineattr[x] & CUCUL_ITALICS)
  512. cur += sprintf(cur, "[i]");
  513. if(lineattr[x] & CUCUL_UNDERLINE)
  514. cur += sprintf(cur, "[s]");
  515. if(lineattr[x] & CUCUL_BLINK)
  516. ; /* FIXME */
  517. for(i = 0; i < len; i++)
  518. {
  519. if(linechar[x + i] == CUCUL_MAGIC_FULLWIDTH)
  520. ;
  521. else if(linechar[x + i] == ' ')
  522. *cur++ = '_';
  523. else
  524. cur += cucul_utf32_to_utf8(cur, linechar[x + i]);
  525. }
  526. if(lineattr[x] & CUCUL_BLINK)
  527. ; /* FIXME */
  528. if(lineattr[x] & CUCUL_UNDERLINE)
  529. cur += sprintf(cur, "[/s]");
  530. if(lineattr[x] & CUCUL_ITALICS)
  531. cur += sprintf(cur, "[/i]");
  532. if(lineattr[x] & CUCUL_BOLD)
  533. cur += sprintf(cur, "[/g]");
  534. if(linechar[x] == ' ' || needfront)
  535. cur += sprintf(cur, "[/c]");
  536. if(needback)
  537. cur += sprintf(cur, "[/f]");
  538. }
  539. cur += sprintf(cur, "\n");
  540. }
  541. /* Footer */
  542. cur += sprintf(cur, "[/font]\n");
  543. /* Crop to really used size */
  544. debug("bbfr export: alloc %lu bytes, realloc %lu",
  545. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  546. *bytes = (uintptr_t)(cur - data);
  547. data = realloc(data, *bytes);
  548. return data;
  549. }
  550. /* Export a text file with IRC colours */
  551. static void *export_irc(cucul_canvas_t const *cv, unsigned long int *bytes)
  552. {
  553. static uint8_t const palette[] =
  554. {
  555. 1, 2, 3, 10, 5, 6, 7, 15, /* Dark */
  556. 14, 12, 9, 11, 4, 13, 8, 0, /* Light */
  557. };
  558. char *data, *cur;
  559. unsigned int x, y;
  560. /* 14 bytes assumed for max length per pixel. Worst case scenario:
  561. * ^Cxx,yy 6 bytes
  562. * ^B^B 2 bytes
  563. * ch 6 bytes
  564. * 3 bytes for max length per line. Worst case scenario:
  565. * <spc> 1 byte (for empty lines)
  566. * \r\n 2 bytes
  567. * In real life, the average bytes per pixel value will be around 5.
  568. */
  569. *bytes = 2 + cv->height * (3 + cv->width * 14);
  570. cur = data = malloc(*bytes);
  571. for(y = 0; y < cv->height; y++)
  572. {
  573. uint32_t *lineattr = cv->attrs + y * cv->width;
  574. uint32_t *linechar = cv->chars + y * cv->width;
  575. uint8_t prevfg = 0x10;
  576. uint8_t prevbg = 0x10;
  577. for(x = 0; x < cv->width; x++)
  578. {
  579. uint32_t attr = lineattr[x];
  580. uint32_t ch = linechar[x];
  581. uint8_t ansifg, ansibg, fg, bg;
  582. if(ch == CUCUL_MAGIC_FULLWIDTH)
  583. continue;
  584. ansifg = cucul_attr_to_ansi_fg(attr);
  585. ansibg = cucul_attr_to_ansi_bg(attr);
  586. fg = ansifg < 0x10 ? palette[ansifg] : 0x10;
  587. bg = ansibg < 0x10 ? palette[ansibg] : 0x10;
  588. /* TODO: optimise series of same fg / same bg
  589. * don't change fg value if ch == ' '
  590. * make sure the \x03,%d trick works everywhere */
  591. if(bg != prevbg || fg != prevfg)
  592. {
  593. int need_escape = 0;
  594. if(bg == 0x10)
  595. {
  596. if(fg == 0x10)
  597. cur += sprintf(cur, "\x0f");
  598. else
  599. {
  600. if(prevbg == 0x10)
  601. cur += sprintf(cur, "\x03%d", fg);
  602. else
  603. cur += sprintf(cur, "\x0f\x03%d", fg);
  604. if(ch == (uint32_t)',')
  605. need_escape = 1;
  606. }
  607. }
  608. else
  609. {
  610. if(fg == 0x10)
  611. cur += sprintf(cur, "\x0f\x03,%d", bg);
  612. else
  613. cur += sprintf(cur, "\x03%d,%d", fg, bg);
  614. }
  615. if(ch >= (uint32_t)'0' && ch <= (uint32_t)'9')
  616. need_escape = 1;
  617. if(need_escape)
  618. cur += sprintf(cur, "\x02\x02");
  619. }
  620. cur += cucul_utf32_to_utf8(cur, ch);
  621. prevfg = fg;
  622. prevbg = bg;
  623. }
  624. /* TODO: do the same the day we optimise whole lines above */
  625. if(!cv->width)
  626. *cur++ = ' ';
  627. *cur++ = '\r';
  628. *cur++ = '\n';
  629. }
  630. /* Crop to really used size */
  631. debug("IRC export: alloc %lu bytes, realloc %lu",
  632. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  633. *bytes = (uintptr_t)(cur - data);
  634. data = realloc(data, *bytes);
  635. return data;
  636. }
  637. /* Export a PostScript document. */
  638. static void *export_ps(cucul_canvas_t const *cv, unsigned long int *bytes)
  639. {
  640. static char const *ps_header =
  641. "%!\n"
  642. "%% libcaca PDF export\n"
  643. "%%LanguageLevel: 2\n"
  644. "%%Pages: 1\n"
  645. "%%DocumentData: Clean7Bit\n"
  646. "/csquare {\n"
  647. " newpath\n"
  648. " 0 0 moveto\n"
  649. " 0 1 rlineto\n"
  650. " 1 0 rlineto\n"
  651. " 0 -1 rlineto\n"
  652. " closepath\n"
  653. " setrgbcolor\n"
  654. " fill\n"
  655. "} def\n"
  656. "/S {\n"
  657. " Show\n"
  658. "} bind def\n"
  659. "/Courier-Bold findfont\n"
  660. "8 scalefont\n"
  661. "setfont\n"
  662. "gsave\n"
  663. "6 10 scale\n";
  664. char *data, *cur;
  665. unsigned int x, y;
  666. /* 200 is arbitrary but should be ok */
  667. *bytes = strlen(ps_header) + 100 + cv->height * (32 + cv->width * 200);
  668. cur = data = malloc(*bytes);
  669. /* Header */
  670. cur += sprintf(cur, "%s", ps_header);
  671. cur += sprintf(cur, "0 %d translate\n", cv->height);
  672. /* Background, drawn using csquare macro defined in header */
  673. for(y = cv->height; y--; )
  674. {
  675. uint32_t *lineattr = cv->attrs + y * cv->width;
  676. for(x = 0; x < cv->width; x++)
  677. {
  678. uint8_t argb[8];
  679. cucul_attr_to_argb64(*lineattr++, argb);
  680. cur += sprintf(cur, "1 0 translate\n %f %f %f csquare\n",
  681. (float)argb[1] * (1.0 / 0xf),
  682. (float)argb[2] * (1.0 / 0xf),
  683. (float)argb[3] * (1.0 / 0xf));
  684. }
  685. /* Return to beginning of the line, and jump to the next one */
  686. cur += sprintf(cur, "-%d 1 translate\n", cv->width);
  687. }
  688. cur += sprintf(cur, "grestore\n"); /* Restore transformation matrix */
  689. cur += sprintf(cur, "0 %d translate\n", cv->height*10);
  690. for(y = cv->height; y--; )
  691. {
  692. uint32_t *lineattr = cv->attrs + (cv->height - y - 1) * cv->width;
  693. uint32_t *linechar = cv->chars + (cv->height - y - 1) * cv->width;
  694. for(x = 0; x < cv->width; x++)
  695. {
  696. uint8_t argb[8];
  697. uint32_t ch = *linechar++;
  698. cucul_attr_to_argb64(*lineattr++, argb);
  699. cur += sprintf(cur, "newpath\n");
  700. cur += sprintf(cur, "%d %d moveto\n", (x + 1) * 6, y * 10 + 2);
  701. cur += sprintf(cur, "%f %f %f setrgbcolor\n",
  702. (float)argb[5] * (1.0 / 0xf),
  703. (float)argb[6] * (1.0 / 0xf),
  704. (float)argb[7] * (1.0 / 0xf));
  705. if(ch < 0x00000020)
  706. cur += sprintf(cur, "(?) show\n");
  707. else if(ch >= 0x00000080)
  708. cur += sprintf(cur, "(?) show\n");
  709. else switch((uint8_t)(ch & 0x7f))
  710. {
  711. case '\\':
  712. case '(':
  713. case ')':
  714. cur += sprintf(cur, "(\\%c) show\n", (unsigned char)ch);
  715. break;
  716. default:
  717. cur += sprintf(cur, "(%c) show\n", (unsigned char)ch);
  718. break;
  719. }
  720. }
  721. }
  722. cur += sprintf(cur, "showpage\n");
  723. /* Crop to really used size */
  724. debug("PS export: alloc %lu bytes, realloc %lu",
  725. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  726. *bytes = (uintptr_t)(cur - data);
  727. data = realloc(data, *bytes);
  728. return data;
  729. }
  730. /* Export an SVG vector image */
  731. static void *export_svg(cucul_canvas_t const *cv, unsigned long int *bytes)
  732. {
  733. static char const svg_header[] =
  734. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  735. "<svg width=\"%d\" height=\"%d\" viewBox=\"0 0 %d %d\""
  736. " xmlns=\"http://www.w3.org/2000/svg\""
  737. " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
  738. " xml:space=\"preserve\" version=\"1.1\" baseProfile=\"full\">\n";
  739. char *data, *cur;
  740. unsigned int x, y;
  741. /* 200 is arbitrary but should be ok */
  742. *bytes = strlen(svg_header) + 128 + cv->width * cv->height * 200;
  743. cur = data = malloc(*bytes);
  744. /* Header */
  745. cur += sprintf(cur, svg_header, cv->width * 6, cv->height * 10,
  746. cv->width * 6, cv->height * 10);
  747. cur += sprintf(cur, " <g id=\"mainlayer\" font-size=\"10\""
  748. " style=\"font-family: monospace\">\n");
  749. /* Background */
  750. for(y = 0; y < cv->height; y++)
  751. {
  752. uint32_t *lineattr = cv->attrs + y * cv->width;
  753. for(x = 0; x < cv->width; x++)
  754. {
  755. cur += sprintf(cur, "<rect style=\"fill:#%.03x\" x=\"%d\" y=\"%d\""
  756. " width=\"6\" height=\"10\"/>\n",
  757. cucul_attr_to_rgb12_bg(*lineattr++),
  758. x * 6, y * 10);
  759. }
  760. }
  761. /* Text */
  762. for(y = 0; y < cv->height; y++)
  763. {
  764. uint32_t *lineattr = cv->attrs + y * cv->width;
  765. uint32_t *linechar = cv->chars + y * cv->width;
  766. for(x = 0; x < cv->width; x++)
  767. {
  768. uint32_t ch = *linechar++;
  769. if(ch == ' ' || ch == CUCUL_MAGIC_FULLWIDTH)
  770. {
  771. lineattr++;
  772. continue;
  773. }
  774. cur += sprintf(cur, "<text style=\"fill:#%.03x\" "
  775. "x=\"%d\" y=\"%d\">",
  776. cucul_attr_to_rgb12_fg(*lineattr++),
  777. x * 6, (y * 10) + 8);
  778. if(ch < 0x00000020)
  779. *cur++ = '?';
  780. else if(ch > 0x0000007f)
  781. cur += cucul_utf32_to_utf8(cur, ch);
  782. else switch((uint8_t)ch)
  783. {
  784. case '>': cur += sprintf(cur, "&gt;"); break;
  785. case '<': cur += sprintf(cur, "&lt;"); break;
  786. case '&': cur += sprintf(cur, "&amp;"); break;
  787. default: *cur++ = ch; break;
  788. }
  789. cur += sprintf(cur, "</text>\n");
  790. }
  791. }
  792. cur += sprintf(cur, " </g>\n");
  793. cur += sprintf(cur, "</svg>\n");
  794. /* Crop to really used size */
  795. debug("SVG export: alloc %lu bytes, realloc %lu",
  796. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  797. *bytes = (uintptr_t)(cur - data);
  798. data = realloc(data, *bytes);
  799. return data;
  800. }
  801. /* Export a TGA image */
  802. static void *export_tga(cucul_canvas_t const *cv, unsigned long int *bytes)
  803. {
  804. char const * const *fontlist;
  805. char *data, *cur;
  806. cucul_font_t *f;
  807. unsigned int i, w, h;
  808. fontlist = cucul_get_font_list();
  809. if(!fontlist[0])
  810. {
  811. seterrno(EINVAL);
  812. return NULL;
  813. }
  814. f = cucul_load_font(fontlist[0], 0);
  815. w = cucul_get_canvas_width(cv) * cucul_get_font_width(f);
  816. h = cucul_get_canvas_height(cv) * cucul_get_font_height(f);
  817. *bytes = w * h * 4 + 18; /* 32 bpp + 18 bytes for the header */
  818. cur = data = malloc(*bytes);
  819. /* ID Length */
  820. cur += sprintf(cur, "%c", 0);
  821. /* Color Map Type: no colormap */
  822. cur += sprintf(cur, "%c", 0);
  823. /* Image Type: uncompressed truecolor */
  824. cur += sprintf(cur, "%c", 2);
  825. /* Color Map Specification: no color map */
  826. memset(cur, 0, 5); cur += 5;
  827. /* Image Specification */
  828. cur += sprintf(cur, "%c%c", 0, 0); /* X Origin */
  829. cur += sprintf(cur, "%c%c", 0, 0); /* Y Origin */
  830. cur += sprintf(cur, "%c%c", w & 0xff, w >> 8); /* Width */
  831. cur += sprintf(cur, "%c%c", h & 0xff, h >> 8); /* Height */
  832. cur += sprintf(cur, "%c", 32); /* Pixel Depth */
  833. cur += sprintf(cur, "%c", 40); /* Image Descriptor */
  834. /* Image ID: no ID */
  835. /* Color Map Data: no colormap */
  836. /* Image Data */
  837. cucul_render_canvas(cv, f, cur, w, h, 4 * w);
  838. /* Swap bytes. What a waste of time. */
  839. for(i = 0; i < w * h * 4; i += 4)
  840. {
  841. char c;
  842. c = cur[i]; cur[i] = cur[i + 3]; cur[i + 3] = c;
  843. c = cur[i + 1]; cur[i + 1] = cur[i + 2]; cur[i + 2] = c;
  844. }
  845. cucul_free_font(f);
  846. return data;
  847. }