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.
 
 
 
 
 
 

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