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.

export.c 28 KiB

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