25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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