Rect.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/StdLibExtras.h>
  27. #include <AK/String.h>
  28. #include <AK/Vector.h>
  29. #include <LibGfx/Rect.h>
  30. namespace Gfx {
  31. void Rect::intersect(const Rect& other)
  32. {
  33. int l = max(left(), other.left());
  34. int r = min(right(), other.right());
  35. int t = max(top(), other.top());
  36. int b = min(bottom(), other.bottom());
  37. if (l > r || t > b) {
  38. m_location = {};
  39. m_size = {};
  40. return;
  41. }
  42. m_location.set_x(l);
  43. m_location.set_y(t);
  44. m_size.set_width((r - l) + 1);
  45. m_size.set_height((b - t) + 1);
  46. }
  47. Rect Rect::united(const Rect& other) const
  48. {
  49. if (is_null())
  50. return other;
  51. if (other.is_null())
  52. return *this;
  53. Rect rect;
  54. rect.set_left(min(left(), other.left()));
  55. rect.set_top(min(top(), other.top()));
  56. rect.set_right(max(right(), other.right()));
  57. rect.set_bottom(max(bottom(), other.bottom()));
  58. return rect;
  59. }
  60. Vector<Rect, 4> Rect::shatter(const Rect& hammer) const
  61. {
  62. Vector<Rect, 4> pieces;
  63. if (!intersects(hammer)) {
  64. pieces.unchecked_append(*this);
  65. return pieces;
  66. }
  67. Rect top_shard {
  68. x(),
  69. y(),
  70. width(),
  71. hammer.y() - y()
  72. };
  73. Rect bottom_shard {
  74. x(),
  75. hammer.y() + hammer.height(),
  76. width(),
  77. (y() + height()) - (hammer.y() + hammer.height())
  78. };
  79. Rect left_shard {
  80. x(),
  81. max(hammer.y(), y()),
  82. hammer.x() - x(),
  83. min((hammer.y() + hammer.height()), (y() + height())) - max(hammer.y(), y())
  84. };
  85. Rect right_shard {
  86. hammer.x() + hammer.width(),
  87. max(hammer.y(), y()),
  88. right() - hammer.right(),
  89. min((hammer.y() + hammer.height()), (y() + height())) - max(hammer.y(), y())
  90. };
  91. if (intersects(top_shard))
  92. pieces.unchecked_append(top_shard);
  93. if (intersects(bottom_shard))
  94. pieces.unchecked_append(bottom_shard);
  95. if (intersects(left_shard))
  96. pieces.unchecked_append(left_shard);
  97. if (intersects(right_shard))
  98. pieces.unchecked_append(right_shard);
  99. return pieces;
  100. }
  101. void Rect::align_within(const Rect& other, TextAlignment alignment)
  102. {
  103. switch (alignment) {
  104. case TextAlignment::Center:
  105. center_within(other);
  106. return;
  107. case TextAlignment::TopLeft:
  108. set_location(other.location());
  109. return;
  110. case TextAlignment::TopRight:
  111. set_x(other.x() + other.width() - width());
  112. set_y(other.y());
  113. return;
  114. case TextAlignment::CenterLeft:
  115. set_x(other.x());
  116. center_vertically_within(other);
  117. return;
  118. case TextAlignment::CenterRight:
  119. set_x(other.x() + other.width() - width());
  120. center_vertically_within(other);
  121. return;
  122. }
  123. }
  124. String Rect::to_string() const
  125. {
  126. return String::format("[%d,%d %dx%d]", x(), y(), width(), height());
  127. }
  128. const LogStream& operator<<(const LogStream& stream, const Rect& value)
  129. {
  130. return stream << value.to_string();
  131. }
  132. }