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