Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / svg / SVGGradientElement.cpp
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
36 namespace WebCore {
37
38 template<> const SVGEnumerationStringEntries& getStaticStringEntries<SVGSpreadMethodType>()
39 {
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"));
45     }
46     return entries;
47 }
48
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))
55 {
56     ScriptWrappable::init(this);
57     addToPropertyMap(m_gradientTransform);
58     addToPropertyMap(m_spreadMethod);
59     addToPropertyMap(m_gradientUnits);
60 }
61
62 bool SVGGradientElement::isSupportedAttribute(const QualifiedName& attrName)
63 {
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);
70     }
71     return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
72 }
73
74 void SVGGradientElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
75 {
76     if (!isSupportedAttribute(name)) {
77         SVGElement::parseAttribute(name, value);
78         return;
79     }
80
81     SVGParsingError parseError = NoError;
82
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)) {
90     } else
91         ASSERT_NOT_REACHED();
92
93     reportAttributeParsingError(parseError, name, value);
94 }
95
96 void SVGGradientElement::svgAttributeChanged(const QualifiedName& attrName)
97 {
98     if (!isSupportedAttribute(attrName)) {
99         SVGElement::svgAttributeChanged(attrName);
100         return;
101     }
102
103     SVGElementInstance::InvalidationGuard invalidationGuard(this);
104
105     RenderSVGResourceContainer* renderer = toRenderSVGResourceContainer(this->renderer());
106     if (renderer)
107         renderer->invalidateCacheAndMarkForLayout();
108 }
109
110 void SVGGradientElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
111 {
112     SVGElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
113
114     if (changedByParser)
115         return;
116
117     if (RenderObject* object = renderer())
118         object->setNeedsLayout();
119 }
120
121 Vector<Gradient::ColorStop> SVGGradientElement::buildStops()
122 {
123     Vector<Gradient::ColorStop> stops;
124
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;
131
132         stops.append(Gradient::ColorStop(offset, stop->stopColorIncludingOpacity()));
133     }
134
135     return stops;
136 }
137
138 }