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.
 
 
 
 
 
 

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