SVGLinearGradientElement.cpp [plain text]
#include "config.h"
#if ENABLE(SVG)
#include "SVGLinearGradientElement.h"
#include "Document.h"
#include "FloatPoint.h"
#include "LinearGradientAttributes.h"
#include "SVGLength.h"
#include "SVGNames.h"
#include "SVGPaintServerLinearGradient.h"
#include "SVGTransform.h"
#include "SVGTransformList.h"
#include "SVGUnitTypes.h"
namespace WebCore {
SVGLinearGradientElement::SVGLinearGradientElement(const QualifiedName& tagName, Document* doc)
: SVGGradientElement(tagName, doc)
, m_x1(this, SVGNames::x1Attr, LengthModeWidth)
, m_y1(this, SVGNames::y1Attr, LengthModeHeight)
, m_x2(this, SVGNames::x2Attr, LengthModeWidth, "100%")
, m_y2(this, SVGNames::y2Attr, LengthModeHeight)
{
}
SVGLinearGradientElement::~SVGLinearGradientElement()
{
}
void SVGLinearGradientElement::parseMappedAttribute(MappedAttribute* attr)
{
if (attr->name() == SVGNames::x1Attr)
setX1BaseValue(SVGLength(LengthModeWidth, attr->value()));
else if (attr->name() == SVGNames::y1Attr)
setY1BaseValue(SVGLength(LengthModeHeight, attr->value()));
else if (attr->name() == SVGNames::x2Attr)
setX2BaseValue(SVGLength(LengthModeWidth, attr->value()));
else if (attr->name() == SVGNames::y2Attr)
setY2BaseValue(SVGLength(LengthModeHeight, attr->value()));
else
SVGGradientElement::parseMappedAttribute(attr);
}
void SVGLinearGradientElement::svgAttributeChanged(const QualifiedName& attrName)
{
SVGGradientElement::svgAttributeChanged(attrName);
if (!m_resource)
return;
if (attrName == SVGNames::x1Attr || attrName == SVGNames::y1Attr ||
attrName == SVGNames::x2Attr || attrName == SVGNames::y2Attr)
m_resource->invalidate();
}
void SVGLinearGradientElement::buildGradient() const
{
LinearGradientAttributes attributes = collectGradientProperties();
RefPtr<SVGPaintServerLinearGradient> linearGradient = WTF::static_pointer_cast<SVGPaintServerLinearGradient>(m_resource);
FloatPoint startPoint = FloatPoint::narrowPrecision(attributes.x1(), attributes.y1());
FloatPoint endPoint = FloatPoint::narrowPrecision(attributes.x2(), attributes.y2());
RefPtr<Gradient> gradient = Gradient::create(startPoint, endPoint);
Vector<SVGGradientStop> m_stops = attributes.stops();
float previousOffset = 0.0f;
for (unsigned i = 0; i < m_stops.size(); ++i) {
float offset = std::min(std::max(previousOffset, m_stops[i].first), 1.0f);
previousOffset = offset;
gradient->addColorStop(offset, m_stops[i].second);
}
linearGradient->setGradient(gradient);
if (attributes.stops().isEmpty())
return;
linearGradient->setBoundingBoxMode(attributes.boundingBoxMode());
linearGradient->setGradientStops(attributes.stops());
linearGradient->setGradientSpreadMethod(attributes.spreadMethod());
linearGradient->setGradientTransform(attributes.gradientTransform());
linearGradient->setGradientStart(startPoint);
linearGradient->setGradientEnd(endPoint);
}
LinearGradientAttributes SVGLinearGradientElement::collectGradientProperties() const
{
LinearGradientAttributes attributes;
HashSet<const SVGGradientElement*> processedGradients;
bool isLinear = 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 (isLinear) {
const SVGLinearGradientElement* linear = static_cast<const SVGLinearGradientElement*>(current);
if (!attributes.hasX1() && current->hasAttribute(SVGNames::x1Attr))
attributes.setX1(linear->x1().valueAsPercentage());
if (!attributes.hasY1() && current->hasAttribute(SVGNames::y1Attr))
attributes.setY1(linear->y1().valueAsPercentage());
if (!attributes.hasX2() && current->hasAttribute(SVGNames::x2Attr))
attributes.setX2(linear->x2().valueAsPercentage());
if (!attributes.hasY2() && current->hasAttribute(SVGNames::y2Attr))
attributes.setY2(linear->y2().valueAsPercentage());
}
processedGradients.add(current);
Node* refNode = ownerDocument()->getElementById(SVGURIReference::getTarget(current->href()));
if (refNode && (refNode->hasTagName(SVGNames::linearGradientTag) || refNode->hasTagName(SVGNames::radialGradientTag))) {
current = static_cast<const SVGGradientElement*>(const_cast<const Node*>(refNode));
if (processedGradients.contains(current))
return LinearGradientAttributes();
isLinear = current->gradientType() == LinearGradientPaintServer;
} else
current = 0;
}
return attributes;
}
}
#endif // ENABLE(SVG)