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.
 
 
 
 
 
 

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