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.
 
 
 
 
 
 

459 lines
13 KiB

  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  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_free() 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_create(unsigned int width, unsigned int height)
  42. {
  43. cucul_t *qq = malloc(sizeof(cucul_t));
  44. cucul_read_environment(qq);
  45. qq->refcount = 0;
  46. qq->fgcolor = CUCUL_COLOR_LIGHTGRAY;
  47. qq->bgcolor = CUCUL_COLOR_BLACK;
  48. qq->width = qq->width = 0;
  49. qq->chars = NULL;
  50. qq->attr = NULL;
  51. qq->empty_line = qq->scratch_line = NULL;
  52. /* Initialise to a default size. 80x32 is arbitrary but matches AAlib's
  53. * default X11 window. When a graphic driver attaches to us, it can set
  54. * a different size. */
  55. if(width && height)
  56. _cucul_set_size(qq, width, height);
  57. else
  58. _cucul_set_size(qq, 80, 32);
  59. if(_cucul_init_bitmap())
  60. {
  61. free(qq);
  62. return NULL;
  63. }
  64. return qq;
  65. }
  66. /** \brief Set the canvas size.
  67. *
  68. * This function sets the canvas width and height, in character cells.
  69. *
  70. * The contents of the canvas are preserved to the extent of the new
  71. * canvas size. Newly allocated character cells at the right and/or at
  72. * the bottom of the canvas are filled with spaces.
  73. *
  74. * It is an error to try to resize the canvas if an output driver has
  75. * been attached to the canvas using caca_attach(). You need to remove
  76. * the output driver using caca_detach() before you can change the
  77. * canvas size again. However, the caca output driver can cause a canvas
  78. * resize through user interaction. See the caca_event() documentation
  79. * for more about 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_create(). After
  233. * cucul_free() has been called, no other \e libcucul functions may be used
  234. * unless a new call to cucul_create() is done.
  235. */
  236. void cucul_free(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. free(qq);
  244. }
  245. struct cucul_export * cucul_create_export(cucul_t *qq, enum cucul_format format)
  246. {
  247. struct cucul_export *ex;
  248. ex = malloc(sizeof(struct cucul_export));
  249. switch(format)
  250. {
  251. case CUCUL_FORMAT_ANSI:
  252. _cucul_get_ansi(qq, ex);
  253. break;
  254. case CUCUL_FORMAT_HTML:
  255. _cucul_get_html(qq, ex);
  256. break;
  257. case CUCUL_FORMAT_HTML3:
  258. _cucul_get_html3(qq, ex);
  259. break;
  260. case CUCUL_FORMAT_IRC:
  261. _cucul_get_irc(qq, ex);
  262. break;
  263. case CUCUL_FORMAT_PS:
  264. _cucul_get_ps(qq, ex);
  265. break;
  266. case CUCUL_FORMAT_SVG:
  267. _cucul_get_svg(qq, ex);
  268. break;
  269. default:
  270. free(ex);
  271. return NULL;
  272. }
  273. return ex;
  274. }
  275. void cucul_free_export(struct cucul_export *ex)
  276. {
  277. free(ex->buffer);
  278. free(ex);
  279. }
  280. /*
  281. * XXX: The following functions are local.
  282. */
  283. static void cucul_read_environment(cucul_t * qq)
  284. {
  285. /* FIXME: if strcasecmp isn't available, use strcmp */
  286. #if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
  287. char *var;
  288. #endif
  289. cucul_set_feature(qq, CUCUL_BACKGROUND);
  290. cucul_set_feature(qq, CUCUL_ANTIALIASING);
  291. cucul_set_feature(qq, CUCUL_DITHERING);
  292. #if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
  293. if((var = getenv("CUCUL_BACKGROUND")) && *var)
  294. {
  295. if(!strcasecmp("black", var))
  296. cucul_set_feature(qq, CUCUL_BACKGROUND_BLACK);
  297. else if(!strcasecmp("solid", var))
  298. cucul_set_feature(qq, CUCUL_BACKGROUND_SOLID);
  299. }
  300. if((var = getenv("CUCUL_ANTIALIASING")) && *var)
  301. {
  302. if(!strcasecmp("none", var))
  303. cucul_set_feature(qq, CUCUL_ANTIALIASING_NONE);
  304. else if(!strcasecmp("prefilter", var))
  305. cucul_set_feature(qq, CUCUL_ANTIALIASING_PREFILTER);
  306. }
  307. if((var = getenv("CUCUL_DITHERING")) && *var)
  308. {
  309. if(!strcasecmp("none", var))
  310. cucul_set_feature(qq, CUCUL_DITHERING_NONE);
  311. else if(!strcasecmp("ordered2", var))
  312. cucul_set_feature(qq, CUCUL_DITHERING_ORDERED2);
  313. else if(!strcasecmp("ordered4", var))
  314. cucul_set_feature(qq, CUCUL_DITHERING_ORDERED4);
  315. else if(!strcasecmp("ordered8", var))
  316. cucul_set_feature(qq, CUCUL_DITHERING_ORDERED8);
  317. else if(!strcasecmp("random", var))
  318. cucul_set_feature(qq, CUCUL_DITHERING_RANDOM);
  319. else if(!strcasecmp("fstein", var))
  320. cucul_set_feature(qq, CUCUL_DITHERING_FSTEIN);
  321. }
  322. #endif
  323. }
  324. void _cucul_set_size(cucul_t *qq, unsigned int width, unsigned int height)
  325. {
  326. unsigned int x, y, old_width, old_height, new_size, old_size;
  327. old_width = qq->width;
  328. old_height = qq->height;
  329. old_size = old_width * old_height;
  330. qq->width = width;
  331. qq->height = height;
  332. new_size = width * height;
  333. /* Step 1: if new area is bigger, resize the memory area now. */
  334. if(new_size > old_size)
  335. {
  336. qq->chars = realloc(qq->chars, new_size * sizeof(uint32_t));
  337. qq->attr = realloc(qq->attr, new_size * sizeof(uint8_t));
  338. }
  339. /* Step 2: move line data if necessary. */
  340. if(width == old_width)
  341. {
  342. /* Width did not change, which means we do not need to move data. */
  343. ;
  344. }
  345. else if(width > old_width)
  346. {
  347. /* New width is bigger than old width, which means we need to
  348. * copy lines starting from the bottom of the screen otherwise
  349. * we will overwrite information. */
  350. for(y = height < old_height ? height : old_height; y--; )
  351. {
  352. for(x = old_width; x--; )
  353. {
  354. qq->chars[y * width + x] = qq->chars[y * old_width + x];
  355. qq->attr[y * width + x] = qq->attr[y * old_width + x];
  356. }
  357. /* Zero the end of the line */
  358. for(x = width - old_width; x--; )
  359. qq->chars[y * width + old_width + x] = (uint32_t)' ';
  360. memset(qq->attr + y * width + old_width, 0,
  361. width - old_width);
  362. }
  363. }
  364. else
  365. {
  366. /* New width is smaller. Copy as many lines as possible. Ignore
  367. * the first line, it is already in place. */
  368. unsigned int lines = height < old_height ? height : old_height;
  369. for(y = 1; y < lines; y++)
  370. {
  371. for(x = 0; x < width; x++)
  372. {
  373. qq->chars[y * width + x] = qq->chars[y * old_width + x];
  374. qq->attr[y * width + x] = qq->attr[y * old_width + x];
  375. }
  376. }
  377. }
  378. /* Step 3: fill the bottom of the new screen if necessary. */
  379. if(height > old_height)
  380. {
  381. /* Zero the bottom of the screen */
  382. for(x = (height - old_height) * width; x--; )
  383. qq->chars[old_height * width + x] = (uint32_t)' ';
  384. memset(qq->attr + old_height * width, 0,
  385. (height - old_height) * width);
  386. }
  387. /* Step 4: if new area is smaller, resize memory area now. */
  388. if(new_size <= old_size)
  389. {
  390. qq->chars = realloc(qq->chars, new_size * sizeof(uint32_t));
  391. qq->attr = realloc(qq->attr, new_size * sizeof(uint8_t));
  392. }
  393. /* Recompute the scratch line and the empty line */
  394. if(width != old_width)
  395. {
  396. qq->empty_line = realloc(qq->empty_line, width + 1);
  397. memset(qq->empty_line, ' ', width);
  398. qq->empty_line[width] = '\0';
  399. qq->scratch_line = realloc(qq->scratch_line, width + 1);
  400. }
  401. }