Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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