25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

856 satır
25 KiB

  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * 2006 Jean-Yves Lamoureux <jylam@lnxscene.org>
  5. * All Rights Reserved
  6. *
  7. * $Id$
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the Do What The Fuck You Want To
  11. * Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * This file contains various export functions
  16. */
  17. #include "config.h"
  18. #include "common.h"
  19. #if !defined(__KERNEL__)
  20. # if defined(HAVE_ERRNO_H)
  21. # include <errno.h>
  22. # endif
  23. # include <stdlib.h>
  24. # include <stdio.h>
  25. # include <string.h>
  26. #endif
  27. #include "cucul.h"
  28. #include "cucul_internals.h"
  29. static int export_caca(cucul_canvas_t *, cucul_buffer_t *);
  30. static int export_ansi(cucul_canvas_t *, cucul_buffer_t *);
  31. static int export_utf8(cucul_canvas_t *, cucul_buffer_t *);
  32. static int export_html(cucul_canvas_t *, cucul_buffer_t *);
  33. static int export_html3(cucul_canvas_t *, cucul_buffer_t *);
  34. static int export_irc(cucul_canvas_t *, cucul_buffer_t *);
  35. static int export_ps(cucul_canvas_t *, cucul_buffer_t *);
  36. static int export_svg(cucul_canvas_t *, cucul_buffer_t *);
  37. static int export_tga(cucul_canvas_t *, cucul_buffer_t *);
  38. /** \brief Export a canvas into a foreign format.
  39. *
  40. * This function exports a libcucul canvas into various foreign formats such
  41. * as ANSI art, HTML, IRC colours, etc. One should use cucul_get_buffer_data()
  42. * and cucul_get_buffer_size() to access the buffer contents. The allocated
  43. * data is valid until cucul_free_buffer() is called.
  44. *
  45. * Valid values for \c format are:
  46. * - \c "caca": export native libcaca files.
  47. * - \c "ansi": export ANSI art (CP437 charset with ANSI colour codes).
  48. * - \c "html": export an HTML page with CSS information.
  49. * - \c "html3": export an HTML table that should be compatible with
  50. * most navigators, including textmode ones.
  51. * - \c "irc": export UTF-8 text with mIRC colour codes.
  52. * - \c "ps": export a PostScript document.
  53. * - \c "svg": export an SVG vector image.
  54. * - \c "tga": export a TGA image.
  55. *
  56. * If an error occurs, NULL is returned and \b errno is set accordingly:
  57. * - \c EINVAL Unsupported format requested.
  58. * - \c ENOMEM Not enough memory to allocate output buffer.
  59. *
  60. * \param cv A libcucul canvas
  61. * \param format A string describing the requested output format.
  62. * \return A libcucul buffer, or NULL in case of error.
  63. */
  64. cucul_buffer_t * cucul_export_canvas(cucul_canvas_t *cv, char const *format)
  65. {
  66. cucul_buffer_t *ex;
  67. int ret = -1;
  68. ex = malloc(sizeof(cucul_buffer_t));
  69. if(!ex)
  70. {
  71. #if defined(HAVE_ERRNO_H)
  72. errno = ENOMEM;
  73. #endif
  74. return NULL;
  75. }
  76. ex->size = 0;
  77. ex->data = NULL;
  78. ex->user_data = 0;
  79. if(!strcasecmp("caca", format))
  80. ret = export_caca(cv, ex);
  81. else if(!strcasecmp("ansi", format))
  82. ret = export_ansi(cv, ex);
  83. else if(!strcasecmp("utf8", format))
  84. ret = export_utf8(cv, ex);
  85. else if(!strcasecmp("html", format))
  86. ret = export_html(cv, ex);
  87. else if(!strcasecmp("html3", format))
  88. ret = export_html3(cv, ex);
  89. else if(!strcasecmp("irc", format))
  90. ret = export_irc(cv, ex);
  91. else if(!strcasecmp("ps", format))
  92. ret = export_ps(cv, ex);
  93. else if(!strcasecmp("svg", format))
  94. ret = export_svg(cv, ex);
  95. else if(!strcasecmp("tga", format))
  96. ret = export_tga(cv, ex);
  97. if(ret < 0)
  98. {
  99. free(ex);
  100. #if defined(HAVE_ERRNO_H)
  101. errno = EINVAL;
  102. #endif
  103. return NULL;
  104. }
  105. return ex;
  106. }
  107. /** \brief Get available export formats
  108. *
  109. * Return a list of available export formats. The list is a NULL-terminated
  110. * array of strings, interleaving a string containing the internal value for
  111. * the export format, to be used with cucul_export_canvas(), and a string
  112. * containing the natural language description for that export format.
  113. *
  114. * This function never fails.
  115. *
  116. * \return An array of strings.
  117. */
  118. char const * const * cucul_get_export_list(void)
  119. {
  120. static char const * const list[] =
  121. {
  122. "caca", "native libcaca format",
  123. "ansi", "ANSI",
  124. "utf8", "UTF-8 with ANSI escape codes",
  125. "html", "HTML",
  126. "html3", "backwards-compatible HTML",
  127. "irc", "IRC with mIRC colours",
  128. "ps", "PostScript document",
  129. "svg", "SVG vector image",
  130. "tga", "TGA image",
  131. NULL, NULL
  132. };
  133. return list;
  134. }
  135. /*
  136. * XXX: the following functions are local.
  137. */
  138. /* Generate a native libcaca canvas file. */
  139. static int export_caca(cucul_canvas_t *cv, cucul_buffer_t *ex)
  140. {
  141. uint32_t *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) & 0x7ffe) == (CUCUL_DEFAULT << 4)) ?
  202. 0x10 : palette[_cucul_attr_to_ansi4fg(attr)];
  203. bg = (((attr >> 18) & 0x7ffe) == (CUCUL_TRANSPARENT << 4)) ?
  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. * up to 9 chars for "&#xxxxxx;", far less for pure ASCII
  304. * 7 chars for "</span>" */
  305. ex->size = 1000 + cv->height * (7 + cv->width * (47 + 9 + 7));
  306. ex->data = malloc(ex->size);
  307. cur = ex->data;
  308. /* HTML header */
  309. cur += sprintf(cur, "<html><head>\n");
  310. cur += sprintf(cur, "<title>Generated by libcaca %s</title>\n", VERSION);
  311. cur += sprintf(cur, "</head><body>\n");
  312. cur += sprintf(cur, "<div cellpadding='0' cellspacing='0' style='%s'>\n",
  313. "font-family: monospace, fixed; font-weight: bold;");
  314. for(y = 0; y < cv->height; y++)
  315. {
  316. uint32_t *lineattr = cv->attrs + y * cv->width;
  317. uint32_t *linechar = cv->chars + y * cv->width;
  318. for(x = 0; x < cv->width; x += len)
  319. {
  320. cur += sprintf(cur, "<span style=\"color:#%.03x;"
  321. "background-color:#%.03x\">",
  322. _cucul_attr_to_rgb12fg(lineattr[x]),
  323. _cucul_attr_to_rgb12bg(lineattr[x]));
  324. for(len = 0;
  325. x + len < cv->width && lineattr[x + len] == lineattr[x];
  326. len++)
  327. {
  328. if(linechar[x + len] == CUCUL_MAGIC_FULLWIDTH)
  329. ;
  330. else if(linechar[x + len] <= 0x00000020)
  331. cur += sprintf(cur, "&nbsp;");
  332. else if(linechar[x + len] < 0x00000080)
  333. cur += sprintf(cur, "%c",
  334. (unsigned char)linechar[x + len]);
  335. else
  336. cur += sprintf(cur, "&#%i;",
  337. (unsigned int)linechar[x + len]);
  338. }
  339. cur += sprintf(cur, "</span>");
  340. }
  341. /* New line */
  342. cur += sprintf(cur, "<br />\n");
  343. }
  344. cur += sprintf(cur, "</div></body></html>\n");
  345. /* Crop to really used size */
  346. ex->size = strlen(ex->data) + 1;
  347. ex->data = realloc(ex->data, ex->size);
  348. return 0;
  349. }
  350. /* Export an HTML3 document. This function is way bigger than export_html(),
  351. * but permits viewing in old browsers (or limited ones such as links). It
  352. * will not work under gecko (mozilla rendering engine) unless you set a
  353. * correct header. */
  354. static int export_html3(cucul_canvas_t *cv, cucul_buffer_t *ex)
  355. {
  356. char *cur;
  357. unsigned int x, y, len;
  358. /* The HTML table markup: less than 1000 bytes
  359. * A line: 10 chars for "<tr></tr>\n"
  360. * A glyph: 40 chars for "<td bgcolor=#xxxxxx><font color=#xxxxxx>"
  361. * up to 9 chars for "&#xxxxxx;", far less for pure ASCII
  362. * 12 chars for "</font></td>" */
  363. ex->size = 1000 + cv->height * (10 + cv->width * (40 + 9 + 12));
  364. ex->data = malloc(ex->size);
  365. cur = ex->data;
  366. /* Table */
  367. cur += sprintf(cur, "<table cols='%d' cellpadding='0' cellspacing='0'>\n",
  368. cv->height);
  369. for(y = 0; y < cv->height; y++)
  370. {
  371. uint32_t *lineattr = cv->attrs + y * cv->width;
  372. uint32_t *linechar = cv->chars + y * cv->width;
  373. cur += sprintf(cur, "<tr>");
  374. for(x = 0; x < cv->width; x += len)
  375. {
  376. unsigned int i;
  377. /* Use colspan option to factor cells with same attributes
  378. * (see below) */
  379. len = 1;
  380. while(x + len < cv->width && lineattr[x + len] == lineattr[x])
  381. len++;
  382. cur += sprintf(cur, "<td bgcolor=#%.06lx", (unsigned long int)
  383. _cucul_attr_to_rgb24bg(lineattr[x]));
  384. if(len > 1)
  385. cur += sprintf(cur, " colspan=%d", len);
  386. cur += sprintf(cur, "><font color=#%.06lx>", (unsigned long int)
  387. _cucul_attr_to_rgb24fg(lineattr[x]));
  388. for(i = 0; i < len; i++)
  389. {
  390. if(linechar[x + i] == CUCUL_MAGIC_FULLWIDTH)
  391. ;
  392. else if(linechar[x + i] <= 0x00000020)
  393. cur += sprintf(cur, "&nbsp;");
  394. else if(linechar[x + i] < 0x00000080)
  395. cur += sprintf(cur, "%c", (unsigned char)linechar[x + i]);
  396. else
  397. cur += sprintf(cur, "&#%i;", (unsigned int)linechar[x + i]);
  398. }
  399. cur += sprintf(cur, "</font></td>");
  400. }
  401. cur += sprintf(cur, "</tr>\n");
  402. }
  403. /* Footer */
  404. cur += sprintf(cur, "</table>\n");
  405. /* Crop to really used size */
  406. ex->size = (uintptr_t)(cur - ex->data);
  407. ex->data = realloc(ex->data, ex->size);
  408. return 0;
  409. }
  410. /* Export a text file with IRC colours */
  411. static int export_irc(cucul_canvas_t *cv, cucul_buffer_t *ex)
  412. {
  413. static uint8_t const palette[] =
  414. {
  415. 1, 2, 3, 10, 5, 6, 7, 15, /* Dark */
  416. 14, 12, 9, 11, 4, 13, 8, 0, /* Light */
  417. };
  418. char *cur;
  419. unsigned int x, y;
  420. /* 14 bytes assumed for max length per pixel. Worst case scenario:
  421. * ^Cxx,yy 6 bytes
  422. * ^B^B 2 bytes
  423. * ch 6 bytes
  424. * 3 bytes for max length per line. Worst case scenario:
  425. * <spc> 1 byte (for empty lines)
  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->height * (3 + cv->width * 14);
  430. ex->data = malloc(ex->size);
  431. cur = ex->data;
  432. for(y = 0; y < cv->height; y++)
  433. {
  434. uint32_t *lineattr = cv->attrs + y * cv->width;
  435. uint32_t *linechar = cv->chars + y * cv->width;
  436. uint8_t prevfg = 0x10;
  437. uint8_t prevbg = 0x10;
  438. for(x = 0; x < cv->width; x++)
  439. {
  440. uint32_t attr = lineattr[x];
  441. uint8_t fg = palette[_cucul_attr_to_ansi4fg(attr)];
  442. uint8_t bg = palette[_cucul_attr_to_ansi4bg(attr)];
  443. uint32_t ch = linechar[x];
  444. if(ch == CUCUL_MAGIC_FULLWIDTH)
  445. continue;
  446. if(((attr >> 4) & 0x7ffe) == (CUCUL_DEFAULT << 4))
  447. fg = 0x10;
  448. if(((attr >> 18) & 0x7ffe) == (CUCUL_TRANSPARENT << 4))
  449. bg = 0x10;
  450. /* TODO: optimise series of same fg / same bg
  451. * don't change fg value if ch == ' '
  452. * make sure the \x03,%d trick works everywhere */
  453. if(bg != prevbg || fg != prevfg)
  454. {
  455. int need_escape = 0;
  456. if(bg == 0x10)
  457. {
  458. if(fg == 0x10)
  459. cur += sprintf(cur, "\x0f");
  460. else
  461. {
  462. if(prevbg == 0x10)
  463. cur += sprintf(cur, "\x03%d", fg);
  464. else
  465. cur += sprintf(cur, "\x0f\x03%d", fg);
  466. if(ch == (uint32_t)',')
  467. need_escape = 1;
  468. }
  469. }
  470. else
  471. {
  472. if(fg == 0x10)
  473. cur += sprintf(cur, "\x0f\x03,%d", bg);
  474. else
  475. cur += sprintf(cur, "\x03%d,%d", fg, bg);
  476. }
  477. if(ch >= (uint32_t)'0' && ch <= (uint32_t)'9')
  478. need_escape = 1;
  479. if(need_escape)
  480. cur += sprintf(cur, "\x02\x02");
  481. }
  482. cur += cucul_utf32_to_utf8(cur, ch);
  483. prevfg = fg;
  484. prevbg = bg;
  485. }
  486. /* TODO: do the same the day we optimise whole lines above */
  487. if(!cv->width)
  488. *cur++ = ' ';
  489. *cur++ = '\r';
  490. *cur++ = '\n';
  491. }
  492. /* Crop to really used size */
  493. ex->size = (uintptr_t)(cur - ex->data);
  494. ex->data = realloc(ex->data, ex->size);
  495. return 0;
  496. }
  497. /* Export a PostScript document. */
  498. static int export_ps(cucul_canvas_t *cv, cucul_buffer_t *ex)
  499. {
  500. static char const *ps_header =
  501. "%!\n"
  502. "%% libcaca PDF export\n"
  503. "%%LanguageLevel: 2\n"
  504. "%%Pages: 1\n"
  505. "%%DocumentData: Clean7Bit\n"
  506. "/csquare {\n"
  507. " newpath\n"
  508. " 0 0 moveto\n"
  509. " 0 1 rlineto\n"
  510. " 1 0 rlineto\n"
  511. " 0 -1 rlineto\n"
  512. " closepath\n"
  513. " setrgbcolor\n"
  514. " fill\n"
  515. "} def\n"
  516. "/S {\n"
  517. " Show\n"
  518. "} bind def\n"
  519. "/Courier-Bold findfont\n"
  520. "8 scalefont\n"
  521. "setfont\n"
  522. "gsave\n"
  523. "6 10 scale\n";
  524. char *cur;
  525. unsigned int x, y;
  526. /* 200 is arbitrary but should be ok */
  527. ex->size = strlen(ps_header) + 100 + cv->height * (32 + cv->width * 200);
  528. ex->data = malloc(ex->size);
  529. cur = ex->data;
  530. /* Header */
  531. cur += sprintf(cur, "%s", ps_header);
  532. cur += sprintf(cur, "0 %d translate\n", cv->height);
  533. /* Background, drawn using csquare macro defined in header */
  534. for(y = cv->height; y--; )
  535. {
  536. uint32_t *lineattr = cv->attrs + y * cv->width;
  537. for(x = 0; x < cv->width; x++)
  538. {
  539. uint8_t argb[8];
  540. _cucul_attr_to_argb4(*lineattr++, argb);
  541. cur += sprintf(cur, "1 0 translate\n %f %f %f csquare\n",
  542. (float)argb[1] * (1.0 / 0xf),
  543. (float)argb[2] * (1.0 / 0xf),
  544. (float)argb[3] * (1.0 / 0xf));
  545. }
  546. /* Return to beginning of the line, and jump to the next one */
  547. cur += sprintf(cur, "-%d 1 translate\n", cv->width);
  548. }
  549. cur += sprintf(cur, "grestore\n"); /* Restore transformation matrix */
  550. cur += sprintf(cur, "0 %d translate\n", cv->height*10);
  551. for(y = cv->height; y--; )
  552. {
  553. uint32_t *lineattr = cv->attrs + (cv->height - y - 1) * cv->width;
  554. uint32_t *linechar = cv->chars + (cv->height - y - 1) * cv->width;
  555. for(x = 0; x < cv->width; x++)
  556. {
  557. uint8_t argb[8];
  558. uint32_t ch = *linechar++;
  559. _cucul_attr_to_argb4(*lineattr++, argb);
  560. cur += sprintf(cur, "newpath\n");
  561. cur += sprintf(cur, "%d %d moveto\n", (x + 1) * 6, y * 10 + 2);
  562. cur += sprintf(cur, "%f %f %f setrgbcolor\n",
  563. (float)argb[5] * (1.0 / 0xf),
  564. (float)argb[6] * (1.0 / 0xf),
  565. (float)argb[7] * (1.0 / 0xf));
  566. if(ch < 0x00000020)
  567. cur += sprintf(cur, "(?) show\n");
  568. else if(ch >= 0x00000080)
  569. cur += sprintf(cur, "(?) show\n");
  570. else switch((uint8_t)(ch & 0x7f))
  571. {
  572. case '\\':
  573. case '(':
  574. case ')':
  575. cur += sprintf(cur, "(\\%c) show\n", (unsigned char)ch);
  576. break;
  577. default:
  578. cur += sprintf(cur, "(%c) show\n", (unsigned char)ch);
  579. break;
  580. }
  581. }
  582. }
  583. cur += sprintf(cur, "showpage\n");
  584. /* Crop to really used size */
  585. ex->size = (uintptr_t)(cur - ex->data);
  586. ex->data = realloc(ex->data, ex->size);
  587. return 0;
  588. }
  589. /* Export an SVG vector image */
  590. static int export_svg(cucul_canvas_t *cv, cucul_buffer_t *ex)
  591. {
  592. static char const svg_header[] =
  593. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  594. "<svg width=\"%d\" height=\"%d\" viewBox=\"0 0 %d %d\""
  595. " xmlns=\"http://www.w3.org/2000/svg\""
  596. " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
  597. " xml:space=\"preserve\" version=\"1.1\" baseProfile=\"full\">\n";
  598. char *cur;
  599. unsigned int x, y;
  600. /* 200 is arbitrary but should be ok */
  601. ex->size = strlen(svg_header) + 128 + cv->width * cv->height * 200;
  602. ex->data = malloc(ex->size);
  603. cur = ex->data;
  604. /* Header */
  605. cur += sprintf(cur, svg_header, cv->width * 6, cv->height * 10,
  606. cv->width * 6, cv->height * 10);
  607. cur += sprintf(cur, " <g id=\"mainlayer\" font-size=\"10\""
  608. " style=\"font-family: monospace\">\n");
  609. /* Background */
  610. for(y = 0; y < cv->height; y++)
  611. {
  612. uint32_t *lineattr = cv->attrs + y * cv->width;
  613. for(x = 0; x < cv->width; x++)
  614. {
  615. cur += sprintf(cur, "<rect style=\"fill:#%.03x\" x=\"%d\" y=\"%d\""
  616. " width=\"6\" height=\"10\"/>\n",
  617. _cucul_attr_to_rgb12bg(*lineattr++),
  618. x * 6, y * 10);
  619. }
  620. }
  621. /* Text */
  622. for(y = 0; y < cv->height; y++)
  623. {
  624. uint32_t *lineattr = cv->attrs + y * cv->width;
  625. uint32_t *linechar = cv->chars + y * cv->width;
  626. for(x = 0; x < cv->width; x++)
  627. {
  628. uint32_t ch = *linechar++;
  629. if(ch == ' ' || ch == CUCUL_MAGIC_FULLWIDTH)
  630. {
  631. lineattr++;
  632. continue;
  633. }
  634. cur += sprintf(cur, "<text style=\"fill:#%.03x\" "
  635. "x=\"%d\" y=\"%d\">",
  636. _cucul_attr_to_rgb12fg(*lineattr++),
  637. x * 6, (y * 10) + 8);
  638. if(ch < 0x00000020)
  639. *cur++ = '?';
  640. else if(ch > 0x0000007f)
  641. cur += cucul_utf32_to_utf8(cur, ch);
  642. else switch((uint8_t)ch)
  643. {
  644. case '>': cur += sprintf(cur, "&gt;"); break;
  645. case '<': cur += sprintf(cur, "&lt;"); break;
  646. case '&': cur += sprintf(cur, "&amp;"); break;
  647. default: *cur++ = ch; break;
  648. }
  649. cur += sprintf(cur, "</text>\n");
  650. }
  651. }
  652. cur += sprintf(cur, " </g>\n");
  653. cur += sprintf(cur, "</svg>\n");
  654. /* Crop to really used size */
  655. ex->size = (uintptr_t)(cur - ex->data);
  656. ex->data = realloc(ex->data, ex->size);
  657. return 0;
  658. }
  659. /* Export a TGA image */
  660. static int export_tga(cucul_canvas_t *cv, cucul_buffer_t *ex)
  661. {
  662. char const * const *fontlist;
  663. char * cur;
  664. cucul_font_t *f;
  665. unsigned int i, w, h;
  666. fontlist = cucul_get_font_list();
  667. if(!fontlist[0])
  668. return -1;
  669. f = cucul_load_font(fontlist[0], 0);
  670. w = cucul_get_canvas_width(cv) * cucul_get_font_width(f);
  671. h = cucul_get_canvas_height(cv) * cucul_get_font_height(f);
  672. ex->size = w * h * 4 + 18; /* 32 bpp + 18 bytes for the header */
  673. ex->data = malloc(ex->size);
  674. cur = ex->data;
  675. /* ID Length */
  676. cur += sprintf(cur, "%c", 0);
  677. /* Color Map Type: no colormap */
  678. cur += sprintf(cur, "%c", 0);
  679. /* Image Type: uncompressed truecolor */
  680. cur += sprintf(cur, "%c", 2);
  681. /* Color Map Specification: no color map */
  682. memset(cur, 0, 5); cur += 5;
  683. /* Image Specification */
  684. cur += sprintf(cur, "%c%c", 0, 0); /* X Origin */
  685. cur += sprintf(cur, "%c%c", 0, 0); /* Y Origin */
  686. cur += sprintf(cur, "%c%c", w & 0xff, w >> 8); /* Width */
  687. cur += sprintf(cur, "%c%c", h & 0xff, h >> 8); /* Height */
  688. cur += sprintf(cur, "%c", 32); /* Pixel Depth */
  689. cur += sprintf(cur, "%c", 40); /* Image Descriptor */
  690. /* Image ID: no ID */
  691. /* Color Map Data: no colormap */
  692. /* Image Data */
  693. cucul_render_canvas(cv, f, cur, w, h, 4 * w);
  694. /* Swap bytes. What a waste of time. */
  695. for(i = 0; i < w * h * 4; i += 4)
  696. {
  697. char w;
  698. w = cur[i]; cur[i] = cur[i + 3]; cur[i + 3] = w;
  699. w = cur[i + 1]; cur[i + 1] = cur[i + 2]; cur[i + 2] = w;
  700. }
  701. cucul_free_font(f);
  702. return 0;
  703. }