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/rendering/svg/RenderSVGPath.h"
29 #include "core/rendering/svg/RenderSVGResourceLinearGradient.h"
30 #include "core/rendering/svg/RenderSVGResourceRadialGradient.h"
31 #include "core/svg/SVGStopElement.h"
32 #include "core/svg/SVGTransformList.h"
36 template<> const SVGEnumerationStringEntries& getStaticStringEntries<SVGSpreadMethodType>()
38 DEFINE_STATIC_LOCAL(SVGEnumerationStringEntries, entries, ());
39 if (entries.isEmpty()) {
40 entries.append(std::make_pair(SVGSpreadMethodPad, "pad"));
41 entries.append(std::make_pair(SVGSpreadMethodReflect, "reflect"));
42 entries.append(std::make_pair(SVGSpreadMethodRepeat, "repeat"));
47 SVGGradientElement::SVGGradientElement(const QualifiedName& tagName, Document& document)
48 : SVGElement(tagName, document)
49 , SVGURIReference(this)
50 , m_gradientTransform(SVGAnimatedTransformList::create(this, SVGNames::gradientTransformAttr, SVGTransformList::create()))
51 , m_spreadMethod(SVGAnimatedEnumeration<SVGSpreadMethodType>::create(this, SVGNames::spreadMethodAttr, SVGSpreadMethodPad))
52 , m_gradientUnits(SVGAnimatedEnumeration<SVGUnitTypes::SVGUnitType>::create(this, SVGNames::gradientUnitsAttr, SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX))
54 addToPropertyMap(m_gradientTransform);
55 addToPropertyMap(m_spreadMethod);
56 addToPropertyMap(m_gradientUnits);
59 bool SVGGradientElement::isSupportedAttribute(const QualifiedName& attrName)
61 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
62 if (supportedAttributes.isEmpty()) {
63 SVGURIReference::addSupportedAttributes(supportedAttributes);
64 supportedAttributes.add(SVGNames::gradientUnitsAttr);
65 supportedAttributes.add(SVGNames::gradientTransformAttr);
66 supportedAttributes.add(SVGNames::spreadMethodAttr);
68 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
71 void SVGGradientElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
73 if (!isSupportedAttribute(name)) {
74 SVGElement::parseAttribute(name, value);
78 SVGParsingError parseError = NoError;
80 if (name == SVGNames::gradientTransformAttr)
81 m_gradientTransform->setBaseValueAsString(value, parseError);
82 else if (name == SVGNames::gradientUnitsAttr)
83 m_gradientUnits->setBaseValueAsString(value, parseError);
84 else if (name == SVGNames::spreadMethodAttr)
85 m_spreadMethod->setBaseValueAsString(value, parseError);
86 else if (SVGURIReference::parseAttribute(name, value, parseError)) {
90 reportAttributeParsingError(parseError, name, value);
93 void SVGGradientElement::svgAttributeChanged(const QualifiedName& attrName)
95 if (!isSupportedAttribute(attrName)) {
96 SVGElement::svgAttributeChanged(attrName);
100 SVGElement::InvalidationGuard invalidationGuard(this);
102 RenderSVGResourceContainer* renderer = toRenderSVGResourceContainer(this->renderer());
104 renderer->invalidateCacheAndMarkForLayout();
107 void SVGGradientElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
109 SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
114 if (RenderObject* object = renderer())
115 object->setNeedsLayoutAndFullPaintInvalidation();
118 Vector<Gradient::ColorStop> SVGGradientElement::buildStops()
120 Vector<Gradient::ColorStop> stops;
122 float previousOffset = 0.0f;
123 for (SVGStopElement* stop = Traversal<SVGStopElement>::firstChild(*this); stop; stop = Traversal<SVGStopElement>::nextSibling(*stop)) {
124 // Figure out right monotonic offset
125 float offset = stop->offset()->currentValue()->value();
126 offset = std::min(std::max(previousOffset, offset), 1.0f);
127 previousOffset = offset;
129 stops.append(Gradient::ColorStop(offset, stop->stopColorIncludingOpacity()));