您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 ellipse and circle drawing functions, both filled
  15. * and outline.
  16. */
  17. #include "config.h"
  18. #include "common.h"
  19. #if !defined(__KERNEL__)
  20. # include <stdlib.h>
  21. #endif
  22. #include "cucul.h"
  23. #include "cucul_internals.h"
  24. static void ellipsepoints(cucul_canvas_t *, int, int, int, int, uint32_t);
  25. /** \brief Draw a circle on the canvas using the given character.
  26. *
  27. * \param cv The handle to the libcucul canvas.
  28. * \param x Center X coordinate.
  29. * \param y Center Y coordinate.
  30. * \param r Circle radius.
  31. * \param str UTF-8 string representing the character that should be used
  32. * to draw the circle outline.
  33. * \return void
  34. */
  35. void cucul_draw_circle(cucul_canvas_t *cv, int x, int y, int r, char const *str)
  36. {
  37. int test, dx, dy;
  38. uint32_t ch = _cucul_utf8_to_utf32(str);
  39. /* Optimized Bresenham. Kick ass. */
  40. for(test = 0, dx = 0, dy = r ; dx <= dy ; dx++)
  41. {
  42. ellipsepoints(cv, x, y, dx, dy, ch);
  43. ellipsepoints(cv, x, y, dy, dx, ch);
  44. test += test > 0 ? dx - dy-- : dx;
  45. }
  46. }
  47. /** \brief Fill an ellipse on the canvas using the given character.
  48. *
  49. * \param cv The handle to the libcucul canvas.
  50. * \param xo Center X coordinate.
  51. * \param yo Center Y coordinate.
  52. * \param a Ellipse X radius.
  53. * \param b Ellipse Y radius.
  54. * \param str UTF-8 string representing the character that should be used
  55. * to fill the ellipse.
  56. * \return void
  57. */
  58. void cucul_fill_ellipse(cucul_canvas_t *cv, int xo, int yo, int a, int b,
  59. char const *str)
  60. {
  61. int d2;
  62. int x = 0;
  63. int y = b;
  64. int d1 = b*b - (a*a*b) + (a*a/4);
  65. while(a*a*y - a*a/2 > b*b*(x+1))
  66. {
  67. if(d1 < 0)
  68. {
  69. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  70. }
  71. else
  72. {
  73. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  74. cucul_draw_line(cv, xo - x, yo - y, xo + x, yo - y, str);
  75. cucul_draw_line(cv, xo - x, yo + y, xo + x, yo + y, str);
  76. y--;
  77. }
  78. x++;
  79. }
  80. cucul_draw_line(cv, xo - x, yo - y, xo + x, yo - y, str);
  81. cucul_draw_line(cv, xo - x, yo + y, xo + x, yo + y, str);
  82. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  83. while(y > 0)
  84. {
  85. if(d2 < 0)
  86. {
  87. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  88. x++;
  89. }
  90. else
  91. {
  92. d2 += a*a*(-2*y+3);
  93. }
  94. y--;
  95. cucul_draw_line(cv, xo - x, yo - y, xo + x, yo - y, str);
  96. cucul_draw_line(cv, xo - x, yo + y, xo + x, yo + y, str);
  97. }
  98. }
  99. /** \brief Draw an ellipse on the canvas using the given character.
  100. *
  101. * \param cv The handle to the libcucul canvas.
  102. * \param xo Center X coordinate.
  103. * \param yo Center Y coordinate.
  104. * \param a Ellipse X radius.
  105. * \param b Ellipse Y radius.
  106. * \param str UTF-8 string representing the character that should be used
  107. * to draw the ellipse outline.
  108. * \return void
  109. */
  110. void cucul_draw_ellipse(cucul_canvas_t *cv, int xo, int yo, int a, int b,
  111. char const *str)
  112. {
  113. int d2;
  114. int x = 0;
  115. int y = b;
  116. int d1 = b*b - (a*a*b) + (a*a/4);
  117. uint32_t ch = _cucul_utf8_to_utf32(str);
  118. ellipsepoints(cv, xo, yo, x, y, ch);
  119. while(a*a*y - a*a/2 > b*b*(x+1))
  120. {
  121. if(d1 < 0)
  122. {
  123. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  124. }
  125. else
  126. {
  127. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  128. y--;
  129. }
  130. x++;
  131. ellipsepoints(cv, xo, yo, x, y, ch);
  132. }
  133. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  134. while(y > 0)
  135. {
  136. if(d2 < 0)
  137. {
  138. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  139. x++;
  140. }
  141. else
  142. {
  143. d2 += a*a*(-2*y+3);
  144. }
  145. y--;
  146. ellipsepoints(cv, xo, yo, x, y, ch);
  147. }
  148. }
  149. /** \brief Draw a thin ellipse on the canvas.
  150. *
  151. * \param cv The handle to the libcucul canvas.
  152. * \param xo Center X coordinate.
  153. * \param yo Center Y coordinate.
  154. * \param a Ellipse X radius.
  155. * \param b Ellipse Y radius.
  156. * \return void
  157. */
  158. void cucul_draw_thin_ellipse(cucul_canvas_t *cv, int xo, int yo, int a, int b)
  159. {
  160. /* FIXME: this is not correct */
  161. int d2;
  162. int x = 0;
  163. int y = b;
  164. int d1 = b*b - (a*a*b) + (a*a/4);
  165. ellipsepoints(cv, xo, yo, x, y, '-');
  166. while(a*a*y - a*a/2 > b*b*(x+1))
  167. {
  168. if(d1 < 0)
  169. {
  170. d1 += b*b*(2*x+1); /* XXX: "Computer Graphics" has + 3 here. */
  171. }
  172. else
  173. {
  174. d1 += b*b*(2*x*1) + a*a*(-2*y+2);
  175. y--;
  176. }
  177. x++;
  178. ellipsepoints(cv, xo, yo, x, y, '-');
  179. }
  180. d2 = b*b*(x+0.5)*(x+0.5) + a*a*(y-1)*(y-1) - a*a*b*b;
  181. while(y > 0)
  182. {
  183. if(d2 < 0)
  184. {
  185. d2 += b*b*(2*x+2) + a*a*(-2*y+3);
  186. x++;
  187. }
  188. else
  189. {
  190. d2 += a*a*(-2*y+3);
  191. }
  192. y--;
  193. ellipsepoints(cv, xo, yo, x, y, '|');
  194. }
  195. }
  196. static void ellipsepoints(cucul_canvas_t *cv, int xo, int yo, int x, int y,
  197. uint32_t ch)
  198. {
  199. uint8_t b = 0;
  200. if(xo + x >= 0 && xo + x < (int)cv->width)
  201. b |= 0x1;
  202. if(xo - x >= 0 && xo - x < (int)cv->width)
  203. b |= 0x2;
  204. if(yo + y >= 0 && yo + y < (int)cv->height)
  205. b |= 0x4;
  206. if(yo - y >= 0 && yo - y < (int)cv->height)
  207. b |= 0x8;
  208. if((b & (0x1|0x4)) == (0x1|0x4))
  209. _cucul_putchar32(cv, xo + x, yo + y, ch);
  210. if((b & (0x2|0x4)) == (0x2|0x4))
  211. _cucul_putchar32(cv, xo - x, yo + y, ch);
  212. if((b & (0x1|0x8)) == (0x1|0x8))
  213. _cucul_putchar32(cv, xo + x, yo - y, ch);
  214. if((b & (0x2|0x8)) == (0x2|0x8))
  215. _cucul_putchar32(cv, xo - x, yo - y, ch);
  216. }