Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

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