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 | |
---|
12 | /** \file canvas.c |
---|
13 | * \version \$Id: canvas.c 733 2006-04-10 09:17:51Z sam $ |
---|
14 | * \author Sam Hocevar <sam@zoy.org> |
---|
15 | * \brief Canvas drawing |
---|
16 | * |
---|
17 | * This file contains various canvas handling functions such as character |
---|
18 | * and string drawing. |
---|
19 | */ |
---|
20 | |
---|
21 | #include "config.h" |
---|
22 | |
---|
23 | #if !defined(__KERNEL__) |
---|
24 | # include <stdio.h> /* BUFSIZ */ |
---|
25 | # include <string.h> |
---|
26 | # include <stdlib.h> |
---|
27 | # include <stdarg.h> |
---|
28 | # if defined(HAVE_UNISTD_H) |
---|
29 | # include <unistd.h> |
---|
30 | # endif |
---|
31 | # if defined(HAVE_SIGNAL_H) |
---|
32 | # include <signal.h> |
---|
33 | # endif |
---|
34 | # if defined(HAVE_SYS_IOCTL_H) |
---|
35 | # include <sys/ioctl.h> |
---|
36 | # endif |
---|
37 | #endif |
---|
38 | |
---|
39 | #include "cucul.h" |
---|
40 | #include "cucul_internals.h" |
---|
41 | |
---|
42 | /** \brief Set the default colour pair. |
---|
43 | * |
---|
44 | * This function sets the default colour pair. String functions such as |
---|
45 | * caca_printf() and graphical primitive functions such as caca_draw_line() |
---|
46 | * will use these colour pairs. |
---|
47 | * |
---|
48 | * \param fgcolor The requested foreground colour. |
---|
49 | * \param bgcolor The requested background colour. |
---|
50 | */ |
---|
51 | void cucul_set_color(cucul_t *qq, unsigned int fgcolor, unsigned int bgcolor) |
---|
52 | { |
---|
53 | /* FIXME */ |
---|
54 | if(fgcolor < 0 || fgcolor > 15 || bgcolor < 0 || bgcolor > 15) |
---|
55 | return; |
---|
56 | |
---|
57 | qq->fgcolor = fgcolor; |
---|
58 | qq->bgcolor = bgcolor; |
---|
59 | } |
---|
60 | |
---|
61 | /** \brief Print an ASCII character. |
---|
62 | * |
---|
63 | * This function prints an ASCII character at the given coordinates, using |
---|
64 | * the default foreground and background values. If the coordinates are |
---|
65 | * outside the screen boundaries, nothing is printed. If the character |
---|
66 | * value is a non-printable character or is outside the ASCII range, it is |
---|
67 | * replaced with a space. To print a sequence of bytes forming an UTF-8 |
---|
68 | * character, use cucul_putstr() instead. |
---|
69 | * |
---|
70 | * \param x X coordinate. |
---|
71 | * \param y Y coordinate. |
---|
72 | * \param c The character to print. |
---|
73 | */ |
---|
74 | void cucul_putchar(cucul_t *qq, int x, int y, char c) |
---|
75 | { |
---|
76 | if(x < 0 || x >= (int)qq->width || |
---|
77 | y < 0 || y >= (int)qq->height) |
---|
78 | return; |
---|
79 | |
---|
80 | if((unsigned char)c < 0x20 || (unsigned char)c > 0x7f) |
---|
81 | c = 0x20; |
---|
82 | |
---|
83 | qq->chars[x + y * qq->width] = c; |
---|
84 | qq->attr[x + y * qq->width] = (qq->bgcolor << 16) | qq->fgcolor; |
---|
85 | } |
---|
86 | |
---|
87 | /** \brief Print a string. |
---|
88 | * |
---|
89 | * This function prints an UTF-8 string at the given coordinates, using the |
---|
90 | * default foreground and background values. The coordinates may be outside |
---|
91 | * the screen boundaries (eg. a negative Y coordinate) and the string will |
---|
92 | * be cropped accordingly if it is too long. |
---|
93 | * |
---|
94 | * \param x X coordinate. |
---|
95 | * \param y Y coordinate. |
---|
96 | * \param s The string to print. |
---|
97 | */ |
---|
98 | void cucul_putstr(cucul_t *qq, int x, int y, char const *s) |
---|
99 | { |
---|
100 | uint32_t *chars, *attr; |
---|
101 | unsigned int len; |
---|
102 | |
---|
103 | if(y < 0 || y >= (int)qq->height || x >= (int)qq->width) |
---|
104 | return; |
---|
105 | |
---|
106 | len = _cucul_strlen_utf8(s); |
---|
107 | |
---|
108 | if(x < 0) |
---|
109 | { |
---|
110 | if(len < (unsigned int)-x) |
---|
111 | return; |
---|
112 | len -= -x; |
---|
113 | s = _cucul_skip_utf8(s, -x); |
---|
114 | x = 0; |
---|
115 | } |
---|
116 | |
---|
117 | chars = qq->chars + x + y * qq->width; |
---|
118 | attr = qq->attr + x + y * qq->width; |
---|
119 | |
---|
120 | if(x + len >= qq->width) |
---|
121 | len = qq->width - x; |
---|
122 | |
---|
123 | while(len) |
---|
124 | { |
---|
125 | *chars++ = _cucul_utf8_to_utf32(s); |
---|
126 | *attr++ = (qq->bgcolor << 16) | qq->fgcolor; |
---|
127 | |
---|
128 | s = _cucul_skip_utf8(s, 1); |
---|
129 | len--; |
---|
130 | } |
---|
131 | } |
---|
132 | |
---|
133 | /** \brief Format a string. |
---|
134 | * |
---|
135 | * This function formats a string at the given coordinates, using the |
---|
136 | * default foreground and background values. The coordinates may be outside |
---|
137 | * the screen boundaries (eg. a negative Y coordinate) and the string will |
---|
138 | * be cropped accordingly if it is too long. The syntax of the format |
---|
139 | * string is the same as for the C printf() function. |
---|
140 | * |
---|
141 | * \param x X coordinate. |
---|
142 | * \param y Y coordinate. |
---|
143 | * \param format The format string to print. |
---|
144 | * \param ... Arguments to the format string. |
---|
145 | */ |
---|
146 | void cucul_printf(cucul_t *qq, int x, int y, char const *format, ...) |
---|
147 | { |
---|
148 | char tmp[BUFSIZ]; |
---|
149 | char *buf = tmp; |
---|
150 | va_list args; |
---|
151 | |
---|
152 | if(y < 0 || y >= (int)qq->height || x >= (int)qq->width) |
---|
153 | return; |
---|
154 | |
---|
155 | if(qq->width - x + 1 > BUFSIZ) |
---|
156 | buf = malloc(qq->width - x + 1); |
---|
157 | |
---|
158 | va_start(args, format); |
---|
159 | #if defined(HAVE_VSNPRINTF) |
---|
160 | vsnprintf(buf, qq->width - x + 1, format, args); |
---|
161 | #else |
---|
162 | vsprintf(buf, format, args); |
---|
163 | #endif |
---|
164 | buf[qq->width - x] = '\0'; |
---|
165 | va_end(args); |
---|
166 | |
---|
167 | cucul_putstr(qq, x, y, buf); |
---|
168 | |
---|
169 | if(buf != tmp) |
---|
170 | free(buf); |
---|
171 | } |
---|
172 | |
---|
173 | /** \brief Clear the screen. |
---|
174 | * |
---|
175 | * This function clears the screen using a black background. |
---|
176 | */ |
---|
177 | void cucul_clear(cucul_t *qq) |
---|
178 | { |
---|
179 | uint16_t oldfg = qq->fgcolor; |
---|
180 | uint16_t oldbg = qq->bgcolor; |
---|
181 | int y = qq->height; |
---|
182 | |
---|
183 | cucul_set_color(qq, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK); |
---|
184 | |
---|
185 | /* We could use SLsmg_cls() etc., but drawing empty lines is much faster */ |
---|
186 | while(y--) |
---|
187 | cucul_putstr(qq, 0, y, qq->empty_line); |
---|
188 | |
---|
189 | qq->fgcolor = oldfg; |
---|
190 | qq->bgcolor = oldbg; |
---|
191 | } |
---|
192 | |
---|
193 | /** \brief Blit a canvas onto another one. |
---|
194 | * |
---|
195 | * This function blits a canvas onto another one at the given coordinates. |
---|
196 | * An optional mask canvas can be used. |
---|
197 | * |
---|
198 | * \param dst The destination canvas. |
---|
199 | * \param x X coordinate. |
---|
200 | * \param y Y coordinate. |
---|
201 | * \param src The source canvas. |
---|
202 | * \param mask The mask canvas. |
---|
203 | */ |
---|
204 | void cucul_blit(cucul_t *dst, int x, int y, |
---|
205 | cucul_t const *src, cucul_t const *mask) |
---|
206 | { |
---|
207 | int i, j, starti, startj, endi, endj; |
---|
208 | |
---|
209 | if(mask && (src->width != mask->width || src->height != mask->height)) |
---|
210 | return; |
---|
211 | |
---|
212 | starti = x < 0 ? -x : 0; |
---|
213 | startj = y < 0 ? -y : 0; |
---|
214 | endi = (x + src->width >= dst->width) ? dst->width - x : src->width; |
---|
215 | endj = (y + src->height >= dst->height) ? dst->height - y : src->height; |
---|
216 | |
---|
217 | if(starti >= endi || startj >= endj) |
---|
218 | return; |
---|
219 | |
---|
220 | for(j = startj; j < endj; j++) |
---|
221 | { |
---|
222 | if(mask) |
---|
223 | { |
---|
224 | for(i = starti; i < endi; i++) |
---|
225 | { |
---|
226 | if(mask->chars[j * src->width + i] == (uint32_t)' ') |
---|
227 | continue; |
---|
228 | |
---|
229 | dst->chars[(j + y) * dst->width + (i + x)] |
---|
230 | = src->chars[j * src->width + i]; |
---|
231 | dst->attr[(j + y) * dst->width + (i + x)] |
---|
232 | = src->attr[j * src->width + i]; |
---|
233 | } |
---|
234 | } |
---|
235 | else |
---|
236 | { |
---|
237 | memcpy(dst->chars + (j + y) * dst->width + starti + x, |
---|
238 | src->chars + j * src->width + starti, |
---|
239 | (endi - starti) * 4); |
---|
240 | memcpy(dst->attr + (j + y) * dst->width + starti + x, |
---|
241 | src->attr + j * src->width + starti, |
---|
242 | (endi - starti) * 4); |
---|
243 | } |
---|
244 | } |
---|
245 | } |
---|
246 | |
---|
247 | /* |
---|
248 | * XXX: The following functions are not exported |
---|
249 | */ |
---|
250 | |
---|
251 | void _cucul_putchar32(cucul_t *qq, int x, int y, uint32_t c) |
---|
252 | { |
---|
253 | if(x < 0 || x >= (int)qq->width || |
---|
254 | y < 0 || y >= (int)qq->height) |
---|
255 | return; |
---|
256 | |
---|
257 | qq->chars[x + y * qq->width] = c; |
---|
258 | qq->attr[x + y * qq->width] = (qq->bgcolor << 16) | qq->fgcolor; |
---|
259 | } |
---|
260 | |
---|