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.
 
 
 
 
 
 

303 lines
8.1 KiB

  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 Get the current foreground colour.
  55. *
  56. * This function returns the current foreground colour that was set with
  57. * cucul_set_color().
  58. *
  59. * \return The current foreground colour.
  60. */
  61. enum cucul_color cucul_get_fg_color(cucul_t const *qq)
  62. {
  63. return qq->fgcolor;
  64. }
  65. /** \brief Get the current background colour.
  66. *
  67. * This function returns the current background colour that was set with
  68. * cucul_set_color().
  69. *
  70. * \return The current background colour.
  71. */
  72. enum cucul_color cucul_get_bg_color(cucul_t const *qq)
  73. {
  74. return qq->bgcolor;
  75. }
  76. /** \brief Print an ASCII character.
  77. *
  78. * This function prints an ASCII character at the given coordinates, using
  79. * the default foreground and background values. If the coordinates are
  80. * outside the screen boundaries, nothing is printed. If the character
  81. * value is a non-printable character or is outside the ASCII range, it is
  82. * replaced with a space. To print a sequence of bytes forming an UTF-8
  83. * character, use cucul_putstr() instead.
  84. *
  85. * \param x X coordinate.
  86. * \param y Y coordinate.
  87. * \param c The character to print.
  88. */
  89. void cucul_putchar(cucul_t *qq, int x, int y, char c)
  90. {
  91. if(x < 0 || x >= (int)qq->width ||
  92. y < 0 || y >= (int)qq->height)
  93. return;
  94. if((unsigned char)c < 0x20 || (unsigned char)c > 0x7f)
  95. c = 0x20;
  96. qq->chars[x + y * qq->width] = c;
  97. qq->attr[x + y * qq->width] = (qq->bgcolor << 4) | qq->fgcolor;
  98. }
  99. /** \brief Print a string.
  100. *
  101. * This function prints an UTF-8 string at the given coordinates, using the
  102. * default foreground and background values. The coordinates may be outside
  103. * the screen boundaries (eg. a negative Y coordinate) and the string will
  104. * be cropped accordingly if it is too long.
  105. *
  106. * \param x X coordinate.
  107. * \param y Y coordinate.
  108. * \param s The string to print.
  109. */
  110. void cucul_putstr(cucul_t *qq, int x, int y, char const *s)
  111. {
  112. uint32_t *chars;
  113. uint8_t *attr;
  114. unsigned int len;
  115. if(y < 0 || y >= (int)qq->height || x >= (int)qq->width)
  116. return;
  117. len = _cucul_strlen_utf8(s);
  118. if(x < 0)
  119. {
  120. if(len < (unsigned int)-x)
  121. return;
  122. len -= -x;
  123. s = _cucul_skip_utf8(s, -x);
  124. x = 0;
  125. }
  126. chars = qq->chars + x + y * qq->width;
  127. attr = qq->attr + x + y * qq->width;
  128. if(x + len >= qq->width)
  129. len = qq->width - x;
  130. while(len)
  131. {
  132. *chars++ = _cucul_utf8_to_utf32(s);
  133. *attr++ = (qq->bgcolor << 4) | qq->fgcolor;
  134. s = _cucul_skip_utf8(s, 1);
  135. len--;
  136. }
  137. }
  138. /** \brief Format a string.
  139. *
  140. * This function formats a string at the given coordinates, using the
  141. * default foreground and background values. The coordinates may be outside
  142. * the screen boundaries (eg. a negative Y coordinate) and the string will
  143. * be cropped accordingly if it is too long. The syntax of the format
  144. * string is the same as for the C printf() function.
  145. *
  146. * \param x X coordinate.
  147. * \param y Y coordinate.
  148. * \param format The format string to print.
  149. * \param ... Arguments to the format string.
  150. */
  151. void cucul_printf(cucul_t *qq, int x, int y, char const *format, ...)
  152. {
  153. char tmp[BUFSIZ];
  154. char *buf = tmp;
  155. va_list args;
  156. if(y < 0 || y >= (int)qq->height || x >= (int)qq->width)
  157. return;
  158. if(qq->width - x + 1 > BUFSIZ)
  159. buf = malloc(qq->width - x + 1);
  160. va_start(args, format);
  161. #if defined(HAVE_VSNPRINTF)
  162. vsnprintf(buf, qq->width - x + 1, format, args);
  163. #else
  164. vsprintf(buf, format, args);
  165. #endif
  166. buf[qq->width - x] = '\0';
  167. va_end(args);
  168. cucul_putstr(qq, x, y, buf);
  169. if(buf != tmp)
  170. free(buf);
  171. }
  172. /** \brief Get the screen.
  173. *
  174. * This function fills a byte array with the character values.
  175. */
  176. void cucul_get_screen(cucul_t const *qq, char *buffer)
  177. {
  178. unsigned int x, y;
  179. for(y = 0; y < qq->height; y++)
  180. {
  181. for(x = 0; x < qq->width; x++)
  182. {
  183. *buffer++ = qq->attr[x + y * qq->width];
  184. *buffer++ = qq->chars[x + y * qq->width] & 0x7f; /* FIXME: ASCII */
  185. }
  186. }
  187. }
  188. /** \brief Clear the screen.
  189. *
  190. * This function clears the screen using a black background.
  191. */
  192. void cucul_clear(cucul_t *qq)
  193. {
  194. enum cucul_color oldfg = cucul_get_fg_color(qq);
  195. enum cucul_color oldbg = cucul_get_bg_color(qq);
  196. int y = qq->height;
  197. cucul_set_color(qq, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK);
  198. /* We could use SLsmg_cls() etc., but drawing empty lines is much faster */
  199. while(y--)
  200. cucul_putstr(qq, 0, y, qq->empty_line);
  201. cucul_set_color(qq, oldfg, oldbg);
  202. }
  203. /** \brief Blit a canvas onto another one.
  204. *
  205. * This function blits a canvas onto another one at the given coordinates.
  206. * An optional mask canvas can be used.
  207. *
  208. * \param dst The destination canvas.
  209. * \param x X coordinate.
  210. * \param y Y coordinate.
  211. * \param src The source canvas.
  212. * \param mask The mask canvas.
  213. */
  214. void cucul_blit(cucul_t *dst, int x, int y,
  215. cucul_t const *src, cucul_t const *mask)
  216. {
  217. int i, j, starti, startj, endi, endj;
  218. if(mask && (src->width != mask->width || src->height != mask->height))
  219. return;
  220. starti = x < 0 ? -x : 0;
  221. startj = y < 0 ? -y : 0;
  222. endi = (x + src->width >= dst->width) ? dst->width - x : src->width;
  223. endj = (y + src->height >= dst->height) ? dst->height - y : src->height;
  224. if(starti >= endi || startj >= endj)
  225. return;
  226. for(j = startj; j < endj; j++)
  227. {
  228. if(mask)
  229. {
  230. for(i = starti; i < endi; i++)
  231. {
  232. if(mask->chars[j * src->width + i] == (uint32_t)' ')
  233. continue;
  234. dst->chars[(j + y) * dst->width + (i + x)]
  235. = src->chars[j * src->width + i];
  236. dst->attr[(j + y) * dst->width + (i + x)]
  237. = src->attr[j * src->width + i];
  238. }
  239. }
  240. else
  241. {
  242. memcpy(dst->chars + (j + y) * dst->width + starti + x,
  243. src->chars + j * src->width + starti,
  244. (endi - starti) * 4);
  245. memcpy(dst->attr + (j + y) * dst->width + starti + x,
  246. src->attr + j * src->width + starti,
  247. (endi - starti) * 1);
  248. }
  249. }
  250. }
  251. /*
  252. * XXX: The following functions are not exported
  253. */
  254. void _cucul_putchar32(cucul_t *qq, int x, int y, uint32_t c)
  255. {
  256. if(x < 0 || x >= (int)qq->width ||
  257. y < 0 || y >= (int)qq->height)
  258. return;
  259. qq->chars[x + y * qq->width] = c;
  260. qq->attr[x + y * qq->width] = (qq->bgcolor << 4) | qq->fgcolor;
  261. }