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

Last change on this file since 678 was 678, checked in by Sam Hocevar, 14 years ago
  • Made cucul_putchar32() an internal-only function.
  • Changed the line, box, ellipsis etc. prototypes so that they use an UTF-8 string instead of a single character as their last argument.
  • 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 *  This library is free software; you can redistribute it and/or
7 *  modify it under the terms of the Do What The Fuck You Want To
8 *  Public License, Version 2, as published by Sam Hocevar. See
9 *  http://sam.zoy.org/wtfpl/COPYING for more details.
10 */
11
12/** \file line.c
13 *  \version \$Id: line.c 678 2006-03-23 14:07:32Z sam $
14 *  \author Sam Hocevar <sam@zoy.org>
15 *  \brief Line drawing
16 *
17 *  This file contains line and polyline drawing functions, with both thin
18 *  and thick styles.
19 */
20
21#include "config.h"
22
23#if !defined(__KERNEL__)
24#   include <stdlib.h>
25#endif
26
27#include "cucul.h"
28#include "cucul_internals.h"
29
30#if !defined(_DOXYGEN_SKIP_ME)
31struct line
32{
33    int x1, y1;
34    int x2, y2;
35    uint32_t c;
36    void (*draw) (cucul_t *, struct line*);
37};
38#endif
39
40static void clip_line(cucul_t*, struct line*);
41static uint8_t clip_bits(cucul_t*, int, int);
42static void draw_solid_line(cucul_t*, struct line*);
43static void draw_thin_line(cucul_t*, struct line*);
44
45/**
46 * \brief Draw a line on the screen using the given character.
47 *
48 * \param x1 X coordinate of the first point.
49 * \param y1 Y coordinate of the first point.
50 * \param x2 X coordinate of the second point.
51 * \param y2 Y coordinate of the second point.
52 * \param str UTF-8 string containing the character to use to draw the line.
53 * \return void
54 */
55void cucul_draw_line(cucul_t *qq, int x1, int y1, int x2, int y2,
56                     char const *str)
57{
58    struct line s;
59    s.x1 = x1;
60    s.y1 = y1;
61    s.x2 = x2;
62    s.y2 = y2;
63    s.c = _cucul_utf8_to_utf32(str);
64    s.draw = draw_solid_line;
65    clip_line(qq, &s);
66}
67
68/**
69 * \brief Draw a polyline on the screen using the given character and
70 *       coordinate arrays. The first and last points are not connected,
71 *       so in order to draw a polygon you need to specify the starting
72 *       point at the end of the list as well.
73 *
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_t *qq, int const x[], int const y[], int n,
81                         char const *str)
82{
83    int i;
84    struct line s;
85    s.c = _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(qq, &s);
95    }
96}
97
98/**
99 * \brief Draw a thin line on the screen, using ASCII art.
100 *
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_t *qq, 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(qq, &s);
116}
117
118/**
119 * \brief Draw a thin polyline on the screen using the given coordinate
120 *       arrays and with ASCII art. The first and last points are not
121 *       connected, so in order to draw a polygon you need to specify the
122 *       starting point at the end of the list as well.
123 *
124 * \param x Array of X coordinates. Must have \p n + 1 elements.
125 * \param y Array of Y coordinates. Must have \p n + 1 elements.
126 * \param n Number of lines to draw.
127 * \return void
128 */
129void cucul_draw_thin_polyline(cucul_t *qq, int const x[], int const y[], int n)
130{
131    int i;
132    struct line s;
133    s.draw = draw_thin_line;
134
135    for(i = 0; i < n; i++)
136    {
137        s.x1 = x[i];
138        s.y1 = y[i];
139        s.x2 = x[i+1];
140        s.y2 = y[i+1];
141        clip_line(qq, &s);
142    }
143}
144
145/*
146 * XXX: The following functions are local.
147 */
148
149/**
150 * \brief Generic Cohen-Sutherland line clipping function.
151 *
152 * \param s a line structure
153 * \return void
154 */
155static void clip_line(cucul_t *qq, struct line* s)
156{
157    uint8_t bits1, bits2;
158
159    bits1 = clip_bits(qq, s->x1, s->y1);
160    bits2 = clip_bits(qq, s->x2, s->y2);
161
162    if(bits1 & bits2)
163        return;
164
165    if(bits1 == 0)
166    {
167        if(bits2 == 0)
168            s->draw(qq, s);
169        else
170        {
171            int tmp;
172            tmp = s->x1; s->x1 = s->x2; s->x2 = tmp;
173            tmp = s->y1; s->y1 = s->y2; s->y2 = tmp;
174            clip_line(qq, s);
175        }
176
177        return;
178    }
179
180    if(bits1 & (1<<0))
181    {
182        s->y1 = s->y2 - (s->x2 - 0) * (s->y2 - s->y1) / (s->x2 - s->x1);
183        s->x1 = 0;
184    }
185    else if(bits1 & (1<<1))
186    {
187        int xmax = qq->width - 1;
188        s->y1 = s->y2 - (s->x2 - xmax) * (s->y2 - s->y1) / (s->x2 - s->x1);
189        s->x1 = xmax;
190    }
191    else if(bits1 & (1<<2))
192    {
193        s->x1 = s->x2 - (s->y2 - 0) * (s->x2 - s->x1) / (s->y2 - s->y1);
194        s->y1 = 0;
195    }
196    else if(bits1 & (1<<3))
197    {
198        int ymax = qq->height - 1;
199        s->x1 = s->x2 - (s->y2 - ymax) * (s->x2 - s->x1) / (s->y2 - s->y1);
200        s->y1 = ymax;
201    }
202
203    clip_line(qq, s);
204}
205
206/**
207 * \brief Helper function for clip_line().
208 *
209 * \param x X coordinate of the point.
210 * \param y Y coordinate of the point.
211 * \return The clipping bits for the given point.
212 */
213static uint8_t clip_bits(cucul_t *qq, int x, int y)
214{
215    uint8_t b = 0;
216
217    if(x < 0)
218        b |= (1<<0);
219    else if(x >= (int)qq->width)
220        b |= (1<<1);
221
222    if(y < 0)
223        b |= (1<<2);
224    else if(y >= (int)qq->height)
225        b |= (1<<3);
226
227    return b;
228}
229
230/**
231 * \brief Solid line drawing function, using Bresenham's mid-point line
232 *       scan-conversion algorithm.
233 *
234 * \param s a line structure
235 * \return void
236 */
237static void draw_solid_line(cucul_t *qq, struct line* s)
238{
239    int x1, y1, x2, y2;
240    int dx, dy;
241    int xinc, yinc;
242
243    x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2;
244
245    dx = abs(x2 - x1);
246    dy = abs(y2 - y1);
247
248    xinc = (x1 > x2) ? -1 : 1;
249    yinc = (y1 > y2) ? -1 : 1;
250
251    if(dx >= dy)
252    {
253        int dpr = dy << 1;
254        int dpru = dpr - (dx << 1);
255        int delta = dpr - dx;
256
257        for(; dx>=0; dx--)
258        {
259            _cucul_putchar32(qq, x1, y1, s->c);
260            if(delta > 0)
261            {
262                x1 += xinc;
263                y1 += yinc;
264                delta += dpru;
265            }
266            else
267            {
268                x1 += xinc;
269                delta += dpr;
270            }
271        }
272    }
273    else
274    {
275        int dpr = dx << 1;
276        int dpru = dpr - (dy << 1);
277        int delta = dpr - dy;
278
279        for(; dy >= 0; dy--)
280        {
281            _cucul_putchar32(qq, x1, y1, s->c);
282            if(delta > 0)
283            {
284                x1 += xinc;
285                y1 += yinc;
286                delta += dpru;
287            }
288            else
289            {
290                y1 += yinc;
291                delta += dpr;
292            }
293        }
294    }
295}
296
297/**
298 * \brief Thin line drawing function, using Bresenham's mid-point line
299 *        scan-conversion algorithm and ASCII art graphics.
300 *
301 * \param s a line structure
302 * \return void
303 */
304static void draw_thin_line(cucul_t *qq, struct line* s)
305{
306    uint32_t charmapx[2], charmapy[2];
307    int x1, y1, x2, y2;
308    int dx, dy;
309    int yinc;
310
311    if(s->x2 >= s->x1)
312    {
313        charmapx[0] = (s->y1 > s->y2) ? (uint32_t)',' : (uint32_t)'`';
314        charmapx[1] = (s->y1 > s->y2) ? (uint32_t)'\'' : (uint32_t)'.';
315        x1 = s->x1; y1 = s->y1; x2 = s->x2; y2 = s->y2;
316    }
317    else
318    {
319        charmapx[0] = (s->y1 > s->y2) ? (uint32_t)'`' : (uint32_t)'.';
320        charmapx[1] = (s->y1 > s->y2) ? (uint32_t)',' : (uint32_t)'\'';
321        x2 = s->x1; y2 = s->y1; x1 = s->x2; y1 = s->y2;
322    }
323
324    dx = abs(x2 - x1);
325    dy = abs(y2 - y1);
326
327    if(y1 > y2)
328    {
329        charmapy[0] = (uint32_t)',';
330        charmapy[1] = (uint32_t)'\'';
331        yinc = -1;
332    }
333    else
334    {
335        yinc = 1;
336        charmapy[0] = (uint32_t)'`';
337        charmapy[1] = (uint32_t)'.';
338    }
339
340    if(dx >= dy)
341    {
342        int dpr = dy << 1;
343        int dpru = dpr - (dx << 1);
344        int delta = dpr - dx;
345        int prev = 0;
346
347        for(; dx>=0; dx--)
348        {
349            if(delta > 0)
350            {
351                _cucul_putchar32(qq, x1, y1, charmapy[1]);
352                x1++;
353                y1 += yinc;
354                delta += dpru;
355                prev = 1;
356            }
357            else
358            {
359                if(prev)
360                    _cucul_putchar32(qq, x1, y1, charmapy[0]);
361                else
362                    _cucul_putchar32(qq, x1, y1, (uint32_t)'-');
363                x1++;
364                delta += dpr;
365                prev = 0;
366            }
367        }
368    }
369    else
370    {
371        int dpr = dx << 1;
372        int dpru = dpr - (dy << 1);
373        int delta = dpr - dy;
374
375        for(; dy >= 0; dy--)
376        {
377            if(delta > 0)
378            {
379                _cucul_putchar32(qq, x1, y1, charmapx[0]);
380                _cucul_putchar32(qq, x1 + 1, y1, charmapx[1]);
381                x1++;
382                y1 += yinc;
383                delta += dpru;
384            }
385            else
386            {
387                _cucul_putchar32(qq, x1, y1, (uint32_t)'|');
388                y1 += yinc;
389                delta += dpr;
390            }
391        }
392    }
393}
394
Note: See TracBrowser for help on using the repository browser.