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

Last change on this file since 859 was 859, checked in by Sam Hocevar, 14 years ago
  • Removed duplicate uint*_t defines from *_internal.h and included common.h in all .c files that needed it.
  • Property svn:keywords set to Id
File size: 4.2 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 859 2006-04-24 20:35:59Z 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 *  \param cv The handle to the libcucul canvas.
31 *  \param x1 X coordinate of the upper-left corner of the box.
32 *  \param y1 Y coordinate of the upper-left corner of the box.
33 *  \param x2 X coordinate of the lower-right corner of the box.
34 *  \param y2 Y coordinate of the lower-right corner of the box.
35 *  \param str UTF-8 string containing the character to use to draw the box.
36 *  \return void
37 */
38void cucul_draw_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
39                    char const *str)
40{
41    cucul_draw_line(cv, x1, y1, x1, y2, str);
42    cucul_draw_line(cv, x1, y2, x2, y2, str);
43    cucul_draw_line(cv, x2, y2, x2, y1, str);
44    cucul_draw_line(cv, x2, y1, x1, y1, str);
45}
46
47/** \brief Draw a thin box on the canvas.
48 *
49 *  \param cv The handle to the libcucul canvas.
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 */
56void cucul_draw_thin_box(cucul_canvas_t *cv, 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 = cv->width - 1;
73    ymax = cv->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_putchar32(cv, x, y1, (uint32_t)'-');
82
83    if(y2 <= ymax)
84        for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
85            _cucul_putchar32(cv, x, y2, (uint32_t)'-');
86
87    if(x1 >= 0)
88        for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
89            _cucul_putchar32(cv, x1, y, (uint32_t)'|');
90
91    if(x2 <= xmax)
92        for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
93            _cucul_putchar32(cv, x2, y, (uint32_t)'|');
94
95    /* Draw corners */
96    if(x1 >= 0 && y1 >= 0)
97        _cucul_putchar32(cv, x1, y1, (uint32_t)',');
98
99    if(x1 >= 0 && y2 <= ymax)
100        _cucul_putchar32(cv, x1, y2, (uint32_t)'`');
101
102    if(x2 <= xmax && y1 >= 0)
103        _cucul_putchar32(cv, x2, y1, (uint32_t)'.');
104
105    if(x2 <= xmax && y2 <= ymax)
106        _cucul_putchar32(cv, x2, y2, (uint32_t)'\'');
107}
108
109/** \brief Fill a box on the canvas using the given character.
110 *
111 *  \param cv The handle to the libcucul canvas.
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 str UTF-8 string containing the character to fill the box with.
117 *  \return void
118 */
119void cucul_fill_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
120                    char const *str)
121{
122    int x, y, xmax, ymax;
123    uint32_t ch;
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;
142
143    if(x1 < 0) x1 = 0;
144    if(y1 < 0) y1 = 0;
145    if(x2 > xmax) x2 = xmax;
146    if(y2 > ymax) y2 = ymax;
147
148    ch = _cucul_utf8_to_utf32(str);
149
150    for(y = y1; y <= y2; y++)
151        for(x = x1; x <= x2; x++)
152            _cucul_putchar32(cv, x, y, ch);
153}
154
Note: See TracBrowser for help on using the repository browser.