source: libcaca/trunk/cucul/attr.c @ 1947

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