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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software; you can redistribute it and/or
  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.
  12. */
  13. /*
  14. * This file contains box drawing functions, both filled and outline.
  15. */
  16. #include "config.h"
  17. #include "common.h"
  18. #if !defined(__KERNEL__)
  19. # include <stdlib.h>
  20. #endif
  21. #include "cucul.h"
  22. #include "cucul_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 libcucul canvas.
  28. * \param x1 X coordinate of the upper-left corner of the box.
  29. * \param y1 Y coordinate of the upper-left corner of the box.
  30. * \param x2 X coordinate of the lower-right corner of the box.
  31. * \param y2 Y coordinate of the lower-right corner 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 cucul_draw_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
  36. unsigned long int ch)
  37. {
  38. cucul_draw_line(cv, x1, y1, x1, y2, ch);
  39. cucul_draw_line(cv, x1, y2, x2, y2, ch);
  40. cucul_draw_line(cv, x2, y2, x2, y1, ch);
  41. cucul_draw_line(cv, x2, y1, x1, y1, ch);
  42. return 0;
  43. }
  44. /** \brief Draw a thin box on the canvas.
  45. *
  46. * This function never fails.
  47. *
  48. * \param cv The handle to the libcucul canvas.
  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 This function always returns 0.
  54. */
  55. int cucul_draw_thin_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2)
  56. {
  57. int x, y, xmax, ymax;
  58. if(x1 > x2)
  59. {
  60. int tmp = x1;
  61. x1 = x2; x2 = tmp;
  62. }
  63. if(y1 > y2)
  64. {
  65. int tmp = y1;
  66. y1 = y2; y2 = tmp;
  67. }
  68. xmax = cv->width - 1;
  69. ymax = cv->height - 1;
  70. if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
  71. return 0;
  72. /* Draw edges */
  73. if(y1 >= 0)
  74. for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
  75. cucul_put_char(cv, x, y1, '-');
  76. if(y2 <= ymax)
  77. for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
  78. cucul_put_char(cv, x, y2, '-');
  79. if(x1 >= 0)
  80. for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
  81. cucul_put_char(cv, x1, y, '|');
  82. if(x2 <= xmax)
  83. for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
  84. cucul_put_char(cv, x2, y, '|');
  85. /* Draw corners */
  86. cucul_put_char(cv, x1, y1, ',');
  87. cucul_put_char(cv, x1, y2, '`');
  88. cucul_put_char(cv, x2, y1, '.');
  89. cucul_put_char(cv, x2, y2, '\'');
  90. return 0;
  91. }
  92. /** \brief Draw a box on the canvas using CP437 characters.
  93. *
  94. * This function never fails.
  95. *
  96. * \param cv The handle to the libcucul canvas.
  97. * \param x1 X coordinate of the upper-left corner of the box.
  98. * \param y1 Y coordinate of the upper-left corner of the box.
  99. * \param x2 X coordinate of the lower-right corner of the box.
  100. * \param y2 Y coordinate of the lower-right corner of the box.
  101. * \return This function always returns 0.
  102. */
  103. int cucul_draw_cp437_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2)
  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 = cv->width - 1;
  117. ymax = cv->height - 1;
  118. if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
  119. return 0;
  120. /* Draw edges */
  121. if(y1 >= 0)
  122. for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
  123. cucul_put_char(cv, x, y1, 0x2500); /* ─ */
  124. if(y2 <= ymax)
  125. for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
  126. cucul_put_char(cv, x, y2, 0x2500); /* ─ */
  127. if(x1 >= 0)
  128. for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
  129. cucul_put_char(cv, x1, y, 0x2502); /* │ */
  130. if(x2 <= xmax)
  131. for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
  132. cucul_put_char(cv, x2, y, 0x2502); /* │ */
  133. /* Draw corners */
  134. cucul_put_char(cv, x1, y1, 0x250c); /* ┌ */
  135. cucul_put_char(cv, x1, y2, 0x2514); /* └ */
  136. cucul_put_char(cv, x2, y1, 0x2510); /* ┐ */
  137. cucul_put_char(cv, x2, y2, 0x2518); /* ┘ */
  138. return 0;
  139. }
  140. /** \brief Fill a box on the canvas using the given character.
  141. *
  142. * This function never fails.
  143. *
  144. * \param cv The handle to the libcucul canvas.
  145. * \param x1 X coordinate of the upper-left corner of the box.
  146. * \param y1 Y coordinate of the upper-left corner of the box.
  147. * \param x2 X coordinate of the lower-right corner of the box.
  148. * \param y2 Y coordinate of the lower-right corner of the box.
  149. * \param ch UTF-32 character to be used to draw the box.
  150. * \return This function always returns 0.
  151. */
  152. int cucul_fill_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
  153. unsigned long int ch)
  154. {
  155. int x, y, xmax, ymax;
  156. if(x1 > x2)
  157. {
  158. int tmp = x1;
  159. x1 = x2; x2 = tmp;
  160. }
  161. if(y1 > y2)
  162. {
  163. int tmp = y1;
  164. y1 = y2; y2 = tmp;
  165. }
  166. xmax = cv->width - 1;
  167. ymax = cv->height - 1;
  168. if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
  169. return 0;
  170. if(x1 < 0) x1 = 0;
  171. if(y1 < 0) y1 = 0;
  172. if(x2 > xmax) x2 = xmax;
  173. if(y2 > ymax) y2 = ymax;
  174. for(y = y1; y <= y2; y++)
  175. for(x = x1; x <= x2; x++)
  176. cucul_put_char(cv, x, y, ch);
  177. return 0;
  178. }