SVGLinearGradientElement.cpp [plain text]
#include "config.h"
#if ENABLE(SVG)
#include "SVGLinearGradientElement.h"
#include "Document.h"
#include "FloatPoint.h"
#include "LinearGradientAttributes.h"
#include "MappedAttribute.h"
#include "RenderSVGResourceLinearGradient.h"
#include "SVGLength.h"
#include "SVGNames.h"
#include "SVGTransform.h"
#include "SVGTransformList.h"
#include "SVGUnitTypes.h"
namespace WebCore {
SVGLinearGradientElement::SVGLinearGradientElement(const QualifiedName& tagName, Document* doc)
: SVGGradientElement(tagName, doc)
, m_x1(LengthModeWidth)
, m_y1(LengthModeHeight)
, m_x2(LengthModeWidth, "100%")
, m_y2(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 (attrName == SVGNames::x1Attr
|| attrName == SVGNames::y1Attr
|| attrName == SVGNames::x2Attr
|| attrName == SVGNames::y2Attr)
invalidateResourceClients();
}
void SVGLinearGradientElement::synchronizeProperty(const QualifiedName& attrName)
{
SVGGradientElement::synchronizeProperty(attrName);
if (attrName == anyQName()) {
synchronizeX1();
synchronizeY1();
synchronizeX2();
synchronizeY2();
return;
}
if (attrName == SVGNames::x1Attr)
synchronizeX1();
else if (attrName == SVGNames::y1Attr)
synchronizeY1();
else if (attrName == SVGNames::x2Attr)
synchronizeX2();
else if (attrName == SVGNames::y2Attr)
synchronizeY2();
}
RenderObject* SVGLinearGradientElement::createRenderer(RenderArena* arena, RenderStyle*)
{
return new (arena) RenderSVGResourceLinearGradient(this);
}
LinearGradientAttributes SVGLinearGradientElement::collectGradientProperties()
{
LinearGradientAttributes attributes;
HashSet<SVGGradientElement*> processedGradients;
bool isLinear = true;
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<Gradient::ColorStop>& stops(current->buildStops());
if (!stops.isEmpty())
attributes.setStops(stops);
}
if (isLinear) {
SVGLinearGradientElement* linear = static_cast<SVGLinearGradientElement*>(current);
if (!attributes.hasX1() && current->hasAttribute(SVGNames::x1Attr))
attributes.setX1(linear->x1());
if (!attributes.hasY1() && current->hasAttribute(SVGNames::y1Attr))
attributes.setY1(linear->y1());
if (!attributes.hasX2() && current->hasAttribute(SVGNames::x2Attr))
attributes.setX2(linear->x2());
if (!attributes.hasY2() && current->hasAttribute(SVGNames::y2Attr))
attributes.setY2(linear->y2());
}
processedGradients.add(current);
Node* refNode = ownerDocument()->getElementById(SVGURIReference::getTarget(current->href()));
if (refNode && (refNode->hasTagName(SVGNames::linearGradientTag) || refNode->hasTagName(SVGNames::radialGradientTag))) {
current = static_cast<SVGGradientElement*>(refNode);
if (processedGradients.contains(current))
return LinearGradientAttributes();
isLinear = current->hasTagName(SVGNames::linearGradientTag);
} else
current = 0;
}
return attributes;
}
void SVGLinearGradientElement::calculateStartEndPoints(const LinearGradientAttributes& attributes, FloatPoint& startPoint, FloatPoint& endPoint)
{
if (attributes.boundingBoxMode()) {
startPoint = FloatPoint(attributes.x1().valueAsPercentage(), attributes.y1().valueAsPercentage());
endPoint = FloatPoint(attributes.x2().valueAsPercentage(), attributes.y2().valueAsPercentage());
} else {
startPoint = FloatPoint(attributes.x1().value(this), attributes.y1().value(this));
endPoint = FloatPoint(attributes.x2().value(this), attributes.y2().value(this));
}
}
}
#endif // ENABLE(SVG)