You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

155 lines
3.9 KiB

  1. /*
  2. * libcucul Unicode canvas library
  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. /** \file box.c
  12. * \version \$Id$
  13. * \author Sam Hocevar <sam@zoy.org>
  14. * \brief Simple box drawing
  15. *
  16. * This file contains box drawing functions, both filled and outline.
  17. */
  18. #include "config.h"
  19. #if defined(HAVE_INTTYPES_H) || defined(_DOXYGEN_SKIP_ME)
  20. # include <inttypes.h>
  21. #else
  22. typedef unsigned char uint8_t;
  23. #endif
  24. #include <stdlib.h>
  25. #include "cucul.h"
  26. #include "cucul_internals.h"
  27. /**
  28. * \brief Draw a box on the screen using the given character.
  29. *
  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 c Character to draw the box outline with.
  35. * \return void
  36. */
  37. void cucul_draw_box(cucul_t *qq, int x1, int y1, int x2, int y2, char c)
  38. {
  39. cucul_draw_line(qq, x1, y1, x1, y2, c);
  40. cucul_draw_line(qq, x1, y2, x2, y2, c);
  41. cucul_draw_line(qq, x2, y2, x2, y1, c);
  42. cucul_draw_line(qq, x2, y1, x1, y1, c);
  43. }
  44. /**
  45. * \brief Draw a thin box on the screen.
  46. *
  47. * \param x1 X coordinate of the upper-left corner of the box.
  48. * \param y1 Y coordinate of the upper-left corner of the box.
  49. * \param x2 X coordinate of the lower-right corner of the box.
  50. * \param y2 Y coordinate of the lower-right corner of the box.
  51. * \return void
  52. */
  53. void cucul_draw_thin_box(cucul_t *qq, int x1, int y1, int x2, int y2)
  54. {
  55. int x, y, xmax, ymax;
  56. if(x1 > x2)
  57. {
  58. int tmp = x1;
  59. x1 = x2; x2 = tmp;
  60. }
  61. if(y1 > y2)
  62. {
  63. int tmp = y1;
  64. y1 = y2; y2 = tmp;
  65. }
  66. xmax = qq->width - 1;
  67. ymax = qq->height - 1;
  68. if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
  69. return;
  70. /* Draw edges */
  71. if(y1 >= 0)
  72. for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
  73. cucul_putchar(qq, x, y1, '-');
  74. if(y2 <= ymax)
  75. for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
  76. cucul_putchar(qq, x, y2, '-');
  77. if(x1 >= 0)
  78. for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
  79. cucul_putchar(qq, x1, y, '|');
  80. if(x2 <= xmax)
  81. for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
  82. cucul_putchar(qq, x2, y, '|');
  83. /* Draw corners */
  84. if(x1 >= 0 && y1 >= 0)
  85. cucul_putchar(qq, x1, y1, ',');
  86. if(x1 >= 0 && y2 <= ymax)
  87. cucul_putchar(qq, x1, y2, '`');
  88. if(x2 <= xmax && y1 >= 0)
  89. cucul_putchar(qq, x2, y1, '.');
  90. if(x2 <= xmax && y2 <= ymax)
  91. cucul_putchar(qq, x2, y2, '\'');
  92. }
  93. /**
  94. * \brief Fill a box on the screen using the given character.
  95. *
  96. * \param x1 X coordinate of the upper-left corner of the box.
  97. * \param y1 Y coordinate of the upper-left corner of the box.
  98. * \param x2 X coordinate of the lower-right corner of the box.
  99. * \param y2 Y coordinate of the lower-right corner of the box.
  100. * \param c Character to fill the box with.
  101. * \return void
  102. */
  103. void cucul_fill_box(cucul_t *qq, int x1, int y1, int x2, int y2, char c)
  104. {
  105. int x, y, xmax, ymax;
  106. if(x1 > x2)
  107. {
  108. int tmp = x1;
  109. x1 = x2; x2 = tmp;
  110. }
  111. if(y1 > y2)
  112. {
  113. int tmp = y1;
  114. y1 = y2; y2 = tmp;
  115. }
  116. xmax = qq->width - 1;
  117. ymax = qq->height - 1;
  118. if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
  119. return;
  120. if(x1 < 0) x1 = 0;
  121. if(y1 < 0) y1 = 0;
  122. if(x2 > xmax) x2 = xmax;
  123. if(y2 > ymax) y2 = ymax;
  124. for(y = y1; y <= y2; y++)
  125. for(x = x1; x <= x2; x++)
  126. cucul_putchar(qq, x, y, c);
  127. }