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.
 
 
 
 
 
 

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