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.
 
 
 
 
 
 

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