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

Last change on this file since 2306 was 2306, checked in by Sam Hocevar, 12 years ago
  • Documentation fixes (due to renamings, some parameters were no longer documented).
  • Property svn:keywords set to Id
File size: 5.6 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 2306 2008-04-19 19:40:25Z 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
21#if !defined(__KERNEL__)
22#   include <stdlib.h>
23#endif
24
25#include "cucul.h"
26#include "cucul_internals.h"
27
28/** \brief Draw a box on the canvas using the given character.
29 *
30 *  This function never fails.
31 *
32 *  \param cv The handle to the libcucul canvas.
33 *  \param x X coordinate of the upper-left corner of the box.
34 *  \param y Y coordinate of the upper-left corner of the box.
35 *  \param w Width of the box.
36 *  \param h Height of the box.
37 *  \param ch UTF-32 character to be used to draw the box.
38 *  \return This function always returns 0.
39 */
40int cucul_draw_box(cucul_canvas_t *cv, int x, int y, int w, int h, uint32_t ch)
41{
42    int x2 = x + w - 1;
43    int y2 = y + h - 1;
44
45    cucul_draw_line(cv,  x,  y,  x, y2, ch);
46    cucul_draw_line(cv,  x, y2, x2, y2, ch);
47    cucul_draw_line(cv, x2, y2, x2,  y, ch);
48    cucul_draw_line(cv, x2,  y,  x,  y, ch);
49
50    return 0;
51}
52
53/** \brief Draw a thin box on the canvas.
54 *
55 *  This function never fails.
56 *
57 *  \param cv The handle to the libcucul canvas.
58 *  \param x X coordinate of the upper-left corner of the box.
59 *  \param y Y coordinate of the upper-left corner of the box.
60 *  \param w Width of the box.
61 *  \param h Height of the box.
62 *  \return This function always returns 0.
63 */
64int cucul_draw_thin_box(cucul_canvas_t *cv, int x, int y, int w, int h)
65{
66    int i, j, xmax, ymax;
67
68    int x2 = x + w - 1;
69    int y2 = y + h - 1;
70
71    if(x > x2)
72    {
73        int tmp = x;
74        x = x2; x2 = tmp;
75    }
76
77    if(y > y2)
78    {
79        int tmp = y;
80        y = y2; y2 = tmp;
81    }
82
83    xmax = cv->width - 1;
84    ymax = cv->height - 1;
85
86    if(x2 < 0 || y2 < 0 || x > xmax || y > ymax)
87        return 0;
88
89    /* Draw edges */
90    if(y >= 0)
91        for(i = x < 0 ? 1 : x + 1; i < x2 && i < xmax; i++)
92            cucul_put_char(cv, i, y, '-');
93
94    if(y2 <= ymax)
95        for(i = x < 0 ? 1 : x + 1; i < x2 && i < xmax; i++)
96            cucul_put_char(cv, i, y2, '-');
97
98    if(x >= 0)
99        for(j = y < 0 ? 1 : y + 1; j < y2 && j < ymax; j++)
100            cucul_put_char(cv, x, j, '|');
101
102    if(x2 <= xmax)
103        for(j = y < 0 ? 1 : y + 1; j < y2 && j < ymax; j++)
104            cucul_put_char(cv, x2, j, '|');
105
106    /* Draw corners */
107    cucul_put_char(cv, x, y, ',');
108    cucul_put_char(cv, x, y2, '`');
109    cucul_put_char(cv, x2, y, '.');
110    cucul_put_char(cv, x2, y2, '\'');
111
112    return 0;
113}
114
115/** \brief Draw a box on the canvas using CP437 characters.
116 *
117 *  This function never fails.
118 *
119 *  \param cv The handle to the libcucul canvas.
120 *  \param x X coordinate of the upper-left corner of the box.
121 *  \param y Y coordinate of the upper-left corner of the box.
122 *  \param w Width of the box.
123 *  \param h Height of the box.
124 *  \return This function always returns 0.
125 */
126int cucul_draw_cp437_box(cucul_canvas_t *cv, int x, int y, int w, int h)
127{
128    int i, j, xmax, ymax;
129
130    int x2 = x + w - 1;
131    int y2 = y + h - 1;
132
133    if(x > x2)
134    {
135        int tmp = x;
136        x = x2; x2 = tmp;
137    }
138
139    if(y > y2)
140    {
141        int tmp = y;
142        y = y2; y2 = tmp;
143    }
144
145    xmax = cv->width - 1;
146    ymax = cv->height - 1;
147
148    if(x2 < 0 || y2 < 0 || x > xmax || y > ymax)
149        return 0;
150
151    /* Draw edges */
152    if(y >= 0)
153        for(i = x < 0 ? 1 : x + 1; i < x2 && i < xmax; i++)
154            cucul_put_char(cv, i, y, 0x2500); /* ─ */
155
156    if(y2 <= ymax)
157        for(i = x < 0 ? 1 : x + 1; i < x2 && i < xmax; i++)
158            cucul_put_char(cv, i, y2, 0x2500); /* ─ */
159
160    if(x >= 0)
161        for(j = y < 0 ? 1 : y + 1; j < y2 && j < ymax; j++)
162            cucul_put_char(cv, x, j, 0x2502); /* │ */
163
164    if(x2 <= xmax)
165        for(j = y < 0 ? 1 : y + 1; j < y2 && j < ymax; j++)
166            cucul_put_char(cv, x2, j, 0x2502); /* │ */
167
168    /* Draw corners */
169    cucul_put_char(cv, x, y, 0x250c); /* ┌ */
170    cucul_put_char(cv, x, y2, 0x2514); /* └ */
171    cucul_put_char(cv, x2, y, 0x2510); /* ┐ */
172    cucul_put_char(cv, x2, y2, 0x2518); /* ┘ */
173
174    return 0;
175}
176
177/** \brief Fill a box on the canvas using the given character.
178 *
179 *  This function never fails.
180 *
181 *  \param cv The handle to the libcucul canvas.
182 *  \param x X coordinate of the upper-left corner of the box.
183 *  \param y Y coordinate of the upper-left corner of the box.
184 *  \param w Width of the box.
185 *  \param h Height of the box.
186 *  \param ch UTF-32 character to be used to draw the box.
187 *  \return This function always returns 0.
188 */
189int cucul_fill_box(cucul_canvas_t *cv, int x, int y, int w, int h,
190                   uint32_t ch)
191{
192    int i, j, xmax, ymax;
193
194    int x2 = x + w - 1;
195    int y2 = y + h - 1;
196
197    if(x > x2)
198    {
199        int tmp = x;
200        x = x2; x2 = tmp;
201    }
202
203    if(y > y2)
204    {
205        int tmp = y;
206        y = y2; y2 = tmp;
207    }
208
209    xmax = cv->width - 1;
210    ymax = cv->height - 1;
211
212    if(x2 < 0 || y2 < 0 || x > xmax || y > ymax)
213        return 0;
214
215    if(x < 0) x = 0;
216    if(y < 0) y = 0;
217    if(x2 > xmax) x2 = xmax;
218    if(y2 > ymax) y2 = ymax;
219
220    for(j = y; j <= y2; j++)
221        for(i = x; i <= x2; i++)
222            cucul_put_char(cv, i, j, ch);
223
224    return 0;
225}
226
Note: See TracBrowser for help on using the repository browser.