Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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