You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

canvas.c 7.5 KiB

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