1/*
2 * Copyright (C) 2005 Oliver Hunt <ojh16@student.canterbury.ac.nz>
3 * Copyright (C) 2018 Apple Inc. All rights reserved.
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#include "SVGFEDiffuseLightingElement.h"
23
24#include "FEDiffuseLighting.h"
25#include "FilterEffect.h"
26#include "RenderStyle.h"
27#include "SVGFELightElement.h"
28#include "SVGFilterBuilder.h"
29#include "SVGNames.h"
30#include "SVGParserUtilities.h"
31#include <wtf/IsoMallocInlines.h>
32
33namespace WebCore {
34
35WTF_MAKE_ISO_ALLOCATED_IMPL(SVGFEDiffuseLightingElement);
36
37inline SVGFEDiffuseLightingElement::SVGFEDiffuseLightingElement(const QualifiedName& tagName, Document& document)
38 : SVGFilterPrimitiveStandardAttributes(tagName, document)
39{
40 ASSERT(hasTagName(SVGNames::feDiffuseLightingTag));
41
42 static std::once_flag onceFlag;
43 std::call_once(onceFlag, [] {
44 PropertyRegistry::registerProperty<SVGNames::inAttr, &SVGFEDiffuseLightingElement::m_in1>();
45 PropertyRegistry::registerProperty<SVGNames::diffuseConstantAttr, &SVGFEDiffuseLightingElement::m_diffuseConstant>();
46 PropertyRegistry::registerProperty<SVGNames::surfaceScaleAttr, &SVGFEDiffuseLightingElement::m_surfaceScale>();
47 PropertyRegistry::registerProperty<SVGNames::kernelUnitLengthAttr, &SVGFEDiffuseLightingElement::m_kernelUnitLengthX, &SVGFEDiffuseLightingElement::m_kernelUnitLengthY>();
48 });
49}
50
51Ref<SVGFEDiffuseLightingElement> SVGFEDiffuseLightingElement::create(const QualifiedName& tagName, Document& document)
52{
53 return adoptRef(*new SVGFEDiffuseLightingElement(tagName, document));
54}
55
56void SVGFEDiffuseLightingElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
57{
58 if (name == SVGNames::inAttr) {
59 m_in1->setBaseValInternal(value);
60 return;
61 }
62
63 if (name == SVGNames::surfaceScaleAttr) {
64 m_surfaceScale->setBaseValInternal(value.toFloat());
65 return;
66 }
67
68 if (name == SVGNames::diffuseConstantAttr) {
69 m_diffuseConstant->setBaseValInternal(value.toFloat());
70 return;
71 }
72
73 if (name == SVGNames::kernelUnitLengthAttr) {
74 float x, y;
75 if (parseNumberOptionalNumber(value, x, y)) {
76 m_kernelUnitLengthX->setBaseValInternal(x);
77 m_kernelUnitLengthY->setBaseValInternal(y);
78 }
79 return;
80 }
81
82 SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
83}
84
85bool SVGFEDiffuseLightingElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
86{
87 FEDiffuseLighting* diffuseLighting = static_cast<FEDiffuseLighting*>(effect);
88
89 if (attrName == SVGNames::lighting_colorAttr) {
90 RenderObject* renderer = this->renderer();
91 ASSERT(renderer);
92 Color color = renderer->style().colorByApplyingColorFilter(renderer->style().svgStyle().lightingColor());
93 return diffuseLighting->setLightingColor(color);
94 }
95 if (attrName == SVGNames::surfaceScaleAttr)
96 return diffuseLighting->setSurfaceScale(surfaceScale());
97 if (attrName == SVGNames::diffuseConstantAttr)
98 return diffuseLighting->setDiffuseConstant(diffuseConstant());
99
100 auto& lightSource = const_cast<LightSource&>(diffuseLighting->lightSource());
101 const SVGFELightElement* lightElement = SVGFELightElement::findLightElement(this);
102 ASSERT(lightElement);
103
104 if (attrName == SVGNames::azimuthAttr)
105 return lightSource.setAzimuth(lightElement->azimuth());
106 if (attrName == SVGNames::elevationAttr)
107 return lightSource.setElevation(lightElement->elevation());
108 if (attrName == SVGNames::xAttr)
109 return lightSource.setX(lightElement->x());
110 if (attrName == SVGNames::yAttr)
111 return lightSource.setY(lightElement->y());
112 if (attrName == SVGNames::zAttr)
113 return lightSource.setZ(lightElement->z());
114 if (attrName == SVGNames::pointsAtXAttr)
115 return lightSource.setPointsAtX(lightElement->pointsAtX());
116 if (attrName == SVGNames::pointsAtYAttr)
117 return lightSource.setPointsAtY(lightElement->pointsAtY());
118 if (attrName == SVGNames::pointsAtZAttr)
119 return lightSource.setPointsAtZ(lightElement->pointsAtZ());
120 if (attrName == SVGNames::specularExponentAttr)
121 return lightSource.setSpecularExponent(lightElement->specularExponent());
122 if (attrName == SVGNames::limitingConeAngleAttr)
123 return lightSource.setLimitingConeAngle(lightElement->limitingConeAngle());
124
125 ASSERT_NOT_REACHED();
126 return false;
127}
128
129void SVGFEDiffuseLightingElement::svgAttributeChanged(const QualifiedName& attrName)
130{
131 if (attrName == SVGNames::surfaceScaleAttr || attrName == SVGNames::diffuseConstantAttr || attrName == SVGNames::kernelUnitLengthAttr || attrName == SVGNames::lighting_colorAttr) {
132 InstanceInvalidationGuard guard(*this);
133 primitiveAttributeChanged(attrName);
134 return;
135 }
136
137 if (attrName == SVGNames::inAttr) {
138 InstanceInvalidationGuard guard(*this);
139 invalidate();
140 return;
141 }
142
143 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
144}
145
146void SVGFEDiffuseLightingElement::lightElementAttributeChanged(const SVGFELightElement* lightElement, const QualifiedName& attrName)
147{
148 if (SVGFELightElement::findLightElement(this) != lightElement)
149 return;
150
151 // The light element has different attribute names.
152 primitiveAttributeChanged(attrName);
153}
154
155RefPtr<FilterEffect> SVGFEDiffuseLightingElement::build(SVGFilterBuilder* filterBuilder, Filter& filter) const
156{
157 auto input1 = filterBuilder->getEffectById(in1());
158
159 if (!input1)
160 return nullptr;
161
162 auto lightElement = makeRefPtr(SVGFELightElement::findLightElement(this));
163 if (!lightElement)
164 return nullptr;
165
166 auto lightSource = lightElement->lightSource(*filterBuilder);
167
168 RenderObject* renderer = this->renderer();
169 if (!renderer)
170 return nullptr;
171
172 Color color = renderer->style().colorByApplyingColorFilter(renderer->style().svgStyle().lightingColor());
173
174 auto effect = FEDiffuseLighting::create(filter, color, surfaceScale(), diffuseConstant(), kernelUnitLengthX(), kernelUnitLengthY(), WTFMove(lightSource));
175 effect->inputEffects().append(input1);
176 return effect;
177}
178
179}
180