#include "config.h"
#include "BasicShapes.h"
#include "BasicShapeFunctions.h"
#include "CalculationValue.h"
#include "FloatRect.h"
#include "FloatRoundedRect.h"
#include "LengthFunctions.h"
#include "Path.h"
#include "RenderBox.h"
namespace WebCore {
void BasicShapeCenterCoordinate::updateComputedLength()
{
if (m_direction == TopLeft) {
m_computedLength = m_length.isUndefined() ? Length(0, Fixed) : m_length;
return;
}
if (m_length.isUndefined()) {
m_computedLength = Length(100, Percent);
return;
}
auto lhs = std::make_unique<CalcExpressionLength>(Length(100, Percent));
auto rhs = std::make_unique<CalcExpressionLength>(m_length);
auto op = std::make_unique<CalcExpressionBinaryOperation>(WTF::move(lhs), WTF::move(rhs), CalcSubtract);
m_computedLength = Length(CalculationValue::create(WTF::move(op), CalculationRangeAll));
}
bool BasicShape::canBlend(const BasicShape* other) const
{
if (type() != other->type())
return false;
if (type() == BasicShape::BasicShapePolygonType
&& (static_cast<const BasicShapePolygon*>(this)->values().size() != static_cast<const BasicShapePolygon*>(other)->values().size()
|| static_cast<const BasicShapePolygon*>(this)->windRule() != static_cast<const BasicShapePolygon*>(other)->windRule()))
return false;
if (type() == BasicShape::BasicShapeCircleType) {
const BasicShapeCircle* thisCircle = static_cast<const BasicShapeCircle*>(this);
const BasicShapeCircle* otherCircle = static_cast<const BasicShapeCircle*>(other);
if (!thisCircle->radius().canBlend(otherCircle->radius()))
return false;
}
if (type() != BasicShape::BasicShapeEllipseType)
return true;
const BasicShapeEllipse* thisEllipse = static_cast<const BasicShapeEllipse*>(this);
const BasicShapeEllipse* otherEllipse = static_cast<const BasicShapeEllipse*>(other);
return (thisEllipse->radiusX().canBlend(otherEllipse->radiusX())
&& thisEllipse->radiusY().canBlend(otherEllipse->radiusY()));
}
float BasicShapeCircle::floatValueForRadiusInBox(float boxWidth, float boxHeight) const
{
if (m_radius.type() == BasicShapeRadius::Value)
return floatValueForLength(m_radius.value(), sqrtf((boxWidth * boxWidth + boxHeight * boxHeight) / 2));
float centerX = floatValueForCenterCoordinate(m_centerX, boxWidth);
float centerY = floatValueForCenterCoordinate(m_centerY, boxHeight);
if (m_radius.type() == BasicShapeRadius::ClosestSide)
return std::min(std::min(centerX, boxWidth - centerX), std::min(centerY, boxHeight - centerY));
return std::max(std::max(centerX, boxWidth - centerX), std::max(centerY, boxHeight - centerY));
}
void BasicShapeCircle::path(Path& path, const FloatRect& boundingBox)
{
ASSERT(path.isEmpty());
float centerX = floatValueForCenterCoordinate(m_centerX, boundingBox.width());
float centerY = floatValueForCenterCoordinate(m_centerY, boundingBox.height());
float radius = floatValueForRadiusInBox(boundingBox.width(), boundingBox.height());
path.addEllipse(FloatRect(
centerX - radius + boundingBox.x(),
centerY - radius + boundingBox.y(),
radius * 2,
radius * 2
));
}
PassRefPtr<BasicShape> BasicShapeCircle::blend(const BasicShape* other, double progress) const
{
ASSERT(type() == other->type());
const BasicShapeCircle* o = static_cast<const BasicShapeCircle*>(other);
RefPtr<BasicShapeCircle> result = BasicShapeCircle::create();
result->setCenterX(m_centerX.blend(o->centerX(), progress));
result->setCenterY(m_centerY.blend(o->centerY(), progress));
result->setRadius(m_radius.blend(o->radius(), progress));
return result.release();
}
float BasicShapeEllipse::floatValueForRadiusInBox(const BasicShapeRadius& radius, float center, float boxWidthOrHeight) const
{
if (radius.type() == BasicShapeRadius::Value)
return floatValueForLength(radius.value(), boxWidthOrHeight);
if (radius.type() == BasicShapeRadius::ClosestSide)
return std::min(center, boxWidthOrHeight - center);
ASSERT(radius.type() == BasicShapeRadius::FarthestSide);
return std::max(center, boxWidthOrHeight - center);
}
void BasicShapeEllipse::path(Path& path, const FloatRect& boundingBox)
{
ASSERT(path.isEmpty());
float centerX = floatValueForCenterCoordinate(m_centerX, boundingBox.width());
float centerY = floatValueForCenterCoordinate(m_centerY, boundingBox.height());
float radiusX = floatValueForRadiusInBox(m_radiusX, centerX, boundingBox.width());
float radiusY = floatValueForRadiusInBox(m_radiusY, centerY, boundingBox.height());
path.addEllipse(FloatRect(
centerX - radiusX + boundingBox.x(),
centerY - radiusY + boundingBox.y(),
radiusX * 2,
radiusY * 2));
}
PassRefPtr<BasicShape> BasicShapeEllipse::blend(const BasicShape* other, double progress) const
{
ASSERT(type() == other->type());
const BasicShapeEllipse* o = static_cast<const BasicShapeEllipse*>(other);
RefPtr<BasicShapeEllipse> result = BasicShapeEllipse::create();
if (m_radiusX.type() != BasicShapeRadius::Value || o->radiusX().type() != BasicShapeRadius::Value
|| m_radiusY.type() != BasicShapeRadius::Value || o->radiusY().type() != BasicShapeRadius::Value) {
result->setCenterX(o->centerX());
result->setCenterY(o->centerY());
result->setRadiusX(o->radiusX());
result->setRadiusY(o->radiusY());
return result;
}
result->setCenterX(m_centerX.blend(o->centerX(), progress));
result->setCenterY(m_centerY.blend(o->centerY(), progress));
result->setRadiusX(m_radiusX.blend(o->radiusX(), progress));
result->setRadiusY(m_radiusY.blend(o->radiusY(), progress));
return result.release();
}
void BasicShapePolygon::path(Path& path, const FloatRect& boundingBox)
{
ASSERT(path.isEmpty());
ASSERT(!(m_values.size() % 2));
size_t length = m_values.size();
if (!length)
return;
path.moveTo(FloatPoint(floatValueForLength(m_values.at(0), boundingBox.width()) + boundingBox.x(),
floatValueForLength(m_values.at(1), boundingBox.height()) + boundingBox.y()));
for (size_t i = 2; i < length; i = i + 2) {
path.addLineTo(FloatPoint(floatValueForLength(m_values.at(i), boundingBox.width()) + boundingBox.x(),
floatValueForLength(m_values.at(i + 1), boundingBox.height()) + boundingBox.y()));
}
path.closeSubpath();
}
PassRefPtr<BasicShape> BasicShapePolygon::blend(const BasicShape* other, double progress) const
{
ASSERT(type() == other->type());
const BasicShapePolygon* o = static_cast<const BasicShapePolygon*>(other);
ASSERT(m_values.size() == o->values().size());
ASSERT(!(m_values.size() % 2));
size_t length = m_values.size();
RefPtr<BasicShapePolygon> result = BasicShapePolygon::create();
if (!length)
return result.release();
result->setWindRule(o->windRule());
for (size_t i = 0; i < length; i = i + 2) {
result->appendPoint(m_values.at(i).blend(o->values().at(i), progress),
m_values.at(i + 1).blend(o->values().at(i + 1), progress));
}
return result.release();
}
static FloatSize floatSizeForLengthSize(const LengthSize& lengthSize, const FloatRect& boundingBox)
{
return FloatSize(floatValueForLength(lengthSize.width(), boundingBox.width()),
floatValueForLength(lengthSize.height(), boundingBox.height()));
}
void BasicShapeInset::path(Path& path, const FloatRect& boundingBox)
{
ASSERT(path.isEmpty());
float left = floatValueForLength(m_left, boundingBox.width());
float top = floatValueForLength(m_top, boundingBox.height());
FloatRoundedRect r = FloatRoundedRect(
FloatRect(
left + boundingBox.x(),
top + boundingBox.y(),
std::max<float>(boundingBox.width() - left - floatValueForLength(m_right, boundingBox.width()), 0),
std::max<float>(boundingBox.height() - top - floatValueForLength(m_bottom, boundingBox.height()), 0)
),
floatSizeForLengthSize(m_topLeftRadius, boundingBox),
floatSizeForLengthSize(m_topRightRadius, boundingBox),
floatSizeForLengthSize(m_bottomLeftRadius, boundingBox),
floatSizeForLengthSize(m_bottomRightRadius, boundingBox)
);
path.addRoundedRect(r);
}
PassRefPtr<BasicShape> BasicShapeInset::blend(const BasicShape* other, double progress) const
{
ASSERT(type() == other->type());
const BasicShapeInset* o = static_cast<const BasicShapeInset*>(other);
RefPtr<BasicShapeInset> result = BasicShapeInset::create();
result->setTop(m_top.blend(o->top(), progress));
result->setRight(m_right.blend(o->right(), progress));
result->setBottom(m_bottom.blend(o->bottom(), progress));
result->setLeft(m_left.blend(o->left(), progress));
result->setTopLeftRadius(m_topLeftRadius.blend(o->topLeftRadius(), progress));
result->setTopRightRadius(m_topRightRadius.blend(o->topRightRadius(), progress));
result->setBottomRightRadius(m_bottomRightRadius.blend(o->bottomRightRadius(), progress));
result->setBottomLeftRadius(m_bottomLeftRadius.blend(o->bottomLeftRadius(), progress));
return result.release();
}
}