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.

преди 19 години
преди 18 години
преди 19 години
преди 19 години
преди 19 години
преди 19 години
преди 19 години
преди 19 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2010 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * This library is free software. It comes without any warranty, to
  7. * the extent permitted by applicable law. You can redistribute it
  8. * and/or modify it under the terms of the Do What The Fuck You Want
  9. * To Public License, Version 2, as published by Sam Hocevar. See
  10. * http://sam.zoy.org/wtfpl/COPYING for more details.
  11. */
  12. /*
  13. * This file contains various canvas handling functions such as character
  14. * and string drawing.
  15. */
  16. #include "config.h"
  17. #if !defined(__KERNEL__)
  18. # include <stdio.h> /* BUFSIZ */
  19. # include <string.h>
  20. # include <stdlib.h>
  21. # include <stdarg.h>
  22. # if defined(HAVE_UNISTD_H)
  23. # include <unistd.h>
  24. # endif
  25. # if defined(HAVE_SIGNAL_H)
  26. # include <signal.h>
  27. # endif
  28. # if defined(HAVE_SYS_IOCTL_H)
  29. # include <sys/ioctl.h>
  30. # endif
  31. #endif
  32. #include "caca.h"
  33. #include "caca_internals.h"
  34. /** \brief Set cursor position.
  35. *
  36. * Put the cursor at the given coordinates. Functions making use of the
  37. * cursor will use the new values. Setting the cursor position outside the
  38. * canvas is legal but the cursor will not be shown.
  39. *
  40. * This function never fails.
  41. *
  42. * \param cv A handle to the libcaca canvas.
  43. * \param x X cursor coordinate.
  44. * \param y Y cursor coordinate.
  45. * \return This function always returns 0.
  46. */
  47. int caca_gotoxy(caca_canvas_t *cv, int x, int y)
  48. {
  49. cv->frames[cv->frame].x = x;
  50. cv->frames[cv->frame].y = y;
  51. return 0;
  52. }
  53. /** \brief Get X cursor position.
  54. *
  55. * Retrieve the X coordinate of the cursor's position.
  56. *
  57. * This function never fails.
  58. *
  59. * \param cv A handle to the libcaca canvas.
  60. * \return The cursor's X coordinate.
  61. */
  62. int caca_wherex(caca_canvas_t const *cv)
  63. {
  64. return cv->frames[cv->frame].x;
  65. }
  66. /** \brief Get Y cursor position.
  67. *
  68. * Retrieve the Y coordinate of the cursor's position.
  69. *
  70. * This function never fails.
  71. *
  72. * \param cv A handle to the libcaca canvas.
  73. * \return The cursor's Y coordinate.
  74. */
  75. int caca_wherey(caca_canvas_t const *cv)
  76. {
  77. return cv->frames[cv->frame].y;
  78. }
  79. /** \brief Print an ASCII or Unicode character.
  80. *
  81. * Print an ASCII or Unicode character at the given coordinates, using
  82. * the default foreground and background colour values.
  83. *
  84. * If the coordinates are outside the canvas boundaries, nothing is printed.
  85. * If a fullwidth Unicode character gets overwritten, its remaining visible
  86. * parts are replaced with spaces. If the canvas' boundaries would split the
  87. * fullwidth character in two, a space is printed instead.
  88. *
  89. * The behaviour when printing non-printable characters or invalid UTF-32
  90. * characters is undefined. To print a sequence of bytes forming an UTF-8
  91. * character instead of an UTF-32 character, use the caca_put_str() function.
  92. *
  93. * This function returns the width of the printed character. If it is a
  94. * fullwidth character, 2 is returned. Otherwise, 1 is returned.
  95. *
  96. * This function never fails.
  97. *
  98. * \param cv A handle to the libcaca canvas.
  99. * \param x X coordinate.
  100. * \param y Y coordinate.
  101. * \param ch The character to print.
  102. * \return The width of the printed character: 2 for a fullwidth character,
  103. * 1 otherwise.
  104. */
  105. int caca_put_char(caca_canvas_t *cv, int x, int y, uint32_t ch)
  106. {
  107. uint32_t *curchar, *curattr, attr;
  108. int fullwidth, xmin, xmax, ret;
  109. if(ch == CACA_MAGIC_FULLWIDTH)
  110. return 1;
  111. fullwidth = caca_utf32_is_fullwidth(ch);
  112. ret = fullwidth ? 2 : 1;
  113. if(x >= (int)cv->width || y < 0 || y >= (int)cv->height)
  114. return ret;
  115. if(x == -1 && fullwidth)
  116. {
  117. x = 0;
  118. ch = ' ';
  119. fullwidth = 0;
  120. }
  121. else if(x < 0)
  122. return ret;
  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(!cv->dirty_disabled
  167. && (curchar[0] != ch || curattr[0] != attr))
  168. caca_add_dirty_rect(cv, xmin, y, xmax - xmin + 1, 1);
  169. curchar[0] = ch;
  170. curattr[0] = attr;
  171. return ret;
  172. }
  173. /** \brief Get the Unicode character at the given coordinates.
  174. *
  175. * Get the ASCII or Unicode value of the character at the given
  176. * coordinates. If the value is less or equal to 127 (0x7f),
  177. * the character can be printed as ASCII. Otherise, it must be handled
  178. * as a UTF-32 value.
  179. *
  180. * If the coordinates are outside the canvas boundaries, a space (0x20)
  181. * is returned.
  182. *
  183. * A special exception is when CACA_MAGIC_FULLWIDTH is returned. This
  184. * value is guaranteed not to be a valid Unicode character, and indicates
  185. * that the character at the left of the requested one is a fullwidth
  186. * character.
  187. *
  188. * This function never fails.
  189. *
  190. * \param cv A handle to the libcaca canvas.
  191. * \param x X coordinate.
  192. * \param y Y coordinate.
  193. * \return The Unicode character at the given coordinates.
  194. */
  195. uint32_t caca_get_char(caca_canvas_t const *cv, int x, int y)
  196. {
  197. if(x < 0 || x >= (int)cv->width || y < 0 || y >= (int)cv->height)
  198. return ' ';
  199. return cv->chars[x + y * cv->width];
  200. }
  201. /** \brief Print a string.
  202. *
  203. * Print an UTF-8 string at the given coordinates, using the default
  204. * foreground and background values. The coordinates may be outside the
  205. * canvas boundaries (eg. a negative Y coordinate) and the string will
  206. * be cropped accordingly if it is too long.
  207. *
  208. * See caca_put_char() for more information on how fullwidth characters
  209. * are handled when overwriting each other or at the canvas' boundaries.
  210. *
  211. * This function returns the number of cells printed by the string. It is
  212. * not the number of characters printed, because fullwidth characters
  213. * account for two cells.
  214. *
  215. * This function never fails.
  216. *
  217. * \param cv A handle to the libcaca canvas.
  218. * \param x X coordinate.
  219. * \param y Y coordinate.
  220. * \param s The string to print.
  221. * \return The number of cells printed.
  222. */
  223. int caca_put_str(caca_canvas_t *cv, int x, int y, char const *s)
  224. {
  225. size_t rd;
  226. int len = 0;
  227. if(y < 0 || y >= (int)cv->height || x >= (int)cv->width)
  228. {
  229. while(*s)
  230. {
  231. len += caca_utf32_is_fullwidth(caca_utf8_to_utf32(s, &rd)) ? 2 : 1;
  232. s += rd;
  233. }
  234. return len;
  235. }
  236. while(*s)
  237. {
  238. uint32_t ch = caca_utf8_to_utf32(s, &rd);
  239. if(x + len >= -1 && x + len < (int)cv->width)
  240. caca_put_char(cv, x + len, y, ch);
  241. len += caca_utf32_is_fullwidth(ch) ? 2 : 1;
  242. s += rd;
  243. }
  244. return len;
  245. }
  246. /** \brief Print a formated string.
  247. *
  248. * Format a string at the given coordinates, using the default foreground
  249. * and background values. The coordinates may be outside the canvas
  250. * boundaries (eg. a negative Y coordinate) and the string will be cropped
  251. * accordingly if it is too long. The syntax of the format string is the
  252. * same as for the C printf() function.
  253. *
  254. * This function returns the number of cells printed by the string. It is
  255. * not the number of characters printed, because fullwidth characters
  256. * account for two cells.
  257. *
  258. * This function never fails.
  259. *
  260. * \param cv A handle to the libcaca canvas.
  261. * \param x X coordinate.
  262. * \param y Y coordinate.
  263. * \param format The format string to print.
  264. * \param ... Arguments to the format string.
  265. * \return The number of cells printed.
  266. */
  267. int caca_printf(caca_canvas_t *cv, int x, int y, char const *format, ...)
  268. {
  269. va_list args;
  270. int ret;
  271. va_start(args, format);
  272. ret = caca_vprintf(cv, x, y, format, args);
  273. va_end(args);
  274. return ret;
  275. }
  276. /** \brief Print a formated string (va_list version).
  277. *
  278. * Format a string at the given coordinates, using the default foreground
  279. * and background values. The coordinates may be outside the canvas
  280. * boundaries (eg. a negative Y coordinate) and the string will be cropped
  281. * accordingly if it is too long. The syntax of the format string is the
  282. * same as for the C vprintf() function.
  283. *
  284. * This function returns the number of cells printed by the string. It is
  285. * not the number of characters printed, because fullwidth characters
  286. * account for two cells.
  287. *
  288. * This function never fails.
  289. *
  290. * \param cv A handle to the libcaca canvas.
  291. * \param x X coordinate.
  292. * \param y Y coordinate.
  293. * \param format The format string to print.
  294. * \param args A va_list containting the arguments to the format string.
  295. * \return The number of cells printed.
  296. */
  297. int caca_vprintf(caca_canvas_t *cv, int x, int y, char const *format,
  298. va_list args)
  299. {
  300. char tmp[BUFSIZ];
  301. char *buf = tmp;
  302. int ret;
  303. if(cv->width - x + 1 > BUFSIZ)
  304. buf = malloc(cv->width - x + 1);
  305. #if defined(HAVE_VSNPRINTF)
  306. vsnprintf(buf, cv->width - x + 1, format, args);
  307. #else
  308. vsprintf(buf, format, args);
  309. #endif
  310. buf[cv->width - x] = '\0';
  311. ret = caca_put_str(cv, x, y, buf);
  312. if(buf != tmp)
  313. free(buf);
  314. return ret;
  315. }
  316. /** \brief Clear the canvas.
  317. *
  318. * Clear the canvas using the current foreground and background colours.
  319. *
  320. * This function never fails.
  321. *
  322. * \param cv The canvas to clear.
  323. * \return This function always returns 0.
  324. */
  325. int caca_clear_canvas(caca_canvas_t *cv)
  326. {
  327. uint32_t attr = cv->curattr;
  328. int n;
  329. for(n = cv->width * cv->height; n--; )
  330. {
  331. cv->chars[n] = (uint32_t)' ';
  332. cv->attrs[n] = attr;
  333. }
  334. if(!cv->dirty_disabled)
  335. caca_add_dirty_rect(cv, 0, 0, cv->width, cv->height);
  336. return 0;
  337. }
  338. /** \brief Set cursor handle.
  339. *
  340. * Set the canvas' handle. Blitting functions will use the handle value
  341. * to put the canvas at the proper coordinates.
  342. *
  343. * This function never fails.
  344. *
  345. * \param cv A handle to the libcaca canvas.
  346. * \param x X handle coordinate.
  347. * \param y Y handle coordinate.
  348. * \return This function always returns 0.
  349. */
  350. int caca_set_canvas_handle(caca_canvas_t *cv, int x, int y)
  351. {
  352. cv->frames[cv->frame].handlex = x;
  353. cv->frames[cv->frame].handley = y;
  354. return 0;
  355. }
  356. /** \brief Get X handle position.
  357. *
  358. * Retrieve the X coordinate of the canvas' handle.
  359. *
  360. * This function never fails.
  361. *
  362. * \param cv A handle to the libcaca canvas.
  363. * \return The canvas' handle's X coordinate.
  364. */
  365. int caca_get_canvas_handle_x(caca_canvas_t const *cv)
  366. {
  367. return cv->frames[cv->frame].handlex;
  368. }
  369. /** \brief Get Y handle position.
  370. *
  371. * Retrieve the Y coordinate of the canvas' handle.
  372. *
  373. * This function never fails.
  374. *
  375. * \param cv A handle to the libcaca canvas.
  376. * \return The canvas' handle's Y coordinate.
  377. */
  378. int caca_get_canvas_handle_y(caca_canvas_t const *cv)
  379. {
  380. return cv->frames[cv->frame].handley;
  381. }
  382. /** \brief Blit a canvas onto another one.
  383. *
  384. * Blit a canvas onto another one at the given coordinates.
  385. * An optional mask canvas can be used.
  386. *
  387. * If an error occurs, -1 is returned and \b errno is set accordingly:
  388. * - \c EINVAL A mask was specified but the mask size and source canvas
  389. * size do not match.
  390. *
  391. * \param dst The destination canvas.
  392. * \param x X coordinate.
  393. * \param y Y coordinate.
  394. * \param src The source canvas.
  395. * \param mask The mask canvas.
  396. * \return 0 in case of success, -1 if an error occurred.
  397. */
  398. int caca_blit(caca_canvas_t *dst, int x, int y,
  399. caca_canvas_t const *src, caca_canvas_t const *mask)
  400. {
  401. int i, j, starti, startj, endi, endj, stride, bleed_left, bleed_right;
  402. if(mask && (src->width != mask->width || src->height != mask->height))
  403. {
  404. seterrno(EINVAL);
  405. return -1;
  406. }
  407. x -= src->frames[src->frame].handlex;
  408. y -= src->frames[src->frame].handley;
  409. starti = x < 0 ? -x : 0;
  410. startj = y < 0 ? -y : 0;
  411. endi = (x + src->width >= dst->width) ? dst->width - x : src->width;
  412. endj = (y + src->height >= dst->height) ? dst->height - y : src->height;
  413. stride = endi - starti;
  414. if(starti > src->width || startj > src->height
  415. || starti >= endi || startj >= endj)
  416. return 0;
  417. bleed_left = bleed_right = 0;
  418. for(j = startj; j < endj; j++)
  419. {
  420. int dstix = (j + y) * dst->width + starti + x;
  421. int srcix = j * src->width + starti;
  422. /* FIXME: we are ignoring the mask here */
  423. if((starti + x) && dst->chars[dstix] == CACA_MAGIC_FULLWIDTH)
  424. {
  425. dst->chars[dstix - 1] = ' ';
  426. bleed_left = 1;
  427. }
  428. if(endi + x < dst->width
  429. && dst->chars[dstix + stride] == CACA_MAGIC_FULLWIDTH)
  430. {
  431. dst->chars[dstix + stride] = ' ';
  432. bleed_right = 1;
  433. }
  434. if(mask)
  435. {
  436. for(i = 0; i < stride; i++)
  437. {
  438. if(mask->chars[srcix + i] == (uint32_t)' ')
  439. continue;
  440. if(dst->chars[dstix + i] != src->chars[srcix + i] ||
  441. dst->attrs[dstix + i] != src->attrs[srcix + i])
  442. {
  443. dst->chars[dstix + i] = src->chars[srcix + i];
  444. dst->attrs[dstix + i] = src->attrs[srcix + i];
  445. if(!dst->dirty_disabled)
  446. caca_add_dirty_rect(dst, x + starti + i, y + j, 1, 1);
  447. }
  448. }
  449. }
  450. else
  451. {
  452. if(memcmp(dst->chars + dstix, src->chars + srcix, stride * 4) ||
  453. memcmp(dst->attrs + dstix, src->attrs + srcix, stride * 4))
  454. {
  455. /* FIXME be more precise ? */
  456. memcpy(dst->chars + dstix, src->chars + srcix, stride * 4);
  457. memcpy(dst->attrs + dstix, src->attrs + srcix, stride * 4);
  458. if(!dst->dirty_disabled)
  459. caca_add_dirty_rect(dst, x + starti, y + j, stride, 1);
  460. }
  461. }
  462. /* Fix split fullwidth chars */
  463. if(src->chars[srcix] == CACA_MAGIC_FULLWIDTH)
  464. dst->chars[dstix] = ' ';
  465. if(endi < src->width && src->chars[endi] == CACA_MAGIC_FULLWIDTH)
  466. dst->chars[dstix + stride - 1] = ' ';
  467. }
  468. return 0;
  469. }
  470. /** \brief Set a canvas' new boundaries.
  471. *
  472. * Set new boundaries for a canvas. This function can be used to crop a
  473. * canvas, to expand it or for combinations of both actions. All frames
  474. * are affected by this function.
  475. *
  476. * If an error occurs, -1 is returned and \b errno is set accordingly:
  477. * - \c EINVAL Specified width or height is invalid.
  478. * - \c EBUSY The canvas is in use by a display driver and cannot be resized.
  479. * - \c ENOMEM Not enough memory for the requested canvas size. If this
  480. * happens, the canvas handle becomes invalid and should not be used.
  481. *
  482. * \param cv The canvas to crop.
  483. * \param x X coordinate of the top-left corner.
  484. * \param y Y coordinate of the top-left corner.
  485. * \param w The width of the cropped area.
  486. * \param h The height of the cropped area.
  487. * \return 0 in case of success, -1 if an error occurred.
  488. */
  489. int caca_set_canvas_boundaries(caca_canvas_t *cv, int x, int y, int w, int h)
  490. {
  491. caca_canvas_t *new;
  492. int f, saved_f, framecount;
  493. if(cv->refcount)
  494. {
  495. seterrno(EBUSY);
  496. return -1;
  497. }
  498. if(w < 0 || h < 0)
  499. {
  500. seterrno(EINVAL);
  501. return -1;
  502. }
  503. new = caca_create_canvas(w, h);
  504. framecount = caca_get_frame_count(cv);
  505. saved_f = cv->frame;
  506. for(f = 0; f < framecount; f++)
  507. {
  508. if(f)
  509. caca_create_frame(new, framecount);
  510. caca_set_frame(cv, f);
  511. caca_set_frame(new, f);
  512. caca_blit(new, -x, -y, cv, NULL);
  513. free(cv->frames[f].chars);
  514. free(cv->frames[f].attrs);
  515. }
  516. free(cv->frames);
  517. cv->frames = new->frames;
  518. free(new);
  519. caca_set_frame(cv, saved_f);
  520. _caca_load_frame_info(cv);
  521. /* FIXME: this may be optimised somewhat */
  522. if(!cv->dirty_disabled)
  523. caca_add_dirty_rect(cv, 0, 0, cv->width, cv->height);
  524. return 0;
  525. }
  526. /*
  527. * XXX: The following functions are aliases.
  528. */
  529. int cucul_gotoxy(cucul_canvas_t *, int, int) CACA_ALIAS(caca_gotoxy);
  530. int cucul_get_cursor_x(cucul_canvas_t const *) CACA_ALIAS(caca_wherex);
  531. int cucul_get_cursor_y(cucul_canvas_t const *) CACA_ALIAS(caca_wherey);
  532. int caca_get_cursor_x(caca_canvas_t const *) CACA_ALIAS(caca_wherex);
  533. int caca_get_cursor_y(caca_canvas_t const *) CACA_ALIAS(caca_wherey);
  534. int cucul_put_char(cucul_canvas_t *, int, int, uint32_t)
  535. CACA_ALIAS(caca_put_char);
  536. uint32_t cucul_get_char(cucul_canvas_t const *, int, int)
  537. CACA_ALIAS(caca_get_char);
  538. int cucul_put_str(cucul_canvas_t *, int, int, char const *)
  539. CACA_ALIAS(caca_put_str);
  540. int cucul_printf(cucul_canvas_t *, int, int, char const *, ...)
  541. CACA_ALIAS(caca_printf);
  542. int cucul_clear_canvas(cucul_canvas_t *) CACA_ALIAS(caca_clear_canvas);
  543. int cucul_set_canvas_handle(cucul_canvas_t *, int, int)
  544. CACA_ALIAS(caca_set_canvas_handle);
  545. int cucul_get_canvas_handle_x(cucul_canvas_t const *)
  546. CACA_ALIAS(caca_get_canvas_handle_x);
  547. int cucul_get_canvas_handle_y(cucul_canvas_t const *)
  548. CACA_ALIAS(caca_get_canvas_handle_y);
  549. int cucul_blit(cucul_canvas_t *, int, int, cucul_canvas_t const *,
  550. cucul_canvas_t const *) CACA_ALIAS(caca_blit);
  551. int cucul_set_canvas_boundaries(cucul_canvas_t *, int, int, int, int)
  552. CACA_ALIAS(caca_set_canvas_boundaries);