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.
 
 
 
 
 
 

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