#pragma once
#include "DOMMatrix2DInit.h"
#include "SVGMatrix.h"
#include "SVGTransformValue.h"
#include "SVGValueProperty.h"
namespace WebCore {
class SVGTransform : public SVGValueProperty<SVGTransformValue>, public SVGPropertyOwner {
public:
static Ref<SVGTransform> create(SVGTransformValue::SVGTransformType type)
{
return adoptRef(*new SVGTransform(type));
}
static Ref<SVGTransform> create(const AffineTransform& transform = { })
{
return adoptRef(*new SVGTransform(SVGTransformValue::SVG_TRANSFORM_MATRIX, transform));
}
static Ref<SVGTransform> create(const SVGTransformValue& value)
{
return adoptRef(*new SVGTransform(value.type(), value.matrix()->value(), value.angle(), value.rotationCenter()));
}
template<typename T>
static ExceptionOr<Ref<SVGTransform>> create(ExceptionOr<T>&& value)
{
if (value.hasException())
return value.releaseException();
return create(value.releaseReturnValue());
}
~SVGTransform()
{
m_value.matrix()->detach();
}
Ref<SVGTransform> clone() const
{
return SVGTransform::create(m_value);
}
unsigned short type() { return m_value.type(); }
float angle() { return m_value.angle(); }
const Ref<SVGMatrix>& matrix() { return m_value.matrix(); }
ExceptionOr<void> setMatrix(DOMMatrix2DInit&& matrixInit)
{
if (isReadOnly())
return Exception { NoModificationAllowedError };
AffineTransform transform;
if (matrixInit.a.hasValue())
transform.setA(matrixInit.a.value());
if (matrixInit.b.hasValue())
transform.setB(matrixInit.b.value());
if (matrixInit.c.hasValue())
transform.setC(matrixInit.c.value());
if (matrixInit.d.hasValue())
transform.setD(matrixInit.d.value());
if (matrixInit.e.hasValue())
transform.setE(matrixInit.e.value());
if (matrixInit.f.hasValue())
transform.setF(matrixInit.f.value());
m_value.setMatrix(transform);
commitChange();
return { };
}
ExceptionOr<void> setTranslate(float tx, float ty)
{
if (isReadOnly())
return Exception { NoModificationAllowedError };
m_value.setTranslate(tx, ty);
commitChange();
return { };
}
ExceptionOr<void> setScale(float sx, float sy)
{
if (isReadOnly())
return Exception { NoModificationAllowedError };
m_value.setScale(sx, sy);
commitChange();
return { };
}
ExceptionOr<void> setRotate(float angle, float cx, float cy)
{
if (isReadOnly())
return Exception { NoModificationAllowedError };
m_value.setRotate(angle, cx, cy);
commitChange();
return { };
}
ExceptionOr<void> setSkewX(float angle)
{
if (isReadOnly())
return Exception { NoModificationAllowedError };
m_value.setSkewX(angle);
commitChange();
return { };
}
ExceptionOr<void> setSkewY(float angle)
{
if (isReadOnly())
return Exception { NoModificationAllowedError };
m_value.setSkewY(angle);
commitChange();
return { };
}
void attach(SVGPropertyOwner* owner, SVGPropertyAccess access) override
{
Base::attach(owner, access);
m_value.matrix()->reattach(this, access);
}
void detach() override
{
Base::detach();
m_value.matrix()->reattach(this, access());
}
private:
using Base = SVGValueProperty<SVGTransformValue>;
SVGTransform(SVGTransformValue::SVGTransformType type, const AffineTransform& transform = { }, float angle = 0, const FloatPoint& rotationCenter = { })
: Base(SVGTransformValue(type, SVGMatrix::create(this, SVGPropertyAccess::ReadWrite, transform), angle, rotationCenter))
{
}
SVGPropertyOwner* owner() const override { return m_owner; }
void commitPropertyChange(SVGProperty* property) override
{
ASSERT_UNUSED(property, property == m_value.matrix().ptr());
if (owner())
owner()->commitPropertyChange(this);
m_value.matrixDidChange();
}
String valueAsString() const override
{
return m_value.valueAsString();
}
};
}