Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

246 рядки
6.7 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 given background colour.
  136. *
  137. * \param cv The canvas to clear.
  138. * \param bg The background colour to use.
  139. */
  140. void cucul_clear_canvas(cucul_canvas_t *cv, unsigned char bg)
  141. {
  142. uint16_t oldfg = cv->fgcolor;
  143. uint16_t oldbg = cv->bgcolor;
  144. int y = cv->height;
  145. cucul_set_color(cv, CUCUL_COLOR_DEFAULT, bg);
  146. /* We could use SLsmg_cls() etc., but drawing empty lines is much faster */
  147. while(y--)
  148. cucul_putstr(cv, 0, y, cv->empty_line);
  149. cv->fgcolor = oldfg;
  150. cv->bgcolor = oldbg;
  151. }
  152. /** \brief Blit a canvas onto another one.
  153. *
  154. * This function blits a canvas onto another one at the given coordinates.
  155. * An optional mask canvas can be used.
  156. *
  157. * \param dst The destination canvas.
  158. * \param x X coordinate.
  159. * \param y Y coordinate.
  160. * \param src The source canvas.
  161. * \param mask The mask canvas.
  162. */
  163. void cucul_blit(cucul_canvas_t *dst, int x, int y,
  164. cucul_canvas_t const *src, cucul_canvas_t const *mask)
  165. {
  166. int i, j, starti, startj, endi, endj;
  167. if(mask && (src->width != mask->width || src->height != mask->height))
  168. return;
  169. starti = x < 0 ? -x : 0;
  170. startj = y < 0 ? -y : 0;
  171. endi = (x + src->width >= dst->width) ? dst->width - x : src->width;
  172. endj = (y + src->height >= dst->height) ? dst->height - y : src->height;
  173. if(starti >= endi || startj >= endj)
  174. return;
  175. for(j = startj; j < endj; j++)
  176. {
  177. if(mask)
  178. {
  179. for(i = starti; i < endi; i++)
  180. {
  181. if(mask->chars[j * src->width + i] == (uint32_t)' ')
  182. continue;
  183. dst->chars[(j + y) * dst->width + (i + x)]
  184. = src->chars[j * src->width + i];
  185. dst->attr[(j + y) * dst->width + (i + x)]
  186. = src->attr[j * src->width + i];
  187. }
  188. }
  189. else
  190. {
  191. memcpy(dst->chars + (j + y) * dst->width + starti + x,
  192. src->chars + j * src->width + starti,
  193. (endi - starti) * 4);
  194. memcpy(dst->attr + (j + y) * dst->width + starti + x,
  195. src->attr + j * src->width + starti,
  196. (endi - starti) * 4);
  197. }
  198. }
  199. }
  200. /*
  201. * XXX: The following functions are not exported
  202. */
  203. void _cucul_putchar32(cucul_canvas_t *cv, int x, int y, uint32_t ch)
  204. {
  205. if(x < 0 || x >= (int)cv->width ||
  206. y < 0 || y >= (int)cv->height)
  207. return;
  208. cv->chars[x + y * cv->width] = ch;
  209. cv->attr[x + y * cv->width] = (cv->bgcolor << 16) | cv->fgcolor;
  210. }