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 "core/svg/SVGFEConvolveMatrixElement.h"
23 #include "core/SVGNames.h"
24 #include "core/dom/Document.h"
25 #include "core/svg/SVGDocumentExtensions.h"
26 #include "core/svg/SVGParserUtilities.h"
27 #include "core/svg/graphics/filters/SVGFilterBuilder.h"
28 #include "platform/geometry/FloatPoint.h"
29 #include "platform/geometry/IntPoint.h"
30 #include "platform/geometry/IntSize.h"
31 #include "platform/graphics/filters/FilterEffect.h"
35 template<> const SVGEnumerationStringEntries& getStaticStringEntries<EdgeModeType>()
37 DEFINE_STATIC_LOCAL(SVGEnumerationStringEntries, entries, ());
38 if (entries.isEmpty()) {
39 entries.append(std::make_pair(EDGEMODE_DUPLICATE, "duplicate"));
40 entries.append(std::make_pair(EDGEMODE_WRAP, "wrap"));
41 entries.append(std::make_pair(EDGEMODE_NONE, "none"));
46 class SVGAnimatedOrder : public SVGAnimatedIntegerOptionalInteger {
48 static PassRefPtr<SVGAnimatedOrder> create(SVGElement* contextElement)
50 return adoptRef(new SVGAnimatedOrder(contextElement));
53 void setBaseValueAsString(const String&, SVGParsingError&) override;
56 SVGAnimatedOrder(SVGElement* contextElement)
57 : SVGAnimatedIntegerOptionalInteger(contextElement, SVGNames::orderAttr, 0, 0)
62 void SVGAnimatedOrder::setBaseValueAsString(const String& value, SVGParsingError& parseError)
64 SVGAnimatedIntegerOptionalInteger::setBaseValueAsString(value, parseError);
66 ASSERT(contextElement());
67 if (parseError == NoError && (firstInteger()->baseValue()->value() < 1 || secondInteger()->baseValue()->value() < 1)) {
68 contextElement()->document().accessSVGExtensions().reportWarning(
69 "feConvolveMatrix: problem parsing order=\"" + value
70 + "\". Filtered element will not be displayed.");
74 inline SVGFEConvolveMatrixElement::SVGFEConvolveMatrixElement(Document& document)
75 : SVGFilterPrimitiveStandardAttributes(SVGNames::feConvolveMatrixTag, document)
76 , m_bias(SVGAnimatedNumber::create(this, SVGNames::biasAttr, SVGNumber::create()))
77 , m_divisor(SVGAnimatedNumber::create(this, SVGNames::divisorAttr, SVGNumber::create()))
78 , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
79 , m_edgeMode(SVGAnimatedEnumeration<EdgeModeType>::create(this, SVGNames::edgeModeAttr, EDGEMODE_DUPLICATE))
80 , m_kernelMatrix(SVGAnimatedNumberList::create(this, SVGNames::kernelMatrixAttr, SVGNumberList::create()))
81 , m_kernelUnitLength(SVGAnimatedNumberOptionalNumber::create(this, SVGNames::kernelUnitLengthAttr))
82 , m_order(SVGAnimatedOrder::create(this))
83 , m_preserveAlpha(SVGAnimatedBoolean::create(this, SVGNames::preserveAlphaAttr, SVGBoolean::create()))
84 , m_targetX(SVGAnimatedInteger::create(this, SVGNames::targetXAttr, SVGInteger::create()))
85 , m_targetY(SVGAnimatedInteger::create(this, SVGNames::targetYAttr, SVGInteger::create()))
87 addToPropertyMap(m_preserveAlpha);
88 addToPropertyMap(m_divisor);
89 addToPropertyMap(m_bias);
90 addToPropertyMap(m_kernelUnitLength);
91 addToPropertyMap(m_kernelMatrix);
92 addToPropertyMap(m_in1);
93 addToPropertyMap(m_edgeMode);
94 addToPropertyMap(m_order);
95 addToPropertyMap(m_targetX);
96 addToPropertyMap(m_targetY);
99 DEFINE_NODE_FACTORY(SVGFEConvolveMatrixElement)
101 bool SVGFEConvolveMatrixElement::isSupportedAttribute(const QualifiedName& attrName)
103 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
104 if (supportedAttributes.isEmpty()) {
105 supportedAttributes.add(SVGNames::inAttr);
106 supportedAttributes.add(SVGNames::orderAttr);
107 supportedAttributes.add(SVGNames::kernelMatrixAttr);
108 supportedAttributes.add(SVGNames::edgeModeAttr);
109 supportedAttributes.add(SVGNames::divisorAttr);
110 supportedAttributes.add(SVGNames::biasAttr);
111 supportedAttributes.add(SVGNames::targetXAttr);
112 supportedAttributes.add(SVGNames::targetYAttr);
113 supportedAttributes.add(SVGNames::kernelUnitLengthAttr);
114 supportedAttributes.add(SVGNames::preserveAlphaAttr);
116 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
119 void SVGFEConvolveMatrixElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
121 parseAttributeNew(name, value);
124 bool SVGFEConvolveMatrixElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
126 FEConvolveMatrix* convolveMatrix = static_cast<FEConvolveMatrix*>(effect);
127 if (attrName == SVGNames::edgeModeAttr)
128 return convolveMatrix->setEdgeMode(m_edgeMode->currentValue()->enumValue());
129 if (attrName == SVGNames::divisorAttr)
130 return convolveMatrix->setDivisor(m_divisor->currentValue()->value());
131 if (attrName == SVGNames::biasAttr)
132 return convolveMatrix->setBias(m_bias->currentValue()->value());
133 if (attrName == SVGNames::targetXAttr)
134 return convolveMatrix->setTargetOffset(IntPoint(m_targetX->currentValue()->value(), m_targetY->currentValue()->value()));
135 if (attrName == SVGNames::targetYAttr)
136 return convolveMatrix->setTargetOffset(IntPoint(m_targetX->currentValue()->value(), m_targetY->currentValue()->value()));
137 if (attrName == SVGNames::kernelUnitLengthAttr)
138 return convolveMatrix->setKernelUnitLength(FloatPoint(kernelUnitLengthX()->currentValue()->value(), kernelUnitLengthY()->currentValue()->value()));
139 if (attrName == SVGNames::preserveAlphaAttr)
140 return convolveMatrix->setPreserveAlpha(m_preserveAlpha->currentValue()->value());
142 ASSERT_NOT_REACHED();
146 void SVGFEConvolveMatrixElement::svgAttributeChanged(const QualifiedName& attrName)
148 if (!isSupportedAttribute(attrName)) {
149 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
153 SVGElement::InvalidationGuard invalidationGuard(this);
155 if (attrName == SVGNames::edgeModeAttr
156 || attrName == SVGNames::divisorAttr
157 || attrName == SVGNames::biasAttr
158 || attrName == SVGNames::targetXAttr
159 || attrName == SVGNames::targetYAttr
160 || attrName == SVGNames::kernelUnitLengthAttr
161 || attrName == SVGNames::preserveAlphaAttr) {
162 primitiveAttributeChanged(attrName);
166 if (attrName == SVGNames::inAttr
167 || attrName == SVGNames::orderAttr
168 || attrName == SVGNames::kernelMatrixAttr) {
173 ASSERT_NOT_REACHED();
176 PassRefPtr<FilterEffect> SVGFEConvolveMatrixElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
178 FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
183 int orderXValue = orderX()->currentValue()->value();
184 int orderYValue = orderY()->currentValue()->value();
185 if (!hasAttribute(SVGNames::orderAttr)) {
189 // Spec says order must be > 0. Bail if it is not.
190 if (orderXValue < 1 || orderYValue < 1)
192 RefPtr<SVGNumberList> kernelMatrix = this->m_kernelMatrix->currentValue();
193 size_t kernelMatrixSize = kernelMatrix->length();
194 // The spec says this is a requirement, and should bail out if fails
195 if (orderXValue * orderYValue != static_cast<int>(kernelMatrixSize))
198 int targetXValue = m_targetX->currentValue()->value();
199 int targetYValue = m_targetY->currentValue()->value();
200 if (hasAttribute(SVGNames::targetXAttr) && (targetXValue < 0 || targetXValue >= orderXValue))
202 // The spec says the default value is: targetX = floor ( orderX / 2 ))
203 if (!hasAttribute(SVGNames::targetXAttr))
204 targetXValue = static_cast<int>(floorf(orderXValue / 2));
205 if (hasAttribute(SVGNames::targetYAttr) && (targetYValue < 0 || targetYValue >= orderYValue))
207 // The spec says the default value is: targetY = floor ( orderY / 2 ))
208 if (!hasAttribute(SVGNames::targetYAttr))
209 targetYValue = static_cast<int>(floorf(orderYValue / 2));
211 // Spec says default kernelUnitLength is 1.0, and a specified length cannot be 0.
212 // FIXME: Why is this cast from float -> int -> float?
213 int kernelUnitLengthXValue = kernelUnitLengthX()->currentValue()->value();
214 int kernelUnitLengthYValue = kernelUnitLengthY()->currentValue()->value();
215 if (!hasAttribute(SVGNames::kernelUnitLengthAttr)) {
216 kernelUnitLengthXValue = 1;
217 kernelUnitLengthYValue = 1;
219 if (kernelUnitLengthXValue <= 0 || kernelUnitLengthYValue <= 0)
222 float divisorValue = m_divisor->currentValue()->value();
223 if (hasAttribute(SVGNames::divisorAttr) && !divisorValue)
225 if (!hasAttribute(SVGNames::divisorAttr)) {
226 for (size_t i = 0; i < kernelMatrixSize; ++i)
227 divisorValue += kernelMatrix->at(i)->value();
232 RefPtr<FilterEffect> effect = FEConvolveMatrix::create(filter,
233 IntSize(orderXValue, orderYValue), divisorValue,
234 m_bias->currentValue()->value(), IntPoint(targetXValue, targetYValue), m_edgeMode->currentValue()->enumValue(),
235 FloatPoint(kernelUnitLengthXValue, kernelUnitLengthYValue), m_preserveAlpha->currentValue()->value(), m_kernelMatrix->currentValue()->toFloatVector());
236 effect->inputEffects().append(input1);
237 return effect.release();