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 6.0 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software. It comes without any warranty, to
  9. * the extent permitted by applicable law. You can redistribute it
  10. * and/or modify it under the terms of the Do What The Fuck You Want
  11. * To Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. /*
  15. * This file contains box drawing functions, both filled and outline.
  16. */
  17. #include "config.h"
  18. #if !defined(__KERNEL__)
  19. # include <stdlib.h>
  20. #endif
  21. #include "caca.h"
  22. #include "caca_internals.h"
  23. /** \brief Draw a box on the canvas using the given character.
  24. *
  25. * This function never fails.
  26. *
  27. * \param cv The handle to the libcaca canvas.
  28. * \param x X coordinate of the upper-left corner of the box.
  29. * \param y Y coordinate of the upper-left corner of the box.
  30. * \param w Width of the box.
  31. * \param h Height of the box.
  32. * \param ch UTF-32 character to be used to draw the box.
  33. * \return This function always returns 0.
  34. */
  35. int caca_draw_box(caca_canvas_t *cv, int x, int y, int w, int h, uint32_t ch)
  36. {
  37. int x2 = x + w - 1;
  38. int y2 = y + h - 1;
  39. caca_draw_line(cv, x, y, x, y2, ch);
  40. caca_draw_line(cv, x, y2, x2, y2, ch);
  41. caca_draw_line(cv, x2, y2, x2, y, ch);
  42. caca_draw_line(cv, x2, y, x, y, ch);
  43. return 0;
  44. }
  45. /** \brief Draw a thin box on the canvas.
  46. *
  47. * This function never fails.
  48. *
  49. * \param cv The handle to the libcaca canvas.
  50. * \param x X coordinate of the upper-left corner of the box.
  51. * \param y Y coordinate of the upper-left corner of the box.
  52. * \param w Width of the box.
  53. * \param h Height of the box.
  54. * \return This function always returns 0.
  55. */
  56. int caca_draw_thin_box(caca_canvas_t *cv, int x, int y, int w, int h)
  57. {
  58. int i, j, xmax, ymax;
  59. int x2 = x + w - 1;
  60. int y2 = y + h - 1;
  61. if(x > x2)
  62. {
  63. int tmp = x;
  64. x = x2; x2 = tmp;
  65. }
  66. if(y > y2)
  67. {
  68. int tmp = y;
  69. y = y2; y2 = tmp;
  70. }
  71. xmax = cv->width - 1;
  72. ymax = cv->height - 1;
  73. if(x2 < 0 || y2 < 0 || x > xmax || y > ymax)
  74. return 0;
  75. /* Draw edges */
  76. if(y >= 0)
  77. for(i = x < 0 ? 1 : x + 1; i < x2 && i < xmax; i++)
  78. caca_put_char(cv, i, y, '-');
  79. if(y2 <= ymax)
  80. for(i = x < 0 ? 1 : x + 1; i < x2 && i < xmax; i++)
  81. caca_put_char(cv, i, y2, '-');
  82. if(x >= 0)
  83. for(j = y < 0 ? 1 : y + 1; j < y2 && j < ymax; j++)
  84. caca_put_char(cv, x, j, '|');
  85. if(x2 <= xmax)
  86. for(j = y < 0 ? 1 : y + 1; j < y2 && j < ymax; j++)
  87. caca_put_char(cv, x2, j, '|');
  88. /* Draw corners */
  89. caca_put_char(cv, x, y, ',');
  90. caca_put_char(cv, x, y2, '`');
  91. caca_put_char(cv, x2, y, '.');
  92. caca_put_char(cv, x2, y2, '\'');
  93. return 0;
  94. }
  95. /** \brief Draw a box on the canvas using CP437 characters.
  96. *
  97. * This function never fails.
  98. *
  99. * \param cv The handle to the libcaca canvas.
  100. * \param x X coordinate of the upper-left corner of the box.
  101. * \param y Y coordinate of the upper-left corner of the box.
  102. * \param w Width of the box.
  103. * \param h Height of the box.
  104. * \return This function always returns 0.
  105. */
  106. int caca_draw_cp437_box(caca_canvas_t *cv, int x, int y, int w, int h)
  107. {
  108. int i, j, xmax, ymax;
  109. int x2 = x + w - 1;
  110. int y2 = y + h - 1;
  111. if(x > x2)
  112. {
  113. int tmp = x;
  114. x = x2; x2 = tmp;
  115. }
  116. if(y > y2)
  117. {
  118. int tmp = y;
  119. y = y2; y2 = tmp;
  120. }
  121. xmax = cv->width - 1;
  122. ymax = cv->height - 1;
  123. if(x2 < 0 || y2 < 0 || x > xmax || y > ymax)
  124. return 0;
  125. /* Draw edges */
  126. if(y >= 0)
  127. for(i = x < 0 ? 1 : x + 1; i < x2 && i < xmax; i++)
  128. caca_put_char(cv, i, y, 0x2500); /* ─ */
  129. if(y2 <= ymax)
  130. for(i = x < 0 ? 1 : x + 1; i < x2 && i < xmax; i++)
  131. caca_put_char(cv, i, y2, 0x2500); /* ─ */
  132. if(x >= 0)
  133. for(j = y < 0 ? 1 : y + 1; j < y2 && j < ymax; j++)
  134. caca_put_char(cv, x, j, 0x2502); /* │ */
  135. if(x2 <= xmax)
  136. for(j = y < 0 ? 1 : y + 1; j < y2 && j < ymax; j++)
  137. caca_put_char(cv, x2, j, 0x2502); /* │ */
  138. /* Draw corners */
  139. caca_put_char(cv, x, y, 0x250c); /* ┌ */
  140. caca_put_char(cv, x, y2, 0x2514); /* └ */
  141. caca_put_char(cv, x2, y, 0x2510); /* ┐ */
  142. caca_put_char(cv, x2, y2, 0x2518); /* ┘ */
  143. return 0;
  144. }
  145. /** \brief Fill a box on the canvas using the given character.
  146. *
  147. * This function never fails.
  148. *
  149. * \param cv The handle to the libcaca canvas.
  150. * \param x X coordinate of the upper-left corner of the box.
  151. * \param y Y coordinate of the upper-left corner of the box.
  152. * \param w Width of the box.
  153. * \param h Height of the box.
  154. * \param ch UTF-32 character to be used to draw the box.
  155. * \return This function always returns 0.
  156. */
  157. int caca_fill_box(caca_canvas_t *cv, int x, int y, int w, int h,
  158. uint32_t ch)
  159. {
  160. int i, j, xmax, ymax;
  161. int x2 = x + w - 1;
  162. int y2 = y + h - 1;
  163. if(x > x2)
  164. {
  165. int tmp = x;
  166. x = x2; x2 = tmp;
  167. }
  168. if(y > y2)
  169. {
  170. int tmp = y;
  171. y = y2; y2 = tmp;
  172. }
  173. xmax = cv->width - 1;
  174. ymax = cv->height - 1;
  175. if(x2 < 0 || y2 < 0 || x > xmax || y > ymax)
  176. return 0;
  177. if(x < 0) x = 0;
  178. if(y < 0) y = 0;
  179. if(x2 > xmax) x2 = xmax;
  180. if(y2 > ymax) y2 = ymax;
  181. for(j = y; j <= y2; j++)
  182. for(i = x; i <= x2; i++)
  183. caca_put_char(cv, i, j, ch);
  184. return 0;
  185. }
  186. /*
  187. * XXX: The following functions are aliases.
  188. */
  189. int cucul_draw_box(cucul_canvas_t *, int, int, int, int, uint32_t)
  190. CACA_ALIAS(caca_draw_box);
  191. int cucul_draw_thin_box(cucul_canvas_t *, int, int, int, int)
  192. CACA_ALIAS(caca_draw_thin_box);
  193. int cucul_draw_cp437_box(cucul_canvas_t *, int, int, int, int)
  194. CACA_ALIAS(caca_draw_cp437_box);
  195. int cucul_fill_box(cucul_canvas_t *, int, int, int, int, uint32_t)
  196. CACA_ALIAS(caca_fill_box);