Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

500 Zeilen
14 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 Set cursor position.
  37. *
  38. * Put the cursor at the given coordinates. Functions making use of the
  39. * cursor will use the new values. Setting the cursor position outside the
  40. * canvas is legal but the cursor will not be shown.
  41. *
  42. * This function never fails.
  43. *
  44. * \param cv A handle to the libcucul canvas.
  45. * \param x X cursor coordinate.
  46. * \param y Y cursor coordinate.
  47. * \return This function always returns 0.
  48. */
  49. int cucul_gotoxy(cucul_canvas_t *cv, int x, int y)
  50. {
  51. cv->frames[cv->frame].x = x;
  52. cv->frames[cv->frame].y = y;
  53. return 0;
  54. }
  55. /** \brief Get X cursor position.
  56. *
  57. * Retrieve the X coordinate of the cursor's position.
  58. *
  59. * This function never fails.
  60. *
  61. * \param cv A handle to the libcucul canvas.
  62. * \return The cursor's X coordinate.
  63. */
  64. int cucul_get_cursor_x(cucul_canvas_t *cv)
  65. {
  66. return cv->frames[cv->frame].x;
  67. }
  68. /** \brief Get Y cursor position.
  69. *
  70. * Retrieve the Y coordinate of the cursor's position.
  71. *
  72. * This function never fails.
  73. *
  74. * \param cv A handle to the libcucul canvas.
  75. * \return The cursor's Y coordinate.
  76. */
  77. int cucul_get_cursor_y(cucul_canvas_t *cv)
  78. {
  79. return cv->frames[cv->frame].y;
  80. }
  81. /** \brief Print an ASCII or Unicode character.
  82. *
  83. * Print an ASCII or Unicode character at the given coordinates, using
  84. * the default foreground and background colour values.
  85. *
  86. * If the coordinates are outside the canvas boundaries, nothing is printed.
  87. * If a fullwidth Unicode character gets overwritten, its remaining visible
  88. * parts are replaced with spaces. If the canvas' boundaries would split the
  89. * fullwidth character in two, a space is printed instead.
  90. *
  91. * The behaviour when printing non-printable characters or invalid UTF-32
  92. * characters is undefined. To print a sequence of bytes forming an UTF-8
  93. * character instead of an UTF-32 character, use the cucul_put_str() function.
  94. *
  95. * This function never fails.
  96. *
  97. * \param cv A handle to the libcucul canvas.
  98. * \param x X coordinate.
  99. * \param y Y coordinate.
  100. * \param ch The character to print.
  101. * \return This function always returns 0.
  102. */
  103. int cucul_put_char(cucul_canvas_t *cv, int x, int y, unsigned long int ch)
  104. {
  105. uint32_t *curchar, *curattr, attr;
  106. int fullwidth;
  107. if(x >= (int)cv->width || y < 0 || y >= (int)cv->height)
  108. return 0;
  109. if(ch == CUCUL_MAGIC_FULLWIDTH)
  110. return 0;
  111. fullwidth = cucul_utf32_is_fullwidth(ch);
  112. if(x == -1 && fullwidth)
  113. {
  114. x = 0;
  115. ch = ' ';
  116. fullwidth = 0;
  117. }
  118. else if(x < 0)
  119. return 0;
  120. curchar = cv->chars + x + y * cv->width;
  121. curattr = cv->attrs + x + y * cv->width;
  122. attr = cv->curattr;
  123. /* When overwriting the right part of a fullwidth character,
  124. * replace its left part with a space. */
  125. if(x && curchar[0] == CUCUL_MAGIC_FULLWIDTH)
  126. curchar[-1] = ' ';
  127. if(fullwidth)
  128. {
  129. if(x + 1 == (int)cv->width)
  130. ch = ' ';
  131. else
  132. {
  133. /* When overwriting the left part of a fullwidth character,
  134. * replace its right part with a space. */
  135. if(x + 2 < (int)cv->width && curchar[2] == CUCUL_MAGIC_FULLWIDTH)
  136. curchar[2] = ' ';
  137. curchar[1] = CUCUL_MAGIC_FULLWIDTH;
  138. curattr[1] = attr;
  139. }
  140. }
  141. else
  142. {
  143. /* When overwriting the left part of a fullwidth character,
  144. * replace its right part with a space. */
  145. if(x + 1 != (int)cv->width && curchar[1] == CUCUL_MAGIC_FULLWIDTH)
  146. curchar[1] = ' ';
  147. }
  148. curchar[0] = ch;
  149. curattr[0] = attr;
  150. return 0;
  151. }
  152. /** \brief Get the Unicode character at the given coordinates.
  153. *
  154. * Get the ASCII or Unicode value of the character at the given
  155. * coordinates. If the value is less or equal to 127 (0x7f),
  156. * the character can be printed as ASCII. Otherise, it must be handled
  157. * as a UTF-32 value.
  158. *
  159. * If the coordinates are outside the canvas boundaries, a space (0x20)
  160. * is returned.
  161. *
  162. * A special exception is when CUCUL_MAGIC_FULLWIDTH is returned. This
  163. * value is guaranteed not to be a valid Unicode character, and indicates
  164. * that the character at the left of the requested one is a fullwidth
  165. * character.
  166. *
  167. * This function never fails.
  168. *
  169. * \param cv A handle to the libcucul canvas.
  170. * \param x X coordinate.
  171. * \param y Y coordinate.
  172. * \return This function always returns 0.
  173. */
  174. unsigned long int cucul_get_char(cucul_canvas_t *cv, int x, int y)
  175. {
  176. if(x < 0 || x >= (int)cv->width || y < 0 || y >= (int)cv->height)
  177. return ' ';
  178. return (unsigned long int)cv->chars[x + y * cv->width];
  179. }
  180. /** \brief Print a string.
  181. *
  182. * Print an UTF-8 string at the given coordinates, using the default
  183. * foreground and background values. The coordinates may be outside the
  184. * canvas boundaries (eg. a negative Y coordinate) and the string will
  185. * be cropped accordingly if it is too long.
  186. *
  187. * See cucul_put_char() for more information on how fullwidth characters
  188. * are handled when overwriting each other or at the canvas' boundaries.
  189. *
  190. * This function never fails.
  191. *
  192. * \param cv A handle to the libcucul canvas.
  193. * \param x X coordinate.
  194. * \param y Y coordinate.
  195. * \param s The string to print.
  196. * \return This function always returns 0.
  197. */
  198. int cucul_put_str(cucul_canvas_t *cv, int x, int y, char const *s)
  199. {
  200. unsigned int read;
  201. if(y < 0 || y >= (int)cv->height || x >= (int)cv->width)
  202. return 0;
  203. while(*s && x < -1)
  204. {
  205. x += cucul_utf32_is_fullwidth(cucul_utf8_to_utf32(s, &read)) ? 2 : 1;
  206. s += read;
  207. }
  208. while(*s && x < (int)cv->width)
  209. {
  210. uint32_t ch = cucul_utf8_to_utf32(s, &read);
  211. cucul_put_char(cv, x, y, ch);
  212. x += cucul_utf32_is_fullwidth(ch) ? 2 : 1;
  213. s += read;
  214. }
  215. return 0;
  216. }
  217. /** \brief Print a formated string.
  218. *
  219. * Format a string at the given coordinates, using the default foreground
  220. * and background values. The coordinates may be outside the canvas
  221. * boundaries (eg. a negative Y coordinate) and the string will be cropped
  222. * accordingly if it is too long. The syntax of the format string is the
  223. * same as for the C printf() function.
  224. *
  225. * This function never fails.
  226. *
  227. * \param cv A handle to the libcucul canvas.
  228. * \param x X coordinate.
  229. * \param y Y coordinate.
  230. * \param format The format string to print.
  231. * \param ... Arguments to the format string.
  232. * \return This function always returns 0.
  233. */
  234. int cucul_printf(cucul_canvas_t *cv, int x, int y, char const *format, ...)
  235. {
  236. char tmp[BUFSIZ];
  237. char *buf = tmp;
  238. va_list args;
  239. if(y < 0 || y >= (int)cv->height || x >= (int)cv->width)
  240. return 0;
  241. if(cv->width - x + 1 > BUFSIZ)
  242. buf = malloc(cv->width - x + 1);
  243. va_start(args, format);
  244. #if defined(HAVE_VSNPRINTF)
  245. vsnprintf(buf, cv->width - x + 1, format, args);
  246. #else
  247. vsprintf(buf, format, args);
  248. #endif
  249. buf[cv->width - x] = '\0';
  250. va_end(args);
  251. cucul_put_str(cv, x, y, buf);
  252. if(buf != tmp)
  253. free(buf);
  254. return 0;
  255. }
  256. /** \brief Clear the canvas.
  257. *
  258. * Clear the canvas using the current foreground and background colours.
  259. *
  260. * This function never fails.
  261. *
  262. * \param cv The canvas to clear.
  263. * \return This function always returns 0.
  264. */
  265. int cucul_clear_canvas(cucul_canvas_t *cv)
  266. {
  267. uint32_t attr = cv->curattr;
  268. unsigned int n;
  269. for(n = cv->width * cv->height; n--; )
  270. {
  271. cv->chars[n] = (uint32_t)' ';
  272. cv->attrs[n] = attr;
  273. }
  274. return 0;
  275. }
  276. /** \brief Set cursor handle.
  277. *
  278. * Set the canvas' handle. Blitting functions will use the handle value
  279. * to put the canvas at the proper coordinates.
  280. *
  281. * This function never fails.
  282. *
  283. * \param cv A handle to the libcucul canvas.
  284. * \param x X handle coordinate.
  285. * \param y Y handle coordinate.
  286. * \return This function always returns 0.
  287. */
  288. int cucul_set_canvas_handle(cucul_canvas_t *cv, int x, int y)
  289. {
  290. cv->frames[cv->frame].handlex = x;
  291. cv->frames[cv->frame].handley = y;
  292. return 0;
  293. }
  294. /** \brief Get X handle position.
  295. *
  296. * Retrieve the X coordinate of the canvas' handle.
  297. *
  298. * This function never fails.
  299. *
  300. * \param cv A handle to the libcucul canvas.
  301. * \return The canvas' handle's X coordinate.
  302. */
  303. int cucul_get_canvas_handle_x(cucul_canvas_t *cv)
  304. {
  305. return cv->frames[cv->frame].handlex;
  306. }
  307. /** \brief Get Y handle position.
  308. *
  309. * Retrieve the Y coordinate of the canvas' handle.
  310. *
  311. * This function never fails.
  312. *
  313. * \param cv A handle to the libcucul canvas.
  314. * \return The canvas' handle's Y coordinate.
  315. */
  316. int cucul_get_canvas_handle_y(cucul_canvas_t *cv)
  317. {
  318. return cv->frames[cv->frame].handley;
  319. }
  320. /** \brief Blit a canvas onto another one.
  321. *
  322. * Blit a canvas onto another one at the given coordinates.
  323. * An optional mask canvas can be used.
  324. *
  325. * If an error occurs, -1 is returned and \b errno is set accordingly:
  326. * - \c EINVAL A mask was specified but the mask size and source canvas
  327. * size do not match.
  328. *
  329. * \param dst The destination canvas.
  330. * \param x X coordinate.
  331. * \param y Y coordinate.
  332. * \param src The source canvas.
  333. * \param mask The mask canvas.
  334. * \return 0 in case of success, -1 if an error occurred.
  335. */
  336. int cucul_blit(cucul_canvas_t *dst, int x, int y,
  337. cucul_canvas_t const *src, cucul_canvas_t const *mask)
  338. {
  339. int i, j, starti, startj, endi, endj;
  340. if(mask && (src->width != mask->width || src->height != mask->height))
  341. {
  342. seterrno(EINVAL);
  343. return -1;
  344. }
  345. x -= src->frames[src->frame].handlex;
  346. y -= src->frames[src->frame].handley;
  347. starti = x < 0 ? -x : 0;
  348. startj = y < 0 ? -y : 0;
  349. endi = (x + src->width >= dst->width) ? dst->width - x : src->width;
  350. endj = (y + src->height >= dst->height) ? dst->height - y : src->height;
  351. if((unsigned int)starti > src->width || (unsigned int)startj > src->height
  352. || starti >= endi || startj >= endj)
  353. return 0;
  354. for(j = startj; j < endj; j++)
  355. {
  356. unsigned int dstix = (j + y) * dst->width + starti + x;
  357. unsigned int srcix = j * src->width + starti;
  358. int stride = endi - starti;
  359. /* FIXME: we are ignoring the mask here */
  360. if((starti + x) && dst->chars[dstix] == CUCUL_MAGIC_FULLWIDTH)
  361. dst->chars[dstix - 1] = ' ';
  362. if((unsigned int)(endi + x) < dst->width
  363. && dst->chars[dstix + stride] == CUCUL_MAGIC_FULLWIDTH)
  364. dst->chars[dstix + stride] = ' ';
  365. if(mask)
  366. {
  367. for(i = 0; i < stride; i++)
  368. {
  369. if(mask->chars[srcix + i] == (uint32_t)' ')
  370. continue;
  371. dst->chars[dstix + i] = src->chars[srcix + i];
  372. dst->attrs[dstix + i] = src->attrs[srcix + i];
  373. }
  374. }
  375. else
  376. {
  377. memcpy(dst->chars + dstix, src->chars + srcix, stride * 4);
  378. memcpy(dst->attrs + dstix, src->attrs + srcix, stride * 4);
  379. }
  380. /* Fix split fullwidth chars */
  381. if(src->chars[srcix] == CUCUL_MAGIC_FULLWIDTH)
  382. dst->chars[dstix] = ' ';
  383. if((unsigned int)endi < src->width
  384. && src->chars[endi] == CUCUL_MAGIC_FULLWIDTH)
  385. dst->chars[dstix + stride - 1] = ' ';
  386. }
  387. return 0;
  388. }
  389. /** \brief Set a canvas' new boundaries.
  390. *
  391. * Set new boundaries for a canvas. This function can be used to crop a
  392. * canvas, to expand it or for combinations of both actions. All frames
  393. * are affected by this function.
  394. *
  395. * If an error occurs, -1 is returned and \b errno is set accordingly:
  396. * - \c EBUSY The canvas is in use by a display driver and cannot be resized.
  397. * - \c ENOMEM Not enough memory for the requested canvas size. If this
  398. * happens, the canvas handle becomes invalid and should not be used.
  399. *
  400. * \param cv The canvas to crop.
  401. * \param x X coordinate of the top-left corner.
  402. * \param y Y coordinate of the top-left corner.
  403. * \param w The width of the cropped area.
  404. * \param h The height of the cropped area.
  405. * \return 0 in case of success, -1 if an error occurred.
  406. */
  407. int cucul_set_canvas_boundaries(cucul_canvas_t *cv, int x, int y,
  408. unsigned int w, unsigned int h)
  409. {
  410. cucul_canvas_t *new;
  411. unsigned int f, saved_f, framecount;
  412. if(cv->refcount)
  413. {
  414. seterrno(EBUSY);
  415. return -1;
  416. }
  417. new = cucul_create_canvas(w, h);
  418. framecount = cucul_get_canvas_frame_count(cv);
  419. saved_f = cv->frame;
  420. for(f = 0; f < framecount; f++)
  421. {
  422. if(f)
  423. cucul_create_canvas_frame(new, framecount);
  424. cucul_set_canvas_frame(cv, f);
  425. cucul_set_canvas_frame(new, f);
  426. cucul_blit(new, -x, -y, cv, NULL);
  427. free(cv->frames[f].chars);
  428. free(cv->frames[f].attrs);
  429. }
  430. free(cv->frames);
  431. memcpy(cv, new, sizeof(cucul_canvas_t));
  432. free(new);
  433. cucul_set_canvas_frame(cv, saved_f);
  434. return 0;
  435. }