Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

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