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.
 
 
 
 
 
 

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