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.
 
 
 
 
 
 

167 line
4.4 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; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. /*
  14. * This file contains box drawing functions, both filled and outline.
  15. */
  16. #include "config.h"
  17. #include "common.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 x1 X coordinate of the upper-left corner of the box.
  29. * \param y1 Y coordinate of the upper-left corner of the box.
  30. * \param x2 X coordinate of the lower-right corner of the box.
  31. * \param y2 Y coordinate of the lower-right corner of the box.
  32. * \param str UTF-8 string containing the character to use 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 x2, int y2,
  36. char const *str)
  37. {
  38. cucul_draw_line(cv, x1, y1, x1, y2, str);
  39. cucul_draw_line(cv, x1, y2, x2, y2, str);
  40. cucul_draw_line(cv, x2, y2, x2, y1, str);
  41. cucul_draw_line(cv, x2, y1, x1, y1, str);
  42. return 0;
  43. }
  44. /** \brief Draw a thin box on the canvas.
  45. *
  46. * This function never fails.
  47. *
  48. * \param cv The handle to the libcucul canvas.
  49. * \param x1 X coordinate of the upper-left corner of the box.
  50. * \param y1 Y coordinate of the upper-left corner of the box.
  51. * \param x2 X coordinate of the lower-right corner of the box.
  52. * \param y2 Y coordinate of the lower-right corner of the box.
  53. * \return This function always returns 0.
  54. */
  55. int cucul_draw_thin_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2)
  56. {
  57. int x, y, xmax, ymax;
  58. if(x1 > x2)
  59. {
  60. int tmp = x1;
  61. x1 = x2; x2 = tmp;
  62. }
  63. if(y1 > y2)
  64. {
  65. int tmp = y1;
  66. y1 = y2; y2 = tmp;
  67. }
  68. xmax = cv->width - 1;
  69. ymax = cv->height - 1;
  70. if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
  71. return 0;
  72. /* Draw edges */
  73. if(y1 >= 0)
  74. for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
  75. _cucul_putchar32(cv, x, y1, (uint32_t)'-');
  76. if(y2 <= ymax)
  77. for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
  78. _cucul_putchar32(cv, x, y2, (uint32_t)'-');
  79. if(x1 >= 0)
  80. for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
  81. _cucul_putchar32(cv, x1, y, (uint32_t)'|');
  82. if(x2 <= xmax)
  83. for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
  84. _cucul_putchar32(cv, x2, y, (uint32_t)'|');
  85. /* Draw corners */
  86. if(x1 >= 0 && y1 >= 0)
  87. _cucul_putchar32(cv, x1, y1, (uint32_t)',');
  88. if(x1 >= 0 && y2 <= ymax)
  89. _cucul_putchar32(cv, x1, y2, (uint32_t)'`');
  90. if(x2 <= xmax && y1 >= 0)
  91. _cucul_putchar32(cv, x2, y1, (uint32_t)'.');
  92. if(x2 <= xmax && y2 <= ymax)
  93. _cucul_putchar32(cv, x2, y2, (uint32_t)'\'');
  94. return 0;
  95. }
  96. /** \brief Fill a box on the canvas using the given character.
  97. *
  98. * This function never fails.
  99. *
  100. * \param cv The handle to the libcucul canvas.
  101. * \param x1 X coordinate of the upper-left corner of the box.
  102. * \param y1 Y coordinate of the upper-left corner of the box.
  103. * \param x2 X coordinate of the lower-right corner of the box.
  104. * \param y2 Y coordinate of the lower-right corner of the box.
  105. * \param str UTF-8 string containing the character to fill the box with.
  106. * \return This function always returns 0.
  107. */
  108. int cucul_fill_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
  109. char const *str)
  110. {
  111. int x, y, xmax, ymax;
  112. uint32_t ch;
  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. if(x1 < 0) x1 = 0;
  128. if(y1 < 0) y1 = 0;
  129. if(x2 > xmax) x2 = xmax;
  130. if(y2 > ymax) y2 = ymax;
  131. ch = _cucul_utf8_to_utf32(str);
  132. for(y = y1; y <= y2; y++)
  133. for(x = x1; x <= x2; x++)
  134. _cucul_putchar32(cv, x, y, ch);
  135. return 0;
  136. }