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.
 
 
 
 
 
 

217 lines
5.8 KiB

  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. 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. #include "common.h"
  19. #if !defined(__KERNEL__)
  20. # include <stdlib.h>
  21. #endif
  22. #include "cucul.h"
  23. #include "cucul_internals.h"
  24. /** \brief Draw a box on the canvas using the given character.
  25. *
  26. * This function never fails.
  27. *
  28. * \param cv The handle to the libcucul canvas.
  29. * \param x1 X coordinate of the upper-left corner of the box.
  30. * \param y1 Y coordinate of the upper-left corner of the box.
  31. * \param x2 X coordinate of the lower-right corner of the box.
  32. * \param y2 Y coordinate of the lower-right corner of the box.
  33. * \param ch UTF-32 character to be used to draw the box.
  34. * \return This function always returns 0.
  35. */
  36. int cucul_draw_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
  37. unsigned long int ch)
  38. {
  39. cucul_draw_line(cv, x1, y1, x1, y2, ch);
  40. cucul_draw_line(cv, x1, y2, x2, y2, ch);
  41. cucul_draw_line(cv, x2, y2, x2, y1, ch);
  42. cucul_draw_line(cv, x2, y1, x1, y1, 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 libcucul canvas.
  50. * \param x1 X coordinate of the upper-left corner of the box.
  51. * \param y1 Y coordinate of the upper-left corner of the box.
  52. * \param x2 X coordinate of the lower-right corner of the box.
  53. * \param y2 Y coordinate of the lower-right corner of the box.
  54. * \return This function always returns 0.
  55. */
  56. int cucul_draw_thin_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2)
  57. {
  58. int x, y, xmax, ymax;
  59. if(x1 > x2)
  60. {
  61. int tmp = x1;
  62. x1 = x2; x2 = tmp;
  63. }
  64. if(y1 > y2)
  65. {
  66. int tmp = y1;
  67. y1 = y2; y2 = tmp;
  68. }
  69. xmax = cv->width - 1;
  70. ymax = cv->height - 1;
  71. if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
  72. return 0;
  73. /* Draw edges */
  74. if(y1 >= 0)
  75. for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
  76. cucul_put_char(cv, x, y1, '-');
  77. if(y2 <= ymax)
  78. for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
  79. cucul_put_char(cv, x, y2, '-');
  80. if(x1 >= 0)
  81. for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
  82. cucul_put_char(cv, x1, y, '|');
  83. if(x2 <= xmax)
  84. for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
  85. cucul_put_char(cv, x2, y, '|');
  86. /* Draw corners */
  87. cucul_put_char(cv, x1, y1, ',');
  88. cucul_put_char(cv, x1, y2, '`');
  89. cucul_put_char(cv, x2, y1, '.');
  90. cucul_put_char(cv, x2, y2, '\'');
  91. return 0;
  92. }
  93. /** \brief Draw a box on the canvas using CP437 characters.
  94. *
  95. * This function never fails.
  96. *
  97. * \param cv The handle to the libcucul canvas.
  98. * \param x1 X coordinate of the upper-left corner of the box.
  99. * \param y1 Y coordinate of the upper-left corner of the box.
  100. * \param x2 X coordinate of the lower-right corner of the box.
  101. * \param y2 Y coordinate of the lower-right corner of the box.
  102. * \return This function always returns 0.
  103. */
  104. int cucul_draw_cp437_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2)
  105. {
  106. int x, y, xmax, ymax;
  107. if(x1 > x2)
  108. {
  109. int tmp = x1;
  110. x1 = x2; x2 = tmp;
  111. }
  112. if(y1 > y2)
  113. {
  114. int tmp = y1;
  115. y1 = y2; y2 = tmp;
  116. }
  117. xmax = cv->width - 1;
  118. ymax = cv->height - 1;
  119. if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
  120. return 0;
  121. /* Draw edges */
  122. if(y1 >= 0)
  123. for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
  124. cucul_put_char(cv, x, y1, 0x2500); /* ─ */
  125. if(y2 <= ymax)
  126. for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
  127. cucul_put_char(cv, x, y2, 0x2500); /* ─ */
  128. if(x1 >= 0)
  129. for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
  130. cucul_put_char(cv, x1, y, 0x2502); /* │ */
  131. if(x2 <= xmax)
  132. for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
  133. cucul_put_char(cv, x2, y, 0x2502); /* │ */
  134. /* Draw corners */
  135. cucul_put_char(cv, x1, y1, 0x250c); /* ┌ */
  136. cucul_put_char(cv, x1, y2, 0x2514); /* └ */
  137. cucul_put_char(cv, x2, y1, 0x2510); /* ┐ */
  138. cucul_put_char(cv, x2, y2, 0x2518); /* ┘ */
  139. return 0;
  140. }
  141. /** \brief Fill a box on the canvas using the given character.
  142. *
  143. * This function never fails.
  144. *
  145. * \param cv The handle to the libcucul canvas.
  146. * \param x1 X coordinate of the upper-left corner of the box.
  147. * \param y1 Y coordinate of the upper-left corner of the box.
  148. * \param x2 X coordinate of the lower-right corner of the box.
  149. * \param y2 Y coordinate of the lower-right corner of the box.
  150. * \param ch UTF-32 character to be used to draw the box.
  151. * \return This function always returns 0.
  152. */
  153. int cucul_fill_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
  154. unsigned long int ch)
  155. {
  156. int x, y, xmax, ymax;
  157. if(x1 > x2)
  158. {
  159. int tmp = x1;
  160. x1 = x2; x2 = tmp;
  161. }
  162. if(y1 > y2)
  163. {
  164. int tmp = y1;
  165. y1 = y2; y2 = tmp;
  166. }
  167. xmax = cv->width - 1;
  168. ymax = cv->height - 1;
  169. if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
  170. return 0;
  171. if(x1 < 0) x1 = 0;
  172. if(y1 < 0) y1 = 0;
  173. if(x2 > xmax) x2 = xmax;
  174. if(y2 > ymax) y2 = ymax;
  175. for(y = y1; y <= y2; y++)
  176. for(x = x1; x <= x2; x++)
  177. cucul_put_char(cv, x, y, ch);
  178. return 0;
  179. }