Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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