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ů.
 
 
 
 
 
 

260 řádky
6.8 KiB

  1. /*
  2. * libcucul Unicode canvas library
  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 char.c
  12. * \version \$Id$
  13. * \author Sam Hocevar <sam@zoy.org>
  14. * \brief Character drawing
  15. *
  16. * This file contains character and string drawing functions.
  17. */
  18. #include "config.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 Set the default colour pair.
  37. *
  38. * This function sets the default colour pair. String functions such as
  39. * caca_printf() and graphical primitive functions such as caca_draw_line()
  40. * will use these colour pairs.
  41. *
  42. * \param fgcolor The requested foreground colour.
  43. * \param bgcolor The requested background colour.
  44. */
  45. void cucul_set_color(cucul_t *qq, enum cucul_color fgcolor, enum cucul_color bgcolor)
  46. {
  47. if(fgcolor < 0 || fgcolor > 15 || bgcolor < 0 || bgcolor > 15)
  48. return;
  49. qq->fgcolor = fgcolor;
  50. qq->bgcolor = bgcolor;
  51. }
  52. /** \brief Get the current foreground colour.
  53. *
  54. * This function returns the current foreground colour that was set with
  55. * cucul_set_color().
  56. *
  57. * \return The current foreground colour.
  58. */
  59. enum cucul_color cucul_get_fg_color(cucul_t *qq)
  60. {
  61. return qq->fgcolor;
  62. }
  63. /** \brief Get the current background colour.
  64. *
  65. * This function returns the current background colour that was set with
  66. * cucul_set_color().
  67. *
  68. * \return The current background colour.
  69. */
  70. enum cucul_color cucul_get_bg_color(cucul_t *qq)
  71. {
  72. return qq->bgcolor;
  73. }
  74. /** \brief Print an ASCII character.
  75. *
  76. * This function prints an ASCII character at the given coordinates, using
  77. * the default foreground and background values. If the coordinates are
  78. * outside the screen boundaries, nothing is printed. If the character
  79. * value is a non-printable character or is outside the ASCII range, it is
  80. * replaced with a space. To print a sequence of bytes forming an UTF-8
  81. * character, use cucul_putstr() instead.
  82. *
  83. * \param x X coordinate.
  84. * \param y Y coordinate.
  85. * \param c The character to print.
  86. */
  87. void cucul_putchar(cucul_t *qq, int x, int y, char c)
  88. {
  89. if(x < 0 || x >= (int)qq->width ||
  90. y < 0 || y >= (int)qq->height)
  91. return;
  92. if((unsigned char)c < 0x20 || (unsigned char)c > 0x7f)
  93. c = 0x20;
  94. qq->chars[x + y * qq->width] = c;
  95. qq->attr[x + y * qq->width] = (qq->bgcolor << 4) | qq->fgcolor;
  96. }
  97. /** \brief Print a Unicode character.
  98. *
  99. * FIXME: do we really want this function?
  100. *
  101. * This function prints a Unicode character (native-endian, 32 bits UCS-4,
  102. * also known as UTF-32) at the given coordinates, using the default
  103. * foreground and background values. If the coordinates are outside the
  104. * screen boundaries, nothing is printed. If the character is an invalid
  105. * Unicode character, it is replaced with a space.
  106. *
  107. * \param x X coordinate.
  108. * \param y Y coordinate.
  109. * \param c The character to print.
  110. */
  111. void cucul_putchar32(cucul_t *qq, int x, int y, unsigned long int c)
  112. {
  113. if(x < 0 || x >= (int)qq->width ||
  114. y < 0 || y >= (int)qq->height)
  115. return;
  116. if(c < 0x20 || c > 0x7f)
  117. c = 0x20;
  118. qq->chars[x + y * qq->width] = c;
  119. qq->attr[x + y * qq->width] = (qq->bgcolor << 4) | qq->fgcolor;
  120. }
  121. /** \brief Print a string.
  122. *
  123. * This function prints an UTF-8 string at the given coordinates, using the
  124. * default foreground and background values. The coordinates may be outside
  125. * the screen boundaries (eg. a negative Y coordinate) and the string will
  126. * be cropped accordingly if it is too long.
  127. *
  128. * \param x X coordinate.
  129. * \param y Y coordinate.
  130. * \param s The string to print.
  131. */
  132. void cucul_putstr(cucul_t *qq, int x, int y, char const *s)
  133. {
  134. uint32_t *chars;
  135. uint8_t *attr;
  136. unsigned int len;
  137. if(y < 0 || y >= (int)qq->height || x >= (int)qq->width)
  138. return;
  139. len = _cucul_strlen_utf8(s);
  140. if(x < 0)
  141. {
  142. if(len < (unsigned int)-x)
  143. return;
  144. len -= -x;
  145. s = _cucul_skip_utf8(s, -x);
  146. x = 0;
  147. }
  148. chars = qq->chars + x + y * qq->width;
  149. attr = qq->attr + x + y * qq->width;
  150. if(x + len >= qq->width)
  151. len = qq->width - x;
  152. while(len)
  153. {
  154. *chars++ = _cucul_utf8_to_utf32(s);
  155. *attr++ = (qq->bgcolor << 4) | qq->fgcolor;
  156. s = _cucul_skip_utf8(s, 1);
  157. len--;
  158. }
  159. }
  160. /** \brief Format a string.
  161. *
  162. * This function formats a string at the given coordinates, using the
  163. * default foreground and background values. The coordinates may be outside
  164. * the screen boundaries (eg. a negative Y coordinate) and the string will
  165. * be cropped accordingly if it is too long. The syntax of the format
  166. * string is the same as for the C printf() function.
  167. *
  168. * \param x X coordinate.
  169. * \param y Y coordinate.
  170. * \param format The format string to print.
  171. * \param ... Arguments to the format string.
  172. */
  173. void cucul_printf(cucul_t *qq, int x, int y, char const *format, ...)
  174. {
  175. char tmp[BUFSIZ];
  176. char *buf = tmp;
  177. va_list args;
  178. if(y < 0 || y >= (int)qq->height || x >= (int)qq->width)
  179. return;
  180. if(qq->width - x + 1 > BUFSIZ)
  181. buf = malloc(qq->width - x + 1);
  182. va_start(args, format);
  183. #if defined(HAVE_VSNPRINTF)
  184. vsnprintf(buf, qq->width - x + 1, format, args);
  185. #else
  186. vsprintf(buf, format, args);
  187. #endif
  188. buf[qq->width - x] = '\0';
  189. va_end(args);
  190. cucul_putstr(qq, x, y, buf);
  191. if(buf != tmp)
  192. free(buf);
  193. }
  194. /** \brief Get the screen.
  195. *
  196. * This function fills a byte array with the character values.
  197. */
  198. void cucul_get_screen(cucul_t *qq, char *buffer)
  199. {
  200. unsigned int x, y;
  201. for(y = 0; y < qq->height; y++)
  202. {
  203. for(x = 0; x < qq->width; x++)
  204. {
  205. *buffer++ = qq->attr[x + y * qq->width];
  206. *buffer++ = qq->chars[x + y * qq->width] & 0x7f; /* FIXME: ASCII */
  207. }
  208. }
  209. }
  210. /** \brief Clear the screen.
  211. *
  212. * This function clears the screen using a black background.
  213. */
  214. void cucul_clear(cucul_t *qq)
  215. {
  216. enum cucul_color oldfg = cucul_get_fg_color(qq);
  217. enum cucul_color oldbg = cucul_get_bg_color(qq);
  218. int y = qq->height;
  219. cucul_set_color(qq, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK);
  220. /* We could use SLsmg_cls() etc., but drawing empty lines is much faster */
  221. while(y--)
  222. cucul_putstr(qq, 0, y, qq->empty_line);
  223. cucul_set_color(qq, oldfg, oldbg);
  224. }