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.
22 #include "core/svg/SVGFEConvolveMatrixElement.h"
25 #include "platform/graphics/filters/FilterEffect.h"
26 #include "core/svg/SVGElementInstance.h"
27 #include "core/svg/SVGParserUtilities.h"
28 #include "core/svg/graphics/filters/SVGFilterBuilder.h"
29 #include "platform/geometry/FloatPoint.h"
30 #include "platform/geometry/IntPoint.h"
31 #include "platform/geometry/IntSize.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 inline SVGFEConvolveMatrixElement::SVGFEConvolveMatrixElement(Document& document)
47 : SVGFilterPrimitiveStandardAttributes(SVGNames::feConvolveMatrixTag, document)
48 , m_bias(SVGAnimatedNumber::create(this, SVGNames::biasAttr, SVGNumber::create()))
49 , m_divisor(SVGAnimatedNumber::create(this, SVGNames::divisorAttr, SVGNumber::create()))
50 , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
51 , m_edgeMode(SVGAnimatedEnumeration<EdgeModeType>::create(this, SVGNames::edgeModeAttr, EDGEMODE_DUPLICATE))
52 , m_kernelMatrix(SVGAnimatedNumberList::create(this, SVGNames::kernelMatrixAttr, SVGNumberList::create()))
53 , m_kernelUnitLength(SVGAnimatedNumberOptionalNumber::create(this, SVGNames::kernelUnitLengthAttr))
54 , m_order(SVGAnimatedIntegerOptionalInteger::create(this, SVGNames::orderAttr))
55 , m_preserveAlpha(SVGAnimatedBoolean::create(this, SVGNames::preserveAlphaAttr, SVGBoolean::create()))
56 , m_targetX(SVGAnimatedInteger::create(this, SVGNames::targetXAttr, SVGInteger::create()))
57 , m_targetY(SVGAnimatedInteger::create(this, SVGNames::targetYAttr, SVGInteger::create()))
59 ScriptWrappable::init(this);
61 addToPropertyMap(m_preserveAlpha);
62 addToPropertyMap(m_divisor);
63 addToPropertyMap(m_bias);
64 addToPropertyMap(m_kernelUnitLength);
65 addToPropertyMap(m_kernelMatrix);
66 addToPropertyMap(m_in1);
67 addToPropertyMap(m_edgeMode);
68 addToPropertyMap(m_order);
69 addToPropertyMap(m_targetX);
70 addToPropertyMap(m_targetY);
73 PassRefPtr<SVGFEConvolveMatrixElement> SVGFEConvolveMatrixElement::create(Document& document)
75 return adoptRef(new SVGFEConvolveMatrixElement(document));
78 bool SVGFEConvolveMatrixElement::isSupportedAttribute(const QualifiedName& attrName)
80 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
81 if (supportedAttributes.isEmpty()) {
82 supportedAttributes.add(SVGNames::inAttr);
83 supportedAttributes.add(SVGNames::orderAttr);
84 supportedAttributes.add(SVGNames::kernelMatrixAttr);
85 supportedAttributes.add(SVGNames::edgeModeAttr);
86 supportedAttributes.add(SVGNames::divisorAttr);
87 supportedAttributes.add(SVGNames::biasAttr);
88 supportedAttributes.add(SVGNames::targetXAttr);
89 supportedAttributes.add(SVGNames::targetYAttr);
90 supportedAttributes.add(SVGNames::kernelUnitLengthAttr);
91 supportedAttributes.add(SVGNames::preserveAlphaAttr);
93 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
96 void SVGFEConvolveMatrixElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
98 if (!isSupportedAttribute(name)) {
99 SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
103 SVGParsingError parseError = NoError;
105 if (name == SVGNames::inAttr)
106 m_in1->setBaseValueAsString(value, parseError);
107 else if (name == SVGNames::divisorAttr)
108 m_divisor->setBaseValueAsString(value, parseError);
109 else if (name == SVGNames::biasAttr)
110 m_bias->setBaseValueAsString(value, parseError);
111 else if (name == SVGNames::kernelUnitLengthAttr)
112 m_kernelUnitLength->setBaseValueAsString(value, parseError);
113 else if (name == SVGNames::kernelMatrixAttr)
114 m_kernelMatrix->setBaseValueAsString(value, parseError);
115 else if (name == SVGNames::preserveAlphaAttr)
116 m_preserveAlpha->setBaseValueAsString(value, parseError);
117 else if (name == SVGNames::edgeModeAttr)
118 m_edgeMode->setBaseValueAsString(value, parseError);
119 else if (name == SVGNames::targetXAttr)
120 m_targetX->setBaseValueAsString(value, parseError);
121 else if (name == SVGNames::targetYAttr)
122 m_targetY->setBaseValueAsString(value, parseError);
123 else if (name == SVGNames::orderAttr) {
124 m_order->setBaseValueAsString(value, parseError);
125 if (parseError == NoError && (orderX()->baseValue()->value() < 1 || orderY()->baseValue()->value() < 1)) {
126 document().accessSVGExtensions().reportWarning(
127 "feConvolveMatrix: problem parsing order=\"" + value
128 + "\". Filtered element will not be displayed.");
131 ASSERT_NOT_REACHED();
133 reportAttributeParsingError(parseError, name, value);
136 bool SVGFEConvolveMatrixElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
138 FEConvolveMatrix* convolveMatrix = static_cast<FEConvolveMatrix*>(effect);
139 if (attrName == SVGNames::edgeModeAttr)
140 return convolveMatrix->setEdgeMode(m_edgeMode->currentValue()->enumValue());
141 if (attrName == SVGNames::divisorAttr)
142 return convolveMatrix->setDivisor(m_divisor->currentValue()->value());
143 if (attrName == SVGNames::biasAttr)
144 return convolveMatrix->setBias(m_bias->currentValue()->value());
145 if (attrName == SVGNames::targetXAttr)
146 return convolveMatrix->setTargetOffset(IntPoint(m_targetX->currentValue()->value(), m_targetY->currentValue()->value()));
147 if (attrName == SVGNames::targetYAttr)
148 return convolveMatrix->setTargetOffset(IntPoint(m_targetX->currentValue()->value(), m_targetY->currentValue()->value()));
149 if (attrName == SVGNames::kernelUnitLengthAttr)
150 return convolveMatrix->setKernelUnitLength(FloatPoint(kernelUnitLengthX()->currentValue()->value(), kernelUnitLengthY()->currentValue()->value()));
151 if (attrName == SVGNames::preserveAlphaAttr)
152 return convolveMatrix->setPreserveAlpha(m_preserveAlpha->currentValue()->value());
154 ASSERT_NOT_REACHED();
158 void SVGFEConvolveMatrixElement::svgAttributeChanged(const QualifiedName& attrName)
160 if (!isSupportedAttribute(attrName)) {
161 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
165 SVGElementInstance::InvalidationGuard invalidationGuard(this);
167 if (attrName == SVGNames::edgeModeAttr
168 || attrName == SVGNames::divisorAttr
169 || attrName == SVGNames::biasAttr
170 || attrName == SVGNames::targetXAttr
171 || attrName == SVGNames::targetYAttr
172 || attrName == SVGNames::kernelUnitLengthAttr
173 || attrName == SVGNames::preserveAlphaAttr) {
174 primitiveAttributeChanged(attrName);
178 if (attrName == SVGNames::inAttr
179 || attrName == SVGNames::orderAttr
180 || attrName == SVGNames::kernelMatrixAttr) {
185 ASSERT_NOT_REACHED();
188 PassRefPtr<FilterEffect> SVGFEConvolveMatrixElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
190 FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
195 int orderXValue = orderX()->currentValue()->value();
196 int orderYValue = orderY()->currentValue()->value();
197 if (!hasAttribute(SVGNames::orderAttr)) {
201 // Spec says order must be > 0. Bail if it is not.
202 if (orderXValue < 1 || orderYValue < 1)
204 RefPtr<SVGNumberList> kernelMatrix = this->m_kernelMatrix->currentValue();
205 size_t kernelMatrixSize = kernelMatrix->length();
206 // The spec says this is a requirement, and should bail out if fails
207 if (orderXValue * orderYValue != static_cast<int>(kernelMatrixSize))
210 int targetXValue = m_targetX->currentValue()->value();
211 int targetYValue = m_targetY->currentValue()->value();
212 if (hasAttribute(SVGNames::targetXAttr) && (targetXValue < 0 || targetXValue >= orderXValue))
214 // The spec says the default value is: targetX = floor ( orderX / 2 ))
215 if (!hasAttribute(SVGNames::targetXAttr))
216 targetXValue = static_cast<int>(floorf(orderXValue / 2));
217 if (hasAttribute(SVGNames::targetYAttr) && (targetYValue < 0 || targetYValue >= orderYValue))
219 // The spec says the default value is: targetY = floor ( orderY / 2 ))
220 if (!hasAttribute(SVGNames::targetYAttr))
221 targetYValue = static_cast<int>(floorf(orderYValue / 2));
223 // Spec says default kernelUnitLength is 1.0, and a specified length cannot be 0.
224 // FIXME: Why is this cast from float -> int -> float?
225 int kernelUnitLengthXValue = kernelUnitLengthX()->currentValue()->value();
226 int kernelUnitLengthYValue = kernelUnitLengthY()->currentValue()->value();
227 if (!hasAttribute(SVGNames::kernelUnitLengthAttr)) {
228 kernelUnitLengthXValue = 1;
229 kernelUnitLengthYValue = 1;
231 if (kernelUnitLengthXValue <= 0 || kernelUnitLengthYValue <= 0)
234 float divisorValue = m_divisor->currentValue()->value();
235 if (hasAttribute(SVGNames::divisorAttr) && !divisorValue)
237 if (!hasAttribute(SVGNames::divisorAttr)) {
238 for (size_t i = 0; i < kernelMatrixSize; ++i)
239 divisorValue += kernelMatrix->at(i)->value();
244 RefPtr<FilterEffect> effect = FEConvolveMatrix::create(filter,
245 IntSize(orderXValue, orderYValue), divisorValue,
246 m_bias->currentValue()->value(), IntPoint(targetXValue, targetYValue), m_edgeMode->currentValue()->enumValue(),
247 FloatPoint(kernelUnitLengthXValue, kernelUnitLengthYValue), m_preserveAlpha->currentValue()->value(), m_kernelMatrix->currentValue()->toFloatVector());
248 effect->inputEffects().append(input1);
249 return effect.release();
252 } // namespace WebCore