Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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