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.
 
 
 
 
 
 

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