25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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