#ifndef IntRect_h
#define IntRect_h
#include "platform/geometry/IntPoint.h"
#include "wtf/FastAllocBase.h"
#include "wtf/Vector.h"
#if OS(MACOSX)
typedef struct CGRect CGRect;
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#endif
#endif
struct SkRect;
struct SkIRect;
namespace WebCore {
class FloatRect;
class LayoutRect;
class PLATFORM_EXPORT IntRect {
WTF_MAKE_FAST_ALLOCATED;
public:
IntRect() { }
IntRect(const IntPoint& location, const IntSize& size)
: m_location(location), m_size(size) { }
IntRect(int x, int y, int width, int height)
: m_location(IntPoint(x, y)), m_size(IntSize(width, height)) { }
explicit IntRect(const FloatRect&);
explicit IntRect(const LayoutRect&);
IntPoint location() const { return m_location; }
IntSize size() const { return m_size; }
void setLocation(const IntPoint& location) { m_location = location; }
void setSize(const IntSize& size) { m_size = size; }
int x() const { return m_location.x(); }
int y() const { return m_location.y(); }
int maxX() const { return x() + width(); }
int maxY() const { return y() + height(); }
int width() const { return m_size.width(); }
int height() const { return m_size.height(); }
void setX(int x) { m_location.setX(x); }
void setY(int y) { m_location.setY(y); }
void setWidth(int width) { m_size.setWidth(width); }
void setHeight(int height) { m_size.setHeight(height); }
bool isEmpty() const { return m_size.isEmpty(); }
IntPoint center() const { return IntPoint(x() + width() / 2, y() + height() / 2); }
void move(const IntSize& size) { m_location += size; }
void moveBy(const IntPoint& offset) { m_location.move(offset.x(), offset.y()); }
void move(int dx, int dy) { m_location.move(dx, dy); }
void expand(const IntSize& size) { m_size += size; }
void expand(int dw, int dh) { m_size.expand(dw, dh); }
void contract(const IntSize& size) { m_size -= size; }
void contract(int dw, int dh) { m_size.expand(-dw, -dh); }
void shiftXEdgeTo(int edge)
{
int delta = edge - x();
setX(edge);
setWidth(std::max(0, width() - delta));
}
void shiftMaxXEdgeTo(int edge)
{
int delta = edge - maxX();
setWidth(std::max(0, width() + delta));
}
void shiftYEdgeTo(int edge)
{
int delta = edge - y();
setY(edge);
setHeight(std::max(0, height() - delta));
}
void shiftMaxYEdgeTo(int edge)
{
int delta = edge - maxY();
setHeight(std::max(0, height() + delta));
}
IntPoint minXMinYCorner() const { return m_location; }
IntPoint maxXMinYCorner() const { return IntPoint(m_location.x() + m_size.width(), m_location.y()); }
IntPoint minXMaxYCorner() const { return IntPoint(m_location.x(), m_location.y() + m_size.height()); }
IntPoint maxXMaxYCorner() const { return IntPoint(m_location.x() + m_size.width(), m_location.y() + m_size.height()); }
bool intersects(const IntRect&) const;
bool contains(const IntRect&) const;
bool contains(int px, int py) const
{ return px >= x() && px < maxX() && py >= y() && py < maxY(); }
bool contains(const IntPoint& point) const { return contains(point.x(), point.y()); }
void intersect(const IntRect&);
void unite(const IntRect&);
void uniteIfNonZero(const IntRect&);
void inflateX(int dx)
{
m_location.setX(m_location.x() - dx);
m_size.setWidth(m_size.width() + dx + dx);
}
void inflateY(int dy)
{
m_location.setY(m_location.y() - dy);
m_size.setHeight(m_size.height() + dy + dy);
}
void inflate(int d) { inflateX(d); inflateY(d); }
void scale(float s);
IntSize differenceToPoint(const IntPoint&) const;
int distanceSquaredToPoint(const IntPoint& p) const { return differenceToPoint(p).diagonalLengthSquared(); }
IntRect transposedRect() const { return IntRect(m_location.transposedPoint(), m_size.transposedSize()); }
#if OS(MACOSX)
operator CGRect() const;
#if defined(__OBJC__) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
operator NSRect() const;
#endif
#endif
operator SkRect() const;
operator SkIRect() const;
private:
IntPoint m_location;
IntSize m_size;
};
inline IntRect intersection(const IntRect& a, const IntRect& b)
{
IntRect c = a;
c.intersect(b);
return c;
}
inline IntRect unionRect(const IntRect& a, const IntRect& b)
{
IntRect c = a;
c.unite(b);
return c;
}
PLATFORM_EXPORT IntRect unionRect(const Vector<IntRect>&);
inline bool operator==(const IntRect& a, const IntRect& b)
{
return a.location() == b.location() && a.size() == b.size();
}
inline bool operator!=(const IntRect& a, const IntRect& b)
{
return a.location() != b.location() || a.size() != b.size();
}
#if OS(MACOSX)
PLATFORM_EXPORT IntRect enclosingIntRect(const CGRect&);
#if defined(__OBJC__) && !defined(NSGEOMETRY_TYPES_SAME_AS_CGGEOMETRY_TYPES)
PLATFORM_EXPORT IntRect enclosingIntRect(const NSRect&);
#endif
#endif
}
#endif