You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

282 lines
8.0 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_canvas()
  30. * should 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_canvas_t * cucul_create_canvas(unsigned int width, unsigned int height)
  40. {
  41. cucul_canvas_t *cv = malloc(sizeof(cucul_canvas_t));
  42. cv->refcount = 0;
  43. cv->fgcolor = CUCUL_COLOR_LIGHTGRAY;
  44. cv->bgcolor = CUCUL_COLOR_BLACK;
  45. cv->width = cv->height = 0;
  46. cv->chars = NULL;
  47. cv->attr = NULL;
  48. /* Initialise to a default size. 80x32 is arbitrary but matches AAlib's
  49. * default X11 window. When a graphic driver attaches to us, it can set
  50. * a different size. */
  51. if(width && height)
  52. _cucul_set_canvas_size(cv, width, height);
  53. else
  54. _cucul_set_canvas_size(cv, 80, 32);
  55. if(_cucul_init_dither())
  56. {
  57. free(cv);
  58. return NULL;
  59. }
  60. return cv;
  61. }
  62. /** \brief Resize a canvas.
  63. *
  64. * This function sets the canvas width and height, in character cells.
  65. *
  66. * The contents of the canvas are preserved to the extent of the new
  67. * canvas size. Newly allocated character cells at the right and/or at
  68. * the bottom of the canvas are filled with spaces.
  69. *
  70. * It is an error to try to resize the canvas if an output driver has
  71. * been attached to the canvas using caca_create_display(). You need to
  72. * remove the output driver using caca_free_display() before you can change
  73. * the canvas size again. However, the caca output driver can cause a
  74. * canvas resize through user interaction. See the caca_event() documentation
  75. * for more about this.
  76. *
  77. * \param cv A libcucul canvas
  78. * \param width The desired canvas width
  79. * \param height The desired canvas height
  80. */
  81. void cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
  82. unsigned int height)
  83. {
  84. if(cv->refcount)
  85. return;
  86. _cucul_set_canvas_size(cv, width, height);
  87. }
  88. /** \brief Get the canvas width.
  89. *
  90. * This function returns the current canvas width, in character cells.
  91. *
  92. * \param cv A libcucul canvas
  93. * \return The canvas width.
  94. */
  95. unsigned int cucul_get_canvas_width(cucul_canvas_t *cv)
  96. {
  97. return cv->width;
  98. }
  99. /** \brief Get the canvas height.
  100. *
  101. * This function returns the current canvas height, in character cells.
  102. *
  103. * \param cv A libcucul canvas
  104. * \return The canvas height.
  105. */
  106. unsigned int cucul_get_canvas_height(cucul_canvas_t *cv)
  107. {
  108. return cv->height;
  109. }
  110. /** \brief Translate a colour index into the colour's name.
  111. *
  112. * This function translates a cucul_color enum into a human-readable
  113. * description string of the associated colour.
  114. *
  115. * \param color The colour value.
  116. * \return A static string containing the colour's name.
  117. */
  118. char const *cucul_get_color_name(unsigned int color)
  119. {
  120. static char const *color_names[] =
  121. {
  122. "black",
  123. "blue",
  124. "green",
  125. "cyan",
  126. "red",
  127. "magenta",
  128. "brown",
  129. "light gray",
  130. "dark gray",
  131. "light blue",
  132. "light green",
  133. "light cyan",
  134. "light red",
  135. "light magenta",
  136. "yellow",
  137. "white",
  138. };
  139. if(color < 0 || color > 15)
  140. return "unknown";
  141. return color_names[color];
  142. }
  143. /** \brief Uninitialise \e libcucul.
  144. *
  145. * This function frees all resources allocated by cucul_create_canvas(). After
  146. * cucul_free_canvas() has been called, no other \e libcucul functions may be
  147. * used unless a new call to cucul_create_canvas() is done.
  148. *
  149. * \param cv A libcucul canvas
  150. */
  151. void cucul_free_canvas(cucul_canvas_t *cv)
  152. {
  153. _cucul_end_dither();
  154. free(cv->scratch_line);
  155. free(cv->chars);
  156. free(cv->attr);
  157. free(cv);
  158. }
  159. /** \brief Generate a random integer within a range.
  160. *
  161. * \param min The lower bound of the integer range.
  162. * \param max The upper bound of the integer range.
  163. * \return A random integer comprised between \p min and \p max - 1
  164. * (inclusive).
  165. */
  166. int cucul_rand(int min, int max)
  167. {
  168. return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0));
  169. }
  170. /*
  171. * XXX: The following functions are local.
  172. */
  173. void _cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
  174. unsigned int height)
  175. {
  176. unsigned int x, y, old_width, old_height, new_size, old_size;
  177. old_width = cv->width;
  178. old_height = cv->height;
  179. old_size = old_width * old_height;
  180. cv->width = width;
  181. cv->height = height;
  182. new_size = width * height;
  183. /* Step 1: if new area is bigger, resize the memory area now. */
  184. if(new_size > old_size)
  185. {
  186. cv->chars = realloc(cv->chars, new_size * sizeof(uint32_t));
  187. cv->attr = realloc(cv->attr, new_size * sizeof(uint32_t));
  188. }
  189. /* Step 2: move line data if necessary. */
  190. if(width == old_width)
  191. {
  192. /* Width did not change, which means we do not need to move data. */
  193. ;
  194. }
  195. else if(width > old_width)
  196. {
  197. /* New width is bigger than old width, which means we need to
  198. * copy lines starting from the bottom of the screen otherwise
  199. * we will overwrite information. */
  200. for(y = height < old_height ? height : old_height; y--; )
  201. {
  202. for(x = old_width; x--; )
  203. {
  204. cv->chars[y * width + x] = cv->chars[y * old_width + x];
  205. cv->attr[y * width + x] = cv->attr[y * old_width + x];
  206. }
  207. /* Zero the end of the line */
  208. for(x = width - old_width; x--; )
  209. cv->chars[y * width + old_width + x] = (uint32_t)' ';
  210. memset(cv->attr + y * width + old_width, 0,
  211. (width - old_width) * 4);
  212. }
  213. }
  214. else
  215. {
  216. /* New width is smaller. Copy as many lines as possible. Ignore
  217. * the first line, it is already in place. */
  218. unsigned int lines = height < old_height ? height : old_height;
  219. for(y = 1; y < lines; y++)
  220. {
  221. for(x = 0; x < width; x++)
  222. {
  223. cv->chars[y * width + x] = cv->chars[y * old_width + x];
  224. cv->attr[y * width + x] = cv->attr[y * old_width + x];
  225. }
  226. }
  227. }
  228. /* Step 3: fill the bottom of the new screen if necessary. */
  229. if(height > old_height)
  230. {
  231. /* Zero the bottom of the screen */
  232. for(x = (height - old_height) * width; x--; )
  233. cv->chars[old_height * width + x] = (uint32_t)' ';
  234. memset(cv->attr + old_height * width, 0,
  235. (height - old_height) * width * 4);
  236. }
  237. /* Step 4: if new area is smaller, resize memory area now. */
  238. if(new_size <= old_size)
  239. {
  240. cv->chars = realloc(cv->chars, new_size * sizeof(uint32_t));
  241. cv->attr = realloc(cv->attr, new_size * sizeof(uint32_t));
  242. }
  243. /* Recompute the scratch line and the empty line */
  244. if(width != old_width)
  245. cv->scratch_line = realloc(cv->scratch_line, width + 1);
  246. }