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

Last change on this file since 1330 was 1330, checked in by Sam Hocevar, 14 years ago
  • Switch back to UTF-32 arguments for drawing primitives, now that we properly export cucul_utf8_to_utf32(). Evil API breakage, but no one uses these functions anyway.
  • 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 1330 2006-11-11 08:11:26Z 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 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 x2, int y2,
41                   unsigned long int ch)
42{
43    cucul_draw_line(cv, x1, y1, x1, y2, ch);
44    cucul_draw_line(cv, x1, y2, x2, y2, ch);
45    cucul_draw_line(cv, x2, y2, x2, y1, ch);
46    cucul_draw_line(cv, x2, y1, x1, y1, ch);
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 */
62int 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 */
121int 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 ch UTF-32 character to be used to draw the box.
179 *  \return This function always returns 0.
180 */
181int cucul_fill_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
182                   unsigned long int ch)
183{
184    int x, y, xmax, ymax;
185
186    if(x1 > x2)
187    {
188        int tmp = x1;
189        x1 = x2; x2 = tmp;
190    }
191
192    if(y1 > y2)
193    {
194        int tmp = y1;
195        y1 = y2; y2 = tmp;
196    }
197
198    xmax = cv->width - 1;
199    ymax = cv->height - 1;
200
201    if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
202        return 0;
203
204    if(x1 < 0) x1 = 0;
205    if(y1 < 0) y1 = 0;
206    if(x2 > xmax) x2 = xmax;
207    if(y2 > ymax) y2 = ymax;
208
209    for(y = y1; y <= y2; y++)
210        for(x = x1; x <= x2; x++)
211            cucul_putchar(cv, x, y, ch);
212
213    return 0;
214}
215
Note: See TracBrowser for help on using the repository browser.