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

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