don't discard iframe children.
[kdelibs.git] / khtml / svg / SVGLinearGradientElement.cpp
blob4810cac75ad12103e98daaea32482c4bd1aaddfe
1 /*
2 Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
5 This file is part of the KDE project
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
23 #include "config.h"
24 #include "wtf/Platform.h"
26 #if ENABLE(SVG)
27 #include "SVGLinearGradientElement.h"
29 #include "FloatPoint.h"
30 #include "LinearGradientAttributes.h"
31 #include "SVGLength.h"
32 #include "SVGNames.h"
33 #include "SVGPaintServerLinearGradient.h"
34 #include "SVGTransform.h"
35 #include "SVGTransformList.h"
36 #include "SVGUnitTypes.h"
38 namespace WebCore {
40 SVGLinearGradientElement::SVGLinearGradientElement(const QualifiedName& tagName, Document* doc)
41 : SVGGradientElement(tagName, doc)
42 , m_x1(this, LengthModeWidth)
43 , m_y1(this, LengthModeHeight)
44 , m_x2(this, LengthModeWidth)
45 , m_y2(this, LengthModeHeight)
47 // Spec: If the attribute is not specified, the effect is as if a value of "100%" were specified.
48 setX2BaseValue(SVGLength(this, LengthModeWidth, "100%"));
51 SVGLinearGradientElement::~SVGLinearGradientElement()
55 ANIMATED_PROPERTY_DEFINITIONS(SVGLinearGradientElement, SVGLength, Length, length, X1, x1, SVGNames::x1Attr, m_x1)
56 ANIMATED_PROPERTY_DEFINITIONS(SVGLinearGradientElement, SVGLength, Length, length, Y1, y1, SVGNames::y1Attr, m_y1)
57 ANIMATED_PROPERTY_DEFINITIONS(SVGLinearGradientElement, SVGLength, Length, length, X2, x2, SVGNames::x2Attr, m_x2)
58 ANIMATED_PROPERTY_DEFINITIONS(SVGLinearGradientElement, SVGLength, Length, length, Y2, y2, SVGNames::y2Attr, m_y2)
60 void SVGLinearGradientElement::parseMappedAttribute(MappedAttribute* attr)
62 if (attr->name() == SVGNames::x1Attr)
63 setX1BaseValue(SVGLength(this, LengthModeWidth, attr->value()));
64 else if (attr->name() == SVGNames::y1Attr)
65 setY1BaseValue(SVGLength(this, LengthModeHeight, attr->value()));
66 else if (attr->name() == SVGNames::x2Attr)
67 setX2BaseValue(SVGLength(this, LengthModeWidth, attr->value()));
68 else if (attr->name() == SVGNames::y2Attr)
69 setY2BaseValue(SVGLength(this, LengthModeHeight, attr->value()));
70 else
71 SVGGradientElement::parseMappedAttribute(attr);
74 void SVGLinearGradientElement::svgAttributeChanged(const QualifiedName& attrName)
76 SVGGradientElement::svgAttributeChanged(attrName);
78 if (!m_resource)
79 return;
81 if (attrName == SVGNames::x1Attr || attrName == SVGNames::y1Attr ||
82 attrName == SVGNames::x2Attr || attrName == SVGNames::y2Attr)
83 m_resource->invalidate();
86 void SVGLinearGradientElement::buildGradient() const
88 LinearGradientAttributes attributes = collectGradientProperties();
90 // If we didn't find any gradient containing stop elements, ignore the request.
91 if (attributes.stops().isEmpty())
92 return;
94 RefPtr<SVGPaintServerLinearGradient> linearGradient = WTF::static_pointer_cast<SVGPaintServerLinearGradient>(m_resource);
96 linearGradient->setGradientStops(attributes.stops());
97 linearGradient->setBoundingBoxMode(attributes.boundingBoxMode());
98 linearGradient->setGradientSpreadMethod(attributes.spreadMethod());
99 linearGradient->setGradientTransform(attributes.gradientTransform());
100 linearGradient->setGradientStart(FloatPoint::narrowPrecision(attributes.x1(), attributes.y1()));
101 linearGradient->setGradientEnd(FloatPoint::narrowPrecision(attributes.x2(), attributes.y2()));
104 LinearGradientAttributes SVGLinearGradientElement::collectGradientProperties() const
106 LinearGradientAttributes attributes;
107 HashSet<const SVGGradientElement*> processedGradients;
109 bool isLinear = true;
110 const SVGGradientElement* current = this;
112 while (current) {
113 if (!attributes.hasSpreadMethod() && current->hasAttribute(SVGNames::spreadMethodAttr))
114 attributes.setSpreadMethod((SVGGradientSpreadMethod) current->spreadMethod());
116 if (!attributes.hasBoundingBoxMode() && current->hasAttribute(SVGNames::gradientUnitsAttr))
117 attributes.setBoundingBoxMode(current->getAttribute(SVGNames::gradientUnitsAttr) == "objectBoundingBox");
119 if (!attributes.hasGradientTransform() && current->hasAttribute(SVGNames::gradientTransformAttr))
120 attributes.setGradientTransform(current->gradientTransform()->consolidate().matrix());
122 if (!attributes.hasStops()) {
123 const Vector<SVGGradientStop>& stops(current->buildStops());
124 kDebug() << "stops.isEmpty()" << stops.isEmpty() << endl;
125 if (!stops.isEmpty())
126 attributes.setStops(stops);
129 if (isLinear) {
130 const SVGLinearGradientElement* linear = static_cast<const SVGLinearGradientElement*>(current);
132 if (!attributes.hasX1() && current->hasAttribute(SVGNames::x1Attr))
133 attributes.setX1(linear->x1().valueAsPercentage());
135 if (!attributes.hasY1() && current->hasAttribute(SVGNames::y1Attr))
136 attributes.setY1(linear->y1().valueAsPercentage());
138 if (!attributes.hasX2() && current->hasAttribute(SVGNames::x2Attr))
139 attributes.setX2(linear->x2().valueAsPercentage());
141 if (!attributes.hasY2() && current->hasAttribute(SVGNames::y2Attr))
142 attributes.setY2(linear->y2().valueAsPercentage());
145 processedGradients.add(current);
147 // Respect xlink:href, take attributes from referenced element
148 Node* refNode = ownerDocument()->getElementById(SVGURIReference::getTarget(current->href()));
149 if (refNode && (refNode->hasTagName(SVGNames::linearGradientTag) || refNode->hasTagName(SVGNames::radialGradientTag))) {
150 current = static_cast<const SVGGradientElement*>(const_cast<const Node*>(refNode));
151 int ec;
152 kDebug() << "take attributes from" << current->getAttributeNS("", "id", ec) << endl;
154 // Cycle detection
155 if (processedGradients.contains(current))
156 return LinearGradientAttributes();
158 isLinear = current->gradientType() == LinearGradientPaintServer;
159 } else
160 current = 0;
163 return attributes;
166 // KHTML ElementImpl pure virtual method
167 quint32 SVGLinearGradientElement::id() const
169 return SVGNames::linearGradientTag.id();
172 DOMString SVGLinearGradientElement::tagName() const
174 return SVGNames::linearGradientTag.tagName();
179 #endif // ENABLE(SVG)