source: libcaca/trunk/cucul/line.c @ 810

Last change on this file since 810 was 810, checked in by Sam Hocevar, 14 years ago
  • Renamed cucul_t into cucul_canvas_t. Eh ouais mon con.
  • Property svn:keywords set to Id
File size: 9.0 KB
Line 
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 810 2006-04-18 12:59:07Z 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)
29struct 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
38static void clip_line(cucul_canvas_t*, struct line*);
39static uint8_t clip_bits(cucul_canvas_t*, int, int);
40static void draw_solid_line(cucul_canvas_t*, struct line*);
41static 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 c 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 */
53void cucul_draw_line(cucul_canvas_t *c, 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(c, &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 c 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 */
80void cucul_draw_polyline(cucul_canvas_t *c, int const x[], int const y[], int n,
81                         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(c, &s);
95    }
96}
97
98/** \brief Draw a thin line on the canvas, using ASCII art.
99 *
100 *  \param c 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 */
107void cucul_draw_thin_line(cucul_canvas_t *c, 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(c, &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 c 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 */
131void cucul_draw_thin_polyline(cucul_canvas_t *c, int const x[], int const y[], int n)
132{
133    int i;
134    struct line s;
135    s.draw = draw_thin_line;
136
137    for(i = 0; i < n; i++)
138    {
139        s.x1 = x[i];
140        s.y1 = y[i];
141        s.x2 = x[i+1];
142        s.y2 = y[i+1];
143        clip_line(c, &s);
144    }
145}
146
147/*
148 * XXX: The following functions are local.
149 */
150
151/* Generic Cohen-Sutherland line clipping function. */
152static void clip_line(cucul_canvas_t *c, struct line* s)
153{
154    uint8_t bits1, bits2;
155
156    bits1 = clip_bits(c, s->x1, s->y1);
157    bits2 = clip_bits(c, s->x2, s->y2);
158
159    if(bits1 & bits2)
160        return;
161
162    if(bits1 == 0)
163    {
164        if(bits2 == 0)
165            s->draw(c, s);
166        else
167        {
168            int tmp;
169            tmp = s->x1; s->x1 = s->x2; s->x2 = tmp;
170            tmp = s->y1; s->y1 = s->y2; s->y2 = tmp;
171            clip_line(c, s);
172        }
173
174        return;
175    }
176
177    if(bits1 & (1<<0))
178    {
179        s->y1 = s->y2 - (s->x2 - 0) * (s->y2 - s->y1) / (s->x2 - s->x1);
180        s->x1 = 0;
181    }
182    else if(bits1 & (1<<1))
183    {
184        int xmax = c->width - 1;
185        s->y1 = s->y2 - (s->x2 - xmax) * (s->y2 - s->y1) / (s->x2 - s->x1);
186        s->x1 = xmax;
187    }
188    else if(bits1 & (1<<2))
189    {
190        s->x1 = s->x2 - (s->y2 - 0) * (s->x2 - s->x1) / (s->y2 - s->y1);
191        s->y1 = 0;
192    }
193    else if(bits1 & (1<<3))
194    {
195        int ymax = c->height - 1;
196        s->x1 = s->x2 - (s->y2 - ymax) * (s->x2 - s->x1) / (s->y2 - s->y1);
197        s->y1 = ymax;
198    }
199
200    clip_line(c, s);
201}
202
203/* Helper function for clip_line(). */
204static uint8_t clip_bits(cucul_canvas_t *c, int x, int y)
205{
206    uint8_t b = 0;
207
208    if(x < 0)
209        b |= (1<<0);
210    else if(x >= (int)c->width)
211        b |= (1<<1);
212
213    if(y < 0)
214        b |= (1<<2);
215    else if(y >= (int)c->height)
216        b |= (1<<3);
217
218    return b;
219}
220
221/* Solid line drawing function, using Bresenham's mid-point line
222 * scan-conversion algorithm. */
223static void draw_solid_line(cucul_canvas_t *c, struct line* s)
224{
225    int x1, y1, x2, y2;
226    int dx, dy;
227    int xinc, yinc;
228
229    x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2;
230
231    dx = abs(x2 - x1);
232    dy = abs(y2 - y1);
233
234    xinc = (x1 > x2) ? -1 : 1;
235    yinc = (y1 > y2) ? -1 : 1;
236
237    if(dx >= dy)
238    {
239        int dpr = dy << 1;
240        int dpru = dpr - (dx << 1);
241        int delta = dpr - dx;
242
243        for(; dx>=0; dx--)
244        {
245            _cucul_putchar32(c, x1, y1, s->ch);
246            if(delta > 0)
247            {
248                x1 += xinc;
249                y1 += yinc;
250                delta += dpru;
251            }
252            else
253            {
254                x1 += xinc;
255                delta += dpr;
256            }
257        }
258    }
259    else
260    {
261        int dpr = dx << 1;
262        int dpru = dpr - (dy << 1);
263        int delta = dpr - dy;
264
265        for(; dy >= 0; dy--)
266        {
267            _cucul_putchar32(c, x1, y1, s->ch);
268            if(delta > 0)
269            {
270                x1 += xinc;
271                y1 += yinc;
272                delta += dpru;
273            }
274            else
275            {
276                y1 += yinc;
277                delta += dpr;
278            }
279        }
280    }
281}
282
283/* Thin line drawing function, using Bresenham's mid-point line
284 * scan-conversion algorithm and ASCII art graphics. */
285static void draw_thin_line(cucul_canvas_t *c, struct line* s)
286{
287    uint32_t charmapx[2], charmapy[2];
288    int x1, y1, x2, y2;
289    int dx, dy;
290    int yinc;
291
292    if(s->x2 >= s->x1)
293    {
294        charmapx[0] = (s->y1 > s->y2) ? (uint32_t)',' : (uint32_t)'`';
295        charmapx[1] = (s->y1 > s->y2) ? (uint32_t)'\'' : (uint32_t)'.';
296        x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2;
297    }
298    else
299    {
300        charmapx[0] = (s->y1 > s->y2) ? (uint32_t)'`' : (uint32_t)'.';
301        charmapx[1] = (s->y1 > s->y2) ? (uint32_t)',' : (uint32_t)'\'';
302        x2 = s->x1; y2 = s->y1; x1 = s->x2; y1 = s->y2;
303    }
304
305    dx = abs(x2 - x1);
306    dy = abs(y2 - y1);
307
308    if(y1 > y2)
309    {
310        charmapy[0] = (uint32_t)',';
311        charmapy[1] = (uint32_t)'\'';
312        yinc = -1;
313    }
314    else
315    {
316        yinc = 1;
317        charmapy[0] = (uint32_t)'`';
318        charmapy[1] = (uint32_t)'.';
319    }
320
321    if(dx >= dy)
322    {
323        int dpr = dy << 1;
324        int dpru = dpr - (dx << 1);
325        int delta = dpr - dx;
326        int prev = 0;
327
328        for(; dx>=0; dx--)
329        {
330            if(delta > 0)
331            {
332                _cucul_putchar32(c, x1, y1, charmapy[1]);
333                x1++;
334                y1 += yinc;
335                delta += dpru;
336                prev = 1;
337            }
338            else
339            {
340                if(prev)
341                    _cucul_putchar32(c, x1, y1, charmapy[0]);
342                else
343                    _cucul_putchar32(c, x1, y1, (uint32_t)'-');
344                x1++;
345                delta += dpr;
346                prev = 0;
347            }
348        }
349    }
350    else
351    {
352        int dpr = dx << 1;
353        int dpru = dpr - (dy << 1);
354        int delta = dpr - dy;
355
356        for(; dy >= 0; dy--)
357        {
358            if(delta > 0)
359            {
360                _cucul_putchar32(c, x1, y1, charmapx[0]);
361                _cucul_putchar32(c, x1 + 1, y1, charmapx[1]);
362                x1++;
363                y1 += yinc;
364                delta += dpru;
365            }
366            else
367            {
368                _cucul_putchar32(c, x1, y1, (uint32_t)'|');
369                y1 += yinc;
370                delta += dpr;
371            }
372        }
373    }
374}
375
Note: See TracBrowser for help on using the repository browser.