Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

před 19 roky
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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, enum cucul_color fgcolor,
  47. enum cucul_color bgcolor)
  48. {
  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. enum cucul_color oldfg = qq->fgcolor;
  156. enum cucul_color 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. cucul_set_color(qq, oldfg, oldbg);
  163. }
  164. /** \brief Blit a canvas onto another one.
  165. *
  166. * This function blits a canvas onto another one at the given coordinates.
  167. * An optional mask canvas can be used.
  168. *
  169. * \param dst The destination canvas.
  170. * \param x X coordinate.
  171. * \param y Y coordinate.
  172. * \param src The source canvas.
  173. * \param mask The mask canvas.
  174. */
  175. void cucul_blit(cucul_t *dst, int x, int y,
  176. cucul_t const *src, cucul_t const *mask)
  177. {
  178. int i, j, starti, startj, endi, endj;
  179. if(mask && (src->width != mask->width || src->height != mask->height))
  180. return;
  181. starti = x < 0 ? -x : 0;
  182. startj = y < 0 ? -y : 0;
  183. endi = (x + src->width >= dst->width) ? dst->width - x : src->width;
  184. endj = (y + src->height >= dst->height) ? dst->height - y : src->height;
  185. if(starti >= endi || startj >= endj)
  186. return;
  187. for(j = startj; j < endj; j++)
  188. {
  189. if(mask)
  190. {
  191. for(i = starti; i < endi; i++)
  192. {
  193. if(mask->chars[j * src->width + i] == (uint32_t)' ')
  194. continue;
  195. dst->chars[(j + y) * dst->width + (i + x)]
  196. = src->chars[j * src->width + i];
  197. dst->attr[(j + y) * dst->width + (i + x)]
  198. = src->attr[j * src->width + i];
  199. }
  200. }
  201. else
  202. {
  203. memcpy(dst->chars + (j + y) * dst->width + starti + x,
  204. src->chars + j * src->width + starti,
  205. (endi - starti) * 4);
  206. memcpy(dst->attr + (j + y) * dst->width + starti + x,
  207. src->attr + j * src->width + starti,
  208. (endi - starti) * 4);
  209. }
  210. }
  211. }
  212. /*
  213. * XXX: The following functions are not exported
  214. */
  215. void _cucul_putchar32(cucul_t *qq, int x, int y, uint32_t c)
  216. {
  217. if(x < 0 || x >= (int)qq->width ||
  218. y < 0 || y >= (int)qq->height)
  219. return;
  220. qq->chars[x + y * qq->width] = c;
  221. qq->attr[x + y * qq->width] = (qq->bgcolor << 16) | qq->fgcolor;
  222. }