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