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

Last change on this file since 769 was 769, checked in by Sam Hocevar, 14 years ago
  • Removed \file directives from all files except caca.h and cucul.h, to remove redundencies in the Doxygen documentation.
  • Property svn:keywords set to Id
File size: 9.1 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 769 2006-04-14 07:30:53Z 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 c;
34    void (*draw) (cucul_t *, struct line*);
35};
36#endif
37
38static void clip_line(cucul_t*, struct line*);
39static uint8_t clip_bits(cucul_t*, int, int);
40static void draw_solid_line(cucul_t*, struct line*);
41static void draw_thin_line(cucul_t*, struct line*);
42
43/**
44 * \brief Draw a line on the screen using the given character.
45 *
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_t *qq, 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.c = _cucul_utf8_to_utf32(str);
62    s.draw = draw_solid_line;
63    clip_line(qq, &s);
64}
65
66/**
67 * \brief Draw a polyline on the screen using the given character and
68 *       coordinate arrays. The first and last points are not connected,
69 *       so in order to draw a polygon you need to specify the starting
70 *       point at the end of the list as well.
71 *
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 void
77 */
78void cucul_draw_polyline(cucul_t *qq, int const x[], int const y[], int n,
79                         char const *str)
80{
81    int i;
82    struct line s;
83    s.c = _cucul_utf8_to_utf32(str);
84    s.draw = draw_solid_line;
85
86    for(i = 0; i < n; i++)
87    {
88        s.x1 = x[i];
89        s.y1 = y[i];
90        s.x2 = x[i+1];
91        s.y2 = y[i+1];
92        clip_line(qq, &s);
93    }
94}
95
96/**
97 * \brief Draw a thin line on the screen, using ASCII art.
98 *
99 * \param x1 X coordinate of the first point.
100 * \param y1 Y coordinate of the first point.
101 * \param x2 X coordinate of the second point.
102 * \param y2 Y coordinate of the second point.
103 * \return void
104 */
105void cucul_draw_thin_line(cucul_t *qq, int x1, int y1, int x2, int y2)
106{
107    struct line s;
108    s.x1 = x1;
109    s.y1 = y1;
110    s.x2 = x2;
111    s.y2 = y2;
112    s.draw = draw_thin_line;
113    clip_line(qq, &s);
114}
115
116/**
117 * \brief Draw a thin polyline on the screen using the given coordinate
118 *       arrays and with ASCII art. The first and last points are not
119 *       connected, so in order to draw a polygon you need to specify the
120 *       starting point at the end of the list as well.
121 *
122 * \param x Array of X coordinates. Must have \p n + 1 elements.
123 * \param y Array of Y coordinates. Must have \p n + 1 elements.
124 * \param n Number of lines to draw.
125 * \return void
126 */
127void cucul_draw_thin_polyline(cucul_t *qq, int const x[], int const y[], int n)
128{
129    int i;
130    struct line s;
131    s.draw = draw_thin_line;
132
133    for(i = 0; i < n; i++)
134    {
135        s.x1 = x[i];
136        s.y1 = y[i];
137        s.x2 = x[i+1];
138        s.y2 = y[i+1];
139        clip_line(qq, &s);
140    }
141}
142
143/*
144 * XXX: The following functions are local.
145 */
146
147/**
148 * \brief Generic Cohen-Sutherland line clipping function.
149 *
150 * \param s a line structure
151 * \return void
152 */
153static void clip_line(cucul_t *qq, struct line* s)
154{
155    uint8_t bits1, bits2;
156
157    bits1 = clip_bits(qq, s->x1, s->y1);
158    bits2 = clip_bits(qq, s->x2, s->y2);
159
160    if(bits1 & bits2)
161        return;
162
163    if(bits1 == 0)
164    {
165        if(bits2 == 0)
166            s->draw(qq, 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(qq, 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 = qq->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 = qq->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(qq, s);
202}
203
204/**
205 * \brief Helper function for clip_line().
206 *
207 * \param x X coordinate of the point.
208 * \param y Y coordinate of the point.
209 * \return The clipping bits for the given point.
210 */
211static uint8_t clip_bits(cucul_t *qq, int x, int y)
212{
213    uint8_t b = 0;
214
215    if(x < 0)
216        b |= (1<<0);
217    else if(x >= (int)qq->width)
218        b |= (1<<1);
219
220    if(y < 0)
221        b |= (1<<2);
222    else if(y >= (int)qq->height)
223        b |= (1<<3);
224
225    return b;
226}
227
228/**
229 * \brief Solid line drawing function, using Bresenham's mid-point line
230 *       scan-conversion algorithm.
231 *
232 * \param s a line structure
233 * \return void
234 */
235static void draw_solid_line(cucul_t *qq, struct line* s)
236{
237    int x1, y1, x2, y2;
238    int dx, dy;
239    int xinc, yinc;
240
241    x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2;
242
243    dx = abs(x2 - x1);
244    dy = abs(y2 - y1);
245
246    xinc = (x1 > x2) ? -1 : 1;
247    yinc = (y1 > y2) ? -1 : 1;
248
249    if(dx >= dy)
250    {
251        int dpr = dy << 1;
252        int dpru = dpr - (dx << 1);
253        int delta = dpr - dx;
254
255        for(; dx>=0; dx--)
256        {
257            _cucul_putchar32(qq, x1, y1, s->c);
258            if(delta > 0)
259            {
260                x1 += xinc;
261                y1 += yinc;
262                delta += dpru;
263            }
264            else
265            {
266                x1 += xinc;
267                delta += dpr;
268            }
269        }
270    }
271    else
272    {
273        int dpr = dx << 1;
274        int dpru = dpr - (dy << 1);
275        int delta = dpr - dy;
276
277        for(; dy >= 0; dy--)
278        {
279            _cucul_putchar32(qq, x1, y1, s->c);
280            if(delta > 0)
281            {
282                x1 += xinc;
283                y1 += yinc;
284                delta += dpru;
285            }
286            else
287            {
288                y1 += yinc;
289                delta += dpr;
290            }
291        }
292    }
293}
294
295/**
296 * \brief Thin line drawing function, using Bresenham's mid-point line
297 *        scan-conversion algorithm and ASCII art graphics.
298 *
299 * \param s a line structure
300 * \return void
301 */
302static void draw_thin_line(cucul_t *qq, struct line* s)
303{
304    uint32_t charmapx[2], charmapy[2];
305    int x1, y1, x2, y2;
306    int dx, dy;
307    int yinc;
308
309    if(s->x2 >= s->x1)
310    {
311        charmapx[0] = (s->y1 > s->y2) ? (uint32_t)',' : (uint32_t)'`';
312        charmapx[1] = (s->y1 > s->y2) ? (uint32_t)'\'' : (uint32_t)'.';
313        x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2;
314    }
315    else
316    {
317        charmapx[0] = (s->y1 > s->y2) ? (uint32_t)'`' : (uint32_t)'.';
318        charmapx[1] = (s->y1 > s->y2) ? (uint32_t)',' : (uint32_t)'\'';
319        x2 = s->x1; y2 = s->y1; x1 = s->x2; y1 = s->y2;
320    }
321
322    dx = abs(x2 - x1);
323    dy = abs(y2 - y1);
324
325    if(y1 > y2)
326    {
327        charmapy[0] = (uint32_t)',';
328        charmapy[1] = (uint32_t)'\'';
329        yinc = -1;
330    }
331    else
332    {
333        yinc = 1;
334        charmapy[0] = (uint32_t)'`';
335        charmapy[1] = (uint32_t)'.';
336    }
337
338    if(dx >= dy)
339    {
340        int dpr = dy << 1;
341        int dpru = dpr - (dx << 1);
342        int delta = dpr - dx;
343        int prev = 0;
344
345        for(; dx>=0; dx--)
346        {
347            if(delta > 0)
348            {
349                _cucul_putchar32(qq, x1, y1, charmapy[1]);
350                x1++;
351                y1 += yinc;
352                delta += dpru;
353                prev = 1;
354            }
355            else
356            {
357                if(prev)
358                    _cucul_putchar32(qq, x1, y1, charmapy[0]);
359                else
360                    _cucul_putchar32(qq, x1, y1, (uint32_t)'-');
361                x1++;
362                delta += dpr;
363                prev = 0;
364            }
365        }
366    }
367    else
368    {
369        int dpr = dx << 1;
370        int dpru = dpr - (dy << 1);
371        int delta = dpr - dy;
372
373        for(; dy >= 0; dy--)
374        {
375            if(delta > 0)
376            {
377                _cucul_putchar32(qq, x1, y1, charmapx[0]);
378                _cucul_putchar32(qq, x1 + 1, y1, charmapx[1]);
379                x1++;
380                y1 += yinc;
381                delta += dpru;
382            }
383            else
384            {
385                _cucul_putchar32(qq, x1, y1, (uint32_t)'|');
386                y1 += yinc;
387                delta += dpr;
388            }
389        }
390    }
391}
392
Note: See TracBrowser for help on using the repository browser.