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 години
преди 18 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. /*
  14. * This file contains various canvas handling functions such as character
  15. * and string drawing.
  16. */
  17. #include "config.h"
  18. #include "common.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_ERRNO_H)
  25. # include <errno.h>
  26. # endif
  27. # if defined(HAVE_UNISTD_H)
  28. # include <unistd.h>
  29. # endif
  30. # if defined(HAVE_SIGNAL_H)
  31. # include <signal.h>
  32. # endif
  33. # if defined(HAVE_SYS_IOCTL_H)
  34. # include <sys/ioctl.h>
  35. # endif
  36. #endif
  37. #include "cucul.h"
  38. #include "cucul_internals.h"
  39. /** \brief Set cursor position.
  40. *
  41. * Put the cursor at the given coordinates. Functions making use of the
  42. * cursor will use the new values. Setting the cursor position outside the
  43. * canvas is legal but the cursor will not be shown.
  44. *
  45. * This function never fails.
  46. *
  47. * \param cv A handle to the libcucul canvas.
  48. * \param x X cursor coordinate.
  49. * \param y Y cursor coordinate.
  50. * \return This function always returns 0.
  51. */
  52. int cucul_gotoxy(cucul_canvas_t *cv, int x, int y)
  53. {
  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 libcucul canvas.
  65. * \return The cursor's X coordinate.
  66. */
  67. int cucul_get_cursor_x(cucul_canvas_t *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 libcucul canvas.
  78. * \return The cursor's Y coordinate.
  79. */
  80. int cucul_get_cursor_y(cucul_canvas_t *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 cucul_put_str() function.
  97. *
  98. * This function never fails.
  99. *
  100. * \param cv A handle to the libcucul 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 cucul_put_char(cucul_canvas_t *cv, int x, int y, unsigned long int ch)
  107. {
  108. uint32_t *curchar, *curattr, attr;
  109. int fullwidth;
  110. if(x >= (int)cv->width || y < 0 || y >= (int)cv->height)
  111. return 0;
  112. if(ch == CUCUL_MAGIC_FULLWIDTH)
  113. return 0;
  114. fullwidth = cucul_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. /* When overwriting the right part of a fullwidth character,
  127. * replace its left part with a space. */
  128. if(x && curchar[0] == CUCUL_MAGIC_FULLWIDTH)
  129. curchar[-1] = ' ';
  130. if(fullwidth)
  131. {
  132. if(x + 1 == (int)cv->width)
  133. ch = ' ';
  134. else
  135. {
  136. /* When overwriting the left part of a fullwidth character,
  137. * replace its right part with a space. */
  138. if(x + 2 < (int)cv->width && curchar[2] == CUCUL_MAGIC_FULLWIDTH)
  139. curchar[2] = ' ';
  140. curchar[1] = CUCUL_MAGIC_FULLWIDTH;
  141. curattr[1] = attr;
  142. }
  143. }
  144. else
  145. {
  146. /* When overwriting the left part of a fullwidth character,
  147. * replace its right part with a space. */
  148. if(x + 1 != (int)cv->width && curchar[1] == CUCUL_MAGIC_FULLWIDTH)
  149. curchar[1] = ' ';
  150. }
  151. curchar[0] = ch;
  152. curattr[0] = attr;
  153. return 0;
  154. }
  155. /** \brief Get the Unicode character at the given coordinates.
  156. *
  157. * Get the ASCII or Unicode value of the character at the given
  158. * coordinates. If the value is less or equal to 127 (0x7f),
  159. * the character can be printed as ASCII. Otherise, it must be handled
  160. * as a UTF-32 value.
  161. *
  162. * If the coordinates are outside the canvas boundaries, a space (0x20)
  163. * is returned.
  164. *
  165. * A special exception is when CUCUL_MAGIC_FULLWIDTH is returned. This
  166. * value is guaranteed not to be a valid Unicode character, and indicates
  167. * that the character at the left of the requested one is a fullwidth
  168. * character.
  169. *
  170. * This function never fails.
  171. *
  172. * \param cv A handle to the libcucul canvas.
  173. * \param x X coordinate.
  174. * \param y Y coordinate.
  175. * \return This function always returns 0.
  176. */
  177. unsigned long int cucul_get_char(cucul_canvas_t *cv, int x, int y)
  178. {
  179. if(x < 0 || x >= (int)cv->width || y < 0 || y >= (int)cv->height)
  180. return ' ';
  181. return (unsigned long int)cv->chars[x + y * cv->width];
  182. }
  183. /** \brief Print a string.
  184. *
  185. * Print an UTF-8 string at the given coordinates, using the default
  186. * foreground and background values. The coordinates may be outside the
  187. * canvas boundaries (eg. a negative Y coordinate) and the string will
  188. * be cropped accordingly if it is too long.
  189. *
  190. * See cucul_put_char() for more information on how fullwidth characters
  191. * are handled when overwriting each other or at the canvas' boundaries.
  192. *
  193. * This function never fails.
  194. *
  195. * \param cv A handle to the libcucul canvas.
  196. * \param x X coordinate.
  197. * \param y Y coordinate.
  198. * \param s The string to print.
  199. * \return This function always returns 0.
  200. */
  201. int cucul_put_str(cucul_canvas_t *cv, int x, int y, char const *s)
  202. {
  203. unsigned int read;
  204. if(y < 0 || y >= (int)cv->height || x >= (int)cv->width)
  205. return 0;
  206. while(*s && x < -1)
  207. {
  208. x += cucul_utf32_is_fullwidth(cucul_utf8_to_utf32(s, &read)) ? 2 : 1;
  209. s += read;
  210. }
  211. while(*s && x < (int)cv->width)
  212. {
  213. uint32_t ch = cucul_utf8_to_utf32(s, &read);
  214. cucul_put_char(cv, x, y, ch);
  215. x += cucul_utf32_is_fullwidth(ch) ? 2 : 1;
  216. s += read;
  217. }
  218. return 0;
  219. }
  220. /** \brief Print a formated string.
  221. *
  222. * Format a string at the given coordinates, using the default foreground
  223. * and background values. The coordinates may be outside the canvas
  224. * boundaries (eg. a negative Y coordinate) and the string will be cropped
  225. * accordingly if it is too long. The syntax of the format string is the
  226. * same as for the C printf() function.
  227. *
  228. * This function never fails.
  229. *
  230. * \param cv A handle to the libcucul canvas.
  231. * \param x X coordinate.
  232. * \param y Y coordinate.
  233. * \param format The format string to print.
  234. * \param ... Arguments to the format string.
  235. * \return This function always returns 0.
  236. */
  237. int cucul_printf(cucul_canvas_t *cv, int x, int y, char const *format, ...)
  238. {
  239. char tmp[BUFSIZ];
  240. char *buf = tmp;
  241. va_list args;
  242. if(y < 0 || y >= (int)cv->height || x >= (int)cv->width)
  243. return 0;
  244. if(cv->width - x + 1 > BUFSIZ)
  245. buf = malloc(cv->width - x + 1);
  246. va_start(args, format);
  247. #if defined(HAVE_VSNPRINTF)
  248. vsnprintf(buf, cv->width - x + 1, format, args);
  249. #else
  250. vsprintf(buf, format, args);
  251. #endif
  252. buf[cv->width - x] = '\0';
  253. va_end(args);
  254. cucul_put_str(cv, x, y, buf);
  255. if(buf != tmp)
  256. free(buf);
  257. return 0;
  258. }
  259. /** \brief Clear the canvas.
  260. *
  261. * Clear the canvas using the current foreground and background colours.
  262. *
  263. * This function never fails.
  264. *
  265. * \param cv The canvas to clear.
  266. * \return This function always returns 0.
  267. */
  268. int cucul_clear_canvas(cucul_canvas_t *cv)
  269. {
  270. uint32_t attr = cv->curattr;
  271. unsigned int n;
  272. for(n = cv->width * cv->height; n--; )
  273. {
  274. cv->chars[n] = (uint32_t)' ';
  275. cv->attrs[n] = attr;
  276. }
  277. return 0;
  278. }
  279. /** \brief Set cursor handle.
  280. *
  281. * Set the canvas' handle. Blitting functions will use the handle value
  282. * to put the canvas at the proper coordinates.
  283. *
  284. * This function never fails.
  285. *
  286. * \param cv A handle to the libcucul canvas.
  287. * \param x X handle coordinate.
  288. * \param y Y handle coordinate.
  289. * \return This function always returns 0.
  290. */
  291. int cucul_set_canvas_handle(cucul_canvas_t *cv, int x, int y)
  292. {
  293. cv->frames[cv->frame].handlex = x;
  294. cv->frames[cv->frame].handley = y;
  295. return 0;
  296. }
  297. /** \brief Get X handle position.
  298. *
  299. * Retrieve the X coordinate of the canvas' handle.
  300. *
  301. * This function never fails.
  302. *
  303. * \param cv A handle to the libcucul canvas.
  304. * \return The canvas' handle's X coordinate.
  305. */
  306. int cucul_get_canvas_handle_x(cucul_canvas_t *cv)
  307. {
  308. return cv->frames[cv->frame].handlex;
  309. }
  310. /** \brief Get Y handle position.
  311. *
  312. * Retrieve the Y coordinate of the canvas' handle.
  313. *
  314. * This function never fails.
  315. *
  316. * \param cv A handle to the libcucul canvas.
  317. * \return The canvas' handle's Y coordinate.
  318. */
  319. int cucul_get_canvas_handle_y(cucul_canvas_t *cv)
  320. {
  321. return cv->frames[cv->frame].handley;
  322. }
  323. /** \brief Blit a canvas onto another one.
  324. *
  325. * Blit a canvas onto another one at the given coordinates.
  326. * An optional mask canvas can be used.
  327. *
  328. * If an error occurs, -1 is returned and \b errno is set accordingly:
  329. * - \c EINVAL A mask was specified but the mask size and source canvas
  330. * size do not match.
  331. *
  332. * \param dst The destination canvas.
  333. * \param x X coordinate.
  334. * \param y Y coordinate.
  335. * \param src The source canvas.
  336. * \param mask The mask canvas.
  337. * \return 0 in case of success, -1 if an error occurred.
  338. */
  339. int cucul_blit(cucul_canvas_t *dst, int x, int y,
  340. cucul_canvas_t const *src, cucul_canvas_t const *mask)
  341. {
  342. int i, j, starti, startj, endi, endj;
  343. if(mask && (src->width != mask->width || src->height != mask->height))
  344. {
  345. #if defined(HAVE_ERRNO_H)
  346. errno = EINVAL;
  347. #endif
  348. return -1;
  349. }
  350. x -= src->frames[src->frame].handlex;
  351. y -= src->frames[src->frame].handley;
  352. starti = x < 0 ? -x : 0;
  353. startj = y < 0 ? -y : 0;
  354. endi = (x + src->width >= dst->width) ? dst->width - x : src->width;
  355. endj = (y + src->height >= dst->height) ? dst->height - y : src->height;
  356. if((unsigned int)starti > src->width || (unsigned int)startj > src->height
  357. || starti >= endi || startj >= endj)
  358. return 0;
  359. for(j = startj; j < endj; j++)
  360. {
  361. unsigned int dstix = (j + y) * dst->width + starti + x;
  362. unsigned int srcix = j * src->width + starti;
  363. int stride = endi - starti;
  364. /* FIXME: we are ignoring the mask here */
  365. if((starti + x) && dst->chars[dstix] == CUCUL_MAGIC_FULLWIDTH)
  366. dst->chars[dstix - 1] = ' ';
  367. if((unsigned int)(endi + x) < dst->width
  368. && dst->chars[dstix + stride] == CUCUL_MAGIC_FULLWIDTH)
  369. dst->chars[dstix + stride] = ' ';
  370. if(mask)
  371. {
  372. for(i = 0; i < stride; i++)
  373. {
  374. if(mask->chars[srcix + i] == (uint32_t)' ')
  375. continue;
  376. dst->chars[dstix + i] = src->chars[srcix + i];
  377. dst->attrs[dstix + i] = src->attrs[srcix + i];
  378. }
  379. }
  380. else
  381. {
  382. memcpy(dst->chars + dstix, src->chars + srcix, stride * 4);
  383. memcpy(dst->attrs + dstix, src->attrs + srcix, stride * 4);
  384. }
  385. /* Fix split fullwidth chars */
  386. if(src->chars[srcix] == CUCUL_MAGIC_FULLWIDTH)
  387. dst->chars[dstix] = ' ';
  388. if((unsigned int)endi < src->width
  389. && src->chars[endi] == CUCUL_MAGIC_FULLWIDTH)
  390. dst->chars[dstix + stride - 1] = ' ';
  391. }
  392. return 0;
  393. }
  394. /** \brief Set a canvas' new boundaries.
  395. *
  396. * Set new boundaries for a canvas. This function can be used to crop a
  397. * canvas, to expand it or for combinations of both actions. All frames
  398. * are affected by this function.
  399. *
  400. * If an error occurs, -1 is returned and \b errno is set accordingly:
  401. * - \c EBUSY The canvas is in use by a display driver and cannot be resized.
  402. * - \c ENOMEM Not enough memory for the requested canvas size. If this
  403. * happens, the canvas handle becomes invalid and should not be used.
  404. *
  405. * \param cv The canvas to crop.
  406. * \param x X coordinate of the top-left corner.
  407. * \param y Y coordinate of the top-left corner.
  408. * \param w The width of the cropped area.
  409. * \param h The height of the cropped area.
  410. * \return 0 in case of success, -1 if an error occurred.
  411. */
  412. int cucul_set_canvas_boundaries(cucul_canvas_t *cv, int x, int y,
  413. unsigned int w, unsigned int h)
  414. {
  415. cucul_canvas_t *new;
  416. unsigned int f, saved_f, framecount;
  417. if(cv->refcount)
  418. {
  419. #if defined(HAVE_ERRNO_H)
  420. errno = EBUSY;
  421. #endif
  422. return -1;
  423. }
  424. new = cucul_create_canvas(w, h);
  425. framecount = cucul_get_canvas_frame_count(cv);
  426. saved_f = cv->frame;
  427. for(f = 0; f < framecount; f++)
  428. {
  429. if(f)
  430. cucul_create_canvas_frame(new, framecount);
  431. cucul_set_canvas_frame(cv, f);
  432. cucul_set_canvas_frame(new, f);
  433. cucul_blit(new, -x, -y, cv, NULL);
  434. free(cv->frames[f].chars);
  435. free(cv->frames[f].attrs);
  436. }
  437. free(cv->frames);
  438. memcpy(cv, new, sizeof(cucul_canvas_t));
  439. free(new);
  440. cucul_set_canvas_frame(cv, saved_f);
  441. return 0;
  442. }