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.
 
 
 
 
 
 

348 lines
8.0 KiB

  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. 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 ellipse and circle drawing functions, both filled
  16. * and outline.
  17. */
  18. #include "config.h"
  19. #include "common.h"
  20. #if !defined(__KERNEL__)
  21. # include <stdlib.h>
  22. #endif
  23. #include "cucul.h"
  24. #include "cucul_internals.h"
  25. static void ellipsepoints(cucul_canvas_t *, int, int, int, int, uint32_t, int);
  26. /** \brief Draw a circle on the canvas using the given character.
  27. *
  28. * This function never fails.
  29. *
  30. * \param cv The handle to the libcucul canvas.
  31. * \param x Center X coordinate.
  32. * \param y Center Y coordinate.
  33. * \param r Circle radius.
  34. * \param ch UTF-32 character to be used to draw the circle outline.
  35. * \return This function always returns 0.
  36. */
  37. int cucul_draw_circle(cucul_canvas_t *cv, int x, int y, int r,
  38. unsigned long int ch)
  39. {
  40. int test, dx, dy;
  41. /* Optimized Bresenham. Kick ass. */
  42. for(test = 0, dx = 0, dy = r ; dx <= dy ; dx++)
  43. {
  44. ellipsepoints(cv, x, y, dx, dy, ch, 1);
  45. ellipsepoints(cv, x, y, dy, dx, ch, 1);
  46. test += test > 0 ? dx - dy-- : dx;
  47. }
  48. return 0;
  49. }
  50. /** \brief Fill an ellipse on the canvas using the given character.
  51. *
  52. * This function never fails.
  53. *
  54. * \param cv The handle to the libcucul canvas.
  55. * \param xo Center X coordinate.
  56. * \param yo Center Y coordinate.
  57. * \param a Ellipse X radius.
  58. * \param b Ellipse Y radius.
  59. * \param ch UTF-32 character to be used to fill the ellipse.
  60. * \return This function always returns 0.
  61. */
  62. int cucul_fill_ellipse(cucul_canvas_t *cv, int xo, int yo, int a, int b,
  63. unsigned long int ch)
  64. {
  65. int d2;
  66. int x = 0;
  67. int y = b;
  68. int d1 = b*b - (a*a*b) + (a*a/4);
  69. while(a*a*y - a*a/2 > b*b*(x+1))
  70. {
  71. if(d1 < 0)
  72. {
  73. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  74. }
  75. else
  76. {
  77. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  78. cucul_draw_line(cv, xo - x, yo - y, xo + x, yo - y, ch);
  79. cucul_draw_line(cv, xo - x, yo + y, xo + x, yo + y, ch);
  80. y--;
  81. }
  82. x++;
  83. }
  84. cucul_draw_line(cv, xo - x, yo - y, xo + x, yo - y, ch);
  85. cucul_draw_line(cv, xo - x, yo + y, xo + x, yo + y, ch);
  86. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  87. while(y > 0)
  88. {
  89. if(d2 < 0)
  90. {
  91. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  92. x++;
  93. }
  94. else
  95. {
  96. d2 += a*a*(-2*y+3);
  97. }
  98. y--;
  99. cucul_draw_line(cv, xo - x, yo - y, xo + x, yo - y, ch);
  100. cucul_draw_line(cv, xo - x, yo + y, xo + x, yo + y, ch);
  101. }
  102. return 0;
  103. }
  104. /** \brief Draw an ellipse on the canvas using the given character.
  105. *
  106. * This function never fails.
  107. *
  108. * \param cv The handle to the libcucul canvas.
  109. * \param xo Center X coordinate.
  110. * \param yo Center Y coordinate.
  111. * \param a Ellipse X radius.
  112. * \param b Ellipse Y radius.
  113. * \param ch UTF-32 character to be used to draw the ellipse outline.
  114. * \return This function always returns 0.
  115. */
  116. int cucul_draw_ellipse(cucul_canvas_t *cv, int xo, int yo, int a, int b,
  117. unsigned long int ch)
  118. {
  119. int d2;
  120. int x = 0;
  121. int y = b;
  122. int d1 = b*b - (a*a*b) + (a*a/4);
  123. ellipsepoints(cv, xo, yo, x, y, ch, 0);
  124. while(a*a*y - a*a/2 > b*b*(x+1))
  125. {
  126. if(d1 < 0)
  127. {
  128. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  129. }
  130. else
  131. {
  132. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  133. y--;
  134. }
  135. x++;
  136. ellipsepoints(cv, xo, yo, x, y, ch, 0);
  137. }
  138. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  139. while(y > 0)
  140. {
  141. if(d2 < 0)
  142. {
  143. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  144. x++;
  145. }
  146. else
  147. {
  148. d2 += a*a*(-2*y+3);
  149. }
  150. y--;
  151. ellipsepoints(cv, xo, yo, x, y, ch, 0);
  152. }
  153. return 0;
  154. }
  155. /** \brief Draw a thin ellipse on the canvas.
  156. *
  157. * This function never fails.
  158. *
  159. * \param cv The handle to the libcucul canvas.
  160. * \param xo Center X coordinate.
  161. * \param yo Center Y coordinate.
  162. * \param a Ellipse X radius.
  163. * \param b Ellipse Y radius.
  164. * \return This function always returns 0.
  165. */
  166. int cucul_draw_thin_ellipse(cucul_canvas_t *cv, int xo, int yo, int a, int b)
  167. {
  168. /* FIXME: this is not correct */
  169. int d2;
  170. int x = 0;
  171. int y = b;
  172. int d1 = b*b - (a*a*b) + (a*a/4);
  173. ellipsepoints(cv, xo, yo, x, y, '-', 1);
  174. while(a*a*y - a*a/2 > b*b*(x+1))
  175. {
  176. if(d1 < 0)
  177. {
  178. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  179. ellipsepoints(cv, xo, yo, x + 1, y, '0', 1);
  180. }
  181. else
  182. {
  183. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  184. y--;
  185. ellipsepoints(cv, xo, yo, x + 1, y, '1', 1);
  186. }
  187. x++;
  188. }
  189. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  190. while(y > 0)
  191. {
  192. if(d2 < 0)
  193. {
  194. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  195. x++;
  196. ellipsepoints(cv, xo, yo, x , y - 1, '2', 1);
  197. }
  198. else
  199. {
  200. d2 += a*a*(-2*y+3);
  201. ellipsepoints(cv, xo, yo, x , y - 1, '3', 1);
  202. }
  203. y--;
  204. }
  205. return 0;
  206. }
  207. static void ellipsepoints(cucul_canvas_t *cv, int xo, int yo, int x, int y,
  208. uint32_t ch, int thin)
  209. {
  210. uint8_t b = 0;
  211. if(xo + x >= 0 && xo + x < (int)cv->width)
  212. b |= 0x1;
  213. if(xo - x >= 0 && xo - x < (int)cv->width)
  214. b |= 0x2;
  215. if(yo + y >= 0 && yo + y < (int)cv->height)
  216. b |= 0x4;
  217. if(yo - y >= 0 && yo - y < (int)cv->height)
  218. b |= 0x8;
  219. if((b & (0x1|0x4)) == (0x1|0x4)) {
  220. char c = ch;
  221. if(thin) {
  222. switch(c) {
  223. case '0':
  224. c = '-';
  225. break;
  226. case '1':
  227. c = ',';
  228. break;
  229. case '2':
  230. c = '/';
  231. break;
  232. case '3':
  233. c = '|';
  234. break;
  235. }
  236. }
  237. cucul_put_char(cv, xo + x, yo + y, c);
  238. }
  239. if((b & (0x2|0x4)) == (0x2|0x4)) {
  240. char c = ch;
  241. if(thin) {
  242. switch(c) {
  243. case '0':
  244. c = '-';
  245. break;
  246. case '1':
  247. c = '.';
  248. break;
  249. case '2':
  250. c = '\\';
  251. break;
  252. case '3':
  253. c = '|';
  254. break;
  255. }
  256. }
  257. cucul_put_char(cv, xo - x, yo + y, c);
  258. }
  259. if((b & (0x1|0x8)) == (0x1|0x8)) {
  260. char c = ch;
  261. if(thin) {
  262. switch(c) {
  263. case '0':
  264. c = '-';
  265. break;
  266. case '1':
  267. c = '`';
  268. break;
  269. case '2':
  270. c = '\\';
  271. break;
  272. case '3':
  273. c = '|';
  274. break;
  275. }
  276. }
  277. cucul_put_char(cv, xo + x, yo - y, c);
  278. }
  279. if((b & (0x2|0x8)) == (0x2|0x8)) {
  280. char c = ch;
  281. if(thin) {
  282. switch(c) {
  283. case '0':
  284. c = '-';
  285. break;
  286. case '1':
  287. c = '\'';
  288. break;
  289. case '2':
  290. c = '/';
  291. break;
  292. case '3':
  293. c = '|';
  294. break;
  295. }
  296. }
  297. cucul_put_char(cv, xo - x, yo - y, c);
  298. }
  299. }