Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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