Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

387 rader
11 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_ERRNO_H)
  25. # include <errno.h>
  26. # endif
  27. # if defined(HAVE_UNISTD_H)
  28. # include <unistd.h>
  29. # endif
  30. # if defined(HAVE_SIGNAL_H)
  31. # include <signal.h>
  32. # endif
  33. # if defined(HAVE_SYS_IOCTL_H)
  34. # include <sys/ioctl.h>
  35. # endif
  36. #endif
  37. #include "cucul.h"
  38. #include "cucul_internals.h"
  39. /** \brief Print an ASCII or Unicode character.
  40. *
  41. * This function prints an ASCII or Unicode character at the given
  42. * coordinates, using the default foreground and background values.
  43. *
  44. * If the coordinates are outside the canvas boundaries, nothing is printed.
  45. * If a fullwidth Unicode character gets overwritten, its remaining parts
  46. * are replaced with spaces. If the canvas' boundaries would split the
  47. * fullwidth character in two, a space is printed instead.
  48. *
  49. * The behaviour when printing non-printable characters or invalid UTF-32
  50. * characters is undefined. To print a sequence of bytes forming an UTF-8
  51. * character instead of an UTF-32 character, use the cucul_putstr() function
  52. * instead.
  53. *
  54. * This function never fails.
  55. *
  56. * \param cv A handle to the libcucul canvas.
  57. * \param x X coordinate.
  58. * \param y Y coordinate.
  59. * \param ch The character to print.
  60. * \return This function always returns 0.
  61. */
  62. int cucul_putchar(cucul_canvas_t *cv, int x, int y, unsigned long int ch)
  63. {
  64. uint32_t *curchar, *curattr, attr;
  65. int fullwidth;
  66. if(!ch || x >= (int)cv->width || y < 0 || y >= (int)cv->height)
  67. return 0;
  68. fullwidth = cucul_utf32_is_fullwidth(ch);
  69. if(x == -1 && fullwidth)
  70. {
  71. x = 0;
  72. ch = ' ';
  73. fullwidth = 0;
  74. }
  75. else if(x < 0)
  76. return 0;
  77. curchar = cv->chars + x + y * cv->width;
  78. curattr = cv->attr + x + y * cv->width;
  79. attr = (cv->bgcolor << 16) | cv->fgcolor;
  80. /* When overwriting the right part of a fullwidth character,
  81. * replace its left part with a space. */
  82. if(x && curchar[0] == MAGIC_FULLWIDTH)
  83. curchar[-1] = ' ';
  84. if(fullwidth)
  85. {
  86. if(x + 1 == (int)cv->width)
  87. ch = ' ';
  88. else
  89. {
  90. /* When overwriting the left part of a fullwidth character,
  91. * replace its right part with a space. */
  92. if(x + 2 < (int)cv->width && curchar[2] == MAGIC_FULLWIDTH)
  93. curchar[2] = ' ';
  94. curchar[1] = MAGIC_FULLWIDTH;
  95. curattr[1] = attr;
  96. }
  97. }
  98. else
  99. {
  100. /* When overwriting the left part of a fullwidth character,
  101. * replace its right part with a space. */
  102. if(x + 1 != (int)cv->width && curchar[1] == MAGIC_FULLWIDTH)
  103. curchar[1] = ' ';
  104. }
  105. curchar[0] = ch;
  106. curattr[0] = attr;
  107. return 0;
  108. }
  109. /** \brief Get the Unicode character at the given coordinates.
  110. *
  111. * This function gets the ASCII or Unicode value of the character at
  112. * the given coordinates. If the value is less or equal to 127 (0x7f),
  113. * the character can be printed as ASCII. Otherise, it must be handled
  114. * as a UTF-32 value.
  115. *
  116. * If the coordinates are outside the canvas boundaries, a space (0x20)
  117. * is returned. FIXME: explain MAGIC_FULLWIDTH
  118. *
  119. * This function never fails.
  120. *
  121. * \param cv A handle to the libcucul canvas.
  122. * \param x X coordinate.
  123. * \param y Y coordinate.
  124. * \param ch The requested character value.
  125. * \return The character always returns 0.
  126. */
  127. unsigned long int cucul_getchar(cucul_canvas_t *cv, int x, int y)
  128. {
  129. if(x < 0 || x >= (int)cv->width || y < 0 || y >= (int)cv->height)
  130. return ' ';
  131. return (unsigned long int)cv->chars[x + y * cv->width];
  132. }
  133. /** \brief Print a string.
  134. *
  135. * This function prints an UTF-8 string at the given coordinates, using the
  136. * default foreground and background values. The coordinates may be outside
  137. * the canvas boundaries (eg. a negative Y coordinate) and the string will
  138. * be cropped accordingly if it is too long.
  139. *
  140. * See cucul_putchar() for more information on how fullwidth characters
  141. * are handled when overwriting each other or at the canvas' boundaries.
  142. *
  143. * This function never fails.
  144. *
  145. * \param cv A handle to the libcucul canvas.
  146. * \param x X coordinate.
  147. * \param y Y coordinate.
  148. * \param s The string to print.
  149. * \return This function always returns 0.
  150. */
  151. int cucul_putstr(cucul_canvas_t *cv, int x, int y, char const *s)
  152. {
  153. unsigned int read;
  154. if(y < 0 || y >= (int)cv->height || x >= (int)cv->width)
  155. return 0;
  156. while(*s && x < -1)
  157. {
  158. x += cucul_utf32_is_fullwidth(cucul_utf8_to_utf32(s, &read)) ? 2 : 1;
  159. s += read;
  160. }
  161. while(*s && x < (int)cv->width)
  162. {
  163. uint32_t ch = cucul_utf8_to_utf32(s, &read);
  164. cucul_putchar(cv, x, y, ch);
  165. x += cucul_utf32_is_fullwidth(ch) ? 2 : 1;
  166. s += read;
  167. }
  168. return 0;
  169. }
  170. /** \brief Print a formated string.
  171. *
  172. * This function formats a string at the given coordinates, using the
  173. * default foreground and background values. The coordinates may be outside
  174. * the canvas boundaries (eg. a negative Y coordinate) and the string will
  175. * be cropped accordingly if it is too long. The syntax of the format
  176. * string is the same as for the C printf() function.
  177. *
  178. * This function never fails.
  179. *
  180. * \param cv A handle to the libcucul canvas.
  181. * \param x X coordinate.
  182. * \param y Y coordinate.
  183. * \param format The format string to print.
  184. * \param ... Arguments to the format string.
  185. * \return This function always returns 0.
  186. */
  187. int cucul_printf(cucul_canvas_t *cv, int x, int y, char const *format, ...)
  188. {
  189. char tmp[BUFSIZ];
  190. char *buf = tmp;
  191. va_list args;
  192. if(y < 0 || y >= (int)cv->height || x >= (int)cv->width)
  193. return 0;
  194. if(cv->width - x + 1 > BUFSIZ)
  195. buf = malloc(cv->width - x + 1);
  196. va_start(args, format);
  197. #if defined(HAVE_VSNPRINTF)
  198. vsnprintf(buf, cv->width - x + 1, format, args);
  199. #else
  200. vsprintf(buf, format, args);
  201. #endif
  202. buf[cv->width - x] = '\0';
  203. va_end(args);
  204. cucul_putstr(cv, x, y, buf);
  205. if(buf != tmp)
  206. free(buf);
  207. return 0;
  208. }
  209. /** \brief Clear the canvas.
  210. *
  211. * This function clears the canvas using the current background colour.
  212. *
  213. * This function never fails.
  214. *
  215. * \param cv The canvas to clear.
  216. * \return This function always returns 0.
  217. */
  218. int cucul_clear_canvas(cucul_canvas_t *cv)
  219. {
  220. uint32_t color = (cv->bgcolor << 16) | cv->fgcolor;
  221. unsigned int n;
  222. for(n = cv->width * cv->height; n--; )
  223. {
  224. cv->chars[n] = (uint32_t)' ';
  225. cv->attr[n] = color;
  226. }
  227. return 0;
  228. }
  229. /** \brief Blit a canvas onto another one.
  230. *
  231. * This function blits a canvas onto another one at the given coordinates.
  232. * An optional mask canvas can be used.
  233. *
  234. * If an error occurs, -1 is returned and \b errno is set accordingly:
  235. * - \c EINVAL A mask was specified but the mask size and source canvas
  236. * size do not match.
  237. *
  238. * \param dst The destination canvas.
  239. * \param x X coordinate.
  240. * \param y Y coordinate.
  241. * \param src The source canvas.
  242. * \param mask The mask canvas.
  243. * \return 0 in case of success, -1 if an error occurred.
  244. */
  245. int cucul_blit(cucul_canvas_t *dst, int x, int y,
  246. cucul_canvas_t const *src, cucul_canvas_t const *mask)
  247. {
  248. int i, j, starti, startj, endi, endj;
  249. if(mask && (src->width != mask->width || src->height != mask->height))
  250. {
  251. #if defined(HAVE_ERRNO_H)
  252. errno = EINVAL;
  253. #endif
  254. return -1;
  255. }
  256. starti = x < 0 ? -x : 0;
  257. startj = y < 0 ? -y : 0;
  258. endi = (x + src->width >= dst->width) ? dst->width - x : src->width;
  259. endj = (y + src->height >= dst->height) ? dst->height - y : src->height;
  260. if((unsigned int)starti > src->width || (unsigned int)startj > src->height
  261. || starti >= endi || startj >= endj)
  262. return 0;
  263. for(j = startj; j < endj; j++)
  264. {
  265. if(mask)
  266. {
  267. for(i = starti; i < endi; i++)
  268. {
  269. if(mask->chars[j * src->width + i] == (uint32_t)' ')
  270. continue;
  271. dst->chars[(j + y) * dst->width + (i + x)]
  272. = src->chars[j * src->width + i];
  273. dst->attr[(j + y) * dst->width + (i + x)]
  274. = src->attr[j * src->width + i];
  275. }
  276. }
  277. else
  278. {
  279. memcpy(dst->chars + (j + y) * dst->width + starti + x,
  280. src->chars + j * src->width + starti,
  281. (endi - starti) * 4);
  282. memcpy(dst->attr + (j + y) * dst->width + starti + x,
  283. src->attr + j * src->width + starti,
  284. (endi - starti) * 4);
  285. }
  286. }
  287. return 0;
  288. }
  289. /** \brief Set a canvas' new boundaries.
  290. *
  291. * This function sets new boundaries for a canvas. It can be used to crop a
  292. * canvas, to expand it or for combinations of both actions.
  293. *
  294. * If an error occurs, -1 is returned and \b errno is set accordingly:
  295. * - \c EBUSY The canvas is in use by a display driver and cannot be resized.
  296. * - \c ENOMEM Not enough memory for the requested canvas size. If this
  297. * happens, the canvas handle becomes invalid and should not be used.
  298. *
  299. * \param cv The canvas to crop.
  300. * \param x X coordinate of the top-left corner.
  301. * \param y Y coordinate of the top-left corner.
  302. * \param w The width of the cropped area.
  303. * \param h The height of the cropped area.
  304. * \return 0 in case of success, -1 if an error occurred.
  305. */
  306. int cucul_set_canvas_boundaries(cucul_canvas_t *cv, int x, int y,
  307. unsigned int w, unsigned int h)
  308. {
  309. cucul_canvas_t *new;
  310. unsigned int f, saved_f, framecount;
  311. if(cv->refcount)
  312. {
  313. #if defined(HAVE_ERRNO_H)
  314. errno = EBUSY;
  315. #endif
  316. return -1;
  317. }
  318. new = cucul_create_canvas(w, h);
  319. framecount = cucul_get_canvas_frame_count(cv);
  320. saved_f = cv->frame;
  321. for(f = 0; f < framecount; f++)
  322. {
  323. if(f)
  324. cucul_create_canvas_frame(new, framecount);
  325. cucul_set_canvas_frame(cv, f);
  326. cucul_set_canvas_frame(new, f);
  327. cucul_blit(new, -x, -y, cv, NULL);
  328. free(cv->allchars[f]);
  329. free(cv->allattr[f]);
  330. }
  331. free(cv->allchars);
  332. free(cv->allattr);
  333. memcpy(cv, new, sizeof(cucul_canvas_t));
  334. free(new);
  335. cucul_set_canvas_frame(cv, saved_f);
  336. return 0;
  337. }