1/*
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.
5 *
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.
10 *
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.
15 *
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.
20 */
21
22#include "config.h"
23
24#include "core/svg/SVGGradientElement.h"
25
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"
35
36namespace WebCore {
37
38// Animated property definitions
39DEFINE_ANIMATED_ENUMERATION(SVGGradientElement, SVGNames::spreadMethodAttr, SpreadMethod, spreadMethod, SVGSpreadMethodType)
40DEFINE_ANIMATED_ENUMERATION(SVGGradientElement, SVGNames::gradientUnitsAttr, GradientUnits, gradientUnits, SVGUnitTypes::SVGUnitType)
41DEFINE_ANIMATED_TRANSFORM_LIST(SVGGradientElement, SVGNames::gradientTransformAttr, GradientTransform, gradientTransform)
42DEFINE_ANIMATED_STRING(SVGGradientElement, XLinkNames::hrefAttr, Href, href)
43DEFINE_ANIMATED_BOOLEAN(SVGGradientElement, SVGNames::externalResourcesRequiredAttr, ExternalResourcesRequired, externalResourcesRequired)
44
45BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGGradientElement)
46    REGISTER_LOCAL_ANIMATED_PROPERTY(spreadMethod)
47    REGISTER_LOCAL_ANIMATED_PROPERTY(gradientUnits)
48    REGISTER_LOCAL_ANIMATED_PROPERTY(gradientTransform)
49    REGISTER_LOCAL_ANIMATED_PROPERTY(href)
50    REGISTER_LOCAL_ANIMATED_PROPERTY(externalResourcesRequired)
51    REGISTER_PARENT_ANIMATED_PROPERTIES(SVGElement)
52END_REGISTER_ANIMATED_PROPERTIES
53
54SVGGradientElement::SVGGradientElement(const QualifiedName& tagName, Document& document)
55    : SVGElement(tagName, document)
56    , m_spreadMethod(SVGSpreadMethodPad)
57    , m_gradientUnits(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX)
58{
59    ScriptWrappable::init(this);
60    registerAnimatedPropertiesForSVGGradientElement();
61}
62
63bool SVGGradientElement::isSupportedAttribute(const QualifiedName& attrName)
64{
65    DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
66    if (supportedAttributes.isEmpty()) {
67        SVGURIReference::addSupportedAttributes(supportedAttributes);
68        SVGExternalResourcesRequired::addSupportedAttributes(supportedAttributes);
69        supportedAttributes.add(SVGNames::gradientUnitsAttr);
70        supportedAttributes.add(SVGNames::gradientTransformAttr);
71        supportedAttributes.add(SVGNames::spreadMethodAttr);
72    }
73    return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
74}
75
76void SVGGradientElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
77{
78    if (!isSupportedAttribute(name)) {
79        SVGElement::parseAttribute(name, value);
80        return;
81    }
82
83    if (name == SVGNames::gradientUnitsAttr) {
84        SVGUnitTypes::SVGUnitType propertyValue = SVGPropertyTraits<SVGUnitTypes::SVGUnitType>::fromString(value);
85        if (propertyValue > 0)
86            setGradientUnitsBaseValue(propertyValue);
87        return;
88    }
89
90    if (name == SVGNames::gradientTransformAttr) {
91        SVGTransformList newList;
92        newList.parse(value);
93        detachAnimatedGradientTransformListWrappers(newList.size());
94        setGradientTransformBaseValue(newList);
95        return;
96    }
97
98    if (name == SVGNames::spreadMethodAttr) {
99        SVGSpreadMethodType propertyValue = SVGPropertyTraits<SVGSpreadMethodType>::fromString(value);
100        if (propertyValue > 0)
101            setSpreadMethodBaseValue(propertyValue);
102        return;
103    }
104
105    if (SVGURIReference::parseAttribute(name, value))
106        return;
107    if (SVGExternalResourcesRequired::parseAttribute(name, value))
108        return;
109
110    ASSERT_NOT_REACHED();
111}
112
113void SVGGradientElement::svgAttributeChanged(const QualifiedName& attrName)
114{
115    if (!isSupportedAttribute(attrName)) {
116        SVGElement::svgAttributeChanged(attrName);
117        return;
118    }
119
120    SVGElementInstance::InvalidationGuard invalidationGuard(this);
121
122    RenderSVGResourceContainer* renderer = toRenderSVGResourceContainer(this->renderer());
123    if (renderer)
124        renderer->invalidateCacheAndMarkForLayout();
125}
126
127void SVGGradientElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
128{
129    SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
130
131    if (changedByParser)
132        return;
133
134    if (RenderObject* object = renderer())
135        object->setNeedsLayout();
136}
137
138Vector<Gradient::ColorStop> SVGGradientElement::buildStops()
139{
140    Vector<Gradient::ColorStop> stops;
141
142    float previousOffset = 0.0f;
143    for (Node* n = firstChild(); n; n = n->nextSibling()) {
144        SVGElement* element = n->isSVGElement() ? toSVGElement(n) : 0;
145        if (!element || !element->isGradientStop())
146            continue;
147
148        SVGStopElement* stop = toSVGStopElement(element);
149        Color color = stop->stopColorIncludingOpacity();
150
151        // Figure out right monotonic offset
152        float offset = stop->offsetCurrentValue();
153        offset = std::min(std::max(previousOffset, offset), 1.0f);
154        previousOffset = offset;
155
156        // Extract individual channel values
157        // FIXME: Why doesn't ColorStop take a Color and an offset??
158        float r, g, b, a;
159        color.getRGBA(r, g, b, a);
160
161        stops.append(Gradient::ColorStop(offset, r, g, b, a));
162    }
163
164    return stops;
165}
166
167}
168