Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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