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

Last change on this file since 773 was 773, checked in by Sam Hocevar, 14 years ago
  • Improved documentation in many places.
  • Property svn:keywords set to Id
File size: 4.1 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 773 2006-04-14 12:10:18Z 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
20#if !defined(__KERNEL__)
21#   include <stdlib.h>
22#endif
23
24#include "cucul.h"
25#include "cucul_internals.h"
26
27/** \brief Draw a box on the canvas using the given character.
28 *
29 *  \param qq The handle to the libcucul canvas.
30 *  \param x1 X coordinate of the upper-left corner of the box.
31 *  \param y1 Y coordinate of the upper-left corner of the box.
32 *  \param x2 X coordinate of the lower-right corner of the box.
33 *  \param y2 Y coordinate of the lower-right corner of the box.
34 *  \param str UTF-8 string containing the character to use to draw the box.
35 *  \return void
36 */
37void cucul_draw_box(cucul_t *qq, int x1, int y1, int x2, int y2,
38                    char const *str)
39{
40    cucul_draw_line(qq, x1, y1, x1, y2, str);
41    cucul_draw_line(qq, x1, y2, x2, y2, str);
42    cucul_draw_line(qq, x2, y2, x2, y1, str);
43    cucul_draw_line(qq, x2, y1, x1, y1, str);
44}
45
46/** \brief Draw a thin box on the canvas.
47 *
48 *  \param qq The handle to the libcucul canvas.
49 *  \param x1 X coordinate of the upper-left corner of the box.
50 *  \param y1 Y coordinate of the upper-left corner of the box.
51 *  \param x2 X coordinate of the lower-right corner of the box.
52 *  \param y2 Y coordinate of the lower-right corner of the box.
53 *  \return void
54 */
55void cucul_draw_thin_box(cucul_t *qq, int x1, int y1, int x2, int y2)
56{
57    int x, y, xmax, ymax;
58
59    if(x1 > x2)
60    {
61        int tmp = x1;
62        x1 = x2; x2 = tmp;
63    }
64
65    if(y1 > y2)
66    {
67        int tmp = y1;
68        y1 = y2; y2 = tmp;
69    }
70
71    xmax = qq->width - 1;
72    ymax = qq->height - 1;
73
74    if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
75        return;
76
77    /* Draw edges */
78    if(y1 >= 0)
79        for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
80            _cucul_putchar32(qq, x, y1, (uint32_t)'-');
81
82    if(y2 <= ymax)
83        for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
84            _cucul_putchar32(qq, x, y2, (uint32_t)'-');
85
86    if(x1 >= 0)
87        for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
88            _cucul_putchar32(qq, x1, y, (uint32_t)'|');
89
90    if(x2 <= xmax)
91        for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
92            _cucul_putchar32(qq, x2, y, (uint32_t)'|');
93
94    /* Draw corners */
95    if(x1 >= 0 && y1 >= 0)
96        _cucul_putchar32(qq, x1, y1, (uint32_t)',');
97
98    if(x1 >= 0 && y2 <= ymax)
99        _cucul_putchar32(qq, x1, y2, (uint32_t)'`');
100
101    if(x2 <= xmax && y1 >= 0)
102        _cucul_putchar32(qq, x2, y1, (uint32_t)'.');
103
104    if(x2 <= xmax && y2 <= ymax)
105        _cucul_putchar32(qq, x2, y2, (uint32_t)'\'');
106}
107
108/** \brief Fill a box on the canvas using the given character.
109 *
110 *  \param qq The handle to the libcucul canvas.
111 *  \param x1 X coordinate of the upper-left corner of the box.
112 *  \param y1 Y coordinate of the upper-left corner of the box.
113 *  \param x2 X coordinate of the lower-right corner of the box.
114 *  \param y2 Y coordinate of the lower-right corner of the box.
115 *  \param str UTF-8 string containing the character to fill the box with.
116 *  \return void
117 */
118void cucul_fill_box(cucul_t *qq, int x1, int y1, int x2, int y2,
119                    char const *str)
120{
121    int x, y, xmax, ymax;
122    uint32_t c;
123
124    if(x1 > x2)
125    {
126        int tmp = x1;
127        x1 = x2; x2 = tmp;
128    }
129
130    if(y1 > y2)
131    {
132        int tmp = y1;
133        y1 = y2; y2 = tmp;
134    }
135
136    xmax = qq->width - 1;
137    ymax = qq->height - 1;
138
139    if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
140        return;
141
142    if(x1 < 0) x1 = 0;
143    if(y1 < 0) y1 = 0;
144    if(x2 > xmax) x2 = xmax;
145    if(y2 > ymax) y2 = ymax;
146
147    c = _cucul_utf8_to_utf32(str);
148
149    for(y = y1; y <= y2; y++)
150        for(x = x1; x <= x2; x++)
151            _cucul_putchar32(qq, x, y, c);
152}
153
Note: See TracBrowser for help on using the repository browser.