1/*
2 * Copyright (C) 2004, 2005, 2007, 2008 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006, 2007 Rob Buis <buis@kde.org>
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21#include "config.h"
22
23#include "core/svg/SVGStopElement.h"
24
25#include "core/rendering/svg/RenderSVGGradientStop.h"
26#include "core/rendering/svg/RenderSVGResource.h"
27#include "core/svg/SVGElementInstance.h"
28
29namespace WebCore {
30
31// Animated property definitions
32DEFINE_ANIMATED_NUMBER(SVGStopElement, SVGNames::offsetAttr, Offset, offset)
33
34BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGStopElement)
35    REGISTER_LOCAL_ANIMATED_PROPERTY(offset)
36    REGISTER_PARENT_ANIMATED_PROPERTIES(SVGElement)
37END_REGISTER_ANIMATED_PROPERTIES
38
39inline SVGStopElement::SVGStopElement(Document& document)
40    : SVGElement(SVGNames::stopTag, document)
41    , m_offset(0)
42{
43    ScriptWrappable::init(this);
44    registerAnimatedPropertiesForSVGStopElement();
45}
46
47PassRefPtr<SVGStopElement> SVGStopElement::create(Document& document)
48{
49    return adoptRef(new SVGStopElement(document));
50}
51
52bool SVGStopElement::isSupportedAttribute(const QualifiedName& attrName)
53{
54    DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
55    if (supportedAttributes.isEmpty())
56        supportedAttributes.add(SVGNames::offsetAttr);
57    return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
58}
59
60void SVGStopElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
61{
62    if (!isSupportedAttribute(name)) {
63        SVGElement::parseAttribute(name, value);
64        return;
65    }
66
67    if (name == SVGNames::offsetAttr) {
68        if (value.endsWith('%'))
69            setOffsetBaseValue(value.string().left(value.length() - 1).toFloat() / 100.0f);
70        else
71            setOffsetBaseValue(value.toFloat());
72        return;
73    }
74
75    ASSERT_NOT_REACHED();
76}
77
78void SVGStopElement::svgAttributeChanged(const QualifiedName& attrName)
79{
80    if (!isSupportedAttribute(attrName)) {
81        SVGElement::svgAttributeChanged(attrName);
82        return;
83    }
84
85    SVGElementInstance::InvalidationGuard invalidationGuard(this);
86
87    if (!renderer())
88        return;
89
90    if (attrName == SVGNames::offsetAttr) {
91        RenderSVGResource::markForLayoutAndParentResourceInvalidation(renderer());
92        return;
93    }
94
95    ASSERT_NOT_REACHED();
96}
97
98RenderObject* SVGStopElement::createRenderer(RenderStyle*)
99{
100    return new RenderSVGGradientStop(this);
101}
102
103bool SVGStopElement::rendererIsNeeded(const RenderStyle&)
104{
105    return true;
106}
107
108Color SVGStopElement::stopColorIncludingOpacity() const
109{
110    RenderStyle* style = renderer() ? renderer()->style() : 0;
111    // FIXME: This check for null style exists to address Bug WK 90814, a rare crash condition in
112    // which the renderer or style is null. This entire class is scheduled for removal (Bug WK 86941)
113    // and we will tolerate this null check until then.
114    if (!style || !style->svgStyle())
115        return Color(Color::transparent, true); // Transparent black.
116
117    const SVGRenderStyle* svgStyle = style->svgStyle();
118    return colorWithOverrideAlpha(svgStyle->stopColor().rgb(), svgStyle->stopOpacity());
119}
120
121}
122