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.
 
 
 
 
 
 

633 lines
18 KiB

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