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 1329 2006-11-11 07:56:30Z sam $ |
---|
7 | * |
---|
8 | * This library is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the Do What The Fuck You Want To |
---|
10 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
11 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
12 | */ |
---|
13 | |
---|
14 | /* |
---|
15 | * This file contains box drawing functions, both filled and outline. |
---|
16 | */ |
---|
17 | |
---|
18 | #include "config.h" |
---|
19 | #include "common.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 x1 X coordinate of the upper-left corner of the box. |
---|
34 | * \param y1 Y coordinate of the upper-left corner of the box. |
---|
35 | * \param x2 X coordinate of the lower-right corner of the box. |
---|
36 | * \param y2 Y coordinate of the lower-right corner of the box. |
---|
37 | * \param str UTF-8 string containing the character to use to draw the box. |
---|
38 | * \return This function always returns 0. |
---|
39 | */ |
---|
40 | int cucul_draw_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2, |
---|
41 | char const *str) |
---|
42 | { |
---|
43 | cucul_draw_line(cv, x1, y1, x1, y2, str); |
---|
44 | cucul_draw_line(cv, x1, y2, x2, y2, str); |
---|
45 | cucul_draw_line(cv, x2, y2, x2, y1, str); |
---|
46 | cucul_draw_line(cv, x2, y1, x1, y1, str); |
---|
47 | |
---|
48 | return 0; |
---|
49 | } |
---|
50 | |
---|
51 | /** \brief Draw a thin box on the canvas. |
---|
52 | * |
---|
53 | * This function never fails. |
---|
54 | * |
---|
55 | * \param cv The handle to the libcucul canvas. |
---|
56 | * \param x1 X coordinate of the upper-left corner of the box. |
---|
57 | * \param y1 Y coordinate of the upper-left corner of the box. |
---|
58 | * \param x2 X coordinate of the lower-right corner of the box. |
---|
59 | * \param y2 Y coordinate of the lower-right corner of the box. |
---|
60 | * \return This function always returns 0. |
---|
61 | */ |
---|
62 | int cucul_draw_thin_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2) |
---|
63 | { |
---|
64 | int x, y, xmax, ymax; |
---|
65 | |
---|
66 | if(x1 > x2) |
---|
67 | { |
---|
68 | int tmp = x1; |
---|
69 | x1 = x2; x2 = tmp; |
---|
70 | } |
---|
71 | |
---|
72 | if(y1 > y2) |
---|
73 | { |
---|
74 | int tmp = y1; |
---|
75 | y1 = y2; y2 = tmp; |
---|
76 | } |
---|
77 | |
---|
78 | xmax = cv->width - 1; |
---|
79 | ymax = cv->height - 1; |
---|
80 | |
---|
81 | if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax) |
---|
82 | return 0; |
---|
83 | |
---|
84 | /* Draw edges */ |
---|
85 | if(y1 >= 0) |
---|
86 | for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++) |
---|
87 | cucul_putchar(cv, x, y1, '-'); |
---|
88 | |
---|
89 | if(y2 <= ymax) |
---|
90 | for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++) |
---|
91 | cucul_putchar(cv, x, y2, '-'); |
---|
92 | |
---|
93 | if(x1 >= 0) |
---|
94 | for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++) |
---|
95 | cucul_putchar(cv, x1, y, '|'); |
---|
96 | |
---|
97 | if(x2 <= xmax) |
---|
98 | for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++) |
---|
99 | cucul_putchar(cv, x2, y, '|'); |
---|
100 | |
---|
101 | /* Draw corners */ |
---|
102 | cucul_putchar(cv, x1, y1, ','); |
---|
103 | cucul_putchar(cv, x1, y2, '`'); |
---|
104 | cucul_putchar(cv, x2, y1, '.'); |
---|
105 | cucul_putchar(cv, x2, y2, '\''); |
---|
106 | |
---|
107 | return 0; |
---|
108 | } |
---|
109 | |
---|
110 | /** \brief Draw a box on the canvas using CP437 characters. |
---|
111 | * |
---|
112 | * This function never fails. |
---|
113 | * |
---|
114 | * \param cv The handle to the libcucul canvas. |
---|
115 | * \param x1 X coordinate of the upper-left corner of the box. |
---|
116 | * \param y1 Y coordinate of the upper-left corner of the box. |
---|
117 | * \param x2 X coordinate of the lower-right corner of the box. |
---|
118 | * \param y2 Y coordinate of the lower-right corner of the box. |
---|
119 | * \return This function always returns 0. |
---|
120 | */ |
---|
121 | int cucul_draw_cp437_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2) |
---|
122 | { |
---|
123 | int x, y, xmax, ymax; |
---|
124 | |
---|
125 | if(x1 > x2) |
---|
126 | { |
---|
127 | int tmp = x1; |
---|
128 | x1 = x2; x2 = tmp; |
---|
129 | } |
---|
130 | |
---|
131 | if(y1 > y2) |
---|
132 | { |
---|
133 | int tmp = y1; |
---|
134 | y1 = y2; y2 = tmp; |
---|
135 | } |
---|
136 | |
---|
137 | xmax = cv->width - 1; |
---|
138 | ymax = cv->height - 1; |
---|
139 | |
---|
140 | if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax) |
---|
141 | return 0; |
---|
142 | |
---|
143 | /* Draw edges */ |
---|
144 | if(y1 >= 0) |
---|
145 | for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++) |
---|
146 | cucul_putchar(cv, x, y1, 0x2500); /* ─ */ |
---|
147 | |
---|
148 | if(y2 <= ymax) |
---|
149 | for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++) |
---|
150 | cucul_putchar(cv, x, y2, 0x2500); /* ─ */ |
---|
151 | |
---|
152 | if(x1 >= 0) |
---|
153 | for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++) |
---|
154 | cucul_putchar(cv, x1, y, 0x2502); /* │ */ |
---|
155 | |
---|
156 | if(x2 <= xmax) |
---|
157 | for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++) |
---|
158 | cucul_putchar(cv, x2, y, 0x2502); /* │ */ |
---|
159 | |
---|
160 | /* Draw corners */ |
---|
161 | cucul_putchar(cv, x1, y1, 0x250c); /* ┌ */ |
---|
162 | cucul_putchar(cv, x1, y2, 0x2514); /* └ */ |
---|
163 | cucul_putchar(cv, x2, y1, 0x2510); /* ┐ */ |
---|
164 | cucul_putchar(cv, x2, y2, 0x2518); /* ┘ */ |
---|
165 | |
---|
166 | return 0; |
---|
167 | } |
---|
168 | |
---|
169 | /** \brief Fill a box on the canvas using the given character. |
---|
170 | * |
---|
171 | * This function never fails. |
---|
172 | * |
---|
173 | * \param cv The handle to the libcucul canvas. |
---|
174 | * \param x1 X coordinate of the upper-left corner of the box. |
---|
175 | * \param y1 Y coordinate of the upper-left corner of the box. |
---|
176 | * \param x2 X coordinate of the lower-right corner of the box. |
---|
177 | * \param y2 Y coordinate of the lower-right corner of the box. |
---|
178 | * \param str UTF-8 string containing the character to fill the box with. |
---|
179 | * \return This function always returns 0. |
---|
180 | */ |
---|
181 | int cucul_fill_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2, |
---|
182 | char const *str) |
---|
183 | { |
---|
184 | int x, y, xmax, ymax; |
---|
185 | uint32_t ch; |
---|
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 | ch = cucul_utf8_to_utf32(str, NULL); |
---|
211 | |
---|
212 | for(y = y1; y <= y2; y++) |
---|
213 | for(x = x1; x <= x2; x++) |
---|
214 | cucul_putchar(cv, x, y, ch); |
---|
215 | |
---|
216 | return 0; |
---|
217 | } |
---|
218 | |
---|