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 32 KiB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  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. /* The HTML table markup: less than 1000 bytes
  389. * A line: 10 chars for "<tr></tr>\n"
  390. * A glyph: 40 chars for "<td bgcolor=#xxxxxx><font color=#xxxxxx>"
  391. * up to 36 chars for "<b><i><u><blink></blink></u></i></b>"
  392. * up to 9 chars for "&#xxxxxx;", far less for pure ASCII
  393. * 12 chars for "</font></td>" */
  394. *bytes = 1000 + cv->height * (10 + cv->width * (40 + 36 + 9 + 12));
  395. cur = data = malloc(*bytes);
  396. /* Table */
  397. cur += sprintf(cur, "<table cols='%d' cellpadding='0' cellspacing='0'>\n",
  398. cv->height);
  399. for(y = 0; y < cv->height; y++)
  400. {
  401. uint32_t *lineattr = cv->attrs + y * cv->width;
  402. uint32_t *linechar = cv->chars + y * cv->width;
  403. cur += sprintf(cur, "<tr>");
  404. for(x = 0; x < cv->width; x += len)
  405. {
  406. int i, needfont;
  407. /* Use colspan option to factor cells with same attributes
  408. * (see below) */
  409. len = 1;
  410. while(x + len < cv->width && lineattr[x + len] == lineattr[x])
  411. len++;
  412. cur += sprintf(cur, "<td");
  413. if(caca_attr_to_ansi_fg(lineattr[x]) < 0x10)
  414. cur += sprintf(cur, " bgcolor=#%.06lx", (unsigned long int)
  415. _caca_attr_to_rgb24bg(lineattr[x]));
  416. if(len > 1)
  417. cur += sprintf(cur, " colspan=%d", len);
  418. cur += sprintf(cur, ">");
  419. needfont = caca_attr_to_ansi_bg(lineattr[x]) < 0x10;
  420. if(needfont)
  421. cur += sprintf(cur, "<font color=#%.06lx>", (unsigned long int)
  422. _caca_attr_to_rgb24fg(lineattr[x]));
  423. if(lineattr[x] & CACA_BOLD)
  424. cur += sprintf(cur, "<b>");
  425. if(lineattr[x] & CACA_ITALICS)
  426. cur += sprintf(cur, "<i>");
  427. if(lineattr[x] & CACA_UNDERLINE)
  428. cur += sprintf(cur, "<u>");
  429. if(lineattr[x] & CACA_BLINK)
  430. cur += sprintf(cur, "<blink>");
  431. for(i = 0; i < len; i++)
  432. {
  433. if(linechar[x + i] == CACA_MAGIC_FULLWIDTH)
  434. ;
  435. else if(linechar[x + i] <= 0x00000020)
  436. cur += sprintf(cur, "&nbsp;");
  437. else if(linechar[x + i] < 0x00000080)
  438. cur += sprintf(cur, "%c", (uint8_t)linechar[x + i]);
  439. else
  440. cur += sprintf(cur, "&#%i;", (unsigned int)linechar[x + i]);
  441. }
  442. if(lineattr[x] & CACA_BLINK)
  443. cur += sprintf(cur, "</blink>");
  444. if(lineattr[x] & CACA_UNDERLINE)
  445. cur += sprintf(cur, "</u>");
  446. if(lineattr[x] & CACA_ITALICS)
  447. cur += sprintf(cur, "</i>");
  448. if(lineattr[x] & CACA_BOLD)
  449. cur += sprintf(cur, "</b>");
  450. if(needfont)
  451. cur += sprintf(cur, "</font>");
  452. cur += sprintf(cur, "</td>");
  453. }
  454. cur += sprintf(cur, "</tr>\n");
  455. }
  456. /* Footer */
  457. cur += sprintf(cur, "</table>\n");
  458. /* Crop to really used size */
  459. debug("html3 export: alloc %lu bytes, realloc %lu",
  460. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  461. *bytes = (uintptr_t)(cur - data);
  462. data = realloc(data, *bytes);
  463. return data;
  464. }
  465. static void *export_bbfr(caca_canvas_t const *cv, size_t *bytes)
  466. {
  467. char *data, *cur;
  468. int x, y, len;
  469. /* The font markup: less than 100 bytes
  470. * A line: 1 char for "\n"
  471. * A glyph: 22 chars for "[f=#xxxxxx][c=#xxxxxx]"
  472. * up to 21 chars for "[g][i][s][/s][/i][/g]"
  473. * up to 6 chars for the UTF-8 glyph
  474. * 8 chars for "[/c][/f]" */
  475. *bytes = 100 + cv->height * (1 + cv->width * (22 + 21 + 6 + 8));
  476. cur = data = malloc(*bytes);
  477. /* Table */
  478. cur += sprintf(cur, "[font=Courier New]");
  479. for(y = 0; y < cv->height; y++)
  480. {
  481. uint32_t *lineattr = cv->attrs + y * cv->width;
  482. uint32_t *linechar = cv->chars + y * cv->width;
  483. for(x = 0; x < cv->width; x += len)
  484. {
  485. int i, needback, needfront;
  486. /* Use colspan option to factor cells with same attributes
  487. * (see below) */
  488. len = 1;
  489. if(linechar[x] == ' ')
  490. while(x + len < cv->width && lineattr[x + len] == lineattr[x]
  491. && linechar[x] == ' ')
  492. len++;
  493. else
  494. while(x + len < cv->width && lineattr[x + len] == lineattr[x]
  495. && linechar[x] != ' ')
  496. len++;
  497. needback = caca_attr_to_ansi_bg(lineattr[x]) < 0x10;
  498. needfront = caca_attr_to_ansi_fg(lineattr[x]) < 0x10;
  499. if(needback)
  500. cur += sprintf(cur, "[f=#%.06lx]", (unsigned long int)
  501. _caca_attr_to_rgb24bg(lineattr[x]));
  502. if(linechar[x] == ' ')
  503. cur += sprintf(cur, "[c=#%.06lx]", (unsigned long int)
  504. _caca_attr_to_rgb24bg(lineattr[x]));
  505. else if(needfront)
  506. cur += sprintf(cur, "[c=#%.06lx]", (unsigned long int)
  507. _caca_attr_to_rgb24fg(lineattr[x]));
  508. if(lineattr[x] & CACA_BOLD)
  509. cur += sprintf(cur, "[g]");
  510. if(lineattr[x] & CACA_ITALICS)
  511. cur += sprintf(cur, "[i]");
  512. if(lineattr[x] & CACA_UNDERLINE)
  513. cur += sprintf(cur, "[s]");
  514. if(lineattr[x] & CACA_BLINK)
  515. ; /* FIXME */
  516. for(i = 0; i < len; i++)
  517. {
  518. if(linechar[x + i] == CACA_MAGIC_FULLWIDTH)
  519. ;
  520. else if(linechar[x + i] == ' ')
  521. *cur++ = '_';
  522. else
  523. cur += caca_utf32_to_utf8(cur, linechar[x + i]);
  524. }
  525. if(lineattr[x] & CACA_BLINK)
  526. ; /* FIXME */
  527. if(lineattr[x] & CACA_UNDERLINE)
  528. cur += sprintf(cur, "[/s]");
  529. if(lineattr[x] & CACA_ITALICS)
  530. cur += sprintf(cur, "[/i]");
  531. if(lineattr[x] & CACA_BOLD)
  532. cur += sprintf(cur, "[/g]");
  533. if(linechar[x] == ' ' || needfront)
  534. cur += sprintf(cur, "[/c]");
  535. if(needback)
  536. cur += sprintf(cur, "[/f]");
  537. }
  538. cur += sprintf(cur, "\n");
  539. }
  540. /* Footer */
  541. cur += sprintf(cur, "[/font]\n");
  542. /* Crop to really used size */
  543. debug("bbfr export: alloc %lu bytes, realloc %lu",
  544. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  545. *bytes = (uintptr_t)(cur - data);
  546. data = realloc(data, *bytes);
  547. return data;
  548. }
  549. /* Export a text file with IRC colours */
  550. static void *export_irc(caca_canvas_t const *cv, size_t *bytes)
  551. {
  552. static uint8_t const palette[] =
  553. {
  554. 1, 2, 3, 10, 5, 6, 7, 15, /* Dark */
  555. 14, 12, 9, 11, 4, 13, 8, 0, /* Light */
  556. };
  557. char *data, *cur;
  558. int x, y;
  559. /* 14 bytes assumed for max length per pixel. Worst case scenario:
  560. * ^Cxx,yy 6 bytes
  561. * ^B^B 2 bytes
  562. * ch 6 bytes
  563. * 3 bytes for max length per line. Worst case scenario:
  564. * <spc> 1 byte (for empty lines)
  565. * \r\n 2 bytes
  566. * In real life, the average bytes per pixel value will be around 5.
  567. */
  568. *bytes = 2 + cv->height * (3 + cv->width * 14);
  569. cur = data = malloc(*bytes);
  570. for(y = 0; y < cv->height; y++)
  571. {
  572. uint32_t *lineattr = cv->attrs + y * cv->width;
  573. uint32_t *linechar = cv->chars + y * cv->width;
  574. uint8_t prevfg = 0x10;
  575. uint8_t prevbg = 0x10;
  576. for(x = 0; x < cv->width; x++)
  577. {
  578. uint32_t attr = lineattr[x];
  579. uint32_t ch = linechar[x];
  580. uint8_t ansifg, ansibg, fg, bg;
  581. if(ch == CACA_MAGIC_FULLWIDTH)
  582. continue;
  583. ansifg = caca_attr_to_ansi_fg(attr);
  584. ansibg = caca_attr_to_ansi_bg(attr);
  585. fg = ansifg < 0x10 ? palette[ansifg] : 0x10;
  586. bg = ansibg < 0x10 ? palette[ansibg] : 0x10;
  587. /* TODO: optimise series of same fg / same bg
  588. * don't change fg value if ch == ' '
  589. * make sure the \x03,%d trick works everywhere */
  590. if(bg != prevbg || fg != prevfg)
  591. {
  592. int need_escape = 0;
  593. if(bg == 0x10)
  594. {
  595. if(fg == 0x10)
  596. cur += sprintf(cur, "\x0f");
  597. else
  598. {
  599. if(prevbg == 0x10)
  600. cur += sprintf(cur, "\x03%d", fg);
  601. else
  602. cur += sprintf(cur, "\x0f\x03%d", fg);
  603. if(ch == (uint32_t)',')
  604. need_escape = 1;
  605. }
  606. }
  607. else
  608. {
  609. if(fg == 0x10)
  610. cur += sprintf(cur, "\x0f\x03,%d", bg);
  611. else
  612. cur += sprintf(cur, "\x03%d,%d", fg, bg);
  613. }
  614. if(ch >= (uint32_t)'0' && ch <= (uint32_t)'9')
  615. need_escape = 1;
  616. if(need_escape)
  617. cur += sprintf(cur, "\x02\x02");
  618. }
  619. cur += caca_utf32_to_utf8(cur, ch);
  620. prevfg = fg;
  621. prevbg = bg;
  622. }
  623. /* TODO: do the same the day we optimise whole lines above */
  624. if(!cv->width)
  625. *cur++ = ' ';
  626. *cur++ = '\r';
  627. *cur++ = '\n';
  628. }
  629. /* Crop to really used size */
  630. debug("IRC export: alloc %lu bytes, realloc %lu",
  631. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  632. *bytes = (uintptr_t)(cur - data);
  633. data = realloc(data, *bytes);
  634. return data;
  635. }
  636. /* Export a PostScript document. */
  637. static void *export_ps(caca_canvas_t const *cv, size_t *bytes)
  638. {
  639. static char const *ps_header =
  640. "%!\n"
  641. "%% libcaca PDF export\n"
  642. "%%LanguageLevel: 2\n"
  643. "%%Pages: 1\n"
  644. "%%DocumentData: Clean7Bit\n"
  645. "/csquare {\n"
  646. " newpath\n"
  647. " 0 0 moveto\n"
  648. " 0 1 rlineto\n"
  649. " 1 0 rlineto\n"
  650. " 0 -1 rlineto\n"
  651. " closepath\n"
  652. " setrgbcolor\n"
  653. " fill\n"
  654. "} def\n"
  655. "/S {\n"
  656. " Show\n"
  657. "} bind def\n"
  658. "/Courier-Bold findfont\n"
  659. "8 scalefont\n"
  660. "setfont\n"
  661. "gsave\n"
  662. "6 10 scale\n";
  663. char *data, *cur;
  664. int x, y;
  665. /* 200 is arbitrary but should be ok */
  666. *bytes = strlen(ps_header) + 100 + cv->height * (32 + cv->width * 200);
  667. cur = data = malloc(*bytes);
  668. /* Header */
  669. cur += sprintf(cur, "%s", ps_header);
  670. cur += sprintf(cur, "0 %d translate\n", cv->height);
  671. /* Background, drawn using csquare macro defined in header */
  672. for(y = cv->height; y--; )
  673. {
  674. uint32_t *lineattr = cv->attrs + y * cv->width;
  675. for(x = 0; x < cv->width; x++)
  676. {
  677. uint8_t argb[8];
  678. caca_attr_to_argb64(*lineattr++, argb);
  679. cur += sprintf(cur, "1 0 translate\n %f %f %f csquare\n",
  680. (float)argb[1] * (1.0 / 0xf),
  681. (float)argb[2] * (1.0 / 0xf),
  682. (float)argb[3] * (1.0 / 0xf));
  683. }
  684. /* Return to beginning of the line, and jump to the next one */
  685. cur += sprintf(cur, "-%d 1 translate\n", cv->width);
  686. }
  687. cur += sprintf(cur, "grestore\n"); /* Restore transformation matrix */
  688. cur += sprintf(cur, "0 %d translate\n", cv->height*10);
  689. for(y = cv->height; y--; )
  690. {
  691. uint32_t *lineattr = cv->attrs + (cv->height - y - 1) * cv->width;
  692. uint32_t *linechar = cv->chars + (cv->height - y - 1) * cv->width;
  693. for(x = 0; x < cv->width; x++)
  694. {
  695. uint8_t argb[8];
  696. uint32_t ch = *linechar++;
  697. caca_attr_to_argb64(*lineattr++, argb);
  698. cur += sprintf(cur, "newpath\n");
  699. cur += sprintf(cur, "%d %d moveto\n", (x + 1) * 6, y * 10 + 2);
  700. cur += sprintf(cur, "%f %f %f setrgbcolor\n",
  701. (float)argb[5] * (1.0 / 0xf),
  702. (float)argb[6] * (1.0 / 0xf),
  703. (float)argb[7] * (1.0 / 0xf));
  704. if(ch < 0x00000020)
  705. cur += sprintf(cur, "(?) show\n");
  706. else if(ch >= 0x00000080)
  707. cur += sprintf(cur, "(?) show\n");
  708. else switch((uint8_t)(ch & 0x7f))
  709. {
  710. case '\\':
  711. case '(':
  712. case ')':
  713. cur += sprintf(cur, "(\\%c) show\n", (uint8_t)ch);
  714. break;
  715. default:
  716. cur += sprintf(cur, "(%c) show\n", (uint8_t)ch);
  717. break;
  718. }
  719. }
  720. }
  721. cur += sprintf(cur, "showpage\n");
  722. /* Crop to really used size */
  723. debug("PS export: alloc %lu bytes, realloc %lu",
  724. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  725. *bytes = (uintptr_t)(cur - data);
  726. data = realloc(data, *bytes);
  727. return data;
  728. }
  729. /* Export an SVG vector image */
  730. static void *export_svg(caca_canvas_t const *cv, size_t *bytes)
  731. {
  732. static char const svg_header[] =
  733. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  734. "<svg width=\"%d\" height=\"%d\" viewBox=\"0 0 %d %d\""
  735. " xmlns=\"http://www.w3.org/2000/svg\""
  736. " xmlns:xlink=\"http://www.w3.org/1999/xlink\""
  737. " xml:space=\"preserve\" version=\"1.1\" baseProfile=\"full\">\n";
  738. char *data, *cur;
  739. int x, y;
  740. /* 200 is arbitrary but should be ok */
  741. *bytes = strlen(svg_header) + 128 + cv->width * cv->height * 200;
  742. cur = data = malloc(*bytes);
  743. /* Header */
  744. cur += sprintf(cur, svg_header, cv->width * 6, cv->height * 10,
  745. cv->width * 6, cv->height * 10);
  746. cur += sprintf(cur, " <g id=\"mainlayer\" font-size=\"10\""
  747. " style=\"font-family: monospace\">\n");
  748. /* Background */
  749. for(y = 0; y < cv->height; y++)
  750. {
  751. uint32_t *lineattr = cv->attrs + y * cv->width;
  752. for(x = 0; x < cv->width; x++)
  753. {
  754. cur += sprintf(cur, "<rect style=\"fill:#%.03x\" x=\"%d\" y=\"%d\""
  755. " width=\"6\" height=\"10\"/>\n",
  756. caca_attr_to_rgb12_bg(*lineattr++),
  757. x * 6, y * 10);
  758. }
  759. }
  760. /* Text */
  761. for(y = 0; y < cv->height; y++)
  762. {
  763. uint32_t *lineattr = cv->attrs + y * cv->width;
  764. uint32_t *linechar = cv->chars + y * cv->width;
  765. for(x = 0; x < cv->width; x++)
  766. {
  767. uint32_t ch = *linechar++;
  768. if(ch == ' ' || ch == CACA_MAGIC_FULLWIDTH)
  769. {
  770. lineattr++;
  771. continue;
  772. }
  773. cur += sprintf(cur, "<text style=\"fill:#%.03x\" "
  774. "x=\"%d\" y=\"%d\">",
  775. caca_attr_to_rgb12_fg(*lineattr++),
  776. x * 6, (y * 10) + 8);
  777. if(ch < 0x00000020)
  778. *cur++ = '?';
  779. else if(ch > 0x0000007f)
  780. cur += caca_utf32_to_utf8(cur, ch);
  781. else switch((uint8_t)ch)
  782. {
  783. case '>': cur += sprintf(cur, "&gt;"); break;
  784. case '<': cur += sprintf(cur, "&lt;"); break;
  785. case '&': cur += sprintf(cur, "&amp;"); break;
  786. default: *cur++ = ch; break;
  787. }
  788. cur += sprintf(cur, "</text>\n");
  789. }
  790. }
  791. cur += sprintf(cur, " </g>\n");
  792. cur += sprintf(cur, "</svg>\n");
  793. /* Crop to really used size */
  794. debug("SVG export: alloc %lu bytes, realloc %lu",
  795. (unsigned long int)*bytes, (unsigned long int)(cur - data));
  796. *bytes = (uintptr_t)(cur - data);
  797. data = realloc(data, *bytes);
  798. return data;
  799. }
  800. /* Export a TGA image */
  801. static void *export_tga(caca_canvas_t const *cv, size_t *bytes)
  802. {
  803. char const * const *fontlist;
  804. char *data, *cur;
  805. caca_font_t *f;
  806. int i, w, h;
  807. fontlist = caca_get_font_list();
  808. if(!fontlist[0])
  809. {
  810. seterrno(EINVAL);
  811. return NULL;
  812. }
  813. f = caca_load_font(fontlist[0], 0);
  814. w = caca_get_canvas_width(cv) * caca_get_font_width(f);
  815. h = caca_get_canvas_height(cv) * caca_get_font_height(f);
  816. *bytes = w * h * 4 + 18; /* 32 bpp + 18 bytes for the header */
  817. cur = data = malloc(*bytes);
  818. /* ID Length */
  819. cur += sprintf(cur, "%c", 0);
  820. /* Color Map Type: no colormap */
  821. cur += sprintf(cur, "%c", 0);
  822. /* Image Type: uncompressed truecolor */
  823. cur += sprintf(cur, "%c", 2);
  824. /* Color Map Specification: no color map */
  825. memset(cur, 0, 5); cur += 5;
  826. /* Image Specification */
  827. cur += sprintf(cur, "%c%c", 0, 0); /* X Origin */
  828. cur += sprintf(cur, "%c%c", 0, 0); /* Y Origin */
  829. cur += sprintf(cur, "%c%c", w & 0xff, w >> 8); /* Width */
  830. cur += sprintf(cur, "%c%c", h & 0xff, h >> 8); /* Height */
  831. cur += sprintf(cur, "%c", 32); /* Pixel Depth */
  832. cur += sprintf(cur, "%c", 40); /* Image Descriptor */
  833. /* Image ID: no ID */
  834. /* Color Map Data: no colormap */
  835. /* Image Data */
  836. caca_render_canvas(cv, f, cur, w, h, 4 * w);
  837. /* Swap bytes. What a waste of time. */
  838. for(i = 0; i < w * h * 4; i += 4)
  839. {
  840. char c;
  841. c = cur[i]; cur[i] = cur[i + 3]; cur[i + 3] = c;
  842. c = cur[i + 1]; cur[i + 1] = cur[i + 2]; cur[i + 2] = c;
  843. }
  844. caca_free_font(f);
  845. return data;
  846. }
  847. /*
  848. * XXX: The following functions are aliases.
  849. */
  850. void *cucul_export_memory(cucul_canvas_t const *, char const *,
  851. size_t *) CACA_ALIAS(caca_export_memory);
  852. char const * const * cucul_get_export_list(void)
  853. CACA_ALIAS(caca_get_export_list);