|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315 |
- /*
- * libcucul Canvas for ultrafast compositing of Unicode letters
- * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
- * All Rights Reserved
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the Do What The Fuck You Want To
- * Public License, Version 2, as published by Sam Hocevar. See
- * http://sam.zoy.org/wtfpl/COPYING for more details.
- */
-
- /** \file canvas.c
- * \version \$Id$
- * \author Sam Hocevar <sam@zoy.org>
- * \brief Canvas drawing
- *
- * This file contains various canvas handling functions such as character
- * and string drawing.
- */
-
- #include "config.h"
-
- #if !defined(__KERNEL__)
- # include <stdio.h> /* BUFSIZ */
- # include <string.h>
- # include <stdlib.h>
- # include <stdarg.h>
- # if defined(HAVE_UNISTD_H)
- # include <unistd.h>
- # endif
- # if defined(HAVE_SIGNAL_H)
- # include <signal.h>
- # endif
- # if defined(HAVE_SYS_IOCTL_H)
- # include <sys/ioctl.h>
- # endif
- #endif
-
- #include "cucul.h"
- #include "cucul_internals.h"
-
- /** \brief Set the default colour pair.
- *
- * This function sets the default colour pair. String functions such as
- * caca_printf() and graphical primitive functions such as caca_draw_line()
- * will use these colour pairs.
- *
- * \param fgcolor The requested foreground colour.
- * \param bgcolor The requested background colour.
- */
- void cucul_set_color(cucul_t *qq, enum cucul_color fgcolor,
- enum cucul_color bgcolor)
- {
- if(fgcolor < 0 || fgcolor > 15 || bgcolor < 0 || bgcolor > 15)
- return;
-
- qq->fgcolor = fgcolor;
- qq->bgcolor = bgcolor;
- }
-
- /** \brief Get the current foreground colour.
- *
- * This function returns the current foreground colour that was set with
- * cucul_set_color().
- *
- * \return The current foreground colour.
- */
- enum cucul_color cucul_get_fg_color(cucul_t const *qq)
- {
- return qq->fgcolor;
- }
-
- /** \brief Get the current background colour.
- *
- * This function returns the current background colour that was set with
- * cucul_set_color().
- *
- * \return The current background colour.
- */
- enum cucul_color cucul_get_bg_color(cucul_t const *qq)
- {
- return qq->bgcolor;
- }
-
- /** \brief Print an ASCII character.
- *
- * This function prints an ASCII character at the given coordinates, using
- * the default foreground and background values. If the coordinates are
- * outside the screen boundaries, nothing is printed. If the character
- * value is a non-printable character or is outside the ASCII range, it is
- * replaced with a space. To print a sequence of bytes forming an UTF-8
- * character, use cucul_putstr() instead.
- *
- * \param x X coordinate.
- * \param y Y coordinate.
- * \param c The character to print.
- */
- void cucul_putchar(cucul_t *qq, int x, int y, char c)
- {
- if(x < 0 || x >= (int)qq->width ||
- y < 0 || y >= (int)qq->height)
- return;
-
- if((unsigned char)c < 0x20 || (unsigned char)c > 0x7f)
- c = 0x20;
-
- qq->chars[x + y * qq->width] = c;
- qq->attr[x + y * qq->width] = (qq->bgcolor << 4) | qq->fgcolor;
- }
-
- /** \brief Print a Unicode character.
- *
- * FIXME: do we really want this function?
- *
- * This function prints a Unicode character (native-endian, 32 bits UCS-4,
- * also known as UTF-32) at the given coordinates, using the default
- * foreground and background values. If the coordinates are outside the
- * screen boundaries, nothing is printed. If the character is an invalid
- * Unicode character, it is replaced with a space.
- *
- * \param x X coordinate.
- * \param y Y coordinate.
- * \param c The character to print.
- */
- void cucul_putchar32(cucul_t *qq, int x, int y, unsigned long int c)
- {
- if(x < 0 || x >= (int)qq->width ||
- y < 0 || y >= (int)qq->height)
- return;
-
- if(c < 0x20 || c > 0x7f)
- c = 0x20;
-
- qq->chars[x + y * qq->width] = c;
- qq->attr[x + y * qq->width] = (qq->bgcolor << 4) | qq->fgcolor;
- }
-
- /** \brief Print a string.
- *
- * This function prints an UTF-8 string at the given coordinates, using the
- * default foreground and background values. The coordinates may be outside
- * the screen boundaries (eg. a negative Y coordinate) and the string will
- * be cropped accordingly if it is too long.
- *
- * \param x X coordinate.
- * \param y Y coordinate.
- * \param s The string to print.
- */
- void cucul_putstr(cucul_t *qq, int x, int y, char const *s)
- {
- uint32_t *chars;
- uint8_t *attr;
- unsigned int len;
-
- if(y < 0 || y >= (int)qq->height || x >= (int)qq->width)
- return;
-
- len = _cucul_strlen_utf8(s);
-
- if(x < 0)
- {
- if(len < (unsigned int)-x)
- return;
- len -= -x;
- s = _cucul_skip_utf8(s, -x);
- x = 0;
- }
-
- chars = qq->chars + x + y * qq->width;
- attr = qq->attr + x + y * qq->width;
-
- if(x + len >= qq->width)
- len = qq->width - x;
-
- while(len)
- {
- *chars++ = _cucul_utf8_to_utf32(s);
- *attr++ = (qq->bgcolor << 4) | qq->fgcolor;
-
- s = _cucul_skip_utf8(s, 1);
- len--;
- }
- }
-
- /** \brief Format a string.
- *
- * This function formats a string at the given coordinates, using the
- * default foreground and background values. The coordinates may be outside
- * the screen boundaries (eg. a negative Y coordinate) and the string will
- * be cropped accordingly if it is too long. The syntax of the format
- * string is the same as for the C printf() function.
- *
- * \param x X coordinate.
- * \param y Y coordinate.
- * \param format The format string to print.
- * \param ... Arguments to the format string.
- */
- void cucul_printf(cucul_t *qq, int x, int y, char const *format, ...)
- {
- char tmp[BUFSIZ];
- char *buf = tmp;
- va_list args;
-
- if(y < 0 || y >= (int)qq->height || x >= (int)qq->width)
- return;
-
- if(qq->width - x + 1 > BUFSIZ)
- buf = malloc(qq->width - x + 1);
-
- va_start(args, format);
- #if defined(HAVE_VSNPRINTF)
- vsnprintf(buf, qq->width - x + 1, format, args);
- #else
- vsprintf(buf, format, args);
- #endif
- buf[qq->width - x] = '\0';
- va_end(args);
-
- cucul_putstr(qq, x, y, buf);
-
- if(buf != tmp)
- free(buf);
- }
-
- /** \brief Get the screen.
- *
- * This function fills a byte array with the character values.
- */
- void cucul_get_screen(cucul_t const *qq, char *buffer)
- {
- unsigned int x, y;
-
- for(y = 0; y < qq->height; y++)
- {
- for(x = 0; x < qq->width; x++)
- {
- *buffer++ = qq->attr[x + y * qq->width];
- *buffer++ = qq->chars[x + y * qq->width] & 0x7f; /* FIXME: ASCII */
- }
- }
- }
-
- /** \brief Clear the screen.
- *
- * This function clears the screen using a black background.
- */
- void cucul_clear(cucul_t *qq)
- {
- enum cucul_color oldfg = cucul_get_fg_color(qq);
- enum cucul_color oldbg = cucul_get_bg_color(qq);
- int y = qq->height;
-
- cucul_set_color(qq, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK);
-
- /* We could use SLsmg_cls() etc., but drawing empty lines is much faster */
- while(y--)
- cucul_putstr(qq, 0, y, qq->empty_line);
-
- cucul_set_color(qq, oldfg, oldbg);
- }
-
- /** \brief Blit a canvas onto another one.
- *
- * This function blits a canvas onto another one at the given coordinates.
- * An optional mask canvas can be used.
- *
- * \param dst The destination canvas.
- * \param x X coordinate.
- * \param y Y coordinate.
- * \param src The source canvas.
- * \param mask The mask canvas.
- */
- void cucul_blit(cucul_t *dst, int x, int y,
- cucul_t const *src, cucul_t const *mask)
- {
- int i, j, starti, startj, endi, endj;
-
- if(mask && (src->width != mask->width || src->height != mask->height))
- return;
-
- starti = x < 0 ? -x : 0;
- startj = y < 0 ? -y : 0;
- endi = (x + src->width >= dst->width) ? dst->width - x : src->width;
- endj = (y + src->height >= dst->height) ? dst->height - y : src->height;
-
- if(starti >= endi || startj >= endj)
- return;
-
- for(j = startj; j < endj; j++)
- {
- if(mask)
- {
- for(i = starti; i < endi; i++)
- {
- if(mask->chars[j * src->width + i] == (uint32_t)' ')
- continue;
-
- dst->chars[(j + y) * dst->width + (i + x)]
- = src->chars[j * src->width + i];
- dst->attr[(j + y) * dst->width + (i + x)]
- = src->attr[j * src->width + i];
- }
- }
- else
- {
- memcpy(dst->chars + (j + y) * dst->width + starti + x,
- src->chars + j * src->width + starti,
- (endi - starti) * 4);
- memcpy(dst->attr + (j + y) * dst->width + starti + x,
- src->attr + j * src->width + starti,
- (endi - starti) * 1);
- }
- }
- }
-
|