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.
 
 
 
 
 
 

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