25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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