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.
 
 
 
 
 
 

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