您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

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