2 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB. If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
21 #include "SVGFEConvolveMatrixElement.h"
24 #include "FilterEffect.h"
25 #include "FloatPoint.h"
28 #include "SVGElementInstance.h"
29 #include "SVGFilterBuilder.h"
31 #include "SVGParserUtilities.h"
32 #include <wtf/NeverDestroyed.h>
36 // Animated property definitions
37 DEFINE_ANIMATED_STRING(SVGFEConvolveMatrixElement, SVGNames::inAttr, In1, in1)
38 DEFINE_ANIMATED_INTEGER_MULTIPLE_WRAPPERS(SVGFEConvolveMatrixElement, SVGNames::orderAttr, orderXIdentifier(), OrderX, orderX)
39 DEFINE_ANIMATED_INTEGER_MULTIPLE_WRAPPERS(SVGFEConvolveMatrixElement, SVGNames::orderAttr, orderYIdentifier(), OrderY, orderY)
40 DEFINE_ANIMATED_NUMBER_LIST(SVGFEConvolveMatrixElement, SVGNames::kernelMatrixAttr, KernelMatrix, kernelMatrix)
41 DEFINE_ANIMATED_NUMBER(SVGFEConvolveMatrixElement, SVGNames::divisorAttr, Divisor, divisor)
42 DEFINE_ANIMATED_NUMBER(SVGFEConvolveMatrixElement, SVGNames::biasAttr, Bias, bias)
43 DEFINE_ANIMATED_INTEGER(SVGFEConvolveMatrixElement, SVGNames::targetXAttr, TargetX, targetX)
44 DEFINE_ANIMATED_INTEGER(SVGFEConvolveMatrixElement, SVGNames::targetYAttr, TargetY, targetY)
45 DEFINE_ANIMATED_ENUMERATION(SVGFEConvolveMatrixElement, SVGNames::edgeModeAttr, EdgeMode, edgeMode, EdgeModeType)
46 DEFINE_ANIMATED_NUMBER_MULTIPLE_WRAPPERS(SVGFEConvolveMatrixElement, SVGNames::kernelUnitLengthAttr, kernelUnitLengthXIdentifier(), KernelUnitLengthX, kernelUnitLengthX)
47 DEFINE_ANIMATED_NUMBER_MULTIPLE_WRAPPERS(SVGFEConvolveMatrixElement, SVGNames::kernelUnitLengthAttr, kernelUnitLengthYIdentifier(), KernelUnitLengthY, kernelUnitLengthY)
48 DEFINE_ANIMATED_BOOLEAN(SVGFEConvolveMatrixElement, SVGNames::preserveAlphaAttr, PreserveAlpha, preserveAlpha)
50 BEGIN_REGISTER_ANIMATED_PROPERTIES(SVGFEConvolveMatrixElement)
51 REGISTER_LOCAL_ANIMATED_PROPERTY(in1)
52 REGISTER_LOCAL_ANIMATED_PROPERTY(orderX)
53 REGISTER_LOCAL_ANIMATED_PROPERTY(orderY)
54 REGISTER_LOCAL_ANIMATED_PROPERTY(kernelMatrix)
55 REGISTER_LOCAL_ANIMATED_PROPERTY(divisor)
56 REGISTER_LOCAL_ANIMATED_PROPERTY(bias)
57 REGISTER_LOCAL_ANIMATED_PROPERTY(targetX)
58 REGISTER_LOCAL_ANIMATED_PROPERTY(targetY)
59 REGISTER_LOCAL_ANIMATED_PROPERTY(edgeMode)
60 REGISTER_LOCAL_ANIMATED_PROPERTY(kernelUnitLengthX)
61 REGISTER_LOCAL_ANIMATED_PROPERTY(kernelUnitLengthY)
62 REGISTER_LOCAL_ANIMATED_PROPERTY(preserveAlpha)
63 REGISTER_PARENT_ANIMATED_PROPERTIES(SVGFilterPrimitiveStandardAttributes)
64 END_REGISTER_ANIMATED_PROPERTIES
66 inline SVGFEConvolveMatrixElement::SVGFEConvolveMatrixElement(const QualifiedName& tagName, Document& document)
67 : SVGFilterPrimitiveStandardAttributes(tagName, document)
68 , m_edgeMode(EDGEMODE_DUPLICATE)
70 ASSERT(hasTagName(SVGNames::feConvolveMatrixTag));
71 registerAnimatedPropertiesForSVGFEConvolveMatrixElement();
74 PassRefPtr<SVGFEConvolveMatrixElement> SVGFEConvolveMatrixElement::create(const QualifiedName& tagName, Document& document)
76 return adoptRef(new SVGFEConvolveMatrixElement(tagName, document));
79 const AtomicString& SVGFEConvolveMatrixElement::kernelUnitLengthXIdentifier()
81 DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGKernelUnitLengthX", AtomicString::ConstructFromLiteral));
85 const AtomicString& SVGFEConvolveMatrixElement::kernelUnitLengthYIdentifier()
87 DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGKernelUnitLengthY", AtomicString::ConstructFromLiteral));
91 const AtomicString& SVGFEConvolveMatrixElement::orderXIdentifier()
93 DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGOrderX", AtomicString::ConstructFromLiteral));
97 const AtomicString& SVGFEConvolveMatrixElement::orderYIdentifier()
99 DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, s_identifier, ("SVGOrderY", AtomicString::ConstructFromLiteral));
103 bool SVGFEConvolveMatrixElement::isSupportedAttribute(const QualifiedName& attrName)
105 static NeverDestroyed<HashSet<QualifiedName>> supportedAttributes;
106 if (supportedAttributes.get().isEmpty()) {
107 supportedAttributes.get().add(SVGNames::inAttr);
108 supportedAttributes.get().add(SVGNames::orderAttr);
109 supportedAttributes.get().add(SVGNames::kernelMatrixAttr);
110 supportedAttributes.get().add(SVGNames::edgeModeAttr);
111 supportedAttributes.get().add(SVGNames::divisorAttr);
112 supportedAttributes.get().add(SVGNames::biasAttr);
113 supportedAttributes.get().add(SVGNames::targetXAttr);
114 supportedAttributes.get().add(SVGNames::targetYAttr);
115 supportedAttributes.get().add(SVGNames::kernelUnitLengthAttr);
116 supportedAttributes.get().add(SVGNames::preserveAlphaAttr);
118 return supportedAttributes.get().contains<SVGAttributeHashTranslator>(attrName);
121 void SVGFEConvolveMatrixElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
123 if (!isSupportedAttribute(name)) {
124 SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
128 if (name == SVGNames::inAttr) {
129 setIn1BaseValue(value);
133 if (name == SVGNames::orderAttr) {
135 if (parseNumberOptionalNumber(value, x, y) && x >= 1 && y >= 1) {
136 setOrderXBaseValue(x);
137 setOrderYBaseValue(y);
139 document().accessSVGExtensions().reportWarning(
140 "feConvolveMatrix: problem parsing order=\"" + value
141 + "\". Filtered element will not be displayed.");
145 if (name == SVGNames::edgeModeAttr) {
146 EdgeModeType propertyValue = SVGPropertyTraits<EdgeModeType>::fromString(value);
147 if (propertyValue > 0)
148 setEdgeModeBaseValue(propertyValue);
150 document().accessSVGExtensions().reportWarning(
151 "feConvolveMatrix: problem parsing edgeMode=\"" + value
152 + "\". Filtered element will not be displayed.");
156 if (name == SVGNames::kernelMatrixAttr) {
157 SVGNumberList newList;
158 newList.parse(value);
159 detachAnimatedKernelMatrixListWrappers(newList.size());
160 setKernelMatrixBaseValue(newList);
164 if (name == SVGNames::divisorAttr) {
165 float divisor = value.toFloat();
167 setDivisorBaseValue(divisor);
169 document().accessSVGExtensions().reportWarning(
170 "feConvolveMatrix: problem parsing divisor=\"" + value
171 + "\". Filtered element will not be displayed.");
175 if (name == SVGNames::biasAttr) {
176 setBiasBaseValue(value.toFloat());
180 if (name == SVGNames::targetXAttr) {
181 setTargetXBaseValue(value.string().toUIntStrict());
185 if (name == SVGNames::targetYAttr) {
186 setTargetYBaseValue(value.string().toUIntStrict());
190 if (name == SVGNames::kernelUnitLengthAttr) {
192 if (parseNumberOptionalNumber(value, x, y) && x > 0 && y > 0) {
193 setKernelUnitLengthXBaseValue(x);
194 setKernelUnitLengthYBaseValue(y);
196 document().accessSVGExtensions().reportWarning(
197 "feConvolveMatrix: problem parsing kernelUnitLength=\"" + value
198 + "\". Filtered element will not be displayed.");
202 if (name == SVGNames::preserveAlphaAttr) {
204 setPreserveAlphaBaseValue(true);
205 else if (value == "false")
206 setPreserveAlphaBaseValue(false);
208 document().accessSVGExtensions().reportWarning(
209 "feConvolveMatrix: problem parsing preserveAlphaAttr=\"" + value
210 + "\". Filtered element will not be displayed.");
214 ASSERT_NOT_REACHED();
217 bool SVGFEConvolveMatrixElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
219 FEConvolveMatrix* convolveMatrix = static_cast<FEConvolveMatrix*>(effect);
220 if (attrName == SVGNames::edgeModeAttr)
221 return convolveMatrix->setEdgeMode(edgeMode());
222 if (attrName == SVGNames::divisorAttr)
223 return convolveMatrix->setDivisor(divisor());
224 if (attrName == SVGNames::biasAttr)
225 return convolveMatrix->setBias(bias());
226 if (attrName == SVGNames::targetXAttr)
227 return convolveMatrix->setTargetOffset(IntPoint(targetX(), targetY()));
228 if (attrName == SVGNames::targetYAttr)
229 return convolveMatrix->setTargetOffset(IntPoint(targetX(), targetY()));
230 if (attrName == SVGNames::kernelUnitLengthAttr)
231 return convolveMatrix->setKernelUnitLength(FloatPoint(kernelUnitLengthX(), kernelUnitLengthY()));
232 if (attrName == SVGNames::preserveAlphaAttr)
233 return convolveMatrix->setPreserveAlpha(preserveAlpha());
235 ASSERT_NOT_REACHED();
239 void SVGFEConvolveMatrixElement::setOrder(float x, float y)
241 setOrderXBaseValue(x);
242 setOrderYBaseValue(y);
246 void SVGFEConvolveMatrixElement::setKernelUnitLength(float x, float y)
248 setKernelUnitLengthXBaseValue(x);
249 setKernelUnitLengthYBaseValue(y);
253 void SVGFEConvolveMatrixElement::svgAttributeChanged(const QualifiedName& attrName)
255 if (!isSupportedAttribute(attrName)) {
256 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
260 SVGElementInstance::InvalidationGuard invalidationGuard(this);
262 if (attrName == SVGNames::edgeModeAttr
263 || attrName == SVGNames::divisorAttr
264 || attrName == SVGNames::biasAttr
265 || attrName == SVGNames::targetXAttr
266 || attrName == SVGNames::targetYAttr
267 || attrName == SVGNames::kernelUnitLengthAttr
268 || attrName == SVGNames::preserveAlphaAttr) {
269 primitiveAttributeChanged(attrName);
273 if (attrName == SVGNames::inAttr
274 || attrName == SVGNames::orderAttr
275 || attrName == SVGNames::kernelMatrixAttr) {
280 ASSERT_NOT_REACHED();
283 PassRefPtr<FilterEffect> SVGFEConvolveMatrixElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
285 FilterEffect* input1 = filterBuilder->getEffectById(in1());
290 int orderXValue = orderX();
291 int orderYValue = orderY();
292 if (!hasAttribute(SVGNames::orderAttr)) {
296 // Spec says order must be > 0. Bail if it is not.
297 if (orderXValue < 1 || orderYValue < 1)
299 SVGNumberList& kernelMatrix = this->kernelMatrix();
300 int kernelMatrixSize = kernelMatrix.size();
301 // The spec says this is a requirement, and should bail out if fails
302 if (orderXValue * orderYValue != kernelMatrixSize)
305 int targetXValue = targetX();
306 int targetYValue = targetY();
307 if (hasAttribute(SVGNames::targetXAttr) && (targetXValue < 0 || targetXValue >= orderXValue))
309 // The spec says the default value is: targetX = floor ( orderX / 2 ))
310 if (!hasAttribute(SVGNames::targetXAttr))
311 targetXValue = static_cast<int>(floorf(orderXValue / 2));
312 if (hasAttribute(SVGNames::targetYAttr) && (targetYValue < 0 || targetYValue >= orderYValue))
314 // The spec says the default value is: targetY = floor ( orderY / 2 ))
315 if (!hasAttribute(SVGNames::targetYAttr))
316 targetYValue = static_cast<int>(floorf(orderYValue / 2));
318 // Spec says default kernelUnitLength is 1.0, and a specified length cannot be 0.
319 int kernelUnitLengthXValue = kernelUnitLengthX();
320 int kernelUnitLengthYValue = kernelUnitLengthY();
321 if (!hasAttribute(SVGNames::kernelUnitLengthAttr)) {
322 kernelUnitLengthXValue = 1;
323 kernelUnitLengthYValue = 1;
325 if (kernelUnitLengthXValue <= 0 || kernelUnitLengthYValue <= 0)
328 float divisorValue = divisor();
329 if (hasAttribute(SVGNames::divisorAttr) && !divisorValue)
331 if (!hasAttribute(SVGNames::divisorAttr)) {
332 for (int i = 0; i < kernelMatrixSize; ++i)
333 divisorValue += kernelMatrix.at(i);
338 RefPtr<FilterEffect> effect = FEConvolveMatrix::create(filter,
339 IntSize(orderXValue, orderYValue), divisorValue,
340 bias(), IntPoint(targetXValue, targetYValue), edgeMode(),
341 FloatPoint(kernelUnitLengthXValue, kernelUnitLengthYValue), preserveAlpha(), kernelMatrix);
342 effect->inputEffects().append(input1);
343 return effect.release();
346 } // namespace WebCore