Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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