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.
 
 
 
 
 
 

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