SVGRadialGradientElement.cpp [plain text]
#include "config.h"
#if ENABLE(SVG)
#include "SVGRadialGradientElement.h"
#include "FloatConversion.h"
#include "FloatPoint.h"
#include "MappedAttribute.h"
#include "RadialGradientAttributes.h"
#include "RenderObject.h"
#include "SVGLength.h"
#include "SVGNames.h"
#include "SVGPaintServerRadialGradient.h"
#include "SVGStopElement.h"
#include "SVGTransform.h"
#include "SVGTransformList.h"
#include "SVGUnitTypes.h"
namespace WebCore {
SVGRadialGradientElement::SVGRadialGradientElement(const QualifiedName& tagName, Document* doc)
: SVGGradientElement(tagName, doc)
, m_cx(LengthModeWidth, "50%")
, m_cy(LengthModeHeight, "50%")
, m_r(LengthModeOther, "50%")
, m_fx(LengthModeWidth)
, m_fy(LengthModeHeight)
{
}
SVGRadialGradientElement::~SVGRadialGradientElement()
{
}
void SVGRadialGradientElement::parseMappedAttribute(MappedAttribute* attr)
{
if (attr->name() == SVGNames::cxAttr)
setCxBaseValue(SVGLength(LengthModeWidth, attr->value()));
else if (attr->name() == SVGNames::cyAttr)
setCyBaseValue(SVGLength(LengthModeHeight, attr->value()));
else if (attr->name() == SVGNames::rAttr) {
setRBaseValue(SVGLength(LengthModeOther, attr->value()));
if (rBaseValue().value(this) < 0.0)
document()->accessSVGExtensions()->reportError("A negative value for radial gradient radius <r> is not allowed");
} else if (attr->name() == SVGNames::fxAttr)
setFxBaseValue(SVGLength(LengthModeWidth, attr->value()));
else if (attr->name() == SVGNames::fyAttr)
setFyBaseValue(SVGLength(LengthModeHeight, attr->value()));
else
SVGGradientElement::parseMappedAttribute(attr);
}
void SVGRadialGradientElement::svgAttributeChanged(const QualifiedName& attrName)
{
SVGGradientElement::svgAttributeChanged(attrName);
if (!m_resource)
return;
if (attrName == SVGNames::cxAttr || attrName == SVGNames::cyAttr ||
attrName == SVGNames::fxAttr || attrName == SVGNames::fyAttr ||
attrName == SVGNames::rAttr)
m_resource->invalidate();
}
void SVGRadialGradientElement::synchronizeProperty(const QualifiedName& attrName)
{
SVGGradientElement::synchronizeProperty(attrName);
if (attrName == anyQName()) {
synchronizeCx();
synchronizeCy();
synchronizeFx();
synchronizeFy();
synchronizeR();
return;
}
if (attrName == SVGNames::cxAttr)
synchronizeCx();
else if (attrName == SVGNames::cyAttr)
synchronizeCy();
else if (attrName == SVGNames::fxAttr)
synchronizeFx();
else if (attrName == SVGNames::fyAttr)
synchronizeFy();
else if (attrName == SVGNames::rAttr)
synchronizeR();
}
void SVGRadialGradientElement::buildGradient() const
{
RadialGradientAttributes attributes = collectGradientProperties();
RefPtr<SVGPaintServerRadialGradient> radialGradient = WTF::static_pointer_cast<SVGPaintServerRadialGradient>(m_resource);
FloatPoint focalPoint;
FloatPoint centerPoint;
float radius;
if (attributes.boundingBoxMode()) {
focalPoint = FloatPoint(attributes.fx().valueAsPercentage(), attributes.fy().valueAsPercentage());
centerPoint = FloatPoint(attributes.cx().valueAsPercentage(), attributes.cy().valueAsPercentage());
radius = attributes.r().valueAsPercentage();
} else {
focalPoint = FloatPoint(attributes.fx().value(this), attributes.fy().value(this));
centerPoint = FloatPoint(attributes.cx().value(this), attributes.cy().value(this));
radius = attributes.r().value(this);
}
FloatPoint adjustedFocalPoint = focalPoint;
float dfx = focalPoint.x() - centerPoint.x();
float dfy = focalPoint.y() - centerPoint.y();
float rMax = 0.99f * radius;
if (sqrt(dfx * dfx + dfy * dfy) > rMax) {
float angle = atan2f(dfy, dfx);
dfx = cosf(angle) * rMax;
dfy = sinf(angle) * rMax;
adjustedFocalPoint = FloatPoint(dfx + centerPoint.x(), dfy + centerPoint.y());
}
RefPtr<Gradient> gradient = Gradient::create(
adjustedFocalPoint,
0.f, centerPoint,
radius);
gradient->setSpreadMethod(attributes.spreadMethod());
Vector<SVGGradientStop> stops = attributes.stops();
float previousOffset = 0.0f;
for (unsigned i = 0; i < stops.size(); ++i) {
float offset = std::min(std::max(previousOffset, stops[i].first), 1.0f);
previousOffset = offset;
gradient->addColorStop(offset, stops[i].second);
}
radialGradient->setGradient(gradient);
if (attributes.stops().isEmpty())
return;
radialGradient->setBoundingBoxMode(attributes.boundingBoxMode());
radialGradient->setGradientTransform(attributes.gradientTransform());
radialGradient->setGradientCenter(centerPoint);
radialGradient->setGradientFocal(focalPoint);
radialGradient->setGradientRadius(radius);
radialGradient->setGradientStops(attributes.stops());
}
RadialGradientAttributes SVGRadialGradientElement::collectGradientProperties() const
{
RadialGradientAttributes attributes;
HashSet<const SVGGradientElement*> processedGradients;
bool isRadial = true;
const SVGGradientElement* current = this;
while (current) {
if (!attributes.hasSpreadMethod() && current->hasAttribute(SVGNames::spreadMethodAttr))
attributes.setSpreadMethod((GradientSpreadMethod) current->spreadMethod());
if (!attributes.hasBoundingBoxMode() && current->hasAttribute(SVGNames::gradientUnitsAttr))
attributes.setBoundingBoxMode(current->gradientUnits() == SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX);
if (!attributes.hasGradientTransform() && current->hasAttribute(SVGNames::gradientTransformAttr))
attributes.setGradientTransform(current->gradientTransform()->consolidate().matrix());
if (!attributes.hasStops()) {
const Vector<SVGGradientStop>& stops(current->buildStops());
if (!stops.isEmpty())
attributes.setStops(stops);
}
if (isRadial) {
const SVGRadialGradientElement* radial = static_cast<const SVGRadialGradientElement*>(current);
if (!attributes.hasCx() && current->hasAttribute(SVGNames::cxAttr))
attributes.setCx(radial->cx());
if (!attributes.hasCy() && current->hasAttribute(SVGNames::cyAttr))
attributes.setCy(radial->cy());
if (!attributes.hasR() && current->hasAttribute(SVGNames::rAttr))
attributes.setR(radial->r());
if (!attributes.hasFx() && current->hasAttribute(SVGNames::fxAttr))
attributes.setFx(radial->fx());
if (!attributes.hasFy() && current->hasAttribute(SVGNames::fyAttr))
attributes.setFy(radial->fy());
}
processedGradients.add(current);
Node* refNode = ownerDocument()->getElementById(SVGURIReference::getTarget(current->href()));
if (refNode && (refNode->hasTagName(SVGNames::radialGradientTag) || refNode->hasTagName(SVGNames::linearGradientTag))) {
current = static_cast<const SVGGradientElement*>(const_cast<const Node*>(refNode));
if (processedGradients.contains(current))
return RadialGradientAttributes();
isRadial = current->gradientType() == RadialGradientPaintServer;
} else
current = 0;
}
if (!attributes.hasFx())
attributes.setFx(attributes.cx());
if (!attributes.hasFy())
attributes.setFy(attributes.cy());
return attributes;
}
}
#endif // ENABLE(SVG)