25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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