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) 2018-2019 Apple Inc. All rights reserved. |
5 | * |
6 | * This library is free software; you can redistribute it and/or |
7 | * modify it under the terms of the GNU Library General Public |
8 | * License as published by the Free Software Foundation; either |
9 | * version 2 of the License, or (at your option) any later version. |
10 | * |
11 | * This library is distributed in the hope that it will be useful, |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 | * Library General Public License for more details. |
15 | * |
16 | * You should have received a copy of the GNU Library General Public License |
17 | * along with this library; see the file COPYING.LIB. If not, write to |
18 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
19 | * Boston, MA 02110-1301, USA. |
20 | */ |
21 | |
22 | #include "config.h" |
23 | #include "SVGFECompositeElement.h" |
24 | |
25 | #include "FilterEffect.h" |
26 | #include "SVGFilterBuilder.h" |
27 | #include "SVGNames.h" |
28 | #include <wtf/IsoMallocInlines.h> |
29 | |
30 | namespace WebCore { |
31 | |
32 | WTF_MAKE_ISO_ALLOCATED_IMPL(SVGFECompositeElement); |
33 | |
34 | inline SVGFECompositeElement::SVGFECompositeElement(const QualifiedName& tagName, Document& document) |
35 | : SVGFilterPrimitiveStandardAttributes(tagName, document) |
36 | { |
37 | ASSERT(hasTagName(SVGNames::feCompositeTag)); |
38 | |
39 | static std::once_flag onceFlag; |
40 | std::call_once(onceFlag, [] { |
41 | PropertyRegistry::registerProperty<SVGNames::inAttr, &SVGFECompositeElement::m_in1>(); |
42 | PropertyRegistry::registerProperty<SVGNames::in2Attr, &SVGFECompositeElement::m_in2>(); |
43 | PropertyRegistry::registerProperty<SVGNames::operatorAttr, CompositeOperationType, &SVGFECompositeElement::m_svgOperator>(); |
44 | PropertyRegistry::registerProperty<SVGNames::k1Attr, &SVGFECompositeElement::m_k1>(); |
45 | PropertyRegistry::registerProperty<SVGNames::k2Attr, &SVGFECompositeElement::m_k2>(); |
46 | PropertyRegistry::registerProperty<SVGNames::k3Attr, &SVGFECompositeElement::m_k3>(); |
47 | PropertyRegistry::registerProperty<SVGNames::k4Attr, &SVGFECompositeElement::m_k4>(); |
48 | }); |
49 | } |
50 | |
51 | Ref<SVGFECompositeElement> SVGFECompositeElement::create(const QualifiedName& tagName, Document& document) |
52 | { |
53 | return adoptRef(*new SVGFECompositeElement(tagName, document)); |
54 | } |
55 | |
56 | void SVGFECompositeElement::parseAttribute(const QualifiedName& name, const AtomicString& value) |
57 | { |
58 | if (name == SVGNames::operatorAttr) { |
59 | CompositeOperationType propertyValue = SVGPropertyTraits<CompositeOperationType>::fromString(value); |
60 | if (propertyValue > 0) |
61 | m_svgOperator->setBaseValInternal<CompositeOperationType>(propertyValue); |
62 | return; |
63 | } |
64 | |
65 | if (name == SVGNames::inAttr) { |
66 | m_in1->setBaseValInternal(value); |
67 | return; |
68 | } |
69 | |
70 | if (name == SVGNames::in2Attr) { |
71 | m_in2->setBaseValInternal(value); |
72 | return; |
73 | } |
74 | |
75 | if (name == SVGNames::k1Attr) { |
76 | m_k1->setBaseValInternal(value.toFloat()); |
77 | return; |
78 | } |
79 | |
80 | if (name == SVGNames::k2Attr) { |
81 | m_k2->setBaseValInternal(value.toFloat()); |
82 | return; |
83 | } |
84 | |
85 | if (name == SVGNames::k3Attr) { |
86 | m_k3->setBaseValInternal(value.toFloat()); |
87 | return; |
88 | } |
89 | |
90 | if (name == SVGNames::k4Attr) { |
91 | m_k4->setBaseValInternal(value.toFloat()); |
92 | return; |
93 | } |
94 | |
95 | SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value); |
96 | } |
97 | |
98 | bool SVGFECompositeElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName) |
99 | { |
100 | FEComposite* composite = static_cast<FEComposite*>(effect); |
101 | if (attrName == SVGNames::operatorAttr) |
102 | return composite->setOperation(svgOperator()); |
103 | if (attrName == SVGNames::k1Attr) |
104 | return composite->setK1(k1()); |
105 | if (attrName == SVGNames::k2Attr) |
106 | return composite->setK2(k2()); |
107 | if (attrName == SVGNames::k3Attr) |
108 | return composite->setK3(k3()); |
109 | if (attrName == SVGNames::k4Attr) |
110 | return composite->setK4(k4()); |
111 | |
112 | ASSERT_NOT_REACHED(); |
113 | return false; |
114 | } |
115 | |
116 | |
117 | void SVGFECompositeElement::svgAttributeChanged(const QualifiedName& attrName) |
118 | { |
119 | if (attrName == SVGNames::operatorAttr || attrName == SVGNames::k1Attr || attrName == SVGNames::k2Attr || attrName == SVGNames::k3Attr || attrName == SVGNames::k4Attr) { |
120 | InstanceInvalidationGuard guard(*this); |
121 | primitiveAttributeChanged(attrName); |
122 | return; |
123 | } |
124 | |
125 | if (attrName == SVGNames::inAttr || attrName == SVGNames::in2Attr) { |
126 | InstanceInvalidationGuard guard(*this); |
127 | invalidate(); |
128 | return; |
129 | } |
130 | |
131 | SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName); |
132 | } |
133 | |
134 | RefPtr<FilterEffect> SVGFECompositeElement::build(SVGFilterBuilder* filterBuilder, Filter& filter) const |
135 | { |
136 | auto input1 = filterBuilder->getEffectById(in1()); |
137 | auto input2 = filterBuilder->getEffectById(in2()); |
138 | |
139 | if (!input1 || !input2) |
140 | return nullptr; |
141 | |
142 | auto effect = FEComposite::create(filter, svgOperator(), k1(), k2(), k3(), k4()); |
143 | FilterEffectVector& inputEffects = effect->inputEffects(); |
144 | inputEffects.reserveCapacity(2); |
145 | inputEffects.append(input1); |
146 | inputEffects.append(input2); |
147 | return effect; |
148 | } |
149 | |
150 | } |
151 | |