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

Last change on this file since 811 was 811, checked in by Sam Hocevar, 14 years ago
  • Renamed caca_t into caca_display_t.
  • Renamed canvas handle variables from "c" to "cv". Eh ouais mon gros.
  • Property svn:keywords set to Id
File size: 4.2 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 811 2006-04-18 15:11:25Z sam $
7 *
[268]8 *  This library is free software; you can redistribute it and/or
[522]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.
[151]12 */
13
[769]14/*
[268]15 *  This file contains box drawing functions, both filled and outline.
[205]16 */
17
[151]18#include "config.h"
19
[568]20#if !defined(__KERNEL__)
21#   include <stdlib.h>
22#endif
[151]23
[524]24#include "cucul.h"
25#include "cucul_internals.h"
[151]26
[773]27/** \brief Draw a box on the canvas using the given character.
[257]28 *
[811]29 *  \param cv The handle to the libcucul canvas.
[773]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
[257]36 */
[811]37void cucul_draw_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
[678]38                    char const *str)
[151]39{
[811]40    cucul_draw_line(cv, x1, y1, x1, y2, str);
41    cucul_draw_line(cv, x1, y2, x2, y2, str);
42    cucul_draw_line(cv, x2, y2, x2, y1, str);
43    cucul_draw_line(cv, x2, y1, x1, y1, str);
[151]44}
45
[773]46/** \brief Draw a thin box on the canvas.
[257]47 *
[811]48 *  \param cv The handle to the libcucul canvas.
[773]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
[257]54 */
[811]55void cucul_draw_thin_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2)
[151]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
[811]71    xmax = cv->width - 1;
72    ymax = cv->height - 1;
[151]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++)
[811]80            _cucul_putchar32(cv, x, y1, (uint32_t)'-');
[151]81
[153]82    if(y2 <= ymax)
[151]83        for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
[811]84            _cucul_putchar32(cv, x, y2, (uint32_t)'-');
[151]85
86    if(x1 >= 0)
87        for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
[811]88            _cucul_putchar32(cv, x1, y, (uint32_t)'|');
[151]89
[153]90    if(x2 <= xmax)
[151]91        for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
[811]92            _cucul_putchar32(cv, x2, y, (uint32_t)'|');
[151]93
94    /* Draw corners */
95    if(x1 >= 0 && y1 >= 0)
[811]96        _cucul_putchar32(cv, x1, y1, (uint32_t)',');
[151]97
98    if(x1 >= 0 && y2 <= ymax)
[811]99        _cucul_putchar32(cv, x1, y2, (uint32_t)'`');
[151]100
101    if(x2 <= xmax && y1 >= 0)
[811]102        _cucul_putchar32(cv, x2, y1, (uint32_t)'.');
[151]103
104    if(x2 <= xmax && y2 <= ymax)
[811]105        _cucul_putchar32(cv, x2, y2, (uint32_t)'\'');
[151]106}
107
[773]108/** \brief Fill a box on the canvas using the given character.
[257]109 *
[811]110 *  \param cv The handle to the libcucul canvas.
[773]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
[257]117 */
[811]118void cucul_fill_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
[678]119                    char const *str)
[151]120{
121    int x, y, xmax, ymax;
[810]122    uint32_t ch;
[151]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
[811]136    xmax = cv->width - 1;
137    ymax = cv->height - 1;
[151]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
[810]147    ch = _cucul_utf8_to_utf32(str);
[678]148
[151]149    for(y = y1; y <= y2; y++)
150        for(x = x1; x <= x2; x++)
[811]151            _cucul_putchar32(cv, x, y, ch);
[151]152}
153
Note: See TracBrowser for help on using the repository browser.