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 | * This library is free software; you can redistribute it and/or |
---|
7 | * modify it under the terms of the Do What The Fuck You Want To |
---|
8 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
9 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
10 | */ |
---|
11 | |
---|
12 | /** \file box.c |
---|
13 | * \version \$Id: box.c 672 2006-03-22 21:15:57Z sam $ |
---|
14 | * \author Sam Hocevar <sam@zoy.org> |
---|
15 | * \brief Simple box drawing |
---|
16 | * |
---|
17 | * This file contains box drawing functions, both filled and outline. |
---|
18 | */ |
---|
19 | |
---|
20 | #include "config.h" |
---|
21 | |
---|
22 | #if !defined(__KERNEL__) |
---|
23 | # include <stdlib.h> |
---|
24 | #endif |
---|
25 | |
---|
26 | #include "cucul.h" |
---|
27 | #include "cucul_internals.h" |
---|
28 | |
---|
29 | /** |
---|
30 | * \brief Draw a box on the screen using the given character. |
---|
31 | * |
---|
32 | * \param x1 X coordinate of the upper-left corner of the box. |
---|
33 | * \param y1 Y coordinate of the upper-left corner of the box. |
---|
34 | * \param x2 X coordinate of the lower-right corner of the box. |
---|
35 | * \param y2 Y coordinate of the lower-right corner of the box. |
---|
36 | * \param c Character to draw the box outline with. |
---|
37 | * \return void |
---|
38 | */ |
---|
39 | void cucul_draw_box(cucul_t *qq, int x1, int y1, int x2, int y2, char c) |
---|
40 | { |
---|
41 | cucul_draw_line(qq, x1, y1, x1, y2, c); |
---|
42 | cucul_draw_line(qq, x1, y2, x2, y2, c); |
---|
43 | cucul_draw_line(qq, x2, y2, x2, y1, c); |
---|
44 | cucul_draw_line(qq, x2, y1, x1, y1, c); |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | * \brief Draw a thin box on the screen. |
---|
49 | * |
---|
50 | * \param x1 X coordinate of the upper-left corner of the box. |
---|
51 | * \param y1 Y coordinate of the upper-left corner of the box. |
---|
52 | * \param x2 X coordinate of the lower-right corner of the box. |
---|
53 | * \param y2 Y coordinate of the lower-right corner of the box. |
---|
54 | * \return void |
---|
55 | */ |
---|
56 | void cucul_draw_thin_box(cucul_t *qq, int x1, int y1, int x2, int y2) |
---|
57 | { |
---|
58 | int x, y, xmax, ymax; |
---|
59 | |
---|
60 | if(x1 > x2) |
---|
61 | { |
---|
62 | int tmp = x1; |
---|
63 | x1 = x2; x2 = tmp; |
---|
64 | } |
---|
65 | |
---|
66 | if(y1 > y2) |
---|
67 | { |
---|
68 | int tmp = y1; |
---|
69 | y1 = y2; y2 = tmp; |
---|
70 | } |
---|
71 | |
---|
72 | xmax = qq->width - 1; |
---|
73 | ymax = qq->height - 1; |
---|
74 | |
---|
75 | if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax) |
---|
76 | return; |
---|
77 | |
---|
78 | /* Draw edges */ |
---|
79 | if(y1 >= 0) |
---|
80 | for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++) |
---|
81 | cucul_putchar(qq, x, y1, '-'); |
---|
82 | |
---|
83 | if(y2 <= ymax) |
---|
84 | for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++) |
---|
85 | cucul_putchar(qq, x, y2, '-'); |
---|
86 | |
---|
87 | if(x1 >= 0) |
---|
88 | for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++) |
---|
89 | cucul_putchar(qq, x1, y, '|'); |
---|
90 | |
---|
91 | if(x2 <= xmax) |
---|
92 | for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++) |
---|
93 | cucul_putchar(qq, x2, y, '|'); |
---|
94 | |
---|
95 | /* Draw corners */ |
---|
96 | if(x1 >= 0 && y1 >= 0) |
---|
97 | cucul_putchar(qq, x1, y1, ','); |
---|
98 | |
---|
99 | if(x1 >= 0 && y2 <= ymax) |
---|
100 | cucul_putchar(qq, x1, y2, '`'); |
---|
101 | |
---|
102 | if(x2 <= xmax && y1 >= 0) |
---|
103 | cucul_putchar(qq, x2, y1, '.'); |
---|
104 | |
---|
105 | if(x2 <= xmax && y2 <= ymax) |
---|
106 | cucul_putchar(qq, x2, y2, '\''); |
---|
107 | } |
---|
108 | |
---|
109 | /** |
---|
110 | * \brief Fill a box on the screen using the given character. |
---|
111 | * |
---|
112 | * \param x1 X coordinate of the upper-left corner of the box. |
---|
113 | * \param y1 Y coordinate of the upper-left corner of the box. |
---|
114 | * \param x2 X coordinate of the lower-right corner of the box. |
---|
115 | * \param y2 Y coordinate of the lower-right corner of the box. |
---|
116 | * \param c Character to fill the box with. |
---|
117 | * \return void |
---|
118 | */ |
---|
119 | void cucul_fill_box(cucul_t *qq, int x1, int y1, int x2, int y2, char c) |
---|
120 | { |
---|
121 | int x, y, xmax, ymax; |
---|
122 | |
---|
123 | if(x1 > x2) |
---|
124 | { |
---|
125 | int tmp = x1; |
---|
126 | x1 = x2; x2 = tmp; |
---|
127 | } |
---|
128 | |
---|
129 | if(y1 > y2) |
---|
130 | { |
---|
131 | int tmp = y1; |
---|
132 | y1 = y2; y2 = tmp; |
---|
133 | } |
---|
134 | |
---|
135 | xmax = qq->width - 1; |
---|
136 | ymax = qq->height - 1; |
---|
137 | |
---|
138 | if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax) |
---|
139 | return; |
---|
140 | |
---|
141 | if(x1 < 0) x1 = 0; |
---|
142 | if(y1 < 0) y1 = 0; |
---|
143 | if(x2 > xmax) x2 = xmax; |
---|
144 | if(y2 > ymax) y2 = ymax; |
---|
145 | |
---|
146 | for(y = y1; y <= y2; y++) |
---|
147 | for(x = x1; x <= x2; x++) |
---|
148 | cucul_putchar(qq, x, y, c); |
---|
149 | } |
---|
150 | |
---|