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

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