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.
 
 
 
 
 
 

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