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.
 
 
 
 
 
 

812 lines
25 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 void export_caca(cucul_canvas_t *, cucul_buffer_t *);
  30. static void export_ansi(cucul_canvas_t *, cucul_buffer_t *);
  31. static void export_utf8(cucul_canvas_t *, cucul_buffer_t *);
  32. static void export_html(cucul_canvas_t *, cucul_buffer_t *);
  33. static void export_html3(cucul_canvas_t *, cucul_buffer_t *);
  34. static void export_irc(cucul_canvas_t *, cucul_buffer_t *);
  35. static void export_ps(cucul_canvas_t *, cucul_buffer_t *);
  36. static void export_svg(cucul_canvas_t *, cucul_buffer_t *);
  37. static void export_tga(cucul_canvas_t *, cucul_buffer_t *);
  38. /** \brief Export a canvas into a foreign format.
  39. *
  40. * This function exports a libcucul canvas into various foreign formats such
  41. * as ANSI art, HTML, IRC colours, etc. One should use cucul_get_buffer_data()
  42. * and cucul_get_buffer_size() to access the buffer contents. The allocated
  43. * data is valid until cucul_free_buffer() is called.
  44. *
  45. * Valid values for \c format are:
  46. * - \c "caca": export native libcaca files.
  47. * - \c "ansi": export ANSI art (CP437 charset with ANSI colour codes).
  48. * - \c "html": export an HTML page with CSS information.
  49. * - \c "html3": export an HTML table that should be compatible with
  50. * most navigators, including textmode ones.
  51. * - \c "irc": export UTF-8 text with mIRC colour codes.
  52. * - \c "ps": export a PostScript document.
  53. * - \c "svg": export an SVG vector image.
  54. * - \c "tga": export a TGA image.
  55. *
  56. * If an error occurs, NULL is returned and \b errno is set accordingly:
  57. * - \c EINVAL Invalid format requested.
  58. * - \c ENOMEM Not enough memory to allocate output buffer.
  59. *
  60. * \param cv A libcucul canvas
  61. * \param format A string describing the requested output format.
  62. * \return A libcucul buffer, or NULL in case of error.
  63. */
  64. cucul_buffer_t * cucul_export_canvas(cucul_canvas_t *cv, char const *format)
  65. {
  66. cucul_buffer_t *ex;
  67. ex = malloc(sizeof(cucul_buffer_t));
  68. if(!ex)
  69. {
  70. #if defined(HAVE_ERRNO_H)
  71. errno = ENOMEM;
  72. #endif
  73. return NULL;
  74. }
  75. ex->size = 0;
  76. ex->data = NULL;
  77. ex->user_data = 0;
  78. if(!strcasecmp("caca", format))
  79. export_caca(cv, ex);
  80. else if(!strcasecmp("ansi", format))
  81. export_ansi(cv, ex);
  82. else if(!strcasecmp("utf8", format))
  83. export_utf8(cv, ex);
  84. else if(!strcasecmp("html", format))
  85. export_html(cv, ex);
  86. else if(!strcasecmp("html3", format))
  87. export_html3(cv, ex);
  88. else if(!strcasecmp("irc", format))
  89. export_irc(cv, ex);
  90. else if(!strcasecmp("ps", format))
  91. export_ps(cv, ex);
  92. else if(!strcasecmp("svg", format))
  93. export_svg(cv, ex);
  94. else if(!strcasecmp("tga", format))
  95. export_tga(cv, ex);
  96. if(ex->size == 0)
  97. {
  98. free(ex);
  99. #if defined(HAVE_ERRNO_H)
  100. errno = EINVAL;
  101. #endif
  102. return NULL;
  103. }
  104. return ex;
  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_canvas(), 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. "html", "HTML",
  125. "html3", "backwards-compatible HTML",
  126. "irc", "IRC with mIRC colours",
  127. "ps", "PostScript document",
  128. "svg", "SVG vector image",
  129. "tga", "TGA image",
  130. NULL, NULL
  131. };
  132. return list;
  133. }
  134. /*
  135. * XXX: the following functions are local.
  136. */
  137. /* Generate a native libcaca canvas file. */
  138. static void export_caca(cucul_canvas_t *cv, cucul_buffer_t *ex)
  139. {
  140. uint32_t *attr = cv->attr;
  141. uint32_t *chars = cv->chars;
  142. char *cur;
  143. uint32_t w, h;
  144. unsigned int n;
  145. /* 16 bytes for the canvas, 8 bytes for each character cell. */
  146. ex->size = 16 + 8 * cv->width * cv->height;
  147. ex->data = malloc(ex->size);
  148. cur = ex->data;
  149. w = cv->width;
  150. h = cv->height;
  151. cur += sprintf(cur, "CACACANV%c%c%c%c%c%c%c%c",
  152. (unsigned char)(w >> 24), (unsigned char)((w >> 16) & 0xff),
  153. (unsigned char)((w >> 8) & 0xff), (unsigned char)(w & 0xff),
  154. (unsigned char)(h >> 24), (unsigned char)((h >> 16) & 0xff),
  155. (unsigned char)((h >> 8) & 0xff), (unsigned char)(h & 0xff));
  156. for(n = cv->height * cv->width; n--; )
  157. {
  158. uint32_t ch = *chars++;
  159. uint32_t a = *attr++;
  160. *cur++ = ch >> 24;
  161. *cur++ = (ch >> 16) & 0xff;
  162. *cur++ = (ch >> 8) & 0xff;
  163. *cur++ = ch & 0xff;
  164. *cur++ = a >> 24;
  165. *cur++ = (a >> 16) & 0xff;
  166. *cur++ = (a >> 8) & 0xff;
  167. *cur++ = a & 0xff;
  168. }
  169. }
  170. /* Generate UTF-8 representation of current canvas. */
  171. static void export_utf8(cucul_canvas_t *cv, cucul_buffer_t *ex)
  172. {
  173. static uint8_t const palette[] =
  174. {
  175. 0, 4, 2, 6, 1, 5, 3, 7,
  176. 8, 12, 10, 14, 9, 13, 11, 15
  177. };
  178. char *cur;
  179. unsigned int x, y;
  180. /* 23 bytes assumed for max length per pixel ('\e[5;1;3x;4y;9x;10ym' plus
  181. * 4 max bytes for a UTF-8 character).
  182. * Add height*9 to that (zeroes color at the end and jump to next line) */
  183. ex->size = (cv->height * 9) + (cv->width * cv->height * 23);
  184. ex->data = malloc(ex->size);
  185. cur = ex->data;
  186. for(y = 0; y < cv->height; y++)
  187. {
  188. uint32_t *lineattr = cv->attr + y * cv->width;
  189. uint32_t *linechar = cv->chars + y * cv->width;
  190. uint8_t prevfg = -1;
  191. uint8_t prevbg = -1;
  192. for(x = 0; x < cv->width; x++)
  193. {
  194. uint8_t fg = palette[_cucul_argb32_to_ansi4fg(lineattr[x])];
  195. uint8_t bg = palette[_cucul_argb32_to_ansi4bg(lineattr[x])];
  196. uint32_t ch = linechar[x];
  197. if(fg != prevfg || bg != prevbg)
  198. {
  199. cur += sprintf(cur, "\033[0;");
  200. if(fg < 8)
  201. if(bg < 8)
  202. cur += sprintf(cur, "3%d;4%dm", fg, bg);
  203. else
  204. cur += sprintf(cur, "5;3%d;4%d;10%dm",
  205. fg, bg - 8, bg - 8);
  206. else
  207. if(bg < 8)
  208. cur += sprintf(cur, "1;3%d;4%d;9%dm",
  209. fg - 8, bg, fg - 8);
  210. else
  211. cur += sprintf(cur, "5;1;3%d;4%d;9%d;10%dm",
  212. fg - 8, bg - 8, fg - 8, bg - 8);
  213. }
  214. cur += cucul_utf32_to_utf8(cur, ch);
  215. prevfg = fg;
  216. prevbg = bg;
  217. }
  218. cur += sprintf(cur, "\033[0m\r\n");
  219. }
  220. /* Crop to really used size */
  221. ex->size = (uintptr_t)(cur - ex->data);
  222. ex->data = realloc(ex->data, ex->size);
  223. }
  224. /* Generate ANSI representation of current canvas. */
  225. static void export_ansi(cucul_canvas_t *cv, cucul_buffer_t *ex)
  226. {
  227. static uint8_t const palette[] =
  228. {
  229. 0, 4, 2, 6, 1, 5, 3, 7,
  230. 8, 12, 10, 14, 9, 13, 11, 15
  231. };
  232. char *cur;
  233. unsigned int x, y;
  234. uint8_t prevfg = -1;
  235. uint8_t prevbg = -1;
  236. /* 16 bytes assumed for max length per pixel ('\e[5;1;3x;4ym' plus
  237. * 1 byte for a CP437 character).
  238. * Add height*9 to that (zeroes color at the end and jump to next line) */
  239. ex->size = (cv->height * 9) + (cv->width * cv->height * 16);
  240. ex->data = malloc(ex->size);
  241. cur = ex->data;
  242. for(y = 0; y < cv->height; y++)
  243. {
  244. uint32_t *lineattr = cv->attr + y * cv->width;
  245. uint32_t *linechar = cv->chars + y * cv->width;
  246. for(x = 0; x < cv->width; x++)
  247. {
  248. uint8_t fg = palette[_cucul_argb32_to_ansi4fg(lineattr[x])];
  249. uint8_t bg = palette[_cucul_argb32_to_ansi4bg(lineattr[x])];
  250. uint32_t ch = linechar[x];
  251. if(fg != prevfg || bg != prevbg)
  252. {
  253. cur += sprintf(cur, "\033[0;");
  254. if(fg < 8)
  255. if(bg < 8)
  256. cur += sprintf(cur, "3%d;4%dm", fg, bg);
  257. else
  258. cur += sprintf(cur, "5;3%d;4%dm", fg, bg - 8);
  259. else
  260. if(bg < 8)
  261. cur += sprintf(cur, "1;3%d;4%dm", fg - 8, bg);
  262. else
  263. cur += sprintf(cur, "5;1;3%d;4%dm", fg - 8, bg - 8);
  264. }
  265. *cur++ = cucul_utf32_to_cp437(ch);
  266. prevfg = fg;
  267. prevbg = bg;
  268. }
  269. if(cv->width == 80)
  270. {
  271. cur += sprintf(cur, "\033[s\n\033[u");
  272. }
  273. else
  274. {
  275. cur += sprintf(cur, "\033[0m\r\n");
  276. prevfg = -1;
  277. prevbg = -1;
  278. }
  279. }
  280. /* Crop to really used size */
  281. ex->size = (uintptr_t)(cur - ex->data);
  282. ex->data = realloc(ex->data, ex->size);
  283. }
  284. /* Generate HTML representation of current canvas. */
  285. static void export_html(cucul_canvas_t *cv, cucul_buffer_t *ex)
  286. {
  287. char *cur;
  288. unsigned int x, y, len;
  289. /* The HTML header: less than 1000 bytes
  290. * A line: 7 chars for "<br />\n"
  291. * A glyph: 47 chars for "<span style="color:#xxx;background-color:#xxx">"
  292. * up to 9 chars for "&#xxxxxx;", far less for pure ASCII
  293. * 7 chars for "</span>" */
  294. ex->size = 1000 + cv->height * (7 + cv->width * (47 + 9 + 7));
  295. ex->data = malloc(ex->size);
  296. cur = ex->data;
  297. /* HTML header */
  298. cur += sprintf(cur, "<html><head>\n");
  299. cur += sprintf(cur, "<title>Generated by libcaca %s</title>\n", VERSION);
  300. cur += sprintf(cur, "</head><body>\n");
  301. cur += sprintf(cur, "<div cellpadding='0' cellspacing='0' style='%s'>\n",
  302. "font-family: monospace, fixed; font-weight: bold;");
  303. for(y = 0; y < cv->height; y++)
  304. {
  305. uint32_t *lineattr = cv->attr + y * cv->width;
  306. uint32_t *linechar = cv->chars + y * cv->width;
  307. for(x = 0; x < cv->width; x += len)
  308. {
  309. cur += sprintf(cur, "<span style=\"color:#%.03x;"
  310. "background-color:#%.03x\">",
  311. _cucul_argb32_to_rgb12fg(lineattr[x]),
  312. _cucul_argb32_to_rgb12bg(lineattr[x]));
  313. for(len = 0;
  314. x + len < cv->width && lineattr[x + len] == lineattr[x];
  315. len++)
  316. {
  317. if(linechar[x + len] <= 0x00000020)
  318. cur += sprintf(cur, "&nbsp;");
  319. else if(linechar[x + len] < 0x00000080)
  320. cur += sprintf(cur, "%c",
  321. (unsigned char)linechar[x + len]);
  322. else
  323. cur += sprintf(cur, "&#%i;",
  324. (unsigned int)linechar[x + len]);
  325. }
  326. cur += sprintf(cur, "</span>");
  327. }
  328. /* New line */
  329. cur += sprintf(cur, "<br />\n");
  330. }
  331. cur += sprintf(cur, "</div></body></html>\n");
  332. /* Crop to really used size */
  333. ex->size = strlen(ex->data) + 1;
  334. ex->data = realloc(ex->data, ex->size);
  335. }
  336. /* Export an HTML3 document. This function is way bigger than export_html(),
  337. * but permits viewing in old browsers (or limited ones such as links). It
  338. * will not work under gecko (mozilla rendering engine) unless you set a
  339. * correct header. */
  340. static void export_html3(cucul_canvas_t *cv, cucul_buffer_t *ex)
  341. {
  342. char *cur;
  343. unsigned int x, y, len;
  344. /* The HTML table markup: less than 1000 bytes
  345. * A line: 10 chars for "<tr></tr>\n"
  346. * A glyph: 40 chars for "<td bgcolor=#xxxxxx><font color=#xxxxxx>"
  347. * up to 9 chars for "&#xxxxxx;", far less for pure ASCII
  348. * 12 chars for "</font></td>" */
  349. ex->size = 1000 + cv->height * (10 + cv->width * (40 + 9 + 12));
  350. ex->data = malloc(ex->size);
  351. cur = ex->data;
  352. /* Table */
  353. cur += sprintf(cur, "<table cols='%d' cellpadding='0' cellspacing='0'>\n",
  354. cv->height);
  355. for(y = 0; y < cv->height; y++)
  356. {
  357. uint32_t *lineattr = cv->attr + y * cv->width;
  358. uint32_t *linechar = cv->chars + y * cv->width;
  359. cur += sprintf(cur, "<tr>");
  360. for(x = 0; x < cv->width; x += len)
  361. {
  362. unsigned int i;
  363. /* Use colspan option to factor cells with same attributes
  364. * (see below) */
  365. len = 1;
  366. while(x + len < cv->width && lineattr[x + len] == lineattr[x])
  367. len++;
  368. cur += sprintf(cur, "<td bgcolor=#%.06lx", (unsigned long int)
  369. _cucul_argb32_to_rgb24bg(lineattr[x]));
  370. if(len > 1)
  371. cur += sprintf(cur, " colspan=%d", len);
  372. cur += sprintf(cur, "><font color=#%.06lx>", (unsigned long int)
  373. _cucul_argb32_to_rgb24fg(lineattr[x]));
  374. for(i = 0; i < len; i++)
  375. {
  376. if(linechar[x + i] <= 0x00000020)
  377. cur += sprintf(cur, "&nbsp;");
  378. else if(linechar[x + i] < 0x00000080)
  379. cur += sprintf(cur, "%c", (unsigned char)linechar[x + i]);
  380. else
  381. cur += sprintf(cur, "&#%i;", (unsigned int)linechar[x + i]);
  382. }
  383. cur += sprintf(cur, "</font></td>");
  384. }
  385. cur += sprintf(cur, "</tr>\n");
  386. }
  387. /* Footer */
  388. cur += sprintf(cur, "</table>\n");
  389. /* Crop to really used size */
  390. ex->size = (uintptr_t)(cur - ex->data);
  391. ex->data = realloc(ex->data, ex->size);
  392. }
  393. /* Export a text file with IRC colours */
  394. static void export_irc(cucul_canvas_t *cv, cucul_buffer_t *ex)
  395. {
  396. static uint8_t const palette[] =
  397. {
  398. 1, 2, 3, 10, 5, 6, 7, 15, /* Dark */
  399. 14, 12, 9, 11, 4, 13, 8, 0, /* Light */
  400. };
  401. char *cur;
  402. unsigned int x, y;
  403. /* 16 bytes assumed for max length per pixel. Worst case scenario:
  404. * ^Cxx,yy 6 bytes
  405. * ^B^B 2 bytes
  406. * ch 6 bytes
  407. * \r\n 2 bytes
  408. * In real life, the average bytes per pixel value will be around 5.
  409. */
  410. ex->size = 2 + (cv->width * cv->height * 16);
  411. ex->data = malloc(ex->size);
  412. cur = ex->data;
  413. for(y = 0; y < cv->height; y++)
  414. {
  415. uint32_t *lineattr = cv->attr + y * cv->width;
  416. uint32_t *linechar = cv->chars + y * cv->width;
  417. uint8_t prevfg = 0x10;
  418. uint8_t prevbg = 0x10;
  419. for(x = 0; x < cv->width; x++)
  420. {
  421. uint32_t attr = lineattr[x];
  422. uint8_t fg = palette[_cucul_argb32_to_ansi4fg(attr)];
  423. uint8_t bg = palette[_cucul_argb32_to_ansi4bg(attr)];
  424. uint32_t ch = linechar[x];
  425. if((attr & 0xffff) == CUCUL_COLOR_DEFAULT)
  426. fg = 0x10;
  427. if((attr >> 16) == CUCUL_COLOR_TRANSPARENT)
  428. bg = 0x10;
  429. #if 0
  430. if(bg == prevbg)
  431. {
  432. if(fg == prevfg)
  433. ; /* Same fg/bg, do nothing */
  434. else if(ch == (uint32_t)' ')
  435. fg = prevfg; /* Hackety hack */
  436. else
  437. {
  438. cur += sprintf(cur, "\x03%d", fg);
  439. if(ch >= (uint32_t)'0' && ch <= (uint32_t)'9')
  440. cur += sprintf(cur, "\x02\x02");
  441. }
  442. }
  443. else
  444. {
  445. if(fg == prevfg)
  446. cur += sprintf(cur, "\x03,%d", bg);
  447. else
  448. cur += sprintf(cur, "\x03%d,%d", fg, bg);
  449. if(ch >= (uint32_t)'0' && ch <= (uint32_t)'9')
  450. cur += sprintf(cur, "\x02\x02");
  451. }
  452. #else
  453. if(bg != prevbg || fg != prevfg)
  454. {
  455. if(bg == 0x10)
  456. {
  457. if(fg == 0x10)
  458. cur += sprintf(cur, "\x0f");
  459. else if(prevbg == 0x10)
  460. cur += sprintf(cur, "\x03%d", fg);
  461. else
  462. cur += sprintf(cur, "\x0f\x03%d", fg);
  463. }
  464. else
  465. {
  466. if(fg == 0x10)
  467. cur += sprintf(cur, "\x0f\x03,%d", bg);
  468. else
  469. cur += sprintf(cur, "\x03%d,%d", fg, bg);
  470. }
  471. if(ch >= (uint32_t)'0' && ch <= (uint32_t)'9')
  472. cur += sprintf(cur, "\x02\x02");
  473. }
  474. #endif
  475. cur += cucul_utf32_to_utf8(cur, ch);
  476. prevfg = fg;
  477. prevbg = bg;
  478. }
  479. *cur++ = '\r';
  480. *cur++ = '\n';
  481. }
  482. /* Crop to really used size */
  483. ex->size = (uintptr_t)(cur - ex->data);
  484. ex->data = realloc(ex->data, ex->size);
  485. }
  486. /* Export a PostScript document. */
  487. static void export_ps(cucul_canvas_t *cv, cucul_buffer_t *ex)
  488. {
  489. static char const *ps_header =
  490. "%!\n"
  491. "%% libcaca PDF export\n"
  492. "%%LanguageLevel: 2\n"
  493. "%%Pages: 1\n"
  494. "%%DocumentData: Clean7Bit\n"
  495. "/csquare {\n"
  496. " newpath\n"
  497. " 0 0 moveto\n"
  498. " 0 1 rlineto\n"
  499. " 1 0 rlineto\n"
  500. " 0 -1 rlineto\n"
  501. " closepath\n"
  502. " setrgbcolor\n"
  503. " fill\n"
  504. "} def\n"
  505. "/S {\n"
  506. " Show\n"
  507. "} bind def\n"
  508. "/Courier-Bold findfont\n"
  509. "8 scalefont\n"
  510. "setfont\n"
  511. "gsave\n"
  512. "6 10 scale\n";
  513. char *cur;
  514. unsigned int x, y;
  515. /* 200 is arbitrary but should be ok */
  516. ex->size = strlen(ps_header) + (cv->width * cv->height * 200);
  517. ex->data = malloc(ex->size);
  518. cur = ex->data;
  519. /* Header */
  520. cur += sprintf(cur, "%s", ps_header);
  521. cur += sprintf(cur, "0 %d translate\n", cv->height);
  522. /* Background, drawn using csquare macro defined in header */
  523. for(y = cv->height; y--; )
  524. {
  525. uint32_t *lineattr = cv->attr + y * cv->width;
  526. for(x = 0; x < cv->width; x++)
  527. {
  528. uint8_t argb[8];
  529. _cucul_argb32_to_argb4(*lineattr++, argb);
  530. cur += sprintf(cur, "1 0 translate\n %f %f %f csquare\n",
  531. (float)argb[1] * (1.0 / 0xf),
  532. (float)argb[2] * (1.0 / 0xf),
  533. (float)argb[3] * (1.0 / 0xf));
  534. }
  535. /* Return to beginning of the line, and jump to the next one */
  536. cur += sprintf(cur, "-%d 1 translate\n", cv->width);
  537. }
  538. cur += sprintf(cur, "grestore\n"); /* Restore transformation matrix */
  539. cur += sprintf(cur, "0 %d translate\n", cv->height*10);
  540. for(y = cv->height; y--; )
  541. {
  542. uint32_t *lineattr = cv->attr + (cv->height - y - 1) * cv->width;
  543. uint32_t *linechar = cv->chars + (cv->height - y - 1) * cv->width;
  544. for(x = 0; x < cv->width; x++)
  545. {
  546. uint8_t argb[8];
  547. uint32_t ch = *linechar++;
  548. _cucul_argb32_to_argb4(*lineattr++, argb);
  549. cur += sprintf(cur, "newpath\n");
  550. cur += sprintf(cur, "%d %d moveto\n", (x + 1) * 6, y * 10 + 2);
  551. cur += sprintf(cur, "%f %f %f setrgbcolor\n",
  552. (float)argb[5] * (1.0 / 0xf),
  553. (float)argb[6] * (1.0 / 0xf),
  554. (float)argb[7] * (1.0 / 0xf));
  555. if(ch < 0x00000020)
  556. cur += sprintf(cur, "(?) show\n");
  557. else if(ch >= 0x00000080)
  558. cur += sprintf(cur, "(?) show\n");
  559. else switch((uint8_t)(ch & 0x7f))
  560. {
  561. case '\\':
  562. case '(':
  563. case ')':
  564. cur += sprintf(cur, "(\\%c) show\n", (unsigned char)ch);
  565. break;
  566. default:
  567. cur += sprintf(cur, "(%c) show\n", (unsigned char)ch);
  568. break;
  569. }
  570. }
  571. }
  572. cur += sprintf(cur, "showpage\n");
  573. /* Crop to really used size */
  574. ex->size = (uintptr_t)(cur - ex->data);
  575. ex->data = realloc(ex->data, ex->size);
  576. }
  577. /* Export an SVG vector image */
  578. static void export_svg(cucul_canvas_t *cv, cucul_buffer_t *ex)
  579. {
  580. static char const svg_header[] =
  581. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  582. "<svg width=\"%d\" height=\"%d\" viewBox=\"0 0 %d %d\""
  583. " xmlns=\"http://www.w3.org/2000/svg\""
  584. " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
  585. " xml:space=\"preserve\" version=\"1.1\" baseProfile=\"full\">\n";
  586. char *cur;
  587. unsigned int x, y;
  588. /* 200 is arbitrary but should be ok */
  589. ex->size = strlen(svg_header) + (cv->width * cv->height * 200);
  590. ex->data = malloc(ex->size);
  591. cur = ex->data;
  592. /* Header */
  593. cur += sprintf(cur, svg_header, cv->width * 6, cv->height * 10,
  594. cv->width * 6, cv->height * 10);
  595. cur += sprintf(cur, " <g id=\"mainlayer\" font-size=\"12\">\n");
  596. /* Background */
  597. for(y = 0; y < cv->height; y++)
  598. {
  599. uint32_t *lineattr = cv->attr + y * cv->width;
  600. for(x = 0; x < cv->width; x++)
  601. {
  602. cur += sprintf(cur, "<rect style=\"fill:#%.03x\" x=\"%d\" y=\"%d\""
  603. " width=\"6\" height=\"10\"/>\n",
  604. _cucul_argb32_to_rgb12bg(*lineattr++),
  605. x * 6, y * 10);
  606. }
  607. }
  608. /* Text */
  609. for(y = 0; y < cv->height; y++)
  610. {
  611. uint32_t *lineattr = cv->attr + y * cv->width;
  612. uint32_t *linechar = cv->chars + y * cv->width;
  613. for(x = 0; x < cv->width; x++)
  614. {
  615. uint32_t ch = *linechar++;
  616. cur += sprintf(cur, "<text style=\"fill:#%.03x\" "
  617. "x=\"%d\" y=\"%d\">",
  618. _cucul_argb32_to_rgb12fg(*lineattr++),
  619. x * 6, (y * 10) + 10);
  620. if(ch < 0x00000020)
  621. *cur++ = '?';
  622. else if(ch > 0x0000007f)
  623. cur += cucul_utf32_to_utf8(cur, ch);
  624. else switch((uint8_t)ch)
  625. {
  626. case '>': cur += sprintf(cur, "&gt;"); break;
  627. case '<': cur += sprintf(cur, "&lt;"); break;
  628. case '&': cur += sprintf(cur, "&amp;"); break;
  629. default: *cur++ = ch; break;
  630. }
  631. cur += sprintf(cur, "</text>\n");
  632. }
  633. }
  634. cur += sprintf(cur, " </g>\n");
  635. cur += sprintf(cur, "</svg>\n");
  636. /* Crop to really used size */
  637. ex->size = (uintptr_t)(cur - ex->data);
  638. ex->data = realloc(ex->data, ex->size);
  639. }
  640. /* Export a TGA image */
  641. static void export_tga(cucul_canvas_t *cv, cucul_buffer_t *ex)
  642. {
  643. char const * const * fontlist;
  644. char * cur;
  645. cucul_font_t *f;
  646. unsigned int i, w, h;
  647. fontlist = cucul_get_font_list();
  648. if(!fontlist[0])
  649. return;
  650. f = cucul_load_font(fontlist[0], 0);
  651. w = cucul_get_canvas_width(cv) * cucul_get_font_width(f);
  652. h = cucul_get_canvas_height(cv) * cucul_get_font_height(f);
  653. ex->size = w * h * 4 + 18; /* 32 bpp + 18 bytes for the header */
  654. ex->data = malloc(ex->size);
  655. cur = ex->data;
  656. /* ID Length */
  657. cur += sprintf(cur, "%c", 0);
  658. /* Color Map Type: no colormap */
  659. cur += sprintf(cur, "%c", 0);
  660. /* Image Type: uncompressed truecolor */
  661. cur += sprintf(cur, "%c", 2);
  662. /* Color Map Specification: no color map */
  663. memset(cur, 0, 5); cur += 5;
  664. /* Image Specification */
  665. cur += sprintf(cur, "%c%c", 0, 0); /* X Origin */
  666. cur += sprintf(cur, "%c%c", 0, 0); /* Y Origin */
  667. cur += sprintf(cur, "%c%c", w & 0xff, w >> 8); /* Width */
  668. cur += sprintf(cur, "%c%c", h & 0xff, h >> 8); /* Height */
  669. cur += sprintf(cur, "%c", 32); /* Pixel Depth */
  670. cur += sprintf(cur, "%c", 40); /* Image Descriptor */
  671. /* Image ID: no ID */
  672. /* Color Map Data: no colormap */
  673. /* Image Data */
  674. cucul_render_canvas(cv, f, cur, w, h, 4 * w);
  675. /* Swap bytes. What a waste of time. */
  676. for(i = 0; i < w * h * 4; i += 4)
  677. {
  678. char w;
  679. w = cur[i]; cur[i] = cur[i + 3]; cur[i + 3] = w;
  680. w = cur[i + 1]; cur[i + 1] = cur[i + 2]; cur[i + 2] = w;
  681. }
  682. cucul_free_font(f);
  683. }