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"
24 #include "core/SVGNames.h"
25 #include "platform/graphics/filters/FilterEffect.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"
34 template<> const SVGEnumerationStringEntries& getStaticStringEntries<EdgeModeType>()
36 DEFINE_STATIC_LOCAL(SVGEnumerationStringEntries, entries, ());
37 if (entries.isEmpty()) {
38 entries.append(std::make_pair(EDGEMODE_DUPLICATE, "duplicate"));
39 entries.append(std::make_pair(EDGEMODE_WRAP, "wrap"));
40 entries.append(std::make_pair(EDGEMODE_NONE, "none"));
45 inline SVGFEConvolveMatrixElement::SVGFEConvolveMatrixElement(Document& document)
46 : SVGFilterPrimitiveStandardAttributes(SVGNames::feConvolveMatrixTag, document)
47 , m_bias(SVGAnimatedNumber::create(this, SVGNames::biasAttr, SVGNumber::create()))
48 , m_divisor(SVGAnimatedNumber::create(this, SVGNames::divisorAttr, SVGNumber::create()))
49 , m_in1(SVGAnimatedString::create(this, SVGNames::inAttr, SVGString::create()))
50 , m_edgeMode(SVGAnimatedEnumeration<EdgeModeType>::create(this, SVGNames::edgeModeAttr, EDGEMODE_DUPLICATE))
51 , m_kernelMatrix(SVGAnimatedNumberList::create(this, SVGNames::kernelMatrixAttr, SVGNumberList::create()))
52 , m_kernelUnitLength(SVGAnimatedNumberOptionalNumber::create(this, SVGNames::kernelUnitLengthAttr))
53 , m_order(SVGAnimatedIntegerOptionalInteger::create(this, SVGNames::orderAttr))
54 , m_preserveAlpha(SVGAnimatedBoolean::create(this, SVGNames::preserveAlphaAttr, SVGBoolean::create()))
55 , m_targetX(SVGAnimatedInteger::create(this, SVGNames::targetXAttr, SVGInteger::create()))
56 , m_targetY(SVGAnimatedInteger::create(this, SVGNames::targetYAttr, SVGInteger::create()))
58 ScriptWrappable::init(this);
60 addToPropertyMap(m_preserveAlpha);
61 addToPropertyMap(m_divisor);
62 addToPropertyMap(m_bias);
63 addToPropertyMap(m_kernelUnitLength);
64 addToPropertyMap(m_kernelMatrix);
65 addToPropertyMap(m_in1);
66 addToPropertyMap(m_edgeMode);
67 addToPropertyMap(m_order);
68 addToPropertyMap(m_targetX);
69 addToPropertyMap(m_targetY);
72 DEFINE_NODE_FACTORY(SVGFEConvolveMatrixElement)
74 bool SVGFEConvolveMatrixElement::isSupportedAttribute(const QualifiedName& attrName)
76 DEFINE_STATIC_LOCAL(HashSet<QualifiedName>, supportedAttributes, ());
77 if (supportedAttributes.isEmpty()) {
78 supportedAttributes.add(SVGNames::inAttr);
79 supportedAttributes.add(SVGNames::orderAttr);
80 supportedAttributes.add(SVGNames::kernelMatrixAttr);
81 supportedAttributes.add(SVGNames::edgeModeAttr);
82 supportedAttributes.add(SVGNames::divisorAttr);
83 supportedAttributes.add(SVGNames::biasAttr);
84 supportedAttributes.add(SVGNames::targetXAttr);
85 supportedAttributes.add(SVGNames::targetYAttr);
86 supportedAttributes.add(SVGNames::kernelUnitLengthAttr);
87 supportedAttributes.add(SVGNames::preserveAlphaAttr);
89 return supportedAttributes.contains<SVGAttributeHashTranslator>(attrName);
92 void SVGFEConvolveMatrixElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
94 if (!isSupportedAttribute(name)) {
95 SVGFilterPrimitiveStandardAttributes::parseAttribute(name, value);
99 SVGParsingError parseError = NoError;
101 if (name == SVGNames::inAttr)
102 m_in1->setBaseValueAsString(value, parseError);
103 else if (name == SVGNames::divisorAttr)
104 m_divisor->setBaseValueAsString(value, parseError);
105 else if (name == SVGNames::biasAttr)
106 m_bias->setBaseValueAsString(value, parseError);
107 else if (name == SVGNames::kernelUnitLengthAttr)
108 m_kernelUnitLength->setBaseValueAsString(value, parseError);
109 else if (name == SVGNames::kernelMatrixAttr)
110 m_kernelMatrix->setBaseValueAsString(value, parseError);
111 else if (name == SVGNames::preserveAlphaAttr)
112 m_preserveAlpha->setBaseValueAsString(value, parseError);
113 else if (name == SVGNames::edgeModeAttr)
114 m_edgeMode->setBaseValueAsString(value, parseError);
115 else if (name == SVGNames::targetXAttr)
116 m_targetX->setBaseValueAsString(value, parseError);
117 else if (name == SVGNames::targetYAttr)
118 m_targetY->setBaseValueAsString(value, parseError);
119 else if (name == SVGNames::orderAttr) {
120 m_order->setBaseValueAsString(value, parseError);
121 if (parseError == NoError && (orderX()->baseValue()->value() < 1 || orderY()->baseValue()->value() < 1)) {
122 document().accessSVGExtensions().reportWarning(
123 "feConvolveMatrix: problem parsing order=\"" + value
124 + "\". Filtered element will not be displayed.");
127 ASSERT_NOT_REACHED();
129 reportAttributeParsingError(parseError, name, value);
132 bool SVGFEConvolveMatrixElement::setFilterEffectAttribute(FilterEffect* effect, const QualifiedName& attrName)
134 FEConvolveMatrix* convolveMatrix = static_cast<FEConvolveMatrix*>(effect);
135 if (attrName == SVGNames::edgeModeAttr)
136 return convolveMatrix->setEdgeMode(m_edgeMode->currentValue()->enumValue());
137 if (attrName == SVGNames::divisorAttr)
138 return convolveMatrix->setDivisor(m_divisor->currentValue()->value());
139 if (attrName == SVGNames::biasAttr)
140 return convolveMatrix->setBias(m_bias->currentValue()->value());
141 if (attrName == SVGNames::targetXAttr)
142 return convolveMatrix->setTargetOffset(IntPoint(m_targetX->currentValue()->value(), m_targetY->currentValue()->value()));
143 if (attrName == SVGNames::targetYAttr)
144 return convolveMatrix->setTargetOffset(IntPoint(m_targetX->currentValue()->value(), m_targetY->currentValue()->value()));
145 if (attrName == SVGNames::kernelUnitLengthAttr)
146 return convolveMatrix->setKernelUnitLength(FloatPoint(kernelUnitLengthX()->currentValue()->value(), kernelUnitLengthY()->currentValue()->value()));
147 if (attrName == SVGNames::preserveAlphaAttr)
148 return convolveMatrix->setPreserveAlpha(m_preserveAlpha->currentValue()->value());
150 ASSERT_NOT_REACHED();
154 void SVGFEConvolveMatrixElement::svgAttributeChanged(const QualifiedName& attrName)
156 if (!isSupportedAttribute(attrName)) {
157 SVGFilterPrimitiveStandardAttributes::svgAttributeChanged(attrName);
161 SVGElement::InvalidationGuard invalidationGuard(this);
163 if (attrName == SVGNames::edgeModeAttr
164 || attrName == SVGNames::divisorAttr
165 || attrName == SVGNames::biasAttr
166 || attrName == SVGNames::targetXAttr
167 || attrName == SVGNames::targetYAttr
168 || attrName == SVGNames::kernelUnitLengthAttr
169 || attrName == SVGNames::preserveAlphaAttr) {
170 primitiveAttributeChanged(attrName);
174 if (attrName == SVGNames::inAttr
175 || attrName == SVGNames::orderAttr
176 || attrName == SVGNames::kernelMatrixAttr) {
181 ASSERT_NOT_REACHED();
184 PassRefPtr<FilterEffect> SVGFEConvolveMatrixElement::build(SVGFilterBuilder* filterBuilder, Filter* filter)
186 FilterEffect* input1 = filterBuilder->getEffectById(AtomicString(m_in1->currentValue()->value()));
191 int orderXValue = orderX()->currentValue()->value();
192 int orderYValue = orderY()->currentValue()->value();
193 if (!hasAttribute(SVGNames::orderAttr)) {
197 // Spec says order must be > 0. Bail if it is not.
198 if (orderXValue < 1 || orderYValue < 1)
200 RefPtr<SVGNumberList> kernelMatrix = this->m_kernelMatrix->currentValue();
201 size_t kernelMatrixSize = kernelMatrix->length();
202 // The spec says this is a requirement, and should bail out if fails
203 if (orderXValue * orderYValue != static_cast<int>(kernelMatrixSize))
206 int targetXValue = m_targetX->currentValue()->value();
207 int targetYValue = m_targetY->currentValue()->value();
208 if (hasAttribute(SVGNames::targetXAttr) && (targetXValue < 0 || targetXValue >= orderXValue))
210 // The spec says the default value is: targetX = floor ( orderX / 2 ))
211 if (!hasAttribute(SVGNames::targetXAttr))
212 targetXValue = static_cast<int>(floorf(orderXValue / 2));
213 if (hasAttribute(SVGNames::targetYAttr) && (targetYValue < 0 || targetYValue >= orderYValue))
215 // The spec says the default value is: targetY = floor ( orderY / 2 ))
216 if (!hasAttribute(SVGNames::targetYAttr))
217 targetYValue = static_cast<int>(floorf(orderYValue / 2));
219 // Spec says default kernelUnitLength is 1.0, and a specified length cannot be 0.
220 // FIXME: Why is this cast from float -> int -> float?
221 int kernelUnitLengthXValue = kernelUnitLengthX()->currentValue()->value();
222 int kernelUnitLengthYValue = kernelUnitLengthY()->currentValue()->value();
223 if (!hasAttribute(SVGNames::kernelUnitLengthAttr)) {
224 kernelUnitLengthXValue = 1;
225 kernelUnitLengthYValue = 1;
227 if (kernelUnitLengthXValue <= 0 || kernelUnitLengthYValue <= 0)
230 float divisorValue = m_divisor->currentValue()->value();
231 if (hasAttribute(SVGNames::divisorAttr) && !divisorValue)
233 if (!hasAttribute(SVGNames::divisorAttr)) {
234 for (size_t i = 0; i < kernelMatrixSize; ++i)
235 divisorValue += kernelMatrix->at(i)->value();
240 RefPtr<FilterEffect> effect = FEConvolveMatrix::create(filter,
241 IntSize(orderXValue, orderYValue), divisorValue,
242 m_bias->currentValue()->value(), IntPoint(targetXValue, targetYValue), m_edgeMode->currentValue()->enumValue(),
243 FloatPoint(kernelUnitLengthXValue, kernelUnitLengthYValue), m_preserveAlpha->currentValue()->value(), m_kernelMatrix->currentValue()->toFloatVector());
244 effect->inputEffects().append(input1);
245 return effect.release();
248 } // namespace WebCore