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.
 
 
 
 
 
 

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