SVGGradientElement.cpp [plain text]
#include "config.h"
#if SVG_SUPPORT
#include "SVGGradientElement.h"
#include "Attr.h"
#include "Document.h"
#include "RenderView.h"
#include "SVGAnimatedEnumeration.h"
#include "SVGAnimatedNumber.h"
#include "SVGAnimatedTransformList.h"
#include "SVGDOMImplementation.h"
#include "SVGHelper.h"
#include "SVGNames.h"
#include "SVGRenderStyle.h"
#include "SVGStopElement.h"
#include "SVGTransformList.h"
#include "SVGTransformable.h"
#include "cssstyleselector.h"
#include "ksvg.h"
#include <kcanvas/device/KRenderingDevice.h>
#include <kcanvas/device/KRenderingPaintServerGradient.h>
using namespace WebCore;
SVGGradientElement::SVGGradientElement(const QualifiedName& tagName, Document *doc) : SVGStyledElement(tagName, doc), SVGURIReference(), SVGExternalResourcesRequired()
{
m_resource = 0;
}
SVGGradientElement::~SVGGradientElement()
{
delete m_resource;
}
SVGAnimatedEnumeration *SVGGradientElement::gradientUnits() const
{
if (!m_gradientUnits) {
lazy_create<SVGAnimatedEnumeration>(m_gradientUnits, this);
m_gradientUnits->setBaseVal(SVG_UNIT_TYPE_OBJECTBOUNDINGBOX);
}
return m_gradientUnits.get();
}
SVGAnimatedTransformList *SVGGradientElement::gradientTransform() const
{
return lazy_create<SVGAnimatedTransformList>(m_gradientTransform, this);
}
SVGAnimatedEnumeration *SVGGradientElement::spreadMethod() const
{
return lazy_create<SVGAnimatedEnumeration>(m_spreadMethod, this);
}
void SVGGradientElement::parseMappedAttribute(MappedAttribute *attr)
{
const String& value = attr->value();
if (attr->name() == SVGNames::gradientUnitsAttr) {
if(value == "userSpaceOnUse")
gradientUnits()->setBaseVal(SVG_UNIT_TYPE_USERSPACEONUSE);
else if(value == "objectBoundingBox")
gradientUnits()->setBaseVal(SVG_UNIT_TYPE_OBJECTBOUNDINGBOX);
} else if (attr->name() == SVGNames::gradientTransformAttr) {
SVGTransformList *gradientTransforms = gradientTransform()->baseVal();
SVGTransformable::parseTransformAttribute(gradientTransforms, attr->value());
} else if (attr->name() == SVGNames::spreadMethodAttr) {
if(value == "reflect")
spreadMethod()->setBaseVal(SVG_SPREADMETHOD_REFLECT);
else if(value == "repeat")
spreadMethod()->setBaseVal(SVG_SPREADMETHOD_REPEAT);
else if(value == "pad")
spreadMethod()->setBaseVal(SVG_SPREADMETHOD_PAD);
} else {
if (SVGURIReference::parseMappedAttribute(attr))
return;
if (SVGExternalResourcesRequired::parseMappedAttribute(attr))
return;
SVGStyledElement::parseMappedAttribute(attr);
}
}
void SVGGradientElement::notifyAttributeChange() const
{
if(ownerDocument()->parsing() || !attached())
return;
buildGradient(m_resource);
m_resource->invalidate();
const KCanvasItemList &clients = m_resource->clients();
for(KCanvasItemList::ConstIterator it = clients.begin(); it != clients.end(); ++it)
{
const RenderPath *current = (*it);
SVGStyledElement *styled = (current ? static_cast<SVGStyledElement *>(current->element()) : 0);
if(styled)
styled->setChanged(true);
}
}
KRenderingPaintServerGradient *SVGGradientElement::canvasResource()
{
if (!m_resource) {
KRenderingPaintServer *temp = renderingDevice()->createPaintServer(gradientType());
m_resource = static_cast<KRenderingPaintServerGradient *>(temp);
m_resource->setListener(this);
buildGradient(m_resource);
}
return m_resource;
}
void SVGGradientElement::resourceNotification() const
{
buildGradient(m_resource);
}
void SVGGradientElement::rebuildStops() const
{
if (m_resource && !ownerDocument()->parsing()) {
Vector<KCGradientStop> stops;
RenderStyle* gradientStyle = const_cast<SVGGradientElement*>(this)->styleForRenderer(parent()->renderer());
for (Node *n = firstChild(); n; n = n->nextSibling()) {
SVGElement *element = svg_dynamic_cast(n);
if (element && element->isGradientStop()) {
SVGStopElement *stop = static_cast<SVGStopElement *>(element);
float stopOffset = stop->offset()->baseVal();
RenderStyle *stopStyle = document()->styleSelector()->styleForElement(stop, gradientStyle);
Color c = stopStyle->svgStyle()->stopColor();
float opacity = stopStyle->svgStyle()->stopOpacity();
stops.append(makeGradientStop(stopOffset, makeRGBA(c.red(), c.green(), c.blue(), int(opacity * 255.))));
stopStyle->deref(view()->renderArena());
}
}
gradientStyle->deref(view()->renderArena());
m_resource->setGradientStops(stops);
}
}
#endif // SVG_SUPPORT