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.
 
 
 
 
 
 

885 lines
27 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 *attrs = cv->attrs;
  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 = *attrs++;
  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->attrs + 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 >> 4) & 0x3fff) == (CUCUL_DEFAULT | 0x40)) ?
  202. 0x10 : palette[_cucul_attr_to_ansi4fg(attr)];
  203. bg = (((attr >> 18) & 0x3fff) == (CUCUL_TRANSPARENT | 0x40)) ?
  204. 0x10 : palette[_cucul_attr_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->attrs + 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_attr_to_ansi4fg(lineattr[x])];
  257. uint8_t bg = palette[_cucul_attr_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. * 83 chars for ";font-weight..."
  304. * up to 9 chars for "&#xxxxxx;", far less for pure ASCII
  305. * 7 chars for "</span>" */
  306. ex->size = 1000 + cv->height * (7 + cv->width * (47 + 83 + 9 + 7));
  307. ex->data = malloc(ex->size);
  308. cur = ex->data;
  309. /* HTML header */
  310. cur += sprintf(cur, "<html><head>\n");
  311. cur += sprintf(cur, "<title>Generated by libcaca %s</title>\n", VERSION);
  312. cur += sprintf(cur, "</head><body>\n");
  313. cur += sprintf(cur, "<div cellpadding='0' cellspacing='0' style='%s'>\n",
  314. "font-family: monospace, fixed; font-weight: bold;");
  315. for(y = 0; y < cv->height; y++)
  316. {
  317. uint32_t *lineattr = cv->attrs + y * cv->width;
  318. uint32_t *linechar = cv->chars + y * cv->width;
  319. for(x = 0; x < cv->width; x += len)
  320. {
  321. cur += sprintf(cur, "<span style=\"color:#%.03x;"
  322. "background-color:#%.03x",
  323. _cucul_attr_to_rgb12fg(lineattr[x]),
  324. _cucul_attr_to_rgb12bg(lineattr[x]));
  325. if(lineattr[x] & CUCUL_BOLD)
  326. cur += sprintf(cur, ";font-weight:bold");
  327. if(lineattr[x] & CUCUL_ITALICS)
  328. cur += sprintf(cur, ";font-style:italic");
  329. if(lineattr[x] & CUCUL_UNDERLINE)
  330. cur += sprintf(cur, ";text-decoration:underline");
  331. if(lineattr[x] & CUCUL_BLINK)
  332. cur += sprintf(cur, ";text-decoration:blink");
  333. cur += sprintf(cur, "\">");
  334. for(len = 0;
  335. x + len < cv->width && lineattr[x + len] == lineattr[x];
  336. len++)
  337. {
  338. if(linechar[x + len] == CUCUL_MAGIC_FULLWIDTH)
  339. ;
  340. else if(linechar[x + len] <= 0x00000020)
  341. cur += sprintf(cur, "&nbsp;");
  342. else if(linechar[x + len] < 0x00000080)
  343. cur += sprintf(cur, "%c",
  344. (unsigned char)linechar[x + len]);
  345. else
  346. cur += sprintf(cur, "&#%i;",
  347. (unsigned int)linechar[x + len]);
  348. }
  349. cur += sprintf(cur, "</span>");
  350. }
  351. /* New line */
  352. cur += sprintf(cur, "<br />\n");
  353. }
  354. cur += sprintf(cur, "</div></body></html>\n");
  355. /* Crop to really used size */
  356. ex->size = strlen(ex->data) + 1;
  357. ex->data = realloc(ex->data, ex->size);
  358. return 0;
  359. }
  360. /* Export an HTML3 document. This function is way bigger than export_html(),
  361. * but permits viewing in old browsers (or limited ones such as links). It
  362. * will not work under gecko (mozilla rendering engine) unless you set a
  363. * correct header. */
  364. static int export_html3(cucul_canvas_t *cv, cucul_buffer_t *ex)
  365. {
  366. char *cur;
  367. unsigned int x, y, len;
  368. /* The HTML table markup: less than 1000 bytes
  369. * A line: 10 chars for "<tr></tr>\n"
  370. * A glyph: 40 chars for "<td bgcolor=#xxxxxx><font color=#xxxxxx>"
  371. * up to 36 chars for "<b><i><u><blink></blink></u></i></b>"
  372. * up to 9 chars for "&#xxxxxx;", far less for pure ASCII
  373. * 12 chars for "</font></td>" */
  374. ex->size = 1000 + cv->height * (10 + cv->width * (40 + 36 + 9 + 12));
  375. ex->data = malloc(ex->size);
  376. cur = ex->data;
  377. /* Table */
  378. cur += sprintf(cur, "<table cols='%d' cellpadding='0' cellspacing='0'>\n",
  379. cv->height);
  380. for(y = 0; y < cv->height; y++)
  381. {
  382. uint32_t *lineattr = cv->attrs + y * cv->width;
  383. uint32_t *linechar = cv->chars + y * cv->width;
  384. cur += sprintf(cur, "<tr>");
  385. for(x = 0; x < cv->width; x += len)
  386. {
  387. unsigned int i;
  388. /* Use colspan option to factor cells with same attributes
  389. * (see below) */
  390. len = 1;
  391. while(x + len < cv->width && lineattr[x + len] == lineattr[x])
  392. len++;
  393. cur += sprintf(cur, "<td bgcolor=#%.06lx", (unsigned long int)
  394. _cucul_attr_to_rgb24bg(lineattr[x]));
  395. if(len > 1)
  396. cur += sprintf(cur, " colspan=%d", len);
  397. cur += sprintf(cur, "><font color=#%.06lx>", (unsigned long int)
  398. _cucul_attr_to_rgb24fg(lineattr[x]));
  399. if(lineattr[x] & CUCUL_BOLD)
  400. cur += sprintf(cur, "<b>");
  401. if(lineattr[x] & CUCUL_ITALICS)
  402. cur += sprintf(cur, "<i>");
  403. if(lineattr[x] & CUCUL_UNDERLINE)
  404. cur += sprintf(cur, "<u>");
  405. if(lineattr[x] & CUCUL_BLINK)
  406. cur += sprintf(cur, "<blink>");
  407. for(i = 0; i < len; i++)
  408. {
  409. if(linechar[x + i] == CUCUL_MAGIC_FULLWIDTH)
  410. ;
  411. else if(linechar[x + i] <= 0x00000020)
  412. cur += sprintf(cur, "&nbsp;");
  413. else if(linechar[x + i] < 0x00000080)
  414. cur += sprintf(cur, "%c", (unsigned char)linechar[x + i]);
  415. else
  416. cur += sprintf(cur, "&#%i;", (unsigned int)linechar[x + i]);
  417. }
  418. if(lineattr[x] & CUCUL_BLINK)
  419. cur += sprintf(cur, "</blink>");
  420. if(lineattr[x] & CUCUL_UNDERLINE)
  421. cur += sprintf(cur, "</u>");
  422. if(lineattr[x] & CUCUL_ITALICS)
  423. cur += sprintf(cur, "</i>");
  424. if(lineattr[x] & CUCUL_BOLD)
  425. cur += sprintf(cur, "</b>");
  426. cur += sprintf(cur, "</font></td>");
  427. }
  428. cur += sprintf(cur, "</tr>\n");
  429. }
  430. /* Footer */
  431. cur += sprintf(cur, "</table>\n");
  432. /* Crop to really used size */
  433. ex->size = (uintptr_t)(cur - ex->data);
  434. ex->data = realloc(ex->data, ex->size);
  435. return 0;
  436. }
  437. /* Export a text file with IRC colours */
  438. static int export_irc(cucul_canvas_t *cv, cucul_buffer_t *ex)
  439. {
  440. static uint8_t const palette[] =
  441. {
  442. 1, 2, 3, 10, 5, 6, 7, 15, /* Dark */
  443. 14, 12, 9, 11, 4, 13, 8, 0, /* Light */
  444. };
  445. char *cur;
  446. unsigned int x, y;
  447. /* 14 bytes assumed for max length per pixel. Worst case scenario:
  448. * ^Cxx,yy 6 bytes
  449. * ^B^B 2 bytes
  450. * ch 6 bytes
  451. * 3 bytes for max length per line. Worst case scenario:
  452. * <spc> 1 byte (for empty lines)
  453. * \r\n 2 bytes
  454. * In real life, the average bytes per pixel value will be around 5.
  455. */
  456. ex->size = 2 + cv->height * (3 + cv->width * 14);
  457. ex->data = malloc(ex->size);
  458. cur = ex->data;
  459. for(y = 0; y < cv->height; y++)
  460. {
  461. uint32_t *lineattr = cv->attrs + y * cv->width;
  462. uint32_t *linechar = cv->chars + y * cv->width;
  463. uint8_t prevfg = 0x10;
  464. uint8_t prevbg = 0x10;
  465. for(x = 0; x < cv->width; x++)
  466. {
  467. uint32_t attr = lineattr[x];
  468. uint8_t fg = palette[_cucul_attr_to_ansi4fg(attr)];
  469. uint8_t bg = palette[_cucul_attr_to_ansi4bg(attr)];
  470. uint32_t ch = linechar[x];
  471. if(ch == CUCUL_MAGIC_FULLWIDTH)
  472. continue;
  473. if(((attr >> 4) & 0x3fff) == (CUCUL_DEFAULT | 0x40))
  474. fg = 0x10;
  475. if(((attr >> 18) & 0x3fff) == (CUCUL_TRANSPARENT | 0x40))
  476. bg = 0x10;
  477. /* TODO: optimise series of same fg / same bg
  478. * don't change fg value if ch == ' '
  479. * make sure the \x03,%d trick works everywhere */
  480. if(bg != prevbg || fg != prevfg)
  481. {
  482. int need_escape = 0;
  483. if(bg == 0x10)
  484. {
  485. if(fg == 0x10)
  486. cur += sprintf(cur, "\x0f");
  487. else
  488. {
  489. if(prevbg == 0x10)
  490. cur += sprintf(cur, "\x03%d", fg);
  491. else
  492. cur += sprintf(cur, "\x0f\x03%d", fg);
  493. if(ch == (uint32_t)',')
  494. need_escape = 1;
  495. }
  496. }
  497. else
  498. {
  499. if(fg == 0x10)
  500. cur += sprintf(cur, "\x0f\x03,%d", bg);
  501. else
  502. cur += sprintf(cur, "\x03%d,%d", fg, bg);
  503. }
  504. if(ch >= (uint32_t)'0' && ch <= (uint32_t)'9')
  505. need_escape = 1;
  506. if(need_escape)
  507. cur += sprintf(cur, "\x02\x02");
  508. }
  509. cur += cucul_utf32_to_utf8(cur, ch);
  510. prevfg = fg;
  511. prevbg = bg;
  512. }
  513. /* TODO: do the same the day we optimise whole lines above */
  514. if(!cv->width)
  515. *cur++ = ' ';
  516. *cur++ = '\r';
  517. *cur++ = '\n';
  518. }
  519. /* Crop to really used size */
  520. ex->size = (uintptr_t)(cur - ex->data);
  521. ex->data = realloc(ex->data, ex->size);
  522. return 0;
  523. }
  524. /* Export a PostScript document. */
  525. static int export_ps(cucul_canvas_t *cv, cucul_buffer_t *ex)
  526. {
  527. static char const *ps_header =
  528. "%!\n"
  529. "%% libcaca PDF export\n"
  530. "%%LanguageLevel: 2\n"
  531. "%%Pages: 1\n"
  532. "%%DocumentData: Clean7Bit\n"
  533. "/csquare {\n"
  534. " newpath\n"
  535. " 0 0 moveto\n"
  536. " 0 1 rlineto\n"
  537. " 1 0 rlineto\n"
  538. " 0 -1 rlineto\n"
  539. " closepath\n"
  540. " setrgbcolor\n"
  541. " fill\n"
  542. "} def\n"
  543. "/S {\n"
  544. " Show\n"
  545. "} bind def\n"
  546. "/Courier-Bold findfont\n"
  547. "8 scalefont\n"
  548. "setfont\n"
  549. "gsave\n"
  550. "6 10 scale\n";
  551. char *cur;
  552. unsigned int x, y;
  553. /* 200 is arbitrary but should be ok */
  554. ex->size = strlen(ps_header) + 100 + cv->height * (32 + cv->width * 200);
  555. ex->data = malloc(ex->size);
  556. cur = ex->data;
  557. /* Header */
  558. cur += sprintf(cur, "%s", ps_header);
  559. cur += sprintf(cur, "0 %d translate\n", cv->height);
  560. /* Background, drawn using csquare macro defined in header */
  561. for(y = cv->height; y--; )
  562. {
  563. uint32_t *lineattr = cv->attrs + y * cv->width;
  564. for(x = 0; x < cv->width; x++)
  565. {
  566. uint8_t argb[8];
  567. _cucul_attr_to_argb4(*lineattr++, argb);
  568. cur += sprintf(cur, "1 0 translate\n %f %f %f csquare\n",
  569. (float)argb[1] * (1.0 / 0xf),
  570. (float)argb[2] * (1.0 / 0xf),
  571. (float)argb[3] * (1.0 / 0xf));
  572. }
  573. /* Return to beginning of the line, and jump to the next one */
  574. cur += sprintf(cur, "-%d 1 translate\n", cv->width);
  575. }
  576. cur += sprintf(cur, "grestore\n"); /* Restore transformation matrix */
  577. cur += sprintf(cur, "0 %d translate\n", cv->height*10);
  578. for(y = cv->height; y--; )
  579. {
  580. uint32_t *lineattr = cv->attrs + (cv->height - y - 1) * cv->width;
  581. uint32_t *linechar = cv->chars + (cv->height - y - 1) * cv->width;
  582. for(x = 0; x < cv->width; x++)
  583. {
  584. uint8_t argb[8];
  585. uint32_t ch = *linechar++;
  586. _cucul_attr_to_argb4(*lineattr++, argb);
  587. cur += sprintf(cur, "newpath\n");
  588. cur += sprintf(cur, "%d %d moveto\n", (x + 1) * 6, y * 10 + 2);
  589. cur += sprintf(cur, "%f %f %f setrgbcolor\n",
  590. (float)argb[5] * (1.0 / 0xf),
  591. (float)argb[6] * (1.0 / 0xf),
  592. (float)argb[7] * (1.0 / 0xf));
  593. if(ch < 0x00000020)
  594. cur += sprintf(cur, "(?) show\n");
  595. else if(ch >= 0x00000080)
  596. cur += sprintf(cur, "(?) show\n");
  597. else switch((uint8_t)(ch & 0x7f))
  598. {
  599. case '\\':
  600. case '(':
  601. case ')':
  602. cur += sprintf(cur, "(\\%c) show\n", (unsigned char)ch);
  603. break;
  604. default:
  605. cur += sprintf(cur, "(%c) show\n", (unsigned char)ch);
  606. break;
  607. }
  608. }
  609. }
  610. cur += sprintf(cur, "showpage\n");
  611. /* Crop to really used size */
  612. ex->size = (uintptr_t)(cur - ex->data);
  613. ex->data = realloc(ex->data, ex->size);
  614. return 0;
  615. }
  616. /* Export an SVG vector image */
  617. static int export_svg(cucul_canvas_t *cv, cucul_buffer_t *ex)
  618. {
  619. static char const svg_header[] =
  620. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  621. "<svg width=\"%d\" height=\"%d\" viewBox=\"0 0 %d %d\""
  622. " xmlns=\"http://www.w3.org/2000/svg\""
  623. " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
  624. " xml:space=\"preserve\" version=\"1.1\" baseProfile=\"full\">\n";
  625. char *cur;
  626. unsigned int x, y;
  627. /* 200 is arbitrary but should be ok */
  628. ex->size = strlen(svg_header) + 128 + cv->width * cv->height * 200;
  629. ex->data = malloc(ex->size);
  630. cur = ex->data;
  631. /* Header */
  632. cur += sprintf(cur, svg_header, cv->width * 6, cv->height * 10,
  633. cv->width * 6, cv->height * 10);
  634. cur += sprintf(cur, " <g id=\"mainlayer\" font-size=\"10\""
  635. " style=\"font-family: monospace\">\n");
  636. /* Background */
  637. for(y = 0; y < cv->height; y++)
  638. {
  639. uint32_t *lineattr = cv->attrs + y * cv->width;
  640. for(x = 0; x < cv->width; x++)
  641. {
  642. cur += sprintf(cur, "<rect style=\"fill:#%.03x\" x=\"%d\" y=\"%d\""
  643. " width=\"6\" height=\"10\"/>\n",
  644. _cucul_attr_to_rgb12bg(*lineattr++),
  645. x * 6, y * 10);
  646. }
  647. }
  648. /* Text */
  649. for(y = 0; y < cv->height; y++)
  650. {
  651. uint32_t *lineattr = cv->attrs + y * cv->width;
  652. uint32_t *linechar = cv->chars + y * cv->width;
  653. for(x = 0; x < cv->width; x++)
  654. {
  655. uint32_t ch = *linechar++;
  656. if(ch == ' ' || ch == CUCUL_MAGIC_FULLWIDTH)
  657. {
  658. lineattr++;
  659. continue;
  660. }
  661. cur += sprintf(cur, "<text style=\"fill:#%.03x\" "
  662. "x=\"%d\" y=\"%d\">",
  663. _cucul_attr_to_rgb12fg(*lineattr++),
  664. x * 6, (y * 10) + 8);
  665. if(ch < 0x00000020)
  666. *cur++ = '?';
  667. else if(ch > 0x0000007f)
  668. cur += cucul_utf32_to_utf8(cur, ch);
  669. else switch((uint8_t)ch)
  670. {
  671. case '>': cur += sprintf(cur, "&gt;"); break;
  672. case '<': cur += sprintf(cur, "&lt;"); break;
  673. case '&': cur += sprintf(cur, "&amp;"); break;
  674. default: *cur++ = ch; break;
  675. }
  676. cur += sprintf(cur, "</text>\n");
  677. }
  678. }
  679. cur += sprintf(cur, " </g>\n");
  680. cur += sprintf(cur, "</svg>\n");
  681. /* Crop to really used size */
  682. ex->size = (uintptr_t)(cur - ex->data);
  683. ex->data = realloc(ex->data, ex->size);
  684. return 0;
  685. }
  686. /* Export a TGA image */
  687. static int export_tga(cucul_canvas_t *cv, cucul_buffer_t *ex)
  688. {
  689. char const * const *fontlist;
  690. char * cur;
  691. cucul_font_t *f;
  692. unsigned int i, w, h;
  693. fontlist = cucul_get_font_list();
  694. if(!fontlist[0])
  695. return -1;
  696. f = cucul_load_font(fontlist[0], 0);
  697. w = cucul_get_canvas_width(cv) * cucul_get_font_width(f);
  698. h = cucul_get_canvas_height(cv) * cucul_get_font_height(f);
  699. ex->size = w * h * 4 + 18; /* 32 bpp + 18 bytes for the header */
  700. ex->data = malloc(ex->size);
  701. cur = ex->data;
  702. /* ID Length */
  703. cur += sprintf(cur, "%c", 0);
  704. /* Color Map Type: no colormap */
  705. cur += sprintf(cur, "%c", 0);
  706. /* Image Type: uncompressed truecolor */
  707. cur += sprintf(cur, "%c", 2);
  708. /* Color Map Specification: no color map */
  709. memset(cur, 0, 5); cur += 5;
  710. /* Image Specification */
  711. cur += sprintf(cur, "%c%c", 0, 0); /* X Origin */
  712. cur += sprintf(cur, "%c%c", 0, 0); /* Y Origin */
  713. cur += sprintf(cur, "%c%c", w & 0xff, w >> 8); /* Width */
  714. cur += sprintf(cur, "%c%c", h & 0xff, h >> 8); /* Height */
  715. cur += sprintf(cur, "%c", 32); /* Pixel Depth */
  716. cur += sprintf(cur, "%c", 40); /* Image Descriptor */
  717. /* Image ID: no ID */
  718. /* Color Map Data: no colormap */
  719. /* Image Data */
  720. cucul_render_canvas(cv, f, cur, w, h, 4 * w);
  721. /* Swap bytes. What a waste of time. */
  722. for(i = 0; i < w * h * 4; i += 4)
  723. {
  724. char w;
  725. w = cur[i]; cur[i] = cur[i + 3]; cur[i + 3] = w;
  726. w = cur[i + 1]; cur[i + 1] = cur[i + 2]; cur[i + 2] = w;
  727. }
  728. cucul_free_font(f);
  729. return 0;
  730. }