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

Last change on this file since 1341 was 1341, checked in by Sam Hocevar, 16 years ago
  • Cosmetic code and documentation changes here and there.
  • Updated TODO.
  • Property svn:keywords set to Id
File size: 11.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 1341 2006-11-11 13:29:03Z 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 or Unicode character.
45 *
46 *  Print an ASCII or Unicode character at the given coordinates, using
47 *  the default foreground and background colour values.
48 *
49 *  If the coordinates are outside the canvas boundaries, nothing is printed.
50 *  If a fullwidth Unicode character gets overwritten, its remaining visible
51 *  parts are replaced with spaces. If the canvas' boundaries would split the
52 *  fullwidth character in two, a space is printed instead.
53 *
54 *  The behaviour when printing non-printable characters or invalid UTF-32
55 *  characters is undefined. To print a sequence of bytes forming an UTF-8
56 *  character instead of an UTF-32 character, use the cucul_putstr() function.
57 *
58 *  This function never fails.
59 *
60 *  \param cv A handle to the libcucul canvas.
61 *  \param x X coordinate.
62 *  \param y Y coordinate.
63 *  \param ch The character to print.
64 *  \return This function always returns 0.
65 */
66int cucul_putchar(cucul_canvas_t *cv, int x, int y, unsigned long int ch)
67{
68    uint32_t *curchar, *curattr, attr;
69    int fullwidth;
70
71    if(x >= (int)cv->width || y < 0 || y >= (int)cv->height)
72        return 0;
73
74    if(ch == CUCUL_MAGIC_FULLWIDTH)
75        return 0;
76
77    fullwidth = cucul_utf32_is_fullwidth(ch);
78
79    if(x == -1 && fullwidth)
80    {
81        x = 0;
82        ch = ' ';
83        fullwidth = 0;
84    }
85    else if(x < 0)
86        return 0;
87
88    curchar = cv->chars + x + y * cv->width;
89    curattr = cv->attrs + x + y * cv->width;
90    attr = cv->curattr;
91
92    /* When overwriting the right part of a fullwidth character,
93     * replace its left part with a space. */
94    if(x && curchar[0] == CUCUL_MAGIC_FULLWIDTH)
95        curchar[-1] = ' ';
96
97    if(fullwidth)
98    {
99        if(x + 1 == (int)cv->width)
100            ch = ' ';
101        else
102        {
103            /* When overwriting the left part of a fullwidth character,
104             * replace its right part with a space. */
105            if(x + 2 < (int)cv->width && curchar[2] == CUCUL_MAGIC_FULLWIDTH)
106                curchar[2] = ' ';
107
108            curchar[1] = CUCUL_MAGIC_FULLWIDTH;
109            curattr[1] = attr;
110        }
111    }
112    else
113    {
114        /* When overwriting the left part of a fullwidth character,
115         * replace its right part with a space. */
116        if(x + 1 != (int)cv->width && curchar[1] == CUCUL_MAGIC_FULLWIDTH)
117            curchar[1] = ' ';
118    }
119
120    curchar[0] = ch;
121    curattr[0] = attr;
122
123    return 0;
124}
125
126/** \brief Get the Unicode character at the given coordinates.
127 *
128 *  Get the ASCII or Unicode value of the character at the given
129 *  coordinates. If the value is less or equal to 127 (0x7f),
130 *  the character can be printed as ASCII. Otherise, it must be handled
131 *  as a UTF-32 value.
132 *
133 *  If the coordinates are outside the canvas boundaries, a space (0x20)
134 *  is returned.
135 *
136 *  A special exception is when CUCUL_MAGIC_FULLWIDTH is returned. This
137 *  value is guaranteed not to be a valid Unicode character, and indicates
138 *  that the character at the left of the requested one is a fullwidth
139 *  character.
140 *
141 *  This function never fails.
142 *
143 *  \param cv A handle to the libcucul canvas.
144 *  \param x X coordinate.
145 *  \param y Y coordinate.
146 *  \return This function always returns 0.
147 */
148unsigned long int cucul_getchar(cucul_canvas_t *cv, int x, int y)
149{
150    if(x < 0 || x >= (int)cv->width || y < 0 || y >= (int)cv->height)
151        return ' ';
152
153    return (unsigned long int)cv->chars[x + y * cv->width];
154}
155
156/** \brief Print a string.
157 *
158 *  Print an UTF-8 string at the given coordinates, using the default
159 *  foreground and background values. The coordinates may be outside the
160 *  canvas boundaries (eg. a negative Y coordinate) and the string will
161 *  be cropped accordingly if it is too long.
162 *
163 *  See cucul_putchar() for more information on how fullwidth characters
164 *  are handled when overwriting each other or at the canvas' boundaries.
165 *
166 *  This function never fails.
167 *
168 *  \param cv A handle to the libcucul canvas.
169 *  \param x X coordinate.
170 *  \param y Y coordinate.
171 *  \param s The string to print.
172 *  \return This function always returns 0.
173 */
174int cucul_putstr(cucul_canvas_t *cv, int x, int y, char const *s)
175{
176    unsigned int read;
177
178    if(y < 0 || y >= (int)cv->height || x >= (int)cv->width)
179        return 0;
180
181    while(*s && x < -1)
182    {
183        x += cucul_utf32_is_fullwidth(cucul_utf8_to_utf32(s, &read)) ? 2 : 1;
184        s += read;
185    }
186
187    while(*s && x < (int)cv->width)
188    {
189        uint32_t ch = cucul_utf8_to_utf32(s, &read);
190        cucul_putchar(cv, x, y, ch);
191        x += cucul_utf32_is_fullwidth(ch) ? 2 : 1;
192        s += read;
193    }
194
195    return 0;
196}
197
198/** \brief Print a formated string.
199 *
200 *  Format a string at the given coordinates, using the default foreground
201 *  and background values. The coordinates may be outside the canvas
202 *  boundaries (eg. a negative Y coordinate) and the string will be cropped
203 *  accordingly if it is too long. The syntax of the format string is the
204 *  same as for the C printf() function.
205 *
206 *  This function never fails.
207 *
208 *  \param cv A handle to the libcucul canvas.
209 *  \param x X coordinate.
210 *  \param y Y coordinate.
211 *  \param format The format string to print.
212 *  \param ... Arguments to the format string.
213 *  \return This function always returns 0.
214 */
215int cucul_printf(cucul_canvas_t *cv, int x, int y, char const *format, ...)
216{
217    char tmp[BUFSIZ];
218    char *buf = tmp;
219    va_list args;
220
221    if(y < 0 || y >= (int)cv->height || x >= (int)cv->width)
222        return 0;
223
224    if(cv->width - x + 1 > BUFSIZ)
225        buf = malloc(cv->width - x + 1);
226
227    va_start(args, format);
228#if defined(HAVE_VSNPRINTF)
229    vsnprintf(buf, cv->width - x + 1, format, args);
230#else
231    vsprintf(buf, format, args);
232#endif
233    buf[cv->width - x] = '\0';
234    va_end(args);
235
236    cucul_putstr(cv, x, y, buf);
237
238    if(buf != tmp)
239        free(buf);
240
241    return 0;
242}
243
244/** \brief Clear the canvas.
245 *
246 *  Clear the canvas using the current foreground and background colours.
247 *
248 *  This function never fails.
249 *
250 *  \param cv The canvas to clear.
251 *  \return This function always returns 0.
252 */
253int cucul_clear_canvas(cucul_canvas_t *cv)
254{
255    uint32_t attr = cv->curattr;
256    unsigned int n;
257
258    for(n = cv->width * cv->height; n--; )
259    {
260        cv->chars[n] = (uint32_t)' ';
261        cv->attrs[n] = attr;
262    }
263
264    return 0;
265}
266
267/** \brief Blit a canvas onto another one.
268 *
269 *  Blit a canvas onto another one at the given coordinates.
270 *  An optional mask canvas can be used.
271 *
272 *  If an error occurs, -1 is returned and \b errno is set accordingly:
273 *  - \c EINVAL A mask was specified but the mask size and source canvas
274 *    size do not match.
275 *
276 *  \param dst The destination canvas.
277 *  \param x X coordinate.
278 *  \param y Y coordinate.
279 *  \param src The source canvas.
280 *  \param mask The mask canvas.
281 *  \return 0 in case of success, -1 if an error occurred.
282 */
283int cucul_blit(cucul_canvas_t *dst, int x, int y,
284               cucul_canvas_t const *src, cucul_canvas_t const *mask)
285{
286    int i, j, starti, startj, endi, endj;
287
288    if(mask && (src->width != mask->width || src->height != mask->height))
289    {
290#if defined(HAVE_ERRNO_H)
291        errno = EINVAL;
292#endif
293        return -1;
294    }
295
296    starti = x < 0 ? -x : 0;
297    startj = y < 0 ? -y : 0;
298    endi = (x + src->width >= dst->width) ? dst->width - x : src->width;
299    endj = (y + src->height >= dst->height) ? dst->height - y : src->height;
300
301    if((unsigned int)starti > src->width || (unsigned int)startj > src->height
302        || starti >= endi || startj >= endj)
303        return 0;
304
305    for(j = startj; j < endj; j++)
306    {
307        unsigned int dstix = (j + y) * dst->width + starti + x;
308        unsigned int srcix = j * src->width + starti;
309        int stride = endi - starti;
310
311        /* FIXME: we are ignoring the mask here */
312        if((starti + x) && dst->chars[dstix] == CUCUL_MAGIC_FULLWIDTH)
313            dst->chars[dstix - 1] = ' ';
314
315        if((unsigned int)(endi + x) < dst->width
316                && dst->chars[dstix + stride] == CUCUL_MAGIC_FULLWIDTH)
317            dst->chars[dstix + stride] = ' ';
318
319        if(mask)
320        {
321            for(i = 0; i < stride; i++)
322            {
323                if(mask->chars[srcix + i] == (uint32_t)' ')
324                    continue;
325
326                dst->chars[dstix + i] = src->chars[srcix + i];
327                dst->attrs[dstix + i] = src->attrs[srcix + i];
328            }
329        }
330        else
331        {
332            memcpy(dst->chars + dstix, src->chars + srcix, stride * 4);
333            memcpy(dst->attrs + dstix, src->attrs + srcix, stride * 4);
334        }
335
336        /* Fix split fullwidth chars */
337        if(src->chars[srcix] == CUCUL_MAGIC_FULLWIDTH)
338            dst->chars[dstix] = ' ';
339
340        if((unsigned int)endi < src->width
341                && src->chars[endi] == CUCUL_MAGIC_FULLWIDTH)
342            dst->chars[dstix + stride - 1] = ' ';
343    }
344
345    return 0;
346}
347
348/** \brief Set a canvas' new boundaries.
349 *
350 *  Set new boundaries for a canvas. This function can be used to crop a
351 *  canvas, to expand it or for combinations of both actions. All frames
352 *  are affected by this function.
353 *
354 *  If an error occurs, -1 is returned and \b errno is set accordingly:
355 *  - \c EBUSY The canvas is in use by a display driver and cannot be resized.
356 *  - \c ENOMEM Not enough memory for the requested canvas size. If this
357 *    happens, the canvas handle becomes invalid and should not be used.
358 *
359 *  \param cv The canvas to crop.
360 *  \param x X coordinate of the top-left corner.
361 *  \param y Y coordinate of the top-left corner.
362 *  \param w The width of the cropped area.
363 *  \param h The height of the cropped area.
364 *  \return 0 in case of success, -1 if an error occurred.
365 */
366int cucul_set_canvas_boundaries(cucul_canvas_t *cv, int x, int y,
367                                unsigned int w, unsigned int h)
368{
369    cucul_canvas_t *new;
370    unsigned int f, saved_f, framecount;
371
372    if(cv->refcount)
373    {
374#if defined(HAVE_ERRNO_H)
375        errno = EBUSY;
376#endif
377        return -1;
378    }
379
380    new = cucul_create_canvas(w, h);
381
382    framecount = cucul_get_canvas_frame_count(cv);
383    saved_f = cv->frame;
384
385    for(f = 0; f < framecount; f++)
386    {
387        if(f)
388            cucul_create_canvas_frame(new, framecount);
389
390        cucul_set_canvas_frame(cv, f);
391        cucul_set_canvas_frame(new, f);
392        cucul_blit(new, -x, -y, cv, NULL);
393
394        free(cv->frames[f].chars);
395        free(cv->frames[f].attrs);
396    }
397    free(cv->frames);
398
399    memcpy(cv, new, sizeof(cucul_canvas_t));
400    free(new);
401
402    cucul_set_canvas_frame(cv, saved_f);
403
404    return 0;
405}
406
Note: See TracBrowser for help on using the repository browser.