Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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