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.
 
 
 
 
 
 

218 lines
5.5 KiB

  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. static int draw_box(caca_canvas_t *cv, int x, int y, int w, int h,
  24. uint32_t const *chars);
  25. /** \brief Draw a box on the canvas using the given character.
  26. *
  27. * This function never fails.
  28. *
  29. * \param cv The handle to the libcaca canvas.
  30. * \param x X coordinate of the upper-left corner of the box.
  31. * \param y Y coordinate of the upper-left corner of the box.
  32. * \param w Width of the box.
  33. * \param h Height of the box.
  34. * \param ch UTF-32 character to be used to draw the box.
  35. * \return This function always returns 0.
  36. */
  37. int caca_draw_box(caca_canvas_t *cv, int x, int y, int w, int h, uint32_t ch)
  38. {
  39. int x2 = x + w - 1;
  40. int y2 = y + h - 1;
  41. caca_draw_line(cv, x, y, x, y2, ch);
  42. caca_draw_line(cv, x, y2, x2, y2, ch);
  43. caca_draw_line(cv, x2, y2, x2, y, ch);
  44. caca_draw_line(cv, x2, y, x, y, 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 libcaca 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 caca_draw_thin_box(caca_canvas_t *cv, int x, int y, int w, int h)
  59. {
  60. static uint32_t const ascii_chars[] =
  61. {
  62. '-', '|', ',', '`', '.', '\''
  63. };
  64. return draw_box(cv, x, y, w, h, ascii_chars);
  65. }
  66. /** \brief Draw a box on the canvas using CP437 characters.
  67. *
  68. * This function never fails.
  69. *
  70. * \param cv The handle to the libcaca canvas.
  71. * \param x X coordinate of the upper-left corner of the box.
  72. * \param y Y coordinate of the upper-left corner of the box.
  73. * \param w Width of the box.
  74. * \param h Height of the box.
  75. * \return This function always returns 0.
  76. */
  77. int caca_draw_cp437_box(caca_canvas_t *cv, int x, int y, int w, int h)
  78. {
  79. static uint32_t const cp437_chars[] =
  80. {
  81. /* ─ │ ┌ └ ┐ ┘ */
  82. 0x2500, 0x2502, 0x250c, 0x2514, 0x2510, 0x2518
  83. };
  84. return draw_box(cv, x, y, w, h, cp437_chars);
  85. }
  86. /** \brief Fill a box on the canvas using the given character.
  87. *
  88. * This function never fails.
  89. *
  90. * \param cv The handle to the libcaca canvas.
  91. * \param x X coordinate of the upper-left corner of the box.
  92. * \param y Y coordinate of the upper-left corner of the box.
  93. * \param w Width of the box.
  94. * \param h Height of the box.
  95. * \param ch UTF-32 character to be used to draw the box.
  96. * \return This function always returns 0.
  97. */
  98. int caca_fill_box(caca_canvas_t *cv, int x, int y, int w, int h,
  99. uint32_t ch)
  100. {
  101. int i, j, xmax, ymax;
  102. int x2 = x + w - 1;
  103. int y2 = y + h - 1;
  104. if(x > x2)
  105. {
  106. int tmp = x;
  107. x = x2; x2 = tmp;
  108. }
  109. if(y > y2)
  110. {
  111. int tmp = y;
  112. y = y2; y2 = tmp;
  113. }
  114. xmax = cv->width - 1;
  115. ymax = cv->height - 1;
  116. if(x2 < 0 || y2 < 0 || x > xmax || y > ymax)
  117. return 0;
  118. if(x < 0) x = 0;
  119. if(y < 0) y = 0;
  120. if(x2 > xmax) x2 = xmax;
  121. if(y2 > ymax) y2 = ymax;
  122. for(j = y; j <= y2; j++)
  123. for(i = x; i <= x2; i++)
  124. caca_put_char(cv, i, j, ch);
  125. return 0;
  126. }
  127. /*
  128. * XXX: The following functions are local.
  129. */
  130. static int draw_box(caca_canvas_t *cv, int x, int y, int w, int h,
  131. uint32_t const *chars)
  132. {
  133. int i, j, xmax, ymax;
  134. int x2 = x + w - 1;
  135. int y2 = y + h - 1;
  136. if(x > x2)
  137. {
  138. int tmp = x;
  139. x = x2; x2 = tmp;
  140. }
  141. if(y > y2)
  142. {
  143. int tmp = y;
  144. y = y2; y2 = tmp;
  145. }
  146. xmax = cv->width - 1;
  147. ymax = cv->height - 1;
  148. if(x2 < 0 || y2 < 0 || x > xmax || y > ymax)
  149. return 0;
  150. /* Draw edges */
  151. if(y >= 0)
  152. for(i = x < 0 ? 1 : x + 1; i < x2 && i < xmax; i++)
  153. caca_put_char(cv, i, y, chars[0]);
  154. if(y2 <= ymax)
  155. for(i = x < 0 ? 1 : x + 1; i < x2 && i < xmax; i++)
  156. caca_put_char(cv, i, y2, chars[0]);
  157. if(x >= 0)
  158. for(j = y < 0 ? 1 : y + 1; j < y2 && j < ymax; j++)
  159. caca_put_char(cv, x, j, chars[1]);
  160. if(x2 <= xmax)
  161. for(j = y < 0 ? 1 : y + 1; j < y2 && j < ymax; j++)
  162. caca_put_char(cv, x2, j, chars[1]);
  163. /* Draw corners */
  164. caca_put_char(cv, x, y, chars[2]);
  165. caca_put_char(cv, x, y2, chars[3]);
  166. caca_put_char(cv, x2, y, chars[4]);
  167. caca_put_char(cv, x2, y2, chars[5]);
  168. return 0;
  169. }
  170. /*
  171. * XXX: The following functions are aliases.
  172. */
  173. int cucul_draw_box(cucul_canvas_t *, int, int, int, int, uint32_t)
  174. CACA_ALIAS(caca_draw_box);
  175. int cucul_draw_thin_box(cucul_canvas_t *, int, int, int, int)
  176. CACA_ALIAS(caca_draw_thin_box);
  177. int cucul_draw_cp437_box(cucul_canvas_t *, int, int, int, int)
  178. CACA_ALIAS(caca_draw_cp437_box);
  179. int cucul_fill_box(cucul_canvas_t *, int, int, int, int, uint32_t)
  180. CACA_ALIAS(caca_fill_box);