2 * Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
4 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
24 #include "core/svg/SVGGradientElement.h"
26 #include "XLinkNames.h"
27 #include "core/dom/Attribute.h"
28 #include "core/rendering/svg/RenderSVGHiddenContainer.h"
29 #include "core/rendering/svg/RenderSVGPath.h"
30 #include "core/rendering/svg/RenderSVGResourceLinearGradient.h"
31 #include "core/rendering/svg/RenderSVGResourceRadialGradient.h"
32 #include "core/svg/SVGElementInstance.h"
33 #include "core/svg/SVGStopElement.h"
34 #include "core/svg/SVGTransformList.h"
38 template<> const SVGEnumerationStringEntries& getStaticStringEntries<SVGSpreadMethodType>()
40 DEFINE_STATIC_LOCAL(SVGEnumerationStringEntries, entries, ());
41 if (entries.isEmpty()) {
42 entries.append(std::make_pair(SVGSpreadMethodPad, "pad"));
43 entries.append(std::make_pair(SVGSpreadMethodReflect, "reflect"));
44 entries.append(std::make_pair(SVGSpreadMethodRepeat, "repeat"));
49 SVGGradientElement::SVGGradientElement(const QualifiedName& tagName, Document& document)
50 : SVGElement(tagName, document)
51 , SVGURIReference(this)
52 , m_gradientTransform(SVGAnimatedTransformList::create(this, SVGNames::gradientTransformAttr, SVGTransformList::create()))
53 , m_spreadMethod(SVGAnimatedEnumeration<SVGSpreadMethodType>::create(this, SVGNames::spreadMethodAttr, SVGSpreadMethodPad))
54 , m_gradientUnits(SVGAnimatedEnumeration<SVGUnitTypes::SVGUnitType>::create(this, SVGNames::gradientUnitsAttr, SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX))
56 ScriptWrappable::init(this);
57 addToPropertyMap(m_gradientTransform);
58 addToPropertyMap(m_spreadMethod);
59 addToPropertyMap(m_gradientUnits);
62 bool SVGGradientElement::isSupportedAttribute(const QualifiedName& attrName)
64 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
65 if (supportedAttributes.isEmpty()) {
66 SVGURIReference::addSupportedAttributes(supportedAttributes);
67 supportedAttributes.add(SVGNames::gradientUnitsAttr);
68 supportedAttributes.add(SVGNames::gradientTransformAttr);
69 supportedAttributes.add(SVGNames::spreadMethodAttr);
71 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
74 void SVGGradientElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
76 if (!isSupportedAttribute(name)) {
77 SVGElement::parseAttribute(name, value);
81 SVGParsingError parseError = NoError;
83 if (name == SVGNames::gradientTransformAttr)
84 m_gradientTransform->setBaseValueAsString(value, parseError);
85 else if (name == SVGNames::gradientUnitsAttr)
86 m_gradientUnits->setBaseValueAsString(value, parseError);
87 else if (name == SVGNames::spreadMethodAttr)
88 m_spreadMethod->setBaseValueAsString(value, parseError);
89 else if (SVGURIReference::parseAttribute(name, value, parseError)) {
93 reportAttributeParsingError(parseError, name, value);
96 void SVGGradientElement::svgAttributeChanged(const QualifiedName& attrName)
98 if (!isSupportedAttribute(attrName)) {
99 SVGElement::svgAttributeChanged(attrName);
103 SVGElementInstance::InvalidationGuard invalidationGuard(this);
105 RenderSVGResourceContainer* renderer = toRenderSVGResourceContainer(this->renderer());
107 renderer->invalidateCacheAndMarkForLayout();
110 void SVGGradientElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
112 SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
117 if (RenderObject* object = renderer())
118 object->setNeedsLayout();
121 Vector<Gradient::ColorStop> SVGGradientElement::buildStops()
123 Vector<Gradient::ColorStop> stops;
125 float previousOffset = 0.0f;
126 for (SVGStopElement* stop = Traversal<SVGStopElement>::firstChild(*this); stop; stop = Traversal<SVGStopElement>::nextSibling(*stop)) {
127 // Figure out right monotonic offset
128 float offset = stop->offset()->currentValue()->value();
129 offset = std::min(std::max(previousOffset, offset), 1.0f);
130 previousOffset = offset;
132 stops.append(Gradient::ColorStop(offset, stop->stopColorIncludingOpacity()));