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.
 
 
 
 
 
 

392 lines
9.1 KiB

  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright © 2002—2018 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * This library is free software. It comes without any warranty, to
  7. * the extent permitted by applicable law. You can redistribute it
  8. * and/or modify it under the terms of the Do What the Fuck You Want
  9. * to Public License, Version 2, as published by Sam Hocevar. See
  10. * http://www.wtfpl.net/ for more details.
  11. */
  12. /*
  13. * This file contains line and polyline drawing functions, with both thin
  14. * and thick styles.
  15. */
  16. #include "config.h"
  17. #if !defined(__KERNEL__)
  18. # include <stdlib.h>
  19. #endif
  20. #include "caca.h"
  21. #include "caca_internals.h"
  22. #if !defined(_DOXYGEN_SKIP_ME)
  23. struct line
  24. {
  25. int x1, y1;
  26. int x2, y2;
  27. uint32_t ch;
  28. void (*draw) (caca_canvas_t *, struct line*);
  29. };
  30. #endif
  31. static void clip_line(caca_canvas_t*, struct line*);
  32. static uint8_t clip_bits(caca_canvas_t*, int, int);
  33. static void draw_solid_line(caca_canvas_t*, struct line*);
  34. static void draw_thin_line(caca_canvas_t*, struct line*);
  35. /** \brief Draw a line on the canvas using the given character.
  36. *
  37. * This function never fails.
  38. *
  39. * \param cv The handle to the libcaca 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 ch UTF-32 character to be used to draw the line.
  45. * \return This function always returns 0.
  46. */
  47. int caca_draw_line(caca_canvas_t *cv, int x1, int y1, int x2, int y2,
  48. uint32_t ch)
  49. {
  50. struct line s;
  51. s.x1 = x1;
  52. s.y1 = y1;
  53. s.x2 = x2;
  54. s.y2 = y2;
  55. s.ch = ch;
  56. s.draw = draw_solid_line;
  57. clip_line(cv, &s);
  58. return 0;
  59. }
  60. /** \brief Draw a polyline.
  61. *
  62. * Draw a polyline on the canvas using the given character and coordinate
  63. * arrays. The first and last points are not connected, hence in order to
  64. * draw a polygon you need to specify the starting point at the end of the
  65. * list as well.
  66. *
  67. * This function never fails.
  68. *
  69. * \param cv The handle to the libcaca canvas.
  70. * \param x Array of X coordinates. Must have \p n + 1 elements.
  71. * \param y Array of Y coordinates. Must have \p n + 1 elements.
  72. * \param n Number of lines to draw.
  73. * \param ch UTF-32 character to be used to draw the lines.
  74. * \return This function always returns 0.
  75. */
  76. int caca_draw_polyline(caca_canvas_t *cv, int const x[], int const y[],
  77. int n, uint32_t ch)
  78. {
  79. int i;
  80. struct line s;
  81. s.ch = ch;
  82. s.draw = draw_solid_line;
  83. for(i = 0; i < n; i++)
  84. {
  85. s.x1 = x[i];
  86. s.y1 = y[i];
  87. s.x2 = x[i+1];
  88. s.y2 = y[i+1];
  89. clip_line(cv, &s);
  90. }
  91. return 0;
  92. }
  93. /** \brief Draw a thin line on the canvas, using ASCII art.
  94. *
  95. * This function never fails.
  96. *
  97. * \param cv The handle to the libcaca canvas.
  98. * \param x1 X coordinate of the first point.
  99. * \param y1 Y coordinate of the first point.
  100. * \param x2 X coordinate of the second point.
  101. * \param y2 Y coordinate of the second point.
  102. * \return This function always returns 0.
  103. */
  104. int caca_draw_thin_line(caca_canvas_t *cv, int x1, int y1, int x2, int y2)
  105. {
  106. struct line s;
  107. s.x1 = x1;
  108. s.y1 = y1;
  109. s.x2 = x2;
  110. s.y2 = y2;
  111. s.draw = draw_thin_line;
  112. clip_line(cv, &s);
  113. return 0;
  114. }
  115. /** \brief Draw an ASCII art thin polyline.
  116. *
  117. * Draw a thin polyline on the canvas using the given coordinate arrays and
  118. * with ASCII art. The first and last points are not connected, so in order
  119. * to draw a polygon you need to specify the starting point at the end of
  120. * the list as well.
  121. *
  122. * This function never fails.
  123. *
  124. * \param cv The handle to the libcaca canvas.
  125. * \param x Array of X coordinates. Must have \p n + 1 elements.
  126. * \param y Array of Y coordinates. Must have \p n + 1 elements.
  127. * \param n Number of lines to draw.
  128. * \return This function always returns 0.
  129. */
  130. int caca_draw_thin_polyline(caca_canvas_t *cv, int const x[], int const y[],
  131. int n)
  132. {
  133. int i;
  134. struct line s;
  135. s.draw = draw_thin_line;
  136. for(i = 0; i < n; i++)
  137. {
  138. s.x1 = x[i];
  139. s.y1 = y[i];
  140. s.x2 = x[i+1];
  141. s.y2 = y[i+1];
  142. clip_line(cv, &s);
  143. }
  144. return 0;
  145. }
  146. /*
  147. * XXX: The following functions are local.
  148. */
  149. /* Generic Cohen-Sutherland line clipping function. */
  150. static void clip_line(caca_canvas_t *cv, struct line* s)
  151. {
  152. uint8_t bits1, bits2;
  153. bits1 = clip_bits(cv, s->x1, s->y1);
  154. bits2 = clip_bits(cv, s->x2, s->y2);
  155. if(bits1 & bits2)
  156. return;
  157. if(bits1 == 0)
  158. {
  159. if(bits2 == 0)
  160. s->draw(cv, s);
  161. else
  162. {
  163. int tmp;
  164. tmp = s->x1; s->x1 = s->x2; s->x2 = tmp;
  165. tmp = s->y1; s->y1 = s->y2; s->y2 = tmp;
  166. clip_line(cv, s);
  167. }
  168. return;
  169. }
  170. if(bits1 & (1<<0))
  171. {
  172. s->y1 = s->y2 - (s->x2 - 0) * (s->y2 - s->y1) / (s->x2 - s->x1);
  173. s->x1 = 0;
  174. }
  175. else if(bits1 & (1<<1))
  176. {
  177. int xmax = cv->width - 1;
  178. s->y1 = s->y2 - (s->x2 - xmax) * (s->y2 - s->y1) / (s->x2 - s->x1);
  179. s->x1 = xmax;
  180. }
  181. else if(bits1 & (1<<2))
  182. {
  183. s->x1 = s->x2 - (s->y2 - 0) * (s->x2 - s->x1) / (s->y2 - s->y1);
  184. s->y1 = 0;
  185. }
  186. else if(bits1 & (1<<3))
  187. {
  188. int ymax = cv->height - 1;
  189. s->x1 = s->x2 - (s->y2 - ymax) * (s->x2 - s->x1) / (s->y2 - s->y1);
  190. s->y1 = ymax;
  191. }
  192. clip_line(cv, s);
  193. }
  194. /* Helper function for clip_line(). */
  195. static uint8_t clip_bits(caca_canvas_t *cv, int x, int y)
  196. {
  197. uint8_t b = 0;
  198. if(x < 0)
  199. b |= (1<<0);
  200. else if(x >= (int)cv->width)
  201. b |= (1<<1);
  202. if(y < 0)
  203. b |= (1<<2);
  204. else if(y >= (int)cv->height)
  205. b |= (1<<3);
  206. return b;
  207. }
  208. /* Solid line drawing function, using Bresenham's mid-point line
  209. * scan-conversion algorithm. */
  210. static void draw_solid_line(caca_canvas_t *cv, struct line* s)
  211. {
  212. int x1, y1, x2, y2;
  213. int dx, dy;
  214. int xinc, yinc;
  215. x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2;
  216. dx = abs(x2 - x1);
  217. dy = abs(y2 - y1);
  218. xinc = (x1 > x2) ? -1 : 1;
  219. yinc = (y1 > y2) ? -1 : 1;
  220. if(dx >= dy)
  221. {
  222. int dpr = dy << 1;
  223. int dpru = dpr - (dx << 1);
  224. int delta = dpr - dx;
  225. for(; dx>=0; dx--)
  226. {
  227. caca_put_char(cv, x1, y1, s->ch);
  228. if(delta > 0)
  229. {
  230. x1 += xinc;
  231. y1 += yinc;
  232. delta += dpru;
  233. }
  234. else
  235. {
  236. x1 += xinc;
  237. delta += dpr;
  238. }
  239. }
  240. }
  241. else
  242. {
  243. int dpr = dx << 1;
  244. int dpru = dpr - (dy << 1);
  245. int delta = dpr - dy;
  246. for(; dy >= 0; dy--)
  247. {
  248. caca_put_char(cv, x1, y1, s->ch);
  249. if(delta > 0)
  250. {
  251. x1 += xinc;
  252. y1 += yinc;
  253. delta += dpru;
  254. }
  255. else
  256. {
  257. y1 += yinc;
  258. delta += dpr;
  259. }
  260. }
  261. }
  262. }
  263. /* Thin line drawing function, using Bresenham's mid-point line
  264. * scan-conversion algorithm and ASCII art graphics. */
  265. static void draw_thin_line(caca_canvas_t *cv, struct line* s)
  266. {
  267. uint32_t charmapx[2], charmapy[2];
  268. int x1, y1, x2, y2;
  269. int dx, dy;
  270. int yinc;
  271. if(s->x2 >= s->x1)
  272. {
  273. charmapx[0] = (s->y1 > s->y2) ? ',' : '`';
  274. charmapx[1] = (s->y1 > s->y2) ? '\'' : '.';
  275. x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2;
  276. }
  277. else
  278. {
  279. charmapx[0] = (s->y1 > s->y2) ? '`' : '.';
  280. charmapx[1] = (s->y1 > s->y2) ? ',' : '\'';
  281. x2 = s->x1; y2 = s->y1; x1 = s->x2; y1 = s->y2;
  282. }
  283. dx = abs(x2 - x1);
  284. dy = abs(y2 - y1);
  285. if(y1 > y2)
  286. {
  287. charmapy[0] = ',';
  288. charmapy[1] = '\'';
  289. yinc = -1;
  290. }
  291. else
  292. {
  293. yinc = 1;
  294. charmapy[0] = '`';
  295. charmapy[1] = '.';
  296. }
  297. if(dx >= dy)
  298. {
  299. int dpr = dy << 1;
  300. int dpru = dpr - (dx << 1);
  301. int delta = dpr - dx;
  302. int prev = 0;
  303. for(; dx>=0; dx--)
  304. {
  305. if(delta > 0)
  306. {
  307. caca_put_char(cv, x1, y1, charmapy[1]);
  308. x1++;
  309. y1 += yinc;
  310. delta += dpru;
  311. prev = 1;
  312. }
  313. else
  314. {
  315. if(prev)
  316. caca_put_char(cv, x1, y1, charmapy[0]);
  317. else
  318. caca_put_char(cv, x1, y1, '-');
  319. x1++;
  320. delta += dpr;
  321. prev = 0;
  322. }
  323. }
  324. }
  325. else
  326. {
  327. int dpr = dx << 1;
  328. int dpru = dpr - (dy << 1);
  329. int delta = dpr - dy;
  330. for(; dy >= 0; dy--)
  331. {
  332. if(delta > 0)
  333. {
  334. caca_put_char(cv, x1, y1, charmapx[0]);
  335. caca_put_char(cv, x1 + 1, y1, charmapx[1]);
  336. x1++;
  337. y1 += yinc;
  338. delta += dpru;
  339. }
  340. else
  341. {
  342. caca_put_char(cv, x1, y1, '|');
  343. y1 += yinc;
  344. delta += dpr;
  345. }
  346. }
  347. }
  348. }