Rect.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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/Vector.h>
  28. #include <LibGfx/Rect.h>
  29. namespace Gfx {
  30. void Rect::intersect(const Rect& other)
  31. {
  32. int l = max(left(), other.left());
  33. int r = min(right(), other.right());
  34. int t = max(top(), other.top());
  35. int b = min(bottom(), other.bottom());
  36. if (l > r || t > b) {
  37. m_location = {};
  38. m_size = {};
  39. return;
  40. }
  41. m_location.set_x(l);
  42. m_location.set_y(t);
  43. m_size.set_width((r - l) + 1);
  44. m_size.set_height((b - t) + 1);
  45. }
  46. Rect Rect::united(const Rect& other) const
  47. {
  48. if (is_null())
  49. return other;
  50. if (other.is_null())
  51. return *this;
  52. Rect rect;
  53. rect.set_left(min(left(), other.left()));
  54. rect.set_top(min(top(), other.top()));
  55. rect.set_right(max(right(), other.right()));
  56. rect.set_bottom(max(bottom(), other.bottom()));
  57. return rect;
  58. }
  59. Vector<Rect, 4> Rect::shatter(const Rect& hammer) const
  60. {
  61. Vector<Rect, 4> pieces;
  62. if (!intersects(hammer)) {
  63. pieces.unchecked_append(*this);
  64. return pieces;
  65. }
  66. Rect top_shard {
  67. x(),
  68. y(),
  69. width(),
  70. hammer.y() - y()
  71. };
  72. Rect bottom_shard {
  73. x(),
  74. hammer.y() + hammer.height(),
  75. width(),
  76. (y() + height()) - (hammer.y() + hammer.height())
  77. };
  78. Rect left_shard {
  79. x(),
  80. max(hammer.y(), y()),
  81. hammer.x() - x(),
  82. min((hammer.y() + hammer.height()), (y() + height())) - max(hammer.y(), y())
  83. };
  84. Rect right_shard {
  85. hammer.x() + hammer.width(),
  86. max(hammer.y(), y()),
  87. right() - hammer.right(),
  88. min((hammer.y() + hammer.height()), (y() + height())) - max(hammer.y(), y())
  89. };
  90. if (intersects(top_shard))
  91. pieces.unchecked_append(top_shard);
  92. if (intersects(bottom_shard))
  93. pieces.unchecked_append(bottom_shard);
  94. if (intersects(left_shard))
  95. pieces.unchecked_append(left_shard);
  96. if (intersects(right_shard))
  97. pieces.unchecked_append(right_shard);
  98. return pieces;
  99. }
  100. void Rect::align_within(const Rect& other, TextAlignment alignment)
  101. {
  102. switch (alignment) {
  103. case TextAlignment::Center:
  104. center_within(other);
  105. return;
  106. case TextAlignment::TopLeft:
  107. set_location(other.location());
  108. return;
  109. case TextAlignment::TopRight:
  110. set_x(other.x() + other.width() - width());
  111. set_y(other.y());
  112. return;
  113. case TextAlignment::CenterLeft:
  114. set_x(other.x());
  115. center_vertically_within(other);
  116. return;
  117. case TextAlignment::CenterRight:
  118. set_x(other.x() + other.width() - width());
  119. center_vertically_within(other);
  120. return;
  121. }
  122. }
  123. }