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

Last change on this file since 958 was 958, checked in by Sam Hocevar, 14 years ago
  • Changed cucul_putchar's prototype so that it accepts a 32-bit unsigned int which shall be used as an UTF-32 character. We do not have any casting problems due to the signedness of chars because all characters were ASCII (ie. <= 0x7f) beforehands.
  • Property svn:keywords set to Id
File size: 4.3 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 958 2006-05-18 06:23:47Z 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 str UTF-8 string containing the character to use 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                   char const *str)
42{
43    cucul_draw_line(cv, x1, y1, x1, y2, str);
44    cucul_draw_line(cv, x1, y2, x2, y2, str);
45    cucul_draw_line(cv, x2, y2, x2, y1, str);
46    cucul_draw_line(cv, x2, y1, x1, y1, str);
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    if(x1 >= 0 && y1 >= 0)
103        cucul_putchar(cv, x1, y1, ',');
104
105    if(x1 >= 0 && y2 <= ymax)
106        cucul_putchar(cv, x1, y2, '`');
107
108    if(x2 <= xmax && y1 >= 0)
109        cucul_putchar(cv, x2, y1, '.');
110
111    if(x2 <= xmax && y2 <= ymax)
112        cucul_putchar(cv, x2, y2, '\'');
113
114    return 0;
115}
116
117/** \brief Fill a box on the canvas using the given character.
118 *
119 *  This function never fails.
120 *
121 *  \param cv The handle to the libcucul canvas.
122 *  \param x1 X coordinate of the upper-left corner of the box.
123 *  \param y1 Y coordinate of the upper-left corner of the box.
124 *  \param x2 X coordinate of the lower-right corner of the box.
125 *  \param y2 Y coordinate of the lower-right corner of the box.
126 *  \param str UTF-8 string containing the character to fill the box with.
127 *  \return This function always returns 0.
128 */
129int cucul_fill_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
130                   char const *str)
131{
132    int x, y, xmax, ymax;
133    uint32_t ch;
134
135    if(x1 > x2)
136    {
137        int tmp = x1;
138        x1 = x2; x2 = tmp;
139    }
140
141    if(y1 > y2)
142    {
143        int tmp = y1;
144        y1 = y2; y2 = tmp;
145    }
146
147    xmax = cv->width - 1;
148    ymax = cv->height - 1;
149
150    if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
151        return 0;
152
153    if(x1 < 0) x1 = 0;
154    if(y1 < 0) y1 = 0;
155    if(x2 > xmax) x2 = xmax;
156    if(y2 > ymax) y2 = ymax;
157
158    ch = _cucul_utf8_to_utf32(str);
159
160    for(y = y1; y <= y2; y++)
161        for(x = x1; x <= x2; x++)
162            cucul_putchar(cv, x, y, ch);
163
164    return 0;
165}
166
Note: See TracBrowser for help on using the repository browser.