SVGGradientElement.cpp [plain text]
#include "config.h"
#include "SVGGradientElement.h"
#include "ElementIterator.h"
#include "RenderSVGHiddenContainer.h"
#include "RenderSVGResourceLinearGradient.h"
#include "RenderSVGResourceRadialGradient.h"
#include "SVGNames.h"
#include "SVGStopElement.h"
#include "SVGTransformable.h"
#include "StyleResolver.h"
#include <wtf/IsoMallocInlines.h>
#include <wtf/NeverDestroyed.h>
namespace WebCore {
WTF_MAKE_ISO_ALLOCATED_IMPL(SVGGradientElement);
SVGGradientElement::SVGGradientElement(const QualifiedName& tagName, Document& document)
: SVGElement(tagName, document)
, SVGURIReference(this)
{
static std::once_flag onceFlag;
std::call_once(onceFlag, [] {
PropertyRegistry::registerProperty<SVGNames::spreadMethodAttr, SVGSpreadMethodType, &SVGGradientElement::m_spreadMethod>();
PropertyRegistry::registerProperty<SVGNames::gradientUnitsAttr, SVGUnitTypes::SVGUnitType, &SVGGradientElement::m_gradientUnits>();
PropertyRegistry::registerProperty<SVGNames::gradientTransformAttr, &SVGGradientElement::m_gradientTransform>();
});
}
void SVGGradientElement::parseAttribute(const QualifiedName& name, const AtomString& value)
{
if (name == SVGNames::gradientUnitsAttr) {
auto propertyValue = SVGPropertyTraits<SVGUnitTypes::SVGUnitType>::fromString(value);
if (propertyValue > 0)
m_gradientUnits->setBaseValInternal<SVGUnitTypes::SVGUnitType>(propertyValue);
return;
}
if (name == SVGNames::gradientTransformAttr) {
m_gradientTransform->baseVal()->parse(value);
return;
}
if (name == SVGNames::spreadMethodAttr) {
auto propertyValue = SVGPropertyTraits<SVGSpreadMethodType>::fromString(value);
if (propertyValue > 0)
m_spreadMethod->setBaseValInternal<SVGSpreadMethodType>(propertyValue);
return;
}
SVGElement::parseAttribute(name, value);
SVGURIReference::parseAttribute(name, value);
}
void SVGGradientElement::svgAttributeChanged(const QualifiedName& attrName)
{
if (PropertyRegistry::isKnownAttribute(attrName) || SVGURIReference::isKnownAttribute(attrName)) {
InstanceInvalidationGuard guard(*this);
if (RenderObject* object = renderer())
object->setNeedsLayout();
return;
}
SVGElement::svgAttributeChanged(attrName);
}
void SVGGradientElement::childrenChanged(const ChildChange& change)
{
SVGElement::childrenChanged(change);
if (change.source == ChildChangeSource::Parser)
return;
if (RenderObject* object = renderer())
object->setNeedsLayout();
}
Vector<Gradient::ColorStop> SVGGradientElement::buildStops()
{
Vector<Gradient::ColorStop> stops;
float previousOffset = 0.0f;
for (auto& stop : childrenOfType<SVGStopElement>(*this)) {
const Color& color = stop.stopColorIncludingOpacity();
float offset = stop.offset();
offset = std::min(std::max(previousOffset, offset), 1.0f);
previousOffset = offset;
stops.append(Gradient::ColorStop(offset, color));
}
return stops;
}
}