Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

export.c 34 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. /*
  2. * libcaca Colour ASCII-Art library
  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. #if !defined(__KERNEL__)
  20. # include <stdlib.h>
  21. # include <stdio.h>
  22. # include <string.h>
  23. #endif
  24. #include "caca.h"
  25. #include "caca_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(caca_canvas_t const *, size_t *);
  41. static void *export_ansi(caca_canvas_t const *, size_t *);
  42. static void *export_utf8(caca_canvas_t const *, size_t *, int);
  43. static void *export_html(caca_canvas_t const *, size_t *);
  44. static void *export_html3(caca_canvas_t const *, size_t *);
  45. static void *export_bbfr(caca_canvas_t const *, size_t *);
  46. static void *export_irc(caca_canvas_t const *, size_t *);
  47. static void *export_ps(caca_canvas_t const *, size_t *);
  48. static void *export_svg(caca_canvas_t const *, size_t *);
  49. static void *export_tga(caca_canvas_t const *, size_t *);
  50. /** \brief Export a canvas into a foreign format.
  51. *
  52. * This function exports a libcaca 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 libcaca canvas
  72. * \param format A string describing the requested output format.
  73. * \param bytes A pointer to a size_t where the number of allocated bytes
  74. * will be written.
  75. * \return A pointer to the exported memory area, or NULL in case of error.
  76. */
  77. void *caca_export_memory(caca_canvas_t const *cv, char const *format,
  78. size_t *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("bbfr", format))
  93. return export_bbfr(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. seterrno(EINVAL);
  103. return NULL;
  104. }
  105. /** \brief Get available export formats
  106. *
  107. * Return a list of available export formats. The list is a NULL-terminated
  108. * array of strings, interleaving a string containing the internal value for
  109. * the export format, to be used with caca_export_memory(), and a string
  110. * containing the natural language description for that export format.
  111. *
  112. * This function never fails.
  113. *
  114. * \return An array of strings.
  115. */
  116. char const * const * caca_get_export_list(void)
  117. {
  118. static char const * const list[] =
  119. {
  120. "caca", "native libcaca format",
  121. "ansi", "ANSI",
  122. "utf8", "UTF-8 with ANSI escape codes",
  123. "utf8cr", "UTF-8 with ANSI escape codes and MS-DOS \\r",
  124. "html", "HTML",
  125. "html3", "backwards-compatible HTML",
  126. "bbfr", "BBCode (French)",
  127. "irc", "IRC with mIRC colours",
  128. "ps", "PostScript document",
  129. "svg", "SVG vector image",
  130. "tga", "TGA image",
  131. NULL, NULL
  132. };
  133. return list;
  134. }
  135. /*
  136. * XXX: the following functions are local.
  137. */
  138. /* Generate a native libcaca canvas file. */
  139. static void *export_caca(caca_canvas_t const *cv, size_t *bytes)
  140. {
  141. char *data, *cur;
  142. int f, n;
  143. /* 52 bytes for the header:
  144. * - 4 bytes for "\xCA\xCA" + "CV"
  145. * - 16 bytes for the canvas header
  146. * - 32 bytes for the frame info
  147. * 8 bytes for each character cell */
  148. *bytes = 20 + (32 + 8 * cv->width * cv->height) * cv->framecount;
  149. cur = data = malloc(*bytes);
  150. /* magic */
  151. cur += sprintf(cur, "%s", "\xCA\xCA" "CV");
  152. /* canvas_header */
  153. cur += sprintu32(cur, 16 + 32 * cv->framecount);
  154. cur += sprintu32(cur, cv->width * cv->height * 8 * cv->framecount);
  155. cur += sprintu16(cur, 0x0001);
  156. cur += sprintu32(cur, cv->framecount);
  157. cur += sprintu16(cur, 0x0000);
  158. /* frame_info */
  159. for(f = 0; f < cv->framecount; f++)
  160. {
  161. cur += sprintu32(cur, cv->width);
  162. cur += sprintu32(cur, cv->height);
  163. cur += sprintu32(cur, 0);
  164. cur += sprintu32(cur, cv->curattr);
  165. cur += sprintu32(cur, cv->frames[f].x);
  166. cur += sprintu32(cur, cv->frames[f].y);
  167. cur += sprintu32(cur, cv->frames[f].handlex);
  168. cur += sprintu32(cur, cv->frames[f].handley);
  169. }
  170. /* canvas_data */
  171. for(f = 0; f < cv->framecount; f++)
  172. {
  173. uint32_t *attrs = cv->frames[f].attrs;
  174. uint32_t *chars = cv->frames[f].chars;
  175. for(n = cv->height * cv->width; n--; )
  176. {
  177. cur += sprintu32(cur, *chars++);
  178. cur += sprintu32(cur, *attrs++);
  179. }
  180. }
  181. return data;
  182. }
  183. /* Generate UTF-8 representation of current canvas. */
  184. static void *export_utf8(caca_canvas_t const *cv, size_t *bytes,
  185. int cr)
  186. {
  187. static uint8_t const palette[] =
  188. {
  189. 0, 4, 2, 6, 1, 5, 3, 7,
  190. 8, 12, 10, 14, 9, 13, 11, 15
  191. };
  192. char *data, *cur;
  193. int x, y;
  194. /* 23 bytes assumed for max length per pixel ('\e[5;1;3x;4y;9x;10ym' plus
  195. * 4 max bytes for a UTF-8 character).
  196. * Add height*9 to that (zeroes color at the end and jump to next line) */
  197. *bytes = (cv->height * 9) + (cv->width * cv->height * 23);
  198. cur = data = malloc(*bytes);
  199. for(y = 0; y < cv->height; y++)
  200. {
  201. uint32_t *lineattr = cv->attrs + y * cv->width;
  202. uint32_t *linechar = cv->chars + y * cv->width;
  203. uint8_t prevfg = 0x10;
  204. uint8_t prevbg = 0x10;
  205. for(x = 0; x < cv->width; x++)
  206. {
  207. uint32_t attr = lineattr[x];
  208. uint32_t ch = linechar[x];
  209. uint8_t ansifg, ansibg, fg, bg;
  210. if(ch == CACA_MAGIC_FULLWIDTH)
  211. continue;
  212. ansifg = caca_attr_to_ansi_fg(attr);
  213. ansibg = caca_attr_to_ansi_bg(attr);
  214. fg = ansifg < 0x10 ? palette[ansifg] : 0x10;
  215. bg = ansibg < 0x10 ? palette[ansibg] : 0x10;
  216. /* TODO: the [0 could be omitted in some cases */
  217. if(fg != prevfg || bg != prevbg)
  218. {
  219. cur += sprintf(cur, "\033[0");
  220. if(fg < 8)
  221. cur += sprintf(cur, ";3%d", fg);
  222. else if(fg < 16)
  223. cur += sprintf(cur, ";1;3%d;9%d", fg - 8, fg - 8);
  224. if(bg < 8)
  225. cur += sprintf(cur, ";4%d", bg);
  226. else if(bg < 16)
  227. cur += sprintf(cur, ";5;4%d;10%d", bg - 8, bg - 8);
  228. cur += sprintf(cur, "m");
  229. }
  230. cur += caca_utf32_to_utf8(cur, ch);
  231. prevfg = fg;
  232. prevbg = bg;
  233. }
  234. if(prevfg != 0x10 || prevbg != 0x10)
  235. cur += sprintf(cur, "\033[0m");
  236. cur += sprintf(cur, cr ? "\r\n" : "\n");
  237. }
  238. /* Crop to really used size */
  239. debug("utf8 export: alloc %lu bytes, realloc %lu",
  240. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  241. *bytes = (uintptr_t)(cur - data);
  242. data = realloc(data, *bytes);
  243. return data;
  244. }
  245. /* Generate ANSI representation of current canvas. */
  246. static void *export_ansi(caca_canvas_t const *cv, size_t *bytes)
  247. {
  248. static uint8_t const palette[] =
  249. {
  250. 0, 4, 2, 6, 1, 5, 3, 7,
  251. 8, 12, 10, 14, 9, 13, 11, 15
  252. };
  253. char *data, *cur;
  254. int x, y;
  255. uint8_t prevfg = -1;
  256. uint8_t prevbg = -1;
  257. /* 16 bytes assumed for max length per pixel ('\e[5;1;3x;4ym' plus
  258. * 1 byte for a CP437 character).
  259. * Add height*9 to that (zeroes color at the end and jump to next line) */
  260. *bytes = (cv->height * 9) + (cv->width * cv->height * 16);
  261. cur = data = malloc(*bytes);
  262. for(y = 0; y < cv->height; y++)
  263. {
  264. uint32_t *lineattr = cv->attrs + y * cv->width;
  265. uint32_t *linechar = cv->chars + y * cv->width;
  266. for(x = 0; x < cv->width; x++)
  267. {
  268. uint8_t ansifg = caca_attr_to_ansi_fg(lineattr[x]);
  269. uint8_t ansibg = caca_attr_to_ansi_bg(lineattr[x]);
  270. uint8_t fg = ansifg < 0x10 ? palette[ansifg] : CACA_LIGHTGRAY;
  271. uint8_t bg = ansibg < 0x10 ? palette[ansibg] : CACA_BLACK;
  272. uint32_t ch = linechar[x];
  273. if(ch == CACA_MAGIC_FULLWIDTH)
  274. ch = '?';
  275. if(fg != prevfg || bg != prevbg)
  276. {
  277. cur += sprintf(cur, "\033[0;");
  278. if(fg < 8)
  279. if(bg < 8)
  280. cur += sprintf(cur, "3%d;4%dm", fg, bg);
  281. else
  282. cur += sprintf(cur, "5;3%d;4%dm", fg, bg - 8);
  283. else
  284. if(bg < 8)
  285. cur += sprintf(cur, "1;3%d;4%dm", fg - 8, bg);
  286. else
  287. cur += sprintf(cur, "5;1;3%d;4%dm", fg - 8, bg - 8);
  288. }
  289. *cur++ = caca_utf32_to_cp437(ch);
  290. prevfg = fg;
  291. prevbg = bg;
  292. }
  293. if(cv->width == 80)
  294. {
  295. cur += sprintf(cur, "\033[s\n\033[u");
  296. }
  297. else
  298. {
  299. cur += sprintf(cur, "\033[0m\r\n");
  300. prevfg = -1;
  301. prevbg = -1;
  302. }
  303. }
  304. /* Crop to really used size */
  305. debug("ansi export: alloc %lu bytes, realloc %lu",
  306. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  307. *bytes = (uintptr_t)(cur - data);
  308. data = realloc(data, *bytes);
  309. return data;
  310. }
  311. /* Generate HTML representation of current canvas. */
  312. static void *export_html(caca_canvas_t const *cv, size_t *bytes)
  313. {
  314. char *data, *cur;
  315. int x, y, len;
  316. /* The HTML header: less than 1000 bytes
  317. * A line: 7 chars for "<br />\n"
  318. * A glyph: 47 chars for "<span style="color:#xxx;background-color:#xxx">"
  319. * 83 chars for ";font-weight..."
  320. * up to 9 chars for "&#xxxxxx;", far less for pure ASCII
  321. * 7 chars for "</span>" */
  322. *bytes = 1000 + cv->height * (7 + cv->width * (47 + 83 + 9 + 7));
  323. cur = data = malloc(*bytes);
  324. /* HTML header */
  325. cur += sprintf(cur, "<html><head>\n");
  326. cur += sprintf(cur, "<title>Generated by libcaca %s</title>\n",
  327. caca_get_version());
  328. cur += sprintf(cur, "</head><body>\n");
  329. cur += sprintf(cur, "<div cellpadding='0' cellspacing='0' style='%s'>\n",
  330. "font-family: monospace, fixed; font-weight: bold;");
  331. for(y = 0; y < cv->height; y++)
  332. {
  333. uint32_t *lineattr = cv->attrs + y * cv->width;
  334. uint32_t *linechar = cv->chars + y * cv->width;
  335. for(x = 0; x < cv->width; x += len)
  336. {
  337. cur += sprintf(cur, "<span style=\"");
  338. if(caca_attr_to_ansi_fg(lineattr[x]) < 0x10)
  339. cur += sprintf(cur, ";color:#%.03x",
  340. caca_attr_to_rgb12_fg(lineattr[x]));
  341. if(caca_attr_to_ansi_bg(lineattr[x]) < 0x10)
  342. cur += sprintf(cur, ";background-color:#%.03x",
  343. caca_attr_to_rgb12_bg(lineattr[x]));
  344. if(lineattr[x] & CACA_BOLD)
  345. cur += sprintf(cur, ";font-weight:bold");
  346. if(lineattr[x] & CACA_ITALICS)
  347. cur += sprintf(cur, ";font-style:italic");
  348. if(lineattr[x] & CACA_UNDERLINE)
  349. cur += sprintf(cur, ";text-decoration:underline");
  350. if(lineattr[x] & CACA_BLINK)
  351. cur += sprintf(cur, ";text-decoration:blink");
  352. cur += sprintf(cur, "\">");
  353. for(len = 0;
  354. x + len < cv->width && lineattr[x + len] == lineattr[x];
  355. len++)
  356. {
  357. if(linechar[x + len] == CACA_MAGIC_FULLWIDTH)
  358. ;
  359. else if(linechar[x + len] <= 0x00000020)
  360. cur += sprintf(cur, "&nbsp;");
  361. else if(linechar[x + len] < 0x00000080)
  362. cur += sprintf(cur, "%c", (uint8_t)linechar[x + len]);
  363. else
  364. cur += sprintf(cur, "&#%i;",
  365. (unsigned int)linechar[x + len]);
  366. }
  367. cur += sprintf(cur, "</span>");
  368. }
  369. /* New line */
  370. cur += sprintf(cur, "<br />\n");
  371. }
  372. cur += sprintf(cur, "</div></body></html>\n");
  373. /* Crop to really used size */
  374. debug("html export: alloc %lu bytes, realloc %lu",
  375. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  376. *bytes = (uintptr_t)(cur - data);
  377. data = realloc(data, *bytes);
  378. return data;
  379. }
  380. /* Export an HTML3 document. This function is way bigger than export_html(),
  381. * but permits viewing in old browsers (or limited ones such as links). It
  382. * will not work under gecko (mozilla rendering engine) unless you set a
  383. * correct header. */
  384. static void *export_html3(caca_canvas_t const *cv, size_t *bytes)
  385. {
  386. char *data, *cur;
  387. int x, y, len;
  388. int maxcols;
  389. /* Table */
  390. maxcols = 0;
  391. for(y = 0; y < cv->height; y++)
  392. {
  393. uint32_t *linechar = cv->chars + y * cv->width;
  394. int cols = 0;
  395. for(x = 0; x < cv->width; x++)
  396. {
  397. if(linechar[x] == 0x00000009)
  398. while((cols + 1) % 8)
  399. cols ++;
  400. cols ++;
  401. }
  402. if (cols > maxcols)
  403. maxcols = cols;
  404. }
  405. /* The HTML table markup: less than 1000 bytes
  406. * A line: 10 chars for "<tr></tr>\n"
  407. * A glyph: up to 44 chars for "<td bgcolor=\"#xxxxxx\"><font color=\"#xxxxxx\">"
  408. * up to 45 chars for "<tt><b><i><u><blink></blink></u></i></b></tt>"
  409. * up to 9 chars for "&#xxxxxx;" (far less for pure ASCII)
  410. * 12 chars for "</font></td>" */
  411. *bytes = 1000 + cv->height * (10 + maxcols * (44 + 45 + 9 + 12));
  412. cur = data = malloc(*bytes);
  413. cur += sprintf(cur, "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n");
  414. for(y = 0; y < cv->height; y++)
  415. {
  416. uint32_t *lineattr = cv->attrs + y * cv->width;
  417. uint32_t *linechar = cv->chars + y * cv->width;
  418. int taboff = 0;
  419. cur += sprintf(cur, "<tr>");
  420. for(x = 0; x < cv->width; x += len)
  421. {
  422. int i, needfont;
  423. int thistab = 0;
  424. /* Use colspan option to factor cells with same attributes
  425. * (see below) */
  426. len = 1;
  427. while((x + len < cv->width) && (lineattr[x + len] == lineattr[x]))
  428. len++;
  429. for(i = 0; i < len; i++)
  430. if(linechar[x + i] == 0x00000009)
  431. while((x + i + taboff + thistab + 1) % 8)
  432. thistab ++;
  433. cur += sprintf(cur, "<td");
  434. if(caca_attr_to_ansi_bg(lineattr[x]) < 0x10)
  435. cur += sprintf(cur, " bgcolor=\"#%.06lx\"", (unsigned long int)
  436. _caca_attr_to_rgb24bg(lineattr[x]));
  437. if((len + thistab) > 1)
  438. cur += sprintf(cur, " colspan=\"%d\"", len + thistab);
  439. cur += sprintf(cur, ">");
  440. needfont = caca_attr_to_ansi_fg(lineattr[x]) < 0x10;
  441. if(needfont)
  442. cur += sprintf(cur, "<font color=\"#%.06lx\">", (unsigned long int)
  443. _caca_attr_to_rgb24fg(lineattr[x]));
  444. cur += sprintf(cur, "<tt>");
  445. if(lineattr[x] & CACA_BOLD)
  446. cur += sprintf(cur, "<b>");
  447. if(lineattr[x] & CACA_ITALICS)
  448. cur += sprintf(cur, "<i>");
  449. if(lineattr[x] & CACA_UNDERLINE)
  450. cur += sprintf(cur, "<u>");
  451. if(lineattr[x] & CACA_BLINK)
  452. cur += sprintf(cur, "<blink>");
  453. for(i = 0; i < len; i++)
  454. {
  455. if(linechar[x + i] == CACA_MAGIC_FULLWIDTH)
  456. ;
  457. else if(linechar[x + i] <= 0x00000020)
  458. {
  459. if(linechar[x + i] == 0x00000009)
  460. {
  461. while((x + i + taboff + 1) % 8)
  462. {
  463. cur += sprintf(cur, "&nbsp;");
  464. taboff ++;
  465. }
  466. }
  467. cur += sprintf(cur, "&nbsp;");
  468. }
  469. else if(linechar[x + i] == '&')
  470. cur += sprintf(cur, "&amp;");
  471. else if(linechar[x + i] == '<')
  472. cur += sprintf(cur, "&lt;");
  473. else if(linechar[x + i] == '>')
  474. cur += sprintf(cur, "&gt;");
  475. else if(linechar[x + i] == '\"')
  476. cur += sprintf(cur, "&quot;");
  477. else if(linechar[x + i] == '\'')
  478. cur += sprintf(cur, "&#39;");
  479. else if(linechar[x + i] < 0x00000080)
  480. cur += sprintf(cur, "%c", (uint8_t)linechar[x + i]);
  481. else
  482. cur += sprintf(cur, "&#%i;", (unsigned int)linechar[x + i]);
  483. }
  484. if(lineattr[x] & CACA_BLINK)
  485. cur += sprintf(cur, "</blink>");
  486. if(lineattr[x] & CACA_UNDERLINE)
  487. cur += sprintf(cur, "</u>");
  488. if(lineattr[x] & CACA_ITALICS)
  489. cur += sprintf(cur, "</i>");
  490. if(lineattr[x] & CACA_BOLD)
  491. cur += sprintf(cur, "</b>");
  492. cur += sprintf(cur, "</tt>");
  493. if(needfont)
  494. cur += sprintf(cur, "</font>");
  495. cur += sprintf(cur, "</td>");
  496. }
  497. cur += sprintf(cur, "</tr>\n");
  498. }
  499. /* Footer */
  500. cur += sprintf(cur, "</table>\n");
  501. /* Crop to really used size */
  502. debug("html3 export: alloc %lu bytes, realloc %lu",
  503. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  504. *bytes = (uintptr_t)(cur - data);
  505. data = realloc(data, *bytes);
  506. return data;
  507. }
  508. static void *export_bbfr(caca_canvas_t const *cv, size_t *bytes)
  509. {
  510. char *data, *cur;
  511. int x, y, len;
  512. /* The font markup: less than 100 bytes
  513. * A line: 1 char for "\n"
  514. * A glyph: 22 chars for "[f=#xxxxxx][c=#xxxxxx]"
  515. * up to 21 chars for "[g][i][s][/s][/i][/g]"
  516. * up to 6 chars for the UTF-8 glyph
  517. * 8 chars for "[/c][/f]" */
  518. *bytes = 100 + cv->height * (1 + cv->width * (22 + 21 + 6 + 8));
  519. cur = data = malloc(*bytes);
  520. /* Table */
  521. cur += sprintf(cur, "[font=Courier New]");
  522. for(y = 0; y < cv->height; y++)
  523. {
  524. uint32_t *lineattr = cv->attrs + y * cv->width;
  525. uint32_t *linechar = cv->chars + y * cv->width;
  526. for(x = 0; x < cv->width; x += len)
  527. {
  528. int i, needback, needfront;
  529. /* Use colspan option to factor cells with same attributes
  530. * (see below) */
  531. len = 1;
  532. if(linechar[x] == ' ')
  533. while(x + len < cv->width && lineattr[x + len] == lineattr[x]
  534. && linechar[x] == ' ')
  535. len++;
  536. else
  537. while(x + len < cv->width && lineattr[x + len] == lineattr[x]
  538. && linechar[x] != ' ')
  539. len++;
  540. needback = caca_attr_to_ansi_bg(lineattr[x]) < 0x10;
  541. needfront = caca_attr_to_ansi_fg(lineattr[x]) < 0x10;
  542. if(needback)
  543. cur += sprintf(cur, "[f=#%.06lx]", (unsigned long int)
  544. _caca_attr_to_rgb24bg(lineattr[x]));
  545. if(linechar[x] == ' ')
  546. cur += sprintf(cur, "[c=#%.06lx]", (unsigned long int)
  547. _caca_attr_to_rgb24bg(lineattr[x]));
  548. else if(needfront)
  549. cur += sprintf(cur, "[c=#%.06lx]", (unsigned long int)
  550. _caca_attr_to_rgb24fg(lineattr[x]));
  551. if(lineattr[x] & CACA_BOLD)
  552. cur += sprintf(cur, "[g]");
  553. if(lineattr[x] & CACA_ITALICS)
  554. cur += sprintf(cur, "[i]");
  555. if(lineattr[x] & CACA_UNDERLINE)
  556. cur += sprintf(cur, "[s]");
  557. if(lineattr[x] & CACA_BLINK)
  558. ; /* FIXME */
  559. for(i = 0; i < len; i++)
  560. {
  561. if(linechar[x + i] == CACA_MAGIC_FULLWIDTH)
  562. ;
  563. else if(linechar[x + i] == ' ')
  564. *cur++ = '_';
  565. else
  566. cur += caca_utf32_to_utf8(cur, linechar[x + i]);
  567. }
  568. if(lineattr[x] & CACA_BLINK)
  569. ; /* FIXME */
  570. if(lineattr[x] & CACA_UNDERLINE)
  571. cur += sprintf(cur, "[/s]");
  572. if(lineattr[x] & CACA_ITALICS)
  573. cur += sprintf(cur, "[/i]");
  574. if(lineattr[x] & CACA_BOLD)
  575. cur += sprintf(cur, "[/g]");
  576. if(linechar[x] == ' ' || needfront)
  577. cur += sprintf(cur, "[/c]");
  578. if(needback)
  579. cur += sprintf(cur, "[/f]");
  580. }
  581. cur += sprintf(cur, "\n");
  582. }
  583. /* Footer */
  584. cur += sprintf(cur, "[/font]\n");
  585. /* Crop to really used size */
  586. debug("bbfr export: alloc %lu bytes, realloc %lu",
  587. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  588. *bytes = (uintptr_t)(cur - data);
  589. data = realloc(data, *bytes);
  590. return data;
  591. }
  592. /* Export a text file with IRC colours */
  593. static void *export_irc(caca_canvas_t const *cv, size_t *bytes)
  594. {
  595. static uint8_t const palette[] =
  596. {
  597. 1, 2, 3, 10, 5, 6, 7, 15, /* Dark */
  598. 14, 12, 9, 11, 4, 13, 8, 0, /* Light */
  599. };
  600. char *data, *cur;
  601. int x, y;
  602. /* 14 bytes assumed for max length per pixel. Worst case scenario:
  603. * ^Cxx,yy 6 bytes
  604. * ^B^B 2 bytes
  605. * ch 6 bytes
  606. * 3 bytes for max length per line. Worst case scenario:
  607. * <spc> 1 byte (for empty lines)
  608. * \r\n 2 bytes
  609. * In real life, the average bytes per pixel value will be around 5.
  610. */
  611. *bytes = 2 + cv->height * (3 + cv->width * 14);
  612. cur = data = malloc(*bytes);
  613. for(y = 0; y < cv->height; y++)
  614. {
  615. uint32_t *lineattr = cv->attrs + y * cv->width;
  616. uint32_t *linechar = cv->chars + y * cv->width;
  617. uint8_t prevfg = 0x10;
  618. uint8_t prevbg = 0x10;
  619. for(x = 0; x < cv->width; x++)
  620. {
  621. uint32_t attr = lineattr[x];
  622. uint32_t ch = linechar[x];
  623. uint8_t ansifg, ansibg, fg, bg;
  624. if(ch == CACA_MAGIC_FULLWIDTH)
  625. continue;
  626. ansifg = caca_attr_to_ansi_fg(attr);
  627. ansibg = caca_attr_to_ansi_bg(attr);
  628. fg = ansifg < 0x10 ? palette[ansifg] : 0x10;
  629. bg = ansibg < 0x10 ? palette[ansibg] : 0x10;
  630. /* TODO: optimise series of same fg / same bg
  631. * don't change fg value if ch == ' '
  632. * make sure the \x03,%d trick works everywhere */
  633. if(bg != prevbg || fg != prevfg)
  634. {
  635. int need_escape = 0;
  636. if(bg == 0x10)
  637. {
  638. if(fg == 0x10)
  639. cur += sprintf(cur, "\x0f");
  640. else
  641. {
  642. if(prevbg == 0x10)
  643. cur += sprintf(cur, "\x03%d", fg);
  644. else
  645. cur += sprintf(cur, "\x0f\x03%d", fg);
  646. if(ch == (uint32_t)',')
  647. need_escape = 1;
  648. }
  649. }
  650. else
  651. {
  652. if(fg == 0x10)
  653. cur += sprintf(cur, "\x0f\x03,%d", bg);
  654. else
  655. cur += sprintf(cur, "\x03%d,%d", fg, bg);
  656. }
  657. if(ch >= (uint32_t)'0' && ch <= (uint32_t)'9')
  658. need_escape = 1;
  659. if(need_escape)
  660. cur += sprintf(cur, "\x02\x02");
  661. }
  662. cur += caca_utf32_to_utf8(cur, ch);
  663. prevfg = fg;
  664. prevbg = bg;
  665. }
  666. /* TODO: do the same the day we optimise whole lines above */
  667. if(!cv->width)
  668. *cur++ = ' ';
  669. *cur++ = '\r';
  670. *cur++ = '\n';
  671. }
  672. /* Crop to really used size */
  673. debug("IRC export: alloc %lu bytes, realloc %lu",
  674. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  675. *bytes = (uintptr_t)(cur - data);
  676. data = realloc(data, *bytes);
  677. return data;
  678. }
  679. /* Export a PostScript document. */
  680. static void *export_ps(caca_canvas_t const *cv, size_t *bytes)
  681. {
  682. static char const *ps_header =
  683. "%!\n"
  684. "%% libcaca PDF export\n"
  685. "%%LanguageLevel: 2\n"
  686. "%%Pages: 1\n"
  687. "%%DocumentData: Clean7Bit\n"
  688. "/csquare {\n"
  689. " newpath\n"
  690. " 0 0 moveto\n"
  691. " 0 1 rlineto\n"
  692. " 1 0 rlineto\n"
  693. " 0 -1 rlineto\n"
  694. " closepath\n"
  695. " setrgbcolor\n"
  696. " fill\n"
  697. "} def\n"
  698. "/S {\n"
  699. " Show\n"
  700. "} bind def\n"
  701. "/Courier-Bold findfont\n"
  702. "8 scalefont\n"
  703. "setfont\n"
  704. "gsave\n"
  705. "6 10 scale\n";
  706. char *data, *cur;
  707. int x, y;
  708. /* 200 is arbitrary but should be ok */
  709. *bytes = strlen(ps_header) + 100 + cv->height * (32 + cv->width * 200);
  710. cur = data = malloc(*bytes);
  711. /* Header */
  712. cur += sprintf(cur, "%s", ps_header);
  713. cur += sprintf(cur, "0 %d translate\n", cv->height);
  714. /* Background, drawn using csquare macro defined in header */
  715. for(y = cv->height; y--; )
  716. {
  717. uint32_t *lineattr = cv->attrs + y * cv->width;
  718. for(x = 0; x < cv->width; x++)
  719. {
  720. uint8_t argb[8];
  721. caca_attr_to_argb64(*lineattr++, argb);
  722. cur += sprintf(cur, "1 0 translate\n %f %f %f csquare\n",
  723. (float)argb[1] * (1.0 / 0xf),
  724. (float)argb[2] * (1.0 / 0xf),
  725. (float)argb[3] * (1.0 / 0xf));
  726. }
  727. /* Return to beginning of the line, and jump to the next one */
  728. cur += sprintf(cur, "-%d 1 translate\n", cv->width);
  729. }
  730. cur += sprintf(cur, "grestore\n"); /* Restore transformation matrix */
  731. cur += sprintf(cur, "0 %d translate\n", cv->height*10);
  732. for(y = cv->height; y--; )
  733. {
  734. uint32_t *lineattr = cv->attrs + (cv->height - y - 1) * cv->width;
  735. uint32_t *linechar = cv->chars + (cv->height - y - 1) * cv->width;
  736. for(x = 0; x < cv->width; x++)
  737. {
  738. uint8_t argb[8];
  739. uint32_t ch = *linechar++;
  740. caca_attr_to_argb64(*lineattr++, argb);
  741. cur += sprintf(cur, "newpath\n");
  742. cur += sprintf(cur, "%d %d moveto\n", (x + 1) * 6, y * 10 + 2);
  743. cur += sprintf(cur, "%f %f %f setrgbcolor\n",
  744. (float)argb[5] * (1.0 / 0xf),
  745. (float)argb[6] * (1.0 / 0xf),
  746. (float)argb[7] * (1.0 / 0xf));
  747. if(ch < 0x00000020)
  748. cur += sprintf(cur, "(?) show\n");
  749. else if(ch >= 0x00000080)
  750. cur += sprintf(cur, "(?) show\n");
  751. else switch((uint8_t)(ch & 0x7f))
  752. {
  753. case '\\':
  754. case '(':
  755. case ')':
  756. cur += sprintf(cur, "(\\%c) show\n", (uint8_t)ch);
  757. break;
  758. default:
  759. cur += sprintf(cur, "(%c) show\n", (uint8_t)ch);
  760. break;
  761. }
  762. }
  763. }
  764. cur += sprintf(cur, "showpage\n");
  765. /* Crop to really used size */
  766. debug("PS export: alloc %lu bytes, realloc %lu",
  767. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  768. *bytes = (uintptr_t)(cur - data);
  769. data = realloc(data, *bytes);
  770. return data;
  771. }
  772. /* Export an SVG vector image */
  773. static void *export_svg(caca_canvas_t const *cv, size_t *bytes)
  774. {
  775. static char const svg_header[] =
  776. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  777. "<svg width=\"%d\" height=\"%d\" viewBox=\"0 0 %d %d\""
  778. " xmlns=\"http://www.w3.org/2000/svg\""
  779. " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
  780. " xml:space=\"preserve\" version=\"1.1\" baseProfile=\"full\">\n";
  781. char *data, *cur;
  782. int x, y;
  783. /* 200 is arbitrary but should be ok */
  784. *bytes = strlen(svg_header) + 128 + cv->width * cv->height * 200;
  785. cur = data = malloc(*bytes);
  786. /* Header */
  787. cur += sprintf(cur, svg_header, cv->width * 6, cv->height * 10,
  788. cv->width * 6, cv->height * 10);
  789. cur += sprintf(cur, " <g id=\"mainlayer\" font-size=\"10\""
  790. " style=\"font-family: monospace\">\n");
  791. /* Background */
  792. for(y = 0; y < cv->height; y++)
  793. {
  794. uint32_t *lineattr = cv->attrs + y * cv->width;
  795. for(x = 0; x < cv->width; x++)
  796. {
  797. cur += sprintf(cur, "<rect style=\"fill:#%.03x\" x=\"%d\" y=\"%d\""
  798. " width=\"6\" height=\"10\"/>\n",
  799. caca_attr_to_rgb12_bg(*lineattr++),
  800. x * 6, y * 10);
  801. }
  802. }
  803. /* Text */
  804. for(y = 0; y < cv->height; y++)
  805. {
  806. uint32_t *lineattr = cv->attrs + y * cv->width;
  807. uint32_t *linechar = cv->chars + y * cv->width;
  808. for(x = 0; x < cv->width; x++)
  809. {
  810. uint32_t ch = *linechar++;
  811. if(ch == ' ' || ch == CACA_MAGIC_FULLWIDTH)
  812. {
  813. lineattr++;
  814. continue;
  815. }
  816. cur += sprintf(cur, "<text style=\"fill:#%.03x\" "
  817. "x=\"%d\" y=\"%d\">",
  818. caca_attr_to_rgb12_fg(*lineattr++),
  819. x * 6, (y * 10) + 8);
  820. if(ch < 0x00000020)
  821. *cur++ = '?';
  822. else if(ch > 0x0000007f)
  823. cur += caca_utf32_to_utf8(cur, ch);
  824. else switch((uint8_t)ch)
  825. {
  826. case '>': cur += sprintf(cur, "&gt;"); break;
  827. case '<': cur += sprintf(cur, "&lt;"); break;
  828. case '&': cur += sprintf(cur, "&amp;"); break;
  829. default: *cur++ = ch; break;
  830. }
  831. cur += sprintf(cur, "</text>\n");
  832. }
  833. }
  834. cur += sprintf(cur, " </g>\n");
  835. cur += sprintf(cur, "</svg>\n");
  836. /* Crop to really used size */
  837. debug("SVG export: alloc %lu bytes, realloc %lu",
  838. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  839. *bytes = (uintptr_t)(cur - data);
  840. data = realloc(data, *bytes);
  841. return data;
  842. }
  843. /* Export a TGA image */
  844. static void *export_tga(caca_canvas_t const *cv, size_t *bytes)
  845. {
  846. char const * const *fontlist;
  847. char *data, *cur;
  848. caca_font_t *f;
  849. int i, w, h;
  850. fontlist = caca_get_font_list();
  851. if(!fontlist[0])
  852. {
  853. seterrno(EINVAL);
  854. return NULL;
  855. }
  856. f = caca_load_font(fontlist[0], 0);
  857. w = caca_get_canvas_width(cv) * caca_get_font_width(f);
  858. h = caca_get_canvas_height(cv) * caca_get_font_height(f);
  859. *bytes = w * h * 4 + 18; /* 32 bpp + 18 bytes for the header */
  860. cur = data = malloc(*bytes);
  861. /* ID Length */
  862. cur += sprintf(cur, "%c", 0);
  863. /* Color Map Type: no colormap */
  864. cur += sprintf(cur, "%c", 0);
  865. /* Image Type: uncompressed truecolor */
  866. cur += sprintf(cur, "%c", 2);
  867. /* Color Map Specification: no color map */
  868. memset(cur, 0, 5); cur += 5;
  869. /* Image Specification */
  870. cur += sprintf(cur, "%c%c", 0, 0); /* X Origin */
  871. cur += sprintf(cur, "%c%c", 0, 0); /* Y Origin */
  872. cur += sprintf(cur, "%c%c", w & 0xff, w >> 8); /* Width */
  873. cur += sprintf(cur, "%c%c", h & 0xff, h >> 8); /* Height */
  874. cur += sprintf(cur, "%c", 32); /* Pixel Depth */
  875. cur += sprintf(cur, "%c", 40); /* Image Descriptor */
  876. /* Image ID: no ID */
  877. /* Color Map Data: no colormap */
  878. /* Image Data */
  879. caca_render_canvas(cv, f, cur, w, h, 4 * w);
  880. /* Swap bytes. What a waste of time. */
  881. for(i = 0; i < w * h * 4; i += 4)
  882. {
  883. char c;
  884. c = cur[i]; cur[i] = cur[i + 3]; cur[i + 3] = c;
  885. c = cur[i + 1]; cur[i + 1] = cur[i + 2]; cur[i + 2] = c;
  886. }
  887. caca_free_font(f);
  888. return data;
  889. }
  890. /*
  891. * XXX: The following functions are aliases.
  892. */
  893. void *cucul_export_memory(cucul_canvas_t const *, char const *,
  894. size_t *) CACA_ALIAS(caca_export_memory);
  895. char const * const * cucul_get_export_list(void)
  896. CACA_ALIAS(caca_get_export_list);