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.
 
 
 
 
 
 

563 lines
16 KiB

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