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.
 
 
 
 
 
 

154 lines
4.2 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. #if !defined(__KERNEL__)
  18. # include <stdlib.h>
  19. #endif
  20. #include "cucul.h"
  21. #include "cucul_internals.h"
  22. /** \brief Draw a box on the canvas using the given character.
  23. *
  24. * \param cv The handle to the libcucul canvas.
  25. * \param x1 X coordinate of the upper-left corner of the box.
  26. * \param y1 Y coordinate of the upper-left corner of the box.
  27. * \param x2 X coordinate of the lower-right corner of the box.
  28. * \param y2 Y coordinate of the lower-right corner of the box.
  29. * \param str UTF-8 string containing the character to use to draw the box.
  30. * \return void
  31. */
  32. void cucul_draw_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
  33. char const *str)
  34. {
  35. cucul_draw_line(cv, x1, y1, x1, y2, str);
  36. cucul_draw_line(cv, x1, y2, x2, y2, str);
  37. cucul_draw_line(cv, x2, y2, x2, y1, str);
  38. cucul_draw_line(cv, x2, y1, x1, y1, str);
  39. }
  40. /** \brief Draw a thin box on the canvas.
  41. *
  42. * \param cv The handle to the libcucul canvas.
  43. * \param x1 X coordinate of the upper-left corner of the box.
  44. * \param y1 Y coordinate of the upper-left corner of the box.
  45. * \param x2 X coordinate of the lower-right corner of the box.
  46. * \param y2 Y coordinate of the lower-right corner of the box.
  47. * \return void
  48. */
  49. void cucul_draw_thin_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2)
  50. {
  51. int x, y, xmax, ymax;
  52. if(x1 > x2)
  53. {
  54. int tmp = x1;
  55. x1 = x2; x2 = tmp;
  56. }
  57. if(y1 > y2)
  58. {
  59. int tmp = y1;
  60. y1 = y2; y2 = tmp;
  61. }
  62. xmax = cv->width - 1;
  63. ymax = cv->height - 1;
  64. if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
  65. return;
  66. /* Draw edges */
  67. if(y1 >= 0)
  68. for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
  69. _cucul_putchar32(cv, x, y1, (uint32_t)'-');
  70. if(y2 <= ymax)
  71. for(x = x1 < 0 ? 1 : x1 + 1; x < x2 && x < xmax; x++)
  72. _cucul_putchar32(cv, x, y2, (uint32_t)'-');
  73. if(x1 >= 0)
  74. for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
  75. _cucul_putchar32(cv, x1, y, (uint32_t)'|');
  76. if(x2 <= xmax)
  77. for(y = y1 < 0 ? 1 : y1 + 1; y < y2 && y < ymax; y++)
  78. _cucul_putchar32(cv, x2, y, (uint32_t)'|');
  79. /* Draw corners */
  80. if(x1 >= 0 && y1 >= 0)
  81. _cucul_putchar32(cv, x1, y1, (uint32_t)',');
  82. if(x1 >= 0 && y2 <= ymax)
  83. _cucul_putchar32(cv, x1, y2, (uint32_t)'`');
  84. if(x2 <= xmax && y1 >= 0)
  85. _cucul_putchar32(cv, x2, y1, (uint32_t)'.');
  86. if(x2 <= xmax && y2 <= ymax)
  87. _cucul_putchar32(cv, x2, y2, (uint32_t)'\'');
  88. }
  89. /** \brief Fill a box on the canvas using the given character.
  90. *
  91. * \param cv The handle to the libcucul canvas.
  92. * \param x1 X coordinate of the upper-left corner of the box.
  93. * \param y1 Y coordinate of the upper-left corner of the box.
  94. * \param x2 X coordinate of the lower-right corner of the box.
  95. * \param y2 Y coordinate of the lower-right corner of the box.
  96. * \param str UTF-8 string containing the character to fill the box with.
  97. * \return void
  98. */
  99. void cucul_fill_box(cucul_canvas_t *cv, int x1, int y1, int x2, int y2,
  100. char const *str)
  101. {
  102. int x, y, xmax, ymax;
  103. uint32_t ch;
  104. if(x1 > x2)
  105. {
  106. int tmp = x1;
  107. x1 = x2; x2 = tmp;
  108. }
  109. if(y1 > y2)
  110. {
  111. int tmp = y1;
  112. y1 = y2; y2 = tmp;
  113. }
  114. xmax = cv->width - 1;
  115. ymax = cv->height - 1;
  116. if(x2 < 0 || y2 < 0 || x1 > xmax || y1 > ymax)
  117. return;
  118. if(x1 < 0) x1 = 0;
  119. if(y1 < 0) y1 = 0;
  120. if(x2 > xmax) x2 = xmax;
  121. if(y2 > ymax) y2 = ymax;
  122. ch = _cucul_utf8_to_utf32(str);
  123. for(y = y1; y <= y2; y++)
  124. for(x = x1; x <= x2; x++)
  125. _cucul_putchar32(cv, x, y, ch);
  126. }