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.
 
 
 
 
 
 

371 rinda
10 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. /** \brief Initialise a \e libcucul canvas.
  27. *
  28. * This function initialises internal \e libcucul structures and the backend
  29. * that will be used for subsequent graphical operations. It must be the
  30. * first \e libcucul function to be called in a function. cucul_free() should
  31. * be called at the end of the program to free all allocated resources.
  32. *
  33. * If one of the desired canvas coordinates is zero, a default canvas size
  34. * of 80x32 is used instead.
  35. *
  36. * \param width The desired canvas width
  37. * \param height The desired canvas height
  38. * \return 0 upon success, a non-zero value if an error occurs.
  39. */
  40. cucul_t * cucul_create(unsigned int width, unsigned int height)
  41. {
  42. cucul_t *qq = malloc(sizeof(cucul_t));
  43. qq->refcount = 0;
  44. qq->fgcolor = CUCUL_COLOR_LIGHTGRAY;
  45. qq->bgcolor = CUCUL_COLOR_BLACK;
  46. qq->width = qq->width = 0;
  47. qq->chars = NULL;
  48. qq->attr = NULL;
  49. qq->empty_line = qq->scratch_line = NULL;
  50. /* Initialise to a default size. 80x32 is arbitrary but matches AAlib's
  51. * default X11 window. When a graphic driver attaches to us, it can set
  52. * a different size. */
  53. if(width && height)
  54. _cucul_set_size(qq, width, height);
  55. else
  56. _cucul_set_size(qq, 80, 32);
  57. if(_cucul_init_bitmap())
  58. {
  59. free(qq);
  60. return NULL;
  61. }
  62. return qq;
  63. }
  64. cucul_t *cucul_load(void *data, unsigned int size)
  65. {
  66. cucul_t *qq;
  67. uint8_t *buf = (uint8_t *)data;
  68. unsigned int width, height, n;
  69. if(size < 12)
  70. return NULL;
  71. if(buf[0] != 'C' || buf[1] != 'A' || buf[2] != 'C' || buf[3] != 'A')
  72. return NULL;
  73. width = ((uint32_t)buf[4] << 24) | ((uint32_t)buf[5] << 16)
  74. | ((uint32_t)buf[6] << 8) | (uint32_t)buf[7];
  75. height = ((uint32_t)buf[8] << 24) | ((uint32_t)buf[9] << 16)
  76. | ((uint32_t)buf[10] << 8) | (uint32_t)buf[11];
  77. if(!width || !height)
  78. return NULL;
  79. if(size != 12 + width * height * 5 + 4)
  80. return NULL;
  81. qq = cucul_create(width, height);
  82. if(!qq)
  83. return NULL;
  84. for(n = height * width; n--; )
  85. {
  86. qq->chars[n] = ((uint32_t)buf[12 + 5 * n] << 24)
  87. | ((uint32_t)buf[13 + 5 * n] << 16)
  88. | ((uint32_t)buf[14 + 5 * n] << 8)
  89. | (uint32_t)buf[15 + 5 * n];
  90. qq->attr[n] = buf[16 + 5 * n];
  91. }
  92. return qq;
  93. }
  94. /** \brief Resize a canvas.
  95. *
  96. * This function sets the canvas width and height, in character cells.
  97. *
  98. * The contents of the canvas are preserved to the extent of the new
  99. * canvas size. Newly allocated character cells at the right and/or at
  100. * the bottom of the canvas are filled with spaces.
  101. *
  102. * It is an error to try to resize the canvas if an output driver has
  103. * been attached to the canvas using caca_attach(). You need to remove
  104. * the output driver using caca_detach() before you can change the
  105. * canvas size again. However, the caca output driver can cause a canvas
  106. * resize through user interaction. See the caca_event() documentation
  107. * for more about this.
  108. *
  109. * \param width The desired canvas width
  110. * \param height The desired canvas height
  111. */
  112. void cucul_set_size(cucul_t *qq, unsigned int width, unsigned int height)
  113. {
  114. if(qq->refcount)
  115. return;
  116. _cucul_set_size(qq, width, height);
  117. }
  118. /** \brief Get the canvas width.
  119. *
  120. * This function returns the current canvas width, in character cells.
  121. *
  122. * \return The canvas width.
  123. */
  124. unsigned int cucul_get_width(cucul_t *qq)
  125. {
  126. return qq->width;
  127. }
  128. /** \brief Get the canvas height.
  129. *
  130. * This function returns the current canvas height, in character cells.
  131. *
  132. * \return The canvas height.
  133. */
  134. unsigned int cucul_get_height(cucul_t *qq)
  135. {
  136. return qq->height;
  137. }
  138. /** \brief Translate a colour index into the colour's name.
  139. *
  140. * This function translates a cucul_color enum into a human-readable
  141. * description string of the associated colour.
  142. *
  143. * \param color The colour value.
  144. * \return A static string containing the colour's name.
  145. */
  146. char const *cucul_get_color_name(enum cucul_color color)
  147. {
  148. static char const *color_names[] =
  149. {
  150. "black",
  151. "blue",
  152. "green",
  153. "cyan",
  154. "red",
  155. "magenta",
  156. "brown",
  157. "light gray",
  158. "dark gray",
  159. "light blue",
  160. "light green",
  161. "light cyan",
  162. "light red",
  163. "light magenta",
  164. "yellow",
  165. "white",
  166. };
  167. if(color < 0 || color > 15)
  168. return "unknown";
  169. return color_names[color];
  170. }
  171. /** \brief Uninitialise \e libcucul.
  172. *
  173. * This function frees all resources allocated by cucul_create(). After
  174. * cucul_free() has been called, no other \e libcucul functions may be used
  175. * unless a new call to cucul_create() is done.
  176. */
  177. void cucul_free(cucul_t *qq)
  178. {
  179. _cucul_end_bitmap();
  180. free(qq->empty_line);
  181. free(qq->scratch_line);
  182. free(qq->chars);
  183. free(qq->attr);
  184. free(qq);
  185. }
  186. struct cucul_export * cucul_create_export(cucul_t *qq, char const *format)
  187. {
  188. struct cucul_export *ex;
  189. ex = malloc(sizeof(struct cucul_export));
  190. if(!strcasecmp("ansi", format))
  191. _cucul_get_ansi(qq, ex);
  192. else if(!strcasecmp("html", format))
  193. _cucul_get_html(qq, ex);
  194. else if(!strcasecmp("html3", format))
  195. _cucul_get_html3(qq, ex);
  196. else if(!strcasecmp("irc", format))
  197. _cucul_get_irc(qq, ex);
  198. else if(!strcasecmp("ps", format))
  199. _cucul_get_ps(qq, ex);
  200. else if(!strcasecmp("svg", format))
  201. _cucul_get_svg(qq, ex);
  202. else
  203. {
  204. free(ex);
  205. return NULL;
  206. }
  207. return ex;
  208. }
  209. /**
  210. * \brief Get available export formats
  211. *
  212. * Return a list of available export formats. The list is a NULL-terminated
  213. * array of strings, interleaving a string containing the internal value for
  214. * the export format, to be used with \e cucul_export(), and a string
  215. * containing the natural language description for that export format.
  216. *
  217. * \return An array of strings.
  218. */
  219. char const * const * cucul_get_export_list(void)
  220. {
  221. static char const * const list[] =
  222. {
  223. "ansi", "ANSI",
  224. "html", "HTML",
  225. "html3", "backwards-compatible HTML",
  226. "irc", "IRC (mIRC colours)",
  227. "ps", "PostScript",
  228. "svg", "SVG",
  229. NULL, NULL
  230. };
  231. return list;
  232. }
  233. void cucul_free_export(struct cucul_export *ex)
  234. {
  235. free(ex->buffer);
  236. free(ex);
  237. }
  238. /*
  239. * XXX: The following functions are local.
  240. */
  241. void _cucul_set_size(cucul_t *qq, unsigned int width, unsigned int height)
  242. {
  243. unsigned int x, y, old_width, old_height, new_size, old_size;
  244. old_width = qq->width;
  245. old_height = qq->height;
  246. old_size = old_width * old_height;
  247. qq->width = width;
  248. qq->height = height;
  249. new_size = width * height;
  250. /* Step 1: if new area is bigger, resize the memory area now. */
  251. if(new_size > old_size)
  252. {
  253. qq->chars = realloc(qq->chars, new_size * sizeof(uint32_t));
  254. qq->attr = realloc(qq->attr, new_size * sizeof(uint8_t));
  255. }
  256. /* Step 2: move line data if necessary. */
  257. if(width == old_width)
  258. {
  259. /* Width did not change, which means we do not need to move data. */
  260. ;
  261. }
  262. else if(width > old_width)
  263. {
  264. /* New width is bigger than old width, which means we need to
  265. * copy lines starting from the bottom of the screen otherwise
  266. * we will overwrite information. */
  267. for(y = height < old_height ? height : old_height; y--; )
  268. {
  269. for(x = old_width; x--; )
  270. {
  271. qq->chars[y * width + x] = qq->chars[y * old_width + x];
  272. qq->attr[y * width + x] = qq->attr[y * old_width + x];
  273. }
  274. /* Zero the end of the line */
  275. for(x = width - old_width; x--; )
  276. qq->chars[y * width + old_width + x] = (uint32_t)' ';
  277. memset(qq->attr + y * width + old_width, 0,
  278. width - old_width);
  279. }
  280. }
  281. else
  282. {
  283. /* New width is smaller. Copy as many lines as possible. Ignore
  284. * the first line, it is already in place. */
  285. unsigned int lines = height < old_height ? height : old_height;
  286. for(y = 1; y < lines; y++)
  287. {
  288. for(x = 0; x < width; x++)
  289. {
  290. qq->chars[y * width + x] = qq->chars[y * old_width + x];
  291. qq->attr[y * width + x] = qq->attr[y * old_width + x];
  292. }
  293. }
  294. }
  295. /* Step 3: fill the bottom of the new screen if necessary. */
  296. if(height > old_height)
  297. {
  298. /* Zero the bottom of the screen */
  299. for(x = (height - old_height) * width; x--; )
  300. qq->chars[old_height * width + x] = (uint32_t)' ';
  301. memset(qq->attr + old_height * width, 0,
  302. (height - old_height) * width);
  303. }
  304. /* Step 4: if new area is smaller, resize memory area now. */
  305. if(new_size <= old_size)
  306. {
  307. qq->chars = realloc(qq->chars, new_size * sizeof(uint32_t));
  308. qq->attr = realloc(qq->attr, new_size * sizeof(uint8_t));
  309. }
  310. /* Recompute the scratch line and the empty line */
  311. if(width != old_width)
  312. {
  313. qq->empty_line = realloc(qq->empty_line, width + 1);
  314. memset(qq->empty_line, ' ', width);
  315. qq->empty_line[width] = '\0';
  316. qq->scratch_line = realloc(qq->scratch_line, width + 1);
  317. }
  318. }