Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

156 строки
4.1 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. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Do What The Fuck You Want To
  8. * Public License, Version 2, as published by Sam Hocevar. See
  9. * http://sam.zoy.org/wtfpl/COPYING for more details.
  10. */
  11. /** \file box.c
  12. * \version \$Id$
  13. * \author Sam Hocevar <sam@zoy.org>
  14. * \brief Simple box drawing
  15. *
  16. * This file contains box drawing functions, both filled and outline.
  17. */
  18. #include "config.h"
  19. #if !defined(__KERNEL__)
  20. # include <stdlib.h>
  21. #endif
  22. #include "cucul.h"
  23. #include "cucul_internals.h"
  24. /**
  25. * \brief Draw a box on the screen using the given character.
  26. *
  27. * \param x1 X coordinate of the upper-left corner of the box.
  28. * \param y1 Y coordinate of the upper-left corner of the box.
  29. * \param x2 X coordinate of the lower-right corner of the box.
  30. * \param y2 Y coordinate of the lower-right corner of the box.
  31. * \param str UTF-8 string containing the character to use to draw the box.
  32. * \return void
  33. */
  34. void cucul_draw_box(cucul_t *qq, int x1, int y1, int x2, int y2,
  35. char const *str)
  36. {
  37. cucul_draw_line(qq, x1, y1, x1, y2, str);
  38. cucul_draw_line(qq, x1, y2, x2, y2, str);
  39. cucul_draw_line(qq, x2, y2, x2, y1, str);
  40. cucul_draw_line(qq, x2, y1, x1, y1, str);
  41. }
  42. /**
  43. * \brief Draw a thin box on the screen.
  44. *
  45. * \param x1 X coordinate of the upper-left corner of the box.
  46. * \param y1 Y coordinate of the upper-left corner of the box.
  47. * \param x2 X coordinate of the lower-right corner of the box.
  48. * \param y2 Y coordinate of the lower-right corner of the box.
  49. * \return void
  50. */
  51. void cucul_draw_thin_box(cucul_t *qq, int x1, int y1, int x2, int y2)
  52. {
  53. int x, y, xmax, ymax;
  54. if(x1 > x2)
  55. {
  56. int tmp = x1;
  57. x1 = x2; x2 = tmp;
  58. }
  59. if(y1 > y2)
  60. {
  61. int tmp = y1;
  62. y1 = y2; y2 = tmp;
  63. }
  64. xmax = qq->width - 1;
  65. ymax = qq->height - 1;
  66. if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
  67. return;
  68. /* Draw edges */
  69. if(y1 >= 0)
  70. for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
  71. _cucul_putchar32(qq, x, y1, (uint32_t)'-');
  72. if(y2 <= ymax)
  73. for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
  74. _cucul_putchar32(qq, x, y2, (uint32_t)'-');
  75. if(x1 >= 0)
  76. for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
  77. _cucul_putchar32(qq, x1, y, (uint32_t)'|');
  78. if(x2 <= xmax)
  79. for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
  80. _cucul_putchar32(qq, x2, y, (uint32_t)'|');
  81. /* Draw corners */
  82. if(x1 >= 0 && y1 >= 0)
  83. _cucul_putchar32(qq, x1, y1, (uint32_t)',');
  84. if(x1 >= 0 && y2 <= ymax)
  85. _cucul_putchar32(qq, x1, y2, (uint32_t)'`');
  86. if(x2 <= xmax && y1 >= 0)
  87. _cucul_putchar32(qq, x2, y1, (uint32_t)'.');
  88. if(x2 <= xmax && y2 <= ymax)
  89. _cucul_putchar32(qq, x2, y2, (uint32_t)'\'');
  90. }
  91. /**
  92. * \brief Fill a box on the screen using the given character.
  93. *
  94. * \param x1 X coordinate of the upper-left corner of the box.
  95. * \param y1 Y coordinate of the upper-left corner of the box.
  96. * \param x2 X coordinate of the lower-right corner of the box.
  97. * \param y2 Y coordinate of the lower-right corner of the box.
  98. * \param str UTF-8 string containing the character to fill the box with.
  99. * \return void
  100. */
  101. void cucul_fill_box(cucul_t *qq, int x1, int y1, int x2, int y2,
  102. char const *str)
  103. {
  104. int x, y, xmax, ymax;
  105. uint32_t c;
  106. if(x1 > x2)
  107. {
  108. int tmp = x1;
  109. x1 = x2; x2 = tmp;
  110. }
  111. if(y1 > y2)
  112. {
  113. int tmp = y1;
  114. y1 = y2; y2 = tmp;
  115. }
  116. xmax = qq->width - 1;
  117. ymax = qq->height - 1;
  118. if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
  119. return;
  120. if(x1 < 0) x1 = 0;
  121. if(y1 < 0) y1 = 0;
  122. if(x2 > xmax) x2 = xmax;
  123. if(y2 > ymax) y2 = ymax;
  124. c = _cucul_utf8_to_utf32(str);
  125. for(y = y1; y <= y2; y++)
  126. for(x = x1; x <= x2; x++)
  127. _cucul_putchar32(qq, x, y, c);
  128. }