Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

457 Zeilen
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 \e libcucul.
  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. int i;
  40. cucul_read_environment(qq);
  41. qq->fgcolor = CUCUL_COLOR_LIGHTGRAY;
  42. qq->bgcolor = CUCUL_COLOR_BLACK;
  43. /* Initialise to a default size. 80x32 is arbitrary but matches AAlib's
  44. * default X11 window. When a graphic driver attaches to us, it can set
  45. * a different 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_export * cucul_get_export(cucul_t *qq, enum cucul_format format)
  242. {
  243. struct cucul_export *ex;
  244. ex = malloc(sizeof(struct cucul_export));
  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. case CUCUL_FORMAT_SVG:
  263. _cucul_get_svg(qq, ex);
  264. break;
  265. default:
  266. free(ex);
  267. return NULL;
  268. }
  269. return ex;
  270. }
  271. void cucul_free_export(struct cucul_export *ex)
  272. {
  273. free(ex->buffer);
  274. free(ex);
  275. }
  276. /*
  277. * XXX: The following functions are local.
  278. */
  279. static void cucul_read_environment(cucul_t * qq)
  280. {
  281. /* FIXME: if strcasecmp isn't available, use strcmp */
  282. #if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
  283. char *var;
  284. #endif
  285. cucul_set_feature(qq, CUCUL_BACKGROUND);
  286. cucul_set_feature(qq, CUCUL_ANTIALIASING);
  287. cucul_set_feature(qq, CUCUL_DITHERING);
  288. #if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
  289. if((var = getenv("CUCUL_BACKGROUND")) && *var)
  290. {
  291. if(!strcasecmp("black", var))
  292. cucul_set_feature(qq, CUCUL_BACKGROUND_BLACK);
  293. else if(!strcasecmp("solid", var))
  294. cucul_set_feature(qq, CUCUL_BACKGROUND_SOLID);
  295. }
  296. if((var = getenv("CUCUL_ANTIALIASING")) && *var)
  297. {
  298. if(!strcasecmp("none", var))
  299. cucul_set_feature(qq, CUCUL_ANTIALIASING_NONE);
  300. else if(!strcasecmp("prefilter", var))
  301. cucul_set_feature(qq, CUCUL_ANTIALIASING_PREFILTER);
  302. }
  303. if((var = getenv("CUCUL_DITHERING")) && *var)
  304. {
  305. if(!strcasecmp("none", var))
  306. cucul_set_feature(qq, CUCUL_DITHERING_NONE);
  307. else if(!strcasecmp("ordered2", var))
  308. cucul_set_feature(qq, CUCUL_DITHERING_ORDERED2);
  309. else if(!strcasecmp("ordered4", var))
  310. cucul_set_feature(qq, CUCUL_DITHERING_ORDERED4);
  311. else if(!strcasecmp("ordered8", var))
  312. cucul_set_feature(qq, CUCUL_DITHERING_ORDERED8);
  313. else if(!strcasecmp("random", var))
  314. cucul_set_feature(qq, CUCUL_DITHERING_RANDOM);
  315. else if(!strcasecmp("fstein", var))
  316. cucul_set_feature(qq, CUCUL_DITHERING_FSTEIN);
  317. }
  318. #endif
  319. }
  320. void _cucul_set_size(cucul_t *qq, unsigned int width, unsigned int height)
  321. {
  322. unsigned int x, y, old_width, old_height, new_size, old_size;
  323. old_width = qq->width;
  324. old_height = qq->height;
  325. old_size = old_width * old_height;
  326. qq->width = width;
  327. qq->height = height;
  328. new_size = width * height;
  329. /* Step 1: if new area is bigger, resize the memory area now. */
  330. if(new_size > old_size)
  331. {
  332. qq->chars = realloc(qq->chars, new_size * sizeof(uint32_t));
  333. qq->attr = realloc(qq->attr, new_size * sizeof(uint8_t));
  334. }
  335. /* Step 2: move line data if necessary. */
  336. if(width == old_width)
  337. {
  338. /* Width did not change, which means we do not need to move data. */
  339. ;
  340. }
  341. else if(width > old_width)
  342. {
  343. /* New width is bigger than old width, which means we need to
  344. * copy lines starting from the bottom of the screen otherwise
  345. * we will overwrite information. */
  346. for(y = height < old_height ? height : old_height; y--; )
  347. {
  348. for(x = old_width; x--; )
  349. {
  350. qq->chars[y * width + x] = qq->chars[y * old_width + x];
  351. qq->attr[y * width + x] = qq->attr[y * old_width + x];
  352. }
  353. /* Zero the end of the line */
  354. for(x = width - old_width; x--; )
  355. qq->chars[y * width + old_width + x] = (uint32_t)' ';
  356. memset(qq->attr + y * width + old_width, 0,
  357. width - old_width);
  358. }
  359. }
  360. else
  361. {
  362. /* New width is smaller. Copy as many lines as possible. Ignore
  363. * the first line, it is already in place. */
  364. unsigned int lines = height < old_height ? height : old_height;
  365. for(y = 1; y < lines; y++)
  366. {
  367. for(x = 0; x < width; x++)
  368. {
  369. qq->chars[y * width + x] = qq->chars[y * old_width + x];
  370. qq->attr[y * width + x] = qq->attr[y * old_width + x];
  371. }
  372. }
  373. }
  374. /* Step 3: fill the bottom of the new screen if necessary. */
  375. if(height > old_height)
  376. {
  377. /* Zero the bottom of the screen */
  378. for(x = (height - old_height) * width; x--; )
  379. qq->chars[old_height * width + x] = (uint32_t)' ';
  380. memset(qq->attr + old_height * width, 0,
  381. (height - old_height) * width);
  382. }
  383. /* Step 4: if new area is smaller, resize memory area now. */
  384. if(new_size <= old_size)
  385. {
  386. qq->chars = realloc(qq->chars, new_size * sizeof(uint32_t));
  387. qq->attr = realloc(qq->attr, new_size * sizeof(uint8_t));
  388. }
  389. /* Recompute the scratch line and the empty line */
  390. if(width != old_width)
  391. {
  392. qq->empty_line = realloc(qq->empty_line, width + 1);
  393. memset(qq->empty_line, ' ', width);
  394. qq->empty_line[width] = '\0';
  395. qq->scratch_line = realloc(qq->scratch_line, width + 1);
  396. }
  397. }