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.
 
 
 
 
 
 

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