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.
 
 
 
 
 
 

436 lines
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(attr < 0x00000010)
  124. curattr[0] = (curattr[0] & 0xfffffff0) | attr;
  125. else
  126. curattr[0] = attr;
  127. if(x && curchar[0] == CUCUL_MAGIC_FULLWIDTH)
  128. curattr[-1] = curattr[0];
  129. else if(x + 1 < (int)cv->width && curchar[1] == CUCUL_MAGIC_FULLWIDTH)
  130. curattr[1] = curattr[0];
  131. return 0;
  132. }
  133. /** \brief Set the default colour pair for text (ANSI version).
  134. *
  135. * Set the default ANSI colour pair for text drawing. String functions such
  136. * as caca_printf() and graphical primitive functions such as caca_draw_line()
  137. * will use these attributes.
  138. *
  139. * Color values are those defined in cucul.h, such as CUCUL_RED
  140. * or CUCUL_TRANSPARENT.
  141. *
  142. * If an error occurs, 0 is returned and \b errno is set accordingly:
  143. * - \c EINVAL At least one of the colour values is invalid.
  144. *
  145. * \param cv A handle to the libcucul canvas.
  146. * \param fg The requested ANSI foreground colour.
  147. * \param bg The requested ANSI background colour.
  148. * \return 0 in case of success, -1 if an error occurred.
  149. */
  150. int cucul_set_color_ansi(cucul_canvas_t *cv, unsigned char fg, unsigned char bg)
  151. {
  152. uint32_t attr;
  153. if(fg > 0x20 || bg > 0x20)
  154. {
  155. seterrno(EINVAL);
  156. return -1;
  157. }
  158. attr = ((uint32_t)(bg | 0x40) << 18) | ((uint32_t)(fg | 0x40) << 4);
  159. cv->curattr = (cv->curattr & 0x0000000f) | attr;
  160. return 0;
  161. }
  162. /** \brief Set the default colour pair for text (truecolor version).
  163. *
  164. * Set the default ARGB colour pair for text drawing. String functions such
  165. * as caca_printf() and graphical primitive functions such as caca_draw_line()
  166. * will use these attributes.
  167. *
  168. * Colors are 16-bit ARGB values, each component being coded on 4 bits. For
  169. * instance, 0xf088 is solid dark cyan (A=15 R=0 G=8 B=8), and 0x8fff is
  170. * white with 50% alpha (A=8 R=15 G=15 B=15).
  171. *
  172. * If an error occurs, 0 is returned and \b errno is set accordingly:
  173. * - \c EINVAL At least one of the colour values is invalid.
  174. *
  175. * \param cv A handle to the libcucul canvas.
  176. * \param fg The requested ARGB foreground colour.
  177. * \param bg The requested ARGB background colour.
  178. * \return 0 in case of success, -1 if an error occurred.
  179. */
  180. int cucul_set_color_argb(cucul_canvas_t *cv, unsigned int fg, unsigned int bg)
  181. {
  182. uint32_t attr;
  183. if(fg > 0xffff || bg > 0xffff)
  184. {
  185. seterrno(EINVAL);
  186. return -1;
  187. }
  188. if(fg < 0x100)
  189. fg += 0x100;
  190. if(bg < 0x100)
  191. bg += 0x100;
  192. fg = ((fg >> 1) & 0x7ff) | ((fg >> 13) << 11);
  193. bg = ((bg >> 1) & 0x7ff) | ((bg >> 13) << 11);
  194. attr = ((uint32_t)bg << 18) | ((uint32_t)fg << 4);
  195. cv->curattr = (cv->curattr & 0x0000000f) | attr;
  196. return 0;
  197. }
  198. /** \brief Get DOS ANSI information from attribute.
  199. *
  200. * Get the ANSI colour pair for a given attribute. The returned value is
  201. * an 8-bit value whose higher 4 bits are the background colour and lower
  202. * 4 bits are the foreground colour.
  203. *
  204. * If the attribute has ARGB colours, the nearest colour is used. Special
  205. * attributes such as \e CUCUL_DEFAULT and \e CUCUL_TRANSPARENT are not
  206. * handled and are both replaced with \e CUCUL_LIGHTGRAY for the foreground
  207. * colour and \e CUCUL_BLACK for the background colour.
  208. *
  209. * This function never fails. If the attribute value is outside the expected
  210. * 32-bit range, higher order bits are simply ignored.
  211. *
  212. * \param attr The requested attribute value.
  213. * \return The corresponding DOS ANSI value.
  214. */
  215. unsigned char cucul_attr_to_ansi(unsigned long int attr)
  216. {
  217. uint8_t fg = nearest_ansi((attr >> 4) & 0x3fff);
  218. uint8_t bg = nearest_ansi(attr >> 18);
  219. return (fg < 0x10 ? fg : CUCUL_LIGHTGRAY)
  220. | ((bg < 0x10 ? bg : CUCUL_BLACK) << 4);
  221. }
  222. /** \brief Get ANSI foreground information from attribute.
  223. *
  224. * Get the ANSI foreground colour value for a given attribute. The returned
  225. * value is either one of the \e CUCUL_RED, \e CUCUL_BLACK etc. predefined
  226. * colours, or the special value \e CUCUL_DEFAULT meaning the media's
  227. * default foreground value, or the special value \e CUCUL_TRANSPARENT.
  228. *
  229. * If the attribute has ARGB colours, the nearest colour is returned.
  230. *
  231. * This function never fails. If the attribute value is outside the expected
  232. * 32-bit range, higher order bits are simply ignored.
  233. *
  234. * \param attr The requested attribute value.
  235. * \return The corresponding ANSI foreground value.
  236. */
  237. unsigned char cucul_attr_to_ansi_fg(unsigned long int attr)
  238. {
  239. return nearest_ansi(((uint16_t)attr >> 4) & 0x3fff);
  240. }
  241. /** \brief Get ANSI background information from attribute.
  242. *
  243. * Get the ANSI background colour value for a given attribute. The returned
  244. * value is either one of the \e CUCUL_RED, \e CUCUL_BLACK etc. predefined
  245. * colours, or the special value \e CUCUL_DEFAULT meaning the media's
  246. * default background value, or the special value \e CUCUL_TRANSPARENT.
  247. *
  248. * If the attribute has ARGB colours, the nearest colour is returned.
  249. *
  250. * This function never fails. If the attribute value is outside the expected
  251. * 32-bit range, higher order bits are simply ignored.
  252. *
  253. * \param attr The requested attribute value.
  254. * \return The corresponding ANSI background value.
  255. */
  256. unsigned char cucul_attr_to_ansi_bg(unsigned long int attr)
  257. {
  258. return nearest_ansi(attr >> 18);
  259. }
  260. /*
  261. * XXX: the following functions are local
  262. */
  263. /* RGB colours for the ANSI palette. There is no real standard, so we
  264. * use the same values as gnome-terminal. The 7th colour (brown) is a bit
  265. * special: 0xfa50 instead of 0xfaa0. */
  266. static const uint16_t ansitab16[16] =
  267. {
  268. 0xf000, 0xf00a, 0xf0a0, 0xf0aa, 0xfa00, 0xfa0a, 0xfa50, 0xfaaa,
  269. 0xf555, 0xf55f, 0xf5f5, 0xf5ff, 0xff55, 0xff5f, 0xfff5, 0xffff,
  270. };
  271. /* Same table, except on 14 bits (3-4-4-3) */
  272. static const uint16_t ansitab14[16] =
  273. {
  274. 0x3800, 0x3805, 0x3850, 0x3855, 0x3d00, 0x3d05, 0x3d28, 0x3d55,
  275. 0x3aaa, 0x3aaf, 0x3afa, 0x3aff, 0x3faa, 0x3faf, 0x3ffa, 0x3fff,
  276. };
  277. static uint8_t nearest_ansi(uint16_t argb14)
  278. {
  279. unsigned int i, best, dist;
  280. if(argb14 < (0x10 | 0x40))
  281. return argb14 ^ 0x40;
  282. if(argb14 == (CUCUL_DEFAULT | 0x40) || argb14 == (CUCUL_TRANSPARENT | 0x40))
  283. return argb14 ^ 0x40;
  284. if(argb14 < 0x0fff) /* too transparent */
  285. return CUCUL_TRANSPARENT;
  286. best = CUCUL_DEFAULT;
  287. dist = 0x3fff;
  288. for(i = 0; i < 16; i++)
  289. {
  290. unsigned int d = 0;
  291. int a, b;
  292. a = (ansitab14[i] >> 7) & 0xf;
  293. b = (argb14 >> 7) & 0xf;
  294. d += (a - b) * (a - b);
  295. a = (ansitab14[i] >> 3) & 0xf;
  296. b = (argb14 >> 3) & 0xf;
  297. d += (a - b) * (a - b);
  298. a = (ansitab14[i] << 1) & 0xf;
  299. b = (argb14 << 1) & 0xf;
  300. d += (a - b) * (a - b);
  301. if(d < dist)
  302. {
  303. dist = d;
  304. best = i;
  305. }
  306. }
  307. return best;
  308. }
  309. uint16_t _cucul_attr_to_rgb12fg(uint32_t attr)
  310. {
  311. uint16_t fg = (attr >> 4) & 0x3fff;
  312. if(fg < (0x10 | 0x40))
  313. return ansitab16[fg ^ 0x40] & 0x0fff;
  314. if(fg == (CUCUL_DEFAULT | 0x40))
  315. return ansitab16[CUCUL_LIGHTGRAY] & 0x0fff;
  316. if(fg == (CUCUL_TRANSPARENT | 0x40))
  317. return ansitab16[CUCUL_LIGHTGRAY] & 0x0fff;
  318. return (fg << 1) & 0x0fff;
  319. }
  320. uint16_t _cucul_attr_to_rgb12bg(uint32_t attr)
  321. {
  322. uint16_t bg = attr >> 18;
  323. if(bg < (0x10 | 0x40))
  324. return ansitab16[bg ^ 0x40] & 0x0fff;
  325. if(bg == (CUCUL_DEFAULT | 0x40))
  326. return ansitab16[CUCUL_BLACK] & 0x0fff;
  327. if(bg == (CUCUL_TRANSPARENT | 0x40))
  328. return ansitab16[CUCUL_BLACK] & 0x0fff;
  329. return (bg << 1) & 0x0fff;
  330. }
  331. #define RGB12TO24(i) \
  332. (((uint32_t)((i & 0xf00) >> 8) * 0x110000) \
  333. | ((uint32_t)((i & 0x0f0) >> 4) * 0x001100) \
  334. | ((uint32_t)(i & 0x00f) * 0x000011))
  335. uint32_t _cucul_attr_to_rgb24fg(uint32_t attr)
  336. {
  337. return RGB12TO24(_cucul_attr_to_rgb12fg(attr));
  338. }
  339. uint32_t _cucul_attr_to_rgb24bg(uint32_t attr)
  340. {
  341. return RGB12TO24(_cucul_attr_to_rgb12bg(attr));
  342. }
  343. void _cucul_attr_to_argb4(uint32_t attr, uint8_t argb[8])
  344. {
  345. uint16_t fg = (attr >> 4) & 0x3fff;
  346. uint16_t bg = attr >> 18;
  347. if(bg < (0x10 | 0x40))
  348. bg = ansitab16[bg ^ 0x40];
  349. else if(bg == (CUCUL_DEFAULT | 0x40))
  350. bg = ansitab16[CUCUL_BLACK];
  351. else if(bg == (CUCUL_TRANSPARENT | 0x40))
  352. bg = 0x0fff;
  353. else
  354. bg = ((bg << 2) & 0xf000) | ((bg << 1) & 0x0fff);
  355. argb[0] = bg >> 12;
  356. argb[1] = (bg >> 8) & 0xf;
  357. argb[2] = (bg >> 4) & 0xf;
  358. argb[3] = bg & 0xf;
  359. if(fg < (0x10 | 0x40))
  360. fg = ansitab16[fg ^ 0x40];
  361. else if(fg == (CUCUL_DEFAULT | 0x40))
  362. fg = ansitab16[CUCUL_LIGHTGRAY];
  363. else if(fg == (CUCUL_TRANSPARENT | 0x40))
  364. fg = 0x0fff;
  365. else
  366. fg = ((fg << 2) & 0xf000) | ((fg << 1) & 0x0fff);
  367. argb[4] = fg >> 12;
  368. argb[5] = (fg >> 8) & 0xf;
  369. argb[6] = (fg >> 4) & 0xf;
  370. argb[7] = fg & 0xf;
  371. }