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.
 
 
 
 
 
 

467 lignes
13 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. /** \brief Load a memory area into a canvas.
  64. *
  65. * This function loads a memory area containing an exported canvas into
  66. * a new \e libcucul canvas.
  67. *
  68. * \param data The memory area to be loaded into a canvas.
  69. * \param size The length of the memory area.
  70. * \return A libcucul canvas, or NULL in case of error.
  71. */
  72. cucul_t *cucul_load(void *data, unsigned int size)
  73. {
  74. cucul_t *qq;
  75. uint8_t *buf = (uint8_t *)data;
  76. unsigned int width, height, n;
  77. if(size < 12)
  78. return NULL;
  79. if(buf[0] != 'C' || buf[1] != 'A' || buf[2] != 'C' || buf[3] != 'A')
  80. return NULL;
  81. width = ((uint32_t)buf[4] << 24) | ((uint32_t)buf[5] << 16)
  82. | ((uint32_t)buf[6] << 8) | (uint32_t)buf[7];
  83. height = ((uint32_t)buf[8] << 24) | ((uint32_t)buf[9] << 16)
  84. | ((uint32_t)buf[10] << 8) | (uint32_t)buf[11];
  85. if(!width || !height)
  86. return NULL;
  87. if(size != 12 + width * height * 8 + 4)
  88. return NULL;
  89. if(buf[size - 4] != 'A' || buf[size - 3] != 'C'
  90. || buf[size - 2] != 'A' || buf[size - 1] != 'C')
  91. return NULL;
  92. qq = cucul_create(width, height);
  93. if(!qq)
  94. return NULL;
  95. for(n = height * width; n--; )
  96. {
  97. qq->chars[n] = ((uint32_t)buf[12 + 8 * n] << 24)
  98. | ((uint32_t)buf[13 + 8 * n] << 16)
  99. | ((uint32_t)buf[14 + 8 * n] << 8)
  100. | (uint32_t)buf[15 + 8 * n];
  101. qq->attr[n] = ((uint32_t)buf[16 + 8 * n] << 24)
  102. | ((uint32_t)buf[17 + 8 * n] << 16)
  103. | ((uint32_t)buf[18 + 8 * n] << 8)
  104. | (uint32_t)buf[19 + 8 * n];
  105. }
  106. return qq;
  107. }
  108. /** \brief Resize a canvas.
  109. *
  110. * This function sets the canvas width and height, in character cells.
  111. *
  112. * The contents of the canvas are preserved to the extent of the new
  113. * canvas size. Newly allocated character cells at the right and/or at
  114. * the bottom of the canvas are filled with spaces.
  115. *
  116. * It is an error to try to resize the canvas if an output driver has
  117. * been attached to the canvas using caca_attach(). You need to remove
  118. * the output driver using caca_detach() before you can change the
  119. * canvas size again. However, the caca output driver can cause a canvas
  120. * resize through user interaction. See the caca_event() documentation
  121. * for more about this.
  122. *
  123. * \param qq A libcucul canvas
  124. * \param width The desired canvas width
  125. * \param height The desired canvas height
  126. */
  127. void cucul_set_size(cucul_t *qq, unsigned int width, unsigned int height)
  128. {
  129. if(qq->refcount)
  130. return;
  131. _cucul_set_size(qq, width, height);
  132. }
  133. /** \brief Get the canvas width.
  134. *
  135. * This function returns the current canvas width, in character cells.
  136. *
  137. * \param qq A libcucul canvas
  138. * \return The canvas width.
  139. */
  140. unsigned int cucul_get_width(cucul_t *qq)
  141. {
  142. return qq->width;
  143. }
  144. /** \brief Get the canvas height.
  145. *
  146. * This function returns the current canvas height, in character cells.
  147. *
  148. * \param qq A libcucul canvas
  149. * \return The canvas height.
  150. */
  151. unsigned int cucul_get_height(cucul_t *qq)
  152. {
  153. return qq->height;
  154. }
  155. /** \brief Translate a colour index into the colour's name.
  156. *
  157. * This function translates a cucul_color enum into a human-readable
  158. * description string of the associated colour.
  159. *
  160. * \param color The colour value.
  161. * \return A static string containing the colour's name.
  162. */
  163. char const *cucul_get_color_name(unsigned int color)
  164. {
  165. static char const *color_names[] =
  166. {
  167. "black",
  168. "blue",
  169. "green",
  170. "cyan",
  171. "red",
  172. "magenta",
  173. "brown",
  174. "light gray",
  175. "dark gray",
  176. "light blue",
  177. "light green",
  178. "light cyan",
  179. "light red",
  180. "light magenta",
  181. "yellow",
  182. "white",
  183. };
  184. if(color < 0 || color > 15)
  185. return "unknown";
  186. return color_names[color];
  187. }
  188. /** \brief Uninitialise \e libcucul.
  189. *
  190. * This function frees all resources allocated by cucul_create(). After
  191. * cucul_free() has been called, no other \e libcucul functions may be used
  192. * unless a new call to cucul_create() is done.
  193. *
  194. * \param qq A libcucul canvas
  195. */
  196. void cucul_free(cucul_t *qq)
  197. {
  198. _cucul_end_dither();
  199. free(qq->empty_line);
  200. free(qq->scratch_line);
  201. free(qq->chars);
  202. free(qq->attr);
  203. free(qq);
  204. }
  205. /** \brief Generate a random integer within a range.
  206. *
  207. * \param min The lower bound of the integer range.
  208. * \param max The upper bound of the integer range.
  209. * \return A random integer comprised between \p min and \p max, inclusive.
  210. */
  211. int cucul_rand(int min, int max)
  212. {
  213. return min + (int)((1.0*(max-min+1)) * rand() / (RAND_MAX+1.0));
  214. }
  215. /** \brief Export a canvas into a foreign format.
  216. *
  217. * This function exports a libcucul canvas into various foreign formats such
  218. * as ANSI art, HTML, IRC colours, etc. One should use cucul_get_buffer_data()
  219. * and cucul_get_buffer_size() to access the buffer contents. The allocated
  220. * data is valid until cucul_free_buffer() is called.
  221. *
  222. * Valid values for \e format are:
  223. *
  224. * \li \e "ansi": export ANSI art (CP437 charset with ANSI colour codes).
  225. *
  226. * \li \e "html": export an HTML page with CSS information.
  227. *
  228. * \li \e "html3": export an HTML table that should be compatible with
  229. * most navigators, including textmode ones.
  230. *
  231. * \li \e "irc": export UTF-8 text with mIRC colour codes.
  232. *
  233. * \li \e "ps": export a PostScript document.
  234. *
  235. * \li \e "svg": export an SVG vector image.
  236. *
  237. * \li \e "tga": export a TGA image.
  238. *
  239. * \param qq A libcucul canvas
  240. * \param format A string describing the requested output format.
  241. */
  242. cucul_buffer_t * cucul_create_export(cucul_t *qq, char const *format)
  243. {
  244. cucul_buffer_t *ex;
  245. ex = malloc(sizeof(cucul_buffer_t));
  246. ex->size = 0;
  247. ex->data = NULL;
  248. if(!strcasecmp("ansi", format))
  249. _cucul_get_ansi(qq, ex);
  250. else if(!strcasecmp("html", format))
  251. _cucul_get_html(qq, ex);
  252. else if(!strcasecmp("html3", format))
  253. _cucul_get_html3(qq, ex);
  254. else if(!strcasecmp("irc", format))
  255. _cucul_get_irc(qq, ex);
  256. else if(!strcasecmp("ps", format))
  257. _cucul_get_ps(qq, ex);
  258. else if(!strcasecmp("svg", format))
  259. _cucul_get_svg(qq, ex);
  260. else if(!strcasecmp("tga", format))
  261. _cucul_get_tga(qq, ex);
  262. if(ex->size == 0)
  263. {
  264. free(ex);
  265. return NULL;
  266. }
  267. return ex;
  268. }
  269. /** \brief Get available export formats
  270. *
  271. * Return a list of available export formats. The list is a NULL-terminated
  272. * array of strings, interleaving a string containing the internal value for
  273. * the export format, to be used with \e cucul_create_export(), and a string
  274. * containing the natural language description for that export format.
  275. *
  276. * \return An array of strings.
  277. */
  278. char const * const * cucul_get_export_list(void)
  279. {
  280. static char const * const list[] =
  281. {
  282. "ansi", "ANSI",
  283. "html", "HTML",
  284. "html3", "backwards-compatible HTML",
  285. "irc", "IRC with mIRC colours",
  286. "ps", "PostScript document",
  287. "svg", "SVG vector image",
  288. "tga", "TGA image",
  289. NULL, NULL
  290. };
  291. return list;
  292. }
  293. /** \brief Get the buffer size.
  294. *
  295. * This function returns the length (in bytes) of the memory area stored
  296. * in the given \e libcucul buffer.
  297. *
  298. * \param buf A \e libcucul buffer
  299. * \return The buffer data length.
  300. */
  301. unsigned long int cucul_get_buffer_size(cucul_buffer_t *buf)
  302. {
  303. return buf->size;
  304. }
  305. /** \brief Get the buffer data.
  306. *
  307. * This function returns a pointer to the memory area stored in the given
  308. * \e libcucul buffer.
  309. *
  310. * \param buf A \e libcucul buffer
  311. * \return A pointer to the buffer memory area.
  312. */
  313. void * cucul_get_buffer_data(cucul_buffer_t *buf)
  314. {
  315. return buf->data;
  316. }
  317. /** \brief Free a buffer.
  318. *
  319. * This function frees the structures associated with the given
  320. * \e libcucul buffer.
  321. *
  322. * \param buf A \e libcucul buffer
  323. */
  324. void cucul_free_buffer(cucul_buffer_t *buf)
  325. {
  326. free(buf->data);
  327. free(buf);
  328. }
  329. /*
  330. * XXX: The following functions are local.
  331. */
  332. void _cucul_set_size(cucul_t *qq, unsigned int width, unsigned int height)
  333. {
  334. unsigned int x, y, old_width, old_height, new_size, old_size;
  335. old_width = qq->width;
  336. old_height = qq->height;
  337. old_size = old_width * old_height;
  338. qq->width = width;
  339. qq->height = height;
  340. new_size = width * height;
  341. /* Step 1: if new area is bigger, resize the memory area now. */
  342. if(new_size > old_size)
  343. {
  344. qq->chars = realloc(qq->chars, new_size * sizeof(uint32_t));
  345. qq->attr = realloc(qq->attr, new_size * sizeof(uint32_t));
  346. }
  347. /* Step 2: move line data if necessary. */
  348. if(width == old_width)
  349. {
  350. /* Width did not change, which means we do not need to move data. */
  351. ;
  352. }
  353. else if(width > old_width)
  354. {
  355. /* New width is bigger than old width, which means we need to
  356. * copy lines starting from the bottom of the screen otherwise
  357. * we will overwrite information. */
  358. for(y = height < old_height ? height : old_height; y--; )
  359. {
  360. for(x = old_width; x--; )
  361. {
  362. qq->chars[y * width + x] = qq->chars[y * old_width + x];
  363. qq->attr[y * width + x] = qq->attr[y * old_width + x];
  364. }
  365. /* Zero the end of the line */
  366. for(x = width - old_width; x--; )
  367. qq->chars[y * width + old_width + x] = (uint32_t)' ';
  368. memset(qq->attr + y * width + old_width, 0,
  369. (width - old_width) * 4);
  370. }
  371. }
  372. else
  373. {
  374. /* New width is smaller. Copy as many lines as possible. Ignore
  375. * the first line, it is already in place. */
  376. unsigned int lines = height < old_height ? height : old_height;
  377. for(y = 1; y < lines; y++)
  378. {
  379. for(x = 0; x < width; x++)
  380. {
  381. qq->chars[y * width + x] = qq->chars[y * old_width + x];
  382. qq->attr[y * width + x] = qq->attr[y * old_width + x];
  383. }
  384. }
  385. }
  386. /* Step 3: fill the bottom of the new screen if necessary. */
  387. if(height > old_height)
  388. {
  389. /* Zero the bottom of the screen */
  390. for(x = (height - old_height) * width; x--; )
  391. qq->chars[old_height * width + x] = (uint32_t)' ';
  392. memset(qq->attr + old_height * width, 0,
  393. (height - old_height) * width * 4);
  394. }
  395. /* Step 4: if new area is smaller, resize memory area now. */
  396. if(new_size <= old_size)
  397. {
  398. qq->chars = realloc(qq->chars, new_size * sizeof(uint32_t));
  399. qq->attr = realloc(qq->attr, new_size * sizeof(uint32_t));
  400. }
  401. /* Recompute the scratch line and the empty line */
  402. if(width != old_width)
  403. {
  404. qq->empty_line = realloc(qq->empty_line, width + 1);
  405. memset(qq->empty_line, ' ', width);
  406. qq->empty_line[width] = '\0';
  407. qq->scratch_line = realloc(qq->scratch_line, width + 1);
  408. }
  409. }