No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

331 líneas
9.5 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 char.c
  12. * \version \$Id$
  13. * \author Sam Hocevar <sam@zoy.org>
  14. * \brief Character drawing
  15. *
  16. * This file contains character and string drawing functions.
  17. */
  18. #include "config.h"
  19. #if defined(HAVE_INTTYPES_H) || defined(_DOXYGEN_SKIP_ME)
  20. # include <inttypes.h>
  21. #else
  22. typedef unsigned int uint32_t;
  23. typedef unsigned char uint8_t;
  24. #endif
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include <string.h>
  28. #include "cucul.h"
  29. #include "cucul_internals.h"
  30. /* HTML */
  31. /** \brief Generate HTML representation of current image.
  32. *
  33. * This function generates and returns the HTML representation of
  34. * the current image.
  35. */
  36. char* cucul_get_html(cucul_t *qq)
  37. {
  38. static int const palette[] =
  39. {
  40. 0x000, 0x008, 0x080, 0x088, 0x800, 0x808, 0x880, 0x888,
  41. 0x444, 0x44f, 0x4f4, 0x4ff, 0xf44, 0xf4f, 0xff4, 0xfff,
  42. };
  43. char *buffer, *cur;
  44. unsigned int x, y, len;
  45. /* 13000 -> css palette
  46. * 40 -> max size used for a pixel (plus 10, never know)*/
  47. /* FIXME: Check this value */
  48. buffer = malloc((13000 + ((qq->width*qq->height) * 40)) * sizeof(char));
  49. cur = buffer;
  50. /* HTML header */
  51. cur += sprintf(cur, "<html>\n<head>\n<title>Generated by libcaca %s</title>\n", VERSION);
  52. /* CSS */
  53. cur += sprintf(cur, "<style>\n");
  54. cur += sprintf(cur, ".caca { font-family: monospace, fixed; font-weight: bold; }");
  55. for(x = 0; x < 0x100; x++)
  56. {
  57. cur += sprintf(cur, ".b%02x { color:#%03x; background-color:#%03x; }\n",
  58. x, palette[x & 0xf ], palette[x >> 4]);
  59. }
  60. cur += sprintf(cur, "</style>\n</head>\n<body>\n");
  61. cur += sprintf(cur, "<div cellpadding='0' cellspacing='0' style='%s'>\n",
  62. "font-family: monospace, fixed; font-weight: bold;");
  63. for(y = 0; y < qq->height; y++)
  64. {
  65. uint8_t *lineattr = qq->attr + y * qq->width;
  66. uint32_t *linechar = qq->chars + y * qq->width;
  67. for(x = 0; x < qq->width; x += len)
  68. {
  69. cur += sprintf(cur, "<span class='b%02x'>", lineattr[x]);
  70. for(len = 0;
  71. x + len < qq->width && lineattr[x + len] == lineattr[x];
  72. len++)
  73. {
  74. if(linechar[x + len] == (uint32_t)' ')
  75. cur += sprintf(cur, "&nbsp;");
  76. else
  77. cur += sprintf(cur, "%c", linechar[x + len] & 0x7f);
  78. }
  79. cur += sprintf(cur, "</span>");
  80. }
  81. /* New line */
  82. cur += sprintf(cur, "<br />\n");
  83. }
  84. cur += sprintf(cur, "</div></body></html>\n");
  85. /* Crop to really used size */
  86. buffer = realloc(buffer, (strlen(buffer) + 1) * sizeof(char));
  87. return buffer;
  88. }
  89. /** \brief Generate HTML3 representation of current image.
  90. *
  91. * This function generates and returns the HTML3 representation of
  92. * the current image. It is way bigger than cucul_get_html(), but
  93. * permits viewing in old browsers (or limited ones such as links)
  94. * Won't work under gecko (mozilla rendering engine) unless you set
  95. * a correct header.
  96. */
  97. char* cucul_get_html3(cucul_t *qq)
  98. {
  99. static int const palette[] =
  100. {
  101. 0x000000, 0x000088, 0x008800, 0x008888,
  102. 0x880000, 0x880088, 0x888800, 0x888888,
  103. 0x444444, 0x4444ff, 0x44ff44, 0x44ffff,
  104. 0xff4444, 0xff44ff, 0xffff44, 0xffffff,
  105. };
  106. char *buffer, *cur;
  107. unsigned int x, y, len;
  108. /* 13000 -> css palette
  109. * 40 -> max size used for a pixel (plus 10, never know) */
  110. buffer = malloc((13000 + ((qq->width*qq->height)*40))*sizeof(char));
  111. cur = buffer;
  112. /* Table */
  113. cur += sprintf(cur, "<table cols='%d' cellpadding='0' cellspacing='0'>\n",
  114. qq->height);
  115. for(y = 0; y < qq->height; y++)
  116. {
  117. uint8_t *lineattr = qq->attr + y * qq->width;
  118. uint32_t *linechar = qq->chars + y * qq->width;
  119. cur += sprintf(cur, "<tr>");
  120. for(x = 0; x < qq->width; x += len)
  121. {
  122. unsigned int i;
  123. /* Use colspan option to factorize cells with same attributes
  124. * (see below) */
  125. len = 1;
  126. while(x + len < qq->width && lineattr[x + len] == lineattr[x])
  127. len++;
  128. cur += sprintf(cur, "<td bgcolor=#%06x", palette[lineattr[x] >> 4]);
  129. if(len > 1)
  130. cur += sprintf(cur, " colspan=%d", len);
  131. cur += sprintf(cur, "><font color=#%06x>",
  132. palette[lineattr[x] & 0x0f]);
  133. for(i = 0; i < len; i++)
  134. {
  135. if(linechar[x + i] == (uint32_t)' ')
  136. cur += sprintf(cur, "&nbsp;");
  137. else
  138. cur += sprintf(cur, "%c", linechar[x + i] & 0x7f);
  139. }
  140. cur += sprintf(cur, "</font></td>");
  141. }
  142. cur += sprintf(cur, "</tr>\n");
  143. }
  144. /* Footer */
  145. cur += sprintf(cur, "</table>\n");
  146. /* Crop to really used size */
  147. buffer = realloc(buffer, (strlen(buffer) + 1) * sizeof(char));
  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)
  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. buffer = realloc(buffer, (strlen(buffer) + 1) * sizeof(char));
  217. return buffer;
  218. }
  219. /** \brief Generate ANSI representation of current image.
  220. *
  221. * This function generates and returns an ANSI representation of
  222. * the current image.
  223. * \param trailing if 0, raw ANSI will be generated. Otherwise, you'll be
  224. * able to cut/paste the result to a function like printf
  225. * \return buffer containing generated ANSI codes as a big string
  226. */
  227. char * cucul_get_ansi(cucul_t *qq, int trailing)
  228. {
  229. static int const palette[] =
  230. {
  231. 30, 34, 32, 36, 31, 35, 33, 37, /* Both lines (light and dark) are the same, */
  232. 30, 34, 32, 36, 31, 35, 33, 37, /* light colors handling is done later */
  233. };
  234. char *buffer, *cur;
  235. unsigned int x, y;
  236. /* 20 bytes assumed for max length per pixel.
  237. * Add height*9 to that (zeroes color at the end and jump to next line) */
  238. buffer = malloc(((qq->height*9) + (qq->width * qq->height * 20)) * sizeof(char));
  239. cur = buffer;
  240. // *cur++ = '';
  241. for(y = 0; y < qq->height; y++)
  242. {
  243. uint8_t *lineattr = qq->attr + y * qq->width;
  244. uint32_t *linechar = qq->chars + y * qq->width;
  245. uint8_t prevfg = -1;
  246. uint8_t prevbg = -1;
  247. for(x = 0; x < qq->width; x++)
  248. {
  249. uint8_t fg = palette[lineattr[x] & 0x0f];
  250. uint8_t bg = (palette[lineattr[x] >> 4])+10;
  251. uint32_t c = linechar[x];
  252. if(!trailing)
  253. cur += sprintf(cur, "\033[");
  254. else
  255. cur += sprintf(cur, "\\033[");
  256. if(fg > 7)
  257. cur += sprintf(cur, "1;%d;%dm",fg,bg);
  258. else
  259. cur += sprintf(cur, "0;%d;%dm",fg,bg);
  260. *cur++ = c & 0x7f;
  261. if((c == '%') && trailing)
  262. *cur++ = c & 0x7f;
  263. prevfg = fg;
  264. prevbg = bg;
  265. }
  266. if(!trailing)
  267. cur += sprintf(cur, "\033[0m\n\r");
  268. else
  269. cur += sprintf(cur, "\\033[0m\\n\n");
  270. }
  271. /* Crop to really used size */
  272. buffer = realloc(buffer, (strlen(buffer) + 1) * sizeof(char));
  273. return buffer;
  274. }