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.
 
 
 
 
 
 

629 lines
18 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. /** \brief Set cursor position.
  35. *
  36. * Put the cursor at the given coordinates. Functions making use of the
  37. * cursor will use the new values. Setting the cursor position outside the
  38. * canvas is legal but the cursor will not be shown.
  39. *
  40. * This function never fails.
  41. *
  42. * \param cv A handle to the libcaca canvas.
  43. * \param x X cursor coordinate.
  44. * \param y Y cursor coordinate.
  45. * \return This function always returns 0.
  46. */
  47. int caca_gotoxy(caca_canvas_t *cv, int x, int y)
  48. {
  49. cv->frames[cv->frame].x = x;
  50. cv->frames[cv->frame].y = y;
  51. return 0;
  52. }
  53. /** \brief Get X cursor position.
  54. *
  55. * Retrieve the X coordinate of the cursor's position.
  56. *
  57. * This function never fails.
  58. *
  59. * \param cv A handle to the libcaca canvas.
  60. * \return The cursor's X coordinate.
  61. */
  62. int caca_wherex(caca_canvas_t const *cv)
  63. {
  64. return cv->frames[cv->frame].x;
  65. }
  66. /** \brief Get Y cursor position.
  67. *
  68. * Retrieve the Y coordinate of the cursor's position.
  69. *
  70. * This function never fails.
  71. *
  72. * \param cv A handle to the libcaca canvas.
  73. * \return The cursor's Y coordinate.
  74. */
  75. int caca_wherey(caca_canvas_t const *cv)
  76. {
  77. return cv->frames[cv->frame].y;
  78. }
  79. /** \brief Print an ASCII or Unicode character.
  80. *
  81. * Print an ASCII or Unicode character at the given coordinates, using
  82. * the default foreground and background colour values.
  83. *
  84. * If the coordinates are outside the canvas boundaries, nothing is printed.
  85. * If a fullwidth Unicode character gets overwritten, its remaining visible
  86. * parts are replaced with spaces. If the canvas' boundaries would split the
  87. * fullwidth character in two, a space is printed instead.
  88. *
  89. * The behaviour when printing non-printable characters or invalid UTF-32
  90. * characters is undefined. To print a sequence of bytes forming an UTF-8
  91. * character instead of an UTF-32 character, use the caca_put_str() function.
  92. *
  93. * This function returns the width of the printed character. If it is a
  94. * fullwidth character, 2 is returned. Otherwise, 1 is returned.
  95. *
  96. * This function never fails.
  97. *
  98. * \param cv A handle to the libcaca canvas.
  99. * \param x X coordinate.
  100. * \param y Y coordinate.
  101. * \param ch The character to print.
  102. * \return The width of the printed character: 2 for a fullwidth character,
  103. * 1 otherwise.
  104. */
  105. int caca_put_char(caca_canvas_t *cv, int x, int y, uint32_t ch)
  106. {
  107. uint32_t *curchar, *curattr, attr;
  108. int fullwidth, xmin, xmax, ret;
  109. if(ch == CACA_MAGIC_FULLWIDTH)
  110. return 1;
  111. fullwidth = caca_utf32_is_fullwidth(ch);
  112. ret = fullwidth ? 2 : 1;
  113. if(x >= (int)cv->width || y < 0 || y >= (int)cv->height)
  114. return ret;
  115. if(x == -1 && fullwidth)
  116. {
  117. x = 0;
  118. ch = ' ';
  119. fullwidth = 0;
  120. }
  121. else if(x < 0)
  122. return ret;
  123. curchar = cv->chars + x + y * cv->width;
  124. curattr = cv->attrs + x + y * cv->width;
  125. attr = cv->curattr;
  126. xmin = xmax = x;
  127. /* When overwriting the right part of a fullwidth character,
  128. * replace its left part with a space. */
  129. if(x && curchar[0] == CACA_MAGIC_FULLWIDTH)
  130. {
  131. curchar[-1] = ' ';
  132. xmin--;
  133. }
  134. if(fullwidth)
  135. {
  136. if(x + 1 == (int)cv->width)
  137. ch = ' ';
  138. else
  139. {
  140. xmax++;
  141. /* When overwriting the left part of a fullwidth character,
  142. * replace its right part with a space. */
  143. if(x + 2 < (int)cv->width && curchar[2] == CACA_MAGIC_FULLWIDTH)
  144. {
  145. curchar[2] = ' ';
  146. xmax++;
  147. }
  148. curchar[1] = CACA_MAGIC_FULLWIDTH;
  149. curattr[1] = attr;
  150. }
  151. }
  152. else
  153. {
  154. /* When overwriting the left part of a fullwidth character,
  155. * replace its right part with a space. */
  156. if(x + 1 != (int)cv->width && curchar[1] == CACA_MAGIC_FULLWIDTH)
  157. {
  158. curchar[1] = ' ';
  159. xmax++;
  160. }
  161. }
  162. /* Only add a dirty rectangle if we are pasting a different character
  163. * or attribute at that place. This does not account for inconsistencies
  164. * in the canvas, ie. if CACA_MAGIC_FULLWIDTH lies at illegal places,
  165. * but it's the caller's responsibility not to corrupt the contents. */
  166. if(!cv->dirty_disabled
  167. && (curchar[0] != ch || curattr[0] != attr))
  168. caca_add_dirty_rect(cv, xmin, y, xmax - xmin + 1, 1);
  169. curchar[0] = ch;
  170. curattr[0] = attr;
  171. return ret;
  172. }
  173. /** \brief Get the Unicode character at the given coordinates.
  174. *
  175. * Get the ASCII or Unicode value of the character at the given
  176. * coordinates. If the value is less or equal to 127 (0x7f),
  177. * the character can be printed as ASCII. Otherise, it must be handled
  178. * as a UTF-32 value.
  179. *
  180. * If the coordinates are outside the canvas boundaries, a space (0x20)
  181. * is returned.
  182. *
  183. * A special exception is when CACA_MAGIC_FULLWIDTH is returned. This
  184. * value is guaranteed not to be a valid Unicode character, and indicates
  185. * that the character at the left of the requested one is a fullwidth
  186. * character.
  187. *
  188. * This function never fails.
  189. *
  190. * \param cv A handle to the libcaca canvas.
  191. * \param x X coordinate.
  192. * \param y Y coordinate.
  193. * \return The Unicode character at the given coordinates.
  194. */
  195. uint32_t caca_get_char(caca_canvas_t const *cv, int x, int y)
  196. {
  197. if(x < 0 || x >= (int)cv->width || y < 0 || y >= (int)cv->height)
  198. return ' ';
  199. return cv->chars[x + y * cv->width];
  200. }
  201. /** \brief Print a string.
  202. *
  203. * Print an UTF-8 string at the given coordinates, using the default
  204. * foreground and background values. The coordinates may be outside the
  205. * canvas boundaries (eg. a negative Y coordinate) and the string will
  206. * be cropped accordingly if it is too long.
  207. *
  208. * See caca_put_char() for more information on how fullwidth characters
  209. * are handled when overwriting each other or at the canvas' boundaries.
  210. *
  211. * This function returns the number of cells printed by the string. It is
  212. * not the number of characters printed, because fullwidth characters
  213. * account for two cells.
  214. *
  215. * This function never fails.
  216. *
  217. * \param cv A handle to the libcaca canvas.
  218. * \param x X coordinate.
  219. * \param y Y coordinate.
  220. * \param s The string to print.
  221. * \return The number of cells printed.
  222. */
  223. int caca_put_str(caca_canvas_t *cv, int x, int y, char const *s)
  224. {
  225. size_t rd;
  226. int len = 0;
  227. if(y < 0 || y >= (int)cv->height || x >= (int)cv->width)
  228. {
  229. while(*s)
  230. {
  231. len += caca_utf32_is_fullwidth(caca_utf8_to_utf32(s, &rd)) ? 2 : 1;
  232. s += rd;
  233. }
  234. return len;
  235. }
  236. while(*s)
  237. {
  238. uint32_t ch = caca_utf8_to_utf32(s, &rd);
  239. if(x + len >= -1 && x + len < (int)cv->width)
  240. caca_put_char(cv, x + len, y, ch);
  241. len += caca_utf32_is_fullwidth(ch) ? 2 : 1;
  242. s += rd;
  243. }
  244. return len;
  245. }
  246. /** \brief Print a formated string.
  247. *
  248. * Format a string at the given coordinates, using the default foreground
  249. * and background values. The coordinates may be outside the canvas
  250. * boundaries (eg. a negative Y coordinate) and the string will be cropped
  251. * accordingly if it is too long. The syntax of the format string is the
  252. * same as for the C printf() function.
  253. *
  254. * This function returns the number of cells printed by the string. It is
  255. * not the number of characters printed, because fullwidth characters
  256. * account for two cells.
  257. *
  258. * This function never fails.
  259. *
  260. * \param cv A handle to the libcaca canvas.
  261. * \param x X coordinate.
  262. * \param y Y coordinate.
  263. * \param format The format string to print.
  264. * \param ... Arguments to the format string.
  265. * \return The number of cells printed.
  266. */
  267. int caca_printf(caca_canvas_t *cv, int x, int y, char const *format, ...)
  268. {
  269. va_list args;
  270. int ret;
  271. va_start(args, format);
  272. ret = caca_vprintf(cv, x, y, format, args);
  273. va_end(args);
  274. return ret;
  275. }
  276. /** \brief Print a formated string (va_list version).
  277. *
  278. * Format a string at the given coordinates, using the default foreground
  279. * and background values. The coordinates may be outside the canvas
  280. * boundaries (eg. a negative Y coordinate) and the string will be cropped
  281. * accordingly if it is too long. The syntax of the format string is the
  282. * same as for the C vprintf() function.
  283. *
  284. * This function returns the number of cells printed by the string. It is
  285. * not the number of characters printed, because fullwidth characters
  286. * account for two cells.
  287. *
  288. * This function never fails.
  289. *
  290. * \param cv A handle to the libcaca canvas.
  291. * \param x X coordinate.
  292. * \param y Y coordinate.
  293. * \param format The format string to print.
  294. * \param args A va_list containting the arguments to the format string.
  295. * \return The number of cells printed.
  296. */
  297. int caca_vprintf(caca_canvas_t *cv, int x, int y, char const *format,
  298. va_list args)
  299. {
  300. char tmp[BUFSIZ];
  301. char *buf = tmp;
  302. int ret;
  303. if(cv->width - x + 1 > BUFSIZ)
  304. buf = malloc(cv->width - x + 1);
  305. #if defined(HAVE_VSNPRINTF_S)
  306. vsnprintf_s(buf, cv->width - x + 1, _TRUNCATE, format, args);
  307. #elif 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);