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.

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