SVGLinearGradientElement.cpp [plain text]
#include "config.h"
#include "SVGLinearGradientElement.h"
#include "Document.h"
#include "FloatPoint.h"
#include "LinearGradientAttributes.h"
#include "RenderSVGResourceLinearGradient.h"
#include "SVGLengthValue.h"
#include "SVGNames.h"
#include "SVGUnitTypes.h"
#include <wtf/IsoMallocInlines.h>
#include <wtf/NeverDestroyed.h>
namespace WebCore {
WTF_MAKE_ISO_ALLOCATED_IMPL(SVGLinearGradientElement);
inline SVGLinearGradientElement::SVGLinearGradientElement(const QualifiedName& tagName, Document& document)
: SVGGradientElement(tagName, document)
{
ASSERT(hasTagName(SVGNames::linearGradientTag));
registerAttributes();
}
Ref<SVGLinearGradientElement> SVGLinearGradientElement::create(const QualifiedName& tagName, Document& document)
{
return adoptRef(*new SVGLinearGradientElement(tagName, document));
}
void SVGLinearGradientElement::registerAttributes()
{
auto& registry = attributeRegistry();
if (!registry.isEmpty())
return;
registry.registerAttribute<SVGNames::x1Attr, &SVGLinearGradientElement::m_x1>();
registry.registerAttribute<SVGNames::y1Attr, &SVGLinearGradientElement::m_y1>();
registry.registerAttribute<SVGNames::x2Attr, &SVGLinearGradientElement::m_x2>();
registry.registerAttribute<SVGNames::y2Attr, &SVGLinearGradientElement::m_y2>();
}
void SVGLinearGradientElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
{
SVGParsingError parseError = NoError;
if (name == SVGNames::x1Attr)
m_x1.setValue(SVGLengthValue::construct(LengthModeWidth, value, parseError));
else if (name == SVGNames::y1Attr)
m_y1.setValue(SVGLengthValue::construct(LengthModeHeight, value, parseError));
else if (name == SVGNames::x2Attr)
m_x2.setValue(SVGLengthValue::construct(LengthModeWidth, value, parseError));
else if (name == SVGNames::y2Attr)
m_y2.setValue(SVGLengthValue::construct(LengthModeHeight, value, parseError));
reportAttributeParsingError(parseError, name, value);
SVGGradientElement::parseAttribute(name, value);
}
void SVGLinearGradientElement::svgAttributeChanged(const QualifiedName& attrName)
{
if (isKnownAttribute(attrName)) {
InstanceInvalidationGuard guard(*this);
updateRelativeLengthsInformation();
if (RenderObject* object = renderer())
object->setNeedsLayout();
return;
}
SVGGradientElement::svgAttributeChanged(attrName);
}
RenderPtr<RenderElement> SVGLinearGradientElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition&)
{
return createRenderer<RenderSVGResourceLinearGradient>(*this, WTFMove(style));
}
static void setGradientAttributes(SVGGradientElement& element, LinearGradientAttributes& attributes, bool isLinear = true)
{
if (!attributes.hasSpreadMethod() && element.hasAttribute(SVGNames::spreadMethodAttr))
attributes.setSpreadMethod(element.spreadMethod());
if (!attributes.hasGradientUnits() && element.hasAttribute(SVGNames::gradientUnitsAttr))
attributes.setGradientUnits(element.gradientUnits());
if (!attributes.hasGradientTransform() && element.hasAttribute(SVGNames::gradientTransformAttr)) {
AffineTransform transform;
element.gradientTransform().concatenate(transform);
attributes.setGradientTransform(transform);
}
if (!attributes.hasStops()) {
const Vector<Gradient::ColorStop>& stops(element.buildStops());
if (!stops.isEmpty())
attributes.setStops(stops);
}
if (isLinear) {
SVGLinearGradientElement& linear = downcast<SVGLinearGradientElement>(element);
if (!attributes.hasX1() && element.hasAttribute(SVGNames::x1Attr))
attributes.setX1(linear.x1());
if (!attributes.hasY1() && element.hasAttribute(SVGNames::y1Attr))
attributes.setY1(linear.y1());
if (!attributes.hasX2() && element.hasAttribute(SVGNames::x2Attr))
attributes.setX2(linear.x2());
if (!attributes.hasY2() && element.hasAttribute(SVGNames::y2Attr))
attributes.setY2(linear.y2());
}
}
bool SVGLinearGradientElement::collectGradientAttributes(LinearGradientAttributes& attributes)
{
if (!renderer())
return false;
HashSet<Ref<SVGGradientElement>> processedGradients;
Ref<SVGGradientElement> current { *this };
setGradientAttributes(current.get(), attributes);
processedGradients.add(current.copyRef());
while (true) {
auto target = SVGURIReference::targetElementFromIRIString(current->href(), treeScope());
if (is<SVGGradientElement>(target.element)) {
current = downcast<SVGGradientElement>(*target.element);
if (processedGradients.contains(current))
return true;
if (!current->renderer())
return false;
setGradientAttributes(current.get(), attributes, current->hasTagName(SVGNames::linearGradientTag));
processedGradients.add(current.copyRef());
} else
return true;
}
ASSERT_NOT_REACHED();
return false;
}
bool SVGLinearGradientElement::selfHasRelativeLengths() const
{
return x1().isRelative()
|| y1().isRelative()
|| x2().isRelative()
|| y2().isRelative();
}
}