Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

345 řádky
9.8 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. cv->empty_line = cv->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_canvas_size(cv, width, height);
  54. else
  55. _cucul_set_canvas_size(cv, 80, 32);
  56. if(_cucul_init_dither())
  57. {
  58. free(cv);
  59. return NULL;
  60. }
  61. return cv;
  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_canvas_t *cucul_load_canvas(void *data, unsigned int size)
  73. {
  74. cucul_canvas_t *cv;
  75. uint8_t *buf = (uint8_t *)data;
  76. unsigned int width, height, n;
  77. if(size < 16)
  78. return NULL;
  79. if(buf[0] != 'C' || buf[1] != 'A' || buf[2] != 'C' || buf[3] != 'A')
  80. return NULL;
  81. if(buf[4] != 'C' || buf[5] != 'A' || buf[6] != 'N' || buf[7] != 'V')
  82. return NULL;
  83. width = ((uint32_t)buf[8] << 24) | ((uint32_t)buf[9] << 16)
  84. | ((uint32_t)buf[10] << 8) | (uint32_t)buf[11];
  85. height = ((uint32_t)buf[12] << 24) | ((uint32_t)buf[13] << 16)
  86. | ((uint32_t)buf[14] << 8) | (uint32_t)buf[15];
  87. if(!width || !height)
  88. return NULL;
  89. if(size != 16 + width * height * 8)
  90. return NULL;
  91. cv = cucul_create_canvas(width, height);
  92. if(!cv)
  93. return NULL;
  94. for(n = height * width; n--; )
  95. {
  96. cv->chars[n] = ((uint32_t)buf[16 + 0 + 8 * n] << 24)
  97. | ((uint32_t)buf[16 + 1 + 8 * n] << 16)
  98. | ((uint32_t)buf[16 + 2 + 8 * n] << 8)
  99. | (uint32_t)buf[16 + 3 + 8 * n];
  100. cv->attr[n] = ((uint32_t)buf[16 + 4 + 8 * n] << 24)
  101. | ((uint32_t)buf[16 + 5 + 8 * n] << 16)
  102. | ((uint32_t)buf[16 + 6 + 8 * n] << 8)
  103. | (uint32_t)buf[16 + 7 + 8 * n];
  104. }
  105. return cv;
  106. }
  107. /** \brief Resize a canvas.
  108. *
  109. * This function sets the canvas width and height, in character cells.
  110. *
  111. * The contents of the canvas are preserved to the extent of the new
  112. * canvas size. Newly allocated character cells at the right and/or at
  113. * the bottom of the canvas are filled with spaces.
  114. *
  115. * It is an error to try to resize the canvas if an output driver has
  116. * been attached to the canvas using caca_create_display(). You need to
  117. * remove the output driver using caca_free_display() before you can change
  118. * the canvas size again. However, the caca output driver can cause a
  119. * canvas resize through user interaction. See the caca_event() documentation
  120. * for more about this.
  121. *
  122. * \param cv A libcucul canvas
  123. * \param width The desired canvas width
  124. * \param height The desired canvas height
  125. */
  126. void cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
  127. unsigned int height)
  128. {
  129. if(cv->refcount)
  130. return;
  131. _cucul_set_canvas_size(cv, width, height);
  132. }
  133. /** \brief Get the canvas width.
  134. *
  135. * This function returns the current canvas width, in character cells.
  136. *
  137. * \param cv A libcucul canvas
  138. * \return The canvas width.
  139. */
  140. unsigned int cucul_get_canvas_width(cucul_canvas_t *cv)
  141. {
  142. return cv->width;
  143. }
  144. /** \brief Get the canvas height.
  145. *
  146. * This function returns the current canvas height, in character cells.
  147. *
  148. * \param cv A libcucul canvas
  149. * \return The canvas height.
  150. */
  151. unsigned int cucul_get_canvas_height(cucul_canvas_t *cv)
  152. {
  153. return cv->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_canvas(). After
  191. * cucul_free_canvas() has been called, no other \e libcucul functions may be
  192. * used unless a new call to cucul_create_canvas() is done.
  193. *
  194. * \param cv A libcucul canvas
  195. */
  196. void cucul_free_canvas(cucul_canvas_t *cv)
  197. {
  198. _cucul_end_dither();
  199. free(cv->empty_line);
  200. free(cv->scratch_line);
  201. free(cv->chars);
  202. free(cv->attr);
  203. free(cv);
  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 - 1
  210. * (inclusive).
  211. */
  212. int cucul_rand(int min, int max)
  213. {
  214. return min + (int)((1.0 * (max - min)) * rand() / (RAND_MAX + 1.0));
  215. }
  216. /*
  217. * XXX: The following functions are local.
  218. */
  219. void _cucul_set_canvas_size(cucul_canvas_t *cv, unsigned int width,
  220. unsigned int height)
  221. {
  222. unsigned int x, y, old_width, old_height, new_size, old_size;
  223. old_width = cv->width;
  224. old_height = cv->height;
  225. old_size = old_width * old_height;
  226. cv->width = width;
  227. cv->height = height;
  228. new_size = width * height;
  229. /* Step 1: if new area is bigger, resize the memory area now. */
  230. if(new_size > old_size)
  231. {
  232. cv->chars = realloc(cv->chars, new_size * sizeof(uint32_t));
  233. cv->attr = realloc(cv->attr, new_size * sizeof(uint32_t));
  234. }
  235. /* Step 2: move line data if necessary. */
  236. if(width == old_width)
  237. {
  238. /* Width did not change, which means we do not need to move data. */
  239. ;
  240. }
  241. else if(width > old_width)
  242. {
  243. /* New width is bigger than old width, which means we need to
  244. * copy lines starting from the bottom of the screen otherwise
  245. * we will overwrite information. */
  246. for(y = height < old_height ? height : old_height; y--; )
  247. {
  248. for(x = old_width; x--; )
  249. {
  250. cv->chars[y * width + x] = cv->chars[y * old_width + x];
  251. cv->attr[y * width + x] = cv->attr[y * old_width + x];
  252. }
  253. /* Zero the end of the line */
  254. for(x = width - old_width; x--; )
  255. cv->chars[y * width + old_width + x] = (uint32_t)' ';
  256. memset(cv->attr + y * width + old_width, 0,
  257. (width - old_width) * 4);
  258. }
  259. }
  260. else
  261. {
  262. /* New width is smaller. Copy as many lines as possible. Ignore
  263. * the first line, it is already in place. */
  264. unsigned int lines = height < old_height ? height : old_height;
  265. for(y = 1; y < lines; y++)
  266. {
  267. for(x = 0; x < width; x++)
  268. {
  269. cv->chars[y * width + x] = cv->chars[y * old_width + x];
  270. cv->attr[y * width + x] = cv->attr[y * old_width + x];
  271. }
  272. }
  273. }
  274. /* Step 3: fill the bottom of the new screen if necessary. */
  275. if(height > old_height)
  276. {
  277. /* Zero the bottom of the screen */
  278. for(x = (height - old_height) * width; x--; )
  279. cv->chars[old_height * width + x] = (uint32_t)' ';
  280. memset(cv->attr + old_height * width, 0,
  281. (height - old_height) * width * 4);
  282. }
  283. /* Step 4: if new area is smaller, resize memory area now. */
  284. if(new_size <= old_size)
  285. {
  286. cv->chars = realloc(cv->chars, new_size * sizeof(uint32_t));
  287. cv->attr = realloc(cv->attr, new_size * sizeof(uint32_t));
  288. }
  289. /* Recompute the scratch line and the empty line */
  290. if(width != old_width)
  291. {
  292. cv->empty_line = realloc(cv->empty_line, width + 1);
  293. memset(cv->empty_line, ' ', width);
  294. cv->empty_line[width] = '\0';
  295. cv->scratch_line = realloc(cv->scratch_line, width + 1);
  296. }
  297. }