Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

381 lignes
10 KiB

  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. /*
  14. * This file contains the main functions used by \e libcucul applications
  15. * to initialise a drawing context.
  16. */
  17. #include "config.h"
  18. #if !defined(__KERNEL__)
  19. # include <stdio.h>
  20. # include <stdlib.h>
  21. # include <string.h>
  22. #endif
  23. #include "cucul.h"
  24. #include "cucul_internals.h"
  25. /** \brief Initialise a \e libcucul canvas.
  26. *
  27. * This function initialises internal \e libcucul structures and the backend
  28. * that will be used for subsequent graphical operations. It must be the
  29. * first \e libcucul function to be called in a function. cucul_free() should
  30. * be called at the end of the program to free all allocated resources.
  31. *
  32. * If one of the desired canvas coordinates is zero, a default canvas size
  33. * of 80x32 is used instead.
  34. *
  35. * \param width The desired canvas width
  36. * \param height The desired canvas height
  37. * \return A libcucul canvas handle upon success, NULL if an error occurred.
  38. */
  39. cucul_t * cucul_create(unsigned int width, unsigned int height)
  40. {
  41. cucul_t *qq = malloc(sizeof(cucul_t));
  42. qq->refcount = 0;
  43. qq->fgcolor = CUCUL_COLOR_LIGHTGRAY;
  44. qq->bgcolor = CUCUL_COLOR_BLACK;
  45. qq->width = qq->height = 0;
  46. qq->chars = NULL;
  47. qq->attr = NULL;
  48. qq->empty_line = qq->scratch_line = NULL;
  49. /* Initialise to a default size. 80x32 is arbitrary but matches AAlib's
  50. * default X11 window. When a graphic driver attaches to us, it can set
  51. * a different size. */
  52. if(width && height)
  53. _cucul_set_size(qq, width, height);
  54. else
  55. _cucul_set_size(qq, 80, 32);
  56. if(_cucul_init_dither())
  57. {
  58. free(qq);
  59. return NULL;
  60. }
  61. return qq;
  62. }
  63. cucul_t *cucul_load(void *data, unsigned int size)
  64. {
  65. cucul_t *qq;
  66. uint8_t *buf = (uint8_t *)data;
  67. unsigned int width, height, n;
  68. if(size < 12)
  69. return NULL;
  70. if(buf[0] != 'C' || buf[1] != 'A' || buf[2] != 'C' || buf[3] != 'A')
  71. return NULL;
  72. width = ((uint32_t)buf[4] << 24) | ((uint32_t)buf[5] << 16)
  73. | ((uint32_t)buf[6] << 8) | (uint32_t)buf[7];
  74. height = ((uint32_t)buf[8] << 24) | ((uint32_t)buf[9] << 16)
  75. | ((uint32_t)buf[10] << 8) | (uint32_t)buf[11];
  76. if(!width || !height)
  77. return NULL;
  78. if(size != 12 + width * height * 8 + 4)
  79. return NULL;
  80. if(buf[size - 4] != 'A' || buf[size - 3] != 'C'
  81. || buf[size - 2] != 'A' || buf[size - 1] != 'C')
  82. return NULL;
  83. qq = cucul_create(width, height);
  84. if(!qq)
  85. return NULL;
  86. for(n = height * width; n--; )
  87. {
  88. qq->chars[n] = ((uint32_t)buf[12 + 8 * n] << 24)
  89. | ((uint32_t)buf[13 + 8 * n] << 16)
  90. | ((uint32_t)buf[14 + 8 * n] << 8)
  91. | (uint32_t)buf[15 + 8 * n];
  92. qq->attr[n] = ((uint32_t)buf[16 + 8 * n] << 24)
  93. | ((uint32_t)buf[17 + 8 * n] << 16)
  94. | ((uint32_t)buf[18 + 8 * n] << 8)
  95. | (uint32_t)buf[19 + 8 * n];
  96. }
  97. return qq;
  98. }
  99. /** \brief Resize a canvas.
  100. *
  101. * This function sets the canvas width and height, in character cells.
  102. *
  103. * The contents of the canvas are preserved to the extent of the new
  104. * canvas size. Newly allocated character cells at the right and/or at
  105. * the bottom of the canvas are filled with spaces.
  106. *
  107. * It is an error to try to resize the canvas if an output driver has
  108. * been attached to the canvas using caca_attach(). You need to remove
  109. * the output driver using caca_detach() before you can change the
  110. * canvas size again. However, the caca output driver can cause a canvas
  111. * resize through user interaction. See the caca_event() documentation
  112. * for more about this.
  113. *
  114. * \param qq A libcucul canvas
  115. * \param width The desired canvas width
  116. * \param height The desired canvas height
  117. */
  118. void cucul_set_size(cucul_t *qq, unsigned int width, unsigned int height)
  119. {
  120. if(qq->refcount)
  121. return;
  122. _cucul_set_size(qq, width, height);
  123. }
  124. /** \brief Get the canvas width.
  125. *
  126. * This function returns the current canvas width, in character cells.
  127. *
  128. * \param qq A libcucul canvas
  129. * \return The canvas width.
  130. */
  131. unsigned int cucul_get_width(cucul_t *qq)
  132. {
  133. return qq->width;
  134. }
  135. /** \brief Get the canvas height.
  136. *
  137. * This function returns the current canvas height, in character cells.
  138. *
  139. * \param qq A libcucul canvas
  140. * \return The canvas height.
  141. */
  142. unsigned int cucul_get_height(cucul_t *qq)
  143. {
  144. return qq->height;
  145. }
  146. /** \brief Translate a colour index into the colour's name.
  147. *
  148. * This function translates a cucul_color enum into a human-readable
  149. * description string of the associated colour.
  150. *
  151. * \param color The colour value.
  152. * \return A static string containing the colour's name.
  153. */
  154. char const *cucul_get_color_name(unsigned int color)
  155. {
  156. static char const *color_names[] =
  157. {
  158. "black",
  159. "blue",
  160. "green",
  161. "cyan",
  162. "red",
  163. "magenta",
  164. "brown",
  165. "light gray",
  166. "dark gray",
  167. "light blue",
  168. "light green",
  169. "light cyan",
  170. "light red",
  171. "light magenta",
  172. "yellow",
  173. "white",
  174. };
  175. if(color < 0 || color > 15)
  176. return "unknown";
  177. return color_names[color];
  178. }
  179. /** \brief Uninitialise \e libcucul.
  180. *
  181. * This function frees all resources allocated by cucul_create(). After
  182. * cucul_free() has been called, no other \e libcucul functions may be used
  183. * unless a new call to cucul_create() is done.
  184. *
  185. * \param qq A libcucul canvas
  186. */
  187. void cucul_free(cucul_t *qq)
  188. {
  189. _cucul_end_dither();
  190. free(qq->empty_line);
  191. free(qq->scratch_line);
  192. free(qq->chars);
  193. free(qq->attr);
  194. free(qq);
  195. }
  196. struct cucul_buffer * cucul_create_export(cucul_t *qq, char const *format)
  197. {
  198. struct cucul_buffer *ex;
  199. ex = malloc(sizeof(struct cucul_buffer));
  200. if(!strcasecmp("ansi", format))
  201. _cucul_get_ansi(qq, ex);
  202. else if(!strcasecmp("html", format))
  203. _cucul_get_html(qq, ex);
  204. else if(!strcasecmp("html3", format))
  205. _cucul_get_html3(qq, ex);
  206. else if(!strcasecmp("irc", format))
  207. _cucul_get_irc(qq, ex);
  208. else if(!strcasecmp("ps", format))
  209. _cucul_get_ps(qq, ex);
  210. else if(!strcasecmp("svg", format))
  211. _cucul_get_svg(qq, ex);
  212. else
  213. {
  214. free(ex);
  215. return NULL;
  216. }
  217. return ex;
  218. }
  219. /** \brief Get available export formats
  220. *
  221. * Return a list of available export formats. The list is a NULL-terminated
  222. * array of strings, interleaving a string containing the internal value for
  223. * the export format, to be used with \e cucul_create_export(), and a string
  224. * containing the natural language description for that export format.
  225. *
  226. * \return An array of strings.
  227. */
  228. char const * const * cucul_get_export_list(void)
  229. {
  230. static char const * const list[] =
  231. {
  232. "ansi", "ANSI",
  233. "html", "HTML",
  234. "html3", "backwards-compatible HTML",
  235. "irc", "IRC (mIRC colours)",
  236. "ps", "PostScript",
  237. "svg", "SVG",
  238. NULL, NULL
  239. };
  240. return list;
  241. }
  242. void cucul_free_export(struct cucul_buffer *ex)
  243. {
  244. free(ex->data);
  245. free(ex);
  246. }
  247. /*
  248. * XXX: The following functions are local.
  249. */
  250. void _cucul_set_size(cucul_t *qq, unsigned int width, unsigned int height)
  251. {
  252. unsigned int x, y, old_width, old_height, new_size, old_size;
  253. old_width = qq->width;
  254. old_height = qq->height;
  255. old_size = old_width * old_height;
  256. qq->width = width;
  257. qq->height = height;
  258. new_size = width * height;
  259. /* Step 1: if new area is bigger, resize the memory area now. */
  260. if(new_size > old_size)
  261. {
  262. qq->chars = realloc(qq->chars, new_size * sizeof(uint32_t));
  263. qq->attr = realloc(qq->attr, new_size * sizeof(uint32_t));
  264. }
  265. /* Step 2: move line data if necessary. */
  266. if(width == old_width)
  267. {
  268. /* Width did not change, which means we do not need to move data. */
  269. ;
  270. }
  271. else if(width > old_width)
  272. {
  273. /* New width is bigger than old width, which means we need to
  274. * copy lines starting from the bottom of the screen otherwise
  275. * we will overwrite information. */
  276. for(y = height < old_height ? height : old_height; y--; )
  277. {
  278. for(x = old_width; x--; )
  279. {
  280. qq->chars[y * width + x] = qq->chars[y * old_width + x];
  281. qq->attr[y * width + x] = qq->attr[y * old_width + x];
  282. }
  283. /* Zero the end of the line */
  284. for(x = width - old_width; x--; )
  285. qq->chars[y * width + old_width + x] = (uint32_t)' ';
  286. memset(qq->attr + y * width + old_width, 0,
  287. (width - old_width) * 4);
  288. }
  289. }
  290. else
  291. {
  292. /* New width is smaller. Copy as many lines as possible. Ignore
  293. * the first line, it is already in place. */
  294. unsigned int lines = height < old_height ? height : old_height;
  295. for(y = 1; y < lines; y++)
  296. {
  297. for(x = 0; x < width; x++)
  298. {
  299. qq->chars[y * width + x] = qq->chars[y * old_width + x];
  300. qq->attr[y * width + x] = qq->attr[y * old_width + x];
  301. }
  302. }
  303. }
  304. /* Step 3: fill the bottom of the new screen if necessary. */
  305. if(height > old_height)
  306. {
  307. /* Zero the bottom of the screen */
  308. for(x = (height - old_height) * width; x--; )
  309. qq->chars[old_height * width + x] = (uint32_t)' ';
  310. memset(qq->attr + old_height * width, 0,
  311. (height - old_height) * width * 4);
  312. }
  313. /* Step 4: if new area is smaller, resize memory area now. */
  314. if(new_size <= old_size)
  315. {
  316. qq->chars = realloc(qq->chars, new_size * sizeof(uint32_t));
  317. qq->attr = realloc(qq->attr, new_size * sizeof(uint32_t));
  318. }
  319. /* Recompute the scratch line and the empty line */
  320. if(width != old_width)
  321. {
  322. qq->empty_line = realloc(qq->empty_line, width + 1);
  323. memset(qq->empty_line, ' ', width);
  324. qq->empty_line[width] = '\0';
  325. qq->scratch_line = realloc(qq->scratch_line, width + 1);
  326. }
  327. }