Rect.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. #include <LibIPC/Decoder.h>
  31. #include <LibIPC/Encoder.h>
  32. namespace Gfx {
  33. template<typename T>
  34. void Rect<T>::intersect(const Rect<T>& other)
  35. {
  36. T l = max(left(), other.left());
  37. T r = min(right(), other.right());
  38. T t = max(top(), other.top());
  39. T b = min(bottom(), other.bottom());
  40. if (l > r || t > b) {
  41. m_location = {};
  42. m_size = {};
  43. return;
  44. }
  45. m_location.set_x(l);
  46. m_location.set_y(t);
  47. m_size.set_width((r - l) + 1);
  48. m_size.set_height((b - t) + 1);
  49. }
  50. template<typename T>
  51. Rect<T> Rect<T>::united(const Rect<T>& other) const
  52. {
  53. if (is_null())
  54. return other;
  55. if (other.is_null())
  56. return *this;
  57. Rect<T> rect;
  58. rect.set_left(min(left(), other.left()));
  59. rect.set_top(min(top(), other.top()));
  60. rect.set_right(max(right(), other.right()));
  61. rect.set_bottom(max(bottom(), other.bottom()));
  62. return rect;
  63. }
  64. template<typename T>
  65. Vector<Rect<T>, 4> Rect<T>::shatter(const Rect<T>& hammer) const
  66. {
  67. Vector<Rect<T>, 4> pieces;
  68. if (!intersects(hammer)) {
  69. pieces.unchecked_append(*this);
  70. return pieces;
  71. }
  72. Rect<T> top_shard {
  73. x(),
  74. y(),
  75. width(),
  76. hammer.y() - y()
  77. };
  78. Rect<T> bottom_shard {
  79. x(),
  80. hammer.y() + hammer.height(),
  81. width(),
  82. (y() + height()) - (hammer.y() + hammer.height())
  83. };
  84. Rect<T> left_shard {
  85. x(),
  86. max(hammer.y(), y()),
  87. hammer.x() - x(),
  88. min((hammer.y() + hammer.height()), (y() + height())) - max(hammer.y(), y())
  89. };
  90. Rect<T> right_shard {
  91. hammer.x() + hammer.width(),
  92. max(hammer.y(), y()),
  93. right() - hammer.right(),
  94. min((hammer.y() + hammer.height()), (y() + height())) - max(hammer.y(), y())
  95. };
  96. if (!top_shard.is_empty())
  97. pieces.unchecked_append(top_shard);
  98. if (!bottom_shard.is_empty())
  99. pieces.unchecked_append(bottom_shard);
  100. if (!left_shard.is_empty())
  101. pieces.unchecked_append(left_shard);
  102. if (!right_shard.is_empty())
  103. pieces.unchecked_append(right_shard);
  104. return pieces;
  105. }
  106. template<typename T>
  107. void Rect<T>::align_within(const Rect<T>& other, TextAlignment alignment)
  108. {
  109. switch (alignment) {
  110. case TextAlignment::Center:
  111. center_within(other);
  112. return;
  113. case TextAlignment::TopLeft:
  114. set_location(other.location());
  115. return;
  116. case TextAlignment::TopRight:
  117. set_x(other.x() + other.width() - width());
  118. set_y(other.y());
  119. return;
  120. case TextAlignment::CenterLeft:
  121. set_x(other.x());
  122. center_vertically_within(other);
  123. return;
  124. case TextAlignment::CenterRight:
  125. set_x(other.x() + other.width() - width());
  126. center_vertically_within(other);
  127. return;
  128. case TextAlignment::BottomRight:
  129. set_x(other.x() + other.width() - width());
  130. set_y(other.y() + other.height() - height());
  131. return;
  132. }
  133. }
  134. template<typename T>
  135. void Rect<T>::set_size_around(const Size<T>& new_size, const Point<T>& fixed_point)
  136. {
  137. const T new_x = fixed_point.x() - (T)(new_size.width() * ((float)(fixed_point.x() - x()) / width()));
  138. const T new_y = fixed_point.y() - (T)(new_size.height() * ((float)(fixed_point.y() - y()) / height()));
  139. set_location({ new_x, new_y });
  140. set_size(new_size);
  141. }
  142. template<>
  143. String IntRect::to_string() const
  144. {
  145. return String::format("[%d,%d %dx%d]", x(), y(), width(), height());
  146. }
  147. template<>
  148. String FloatRect::to_string() const
  149. {
  150. return String::formatted("[{},{} {}x{}]", x(), y(), width(), height());
  151. }
  152. }
  153. namespace IPC {
  154. bool encode(Encoder& encoder, const Gfx::IntRect& rect)
  155. {
  156. encoder << rect.location() << rect.size();
  157. return true;
  158. }
  159. bool decode(Decoder& decoder, Gfx::IntRect& rect)
  160. {
  161. Gfx::IntPoint point;
  162. Gfx::IntSize size;
  163. if (!decoder.decode(point))
  164. return false;
  165. if (!decoder.decode(size))
  166. return false;
  167. rect = { point, size };
  168. return true;
  169. }
  170. }
  171. template class Gfx::Rect<int>;
  172. template class Gfx::Rect<float>;