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.
 
 
 
 
 
 

420 lignes
12 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 cucul.c
  12. * \version \$Id$
  13. * \author Sam Hocevar <sam@zoy.org>
  14. * \brief Main \e libcucul functions
  15. *
  16. * This file contains the main functions used by \e libcucul applications
  17. * to initialise a drawing context.
  18. */
  19. #include "config.h"
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "cucul.h"
  23. #include "cucul_internals.h"
  24. static void cucul_read_environment(cucul_t *);
  25. void _cucul_set_size(cucul_t *, unsigned int, unsigned int);
  26. /** \brief Initialise \e libcucul.
  27. *
  28. * This function initialises internal \e libcucul structures and the backend
  29. * that will be used for subsequent graphical operations. It must be the
  30. * first \e libcucul function to be called in a function. cucul_end() should
  31. * be called at the end of the program to free all allocated resources.
  32. *
  33. * \return 0 upon success, a non-zero value if an error occurs.
  34. */
  35. cucul_t * cucul_init(void)
  36. {
  37. cucul_t *qq = malloc(sizeof(cucul_t));
  38. int i;
  39. cucul_read_environment(qq);
  40. qq->fgcolor = CUCUL_COLOR_LIGHTGRAY;
  41. qq->bgcolor = CUCUL_COLOR_BLACK;
  42. /* Initialise to a default size. When a graphic driver attaches to
  43. * us, we'll adapt to its size. */
  44. qq->width = 80;
  45. qq->height = 32;
  46. qq->chars = malloc(qq->width * qq->height * sizeof(uint32_t));
  47. qq->attr = malloc(qq->width * qq->height * sizeof(uint8_t));
  48. for(i = qq->width * qq->height; i--; )
  49. qq->chars[i] = (uint32_t)' ';
  50. memset(qq->attr, 0, qq->width * qq->height * sizeof(uint8_t));
  51. qq->empty_line = malloc(qq->width + 1);
  52. memset(qq->empty_line, ' ', qq->width);
  53. qq->empty_line[qq->width] = '\0';
  54. qq->scratch_line = malloc(qq->width + 1);
  55. if(_cucul_init_bitmap())
  56. {
  57. free(qq);
  58. return NULL;
  59. }
  60. qq->ansi_buffer = NULL;
  61. return qq;
  62. }
  63. /** \brief Set the canvas size.
  64. *
  65. * This function sets the canvas width and height, in character cells.
  66. *
  67. * It is an error to try to resize the canvas if an output driver has
  68. * been attached to the canvas using caca_attach(). You need to remove
  69. * the output driver using caca_detach() before you can change the
  70. * canvas size again.
  71. *
  72. * However, the caca output driver can cause a canvas resize through
  73. * user interaction. See the caca_event() documentation for more about
  74. * this.
  75. *
  76. * \param width The desired canvas width
  77. * \param height The desired canvas height
  78. */
  79. void cucul_set_size(cucul_t *qq, unsigned int width, unsigned int height)
  80. {
  81. if(qq->refcount)
  82. return;
  83. _cucul_set_size(qq, width, height);
  84. }
  85. /** \brief Get the canvas width.
  86. *
  87. * This function returns the current canvas width, in character cells.
  88. *
  89. * \return The canvas width.
  90. */
  91. unsigned int cucul_get_width(cucul_t *qq)
  92. {
  93. return qq->width;
  94. }
  95. /** \brief Get the canvas height.
  96. *
  97. * This function returns the current canvas height, in character cells.
  98. *
  99. * \return The canvas height.
  100. */
  101. unsigned int cucul_get_height(cucul_t *qq)
  102. {
  103. return qq->height;
  104. }
  105. /** \brief Translate a colour index into the colour's name.
  106. *
  107. * This function translates a cucul_color enum into a human-readable
  108. * description string of the associated colour.
  109. *
  110. * \param color The colour value.
  111. * \return A static string containing the colour's name.
  112. */
  113. char const *cucul_get_color_name(enum cucul_color color)
  114. {
  115. static char const *color_names[] =
  116. {
  117. "black",
  118. "blue",
  119. "green",
  120. "cyan",
  121. "red",
  122. "magenta",
  123. "brown",
  124. "light gray",
  125. "dark gray",
  126. "light blue",
  127. "light green",
  128. "light cyan",
  129. "light red",
  130. "light magenta",
  131. "yellow",
  132. "white",
  133. };
  134. if(color < 0 || color > 15)
  135. return "unknown";
  136. return color_names[color];
  137. }
  138. /** \brief Get the current value of a feature.
  139. *
  140. * This function retrieves the value of an internal \e libcucul feature. A
  141. * generic feature value is expected, such as CUCUL_ANTIALIASING.
  142. *
  143. * \param feature The requested feature.
  144. * \return The current value of the feature or CUCUL_FEATURE_UNKNOWN if an
  145. * error occurred..
  146. */
  147. enum cucul_feature cucul_get_feature(cucul_t *qq, enum cucul_feature feature)
  148. {
  149. switch(feature)
  150. {
  151. case CUCUL_BACKGROUND:
  152. return qq->background;
  153. case CUCUL_ANTIALIASING:
  154. return qq->antialiasing;
  155. case CUCUL_DITHERING:
  156. return qq->dithering;
  157. default:
  158. return CUCUL_FEATURE_UNKNOWN;
  159. }
  160. }
  161. /** \brief Set a feature.
  162. *
  163. * This function sets an internal \e libcucul feature such as the antialiasing
  164. * or dithering modes. If a specific feature such as CUCUL_DITHERING_RANDOM,
  165. * cucul_set_feature() will set it immediately. If a generic feature is given
  166. * instead, such as CUCUL_DITHERING, the default value will be used instead.
  167. *
  168. * \param feature The requested feature.
  169. */
  170. void cucul_set_feature(cucul_t *qq, enum cucul_feature feature)
  171. {
  172. switch(feature)
  173. {
  174. case CUCUL_BACKGROUND:
  175. feature = CUCUL_BACKGROUND_SOLID;
  176. case CUCUL_BACKGROUND_BLACK:
  177. case CUCUL_BACKGROUND_SOLID:
  178. qq->background = feature;
  179. break;
  180. case CUCUL_ANTIALIASING:
  181. feature = CUCUL_ANTIALIASING_PREFILTER;
  182. case CUCUL_ANTIALIASING_NONE:
  183. case CUCUL_ANTIALIASING_PREFILTER:
  184. qq->antialiasing = feature;
  185. break;
  186. case CUCUL_DITHERING:
  187. feature = CUCUL_DITHERING_FSTEIN;
  188. case CUCUL_DITHERING_NONE:
  189. case CUCUL_DITHERING_ORDERED2:
  190. case CUCUL_DITHERING_ORDERED4:
  191. case CUCUL_DITHERING_ORDERED8:
  192. case CUCUL_DITHERING_RANDOM:
  193. case CUCUL_DITHERING_FSTEIN:
  194. qq->dithering = feature;
  195. break;
  196. case CUCUL_FEATURE_UNKNOWN:
  197. break;
  198. }
  199. }
  200. /** \brief Translate a feature value into the feature's name.
  201. *
  202. * This function translates a cucul_feature enum into a human-readable
  203. * description string of the associated feature.
  204. *
  205. * \param feature The feature value.
  206. * \return A static string containing the feature's name.
  207. */
  208. char const *cucul_get_feature_name(enum cucul_feature feature)
  209. {
  210. switch(feature)
  211. {
  212. case CUCUL_BACKGROUND_BLACK: return "black background";
  213. case CUCUL_BACKGROUND_SOLID: return "solid background";
  214. case CUCUL_ANTIALIASING_NONE: return "no antialiasing";
  215. case CUCUL_ANTIALIASING_PREFILTER: return "prefilter antialiasing";
  216. case CUCUL_DITHERING_NONE: return "no dithering";
  217. case CUCUL_DITHERING_ORDERED2: return "2x2 ordered dithering";
  218. case CUCUL_DITHERING_ORDERED4: return "4x4 ordered dithering";
  219. case CUCUL_DITHERING_ORDERED8: return "8x8 ordered dithering";
  220. case CUCUL_DITHERING_RANDOM: return "random dithering";
  221. case CUCUL_DITHERING_FSTEIN: return "Floyd-Steinberg dithering";
  222. default: return "unknown";
  223. }
  224. }
  225. /** \brief Uninitialise \e libcucul.
  226. *
  227. * This function frees all resources allocated by cucul_init(). After
  228. * cucul_end() has been called, no other \e libcucul functions may be used
  229. * unless a new call to cucul_init() is done.
  230. */
  231. void cucul_end(cucul_t *qq)
  232. {
  233. _cucul_end_bitmap();
  234. free(qq->empty_line);
  235. free(qq->scratch_line);
  236. free(qq->chars);
  237. free(qq->attr);
  238. if(qq->ansi_buffer)
  239. free(qq->ansi_buffer);
  240. free(qq);
  241. }
  242. /*
  243. * XXX: The following functions are local.
  244. */
  245. static void cucul_read_environment(cucul_t * qq)
  246. {
  247. /* FIXME: if strcasecmp isn't available, use strcmp */
  248. #if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
  249. char *var;
  250. #endif
  251. cucul_set_feature(qq, CUCUL_BACKGROUND);
  252. cucul_set_feature(qq, CUCUL_ANTIALIASING);
  253. cucul_set_feature(qq, CUCUL_DITHERING);
  254. #if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
  255. if((var = getenv("CUCUL_BACKGROUND")) && *var)
  256. {
  257. if(!strcasecmp("black", var))
  258. cucul_set_feature(qq, CUCUL_BACKGROUND_BLACK);
  259. else if(!strcasecmp("solid", var))
  260. cucul_set_feature(qq, CUCUL_BACKGROUND_SOLID);
  261. }
  262. if((var = getenv("CUCUL_ANTIALIASING")) && *var)
  263. {
  264. if(!strcasecmp("none", var))
  265. cucul_set_feature(qq, CUCUL_ANTIALIASING_NONE);
  266. else if(!strcasecmp("prefilter", var))
  267. cucul_set_feature(qq, CUCUL_ANTIALIASING_PREFILTER);
  268. }
  269. if((var = getenv("CUCUL_DITHERING")) && *var)
  270. {
  271. if(!strcasecmp("none", var))
  272. cucul_set_feature(qq, CUCUL_DITHERING_NONE);
  273. else if(!strcasecmp("ordered2", var))
  274. cucul_set_feature(qq, CUCUL_DITHERING_ORDERED2);
  275. else if(!strcasecmp("ordered4", var))
  276. cucul_set_feature(qq, CUCUL_DITHERING_ORDERED4);
  277. else if(!strcasecmp("ordered8", var))
  278. cucul_set_feature(qq, CUCUL_DITHERING_ORDERED8);
  279. else if(!strcasecmp("random", var))
  280. cucul_set_feature(qq, CUCUL_DITHERING_RANDOM);
  281. else if(!strcasecmp("fstein", var))
  282. cucul_set_feature(qq, CUCUL_DITHERING_FSTEIN);
  283. }
  284. #endif
  285. }
  286. void _cucul_set_size(cucul_t *qq, unsigned int width, unsigned int height)
  287. {
  288. unsigned int x, y, old_width, old_height, new_size, old_size;
  289. old_width = qq->width;
  290. old_height = qq->height;
  291. old_size = old_width * old_height;
  292. qq->width = width;
  293. qq->height = height;
  294. new_size = width * height;
  295. /* Step 1: if new area is bigger, resize the memory area now. */
  296. if(new_size > old_size)
  297. {
  298. qq->chars = realloc(qq->chars, new_size * sizeof(uint32_t));
  299. qq->attr = realloc(qq->attr, new_size * sizeof(uint8_t));
  300. }
  301. /* Step 2: move line data if necessary. */
  302. if(width == old_width)
  303. {
  304. /* Width did not change, which means we do not need to move data. */
  305. ;
  306. }
  307. else if(width > old_width)
  308. {
  309. /* New width is bigger than old width, which means we need to
  310. * copy lines starting from the bottom of the screen otherwise
  311. * we will overwrite information. */
  312. for(y = height < old_height ? height : old_height; y--; )
  313. {
  314. for(x = old_width; x--; )
  315. {
  316. qq->chars[y * width + x] = qq->chars[y * old_width + x];
  317. qq->attr[y * width + x] = qq->attr[y * old_width + x];
  318. }
  319. /* Zero the end of the line */
  320. for(x = width - old_width; x--; )
  321. qq->chars[y * width + old_width + x] = (uint32_t)' ';
  322. memset(qq->attr + y * width + old_width, 0,
  323. width - old_width);
  324. }
  325. }
  326. else
  327. {
  328. /* New width is smaller. Copy as many lines as possible. Ignore
  329. * the first line, it is already in place. */
  330. unsigned int lines = height < old_height ? height : old_height;
  331. for(y = 1; y < lines; y++)
  332. {
  333. for(x = 0; x < width; x++)
  334. {
  335. qq->chars[y * width + x] = qq->chars[y * old_width + x];
  336. qq->attr[y * width + x] = qq->attr[y * old_width + x];
  337. }
  338. }
  339. }
  340. /* Step 3: fill the bottom of the new screen if necessary. */
  341. if(height > old_height)
  342. {
  343. /* Zero the bottom of the screen */
  344. for(x = (height - old_height) * width; x--; )
  345. qq->chars[old_height * width + x] = (uint32_t)' ';
  346. memset(qq->attr + old_height * width, 0,
  347. (height - old_height) * width);
  348. }
  349. /* Step 4: if new area is smaller, resize memory area now. */
  350. if(new_size <= old_size)
  351. {
  352. qq->chars = realloc(qq->chars, new_size * sizeof(uint32_t));
  353. qq->attr = realloc(qq->attr, new_size * sizeof(uint8_t));
  354. }
  355. /* Recompute the scratch line and the empty line */
  356. if(width != old_width)
  357. {
  358. qq->empty_line = realloc(qq->empty_line, width + 1);
  359. memset(qq->empty_line, ' ', width);
  360. qq->empty_line[width] = '\0';
  361. qq->scratch_line = realloc(qq->scratch_line, width + 1);
  362. }
  363. }