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.

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