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.
 
 
 
 
 
 

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