You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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