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.
 
 
 
 
 
 

645 line
19 KiB

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