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.
 
 
 
 
 
 

378 lines
9.1 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; 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 line and polyline drawing functions, with both thin
  15. * and thick styles.
  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. #if !defined(_DOXYGEN_SKIP_ME)
  25. struct line
  26. {
  27. int x1, y1;
  28. int x2, y2;
  29. uint32_t ch;
  30. void (*draw) (cucul_canvas_t *, struct line*);
  31. };
  32. #endif
  33. static void clip_line(cucul_canvas_t*, struct line*);
  34. static uint8_t clip_bits(cucul_canvas_t*, int, int);
  35. static void draw_solid_line(cucul_canvas_t*, struct line*);
  36. static void draw_thin_line(cucul_canvas_t*, struct line*);
  37. /** \brief Draw a line on the canvas using the given character.
  38. *
  39. * \param cv The handle to the libcucul canvas.
  40. * \param x1 X coordinate of the first point.
  41. * \param y1 Y coordinate of the first point.
  42. * \param x2 X coordinate of the second point.
  43. * \param y2 Y coordinate of the second point.
  44. * \param str UTF-8 string containing the character to use to draw the line.
  45. * \return void
  46. */
  47. void cucul_draw_line(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
  48. char const *str)
  49. {
  50. struct line s;
  51. s.x1 = x1;
  52. s.y1 = y1;
  53. s.x2 = x2;
  54. s.y2 = y2;
  55. s.ch = _cucul_utf8_to_utf32(str);
  56. s.draw = draw_solid_line;
  57. clip_line(cv, &s);
  58. }
  59. /** \brief Draw a polyline.
  60. *
  61. * Draw a plyline on the canvas using the given character and coordinate
  62. * arrays. The first and last points are not connected, hence in order to
  63. * draw a polygon you need to specify the starting point at the end of the
  64. * list as well.
  65. *
  66. * \param cv The handle to the libcucul canvas.
  67. * \param x Array of X coordinates. Must have \p n + 1 elements.
  68. * \param y Array of Y coordinates. Must have \p n + 1 elements.
  69. * \param n Number of lines to draw.
  70. * \param str UTF-8 string containing the character to use to draw the lines.
  71. * \return void
  72. */
  73. void cucul_draw_polyline(cucul_canvas_t *cv, int const x[], int const y[],
  74. int n, char const *str)
  75. {
  76. int i;
  77. struct line s;
  78. s.ch = _cucul_utf8_to_utf32(str);
  79. s.draw = draw_solid_line;
  80. for(i = 0; i < n; i++)
  81. {
  82. s.x1 = x[i];
  83. s.y1 = y[i];
  84. s.x2 = x[i+1];
  85. s.y2 = y[i+1];
  86. clip_line(cv, &s);
  87. }
  88. }
  89. /** \brief Draw a thin line on the canvas, using ASCII art.
  90. *
  91. * \param cv The handle to the libcucul canvas.
  92. * \param x1 X coordinate of the first point.
  93. * \param y1 Y coordinate of the first point.
  94. * \param x2 X coordinate of the second point.
  95. * \param y2 Y coordinate of the second point.
  96. * \return void
  97. */
  98. void cucul_draw_thin_line(cucul_canvas_t *cv, int x1, int y1, int x2, int y2)
  99. {
  100. struct line s;
  101. s.x1 = x1;
  102. s.y1 = y1;
  103. s.x2 = x2;
  104. s.y2 = y2;
  105. s.draw = draw_thin_line;
  106. clip_line(cv, &s);
  107. }
  108. /** \brief Draw an ASCII art thin polyline.
  109. *
  110. * Draw a thin polyline on the canvas using the given coordinate arrays and
  111. * with ASCII art. The first and last points are not connected, so in order
  112. * to draw a polygon you need to specify the starting point at the end of
  113. * the list as well.
  114. *
  115. * \param cv The handle to the libcucul canvas.
  116. * \param x Array of X coordinates. Must have \p n + 1 elements.
  117. * \param y Array of Y coordinates. Must have \p n + 1 elements.
  118. * \param n Number of lines to draw.
  119. * \return void
  120. */
  121. void cucul_draw_thin_polyline(cucul_canvas_t *cv, int const x[], int const y[],
  122. int n)
  123. {
  124. int i;
  125. struct line s;
  126. s.draw = draw_thin_line;
  127. for(i = 0; i < n; i++)
  128. {
  129. s.x1 = x[i];
  130. s.y1 = y[i];
  131. s.x2 = x[i+1];
  132. s.y2 = y[i+1];
  133. clip_line(cv, &s);
  134. }
  135. }
  136. /*
  137. * XXX: The following functions are local.
  138. */
  139. /* Generic Cohen-Sutherland line clipping function. */
  140. static void clip_line(cucul_canvas_t *cv, struct line* s)
  141. {
  142. uint8_t bits1, bits2;
  143. bits1 = clip_bits(cv, s->x1, s->y1);
  144. bits2 = clip_bits(cv, s->x2, s->y2);
  145. if(bits1 & bits2)
  146. return;
  147. if(bits1 == 0)
  148. {
  149. if(bits2 == 0)
  150. s->draw(cv, s);
  151. else
  152. {
  153. int tmp;
  154. tmp = s->x1; s->x1 = s->x2; s->x2 = tmp;
  155. tmp = s->y1; s->y1 = s->y2; s->y2 = tmp;
  156. clip_line(cv, s);
  157. }
  158. return;
  159. }
  160. if(bits1 & (1<<0))
  161. {
  162. s->y1 = s->y2 - (s->x2 - 0) * (s->y2 - s->y1) / (s->x2 - s->x1);
  163. s->x1 = 0;
  164. }
  165. else if(bits1 & (1<<1))
  166. {
  167. int xmax = cv->width - 1;
  168. s->y1 = s->y2 - (s->x2 - xmax) * (s->y2 - s->y1) / (s->x2 - s->x1);
  169. s->x1 = xmax;
  170. }
  171. else if(bits1 & (1<<2))
  172. {
  173. s->x1 = s->x2 - (s->y2 - 0) * (s->x2 - s->x1) / (s->y2 - s->y1);
  174. s->y1 = 0;
  175. }
  176. else if(bits1 & (1<<3))
  177. {
  178. int ymax = cv->height - 1;
  179. s->x1 = s->x2 - (s->y2 - ymax) * (s->x2 - s->x1) / (s->y2 - s->y1);
  180. s->y1 = ymax;
  181. }
  182. clip_line(cv, s);
  183. }
  184. /* Helper function for clip_line(). */
  185. static uint8_t clip_bits(cucul_canvas_t *cv, int x, int y)
  186. {
  187. uint8_t b = 0;
  188. if(x < 0)
  189. b |= (1<<0);
  190. else if(x >= (int)cv->width)
  191. b |= (1<<1);
  192. if(y < 0)
  193. b |= (1<<2);
  194. else if(y >= (int)cv->height)
  195. b |= (1<<3);
  196. return b;
  197. }
  198. /* Solid line drawing function, using Bresenham's mid-point line
  199. * scan-conversion algorithm. */
  200. static void draw_solid_line(cucul_canvas_t *cv, struct line* s)
  201. {
  202. int x1, y1, x2, y2;
  203. int dx, dy;
  204. int xinc, yinc;
  205. x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2;
  206. dx = abs(x2 - x1);
  207. dy = abs(y2 - y1);
  208. xinc = (x1 > x2) ? -1 : 1;
  209. yinc = (y1 > y2) ? -1 : 1;
  210. if(dx >= dy)
  211. {
  212. int dpr = dy << 1;
  213. int dpru = dpr - (dx << 1);
  214. int delta = dpr - dx;
  215. for(; dx>=0; dx--)
  216. {
  217. _cucul_putchar32(cv, x1, y1, s->ch);
  218. if(delta > 0)
  219. {
  220. x1 += xinc;
  221. y1 += yinc;
  222. delta += dpru;
  223. }
  224. else
  225. {
  226. x1 += xinc;
  227. delta += dpr;
  228. }
  229. }
  230. }
  231. else
  232. {
  233. int dpr = dx << 1;
  234. int dpru = dpr - (dy << 1);
  235. int delta = dpr - dy;
  236. for(; dy >= 0; dy--)
  237. {
  238. _cucul_putchar32(cv, x1, y1, s->ch);
  239. if(delta > 0)
  240. {
  241. x1 += xinc;
  242. y1 += yinc;
  243. delta += dpru;
  244. }
  245. else
  246. {
  247. y1 += yinc;
  248. delta += dpr;
  249. }
  250. }
  251. }
  252. }
  253. /* Thin line drawing function, using Bresenham's mid-point line
  254. * scan-conversion algorithm and ASCII art graphics. */
  255. static void draw_thin_line(cucul_canvas_t *cv, struct line* s)
  256. {
  257. uint32_t charmapx[2], charmapy[2];
  258. int x1, y1, x2, y2;
  259. int dx, dy;
  260. int yinc;
  261. if(s->x2 >= s->x1)
  262. {
  263. charmapx[0] = (s->y1 > s->y2) ? (uint32_t)',' : (uint32_t)'`';
  264. charmapx[1] = (s->y1 > s->y2) ? (uint32_t)'\'' : (uint32_t)'.';
  265. x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2;
  266. }
  267. else
  268. {
  269. charmapx[0] = (s->y1 > s->y2) ? (uint32_t)'`' : (uint32_t)'.';
  270. charmapx[1] = (s->y1 > s->y2) ? (uint32_t)',' : (uint32_t)'\'';
  271. x2 = s->x1; y2 = s->y1; x1 = s->x2; y1 = s->y2;
  272. }
  273. dx = abs(x2 - x1);
  274. dy = abs(y2 - y1);
  275. if(y1 > y2)
  276. {
  277. charmapy[0] = (uint32_t)',';
  278. charmapy[1] = (uint32_t)'\'';
  279. yinc = -1;
  280. }
  281. else
  282. {
  283. yinc = 1;
  284. charmapy[0] = (uint32_t)'`';
  285. charmapy[1] = (uint32_t)'.';
  286. }
  287. if(dx >= dy)
  288. {
  289. int dpr = dy << 1;
  290. int dpru = dpr - (dx << 1);
  291. int delta = dpr - dx;
  292. int prev = 0;
  293. for(; dx>=0; dx--)
  294. {
  295. if(delta > 0)
  296. {
  297. _cucul_putchar32(cv, x1, y1, charmapy[1]);
  298. x1++;
  299. y1 += yinc;
  300. delta += dpru;
  301. prev = 1;
  302. }
  303. else
  304. {
  305. if(prev)
  306. _cucul_putchar32(cv, x1, y1, charmapy[0]);
  307. else
  308. _cucul_putchar32(cv, x1, y1, (uint32_t)'-');
  309. x1++;
  310. delta += dpr;
  311. prev = 0;
  312. }
  313. }
  314. }
  315. else
  316. {
  317. int dpr = dx << 1;
  318. int dpru = dpr - (dy << 1);
  319. int delta = dpr - dy;
  320. for(; dy >= 0; dy--)
  321. {
  322. if(delta > 0)
  323. {
  324. _cucul_putchar32(cv, x1, y1, charmapx[0]);
  325. _cucul_putchar32(cv, x1 + 1, y1, charmapx[1]);
  326. x1++;
  327. y1 += yinc;
  328. delta += dpru;
  329. }
  330. else
  331. {
  332. _cucul_putchar32(cv, x1, y1, (uint32_t)'|');
  333. y1 += yinc;
  334. delta += dpr;
  335. }
  336. }
  337. }
  338. }