source: libcaca/trunk/cucul/box.c @ 1462

Last change on this file since 1462 was 1462, checked in by Sam Hocevar, 13 years ago
  • Bwarf, typo in the no warranty clause.
  • Property svn:keywords set to Id
File size: 5.8 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: box.c 1462 2006-12-12 01:53:54Z sam $
7 *
8 *  This library is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 *  This file contains box drawing functions, both filled and outline.
17 */
18
19#include "config.h"
20#include "common.h"
21
22#if !defined(__KERNEL__)
23#   include <stdlib.h>
24#endif
25
26#include "cucul.h"
27#include "cucul_internals.h"
28
29/** \brief Draw a box on the canvas using the given character.
30 *
31 *  This function never fails.
32 *
33 *  \param cv The handle to the libcucul canvas.
34 *  \param x1 X coordinate of the upper-left corner of the box.
35 *  \param y1 Y coordinate of the upper-left corner of the box.
36 *  \param x2 X coordinate of the lower-right corner of the box.
37 *  \param y2 Y coordinate of the lower-right corner of the box.
38 *  \param ch UTF-32 character to be used to draw the box.
39 *  \return This function always returns 0.
40 */
41int cucul_draw_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
42                   unsigned long int ch)
43{
44    cucul_draw_line(cv, x1, y1, x1, y2, ch);
45    cucul_draw_line(cv, x1, y2, x2, y2, ch);
46    cucul_draw_line(cv, x2, y2, x2, y1, ch);
47    cucul_draw_line(cv, x2, y1, x1, y1, ch);
48
49    return 0;
50}
51
52/** \brief Draw a thin box on the canvas.
53 *
54 *  This function never fails.
55 *
56 *  \param cv The handle to the libcucul canvas.
57 *  \param x1 X coordinate of the upper-left corner of the box.
58 *  \param y1 Y coordinate of the upper-left corner of the box.
59 *  \param x2 X coordinate of the lower-right corner of the box.
60 *  \param y2 Y coordinate of the lower-right corner of the box.
61 *  \return This function always returns 0.
62 */
63int cucul_draw_thin_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2)
64{
65    int x, y, xmax, ymax;
66
67    if(x1 > x2)
68    {
69        int tmp = x1;
70        x1 = x2; x2 = tmp;
71    }
72
73    if(y1 > y2)
74    {
75        int tmp = y1;
76        y1 = y2; y2 = tmp;
77    }
78
79    xmax = cv->width - 1;
80    ymax = cv->height - 1;
81
82    if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
83        return 0;
84
85    /* Draw edges */
86    if(y1 >= 0)
87        for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
88            cucul_put_char(cv, x, y1, '-');
89
90    if(y2 <= ymax)
91        for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
92            cucul_put_char(cv, x, y2, '-');
93
94    if(x1 >= 0)
95        for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
96            cucul_put_char(cv, x1, y, '|');
97
98    if(x2 <= xmax)
99        for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
100            cucul_put_char(cv, x2, y, '|');
101
102    /* Draw corners */
103    cucul_put_char(cv, x1, y1, ',');
104    cucul_put_char(cv, x1, y2, '`');
105    cucul_put_char(cv, x2, y1, '.');
106    cucul_put_char(cv, x2, y2, '\'');
107
108    return 0;
109}
110
111/** \brief Draw a box on the canvas using CP437 characters.
112 *
113 *  This function never fails.
114 *
115 *  \param cv The handle to the libcucul canvas.
116 *  \param x1 X coordinate of the upper-left corner of the box.
117 *  \param y1 Y coordinate of the upper-left corner of the box.
118 *  \param x2 X coordinate of the lower-right corner of the box.
119 *  \param y2 Y coordinate of the lower-right corner of the box.
120 *  \return This function always returns 0.
121 */
122int cucul_draw_cp437_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2)
123{
124    int x, y, xmax, ymax;
125
126    if(x1 > x2)
127    {
128        int tmp = x1;
129        x1 = x2; x2 = tmp;
130    }
131
132    if(y1 > y2)
133    {
134        int tmp = y1;
135        y1 = y2; y2 = tmp;
136    }
137
138    xmax = cv->width - 1;
139    ymax = cv->height - 1;
140
141    if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
142        return 0;
143
144    /* Draw edges */
145    if(y1 >= 0)
146        for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
147            cucul_put_char(cv, x, y1, 0x2500); /* ─ */
148
149    if(y2 <= ymax)
150        for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
151            cucul_put_char(cv, x, y2, 0x2500); /* ─ */
152
153    if(x1 >= 0)
154        for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
155            cucul_put_char(cv, x1, y, 0x2502); /* │ */
156
157    if(x2 <= xmax)
158        for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
159            cucul_put_char(cv, x2, y, 0x2502); /* │ */
160
161    /* Draw corners */
162    cucul_put_char(cv, x1, y1, 0x250c); /* ┌ */
163    cucul_put_char(cv, x1, y2, 0x2514); /* └ */
164    cucul_put_char(cv, x2, y1, 0x2510); /* ┐ */
165    cucul_put_char(cv, x2, y2, 0x2518); /* ┘ */
166
167    return 0;
168}
169
170/** \brief Fill a box on the canvas using the given character.
171 *
172 *  This function never fails.
173 *
174 *  \param cv The handle to the libcucul canvas.
175 *  \param x1 X coordinate of the upper-left corner of the box.
176 *  \param y1 Y coordinate of the upper-left corner of the box.
177 *  \param x2 X coordinate of the lower-right corner of the box.
178 *  \param y2 Y coordinate of the lower-right corner of the box.
179 *  \param ch UTF-32 character to be used to draw the box.
180 *  \return This function always returns 0.
181 */
182int cucul_fill_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
183                   unsigned long int ch)
184{
185    int x, y, xmax, ymax;
186
187    if(x1 > x2)
188    {
189        int tmp = x1;
190        x1 = x2; x2 = tmp;
191    }
192
193    if(y1 > y2)
194    {
195        int tmp = y1;
196        y1 = y2; y2 = tmp;
197    }
198
199    xmax = cv->width - 1;
200    ymax = cv->height - 1;
201
202    if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
203        return 0;
204
205    if(x1 < 0) x1 = 0;
206    if(y1 < 0) y1 = 0;
207    if(x2 > xmax) x2 = xmax;
208    if(y2 > ymax) y2 = ymax;
209
210    for(y = y1; y <= y2; y++)
211        for(x = x1; x <= x2; x++)
212            cucul_put_char(cv, x, y, ch);
213
214    return 0;
215}
216
Note: See TracBrowser for help on using the repository browser.