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.
 
 
 
 
 
 

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