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

Last change on this file since 678 was 678, checked in by Sam Hocevar, 14 years ago
  • Made cucul_putchar32() an internal-only function.
  • Changed the line, box, ellipsis etc. prototypes so that they use an UTF-8 string instead of a single character as their last argument.
  • 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 *  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 678 2006-03-23 14:07:32Z 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 str UTF-8 string containing the character to use to draw the box.
37 * \return void
38 */
39void cucul_draw_box(cucul_t *qq, int x1, int y1, int x2, int y2,
40                    char const *str)
41{
42    cucul_draw_line(qq, x1, y1, x1, y2, str);
43    cucul_draw_line(qq, x1, y2, x2, y2, str);
44    cucul_draw_line(qq, x2, y2, x2, y1, str);
45    cucul_draw_line(qq, x2, y1, x1, y1, str);
46}
47
48/**
49 * \brief Draw a thin box on the screen.
50 *
51 * \param x1 X coordinate of the upper-left corner of the box.
52 * \param y1 Y coordinate of the upper-left corner of the box.
53 * \param x2 X coordinate of the lower-right corner of the box.
54 * \param y2 Y coordinate of the lower-right corner of the box.
55 * \return void
56 */
57void cucul_draw_thin_box(cucul_t *qq, int x1, int y1, int x2, int y2)
58{
59    int x, y, xmax, ymax;
60
61    if(x1 > x2)
62    {
63        int tmp = x1;
64        x1 = x2; x2 = tmp;
65    }
66
67    if(y1 > y2)
68    {
69        int tmp = y1;
70        y1 = y2; y2 = tmp;
71    }
72
73    xmax = qq->width - 1;
74    ymax = qq->height - 1;
75
76    if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
77        return;
78
79    /* Draw edges */
80    if(y1 >= 0)
81        for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
82            _cucul_putchar32(qq, x, y1, (uint32_t)'-');
83
84    if(y2 <= ymax)
85        for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
86            _cucul_putchar32(qq, x, y2, (uint32_t)'-');
87
88    if(x1 >= 0)
89        for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
90            _cucul_putchar32(qq, x1, y, (uint32_t)'|');
91
92    if(x2 <= xmax)
93        for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
94            _cucul_putchar32(qq, x2, y, (uint32_t)'|');
95
96    /* Draw corners */
97    if(x1 >= 0 && y1 >= 0)
98        _cucul_putchar32(qq, x1, y1, (uint32_t)',');
99
100    if(x1 >= 0 && y2 <= ymax)
101        _cucul_putchar32(qq, x1, y2, (uint32_t)'`');
102
103    if(x2 <= xmax && y1 >= 0)
104        _cucul_putchar32(qq, x2, y1, (uint32_t)'.');
105
106    if(x2 <= xmax && y2 <= ymax)
107        _cucul_putchar32(qq, x2, y2, (uint32_t)'\'');
108}
109
110/**
111 * \brief Fill a box on the screen using the given character.
112 *
113 * \param x1 X coordinate of the upper-left corner of the box.
114 * \param y1 Y coordinate of the upper-left corner of the box.
115 * \param x2 X coordinate of the lower-right corner of the box.
116 * \param y2 Y coordinate of the lower-right corner of the box.
117 * \param str UTF-8 string containing the character to fill the box with.
118 * \return void
119 */
120void cucul_fill_box(cucul_t *qq, int x1, int y1, int x2, int y2,
121                    char const *str)
122{
123    int x, y, xmax, ymax;
124    uint32_t c;
125
126    if(x1 > x2)
127    {
128        int tmp = x1;
129        x1 = x2; x2 = tmp;
130    }
131
132    if(y1 > y2)
133    {
134        int tmp = y1;
135        y1 = y2; y2 = tmp;
136    }
137
138    xmax = qq->width - 1;
139    ymax = qq->height - 1;
140
141    if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
142        return;
143
144    if(x1 < 0) x1 = 0;
145    if(y1 < 0) y1 = 0;
146    if(x2 > xmax) x2 = xmax;
147    if(y2 > ymax) y2 = ymax;
148
149    c = _cucul_utf8_to_utf32(str);
150
151    for(y = y1; y <= y2; y++)
152        for(x = x1; x <= x2; x++)
153            _cucul_putchar32(qq, x, y, c);
154}
155
Note: See TracBrowser for help on using the repository browser.