#include "config.h"
#include "LayoutRect.h"
#include "FloatRect.h"
#include "LayoutUnit.h"
#include <algorithm>
using std::max;
using std::min;
namespace WebCore {
LayoutRect::LayoutRect(const FloatRect& r)
: m_location(LayoutPoint(r.location()))
, m_size(LayoutSize(r.size()))
{
}
bool LayoutRect::intersects(const LayoutRect& other) const
{
return !isEmpty() && !other.isEmpty()
&& x() < other.maxX() && other.x() < maxX()
&& y() < other.maxY() && other.y() < maxY();
}
bool LayoutRect::contains(const LayoutRect& other) const
{
return x() <= other.x() && maxX() >= other.maxX()
&& y() <= other.y() && maxY() >= other.maxY();
}
void LayoutRect::intersect(const LayoutRect& other)
{
LayoutPoint newLocation(max(x(), other.x()), max(y(), other.y()));
LayoutPoint newMaxPoint(min(maxX(), other.maxX()), min(maxY(), other.maxY()));
if (newLocation.x() >= newMaxPoint.x() || newLocation.y() >= newMaxPoint.y()) {
newLocation = LayoutPoint(0, 0);
newMaxPoint = LayoutPoint(0, 0);
}
m_location = newLocation;
m_size = newMaxPoint - newLocation;
}
void LayoutRect::unite(const LayoutRect& other)
{
if (other.isEmpty())
return;
if (isEmpty()) {
*this = other;
return;
}
LayoutPoint newLocation(min(x(), other.x()), min(y(), other.y()));
LayoutPoint newMaxPoint(max(maxX(), other.maxX()), max(maxY(), other.maxY()));
m_location = newLocation;
m_size = newMaxPoint - newLocation;
}
void LayoutRect::uniteIfNonZero(const LayoutRect& other)
{
if (!other.width() && !other.height())
return;
if (!width() && !height()) {
*this = other;
return;
}
LayoutPoint newLocation(min(x(), other.x()), min(y(), other.y()));
LayoutPoint newMaxPoint(max(maxX(), other.maxX()), max(maxY(), other.maxY()));
m_location = newLocation;
m_size = newMaxPoint - newLocation;
}
void LayoutRect::scale(float s)
{
m_location.scale(s, s);
m_size.scale(s);
}
LayoutRect unionRect(const Vector<LayoutRect>& rects)
{
LayoutRect result;
size_t count = rects.size();
for (size_t i = 0; i < count; ++i)
result.unite(rects[i]);
return result;
}
IntRect enclosingIntRect(const LayoutRect& rect)
{
IntPoint location = flooredIntPoint(rect.minXMinYCorner());
IntPoint maxPoint = ceiledIntPoint(rect.maxXMaxYCorner());
return IntRect(location, maxPoint - location);
}
LayoutRect enclosingLayoutRect(const FloatRect& rect)
{
#if ENABLE(SUBPIXEL_LAYOUT)
LayoutPoint location = flooredLayoutPoint(rect.minXMinYCorner());
LayoutPoint maxPoint = ceiledLayoutPoint(rect.maxXMaxYCorner());
return LayoutRect(location, maxPoint - location);
#else
return enclosingIntRect(rect);
#endif
}
}