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

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