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.
 
 
 
 
 
 

434 line
13 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. * $Id$
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. /*
  14. * This file contains functions for attribute management and colourspace
  15. * conversions.
  16. */
  17. #include "config.h"
  18. #include "common.h"
  19. #include "cucul.h"
  20. #include "cucul_internals.h"
  21. static uint8_t nearest_ansi(uint16_t);
  22. /** \brief Get the text attribute at the given coordinates.
  23. *
  24. * Get the internal \e libcucul attribute value of the character at the
  25. * given coordinates. The attribute value has 32 significant bits,
  26. * organised as follows from MSB to LSB:
  27. * - 3 bits for the background alpha
  28. * - 4 bits for the background red component
  29. * - 4 bits for the background green component
  30. * - 3 bits for the background blue component
  31. * - 3 bits for the foreground alpha
  32. * - 4 bits for the foreground red component
  33. * - 4 bits for the foreground green component
  34. * - 3 bits for the foreground blue component
  35. * - 4 bits for the bold, italics, underline and blink flags
  36. *
  37. * If the coordinates are outside the canvas boundaries, the current
  38. * attribute is returned.
  39. *
  40. * This function never fails.
  41. *
  42. * \param cv A handle to the libcucul canvas.
  43. * \param x X coordinate.
  44. * \param y Y coordinate.
  45. * \return The requested attribute.
  46. */
  47. unsigned long int cucul_get_attr(cucul_canvas_t *cv, int x, int y)
  48. {
  49. if(x < 0 || x >= (int)cv->width || y < 0 || y >= (int)cv->height)
  50. return (unsigned long int)cv->curattr;
  51. return (unsigned long int)cv->attrs[x + y * cv->width];
  52. }
  53. /** \brief Set the default character attribute.
  54. *
  55. * Set the default character attribute for drawing. Attributes define
  56. * foreground and background colour, transparency, bold, italics and
  57. * underline styles, as well as blink. String functions such as
  58. * caca_printf() and graphical primitive functions such as caca_draw_line()
  59. * will use this attribute.
  60. *
  61. * The value of \e attr is either:
  62. * - a 32-bit integer as returned by cucul_get_attr(), in which case it
  63. * also contains colour information,
  64. * - a combination (bitwise OR) of style values (\e CUCUL_UNDERLINE,
  65. * \e CUCUL_BLINK, \e CUCUL_BOLD and \e CUCUL_ITALICS), in which case
  66. * setting the attribute does not modify the current colour information.
  67. *
  68. * To retrieve the current attribute value, use cucul_get_attr(-1,-1).
  69. *
  70. * If an error occurs, -1 is returned and \b errno is set accordingly:
  71. * - \c EINVAL The attribute value is out of the 32-bit range.
  72. *
  73. * \param cv A handle to the libcucul canvas.
  74. * \param attr The requested attribute value.
  75. * \return 0 in case of success, -1 if an error occurred.
  76. */
  77. int cucul_set_attr(cucul_canvas_t *cv, unsigned long int attr)
  78. {
  79. if(sizeof(unsigned long int) > sizeof(uint32_t) && attr > 0xffffffff)
  80. {
  81. seterrno(EINVAL);
  82. return -1;
  83. }
  84. if(attr < 0x00000010)
  85. attr = (cv->curattr & 0xfffffff0) | attr;
  86. cv->curattr = attr;
  87. return 0;
  88. }
  89. /** \brief Set the character attribute at the given coordinates.
  90. *
  91. * Set the character attribute, without changing the character's value. If
  92. * the character at the given coordinates is a fullwidth character, both
  93. * cells' attributes are replaced.
  94. *
  95. * The value of \e attr is either:
  96. * - a 32-bit integer as returned by cucul_get_attr(), in which case it
  97. * also contains colour information,
  98. * - a combination (bitwise OR) of style values (\e CUCUL_UNDERLINE,
  99. * \e CUCUL_BLINK, \e CUCUL_BOLD and \e CUCUL_ITALICS), in which case
  100. * setting the attribute does not modify the current colour information.
  101. *
  102. * If an error occurs, -1 is returned and \b errno is set accordingly:
  103. * - \c EINVAL The attribute value is out of the 32-bit range.
  104. *
  105. * \param cv A handle to the libcucul canvas.
  106. * \param x X coordinate.
  107. * \param y Y coordinate.
  108. * \param attr The requested attribute value.
  109. * \return 0 in case of success, -1 if an error occurred.
  110. */
  111. int cucul_put_attr(cucul_canvas_t *cv, int x, int y, unsigned long int attr)
  112. {
  113. uint32_t *curattr, *curchar;
  114. if(sizeof(unsigned long int) > sizeof(uint32_t) && attr > 0xffffffff)
  115. {
  116. seterrno(EINVAL);
  117. return -1;
  118. }
  119. if(x < 0 || x >= (int)cv->width || y < 0 || y >= (int)cv->height)
  120. return 0;
  121. curchar = cv->chars + x + y * cv->width;
  122. curattr = cv->attrs + x + y * cv->width;
  123. if(curattr[0] < 0x00000010)
  124. curattr[0] = (cv->curattr & 0xfffffff0) | curattr[0];
  125. if(x && curchar[0] == CUCUL_MAGIC_FULLWIDTH)
  126. curattr[-1] = curattr[0];
  127. else if(x + 1 < (int)cv->width && curchar[1] == CUCUL_MAGIC_FULLWIDTH)
  128. curattr[1] = curattr[0];
  129. return 0;
  130. }
  131. /** \brief Set the default colour pair for text (ANSI version).
  132. *
  133. * Set the default ANSI colour pair for text drawing. String functions such
  134. * as caca_printf() and graphical primitive functions such as caca_draw_line()
  135. * will use these attributes.
  136. *
  137. * Color values are those defined in cucul.h, such as CUCUL_RED
  138. * or CUCUL_TRANSPARENT.
  139. *
  140. * If an error occurs, 0 is returned and \b errno is set accordingly:
  141. * - \c EINVAL At least one of the colour values is invalid.
  142. *
  143. * \param cv A handle to the libcucul canvas.
  144. * \param fg The requested ANSI foreground colour.
  145. * \param bg The requested ANSI background colour.
  146. * \return 0 in case of success, -1 if an error occurred.
  147. */
  148. int cucul_set_color_ansi(cucul_canvas_t *cv, unsigned char fg, unsigned char bg)
  149. {
  150. uint32_t attr;
  151. if(fg > 0x20 || bg > 0x20)
  152. {
  153. seterrno(EINVAL);
  154. return -1;
  155. }
  156. attr = ((uint32_t)(bg | 0x40) << 18) | ((uint32_t)(fg | 0x40) << 4);
  157. cv->curattr = (cv->curattr & 0x0000000f) | attr;
  158. return 0;
  159. }
  160. /** \brief Set the default colour pair for text (truecolor version).
  161. *
  162. * Set the default ARGB colour pair for text drawing. String functions such
  163. * as caca_printf() and graphical primitive functions such as caca_draw_line()
  164. * will use these attributes.
  165. *
  166. * Colors are 16-bit ARGB values, each component being coded on 4 bits. For
  167. * instance, 0xf088 is solid dark cyan (A=15 R=0 G=8 B=8), and 0x8fff is
  168. * white with 50% alpha (A=8 R=15 G=15 B=15).
  169. *
  170. * If an error occurs, 0 is returned and \b errno is set accordingly:
  171. * - \c EINVAL At least one of the colour values is invalid.
  172. *
  173. * \param cv A handle to the libcucul canvas.
  174. * \param fg The requested ARGB foreground colour.
  175. * \param bg The requested ARGB background colour.
  176. * \return 0 in case of success, -1 if an error occurred.
  177. */
  178. int cucul_set_color_argb(cucul_canvas_t *cv, unsigned int fg, unsigned int bg)
  179. {
  180. uint32_t attr;
  181. if(fg > 0xffff || bg > 0xffff)
  182. {
  183. seterrno(EINVAL);
  184. return -1;
  185. }
  186. if(fg < 0x100)
  187. fg += 0x100;
  188. if(bg < 0x100)
  189. bg += 0x100;
  190. fg = ((fg >> 1) & 0x7ff) | ((fg >> 13) << 11);
  191. bg = ((bg >> 1) & 0x7ff) | ((bg >> 13) << 11);
  192. attr = ((uint32_t)bg << 18) | ((uint32_t)fg << 4);
  193. cv->curattr = (cv->curattr & 0x0000000f) | attr;
  194. return 0;
  195. }
  196. /** \brief Get DOS ANSI information from attribute.
  197. *
  198. * Get the ANSI colour pair for a given attribute. The returned value is
  199. * an 8-bit value whose higher 4 bits are the background colour and lower
  200. * 4 bits are the foreground colour.
  201. *
  202. * If the attribute has ARGB colours, the nearest colour is used. Special
  203. * attributes such as \e CUCUL_DEFAULT and \e CUCUL_TRANSPARENT are not
  204. * handled and are both replaced with \e CUCUL_LIGHTGRAY for the foreground
  205. * colour and \e CUCUL_BLACK for the background colour.
  206. *
  207. * This function never fails. If the attribute value is outside the expected
  208. * 32-bit range, higher order bits are simply ignored.
  209. *
  210. * \param attr The requested attribute value.
  211. * \return The corresponding DOS ANSI value.
  212. */
  213. unsigned char cucul_attr_to_ansi(unsigned long int attr)
  214. {
  215. uint8_t fg = nearest_ansi((attr >> 4) & 0x3fff);
  216. uint8_t bg = nearest_ansi(attr >> 18);
  217. return (fg < 0x10 ? fg : CUCUL_LIGHTGRAY)
  218. | ((bg < 0x10 ? bg : CUCUL_BLACK) << 4);
  219. }
  220. /** \brief Get ANSI foreground information from attribute.
  221. *
  222. * Get the ANSI foreground colour value for a given attribute. The returned
  223. * value is either one of the \e CUCUL_RED, \e CUCUL_BLACK etc. predefined
  224. * colours, or the special value \e CUCUL_DEFAULT meaning the media's
  225. * default foreground value, or the special value \e CUCUL_TRANSPARENT.
  226. *
  227. * If the attribute has ARGB colours, the nearest colour is returned.
  228. *
  229. * This function never fails. If the attribute value is outside the expected
  230. * 32-bit range, higher order bits are simply ignored.
  231. *
  232. * \param attr The requested attribute value.
  233. * \return The corresponding ANSI foreground value.
  234. */
  235. unsigned char cucul_attr_to_ansi_fg(unsigned long int attr)
  236. {
  237. return nearest_ansi(((uint16_t)attr >> 4) & 0x3fff);
  238. }
  239. /** \brief Get ANSI background information from attribute.
  240. *
  241. * Get the ANSI background colour value for a given attribute. The returned
  242. * value is either one of the \e CUCUL_RED, \e CUCUL_BLACK etc. predefined
  243. * colours, or the special value \e CUCUL_DEFAULT meaning the media's
  244. * default background value, or the special value \e CUCUL_TRANSPARENT.
  245. *
  246. * If the attribute has ARGB colours, the nearest colour is returned.
  247. *
  248. * This function never fails. If the attribute value is outside the expected
  249. * 32-bit range, higher order bits are simply ignored.
  250. *
  251. * \param attr The requested attribute value.
  252. * \return The corresponding ANSI background value.
  253. */
  254. unsigned char cucul_attr_to_ansi_bg(unsigned long int attr)
  255. {
  256. return nearest_ansi(attr >> 18);
  257. }
  258. /*
  259. * XXX: the following functions are local
  260. */
  261. /* RGB colours for the ANSI palette. There is no real standard, so we
  262. * use the same values as gnome-terminal. The 7th colour (brown) is a bit
  263. * special: 0xfa50 instead of 0xfaa0. */
  264. static const uint16_t ansitab16[16] =
  265. {
  266. 0xf000, 0xf00a, 0xf0a0, 0xf0aa, 0xfa00, 0xfa0a, 0xfa50, 0xfaaa,
  267. 0xf555, 0xf55f, 0xf5f5, 0xf5ff, 0xff55, 0xff5f, 0xfff5, 0xffff,
  268. };
  269. /* Same table, except on 14 bits (3-4-4-3) */
  270. static const uint16_t ansitab14[16] =
  271. {
  272. 0x3800, 0x3805, 0x3850, 0x3855, 0x3d00, 0x3d05, 0x3d28, 0x3d55,
  273. 0x3aaa, 0x3aaf, 0x3afa, 0x3aff, 0x3faa, 0x3faf, 0x3ffa, 0x3fff,
  274. };
  275. static uint8_t nearest_ansi(uint16_t argb14)
  276. {
  277. unsigned int i, best, dist;
  278. if(argb14 < (0x10 | 0x40))
  279. return argb14 ^ 0x40;
  280. if(argb14 == (CUCUL_DEFAULT | 0x40) || argb14 == (CUCUL_TRANSPARENT | 0x40))
  281. return argb14 ^ 0x40;
  282. if(argb14 < 0x0fff) /* too transparent */
  283. return CUCUL_TRANSPARENT;
  284. best = CUCUL_DEFAULT;
  285. dist = 0x3fff;
  286. for(i = 0; i < 16; i++)
  287. {
  288. unsigned int d = 0;
  289. int a, b;
  290. a = (ansitab14[i] >> 7) & 0xf;
  291. b = (argb14 >> 7) & 0xf;
  292. d += (a - b) * (a - b);
  293. a = (ansitab14[i] >> 3) & 0xf;
  294. b = (argb14 >> 3) & 0xf;
  295. d += (a - b) * (a - b);
  296. a = (ansitab14[i] << 1) & 0xf;
  297. b = (argb14 << 1) & 0xf;
  298. d += (a - b) * (a - b);
  299. if(d < dist)
  300. {
  301. dist = d;
  302. best = i;
  303. }
  304. }
  305. return best;
  306. }
  307. uint16_t _cucul_attr_to_rgb12fg(uint32_t attr)
  308. {
  309. uint16_t fg = (attr >> 4) & 0x3fff;
  310. if(fg < (0x10 | 0x40))
  311. return ansitab16[fg ^ 0x40] & 0x0fff;
  312. if(fg == (CUCUL_DEFAULT | 0x40))
  313. return ansitab16[CUCUL_LIGHTGRAY] & 0x0fff;
  314. if(fg == (CUCUL_TRANSPARENT | 0x40))
  315. return ansitab16[CUCUL_LIGHTGRAY] & 0x0fff;
  316. return (fg << 1) & 0x0fff;
  317. }
  318. uint16_t _cucul_attr_to_rgb12bg(uint32_t attr)
  319. {
  320. uint16_t bg = attr >> 18;
  321. if(bg < (0x10 | 0x40))
  322. return ansitab16[bg ^ 0x40] & 0x0fff;
  323. if(bg == (CUCUL_DEFAULT | 0x40))
  324. return ansitab16[CUCUL_BLACK] & 0x0fff;
  325. if(bg == (CUCUL_TRANSPARENT | 0x40))
  326. return ansitab16[CUCUL_BLACK] & 0x0fff;
  327. return (bg << 1) & 0x0fff;
  328. }
  329. #define RGB12TO24(i) \
  330. (((uint32_t)((i & 0xf00) >> 8) * 0x110000) \
  331. | ((uint32_t)((i & 0x0f0) >> 4) * 0x001100) \
  332. | ((uint32_t)(i & 0x00f) * 0x000011))
  333. uint32_t _cucul_attr_to_rgb24fg(uint32_t attr)
  334. {
  335. return RGB12TO24(_cucul_attr_to_rgb12fg(attr));
  336. }
  337. uint32_t _cucul_attr_to_rgb24bg(uint32_t attr)
  338. {
  339. return RGB12TO24(_cucul_attr_to_rgb12bg(attr));
  340. }
  341. void _cucul_attr_to_argb4(uint32_t attr, uint8_t argb[8])
  342. {
  343. uint16_t fg = (attr >> 4) & 0x3fff;
  344. uint16_t bg = attr >> 18;
  345. if(bg < (0x10 | 0x40))
  346. bg = ansitab16[bg ^ 0x40];
  347. else if(bg == (CUCUL_DEFAULT | 0x40))
  348. bg = ansitab16[CUCUL_BLACK];
  349. else if(bg == (CUCUL_TRANSPARENT | 0x40))
  350. bg = 0x0fff;
  351. else
  352. bg = ((bg << 2) & 0xf000) | ((bg << 1) & 0x0fff);
  353. argb[0] = bg >> 12;
  354. argb[1] = (bg >> 8) & 0xf;
  355. argb[2] = (bg >> 4) & 0xf;
  356. argb[3] = bg & 0xf;
  357. if(fg < (0x10 | 0x40))
  358. fg = ansitab16[fg ^ 0x40];
  359. else if(fg == (CUCUL_DEFAULT | 0x40))
  360. fg = ansitab16[CUCUL_LIGHTGRAY];
  361. else if(fg == (CUCUL_TRANSPARENT | 0x40))
  362. fg = 0x0fff;
  363. else
  364. fg = ((fg << 2) & 0xf000) | ((fg << 1) & 0x0fff);
  365. argb[4] = fg >> 12;
  366. argb[5] = (fg >> 8) & 0xf;
  367. argb[6] = (fg >> 4) & 0xf;
  368. argb[7] = fg & 0xf;
  369. }