Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

629 lignes
18 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2009 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * This file contains various canvas handling functions such as character
  16. * and string drawing.
  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 "caca.h"
  35. #include "caca_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 libcaca canvas.
  45. * \param x X cursor coordinate.
  46. * \param y Y cursor coordinate.
  47. * \return This function always returns 0.
  48. */
  49. int caca_gotoxy(caca_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 libcaca canvas.
  62. * \return The cursor's X coordinate.
  63. */
  64. int caca_wherex(caca_canvas_t const *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 libcaca canvas.
  75. * \return The cursor's Y coordinate.
  76. */
  77. int caca_wherey(caca_canvas_t const *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 caca_put_str() function.
  94. *
  95. * This function returns the width of the printed character. If it is a
  96. * fullwidth character, 2 is returned. Otherwise, 1 is returned.
  97. *
  98. * This function never fails.
  99. *
  100. * \param cv A handle to the libcaca canvas.
  101. * \param x X coordinate.
  102. * \param y Y coordinate.
  103. * \param ch The character to print.
  104. * \return The width of the printed character: 2 for a fullwidth character,
  105. * 1 otherwise.
  106. */
  107. int caca_put_char(caca_canvas_t *cv, int x, int y, uint32_t ch)
  108. {
  109. uint32_t *curchar, *curattr, attr;
  110. int fullwidth, xmin, xmax, ret;
  111. if(ch == CACA_MAGIC_FULLWIDTH)
  112. return 1;
  113. fullwidth = caca_utf32_is_fullwidth(ch);
  114. ret = fullwidth ? 2 : 1;
  115. if(x >= (int)cv->width || y < 0 || y >= (int)cv->height)
  116. return ret;
  117. if(x == -1 && fullwidth)
  118. {
  119. x = 0;
  120. ch = ' ';
  121. fullwidth = 0;
  122. }
  123. else if(x < 0)
  124. return ret;
  125. curchar = cv->chars + x + y * cv->width;
  126. curattr = cv->attrs + x + y * cv->width;
  127. attr = cv->curattr;
  128. xmin = xmax = x;
  129. /* When overwriting the right part of a fullwidth character,
  130. * replace its left part with a space. */
  131. if(x && curchar[0] == CACA_MAGIC_FULLWIDTH)
  132. {
  133. curchar[-1] = ' ';
  134. xmin--;
  135. }
  136. if(fullwidth)
  137. {
  138. if(x + 1 == (int)cv->width)
  139. ch = ' ';
  140. else
  141. {
  142. xmax++;
  143. /* When overwriting the left part of a fullwidth character,
  144. * replace its right part with a space. */
  145. if(x + 2 < (int)cv->width && curchar[2] == CACA_MAGIC_FULLWIDTH)
  146. {
  147. curchar[2] = ' ';
  148. xmax++;
  149. }
  150. curchar[1] = CACA_MAGIC_FULLWIDTH;
  151. curattr[1] = attr;
  152. }
  153. }
  154. else
  155. {
  156. /* When overwriting the left part of a fullwidth character,
  157. * replace its right part with a space. */
  158. if(x + 1 != (int)cv->width && curchar[1] == CACA_MAGIC_FULLWIDTH)
  159. {
  160. curchar[1] = ' ';
  161. xmax++;
  162. }
  163. }
  164. /* Only add a dirty rectangle if we are pasting a different character
  165. * or attribute at that place. This does not account for inconsistencies
  166. * in the canvas, ie. if CACA_MAGIC_FULLWIDTH lies at illegal places,
  167. * but it's the caller's responsibility not to corrupt the contents. */
  168. if(!cv->dirty_disabled
  169. && (curchar[0] != ch || curattr[0] != attr))
  170. caca_add_dirty_rect(cv, xmin, y, xmax - xmin + 1, 1);
  171. curchar[0] = ch;
  172. curattr[0] = attr;
  173. return ret;
  174. }
  175. /** \brief Get the Unicode character at the given coordinates.
  176. *
  177. * Get the ASCII or Unicode value of the character at the given
  178. * coordinates. If the value is less or equal to 127 (0x7f),
  179. * the character can be printed as ASCII. Otherise, it must be handled
  180. * as a UTF-32 value.
  181. *
  182. * If the coordinates are outside the canvas boundaries, a space (0x20)
  183. * is returned.
  184. *
  185. * A special exception is when CACA_MAGIC_FULLWIDTH is returned. This
  186. * value is guaranteed not to be a valid Unicode character, and indicates
  187. * that the character at the left of the requested one is a fullwidth
  188. * character.
  189. *
  190. * This function never fails.
  191. *
  192. * \param cv A handle to the libcaca canvas.
  193. * \param x X coordinate.
  194. * \param y Y coordinate.
  195. * \return The Unicode character at the given coordinates.
  196. */
  197. uint32_t caca_get_char(caca_canvas_t const *cv, int x, int y)
  198. {
  199. if(x < 0 || x >= (int)cv->width || y < 0 || y >= (int)cv->height)
  200. return ' ';
  201. return cv->chars[x + y * cv->width];
  202. }
  203. /** \brief Print a string.
  204. *
  205. * Print an UTF-8 string at the given coordinates, using the default
  206. * foreground and background values. The coordinates may be outside the
  207. * canvas boundaries (eg. a negative Y coordinate) and the string will
  208. * be cropped accordingly if it is too long.
  209. *
  210. * See caca_put_char() for more information on how fullwidth characters
  211. * are handled when overwriting each other or at the canvas' boundaries.
  212. *
  213. * This function returns the number of cells printed by the string. It is
  214. * not the number of characters printed, because fullwidth characters
  215. * account for two cells.
  216. *
  217. * This function never fails.
  218. *
  219. * \param cv A handle to the libcaca canvas.
  220. * \param x X coordinate.
  221. * \param y Y coordinate.
  222. * \param s The string to print.
  223. * \return The number of cells printed.
  224. */
  225. int caca_put_str(caca_canvas_t *cv, int x, int y, char const *s)
  226. {
  227. size_t rd;
  228. int len = 0;
  229. if(y < 0 || y >= (int)cv->height || x >= (int)cv->width)
  230. {
  231. while(*s)
  232. {
  233. len += caca_utf32_is_fullwidth(caca_utf8_to_utf32(s, &rd)) ? 2 : 1;
  234. s += rd;
  235. }
  236. return len;
  237. }
  238. while(*s)
  239. {
  240. uint32_t ch = caca_utf8_to_utf32(s, &rd);
  241. if(x + len >= -1 && x + len < (int)cv->width)
  242. caca_put_char(cv, x + len, y, ch);
  243. len += caca_utf32_is_fullwidth(ch) ? 2 : 1;
  244. s += rd;
  245. }
  246. return len;
  247. }
  248. /** \brief Print a formated string.
  249. *
  250. * Format a string at the given coordinates, using the default foreground
  251. * and background values. The coordinates may be outside the canvas
  252. * boundaries (eg. a negative Y coordinate) and the string will be cropped
  253. * accordingly if it is too long. The syntax of the format string is the
  254. * same as for the C printf() function.
  255. *
  256. * This function returns the number of cells printed by the string. It is
  257. * not the number of characters printed, because fullwidth characters
  258. * account for two cells.
  259. *
  260. * This function never fails.
  261. *
  262. * \param cv A handle to the libcaca canvas.
  263. * \param x X coordinate.
  264. * \param y Y coordinate.
  265. * \param format The format string to print.
  266. * \param ... Arguments to the format string.
  267. * \return The number of cells printed.
  268. */
  269. int caca_printf(caca_canvas_t *cv, int x, int y, char const *format, ...)
  270. {
  271. va_list args;
  272. int ret;
  273. va_start(args, format);
  274. ret = caca_vprintf(cv, x, y, format, args);
  275. va_end(args);
  276. return ret;
  277. }
  278. /** \brief Print a formated string (va_list version).
  279. *
  280. * Format a string at the given coordinates, using the default foreground
  281. * and background values. The coordinates may be outside the canvas
  282. * boundaries (eg. a negative Y coordinate) and the string will be cropped
  283. * accordingly if it is too long. The syntax of the format string is the
  284. * same as for the C vprintf() function.
  285. *
  286. * This function returns the number of cells printed by the string. It is
  287. * not the number of characters printed, because fullwidth characters
  288. * account for two cells.
  289. *
  290. * This function never fails.
  291. *
  292. * \param cv A handle to the libcaca canvas.
  293. * \param x X coordinate.
  294. * \param y Y coordinate.
  295. * \param format The format string to print.
  296. * \param ap A va_list containting the arguments to the format string.
  297. * \return The number of cells printed.
  298. */
  299. int caca_vprintf(caca_canvas_t *cv, int x, int y, char const *format,
  300. va_list args)
  301. {
  302. char tmp[BUFSIZ];
  303. char *buf = tmp;
  304. int ret;
  305. if(cv->width - x + 1 > BUFSIZ)
  306. buf = malloc(cv->width - x + 1);
  307. #if defined(HAVE_VSNPRINTF)
  308. vsnprintf(buf, cv->width - x + 1, format, args);
  309. #else
  310. vsprintf(buf, format, args);
  311. #endif
  312. buf[cv->width - x] = '\0';
  313. ret = caca_put_str(cv, x, y, buf);
  314. if(buf != tmp)
  315. free(buf);
  316. return ret;
  317. }
  318. /** \brief Clear the canvas.
  319. *
  320. * Clear the canvas using the current foreground and background colours.
  321. *
  322. * This function never fails.
  323. *
  324. * \param cv The canvas to clear.
  325. * \return This function always returns 0.
  326. */
  327. int caca_clear_canvas(caca_canvas_t *cv)
  328. {
  329. uint32_t attr = cv->curattr;
  330. int n;
  331. for(n = cv->width * cv->height; n--; )
  332. {
  333. cv->chars[n] = (uint32_t)' ';
  334. cv->attrs[n] = attr;
  335. }
  336. if(!cv->dirty_disabled)
  337. caca_add_dirty_rect(cv, 0, 0, cv->width, cv->height);
  338. return 0;
  339. }
  340. /** \brief Set cursor handle.
  341. *
  342. * Set the canvas' handle. Blitting functions will use the handle value
  343. * to put the canvas at the proper coordinates.
  344. *
  345. * This function never fails.
  346. *
  347. * \param cv A handle to the libcaca canvas.
  348. * \param x X handle coordinate.
  349. * \param y Y handle coordinate.
  350. * \return This function always returns 0.
  351. */
  352. int caca_set_canvas_handle(caca_canvas_t *cv, int x, int y)
  353. {
  354. cv->frames[cv->frame].handlex = x;
  355. cv->frames[cv->frame].handley = y;
  356. return 0;
  357. }
  358. /** \brief Get X handle position.
  359. *
  360. * Retrieve the X coordinate of the canvas' handle.
  361. *
  362. * This function never fails.
  363. *
  364. * \param cv A handle to the libcaca canvas.
  365. * \return The canvas' handle's X coordinate.
  366. */
  367. int caca_get_canvas_handle_x(caca_canvas_t const *cv)
  368. {
  369. return cv->frames[cv->frame].handlex;
  370. }
  371. /** \brief Get Y handle position.
  372. *
  373. * Retrieve the Y coordinate of the canvas' handle.
  374. *
  375. * This function never fails.
  376. *
  377. * \param cv A handle to the libcaca canvas.
  378. * \return The canvas' handle's Y coordinate.
  379. */
  380. int caca_get_canvas_handle_y(caca_canvas_t const *cv)
  381. {
  382. return cv->frames[cv->frame].handley;
  383. }
  384. /** \brief Blit a canvas onto another one.
  385. *
  386. * Blit a canvas onto another one at the given coordinates.
  387. * An optional mask canvas can be used.
  388. *
  389. * If an error occurs, -1 is returned and \b errno is set accordingly:
  390. * - \c EINVAL A mask was specified but the mask size and source canvas
  391. * size do not match.
  392. *
  393. * \param dst The destination canvas.
  394. * \param x X coordinate.
  395. * \param y Y coordinate.
  396. * \param src The source canvas.
  397. * \param mask The mask canvas.
  398. * \return 0 in case of success, -1 if an error occurred.
  399. */
  400. int caca_blit(caca_canvas_t *dst, int x, int y,
  401. caca_canvas_t const *src, caca_canvas_t const *mask)
  402. {
  403. int i, j, starti, startj, endi, endj, stride, bleed_left, bleed_right;
  404. if(mask && (src->width != mask->width || src->height != mask->height))
  405. {
  406. seterrno(EINVAL);
  407. return -1;
  408. }
  409. x -= src->frames[src->frame].handlex;
  410. y -= src->frames[src->frame].handley;
  411. starti = x < 0 ? -x : 0;
  412. startj = y < 0 ? -y : 0;
  413. endi = (x + src->width >= dst->width) ? dst->width - x : src->width;
  414. endj = (y + src->height >= dst->height) ? dst->height - y : src->height;
  415. stride = endi - starti;
  416. if(starti > src->width || startj > src->height
  417. || starti >= endi || startj >= endj)
  418. return 0;
  419. bleed_left = bleed_right = 0;
  420. for(j = startj; j < endj; j++)
  421. {
  422. int dstix = (j + y) * dst->width + starti + x;
  423. int srcix = j * src->width + starti;
  424. /* FIXME: we are ignoring the mask here */
  425. if((starti + x) && dst->chars[dstix] == CACA_MAGIC_FULLWIDTH)
  426. {
  427. dst->chars[dstix - 1] = ' ';
  428. bleed_left = 1;
  429. }
  430. if(endi + x < dst->width
  431. && dst->chars[dstix + stride] == CACA_MAGIC_FULLWIDTH)
  432. {
  433. dst->chars[dstix + stride] = ' ';
  434. bleed_right = 1;
  435. }
  436. if(mask)
  437. {
  438. for(i = 0; i < stride; i++)
  439. {
  440. if(mask->chars[srcix + i] == (uint32_t)' ')
  441. continue;
  442. if(dst->chars[dstix + i] != src->chars[srcix + i] ||
  443. dst->attrs[dstix + i] != src->attrs[srcix + i])
  444. {
  445. dst->chars[dstix + i] = src->chars[srcix + i];
  446. dst->attrs[dstix + i] = src->attrs[srcix + i];
  447. if(!dst->dirty_disabled)
  448. caca_add_dirty_rect(dst, x + starti + i, y + j, 1, 1);
  449. }
  450. }
  451. }
  452. else
  453. {
  454. if(memcmp(dst->chars + dstix, src->chars + srcix, stride * 4) ||
  455. memcmp(dst->attrs + dstix, src->attrs + srcix, stride * 4))
  456. {
  457. /* FIXME be more precise ? */
  458. memcpy(dst->chars + dstix, src->chars + srcix, stride * 4);
  459. memcpy(dst->attrs + dstix, src->attrs + srcix, stride * 4);
  460. if(!dst->dirty_disabled)
  461. caca_add_dirty_rect(dst, x + starti, y + j, stride, 1);
  462. }
  463. }
  464. /* Fix split fullwidth chars */
  465. if(src->chars[srcix] == CACA_MAGIC_FULLWIDTH)
  466. dst->chars[dstix] = ' ';
  467. if(endi < src->width && src->chars[endi] == CACA_MAGIC_FULLWIDTH)
  468. dst->chars[dstix + stride - 1] = ' ';
  469. }
  470. return 0;
  471. }
  472. /** \brief Set a canvas' new boundaries.
  473. *
  474. * Set new boundaries for a canvas. This function can be used to crop a
  475. * canvas, to expand it or for combinations of both actions. All frames
  476. * are affected by this function.
  477. *
  478. * If an error occurs, -1 is returned and \b errno is set accordingly:
  479. * - \c EINVAL Specified width or height is invalid.
  480. * - \c EBUSY The canvas is in use by a display driver and cannot be resized.
  481. * - \c ENOMEM Not enough memory for the requested canvas size. If this
  482. * happens, the canvas handle becomes invalid and should not be used.
  483. *
  484. * \param cv The canvas to crop.
  485. * \param x X coordinate of the top-left corner.
  486. * \param y Y coordinate of the top-left corner.
  487. * \param w The width of the cropped area.
  488. * \param h The height of the cropped area.
  489. * \return 0 in case of success, -1 if an error occurred.
  490. */
  491. int caca_set_canvas_boundaries(caca_canvas_t *cv, int x, int y, int w, int h)
  492. {
  493. caca_canvas_t *new;
  494. int f, saved_f, framecount;
  495. if(cv->refcount)
  496. {
  497. seterrno(EBUSY);
  498. return -1;
  499. }
  500. if(w < 0 || h < 0)
  501. {
  502. seterrno(EINVAL);
  503. return -1;
  504. }
  505. new = caca_create_canvas(w, h);
  506. framecount = caca_get_frame_count(cv);
  507. saved_f = cv->frame;
  508. for(f = 0; f < framecount; f++)
  509. {
  510. if(f)
  511. caca_create_frame(new, framecount);
  512. caca_set_frame(cv, f);
  513. caca_set_frame(new, f);
  514. caca_blit(new, -x, -y, cv, NULL);
  515. free(cv->frames[f].chars);
  516. free(cv->frames[f].attrs);
  517. }
  518. free(cv->frames);
  519. cv->frames = new->frames;
  520. free(new);
  521. caca_set_frame(cv, saved_f);
  522. _caca_load_frame_info(cv);
  523. /* FIXME: this may be optimised somewhat */
  524. if(!cv->dirty_disabled)
  525. caca_add_dirty_rect(cv, 0, 0, cv->width, cv->height);
  526. return 0;
  527. }
  528. /*
  529. * XXX: The following functions are aliases.
  530. */
  531. int cucul_gotoxy(cucul_canvas_t *, int, int) CACA_ALIAS(caca_gotoxy);
  532. int cucul_get_cursor_x(cucul_canvas_t const *) CACA_ALIAS(caca_wherex);
  533. int cucul_get_cursor_y(cucul_canvas_t const *) CACA_ALIAS(caca_wherey);
  534. int caca_get_cursor_x(caca_canvas_t const *) CACA_ALIAS(caca_wherex);
  535. int caca_get_cursor_y(caca_canvas_t const *) CACA_ALIAS(caca_wherey);
  536. int cucul_put_char(cucul_canvas_t *, int, int, uint32_t)
  537. CACA_ALIAS(caca_put_char);
  538. uint32_t cucul_get_char(cucul_canvas_t const *, int, int)
  539. CACA_ALIAS(caca_get_char);
  540. int cucul_put_str(cucul_canvas_t *, int, int, char const *)
  541. CACA_ALIAS(caca_put_str);
  542. int cucul_printf(cucul_canvas_t *, int, int, char const *, ...)
  543. CACA_ALIAS(caca_printf);
  544. int cucul_clear_canvas(cucul_canvas_t *) CACA_ALIAS(caca_clear_canvas);
  545. int cucul_set_canvas_handle(cucul_canvas_t *, int, int)
  546. CACA_ALIAS(caca_set_canvas_handle);
  547. int cucul_get_canvas_handle_x(cucul_canvas_t const *)
  548. CACA_ALIAS(caca_get_canvas_handle_x);
  549. int cucul_get_canvas_handle_y(cucul_canvas_t const *)
  550. CACA_ALIAS(caca_get_canvas_handle_y);
  551. int cucul_blit(cucul_canvas_t *, int, int, cucul_canvas_t const *,
  552. cucul_canvas_t const *) CACA_ALIAS(caca_blit);
  553. int cucul_set_canvas_boundaries(cucul_canvas_t *, int, int, int, int)
  554. CACA_ALIAS(caca_set_canvas_boundaries);