source: libcaca/trunk/caca/box.c @ 4333

Last change on this file since 4333 was 4333, checked in by Sam Hocevar, 10 years ago

Large source code cleanup, getting rid of spaces, tabs, and svn keywords.

  • Property svn:keywords set to Id
File size: 5.9 KB
RevLine 
[151]1/*
[2821]2 *  libcaca       Colour ASCII-Art library
[3585]3 *  Copyright (c) 2002-2009 Sam Hocevar <sam@hocevar.net>
[268]4 *                All Rights Reserved
[151]5 *
[1462]6 *  This library is free software. It comes without any warranty, to
[1452]7 *  the extent permitted by applicable law. You can redistribute it
8 *  and/or modify it under the terms of the Do What The Fuck You Want
9 *  To Public License, Version 2, as published by Sam Hocevar. See
[522]10 *  http://sam.zoy.org/wtfpl/COPYING for more details.
[151]11 */
12
[769]13/*
[268]14 *  This file contains box drawing functions, both filled and outline.
[205]15 */
16
[151]17#include "config.h"
18
[568]19#if !defined(__KERNEL__)
20#   include <stdlib.h>
21#endif
[151]22
[2821]23#include "caca.h"
24#include "caca_internals.h"
[151]25
[3447]26static int draw_box(caca_canvas_t *cv, int x, int y, int w, int h,
27                    uint32_t const *chars);
28
[773]29/** \brief Draw a box on the canvas using the given character.
[257]30 *
[870]31 *  This function never fails.
32 *
[2821]33 *  \param cv The handle to the libcaca canvas.
[2079]34 *  \param x X coordinate of the upper-left corner of the box.
35 *  \param y Y coordinate of the upper-left corner of the box.
36 *  \param w Width of the box.
37 *  \param h Height 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 */
[2821]41int caca_draw_box(caca_canvas_t *cv, int x, int y, int w, int h, uint32_t ch)
[151]42{
[2306]43    int x2 = x + w - 1;
44    int y2 = y + h - 1;
[2079]45
[2821]46    caca_draw_line(cv,  x,  y,  x, y2, ch);
47    caca_draw_line(cv,  x, y2, x2, y2, ch);
48    caca_draw_line(cv, x2, y2, x2,  y, ch);
49    caca_draw_line(cv, x2,  y,  x,  y, ch);
[870]50
51    return 0;
[151]52}
53
[773]54/** \brief Draw a thin box on the canvas.
[257]55 *
[870]56 *  This function never fails.
57 *
[2821]58 *  \param cv The handle to the libcaca canvas.
[2079]59 *  \param x X coordinate of the upper-left corner of the box.
60 *  \param y Y coordinate of the upper-left corner of the box.
61 *  \param w Width of the box.
62 *  \param h Height of the box.
[870]63 *  \return This function always returns 0.
[257]64 */
[2821]65int caca_draw_thin_box(caca_canvas_t *cv, int x, int y, int w, int h)
[151]66{
[3447]67    static uint32_t const ascii_chars[] =
[151]68    {
[3447]69        '-', '|', ',', '`', '.', '\''
70    };
[151]71
[3447]72    return draw_box(cv, x, y, w, h, ascii_chars);
[1329]73}
[151]74
[1329]75/** \brief Draw a box on the canvas using CP437 characters.
76 *
77 *  This function never fails.
78 *
[2821]79 *  \param cv The handle to the libcaca canvas.
[2079]80 *  \param x X coordinate of the upper-left corner of the box.
81 *  \param y Y coordinate of the upper-left corner of the box.
82 *  \param w Width of the box.
83 *  \param h Height of the box.
[1329]84 *  \return This function always returns 0.
85 */
[2821]86int caca_draw_cp437_box(caca_canvas_t *cv, int x, int y, int w, int h)
[1329]87{
[3447]88    static uint32_t const cp437_chars[] =
89    {
90        /* ─ │ ┌ └ ┐ ┘ */
91        0x2500, 0x2502, 0x250c, 0x2514, 0x2510, 0x2518
92    };
93
94    return draw_box(cv, x, y, w, h, cp437_chars);
95}
96
97/** \brief Fill a box on the canvas using the given character.
98 *
99 *  This function never fails.
100 *
101 *  \param cv The handle to the libcaca canvas.
102 *  \param x X coordinate of the upper-left corner of the box.
103 *  \param y Y coordinate of the upper-left corner of the box.
104 *  \param w Width of the box.
105 *  \param h Height of the box.
106 *  \param ch UTF-32 character to be used to draw the box.
107 *  \return This function always returns 0.
108 */
109int caca_fill_box(caca_canvas_t *cv, int x, int y, int w, int h,
110                   uint32_t ch)
111{
[2306]112    int i, j, xmax, ymax;
[151]113
[2306]114    int x2 = x + w - 1;
115    int y2 = y + h - 1;
[2079]116
[2306]117    if(x > x2)
[1329]118    {
[2306]119        int tmp = x;
120        x = x2; x2 = tmp;
[1329]121    }
[870]122
[2306]123    if(y > y2)
[1329]124    {
[2306]125        int tmp = y;
126        y = y2; y2 = tmp;
[1329]127    }
128
129    xmax = cv->width - 1;
130    ymax = cv->height - 1;
131
[2306]132    if(x2 < 0 || y2 < 0 || x > xmax || y > ymax)
[1329]133        return 0;
134
[3447]135    if(x < 0) x = 0;
136    if(y < 0) y = 0;
137    if(x2 > xmax) x2 = xmax;
138    if(y2 > ymax) y2 = ymax;
[1329]139
[3585]140#if 0
141    /* FIXME: this fails with fullwidth character blits. Also, the dirty
142     * rectangle handling may miss fullwidth cells. */
143    /* Optimise dirty rectangle handling, part 1 */
144    cv->dirty_disabled++;
145#endif
146
[3447]147    for(j = y; j <= y2; j++)
148        for(i = x; i <= x2; i++)
149            caca_put_char(cv, i, j, ch);
[1329]150
[3585]151#if 0
152    /* Optimise dirty rectangle handling, part 2 */
153    cv->dirty_disabled--;
154    if(!cv->dirty_disabled)
155        caca_add_dirty_rect(cv, x, y, x2 - x + 1, y2 - y + 1);
156#endif
157
[870]158    return 0;
[151]159}
160
[3447]161/*
162 * XXX: The following functions are local.
[257]163 */
[3447]164
165static int draw_box(caca_canvas_t *cv, int x, int y, int w, int h,
166                    uint32_t const *chars)
[151]167{
[2306]168    int i, j, xmax, ymax;
[151]169
[2306]170    int x2 = x + w - 1;
171    int y2 = y + h - 1;
[2079]172
[2306]173    if(x > x2)
[151]174    {
[2306]175        int tmp = x;
176        x = x2; x2 = tmp;
[151]177    }
178
[2306]179    if(y > y2)
[151]180    {
[2306]181        int tmp = y;
182        y = y2; y2 = tmp;
[151]183    }
184
[811]185    xmax = cv->width - 1;
186    ymax = cv->height - 1;
[151]187
[2306]188    if(x2 < 0 || y2 < 0 || x > xmax || y > ymax)
[870]189        return 0;
[151]190
[3447]191    /* Draw edges */
192    if(y >= 0)
193        for(i = x < 0 ? 1 : x + 1; i < x2 && i < xmax; i++)
194            caca_put_char(cv, i, y, chars[0]);
[151]195
[3447]196    if(y2 <= ymax)
197        for(i = x < 0 ? 1 : x + 1; i < x2 && i < xmax; i++)
198            caca_put_char(cv, i, y2, chars[0]);
[870]199
[3447]200    if(x >= 0)
201        for(j = y < 0 ? 1 : y + 1; j < y2 && j < ymax; j++)
202            caca_put_char(cv, x, j, chars[1]);
203
204    if(x2 <= xmax)
205        for(j = y < 0 ? 1 : y + 1; j < y2 && j < ymax; j++)
206            caca_put_char(cv, x2, j, chars[1]);
207
208    /* Draw corners */
209    caca_put_char(cv, x, y, chars[2]);
210    caca_put_char(cv, x, y2, chars[3]);
211    caca_put_char(cv, x2, y, chars[4]);
212    caca_put_char(cv, x2, y2, chars[5]);
213
[870]214    return 0;
[151]215}
216
[2826]217/*
218 * XXX: The following functions are aliases.
219 */
220
221int cucul_draw_box(cucul_canvas_t *, int, int, int, int, uint32_t)
222         CACA_ALIAS(caca_draw_box);
223int cucul_draw_thin_box(cucul_canvas_t *, int, int, int, int)
224         CACA_ALIAS(caca_draw_thin_box);
225int cucul_draw_cp437_box(cucul_canvas_t *, int, int, int, int)
226         CACA_ALIAS(caca_draw_cp437_box);
227int cucul_fill_box(cucul_canvas_t *, int, int, int, int, uint32_t)
228         CACA_ALIAS(caca_fill_box);
229
Note: See TracBrowser for help on using the repository browser.