"Fossies" - the Fresh Open Source Software Archive 
Member "libcaca-0.99.beta20/caca/box.c" (22 May 2018, 5538 Bytes) of package /linux/privat/libcaca-0.99.beta20.tar.bz2:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /*
2 * libcaca Colour ASCII-Art library
3 * Copyright © 2002—2018 Sam Hocevar <sam@hocevar.net>
4 * All Rights Reserved
5 *
6 * This library is free software. It comes without any warranty, to
7 * the extent permitted by applicable law. You can redistribute it
8 * and/or modify it under the terms of the Do What the Fuck You Want
9 * to Public License, Version 2, as published by Sam Hocevar. See
10 * http://www.wtfpl.net/ for more details.
11 */
12
13 /*
14 * This file contains box drawing functions, both filled and outline.
15 */
16
17 #include "config.h"
18
19 #if !defined(__KERNEL__)
20 # include <stdlib.h>
21 #endif
22
23 #include "caca.h"
24 #include "caca_internals.h"
25
26 static int draw_box(caca_canvas_t *cv, int x, int y, int w, int h,
27 uint32_t const *chars);
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 libcaca 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 */
41 int caca_draw_box(caca_canvas_t *cv, int x, int y, int w, int h, uint32_t ch)
42 {
43 int x2 = x + w - 1;
44 int y2 = y + h - 1;
45
46 caca_draw_line(cv, x, y, x, y2, ch);
47 caca_draw_line(cv, x, y2, x2, y2, ch);
48 caca_draw_line(cv, x2, y2, x2, y, ch);
49 caca_draw_line(cv, x2, y, x, y, 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 libcaca 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 */
65 int caca_draw_thin_box(caca_canvas_t *cv, int x, int y, int w, int h)
66 {
67 static uint32_t const ascii_chars[] =
68 {
69 '-', '|', ',', '`', '.', '\''
70 };
71
72 return draw_box(cv, x, y, w, h, ascii_chars);
73 }
74
75 /** \brief Draw a box on the canvas using CP437 characters.
76 *
77 * This function never fails.
78 *
79 * \param cv The handle to the libcaca canvas.
80 * \param x X coordinate of the upper-left corner of the box.
81 * \param y Y coordinate of the upper-left corner of the box.
82 * \param w Width of the box.
83 * \param h Height of the box.
84 * \return This function always returns 0.
85 */
86 int caca_draw_cp437_box(caca_canvas_t *cv, int x, int y, int w, int h)
87 {
88 static uint32_t const cp437_chars[] =
89 {
90 /* ─ │ ┌ └ ┐ ┘ */
91 0x2500, 0x2502, 0x250c, 0x2514, 0x2510, 0x2518
92 };
93
94 return draw_box(cv, x, y, w, h, cp437_chars);
95 }
96
97 /** \brief Fill a box on the canvas using the given character.
98 *
99 * This function never fails.
100 *
101 * \param cv The handle to the libcaca canvas.
102 * \param x X coordinate of the upper-left corner of the box.
103 * \param y Y coordinate of the upper-left corner of the box.
104 * \param w Width of the box.
105 * \param h Height of the box.
106 * \param ch UTF-32 character to be used to draw the box.
107 * \return This function always returns 0.
108 */
109 int caca_fill_box(caca_canvas_t *cv, int x, int y, int w, int h,
110 uint32_t ch)
111 {
112 int i, j, xmax, ymax;
113
114 int x2 = x + w - 1;
115 int y2 = y + h - 1;
116
117 if(x > x2)
118 {
119 int tmp = x;
120 x = x2; x2 = tmp;
121 }
122
123 if(y > y2)
124 {
125 int tmp = y;
126 y = y2; y2 = tmp;
127 }
128
129 xmax = cv->width - 1;
130 ymax = cv->height - 1;
131
132 if(x2 < 0 || y2 < 0 || x > xmax || y > ymax)
133 return 0;
134
135 if(x < 0) x = 0;
136 if(y < 0) y = 0;
137 if(x2 > xmax) x2 = xmax;
138 if(y2 > ymax) y2 = ymax;
139
140 #if 0
141 /* FIXME: this fails with fullwidth character blits. Also, the dirty
142 * rectangle handling may miss fullwidth cells. */
143 /* Optimise dirty rectangle handling, part 1 */
144 cv->dirty_disabled++;
145 #endif
146
147 for(j = y; j <= y2; j++)
148 for(i = x; i <= x2; i++)
149 caca_put_char(cv, i, j, ch);
150
151 #if 0
152 /* Optimise dirty rectangle handling, part 2 */
153 cv->dirty_disabled--;
154 if(!cv->dirty_disabled)
155 caca_add_dirty_rect(cv, x, y, x2 - x + 1, y2 - y + 1);
156 #endif
157
158 return 0;
159 }
160
161 /*
162 * XXX: The following functions are local.
163 */
164
165 static int draw_box(caca_canvas_t *cv, int x, int y, int w, int h,
166 uint32_t const *chars)
167 {
168 int i, j, xmax, ymax;
169
170 int x2 = x + w - 1;
171 int y2 = y + h - 1;
172
173 if(x > x2)
174 {
175 int tmp = x;
176 x = x2; x2 = tmp;
177 }
178
179 if(y > y2)
180 {
181 int tmp = y;
182 y = y2; y2 = tmp;
183 }
184
185 xmax = cv->width - 1;
186 ymax = cv->height - 1;
187
188 if(x2 < 0 || y2 < 0 || x > xmax || y > ymax)
189 return 0;
190
191 /* Draw edges */
192 if(y >= 0)
193 for(i = x < 0 ? 1 : x + 1; i < x2 && i < xmax; i++)
194 caca_put_char(cv, i, y, chars[0]);
195
196 if(y2 <= ymax)
197 for(i = x < 0 ? 1 : x + 1; i < x2 && i < xmax; i++)
198 caca_put_char(cv, i, y2, chars[0]);
199
200 if(x >= 0)
201 for(j = y < 0 ? 1 : y + 1; j < y2 && j < ymax; j++)
202 caca_put_char(cv, x, j, chars[1]);
203
204 if(x2 <= xmax)
205 for(j = y < 0 ? 1 : y + 1; j < y2 && j < ymax; j++)
206 caca_put_char(cv, x2, j, chars[1]);
207
208 /* Draw corners */
209 caca_put_char(cv, x, y, chars[2]);
210 caca_put_char(cv, x, y2, chars[3]);
211 caca_put_char(cv, x2, y, chars[4]);
212 caca_put_char(cv, x2, y2, chars[5]);
213
214 return 0;
215 }
216