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.

box.c 3.7 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #include <stdlib.h>
  20. #include "cucul.h"
  21. #include "cucul_internals.h"
  22. /**
  23. * \brief Draw a box on the screen using the given character.
  24. *
  25. * \param x1 X coordinate of the upper-left corner of the box.
  26. * \param y1 Y coordinate of the upper-left corner of the box.
  27. * \param x2 X coordinate of the lower-right corner of the box.
  28. * \param y2 Y coordinate of the lower-right corner of the box.
  29. * \param c Character to draw the box outline with.
  30. * \return void
  31. */
  32. void cucul_draw_box(cucul_t *qq, int x1, int y1, int x2, int y2, char c)
  33. {
  34. cucul_draw_line(qq, x1, y1, x1, y2, c);
  35. cucul_draw_line(qq, x1, y2, x2, y2, c);
  36. cucul_draw_line(qq, x2, y2, x2, y1, c);
  37. cucul_draw_line(qq, x2, y1, x1, y1, c);
  38. }
  39. /**
  40. * \brief Draw a thin box on the screen.
  41. *
  42. * \param x1 X coordinate of the upper-left corner of the box.
  43. * \param y1 Y coordinate of the upper-left corner of the box.
  44. * \param x2 X coordinate of the lower-right corner of the box.
  45. * \param y2 Y coordinate of the lower-right corner of the box.
  46. * \return void
  47. */
  48. void cucul_draw_thin_box(cucul_t *qq, int x1, int y1, int x2, int y2)
  49. {
  50. int x, y, xmax, ymax;
  51. if(x1 > x2)
  52. {
  53. int tmp = x1;
  54. x1 = x2; x2 = tmp;
  55. }
  56. if(y1 > y2)
  57. {
  58. int tmp = y1;
  59. y1 = y2; y2 = tmp;
  60. }
  61. xmax = qq->width - 1;
  62. ymax = qq->height - 1;
  63. if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
  64. return;
  65. /* Draw edges */
  66. if(y1 >= 0)
  67. for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
  68. cucul_putchar(qq, x, y1, '-');
  69. if(y2 <= ymax)
  70. for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
  71. cucul_putchar(qq, x, y2, '-');
  72. if(x1 >= 0)
  73. for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
  74. cucul_putchar(qq, x1, y, '|');
  75. if(x2 <= xmax)
  76. for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
  77. cucul_putchar(qq, x2, y, '|');
  78. /* Draw corners */
  79. if(x1 >= 0 && y1 >= 0)
  80. cucul_putchar(qq, x1, y1, ',');
  81. if(x1 >= 0 && y2 <= ymax)
  82. cucul_putchar(qq, x1, y2, '`');
  83. if(x2 <= xmax && y1 >= 0)
  84. cucul_putchar(qq, x2, y1, '.');
  85. if(x2 <= xmax && y2 <= ymax)
  86. cucul_putchar(qq, x2, y2, '\'');
  87. }
  88. /**
  89. * \brief Fill a box on the screen using the given character.
  90. *
  91. * \param x1 X coordinate of the upper-left corner of the box.
  92. * \param y1 Y coordinate of the upper-left corner of the box.
  93. * \param x2 X coordinate of the lower-right corner of the box.
  94. * \param y2 Y coordinate of the lower-right corner of the box.
  95. * \param c Character to fill the box with.
  96. * \return void
  97. */
  98. void cucul_fill_box(cucul_t *qq, int x1, int y1, int x2, int y2, char c)
  99. {
  100. int x, y, xmax, ymax;
  101. if(x1 > x2)
  102. {
  103. int tmp = x1;
  104. x1 = x2; x2 = tmp;
  105. }
  106. if(y1 > y2)
  107. {
  108. int tmp = y1;
  109. y1 = y2; y2 = tmp;
  110. }
  111. xmax = qq->width - 1;
  112. ymax = qq->height - 1;
  113. if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
  114. return;
  115. if(x1 < 0) x1 = 0;
  116. if(y1 < 0) y1 = 0;
  117. if(x2 > xmax) x2 = xmax;
  118. if(y2 > ymax) y2 = ymax;
  119. for(y = y1; y <= y2; y++)
  120. for(x = x1; x <= x2; x++)
  121. cucul_putchar(qq, x, y, c);
  122. }