您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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