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

Last change on this file since 870 was 870, checked in by Sam Hocevar, 14 years ago
  • Check for <errno.h>.
  • Started changing function prototypes so that they return an integer instead of void, just in case they might fail.
  • Property svn:keywords set to Id
File size: 4.4 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 870 2006-04-25 09:59:58Z 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"
[859]19#include "common.h"
[151]20
[568]21#if !defined(__KERNEL__)
22#   include <stdlib.h>
23#endif
[151]24
[524]25#include "cucul.h"
26#include "cucul_internals.h"
[151]27
[773]28/** \brief Draw a box on the canvas using the given character.
[257]29 *
[870]30 *  This function never fails.
31 *
[811]32 *  \param cv The handle to the libcucul canvas.
[773]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.
[870]38 *  \return This function always returns 0.
[257]39 */
[870]40int cucul_draw_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
41                   char const *str)
[151]42{
[811]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);
[870]47
48    return 0;
[151]49}
50
[773]51/** \brief Draw a thin box on the canvas.
[257]52 *
[870]53 *  This function never fails.
54 *
[811]55 *  \param cv The handle to the libcucul canvas.
[773]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.
[870]60 *  \return This function always returns 0.
[257]61 */
[870]62int cucul_draw_thin_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2)
[151]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
[811]78    xmax = cv->width - 1;
79    ymax = cv->height - 1;
[151]80
81    if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
[870]82        return 0;
[151]83
84    /* Draw edges */
85    if(y1 >= 0)
86        for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
[811]87            _cucul_putchar32(cv, x, y1, (uint32_t)'-');
[151]88
[153]89    if(y2 <= ymax)
[151]90        for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
[811]91            _cucul_putchar32(cv, x, y2, (uint32_t)'-');
[151]92
93    if(x1 >= 0)
94        for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
[811]95            _cucul_putchar32(cv, x1, y, (uint32_t)'|');
[151]96
[153]97    if(x2 <= xmax)
[151]98        for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
[811]99            _cucul_putchar32(cv, x2, y, (uint32_t)'|');
[151]100
101    /* Draw corners */
102    if(x1 >= 0 && y1 >= 0)
[811]103        _cucul_putchar32(cv, x1, y1, (uint32_t)',');
[151]104
105    if(x1 >= 0 && y2 <= ymax)
[811]106        _cucul_putchar32(cv, x1, y2, (uint32_t)'`');
[151]107
108    if(x2 <= xmax && y1 >= 0)
[811]109        _cucul_putchar32(cv, x2, y1, (uint32_t)'.');
[151]110
111    if(x2 <= xmax && y2 <= ymax)
[811]112        _cucul_putchar32(cv, x2, y2, (uint32_t)'\'');
[870]113
114    return 0;
[151]115}
116
[773]117/** \brief Fill a box on the canvas using the given character.
[257]118 *
[870]119 *  This function never fails.
120 *
[811]121 *  \param cv The handle to the libcucul canvas.
[773]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.
[870]127 *  \return This function always returns 0.
[257]128 */
[870]129int cucul_fill_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
130                   char const *str)
[151]131{
132    int x, y, xmax, ymax;
[810]133    uint32_t ch;
[151]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
[811]147    xmax = cv->width - 1;
148    ymax = cv->height - 1;
[151]149
150    if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
[870]151        return 0;
[151]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
[810]158    ch = _cucul_utf8_to_utf32(str);
[678]159
[151]160    for(y = y1; y <= y2; y++)
161        for(x = x1; x <= x2; x++)
[811]162            _cucul_putchar32(cv, x, y, ch);
[870]163
164    return 0;
[151]165}
166
Note: See TracBrowser for help on using the repository browser.