Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

326 linhas
9.4 KiB

  1. /*
  2. * libcucul Unicode canvas library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Do What The Fuck You Want To
  8. * Public License, Version 2, as published by Sam Hocevar. See
  9. * http://sam.zoy.org/wtfpl/COPYING for more details.
  10. */
  11. /** \file export.c
  12. * \version \$Id$
  13. * \author Sam Hocevar <sam@zoy.org>
  14. * \author Jean-Yves Lamoureux <jylam@lnxscene.org>
  15. * \brief Export function
  16. *
  17. * This file contains export functions for various file formats such
  18. * as HTML or IRC.
  19. */
  20. #include "config.h"
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include "cucul.h"
  25. #include "cucul_internals.h"
  26. /* HTML */
  27. /** \brief Generate HTML representation of current image.
  28. *
  29. * This function generates and returns the HTML representation of
  30. * the current image.
  31. */
  32. char* cucul_get_html(cucul_t *qq)
  33. {
  34. static int const palette[] =
  35. {
  36. 0x000, 0x008, 0x080, 0x088, 0x800, 0x808, 0x880, 0x888,
  37. 0x444, 0x44f, 0x4f4, 0x4ff, 0xf44, 0xf4f, 0xff4, 0xfff,
  38. };
  39. char *buffer, *cur;
  40. unsigned int x, y, len;
  41. /* 13000 -> css palette
  42. * 40 -> max size used for a pixel (plus 10, never know)*/
  43. /* FIXME: Check this value */
  44. buffer = malloc((13000 + ((qq->width*qq->height) * 40)) * sizeof(char));
  45. cur = buffer;
  46. /* HTML header */
  47. cur += sprintf(cur, "<html>\n<head>\n<title>Generated by libcaca %s</title>\n", VERSION);
  48. /* CSS */
  49. cur += sprintf(cur, "<style>\n");
  50. cur += sprintf(cur, ".caca { font-family: monospace, fixed; font-weight: bold; }");
  51. for(x = 0; x < 0x100; x++)
  52. {
  53. cur += sprintf(cur, ".b%02x { color:#%03x; background-color:#%03x; }\n",
  54. x, palette[x & 0xf ], palette[x >> 4]);
  55. }
  56. cur += sprintf(cur, "</style>\n</head>\n<body>\n");
  57. cur += sprintf(cur, "<div cellpadding='0' cellspacing='0' style='%s'>\n",
  58. "font-family: monospace, fixed; font-weight: bold;");
  59. for(y = 0; y < qq->height; y++)
  60. {
  61. uint8_t *lineattr = qq->attr + y * qq->width;
  62. uint32_t *linechar = qq->chars + y * qq->width;
  63. for(x = 0; x < qq->width; x += len)
  64. {
  65. cur += sprintf(cur, "<span class='b%02x'>", lineattr[x]);
  66. for(len = 0;
  67. x + len < qq->width && lineattr[x + len] == lineattr[x];
  68. len++)
  69. {
  70. if(linechar[x + len] == (uint32_t)' ')
  71. cur += sprintf(cur, "&nbsp;");
  72. else
  73. cur += sprintf(cur, "%c", linechar[x + len] & 0x7f);
  74. }
  75. cur += sprintf(cur, "</span>");
  76. }
  77. /* New line */
  78. cur += sprintf(cur, "<br />\n");
  79. }
  80. cur += sprintf(cur, "</div></body></html>\n");
  81. /* Crop to really used size */
  82. buffer = realloc(buffer, (strlen(buffer) + 1) * sizeof(char));
  83. return buffer;
  84. }
  85. /** \brief Generate HTML3 representation of current image.
  86. *
  87. * This function generates and returns the HTML3 representation of
  88. * the current image. It is way bigger than cucul_get_html(), but
  89. * permits viewing in old browsers (or limited ones such as links)
  90. * Won't work under gecko (mozilla rendering engine) unless you set
  91. * a correct header.
  92. */
  93. char* cucul_get_html3(cucul_t *qq)
  94. {
  95. static int const palette[] =
  96. {
  97. 0x000000, 0x000088, 0x008800, 0x008888,
  98. 0x880000, 0x880088, 0x888800, 0x888888,
  99. 0x444444, 0x4444ff, 0x44ff44, 0x44ffff,
  100. 0xff4444, 0xff44ff, 0xffff44, 0xffffff,
  101. };
  102. char *buffer, *cur;
  103. unsigned int x, y, len;
  104. /* 13000 -> css palette
  105. * 40 -> max size used for a pixel (plus 10, never know) */
  106. buffer = malloc((13000 + ((qq->width*qq->height)*40))*sizeof(char));
  107. cur = buffer;
  108. /* Table */
  109. cur += sprintf(cur, "<table cols='%d' cellpadding='0' cellspacing='0'>\n",
  110. qq->height);
  111. for(y = 0; y < qq->height; y++)
  112. {
  113. uint8_t *lineattr = qq->attr + y * qq->width;
  114. uint32_t *linechar = qq->chars + y * qq->width;
  115. cur += sprintf(cur, "<tr>");
  116. for(x = 0; x < qq->width; x += len)
  117. {
  118. unsigned int i;
  119. /* Use colspan option to factorize cells with same attributes
  120. * (see below) */
  121. len = 1;
  122. while(x + len < qq->width && lineattr[x + len] == lineattr[x])
  123. len++;
  124. cur += sprintf(cur, "<td bgcolor=#%06x", palette[lineattr[x] >> 4]);
  125. if(len > 1)
  126. cur += sprintf(cur, " colspan=%d", len);
  127. cur += sprintf(cur, "><font color=#%06x>",
  128. palette[lineattr[x] & 0x0f]);
  129. for(i = 0; i < len; i++)
  130. {
  131. if(linechar[x + i] == (uint32_t)' ')
  132. cur += sprintf(cur, "&nbsp;");
  133. else
  134. cur += sprintf(cur, "%c", linechar[x + i] & 0x7f);
  135. }
  136. cur += sprintf(cur, "</font></td>");
  137. }
  138. cur += sprintf(cur, "</tr>\n");
  139. }
  140. /* Footer */
  141. cur += sprintf(cur, "</table>\n");
  142. /* Crop to really used size */
  143. buffer = realloc(buffer, (strlen(buffer) + 1) * sizeof(char));
  144. return buffer;
  145. }
  146. /** \brief Generate IRC representation of current image.
  147. *
  148. * This function generates and returns an IRC representation of
  149. * the current image.
  150. */
  151. char* cucul_get_irc(cucul_t *qq)
  152. {
  153. static int const palette[] =
  154. {
  155. 1, 2, 3, 10, 5, 6, 7, 15, /* Dark */
  156. 14, 12, 9, 11, 4, 13, 8, 0, /* Light */
  157. };
  158. char *buffer, *cur;
  159. unsigned int x, y;
  160. /* 11 bytes assumed for max length per pixel. Worst case scenario:
  161. * ^Cxx,yy 6 bytes
  162. * ^B^B 2 bytes
  163. * c 1 byte
  164. * \r\n 2 bytes
  165. * In real life, the average bytes per pixel value will be around 5.
  166. */
  167. buffer = malloc((2 + (qq->width * qq->height * 11)) * sizeof(char));
  168. cur = buffer;
  169. *cur++ = '\x0f';
  170. for(y = 0; y < qq->height; y++)
  171. {
  172. uint8_t *lineattr = qq->attr + y * qq->width;
  173. uint32_t *linechar = qq->chars + y * qq->width;
  174. uint8_t prevfg = -1;
  175. uint8_t prevbg = -1;
  176. for(x = 0; x < qq->width; x++)
  177. {
  178. uint8_t fg = palette[lineattr[x] & 0x0f];
  179. uint8_t bg = palette[lineattr[x] >> 4];
  180. uint32_t c = linechar[x];
  181. if(bg == prevbg)
  182. {
  183. if(fg == prevfg)
  184. ; /* Same fg/bg, do nothing */
  185. else if(c == (uint32_t)' ')
  186. fg = prevfg; /* Hackety hack */
  187. else
  188. {
  189. cur += sprintf(cur, "\x03%d", fg);
  190. if(c >= (uint32_t)'0' && c <= (uint32_t)'9')
  191. cur += sprintf(cur, "\x02\x02");
  192. }
  193. }
  194. else
  195. {
  196. if(fg == prevfg)
  197. cur += sprintf(cur, "\x03,%d", bg);
  198. else
  199. cur += sprintf(cur, "\x03%d,%d", fg, bg);
  200. if(c >= (uint32_t)'0' && c <= (uint32_t)'9')
  201. cur += sprintf(cur, "\x02\x02");
  202. }
  203. *cur++ = c & 0x7f;
  204. prevfg = fg;
  205. prevbg = bg;
  206. }
  207. *cur++ = '\r';
  208. *cur++ = '\n';
  209. }
  210. *cur++ = '\x0f';
  211. /* Crop to really used size */
  212. buffer = realloc(buffer, (strlen(buffer) + 1) * sizeof(char));
  213. return buffer;
  214. }
  215. /** \brief Generate ANSI representation of current image.
  216. *
  217. * This function generates and returns an ANSI representation of
  218. * the current image.
  219. * \param trailing if 0, raw ANSI will be generated. Otherwise, you'll be
  220. * able to cut/paste the result to a function like printf
  221. * \return buffer containing generated ANSI codes as a big string
  222. */
  223. char * cucul_get_ansi(cucul_t *qq, int trailing)
  224. {
  225. static int const palette[] =
  226. {
  227. 30, 34, 32, 36, 31, 35, 33, 37, /* Both lines (light and dark) are the same, */
  228. 30, 34, 32, 36, 31, 35, 33, 37, /* light colors handling is done later */
  229. };
  230. char *buffer, *cur;
  231. unsigned int x, y;
  232. /* 20 bytes assumed for max length per pixel.
  233. * Add height*9 to that (zeroes color at the end and jump to next line) */
  234. buffer = malloc(((qq->height*9) + (qq->width * qq->height * 20)) * sizeof(char));
  235. cur = buffer;
  236. // *cur++ = '';
  237. for(y = 0; y < qq->height; y++)
  238. {
  239. uint8_t *lineattr = qq->attr + y * qq->width;
  240. uint32_t *linechar = qq->chars + y * qq->width;
  241. uint8_t prevfg = -1;
  242. uint8_t prevbg = -1;
  243. for(x = 0; x < qq->width; x++)
  244. {
  245. uint8_t fg = palette[lineattr[x] & 0x0f];
  246. uint8_t bg = (palette[lineattr[x] >> 4])+10;
  247. uint32_t c = linechar[x];
  248. if(!trailing)
  249. cur += sprintf(cur, "\033[");
  250. else
  251. cur += sprintf(cur, "\\033[");
  252. if(fg > 7)
  253. cur += sprintf(cur, "1;%d;%dm",fg,bg);
  254. else
  255. cur += sprintf(cur, "0;%d;%dm",fg,bg);
  256. *cur++ = c & 0x7f;
  257. if((c == '%') && trailing)
  258. *cur++ = c & 0x7f;
  259. prevfg = fg;
  260. prevbg = bg;
  261. }
  262. if(!trailing)
  263. cur += sprintf(cur, "\033[0m\n\r");
  264. else
  265. cur += sprintf(cur, "\\033[0m\\n\n");
  266. }
  267. /* Crop to really used size */
  268. buffer = realloc(buffer, (strlen(buffer) + 1) * sizeof(char));
  269. return buffer;
  270. }