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.
 
 
 
 
 
 

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