25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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