#ifndef LayoutSize_h
#define LayoutSize_h
#include "platform/LayoutUnit.h"
#include "platform/geometry/FloatSize.h"
#include "platform/geometry/IntSize.h"
namespace WebCore {
class LayoutPoint;
enum AspectRatioFit {
AspectRatioFitShrink,
AspectRatioFitGrow
};
class LayoutSize {
public:
LayoutSize() { }
LayoutSize(const IntSize& size) : m_width(size.width()), m_height(size.height()) { }
LayoutSize(LayoutUnit width, LayoutUnit height) : m_width(width), m_height(height) { }
explicit LayoutSize(const FloatSize& size) : m_width(size.width()), m_height(size.height()) { }
LayoutUnit width() const { return m_width; }
LayoutUnit height() const { return m_height; }
void setWidth(LayoutUnit width) { m_width = width; }
void setHeight(LayoutUnit height) { m_height = height; }
bool isEmpty() const { return m_width.rawValue() <= 0 || m_height.rawValue() <= 0; }
bool isZero() const { return !m_width && !m_height; }
float aspectRatio() const { return m_width.toFloat() / m_height.toFloat(); }
void expand(LayoutUnit width, LayoutUnit height)
{
m_width += width;
m_height += height;
}
void shrink(LayoutUnit width, LayoutUnit height)
{
m_width -= width;
m_height -= height;
}
void scale(float scale)
{
m_width *= scale;
m_height *= scale;
}
void scale(float widthScale, float heightScale)
{
m_width *= widthScale;
m_height *= heightScale;
}
LayoutSize expandedTo(const LayoutSize& other) const
{
return LayoutSize(m_width > other.m_width ? m_width : other.m_width,
m_height > other.m_height ? m_height : other.m_height);
}
LayoutSize shrunkTo(const LayoutSize& other) const
{
return LayoutSize(m_width < other.m_width ? m_width : other.m_width,
m_height < other.m_height ? m_height : other.m_height);
}
void clampNegativeToZero()
{
*this = expandedTo(LayoutSize());
}
void clampToMinimumSize(const LayoutSize& minimumSize)
{
if (m_width < minimumSize.width())
m_width = minimumSize.width();
if (m_height < minimumSize.height())
m_height = minimumSize.height();
}
LayoutSize transposedSize() const
{
return LayoutSize(m_height, m_width);
}
LayoutSize fitToAspectRatio(const LayoutSize& aspectRatio, AspectRatioFit fit) const
{
float heightScale = height().toFloat() / aspectRatio.height().toFloat();
float widthScale = width().toFloat() / aspectRatio.width().toFloat();
if ((widthScale > heightScale) != (fit == AspectRatioFitGrow))
return LayoutSize(height() * aspectRatio.width() / aspectRatio.height(), height());
return LayoutSize(width(), width() * aspectRatio.height() / aspectRatio.width());
}
LayoutSize fraction() const
{
return LayoutSize(m_width.fraction(), m_height.fraction());
}
private:
LayoutUnit m_width, m_height;
};
inline LayoutSize& operator+=(LayoutSize& a, const LayoutSize& b)
{
a.setWidth(a.width() + b.width());
a.setHeight(a.height() + b.height());
return a;
}
inline LayoutSize& operator-=(LayoutSize& a, const LayoutSize& b)
{
a.setWidth(a.width() - b.width());
a.setHeight(a.height() - b.height());
return a;
}
inline LayoutSize operator+(const LayoutSize& a, const LayoutSize& b)
{
return LayoutSize(a.width() + b.width(), a.height() + b.height());
}
inline LayoutSize operator-(const LayoutSize& a, const LayoutSize& b)
{
return LayoutSize(a.width() - b.width(), a.height() - b.height());
}
inline LayoutSize operator-(const LayoutSize& size)
{
return LayoutSize(-size.width(), -size.height());
}
inline bool operator==(const LayoutSize& a, const LayoutSize& b)
{
return a.width() == b.width() && a.height() == b.height();
}
inline bool operator!=(const LayoutSize& a, const LayoutSize& b)
{
return a.width() != b.width() || a.height() != b.height();
}
inline IntSize flooredIntSize(const LayoutSize& s)
{
return IntSize(s.width().floor(), s.height().floor());
}
inline IntSize roundedIntSize(const LayoutSize& s)
{
return IntSize(s.width().round(), s.height().round());
}
inline LayoutSize roundedLayoutSize(const FloatSize& s)
{
return LayoutSize(s);
}
}
#endif