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

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