1/*
2 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
3 * Copyright (C) 2018-2019 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 "SVGFEMorphologyElement.h"
23
24#include "FilterEffect.h"
25#include "SVGFilterBuilder.h"
26#include "SVGNames.h"
27#include "SVGParserUtilities.h"
28#include <wtf/IsoMallocInlines.h>
29
30namespace WebCore {
31
32WTF_MAKE_ISO_ALLOCATED_IMPL(SVGFEMorphologyElement);
33
34inline SVGFEMorphologyElement::SVGFEMorphologyElement(const QualifiedName& tagName, Document& document)
35 : SVGFilterPrimitiveStandardAttributes(tagName, document)
36{
37 ASSERT(hasTagName(SVGNames::feMorphologyTag));
38
39 static std::once_flag onceFlag;
40 std::call_once(onceFlag, [] {
41 PropertyRegistry::registerProperty<SVGNames::inAttr, &SVGFEMorphologyElement::m_in1>();
42 PropertyRegistry::registerProperty<SVGNames::operatorAttr, MorphologyOperatorType, &SVGFEMorphologyElement::m_svgOperator>();
43 PropertyRegistry::registerProperty<SVGNames::radiusAttr, &SVGFEMorphologyElement::m_radiusX, &SVGFEMorphologyElement::m_radiusY>();
44 });
45}
46
47Ref<SVGFEMorphologyElement> SVGFEMorphologyElement::create(const QualifiedName& tagName, Document& document)
48{
49 return adoptRef(*new SVGFEMorphologyElement(tagName, document));
50}
51
52void SVGFEMorphologyElement::setRadius(float x, float y)
53{
54 m_radiusX->setBaseValInternal(x);
55 m_radiusY->setBaseValInternal(y);
56 invalidate();
57}
58
59void SVGFEMorphologyElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
60{
61 if (name == SVGNames::operatorAttr) {
62 MorphologyOperatorType propertyValue = SVGPropertyTraits<MorphologyOperatorType>::fromString(value);
63 if (propertyValue > 0)
64 m_svgOperator->setBaseValInternal<MorphologyOperatorType>(propertyValue);
65 return;
66 }
67
68 if (name == SVGNames::inAttr) {
69 m_in1->setBaseValInternal(value);
70 return;
71 }
72
73 if (name == SVGNames::radiusAttr) {
74 float x, y;
75 if (parseNumberOptionalNumber(value, x, y)) {
76 m_radiusX->setBaseValInternal(x);
77 m_radiusY->setBaseValInternal(y);
78 }
79 return;
80 }
81
82 SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
83}
84
85bool SVGFEMorphologyElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
86{
87 FEMorphology* morphology = static_cast<FEMorphology*>(effect);
88 if (attrName == SVGNames::operatorAttr)
89 return morphology->setMorphologyOperator(svgOperator());
90 if (attrName == SVGNames::radiusAttr) {
91 // Both setRadius functions should be evaluated separately.
92 bool isRadiusXChanged = morphology->setRadiusX(radiusX());
93 bool isRadiusYChanged = morphology->setRadiusY(radiusY());
94 return isRadiusXChanged || isRadiusYChanged;
95 }
96
97 ASSERT_NOT_REACHED();
98 return false;
99}
100
101void SVGFEMorphologyElement::svgAttributeChanged(const QualifiedName& attrName)
102{
103 if (attrName == SVGNames::operatorAttr || attrName == SVGNames::radiusAttr) {
104 InstanceInvalidationGuard guard(*this);
105 primitiveAttributeChanged(attrName);
106 return;
107 }
108
109 if (attrName == SVGNames::inAttr) {
110 InstanceInvalidationGuard guard(*this);
111 invalidate();
112 return;
113 }
114
115 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
116}
117
118RefPtr<FilterEffect> SVGFEMorphologyElement::build(SVGFilterBuilder* filterBuilder, Filter& filter) const
119{
120 auto input1 = filterBuilder->getEffectById(in1());
121 float xRadius = radiusX();
122 float yRadius = radiusY();
123
124 if (!input1)
125 return nullptr;
126
127 if (xRadius < 0 || yRadius < 0)
128 return nullptr;
129
130 auto effect = FEMorphology::create(filter, svgOperator(), xRadius, yRadius);
131 effect->inputEffects().append(input1);
132 return effect;
133}
134
135} // namespace WebCore
136