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 "core/XLinkNames.h"
27 #include "core/dom/Attribute.h"
28 #include "core/dom/ElementTraversal.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/SVGStopElement.h"
33 #include "core/svg/SVGTransformList.h"
37 template<> const SVGEnumerationStringEntries& getStaticStringEntries<SVGSpreadMethodType>()
39 DEFINE_STATIC_LOCAL(SVGEnumerationStringEntries, entries, ());
40 if (entries.isEmpty()) {
41 entries.append(std::make_pair(SVGSpreadMethodPad, "pad"));
42 entries.append(std::make_pair(SVGSpreadMethodReflect, "reflect"));
43 entries.append(std::make_pair(SVGSpreadMethodRepeat, "repeat"));
48 SVGGradientElement::SVGGradientElement(const QualifiedName& tagName, Document& document)
49 : SVGElement(tagName, document)
50 , SVGURIReference(this)
51 , m_gradientTransform(SVGAnimatedTransformList::create(this, SVGNames::gradientTransformAttr, SVGTransformList::create()))
52 , m_spreadMethod(SVGAnimatedEnumeration<SVGSpreadMethodType>::create(this, SVGNames::spreadMethodAttr, SVGSpreadMethodPad))
53 , m_gradientUnits(SVGAnimatedEnumeration<SVGUnitTypes::SVGUnitType>::create(this, SVGNames::gradientUnitsAttr, SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX))
55 addToPropertyMap(m_gradientTransform);
56 addToPropertyMap(m_spreadMethod);
57 addToPropertyMap(m_gradientUnits);
60 bool SVGGradientElement::isSupportedAttribute(const QualifiedName& attrName)
62 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
63 if (supportedAttributes.isEmpty()) {
64 SVGURIReference::addSupportedAttributes(supportedAttributes);
65 supportedAttributes.add(SVGNames::gradientUnitsAttr);
66 supportedAttributes.add(SVGNames::gradientTransformAttr);
67 supportedAttributes.add(SVGNames::spreadMethodAttr);
69 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
72 void SVGGradientElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
74 parseAttributeNew(name, value);
77 void SVGGradientElement::svgAttributeChanged(const QualifiedName& attrName)
79 if (!isSupportedAttribute(attrName)) {
80 SVGElement::svgAttributeChanged(attrName);
84 SVGElement::InvalidationGuard invalidationGuard(this);
86 RenderSVGResourceContainer* renderer = toRenderSVGResourceContainer(this->renderer());
88 renderer->invalidateCacheAndMarkForLayout();
91 void SVGGradientElement::childrenChanged(const ChildrenChange& change)
93 SVGElement::childrenChanged(change);
98 if (RenderObject* object = renderer())
99 object->setNeedsLayoutAndFullPaintInvalidation();
102 Vector<Gradient::ColorStop> SVGGradientElement::buildStops()
104 Vector<Gradient::ColorStop> stops;
106 float previousOffset = 0.0f;
107 for (SVGStopElement* stop = Traversal<SVGStopElement>::firstChild(*this); stop; stop = Traversal<SVGStopElement>::nextSibling(*stop)) {
108 // Figure out right monotonic offset
109 float offset = stop->offset()->currentValue()->value();
110 offset = std::min(std::max(previousOffset, offset), 1.0f);
111 previousOffset = offset;
113 stops.append(Gradient::ColorStop(offset, stop->stopColorIncludingOpacity()));