source: libcaca/trunk/cucul/canvas.c @ 874

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