25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

242 lines
6.6 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 Print an ASCII character.
  36. *
  37. * This function prints an ASCII character at the given coordinates, using
  38. * the default foreground and background values. If the coordinates are
  39. * outside the canvas boundaries, nothing is printed. If the character
  40. * value is a non-printable character or is outside the ASCII range, it is
  41. * replaced with a space. To print a sequence of bytes forming an UTF-8
  42. * character, use cucul_putstr() instead.
  43. *
  44. * \param cv A handle to the libcucul canvas.
  45. * \param x X coordinate.
  46. * \param y Y coordinate.
  47. * \param ch The character to print.
  48. */
  49. void cucul_putchar(cucul_canvas_t *cv, int x, int y, char ch)
  50. {
  51. if(x < 0 || x >= (int)cv->width ||
  52. y < 0 || y >= (int)cv->height)
  53. return;
  54. if((unsigned char)ch < 0x20 || (unsigned char)ch > 0x7f)
  55. ch = 0x20;
  56. cv->chars[x + y * cv->width] = ch;
  57. cv->attr[x + y * cv->width] = (cv->bgcolor << 16) | cv->fgcolor;
  58. }
  59. /** \brief Print a string.
  60. *
  61. * This function prints an UTF-8 string at the given coordinates, using the
  62. * default foreground and background values. The coordinates may be outside
  63. * the canvas boundaries (eg. a negative Y coordinate) and the string will
  64. * be cropped accordingly if it is too long.
  65. *
  66. * \param cv A handle to the libcucul canvas.
  67. * \param x X coordinate.
  68. * \param y Y coordinate.
  69. * \param s The string to print.
  70. */
  71. void cucul_putstr(cucul_canvas_t *cv, int x, int y, char const *s)
  72. {
  73. uint32_t *chars, *attr;
  74. unsigned int len;
  75. if(y < 0 || y >= (int)cv->height || x >= (int)cv->width)
  76. return;
  77. len = _cucul_strlen_utf8(s);
  78. if(x < 0)
  79. {
  80. if(len < (unsigned int)-x)
  81. return;
  82. len -= -x;
  83. s = _cucul_skip_utf8(s, -x);
  84. x = 0;
  85. }
  86. chars = cv->chars + x + y * cv->width;
  87. attr = cv->attr + x + y * cv->width;
  88. if(x + len >= cv->width)
  89. len = cv->width - x;
  90. while(len)
  91. {
  92. *chars++ = _cucul_utf8_to_utf32(s);
  93. *attr++ = (cv->bgcolor << 16) | cv->fgcolor;
  94. s = _cucul_skip_utf8(s, 1);
  95. len--;
  96. }
  97. }
  98. /** \brief Print a formated string.
  99. *
  100. * This function formats a string at the given coordinates, using the
  101. * default foreground and background values. The coordinates may be outside
  102. * the canvas boundaries (eg. a negative Y coordinate) and the string will
  103. * be cropped accordingly if it is too long. The syntax of the format
  104. * string is the same as for the C printf() function.
  105. *
  106. * \param cv A handle to the libcucul canvas.
  107. * \param x X coordinate.
  108. * \param y Y coordinate.
  109. * \param format The format string to print.
  110. * \param ... Arguments to the format string.
  111. */
  112. void cucul_printf(cucul_canvas_t *cv, int x, int y, char const *format, ...)
  113. {
  114. char tmp[BUFSIZ];
  115. char *buf = tmp;
  116. va_list args;
  117. if(y < 0 || y >= (int)cv->height || x >= (int)cv->width)
  118. return;
  119. if(cv->width - x + 1 > BUFSIZ)
  120. buf = malloc(cv->width - x + 1);
  121. va_start(args, format);
  122. #if defined(HAVE_VSNPRINTF)
  123. vsnprintf(buf, cv->width - x + 1, format, args);
  124. #else
  125. vsprintf(buf, format, args);
  126. #endif
  127. buf[cv->width - x] = '\0';
  128. va_end(args);
  129. cucul_putstr(cv, x, y, buf);
  130. if(buf != tmp)
  131. free(buf);
  132. }
  133. /** \brief Clear the canvas.
  134. *
  135. * This function clears the canvas using the current background colour.
  136. *
  137. * \param cv The canvas to clear.
  138. */
  139. void cucul_clear_canvas(cucul_canvas_t *cv)
  140. {
  141. uint32_t color = (cv->bgcolor << 16) | cv->fgcolor;
  142. unsigned int n;
  143. /* We could use SLsmg_cls() etc., but drawing empty lines is much faster */
  144. for(n = cv->width * cv->height; n--; )
  145. {
  146. cv->chars[n] = (uint32_t)' ';
  147. cv->attr[n] = color;
  148. }
  149. }
  150. /** \brief Blit a canvas onto another one.
  151. *
  152. * This function blits a canvas onto another one at the given coordinates.
  153. * An optional mask canvas can be used.
  154. *
  155. * \param dst The destination canvas.
  156. * \param x X coordinate.
  157. * \param y Y coordinate.
  158. * \param src The source canvas.
  159. * \param mask The mask canvas.
  160. */
  161. void cucul_blit(cucul_canvas_t *dst, int x, int y,
  162. cucul_canvas_t const *src, cucul_canvas_t const *mask)
  163. {
  164. int i, j, starti, startj, endi, endj;
  165. if(mask && (src->width != mask->width || src->height != mask->height))
  166. return;
  167. starti = x < 0 ? -x : 0;
  168. startj = y < 0 ? -y : 0;
  169. endi = (x + src->width >= dst->width) ? dst->width - x : src->width;
  170. endj = (y + src->height >= dst->height) ? dst->height - y : src->height;
  171. if(starti >= endi || startj >= endj)
  172. return;
  173. for(j = startj; j < endj; j++)
  174. {
  175. if(mask)
  176. {
  177. for(i = starti; i < endi; i++)
  178. {
  179. if(mask->chars[j * src->width + i] == (uint32_t)' ')
  180. continue;
  181. dst->chars[(j + y) * dst->width + (i + x)]
  182. = src->chars[j * src->width + i];
  183. dst->attr[(j + y) * dst->width + (i + x)]
  184. = src->attr[j * src->width + i];
  185. }
  186. }
  187. else
  188. {
  189. memcpy(dst->chars + (j + y) * dst->width + starti + x,
  190. src->chars + j * src->width + starti,
  191. (endi - starti) * 4);
  192. memcpy(dst->attr + (j + y) * dst->width + starti + x,
  193. src->attr + j * src->width + starti,
  194. (endi - starti) * 4);
  195. }
  196. }
  197. }
  198. /*
  199. * XXX: The following functions are not exported
  200. */
  201. void _cucul_putchar32(cucul_canvas_t *cv, int x, int y, uint32_t ch)
  202. {
  203. if(x < 0 || x >= (int)cv->width ||
  204. y < 0 || y >= (int)cv->height)
  205. return;
  206. cv->chars[x + y * cv->width] = ch;
  207. cv->attr[x + y * cv->width] = (cv->bgcolor << 16) | cv->fgcolor;
  208. }