Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * libcaca Colour ASCII-Art library
  3. * Copyright (c) 2002-2010 Sam Hocevar <sam@hocevar.net>
  4. * All Rights Reserved
  5. *
  6. * This library is free software. It comes without any warranty, to
  7. * the extent permitted by applicable law. You can redistribute it
  8. * and/or modify it under the terms of the Do What The Fuck You Want
  9. * To Public License, Version 2, as published by Sam Hocevar. See
  10. * http://sam.zoy.org/wtfpl/COPYING for more details.
  11. */
  12. /*
  13. * This file contains box drawing functions, both filled and outline.
  14. */
  15. #include "config.h"
  16. #if !defined(__KERNEL__)
  17. # include <stdlib.h>
  18. #endif
  19. #include "caca.h"
  20. #include "caca_internals.h"
  21. static int draw_box(caca_canvas_t *cv, int x, int y, int w, int h,
  22. uint32_t const *chars);
  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 libcaca 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 caca_draw_box(caca_canvas_t *cv, int x, int y, int w, int h, uint32_t ch)
  36. {
  37. int x2 = x + w - 1;
  38. int y2 = y + h - 1;
  39. caca_draw_line(cv, x, y, x, y2, ch);
  40. caca_draw_line(cv, x, y2, x2, y2, ch);
  41. caca_draw_line(cv, x2, y2, x2, y, ch);
  42. caca_draw_line(cv, x2, y, x, y, 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 libcaca canvas.
  50. * \param x X coordinate of the upper-left corner of the box.
  51. * \param y Y coordinate of the upper-left corner of the box.
  52. * \param w Width of the box.
  53. * \param h Height of the box.
  54. * \return This function always returns 0.
  55. */
  56. int caca_draw_thin_box(caca_canvas_t *cv, int x, int y, int w, int h)
  57. {
  58. static uint32_t const ascii_chars[] =
  59. {
  60. '-', '|', ',', '`', '.', '\''
  61. };
  62. return draw_box(cv, x, y, w, h, ascii_chars);
  63. }
  64. /** \brief Draw a box on the canvas using CP437 characters.
  65. *
  66. * This function never fails.
  67. *
  68. * \param cv The handle to the libcaca canvas.
  69. * \param x X coordinate of the upper-left corner of the box.
  70. * \param y Y coordinate of the upper-left corner of the box.
  71. * \param w Width of the box.
  72. * \param h Height of the box.
  73. * \return This function always returns 0.
  74. */
  75. int caca_draw_cp437_box(caca_canvas_t *cv, int x, int y, int w, int h)
  76. {
  77. static uint32_t const cp437_chars[] =
  78. {
  79. /* ─ │ ┌ └ ┐ ┘ */
  80. 0x2500, 0x2502, 0x250c, 0x2514, 0x2510, 0x2518
  81. };
  82. return draw_box(cv, x, y, w, h, cp437_chars);
  83. }
  84. /** \brief Fill a box on the canvas using the given character.
  85. *
  86. * This function never fails.
  87. *
  88. * \param cv The handle to the libcaca canvas.
  89. * \param x X coordinate of the upper-left corner of the box.
  90. * \param y Y coordinate of the upper-left corner of the box.
  91. * \param w Width of the box.
  92. * \param h Height of the box.
  93. * \param ch UTF-32 character to be used to draw the box.
  94. * \return This function always returns 0.
  95. */
  96. int caca_fill_box(caca_canvas_t *cv, int x, int y, int w, int h,
  97. uint32_t ch)
  98. {
  99. int i, j, xmax, ymax;
  100. int x2 = x + w - 1;
  101. int y2 = y + h - 1;
  102. if(x > x2)
  103. {
  104. int tmp = x;
  105. x = x2; x2 = tmp;
  106. }
  107. if(y > y2)
  108. {
  109. int tmp = y;
  110. y = y2; y2 = tmp;
  111. }
  112. xmax = cv->width - 1;
  113. ymax = cv->height - 1;
  114. if(x2 < 0 || y2 < 0 || x > xmax || y > ymax)
  115. return 0;
  116. if(x < 0) x = 0;
  117. if(y < 0) y = 0;
  118. if(x2 > xmax) x2 = xmax;
  119. if(y2 > ymax) y2 = ymax;
  120. #if 0
  121. /* FIXME: this fails with fullwidth character blits. Also, the dirty
  122. * rectangle handling may miss fullwidth cells. */
  123. /* Optimise dirty rectangle handling, part 1 */
  124. cv->dirty_disabled++;
  125. #endif
  126. for(j = y; j <= y2; j++)
  127. for(i = x; i <= x2; i++)
  128. caca_put_char(cv, i, j, ch);
  129. #if 0
  130. /* Optimise dirty rectangle handling, part 2 */
  131. cv->dirty_disabled--;
  132. if(!cv->dirty_disabled)
  133. caca_add_dirty_rect(cv, x, y, x2 - x + 1, y2 - y + 1);
  134. #endif
  135. return 0;
  136. }
  137. /*
  138. * XXX: The following functions are local.
  139. */
  140. static int draw_box(caca_canvas_t *cv, int x, int y, int w, int h,
  141. uint32_t const *chars)
  142. {
  143. int i, j, xmax, ymax;
  144. int x2 = x + w - 1;
  145. int y2 = y + h - 1;
  146. if(x > x2)
  147. {
  148. int tmp = x;
  149. x = x2; x2 = tmp;
  150. }
  151. if(y > y2)
  152. {
  153. int tmp = y;
  154. y = y2; y2 = tmp;
  155. }
  156. xmax = cv->width - 1;
  157. ymax = cv->height - 1;
  158. if(x2 < 0 || y2 < 0 || x > xmax || y > ymax)
  159. return 0;
  160. /* Draw edges */
  161. if(y >= 0)
  162. for(i = x < 0 ? 1 : x + 1; i < x2 && i < xmax; i++)
  163. caca_put_char(cv, i, y, chars[0]);
  164. if(y2 <= ymax)
  165. for(i = x < 0 ? 1 : x + 1; i < x2 && i < xmax; i++)
  166. caca_put_char(cv, i, y2, chars[0]);
  167. if(x >= 0)
  168. for(j = y < 0 ? 1 : y + 1; j < y2 && j < ymax; j++)
  169. caca_put_char(cv, x, j, chars[1]);
  170. if(x2 <= xmax)
  171. for(j = y < 0 ? 1 : y + 1; j < y2 && j < ymax; j++)
  172. caca_put_char(cv, x2, j, chars[1]);
  173. /* Draw corners */
  174. caca_put_char(cv, x, y, chars[2]);
  175. caca_put_char(cv, x, y2, chars[3]);
  176. caca_put_char(cv, x2, y, chars[4]);
  177. caca_put_char(cv, x2, y2, chars[5]);
  178. return 0;
  179. }
  180. /*
  181. * XXX: The following functions are aliases.
  182. */
  183. int cucul_draw_box(cucul_canvas_t *, int, int, int, int, uint32_t)
  184. CACA_ALIAS(caca_draw_box);
  185. int cucul_draw_thin_box(cucul_canvas_t *, int, int, int, int)
  186. CACA_ALIAS(caca_draw_thin_box);
  187. int cucul_draw_cp437_box(cucul_canvas_t *, int, int, int, int)
  188. CACA_ALIAS(caca_draw_cp437_box);
  189. int cucul_fill_box(cucul_canvas_t *, int, int, int, int, uint32_t)
  190. CACA_ALIAS(caca_fill_box);