您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

332 行
9.7 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, int *size)
  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. *size = (strlen(buffer) + 1) * sizeof(char);
  83. buffer = realloc(buffer, *size);
  84. return buffer;
  85. }
  86. /** \brief Generate HTML3 representation of current image.
  87. *
  88. * This function generates and returns the HTML3 representation of
  89. * the current image. It is way bigger than cucul_get_html(), but
  90. * permits viewing in old browsers (or limited ones such as links)
  91. * Won't work under gecko (mozilla rendering engine) unless you set
  92. * a correct header.
  93. */
  94. char* cucul_get_html3(cucul_t *qq, int *size)
  95. {
  96. static int const palette[] =
  97. {
  98. 0x000000, 0x000088, 0x008800, 0x008888,
  99. 0x880000, 0x880088, 0x888800, 0x888888,
  100. 0x444444, 0x4444ff, 0x44ff44, 0x44ffff,
  101. 0xff4444, 0xff44ff, 0xffff44, 0xffffff,
  102. };
  103. char *buffer, *cur;
  104. unsigned int x, y, len;
  105. /* 13000 -> css palette
  106. * 40 -> max size used for a pixel (plus 10, never know) */
  107. buffer = malloc((13000 + ((qq->width*qq->height)*40))*sizeof(char));
  108. cur = buffer;
  109. /* Table */
  110. cur += sprintf(cur, "<table cols='%d' cellpadding='0' cellspacing='0'>\n",
  111. qq->height);
  112. for(y = 0; y < qq->height; y++)
  113. {
  114. uint8_t *lineattr = qq->attr + y * qq->width;
  115. uint32_t *linechar = qq->chars + y * qq->width;
  116. cur += sprintf(cur, "<tr>");
  117. for(x = 0; x < qq->width; x += len)
  118. {
  119. unsigned int i;
  120. /* Use colspan option to factorize cells with same attributes
  121. * (see below) */
  122. len = 1;
  123. while(x + len < qq->width && lineattr[x + len] == lineattr[x])
  124. len++;
  125. cur += sprintf(cur, "<td bgcolor=#%06x", palette[lineattr[x] >> 4]);
  126. if(len > 1)
  127. cur += sprintf(cur, " colspan=%d", len);
  128. cur += sprintf(cur, "><font color=#%06x>",
  129. palette[lineattr[x] & 0x0f]);
  130. for(i = 0; i < len; i++)
  131. {
  132. if(linechar[x + i] == (uint32_t)' ')
  133. cur += sprintf(cur, "&nbsp;");
  134. else
  135. cur += sprintf(cur, "%c", linechar[x + i] & 0x7f);
  136. }
  137. cur += sprintf(cur, "</font></td>");
  138. }
  139. cur += sprintf(cur, "</tr>\n");
  140. }
  141. /* Footer */
  142. cur += sprintf(cur, "</table>\n");
  143. /* Crop to really used size */
  144. *size = (strlen(buffer) + 1) * sizeof(char);
  145. buffer = realloc(buffer, *size);
  146. return buffer;
  147. }
  148. /** \brief Generate IRC representation of current image.
  149. *
  150. * This function generates and returns an IRC representation of
  151. * the current image.
  152. */
  153. char* cucul_get_irc(cucul_t *qq, int *size)
  154. {
  155. static int const palette[] =
  156. {
  157. 1, 2, 3, 10, 5, 6, 7, 15, /* Dark */
  158. 14, 12, 9, 11, 4, 13, 8, 0, /* Light */
  159. };
  160. char *buffer, *cur;
  161. unsigned int x, y;
  162. /* 11 bytes assumed for max length per pixel. Worst case scenario:
  163. * ^Cxx,yy 6 bytes
  164. * ^B^B 2 bytes
  165. * c 1 byte
  166. * \r\n 2 bytes
  167. * In real life, the average bytes per pixel value will be around 5.
  168. */
  169. buffer = malloc((2 + (qq->width * qq->height * 11)) * sizeof(char));
  170. cur = buffer;
  171. *cur++ = '\x0f';
  172. for(y = 0; y < qq->height; y++)
  173. {
  174. uint8_t *lineattr = qq->attr + y * qq->width;
  175. uint32_t *linechar = qq->chars + y * qq->width;
  176. uint8_t prevfg = -1;
  177. uint8_t prevbg = -1;
  178. for(x = 0; x < qq->width; x++)
  179. {
  180. uint8_t fg = palette[lineattr[x] & 0x0f];
  181. uint8_t bg = palette[lineattr[x] >> 4];
  182. uint32_t c = linechar[x];
  183. if(bg == prevbg)
  184. {
  185. if(fg == prevfg)
  186. ; /* Same fg/bg, do nothing */
  187. else if(c == (uint32_t)' ')
  188. fg = prevfg; /* Hackety hack */
  189. else
  190. {
  191. cur += sprintf(cur, "\x03%d", fg);
  192. if(c >= (uint32_t)'0' && c <= (uint32_t)'9')
  193. cur += sprintf(cur, "\x02\x02");
  194. }
  195. }
  196. else
  197. {
  198. if(fg == prevfg)
  199. cur += sprintf(cur, "\x03,%d", bg);
  200. else
  201. cur += sprintf(cur, "\x03%d,%d", fg, bg);
  202. if(c >= (uint32_t)'0' && c <= (uint32_t)'9')
  203. cur += sprintf(cur, "\x02\x02");
  204. }
  205. *cur++ = c & 0x7f;
  206. prevfg = fg;
  207. prevbg = bg;
  208. }
  209. *cur++ = '\r';
  210. *cur++ = '\n';
  211. }
  212. *cur++ = '\x0f';
  213. /* Crop to really used size */
  214. *size = (strlen(buffer) + 1) * sizeof(char);
  215. buffer = realloc(buffer, *size);
  216. return buffer;
  217. }
  218. /** \brief Generate ANSI representation of current image.
  219. *
  220. * This function generates and returns an ANSI representation of
  221. * the current image.
  222. * \param trailing if 0, raw ANSI will be generated. Otherwise, you'll be
  223. * able to cut/paste the result to a function like printf
  224. * \return buffer containing generated ANSI codes as a big string
  225. */
  226. char * cucul_get_ansi(cucul_t *qq, int trailing, int *size)
  227. {
  228. static int const palette[] =
  229. {
  230. 30, 34, 32, 36, 31, 35, 33, 37, /* Both lines (light and dark) are the same, */
  231. 30, 34, 32, 36, 31, 35, 33, 37, /* light colors handling is done later */
  232. };
  233. char *cur;
  234. unsigned int x, y;
  235. /* 20 bytes assumed for max length per pixel.
  236. * Add height*9 to that (zeroes color at the end and jump to next line) */
  237. if(qq->ansi_buffer)
  238. free(qq->ansi_buffer);
  239. qq->ansi_buffer = malloc(((qq->height*9) + (qq->width * qq->height * 20)) * sizeof(char));
  240. cur = qq->ansi_buffer;
  241. // *cur++ = '';
  242. for(y = 0; y < qq->height; y++)
  243. {
  244. uint8_t *lineattr = qq->attr + y * qq->width;
  245. uint32_t *linechar = qq->chars + y * qq->width;
  246. uint8_t prevfg = -1;
  247. uint8_t prevbg = -1;
  248. for(x = 0; x < qq->width; x++)
  249. {
  250. uint8_t fg = palette[lineattr[x] & 0x0f];
  251. uint8_t bg = (palette[lineattr[x] >> 4])+10;
  252. uint32_t c = linechar[x];
  253. if(!trailing)
  254. cur += sprintf(cur, "\033[");
  255. else
  256. cur += sprintf(cur, "\\033[");
  257. if(fg > 7)
  258. cur += sprintf(cur, "1;%d;%dm",fg,bg);
  259. else
  260. cur += sprintf(cur, "0;%d;%dm",fg,bg);
  261. *cur++ = c & 0x7f;
  262. if((c == '%') && trailing)
  263. *cur++ = c & 0x7f;
  264. prevfg = fg;
  265. prevbg = bg;
  266. }
  267. if(!trailing)
  268. cur += sprintf(cur, "\033[0m\n\r");
  269. else
  270. cur += sprintf(cur, "\\033[0m\\n\n");
  271. }
  272. /* Crop to really used size */
  273. *size = (strlen(qq->ansi_buffer) + 1)* sizeof(char);
  274. qq->ansi_buffer = realloc(qq->ansi_buffer, *size);
  275. return qq->ansi_buffer;
  276. }