1/*
2 * Copyright (C) 2004, 2005, 2007 Nikolas Zimmermann <zimmermann@kde.org>
3 * Copyright (C) 2004, 2005, 2006 Rob Buis <buis@kde.org>
4 * Copyright (C) 2005 Oliver Hunt <oliver@nerget.com>
5 * Copyright (C) 2018-2019 Apple Inc. All rights reserved.
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#include "SVGFELightElement.h"
25
26#include "ElementIterator.h"
27#include "RenderObject.h"
28#include "RenderSVGResource.h"
29#include "SVGFEDiffuseLightingElement.h"
30#include "SVGFEDistantLightElement.h"
31#include "SVGFEPointLightElement.h"
32#include "SVGFESpecularLightingElement.h"
33#include "SVGFESpotLightElement.h"
34#include "SVGFilterElement.h"
35#include "SVGFilterPrimitiveStandardAttributes.h"
36#include "SVGNames.h"
37#include <wtf/IsoMallocInlines.h>
38
39namespace WebCore {
40
41WTF_MAKE_ISO_ALLOCATED_IMPL(SVGFELightElement);
42
43SVGFELightElement::SVGFELightElement(const QualifiedName& tagName, Document& document)
44 : SVGElement(tagName, document)
45{
46 static std::once_flag onceFlag;
47 std::call_once(onceFlag, [] {
48 PropertyRegistry::registerProperty<SVGNames::azimuthAttr, &SVGFELightElement::m_azimuth>();
49 PropertyRegistry::registerProperty<SVGNames::elevationAttr, &SVGFELightElement::m_elevation>();
50 PropertyRegistry::registerProperty<SVGNames::xAttr, &SVGFELightElement::m_x>();
51 PropertyRegistry::registerProperty<SVGNames::yAttr, &SVGFELightElement::m_y>();
52 PropertyRegistry::registerProperty<SVGNames::zAttr, &SVGFELightElement::m_z>();
53 PropertyRegistry::registerProperty<SVGNames::pointsAtXAttr, &SVGFELightElement::m_pointsAtX>();
54 PropertyRegistry::registerProperty<SVGNames::pointsAtYAttr, &SVGFELightElement::m_pointsAtY>();
55 PropertyRegistry::registerProperty<SVGNames::pointsAtZAttr, &SVGFELightElement::m_pointsAtZ>();
56 PropertyRegistry::registerProperty<SVGNames::specularExponentAttr, &SVGFELightElement::m_specularExponent>();
57 PropertyRegistry::registerProperty<SVGNames::limitingConeAngleAttr, &SVGFELightElement::m_limitingConeAngle>();
58 });
59}
60
61SVGFELightElement* SVGFELightElement::findLightElement(const SVGElement* svgElement)
62{
63 for (auto& child : childrenOfType<SVGElement>(*svgElement)) {
64 if (is<SVGFEDistantLightElement>(child) || is<SVGFEPointLightElement>(child) || is<SVGFESpotLightElement>(child))
65 return static_cast<SVGFELightElement*>(const_cast<SVGElement*>(&child));
66 }
67 return nullptr;
68}
69
70void SVGFELightElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
71{
72 if (name == SVGNames::azimuthAttr) {
73 m_azimuth->setBaseValInternal(value.toFloat());
74 return;
75 }
76
77 if (name == SVGNames::elevationAttr) {
78 m_elevation->setBaseValInternal(value.toFloat());
79 return;
80 }
81
82 if (name == SVGNames::xAttr) {
83 m_x->setBaseValInternal(value.toFloat());
84 return;
85 }
86
87 if (name == SVGNames::yAttr) {
88 m_y->setBaseValInternal(value.toFloat());
89 return;
90 }
91
92 if (name == SVGNames::zAttr) {
93 m_z->setBaseValInternal(value.toFloat());
94 return;
95 }
96
97 if (name == SVGNames::pointsAtXAttr) {
98 m_pointsAtX->setBaseValInternal(value.toFloat());
99 return;
100 }
101
102 if (name == SVGNames::pointsAtYAttr) {
103 m_pointsAtY->setBaseValInternal(value.toFloat());
104 return;
105 }
106
107 if (name == SVGNames::pointsAtZAttr) {
108 m_pointsAtZ->setBaseValInternal(value.toFloat());
109 return;
110 }
111
112 if (name == SVGNames::specularExponentAttr) {
113 m_specularExponent->setBaseValInternal(value.toFloat());
114 return;
115 }
116
117 if (name == SVGNames::limitingConeAngleAttr) {
118 m_limitingConeAngle->setBaseValInternal(value.toFloat());
119 return;
120 }
121
122 SVGElement::parseAttribute(name, value);
123}
124
125void SVGFELightElement::svgAttributeChanged(const QualifiedName& attrName)
126{
127 if (PropertyRegistry::isKnownAttribute(attrName)) {
128 auto parent = makeRefPtr(parentElement());
129 if (!parent)
130 return;
131
132 auto* renderer = parent->renderer();
133 if (!renderer || !renderer->isSVGResourceFilterPrimitive())
134 return;
135
136 if (is<SVGFEDiffuseLightingElement>(*parent)) {
137 InstanceInvalidationGuard guard(*this);
138 downcast<SVGFEDiffuseLightingElement>(*parent).lightElementAttributeChanged(this, attrName);
139 } else if (is<SVGFESpecularLightingElement>(*parent)) {
140 InstanceInvalidationGuard guard(*this);
141 downcast<SVGFESpecularLightingElement>(*parent).lightElementAttributeChanged(this, attrName);
142 }
143
144 return;
145 }
146
147 SVGElement::svgAttributeChanged(attrName);
148}
149
150void SVGFELightElement::childrenChanged(const ChildChange& change)
151{
152 SVGElement::childrenChanged(change);
153
154 if (change.source == ChildChangeSource::Parser)
155 return;
156 auto parent = makeRefPtr(parentNode());
157 if (!parent)
158 return;
159 RenderElement* renderer = parent->renderer();
160 if (renderer && renderer->isSVGResourceFilterPrimitive())
161 RenderSVGResource::markForLayoutAndParentResourceInvalidation(*renderer);
162}
163
164}
165