• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2     Copyright (C) 2004, 2005, 2006, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3                   2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
4 
5     This file is part of the KDE project
6 
7     This library is free software; you can redistribute it and/or
8     modify it under the terms of the GNU Library General Public
9     License as published by the Free Software Foundation; either
10     version 2 of the License, or (at your option) any later version.
11 
12     This library is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15     Library General Public License for more details.
16 
17     You should have received a copy of the GNU Library General Public License
18     along with this library; see the file COPYING.LIB.  If not, write to
19     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20     Boston, MA 02110-1301, USA.
21 */
22 
23 #include "config.h"
24 
25 #if ENABLE(SVG)
26 #include "SVGGradientElement.h"
27 
28 #include "CSSStyleSelector.h"
29 #include "MappedAttribute.h"
30 #include "RenderPath.h"
31 #include "RenderSVGHiddenContainer.h"
32 #include "SVGNames.h"
33 #include "SVGPaintServerLinearGradient.h"
34 #include "SVGPaintServerRadialGradient.h"
35 #include "SVGStopElement.h"
36 #include "SVGTransformList.h"
37 #include "SVGTransformable.h"
38 #include "SVGUnitTypes.h"
39 
40 namespace WebCore {
41 
42 char SVGGradientElementIdentifier[] = "SVGGradientElement";
43 
SVGGradientElement(const QualifiedName & tagName,Document * doc)44 SVGGradientElement::SVGGradientElement(const QualifiedName& tagName, Document* doc)
45     : SVGStyledElement(tagName, doc)
46     , SVGURIReference()
47     , SVGExternalResourcesRequired()
48     , m_spreadMethod(this, SVGNames::spreadMethodAttr)
49     , m_gradientUnits(this, SVGNames::gradientUnitsAttr, SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX)
50     , m_gradientTransform(this, SVGNames::gradientTransformAttr, SVGTransformList::create(SVGNames::gradientTransformAttr))
51 {
52 }
53 
~SVGGradientElement()54 SVGGradientElement::~SVGGradientElement()
55 {
56 }
57 
parseMappedAttribute(MappedAttribute * attr)58 void SVGGradientElement::parseMappedAttribute(MappedAttribute* attr)
59 {
60     if (attr->name() == SVGNames::gradientUnitsAttr) {
61         if (attr->value() == "userSpaceOnUse")
62             setGradientUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_USERSPACEONUSE);
63         else if (attr->value() == "objectBoundingBox")
64             setGradientUnitsBaseValue(SVGUnitTypes::SVG_UNIT_TYPE_OBJECTBOUNDINGBOX);
65     } else if (attr->name() == SVGNames::gradientTransformAttr) {
66         SVGTransformList* gradientTransforms = gradientTransformBaseValue();
67         if (!SVGTransformable::parseTransformAttribute(gradientTransforms, attr->value())) {
68             ExceptionCode ec = 0;
69             gradientTransforms->clear(ec);
70         }
71     } else if (attr->name() == SVGNames::spreadMethodAttr) {
72         if (attr->value() == "reflect")
73             setSpreadMethodBaseValue(SpreadMethodReflect);
74         else if (attr->value() == "repeat")
75             setSpreadMethodBaseValue(SpreadMethodRepeat);
76         else if (attr->value() == "pad")
77             setSpreadMethodBaseValue(SpreadMethodPad);
78     } else {
79         if (SVGURIReference::parseMappedAttribute(attr))
80             return;
81         if (SVGExternalResourcesRequired::parseMappedAttribute(attr))
82             return;
83 
84         SVGStyledElement::parseMappedAttribute(attr);
85     }
86 }
87 
svgAttributeChanged(const QualifiedName & attrName)88 void SVGGradientElement::svgAttributeChanged(const QualifiedName& attrName)
89 {
90     SVGStyledElement::svgAttributeChanged(attrName);
91 
92     if (!m_resource)
93         return;
94 
95     if (attrName == SVGNames::gradientUnitsAttr ||
96         attrName == SVGNames::gradientTransformAttr ||
97         attrName == SVGNames::spreadMethodAttr ||
98         SVGURIReference::isKnownAttribute(attrName) ||
99         SVGExternalResourcesRequired::isKnownAttribute(attrName) ||
100         SVGStyledElement::isKnownAttribute(attrName))
101         m_resource->invalidate();
102 }
103 
childrenChanged(bool changedByParser,Node * beforeChange,Node * afterChange,int childCountDelta)104 void SVGGradientElement::childrenChanged(bool changedByParser, Node* beforeChange, Node* afterChange, int childCountDelta)
105 {
106     SVGStyledElement::childrenChanged(changedByParser, beforeChange, afterChange, childCountDelta);
107 
108     if (m_resource)
109         m_resource->invalidate();
110 }
111 
createRenderer(RenderArena * arena,RenderStyle *)112 RenderObject* SVGGradientElement::createRenderer(RenderArena* arena, RenderStyle*)
113 {
114     return new (arena) RenderSVGHiddenContainer(this);
115 }
116 
canvasResource()117 SVGResource* SVGGradientElement::canvasResource()
118 {
119     if (!m_resource) {
120         if (gradientType() == LinearGradientPaintServer)
121             m_resource = SVGPaintServerLinearGradient::create(this);
122         else
123             m_resource = SVGPaintServerRadialGradient::create(this);
124     }
125 
126     return m_resource.get();
127 }
128 
buildStops() const129 Vector<SVGGradientStop> SVGGradientElement::buildStops() const
130 {
131     Vector<SVGGradientStop> stops;
132     RefPtr<RenderStyle> gradientStyle;
133 
134     for (Node* n = firstChild(); n; n = n->nextSibling()) {
135         SVGElement* element = n->isSVGElement() ? static_cast<SVGElement*>(n) : 0;
136 
137         if (element && element->isGradientStop()) {
138             SVGStopElement* stop = static_cast<SVGStopElement*>(element);
139             float stopOffset = stop->offset();
140 
141             Color color;
142             float opacity;
143 
144             if (stop->renderer()) {
145                 RenderStyle* stopStyle = stop->renderer()->style();
146                 color = stopStyle->svgStyle()->stopColor();
147                 opacity = stopStyle->svgStyle()->stopOpacity();
148             } else {
149                 // If there is no renderer for this stop element, then a parent element
150                 // set display="none" - ie. <g display="none"><linearGradient><stop>..
151                 // Unfortunately we have to manually rebuild the stop style. See pservers-grad-19-b.svg
152                 if (!gradientStyle)
153                     gradientStyle = const_cast<SVGGradientElement*>(this)->styleForRenderer();
154 
155                 RefPtr<RenderStyle> stopStyle = stop->resolveStyle(gradientStyle.get());
156 
157                 color = stopStyle->svgStyle()->stopColor();
158                 opacity = stopStyle->svgStyle()->stopOpacity();
159             }
160 
161             stops.append(makeGradientStop(stopOffset, makeRGBA(color.red(), color.green(), color.blue(), int(opacity * 255.))));
162         }
163     }
164 
165     return stops;
166 }
167 
168 }
169 
170 #endif // ENABLE(SVG)
171