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.
 
 
 
 
 
 

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