1 | /* |
---|
2 | * libcaca Colour ASCII-Art library |
---|
3 | * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: box.c 3447 2009-05-14 00:18:13Z 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 "caca.h" |
---|
26 | #include "caca_internals.h" |
---|
27 | |
---|
28 | static int draw_box(caca_canvas_t *cv, int x, int y, int w, int h, |
---|
29 | uint32_t const *chars); |
---|
30 | |
---|
31 | /** \brief Draw a box on the canvas using the given character. |
---|
32 | * |
---|
33 | * This function never fails. |
---|
34 | * |
---|
35 | * \param cv The handle to the libcaca canvas. |
---|
36 | * \param x X coordinate of the upper-left corner of the box. |
---|
37 | * \param y Y coordinate of the upper-left corner of the box. |
---|
38 | * \param w Width of the box. |
---|
39 | * \param h Height of the box. |
---|
40 | * \param ch UTF-32 character to be used to draw the box. |
---|
41 | * \return This function always returns 0. |
---|
42 | */ |
---|
43 | int caca_draw_box(caca_canvas_t *cv, int x, int y, int w, int h, uint32_t ch) |
---|
44 | { |
---|
45 | int x2 = x + w - 1; |
---|
46 | int y2 = y + h - 1; |
---|
47 | |
---|
48 | caca_draw_line(cv, x, y, x, y2, ch); |
---|
49 | caca_draw_line(cv, x, y2, x2, y2, ch); |
---|
50 | caca_draw_line(cv, x2, y2, x2, y, ch); |
---|
51 | caca_draw_line(cv, x2, y, x, y, ch); |
---|
52 | |
---|
53 | return 0; |
---|
54 | } |
---|
55 | |
---|
56 | /** \brief Draw a thin box on the canvas. |
---|
57 | * |
---|
58 | * This function never fails. |
---|
59 | * |
---|
60 | * \param cv The handle to the libcaca canvas. |
---|
61 | * \param x X coordinate of the upper-left corner of the box. |
---|
62 | * \param y Y coordinate of the upper-left corner of the box. |
---|
63 | * \param w Width of the box. |
---|
64 | * \param h Height of the box. |
---|
65 | * \return This function always returns 0. |
---|
66 | */ |
---|
67 | int caca_draw_thin_box(caca_canvas_t *cv, int x, int y, int w, int h) |
---|
68 | { |
---|
69 | static uint32_t const ascii_chars[] = |
---|
70 | { |
---|
71 | '-', '|', ',', '`', '.', '\'' |
---|
72 | }; |
---|
73 | |
---|
74 | return draw_box(cv, x, y, w, h, ascii_chars); |
---|
75 | } |
---|
76 | |
---|
77 | /** \brief Draw a box on the canvas using CP437 characters. |
---|
78 | * |
---|
79 | * This function never fails. |
---|
80 | * |
---|
81 | * \param cv The handle to the libcaca canvas. |
---|
82 | * \param x X coordinate of the upper-left corner of the box. |
---|
83 | * \param y Y coordinate of the upper-left corner of the box. |
---|
84 | * \param w Width of the box. |
---|
85 | * \param h Height of the box. |
---|
86 | * \return This function always returns 0. |
---|
87 | */ |
---|
88 | int caca_draw_cp437_box(caca_canvas_t *cv, int x, int y, int w, int h) |
---|
89 | { |
---|
90 | static uint32_t const cp437_chars[] = |
---|
91 | { |
---|
92 | /* ─ │ ┌ └ ┐ ┘ */ |
---|
93 | 0x2500, 0x2502, 0x250c, 0x2514, 0x2510, 0x2518 |
---|
94 | }; |
---|
95 | |
---|
96 | return draw_box(cv, x, y, w, h, cp437_chars); |
---|
97 | } |
---|
98 | |
---|
99 | /** \brief Fill a box on the canvas using the given character. |
---|
100 | * |
---|
101 | * This function never fails. |
---|
102 | * |
---|
103 | * \param cv The handle to the libcaca canvas. |
---|
104 | * \param x X coordinate of the upper-left corner of the box. |
---|
105 | * \param y Y coordinate of the upper-left corner of the box. |
---|
106 | * \param w Width of the box. |
---|
107 | * \param h Height of the box. |
---|
108 | * \param ch UTF-32 character to be used to draw the box. |
---|
109 | * \return This function always returns 0. |
---|
110 | */ |
---|
111 | int caca_fill_box(caca_canvas_t *cv, int x, int y, int w, int h, |
---|
112 | uint32_t ch) |
---|
113 | { |
---|
114 | int i, j, xmax, ymax; |
---|
115 | |
---|
116 | int x2 = x + w - 1; |
---|
117 | int y2 = y + h - 1; |
---|
118 | |
---|
119 | if(x > x2) |
---|
120 | { |
---|
121 | int tmp = x; |
---|
122 | x = x2; x2 = tmp; |
---|
123 | } |
---|
124 | |
---|
125 | if(y > y2) |
---|
126 | { |
---|
127 | int tmp = y; |
---|
128 | y = y2; y2 = tmp; |
---|
129 | } |
---|
130 | |
---|
131 | xmax = cv->width - 1; |
---|
132 | ymax = cv->height - 1; |
---|
133 | |
---|
134 | if(x2 < 0 || y2 < 0 || x > xmax || y > ymax) |
---|
135 | return 0; |
---|
136 | |
---|
137 | if(x < 0) x = 0; |
---|
138 | if(y < 0) y = 0; |
---|
139 | if(x2 > xmax) x2 = xmax; |
---|
140 | if(y2 > ymax) y2 = ymax; |
---|
141 | |
---|
142 | for(j = y; j <= y2; j++) |
---|
143 | for(i = x; i <= x2; i++) |
---|
144 | caca_put_char(cv, i, j, ch); |
---|
145 | |
---|
146 | return 0; |
---|
147 | } |
---|
148 | |
---|
149 | /* |
---|
150 | * XXX: The following functions are local. |
---|
151 | */ |
---|
152 | |
---|
153 | static int draw_box(caca_canvas_t *cv, int x, int y, int w, int h, |
---|
154 | uint32_t const *chars) |
---|
155 | { |
---|
156 | int i, j, xmax, ymax; |
---|
157 | |
---|
158 | int x2 = x + w - 1; |
---|
159 | int y2 = y + h - 1; |
---|
160 | |
---|
161 | if(x > x2) |
---|
162 | { |
---|
163 | int tmp = x; |
---|
164 | x = x2; x2 = tmp; |
---|
165 | } |
---|
166 | |
---|
167 | if(y > y2) |
---|
168 | { |
---|
169 | int tmp = y; |
---|
170 | y = y2; y2 = tmp; |
---|
171 | } |
---|
172 | |
---|
173 | xmax = cv->width - 1; |
---|
174 | ymax = cv->height - 1; |
---|
175 | |
---|
176 | if(x2 < 0 || y2 < 0 || x > xmax || y > ymax) |
---|
177 | return 0; |
---|
178 | |
---|
179 | /* Draw edges */ |
---|
180 | if(y >= 0) |
---|
181 | for(i = x < 0 ? 1 : x + 1; i < x2 && i < xmax; i++) |
---|
182 | caca_put_char(cv, i, y, chars[0]); |
---|
183 | |
---|
184 | if(y2 <= ymax) |
---|
185 | for(i = x < 0 ? 1 : x + 1; i < x2 && i < xmax; i++) |
---|
186 | caca_put_char(cv, i, y2, chars[0]); |
---|
187 | |
---|
188 | if(x >= 0) |
---|
189 | for(j = y < 0 ? 1 : y + 1; j < y2 && j < ymax; j++) |
---|
190 | caca_put_char(cv, x, j, chars[1]); |
---|
191 | |
---|
192 | if(x2 <= xmax) |
---|
193 | for(j = y < 0 ? 1 : y + 1; j < y2 && j < ymax; j++) |
---|
194 | caca_put_char(cv, x2, j, chars[1]); |
---|
195 | |
---|
196 | /* Draw corners */ |
---|
197 | caca_put_char(cv, x, y, chars[2]); |
---|
198 | caca_put_char(cv, x, y2, chars[3]); |
---|
199 | caca_put_char(cv, x2, y, chars[4]); |
---|
200 | caca_put_char(cv, x2, y2, chars[5]); |
---|
201 | |
---|
202 | return 0; |
---|
203 | } |
---|
204 | |
---|
205 | /* |
---|
206 | * XXX: The following functions are aliases. |
---|
207 | */ |
---|
208 | |
---|
209 | int cucul_draw_box(cucul_canvas_t *, int, int, int, int, uint32_t) |
---|
210 | CACA_ALIAS(caca_draw_box); |
---|
211 | int cucul_draw_thin_box(cucul_canvas_t *, int, int, int, int) |
---|
212 | CACA_ALIAS(caca_draw_thin_box); |
---|
213 | int cucul_draw_cp437_box(cucul_canvas_t *, int, int, int, int) |
---|
214 | CACA_ALIAS(caca_draw_cp437_box); |
---|
215 | int cucul_fill_box(cucul_canvas_t *, int, int, int, int, uint32_t) |
---|
216 | CACA_ALIAS(caca_fill_box); |
---|
217 | |
---|