Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

1015 lignes
32 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. It comes without any warranty, to
  10. * the extent permitted by applicable law. You can redistribute it
  11. * and/or modify it under the terms of the Do What The Fuck You Want
  12. * To Public License, Version 2, as published by Sam Hocevar. See
  13. * http://sam.zoy.org/wtfpl/COPYING for more details.
  14. */
  15. /*
  16. * This file contains various export functions
  17. */
  18. #include "config.h"
  19. #include "common.h"
  20. #if !defined(__KERNEL__)
  21. # include <stdlib.h>
  22. # include <stdio.h>
  23. # include <string.h>
  24. #endif
  25. #include "cucul.h"
  26. #include "cucul_internals.h"
  27. static inline int sprintu32(char *s, uint32_t x)
  28. {
  29. s[0] = (uint8_t)(x >> 24);
  30. s[1] = (uint8_t)(x >> 16) & 0xff;
  31. s[2] = (uint8_t)(x >> 8) & 0xff;
  32. s[3] = (uint8_t)(x ) & 0xff;
  33. return 4;
  34. }
  35. static inline int sprintu16(char *s, uint16_t x)
  36. {
  37. s[0] = (uint8_t)(x >> 8) & 0xff;
  38. s[1] = (uint8_t)(x ) & 0xff;
  39. return 2;
  40. }
  41. static void *export_caca(cucul_canvas_t const *, unsigned long int *);
  42. static void *export_ansi(cucul_canvas_t const *, unsigned long int *);
  43. static void *export_utf8(cucul_canvas_t const *, unsigned long int *, int);
  44. static void *export_html(cucul_canvas_t const *, unsigned long int *);
  45. static void *export_html3(cucul_canvas_t const *, unsigned long int *);
  46. static void *export_bbfr(cucul_canvas_t const *, unsigned long int *);
  47. static void *export_irc(cucul_canvas_t const *, unsigned long int *);
  48. static void *export_ps(cucul_canvas_t const *, unsigned long int *);
  49. static void *export_svg(cucul_canvas_t const *, unsigned long int *);
  50. static void *export_tga(cucul_canvas_t const *, unsigned long int *);
  51. /** \brief Export a canvas into a foreign format.
  52. *
  53. * This function exports a libcucul canvas into various foreign formats such
  54. * as ANSI art, HTML, IRC colours, etc. The returned pointer should be passed
  55. * to free() to release the allocated storage when it is no longer needed.
  56. *
  57. * Valid values for \c format are:
  58. * - \c "caca": export native libcaca files.
  59. * - \c "ansi": export ANSI art (CP437 charset with ANSI colour codes).
  60. * - \c "html": export an HTML page with CSS information.
  61. * - \c "html3": export an HTML table that should be compatible with
  62. * most navigators, including textmode ones.
  63. * - \c "irc": export UTF-8 text with mIRC colour codes.
  64. * - \c "ps": export a PostScript document.
  65. * - \c "svg": export an SVG vector image.
  66. * - \c "tga": export a TGA image.
  67. *
  68. * If an error occurs, NULL is returned and \b errno is set accordingly:
  69. * - \c EINVAL Unsupported format requested.
  70. * - \c ENOMEM Not enough memory to allocate output buffer.
  71. *
  72. * \param cv A libcucul canvas
  73. * \param format A string describing the requested output format.
  74. * \param bytes A pointer to an unsigned long integer where the number of
  75. * allocated bytes will be written.
  76. * \return A pointer to the exported memory area, or NULL in case of error.
  77. */
  78. void *cucul_export_memory(cucul_canvas_t const *cv, char const *format,
  79. unsigned long int *bytes)
  80. {
  81. if(!strcasecmp("caca", format))
  82. return export_caca(cv, bytes);
  83. if(!strcasecmp("ansi", format))
  84. return export_ansi(cv, bytes);
  85. if(!strcasecmp("utf8", format))
  86. return export_utf8(cv, bytes, 0);
  87. if(!strcasecmp("utf8cr", format))
  88. return export_utf8(cv, bytes, 1);
  89. if(!strcasecmp("html", format))
  90. return export_html(cv, bytes);
  91. if(!strcasecmp("html3", format))
  92. return export_html3(cv, bytes);
  93. if(!strcasecmp("bbfr", format))
  94. return export_bbfr(cv, bytes);
  95. if(!strcasecmp("irc", format))
  96. return export_irc(cv, bytes);
  97. if(!strcasecmp("ps", format))
  98. return export_ps(cv, bytes);
  99. if(!strcasecmp("svg", format))
  100. return export_svg(cv, bytes);
  101. if(!strcasecmp("tga", format))
  102. return export_tga(cv, bytes);
  103. seterrno(EINVAL);
  104. return NULL;
  105. }
  106. /** \brief Get available export formats
  107. *
  108. * Return a list of available export formats. The list is a NULL-terminated
  109. * array of strings, interleaving a string containing the internal value for
  110. * the export format, to be used with cucul_export_memory(), and a string
  111. * containing the natural language description for that export format.
  112. *
  113. * This function never fails.
  114. *
  115. * \return An array of strings.
  116. */
  117. char const * const * cucul_get_export_list(void)
  118. {
  119. static char const * const list[] =
  120. {
  121. "caca", "native libcaca format",
  122. "ansi", "ANSI",
  123. "utf8", "UTF-8 with ANSI escape codes",
  124. "utf8cr", "UTF-8 with ANSI escape codes and MS-DOS \\r",
  125. "html", "HTML",
  126. "html3", "backwards-compatible HTML",
  127. "bbfr", "BBCode (French)",
  128. "irc", "IRC with mIRC colours",
  129. "ps", "PostScript document",
  130. "svg", "SVG vector image",
  131. "tga", "TGA image",
  132. NULL, NULL
  133. };
  134. return list;
  135. }
  136. /*
  137. * XXX: the following functions are local.
  138. */
  139. /* Generate a native libcaca canvas file. */
  140. static void *export_caca(cucul_canvas_t const *cv, unsigned long int *bytes)
  141. {
  142. uint32_t *attrs = cv->attrs;
  143. uint32_t *chars = cv->chars;
  144. char *data, *cur;
  145. unsigned int n;
  146. /* 52 bytes for the header:
  147. * - 4 bytes for "\xCA\xCA" + "CV"
  148. * - 16 bytes for the canvas header
  149. * - 32 bytes for the frame info
  150. * 8 bytes for each character cell */
  151. *bytes = 52 + 8 * cv->width * cv->height;
  152. cur = data = malloc(*bytes);
  153. /* magic */
  154. cur += sprintf(cur, "%s", "\xCA\xCA" "CV");
  155. /* canvas_header */
  156. cur += sprintu32(cur, 16 + 32 * 1);
  157. cur += sprintu32(cur, cv->width * cv->height * 8);
  158. cur += sprintu16(cur, 0x0001);
  159. cur += sprintu32(cur, 1);
  160. cur += sprintu16(cur, 0x0000);
  161. /* frame_info */
  162. cur += sprintu32(cur, cv->width);
  163. cur += sprintu32(cur, cv->height);
  164. cur += sprintu32(cur, 0);
  165. cur += sprintu32(cur, cv->curattr);
  166. cur += sprintu32(cur, cv->frames[0].x);
  167. cur += sprintu32(cur, cv->frames[0].y);
  168. cur += sprintu32(cur, cv->frames[0].handlex);
  169. cur += sprintu32(cur, cv->frames[0].handley);
  170. /* canvas_data */
  171. for(n = cv->height * cv->width; n--; )
  172. {
  173. cur += sprintu32(cur, *chars++);
  174. cur += sprintu32(cur, *attrs++);
  175. }
  176. return data;
  177. }
  178. /* Generate UTF-8 representation of current canvas. */
  179. static void *export_utf8(cucul_canvas_t const *cv, unsigned long int *bytes,
  180. int cr)
  181. {
  182. static uint8_t const palette[] =
  183. {
  184. 0, 4, 2, 6, 1, 5, 3, 7,
  185. 8, 12, 10, 14, 9, 13, 11, 15
  186. };
  187. char *data, *cur;
  188. unsigned int x, y;
  189. /* 23 bytes assumed for max length per pixel ('\e[5;1;3x;4y;9x;10ym' plus
  190. * 4 max bytes for a UTF-8 character).
  191. * Add height*9 to that (zeroes color at the end and jump to next line) */
  192. *bytes = (cv->height * 9) + (cv->width * cv->height * 23);
  193. cur = data = malloc(*bytes);
  194. for(y = 0; y < cv->height; y++)
  195. {
  196. uint32_t *lineattr = cv->attrs + y * cv->width;
  197. uint32_t *linechar = cv->chars + y * cv->width;
  198. uint8_t prevfg = 0x10;
  199. uint8_t prevbg = 0x10;
  200. for(x = 0; x < cv->width; x++)
  201. {
  202. uint32_t attr = lineattr[x];
  203. uint32_t ch = linechar[x];
  204. uint8_t ansifg, ansibg, fg, bg;
  205. if(ch == CUCUL_MAGIC_FULLWIDTH)
  206. continue;
  207. ansifg = cucul_attr_to_ansi_fg(attr);
  208. ansibg = cucul_attr_to_ansi_bg(attr);
  209. fg = ansifg < 0x10 ? palette[ansifg] : 0x10;
  210. bg = ansibg < 0x10 ? palette[ansibg] : 0x10;
  211. /* TODO: the [0 could be omitted in some cases */
  212. if(fg != prevfg || bg != prevbg)
  213. {
  214. cur += sprintf(cur, "\033[0");
  215. if(fg < 8)
  216. cur += sprintf(cur, ";3%d", fg);
  217. else if(fg < 16)
  218. cur += sprintf(cur, ";1;3%d;9%d", fg - 8, fg - 8);
  219. if(bg < 8)
  220. cur += sprintf(cur, ";4%d", bg);
  221. else if(bg < 16)
  222. cur += sprintf(cur, ";5;4%d;10%d", bg - 8, bg - 8);
  223. cur += sprintf(cur, "m");
  224. }
  225. cur += cucul_utf32_to_utf8(cur, ch);
  226. prevfg = fg;
  227. prevbg = bg;
  228. }
  229. if(prevfg != 0x10 || prevbg != 0x10)
  230. cur += sprintf(cur, "\033[0m");
  231. cur += sprintf(cur, cr ? "\r\n" : "\n");
  232. }
  233. /* Crop to really used size */
  234. debug("utf8 export: alloc %lu bytes, realloc %lu",
  235. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  236. *bytes = (uintptr_t)(cur - data);
  237. data = realloc(data, *bytes);
  238. return data;
  239. }
  240. /* Generate ANSI representation of current canvas. */
  241. static void *export_ansi(cucul_canvas_t const *cv, unsigned long int *bytes)
  242. {
  243. static uint8_t const palette[] =
  244. {
  245. 0, 4, 2, 6, 1, 5, 3, 7,
  246. 8, 12, 10, 14, 9, 13, 11, 15
  247. };
  248. char *data, *cur;
  249. unsigned int x, y;
  250. uint8_t prevfg = -1;
  251. uint8_t prevbg = -1;
  252. /* 16 bytes assumed for max length per pixel ('\e[5;1;3x;4ym' plus
  253. * 1 byte for a CP437 character).
  254. * Add height*9 to that (zeroes color at the end and jump to next line) */
  255. *bytes = (cv->height * 9) + (cv->width * cv->height * 16);
  256. cur = data = malloc(*bytes);
  257. for(y = 0; y < cv->height; y++)
  258. {
  259. uint32_t *lineattr = cv->attrs + y * cv->width;
  260. uint32_t *linechar = cv->chars + y * cv->width;
  261. for(x = 0; x < cv->width; x++)
  262. {
  263. uint8_t ansifg = cucul_attr_to_ansi_fg(lineattr[x]);
  264. uint8_t ansibg = cucul_attr_to_ansi_bg(lineattr[x]);
  265. uint8_t fg = ansifg < 0x10 ? palette[ansifg] : CUCUL_LIGHTGRAY;
  266. uint8_t bg = ansibg < 0x10 ? palette[ansibg] : CUCUL_BLACK;
  267. uint32_t ch = linechar[x];
  268. if(ch == CUCUL_MAGIC_FULLWIDTH)
  269. ch = '?';
  270. if(fg != prevfg || bg != prevbg)
  271. {
  272. cur += sprintf(cur, "\033[0;");
  273. if(fg < 8)
  274. if(bg < 8)
  275. cur += sprintf(cur, "3%d;4%dm", fg, bg);
  276. else
  277. cur += sprintf(cur, "5;3%d;4%dm", fg, bg - 8);
  278. else
  279. if(bg < 8)
  280. cur += sprintf(cur, "1;3%d;4%dm", fg - 8, bg);
  281. else
  282. cur += sprintf(cur, "5;1;3%d;4%dm", fg - 8, bg - 8);
  283. }
  284. *cur++ = cucul_utf32_to_cp437(ch);
  285. prevfg = fg;
  286. prevbg = bg;
  287. }
  288. if(cv->width == 80)
  289. {
  290. cur += sprintf(cur, "\033[s\n\033[u");
  291. }
  292. else
  293. {
  294. cur += sprintf(cur, "\033[0m\r\n");
  295. prevfg = -1;
  296. prevbg = -1;
  297. }
  298. }
  299. /* Crop to really used size */
  300. debug("ansi export: alloc %lu bytes, realloc %lu",
  301. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  302. *bytes = (uintptr_t)(cur - data);
  303. data = realloc(data, *bytes);
  304. return data;
  305. }
  306. /* Generate HTML representation of current canvas. */
  307. static void *export_html(cucul_canvas_t const *cv, unsigned long int *bytes)
  308. {
  309. char *data, *cur;
  310. unsigned int x, y, len;
  311. /* The HTML header: less than 1000 bytes
  312. * A line: 7 chars for "<br />\n"
  313. * A glyph: 47 chars for "<span style="color:#xxx;background-color:#xxx">"
  314. * 83 chars for ";font-weight..."
  315. * up to 9 chars for "&#xxxxxx;", far less for pure ASCII
  316. * 7 chars for "</span>" */
  317. *bytes = 1000 + cv->height * (7 + cv->width * (47 + 83 + 9 + 7));
  318. cur = data = malloc(*bytes);
  319. /* HTML header */
  320. cur += sprintf(cur, "<html><head>\n");
  321. cur += sprintf(cur, "<title>Generated by libcaca %s</title>\n",
  322. cucul_get_version());
  323. cur += sprintf(cur, "</head><body>\n");
  324. cur += sprintf(cur, "<div cellpadding='0' cellspacing='0' style='%s'>\n",
  325. "font-family: monospace, fixed; font-weight: bold;");
  326. for(y = 0; y < cv->height; y++)
  327. {
  328. uint32_t *lineattr = cv->attrs + y * cv->width;
  329. uint32_t *linechar = cv->chars + y * cv->width;
  330. for(x = 0; x < cv->width; x += len)
  331. {
  332. cur += sprintf(cur, "<span style=\"");
  333. if(cucul_attr_to_ansi_fg(lineattr[x]) < 0x10)
  334. cur += sprintf(cur, ";color:#%.03x",
  335. cucul_attr_to_rgb12_fg(lineattr[x]));
  336. if(cucul_attr_to_ansi_bg(lineattr[x]) < 0x10)
  337. cur += sprintf(cur, ";background-color:#%.03x",
  338. cucul_attr_to_rgb12_bg(lineattr[x]));
  339. if(lineattr[x] & CUCUL_BOLD)
  340. cur += sprintf(cur, ";font-weight:bold");
  341. if(lineattr[x] & CUCUL_ITALICS)
  342. cur += sprintf(cur, ";font-style:italic");
  343. if(lineattr[x] & CUCUL_UNDERLINE)
  344. cur += sprintf(cur, ";text-decoration:underline");
  345. if(lineattr[x] & CUCUL_BLINK)
  346. cur += sprintf(cur, ";text-decoration:blink");
  347. cur += sprintf(cur, "\">");
  348. for(len = 0;
  349. x + len < cv->width && lineattr[x + len] == lineattr[x];
  350. len++)
  351. {
  352. if(linechar[x + len] == CUCUL_MAGIC_FULLWIDTH)
  353. ;
  354. else if(linechar[x + len] <= 0x00000020)
  355. cur += sprintf(cur, "&nbsp;");
  356. else if(linechar[x + len] < 0x00000080)
  357. cur += sprintf(cur, "%c",
  358. (unsigned char)linechar[x + len]);
  359. else
  360. cur += sprintf(cur, "&#%i;",
  361. (unsigned int)linechar[x + len]);
  362. }
  363. cur += sprintf(cur, "</span>");
  364. }
  365. /* New line */
  366. cur += sprintf(cur, "<br />\n");
  367. }
  368. cur += sprintf(cur, "</div></body></html>\n");
  369. /* Crop to really used size */
  370. debug("html export: alloc %lu bytes, realloc %lu",
  371. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  372. *bytes = (uintptr_t)(cur - data);
  373. data = realloc(data, *bytes);
  374. return data;
  375. }
  376. /* Export an HTML3 document. This function is way bigger than export_html(),
  377. * but permits viewing in old browsers (or limited ones such as links). It
  378. * will not work under gecko (mozilla rendering engine) unless you set a
  379. * correct header. */
  380. static void *export_html3(cucul_canvas_t const *cv, unsigned long int *bytes)
  381. {
  382. char *data, *cur;
  383. unsigned int x, y, len;
  384. /* The HTML table markup: less than 1000 bytes
  385. * A line: 10 chars for "<tr></tr>\n"
  386. * A glyph: 40 chars for "<td bgcolor=#xxxxxx><font color=#xxxxxx>"
  387. * up to 36 chars for "<b><i><u><blink></blink></u></i></b>"
  388. * up to 9 chars for "&#xxxxxx;", far less for pure ASCII
  389. * 12 chars for "</font></td>" */
  390. *bytes = 1000 + cv->height * (10 + cv->width * (40 + 36 + 9 + 12));
  391. cur = data = malloc(*bytes);
  392. /* Table */
  393. cur += sprintf(cur, "<table cols='%d' cellpadding='0' cellspacing='0'>\n",
  394. cv->height);
  395. for(y = 0; y < cv->height; y++)
  396. {
  397. uint32_t *lineattr = cv->attrs + y * cv->width;
  398. uint32_t *linechar = cv->chars + y * cv->width;
  399. cur += sprintf(cur, "<tr>");
  400. for(x = 0; x < cv->width; x += len)
  401. {
  402. unsigned int i, needfont;
  403. /* Use colspan option to factor cells with same attributes
  404. * (see below) */
  405. len = 1;
  406. while(x + len < cv->width && lineattr[x + len] == lineattr[x])
  407. len++;
  408. cur += sprintf(cur, "<td");
  409. if(cucul_attr_to_ansi_fg(lineattr[x]) < 0x10)
  410. cur += sprintf(cur, " bgcolor=#%.06lx", (unsigned long int)
  411. _cucul_attr_to_rgb24bg(lineattr[x]));
  412. if(len > 1)
  413. cur += sprintf(cur, " colspan=%d", len);
  414. cur += sprintf(cur, ">");
  415. needfont = cucul_attr_to_ansi_bg(lineattr[x]) < 0x10;
  416. if(needfont)
  417. cur += sprintf(cur, "<font color=#%.06lx>", (unsigned long int)
  418. _cucul_attr_to_rgb24fg(lineattr[x]));
  419. if(lineattr[x] & CUCUL_BOLD)
  420. cur += sprintf(cur, "<b>");
  421. if(lineattr[x] & CUCUL_ITALICS)
  422. cur += sprintf(cur, "<i>");
  423. if(lineattr[x] & CUCUL_UNDERLINE)
  424. cur += sprintf(cur, "<u>");
  425. if(lineattr[x] & CUCUL_BLINK)
  426. cur += sprintf(cur, "<blink>");
  427. for(i = 0; i < len; i++)
  428. {
  429. if(linechar[x + i] == CUCUL_MAGIC_FULLWIDTH)
  430. ;
  431. else if(linechar[x + i] <= 0x00000020)
  432. cur += sprintf(cur, "&nbsp;");
  433. else if(linechar[x + i] < 0x00000080)
  434. cur += sprintf(cur, "%c", (unsigned char)linechar[x + i]);
  435. else
  436. cur += sprintf(cur, "&#%i;", (unsigned int)linechar[x + i]);
  437. }
  438. if(lineattr[x] & CUCUL_BLINK)
  439. cur += sprintf(cur, "</blink>");
  440. if(lineattr[x] & CUCUL_UNDERLINE)
  441. cur += sprintf(cur, "</u>");
  442. if(lineattr[x] & CUCUL_ITALICS)
  443. cur += sprintf(cur, "</i>");
  444. if(lineattr[x] & CUCUL_BOLD)
  445. cur += sprintf(cur, "</b>");
  446. if(needfont)
  447. cur += sprintf(cur, "</font>");
  448. cur += sprintf(cur, "</td>");
  449. }
  450. cur += sprintf(cur, "</tr>\n");
  451. }
  452. /* Footer */
  453. cur += sprintf(cur, "</table>\n");
  454. /* Crop to really used size */
  455. debug("html3 export: alloc %lu bytes, realloc %lu",
  456. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  457. *bytes = (uintptr_t)(cur - data);
  458. data = realloc(data, *bytes);
  459. return data;
  460. }
  461. static void *export_bbfr(cucul_canvas_t const *cv, unsigned long int *bytes)
  462. {
  463. char *data, *cur;
  464. unsigned int x, y, len;
  465. /* The font markup: less than 100 bytes
  466. * A line: 1 char for "\n"
  467. * A glyph: 22 chars for "[f=#xxxxxx][c=#xxxxxx]"
  468. * up to 21 chars for "[g][i][s][/s][/i][/g]"
  469. * up to 6 chars for the UTF-8 glyph
  470. * 8 chars for "[/c][/f]" */
  471. *bytes = 100 + cv->height * (1 + cv->width * (22 + 21 + 6 + 8));
  472. cur = data = malloc(*bytes);
  473. /* Table */
  474. cur += sprintf(cur, "[font=Courier New]");
  475. for(y = 0; y < cv->height; y++)
  476. {
  477. uint32_t *lineattr = cv->attrs + y * cv->width;
  478. uint32_t *linechar = cv->chars + y * cv->width;
  479. for(x = 0; x < cv->width; x += len)
  480. {
  481. unsigned int i, needback, needfront;
  482. /* Use colspan option to factor cells with same attributes
  483. * (see below) */
  484. len = 1;
  485. if(linechar[x] == ' ')
  486. while(x + len < cv->width && lineattr[x + len] == lineattr[x]
  487. && linechar[x] == ' ')
  488. len++;
  489. else
  490. while(x + len < cv->width && lineattr[x + len] == lineattr[x]
  491. && linechar[x] != ' ')
  492. len++;
  493. needback = cucul_attr_to_ansi_bg(lineattr[x]) < 0x10;
  494. needfront = cucul_attr_to_ansi_fg(lineattr[x]) < 0x10;
  495. if(needback)
  496. cur += sprintf(cur, "[f=#%.06lx]", (unsigned long int)
  497. _cucul_attr_to_rgb24bg(lineattr[x]));
  498. if(linechar[x] == ' ')
  499. cur += sprintf(cur, "[c=#%.06lx]", (unsigned long int)
  500. _cucul_attr_to_rgb24bg(lineattr[x]));
  501. else if(needfront)
  502. cur += sprintf(cur, "[c=#%.06lx]", (unsigned long int)
  503. _cucul_attr_to_rgb24fg(lineattr[x]));
  504. if(lineattr[x] & CUCUL_BOLD)
  505. cur += sprintf(cur, "[g]");
  506. if(lineattr[x] & CUCUL_ITALICS)
  507. cur += sprintf(cur, "[i]");
  508. if(lineattr[x] & CUCUL_UNDERLINE)
  509. cur += sprintf(cur, "[s]");
  510. if(lineattr[x] & CUCUL_BLINK)
  511. ; /* FIXME */
  512. for(i = 0; i < len; i++)
  513. {
  514. if(linechar[x + i] == CUCUL_MAGIC_FULLWIDTH)
  515. ;
  516. else if(linechar[x + i] == ' ')
  517. *cur++ = '_';
  518. else
  519. cur += cucul_utf32_to_utf8(cur, linechar[x + i]);
  520. }
  521. if(lineattr[x] & CUCUL_BLINK)
  522. ; /* FIXME */
  523. if(lineattr[x] & CUCUL_UNDERLINE)
  524. cur += sprintf(cur, "[/s]");
  525. if(lineattr[x] & CUCUL_ITALICS)
  526. cur += sprintf(cur, "[/i]");
  527. if(lineattr[x] & CUCUL_BOLD)
  528. cur += sprintf(cur, "[/g]");
  529. if(linechar[x] == ' ' || needfront)
  530. cur += sprintf(cur, "[/c]");
  531. if(needback)
  532. cur += sprintf(cur, "[/f]");
  533. }
  534. cur += sprintf(cur, "\n");
  535. }
  536. /* Footer */
  537. cur += sprintf(cur, "[/font]\n");
  538. /* Crop to really used size */
  539. debug("bbfr export: alloc %lu bytes, realloc %lu",
  540. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  541. *bytes = (uintptr_t)(cur - data);
  542. data = realloc(data, *bytes);
  543. return data;
  544. }
  545. /* Export a text file with IRC colours */
  546. static void *export_irc(cucul_canvas_t const *cv, unsigned long int *bytes)
  547. {
  548. static uint8_t const palette[] =
  549. {
  550. 1, 2, 3, 10, 5, 6, 7, 15, /* Dark */
  551. 14, 12, 9, 11, 4, 13, 8, 0, /* Light */
  552. };
  553. char *data, *cur;
  554. unsigned int x, y;
  555. /* 14 bytes assumed for max length per pixel. Worst case scenario:
  556. * ^Cxx,yy 6 bytes
  557. * ^B^B 2 bytes
  558. * ch 6 bytes
  559. * 3 bytes for max length per line. Worst case scenario:
  560. * <spc> 1 byte (for empty lines)
  561. * \r\n 2 bytes
  562. * In real life, the average bytes per pixel value will be around 5.
  563. */
  564. *bytes = 2 + cv->height * (3 + cv->width * 14);
  565. cur = data = malloc(*bytes);
  566. for(y = 0; y < cv->height; y++)
  567. {
  568. uint32_t *lineattr = cv->attrs + y * cv->width;
  569. uint32_t *linechar = cv->chars + y * cv->width;
  570. uint8_t prevfg = 0x10;
  571. uint8_t prevbg = 0x10;
  572. for(x = 0; x < cv->width; x++)
  573. {
  574. uint32_t attr = lineattr[x];
  575. uint32_t ch = linechar[x];
  576. uint8_t ansifg, ansibg, fg, bg;
  577. if(ch == CUCUL_MAGIC_FULLWIDTH)
  578. continue;
  579. ansifg = cucul_attr_to_ansi_fg(attr);
  580. ansibg = cucul_attr_to_ansi_bg(attr);
  581. fg = ansifg < 0x10 ? palette[ansifg] : 0x10;
  582. bg = ansibg < 0x10 ? palette[ansibg] : 0x10;
  583. /* TODO: optimise series of same fg / same bg
  584. * don't change fg value if ch == ' '
  585. * make sure the \x03,%d trick works everywhere */
  586. if(bg != prevbg || fg != prevfg)
  587. {
  588. int need_escape = 0;
  589. if(bg == 0x10)
  590. {
  591. if(fg == 0x10)
  592. cur += sprintf(cur, "\x0f");
  593. else
  594. {
  595. if(prevbg == 0x10)
  596. cur += sprintf(cur, "\x03%d", fg);
  597. else
  598. cur += sprintf(cur, "\x0f\x03%d", fg);
  599. if(ch == (uint32_t)',')
  600. need_escape = 1;
  601. }
  602. }
  603. else
  604. {
  605. if(fg == 0x10)
  606. cur += sprintf(cur, "\x0f\x03,%d", bg);
  607. else
  608. cur += sprintf(cur, "\x03%d,%d", fg, bg);
  609. }
  610. if(ch >= (uint32_t)'0' && ch <= (uint32_t)'9')
  611. need_escape = 1;
  612. if(need_escape)
  613. cur += sprintf(cur, "\x02\x02");
  614. }
  615. cur += cucul_utf32_to_utf8(cur, ch);
  616. prevfg = fg;
  617. prevbg = bg;
  618. }
  619. /* TODO: do the same the day we optimise whole lines above */
  620. if(!cv->width)
  621. *cur++ = ' ';
  622. *cur++ = '\r';
  623. *cur++ = '\n';
  624. }
  625. /* Crop to really used size */
  626. debug("IRC export: alloc %lu bytes, realloc %lu",
  627. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  628. *bytes = (uintptr_t)(cur - data);
  629. data = realloc(data, *bytes);
  630. return data;
  631. }
  632. /* Export a PostScript document. */
  633. static void *export_ps(cucul_canvas_t const *cv, unsigned long int *bytes)
  634. {
  635. static char const *ps_header =
  636. "%!\n"
  637. "%% libcaca PDF export\n"
  638. "%%LanguageLevel: 2\n"
  639. "%%Pages: 1\n"
  640. "%%DocumentData: Clean7Bit\n"
  641. "/csquare {\n"
  642. " newpath\n"
  643. " 0 0 moveto\n"
  644. " 0 1 rlineto\n"
  645. " 1 0 rlineto\n"
  646. " 0 -1 rlineto\n"
  647. " closepath\n"
  648. " setrgbcolor\n"
  649. " fill\n"
  650. "} def\n"
  651. "/S {\n"
  652. " Show\n"
  653. "} bind def\n"
  654. "/Courier-Bold findfont\n"
  655. "8 scalefont\n"
  656. "setfont\n"
  657. "gsave\n"
  658. "6 10 scale\n";
  659. char *data, *cur;
  660. unsigned int x, y;
  661. /* 200 is arbitrary but should be ok */
  662. *bytes = strlen(ps_header) + 100 + cv->height * (32 + cv->width * 200);
  663. cur = data = malloc(*bytes);
  664. /* Header */
  665. cur += sprintf(cur, "%s", ps_header);
  666. cur += sprintf(cur, "0 %d translate\n", cv->height);
  667. /* Background, drawn using csquare macro defined in header */
  668. for(y = cv->height; y--; )
  669. {
  670. uint32_t *lineattr = cv->attrs + y * cv->width;
  671. for(x = 0; x < cv->width; x++)
  672. {
  673. uint8_t argb[8];
  674. cucul_attr_to_argb64(*lineattr++, argb);
  675. cur += sprintf(cur, "1 0 translate\n %f %f %f csquare\n",
  676. (float)argb[1] * (1.0 / 0xf),
  677. (float)argb[2] * (1.0 / 0xf),
  678. (float)argb[3] * (1.0 / 0xf));
  679. }
  680. /* Return to beginning of the line, and jump to the next one */
  681. cur += sprintf(cur, "-%d 1 translate\n", cv->width);
  682. }
  683. cur += sprintf(cur, "grestore\n"); /* Restore transformation matrix */
  684. cur += sprintf(cur, "0 %d translate\n", cv->height*10);
  685. for(y = cv->height; y--; )
  686. {
  687. uint32_t *lineattr = cv->attrs + (cv->height - y - 1) * cv->width;
  688. uint32_t *linechar = cv->chars + (cv->height - y - 1) * cv->width;
  689. for(x = 0; x < cv->width; x++)
  690. {
  691. uint8_t argb[8];
  692. uint32_t ch = *linechar++;
  693. cucul_attr_to_argb64(*lineattr++, argb);
  694. cur += sprintf(cur, "newpath\n");
  695. cur += sprintf(cur, "%d %d moveto\n", (x + 1) * 6, y * 10 + 2);
  696. cur += sprintf(cur, "%f %f %f setrgbcolor\n",
  697. (float)argb[5] * (1.0 / 0xf),
  698. (float)argb[6] * (1.0 / 0xf),
  699. (float)argb[7] * (1.0 / 0xf));
  700. if(ch < 0x00000020)
  701. cur += sprintf(cur, "(?) show\n");
  702. else if(ch >= 0x00000080)
  703. cur += sprintf(cur, "(?) show\n");
  704. else switch((uint8_t)(ch & 0x7f))
  705. {
  706. case '\\':
  707. case '(':
  708. case ')':
  709. cur += sprintf(cur, "(\\%c) show\n", (unsigned char)ch);
  710. break;
  711. default:
  712. cur += sprintf(cur, "(%c) show\n", (unsigned char)ch);
  713. break;
  714. }
  715. }
  716. }
  717. cur += sprintf(cur, "showpage\n");
  718. /* Crop to really used size */
  719. debug("PS export: alloc %lu bytes, realloc %lu",
  720. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  721. *bytes = (uintptr_t)(cur - data);
  722. data = realloc(data, *bytes);
  723. return data;
  724. }
  725. /* Export an SVG vector image */
  726. static void *export_svg(cucul_canvas_t const *cv, unsigned long int *bytes)
  727. {
  728. static char const svg_header[] =
  729. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  730. "<svg width=\"%d\" height=\"%d\" viewBox=\"0 0 %d %d\""
  731. " xmlns=\"http://www.w3.org/2000/svg\""
  732. " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
  733. " xml:space=\"preserve\" version=\"1.1\" baseProfile=\"full\">\n";
  734. char *data, *cur;
  735. unsigned int x, y;
  736. /* 200 is arbitrary but should be ok */
  737. *bytes = strlen(svg_header) + 128 + cv->width * cv->height * 200;
  738. cur = data = malloc(*bytes);
  739. /* Header */
  740. cur += sprintf(cur, svg_header, cv->width * 6, cv->height * 10,
  741. cv->width * 6, cv->height * 10);
  742. cur += sprintf(cur, " <g id=\"mainlayer\" font-size=\"10\""
  743. " style=\"font-family: monospace\">\n");
  744. /* Background */
  745. for(y = 0; y < cv->height; y++)
  746. {
  747. uint32_t *lineattr = cv->attrs + y * cv->width;
  748. for(x = 0; x < cv->width; x++)
  749. {
  750. cur += sprintf(cur, "<rect style=\"fill:#%.03x\" x=\"%d\" y=\"%d\""
  751. " width=\"6\" height=\"10\"/>\n",
  752. cucul_attr_to_rgb12_bg(*lineattr++),
  753. x * 6, y * 10);
  754. }
  755. }
  756. /* Text */
  757. for(y = 0; y < cv->height; y++)
  758. {
  759. uint32_t *lineattr = cv->attrs + y * cv->width;
  760. uint32_t *linechar = cv->chars + y * cv->width;
  761. for(x = 0; x < cv->width; x++)
  762. {
  763. uint32_t ch = *linechar++;
  764. if(ch == ' ' || ch == CUCUL_MAGIC_FULLWIDTH)
  765. {
  766. lineattr++;
  767. continue;
  768. }
  769. cur += sprintf(cur, "<text style=\"fill:#%.03x\" "
  770. "x=\"%d\" y=\"%d\">",
  771. cucul_attr_to_rgb12_fg(*lineattr++),
  772. x * 6, (y * 10) + 8);
  773. if(ch < 0x00000020)
  774. *cur++ = '?';
  775. else if(ch > 0x0000007f)
  776. cur += cucul_utf32_to_utf8(cur, ch);
  777. else switch((uint8_t)ch)
  778. {
  779. case '>': cur += sprintf(cur, "&gt;"); break;
  780. case '<': cur += sprintf(cur, "&lt;"); break;
  781. case '&': cur += sprintf(cur, "&amp;"); break;
  782. default: *cur++ = ch; break;
  783. }
  784. cur += sprintf(cur, "</text>\n");
  785. }
  786. }
  787. cur += sprintf(cur, " </g>\n");
  788. cur += sprintf(cur, "</svg>\n");
  789. /* Crop to really used size */
  790. debug("SVG export: alloc %lu bytes, realloc %lu",
  791. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  792. *bytes = (uintptr_t)(cur - data);
  793. data = realloc(data, *bytes);
  794. return data;
  795. }
  796. /* Export a TGA image */
  797. static void *export_tga(cucul_canvas_t const *cv, unsigned long int *bytes)
  798. {
  799. char const * const *fontlist;
  800. char *data, *cur;
  801. cucul_font_t *f;
  802. unsigned int i, w, h;
  803. fontlist = cucul_get_font_list();
  804. if(!fontlist[0])
  805. {
  806. seterrno(EINVAL);
  807. return NULL;
  808. }
  809. f = cucul_load_font(fontlist[0], 0);
  810. w = cucul_get_canvas_width(cv) * cucul_get_font_width(f);
  811. h = cucul_get_canvas_height(cv) * cucul_get_font_height(f);
  812. *bytes = w * h * 4 + 18; /* 32 bpp + 18 bytes for the header */
  813. cur = data = malloc(*bytes);
  814. /* ID Length */
  815. cur += sprintf(cur, "%c", 0);
  816. /* Color Map Type: no colormap */
  817. cur += sprintf(cur, "%c", 0);
  818. /* Image Type: uncompressed truecolor */
  819. cur += sprintf(cur, "%c", 2);
  820. /* Color Map Specification: no color map */
  821. memset(cur, 0, 5); cur += 5;
  822. /* Image Specification */
  823. cur += sprintf(cur, "%c%c", 0, 0); /* X Origin */
  824. cur += sprintf(cur, "%c%c", 0, 0); /* Y Origin */
  825. cur += sprintf(cur, "%c%c", w & 0xff, w >> 8); /* Width */
  826. cur += sprintf(cur, "%c%c", h & 0xff, h >> 8); /* Height */
  827. cur += sprintf(cur, "%c", 32); /* Pixel Depth */
  828. cur += sprintf(cur, "%c", 40); /* Image Descriptor */
  829. /* Image ID: no ID */
  830. /* Color Map Data: no colormap */
  831. /* Image Data */
  832. cucul_render_canvas(cv, f, cur, w, h, 4 * w);
  833. /* Swap bytes. What a waste of time. */
  834. for(i = 0; i < w * h * 4; i += 4)
  835. {
  836. char c;
  837. c = cur[i]; cur[i] = cur[i + 3]; cur[i + 3] = c;
  838. c = cur[i + 1]; cur[i + 1] = cur[i + 2]; cur[i + 2] = c;
  839. }
  840. cucul_free_font(f);
  841. return data;
  842. }