/external/webkit/Source/WebCore/platform/graphics/FloatRect.cpp

https://gitlab.com/brian0218/rk3188_r-box_android4.2.2_sdk · C++ · 208 lines · 143 code · 35 blank · 30 comment · 20 complexity · a050a5e718e92228fcff155fc12d9cd3 MD5 · raw file

  1. /*
  2. * Copyright (C) 2003, 2006, 2007 Apple Inc. All rights reserved.
  3. * Copyright (C) 2005 Nokia. 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
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
  15. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
  18. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  21. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  22. * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (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 "config.h"
  27. #include "FloatRect.h"
  28. #include "FloatConversion.h"
  29. #include "IntRect.h"
  30. #include <algorithm>
  31. #include <math.h>
  32. #include <wtf/MathExtras.h>
  33. using std::max;
  34. using std::min;
  35. namespace WebCore {
  36. FloatRect::FloatRect(const IntRect& r) : m_location(r.location()), m_size(r.size())
  37. {
  38. }
  39. FloatRect FloatRect::narrowPrecision(double x, double y, double width, double height)
  40. {
  41. return FloatRect(narrowPrecisionToFloat(x), narrowPrecisionToFloat(y), narrowPrecisionToFloat(width), narrowPrecisionToFloat(height));
  42. }
  43. bool FloatRect::intersects(const FloatRect& other) const
  44. {
  45. // Checking emptiness handles negative widths as well as zero.
  46. return !isEmpty() && !other.isEmpty()
  47. && x() < other.maxX() && other.x() < maxX()
  48. && y() < other.maxY() && other.y() < maxY();
  49. }
  50. bool FloatRect::contains(const FloatRect& other) const
  51. {
  52. return x() <= other.x() && maxX() >= other.maxX()
  53. && y() <= other.y() && maxY() >= other.maxY();
  54. }
  55. void FloatRect::intersect(const FloatRect& other)
  56. {
  57. float l = max(x(), other.x());
  58. float t = max(y(), other.y());
  59. float r = min(maxX(), other.maxX());
  60. float b = min(maxY(), other.maxY());
  61. // Return a clean empty rectangle for non-intersecting cases.
  62. if (l >= r || t >= b) {
  63. l = 0;
  64. t = 0;
  65. r = 0;
  66. b = 0;
  67. }
  68. setLocationAndSizeFromEdges(l, t, r, b);
  69. }
  70. void FloatRect::unite(const FloatRect& other)
  71. {
  72. // Handle empty special cases first.
  73. if (other.isEmpty())
  74. return;
  75. if (isEmpty()) {
  76. *this = other;
  77. return;
  78. }
  79. float l = min(x(), other.x());
  80. float t = min(y(), other.y());
  81. float r = max(maxX(), other.maxX());
  82. float b = max(maxY(), other.maxY());
  83. setLocationAndSizeFromEdges(l, t, r, b);
  84. }
  85. void FloatRect::uniteIfNonZero(const FloatRect& other)
  86. {
  87. // Handle empty special cases first.
  88. if (!other.width() && !other.height())
  89. return;
  90. if (!width() && !height()) {
  91. *this = other;
  92. return;
  93. }
  94. float left = min(x(), other.x());
  95. float top = min(y(), other.y());
  96. float right = max(maxX(), other.maxX());
  97. float bottom = max(maxY(), other.maxY());
  98. setLocationAndSizeFromEdges(left, top, right, bottom);
  99. }
  100. void FloatRect::scale(float sx, float sy)
  101. {
  102. m_location.setX(x() * sx);
  103. m_location.setY(y() * sy);
  104. m_size.setWidth(width() * sx);
  105. m_size.setHeight(height() * sy);
  106. }
  107. void FloatRect::fitToPoints(const FloatPoint& p0, const FloatPoint& p1)
  108. {
  109. float left = min(p0.x(), p1.x());
  110. float top = min(p0.y(), p1.y());
  111. float right = max(p0.x(), p1.x());
  112. float bottom = max(p0.y(), p1.y());
  113. setLocationAndSizeFromEdges(left, top, right, bottom);
  114. }
  115. namespace {
  116. // Helpers for 3- and 4-way max and min.
  117. template <typename T>
  118. T min3(const T& v1, const T& v2, const T& v3)
  119. {
  120. return min(min(v1, v2), v3);
  121. }
  122. template <typename T>
  123. T max3(const T& v1, const T& v2, const T& v3)
  124. {
  125. return max(max(v1, v2), v3);
  126. }
  127. template <typename T>
  128. T min4(const T& v1, const T& v2, const T& v3, const T& v4)
  129. {
  130. return min(min(v1, v2), min(v3, v4));
  131. }
  132. template <typename T>
  133. T max4(const T& v1, const T& v2, const T& v3, const T& v4)
  134. {
  135. return max(max(v1, v2), max(v3, v4));
  136. }
  137. } // anonymous namespace
  138. void FloatRect::fitToPoints(const FloatPoint& p0, const FloatPoint& p1, const FloatPoint& p2)
  139. {
  140. float left = min3(p0.x(), p1.x(), p2.x());
  141. float top = min3(p0.y(), p1.y(), p2.y());
  142. float right = max3(p0.x(), p1.x(), p2.x());
  143. float bottom = max3(p0.y(), p1.y(), p2.y());
  144. setLocationAndSizeFromEdges(left, top, right, bottom);
  145. }
  146. void FloatRect::fitToPoints(const FloatPoint& p0, const FloatPoint& p1, const FloatPoint& p2, const FloatPoint& p3)
  147. {
  148. float left = min4(p0.x(), p1.x(), p2.x(), p3.x());
  149. float top = min4(p0.y(), p1.y(), p2.y(), p3.y());
  150. float right = max4(p0.x(), p1.x(), p2.x(), p3.x());
  151. float bottom = max4(p0.y(), p1.y(), p2.y(), p3.y());
  152. setLocationAndSizeFromEdges(left, top, right, bottom);
  153. }
  154. IntRect enclosingIntRect(const FloatRect& rect)
  155. {
  156. float left = floorf(rect.x());
  157. float top = floorf(rect.y());
  158. float width = ceilf(rect.maxX()) - left;
  159. float height = ceilf(rect.maxY()) - top;
  160. return IntRect(clampToInteger(left), clampToInteger(top),
  161. clampToInteger(width), clampToInteger(height));
  162. }
  163. FloatRect mapRect(const FloatRect& r, const FloatRect& srcRect, const FloatRect& destRect)
  164. {
  165. if (srcRect.width() == 0 || srcRect.height() == 0)
  166. return FloatRect();
  167. float widthScale = destRect.width() / srcRect.width();
  168. float heightScale = destRect.height() / srcRect.height();
  169. return FloatRect(destRect.x() + (r.x() - srcRect.x()) * widthScale,
  170. destRect.y() + (r.y() - srcRect.y()) * heightScale,
  171. r.width() * widthScale, r.height() * heightScale);
  172. }
  173. }