Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

518 рядки
15 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. /**
  299. * \brief Get available export formats
  300. *
  301. * Return a list of available export formats. The list is a NULL-terminated
  302. * array of strings, interleaving a string containing the internal value for
  303. * the export format, to be used with \e cucul_export(), and a string
  304. * containing the natural language description for that export format.
  305. *
  306. * \return An array of strings.
  307. */
  308. char const * const * cucul_get_export_list(void)
  309. {
  310. static char const * const list[] =
  311. {
  312. "ansi", "ANSI",
  313. "html", "HTML",
  314. "html3", "backwards-compatible HTML",
  315. "irc", "IRC (mIRC colours)",
  316. "ps", "PostScript",
  317. "svg", "SVG",
  318. NULL, NULL
  319. };
  320. return list;
  321. }
  322. void cucul_free_export(struct cucul_export *ex)
  323. {
  324. free(ex->buffer);
  325. free(ex);
  326. }
  327. /*
  328. * XXX: The following functions are local.
  329. */
  330. static void cucul_read_environment(cucul_t * qq)
  331. {
  332. /* FIXME: if strcasecmp isn't available, use strcmp */
  333. #if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
  334. char *var;
  335. #endif
  336. cucul_set_feature(qq, CUCUL_BACKGROUND);
  337. cucul_set_feature(qq, CUCUL_ANTIALIASING);
  338. cucul_set_feature(qq, CUCUL_DITHERING);
  339. #if defined(HAVE_GETENV) && defined(HAVE_STRCASECMP)
  340. if((var = getenv("CUCUL_BACKGROUND")) && *var)
  341. {
  342. if(!strcasecmp("black", var))
  343. cucul_set_feature(qq, CUCUL_BACKGROUND_BLACK);
  344. else if(!strcasecmp("solid", var))
  345. cucul_set_feature(qq, CUCUL_BACKGROUND_SOLID);
  346. }
  347. if((var = getenv("CUCUL_ANTIALIASING")) && *var)
  348. {
  349. if(!strcasecmp("none", var))
  350. cucul_set_feature(qq, CUCUL_ANTIALIASING_NONE);
  351. else if(!strcasecmp("prefilter", var))
  352. cucul_set_feature(qq, CUCUL_ANTIALIASING_PREFILTER);
  353. }
  354. if((var = getenv("CUCUL_DITHERING")) && *var)
  355. {
  356. if(!strcasecmp("none", var))
  357. cucul_set_feature(qq, CUCUL_DITHERING_NONE);
  358. else if(!strcasecmp("ordered2", var))
  359. cucul_set_feature(qq, CUCUL_DITHERING_ORDERED2);
  360. else if(!strcasecmp("ordered4", var))
  361. cucul_set_feature(qq, CUCUL_DITHERING_ORDERED4);
  362. else if(!strcasecmp("ordered8", var))
  363. cucul_set_feature(qq, CUCUL_DITHERING_ORDERED8);
  364. else if(!strcasecmp("random", var))
  365. cucul_set_feature(qq, CUCUL_DITHERING_RANDOM);
  366. else if(!strcasecmp("fstein", var))
  367. cucul_set_feature(qq, CUCUL_DITHERING_FSTEIN);
  368. }
  369. #endif
  370. }
  371. void _cucul_set_size(cucul_t *qq, unsigned int width, unsigned int height)
  372. {
  373. unsigned int x, y, old_width, old_height, new_size, old_size;
  374. old_width = qq->width;
  375. old_height = qq->height;
  376. old_size = old_width * old_height;
  377. qq->width = width;
  378. qq->height = height;
  379. new_size = width * height;
  380. /* Step 1: if new area is bigger, resize the memory area now. */
  381. if(new_size > old_size)
  382. {
  383. qq->chars = realloc(qq->chars, new_size * sizeof(uint32_t));
  384. qq->attr = realloc(qq->attr, new_size * sizeof(uint8_t));
  385. }
  386. /* Step 2: move line data if necessary. */
  387. if(width == old_width)
  388. {
  389. /* Width did not change, which means we do not need to move data. */
  390. ;
  391. }
  392. else if(width > old_width)
  393. {
  394. /* New width is bigger than old width, which means we need to
  395. * copy lines starting from the bottom of the screen otherwise
  396. * we will overwrite information. */
  397. for(y = height < old_height ? height : old_height; y--; )
  398. {
  399. for(x = old_width; x--; )
  400. {
  401. qq->chars[y * width + x] = qq->chars[y * old_width + x];
  402. qq->attr[y * width + x] = qq->attr[y * old_width + x];
  403. }
  404. /* Zero the end of the line */
  405. for(x = width - old_width; x--; )
  406. qq->chars[y * width + old_width + x] = (uint32_t)' ';
  407. memset(qq->attr + y * width + old_width, 0,
  408. width - old_width);
  409. }
  410. }
  411. else
  412. {
  413. /* New width is smaller. Copy as many lines as possible. Ignore
  414. * the first line, it is already in place. */
  415. unsigned int lines = height < old_height ? height : old_height;
  416. for(y = 1; y < lines; y++)
  417. {
  418. for(x = 0; x < width; x++)
  419. {
  420. qq->chars[y * width + x] = qq->chars[y * old_width + x];
  421. qq->attr[y * width + x] = qq->attr[y * old_width + x];
  422. }
  423. }
  424. }
  425. /* Step 3: fill the bottom of the new screen if necessary. */
  426. if(height > old_height)
  427. {
  428. /* Zero the bottom of the screen */
  429. for(x = (height - old_height) * width; x--; )
  430. qq->chars[old_height * width + x] = (uint32_t)' ';
  431. memset(qq->attr + old_height * width, 0,
  432. (height - old_height) * width);
  433. }
  434. /* Step 4: if new area is smaller, resize memory area now. */
  435. if(new_size <= old_size)
  436. {
  437. qq->chars = realloc(qq->chars, new_size * sizeof(uint32_t));
  438. qq->attr = realloc(qq->attr, new_size * sizeof(uint8_t));
  439. }
  440. /* Recompute the scratch line and the empty line */
  441. if(width != old_width)
  442. {
  443. qq->empty_line = realloc(qq->empty_line, width + 1);
  444. memset(qq->empty_line, ' ', width);
  445. qq->empty_line[width] = '\0';
  446. qq->scratch_line = realloc(qq->scratch_line, width + 1);
  447. }
  448. }