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.
 
 
 
 
 
 

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