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

Last change on this file since 1947 was 1462, checked in by Sam Hocevar, 13 years ago
  • Bwarf, typo in the no warranty clause.
  • Property svn:keywords set to Id
File size: 5.8 KB
RevLine 
[151]1/*
[672]2 *  libcucul      Canvas for ultrafast compositing of Unicode letters
[527]3 *  Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
[268]4 *                All Rights Reserved
[151]5 *
[769]6 *  $Id: box.c 1462 2006-12-12 01:53:54Z sam $
7 *
[1462]8 *  This library is free software. It comes without any warranty, to
[1452]9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
[522]12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
[151]13 */
14
[769]15/*
[268]16 *  This file contains box drawing functions, both filled and outline.
[205]17 */
18
[151]19#include "config.h"
[859]20#include "common.h"
[151]21
[568]22#if !defined(__KERNEL__)
23#   include <stdlib.h>
24#endif
[151]25
[524]26#include "cucul.h"
27#include "cucul_internals.h"
[151]28
[773]29/** \brief Draw a box on the canvas using the given character.
[257]30 *
[870]31 *  This function never fails.
32 *
[811]33 *  \param cv The handle to the libcucul canvas.
[773]34 *  \param x1 X coordinate of the upper-left corner of the box.
35 *  \param y1 Y coordinate of the upper-left corner of the box.
36 *  \param x2 X coordinate of the lower-right corner of the box.
37 *  \param y2 Y coordinate of the lower-right corner of the box.
[1330]38 *  \param ch UTF-32 character to be used to draw the box.
[870]39 *  \return This function always returns 0.
[257]40 */
[870]41int cucul_draw_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
[1330]42                   unsigned long int ch)
[151]43{
[1330]44    cucul_draw_line(cv, x1, y1, x1, y2, ch);
45    cucul_draw_line(cv, x1, y2, x2, y2, ch);
46    cucul_draw_line(cv, x2, y2, x2, y1, ch);
47    cucul_draw_line(cv, x2, y1, x1, y1, ch);
[870]48
49    return 0;
[151]50}
51
[773]52/** \brief Draw a thin box on the canvas.
[257]53 *
[870]54 *  This function never fails.
55 *
[811]56 *  \param cv The handle to the libcucul canvas.
[773]57 *  \param x1 X coordinate of the upper-left corner of the box.
58 *  \param y1 Y coordinate of the upper-left corner of the box.
59 *  \param x2 X coordinate of the lower-right corner of the box.
60 *  \param y2 Y coordinate of the lower-right corner of the box.
[870]61 *  \return This function always returns 0.
[257]62 */
[870]63int cucul_draw_thin_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2)
[151]64{
65    int x, y, xmax, ymax;
66
67    if(x1 > x2)
68    {
69        int tmp = x1;
70        x1 = x2; x2 = tmp;
71    }
72
73    if(y1 > y2)
74    {
75        int tmp = y1;
76        y1 = y2; y2 = tmp;
77    }
78
[811]79    xmax = cv->width - 1;
80    ymax = cv->height - 1;
[151]81
82    if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
[870]83        return 0;
[151]84
85    /* Draw edges */
86    if(y1 >= 0)
87        for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
[1347]88            cucul_put_char(cv, x, y1, '-');
[151]89
[153]90    if(y2 <= ymax)
[151]91        for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
[1347]92            cucul_put_char(cv, x, y2, '-');
[151]93
94    if(x1 >= 0)
95        for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
[1347]96            cucul_put_char(cv, x1, y, '|');
[151]97
[153]98    if(x2 <= xmax)
[151]99        for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
[1347]100            cucul_put_char(cv, x2, y, '|');
[151]101
102    /* Draw corners */
[1347]103    cucul_put_char(cv, x1, y1, ',');
104    cucul_put_char(cv, x1, y2, '`');
105    cucul_put_char(cv, x2, y1, '.');
106    cucul_put_char(cv, x2, y2, '\'');
[151]107
[1329]108    return 0;
109}
[151]110
[1329]111/** \brief Draw a box on the canvas using CP437 characters.
112 *
113 *  This function never fails.
114 *
115 *  \param cv The handle to the libcucul canvas.
116 *  \param x1 X coordinate of the upper-left corner of the box.
117 *  \param y1 Y coordinate of the upper-left corner of the box.
118 *  \param x2 X coordinate of the lower-right corner of the box.
119 *  \param y2 Y coordinate of the lower-right corner of the box.
120 *  \return This function always returns 0.
121 */
122int cucul_draw_cp437_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2)
123{
124    int x, y, xmax, ymax;
[151]125
[1329]126    if(x1 > x2)
127    {
128        int tmp = x1;
129        x1 = x2; x2 = tmp;
130    }
[870]131
[1329]132    if(y1 > y2)
133    {
134        int tmp = y1;
135        y1 = y2; y2 = tmp;
136    }
137
138    xmax = cv->width - 1;
139    ymax = cv->height - 1;
140
141    if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
142        return 0;
143
144    /* Draw edges */
145    if(y1 >= 0)
146        for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
[1347]147            cucul_put_char(cv, x, y1, 0x2500); /* ─ */
[1329]148
149    if(y2 <= ymax)
150        for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
[1347]151            cucul_put_char(cv, x, y2, 0x2500); /* ─ */
[1329]152
153    if(x1 >= 0)
154        for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
[1347]155            cucul_put_char(cv, x1, y, 0x2502); /* │ */
[1329]156
157    if(x2 <= xmax)
158        for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
[1347]159            cucul_put_char(cv, x2, y, 0x2502); /* │ */
[1329]160
161    /* Draw corners */
[1347]162    cucul_put_char(cv, x1, y1, 0x250c); /* ┌ */
163    cucul_put_char(cv, x1, y2, 0x2514); /* └ */
164    cucul_put_char(cv, x2, y1, 0x2510); /* ┐ */
165    cucul_put_char(cv, x2, y2, 0x2518); /* ┘ */
[1329]166
[870]167    return 0;
[151]168}
169
[773]170/** \brief Fill a box on the canvas using the given character.
[257]171 *
[870]172 *  This function never fails.
173 *
[811]174 *  \param cv The handle to the libcucul canvas.
[773]175 *  \param x1 X coordinate of the upper-left corner of the box.
176 *  \param y1 Y coordinate of the upper-left corner of the box.
177 *  \param x2 X coordinate of the lower-right corner of the box.
178 *  \param y2 Y coordinate of the lower-right corner of the box.
[1330]179 *  \param ch UTF-32 character to be used to draw the box.
[870]180 *  \return This function always returns 0.
[257]181 */
[870]182int cucul_fill_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
[1330]183                   unsigned long int ch)
[151]184{
185    int x, y, xmax, ymax;
186
187    if(x1 > x2)
188    {
189        int tmp = x1;
190        x1 = x2; x2 = tmp;
191    }
192
193    if(y1 > y2)
194    {
195        int tmp = y1;
196        y1 = y2; y2 = tmp;
197    }
198
[811]199    xmax = cv->width - 1;
200    ymax = cv->height - 1;
[151]201
202    if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
[870]203        return 0;
[151]204
205    if(x1 < 0) x1 = 0;
206    if(y1 < 0) y1 = 0;
207    if(x2 > xmax) x2 = xmax;
208    if(y2 > ymax) y2 = ymax;
209
210    for(y = y1; y <= y2; y++)
211        for(x = x1; x <= x2; x++)
[1347]212            cucul_put_char(cv, x, y, ch);
[870]213
214    return 0;
[151]215}
216
Note: See TracBrowser for help on using the repository browser.