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.
 
 
 
 
 
 

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