Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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