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.
 
 
 
 
 
 

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