Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

261 rader
7.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, unsigned int fgcolor, unsigned int bgcolor)
  47. {
  48. /* FIXME */
  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 << 16) | 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, *attr;
  91. unsigned int len;
  92. if(y < 0 || y >= (int)qq->height || x >= (int)qq->width)
  93. return;
  94. len = _cucul_strlen_utf8(s);
  95. if(x < 0)
  96. {
  97. if(len < (unsigned int)-x)
  98. return;
  99. len -= -x;
  100. s = _cucul_skip_utf8(s, -x);
  101. x = 0;
  102. }
  103. chars = qq->chars + x + y * qq->width;
  104. attr = qq->attr + x + y * qq->width;
  105. if(x + len >= qq->width)
  106. len = qq->width - x;
  107. while(len)
  108. {
  109. *chars++ = _cucul_utf8_to_utf32(s);
  110. *attr++ = (qq->bgcolor << 16) | qq->fgcolor;
  111. s = _cucul_skip_utf8(s, 1);
  112. len--;
  113. }
  114. }
  115. /** \brief Format a string.
  116. *
  117. * This function formats a string at the given coordinates, using the
  118. * default foreground and background values. The coordinates may be outside
  119. * the screen boundaries (eg. a negative Y coordinate) and the string will
  120. * be cropped accordingly if it is too long. The syntax of the format
  121. * string is the same as for the C printf() function.
  122. *
  123. * \param x X coordinate.
  124. * \param y Y coordinate.
  125. * \param format The format string to print.
  126. * \param ... Arguments to the format string.
  127. */
  128. void cucul_printf(cucul_t *qq, int x, int y, char const *format, ...)
  129. {
  130. char tmp[BUFSIZ];
  131. char *buf = tmp;
  132. va_list args;
  133. if(y < 0 || y >= (int)qq->height || x >= (int)qq->width)
  134. return;
  135. if(qq->width - x + 1 > BUFSIZ)
  136. buf = malloc(qq->width - x + 1);
  137. va_start(args, format);
  138. #if defined(HAVE_VSNPRINTF)
  139. vsnprintf(buf, qq->width - x + 1, format, args);
  140. #else
  141. vsprintf(buf, format, args);
  142. #endif
  143. buf[qq->width - x] = '\0';
  144. va_end(args);
  145. cucul_putstr(qq, x, y, buf);
  146. if(buf != tmp)
  147. free(buf);
  148. }
  149. /** \brief Clear the screen.
  150. *
  151. * This function clears the screen using a black background.
  152. */
  153. void cucul_clear(cucul_t *qq)
  154. {
  155. uint16_t oldfg = qq->fgcolor;
  156. uint16_t oldbg = qq->bgcolor;
  157. int y = qq->height;
  158. cucul_set_color(qq, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK);
  159. /* We could use SLsmg_cls() etc., but drawing empty lines is much faster */
  160. while(y--)
  161. cucul_putstr(qq, 0, y, qq->empty_line);
  162. qq->fgcolor = oldfg;
  163. qq->bgcolor = oldbg;
  164. }
  165. /** \brief Blit a canvas onto another one.
  166. *
  167. * This function blits a canvas onto another one at the given coordinates.
  168. * An optional mask canvas can be used.
  169. *
  170. * \param dst The destination canvas.
  171. * \param x X coordinate.
  172. * \param y Y coordinate.
  173. * \param src The source canvas.
  174. * \param mask The mask canvas.
  175. */
  176. void cucul_blit(cucul_t *dst, int x, int y,
  177. cucul_t const *src, cucul_t const *mask)
  178. {
  179. int i, j, starti, startj, endi, endj;
  180. if(mask && (src->width != mask->width || src->height != mask->height))
  181. return;
  182. starti = x < 0 ? -x : 0;
  183. startj = y < 0 ? -y : 0;
  184. endi = (x + src->width >= dst->width) ? dst->width - x : src->width;
  185. endj = (y + src->height >= dst->height) ? dst->height - y : src->height;
  186. if(starti >= endi || startj >= endj)
  187. return;
  188. for(j = startj; j < endj; j++)
  189. {
  190. if(mask)
  191. {
  192. for(i = starti; i < endi; i++)
  193. {
  194. if(mask->chars[j * src->width + i] == (uint32_t)' ')
  195. continue;
  196. dst->chars[(j + y) * dst->width + (i + x)]
  197. = src->chars[j * src->width + i];
  198. dst->attr[(j + y) * dst->width + (i + x)]
  199. = src->attr[j * src->width + i];
  200. }
  201. }
  202. else
  203. {
  204. memcpy(dst->chars + (j + y) * dst->width + starti + x,
  205. src->chars + j * src->width + starti,
  206. (endi - starti) * 4);
  207. memcpy(dst->attr + (j + y) * dst->width + starti + x,
  208. src->attr + j * src->width + starti,
  209. (endi - starti) * 4);
  210. }
  211. }
  212. }
  213. /*
  214. * XXX: The following functions are not exported
  215. */
  216. void _cucul_putchar32(cucul_t *qq, int x, int y, uint32_t c)
  217. {
  218. if(x < 0 || x >= (int)qq->width ||
  219. y < 0 || y >= (int)qq->height)
  220. return;
  221. qq->chars[x + y * qq->width] = c;
  222. qq->attr[x + y * qq->width] = (qq->bgcolor << 16) | qq->fgcolor;
  223. }