Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

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