Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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